Skip to content

Commit

Permalink
Fastpath for comparing tuples of two bitintegers (JuliaLang#48753)
Browse files Browse the repository at this point in the history
  • Loading branch information
LilithHafner authored and mkitti committed Feb 27, 2023
1 parent 456e553 commit cf20620
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
7 changes: 7 additions & 0 deletions base/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,13 @@ isless(x::AbstractFloat, y::AbstractFloat) = (!isnan(x) & (isnan(y) | signless(x
isless(x::Real, y::AbstractFloat) = (!isnan(x) & (isnan(y) | signless(x, y))) | (x < y)
isless(x::AbstractFloat, y::Real ) = (!isnan(x) & (isnan(y) | signless(x, y))) | (x < y)

# Performance optimization to reduce branching
# This is useful for sorting tuples of integers
# TODO: remove this when the compiler can optimize the generic version better
# See #48724 and #48753
isless(a::Tuple{BitInteger, BitInteger}, b::Tuple{BitInteger, BitInteger}) =
isless(a[1], b[1]) | (isequal(a[1], b[1]) & isless(a[2], b[2]))

"""
isgreater(x, y)
Expand Down
17 changes: 17 additions & 0 deletions test/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,23 @@ end

@test isless('a','b')

@testset "isless on pairs of integers (because there is a fastpath)" begin
@test isless((1,2), (1,3))
@test isless((0,-2), (0,2))
@test isless((-1,2), (1,2))
@test isless((-1,-2), (1,2))
@test !isless((1,3), (1,2))
@test !isless((0,2), (0,-2))
@test !isless((1,2), (-1,2))
@test !isless((1,2), (-1,-2))
@test !isless((-1,-2), (-1,-2))

@test isless((typemin(Int), typemin(Int)), (0,0))
@test isless((1, 1), (Int8(2), Int8(2)))
@test !isless((UInt8(200),Int8(-1)), (UInt8(200),Int8(-1)))
@test isless((1, 1), (1, unsigned(2)))
end

@testset "isgreater" begin
# isgreater should be compatible with min.
min1(a, b) = Base.isgreater(a, b) ? b : a
Expand Down

0 comments on commit cf20620

Please sign in to comment.