Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add integer power functions #212

Merged
merged 12 commits into from
Sep 9, 2018
108 changes: 108 additions & 0 deletions std/assembly/math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2303,3 +2303,111 @@ export namespace NativeMathf {
return sx ? -x : x;
}
}

export function ipow32(x: i32, e: i32): i32 {
var out = 1;
if (ASC_SHRINK_LEVEL < 1) {
if (e < 0) return 0;

switch (e) {
case 0: return 1;
case 1: return x;
case 2: return x * x;
}

let log = 32 - clz(e);
if (log <= 5) {
// 32 = 2 ^ 5, so need only five cases.
// But some extra cases needs for properly overflowing
switch (log) {
case 5: {
if (e & 1) out *= x;
e >>= 1;
x *= x;
}
case 4: {
if (e & 1) out *= x;
e >>= 1;
x *= x;
}
case 3: {
if (e & 1) out *= x;
e >>= 1;
x *= x;
}
case 2: {
if (e & 1) out *= x;
e >>= 1;
x *= x;
}
case 1: {
if (e & 1) out *= x;
}
}
return out;
}
}

while (e > 0) {
if (e & 1) out *= x;
e >>= 1;
x *= x;
}
return out;
}

export function ipow64(x: i64, e: i32): i64 {
var out: i64 = 1;
if (ASC_SHRINK_LEVEL < 1) {
if (e < 0) return 0;
switch (e) {
case 0: return 1;
case 1: return x;
case 2: return x * x;
}

let log = 32 - clz(e);
if (log <= 6) {
// 64 = 2 ^ 6, so need only six cases.
// But some extra cases needs for properly overflowing
switch (log) {
case 6: {
if (e & 1) out *= x;
e >>= 1;
x *= x;
}
case 5: {
if (e & 1) out *= x;
e >>= 1;
x *= x;
}
case 4: {
if (e & 1) out *= x;
e >>= 1;
x *= x;
}
case 3: {
if (e & 1) out *= x;
e >>= 1;
x *= x;
}
case 2: {
if (e & 1) out *= x;
e >>= 1;
x *= x;
}
case 1: {
if (e & 1) out *= x;
}
}
return out;
}
}

while (e > 0) {
if (e & 1) out *= x;
e >>= 1;
x *= x;
}
return out;
}
Loading