Skip to content

Commit

Permalink
fix(stdlib): Correct sign bit in _rempio when computing trig reduct…
Browse files Browse the repository at this point in the history
…ion (#2181)
  • Loading branch information
spotandjake authored Oct 26, 2024
1 parent 584c4d2 commit 6a78502
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
4 changes: 2 additions & 2 deletions compiler/test/stdlib/number.test.gr
Original file line number Diff line number Diff line change
Expand Up @@ -987,7 +987,7 @@ assert Number.isClose( // Note: We lose a lot of precision here do to ieee754 re
) // Max F64
assert Number.isClose(
Number.sin(-1.7976931348623157e+308),
0.00496,
-0.00496,
absoluteTolerance=1e7
) // Max F64
assert Number.isNaN(Number.sin(Infinity))
Expand Down Expand Up @@ -1051,7 +1051,7 @@ assert Number.isClose( // Note: We lose a lot of precision here do to ieee754 re
) // Max F64
assert Number.isClose(
Number.tan(-1.7976931348623157e+308),
-0.00496201587,
0.00496201587,
absoluteTolerance=1e7
) // Max F64
assert Number.isNaN(Number.tan(Infinity))
Expand Down
2 changes: 1 addition & 1 deletion stdlib/runtime/math/rempio2.gr
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ provide let rempio2 = (x: WasmF64, i: WasmI64, sign: Bool) => {
let ix = WasmI32.wrapI64(i >>> 32N) & 0x7FFFFFFFn

if (ix < 0x4002D97Cn) { // |x| < 3pi/4, special case with n=+-1
if (sign) {
if (!sign) {
let z = x - pio2_1
let mut y0 = 0.0W
let mut y1 = 0.0W
Expand Down
15 changes: 9 additions & 6 deletions stdlib/runtime/math/trig.gr
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ provide let sin = (x: WasmF64) => { // see: musl/src/math/sin.c
let i = WasmI64.reinterpretF64(x)
// Get high word of x
let ix = WasmI32.wrapI64(i >>> 32N)
use WasmI32.{ (>>>) }
let sign = ix >>> 31n == 1n
let ix = ix & 0x7FFFFFFFn

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

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

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

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

// argument reduction needed
use WasmI32.{ (>>>) }
let (n, y0, y1) = rempio2(x, i, ix >>> 31n == 0n)
let (n, y0, y1) = rempio2(x, i, sign)
let n = fromInt32(n)
let y0 = fromFloat64(y0)
let y1 = fromFloat64(y1)
Expand All @@ -99,6 +101,8 @@ provide let tan = (x: WasmF64) => { // see: musl/src/math/tan.c
let i = WasmI64.reinterpretF64(x)
// Get high word of x
let ix = WasmI32.wrapI64(i >>> 32N)
use WasmI32.{ (>>>) }
let sign = ix >>> 31n == 1n
let ix = ix & 0x7FFFFFFFn

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

use WasmI32.{ (>>>) }
let (n, y0, y1) = rempio2(x, i, ix >>> 31n == 0n)
let (n, y0, y1) = rempio2(x, i, sign)
let n = fromInt32(n)
let y0 = fromFloat64(y0)
let y1 = fromFloat64(y1)
Expand Down

0 comments on commit 6a78502

Please sign in to comment.