diff options
Diffstat (limited to 'boot/runtime/core.c')
| -rw-r--r-- | boot/runtime/core.c | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/boot/runtime/core.c b/boot/runtime/core.c index e377e0a..cc9210e 100644 --- a/boot/runtime/core.c +++ b/boot/runtime/core.c @@ -87,6 +87,26 @@ println(const ascii* s) // `printf` is already linked in via <stdio.h>. +// integer exponentiation by squaring. +// see: https://en.wikipedia.org/wiki/Exponentiation_by_squaring +// negative exponent rounds to 0 for any base except 1 / -1. +integer +pow_integer(integer base, integer exponent) +{ + if (exponent < 0) { + if (base == 1) return 1; + if (base == -1) return (exponent & 1) ? -1 : 1; + return 0; + } + integer result = 1; + while (exponent > 0) { + if (exponent & 1) result *= base; + base *= base; + exponent >>= 1; + } + return result; +} + void* allocate(size_t size) { |
