Skip to content

Simplify Math.signbit. Don't ignore sign bit for NaNs #1467

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 2 commits into from
Sep 25, 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
7 changes: 2 additions & 5 deletions std/assembly/math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1435,9 +1435,7 @@ export namespace NativeMath {
// @ts-ignore: decorator
@inline
export function signbit(x: f64): bool {
// In ECMAScript all NaN values are indistinguishable from each other
// so we need handle NaN and negative NaN in similar way
return <bool>(<i32>(reinterpret<u64>(x) >>> 63) & i32(x == x));
return <bool>(reinterpret<u64>(x) >>> 63);
}

export function sin(x: f64): f64 { // see: musl/src/math/sin.c
Expand Down Expand Up @@ -2714,8 +2712,7 @@ export namespace NativeMathf {
// @ts-ignore: decorator
@inline
export function signbit(x: f32): bool {
// @ts-ignore: type
return <bool>((reinterpret<u32>(x) >>> 31) & (x == x));
return <bool>(reinterpret<u32>(x) >>> 31);
}

export function sin(x: f32): f32 { // see: musl/src/math/sinf.c
Expand Down
2 changes: 1 addition & 1 deletion std/portable/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ Object.defineProperties(globalScope["JSMath"], {
sincos_cos: { value: 0.0, writable: true },
signbit: {
value: function signbit(x) {
F64[0] = x; return Boolean((U64[1] >>> 31) & (x == x));
F64[0] = x; return Boolean(U64[1] >>> 31);
}
},
sincos: {
Expand Down
22 changes: 4 additions & 18 deletions tests/compiler/std/math.optimized.wat
Original file line number Diff line number Diff line change
Expand Up @@ -187,22 +187,16 @@
i64.reinterpret_f64
i64.const 63
i64.shr_u
i32.wrap_i64
local.get $0
local.get $0
f64.eq
i32.and
i64.const 0
i64.ne
i32.const 0
i32.ne
local.get $1
i64.reinterpret_f64
i64.const 63
i64.shr_u
i32.wrap_i64
local.get $1
local.get $1
f64.eq
i32.and
i64.const 0
i64.ne
i32.const 0
i32.ne
i32.eq
Expand Down Expand Up @@ -382,20 +376,12 @@
i32.reinterpret_f32
i32.const 31
i32.shr_u
local.get $0
local.get $0
f32.eq
i32.and
i32.const 0
i32.ne
local.get $1
i32.reinterpret_f32
i32.const 31
i32.shr_u
local.get $1
local.get $1
f32.eq
i32.and
i32.const 0
i32.ne
i32.eq
Expand Down
6 changes: 3 additions & 3 deletions tests/compiler/std/math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3129,7 +3129,7 @@ assert(NativeMath.signbit(-0.0) == true);
assert(NativeMath.signbit(1.0) == false);
assert(NativeMath.signbit(-1.0) == true);
assert(NativeMath.signbit(+NaN) == false);
assert(NativeMath.signbit(-NaN) == false);
assert(NativeMath.signbit(-NaN) == true);
assert(NativeMath.signbit(+Infinity) == false);
assert(NativeMath.signbit(-Infinity) == true);

Expand All @@ -3142,7 +3142,7 @@ assert(NativeMathf.signbit(-0.0) == true);
assert(NativeMathf.signbit(1.0) == false);
assert(NativeMathf.signbit(-1.0) == true);
assert(NativeMathf.signbit(+NaN) == false);
assert(NativeMathf.signbit(-NaN) == false);
assert(NativeMathf.signbit(-NaN) == true);
assert(NativeMathf.signbit(+Infinity) == false);
assert(NativeMathf.signbit(-Infinity) == true);

Expand Down Expand Up @@ -4121,4 +4121,4 @@ assert(1 ** 0.5 == 1.0);
assert(0 ** 0.5 == 0.0);
assert(0 ** -1.0 == Infinity);
assert(0.0 ** 0 == 1.0);
assert(1.0 ** 1 == 1.0);
assert(1.0 ** 1 == 1.0);
114 changes: 22 additions & 92 deletions tests/compiler/std/math.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -269,11 +269,8 @@
i64.reinterpret_f64
i64.const 63
i64.shr_u
i32.wrap_i64
local.get $3
local.get $3
f64.eq
i32.and
i64.const 0
i64.ne
i32.const 0
i32.ne
local.get $1
Expand All @@ -282,11 +279,8 @@
i64.reinterpret_f64
i64.const 63
i64.shr_u
i32.wrap_i64
local.get $3
local.get $3
f64.eq
i32.and
i64.const 0
i64.ne
i32.const 0
i32.ne
i32.eq
Expand Down Expand Up @@ -503,10 +497,6 @@
i32.reinterpret_f32
i32.const 31
i32.shr_u
local.get $3
local.get $3
f32.eq
i32.and
i32.const 0
i32.ne
local.get $1
Expand All @@ -515,10 +505,6 @@
i32.reinterpret_f32
i32.const 31
i32.shr_u
local.get $3
local.get $3
f32.eq
i32.and
i32.const 0
i32.ne
i32.eq
Expand Down Expand Up @@ -48467,11 +48453,8 @@
i64.reinterpret_f64
i64.const 63
i64.shr_u
i32.wrap_i64
local.get $0
local.get $0
f64.eq
i32.and
i64.const 0
i64.ne
i32.const 0
i32.ne
i32.const 0
Expand All @@ -48483,11 +48466,8 @@
i64.reinterpret_f64
i64.const 63
i64.shr_u
i32.wrap_i64
local.get $0
local.get $0
f64.eq
i32.and
i64.const 0
i64.ne
i32.const 0
i32.ne
i32.const 1
Expand All @@ -48499,11 +48479,8 @@
i64.reinterpret_f64
i64.const 63
i64.shr_u
i32.wrap_i64
local.get $0
local.get $0
f64.eq
i32.and
i64.const 0
i64.ne
i32.const 0
i32.ne
i32.const 0
Expand All @@ -48515,11 +48492,8 @@
i64.reinterpret_f64
i64.const 63
i64.shr_u
i32.wrap_i64
local.get $0
local.get $0
f64.eq
i32.and
i64.const 0
i64.ne
i32.const 0
i32.ne
i32.const 1
Expand All @@ -48531,11 +48505,8 @@
i64.reinterpret_f64
i64.const 63
i64.shr_u
i32.wrap_i64
local.get $0
local.get $0
f64.eq
i32.and
i64.const 0
i64.ne
i32.const 0
i32.ne
i32.const 0
Expand All @@ -48548,14 +48519,11 @@
i64.reinterpret_f64
i64.const 63
i64.shr_u
i32.wrap_i64
local.get $0
local.get $0
f64.eq
i32.and
i64.const 0
i64.ne
i32.const 0
i32.ne
i32.const 0
i32.const 1
i32.eq
drop
f64.const inf
Expand All @@ -48564,11 +48532,8 @@
i64.reinterpret_f64
i64.const 63
i64.shr_u
i32.wrap_i64
local.get $0
local.get $0
f64.eq
i32.and
i64.const 0
i64.ne
i32.const 0
i32.ne
i32.const 0
Expand All @@ -48581,11 +48546,8 @@
i64.reinterpret_f64
i64.const 63
i64.shr_u
i32.wrap_i64
local.get $0
local.get $0
f64.eq
i32.and
i64.const 0
i64.ne
i32.const 0
i32.ne
i32.const 1
Expand All @@ -48597,10 +48559,6 @@
i32.reinterpret_f32
i32.const 31
i32.shr_u
local.get $4
local.get $4
f32.eq
i32.and
i32.const 0
i32.ne
i32.const 0
Expand All @@ -48612,10 +48570,6 @@
i32.reinterpret_f32
i32.const 31
i32.shr_u
local.get $4
local.get $4
f32.eq
i32.and
i32.const 0
i32.ne
i32.const 1
Expand All @@ -48627,10 +48581,6 @@
i32.reinterpret_f32
i32.const 31
i32.shr_u
local.get $4
local.get $4
f32.eq
i32.and
i32.const 0
i32.ne
i32.const 0
Expand All @@ -48642,10 +48592,6 @@
i32.reinterpret_f32
i32.const 31
i32.shr_u
local.get $4
local.get $4
f32.eq
i32.and
i32.const 0
i32.ne
i32.const 1
Expand All @@ -48657,10 +48603,6 @@
i32.reinterpret_f32
i32.const 31
i32.shr_u
local.get $4
local.get $4
f32.eq
i32.and
i32.const 0
i32.ne
i32.const 0
Expand All @@ -48673,13 +48615,9 @@
i32.reinterpret_f32
i32.const 31
i32.shr_u
local.get $4
local.get $4
f32.eq
i32.and
i32.const 0
i32.ne
i32.const 0
i32.const 1
i32.eq
drop
f32.const inf
Expand All @@ -48688,10 +48626,6 @@
i32.reinterpret_f32
i32.const 31
i32.shr_u
local.get $4
local.get $4
f32.eq
i32.and
i32.const 0
i32.ne
i32.const 0
Expand All @@ -48704,10 +48638,6 @@
i32.reinterpret_f32
i32.const 31
i32.shr_u
local.get $4
local.get $4
f32.eq
i32.and
i32.const 0
i32.ne
i32.const 1
Expand Down
8 changes: 1 addition & 7 deletions tests/compiler/std/string.optimized.wat
Original file line number Diff line number Diff line change
Expand Up @@ -9249,16 +9249,10 @@
i32.const 3008
i32.const 0
call $~lib/string/parseInt
local.tee $3
i64.reinterpret_f64
i64.const 63
i64.shr_u
i32.wrap_i64
local.get $3
local.get $3
f64.eq
i32.and
i32.eqz
i64.eqz
if
i32.const 0
i32.const 1088
Expand Down
7 changes: 2 additions & 5 deletions tests/compiler/std/string.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -13147,11 +13147,8 @@
i64.reinterpret_f64
i64.const 63
i64.shr_u
i32.wrap_i64
local.get $32
local.get $32
f64.eq
i32.and
i64.const 0
i64.ne
i32.const 0
i32.ne
i32.eqz
Expand Down