Merged sbcl-1.0.14 with the sb-simd 1.3 patches
[sbcl/simd.git] / src / code / array.lisp
blob78bf43d108d93a0408bc2cddc91799c9c060d40f
1 ;;;; functions to implement arrays
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.
12 (in-package "SB!IMPL")
14 #!-sb-fluid
15 (declaim (inline fill-pointer array-has-fill-pointer-p adjustable-array-p
16 array-displacement))
18 ;;;; miscellaneous accessor functions
20 ;;; These functions are only needed by the interpreter, 'cause the
21 ;;; compiler inlines them.
22 (macrolet ((def (name)
23 `(progn
24 (defun ,name (array)
25 (,name array))
26 (defun (setf ,name) (value array)
27 (setf (,name array) value)))))
28 (def %array-fill-pointer)
29 (def %array-fill-pointer-p)
30 (def %array-available-elements)
31 (def %array-data-vector)
32 (def %array-displacement)
33 (def %array-displaced-p))
35 (defun %array-rank (array)
36 (%array-rank array))
38 (defun %array-dimension (array axis)
39 (%array-dimension array axis))
41 (defun %set-array-dimension (array axis value)
42 (%set-array-dimension array axis value))
44 (defun %check-bound (array bound index)
45 (declare (type index bound)
46 (fixnum index))
47 (%check-bound array bound index))
49 (defun %with-array-data/fp (array start end)
50 (%with-array-data-macro array start end :check-bounds t :check-fill-pointer t))
52 (defun %with-array-data (array start end)
53 (%with-array-data-macro array start end :check-bounds t :check-fill-pointer nil))
55 (defun %data-vector-and-index (array index)
56 (if (array-header-p array)
57 (multiple-value-bind (vector index)
58 (%with-array-data array index nil)
59 (values vector index))
60 (values array index)))
62 ;;;; MAKE-ARRAY
63 (eval-when (:compile-toplevel :execute)
64 (sb!xc:defmacro pick-vector-type (type &rest specs)
65 `(cond ,@(mapcar (lambda (spec)
66 `(,(if (eq (car spec) t)
68 `(subtypep ,type ',(car spec)))
69 ,@(cdr spec)))
70 specs))))
72 ;;; These functions are used in the implementation of MAKE-ARRAY for
73 ;;; complex arrays. There are lots of transforms to simplify
74 ;;; MAKE-ARRAY for various easy cases, but not for all reasonable
75 ;;; cases, so e.g. as of sbcl-0.6.6 we still make full calls to
76 ;;; MAKE-ARRAY for any non-simple array. Thus, there's some value to
77 ;;; making this somewhat efficient, at least not doing full calls to
78 ;;; SUBTYPEP in the easy cases.
79 (defun %vector-widetag-and-n-bits (type)
80 (case type
81 ;; Pick off some easy common cases.
83 ;; (Perhaps we should make a much more exhaustive table of easy
84 ;; common cases here. Or perhaps the effort would be better spent
85 ;; on smarter compiler transforms which do the calculation once
86 ;; and for all in any reasonable user programs.)
87 ((t)
88 (values #.sb!vm:simple-vector-widetag #.sb!vm:n-word-bits))
89 ((base-char standard-char #!-sb-unicode character)
90 (values #.sb!vm:simple-base-string-widetag #.sb!vm:n-byte-bits))
91 #!+sb-unicode
92 ((character)
93 (values #.sb!vm:simple-character-string-widetag #.sb!vm:n-word-bits))
94 ((bit)
95 (values #.sb!vm:simple-bit-vector-widetag 1))
96 ;; OK, we have to wade into SUBTYPEPing after all.
98 #.`(pick-vector-type type
99 ,@(map 'list
100 (lambda (saetp)
101 `(,(sb!vm:saetp-specifier saetp)
102 (values ,(sb!vm:saetp-typecode saetp)
103 ,(sb!vm:saetp-n-bits saetp))))
104 sb!vm:*specialized-array-element-type-properties*)))))
106 (defun %complex-vector-widetag (type)
107 (case type
108 ;; Pick off some easy common cases.
109 ((t)
110 #.sb!vm:complex-vector-widetag)
111 ((base-char #!-sb-unicode character)
112 #.sb!vm:complex-base-string-widetag)
113 #!+sb-unicode
114 ((character)
115 #.sb!vm:complex-character-string-widetag)
116 ((nil)
117 #.sb!vm:complex-vector-nil-widetag)
118 ((bit)
119 #.sb!vm:complex-bit-vector-widetag)
120 ;; OK, we have to wade into SUBTYPEPing after all.
122 (pick-vector-type type
123 (nil #.sb!vm:complex-vector-nil-widetag)
124 #!-sb-unicode
125 (character #.sb!vm:complex-base-string-widetag)
126 #!+sb-unicode
127 (base-char #.sb!vm:complex-base-string-widetag)
128 #!+sb-unicode
129 (character #.sb!vm:complex-character-string-widetag)
130 (bit #.sb!vm:complex-bit-vector-widetag)
131 (t #.sb!vm:complex-vector-widetag)))))
133 (defun make-array (dimensions &key
134 (element-type t)
135 (initial-element nil initial-element-p)
136 (initial-contents nil initial-contents-p)
137 adjustable fill-pointer
138 displaced-to displaced-index-offset)
139 (let* ((dimensions (if (listp dimensions) dimensions (list dimensions)))
140 (array-rank (length (the list dimensions)))
141 (simple (and (null fill-pointer)
142 (not adjustable)
143 (null displaced-to))))
144 (declare (fixnum array-rank))
145 (when (and displaced-index-offset (null displaced-to))
146 (error "can't specify :DISPLACED-INDEX-OFFSET without :DISPLACED-TO"))
147 (when (and displaced-to
148 (arrayp displaced-to)
149 (not (equal (array-element-type displaced-to)
150 (upgraded-array-element-type element-type))))
151 (error "Array element type of :DISPLACED-TO array does not match specified element type"))
152 (if (and simple (= array-rank 1))
153 ;; it's a (SIMPLE-ARRAY * (*))
154 (multiple-value-bind (type n-bits)
155 (%vector-widetag-and-n-bits element-type)
156 (declare (type (unsigned-byte 8) type)
157 (type (integer 0 256) n-bits))
158 (let* ((length (car dimensions))
159 (array (allocate-vector
160 type
161 length
162 (ceiling
163 (* (if (or (= type sb!vm:simple-base-string-widetag)
164 #!+sb-unicode
165 (= type
166 sb!vm:simple-character-string-widetag))
167 (1+ length)
168 length)
169 n-bits)
170 sb!vm:n-word-bits))))
171 (declare (type index length))
172 (when initial-element-p
173 (fill array initial-element))
174 (when initial-contents-p
175 (when initial-element-p
176 (error "can't specify both :INITIAL-ELEMENT and ~
177 :INITIAL-CONTENTS"))
178 (unless (= length (length initial-contents))
179 (error "There are ~W elements in the :INITIAL-CONTENTS, but ~
180 the vector length is ~W."
181 (length initial-contents)
182 length))
183 (replace array initial-contents))
184 array))
185 ;; it's either a complex array or a multidimensional array.
186 (let* ((total-size (reduce #'* dimensions))
187 (data (or displaced-to
188 (data-vector-from-inits
189 dimensions total-size element-type
190 initial-contents initial-contents-p
191 initial-element initial-element-p)))
192 (array (make-array-header
193 (cond ((= array-rank 1)
194 (%complex-vector-widetag element-type))
195 (simple sb!vm:simple-array-widetag)
196 (t sb!vm:complex-array-widetag))
197 array-rank)))
198 (cond (fill-pointer
199 (unless (= array-rank 1)
200 (error "Only vectors can have fill pointers."))
201 (let ((length (car dimensions)))
202 (declare (fixnum length))
203 (setf (%array-fill-pointer array)
204 (cond ((eq fill-pointer t)
205 length)
207 (unless (and (fixnump fill-pointer)
208 (>= fill-pointer 0)
209 (<= fill-pointer length))
210 ;; FIXME: should be TYPE-ERROR?
211 (error "invalid fill-pointer ~W"
212 fill-pointer))
213 fill-pointer))))
214 (setf (%array-fill-pointer-p array) t))
216 (setf (%array-fill-pointer array) total-size)
217 (setf (%array-fill-pointer-p array) nil)))
218 (setf (%array-available-elements array) total-size)
219 (setf (%array-data-vector array) data)
220 (cond (displaced-to
221 (when (or initial-element-p initial-contents-p)
222 (error "Neither :INITIAL-ELEMENT nor :INITIAL-CONTENTS ~
223 can be specified along with :DISPLACED-TO"))
224 (let ((offset (or displaced-index-offset 0)))
225 (when (> (+ offset total-size)
226 (array-total-size displaced-to))
227 (error "~S doesn't have enough elements." displaced-to))
228 (setf (%array-displacement array) offset)
229 (setf (%array-displaced-p array) t)))
231 (setf (%array-displaced-p array) nil)))
232 (let ((axis 0))
233 (dolist (dim dimensions)
234 (setf (%array-dimension array axis) dim)
235 (incf axis)))
236 array))))
238 (defun make-static-vector (length &key
239 (element-type '(unsigned-byte 8))
240 (initial-contents nil initial-contents-p)
241 (initial-element nil initial-element-p))
242 "Allocate vector of LENGTH elements in static space. Only allocation
243 of specialized arrays is supported."
244 ;; STEP 1: check inputs fully
246 ;; This way of doing explicit checks before the vector is allocated
247 ;; is expensive, but probably worth the trouble as once we've allocated
248 ;; the vector we have no way to get rid of it anymore...
249 (when (eq t (upgraded-array-element-type element-type))
250 (error "Static arrays of type ~S not supported."
251 element-type))
252 (when initial-contents-p
253 (when initial-element-p
254 (error "can't specify both :INITIAL-ELEMENT and :INITIAL-CONTENTS"))
255 (unless (= length (length initial-contents))
256 (error "There are ~W elements in the :INITIAL-CONTENTS, but the ~
257 vector length is ~W."
258 (length initial-contents)
259 length))
260 (unless (every (lambda (x) (typep x element-type)) initial-contents)
261 (error ":INITIAL-CONTENTS contains elements not of type ~S."
262 element-type)))
263 (when initial-element-p
264 (unless (typep initial-element element-type)
265 (error ":INITIAL-ELEMENT ~S is not of type ~S."
266 initial-element element-type)))
267 ;; STEP 2
269 ;; Allocate and possibly initialize the vector.
270 (multiple-value-bind (type n-bits)
271 (sb!impl::%vector-widetag-and-n-bits element-type)
272 (let ((vector
273 (allocate-static-vector type length
274 (ceiling (* length n-bits)
275 sb!vm:n-word-bits))))
276 (cond (initial-element-p
277 (fill vector initial-element))
278 (initial-contents-p
279 (replace vector initial-contents))
281 vector)))))
283 ;;; DATA-VECTOR-FROM-INITS returns a simple vector that has the
284 ;;; specified array characteristics. Dimensions is only used to pass
285 ;;; to FILL-DATA-VECTOR for error checking on the structure of
286 ;;; initial-contents.
287 (defun data-vector-from-inits (dimensions total-size element-type
288 initial-contents initial-contents-p
289 initial-element initial-element-p)
290 (when (and initial-contents-p initial-element-p)
291 (error "cannot supply both :INITIAL-CONTENTS and :INITIAL-ELEMENT to
292 either MAKE-ARRAY or ADJUST-ARRAY."))
293 (let ((data (if initial-element-p
294 (make-array total-size
295 :element-type element-type
296 :initial-element initial-element)
297 (make-array total-size
298 :element-type element-type))))
299 (cond (initial-element-p
300 (unless (simple-vector-p data)
301 (unless (typep initial-element element-type)
302 (error "~S cannot be used to initialize an array of type ~S."
303 initial-element element-type))
304 (fill (the vector data) initial-element)))
305 (initial-contents-p
306 (fill-data-vector data dimensions initial-contents)))
307 data))
309 (defun vector (&rest objects)
310 #!+sb-doc
311 "Construct a SIMPLE-VECTOR from the given objects."
312 (coerce (the list objects) 'simple-vector))
315 ;;;; accessor/setter functions
317 ;;; Dispatch to an optimized routine the data vector accessors for
318 ;;; each different specialized vector type. Do dispatching by looking
319 ;;; up the widetag in the array rather than with the typecases, which
320 ;;; as of 1.0.5 compiles to a naive sequence of linear TYPEPs. Also
321 ;;; provide separate versions where bounds checking has been moved
322 ;;; from the callee to the caller, since it's much cheaper to do once
323 ;;; the type information is available. Finally, for each of these
324 ;;; routines also provide a slow path, taken for arrays that are not
325 ;;; vectors or not simple.
326 (macrolet ((def (name table-name)
327 `(progn
328 (defvar ,table-name)
329 (defmacro ,name (array-var)
330 `(the function
331 (let ((tag 0)
332 (offset
333 #.(ecase sb!c:*backend-byte-order*
334 (:little-endian
335 (- sb!vm:other-pointer-lowtag))
336 (:big-endian
337 (- (1- sb!vm:n-word-bytes) sb!vm:other-pointer-lowtag)))))
338 ;; WIDETAG-OF needs extra code to handle LIST and
339 ;; FUNCTION lowtags. We're only dispatching on
340 ;; other pointers, so let's do the lowtag
341 ;; extraction manually.
342 (when (sb!vm::%other-pointer-p ,array-var)
343 (setf tag
344 (sb!sys:sap-ref-8 (int-sap (get-lisp-obj-address ,array-var))
345 offset)))
346 ;; SYMBOL-GLOBAL-VALUE is a performance hack
347 ;; for threaded builds.
348 (svref (sb!vm::symbol-global-value ',',table-name) tag)))))))
349 (def !find-data-vector-setter *data-vector-setters*)
350 (def !find-data-vector-setter/check-bounds *data-vector-setters/check-bounds*)
351 (def !find-data-vector-reffer *data-vector-reffers*)
352 (def !find-data-vector-reffer/check-bounds *data-vector-reffers/check-bounds*))
354 (macrolet ((%ref (accessor-getter extra-params)
355 `(funcall (,accessor-getter array) array index ,@extra-params))
356 (define (accessor-name slow-accessor-name accessor-getter
357 extra-params check-bounds)
358 `(progn
359 (defun ,accessor-name (array index ,@extra-params)
360 (declare (optimize speed
361 ;; (SAFETY 0) is ok. All calls to
362 ;; these functions are generated by
363 ;; the compiler, so argument count
364 ;; checking isn't needed. Type checking
365 ;; is done implicitly via the widetag
366 ;; dispatch.
367 (safety 0)))
368 (%ref ,accessor-getter ,extra-params))
369 (defun ,slow-accessor-name (array index ,@extra-params)
370 (declare (optimize speed (safety 0)))
371 (if (not (%array-displaced-p array))
372 ;; The reasonably quick path of non-displaced complex
373 ;; arrays.
374 (let ((array (%array-data-vector array)))
375 (%ref ,accessor-getter ,extra-params))
376 ;; The real slow path.
377 (with-array-data
378 ((vector array)
379 (index (locally
380 (declare (optimize (speed 1) (safety 1)))
381 (,@check-bounds index)))
382 (end)
383 :force-inline t)
384 (declare (ignore end))
385 (,accessor-name vector index ,@extra-params)))))))
386 (define hairy-data-vector-ref slow-hairy-data-vector-ref
387 !find-data-vector-reffer
388 nil (progn))
389 (define hairy-data-vector-set slow-hairy-data-vector-set
390 !find-data-vector-setter
391 (new-value) (progn))
392 (define hairy-data-vector-ref/check-bounds
393 slow-hairy-data-vector-ref/check-bounds
394 !find-data-vector-reffer/check-bounds
395 nil (%check-bound array (array-dimension array 0)))
396 (define hairy-data-vector-set/check-bounds
397 slow-hairy-data-vector-set/check-bounds
398 !find-data-vector-setter/check-bounds
399 (new-value) (%check-bound array (array-dimension array 0))))
401 (defun hairy-ref-error (array index &optional new-value)
402 (declare (ignore index new-value))
403 (error 'type-error
404 :datum array
405 :expected-type 'vector))
407 ;;; Populate the dispatch tables.
408 (macrolet ((define-reffer (saetp check-form)
409 (let* ((type (sb!vm:saetp-specifier saetp))
410 (atype `(simple-array ,type (*))))
411 `(named-lambda optimized-data-vector-ref (vector index)
412 (declare (optimize speed (safety 0)))
413 (data-vector-ref (the ,atype vector)
414 (locally
415 (declare (optimize (safety 1)))
416 (the index
417 (,@check-form index)))))))
418 (define-setter (saetp check-form)
419 (let* ((type (sb!vm:saetp-specifier saetp))
420 (atype `(simple-array ,type (*))))
421 `(named-lambda optimized-data-vector-set (vector index new-value)
422 (declare (optimize speed (safety 0)))
423 (data-vector-set (the ,atype vector)
424 (locally
425 (declare (optimize (safety 1)))
426 (the index
427 (,@check-form index)))
428 (locally
429 ;; SPEED 1 needed to avoid the compiler
430 ;; from downgrading the type check to
431 ;; a cheaper one.
432 (declare (optimize (speed 1)
433 (safety 1)))
434 (the ,type new-value)))
435 ;; For specialized arrays, the return from
436 ;; data-vector-set would have to be reboxed to be a
437 ;; (Lisp) return value; instead, we use the
438 ;; already-boxed value as the return.
439 new-value)))
440 (define-reffers (symbol deffer check-form slow-path)
441 `(progn
442 (setf ,symbol (make-array sb!vm::widetag-mask
443 :initial-element #'hairy-ref-error))
444 ,@(loop for widetag in '(sb!vm:complex-vector-widetag
445 sb!vm:complex-vector-nil-widetag
446 sb!vm:complex-bit-vector-widetag
447 #!+sb-unicode sb!vm:complex-character-string-widetag
448 sb!vm:complex-base-string-widetag
449 sb!vm:simple-array-widetag
450 sb!vm:complex-array-widetag)
451 collect `(setf (svref ,symbol ,widetag) ,slow-path))
452 ,@(loop for saetp across sb!vm:*specialized-array-element-type-properties*
453 for widetag = (sb!vm:saetp-typecode saetp)
454 collect `(setf (svref ,symbol ,widetag)
455 (,deffer ,saetp ,check-form))))))
456 (defun !hairy-data-vector-reffer-init ()
457 (define-reffers *data-vector-reffers* define-reffer
458 (progn)
459 #'slow-hairy-data-vector-ref)
460 (define-reffers *data-vector-setters* define-setter
461 (progn)
462 #'slow-hairy-data-vector-set)
463 (define-reffers *data-vector-reffers/check-bounds* define-reffer
464 (%check-bound vector (length vector))
465 #'slow-hairy-data-vector-ref/check-bounds)
466 (define-reffers *data-vector-setters/check-bounds* define-setter
467 (%check-bound vector (length vector))
468 #'slow-hairy-data-vector-set/check-bounds)))
470 ;;; (Ordinary DATA-VECTOR-REF usage compiles into a vop, but
471 ;;; DATA-VECTOR-REF is also FOLDABLE, and this ordinary function
472 ;;; definition is needed for the compiler to use in constant folding.)
473 (defun data-vector-ref (array index)
474 (hairy-data-vector-ref array index))
476 (defun data-vector-ref-with-offset (array index offset)
477 (hairy-data-vector-ref array (+ index offset)))
479 ;;; SUBSCRIPTS has a dynamic-extent list structure and is destroyed
480 (defun %array-row-major-index (array subscripts
481 &optional (invalid-index-error-p t))
482 (declare (array array)
483 (list subscripts))
484 (let ((rank (array-rank array)))
485 (unless (= rank (length subscripts))
486 (error "wrong number of subscripts, ~W, for array of rank ~W"
487 (length subscripts) rank))
488 (if (array-header-p array)
489 (do ((subs (nreverse subscripts) (cdr subs))
490 (axis (1- (array-rank array)) (1- axis))
491 (chunk-size 1)
492 (result 0))
493 ((null subs) result)
494 (declare (list subs) (fixnum axis chunk-size result))
495 (let ((index (car subs))
496 (dim (%array-dimension array axis)))
497 (declare (fixnum dim))
498 (unless (and (fixnump index) (< -1 index dim))
499 (if invalid-index-error-p
500 (error 'simple-type-error
501 :format-control "invalid index ~W~[~;~:; on axis ~:*~W~] in ~S"
502 :format-arguments (list index axis array)
503 :datum index
504 :expected-type `(integer 0 (,dim)))
505 (return-from %array-row-major-index nil)))
506 (incf result (* chunk-size (the fixnum index)))
507 (setf chunk-size (* chunk-size dim))))
508 (let ((index (first subscripts))
509 (length (length (the (simple-array * (*)) array))))
510 (unless (and (fixnump index) (< -1 index length))
511 (if invalid-index-error-p
512 ;; FIXME: perhaps this should share a format-string
513 ;; with INVALID-ARRAY-INDEX-ERROR or
514 ;; INDEX-TOO-LARGE-ERROR?
515 (error 'simple-type-error
516 :format-control "invalid index ~W in ~S"
517 :format-arguments (list index array)
518 :datum index
519 :expected-type `(integer 0 (,length)))
520 (return-from %array-row-major-index nil)))
521 index))))
523 (defun array-in-bounds-p (array &rest subscripts)
524 #!+sb-doc
525 "Return T if the SUBSCIPTS are in bounds for the ARRAY, NIL otherwise."
526 (if (%array-row-major-index array subscripts nil)
529 (defun array-row-major-index (array &rest subscripts)
530 (declare (dynamic-extent subscripts))
531 (%array-row-major-index array subscripts))
533 (defun aref (array &rest subscripts)
534 #!+sb-doc
535 "Return the element of the ARRAY specified by the SUBSCRIPTS."
536 (declare (dynamic-extent subscripts))
537 (row-major-aref array (%array-row-major-index array subscripts)))
539 (defun %aset (array &rest stuff)
540 (declare (dynamic-extent stuff))
541 (let ((subscripts (butlast stuff))
542 (new-value (car (last stuff))))
543 (setf (row-major-aref array (%array-row-major-index array subscripts))
544 new-value)))
546 ;;; FIXME: What's supposed to happen with functions
547 ;;; like AREF when we (DEFUN (SETF FOO) ..) when
548 ;;; DEFSETF FOO is also defined? It seems as though the logical
549 ;;; thing to do would be to nuke the macro definition for (SETF FOO)
550 ;;; and replace it with the (SETF FOO) function, issuing a warning,
551 ;;; just as for ordinary functions
552 ;;; * (LISP-IMPLEMENTATION-VERSION)
553 ;;; "18a+ release x86-linux 2.4.7 6 November 1998 cvs"
554 ;;; * (DEFMACRO ZOO (X) `(+ ,X ,X))
555 ;;; ZOO
556 ;;; * (DEFUN ZOO (X) (* 3 X))
557 ;;; Warning: ZOO previously defined as a macro.
558 ;;; ZOO
559 ;;; But that doesn't seem to be what happens in CMU CL.
561 ;;; KLUDGE: this is probably because ANSI, in its wisdom (CLHS
562 ;;; 5.1.2.5) requires implementations to support
563 ;;; (SETF (APPLY #'AREF ...) ...)
564 ;;; [and also #'BIT and #'SBIT]. Yes, this is terrifying, and it's
565 ;;; also terrifying that this sequence of definitions causes it to
566 ;;; work.
568 ;;; Also, it would be nice to make DESCRIBE FOO tell whether a symbol
569 ;;; has a setf expansion and/or a setf function defined.
571 #!-sb-fluid (declaim (inline (setf aref)))
572 (defun (setf aref) (new-value array &rest subscripts)
573 (declare (dynamic-extent subscripts))
574 (declare (type array array))
575 (setf (row-major-aref array (%array-row-major-index array subscripts))
576 new-value))
578 (defun row-major-aref (array index)
579 #!+sb-doc
580 "Return the element of array corressponding to the row-major index. This is
581 SETF'able."
582 (declare (optimize (safety 1)))
583 (row-major-aref array index))
585 (defun %set-row-major-aref (array index new-value)
586 (declare (optimize (safety 1)))
587 (setf (row-major-aref array index) new-value))
589 (defun svref (simple-vector index)
590 #!+sb-doc
591 "Return the INDEX'th element of the given Simple-Vector."
592 (declare (optimize (safety 1)))
593 (aref simple-vector index))
595 (defun %svset (simple-vector index new)
596 (declare (optimize (safety 1)))
597 (setf (aref simple-vector index) new))
599 (defun bit (bit-array &rest subscripts)
600 #!+sb-doc
601 "Return the bit from the BIT-ARRAY at the specified SUBSCRIPTS."
602 (declare (type (array bit) bit-array) (optimize (safety 1)))
603 (row-major-aref bit-array (%array-row-major-index bit-array subscripts)))
605 (defun %bitset (bit-array &rest stuff)
606 (declare (type (array bit) bit-array) (optimize (safety 1)))
607 (let ((subscripts (butlast stuff))
608 (new-value (car (last stuff))))
609 (setf (row-major-aref bit-array
610 (%array-row-major-index bit-array subscripts))
611 new-value)))
613 #!-sb-fluid (declaim (inline (setf bit)))
614 (defun (setf bit) (new-value bit-array &rest subscripts)
615 (declare (type (array bit) bit-array) (optimize (safety 1)))
616 (setf (row-major-aref bit-array
617 (%array-row-major-index bit-array subscripts))
618 new-value))
620 (defun sbit (simple-bit-array &rest subscripts)
621 #!+sb-doc
622 "Return the bit from SIMPLE-BIT-ARRAY at the specified SUBSCRIPTS."
623 (declare (type (simple-array bit) simple-bit-array) (optimize (safety 1)))
624 (row-major-aref simple-bit-array
625 (%array-row-major-index simple-bit-array subscripts)))
627 ;;; KLUDGE: Not all these things (%SET-ROW-MAJOR-AREF, %SET-FILL-POINTER,
628 ;;; %SET-FDEFINITION, %SCHARSET, %SBITSET..) seem to deserve separate names.
629 ;;; Could we just DEFUN (SETF SBIT) etc. and get rid of the non-ANSI names?
630 ;;; -- WHN 19990911
631 (defun %sbitset (simple-bit-array &rest stuff)
632 (declare (type (simple-array bit) simple-bit-array) (optimize (safety 1)))
633 (let ((subscripts (butlast stuff))
634 (new-value (car (last stuff))))
635 (setf (row-major-aref simple-bit-array
636 (%array-row-major-index simple-bit-array subscripts))
637 new-value)))
639 #!-sb-fluid (declaim (inline (setf sbit)))
640 (defun (setf sbit) (new-value bit-array &rest subscripts)
641 (declare (type (simple-array bit) bit-array) (optimize (safety 1)))
642 (setf (row-major-aref bit-array
643 (%array-row-major-index bit-array subscripts))
644 new-value))
646 ;;;; miscellaneous array properties
648 (defun array-element-type (array)
649 #!+sb-doc
650 "Return the type of the elements of the array"
651 (let ((widetag (widetag-of array)))
652 (macrolet ((pick-element-type (&rest stuff)
653 `(cond ,@(mapcar (lambda (stuff)
654 (cons
655 (let ((item (car stuff)))
656 (cond ((eq item t)
658 ((listp item)
659 (cons 'or
660 (mapcar (lambda (x)
661 `(= widetag ,x))
662 item)))
664 `(= widetag ,item))))
665 (cdr stuff)))
666 stuff))))
667 #.`(pick-element-type
668 ,@(map 'list
669 (lambda (saetp)
670 `(,(if (sb!vm:saetp-complex-typecode saetp)
671 (list (sb!vm:saetp-typecode saetp)
672 (sb!vm:saetp-complex-typecode saetp))
673 (sb!vm:saetp-typecode saetp))
674 ',(sb!vm:saetp-specifier saetp)))
675 sb!vm:*specialized-array-element-type-properties*)
676 ((sb!vm:simple-array-widetag
677 sb!vm:complex-vector-widetag
678 sb!vm:complex-array-widetag)
679 (with-array-data ((array array) (start) (end))
680 (declare (ignore start end))
681 (array-element-type array)))
683 (error 'type-error :datum array :expected-type 'array))))))
685 (defun array-rank (array)
686 #!+sb-doc
687 "Return the number of dimensions of ARRAY."
688 (if (array-header-p array)
689 (%array-rank array)
692 (defun array-dimension (array axis-number)
693 #!+sb-doc
694 "Return the length of dimension AXIS-NUMBER of ARRAY."
695 (declare (array array) (type index axis-number))
696 (cond ((not (array-header-p array))
697 (unless (= axis-number 0)
698 (error "Vector axis is not zero: ~S" axis-number))
699 (length (the (simple-array * (*)) array)))
700 ((>= axis-number (%array-rank array))
701 (error "Axis number ~W is too big; ~S only has ~D dimension~:P."
702 axis-number array (%array-rank array)))
704 ;; ANSI sayeth (ADJUST-ARRAY dictionary entry):
706 ;; "If A is displaced to B, the consequences are
707 ;; unspecified if B is adjusted in such a way that it no
708 ;; longer has enough elements to satisfy A.
710 ;; In situations where this matters we should be doing a
711 ;; bounds-check, which in turn uses ARRAY-DIMENSION -- so
712 ;; this seems like a good place to signal an error.
713 (multiple-value-bind (target offset) (array-displacement array)
714 (when (and target
715 (> (array-total-size array)
716 (- (array-total-size target) offset)))
717 (error 'displaced-to-array-too-small-error
718 :format-control "~@<The displaced-to array is too small. ~S ~
719 elements after offset required, ~S available.~:@>"
720 :format-arguments (list (array-total-size array)
721 (- (array-total-size target) offset))))
722 (%array-dimension array axis-number)))))
724 (defun array-dimensions (array)
725 #!+sb-doc
726 "Return a list whose elements are the dimensions of the array"
727 (declare (array array))
728 (if (array-header-p array)
729 (do ((results nil (cons (array-dimension array index) results))
730 (index (1- (array-rank array)) (1- index)))
731 ((minusp index) results))
732 (list (array-dimension array 0))))
734 (defun array-total-size (array)
735 #!+sb-doc
736 "Return the total number of elements in the Array."
737 (declare (array array))
738 (if (array-header-p array)
739 (%array-available-elements array)
740 (length (the vector array))))
742 (defun array-displacement (array)
743 #!+sb-doc
744 "Return the values of :DISPLACED-TO and :DISPLACED-INDEX-offset
745 options to MAKE-ARRAY, or NIL and 0 if not a displaced array."
746 (declare (type array array))
747 (if (and (array-header-p array) ; if unsimple and
748 (%array-displaced-p array)) ; displaced
749 (values (%array-data-vector array) (%array-displacement array))
750 (values nil 0)))
752 (defun adjustable-array-p (array)
753 #!+sb-doc
754 "Return T if (ADJUST-ARRAY ARRAY...) would return an array identical
755 to the argument, this happens for complex arrays."
756 (declare (array array))
757 ;; Note that this appears not to be a fundamental limitation.
758 ;; non-vector SIMPLE-ARRAYs are in fact capable of being adjusted,
759 ;; but in practice we test using ADJUSTABLE-ARRAY-P in ADJUST-ARRAY.
760 ;; -- CSR, 2004-03-01.
761 (not (typep array 'simple-array)))
763 ;;;; fill pointer frobbing stuff
765 (defun array-has-fill-pointer-p (array)
766 #!+sb-doc
767 "Return T if the given ARRAY has a fill pointer, or NIL otherwise."
768 (declare (array array))
769 (and (array-header-p array) (%array-fill-pointer-p array)))
771 (defun fill-pointer (vector)
772 #!+sb-doc
773 "Return the FILL-POINTER of the given VECTOR."
774 (declare (vector vector))
775 (if (and (array-header-p vector) (%array-fill-pointer-p vector))
776 (%array-fill-pointer vector)
777 (error 'simple-type-error
778 :datum vector
779 :expected-type '(and vector (satisfies array-has-fill-pointer-p))
780 :format-control "~S is not an array with a fill pointer."
781 :format-arguments (list vector))))
783 (defun %set-fill-pointer (vector new)
784 (declare (vector vector) (fixnum new))
785 (if (and (array-header-p vector) (%array-fill-pointer-p vector))
786 (if (> new (%array-available-elements vector))
787 (error
788 "The new fill pointer, ~S, is larger than the length of the vector."
789 new)
790 (setf (%array-fill-pointer vector) new))
791 (error 'simple-type-error
792 :datum vector
793 :expected-type '(and vector (satisfies array-has-fill-pointer-p))
794 :format-control "~S is not an array with a fill pointer."
795 :format-arguments (list vector))))
797 ;;; FIXME: It'd probably make sense to use a MACROLET to share the
798 ;;; guts of VECTOR-PUSH between VECTOR-PUSH-EXTEND. Such a macro
799 ;;; should probably be based on the VECTOR-PUSH-EXTEND code (which is
800 ;;; new ca. sbcl-0.7.0) rather than the VECTOR-PUSH code (which dates
801 ;;; back to CMU CL).
802 (defun vector-push (new-el array)
803 #!+sb-doc
804 "Attempt to set the element of ARRAY designated by its fill pointer
805 to NEW-EL, and increment the fill pointer by one. If the fill pointer is
806 too large, NIL is returned, otherwise the index of the pushed element is
807 returned."
808 (declare (vector array))
809 (let ((fill-pointer (fill-pointer array)))
810 (declare (fixnum fill-pointer))
811 (cond ((= fill-pointer (%array-available-elements array))
812 nil)
814 (setf (aref array fill-pointer) new-el)
815 (setf (%array-fill-pointer array) (1+ fill-pointer))
816 fill-pointer))))
818 (defun vector-push-extend (new-element
819 vector
820 &optional
821 (min-extension
822 (let ((length (length vector)))
823 (min (1+ length)
824 (- array-dimension-limit length)))))
825 (declare (vector vector) (fixnum min-extension))
826 (let ((fill-pointer (fill-pointer vector)))
827 (declare (fixnum fill-pointer))
828 (when (= fill-pointer (%array-available-elements vector))
829 (adjust-array vector (+ fill-pointer (max 1 min-extension))))
830 ;; disable bounds checking
831 (locally (declare (optimize (safety 0)))
832 (setf (aref vector fill-pointer) new-element))
833 (setf (%array-fill-pointer vector) (1+ fill-pointer))
834 fill-pointer))
836 (defun vector-pop (array)
837 #!+sb-doc
838 "Decrease the fill pointer by 1 and return the element pointed to by the
839 new fill pointer."
840 (declare (vector array))
841 (let ((fill-pointer (fill-pointer array)))
842 (declare (fixnum fill-pointer))
843 (if (zerop fill-pointer)
844 (error "There is nothing left to pop.")
845 ;; disable bounds checking (and any fixnum test)
846 (locally (declare (optimize (safety 0)))
847 (aref array
848 (setf (%array-fill-pointer array)
849 (1- fill-pointer)))))))
852 ;;;; ADJUST-ARRAY
854 (defun adjust-array (array dimensions &key
855 (element-type (array-element-type array))
856 (initial-element nil initial-element-p)
857 (initial-contents nil initial-contents-p)
858 fill-pointer
859 displaced-to displaced-index-offset)
860 #!+sb-doc
861 "Adjust ARRAY's dimensions to the given DIMENSIONS and stuff."
862 (let ((dimensions (if (listp dimensions) dimensions (list dimensions))))
863 (cond ((/= (the fixnum (length (the list dimensions)))
864 (the fixnum (array-rank array)))
865 (error "The number of dimensions not equal to rank of array."))
866 ((not (subtypep element-type (array-element-type array)))
867 (error "The new element type, ~S, is incompatible with old type."
868 element-type))
869 ((and fill-pointer (not (array-has-fill-pointer-p array)))
870 (error 'type-error
871 :datum array
872 :expected-type '(satisfies array-has-fill-pointer-p))))
873 (let ((array-rank (length (the list dimensions))))
874 (declare (fixnum array-rank))
875 (unless (= array-rank 1)
876 (when fill-pointer
877 (error "Only vectors can have fill pointers.")))
878 (cond (initial-contents-p
879 ;; array former contents replaced by INITIAL-CONTENTS
880 (if (or initial-element-p displaced-to)
881 (error "INITIAL-CONTENTS may not be specified with ~
882 the :INITIAL-ELEMENT or :DISPLACED-TO option."))
883 (let* ((array-size (apply #'* dimensions))
884 (array-data (data-vector-from-inits
885 dimensions array-size element-type
886 initial-contents initial-contents-p
887 initial-element initial-element-p)))
888 (if (adjustable-array-p array)
889 (set-array-header array array-data array-size
890 (get-new-fill-pointer array array-size
891 fill-pointer)
892 0 dimensions nil)
893 (if (array-header-p array)
894 ;; simple multidimensional or single dimensional array
895 (make-array dimensions
896 :element-type element-type
897 :initial-contents initial-contents)
898 array-data))))
899 (displaced-to
900 ;; We already established that no INITIAL-CONTENTS was supplied.
901 (when initial-element
902 (error "The :INITIAL-ELEMENT option may not be specified ~
903 with :DISPLACED-TO."))
904 (unless (subtypep element-type (array-element-type displaced-to))
905 (error "can't displace an array of type ~S into another of ~
906 type ~S"
907 element-type (array-element-type displaced-to)))
908 (let ((displacement (or displaced-index-offset 0))
909 (array-size (apply #'* dimensions)))
910 (declare (fixnum displacement array-size))
911 (if (< (the fixnum (array-total-size displaced-to))
912 (the fixnum (+ displacement array-size)))
913 (error "The :DISPLACED-TO array is too small."))
914 (if (adjustable-array-p array)
915 ;; None of the original contents appear in adjusted array.
916 (set-array-header array displaced-to array-size
917 (get-new-fill-pointer array array-size
918 fill-pointer)
919 displacement dimensions t)
920 ;; simple multidimensional or single dimensional array
921 (make-array dimensions
922 :element-type element-type
923 :displaced-to displaced-to
924 :displaced-index-offset
925 displaced-index-offset))))
926 ((= array-rank 1)
927 (let ((old-length (array-total-size array))
928 (new-length (car dimensions))
929 new-data)
930 (declare (fixnum old-length new-length))
931 (with-array-data ((old-data array) (old-start)
932 (old-end old-length))
933 (cond ((or (and (array-header-p array)
934 (%array-displaced-p array))
935 (< old-length new-length))
936 (setf new-data
937 (data-vector-from-inits
938 dimensions new-length element-type
939 initial-contents initial-contents-p
940 initial-element initial-element-p))
941 (replace new-data old-data
942 :start2 old-start :end2 old-end))
943 (t (setf new-data
944 (shrink-vector old-data new-length))))
945 (if (adjustable-array-p array)
946 (set-array-header array new-data new-length
947 (get-new-fill-pointer array new-length
948 fill-pointer)
949 0 dimensions nil)
950 new-data))))
952 (let ((old-length (%array-available-elements array))
953 (new-length (apply #'* dimensions)))
954 (declare (fixnum old-length new-length))
955 (with-array-data ((old-data array) (old-start)
956 (old-end old-length))
957 (declare (ignore old-end))
958 (let ((new-data (if (or (and (array-header-p array)
959 (%array-displaced-p array))
960 (> new-length old-length))
961 (data-vector-from-inits
962 dimensions new-length
963 element-type () nil
964 initial-element initial-element-p)
965 old-data)))
966 (if (or (zerop old-length) (zerop new-length))
967 (when initial-element-p (fill new-data initial-element))
968 (zap-array-data old-data (array-dimensions array)
969 old-start
970 new-data dimensions new-length
971 element-type initial-element
972 initial-element-p))
973 (if (adjustable-array-p array)
974 (set-array-header array new-data new-length
975 new-length 0 dimensions nil)
976 (let ((new-array
977 (make-array-header
978 sb!vm:simple-array-widetag array-rank)))
979 (set-array-header new-array new-data new-length
980 new-length 0 dimensions nil)))))))))))
983 (defun get-new-fill-pointer (old-array new-array-size fill-pointer)
984 (cond ((not fill-pointer)
985 (when (array-has-fill-pointer-p old-array)
986 (when (> (%array-fill-pointer old-array) new-array-size)
987 (error "cannot ADJUST-ARRAY an array (~S) to a size (~S) that is ~
988 smaller than its fill pointer (~S)"
989 old-array new-array-size (fill-pointer old-array)))
990 (%array-fill-pointer old-array)))
991 ((not (array-has-fill-pointer-p old-array))
992 (error "cannot supply a non-NIL value (~S) for :FILL-POINTER ~
993 in ADJUST-ARRAY unless the array (~S) was originally ~
994 created with a fill pointer"
995 fill-pointer
996 old-array))
997 ((numberp fill-pointer)
998 (when (> fill-pointer new-array-size)
999 (error "can't supply a value for :FILL-POINTER (~S) that is larger ~
1000 than the new length of the vector (~S)"
1001 fill-pointer new-array-size))
1002 fill-pointer)
1003 ((eq fill-pointer t)
1004 new-array-size)
1006 (error "bogus value for :FILL-POINTER in ADJUST-ARRAY: ~S"
1007 fill-pointer))))
1009 ;;; Destructively alter VECTOR, changing its length to NEW-LENGTH,
1010 ;;; which must be less than or equal to its current length. This can
1011 ;;; be called on vectors without a fill pointer but it is extremely
1012 ;;; dangerous to do so: shrinking the size of an object (as viewed by
1013 ;;; the gc) makes bounds checking unreliable in the face of interrupts
1014 ;;; or multi-threading. Call it only on provably local vectors.
1015 (defun %shrink-vector (vector new-length)
1016 (declare (vector vector))
1017 (unless (array-header-p vector)
1018 (macrolet ((frob (name &rest things)
1019 `(etypecase ,name
1020 ((simple-array nil (*)) (error 'nil-array-accessed-error))
1021 ,@(mapcar (lambda (thing)
1022 (destructuring-bind (type-spec fill-value)
1023 thing
1024 `(,type-spec
1025 (fill (truly-the ,type-spec ,name)
1026 ,fill-value
1027 :start new-length))))
1028 things))))
1029 ;; Set the 'tail' of the vector to the appropriate type of zero,
1030 ;; "because in some cases we'll scavenge larger areas in one go,
1031 ;; like groups of pages that had triggered the write barrier, or
1032 ;; the whole static space" according to jsnell.
1033 #.`(frob vector
1034 ,@(map 'list
1035 (lambda (saetp)
1036 `((simple-array ,(sb!vm:saetp-specifier saetp) (*))
1037 ,(if (or (eq (sb!vm:saetp-specifier saetp) 'character)
1038 #!+sb-unicode
1039 (eq (sb!vm:saetp-specifier saetp) 'base-char))
1040 *default-init-char-form*
1041 (sb!vm:saetp-initial-element-default saetp))))
1042 (remove-if-not
1043 #'sb!vm:saetp-specifier
1044 sb!vm:*specialized-array-element-type-properties*)))))
1045 ;; Only arrays have fill-pointers, but vectors have their length
1046 ;; parameter in the same place.
1047 (setf (%array-fill-pointer vector) new-length)
1048 vector)
1050 (defun shrink-vector (vector new-length)
1051 (declare (vector vector))
1052 (cond
1053 ((eq (length vector) new-length)
1054 vector)
1055 ((array-has-fill-pointer-p vector)
1056 (setf (%array-fill-pointer vector) new-length)
1057 vector)
1058 (t (subseq vector 0 new-length))))
1060 ;;; Fill in array header with the provided information, and return the array.
1061 (defun set-array-header (array data length fill-pointer displacement dimensions
1062 &optional displacedp)
1063 (setf (%array-data-vector array) data)
1064 (setf (%array-available-elements array) length)
1065 (cond (fill-pointer
1066 (setf (%array-fill-pointer array) fill-pointer)
1067 (setf (%array-fill-pointer-p array) t))
1069 (setf (%array-fill-pointer array) length)
1070 (setf (%array-fill-pointer-p array) nil)))
1071 (setf (%array-displacement array) displacement)
1072 (if (listp dimensions)
1073 (dotimes (axis (array-rank array))
1074 (declare (type index axis))
1075 (setf (%array-dimension array axis) (pop dimensions)))
1076 (setf (%array-dimension array 0) dimensions))
1077 (setf (%array-displaced-p array) displacedp)
1078 array)
1080 ;;;; ZAP-ARRAY-DATA for ADJUST-ARRAY
1082 ;;; a temporary to be used when OLD-DATA and NEW-DATA are EQ.
1083 ;;; KLUDGE: Boy, DYNAMIC-EXTENT would be nice. This is rebound
1084 ;;; to length zero array in each new thread.
1086 ;;; DX is probably a bad idea, because a with a big array it would
1087 ;;; be fairly easy to blow the stack.
1088 (defvar *zap-array-data-temp* (vector))
1089 (declaim (simple-vector *zap-array-data-temp*))
1091 (defun zap-array-data-temp (length initial-element initial-element-p)
1092 (declare (fixnum length))
1093 (let ((tmp *zap-array-data-temp*))
1094 (declare (simple-vector tmp))
1095 (cond ((> length (length tmp))
1096 (setf *zap-array-data-temp*
1097 (if initial-element-p
1098 (make-array length :initial-element initial-element)
1099 (make-array length))))
1100 (initial-element-p
1101 (fill tmp initial-element :end length))
1103 tmp))))
1105 ;;; This does the grinding work for ADJUST-ARRAY. It zaps the data
1106 ;;; from the OLD-DATA in an arrangement specified by the OLD-DIMS to
1107 ;;; the NEW-DATA in an arrangement specified by the NEW-DIMS. OFFSET
1108 ;;; is a displaced offset to be added to computed indices of OLD-DATA.
1109 (defun zap-array-data (old-data old-dims offset new-data new-dims new-length
1110 element-type initial-element initial-element-p)
1111 (declare (list old-dims new-dims))
1112 ;; OLD-DIMS comes from array-dimensions, which returns a fresh list
1113 ;; at least in SBCL.
1114 ;; NEW-DIMS comes from the user.
1115 (setf old-dims (nreverse old-dims)
1116 new-dims (reverse new-dims))
1117 (cond ((eq old-data new-data)
1118 ;; NEW-LENGTH, ELEMENT-TYPE, INITIAL-ELEMENT, and
1119 ;; INITIAL-ELEMENT-P are used when OLD-DATA and NEW-DATA are
1120 ;; EQ; in this case, a temporary must be used and filled
1121 ;; appropriately. specified initial-element.
1122 (when initial-element-p
1123 ;; FIXME: transforming this TYPEP to someting a bit faster
1124 ;; would be a win...
1125 (unless (typep initial-element element-type)
1126 (error "~S can't be used to initialize an array of type ~S."
1127 initial-element element-type)))
1128 (without-interrupts
1129 ;; Need to disable interrupts while using the temp-vector.
1130 ;; An interrupt handler that also happened to call
1131 ;; ADJUST-ARRAY could otherwise stomp on our data here.
1132 (let ((temp (zap-array-data-temp new-length
1133 initial-element initial-element-p)))
1134 (declare (simple-vector temp))
1135 (zap-array-data-aux old-data old-dims offset temp new-dims)
1136 (dotimes (i new-length)
1137 (setf (aref new-data i) (aref temp i)
1138 ;; zero out any garbage right away
1139 (aref temp i) 0)))))
1141 ;; When OLD-DATA and NEW-DATA are not EQ, NEW-DATA has
1142 ;; already been filled with any
1143 (zap-array-data-aux old-data old-dims offset new-data new-dims))))
1145 (defun zap-array-data-aux (old-data old-dims offset new-data new-dims)
1146 (declare (fixnum offset))
1147 (let ((limits (mapcar (lambda (x y)
1148 (declare (fixnum x y))
1149 (1- (the fixnum (min x y))))
1150 old-dims new-dims)))
1151 (macrolet ((bump-index-list (index limits)
1152 `(do ((subscripts ,index (cdr subscripts))
1153 (limits ,limits (cdr limits)))
1154 ((null subscripts) :eof)
1155 (cond ((< (the fixnum (car subscripts))
1156 (the fixnum (car limits)))
1157 (rplaca subscripts
1158 (1+ (the fixnum (car subscripts))))
1159 (return ,index))
1160 (t (rplaca subscripts 0))))))
1161 (do ((index (make-list (length old-dims) :initial-element 0)
1162 (bump-index-list index limits)))
1163 ((eq index :eof))
1164 (setf (aref new-data (row-major-index-from-dims index new-dims))
1165 (aref old-data
1166 (+ (the fixnum (row-major-index-from-dims index old-dims))
1167 offset)))))))
1169 ;;; Figure out the row-major-order index of an array reference from a
1170 ;;; list of subscripts and a list of dimensions. This is for internal
1171 ;;; calls only, and the subscripts and dim-list variables are assumed
1172 ;;; to be reversed from what the user supplied.
1173 (defun row-major-index-from-dims (rev-subscripts rev-dim-list)
1174 (do ((rev-subscripts rev-subscripts (cdr rev-subscripts))
1175 (rev-dim-list rev-dim-list (cdr rev-dim-list))
1176 (chunk-size 1)
1177 (result 0))
1178 ((null rev-dim-list) result)
1179 (declare (fixnum chunk-size result))
1180 (setq result (+ result
1181 (the fixnum (* (the fixnum (car rev-subscripts))
1182 chunk-size))))
1183 (setq chunk-size (* chunk-size (the fixnum (car rev-dim-list))))))
1185 ;;;; some bit stuff
1187 (defun bit-array-same-dimensions-p (array1 array2)
1188 (declare (type (array bit) array1 array2))
1189 (and (= (array-rank array1)
1190 (array-rank array2))
1191 (dotimes (index (array-rank array1) t)
1192 (when (/= (array-dimension array1 index)
1193 (array-dimension array2 index))
1194 (return nil)))))
1196 (defun pick-result-array (result-bit-array bit-array-1)
1197 (case result-bit-array
1198 ((t) bit-array-1)
1199 ((nil) (make-array (array-dimensions bit-array-1)
1200 :element-type 'bit
1201 :initial-element 0))
1203 (unless (bit-array-same-dimensions-p bit-array-1
1204 result-bit-array)
1205 (error "~S and ~S don't have the same dimensions."
1206 bit-array-1 result-bit-array))
1207 result-bit-array)))
1209 (defmacro def-bit-array-op (name function)
1210 `(defun ,name (bit-array-1 bit-array-2 &optional result-bit-array)
1211 #!+sb-doc
1212 ,(format nil
1213 "Perform a bit-wise ~A on the elements of BIT-ARRAY-1 and ~
1214 BIT-ARRAY-2,~% putting the results in RESULT-BIT-ARRAY. ~
1215 If RESULT-BIT-ARRAY is T,~% BIT-ARRAY-1 is used. If ~
1216 RESULT-BIT-ARRAY is NIL or omitted, a new array is~% created. ~
1217 All the arrays must have the same rank and dimensions."
1218 (symbol-name function))
1219 (declare (type (array bit) bit-array-1 bit-array-2)
1220 (type (or (array bit) (member t nil)) result-bit-array))
1221 (unless (bit-array-same-dimensions-p bit-array-1 bit-array-2)
1222 (error "~S and ~S don't have the same dimensions."
1223 bit-array-1 bit-array-2))
1224 (let ((result-bit-array (pick-result-array result-bit-array bit-array-1)))
1225 (if (and (simple-bit-vector-p bit-array-1)
1226 (simple-bit-vector-p bit-array-2)
1227 (simple-bit-vector-p result-bit-array))
1228 (locally (declare (optimize (speed 3) (safety 0)))
1229 (,name bit-array-1 bit-array-2 result-bit-array))
1230 (with-array-data ((data1 bit-array-1) (start1) (end1))
1231 (declare (ignore end1))
1232 (with-array-data ((data2 bit-array-2) (start2) (end2))
1233 (declare (ignore end2))
1234 (with-array-data ((data3 result-bit-array) (start3) (end3))
1235 (do ((index-1 start1 (1+ index-1))
1236 (index-2 start2 (1+ index-2))
1237 (index-3 start3 (1+ index-3)))
1238 ((>= index-3 end3) result-bit-array)
1239 (declare (type index index-1 index-2 index-3))
1240 (setf (sbit data3 index-3)
1241 (logand (,function (sbit data1 index-1)
1242 (sbit data2 index-2))
1243 1))))))))))
1245 (def-bit-array-op bit-and logand)
1246 (def-bit-array-op bit-ior logior)
1247 (def-bit-array-op bit-xor logxor)
1248 (def-bit-array-op bit-eqv logeqv)
1249 (def-bit-array-op bit-nand lognand)
1250 (def-bit-array-op bit-nor lognor)
1251 (def-bit-array-op bit-andc1 logandc1)
1252 (def-bit-array-op bit-andc2 logandc2)
1253 (def-bit-array-op bit-orc1 logorc1)
1254 (def-bit-array-op bit-orc2 logorc2)
1256 (defun bit-not (bit-array &optional result-bit-array)
1257 #!+sb-doc
1258 "Performs a bit-wise logical NOT on the elements of BIT-ARRAY,
1259 putting the results in RESULT-BIT-ARRAY. If RESULT-BIT-ARRAY is T,
1260 BIT-ARRAY is used. If RESULT-BIT-ARRAY is NIL or omitted, a new array is
1261 created. Both arrays must have the same rank and dimensions."
1262 (declare (type (array bit) bit-array)
1263 (type (or (array bit) (member t nil)) result-bit-array))
1264 (let ((result-bit-array (pick-result-array result-bit-array bit-array)))
1265 (if (and (simple-bit-vector-p bit-array)
1266 (simple-bit-vector-p result-bit-array))
1267 (locally (declare (optimize (speed 3) (safety 0)))
1268 (bit-not bit-array result-bit-array))
1269 (with-array-data ((src bit-array) (src-start) (src-end))
1270 (declare (ignore src-end))
1271 (with-array-data ((dst result-bit-array) (dst-start) (dst-end))
1272 (do ((src-index src-start (1+ src-index))
1273 (dst-index dst-start (1+ dst-index)))
1274 ((>= dst-index dst-end) result-bit-array)
1275 (declare (type index src-index dst-index))
1276 (setf (sbit dst dst-index)
1277 (logxor (sbit src src-index) 1))))))))