From ebb267a75a8ec3b65dad0db616dffe80244f76c7 Mon Sep 17 00:00:00 2001 From: Jameson Nash Date: Thu, 15 Oct 2020 00:35:27 -0400 Subject: [PATCH] fixup! faster hashing by avoiding UB --- base/abstractset.jl | 4 ++-- base/float.jl | 4 ++-- test/hashing.jl | 1 + test/show.jl | 2 +- 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/base/abstractset.jl b/base/abstractset.jl index 05b5300952822..179b9f7be5d4b 100644 --- a/base/abstractset.jl +++ b/base/abstractset.jl @@ -64,10 +64,10 @@ julia> union!(a, 1:2:8); julia> a Set{Int64} with 5 elements: - 7 + 5 4 + 7 3 - 5 1 ``` """ diff --git a/base/float.jl b/base/float.jl index 40fe8ff781d44..edd729fe30b4f 100644 --- a/base/float.jl +++ b/base/float.jl @@ -563,12 +563,12 @@ let Tf = Float64, Tu = UInt64, Ti = Int64 # see comments on trunc and hash(Real, UInt) if $(Tf(typemin(Ti))) <= x < $(Tf(typemax(Ti))) xi = fptosi($Ti, x) - if xi == x + if isequal(xi, x) return hash(xi, h) end elseif $(Tf(typemin(Tu))) <= x < $(Tf(typemax(Tu))) xu = fptoui($Tu, x) - if xu == x + if isequal(xu, x) return hash(xu, h) end elseif isnan(x) diff --git a/test/hashing.jl b/test/hashing.jl index 8b7b21aa1b7d9..829317238afeb 100644 --- a/test/hashing.jl +++ b/test/hashing.jl @@ -46,6 +46,7 @@ for T = types, S = types, x = vals #println("$(typeof(b)) $b") @test isequal(a, b) == (hash(a) == hash(b)) end +@test hash(0.0) != hash(-0.0) # issue #8619 @test hash(nextfloat(2.0^63)) == hash(UInt64(nextfloat(2.0^63))) diff --git a/test/show.jl b/test/show.jl index 3945bef2215d5..472b6ef63a128 100644 --- a/test/show.jl +++ b/test/show.jl @@ -1608,7 +1608,7 @@ end # issue #27680 @test showstr(Set([(1.0,1.0), (2.0,2.0), (3.0, 3.0)])) == (sizeof(Int) == 8 ? - "Set([(3.0, 3.0), (2.0, 2.0), (1.0, 1.0)])" : + "Set([(1.0, 1.0), (3.0, 3.0), (2.0, 2.0)])" : "Set([(1.0, 1.0), (2.0, 2.0), (3.0, 3.0)])") # issue #27747