0.8.8.23:
[sbcl/lichteblau.git] / src / code / target-sxhash.lisp
blob99e0830773f2a122a16aa0f5d947ce999e36a60e
1 ;;;; hashing functions
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
12 (in-package "SB!IMPL")
14 ;;; the depthoid explored when calculating hash values
15 ;;;
16 ;;; "Depthoid" here is a sort of mixture of what Common Lisp ordinarily calls
17 ;;; depth and what Common Lisp ordinarily calls length; it's incremented either
18 ;;; when we descend into a compound object or when we step through elements of
19 ;;; a compound object.
20 (defconstant +max-hash-depthoid+ 4)
22 ;;;; mixing hash values
24 ;;; a function for mixing hash values
25 ;;;
26 ;;; desiderata:
27 ;;; * Non-commutativity keeps us from hashing e.g. #(1 5) to the
28 ;;; same value as #(5 1), and ending up in real trouble in some
29 ;;; special cases like bit vectors the way that CMUCL 18b SXHASH
30 ;;; does. (Under CMUCL 18b, SXHASH of any bit vector is 1..)
31 ;;; * We'd like to scatter our hash values over the entire possible range
32 ;;; of values instead of hashing small or common key values (like
33 ;;; 2 and NIL and #\a) to small FIXNUMs the way that the CMUCL 18b
34 ;;; SXHASH function does, again helping to avoid pathologies like
35 ;;; hashing all bit vectors to 1.
36 ;;; * We'd like this to be simple and fast, too.
37 ;;;
38 ;;; FIXME: Should this be INLINE?
39 (declaim (ftype (function ((and fixnum unsigned-byte)
40 (and fixnum unsigned-byte))
41 (and fixnum unsigned-byte)) mix))
42 (defun mix (x y)
43 ;; FIXME: We wouldn't need the nasty (SAFETY 0) here if the compiler
44 ;; were smarter about optimizing ASH. (Without the THE FIXNUM below,
45 ;; and the (SAFETY 0) declaration here to get the compiler to trust
46 ;; it, the sbcl-0.5.0m cross-compiler running under Debian
47 ;; cmucl-2.4.17 turns the ASH into a full call, requiring the
48 ;; UNSIGNED-BYTE 32 argument to be coerced to a bignum, requiring
49 ;; consing, and thus generally obliterating performance.)
50 (declare (optimize (speed 3) (safety 0)))
51 (declare (type (and fixnum unsigned-byte) x y))
52 ;; the ideas here:
53 ;; * Bits diffuse in both directions (shifted left by up to 2 places
54 ;; in the calculation of XY, and shifted right by up to 5 places
55 ;; by the ASH).
56 ;; * The #'+ and #'LOGXOR operations don't commute with each other,
57 ;; so different bit patterns are mixed together as they shift
58 ;; past each other.
59 ;; * The arbitrary constant in the #'LOGXOR expression is intended
60 ;; to help break up any weird anomalies we might otherwise get
61 ;; when hashing highly regular patterns.
62 ;; (These are vaguely like the ideas used in many cryptographic
63 ;; algorithms, but we're not pushing them hard enough here for them
64 ;; to be cryptographically strong.)
65 (let* ((xy (+ (* x 3) y)))
66 (logand most-positive-fixnum
67 (logxor 441516657
69 (ash xy -5)))))
71 ;;;; hashing strings
72 ;;;;
73 ;;;; Note that this operation is used in compiler symbol table
74 ;;;; lookups, so we'd like it to be fast.
75 ;;;;
76 ;;;; As of 2004-03-10, we implement the one-at-a-time algorithm
77 ;;;; designed by Bob Jenkins (see
78 ;;;; <http://burtleburtle.net/bob/hash/doobs.html> for some more
79 ;;;; information).
81 #!-sb-fluid (declaim (inline %sxhash-substring))
82 (defun %sxhash-substring (string &optional (count (length string)))
83 ;; FIXME: As in MIX above, we wouldn't need (SAFETY 0) here if the
84 ;; cross-compiler were smarter about ASH, but we need it for
85 ;; sbcl-0.5.0m. (probably no longer true? We might need SAFETY 0
86 ;; to elide some type checks, but then again if this is inlined in
87 ;; all the critical places, we might not -- CSR, 2004-03-10)
88 (declare (optimize (speed 3) (safety 0)))
89 (declare (type string string))
90 (declare (type index count))
91 (let ((result 0))
92 (declare (type (unsigned-byte 32) result))
93 (unless (typep string '(vector nil))
94 (dotimes (i count)
95 (declare (type index i))
96 (setf result
97 (ldb (byte 32 0)
98 (+ result (char-code (aref string i)))))
99 (setf result
100 (ldb (byte 32 0)
101 (+ result (ash result 10))))
102 (setf result
103 (logxor result (ash result -6)))))
104 (setf result
105 (ldb (byte 32 0)
106 (+ result (ash result 3))))
107 (setf result
108 (logxor result (ash result -11)))
109 (setf result
110 (ldb (byte 32 0)
111 (logxor result (ash result 15))))
112 (logand result most-positive-fixnum)))
113 ;;; test:
114 ;;; (let ((ht (make-hash-table :test 'equal)))
115 ;;; (do-all-symbols (symbol)
116 ;;; (let* ((string (symbol-name symbol))
117 ;;; (hash (%sxhash-substring string)))
118 ;;; (if (gethash hash ht)
119 ;;; (unless (string= (gethash hash ht) string)
120 ;;; (format t "collision: ~S ~S~%" string (gethash hash ht)))
121 ;;; (setf (gethash hash ht) string))))
122 ;;; (format t "final count=~W~%" (hash-table-count ht)))
124 (defun %sxhash-simple-string (x)
125 (declare (optimize speed))
126 (declare (type simple-string x))
127 ;; KLUDGE: this FLET is a workaround (suggested by APD) for presence
128 ;; of let conversion in the cross compiler, which otherwise causes
129 ;; strongly suboptimal register allocation.
130 (flet ((trick (x)
131 (%sxhash-substring x)))
132 (declare (notinline trick))
133 (trick x)))
135 (defun %sxhash-simple-substring (x count)
136 (declare (optimize speed))
137 (declare (type simple-string x))
138 (declare (type index count))
139 ;; see comment in %SXHASH-SIMPLE-STRING
140 (flet ((trick (x count)
141 (%sxhash-substring x count)))
142 (declare (notinline trick))
143 (trick x count)))
145 ;;;; the SXHASH function
147 (defun sxhash (x)
148 ;; profiling SXHASH is hard, but we might as well try to make it go
149 ;; fast, in case it is the bottleneck somwhere. -- CSR, 2003-03-14
150 (declare (optimize speed))
151 (labels ((sxhash-number (x)
152 (etypecase x
153 (fixnum (sxhash x)) ; through DEFTRANSFORM
154 (integer (sb!bignum:sxhash-bignum x))
155 (single-float (sxhash x)) ; through DEFTRANSFORM
156 (double-float (sxhash x)) ; through DEFTRANSFORM
157 #!+long-float (long-float (error "stub: no LONG-FLOAT"))
158 (ratio (let ((result 127810327))
159 (declare (type fixnum result))
160 (mixf result (sxhash-number (numerator x)))
161 (mixf result (sxhash-number (denominator x)))
162 result))
163 (complex (let ((result 535698211))
164 (declare (type fixnum result))
165 (mixf result (sxhash-number (realpart x)))
166 (mixf result (sxhash-number (imagpart x)))
167 result))))
168 (sxhash-recurse (x &optional (depthoid +max-hash-depthoid+))
169 (declare (type index depthoid))
170 (typecase x
171 (cons
172 (if (plusp depthoid)
173 (mix (sxhash-recurse (car x) (1- depthoid))
174 (sxhash-recurse (cdr x) (1- depthoid)))
175 261835505))
176 (instance
177 (if (or (typep x 'structure-object) (typep x 'condition))
178 (logxor 422371266
179 (sxhash ; through DEFTRANSFORM
180 (classoid-name
181 (layout-classoid (%instance-layout x)))))
182 (sxhash-instance x)))
183 (symbol (sxhash x)) ; through DEFTRANSFORM
184 (array
185 (typecase x
186 (simple-string (sxhash x)) ; through DEFTRANSFORM
187 (string (%sxhash-substring x))
188 (simple-bit-vector (sxhash x)) ; through DEFTRANSFORM
189 (bit-vector
190 ;; FIXME: It must surely be possible to do better
191 ;; than this. The problem is that a non-SIMPLE
192 ;; BIT-VECTOR could be displaced to another, with a
193 ;; non-zero offset -- so that significantly more
194 ;; work needs to be done using the %RAW-BITS
195 ;; approach. This will probably do for now.
196 (sxhash-recurse (copy-seq x) depthoid))
197 (t (logxor 191020317 (sxhash (array-rank x))))))
198 (character
199 (logxor 72185131
200 (sxhash (char-code x)))) ; through DEFTRANSFORM
201 ;; general, inefficient case of NUMBER
202 (number (sxhash-number x))
203 (generic-function (sxhash-instance x))
204 (t 42))))
205 (sxhash-recurse x)))
207 ;;;; the PSXHASH function
209 ;;;; FIXME: This code does a lot of unnecessary full calls. It could be made
210 ;;;; more efficient (in both time and space) by rewriting it along the lines
211 ;;;; of the SXHASH code above.
213 ;;; like SXHASH, but for EQUALP hashing instead of EQUAL hashing
214 (defun psxhash (key &optional (depthoid +max-hash-depthoid+))
215 (declare (optimize speed))
216 (declare (type (integer 0 #.+max-hash-depthoid+) depthoid))
217 ;; Note: You might think it would be cleaner to use the ordering given in the
218 ;; table from Figure 5-13 in the EQUALP section of the ANSI specification
219 ;; here. So did I, but that is a snare for the unwary! Nothing in the ANSI
220 ;; spec says that HASH-TABLE can't be a STRUCTURE-OBJECT, and in fact our
221 ;; HASH-TABLEs *are* STRUCTURE-OBJECTs, so we need to pick off the special
222 ;; HASH-TABLE behavior before we fall through to the generic STRUCTURE-OBJECT
223 ;; comparison behavior.
224 (typecase key
225 (array (array-psxhash key depthoid))
226 (hash-table (hash-table-psxhash key))
227 (structure-object (structure-object-psxhash key depthoid))
228 (cons (list-psxhash key depthoid))
229 (number (number-psxhash key))
230 (character (sxhash (char-upcase key)))
231 (t (sxhash key))))
233 (defun array-psxhash (key depthoid)
234 (declare (optimize speed))
235 (declare (type array key))
236 (declare (type (integer 0 #.+max-hash-depthoid+) depthoid))
237 (typecase key
238 ;; VECTORs have to be treated specially because ANSI specifies
239 ;; that we must respect fill pointers.
240 (vector
241 (macrolet ((frob ()
242 '(let ((result 572539))
243 (declare (type fixnum result))
244 (mixf result (length key))
245 (dotimes (i (min depthoid (length key)))
246 (declare (type fixnum i))
247 (mixf result
248 (psxhash (aref key i)
249 (- depthoid 1 i))))
250 result)))
251 ;; CMU can compile SIMPLE-ARRAY operations so much more efficiently
252 ;; than the general case that it's probably worth picking off the
253 ;; common special cases.
254 (typecase key
255 (simple-string
256 ;;(format t "~&SIMPLE-STRING special case~%")
257 (frob))
258 (simple-vector
259 ;;(format t "~&SIMPLE-VECTOR special case~%")
260 (frob))
261 (t (frob)))))
262 ;; Any other array can be hashed by working with its underlying
263 ;; one-dimensional physical representation.
265 (let ((result 60828))
266 (declare (type fixnum result))
267 (dotimes (i (min depthoid (array-rank key)))
268 (mixf result (array-dimension key i)))
269 (dotimes (i (min depthoid (array-total-size key)))
270 (mixf result
271 (psxhash (row-major-aref key i)
272 (- depthoid 1 i))))
273 result))))
275 (defun structure-object-psxhash (key depthoid)
276 (declare (optimize speed))
277 (declare (type structure-object key))
278 (declare (type (integer 0 #.+max-hash-depthoid+) depthoid))
279 (let* ((layout (%instance-layout key)) ; i.e. slot #0
280 (length (layout-length layout))
281 (classoid (layout-classoid layout))
282 (name (classoid-name classoid))
283 (result (mix (sxhash name) (the fixnum 79867))))
284 (declare (type fixnum result))
285 (dotimes (i (min depthoid (1- length)))
286 (declare (type fixnum i))
287 (let ((j (1+ i))) ; skipping slot #0, which is for LAYOUT
288 (declare (type fixnum j))
289 (mixf result
290 (psxhash (%instance-ref key j)
291 (1- depthoid)))))
292 result))
294 (defun list-psxhash (key depthoid)
295 (declare (optimize speed))
296 (declare (type list key))
297 (declare (type (integer 0 #.+max-hash-depthoid+) depthoid))
298 (cond ((null key)
299 (the fixnum 480929))
300 ((zerop depthoid)
301 (the fixnum 779578))
303 (mix (psxhash (car key) (1- depthoid))
304 (psxhash (cdr key) (1- depthoid))))))
306 (defun hash-table-psxhash (key)
307 (declare (optimize speed))
308 (declare (type hash-table key))
309 (let ((result 103924836))
310 (declare (type fixnum result))
311 (mixf result (hash-table-count key))
312 (mixf result (sxhash (hash-table-test key)))
313 result))
315 (defun number-psxhash (key)
316 (declare (optimize speed))
317 (declare (type number key))
318 (flet ((sxhash-double-float (val)
319 (declare (type double-float val))
320 ;; FIXME: Check to make sure that the DEFTRANSFORM kicks in and the
321 ;; resulting code works without consing. (In Debian cmucl 2.4.17,
322 ;; it didn't.)
323 (sxhash val)))
324 (etypecase key
325 (integer (sxhash key))
326 (float (macrolet ((frob (type)
327 (let ((lo (coerce most-negative-fixnum type))
328 (hi (coerce most-positive-fixnum type)))
329 `(cond (;; This clause allows FIXNUM-sized integer
330 ;; values to be handled without consing.
331 (<= ,lo key ,hi)
332 (multiple-value-bind (q r)
333 (floor (the (,type ,lo ,hi) key))
334 (if (zerop (the ,type r))
335 (sxhash q)
336 (sxhash-double-float
337 (coerce key 'double-float)))))
339 (multiple-value-bind (q r) (floor key)
340 (if (zerop (the ,type r))
341 (sxhash q)
342 (sxhash-double-float
343 (coerce key 'double-float)))))))))
344 (etypecase key
345 (single-float (frob single-float))
346 (double-float (frob double-float))
347 #!+long-float
348 (long-float (error "LONG-FLOAT not currently supported")))))
349 (rational (if (and (<= most-negative-double-float
351 most-positive-double-float)
352 (= (coerce key 'double-float) key))
353 (sxhash-double-float (coerce key 'double-float))
354 (sxhash key)))
355 (complex (if (zerop (imagpart key))
356 (number-psxhash (realpart key))
357 (let ((result 330231))
358 (declare (type fixnum result))
359 (mixf result (number-psxhash (realpart key)))
360 (mixf result (number-psxhash (imagpart key)))
361 result))))))