Skip to content

Commit c085185

Browse files
Create Double64.jl
1 parent fd3bbb3 commit c085185

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

src/Double64.jl

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#=
2+
struct MultiFloat{T,N} <: AbstractFloat
3+
_limbs::NTuple{N,T}
4+
end
5+
=#
6+
7+
# This function is needed to work around the following SIMD bug:
8+
# https://github.com/eschnett/SIMD.jl/issues/115
9+
@inline _ntuple_equal(x::NTuple{2,Float64}, y::NTuple{2,Float64}) = (x[1] === y[1]) && (x[2] === y[2])
10+
@inline _ntuple_equal(x::NTuple{N,Vec{M,T}}, y::NTuple{N,Vec{M,T}}) where {N,M,T} = all(all.(x .== y))
11+
12+
const Tuple2 = NTuple{2, Float64]
13+
14+
struct Double64 <: AbstractFloat
15+
hilo::NTuple{2,Float64}
16+
end
17+
18+
Double64() = Double64((0.0, 0.0))
19+
Double64(hi::Float64) = Double64((hi, 0.0))
20+
21+
Base.zero(::Type{Double64}) = Double64((0.0, 0.0))
22+
Base.one(::Type{Double64}) = Double64((1.0, 0.0))
23+
24+
Base.length(::Type{Double64}) = 2
25+
Base.getindex(x::Double64, idx::Integer) = getindex(x, x.hilo[idx])
26+
27+
#
28+
29+
@inline function fast_twosum(a::T, b::T) where {T}
30+
sum = a + b
31+
b_prime = sum - a
32+
b_err = b - b_prime
33+
return (sum, b_err)
34+
end
35+
36+
@inline function fast_twodiff(a::T, b::T) where {T}
37+
diff = a - b
38+
b_prime = a - diff
39+
b_err = b_prime - b
40+
return (diff, b_err)
41+
end
42+
43+
@inline function twosum(a::T, b::T) where {T}
44+
sum = a + b
45+
a_prime = sum - b
46+
b_prime = sum - a_prime
47+
a_err = a - a_prime
48+
b_err = b - b_prime
49+
err = a_err + b_err
50+
return (sum, err)
51+
end
52+
53+
@inline function twodiff(a::T, b::T) where {T}
54+
diff = a - b
55+
a_prime = diff + b
56+
b_prime = a_prime - diff
57+
a_err = a - a_prime
58+
b_err = b - b_prime
59+
err = a_err - b_err
60+
return (diff, err)
61+
end
62+
63+
@inline function twoprod(a::T, b::T) where {T}
64+
prod = a * b
65+
err = fma(a, b, -prod)
66+
return (prod, err)
67+
end
68+
69+
@inline function renormalize(x::Float64, y::Float64)
70+
return( twosum(x, y) )
71+
end
72+
@inline function renormalize(a::Tuple2)
73+
return( twosum(a[1], a[2]) )
74+
end
75+
@inline function renormalize(a::Double64)
76+
Double64(renormalize(a.hilo))
77+
end
78+

0 commit comments

Comments
 (0)