Skip to content

Commit 525b2e1

Browse files
authored
Simplify floating points comparison for Object.is (#2476)
1 parent 819d8a3 commit 525b2e1

File tree

2 files changed

+115
-66
lines changed

2 files changed

+115
-66
lines changed

Diff for: std/assembly/object.ts

+15-14
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
export class Object {
2-
static is<T>(value1: T, value2: T): bool {
2+
static is<T>(x: T, y: T): bool {
33
if (isFloat<T>()) {
4-
if (value1 == value2) {
5-
// 0 == -0, but they are not identical
6-
if (sizeof<T>() == 8) {
7-
// @ts-ignore: typecast
8-
return reinterpret<u64>(value1) == reinterpret<u64>(value2);
9-
} else {
10-
// @ts-ignore: typecast
11-
return reinterpret<u32>(value1) == reinterpret<u32>(value2);
12-
}
4+
// Float pointing is special we shoulr presere following identities:
5+
// 0.0 !=-0.0
6+
// NaN == NaN
7+
if (sizeof<T>() == 8) {
8+
return (
9+
bool(u32(x != x) & u32(y != y) |
10+
u32(reinterpret<u64>(f64(x)) == reinterpret<u64>(f64(y))))
11+
);
12+
} else {
13+
return (
14+
bool(u32(x != x) & u32(y != y) |
15+
u32(reinterpret<u32>(f32(x)) == reinterpret<u32>(f32(y))))
16+
);
1317
}
14-
// NaN != NaN, but they are identical.
15-
// @ts-ignore: typecast
16-
return bool(i32(isNaN(value1)) & i32(isNaN(value2)));
1718
}
1819
// For references, strings, integers and booleans
19-
return value1 == value2;
20+
return x == y;
2021
}
2122
}

0 commit comments

Comments
 (0)