util.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /*
  2. * Utility functions
  3. *
  4. * Copyright (c) 2015 Michael Buesch <m@bues.ch>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. */
  20. #include "util.h"
  21. int32_t pow_int(int16_t b, uint8_t e)
  22. {
  23. int32_t x, y;
  24. if (!e)
  25. return 1;
  26. x = b;
  27. while (!(e & 1)) {
  28. e = (uint8_t)(e >> 1);
  29. x *= x;
  30. }
  31. y = x;
  32. e = (uint8_t)(e >> 1);
  33. while (e) {
  34. x *= x;
  35. if (e & 1)
  36. y *= x;
  37. e = (uint8_t)(e >> 1);
  38. }
  39. return y;
  40. }