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)
24 (define-source-transform char-int (x)
27 (deftransform abs ((x) (rational))
28 '(if (< x 0) (- x) x))
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)))
36 ;;;; character support
38 ;;; In our implementation there are really only BASE-CHARs.
39 (define-source-transform characterp (obj)
42 ;;;; simplifying HAIRY-DATA-VECTOR-REF and HAIRY-DATA-VECTOR-SET
44 (deftransform hairy-data-vector-ref ((array index) (array t) * :important t)
45 "avoid runtime dispatch on array element type"
46 (let ((element-ctype (extract-upgraded-element-type array)))
47 (declare (type ctype element-ctype))
48 (when (eq *wild-type* element-ctype)
49 (give-up-ir1-transform
50 "Upgraded element type of array is not known at compile time."))
51 ;; (The expansion here is basically a degenerate case of
52 ;; WITH-ARRAY-DATA. Since WITH-ARRAY-DATA is implemented as a
53 ;; macro, and macros aren't expanded in transform output, we have
54 ;; to hand-expand it ourselves.)
55 (let ((element-type-specifier (type-specifier element-ctype)))
56 `(multiple-value-bind (array index)
57 (%data-vector-and-index array index)
58 (declare (type (simple-array ,element-type-specifier 1) array))
59 (data-vector-ref array index)))))
61 (deftransform data-vector-ref ((array index)
63 (let ((array-type (continuation-type array)))
64 (unless (array-type-p array-type)
65 (give-up-ir1-transform))
66 (let ((dims (array-type-dimensions array-type)))
67 (when (or (atom dims) (= (length dims) 1))
68 (give-up-ir1-transform))
69 (let ((el-type (array-type-specialized-element-type array-type))
70 (total-size (if (member '* dims)
73 `(data-vector-ref (truly-the (simple-array ,(type-specifier el-type)
75 (%array-data-vector array))
78 (deftransform hairy-data-vector-set ((array index new-value)
82 "avoid runtime dispatch on array element type"
83 (let ((element-ctype (extract-upgraded-element-type array)))
84 (declare (type ctype element-ctype))
85 (when (eq *wild-type* element-ctype)
86 (give-up-ir1-transform
87 "Upgraded element type of array is not known at compile time."))
88 (let ((element-type-specifier (type-specifier element-ctype)))
89 `(multiple-value-bind (array index)
90 (%data-vector-and-index array index)
91 (declare (type (simple-array ,element-type-specifier 1) array)
92 (type ,element-type-specifier new-value))
93 (data-vector-set array
97 (deftransform data-vector-set ((array index new-value)
99 (let ((array-type (continuation-type array)))
100 (unless (array-type-p array-type)
101 (give-up-ir1-transform))
102 (let ((dims (array-type-dimensions array-type)))
103 (when (or (atom dims) (= (length dims) 1))
104 (give-up-ir1-transform))
105 (let ((el-type (array-type-specialized-element-type array-type))
106 (total-size (if (member '* dims)
109 `(data-vector-set (truly-the (simple-array ,(type-specifier el-type)
111 (%array-data-vector array))
115 (defoptimizer (%data-vector-and-index derive-type) ((array index))
116 (let ((atype (continuation-type array)))
117 (when (array-type-p atype)
118 (values-specifier-type
119 `(values (simple-array ,(type-specifier
120 (array-type-specialized-element-type atype))
124 (deftransform %data-vector-and-index ((array index)
129 ;; We do this solely for the -OR-GIVE-UP side effect, since we want
130 ;; to know that the type can be figured out in the end before we
131 ;; proceed, but we don't care yet what the type will turn out to be.
132 (upgraded-element-type-specifier-or-give-up array)
134 '(if (array-header-p array)
135 (values (%array-data-vector array) index)
136 (values array index)))
138 ;;; transforms for getting at simple arrays of (UNSIGNED-BYTE N) when (< N 8)
140 ;;; FIXME: In CMU CL, these were commented out with #+NIL. Why? Should
141 ;;; we fix them or should we delete them? (Perhaps these definitions
142 ;;; predate the various DATA-VECTOR-REF-FOO VOPs which have
143 ;;; (:TRANSLATE DATA-VECTOR-REF), and are redundant now?)
147 (let ((elements-per-word (truncate sb!vm:n-word-bits bits)))
149 (deftransform data-vector-ref ((vector index)
151 `(multiple-value-bind (word bit)
152 (floor index ,',elements-per-word)
153 (ldb ,(ecase sb!vm:target-byte-order
154 (:little-endian '(byte ,bits (* bit ,bits)))
155 (:big-endian '(byte ,bits (- sb!vm:n-word-bits
156 (* (1+ bit) ,bits)))))
157 (%raw-bits vector (+ word sb!vm:vector-data-offset)))))
158 (deftransform data-vector-set ((vector index new-value)
160 `(multiple-value-bind (word bit)
161 (floor index ,',elements-per-word)
162 (setf (ldb ,(ecase sb!vm:target-byte-order
163 (:little-endian '(byte ,bits (* bit ,bits)))
165 '(byte ,bits (- sb!vm:n-word-bits
166 (* (1+ bit) ,bits)))))
167 (%raw-bits vector (+ word sb!vm:vector-data-offset)))
169 (frob simple-bit-vector 1)
170 (frob (simple-array (unsigned-byte 2) (*)) 2)
171 (frob (simple-array (unsigned-byte 4) (*)) 4))
173 ;;;; BIT-VECTOR hackery
175 ;;; SIMPLE-BIT-VECTOR bit-array operations are transformed to a word
176 ;;; loop that does 32 bits at a time.
178 ;;; FIXME: This is a lot of repeatedly macroexpanded code. It should
179 ;;; be a function call instead.
180 (macrolet ((def (bitfun wordfun)
181 `(deftransform ,bitfun ((bit-array-1 bit-array-2 result-bit-array)
186 :node node :policy (>= speed space))
188 ,@(unless (policy node (zerop safety))
189 '((unless (= (length bit-array-1)
191 (length result-bit-array))
192 (error "Argument and/or result bit arrays are not the same length:~
197 (let ((length (length result-bit-array)))
199 ;; We avoid doing anything to 0-length
200 ;; bit-vectors, or rather, the memory that
201 ;; follows them. Other divisible-by-32 cases
202 ;; are handled by the (1- length), below.
205 (do ((index sb!vm:vector-data-offset (1+ index))
206 (end-1 (+ sb!vm:vector-data-offset
207 ;; bit-vectors of length 1-32
208 ;; need precisely one (SETF
209 ;; %RAW-BITS), done here in the
210 ;; epilogue. - CSR, 2002-04-24
211 (truncate (truly-the index (1- length))
212 sb!vm:n-word-bits))))
214 (setf (%raw-bits result-bit-array index)
215 (,',wordfun (%raw-bits bit-array-1 index)
216 (%raw-bits bit-array-2 index)))
218 (declare (optimize (speed 3) (safety 0))
219 (type index index end-1))
220 (setf (%raw-bits result-bit-array index)
221 (,',wordfun (%raw-bits bit-array-1 index)
222 (%raw-bits bit-array-2 index))))))))))
223 (def bit-and 32bit-logical-and)
224 (def bit-ior 32bit-logical-or)
225 (def bit-xor 32bit-logical-xor)
226 (def bit-eqv 32bit-logical-eqv)
227 (def bit-nand 32bit-logical-nand)
228 (def bit-nor 32bit-logical-nor)
229 (def bit-andc1 32bit-logical-andc1)
230 (def bit-andc2 32bit-logical-andc2)
231 (def bit-orc1 32bit-logical-orc1)
232 (def bit-orc2 32bit-logical-orc2))
234 (deftransform bit-not
235 ((bit-array result-bit-array)
236 (simple-bit-vector simple-bit-vector) *
237 :node node :policy (>= speed space))
239 ,@(unless (policy node (zerop safety))
240 '((unless (= (length bit-array)
241 (length result-bit-array))
242 (error "Argument and result bit arrays are not the same length:~
244 bit-array result-bit-array))))
245 (let ((length (length result-bit-array)))
247 ;; We avoid doing anything to 0-length bit-vectors, or
248 ;; rather, the memory that follows them. Other
249 ;; divisible-by-32 cases are handled by the (1- length),
250 ;; below. CSR, 2002-04-24
252 (do ((index sb!vm:vector-data-offset (1+ index))
253 (end-1 (+ sb!vm:vector-data-offset
254 ;; bit-vectors of length 1-32 need precisely
255 ;; one (SETF %RAW-BITS), done here in the
256 ;; epilogue. - CSR, 2002-04-24
257 (truncate (truly-the index (1- length))
258 sb!vm:n-word-bits))))
260 (setf (%raw-bits result-bit-array index)
261 (32bit-logical-not (%raw-bits bit-array index)))
263 (declare (optimize (speed 3) (safety 0))
264 (type index index end-1))
265 (setf (%raw-bits result-bit-array index)
266 (32bit-logical-not (%raw-bits bit-array index))))))))
270 ;;; FIXME: The old CMU CL code used various COPY-TO/FROM-SYSTEM-AREA
271 ;;; stuff (with all the associated bit-index cruft and overflow
272 ;;; issues) even for byte moves. In SBCL, we're converting to byte
273 ;;; moves as problems are discovered with the old code, and this is
274 ;;; currently (ca. sbcl-0.6.12.30) the main interface for code in
275 ;;; SB!KERNEL and SB!SYS (e.g. i/o code). It's not clear that it's the
276 ;;; ideal interface, though, and it probably deserves some thought.
277 (deftransform %byte-blt ((src src-start dst dst-start dst-end)
278 ((or (simple-unboxed-array (*)) system-area-pointer)
280 (or (simple-unboxed-array (*)) system-area-pointer)
283 ;; FIXME: CMU CL had a hairier implementation of this (back when it
284 ;; was still called (%PRIMITIVE BYTE-BLT). It had the small problem
285 ;; that it didn't work for large (>16M) values of SRC-START or
286 ;; DST-START. However, it might have been more efficient. In
287 ;; particular, I don't really know how much the foreign function
288 ;; call costs us here. My guess is that if the overhead is
289 ;; acceptable for SQRT and COS, it's acceptable here, but this
290 ;; should probably be checked. -- WHN
291 '(flet ((sapify (thing)
293 (system-area-pointer thing)
294 ;; FIXME: The code here rather relies on the simple
295 ;; unboxed array here having byte-sized entries. That
296 ;; should be asserted explicitly, I just haven't found
297 ;; a concise way of doing it. (It would be nice to
298 ;; declare it in the DEFKNOWN too.)
299 ((simple-unboxed-array (*)) (vector-sap thing)))))
300 (declare (inline sapify))
302 (memmove (sap+ (sapify dst) dst-start)
303 (sap+ (sapify src) src-start)
304 (- dst-end dst-start)))
307 ;;;; transforms for EQL of floating point values
309 (deftransform eql ((x y) (single-float single-float))
310 '(= (single-float-bits x) (single-float-bits y)))
312 (deftransform eql ((x y) (double-float double-float))
313 '(and (= (double-float-low-bits x) (double-float-low-bits y))
314 (= (double-float-high-bits x) (double-float-high-bits y))))