1.0.2.1: DATA-VECTOR-{REF,SET}-WITH-OFFSET for the x86
[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   #!-sb-eval
23   `(functionp ,x)
24   #!+sb-eval
25   (once-only ((x x))
26     `(and (functionp ,x)
27           (not (sb!eval:interpreted-function-p ,x)))))
28
29 (define-source-transform char-int (x)
30   `(char-code ,x))
31
32 (deftransform abs ((x) (rational))
33   '(if (< x 0) (- x) x))
34
35 ;;; We don't want to clutter the bignum code.
36 #!+x86
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))
47
48 #!+x86
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
65                                             '(value)))
66            (,fun-name thing index (,func off2 off1) ,@(when setter-p
67                                                         '(value))))))))
68
69 #!+x86
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
75                          index offset))
76
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)))
86 \f
87 ;;;; character support
88
89 ;;; In our implementation there are really only BASE-CHARs.
90 #+nil
91 (define-source-transform characterp (obj)
92   `(base-char-p ,obj))
93 \f
94 ;;;; simplifying HAIRY-DATA-VECTOR-REF and HAIRY-DATA-VECTOR-SET
95
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)
101         `(etypecase string
102           ((simple-array character (*))
103            (data-vector-ref string index))
104           #!+sb-unicode
105           ((simple-array base-char (*))
106            (data-vector-ref string index))
107           ((simple-array nil (*))
108            (data-vector-ref string index))))))
109
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)
128                 bare-form
129                 `(the ,(type-specifier declared-element-ctype)
130                       ,bare-form)))))))
131
132 ;;; Transform multi-dimensional array to one dimensional data vector
133 ;;; access.
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)
143                             '*
144                             (reduce #'* dims))))
145         `(data-vector-ref (truly-the (simple-array ,(type-specifier el-type)
146                                                    (,total-size))
147                                      (%array-data-vector array))
148                           index)))))
149
150 ;;; Transform data vector access to a form that opens up optimization
151 ;;; opportunities.
152 #!+x86
153 (deftransform data-vector-ref ((array index) ((or simple-unboxed-array
154                                                   simple-vector)
155                                               t))
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))))
166
167 #!+x86
168 (deftransform data-vector-ref-with-offset ((array index offset)
169                                            ((or simple-unboxed-array
170                                                 simple-vector)
171                                             t t))
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
184                              index offset))))
185
186 (deftransform hairy-data-vector-set ((string index new-value)
187                                      (simple-string t t))
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)
192         `(etypecase string
193           ((simple-array character (*))
194            (data-vector-set string index new-value))
195           #!+sb-unicode
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))))))
200
201 (deftransform hairy-data-vector-set ((array index new-value)
202                                      (array t t)
203                                      *)
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)
221                        new-value))))))))
222
223 ;;; Transform multi-dimensional array to one dimensional data vector
224 ;;; access.
225 (deftransform data-vector-set ((array index new-value)
226                                (simple-array t t))
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)
235                             '*
236                             (reduce #'* dims))))
237         `(data-vector-set (truly-the (simple-array ,(type-specifier el-type)
238                                                    (,total-size))
239                                      (%array-data-vector array))
240                           index
241                           new-value)))))
242
243 ;;; Transform data vector access to a form that opens up optimization
244 ;;; opportunities.
245 #!+x86
246 (deftransform data-vector-set ((array index new-value)
247                                ((or simple-unboxed-array simple-vector)
248                                 t t))
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))))
259
260 #!+x86
261 (deftransform data-vector-set-with-offset ((array index offset new-value)
262                                            ((or simple-unboxed-array
263                                                 simple-vector)
264                                             t t t))
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
277                              index offset t))))
278
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))
285                               (*))
286                 index)))))
287
288 (deftransform %data-vector-and-index ((%array %index)
289                                       (simple-array t)
290                                       *)
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
301
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)
306
307   '(if (array-header-p %array)
308        (values (%array-data-vector %array) %index)
309        (values %array %index)))
310
311 ;;; transforms for getting at simple arrays of (UNSIGNED-BYTE N) when (< N 8)
312 ;;;
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?)
317 #+nil
318 (macrolet
319     ((frob (type bits)
320        (let ((elements-per-word (truncate sb!vm:n-word-bits bits)))
321          `(progn
322             (deftransform data-vector-ref ((vector index)
323                                            (,type *))
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)
332                                            (,type * *))
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)))
337                                (:big-endian
338                                 '(byte ,bits (- sb!vm:n-word-bits
339                                                 (* (1+ bit) ,bits)))))
340                             (%raw-bits vector (+ word sb!vm:vector-data-offset)))
341                        new-value)))))))
342   (frob simple-bit-vector 1)
343   (frob (simple-array (unsigned-byte 2) (*)) 2)
344   (frob (simple-array (unsigned-byte 4) (*)) 4))
345 \f
346 ;;;; BIT-VECTOR hackery
347
348 ;;; SIMPLE-BIT-VECTOR bit-array operations are transformed to a word
349 ;;; loop that does 32 bits at a time.
350 ;;;
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)
355                                      (simple-bit-vector
356                                       simple-bit-vector
357                                       simple-bit-vector)
358                                      *
359                                      :node node :policy (>= speed space))
360                 `(progn
361                    ,@(unless (policy node (zerop safety))
362                              '((unless (= (length bit-array-1)
363                                           (length bit-array-2)
364                                           (length result-bit-array))
365                                  (error "Argument and/or result bit arrays are not the same length:~
366                          ~%  ~S~%  ~S  ~%  ~S"
367                                         bit-array-1
368                                         bit-array-2
369                                         result-bit-array))))
370                   (let ((length (length result-bit-array)))
371                     (if (= length 0)
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.
376                         ;; CSR, 2002-04-24
377                         result-bit-array
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))))
386                             ((>= index end-1)
387                              (setf (%raw-bits result-bit-array index)
388                                    (,',wordfun (%raw-bits bit-array-1 index)
389                                                (%raw-bits bit-array-2 index)))
390                              result-bit-array)
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))
406
407 (deftransform bit-not
408               ((bit-array result-bit-array)
409                (simple-bit-vector simple-bit-vector) *
410                :node node :policy (>= speed space))
411   `(progn
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:~
416                      ~%  ~S~%  ~S"
417                     bit-array result-bit-array))))
418     (let ((length (length result-bit-array)))
419       (if (= length 0)
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.
423           ;; CSR, 2002-04-24
424           result-bit-array
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))))
432               ((>= index end-1)
433                (setf (%raw-bits result-bit-array index)
434                      (word-logical-not (%raw-bits bit-array index)))
435                result-bit-array)
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))))))))
440
441 (deftransform bit-vector-= ((x y) (simple-bit-vector simple-bit-vector))
442   `(and (= (length x) (length y))
443         (let ((length (length x)))
444           (or (= length 0)
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))))
448                    ((>= i end-1)
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)))
452                            (numx
453                             (logand
454                              (ash mask
455                                   ,(ecase sb!c:*backend-byte-order*
456                                      (:little-endian 0)
457                                      (:big-endian
458                                       '(- sb!vm:n-word-bits extra))))
459                              (%raw-bits x i)))
460                            (numy
461                             (logand
462                              (ash mask
463                                   ,(ecase sb!c:*backend-byte-order*
464                                      (:little-endian 0)
465                                      (:big-endian
466                                       '(- sb!vm:n-word-bits extra))))
467                              (%raw-bits y i))))
468                       (declare (type (integer 1 #.sb!vm:n-word-bits) extra)
469                                (type sb!vm:word mask numx numy))
470                       (= 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)
476                     (return nil))))))))
477
478 (deftransform count ((item sequence) (bit simple-bit-vector) *
479                      :policy (>= speed space))
480   `(let ((length (length sequence)))
481     (if (zerop length)
482         0
483         (do ((index sb!vm:vector-data-offset (1+ index))
484              (count 0)
485              (end-1 (+ sb!vm:vector-data-offset
486                        (truncate (truly-the index (1- length))
487                                  sb!vm:n-word-bits))))
488             ((>= index end-1)
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*
494                                                (:little-endian 0)
495                                                (:big-endian
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))
503                         '(- length count)
504                         'count)
505                     '(if (zerop item)
506                          (- length count)
507                          count))))
508           (declare (type index index count end-1)
509                    (optimize (speed 3) (safety 0)))
510           (incf count (logcount (%raw-bits sequence index)))))))
511
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)
516                        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))
520            (value ,value))
521        (if (= length 0)
522            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))))
530                ((>= index end-1)
531                 (setf (%raw-bits sequence index) value)
532                 sequence)
533              (declare (optimize (speed 3) (safety 0))
534                       (type index index end-1))
535              (setf (%raw-bits sequence index) value))))))
536
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))
542                           (accum 0))
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))
549            (value ,value))
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)))
554             ((>= index end)
555              (let ((place (* times sb!vm:n-word-bytes)))
556                (declare (fixnum place))
557                (dotimes (j rem sequence)
558                  (declare (index j))
559                  (setf (schar sequence (the index (+ place j))) item))))
560           (declare (optimize (speed 3) (safety 0))
561                    (type index index))
562           (setf (%raw-bits sequence index) value))))))
563 \f
564 ;;;; %BYTE-BLT
565
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)
575                           index
576                           (or (simple-unboxed-array (*)) system-area-pointer)
577                           index
578                           index))
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)
588             (etypecase 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))
597      (without-gcing
598       (memmove (sap+ (sapify dst) dst-start)
599                (sap+ (sapify src) src-start)
600                (- dst-end dst-start)))
601      (values)))
602 \f
603 ;;;; transforms for EQL of floating point values
604
605 (deftransform eql ((x y) (single-float single-float))
606   '(= (single-float-bits x) (single-float-bits y)))
607
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))))
611
612 \f
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
617
618 (macrolet
619     ((def (name class width)
620        (let ((type (ecase class
621                      (:unsigned 'unsigned-byte)
622                      (:signed 'signed-byte))))
623          `(progn
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)
633                 ',name))
634             (setf (gethash ',name (modular-class-versions (find-modular-class ',class)))
635                   `(ash ,',width))))))
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))
640   (progn
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))
644   (progn
645     #!+x86-64 (def sb!vm::ash-left-smod61 :signed 61)
646     (def sb!vm::ash-left-mod64 :unsigned 64)))
647
648 \f
649 ;;;; word-wise logical operations
650
651 ;;; These transforms assume the presence of modular arithmetic to
652 ;;; generate efficient code.
653
654 (define-source-transform word-logical-not (x)
655   `(logand (lognot (the sb!vm:word ,x)) #.(1- (ash 1 sb!vm:n-word-bits))))
656
657 (deftransform word-logical-and ((x y))
658   '(logand x y))
659
660 (deftransform word-logical-nand ((x y))
661   '(logand (lognand x y) #.(1- (ash 1 sb!vm:n-word-bits))))
662
663 (deftransform word-logical-or ((x y))
664   '(logior x y))
665
666 (deftransform word-logical-nor ((x y))
667   '(logand (lognor x y) #.(1- (ash 1 sb!vm:n-word-bits))))
668
669 (deftransform word-logical-xor ((x y))
670   '(logxor x y))
671
672 (deftransform word-logical-eqv ((x y))
673   '(logand (logeqv x y) #.(1- (ash 1 sb!vm:n-word-bits))))
674
675 (deftransform word-logical-orc1 ((x y))
676   '(logand (logorc1 x y) #.(1- (ash 1 sb!vm:n-word-bits))))
677
678 (deftransform word-logical-orc2 ((x y))
679   '(logand (logorc2 x y) #.(1- (ash 1 sb!vm:n-word-bits))))
680
681 (deftransform word-logical-andc1 ((x y))
682   '(logand (logandc1 x y) #.(1- (ash 1 sb!vm:n-word-bits))))
683
684 (deftransform word-logical-andc2 ((x y))
685   '(logand (logandc2 x y) #.(1- (ash 1 sb!vm:n-word-bits))))
686
687 \f
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
695 ;;; too well.
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)
701                (setf result
702                      (if result
703                          (progn (incf adds) `(+ ,result ,next-factor))
704                          next-factor))))
705       (declare (inline add))
706       (dotimes (bitpos 32)
707         (if first-one
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.
713                        (progn
714                          (incf adds)
715                          (incf shifts 2)
716                          `(- (ash ,arg ,bitpos)
717                              (ash ,arg ,first-one)))))
718               (setf first-one nil))
719             (when (logbitp bitpos num)
720               (setf first-one bitpos))))
721       (when first-one
722         (cond ((= first-one 31))
723               ((= first-one 30) (incf shifts) (add `(ash ,arg 30)))
724               (t
725                (incf shifts 2)
726                (incf adds)
727                (add `(- (ash ,arg 31)
728                         (ash ,arg ,first-one)))))
729         (incf shifts)
730         (add `(ash ,arg 31))))
731     (values (if (plusp adds)
732                 `(logand ,result #.(1- (ash 1 32))) ; using modular arithmetic
733                 result)
734             adds
735             shifts)))