Skip to content

Commit

Permalink
Use struct for Dec128
Browse files Browse the repository at this point in the history
  • Loading branch information
jmkuhn authored and stevengj committed Mar 23, 2022
1 parent 42db550 commit 8ec3f39
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/DecFP.jl
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,18 @@ Base.Rounding.rounding(::Type{T}) where {T<:DecimalFloatingPoint} =
Base.Rounding.setrounding(::Type{T}, r::RoundingMode) where {T<:DecimalFloatingPoint} =
Base.Rounding.setrounding_raw(T, convert(DecFPRoundingMode, r))

primitive type Dec32 <: DecimalFloatingPoint 32 end
primitive type Dec64 <: DecimalFloatingPoint 64 end
Dec32(x::Number) = convert(Dec32, x)
Dec64(x::Number) = convert(Dec64, x)
struct Dec128 <: DecimalFloatingPoint
x::UInt128
Dec128(x::Number) = convert(Dec128, x)
Base.reinterpret(::Type{Dec128}, x::UInt128) = new(x)
end
for w in (32,64,128)
BID = Symbol(string("Dec",w))
Ti = Symbol(string("UInt",w))
@eval primitive type $BID <: DecimalFloatingPoint $w end
$BID(x::Number) = convert($BID, x)

@eval function $BID(x::Real, mode::RoundingMode)
setrounding($BID, mode) do
Expand Down Expand Up @@ -613,11 +620,15 @@ for w in (32,64,128)
end
end

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

Base.reinterpret(::Type{UInt128}, x::Dec128) = x.x
Base.bswap(x::Dec32) = reinterpret(Dec32, bswap(reinterpret(UInt32, x)))
Base.bswap(x::Dec64) = reinterpret(Dec64, bswap(reinterpret(UInt64, x)))
Base.bswap(x::Dec128) = reinterpret(Dec128, bswap(x.x))

Base.round(x::DecimalFloatingPoint, ::RoundingMode{:FromZero}) = signbit(x) ? floor(x) : ceil(x)

for (f) in (:trunc, :floor, :ceil)
Expand Down

0 comments on commit 8ec3f39

Please sign in to comment.