0.9.6.53: in the name of stability and goodwill
[sbcl.git] / src / compiler / generic / vm-tran.lisp
1 ;;;; implementation-dependent transforms
2
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.
11
12 (in-package "SB!C")
13
14 ;;; We need to define these predicates, since the TYPEP source
15 ;;; transform picks whichever predicate was defined last when there
16 ;;; are multiple predicates for equivalent types.
17 (define-source-transform short-float-p (x) `(single-float-p ,x))
18 #!-long-float
19 (define-source-transform long-float-p (x) `(double-float-p ,x))
20
21 (define-source-transform compiled-function-p (x)
22   `(functionp ,x))
23
24 (define-source-transform char-int (x)
25   `(char-code ,x))
26
27 (deftransform abs ((x) (rational))
28   '(if (< x 0) (- x) x))
29
30 ;;; The layout is stored in slot 0.
31 (define-source-transform %instance-layout (x)
32   `(truly-the layout (%instance-ref ,x 0)))
33 (define-source-transform %set-instance-layout (x val)
34   `(%instance-set ,x 0 (the layout ,val)))
35 (define-source-transform %funcallable-instance-layout (x)
36   `(truly-the layout (%funcallable-instance-info ,x 0)))
37 (define-source-transform %set-funcallable-instance-layout (x val)
38   `(setf (%funcallable-instance-info ,x 0) (the layout ,val)))
39 \f
40 ;;;; character support
41
42 ;;; In our implementation there are really only BASE-CHARs.
43 #+nil
44 (define-source-transform characterp (obj)
45   `(base-char-p ,obj))
46 \f
47 ;;;; simplifying HAIRY-DATA-VECTOR-REF and HAIRY-DATA-VECTOR-SET
48
49 (deftransform hairy-data-vector-ref ((string index) (simple-string t))
50   (let ((ctype (lvar-type string)))
51     (if (array-type-p ctype)
52         ;; the other transform will kick in, so that's OK
53         (give-up-ir1-transform)
54         `(etypecase string
55           ((simple-array character (*)) (data-vector-ref string index))
56           #!+sb-unicode
57           ((simple-array base-char (*)) (data-vector-ref string index))
58           ((simple-array nil (*)) (data-vector-ref string index))))))
59
60 (deftransform hairy-data-vector-ref ((array index) (array t) *)
61   "avoid runtime dispatch on array element type"
62   (let ((element-ctype (extract-upgraded-element-type array))
63         (declared-element-ctype (extract-declared-element-type array)))
64     (declare (type ctype element-ctype))
65     (when (eq *wild-type* element-ctype)
66       (give-up-ir1-transform
67        "Upgraded element type of array is not known at compile time."))
68     ;; (The expansion here is basically a degenerate case of
69     ;; WITH-ARRAY-DATA. Since WITH-ARRAY-DATA is implemented as a
70     ;; macro, and macros aren't expanded in transform output, we have
71     ;; to hand-expand it ourselves.)
72     (let ((element-type-specifier (type-specifier element-ctype)))
73       `(multiple-value-bind (array index)
74            (%data-vector-and-index array index)
75          (declare (type (simple-array ,element-type-specifier 1) array))
76          ,(let ((bare-form '(data-vector-ref array index)))
77             (if (type= element-ctype declared-element-ctype)
78                 bare-form
79                 `(the ,(type-specifier declared-element-ctype)
80                       ,bare-form)))))))
81
82 ;;; Transform multi-dimensional to one dimensional SIMPLE-ARRAY
83 ;;; access.
84 (deftransform data-vector-ref ((array index)
85                                (simple-array t))
86   (let ((array-type (lvar-type array)))
87     (unless (array-type-p array-type)
88       (give-up-ir1-transform))
89     (let ((dims (array-type-dimensions array-type)))
90       (when (or (atom dims) (= (length dims) 1))
91         (give-up-ir1-transform))
92       (let ((el-type (array-type-specialized-element-type array-type))
93             (total-size (if (member '* dims)
94                             '*
95                             (reduce #'* dims))))
96         `(data-vector-ref (truly-the (simple-array ,(type-specifier el-type)
97                                                    (,total-size))
98                                      (%array-data-vector array))
99                           index)))))
100
101 (deftransform hairy-data-vector-set ((string index new-value)
102                                      (simple-string t t))
103   (let ((ctype (lvar-type string)))
104     (if (array-type-p ctype)
105         ;; the other transform will kick in, so that's OK
106         (give-up-ir1-transform)
107         `(etypecase string
108           ((simple-array character (*))
109            (data-vector-set string index new-value))
110           #!+sb-unicode
111           ((simple-array base-char (*))
112            (data-vector-set string index new-value))
113           ((simple-array nil (*))
114            (data-vector-set string index new-value))))))
115
116 (deftransform hairy-data-vector-set ((array index new-value)
117                                      (array t t)
118                                      *)
119   "avoid runtime dispatch on array element type"
120   (let ((element-ctype (extract-upgraded-element-type array))
121         (declared-element-ctype (extract-declared-element-type array)))
122     (declare (type ctype element-ctype))
123     (when (eq *wild-type* element-ctype)
124       (give-up-ir1-transform
125        "Upgraded element type of array is not known at compile time."))
126     (let ((element-type-specifier (type-specifier element-ctype)))
127       `(multiple-value-bind (array index)
128            (%data-vector-and-index array index)
129          (declare (type (simple-array ,element-type-specifier 1) array)
130                   (type ,element-type-specifier new-value))
131          ,(if (type= element-ctype declared-element-ctype)
132               '(data-vector-set array index new-value)
133               `(truly-the ,(type-specifier declared-element-ctype)
134                  (data-vector-set array index
135                   (the ,(type-specifier declared-element-ctype)
136                        new-value))))))))
137
138 (deftransform data-vector-set ((array index new-value)
139                                (simple-array t t))
140   (let ((array-type (lvar-type array)))
141     (unless (array-type-p array-type)
142       (give-up-ir1-transform))
143     (let ((dims (array-type-dimensions array-type)))
144       (when (or (atom dims) (= (length dims) 1))
145         (give-up-ir1-transform))
146       (let ((el-type (array-type-specialized-element-type array-type))
147             (total-size (if (member '* dims)
148                             '*
149                             (reduce #'* dims))))
150         `(data-vector-set (truly-the (simple-array ,(type-specifier el-type)
151                                                    (,total-size))
152                                      (%array-data-vector array))
153                           index
154                           new-value)))))
155
156 (defoptimizer (%data-vector-and-index derive-type) ((array index))
157   (let ((atype (lvar-type array)))
158     (when (array-type-p atype)
159       (values-specifier-type
160        `(values (simple-array ,(type-specifier
161                                 (array-type-specialized-element-type atype))
162                               (*))
163                 index)))))
164
165 (deftransform %data-vector-and-index ((%array %index)
166                                       (simple-array t)
167                                       *)
168   ;; KLUDGE: why the percent signs?  Well, ARRAY and INDEX are
169   ;; respectively exported from the CL and SB!INT packages, which
170   ;; means that they're visible to all sorts of things.  If the
171   ;; compiler can prove that the call to ARRAY-HEADER-P, below, either
172   ;; returns T or NIL, it will delete the irrelevant branch.  However,
173   ;; user code might have got here with a variable named CL:ARRAY, and
174   ;; quite often compiler code with a variable named SB!INT:INDEX, so
175   ;; this can generate code deletion notes for innocuous user code:
176   ;; (DEFUN F (ARRAY I) (DECLARE (SIMPLE-VECTOR ARRAY)) (AREF ARRAY I))
177   ;; -- CSR, 2003-04-01
178
179   ;; We do this solely for the -OR-GIVE-UP side effect, since we want
180   ;; to know that the type can be figured out in the end before we
181   ;; proceed, but we don't care yet what the type will turn out to be.
182   (upgraded-element-type-specifier-or-give-up %array)
183
184   '(if (array-header-p %array)
185        (values (%array-data-vector %array) %index)
186        (values %array %index)))
187
188 ;;; transforms for getting at simple arrays of (UNSIGNED-BYTE N) when (< N 8)
189 ;;;
190 ;;; FIXME: In CMU CL, these were commented out with #+NIL. Why? Should
191 ;;; we fix them or should we delete them? (Perhaps these definitions
192 ;;; predate the various DATA-VECTOR-REF-FOO VOPs which have
193 ;;; (:TRANSLATE DATA-VECTOR-REF), and are redundant now?)
194 #+nil
195 (macrolet
196     ((frob (type bits)
197        (let ((elements-per-word (truncate sb!vm:n-word-bits bits)))
198          `(progn
199             (deftransform data-vector-ref ((vector index)
200                                            (,type *))
201               `(multiple-value-bind (word bit)
202                    (floor index ,',elements-per-word)
203                  (ldb ,(ecase sb!vm:target-byte-order
204                          (:little-endian '(byte ,bits (* bit ,bits)))
205                          (:big-endian '(byte ,bits (- sb!vm:n-word-bits
206                                                       (* (1+ bit) ,bits)))))
207                       (%raw-bits vector (+ word sb!vm:vector-data-offset)))))
208             (deftransform data-vector-set ((vector index new-value)
209                                            (,type * *))
210               `(multiple-value-bind (word bit)
211                    (floor index ,',elements-per-word)
212                  (setf (ldb ,(ecase sb!vm:target-byte-order
213                                (:little-endian '(byte ,bits (* bit ,bits)))
214                                (:big-endian
215                                 '(byte ,bits (- sb!vm:n-word-bits
216                                                 (* (1+ bit) ,bits)))))
217                             (%raw-bits vector (+ word sb!vm:vector-data-offset)))
218                        new-value)))))))
219   (frob simple-bit-vector 1)
220   (frob (simple-array (unsigned-byte 2) (*)) 2)
221   (frob (simple-array (unsigned-byte 4) (*)) 4))
222 \f
223 ;;;; BIT-VECTOR hackery
224
225 ;;; SIMPLE-BIT-VECTOR bit-array operations are transformed to a word
226 ;;; loop that does 32 bits at a time.
227 ;;;
228 ;;; FIXME: This is a lot of repeatedly macroexpanded code. It should
229 ;;; be a function call instead.
230 (macrolet ((def (bitfun wordfun)
231              `(deftransform ,bitfun ((bit-array-1 bit-array-2 result-bit-array)
232                                      (simple-bit-vector
233                                       simple-bit-vector
234                                       simple-bit-vector)
235                                      *
236                                      :node node :policy (>= speed space))
237                 `(progn
238                    ,@(unless (policy node (zerop safety))
239                              '((unless (= (length bit-array-1)
240                                           (length bit-array-2)
241                                           (length result-bit-array))
242                                  (error "Argument and/or result bit arrays are not the same length:~
243                          ~%  ~S~%  ~S  ~%  ~S"
244                                         bit-array-1
245                                         bit-array-2
246                                         result-bit-array))))
247                   (let ((length (length result-bit-array)))
248                     (if (= length 0)
249                         ;; We avoid doing anything to 0-length
250                         ;; bit-vectors, or rather, the memory that
251                         ;; follows them. Other divisible-by-32 cases
252                         ;; are handled by the (1- length), below.
253                         ;; CSR, 2002-04-24
254                         result-bit-array
255                         (do ((index sb!vm:vector-data-offset (1+ index))
256                              (end-1 (+ sb!vm:vector-data-offset
257                                        ;; bit-vectors of length 1-32
258                                        ;; need precisely one (SETF
259                                        ;; %RAW-BITS), done here in the
260                                        ;; epilogue. - CSR, 2002-04-24
261                                        (truncate (truly-the index (1- length))
262                                                  sb!vm:n-word-bits))))
263                             ((= index end-1)
264                              (setf (%raw-bits result-bit-array index)
265                                    (,',wordfun (%raw-bits bit-array-1 index)
266                                                (%raw-bits bit-array-2 index)))
267                              result-bit-array)
268                           (declare (optimize (speed 3) (safety 0))
269                                    (type index index end-1))
270                           (setf (%raw-bits result-bit-array index)
271                                 (,',wordfun (%raw-bits bit-array-1 index)
272                                             (%raw-bits bit-array-2 index))))))))))
273  (def bit-and word-logical-and)
274  (def bit-ior word-logical-or)
275  (def bit-xor word-logical-xor)
276  (def bit-eqv word-logical-eqv)
277  (def bit-nand word-logical-nand)
278  (def bit-nor word-logical-nor)
279  (def bit-andc1 word-logical-andc1)
280  (def bit-andc2 word-logical-andc2)
281  (def bit-orc1 word-logical-orc1)
282  (def bit-orc2 word-logical-orc2))
283
284 (deftransform bit-not
285               ((bit-array result-bit-array)
286                (simple-bit-vector simple-bit-vector) *
287                :node node :policy (>= speed space))
288   `(progn
289      ,@(unless (policy node (zerop safety))
290          '((unless (= (length bit-array)
291                       (length result-bit-array))
292              (error "Argument and result bit arrays are not the same length:~
293                      ~%  ~S~%  ~S"
294                     bit-array result-bit-array))))
295     (let ((length (length result-bit-array)))
296       (if (= length 0)
297           ;; We avoid doing anything to 0-length bit-vectors, or rather,
298           ;; the memory that follows them. Other divisible-by
299           ;; n-word-bits cases are handled by the (1- length), below.
300           ;; CSR, 2002-04-24
301           result-bit-array
302           (do ((index sb!vm:vector-data-offset (1+ index))
303                (end-1 (+ sb!vm:vector-data-offset
304                          ;; bit-vectors of length 1 to n-word-bits need
305                          ;; precisely one (SETF %RAW-BITS), done here in
306                          ;; the epilogue. - CSR, 2002-04-24
307                          (truncate (truly-the index (1- length))
308                                    sb!vm:n-word-bits))))
309               ((= index end-1)
310                (setf (%raw-bits result-bit-array index)
311                      (word-logical-not (%raw-bits bit-array index)))
312                result-bit-array)
313             (declare (optimize (speed 3) (safety 0))
314                      (type index index end-1))
315             (setf (%raw-bits result-bit-array index)
316                   (word-logical-not (%raw-bits bit-array index))))))))
317
318 (deftransform bit-vector-= ((x y) (simple-bit-vector simple-bit-vector))
319   `(and (= (length x) (length y))
320         (let ((length (length x)))
321           (or (= length 0)
322               (do* ((i sb!vm:vector-data-offset (+ i 1))
323                     (end-1 (+ sb!vm:vector-data-offset
324                               (floor (1- length) sb!vm:n-word-bits))))
325                    ((= i end-1)
326                     (let* ((extra (1+ (mod (1- length) sb!vm:n-word-bits)))
327                            (mask (ash #.(1- (ash 1 sb!vm:n-word-bits))
328                                       (- extra sb!vm:n-word-bits)))
329                            (numx
330                             (logand
331                              (ash mask
332                                   ,(ecase sb!c:*backend-byte-order*
333                                      (:little-endian 0)
334                                      (:big-endian
335                                       '(- sb!vm:n-word-bits extra))))
336                              (%raw-bits x i)))
337                            (numy
338                             (logand
339                              (ash mask
340                                   ,(ecase sb!c:*backend-byte-order*
341                                      (:little-endian 0)
342                                      (:big-endian
343                                       '(- sb!vm:n-word-bits extra))))
344                              (%raw-bits y i))))
345                       (declare (type (integer 1 #.sb!vm:n-word-bits) extra)
346                                (type sb!vm:word mask numx numy))
347                       (= numx numy)))
348                 (declare (type index i end-1))
349                 (let ((numx (%raw-bits x i))
350                       (numy (%raw-bits y i)))
351                   (declare (type sb!vm:word numx numy))
352                   (unless (= numx numy)
353                     (return nil))))))))
354
355 (deftransform count ((item sequence) (bit simple-bit-vector) *
356                      :policy (>= speed space))
357   `(let ((length (length sequence)))
358     (if (zerop length)
359         0
360         (do ((index sb!vm:vector-data-offset (1+ index))
361              (count 0)
362              (end-1 (+ sb!vm:vector-data-offset
363                        (truncate (truly-the index (1- length))
364                                  sb!vm:n-word-bits))))
365             ((= index end-1)
366              (let* ((extra (1+ (mod (1- length) sb!vm:n-word-bits)))
367                     (mask (ash #.(1- (ash 1 sb!vm:n-word-bits))
368                                (- extra sb!vm:n-word-bits)))
369                     (bits (logand (ash mask
370                                        ,(ecase sb!c:*backend-byte-order*
371                                                (:little-endian 0)
372                                                (:big-endian
373                                                 '(- sb!vm:n-word-bits extra))))
374                                   (%raw-bits sequence index))))
375                (declare (type (integer 1 #.sb!vm:n-word-bits) extra))
376                (declare (type sb!vm:word mask bits))
377                ;; could consider LOGNOT for the zero case instead of
378                ;; doing the subtraction...
379                (incf count ,(if (constant-lvar-p item)
380                                 (if (zerop (lvar-value item))
381                                     '(- extra (logcount bits))
382                                     '(logcount bits))
383                                 '(if (zerop item)
384                                      (- extra (logcount bits))
385                                      (logcount bits))))))
386           (declare (type index index count end-1)
387                    (optimize (speed 3) (safety 0)))
388           (incf count ,(if (constant-lvar-p item)
389                            (if (zerop (lvar-value item))
390                                '(- sb!vm:n-word-bits (logcount (%raw-bits sequence index)))
391                                '(logcount (%raw-bits sequence index)))
392                            '(if (zerop item)
393                              (- sb!vm:n-word-bits (logcount (%raw-bits sequence index)))
394                              (logcount (%raw-bits sequence index)))))))))
395
396 (deftransform fill ((sequence item) (simple-bit-vector bit) *
397                     :policy (>= speed space))
398   (let ((value (if (constant-lvar-p item)
399                    (if (= (lvar-value item) 0)
400                        0
401                        #.(1- (ash 1 sb!vm:n-word-bits)))
402                    `(if (= item 0) 0 #.(1- (ash 1 sb!vm:n-word-bits))))))
403     `(let ((length (length sequence))
404            (value ,value))
405        (if (= length 0)
406            sequence
407            (do ((index sb!vm:vector-data-offset (1+ index))
408                 (end-1 (+ sb!vm:vector-data-offset
409                           ;; bit-vectors of length 1 to n-word-bits need
410                           ;; precisely one (SETF %RAW-BITS), done here
411                           ;; in the epilogue. - CSR, 2002-04-24
412                           (truncate (truly-the index (1- length))
413                                     sb!vm:n-word-bits))))
414                ((= index end-1)
415                 (setf (%raw-bits sequence index) value)
416                 sequence)
417              (declare (optimize (speed 3) (safety 0))
418                       (type index index end-1))
419              (setf (%raw-bits sequence index) value))))))
420
421 (deftransform fill ((sequence item) (simple-base-string base-char) *
422                     :policy (>= speed space))
423   (let ((value (if (constant-lvar-p item)
424                    (let* ((char (lvar-value item))
425                           (code (sb!xc:char-code char))
426                           (accum 0))
427                      (dotimes (i sb!vm:n-word-bytes accum)
428                        (setf accum (logior accum (ash code (* 8 i))))))
429                    `(let ((code (sb!xc:char-code item)))
430                      (logior ,@(loop for i from 0 below sb!vm:n-word-bytes
431                                      collect `(ash code ,(* 8 i))))))))
432     `(let ((length (length sequence))
433            (value ,value))
434       (multiple-value-bind (times rem)
435           (truncate length sb!vm:n-word-bytes)
436         (do ((index sb!vm:vector-data-offset (1+ index))
437              (end (+ times sb!vm:vector-data-offset)))
438             ((= index end)
439              (let ((place (* times sb!vm:n-word-bytes)))
440                (declare (fixnum place))
441                (dotimes (j rem sequence)
442                  (declare (index j))
443                  (setf (schar sequence (the index (+ place j))) item))))
444           (declare (optimize (speed 3) (safety 0))
445                    (type index index))
446           (setf (%raw-bits sequence index) value))))))
447 \f
448 ;;;; %BYTE-BLT
449
450 ;;; FIXME: The old CMU CL code used various COPY-TO/FROM-SYSTEM-AREA
451 ;;; stuff (with all the associated bit-index cruft and overflow
452 ;;; issues) even for byte moves. In SBCL, we're converting to byte
453 ;;; moves as problems are discovered with the old code, and this is
454 ;;; currently (ca. sbcl-0.6.12.30) the main interface for code in
455 ;;; SB!KERNEL and SB!SYS (e.g. i/o code). It's not clear that it's the
456 ;;; ideal interface, though, and it probably deserves some thought.
457 (deftransform %byte-blt ((src src-start dst dst-start dst-end)
458                          ((or (simple-unboxed-array (*)) system-area-pointer)
459                           index
460                           (or (simple-unboxed-array (*)) system-area-pointer)
461                           index
462                           index))
463   ;; FIXME: CMU CL had a hairier implementation of this (back when it
464   ;; was still called (%PRIMITIVE BYTE-BLT). It had the small problem
465   ;; that it didn't work for large (>16M) values of SRC-START or
466   ;; DST-START. However, it might have been more efficient. In
467   ;; particular, I don't really know how much the foreign function
468   ;; call costs us here. My guess is that if the overhead is
469   ;; acceptable for SQRT and COS, it's acceptable here, but this
470   ;; should probably be checked. -- WHN
471   '(flet ((sapify (thing)
472             (etypecase thing
473               (system-area-pointer thing)
474               ;; FIXME: The code here rather relies on the simple
475               ;; unboxed array here having byte-sized entries. That
476               ;; should be asserted explicitly, I just haven't found
477               ;; a concise way of doing it. (It would be nice to
478               ;; declare it in the DEFKNOWN too.)
479               ((simple-unboxed-array (*)) (vector-sap thing)))))
480      (declare (inline sapify))
481      (without-gcing
482       (memmove (sap+ (sapify dst) dst-start)
483                (sap+ (sapify src) src-start)
484                (- dst-end dst-start)))
485      (values)))
486 \f
487 ;;;; transforms for EQL of floating point values
488
489 (deftransform eql ((x y) (single-float single-float))
490   '(= (single-float-bits x) (single-float-bits y)))
491
492 (deftransform eql ((x y) (double-float double-float))
493   '(and (= (double-float-low-bits x) (double-float-low-bits y))
494         (= (double-float-high-bits x) (double-float-high-bits y))))
495
496 \f
497 ;;;; modular functions
498 (define-good-modular-fun logand :unsigned)
499 (define-good-modular-fun logior :unsigned)
500 ;;; FIXME: XOR? ANDC1, ANDC2?  -- CSR, 2003-09-16
501
502 (macrolet
503     ((def (name class width)
504        (let ((type (ecase class
505                      (:unsigned 'unsigned-byte)
506                      (:signed 'signed-byte))))
507          `(progn
508             (defknown ,name (integer (integer 0)) (,type ,width)
509                       (foldable flushable movable))
510             (define-modular-fun-optimizer ash ((integer count) ,class :width width)
511               (when (and (<= width ,width)
512                          (or (and (constant-lvar-p count)
513                                   (plusp (lvar-value count)))
514                              (csubtypep (lvar-type count)
515                                         (specifier-type '(and unsigned-byte fixnum)))))
516                 (cut-to-width integer ,class width)
517                 ',name))
518             (setf (gethash ',name (modular-class-versions (find-modular-class ',class)))
519                   `(ash ,',width))))))
520   ;; This should really be dependent on SB!VM:N-WORD-BITS, but since we
521   ;; don't have a true Alpha64 port yet, we'll have to stick to
522   ;; SB!VM:N-MACHINE-WORD-BITS for the time being.  --njf, 2004-08-14
523   #!+#.(cl:if (cl:= 32 sb!vm:n-machine-word-bits) '(and) '(or))
524   (progn
525     #!+x86 (def sb!vm::ash-left-smod30 :signed 30)
526     (def sb!vm::ash-left-mod32 :unsigned 32))
527   #!+#.(cl:if (cl:= 64 sb!vm:n-machine-word-bits) '(and) '(or))
528   (progn
529     #!+x86-64 (def sb!vm::ash-left-smod61 :signed 61)
530     (def sb!vm::ash-left-mod64 :unsigned 64)))
531
532 \f
533 ;;;; word-wise logical operations
534
535 ;;; These transforms assume the presence of modular arithmetic to
536 ;;; generate efficient code.
537
538 (define-source-transform word-logical-not (x)
539   `(logand (lognot (the sb!vm:word ,x)) #.(1- (ash 1 sb!vm:n-word-bits))))
540
541 (deftransform word-logical-and ((x y))
542   '(logand x y))
543
544 (deftransform word-logical-nand ((x y))
545   '(logand (lognand x y) #.(1- (ash 1 sb!vm:n-word-bits))))
546
547 (deftransform word-logical-or ((x y))
548   '(logior x y))
549
550 (deftransform word-logical-nor ((x y))
551   '(logand (lognor x y) #.(1- (ash 1 sb!vm:n-word-bits))))
552
553 (deftransform word-logical-xor ((x y))
554   '(logxor x y))
555
556 (deftransform word-logical-eqv ((x y))
557   '(logand (logeqv x y) #.(1- (ash 1 sb!vm:n-word-bits))))
558
559 (deftransform word-logical-orc1 ((x y))
560   '(logand (logorc1 x y) #.(1- (ash 1 sb!vm:n-word-bits))))
561
562 (deftransform word-logical-orc2 ((x y))
563   '(logand (logorc2 x y) #.(1- (ash 1 sb!vm:n-word-bits))))
564
565 (deftransform word-logical-andc1 ((x y))
566   '(logand (logandc1 x y) #.(1- (ash 1 sb!vm:n-word-bits))))
567
568 (deftransform word-logical-andc2 ((x y))
569   '(logand (logandc2 x y) #.(1- (ash 1 sb!vm:n-word-bits))))
570
571 \f
572 ;;; There are two different ways the multiplier can be recoded. The
573 ;;; more obvious is to shift X by the correct amount for each bit set
574 ;;; in Y and to sum the results. But if there is a string of bits that
575 ;;; are all set, you can add X shifted by one more then the bit
576 ;;; position of the first set bit and subtract X shifted by the bit
577 ;;; position of the last set bit. We can't use this second method when
578 ;;; the high order bit is bit 31 because shifting by 32 doesn't work
579 ;;; too well.
580 (defun ub32-strength-reduce-constant-multiply (arg num)
581   (declare (type (unsigned-byte 32) num))
582   (let ((adds 0) (shifts 0)
583         (result nil) first-one)
584     (labels ((add (next-factor)
585                (setf result
586                      (if result
587                          (progn (incf adds) `(+ ,result ,next-factor))
588                          next-factor))))
589       (declare (inline add))
590       (dotimes (bitpos 32)
591         (if first-one
592             (when (not (logbitp bitpos num))
593               (add (if (= (1+ first-one) bitpos)
594                        ;; There is only a single bit in the string.
595                        (progn (incf shifts) `(ash ,arg ,first-one))
596                        ;; There are at least two.
597                        (progn
598                          (incf adds)
599                          (incf shifts 2)
600                          `(- (ash ,arg ,bitpos)
601                              (ash ,arg ,first-one)))))
602               (setf first-one nil))
603             (when (logbitp bitpos num)
604               (setf first-one bitpos))))
605       (when first-one
606         (cond ((= first-one 31))
607               ((= first-one 30) (incf shifts) (add `(ash ,arg 30)))
608               (t
609                (incf shifts 2)
610                (incf adds)
611                (add `(- (ash ,arg 31)
612                         (ash ,arg ,first-one)))))
613         (incf shifts)
614         (add `(ash ,arg 31))))
615     (values (if (plusp adds)
616                 `(logand ,result #.(1- (ash 1 32))) ; using modular arithmetic
617                 result)
618             adds
619             shifts)))