Skip to content

Commit

Permalink
simplify and optimize hash(::Real) further
Browse files Browse the repository at this point in the history
  • Loading branch information
Lilith Hafner authored and Lilith Hafner committed May 30, 2023
1 parent 71a0974 commit e4ec12c
Showing 1 changed file with 10 additions and 17 deletions.
27 changes: 10 additions & 17 deletions base/float.jl
Original file line number Diff line number Diff line change
Expand Up @@ -674,34 +674,27 @@ function hash(x::Real, h::UInt)
num = -num
den = -den
end
z = trailing_zeros(num)
if z != 0
num >>= z
pow += z
end
z = trailing_zeros(den)
if z != 0
den >>= z
pow -= z
end
num_z = trailing_zeros(num)
den_z = trailing_zeros(den)
pow += num_z - den_z

# handle values representable as Int64, UInt64, Float64
if den == 1
left = top_set_bit(abs(num)) + pow
right = trailing_zeros(num) + pow
right = pow
if -1074 <= right
if 0 <= right && left <= 64
left <= 63 && return hash(Int64(num) << Int(pow), h)
signbit(num) == signbit(den) && return hash(UInt64(num) << Int(pow), h)
if 0 <= right
left <= 63 && return hash(Int64(num) << Int(den_z), h)
left <= 64 && !signbit(num) && return hash(UInt64(num) << Int(den_z), h)
end # typemin(Int64) handled by Float64 case
left <= 1024 && left - right <= 53 && return hash(ldexp(Float64(num),pow), h)
left <= 1024 && left - right <= 53 && return hash(ldexp(Float64(num), den_z), h)
end
end

# handle generic rational values
h = hash_integer(den, h)
h = hash_integer(den >> den_z, h)
h = hash_integer(pow, h)
h = hash_integer(num, h)
h = hash_integer(num >> num_z, h)
return h
end

Expand Down

0 comments on commit e4ec12c

Please sign in to comment.