Skip to content

Commit

Permalink
lib: add ipow() function to calculate n power of m with integers
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Mazein <[email protected]>
  • Loading branch information
MegaMaddin committed Oct 23, 2020
1 parent 8f12e48 commit af43300
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions include/lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,20 @@ static inline unsigned int next_power_of_two(unsigned int n) {
return n;
}

static inline unsigned long ipow(int base, unsigned int exp) {
unsigned long result = 1;
for (;;) {
if (exp & 1)
result *= base;
exp >>= 1;
if (!exp)
break;
base *= base;
}

return result;
}

/* External declarations */

extern void halt(void);
Expand Down

0 comments on commit af43300

Please sign in to comment.