From d27be66ec5acf0ae5eeef8ee8c0bb4de66f96257 Mon Sep 17 00:00:00 2001 From: Stefan Karpinski Date: Tue, 2 Jun 2020 13:50:49 -0400 Subject: [PATCH] hash_uint: simplify definitions --- base/char.jl | 2 +- base/float.jl | 2 +- base/hashing.jl | 45 +++++---------------------------------------- 3 files changed, 7 insertions(+), 42 deletions(-) diff --git a/base/char.jl b/base/char.jl index 173c84711e551..86c2b712894b7 100644 --- a/base/char.jl +++ b/base/char.jl @@ -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 diff --git a/base/float.jl b/base/float.jl index 42b298ebec595..f800bcc319da3 100644 --- a/base/float.jl +++ b/base/float.jl @@ -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) diff --git a/base/hashing.jl b/base/hashing.jl index 7b962a58ad5ad..7918ffb732fe0 100644 --- a/base/hashing.jl +++ b/base/hashing.jl @@ -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 ##