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 ;; FIXME: All this noise should move into a
58 ;; %DATA-VECTOR-AND-INDEX function, and there should be
59 ;; DEFTRANSFORMs for %DATA-VECTOR-AND-INDEX to optimize the
60 ;; function call away when the array is known to be simple,
61 ;; and to specialize to
62 ;; %DATA-VECTOR-AND-INDEX-IN-VECTOR-CASE when the array is
63 ;; known to have only one dimension.
64 (if (array-header-p array)
65 (%with-array-data array index nil)
67 (declare (type (simple-array ,element-type-specifier 1)
69 (%check-bound array 0 index)
70 (values array index)))
71 (declare (type (simple-array ,element-type-specifier 1) array))
72 (data-vector-ref array index)))))
74 (deftransform data-vector-ref ((array index)
76 (let ((array-type (continuation-type array)))
77 (unless (array-type-p array-type)
78 (give-up-ir1-transform))
79 (let ((dims (array-type-dimensions array-type)))
80 (when (or (atom dims) (= (length dims) 1))
81 (give-up-ir1-transform))
82 (let ((el-type (array-type-element-type array-type))
83 (total-size (if (member '* dims)
86 `(data-vector-ref (truly-the (simple-array ,(type-specifier el-type)
88 (%array-data-vector array))
91 (deftransform hairy-data-vector-set ((array index new-value)
95 "avoid runtime dispatch on array element type"
96 (let ((element-ctype (extract-upgraded-element-type array)))
97 (declare (type ctype element-ctype))
98 (when (eq *wild-type* element-ctype)
99 (give-up-ir1-transform
100 "Upgraded element type of array is not known at compile time."))
101 (let ((element-type-specifier (type-specifier element-ctype)))
102 `(multiple-value-bind (array index)
103 ;; FIXME: All this noise should move into a
104 ;; %DATA-VECTOR-AND-INDEX function, and there should be
105 ;; DEFTRANSFORMs for %DATA-VECTOR-AND-INDEX to optimize the
106 ;; function call away when the array is known to be simple,
107 ;; and to specialize to
108 ;; %DATA-VECTOR-AND-INDEX-IN-VECTOR-CASE when the array is
109 ;; known to have only one dimension.
110 (if (array-header-p array)
111 (%with-array-data array index nil)
113 (declare (type (simple-array ,element-type-specifier 1)
115 (%check-bound array 0 index)
116 (values array index)))
117 (data-vector-set (truly-the (simple-array ,element-type-specifier 1)
122 (deftransform data-vector-set ((array index new-value)
124 (let ((array-type (continuation-type array)))
125 (unless (array-type-p array-type)
126 (give-up-ir1-transform))
127 (let ((dims (array-type-dimensions array-type)))
128 (when (or (atom dims) (= (length dims) 1))
129 (give-up-ir1-transform))
130 (let ((el-type (array-type-element-type array-type))
131 (total-size (if (member '* dims)
134 `(data-vector-set (truly-the (simple-array ,(type-specifier el-type)
136 (%array-data-vector array))
140 ;;; transforms for getting at simple arrays of (UNSIGNED-BYTE N) when (< N 8)
142 ;;; FIXME: In CMU CL, these were commented out with #+NIL. Why? Should
143 ;;; we fix them or should we delete them? (Perhaps these definitions
144 ;;; predate the various DATA-VECTOR-REF-FOO VOPs which have
145 ;;; (:TRANSLATE DATA-VECTOR-REF), and are redundant now?)
149 (let ((elements-per-word (truncate sb!vm:n-word-bits bits)))
151 (deftransform data-vector-ref ((vector index)
153 `(multiple-value-bind (word bit)
154 (floor index ,',elements-per-word)
155 (ldb ,(ecase sb!vm:target-byte-order
156 (:little-endian '(byte ,bits (* bit ,bits)))
157 (:big-endian '(byte ,bits (- sb!vm:n-word-bits
158 (* (1+ bit) ,bits)))))
159 (%raw-bits vector (+ word sb!vm:vector-data-offset)))))
160 (deftransform data-vector-set ((vector index new-value)
162 `(multiple-value-bind (word bit)
163 (floor index ,',elements-per-word)
164 (setf (ldb ,(ecase sb!vm:target-byte-order
165 (:little-endian '(byte ,bits (* bit ,bits)))
167 '(byte ,bits (- sb!vm:n-word-bits
168 (* (1+ bit) ,bits)))))
169 (%raw-bits vector (+ word sb!vm:vector-data-offset)))
171 (frob simple-bit-vector 1)
172 (frob (simple-array (unsigned-byte 2) (*)) 2)
173 (frob (simple-array (unsigned-byte 4) (*)) 4))
175 ;;;; BIT-VECTOR hackery
177 ;;; SIMPLE-BIT-VECTOR bit-array operations are transformed to a word
178 ;;; loop that does 32 bits at a time.
180 ;;; FIXME: This is a lot of repeatedly macroexpanded code. It should
181 ;;; be a function call instead.
182 (macrolet ((def (bitfun wordfun)
183 `(deftransform ,bitfun ((bit-array-1 bit-array-2 result-bit-array)
188 :node node :policy (>= speed space))
190 ,@(unless (policy node (zerop safety))
191 '((unless (= (length bit-array-1)
193 (length result-bit-array))
194 (error "Argument and/or result bit arrays are not the same length:~
199 (do ((index sb!vm:vector-data-offset (1+ index))
200 (end (+ sb!vm:vector-data-offset
202 (+ (length bit-array-1)
203 sb!vm:n-word-bits -1))
204 sb!vm:n-word-bits))))
205 ((= index end) result-bit-array)
206 (declare (optimize (speed 3) (safety 0))
207 (type index index end))
208 (setf (%raw-bits result-bit-array index)
209 (,',wordfun (%raw-bits bit-array-1 index)
210 (%raw-bits bit-array-2 index))))))))
211 (def bit-and 32bit-logical-and)
212 (def bit-ior 32bit-logical-or)
213 (def bit-xor 32bit-logical-xor)
214 (def bit-eqv 32bit-logical-eqv)
215 (def bit-nand 32bit-logical-nand)
216 (def bit-nor 32bit-logical-nor)
217 (def bit-andc1 32bit-logical-andc1)
218 (def bit-andc2 32bit-logical-andc2)
219 (def bit-orc1 32bit-logical-orc1)
220 (def bit-orc2 32bit-logical-orc2))
222 (deftransform bit-not
223 ((bit-array result-bit-array)
224 (simple-bit-vector simple-bit-vector) *
225 :node node :policy (>= speed space))
227 ,@(unless (policy node (zerop safety))
228 '((unless (= (length bit-array)
229 (length result-bit-array))
230 (error "Argument and result bit arrays are not the same length:~
232 bit-array result-bit-array))))
233 (do ((index sb!vm:vector-data-offset (1+ index))
234 (end (+ sb!vm:vector-data-offset
236 (+ (length bit-array)
237 (1- sb!vm:n-word-bits)))
238 sb!vm:n-word-bits))))
239 ((= index end) result-bit-array)
240 (declare (optimize (speed 3) (safety 0))
241 (type index index end))
242 (setf (%raw-bits result-bit-array index)
243 (32bit-logical-not (%raw-bits bit-array index))))))
247 ;;; FIXME: The old CMU CL code used various COPY-TO/FROM-SYSTEM-AREA
248 ;;; stuff (with all the associated bit-index cruft and overflow
249 ;;; issues) even for byte moves. In SBCL, we're converting to byte
250 ;;; moves as problems are discovered with the old code, and this is
251 ;;; currently (ca. sbcl-0.6.12.30) the main interface for code in
252 ;;; SB!KERNEL and SB!SYS (e.g. i/o code). It's not clear that it's the
253 ;;; ideal interface, though, and it probably deserves some thought.
254 (deftransform %byte-blt ((src src-start dst dst-start dst-end)
255 ((or (simple-unboxed-array (*)) system-area-pointer)
257 (or (simple-unboxed-array (*)) system-area-pointer)
260 ;; FIXME: CMU CL had a hairier implementation of this (back when it
261 ;; was still called (%PRIMITIVE BYTE-BLT). It had the small problem
262 ;; that it didn't work for large (>16M) values of SRC-START or
263 ;; DST-START. However, it might have been more efficient. In
264 ;; particular, I don't really know how much the foreign function
265 ;; call costs us here. My guess is that if the overhead is
266 ;; acceptable for SQRT and COS, it's acceptable here, but this
267 ;; should probably be checked. -- WHN
268 '(flet ((sapify (thing)
270 (system-area-pointer thing)
271 ;; FIXME: The code here rather relies on the simple
272 ;; unboxed array here having byte-sized entries. That
273 ;; should be asserted explicitly, I just haven't found
274 ;; a concise way of doing it. (It would be nice to
275 ;; declare it in the DEFKNOWN too.)
276 ((simple-unboxed-array (*)) (vector-sap thing)))))
277 (declare (inline sapify))
279 (memmove (sap+ (sapify dst) dst-start)
280 (sap+ (sapify src) src-start)
281 (- dst-end dst-start)))
284 ;;;; transforms for EQL of floating point values
286 (deftransform eql ((x y) (single-float single-float))
287 '(= (single-float-bits x) (single-float-bits y)))
289 (deftransform eql ((x y) (double-float double-float))
290 '(and (= (double-float-low-bits x) (double-float-low-bits y))
291 (= (double-float-high-bits x) (double-float-high-bits y))))