Skip to content

Commit 6a78502

Browse files
authored
fix(stdlib): Correct sign bit in _rempio when computing trig reduction (#2181)
1 parent 584c4d2 commit 6a78502

File tree

3 files changed

+12
-9
lines changed

3 files changed

+12
-9
lines changed

compiler/test/stdlib/number.test.gr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -987,7 +987,7 @@ assert Number.isClose( // Note: We lose a lot of precision here do to ieee754 re
987987
) // Max F64
988988
assert Number.isClose(
989989
Number.sin(-1.7976931348623157e+308),
990-
0.00496,
990+
-0.00496,
991991
absoluteTolerance=1e7
992992
) // Max F64
993993
assert Number.isNaN(Number.sin(Infinity))
@@ -1051,7 +1051,7 @@ assert Number.isClose( // Note: We lose a lot of precision here do to ieee754 re
10511051
) // Max F64
10521052
assert Number.isClose(
10531053
Number.tan(-1.7976931348623157e+308),
1054-
-0.00496201587,
1054+
0.00496201587,
10551055
absoluteTolerance=1e7
10561056
) // Max F64
10571057
assert Number.isNaN(Number.tan(Infinity))

stdlib/runtime/math/rempio2.gr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ provide let rempio2 = (x: WasmF64, i: WasmI64, sign: Bool) => {
170170
let ix = WasmI32.wrapI64(i >>> 32N) & 0x7FFFFFFFn
171171

172172
if (ix < 0x4002D97Cn) { // |x| < 3pi/4, special case with n=+-1
173-
if (sign) {
173+
if (!sign) {
174174
let z = x - pio2_1
175175
let mut y0 = 0.0W
176176
let mut y1 = 0.0W

stdlib/runtime/math/trig.gr

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ provide let sin = (x: WasmF64) => { // see: musl/src/math/sin.c
2626
let i = WasmI64.reinterpretF64(x)
2727
// Get high word of x
2828
let ix = WasmI32.wrapI64(i >>> 32N)
29+
use WasmI32.{ (>>>) }
30+
let sign = ix >>> 31n == 1n
2931
let ix = ix & 0x7FFFFFFFn
3032

3133
// |x| ~< pi/4
@@ -40,8 +42,7 @@ provide let sin = (x: WasmF64) => { // see: musl/src/math/sin.c
4042
if (ix >= 0x7FF00000n) return x - x
4143

4244
// argument reduction needed
43-
use WasmI32.{ (>>>) }
44-
let (n, y0, y1) = rempio2(x, i, ix >>> 31n == 0n)
45+
let (n, y0, y1) = rempio2(x, i, sign)
4546
let n = fromInt32(n)
4647
let y0 = fromFloat64(y0)
4748
let y1 = fromFloat64(y1)
@@ -63,6 +64,8 @@ provide let cos = (x: WasmF64) => { // see: musl/src/math/cos.c
6364

6465
// Get high word of x
6566
let ix = WasmI32.wrapI64(i >>> 32N)
67+
use WasmI32.{ (>>>) }
68+
let sign = ix >>> 31n == 1n
6669
let ix = ix & 0x7FFFFFFFn
6770

6871
// |x| ~< pi/4
@@ -77,8 +80,7 @@ provide let cos = (x: WasmF64) => { // see: musl/src/math/cos.c
7780
if (ix >= 0x7FF00000n) return x - x
7881

7982
// argument reduction needed
80-
use WasmI32.{ (>>>) }
81-
let (n, y0, y1) = rempio2(x, i, ix >>> 31n == 0n)
83+
let (n, y0, y1) = rempio2(x, i, sign)
8284
let n = fromInt32(n)
8385
let y0 = fromFloat64(y0)
8486
let y1 = fromFloat64(y1)
@@ -99,6 +101,8 @@ provide let tan = (x: WasmF64) => { // see: musl/src/math/tan.c
99101
let i = WasmI64.reinterpretF64(x)
100102
// Get high word of x
101103
let ix = WasmI32.wrapI64(i >>> 32N)
104+
use WasmI32.{ (>>>) }
105+
let sign = ix >>> 31n == 1n
102106
let ix = ix & 0x7FFFFFFFn
103107

104108
// |x| ~< pi/4
@@ -112,8 +116,7 @@ provide let tan = (x: WasmF64) => { // see: musl/src/math/tan.c
112116
// tan(Inf or NaN) is NaN
113117
if (ix >= 0x7FF00000n) return x - x
114118

115-
use WasmI32.{ (>>>) }
116-
let (n, y0, y1) = rempio2(x, i, ix >>> 31n == 0n)
119+
let (n, y0, y1) = rempio2(x, i, sign)
117120
let n = fromInt32(n)
118121
let y0 = fromFloat64(y0)
119122
let y1 = fromFloat64(y1)

0 commit comments

Comments
 (0)