Skip to content

Commit 4d9b05b

Browse files
committed
fix: avoid powi opt in js engine only when exponent <= 2
1 parent f16b08f commit 4d9b05b

File tree

2 files changed

+28
-39
lines changed

2 files changed

+28
-39
lines changed

src/util/math.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,11 @@ export function isPowerOf2(x: i32): bool {
1111
export function accuratePow64(x: f64, y: f64): f64 {
1212
if (!ASC_TARGET) { // ASC_TARGET == JS
1313
// Engines like V8, WebKit and SpiderMonkey uses powi fast path if exponent is integer
14-
// This speculative optimization leads to loose precisions like 10 ** 208 != 1e208
15-
// or/and 10 ** -5 != 1e-5 anymore. For avoid this behaviour we are forcing exponent
14+
// This speculative optimization leads to loose precisions like 10 ** -5 != 1e-5 anymore.
15+
// For avoid this behaviour we are forcing exponent
1616
// to fractional form and compensate this afterwards.
17-
if (isFinite(y) && Math.abs(y) >= 2 && Math.trunc(y) == y) {
18-
if (y < 0) {
19-
return Math.pow(x, y + 0.5) / Math.pow(x, 0.5);
20-
} else {
21-
return Math.pow(x, y - 0.5) * Math.pow(x, 0.5);
22-
}
17+
if (isFinite(y) && y <= -2 && Math.trunc(y) == y) {
18+
return Math.pow(x, y + 0.5) / Math.pow(x, 0.5);
2319
}
2420
}
2521
return Math.pow(x, y);

tests/compiler/std/math.release.wat

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -49901,7 +49901,7 @@
4990149901
call $~lib/builtins/abort
4990249902
unreachable
4990349903
end
49904-
f64.const -nan:0x8000000000000
49904+
f64.const nan:0x8000000000000
4990549905
f64.const nan:0x8000000000000
4990649906
f64.const 0
4990749907
call $std/math/check<f64>
@@ -49945,7 +49945,7 @@
4994549945
call $~lib/builtins/abort
4994649946
unreachable
4994749947
end
49948-
f64.const -nan:0x8000000000000
49948+
f64.const nan:0x8000000000000
4994949949
f64.const nan:0x8000000000000
4995049950
f64.const 0
4995149951
call $std/math/check<f64>
@@ -49967,7 +49967,7 @@
4996749967
call $~lib/builtins/abort
4996849968
unreachable
4996949969
end
49970-
f64.const -nan:0x8000000000000
49970+
f64.const nan:0x8000000000000
4997149971
f64.const nan:0x8000000000000
4997249972
f64.const 0
4997349973
call $std/math/check<f64>
@@ -50033,7 +50033,7 @@
5003350033
call $~lib/builtins/abort
5003450034
unreachable
5003550035
end
50036-
f64.const -nan:0x8000000000000
50036+
f64.const nan:0x8000000000000
5003750037
f64.const nan:0x8000000000000
5003850038
f64.const 0
5003950039
call $std/math/check<f64>
@@ -50099,7 +50099,7 @@
5009950099
call $~lib/builtins/abort
5010050100
unreachable
5010150101
end
50102-
f64.const -nan:0x8000000000000
50102+
f64.const nan:0x8000000000000
5010350103
f64.const nan:0x8000000000000
5010450104
f64.const 0
5010550105
call $std/math/check<f64>
@@ -50165,7 +50165,7 @@
5016550165
call $~lib/builtins/abort
5016650166
unreachable
5016750167
end
50168-
f64.const -nan:0x8000000000000
50168+
f64.const nan:0x8000000000000
5016950169
f64.const nan:0x8000000000000
5017050170
f64.const 0
5017150171
call $std/math/check<f64>
@@ -50253,7 +50253,7 @@
5025350253
call $~lib/builtins/abort
5025450254
unreachable
5025550255
end
50256-
f64.const -nan:0x8000000000000
50256+
f64.const nan:0x8000000000000
5025750257
f64.const nan:0x8000000000000
5025850258
f64.const 0
5025950259
call $std/math/check<f64>
@@ -50363,7 +50363,7 @@
5036350363
call $~lib/builtins/abort
5036450364
unreachable
5036550365
end
50366-
f64.const -nan:0x8000000000000
50366+
f64.const nan:0x8000000000000
5036750367
f64.const nan:0x8000000000000
5036850368
f64.const 0
5036950369
call $std/math/check<f64>
@@ -50517,7 +50517,7 @@
5051750517
call $~lib/builtins/abort
5051850518
unreachable
5051950519
end
50520-
f64.const -nan:0x8000000000000
50520+
f64.const nan:0x8000000000000
5052150521
f64.const nan:0x8000000000000
5052250522
f64.const 0
5052350523
call $std/math/check<f64>
@@ -51749,7 +51749,7 @@
5174951749
call $~lib/builtins/abort
5175051750
unreachable
5175151751
end
51752-
f32.const -nan:0x400000
51752+
f32.const nan:0x400000
5175351753
f32.const nan:0x400000
5175451754
f32.const 0
5175551755
call $std/math/check<f32>
@@ -51775,7 +51775,7 @@
5177551775
call $~lib/builtins/abort
5177651776
unreachable
5177751777
end
51778-
f32.const -nan:0x400000
51778+
f32.const nan:0x400000
5177951779
f32.const nan:0x400000
5178051780
f32.const 0
5178151781
call $std/math/check<f32>
@@ -51788,7 +51788,7 @@
5178851788
call $~lib/builtins/abort
5178951789
unreachable
5179051790
end
51791-
f32.const -nan:0x400000
51791+
f32.const nan:0x400000
5179251792
f32.const nan:0x400000
5179351793
f32.const 0
5179451794
call $std/math/check<f32>
@@ -51827,7 +51827,7 @@
5182751827
call $~lib/builtins/abort
5182851828
unreachable
5182951829
end
51830-
f32.const -nan:0x400000
51830+
f32.const nan:0x400000
5183151831
f32.const nan:0x400000
5183251832
f32.const 0
5183351833
call $std/math/check<f32>
@@ -51866,7 +51866,7 @@
5186651866
call $~lib/builtins/abort
5186751867
unreachable
5186851868
end
51869-
f32.const -nan:0x400000
51869+
f32.const nan:0x400000
5187051870
f32.const nan:0x400000
5187151871
f32.const 0
5187251872
call $std/math/check<f32>
@@ -51905,7 +51905,7 @@
5190551905
call $~lib/builtins/abort
5190651906
unreachable
5190751907
end
51908-
f32.const -nan:0x400000
51908+
f32.const nan:0x400000
5190951909
f32.const nan:0x400000
5191051910
f32.const 0
5191151911
call $std/math/check<f32>
@@ -51957,7 +51957,7 @@
5195751957
call $~lib/builtins/abort
5195851958
unreachable
5195951959
end
51960-
f32.const -nan:0x400000
51960+
f32.const nan:0x400000
5196151961
f32.const nan:0x400000
5196251962
f32.const 0
5196351963
call $std/math/check<f32>
@@ -52022,7 +52022,7 @@
5202252022
call $~lib/builtins/abort
5202352023
unreachable
5202452024
end
52025-
f32.const -nan:0x400000
52025+
f32.const nan:0x400000
5202652026
f32.const nan:0x400000
5202752027
f32.const 0
5202852028
call $std/math/check<f32>
@@ -52048,7 +52048,7 @@
5204852048
call $~lib/builtins/abort
5204952049
unreachable
5205052050
end
52051-
f32.const -nan:0x400000
52051+
f32.const nan:0x400000
5205252052
f32.const nan:0x400000
5205352053
f32.const 0
5205452054
call $std/math/check<f32>
@@ -56190,19 +56190,12 @@
5619056190
call $~lib/builtins/abort
5619156191
unreachable
5619256192
end
56193-
f64.const 0
56194-
f64.const -1
56195-
call $~lib/math/NativeMath.pow
56196-
f64.const inf
56197-
f64.ne
56198-
if
56199-
i32.const 0
56200-
i32.const 1056
56201-
i32.const 4131
56202-
i32.const 1
56203-
call $~lib/builtins/abort
56204-
unreachable
56205-
end
56193+
i32.const 0
56194+
i32.const 1056
56195+
i32.const 4132
56196+
i32.const 1
56197+
call $~lib/builtins/abort
56198+
unreachable
5620656199
)
5620756200
(func $~start
5620856201
call $start:std/math

0 commit comments

Comments
 (0)