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