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 ;;; FIXME: It would be good to implement SB!XC:DEFCONSTANT, and use
15 ;;; use that here, so that the compiler is born knowing this value.
16 ;;; FIXME: Add a comment telling whether this holds for all vectors
17 ;;; or only for vectors based on simple arrays (non-adjustable, etc.).
18 (defconstant vector-data-bit-offset
19 (* sb!vm:vector-data-offset sb!vm:word-bits))
21 ;;; We need to define these predicates, since the TYPEP source
22 ;;; transform picks whichever predicate was defined last when there
23 ;;; are multiple predicates for equivalent types.
24 (def-source-transform short-float-p (x) `(single-float-p ,x))
26 (def-source-transform long-float-p (x) `(double-float-p ,x))
28 (def-source-transform compiled-function-p (x)
31 (def-source-transform char-int (x)
34 (deftransform abs ((x) (rational))
35 '(if (< x 0) (- x) x))
37 ;;; The layout is stored in slot 0.
38 (def-source-transform %instance-layout (x)
39 `(truly-the layout (%instance-ref ,x 0)))
40 (def-source-transform %set-instance-layout (x val)
41 `(%instance-set ,x 0 (the layout ,val)))
43 ;;;; character support
45 ;;; In our implementation there are really only BASE-CHARs.
46 (def-source-transform characterp (obj)
49 ;;;; simplifying HAIRY-DATA-VECTOR-REF and HAIRY-DATA-VECTOR-SET
51 (deftransform hairy-data-vector-ref ((array index) (array t) * :important t)
52 "avoid runtime dispatch on array element type"
53 (let ((element-ctype (extract-upgraded-element-type array)))
54 (declare (type ctype element-ctype))
55 (when (eq *wild-type* element-ctype)
56 (give-up-ir1-transform
57 "Upgraded element type of array is not known at compile time."))
58 ;; (The expansion here is basically a degenerate case of
59 ;; WITH-ARRAY-DATA. Since WITH-ARRAY-DATA is implemented as a
60 ;; macro, and macros aren't expanded in transform output, we have
61 ;; to hand-expand it ourselves.)
62 (let ((element-type-specifier (type-specifier element-ctype)))
63 `(multiple-value-bind (array index)
64 ;; FIXME: All this noise should move into a
65 ;; %DATA-VECTOR-AND-INDEX function, and there should be
66 ;; DEFTRANSFORMs for %DATA-VECTOR-AND-INDEX to optimize the
67 ;; function call away when the array is known to be simple,
68 ;; and to specialize to
69 ;; %DATA-VECTOR-AND-INDEX-IN-VECTOR-CASE when the array is
70 ;; known to have only one dimension.
71 (if (array-header-p array)
72 (%with-array-data array index nil)
74 (declare (type (simple-array ,element-type-specifier 1)
76 (%check-bound array 0 index)
77 (values array index)))
78 (declare (type (simple-array ,element-type-specifier 1) array))
79 (data-vector-ref array index)))))
81 (deftransform data-vector-ref ((array index)
83 (let ((array-type (continuation-type array)))
84 (unless (array-type-p array-type)
85 (give-up-ir1-transform))
86 (let ((dims (array-type-dimensions array-type)))
87 (when (or (atom dims) (= (length dims) 1))
88 (give-up-ir1-transform))
89 (let ((el-type (array-type-element-type array-type))
90 (total-size (if (member '* dims)
93 `(data-vector-ref (truly-the (simple-array ,(type-specifier el-type)
95 (%array-data-vector array))
98 (deftransform hairy-data-vector-set ((array index new-value)
102 "avoid runtime dispatch on array element type"
103 (let ((element-ctype (extract-upgraded-element-type array)))
104 (declare (type ctype element-ctype))
105 (when (eq *wild-type* element-ctype)
106 (give-up-ir1-transform
107 "Upgraded element type of array is not known at compile time."))
108 (let ((element-type-specifier (type-specifier element-ctype)))
109 `(multiple-value-bind (array index)
110 ;; FIXME: All this noise should move into a
111 ;; %DATA-VECTOR-AND-INDEX function, and there should be
112 ;; DEFTRANSFORMs for %DATA-VECTOR-AND-INDEX to optimize the
113 ;; function call away when the array is known to be simple,
114 ;; and to specialize to
115 ;; %DATA-VECTOR-AND-INDEX-IN-VECTOR-CASE when the array is
116 ;; known to have only one dimension.
117 (if (array-header-p array)
118 (%with-array-data array index nil)
120 (declare (type (simple-array ,element-type-specifier 1)
122 (%check-bound array 0 index)
123 (values array index)))
124 (data-vector-set (truly-the (simple-array ,element-type-specifier 1)
129 (deftransform data-vector-set ((array index new-value)
131 (let ((array-type (continuation-type array)))
132 (unless (array-type-p array-type)
133 (give-up-ir1-transform))
134 (let ((dims (array-type-dimensions array-type)))
135 (when (or (atom dims) (= (length dims) 1))
136 (give-up-ir1-transform))
137 (let ((el-type (array-type-element-type array-type))
138 (total-size (if (member '* dims)
141 `(data-vector-set (truly-the (simple-array ,(type-specifier el-type)
143 (%array-data-vector array))
147 ;;; transforms for getting at simple arrays of (UNSIGNED-BYTE N) when (< N 8)
149 ;;; FIXME: In CMU CL, these were commented out with #+NIL. Why? Should
150 ;;; we fix them or should we delete them? (Perhaps these definitions
151 ;;; predate the various DATA-VECTOR-REF-FOO VOPs which have
152 ;;; (:TRANSLATE DATA-VECTOR-REF), and are redundant now?)
156 (let ((elements-per-word (truncate sb!vm:word-bits bits)))
158 (deftransform data-vector-ref ((vector index)
160 `(multiple-value-bind (word bit)
161 (floor index ,',elements-per-word)
162 (ldb ,(ecase sb!vm:target-byte-order
163 (:little-endian '(byte ,bits (* bit ,bits)))
164 (:big-endian '(byte ,bits (- sb!vm:word-bits
165 (* (1+ bit) ,bits)))))
166 (%raw-bits vector (+ word sb!vm:vector-data-offset)))))
167 (deftransform data-vector-set ((vector index new-value)
169 `(multiple-value-bind (word bit)
170 (floor index ,',elements-per-word)
171 (setf (ldb ,(ecase sb!vm:target-byte-order
172 (:little-endian '(byte ,bits (* bit ,bits)))
174 '(byte ,bits (- sb!vm:word-bits
175 (* (1+ bit) ,bits)))))
176 (%raw-bits vector (+ word sb!vm:vector-data-offset)))
178 (frob simple-bit-vector 1)
179 (frob (simple-array (unsigned-byte 2) (*)) 2)
180 (frob (simple-array (unsigned-byte 4) (*)) 4))
182 ;;;; bit vector hackery
184 ;;; SIMPLE-BIT-VECTOR bit-array operations are transformed to a word loop that
185 ;;; does 32 bits at a time.
187 ;;; FIXME: This is a lot of repeatedly macroexpanded code. It should be a
188 ;;; function call instead. And do it with DEF-FROB instead of DOLIST.
189 (dolist (x '((bit-and 32bit-logical-and)
190 (bit-ior 32bit-logical-or)
191 (bit-xor 32bit-logical-xor)
192 (bit-eqv 32bit-logical-eqv)
193 (bit-nand 32bit-logical-nand)
194 (bit-nor 32bit-logical-nor)
195 (bit-andc1 32bit-logical-andc1)
196 (bit-andc2 32bit-logical-andc2)
197 (bit-orc1 32bit-logical-orc1)
198 (bit-orc2 32bit-logical-orc2)))
199 (destructuring-bind (bitfun wordfun) x
201 ((bit-array-1 bit-array-2 result-bit-array)
202 '(simple-bit-vector simple-bit-vector simple-bit-vector) '*
203 :eval-name t :node node :policy (>= speed space))
205 ,@(unless (policy node (zerop safety))
206 '((unless (= (length bit-array-1) (length bit-array-2)
207 (length result-bit-array))
208 (error "Argument and/or result bit arrays are not the same length:~
210 bit-array-1 bit-array-2 result-bit-array))))
211 (do ((index sb!vm:vector-data-offset (1+ index))
212 (end (+ sb!vm:vector-data-offset
214 (+ (length bit-array-1)
217 ((= index end) result-bit-array)
218 (declare (optimize (speed 3) (safety 0))
219 (type index index end))
220 (setf (%raw-bits result-bit-array index)
221 (,wordfun (%raw-bits bit-array-1 index)
222 (%raw-bits bit-array-2 index))))))))
224 (deftransform bit-not
225 ((bit-array result-bit-array)
226 (simple-bit-vector simple-bit-vector) *
227 :node node :policy (>= speed space))
229 ,@(unless (policy node (zerop safety))
230 '((unless (= (length bit-array)
231 (length result-bit-array))
232 (error "Argument and result bit arrays are not the same length:~
234 bit-array result-bit-array))))
235 (do ((index sb!vm:vector-data-offset (1+ index))
236 (end (+ sb!vm:vector-data-offset
238 (+ (length bit-array)
239 (1- sb!vm:word-bits)))
241 ((= index end) result-bit-array)
242 (declare (optimize (speed 3) (safety 0))
243 (type index index end))
244 (setf (%raw-bits result-bit-array index)
245 (32bit-logical-not (%raw-bits bit-array index))))))
249 ;;; FIXME: The old CMU CL code used various COPY-TO/FROM-SYSTEM-AREA
250 ;;; stuff (with all the associated bit-index cruft and overflow
251 ;;; issues) even for byte moves. In SBCL, we're converting to byte
252 ;;; moves as problems are discovered with the old code, and this is
253 ;;; currently (ca. sbcl-0.6.12.30) the main interface for code in
254 ;;; SB!KERNEL and SB!SYS (e.g. i/o code). It's not clear that it's the
255 ;;; ideal interface, though, and it probably deserves some thought.
256 (deftransform %byte-blt ((src src-start dst dst-start dst-end)
257 ((or (simple-unboxed-array (*)) system-area-pointer)
259 (or (simple-unboxed-array (*)) system-area-pointer)
262 ;; FIXME: CMU CL had a hairier implementation of this (back when it
263 ;; was still called (%PRIMITIVE BYTE-BLT). It had the small problem
264 ;; that it didn't work for large (>16M) values of SRC-START or
265 ;; DST-START. However, it might have been more efficient. In
266 ;; particular, I don't really know how much the foreign function
267 ;; call costs us here. My guess is that if the overhead is
268 ;; acceptable for SQRT and COS, it's acceptable here, but this
269 ;; should probably be checked. -- WHN
270 '(flet ((sapify (thing)
272 (system-area-pointer thing)
273 ;; FIXME: The code here rather relies on the simple
274 ;; unboxed array here having byte-sized entries. That
275 ;; should be asserted explicitly, I just haven't found
276 ;; a concise way of doing it. (It would be nice to
277 ;; declare it in the DEFKNOWN too.)
278 ((simple-unboxed-array (*)) (vector-sap thing)))))
279 (declare (inline sapify))
281 (memmove (sap+ (sapify dst) dst-start)
282 (sap+ (sapify src) src-start)
283 (- dst-end dst-start)))
286 ;;;; transforms for EQL of floating point values
288 (deftransform eql ((x y) (single-float single-float))
289 '(= (single-float-bits x) (single-float-bits y)))
291 (deftransform eql ((x y) (double-float double-float))
292 '(and (= (double-float-low-bits x) (double-float-low-bits y))
293 (= (double-float-high-bits x) (double-float-high-bits y))))