Skip to content

Commit 13a142d

Browse files
authored
fix Big hashing (#58377)
1 parent 6a796e0 commit 13a142d

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

base/gmp.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -848,17 +848,17 @@ if Limb === UInt64 === UInt
848848
# an optimized version for BigInt of hash_integer (used e.g. for Rational{BigInt}),
849849
# and of hash
850850

851-
using .Base: hash_uint
851+
using .Base: hash_finalizer
852852

853853
function hash_integer(n::BigInt, h::UInt)
854854
GC.@preserve n begin
855855
s = n.size
856856
s == 0 && return hash_integer(0, h)
857857
p = convert(Ptr{UInt64}, n.d)
858858
b = unsafe_load(p)
859-
h ⊻= hash_uint(ifelse(s < 0, -b, b) h)
859+
h ⊻= hash_finalizer(ifelse(s < 0, -b, b) h)
860860
for k = 2:abs(s)
861-
h ⊻= hash_uint(unsafe_load(p, k) h)
861+
h ⊻= hash_finalizer(unsafe_load(p, k) h)
862862
end
863863
return h
864864
end

test/gmp.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -808,3 +808,11 @@ t = Rational{BigInt}(0, 1)
808808
@test Base.GMP.MPQ.div!(-oo, zo) == -oz
809809
end
810810
end
811+
812+
@testset "hashing" begin
813+
for i in 1:10:100
814+
bint = big(11)^i
815+
bfloat = big(11.0)^i
816+
@test (hash(bint) == hash(bfloat)) == (bint == bfloat)
817+
end
818+
end

0 commit comments

Comments
 (0)