diff options
| author | Mel <mel@rnrd.eu> | 2026-05-24 20:30:11 +0200 |
|---|---|---|
| committer | Mel <mel@rnrd.eu> | 2026-05-24 20:30:11 +0200 |
| commit | 7af54064b23efe1f18fab9e7ee20b0fc811c36b5 (patch) | |
| tree | d8482ce29cdee1892c843f57be881e6fecb98d2d /boot/runtime/core.c | |
| parent | 2a893246711442dd8407264c05991b44d1d1f66f (diff) | |
| download | catskill-7af54064b23efe1f18fab9e7ee20b0fc811c36b5.tar.zst catskill-7af54064b23efe1f18fab9e7ee20b0fc811c36b5.zip | |
Power operator via runtime library implementation
Signed-off-by: Mel <mel@rnrd.eu>
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) { |
