Skip to content

Commit

Permalink
Backport "Fix _unsafe_trunc to reduce the likelihood of arbitrary v…
Browse files Browse the repository at this point in the history
  • Loading branch information
kimikage committed May 13, 2024
1 parent a995a9d commit b62e8ce
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 35 deletions.
19 changes: 12 additions & 7 deletions src/utilities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,17 @@ 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))
_unsafe_trunc(::Type{T}, x) where {T} = unsafe_trunc(T, x)
# issue #202, #211
_unsafe_trunc(::Type{T}, x::BigFloat) where {T <: Integer} = trunc(BigInt, x) % T

# issue #288
function _unsafe_trunc(::Type{T}, x::AbstractFloat) where {T <: Integer}
if T <: ShortInts
return unsafe_trunc(Int32, x) % T
elseif T <: Unsigned
return copysign(unsafe_trunc(T, abs(x)), x)
else
return unsafe_trunc(T, x)
end
# exclude BigFloat (issue #202)
_unsafe_trunc(::Type{T}, x::BigFloat) where {T <: Integer} = unsafe_trunc(T, x)
end
16 changes: 2 additions & 14 deletions test/fixed.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,8 @@ end
issue288_out = String(take!(buf))

@testset "issue288" begin
expected_issue288 = "-1.0Q0f7 -1.0Q0f15 -1.0Q0f15 -1.0Q0f7 "
if issue288_in == expected_issue288 # just leave it in the report
@test issue288_in == expected_issue288
else
@test_broken issue288_in == expected_issue288
@warn """broken: "$issue288_in"\nexpected: "$expected_issue288" """
end
expected_issue288 = "-1.0Q0f7 -1.0Q0f15 -1.0Q0f15 -1.0Q0f7 "
if issue288_out == expected_issue288 # just leave it in the report
@test issue288_out == expected_issue288
else
@test_broken issue288_out == expected_issue288
@warn """broken: "$issue288_out"\nexpected: "$expected_issue288" """
end
@test issue288_in == "-1.0Q0f7 -1.0Q0f15 -1.0Q0f15 -1.0Q0f7 "
@test issue288_out == "-1.0Q0f7 -1.0Q0f15 -1.0Q0f15 -1.0Q0f7 "
end

function test_op(fun::F, ::Type{T}, fx, fy, fxf, fyf, tol) where {F,T}
Expand Down
16 changes: 2 additions & 14 deletions test/normed.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,8 @@ end
issue288_out = String(take!(buf))

@testset "issue288" begin
expected_issue288 = "1.0N0f8 1.0N0f16 1.0N0f16 1.0N0f8 "
if issue288_in == expected_issue288 # just leave it in the report
@test issue288_in == expected_issue288
else
@test_broken issue288_in == expected_issue288
@warn """broken: "$issue288_in"\nexpected: "$expected_issue288" """
end
expected_issue288 = "0.004N0f8 2.0e-5N0f16 2.0e-5N0f16 0.004N0f8 "
if issue288_out == expected_issue288 # just leave it in the report
@test issue288_out == expected_issue288
else
@test_broken issue288_out == expected_issue288
@warn """broken: "$issue288_out"\nexpected: "$expected_issue288" """
end
@test issue288_in == "1.0N0f8 1.0N0f16 1.0N0f16 1.0N0f8 "
@test issue288_out == "0.004N0f8 2.0e-5N0f16 2.0e-5N0f16 0.004N0f8 "
end

@testset "domain of f" begin
Expand Down

0 comments on commit b62e8ce

Please sign in to comment.