Skip to content

Commit 0fa51ab

Browse files
authored
Slightly optimize Math.mod (#1521)
1 parent 76dc85d commit 0fa51ab

File tree

8 files changed

+748
-810
lines changed

8 files changed

+748
-810
lines changed

std/assembly/math.ts

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1583,19 +1583,18 @@ export namespace NativeMath {
15831583
}
15841584
var ux1 = ux << 1;
15851585
if (ux1 <= uy1) {
1586-
if (ux1 == uy1) return 0 * x;
1587-
return x;
1586+
return x * f64(ux1 != uy1);
15881587
}
15891588
if (!ex) {
15901589
ex -= builtin_clz<i64>(ux << 12);
1591-
ux <<= -ex + 1;
1590+
ux <<= 1 - ex;
15921591
} else {
15931592
ux &= <u64>-1 >> 12;
15941593
ux |= 1 << 52;
15951594
}
15961595
if (!ey) {
15971596
ey -= builtin_clz<i64>(uy << 12);
1598-
uy <<= -ey + 1;
1597+
uy <<= 1 - ey;
15991598
} else {
16001599
uy &= <u64>-1 >> 12;
16011600
uy |= 1 << 52;
@@ -1622,8 +1621,7 @@ export namespace NativeMath {
16221621
} else {
16231622
ux >>= -ex + 1;
16241623
}
1625-
ux |= sx << 63;
1626-
return reinterpret<f64>(ux);
1624+
return reinterpret<f64>(ux | (sx << 63));
16271625
}
16281626

16291627
export function rem(x: f64, y: f64): f64 { // see: musl/src/math/remquo.c
@@ -1640,14 +1638,14 @@ export namespace NativeMath {
16401638
var uxi = ux;
16411639
if (!ex) {
16421640
ex -= builtin_clz<i64>(uxi << 12);
1643-
uxi <<= -ex + 1;
1641+
uxi <<= 1 - ex;
16441642
} else {
16451643
uxi &= <u64>-1 >> 12;
16461644
uxi |= 1 << 52;
16471645
}
16481646
if (!ey) {
16491647
ey -= builtin_clz<i64>(uy << 12);
1650-
uy <<= -ey + 1;
1648+
uy <<= 1 - ey;
16511649
} else {
16521650
uy &= <u64>-1 >> 12;
16531651
uy |= 1 << 52;
@@ -2884,27 +2882,26 @@ export namespace NativeMathf {
28842882
var uy = reinterpret<u32>(y);
28852883
var ex = <i32>(ux >> 23 & 0xFF);
28862884
var ey = <i32>(uy >> 23 & 0xFF);
2887-
var sx = ux & 0x80000000;
2885+
var sm = ux & 0x80000000;
28882886
var uy1 = uy << 1;
28892887
if (uy1 == 0 || ex == 0xFF || isNaN<f32>(y)) {
28902888
let m = x * y;
28912889
return m / m;
28922890
}
28932891
var ux1 = ux << 1;
28942892
if (ux1 <= uy1) {
2895-
if (ux1 == uy1) return 0 * x;
2896-
return x;
2893+
return x * f32(ux1 != uy1);
28972894
}
28982895
if (!ex) {
28992896
ex -= builtin_clz<u32>(ux << 9);
2900-
ux <<= -ex + 1;
2897+
ux <<= 1 - ex;
29012898
} else {
29022899
ux &= <u32>-1 >> 9;
29032900
ux |= 1 << 23;
29042901
}
29052902
if (!ey) {
29062903
ey -= builtin_clz<u32>(uy << 9);
2907-
uy <<= -ey + 1;
2904+
uy <<= 1 - ey;
29082905
} else {
29092906
uy &= <u32>-1 >> 9;
29102907
uy |= 1 << 23;
@@ -2931,8 +2928,7 @@ export namespace NativeMathf {
29312928
} else {
29322929
ux >>= -ex + 1;
29332930
}
2934-
ux |= sx;
2935-
return reinterpret<f32>(ux);
2931+
return reinterpret<f32>(ux | sm);
29362932
}
29372933

29382934
export function rem(x: f32, y: f32): f32 { // see: musl/src/math/remquof.c
@@ -2946,14 +2942,14 @@ export namespace NativeMathf {
29462942
if (ux << 1 == 0) return x;
29472943
if (!ex) {
29482944
ex -= builtin_clz<u32>(uxi << 9);
2949-
uxi <<= -ex + 1;
2945+
uxi <<= 1 - ex;
29502946
} else {
29512947
uxi &= <u32>-1 >> 9;
29522948
uxi |= 1 << 23;
29532949
}
29542950
if (!ey) {
29552951
ey -= builtin_clz<u32>(uy << 9);
2956-
uy <<= -ey + 1;
2952+
uy <<= 1 - ey;
29572953
} else {
29582954
uy &= <u32>-1 >> 9;
29592955
uy |= 1 << 23;

tests/compiler/binary.untouched.wat

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,16 +1288,12 @@
12881288
local.get $7
12891289
i32.le_u
12901290
if
1291+
local.get $0
12911292
local.get $9
12921293
local.get $7
1293-
i32.eq
1294-
if
1295-
f32.const 0
1296-
local.get $0
1297-
f32.mul
1298-
return
1299-
end
1300-
local.get $0
1294+
i32.ne
1295+
f32.convert_i32_u
1296+
f32.mul
13011297
return
13021298
end
13031299
local.get $4
@@ -1311,11 +1307,9 @@
13111307
i32.sub
13121308
local.set $4
13131309
local.get $2
1314-
i32.const 0
1310+
i32.const 1
13151311
local.get $4
13161312
i32.sub
1317-
i32.const 1
1318-
i32.add
13191313
i32.shl
13201314
local.set $2
13211315
else
@@ -1343,11 +1337,9 @@
13431337
i32.sub
13441338
local.set $5
13451339
local.get $3
1346-
i32.const 0
1340+
i32.const 1
13471341
local.get $5
13481342
i32.sub
1349-
i32.const 1
1350-
i32.add
13511343
i32.shl
13521344
local.set $3
13531345
else
@@ -1460,8 +1452,6 @@
14601452
local.get $2
14611453
local.get $6
14621454
i32.or
1463-
local.set $2
1464-
local.get $2
14651455
f32.reinterpret_i32
14661456
)
14671457
(func $~lib/math/NativeMathf.pow (param $0 f32) (param $1 f32) (result f32)
@@ -2163,16 +2153,12 @@
21632153
local.get $7
21642154
i64.le_u
21652155
if
2156+
local.get $0
21662157
local.get $9
21672158
local.get $7
2168-
i64.eq
2169-
if
2170-
f64.const 0
2171-
local.get $0
2172-
f64.mul
2173-
return
2174-
end
2175-
local.get $0
2159+
i64.ne
2160+
f64.convert_i32_u
2161+
f64.mul
21762162
return
21772163
end
21782164
local.get $4
@@ -2188,11 +2174,9 @@
21882174
i64.sub
21892175
local.set $4
21902176
local.get $2
2191-
i64.const 0
2177+
i64.const 1
21922178
local.get $4
21932179
i64.sub
2194-
i64.const 1
2195-
i64.add
21962180
i64.shl
21972181
local.set $2
21982182
else
@@ -2222,11 +2206,9 @@
22222206
i64.sub
22232207
local.set $5
22242208
local.get $3
2225-
i64.const 0
2209+
i64.const 1
22262210
local.get $5
22272211
i64.sub
2228-
i64.const 1
2229-
i64.add
22302212
i64.shl
22312213
local.set $3
22322214
else
@@ -2341,8 +2323,6 @@
23412323
i64.const 63
23422324
i64.shl
23432325
i64.or
2344-
local.set $2
2345-
local.get $2
23462326
f64.reinterpret_i64
23472327
)
23482328
(func $start:binary

0 commit comments

Comments
 (0)