abe819914608f56f306eb9b8aca75add0e8ca4c1
[sbcl.git] / src / code / sxhash.lisp
1 ;;;; that part of SXHASH logic which runs not only in the target Lisp but
2 ;;;; in the cross-compilation host Lisp
3
4 ;;;; This software is part of the SBCL system. See the README file for
5 ;;;; more information.
6 ;;;;
7 ;;;; This software is derived from the CMU CL system, which was
8 ;;;; written at Carnegie Mellon University and released into the
9 ;;;; public domain. The software is in the public domain and is
10 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
11 ;;;; files for more information.
12
13 (in-package "SB!C")
14
15 (sb!xc:define-modify-macro mixf (y) mix)
16
17 ;;; SXHASH of FLOAT values is defined directly in terms of DEFTRANSFORM in
18 ;;; order to avoid boxing.
19 (deftransform sxhash ((x) (single-float))
20   '(let* ((val (+ 0.0f0 x))
21           (bits (logand (single-float-bits val) #.(1- (ash 1 32)))))
22      (logxor 66194023
23              (sxhash (the fixnum
24                           (logand most-positive-fixnum
25                                   (logxor bits
26                                           (ash bits -7))))))))
27 (deftransform sxhash ((x) (double-float))
28   '(let* ((val (+ 0.0d0 x))
29           (hi (logand (double-float-high-bits val) #.(1- (ash 1 32))))
30           (lo (double-float-low-bits val))
31           (hilo (logxor hi lo)))
32      (logxor 475038542
33              (sxhash (the fixnum
34                           (logand most-positive-fixnum
35                                   (logxor hilo
36                                           (ash hilo -7))))))))
37
38 ;;; SXHASH of FIXNUM values is defined as a DEFTRANSFORM because it's so
39 ;;; simple.
40 (deftransform sxhash ((x) (fixnum))
41   '(logand most-positive-fixnum
42            (logxor (ash (logand x (ash most-positive-fixnum -4)) 4)
43                    (logand (ash x -1) most-positive-fixnum) ; to get sign bit into hash
44                    361475658)))
45
46 ;;; SXHASH of SIMPLE-BIT-VECTOR values is defined as a DEFTRANSFORM
47 ;;; because it is endian-dependent.
48 (deftransform sxhash ((x) (simple-bit-vector))
49   `(let ((result 410823708))
50     (declare (type fixnum result))
51     (let ((length (length x)))
52       (cond
53         ((= length 0) (mix result (sxhash 0)))
54         (t
55          (mixf result (sxhash (length x)))
56          (do* ((i 0 (+ i 1))
57                ;; FIXME: should we respect DEPTHOID?  SXHASH on
58                ;; strings doesn't seem to...
59                (end-1 (floor (1- length) sb!vm:n-word-bits)))
60               ((= i end-1)
61                (let ((num
62                       (logand
63                        (ash (1- (ash 1 (mod length sb!vm:n-word-bits)))
64                             ,(ecase sb!c:*backend-byte-order*
65                                (:little-endian 0)
66                                (:big-endian
67                                 '(- sb!vm:n-word-bits
68                                     (mod length sb!vm:n-word-bits)))))
69                        (%vector-raw-bits x i))))
70                  (mix result ,(ecase sb!c:*backend-byte-order*
71                                 (:little-endian
72                                  '(logand num most-positive-fixnum))
73                                 (:big-endian
74                                  '(ash num (- sb!vm:n-lowtag-bits)))))))
75            (declare (type index i end-1))
76            (let ((num (%vector-raw-bits x i)))
77              (mixf result ,(ecase sb!c:*backend-byte-order*
78                              (:little-endian
79                               '(logand num most-positive-fixnum))
80                              ;; FIXME: I'm not certain that
81                              ;; N-LOWTAG-BITS is the clearest way of
82                              ;; expressing this: it's essentially the
83                              ;; difference between `(UNSIGNED-BYTE
84                              ;; ,SB!VM:N-WORD-BITS) and (AND FIXNUM
85                              ;; UNSIGNED-BYTE).
86                              (:big-endian
87                               '(ash num (- sb!vm:n-lowtag-bits))))))))))))
88
89 ;;; Some other common SXHASH cases are defined as DEFTRANSFORMs in
90 ;;; order to avoid having to do TYPECASE at runtime.
91 ;;;
92 ;;; We also take the opportunity to handle the cases of constant
93 ;;; strings, and of symbols whose names are known at compile time;
94 ;;; except that since SXHASH on the cross-compilation host is not in
95 ;;; general compatible with SXHASH on the target SBCL, we can't so
96 ;;; easily do this optimization in the cross-compiler, and SBCL itself
97 ;;; doesn't seem to need this optimization, so we don't try.
98 (deftransform sxhash ((x) (simple-string))
99   (if #+sb-xc-host nil #-sb-xc-host (constant-lvar-p x)
100       (sxhash (lvar-value x))
101       '(%sxhash-simple-string x)))
102 (deftransform sxhash ((x) (symbol))
103   (if #+sb-xc-host nil #-sb-xc-host (constant-lvar-p x)
104       (sxhash (lvar-value x))
105       (if (csubtypep (lvar-type x) (specifier-type 'null))
106           ;; FIXME: this isn't in fact as optimized as it could be;
107           ;; this does a memory load, whereas (because we know the
108           ;; layout of NIL) we could simply take the address of NIL
109           ;; (or the contents of NULL-TN) and mask off the appropriate
110           ;; bits, since SYMBOL-HASH of NIL is also NIL's CDR, which
111           ;; is NIL.  -- CSR, 2004-07-14
112           '(symbol-hash x)
113           ;; Cache the value of the symbol's sxhash in the symbol-hash
114           ;; slot.
115           '(let ((result (symbol-hash x)))
116             ;; 0 marks uninitialized slot. We can't use negative
117             ;; values for the uninitialized slots since NIL might be
118             ;; located so high in memory on some platforms that its
119             ;; SYMBOL-HASH (which contains NIL itself) is a negative
120             ;; fixnum.
121             (if (= 0 result)
122                 (let ((sxhash (%sxhash-simple-string (symbol-name x))))
123                   ;; We could do a (logior sxhash #x10000000) to
124                   ;; ensure that we never store a 0 in the
125                   ;; slot. However, it's such an unlikely event
126                   ;; (1/5e8?) that it makes more sense to optimize for
127                   ;; the common case...
128                   (%set-symbol-hash x sxhash)
129                   sxhash)
130                 result)))))
131
132 (deftransform psxhash ((x &optional depthoid) (character &optional t))
133   `(char-code (char-upcase x)))
134
135 (deftransform psxhash ((x &optional depthoid) (integer &optional t))
136   `(sxhash x))