1 ;;;; implementation-dependent transforms
3 ;;;; This software is part of the SBCL system. See the README file for
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.
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))
19 (define-source-transform long-float-p (x) `(double-float-p ,x))
21 (define-source-transform compiled-function-p (x)
27 (not (sb!eval:interpreted-function-p ,x)))))
29 (define-source-transform char-int (x)
32 (deftransform abs ((x) (rational))
33 '(if (< x 0) (- x) x))
35 ;;; We don't want to clutter the bignum code.
37 (define-source-transform sb!bignum:%bignum-ref (bignum index)
38 ;; KLUDGE: We use TRULY-THE here because even though the bignum code
39 ;; is (currently) compiled with (SAFETY 0), the compiler insists on
40 ;; inserting CAST nodes to ensure that INDEX is of the correct type.
41 ;; These CAST nodes do not generate any type checks, but they do
42 ;; interfere with the operation of FOLD-INDEX-ADDRESSING, below.
43 ;; This scenario is a problem for the more user-visible case of
44 ;; folding as well. --njf, 2006-12-01
45 `(sb!bignum:%bignum-ref-with-offset ,bignum
46 (truly-the bignum-index ,index) 0))
49 (defun fold-index-addressing (fun-name element-size lowtag data-offset
50 index offset &optional setter-p)
51 (multiple-value-bind (func index-args) (extract-fun-args index '(+ -) 2)
52 (destructuring-bind (x constant) index-args
53 (declare (ignorable x))
54 (unless (constant-lvar-p constant)
55 (give-up-ir1-transform))
56 (let ((value (lvar-value constant)))
57 (unless (and (integerp value)
58 (sb!vm::foldable-constant-offset-p
59 element-size lowtag data-offset
60 (funcall func value (lvar-value offset))))
61 (give-up-ir1-transform "constant is too large for inlining"))
62 (splice-fun-args index func 2)
63 (format t "preparing to transform with ~A ~D~%" func value)
64 `(lambda (thing index off1 off2 ,@(when setter-p
66 (,fun-name thing index (,func off2 off1) ,@(when setter-p
70 (deftransform sb!bignum:%bignum-ref-with-offset
71 ((bignum index offset) * * :node node)
72 (fold-index-addressing 'sb!bignum:%bignum-ref-with-offset
73 sb!vm:n-word-bits sb!vm:other-pointer-lowtag
74 sb!vm:bignum-digits-offset
77 ;;; The layout is stored in slot 0.
78 (define-source-transform %instance-layout (x)
79 `(truly-the layout (%instance-ref ,x 0)))
80 (define-source-transform %set-instance-layout (x val)
81 `(%instance-set ,x 0 (the layout ,val)))
82 (define-source-transform %funcallable-instance-layout (x)
83 `(truly-the layout (%funcallable-instance-info ,x 0)))
84 (define-source-transform %set-funcallable-instance-layout (x val)
85 `(setf (%funcallable-instance-info ,x 0) (the layout ,val)))
87 ;;;; character support
89 ;;; In our implementation there are really only BASE-CHARs.
91 (define-source-transform characterp (obj)
94 ;;;; simplifying HAIRY-DATA-VECTOR-REF and HAIRY-DATA-VECTOR-SET
96 (deftransform hairy-data-vector-ref ((string index) (simple-string t))
97 (let ((ctype (lvar-type string)))
98 (if (array-type-p ctype)
99 ;; the other transform will kick in, so that's OK
100 (give-up-ir1-transform)
102 ((simple-array character (*))
103 (data-vector-ref string index))
105 ((simple-array base-char (*))
106 (data-vector-ref string index))
107 ((simple-array nil (*))
108 (data-vector-ref string index))))))
110 (deftransform hairy-data-vector-ref ((array index) (array t) *)
111 "avoid runtime dispatch on array element type"
112 (let ((element-ctype (extract-upgraded-element-type array))
113 (declared-element-ctype (extract-declared-element-type array)))
114 (declare (type ctype element-ctype))
115 (when (eq *wild-type* element-ctype)
116 (give-up-ir1-transform
117 "Upgraded element type of array is not known at compile time."))
118 ;; (The expansion here is basically a degenerate case of
119 ;; WITH-ARRAY-DATA. Since WITH-ARRAY-DATA is implemented as a
120 ;; macro, and macros aren't expanded in transform output, we have
121 ;; to hand-expand it ourselves.)
122 (let* ((element-type-specifier (type-specifier element-ctype)))
123 `(multiple-value-bind (array index)
124 (%data-vector-and-index array index)
125 (declare (type (simple-array ,element-type-specifier 1) array))
126 ,(let ((bare-form '(data-vector-ref array index)))
127 (if (type= element-ctype declared-element-ctype)
129 `(the ,(type-specifier declared-element-ctype)
132 ;;; Transform multi-dimensional array to one dimensional data vector
134 (deftransform data-vector-ref ((array index) (simple-array t))
135 (let ((array-type (lvar-type array)))
136 (unless (array-type-p array-type)
137 (give-up-ir1-transform))
138 (let ((dims (array-type-dimensions array-type)))
139 (when (or (atom dims) (= (length dims) 1))
140 (give-up-ir1-transform))
141 (let ((el-type (array-type-specialized-element-type array-type))
142 (total-size (if (member '* dims)
145 `(data-vector-ref (truly-the (simple-array ,(type-specifier el-type)
147 (%array-data-vector array))
150 ;;; Transform data vector access to a form that opens up optimization
153 (deftransform data-vector-ref ((array index) ((or simple-unboxed-array
156 (let ((array-type (lvar-type array)))
157 (unless (array-type-p array-type)
158 (give-up-ir1-transform))
159 (let* ((element-type (type-specifier (array-type-specialized-element-type array-type)))
160 (saetp (find element-type
161 sb!vm:*specialized-array-element-type-properties*
162 :key #'sb!vm:saetp-specifier :test #'equal)))
163 (unless (>= (sb!vm:saetp-n-bits saetp) sb!vm:n-byte-bits)
164 (give-up-ir1-transform))
165 `(data-vector-ref-with-offset array index 0))))
168 (deftransform data-vector-ref-with-offset ((array index offset)
169 ((or simple-unboxed-array
172 (let ((array-type (lvar-type array)))
173 (unless (array-type-p array-type)
174 (give-up-ir1-transform))
175 (let* ((element-type (type-specifier (array-type-specialized-element-type array-type)))
176 (saetp (find element-type
177 sb!vm:*specialized-array-element-type-properties*
178 :key #'sb!vm:saetp-specifier :test #'equal)))
179 (aver (>= (sb!vm:saetp-n-bits saetp) sb!vm:n-byte-bits))
180 (fold-index-addressing 'data-vector-ref-with-offset
181 (sb!vm:saetp-n-bits saetp)
182 sb!vm:other-pointer-lowtag
183 sb!vm:vector-data-offset
186 (deftransform hairy-data-vector-set ((string index new-value)
188 (let ((ctype (lvar-type string)))
189 (if (array-type-p ctype)
190 ;; the other transform will kick in, so that's OK
191 (give-up-ir1-transform)
193 ((simple-array character (*))
194 (data-vector-set string index new-value))
196 ((simple-array base-char (*))
197 (data-vector-set string index new-value))
198 ((simple-array nil (*))
199 (data-vector-set string index new-value))))))
201 (deftransform hairy-data-vector-set ((array index new-value)
204 "avoid runtime dispatch on array element type"
205 (let ((element-ctype (extract-upgraded-element-type array))
206 (declared-element-ctype (extract-declared-element-type array)))
207 (declare (type ctype element-ctype))
208 (when (eq *wild-type* element-ctype)
209 (give-up-ir1-transform
210 "Upgraded element type of array is not known at compile time."))
211 (let ((element-type-specifier (type-specifier element-ctype)))
212 `(multiple-value-bind (array index)
213 (%data-vector-and-index array index)
214 (declare (type (simple-array ,element-type-specifier 1) array)
215 (type ,element-type-specifier new-value))
216 ,(if (type= element-ctype declared-element-ctype)
217 '(data-vector-set array index new-value)
218 `(truly-the ,(type-specifier declared-element-ctype)
219 (data-vector-set array index
220 (the ,(type-specifier declared-element-ctype)
223 ;;; Transform multi-dimensional array to one dimensional data vector
225 (deftransform data-vector-set ((array index new-value)
227 (let ((array-type (lvar-type array)))
228 (unless (array-type-p array-type)
229 (give-up-ir1-transform))
230 (let ((dims (array-type-dimensions array-type)))
231 (when (or (atom dims) (= (length dims) 1))
232 (give-up-ir1-transform))
233 (let ((el-type (array-type-specialized-element-type array-type))
234 (total-size (if (member '* dims)
237 `(data-vector-set (truly-the (simple-array ,(type-specifier el-type)
239 (%array-data-vector array))
243 ;;; Transform data vector access to a form that opens up optimization
246 (deftransform data-vector-set ((array index new-value)
247 ((or simple-unboxed-array simple-vector)
249 (let ((array-type (lvar-type array)))
250 (unless (array-type-p array-type)
251 (give-up-ir1-transform))
252 (let* ((element-type (type-specifier (array-type-specialized-element-type array-type)))
253 (saetp (find element-type
254 sb!vm:*specialized-array-element-type-properties*
255 :key #'sb!vm:saetp-specifier :test #'equal)))
256 (unless (>= (sb!vm:saetp-n-bits saetp) sb!vm:n-byte-bits)
257 (give-up-ir1-transform))
258 `(data-vector-set-with-offset array index 0 new-value))))
261 (deftransform data-vector-set-with-offset ((array index offset new-value)
262 ((or simple-unboxed-array
265 (let ((array-type (lvar-type array)))
266 (unless (array-type-p array-type)
267 (give-up-ir1-transform))
268 (let* ((element-type (type-specifier (array-type-specialized-element-type array-type)))
269 (saetp (find element-type
270 sb!vm:*specialized-array-element-type-properties*
271 :key #'sb!vm:saetp-specifier :test #'equal)))
272 (aver (>= (sb!vm:saetp-n-bits saetp) sb!vm:n-byte-bits))
273 (fold-index-addressing 'data-vector-set-with-offset
274 (sb!vm:saetp-n-bits saetp)
275 sb!vm:other-pointer-lowtag
276 sb!vm:vector-data-offset
279 (defoptimizer (%data-vector-and-index derive-type) ((array index))
280 (let ((atype (lvar-type array)))
281 (when (array-type-p atype)
282 (values-specifier-type
283 `(values (simple-array ,(type-specifier
284 (array-type-specialized-element-type atype))
288 (deftransform %data-vector-and-index ((%array %index)
291 ;; KLUDGE: why the percent signs? Well, ARRAY and INDEX are
292 ;; respectively exported from the CL and SB!INT packages, which
293 ;; means that they're visible to all sorts of things. If the
294 ;; compiler can prove that the call to ARRAY-HEADER-P, below, either
295 ;; returns T or NIL, it will delete the irrelevant branch. However,
296 ;; user code might have got here with a variable named CL:ARRAY, and
297 ;; quite often compiler code with a variable named SB!INT:INDEX, so
298 ;; this can generate code deletion notes for innocuous user code:
299 ;; (DEFUN F (ARRAY I) (DECLARE (SIMPLE-VECTOR ARRAY)) (AREF ARRAY I))
300 ;; -- CSR, 2003-04-01
302 ;; We do this solely for the -OR-GIVE-UP side effect, since we want
303 ;; to know that the type can be figured out in the end before we
304 ;; proceed, but we don't care yet what the type will turn out to be.
305 (upgraded-element-type-specifier-or-give-up %array)
307 '(if (array-header-p %array)
308 (values (%array-data-vector %array) %index)
309 (values %array %index)))
311 ;;; transforms for getting at simple arrays of (UNSIGNED-BYTE N) when (< N 8)
313 ;;; FIXME: In CMU CL, these were commented out with #+NIL. Why? Should
314 ;;; we fix them or should we delete them? (Perhaps these definitions
315 ;;; predate the various DATA-VECTOR-REF-FOO VOPs which have
316 ;;; (:TRANSLATE DATA-VECTOR-REF), and are redundant now?)
320 (let ((elements-per-word (truncate sb!vm:n-word-bits bits)))
322 (deftransform data-vector-ref ((vector index)
324 `(multiple-value-bind (word bit)
325 (floor index ,',elements-per-word)
326 (ldb ,(ecase sb!vm:target-byte-order
327 (:little-endian '(byte ,bits (* bit ,bits)))
328 (:big-endian '(byte ,bits (- sb!vm:n-word-bits
329 (* (1+ bit) ,bits)))))
330 (%raw-bits vector (+ word sb!vm:vector-data-offset)))))
331 (deftransform data-vector-set ((vector index new-value)
333 `(multiple-value-bind (word bit)
334 (floor index ,',elements-per-word)
335 (setf (ldb ,(ecase sb!vm:target-byte-order
336 (:little-endian '(byte ,bits (* bit ,bits)))
338 '(byte ,bits (- sb!vm:n-word-bits
339 (* (1+ bit) ,bits)))))
340 (%raw-bits vector (+ word sb!vm:vector-data-offset)))
342 (frob simple-bit-vector 1)
343 (frob (simple-array (unsigned-byte 2) (*)) 2)
344 (frob (simple-array (unsigned-byte 4) (*)) 4))
346 ;;;; BIT-VECTOR hackery
348 ;;; SIMPLE-BIT-VECTOR bit-array operations are transformed to a word
349 ;;; loop that does 32 bits at a time.
351 ;;; FIXME: This is a lot of repeatedly macroexpanded code. It should
352 ;;; be a function call instead.
353 (macrolet ((def (bitfun wordfun)
354 `(deftransform ,bitfun ((bit-array-1 bit-array-2 result-bit-array)
359 :node node :policy (>= speed space))
361 ,@(unless (policy node (zerop safety))
362 '((unless (= (length bit-array-1)
364 (length result-bit-array))
365 (error "Argument and/or result bit arrays are not the same length:~
370 (let ((length (length result-bit-array)))
372 ;; We avoid doing anything to 0-length
373 ;; bit-vectors, or rather, the memory that
374 ;; follows them. Other divisible-by-32 cases
375 ;; are handled by the (1- length), below.
378 (do ((index sb!vm:vector-data-offset (1+ index))
379 (end-1 (+ sb!vm:vector-data-offset
380 ;; bit-vectors of length 1-32
381 ;; need precisely one (SETF
382 ;; %RAW-BITS), done here in the
383 ;; epilogue. - CSR, 2002-04-24
384 (truncate (truly-the index (1- length))
385 sb!vm:n-word-bits))))
387 (setf (%raw-bits result-bit-array index)
388 (,',wordfun (%raw-bits bit-array-1 index)
389 (%raw-bits bit-array-2 index)))
391 (declare (optimize (speed 3) (safety 0))
392 (type index index end-1))
393 (setf (%raw-bits result-bit-array index)
394 (,',wordfun (%raw-bits bit-array-1 index)
395 (%raw-bits bit-array-2 index))))))))))
396 (def bit-and word-logical-and)
397 (def bit-ior word-logical-or)
398 (def bit-xor word-logical-xor)
399 (def bit-eqv word-logical-eqv)
400 (def bit-nand word-logical-nand)
401 (def bit-nor word-logical-nor)
402 (def bit-andc1 word-logical-andc1)
403 (def bit-andc2 word-logical-andc2)
404 (def bit-orc1 word-logical-orc1)
405 (def bit-orc2 word-logical-orc2))
407 (deftransform bit-not
408 ((bit-array result-bit-array)
409 (simple-bit-vector simple-bit-vector) *
410 :node node :policy (>= speed space))
412 ,@(unless (policy node (zerop safety))
413 '((unless (= (length bit-array)
414 (length result-bit-array))
415 (error "Argument and result bit arrays are not the same length:~
417 bit-array result-bit-array))))
418 (let ((length (length result-bit-array)))
420 ;; We avoid doing anything to 0-length bit-vectors, or rather,
421 ;; the memory that follows them. Other divisible-by
422 ;; n-word-bits cases are handled by the (1- length), below.
425 (do ((index sb!vm:vector-data-offset (1+ index))
426 (end-1 (+ sb!vm:vector-data-offset
427 ;; bit-vectors of length 1 to n-word-bits need
428 ;; precisely one (SETF %RAW-BITS), done here in
429 ;; the epilogue. - CSR, 2002-04-24
430 (truncate (truly-the index (1- length))
431 sb!vm:n-word-bits))))
433 (setf (%raw-bits result-bit-array index)
434 (word-logical-not (%raw-bits bit-array index)))
436 (declare (optimize (speed 3) (safety 0))
437 (type index index end-1))
438 (setf (%raw-bits result-bit-array index)
439 (word-logical-not (%raw-bits bit-array index))))))))
441 (deftransform bit-vector-= ((x y) (simple-bit-vector simple-bit-vector))
442 `(and (= (length x) (length y))
443 (let ((length (length x)))
445 (do* ((i sb!vm:vector-data-offset (+ i 1))
446 (end-1 (+ sb!vm:vector-data-offset
447 (floor (1- length) sb!vm:n-word-bits))))
449 (let* ((extra (1+ (mod (1- length) sb!vm:n-word-bits)))
450 (mask (ash #.(1- (ash 1 sb!vm:n-word-bits))
451 (- extra sb!vm:n-word-bits)))
455 ,(ecase sb!c:*backend-byte-order*
458 '(- sb!vm:n-word-bits extra))))
463 ,(ecase sb!c:*backend-byte-order*
466 '(- sb!vm:n-word-bits extra))))
468 (declare (type (integer 1 #.sb!vm:n-word-bits) extra)
469 (type sb!vm:word mask numx numy))
471 (declare (type index i end-1))
472 (let ((numx (%raw-bits x i))
473 (numy (%raw-bits y i)))
474 (declare (type sb!vm:word numx numy))
475 (unless (= numx numy)
478 (deftransform count ((item sequence) (bit simple-bit-vector) *
479 :policy (>= speed space))
480 `(let ((length (length sequence)))
483 (do ((index sb!vm:vector-data-offset (1+ index))
485 (end-1 (+ sb!vm:vector-data-offset
486 (truncate (truly-the index (1- length))
487 sb!vm:n-word-bits))))
489 (let* ((extra (1+ (mod (1- length) sb!vm:n-word-bits)))
490 (mask (ash #.(1- (ash 1 sb!vm:n-word-bits))
491 (- extra sb!vm:n-word-bits)))
492 (bits (logand (ash mask
493 ,(ecase sb!c:*backend-byte-order*
496 '(- sb!vm:n-word-bits extra))))
497 (%raw-bits sequence index))))
498 (declare (type (integer 1 #.sb!vm:n-word-bits) extra))
499 (declare (type sb!vm:word mask bits))
500 (incf count (logcount bits))
501 ,(if (constant-lvar-p item)
502 (if (zerop (lvar-value item))
508 (declare (type index index count end-1)
509 (optimize (speed 3) (safety 0)))
510 (incf count (logcount (%raw-bits sequence index)))))))
512 (deftransform fill ((sequence item) (simple-bit-vector bit) *
513 :policy (>= speed space))
514 (let ((value (if (constant-lvar-p item)
515 (if (= (lvar-value item) 0)
517 #.(1- (ash 1 sb!vm:n-word-bits)))
518 `(if (= item 0) 0 #.(1- (ash 1 sb!vm:n-word-bits))))))
519 `(let ((length (length sequence))
523 (do ((index sb!vm:vector-data-offset (1+ index))
524 (end-1 (+ sb!vm:vector-data-offset
525 ;; bit-vectors of length 1 to n-word-bits need
526 ;; precisely one (SETF %RAW-BITS), done here
527 ;; in the epilogue. - CSR, 2002-04-24
528 (truncate (truly-the index (1- length))
529 sb!vm:n-word-bits))))
531 (setf (%raw-bits sequence index) value)
533 (declare (optimize (speed 3) (safety 0))
534 (type index index end-1))
535 (setf (%raw-bits sequence index) value))))))
537 (deftransform fill ((sequence item) (simple-base-string base-char) *
538 :policy (>= speed space))
539 (let ((value (if (constant-lvar-p item)
540 (let* ((char (lvar-value item))
541 (code (sb!xc:char-code char))
543 (dotimes (i sb!vm:n-word-bytes accum)
544 (setf accum (logior accum (ash code (* 8 i))))))
545 `(let ((code (sb!xc:char-code item)))
546 (logior ,@(loop for i from 0 below sb!vm:n-word-bytes
547 collect `(ash code ,(* 8 i))))))))
548 `(let ((length (length sequence))
550 (multiple-value-bind (times rem)
551 (truncate length sb!vm:n-word-bytes)
552 (do ((index sb!vm:vector-data-offset (1+ index))
553 (end (+ times sb!vm:vector-data-offset)))
555 (let ((place (* times sb!vm:n-word-bytes)))
556 (declare (fixnum place))
557 (dotimes (j rem sequence)
559 (setf (schar sequence (the index (+ place j))) item))))
560 (declare (optimize (speed 3) (safety 0))
562 (setf (%raw-bits sequence index) value))))))
566 ;;; FIXME: The old CMU CL code used various COPY-TO/FROM-SYSTEM-AREA
567 ;;; stuff (with all the associated bit-index cruft and overflow
568 ;;; issues) even for byte moves. In SBCL, we're converting to byte
569 ;;; moves as problems are discovered with the old code, and this is
570 ;;; currently (ca. sbcl-0.6.12.30) the main interface for code in
571 ;;; SB!KERNEL and SB!SYS (e.g. i/o code). It's not clear that it's the
572 ;;; ideal interface, though, and it probably deserves some thought.
573 (deftransform %byte-blt ((src src-start dst dst-start dst-end)
574 ((or (simple-unboxed-array (*)) system-area-pointer)
576 (or (simple-unboxed-array (*)) system-area-pointer)
579 ;; FIXME: CMU CL had a hairier implementation of this (back when it
580 ;; was still called (%PRIMITIVE BYTE-BLT). It had the small problem
581 ;; that it didn't work for large (>16M) values of SRC-START or
582 ;; DST-START. However, it might have been more efficient. In
583 ;; particular, I don't really know how much the foreign function
584 ;; call costs us here. My guess is that if the overhead is
585 ;; acceptable for SQRT and COS, it's acceptable here, but this
586 ;; should probably be checked. -- WHN
587 '(flet ((sapify (thing)
589 (system-area-pointer thing)
590 ;; FIXME: The code here rather relies on the simple
591 ;; unboxed array here having byte-sized entries. That
592 ;; should be asserted explicitly, I just haven't found
593 ;; a concise way of doing it. (It would be nice to
594 ;; declare it in the DEFKNOWN too.)
595 ((simple-unboxed-array (*)) (vector-sap thing)))))
596 (declare (inline sapify))
598 (memmove (sap+ (sapify dst) dst-start)
599 (sap+ (sapify src) src-start)
600 (- dst-end dst-start)))
603 ;;;; transforms for EQL of floating point values
605 (deftransform eql ((x y) (single-float single-float))
606 '(= (single-float-bits x) (single-float-bits y)))
608 (deftransform eql ((x y) (double-float double-float))
609 '(and (= (double-float-low-bits x) (double-float-low-bits y))
610 (= (double-float-high-bits x) (double-float-high-bits y))))
613 ;;;; modular functions
614 (define-good-modular-fun logand :unsigned)
615 (define-good-modular-fun logior :unsigned)
616 ;;; FIXME: XOR? ANDC1, ANDC2? -- CSR, 2003-09-16
619 ((def (name class width)
620 (let ((type (ecase class
621 (:unsigned 'unsigned-byte)
622 (:signed 'signed-byte))))
624 (defknown ,name (integer (integer 0)) (,type ,width)
625 (foldable flushable movable))
626 (define-modular-fun-optimizer ash ((integer count) ,class :width width)
627 (when (and (<= width ,width)
628 (or (and (constant-lvar-p count)
629 (plusp (lvar-value count)))
630 (csubtypep (lvar-type count)
631 (specifier-type '(and unsigned-byte fixnum)))))
632 (cut-to-width integer ,class width)
634 (setf (gethash ',name (modular-class-versions (find-modular-class ',class)))
636 ;; This should really be dependent on SB!VM:N-WORD-BITS, but since we
637 ;; don't have a true Alpha64 port yet, we'll have to stick to
638 ;; SB!VM:N-MACHINE-WORD-BITS for the time being. --njf, 2004-08-14
639 #!+#.(cl:if (cl:= 32 sb!vm:n-machine-word-bits) '(and) '(or))
641 #!+x86 (def sb!vm::ash-left-smod30 :signed 30)
642 (def sb!vm::ash-left-mod32 :unsigned 32))
643 #!+#.(cl:if (cl:= 64 sb!vm:n-machine-word-bits) '(and) '(or))
645 #!+x86-64 (def sb!vm::ash-left-smod61 :signed 61)
646 (def sb!vm::ash-left-mod64 :unsigned 64)))
649 ;;;; word-wise logical operations
651 ;;; These transforms assume the presence of modular arithmetic to
652 ;;; generate efficient code.
654 (define-source-transform word-logical-not (x)
655 `(logand (lognot (the sb!vm:word ,x)) #.(1- (ash 1 sb!vm:n-word-bits))))
657 (deftransform word-logical-and ((x y))
660 (deftransform word-logical-nand ((x y))
661 '(logand (lognand x y) #.(1- (ash 1 sb!vm:n-word-bits))))
663 (deftransform word-logical-or ((x y))
666 (deftransform word-logical-nor ((x y))
667 '(logand (lognor x y) #.(1- (ash 1 sb!vm:n-word-bits))))
669 (deftransform word-logical-xor ((x y))
672 (deftransform word-logical-eqv ((x y))
673 '(logand (logeqv x y) #.(1- (ash 1 sb!vm:n-word-bits))))
675 (deftransform word-logical-orc1 ((x y))
676 '(logand (logorc1 x y) #.(1- (ash 1 sb!vm:n-word-bits))))
678 (deftransform word-logical-orc2 ((x y))
679 '(logand (logorc2 x y) #.(1- (ash 1 sb!vm:n-word-bits))))
681 (deftransform word-logical-andc1 ((x y))
682 '(logand (logandc1 x y) #.(1- (ash 1 sb!vm:n-word-bits))))
684 (deftransform word-logical-andc2 ((x y))
685 '(logand (logandc2 x y) #.(1- (ash 1 sb!vm:n-word-bits))))
688 ;;; There are two different ways the multiplier can be recoded. The
689 ;;; more obvious is to shift X by the correct amount for each bit set
690 ;;; in Y and to sum the results. But if there is a string of bits that
691 ;;; are all set, you can add X shifted by one more then the bit
692 ;;; position of the first set bit and subtract X shifted by the bit
693 ;;; position of the last set bit. We can't use this second method when
694 ;;; the high order bit is bit 31 because shifting by 32 doesn't work
696 (defun ub32-strength-reduce-constant-multiply (arg num)
697 (declare (type (unsigned-byte 32) num))
698 (let ((adds 0) (shifts 0)
699 (result nil) first-one)
700 (labels ((add (next-factor)
703 (progn (incf adds) `(+ ,result ,next-factor))
705 (declare (inline add))
708 (when (not (logbitp bitpos num))
709 (add (if (= (1+ first-one) bitpos)
710 ;; There is only a single bit in the string.
711 (progn (incf shifts) `(ash ,arg ,first-one))
712 ;; There are at least two.
716 `(- (ash ,arg ,bitpos)
717 (ash ,arg ,first-one)))))
718 (setf first-one nil))
719 (when (logbitp bitpos num)
720 (setf first-one bitpos))))
722 (cond ((= first-one 31))
723 ((= first-one 30) (incf shifts) (add `(ash ,arg 30)))
727 (add `(- (ash ,arg 31)
728 (ash ,arg ,first-one)))))
730 (add `(ash ,arg 31))))
731 (values (if (plusp adds)
732 `(logand ,result #.(1- (ash 1 32))) ; using modular arithmetic