@@ -27,130 +27,18 @@ function hash_uint(n::Uint32)
2727 return a
2828end
2929
30- # # efficient value-based hashing of integers ##
31-
32- function hash_integer (n:: Integer , h:: Uint )
33- h = hash_uint (uint (n & typemax (Uint)) $ h) $ h
34- n = ifelse (n < 0 , oftype (n,- n), n)
35- n >>>= sizeof (Uint) << 3
36- while n != 0
37- h = hash_uint (uint (n & typemax (Uint)) $ h) $ h
38- n >>>= sizeof (Uint) << 3
39- end
40- return h
41- end
42-
43- # # hashing rational values ##
44-
45- #=
46- `decompose(x)`: non-canonical decomposition of rational values as `num*2^pow/den`.
47-
48- The decompose function is the point where rational-valued numeric types that support
49- hashing hook into the hashing protocol. `decompose(x)` should return three integer
50- values `num, pow, den`, such that the value of `x` is mathematically equal to
51-
52- num*2^pow/den
53-
54- The decomposition need not be canonical in the sense that it just needs to be *some*
55- way to express `x` in this form, not any particular way – with the restriction that
56- `num` and `den` may not share any odd common factors. They may, however, have powers
57- of two in common – the generic hashing code will normalize those as necessary.
58-
59- Special values:
60-
61- - `x` is zero: `num` should be zero and `den` should have the same sign as `x`
62- - `x` is infinite: `den` should be zero and `num` should have the same sign as `x`
63- - `x` is not a number: `num` and `den` should both be zero
64- =#
65-
66- decompose (x:: Integer ) = x, 0 , 1
67- decompose (x:: Rational ) = num (x), 0 , den (x)
68-
69- function decompose (x:: Float32 )
70- isnan (x) && return 0 , 0 , 0
71- isinf (x) && return ifelse (x < 0 , - 1 , 1 ), 0 , 0
72- n = reinterpret (Int32, x)
73- s = int32 (n & 0x007fffff )
74- e = int32 (n & 0x7f800000 >> 23 )
75- s |= int32 (e != 0 ) << 23
76- d = ifelse (signbit (n) == 1 , - 1 , 1 )
77- int (s), int (e - 150 + (e == 0 )), d
78- end
79-
80- function decompose (x:: Float64 )
81- isnan (x) && return 0 , 0 , 0
82- isinf (x) && return ifelse (x < 0 , - 1 , 1 ), 0 , 0
83- n = reinterpret (Int64, x)
84- s = int64 (n & 0x000fffffffffffff )
85- e = int64 (n & 0x7ff0000000000000 >> 52 )
86- s |= int64 (e != 0 ) << 52
87- d = ifelse (signbit (n) == 1 , - 1 , 1 )
88- int (s), int (e - 1075 + (e == 0 )), d
89- end
90-
91- # hashing methods for rational-valued types
30+ # # hashing small, built-in numeric types ##
9231
9332hx (a:: Uint64 , b:: Float64 , h:: Uint ) = hash_uint ((3 a + reinterpret (Uint64,b)) - h)
9433
9534hash (x:: Uint64 , h:: Uint ) = hx (x, float64 (x), h)
9635hash (x:: Int64 , h:: Uint ) = hx (reinterpret (Uint64,x), float64 (x), h)
97- hash (x:: Float64 , h:: Uint ) = hx (box (Uint64,fptosi (unbox (Float64,x))), ifelse (x == x,x, NaN ), h)
36+ hash (x:: Float64 , h:: Uint ) = hx (box (Uint64,fptosi (unbox (Float64,x))), ifelse (isnan (x), NaN , x ), h)
9837
9938hash (x:: Union(Int8,Uint8,Int16,Uint16,Int32,Uint32) , h:: Uint ) = hash (int64 (x), h)
10039hash (x:: Float32 , h:: Uint ) = hash (float64 (x), h)
10140
102- function hash (x:: Real , h:: Uint )
103- # decompose x as num*2^pow/den
104- num, pow, den = decompose (x):: (Integer,Integer,Integer)
105-
106- # handle special values
107- num == 0 && den == 0 && return hash (NaN , h)
108- if num == 0
109- den > 0 && return hash (+ 0.0 , h)
110- den < 0 && return hash (- 0.0 , h)
111- end
112- if den == 0
113- num > 0 && return hash (+ Inf , h)
114- num < 0 && return hash (- Inf , h)
115- end
116-
117- # normalize decomposition
118- if den < 0
119- num = - num
120- den = - den
121- end
122- z = trailing_zeros (num)
123- if z != 0
124- num >>= z
125- pow += z
126- end
127- z = trailing_zeros (den)
128- if z != 0
129- den >>= z
130- pow -= z
131- end
132-
133- # handle values representable as Int64, Uint64, Float64
134- if den == 1
135- left = ndigits0z (num,2 ) + pow
136- right = trailing_zeros (num) + pow
137- if - 1074 <= right
138- if 0 <= right && left <= 64
139- left <= 63 && return hash (int64 (num) << int (pow), h)
140- signbit (num) == signbit (den) && return hash (uint64 (num) << int (pow), h)
141- end
142- left <= 1024 && left - right <= 53 && return hash (float64 (num) * 2.0 ^ pow, h)
143- end
144- end
145-
146- # handle "generic" real values
147- h = hash_integer (den, h)
148- h = hash_integer (pow, h)
149- h = hash_integer (num, h)
150- return h
151- end
152-
153- # # hashing complex values ##
41+ # # hashing complex numbers ##
15442
15543const h_imag = 0x32a7a07f3e7cd1f9
15644const hash_0_imag = hash (0 , h_imag)
16654hash (x:: Bool , h:: Uint ) = hash (int (x), h + 0x4cd135a1755139a5 )
16755hash (x:: Char , h:: Uint ) = hash (int (x), h + 0x10f989ff0f886f11 )
16856
169- # # expression hashing ##
57+ # # symbol & expression hashing ##
17058
17159hash (x:: Symbol , h:: Uint ) = hash (object_id (x), h)
17260hash (x:: Expr , h:: Uint ) = hash (x. args, hash (x. head, h + 0x83c7900696d26dc6 ))
0 commit comments