Skip to content

Commit 8611261

Browse files
authored
fix: Fix negative inputs of Math.acosh (#1876)
1 parent 39a99f0 commit 8611261

File tree

8 files changed

+5475
-5418
lines changed

8 files changed

+5475
-5418
lines changed

std/assembly/math.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,11 @@ export namespace NativeMath {
498498

499499
export function acosh(x: f64): f64 { // see: musl/src/math/acosh.c
500500
const s = reinterpret<f64>(0x3FE62E42FEFA39EF);
501-
var e = reinterpret<u64>(x) >> 52 & 0x7FF;
501+
var u = reinterpret<u64>(x);
502+
// Prevent propagation for all input values less than 1.0.
503+
// Note musl lib didn't fix this yet.
504+
if (<i64>u < 0x3FF0000000000000) return (x - x) / 0.0;
505+
var e = u >> 52 & 0x7FF;
502506
if (e < 0x3FF + 1) return log1p(x - 1 + builtin_sqrt<f64>((x - 1) * (x - 1) + 2 * (x - 1)));
503507
if (e < 0x3FF + 26) return log(2 * x - 1 / (x + builtin_sqrt<f64>(x * x - 1)));
504508
return log(x) + s;

tests/compiler/std/array.optimized.wat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4547,7 +4547,7 @@
45474547
if
45484548
i32.const 0
45494549
i32.const 7264
4550-
i32.const 1417
4550+
i32.const 1421
45514551
i32.const 5
45524552
call $~lib/builtins/abort
45534553
unreachable

tests/compiler/std/array.untouched.wat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6650,7 +6650,7 @@
66506650
if
66516651
i32.const 0
66526652
i32.const 6240
6653-
i32.const 1417
6653+
i32.const 1421
66546654
i32.const 5
66556655
call $~lib/builtins/abort
66566656
unreachable

0 commit comments

Comments
 (0)