Skip to content

More hash optimization #50041

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

Merged
merged 3 commits into from
Jun 7, 2023
Merged
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
48 changes: 29 additions & 19 deletions base/float.jl
Original file line number Diff line number Diff line change
Expand Up @@ -635,31 +635,40 @@ isinf(x::Real) = !isnan(x) & !isfinite(x)
isinf(x::IEEEFloat) = abs(x) === oftype(x, Inf)

const hx_NaN = hash_uint64(reinterpret(UInt64, NaN))
let Tf = Float64, Tu = UInt64, Ti = Int64
@eval function hash(x::$Tf, h::UInt)
# see comments on trunc and hash(Real, UInt)
if $(Tf(typemin(Ti))) <= x < $(Tf(typemax(Ti)))
xi = fptosi($Ti, x)
if isequal(xi, x)
return hash(xi, h)
end
elseif $(Tf(typemin(Tu))) <= x < $(Tf(typemax(Tu)))
xu = fptoui($Tu, x)
if isequal(xu, x)
return hash(xu, h)
end
elseif isnan(x)
return hx_NaN ⊻ h # NaN does not have a stable bit pattern
function hash(x::Float64, h::UInt)
# see comments on trunc and hash(Real, UInt)
if typemin(Int64) <= x < typemax(Int64)
xi = fptosi(Int64, x)
if isequal(xi, x)
return hash(xi, h)
end
elseif typemin(UInt64) <= x < typemax(UInt64)
xu = fptoui(UInt64, x)
if isequal(xu, x)
return hash(xu, h)
end
return hash_uint64(bitcast(UInt64, x)) - 3h
elseif isnan(x)
return hx_NaN ⊻ h # NaN does not have a stable bit pattern
end
return hash_uint64(bitcast(UInt64, x)) - 3h
end

hash(x::Float32, h::UInt) = hash(Float64(x), h)
hash(x::Float16, h::UInt) = hash(Float64(x), h)

## generic hashing for rational values ##
function hash(x::Float16, h::UInt)
# see comments on trunc and hash(Real, UInt)
if isfinite(x) # all finite Float16 fit in Int64
xi = fptosi(Int64, x)
if isequal(xi, x)
return hash(xi, h)
end
elseif isnan(x)
return hx_NaN ⊻ h # NaN does not have a stable bit pattern
end
return hash_uint64(bitcast(UInt64, Float64(x))) - 3h
end

## generic hashing for rational values ##
function hash(x::Real, h::UInt)
# decompose x as num*2^pow/den
num, pow, den = decompose(x)
Expand Down Expand Up @@ -691,10 +700,11 @@ function hash(x::Real, h::UInt)
end # typemin(Int64) handled by Float64 case
left <= 1024 && left - right <= 53 && return hash(ldexp(Float64(num), pow), h)
end
else
h = hash_integer(den, h)
end

# handle generic rational values
h = hash_integer(den, h)
h = hash_integer(pow, h)
h = hash_integer(num, h)
return h
Expand Down
1 change: 0 additions & 1 deletion base/gmp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,6 @@ if Limb === UInt64 === UInt
if nd <= 1024 && nd - pow <= 53
return hash(ldexp(flipsign(Float64(limb), sz), pow), h)
end
h = hash_integer(1, h)
h = hash_integer(pow, h)
h ⊻= hash_uint(flipsign(limb, sz) ⊻ h)
for idx = idx+1:asz
Expand Down
9 changes: 6 additions & 3 deletions base/rational.jl
Original file line number Diff line number Diff line change
Expand Up @@ -545,11 +545,14 @@ function hash(x::Rational{<:BitInteger64}, h::UInt)
pow = trailing_zeros(den)
den >>= pow
pow = -pow
if den == 1 && uabs(num) < UInt64(maxintfloat(Float64))
return hash(ldexp(Float64(num),pow),h)
if den == 1
if uabs(num) < UInt64(maxintfloat(Float64))
return hash(ldexp(Float64(num),pow),h)
end
else
h = hash_integer(den, h)
end
end
h = hash_integer(den, h)
h = hash_integer(pow, h)
h = hash_integer(num, h)
return h
Expand Down