Skip to content

Commit 65049ad

Browse files
committed
Fix _unsafe_trunc for BigFloat on ARM
This also moves `_unsafe_trunc` to "src/utilities.jl".
1 parent 3f12729 commit 65049ad

File tree

4 files changed

+15
-11
lines changed

4 files changed

+15
-11
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "FixedPointNumbers"
22
uuid = "53c48c17-4a7d-5ca2-90c5-79b7896eea93"
3-
version = "0.8.3"
3+
version = "0.8.4"
44

55
[deps]
66
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"

src/fixed.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ end
5454

5555
function _convert(::Type{F}, x::Integer) where {T, f, F <: Fixed{T,f}}
5656
if ((typemin(T) >> f) <= x) & (x <= (typemax(T) >> f))
57-
reinterpret(F, unsafe_trunc(T, x) << f)
57+
reinterpret(F, _unsafe_trunc(T, x) << f)
5858
else
5959
throw_converterror(F, x)
6060
end

src/normed.jl

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -323,12 +323,3 @@ end
323323
end
324324
:(Normed{$T,$f})
325325
end
326-
327-
_unsafe_trunc(::Type{T}, x::Integer) where {T} = x % T
328-
_unsafe_trunc(::Type{T}, x) where {T} = unsafe_trunc(T, x)
329-
if !signbit(signed(unsafe_trunc(UInt, -12.345)))
330-
# a workaround for 32-bit ARMv7 (issue #134)
331-
function _unsafe_trunc(::Type{T}, x::AbstractFloat) where {T}
332-
unsafe_trunc(T, unsafe_trunc(typeof(signed(zero(T))), x))
333-
end
334-
end

src/utilities.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ widen1(::Type{Int128}) = Int128
1313
widen1(::Type{UInt128}) = UInt128
1414
widen1(x::Integer) = x % widen1(typeof(x))
1515

16+
signedtype(::Type{T}) where {T <: Integer} = typeof(signed(zero(T)))
17+
1618
const ShortInts = Union{Int8, UInt8, Int16, UInt16}
1719
const LongInts = Union{Int64, UInt64, Int128, UInt128, BigInt}
1820

@@ -34,3 +36,14 @@ significand_bits(::Type{Float32}) = 23
3436
significand_bits(::Type{Float64}) = 52
3537
exponent_bias(::Type{Float32}) = 127
3638
exponent_bias(::Type{Float64}) = 1023
39+
40+
_unsafe_trunc(::Type{T}, x::Integer) where {T} = x % T
41+
_unsafe_trunc(::Type{T}, x) where {T} = unsafe_trunc(T, x)
42+
if !signbit(signed(unsafe_trunc(UInt, -12.345)))
43+
# a workaround for ARM (issue #134)
44+
function _unsafe_trunc(::Type{T}, x::AbstractFloat) where {T <: Integer}
45+
unsafe_trunc(T, unsafe_trunc(signedtype(T), x))
46+
end
47+
# exclude BigFloat (issue #202)
48+
_unsafe_trunc(::Type{T}, x::BigFloat) where {T <: Integer} = unsafe_trunc(T, x)
49+
end

0 commit comments

Comments
 (0)