Skip to content

Commit fa93ae5

Browse files
committed
improve cos_sum
1 parent 62be889 commit fa93ae5

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/misc.jl

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# written by @oscardssmith
44
function cos_sum(x, xn)
55
s = x + xn
6-
n, r = Base.Math.rem_pio2_kernel(s)
6+
n, r = reduce_pi02_med(s)
77
lo = r.lo - ((s - x) - xn)
88
hi = r.hi + lo
99
y = Base.Math.DoubleFloat64(hi, r.hi-hi+lo)
@@ -21,7 +21,7 @@ end
2121
# function to more accurately compute sin(x + xn)
2222
function sin_sum(x, xn)
2323
s = x + xn
24-
n, r = Base.Math.rem_pio2_kernel(s)
24+
n, r = reduce_pi02_med(s)
2525
lo = r.lo - ((s - x) - xn)
2626
hi = r.hi + lo
2727
y = Base.Math.DoubleFloat64(hi, r.hi-hi+lo)
@@ -36,3 +36,12 @@ function sin_sum(x, xn)
3636
return -Base.Math.cos_kernel(y)
3737
end
3838
end
39+
@inline function reduce_pi02_med(x::Float64)
40+
pio2_1 = 1.57079632673412561417e+00
41+
42+
fn = round(x*(2/pi))
43+
r = muladd(-fn, pio2_1, x)
44+
w = fn * 6.07710050650619224932e-11
45+
y = r-w
46+
return unsafe_trunc(Int, fn), Base.Math.DoubleFloat64(y, (r-y)-w)
47+
end

0 commit comments

Comments
 (0)