diff --git a/Project.toml b/Project.toml index a66e29f5..31c836cc 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "FixedPointNumbers" uuid = "53c48c17-4a7d-5ca2-90c5-79b7896eea93" -version = "0.8.3" +version = "0.8.4" [deps] Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2" diff --git a/src/fixed.jl b/src/fixed.jl index b0245848..5beb98df 100644 --- a/src/fixed.jl +++ b/src/fixed.jl @@ -54,7 +54,7 @@ end function _convert(::Type{F}, x::Integer) where {T, f, F <: Fixed{T,f}} if ((typemin(T) >> f) <= x) & (x <= (typemax(T) >> f)) - reinterpret(F, unsafe_trunc(T, x) << f) + reinterpret(F, _unsafe_trunc(T, x) << f) else throw_converterror(F, x) end diff --git a/src/normed.jl b/src/normed.jl index 28f67832..5c2f6c4f 100644 --- a/src/normed.jl +++ b/src/normed.jl @@ -324,12 +324,3 @@ end end :(Normed{$T,$f}) end - -_unsafe_trunc(::Type{T}, x::Integer) where {T} = x % T -_unsafe_trunc(::Type{T}, x) where {T} = unsafe_trunc(T, x) -if !signbit(signed(unsafe_trunc(UInt, -12.345))) - # a workaround for 32-bit ARMv7 (issue #134) - function _unsafe_trunc(::Type{T}, x::AbstractFloat) where {T} - unsafe_trunc(T, unsafe_trunc(typeof(signed(zero(T))), x)) - end -end diff --git a/src/utilities.jl b/src/utilities.jl index 8d1b84b5..2c685de8 100644 --- a/src/utilities.jl +++ b/src/utilities.jl @@ -13,6 +13,8 @@ widen1(::Type{Int128}) = Int128 widen1(::Type{UInt128}) = UInt128 widen1(x::Integer) = x % widen1(typeof(x)) +signedtype(::Type{T}) where {T <: Integer} = typeof(signed(zero(T))) + const ShortInts = Union{Int8, UInt8, Int16, UInt16} const LongInts = Union{Int64, UInt64, Int128, UInt128, BigInt} @@ -34,3 +36,14 @@ significand_bits(::Type{Float32}) = 23 significand_bits(::Type{Float64}) = 52 exponent_bias(::Type{Float32}) = 127 exponent_bias(::Type{Float64}) = 1023 + +_unsafe_trunc(::Type{T}, x::Integer) where {T} = x % T +_unsafe_trunc(::Type{T}, x) where {T} = unsafe_trunc(T, x) +if !signbit(signed(unsafe_trunc(UInt, -12.345))) + # a workaround for ARM (issue #134) + function _unsafe_trunc(::Type{T}, x::AbstractFloat) where {T <: Integer} + unsafe_trunc(T, unsafe_trunc(signedtype(T), x)) + end + # exclude BigFloat (issue #202) + _unsafe_trunc(::Type{T}, x::BigFloat) where {T <: Integer} = unsafe_trunc(T, x) +end