Skip to content

Commit 4ecc452

Browse files
RalphASsimonbyrne
authored andcommitted
bugfix: [u]int128 conversion for huge values (#22)
1 parent 9c4c882 commit 4ecc452

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

src/Quadmath.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,9 @@ function Float128(x::UInt128)
158158
if n <= 113
159159
y = ((x % UInt128) << (113-n)) & significand_mask(Float128)
160160
else
161-
y = ((x >> (n-114)) % UInt128) & 0x001_ffff_ffff_ffff_ffff_ffff_ffff_ffff # keep 1 extra bit
161+
y = ((x >> (n-114)) % UInt128) & 0x0001_ffff_ffff_ffff_ffff_ffff_ffff_ffff # keep 1 extra bit
162162
y = (y+1)>>1 # round, ties up (extra leading bit in case of next exponent)
163-
y &= ~UInt64(trailing_zeros(x) == (n-114)) # fix last bit to round to even
163+
y &= ~UInt128(trailing_zeros(x) == (n-114)) # fix last bit to round to even
164164
end
165165
d = ((n+16382) % UInt128) << 112
166166
# reinterpret(Float128, d + y)
@@ -184,7 +184,7 @@ function Float128(x::Int128)
184184
else
185185
y = ((x >> (n-114)) % UInt128) & 0x0001_ffff_ffff_ffff_ffff_ffff_ffff_ffff # keep 1 extra bit
186186
y = (y+1)>>1 # round, ties up (extra leading bit in case of next exponent)
187-
y &= ~UInt64(trailing_zeros(x) == (n-114)) # fix last bit to round to even
187+
y &= ~UInt128(trailing_zeros(x) == (n-114)) # fix last bit to round to even
188188
end
189189
d = ((n+16382) % UInt128) << 112
190190
# reinterpret(Float128, s | d + y)

test/runtests.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ end
2020
else
2121
@test T(Float128(T(1)) + Float128(T(2))) == T(3)
2222
end
23+
if isbitstype(T) && T <: Integer
24+
nb = 8*sizeof(T) - 5
25+
x = T(9) << nb
26+
xf = Float128(9) * 2.0^nb
27+
@test Float128(x) == xf
28+
@test T(xf) == x
29+
end
2330
end
2431

2532
@testset "conversion $T exceptions" for T in (Int32, Int64, UInt32, UInt64)

0 commit comments

Comments
 (0)