Skip to content

Commit 008996a

Browse files
jmkuhnstevengj
authored andcommitted
Use struct for Dec128
1 parent 1aac6c6 commit 008996a

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

src/DecFP.jl

+14-3
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,18 @@ Base.Rounding.rounding(::Type{T}) where {T<:DecimalFloatingPoint} =
109109
Base.Rounding.setrounding(::Type{T}, r::RoundingMode) where {T<:DecimalFloatingPoint} =
110110
Base.Rounding.setrounding_raw(T, convert(DecFPRoundingMode, r))
111111

112+
primitive type Dec32 <: DecimalFloatingPoint 32 end
113+
primitive type Dec64 <: DecimalFloatingPoint 64 end
114+
Dec32(x::Number) = convert(Dec32, x)
115+
Dec64(x::Number) = convert(Dec64, x)
116+
struct Dec128 <: DecimalFloatingPoint
117+
x::UInt128
118+
Dec128(x::Number) = convert(Dec128, x)
119+
Base.reinterpret(::Type{Dec128}, x::UInt128) = new(x)
120+
end
112121
for w in (32,64,128)
113122
BID = Symbol(string("Dec",w))
114123
Ti = Symbol(string("UInt",w))
115-
@eval primitive type $BID <: DecimalFloatingPoint $w end
116-
$BID(x::Number) = convert($BID, x)
117124

118125
@eval function $BID(x::Real, mode::RoundingMode)
119126
setrounding($BID, mode) do
@@ -613,11 +620,15 @@ for w in (32,64,128)
613620
end
614621
end
615622

616-
@eval Base.bswap(x::$BID) = reinterpret($BID, bswap(reinterpret($Ti, x)))
617623
@eval Base.convert(::Type{Float16}, x::$BID) = convert(Float16, convert(Float32, x))
618624
@eval Base.Float16(x::$BID) = convert(Float16, x)
619625
end # widths w
620626

627+
Base.reinterpret(::Type{UInt128}, x::Dec128) = x.x
628+
Base.bswap(x::Dec32) = reinterpret(Dec32, bswap(reinterpret(UInt32, x)))
629+
Base.bswap(x::Dec64) = reinterpret(Dec64, bswap(reinterpret(UInt64, x)))
630+
Base.bswap(x::Dec128) = reinterpret(Dec128, bswap(x.x))
631+
621632
Base.round(x::DecimalFloatingPoint, ::RoundingMode{:FromZero}) = signbit(x) ? floor(x) : ceil(x)
622633

623634
for (f) in (:trunc, :floor, :ceil)

0 commit comments

Comments
 (0)