Skip to content

Commit 38362a8

Browse files
MaxGraeydcodeIO
authored andcommitted
Add fast paths for Math(f).pow (#1053)
1 parent 76f4450 commit 38362a8

13 files changed

+4197
-2163
lines changed

std/assembly/math.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,6 +1180,18 @@ export namespace NativeMath {
11801180
}
11811181

11821182
export function pow(x: f64, y: f64): f64 { // see: musl/src/math/pow.c and SUN COPYRIGHT NOTICE above
1183+
// TODO: remove this fast pathes after introduced own mid-end IR with "stdlib call simplify" transforms
1184+
if (builtin_abs<f64>(y) <= 2) {
1185+
if (y == 2.0) return x * x;
1186+
if (y == 0.5) return select<f64>(Infinity, builtin_sqrt<f64>(x), builtin_abs<f64>(x) == Infinity);
1187+
if (y == -1.0) return 1 / x;
1188+
if (y == -0.5) {
1189+
if (x == 0.0) return Infinity;
1190+
return select<f64>(0, 1 / builtin_sqrt<f64>(x), builtin_abs<f64>(x) == Infinity);
1191+
}
1192+
if (y == 1.0) return x;
1193+
if (y == 0.0) return 1.0;
1194+
}
11831195
if (ASC_SHRINK_LEVEL < 1) {
11841196
return pow_lut(x, y);
11851197
} else {
@@ -2564,6 +2576,18 @@ export namespace NativeMathf {
25642576
}
25652577

25662578
export function pow(x: f32, y: f32): f32 { // see: musl/src/math/powf.c and SUN COPYRIGHT NOTICE above
2579+
// TODO: remove this fast pathes after introduced own mid-end IR with "stdlib call simplify" transforms
2580+
if (builtin_abs<f32>(y) <= 2) {
2581+
if (y == 2.0) return x * x;
2582+
if (y == 0.5) return select<f32>(Infinity, builtin_sqrt<f32>(x), builtin_abs<f32>(x) == Infinity);
2583+
if (y == -1.0) return 1 / x;
2584+
if (y == -0.5) {
2585+
if (x == 0.0) return Infinity;
2586+
return select<f32>(0, 1 / builtin_sqrt<f32>(x), builtin_abs<f32>(x) == Infinity);
2587+
}
2588+
if (y == 1.0) return x;
2589+
if (y == 0.0) return 1.0;
2590+
}
25672591
return powf_lut(x, y);
25682592
}
25692593

0 commit comments

Comments
 (0)