Skip to content

Slightly optimize Math.mod #1521

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

Merged
merged 5 commits into from
Oct 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 13 additions & 17 deletions std/assembly/math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1583,19 +1583,18 @@ export namespace NativeMath {
}
var ux1 = ux << 1;
if (ux1 <= uy1) {
if (ux1 == uy1) return 0 * x;
return x;
return x * f64(ux1 != uy1);
}
if (!ex) {
ex -= builtin_clz<i64>(ux << 12);
ux <<= -ex + 1;
ux <<= 1 - ex;
} else {
ux &= <u64>-1 >> 12;
ux |= 1 << 52;
}
if (!ey) {
ey -= builtin_clz<i64>(uy << 12);
uy <<= -ey + 1;
uy <<= 1 - ey;
} else {
uy &= <u64>-1 >> 12;
uy |= 1 << 52;
Expand All @@ -1622,8 +1621,7 @@ export namespace NativeMath {
} else {
ux >>= -ex + 1;
}
ux |= sx << 63;
return reinterpret<f64>(ux);
return reinterpret<f64>(ux | (sx << 63));
}

export function rem(x: f64, y: f64): f64 { // see: musl/src/math/remquo.c
Expand All @@ -1640,14 +1638,14 @@ export namespace NativeMath {
var uxi = ux;
if (!ex) {
ex -= builtin_clz<i64>(uxi << 12);
uxi <<= -ex + 1;
uxi <<= 1 - ex;
} else {
uxi &= <u64>-1 >> 12;
uxi |= 1 << 52;
}
if (!ey) {
ey -= builtin_clz<i64>(uy << 12);
uy <<= -ey + 1;
uy <<= 1 - ey;
} else {
uy &= <u64>-1 >> 12;
uy |= 1 << 52;
Expand Down Expand Up @@ -2884,27 +2882,26 @@ export namespace NativeMathf {
var uy = reinterpret<u32>(y);
var ex = <i32>(ux >> 23 & 0xFF);
var ey = <i32>(uy >> 23 & 0xFF);
var sx = ux & 0x80000000;
var sm = ux & 0x80000000;
var uy1 = uy << 1;
if (uy1 == 0 || ex == 0xFF || isNaN<f32>(y)) {
let m = x * y;
return m / m;
}
var ux1 = ux << 1;
if (ux1 <= uy1) {
if (ux1 == uy1) return 0 * x;
return x;
return x * f32(ux1 != uy1);
}
if (!ex) {
ex -= builtin_clz<u32>(ux << 9);
ux <<= -ex + 1;
ux <<= 1 - ex;
} else {
ux &= <u32>-1 >> 9;
ux |= 1 << 23;
}
if (!ey) {
ey -= builtin_clz<u32>(uy << 9);
uy <<= -ey + 1;
uy <<= 1 - ey;
} else {
uy &= <u32>-1 >> 9;
uy |= 1 << 23;
Expand All @@ -2931,8 +2928,7 @@ export namespace NativeMathf {
} else {
ux >>= -ex + 1;
}
ux |= sx;
return reinterpret<f32>(ux);
return reinterpret<f32>(ux | sm);
}

export function rem(x: f32, y: f32): f32 { // see: musl/src/math/remquof.c
Expand All @@ -2946,14 +2942,14 @@ export namespace NativeMathf {
if (ux << 1 == 0) return x;
if (!ex) {
ex -= builtin_clz<u32>(uxi << 9);
uxi <<= -ex + 1;
uxi <<= 1 - ex;
} else {
uxi &= <u32>-1 >> 9;
uxi |= 1 << 23;
}
if (!ey) {
ey -= builtin_clz<u32>(uy << 9);
uy <<= -ey + 1;
uy <<= 1 - ey;
} else {
uy &= <u32>-1 >> 9;
uy |= 1 << 23;
Expand Down
44 changes: 12 additions & 32 deletions tests/compiler/binary.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -1288,16 +1288,12 @@
local.get $7
i32.le_u
if
local.get $0
local.get $9
local.get $7
i32.eq
if
f32.const 0
local.get $0
f32.mul
return
end
local.get $0
i32.ne
f32.convert_i32_u
f32.mul
return
end
local.get $4
Expand All @@ -1311,11 +1307,9 @@
i32.sub
local.set $4
local.get $2
i32.const 0
i32.const 1
local.get $4
i32.sub
i32.const 1
i32.add
i32.shl
local.set $2
else
Expand Down Expand Up @@ -1343,11 +1337,9 @@
i32.sub
local.set $5
local.get $3
i32.const 0
i32.const 1
local.get $5
i32.sub
i32.const 1
i32.add
i32.shl
local.set $3
else
Expand Down Expand Up @@ -1460,8 +1452,6 @@
local.get $2
local.get $6
i32.or
local.set $2
local.get $2
f32.reinterpret_i32
)
(func $~lib/math/NativeMathf.pow (param $0 f32) (param $1 f32) (result f32)
Expand Down Expand Up @@ -2163,16 +2153,12 @@
local.get $7
i64.le_u
if
local.get $0
local.get $9
local.get $7
i64.eq
if
f64.const 0
local.get $0
f64.mul
return
end
local.get $0
i64.ne
f64.convert_i32_u
f64.mul
return
end
local.get $4
Expand All @@ -2188,11 +2174,9 @@
i64.sub
local.set $4
local.get $2
i64.const 0
i64.const 1
local.get $4
i64.sub
i64.const 1
i64.add
i64.shl
local.set $2
else
Expand Down Expand Up @@ -2222,11 +2206,9 @@
i64.sub
local.set $5
local.get $3
i64.const 0
i64.const 1
local.get $5
i64.sub
i64.const 1
i64.add
i64.shl
local.set $3
else
Expand Down Expand Up @@ -2341,8 +2323,6 @@
i64.const 63
i64.shl
i64.or
local.set $2
local.get $2
f64.reinterpret_i64
)
(func $start:binary
Expand Down
Loading