Skip to content

Commit

Permalink
hash_uint: simplify definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanKarpinski committed Jun 5, 2020
1 parent 8c8f7a6 commit d27be66
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 42 deletions.
2 changes: 1 addition & 1 deletion base/char.jl
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ in(x::AbstractChar, y::AbstractChar) = x == y
==(x::Char, y::Char) = reinterpret(UInt32, x) == reinterpret(UInt32, y)
isless(x::Char, y::Char) = reinterpret(UInt32, x) < reinterpret(UInt32, y)
hash(x::Char, h::UInt) =
hash_uint64(((reinterpret(UInt32, x) + UInt64(0xd4d64234)) << 32) UInt64(h))
hash_uint(((reinterpret(UInt32, x) + UInt64(0xd4d64234)) << 32) UInt64(h))

first_utf8_byte(c::Char) = (reinterpret(UInt32, c) >> 24) % UInt8

Expand Down
2 changes: 1 addition & 1 deletion base/float.jl
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ isinf(x::Real) = !isnan(x) & !isfinite(x)

## hashing small, built-in numeric types ##

hx(a::UInt64, b::Float64, h::UInt) = hash_uint64((3a + reinterpret(UInt64,b)) - h)
hx(a::UInt64, b::Float64, h::UInt) = hash_uint((3a + reinterpret(UInt64,b)) - h)
const hx_NaN = hx(UInt64(0), NaN, UInt(0 ))

hash(x::UInt64, h::UInt) = hx(x, Float64(x), h)
Expand Down
45 changes: 5 additions & 40 deletions base/hashing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,46 +24,11 @@ hash(@nospecialize(x), h::UInt) = hash_uint(3h - objectid(x))

## core data hashing functions ##

function hash_64_64(n::UInt64)
a::UInt64 = n
a = ~a + a << 21
a = a a >> 24
a = a + a << 3 + a << 8
a = a a >> 14
a = a + a << 2 + a << 4
a = a a >> 28
a = a + a << 31
return a
end

function hash_64_32(n::UInt64)
a::UInt64 = n
a = ~a + a << 18
a = a a >> 31
a = a * 21
a = a a >> 11
a = a + a << 6
a = a a >> 22
return a % UInt32
end

function hash_32_32(n::UInt32)
a::UInt32 = n
a = a + 0x7ed55d16 + a << 12
a = a 0xc761c23c a >> 19
a = a + 0x165667b1 + a << 5
a = a + 0xd3a2646c a << 9
a = a + 0xfd7046c5 + a << 3
a = a 0xb55a4f09 a >> 16
return a
end

if UInt === UInt64
hash_uint64(x::UInt64) = hash_64_64(x)
hash_uint(x::UInt) = hash_64_64(x)
else
hash_uint64(x::UInt64) = hash_64_32(x)
hash_uint(x::UInt) = hash_32_32(x)
function hash_uint(n::T) where {T <: Union{UInt64, UInt32}}
# random constants, last must be odd
n += 0xe391e8b4ff155ee5 % T
n ⊻= 0xb9d8ebf206d49927 % T
n *= 0xafa64689f53d9ee1 % T
end

## symbol & expression hashing ##
Expand Down

0 comments on commit d27be66

Please sign in to comment.