diff --git a/src/DecFP.jl b/src/DecFP.jl index ad5ed5f..789ec43 100644 --- a/src/DecFP.jl +++ b/src/DecFP.jl @@ -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 @@ -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)