Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

faster min/max for IEEEFloats #45532

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions base/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -765,9 +765,27 @@ max(x::T, y::T) where {T<:AbstractFloat} = ifelse((y > x) | (signbit(y) < signbi
min(x::T, y::T) where {T<:AbstractFloat} = ifelse((y < x) | (signbit(y) > signbit(x)),
ifelse(isnan(x), x, y), ifelse(isnan(y), y, x))

minmax(x::T, y::T) where {T<:AbstractFloat} =
ifelse(isnan(x) | isnan(y), ifelse(isnan(x), (x,x), (y,y)),
ifelse((y > x) | (signbit(x) > signbit(y)), (x,y), (y,x)))
function max(x::T, y::T) where {T<:IEEEFloat}
a = x > y ? x : y
b = y > x ? y : x
# if either argument is NaN, a != b and at least one is NaN
# else if they are opposite zeros, a == b and a !== b
# else a == b and a === b
argplus = a + b # a+b is NaN iff x or y is NaN or they are are opposite-signed Infs - opposite Infs are impossible given the preceding comparisons
argand = reinterpret(T, reinterpret(Unsigned, a) & reinterpret(Unsigned, b)) # no-op for a==b except to prefer +0.0 over -0.0
return isnan(argplus) ? argplus : argand
end

function min(x::T, y::T) where {T<:IEEEFloat}
a = x < y ? x : y
b = y < x ? y : x
# if either argument is NaN, a != b and at least one is NaN
# else if they are opposite zeros, a == b and a !== b
# else a == b and a === b
argplus = a + b # a+b is NaN iff x or y is NaN or they are are opposite-signed Infs - opposite Infs are impossible given the preceding comparisons
argor = reinterpret(T, reinterpret(Unsigned, a) | reinterpret(Unsigned, b)) # no-op for a==b except to prefer -0.0 over +0.0
return isnan(argplus) ? argplus : argor
end


"""
Expand Down
2 changes: 1 addition & 1 deletion base/promotion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,6 @@ minmax(x::Real) = (x, x)

max(x::T, y::T) where {T<:Real} = ifelse(y < x, x, y)
min(x::T, y::T) where {T<:Real} = ifelse(y < x, y, x)
minmax(x::T, y::T) where {T<:Real} = y < x ? (y, x) : (x, y)
minmax(x::T, y::T) where {T<:Real} = (min(x, y), max(x, y))

flipsign(x::T, y::T) where {T<:Signed} = no_op_err("flipsign", T)
4 changes: 2 additions & 2 deletions test/numbers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ end
@test minmax(-Inf, NaN) ≣ (NaN, NaN)
@test minmax(NaN, -Inf) ≣ (NaN, NaN)
@test minmax(NaN, NaN) ≣ (NaN, NaN)
@test min(-0.0,0.0) === min(0.0,-0.0)
@test max(-0.0,0.0) === max(0.0,-0.0)
@test min(-0.0,0.0) === min(0.0,-0.0) === -0.0
@test max(-0.0,0.0) === max(0.0,-0.0) === 0.0
@test minmax(-0.0,0.0) === minmax(0.0,-0.0)
@test max(-3.2, 5.1) == max(5.1, -3.2) == 5.1
@test min(-3.2, 5.1) == min(5.1, -3.2) == -3.2
Expand Down