Skip to content

Commit 404a1ab

Browse files
authored
add definitions for missing base/float.jl methods (#82)
* add definitions for missing base/float.jl methods related to significand and exponent bits
1 parent afbce2c commit 404a1ab

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

src/Quadmath.jl

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ export Float128, ComplexF128, Inf128
66

77
import Base: (*), +, -, /, <, <=, ==, ^, convert,
88
reinterpret, sign_mask, exponent_mask, exponent_one, exponent_half,
9-
significand_mask, exponent, significand,
9+
significand_mask, significand_bits, exponent_bits, exponent_bias,
10+
exponent_max, exponent_raw_max, uinttype, inttype, floattype,
11+
exponent, significand,
1012
promote_rule, widen,
1113
string, print, show, parse,
1214
acos, acosh, asin, asinh, atan, atanh, cosh, cos, sincos,
@@ -127,7 +129,18 @@ exponent_one(::Type{Float128}) = 0x3fff_0000_0000_0000_0000_0000_0000_0000
127129
exponent_half(::Type{Float128}) = 0x3ffe_0000_0000_0000_0000_0000_0000_0000
128130
significand_mask(::Type{Float128}) = 0x0000_ffff_ffff_ffff_ffff_ffff_ffff_ffff
129131

130-
fpinttype(::Type{Float128}) = UInt128
132+
significand_bits(::Type{Float128}) = 112
133+
exponent_bits(::Type{Float128}) = 15
134+
exponent_bias(::Type{Float128}) = 16383
135+
exponent_max(::Type{Float128}) = 16383
136+
exponent_raw_max(::Type{Float128}) = 32767
137+
138+
uinttype(::Type{Float128}) = UInt128
139+
inttype(::Type{Float128}) = Int128
140+
# TODO: Logically speaking, we should also make the following definitions,
141+
# but this would constitute type piracy under Aqua.jl rules.
142+
# floattype(::Type{UInt128}) = Float128
143+
# floattype(::Type{Int128}) = Float128
131144

132145
# conversion
133146
Float128(x::Float128) = x

test/runtests.jl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,25 @@
11
using Test, Random
22
using Quadmath
33

4+
@testset "fp properties" begin
5+
# Test that the definitions of sign_mask, exponent_mask, significand_bits,
6+
# etc. are compatible with the definitions in base/float.jl. Compare to:
7+
# https://github.com/JuliaLang/julia/blob/5595d20a2877560583cd4891ce91605d10b1bb75/base/float.jl#L106
8+
@test Base.significand_bits(Float128) ===
9+
trailing_ones(Base.significand_mask(Float128))
10+
@test Base.exponent_bits(Float128) ===
11+
sizeof(Float128)*8 - Base.significand_bits(Float128) - 1
12+
@test Base.exponent_bias(Float128) === Int(
13+
Base.exponent_one(Float128) >> Base.significand_bits(Float128))
14+
@test Base.exponent_max(Float128) ===
15+
Int(Base.exponent_mask(Float128) >> Base.significand_bits(Float128)) -
16+
Base.exponent_bias(Float128) - 1
17+
@test Base.exponent_raw_max(Float128) === Int(
18+
Base.exponent_mask(Float128) >> Base.significand_bits(Float128))
19+
@test Base.uinttype(Float128) === UInt128
20+
@test Base.inttype(Float128) === Int128
21+
end
22+
423
@testset "fp decomp" begin
524
y = Float128(2.0)
625
x,n = frexp(y)

0 commit comments

Comments
 (0)