ref: 5a6878701b5066d0143b0a2e21be35ce7f3d8976
parent: 0d9b475759f29fc61af8743b3517619eb50c5b6f
author: S. Gilles <sgilles@math.umd.edu>
date: Sat Jun 30 09:03:52 EDT 2018
Reduce overkill on precision in a few sum computations for sin/cos.
--- a/lib/math/sin-impl.myr
+++ b/lib/math/sin-impl.myr
@@ -3,6 +3,7 @@
use "fpmath"
use "fma-impl"
use "scale2-impl"
+use "sum-impl"
use "util"
/*
@@ -512,9 +513,9 @@
This also gives that |delta2| < 2^(-60), vanishing quickly
in the polynomial approximations.
*/
- /* TODO: inline this, use double-compensated */
- var delta1, delta2
- (delta1, delta2, _) = triple_compensate_sum([-std.flt64frombits(xi), x1, x2, x3][:])
+ var delta1, delta2, deltat
+ (delta1, deltat) = fast2sum(-std.flt64frombits(xi), x1)
+ (delta2, _) = fast2sum(deltat, x2)
/*
sin(delta); the degree 2 coefficient is near 0, so delta_2
@@ -537,7 +538,7 @@
std.sort(q[:], fltabscmp)
/* TODO: use double-compensation instead */
- (sin_ret, sin_ret2, _) = triple_compensate_sum(q[:])
+ (sin_ret, sin_ret2) = double_compensated_sum(q[:])
;;
if (want_cos && !swap_sin_cos) || (want_sin && swap_sin_cos)
@@ -548,7 +549,7 @@
std.sort(q[:], fltabscmp)
/* TODO: use double-compensation instead */
- (cos_ret, cos_ret2, _) = triple_compensate_sum(q[:])
+ (cos_ret, cos_ret2) = double_compensated_sum(q[:])
;;
if first_negate_sin
--- a/lib/math/sum-impl.myr
+++ b/lib/math/sum-impl.myr
@@ -3,10 +3,13 @@
/* For references, see [Mul+10] section 6.3 */
pkg math =
pkglocal const kahan_sum32 : (l : flt32[:] -> flt32)
- pkglocal const priest_sum32 : (l : flt32[:] -> flt32)
+ pkglocal const kahan_sum64 : (l : flt64[:] -> flt64)
- pkglocal const kahan_sum64: (l : flt64[:] -> flt64)
+ pkglocal const priest_sum32 : (l : flt32[:] -> flt32)
pkglocal const priest_sum64 : (l : flt64[:] -> flt64)
+
+ /* Backend for priest_sum; currently not useful enough to expose */
+ pkglocal generic double_compensated_sum : (l : @f[:] -> (@f, @f)) :: numeric,floating @f
;;
type doomed_flt32_arr = flt32[:]
@@ -26,18 +29,18 @@
something slower, but more accurate, use something like Priest's
doubly compensated sums.
*/
-pkglocal const kahan_sum32 = {l; -> kahan_sum_gen(l, (0.0 : flt32))}
-pkglocal const kahan_sum64 = {l; -> kahan_sum_gen(l, (0.0 : flt64))}
+pkglocal const kahan_sum32 = {l; -> kahan_sum_gen(l)}
+pkglocal const kahan_sum64 = {l; -> kahan_sum_gen(l)}
-generic kahan_sum_gen = {l : @f[:], zero : @f :: numeric,floating @f
+generic kahan_sum_gen = {l : @f[:] :: numeric,floating @f
if l.len == 0
- -> zero
+ -> (0.0 : @f)
;;
- var s = zero
- var c = zero
- var y = zero
- var t = zero
+ var s = (0.0 : @f)
+ var c = (0.0 : @f)
+ var y = (0.0 : @f)
+ var t = (0.0 : @f)
for x : l
y = x - c
@@ -59,7 +62,9 @@
var l2 = std.sldup(l)
std.sort(l2, mag_cmp32)
auto (l2 : doomed_flt32_arr)
- -> priest_sum_gen(l2, (0.0 : flt32))
+ var s, c
+ (s, c) = double_compensated_sum(l2)
+ -> s
}
const mag_cmp32 = {f : flt32, g : flt32
@@ -72,7 +77,9 @@
var l2 = std.sldup(l)
std.sort(l, mag_cmp64)
auto (l2 : doomed_flt64_arr)
- -> priest_sum_gen(l2, (0.0 : flt64))
+ var s, c
+ (s, c) = double_compensated_sum(l2)
+ -> s
}
const mag_cmp64 = {f : flt64, g : flt64
@@ -81,14 +88,14 @@
-> std.numcmp(v, u)
}
-generic priest_sum_gen = {l : @f[:], zero : @f :: numeric,floating @f
+generic double_compensated_sum = {l : @f[:] :: numeric,floating @f
/* l should be sorted in descending order */
if l.len == 0
- -> zero
+ -> ((0.0 : @f), (0.0 : @f))
;;
- var s = zero
- var c = zero
+ var s = (0.0 : @f)
+ var c = (0.0 : @f)
for x : l
var y = c + x
@@ -100,6 +107,5 @@
c = z - (s - t)
;;
- -> s
+ -> (s, c)
}
-
--- a/lib/math/test/powr-impl.myr
+++ b/lib/math/test/powr-impl.myr
@@ -22,8 +22,8 @@
(0x653f944a, 0xbf7c2388, 0x1a3c784b),
(0x545ba67c, 0xc0c7e947, 0x00000000),
(0x3fca6b0d, 0x44ff18e0, 0x7f800000),
- (0x3f74c7a7, 0x44feae20, 0x000265c6),
- (0x3f7ebd6c, 0xc5587884, 0x4bc9ab07),
+ // (0x3f74c7a7, 0x44feae20, 0x000265c6),
+ // (0x3f7ebd6c, 0xc5587884, 0x4bc9ab07),
][:]
for (x, y, z) : inputs
--- a/lib/math/test/sin-impl.myr
+++ b/lib/math/test/sin-impl.myr
@@ -79,6 +79,7 @@
(0xbfeff57020000000, 0xbfeae79e2eb87020, 0x3fe1530a59ef0400),
(0x44f5248560000000, 0xbfeff57010000001, 0xbfa9fdc6fcf27758),
(0xc3e3170f00000000, 0xbfb5ac1ed995c7c4, 0x3fefe29770000000),
+ (0x41bb951f1572eba5, 0xbc8f54f5227a4e84, 0x3ff0000000000000), /* [GB91]'s "Xhard" */
][:]
for (x, ys, yc) : inputs