Skip to content

Commit 811bc68

Browse files
robustify four_sum(a,b,c,d)
1 parent 723a3fb commit 811bc68

File tree

1 file changed

+18
-19
lines changed

1 file changed

+18
-19
lines changed

src/math/errorfree.jl

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,43 +28,42 @@ end
2828
2929
Computes `hi = fl(a+b+c)` and `md = err(a+b+c), lo = err(md)`.
3030
"""
31-
function three_sum(a::T, b::T, c::T) where {T<:FloatWithFMA}
32-
s, t = two_sum(b, c)
33-
hi, u = two_sum(a, s)
34-
md, lo = two_sum(u, t)
31+
function three_sum(a::T,b::T,c::T) where {T<:FloatWithFMA}
32+
t0, t1 = two_sum(a, b)
33+
hi, t2 = two_sum(t0, c)
34+
md, lo = two_sum(t2, t1)
3535
hi, md = two_hilo_sum(hi, md)
3636
return hi, md, lo
3737
end
3838

39-
4039
"""
4140
four_sum(a, b, c, d)
4241
4342
Computes `hi = fl(a+b+c+d)` and `hm = err(a+b+c+d), ml = err(hm), lo = err(ml)`.
4443
"""
4544
function four_sum(a::T,b::T,c::T,d::T) where {T<:FloatWithFMA}
46-
t0, t1 = two_sum(a , b)
47-
t0, t2 = two_sum(t0, c)
48-
hi, t3 = two_sum(t0, d)
49-
t0, t1 = two_sum(t1, t2)
50-
hm, t2 = two_sum(t0, t3) # here, t0 >= t3
51-
ml, lo = two_sum(t1, t2)
45+
t0, t1 = two_sum(a, b)
46+
t2, t3 = two_sum(c, d)
47+
hi, t4 = two_sum(t0, t2)
48+
t5, lo = two_sum(t1, t3)
49+
hm, ml = two_sum(t4, t5)
50+
ml, lo = two_hilo_sum(ml, lo)
51+
hm, ml = two_hilo_sum(hm, ml)
52+
hi, hm = two_hilo_sum(hi,hm)
5253
return hi, hm, ml, lo
5354
end
5455

55-
56-
5756
"""
5857
two_sum(a, b, c)
5958
6059
Computes `hi = fl(a+b+c)` and `lo = err(a+b+c)`.
6160
"""
62-
function two_sum(a::T, b::T, c::T) where {T<:FloatWithFMA}
63-
s, t = two_sum(b, c)
64-
x, u = two_sum(a, s)
65-
y = u + t
66-
x, y = two_sum(x, y)
67-
return x, y
61+
function two_sum(a::T,b::T,c::T) where {T<:FloatWithFMA}
62+
t0, t1 = two_sum(a, b)
63+
hi, t2 = two_sum(t0, c)
64+
lo = t2 + t1
65+
hi, lo = two_hilo_sum(hi, lo)
66+
return hi, lo
6867
end
6968

7069
"""

0 commit comments

Comments
 (0)