1 ;;;; functions to implement arrays
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.
12 (in-package "SB!IMPL")
15 (declaim (inline adjustable-array-p
18 ;;;; miscellaneous accessor functions
20 ;;; These functions are only needed by the interpreter, 'cause the
21 ;;; compiler inlines them.
22 (macrolet ((def (name)
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)
34 (def %array-diplaced-from))
36 (defun %array-rank (array)
39 (defun %array-dimension (array axis)
40 (%array-dimension array axis))
42 (defun %set-array-dimension (array axis value)
43 (%set-array-dimension array axis value))
45 (defun %check-bound (array bound index)
46 (declare (type index bound)
48 (%check-bound array bound index))
50 (defun %with-array-data/fp (array start end)
51 (%with-array-data-macro array start end :check-bounds t :check-fill-pointer t))
53 (defun %with-array-data (array start end)
54 (%with-array-data-macro array start end :check-bounds t :check-fill-pointer nil))
56 (defun %data-vector-and-index (array index)
57 (if (array-header-p array)
58 (multiple-value-bind (vector index)
59 (%with-array-data array index nil)
60 (values vector index))
61 (values array index)))
64 (eval-when (:compile-toplevel :execute)
65 (sb!xc:defmacro pick-vector-type (type &rest specs)
66 `(cond ,@(mapcar (lambda (spec)
67 `(,(if (eq (car spec) t)
69 `(subtypep ,type ',(car spec)))
73 ;;; These functions are used in the implementation of MAKE-ARRAY for
74 ;;; complex arrays. There are lots of transforms to simplify
75 ;;; MAKE-ARRAY for various easy cases, but not for all reasonable
76 ;;; cases, so e.g. as of sbcl-0.6.6 we still make full calls to
77 ;;; MAKE-ARRAY for any non-simple array. Thus, there's some value to
78 ;;; making this somewhat efficient, at least not doing full calls to
79 ;;; SUBTYPEP in the easy cases.
80 (defun %vector-widetag-and-n-bits (type)
82 ;; Pick off some easy common cases.
84 ;; (Perhaps we should make a much more exhaustive table of easy
85 ;; common cases here. Or perhaps the effort would be better spent
86 ;; on smarter compiler transforms which do the calculation once
87 ;; and for all in any reasonable user programs.)
89 (values #.sb!vm:simple-vector-widetag #.sb!vm:n-word-bits))
90 ((base-char standard-char #!-sb-unicode character)
91 (values #.sb!vm:simple-base-string-widetag #.sb!vm:n-byte-bits))
94 (values #.sb!vm:simple-character-string-widetag #.sb!vm:n-word-bits))
96 (values #.sb!vm:simple-bit-vector-widetag 1))
97 ;; OK, we have to wade into SUBTYPEPing after all.
99 (unless *type-system-initialized*
100 (bug "SUBTYPEP dispatch for MAKE-ARRAY before the type system is ready"))
101 #.`(pick-vector-type type
104 `(,(sb!vm:saetp-specifier saetp)
105 (values ,(sb!vm:saetp-typecode saetp)
106 ,(sb!vm:saetp-n-bits saetp))))
107 sb!vm:*specialized-array-element-type-properties*)))))
109 (defun %complex-vector-widetag (type)
111 ;; Pick off some easy common cases.
113 #.sb!vm:complex-vector-widetag)
114 ((base-char #!-sb-unicode character)
115 #.sb!vm:complex-base-string-widetag)
118 #.sb!vm:complex-character-string-widetag)
120 #.sb!vm:complex-vector-nil-widetag)
122 #.sb!vm:complex-bit-vector-widetag)
123 ;; OK, we have to wade into SUBTYPEPing after all.
125 (pick-vector-type type
126 (nil #.sb!vm:complex-vector-nil-widetag)
128 (character #.sb!vm:complex-base-string-widetag)
130 (base-char #.sb!vm:complex-base-string-widetag)
132 (character #.sb!vm:complex-character-string-widetag)
133 (bit #.sb!vm:complex-bit-vector-widetag)
134 (t #.sb!vm:complex-vector-widetag)))))
136 (defglobal %%simple-array-n-bits%% (make-array (1+ sb!vm:widetag-mask)))
137 #.(loop for info across sb!vm:*specialized-array-element-type-properties*
138 collect `(setf (aref %%simple-array-n-bits%% ,(sb!vm:saetp-typecode info))
139 ,(sb!vm:saetp-n-bits info)) into forms
140 finally (return `(progn ,@forms)))
142 (defun allocate-vector-with-widetag (widetag length &optional n-bits)
143 (declare (type (unsigned-byte 8) widetag)
145 (let ((n-bits (or n-bits (aref %%simple-array-n-bits%% widetag))))
146 (declare (type (integer 0 256) n-bits))
147 (allocate-vector widetag length
149 (* (if (or (= widetag sb!vm:simple-base-string-widetag)
152 sb!vm:simple-character-string-widetag))
156 sb!vm:n-word-bits))))
158 (defun make-array (dimensions &key
160 (initial-element nil initial-element-p)
161 (initial-contents nil initial-contents-p)
162 adjustable fill-pointer
163 displaced-to displaced-index-offset)
164 (let* ((dimensions (if (listp dimensions) dimensions (list dimensions)))
165 (array-rank (length (the list dimensions)))
166 (simple (and (null fill-pointer)
168 (null displaced-to))))
169 (declare (fixnum array-rank))
170 (when (and displaced-index-offset (null displaced-to))
171 (error "can't specify :DISPLACED-INDEX-OFFSET without :DISPLACED-TO"))
172 (when (and displaced-to
173 (arrayp displaced-to)
174 (not (equal (array-element-type displaced-to)
175 (upgraded-array-element-type element-type))))
176 (error "Array element type of :DISPLACED-TO array does not match specified element type"))
177 (if (and simple (= array-rank 1))
178 ;; it's a (SIMPLE-ARRAY * (*))
179 (multiple-value-bind (type n-bits)
180 (%vector-widetag-and-n-bits element-type)
181 (declare (type (unsigned-byte 8) type)
182 (type (integer 0 256) n-bits))
183 (let* ((length (car dimensions))
184 (array (allocate-vector-with-widetag type length n-bits)))
185 (declare (type index length))
186 (when initial-element-p
187 (fill array initial-element))
188 (when initial-contents-p
189 (when initial-element-p
190 (error "can't specify both :INITIAL-ELEMENT and ~
192 (unless (= length (length initial-contents))
193 (error "There are ~W elements in the :INITIAL-CONTENTS, but ~
194 the vector length is ~W."
195 (length initial-contents)
197 (replace array initial-contents))
199 ;; it's either a complex array or a multidimensional array.
200 (let* ((total-size (reduce #'* dimensions))
201 (data (or displaced-to
202 (data-vector-from-inits
203 dimensions total-size element-type nil
204 initial-contents initial-contents-p
205 initial-element initial-element-p)))
206 (array (make-array-header
207 (cond ((= array-rank 1)
208 (%complex-vector-widetag element-type))
209 (simple sb!vm:simple-array-widetag)
210 (t sb!vm:complex-array-widetag))
213 (unless (= array-rank 1)
214 (error "Only vectors can have fill pointers."))
215 (let ((length (car dimensions)))
216 (declare (fixnum length))
217 (setf (%array-fill-pointer array)
218 (cond ((eq fill-pointer t)
221 (unless (and (fixnump fill-pointer)
223 (<= fill-pointer length))
224 ;; FIXME: should be TYPE-ERROR?
225 (error "invalid fill-pointer ~W"
228 (setf (%array-fill-pointer-p array) t))
230 (setf (%array-fill-pointer array) total-size)
231 (setf (%array-fill-pointer-p array) nil)))
232 (setf (%array-available-elements array) total-size)
233 (setf (%array-data-vector array) data)
234 (setf (%array-displaced-from array) nil)
236 (when (or initial-element-p initial-contents-p)
237 (error "Neither :INITIAL-ELEMENT nor :INITIAL-CONTENTS ~
238 can be specified along with :DISPLACED-TO"))
239 (let ((offset (or displaced-index-offset 0)))
240 (when (> (+ offset total-size)
241 (array-total-size displaced-to))
242 (error "~S doesn't have enough elements." displaced-to))
243 (setf (%array-displacement array) offset)
244 (setf (%array-displaced-p array) t)
245 (%save-displaced-array-backpointer array data)))
247 (setf (%array-displaced-p array) nil)))
249 (dolist (dim dimensions)
250 (setf (%array-dimension array axis) dim)
254 (defun make-static-vector (length &key
255 (element-type '(unsigned-byte 8))
256 (initial-contents nil initial-contents-p)
257 (initial-element nil initial-element-p))
258 "Allocate vector of LENGTH elements in static space. Only allocation
259 of specialized arrays is supported."
260 ;; STEP 1: check inputs fully
262 ;; This way of doing explicit checks before the vector is allocated
263 ;; is expensive, but probably worth the trouble as once we've allocated
264 ;; the vector we have no way to get rid of it anymore...
265 (when (eq t (upgraded-array-element-type element-type))
266 (error "Static arrays of type ~S not supported."
268 (when initial-contents-p
269 (when initial-element-p
270 (error "can't specify both :INITIAL-ELEMENT and :INITIAL-CONTENTS"))
271 (unless (= length (length initial-contents))
272 (error "There are ~W elements in the :INITIAL-CONTENTS, but the ~
273 vector length is ~W."
274 (length initial-contents)
276 (unless (every (lambda (x) (typep x element-type)) initial-contents)
277 (error ":INITIAL-CONTENTS contains elements not of type ~S."
279 (when initial-element-p
280 (unless (typep initial-element element-type)
281 (error ":INITIAL-ELEMENT ~S is not of type ~S."
282 initial-element element-type)))
285 ;; Allocate and possibly initialize the vector.
286 (multiple-value-bind (type n-bits)
287 (sb!impl::%vector-widetag-and-n-bits element-type)
289 (allocate-static-vector type length
290 (ceiling (* length n-bits)
291 sb!vm:n-word-bits))))
292 (cond (initial-element-p
293 (fill vector initial-element))
295 (replace vector initial-contents))
299 ;;; DATA-VECTOR-FROM-INITS returns a simple vector that has the
300 ;;; specified array characteristics. Dimensions is only used to pass
301 ;;; to FILL-DATA-VECTOR for error checking on the structure of
302 ;;; initial-contents.
303 (defun data-vector-from-inits (dimensions total-size
305 initial-contents initial-contents-p
306 initial-element initial-element-p)
307 (when initial-element-p
308 (when initial-contents-p
309 (error "cannot supply both :INITIAL-CONTENTS and :INITIAL-ELEMENT to
310 either MAKE-ARRAY or ADJUST-ARRAY."))
311 (unless (typep initial-element element-type)
312 (error "~S cannot be used to initialize an array of type ~S."
313 initial-element element-type)))
314 (let ((data (if widetag
315 (allocate-vector-with-widetag widetag total-size)
316 (make-array total-size :element-type element-type))))
317 (cond (initial-element-p
318 (fill (the vector data) initial-element))
320 (fill-data-vector data dimensions initial-contents)))
323 (defun vector (&rest objects)
325 "Construct a SIMPLE-VECTOR from the given objects."
326 (coerce (the list objects) 'simple-vector))
329 ;;;; accessor/setter functions
331 ;;; Dispatch to an optimized routine the data vector accessors for
332 ;;; each different specialized vector type. Do dispatching by looking
333 ;;; up the widetag in the array rather than with the typecases, which
334 ;;; as of 1.0.5 compiles to a naive sequence of linear TYPEPs. Also
335 ;;; provide separate versions where bounds checking has been moved
336 ;;; from the callee to the caller, since it's much cheaper to do once
337 ;;; the type information is available. Finally, for each of these
338 ;;; routines also provide a slow path, taken for arrays that are not
339 ;;; vectors or not simple.
340 (macrolet ((def (name table-name)
342 (defglobal ,table-name (make-array ,(1+ sb!vm:widetag-mask)))
343 (defmacro ,name (array-var)
346 (when (sb!vm::%other-pointer-p ,array-var)
347 (setf tag (%other-pointer-widetag ,array-var)))
348 (svref ,',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 ;; Used by DO-VECTOR-DATA -- which in turn appears in DOSEQUENCE expansion,
352 ;; meaning we can have post-build dependences on this.
353 (def %find-data-vector-reffer %%data-vector-reffers%%)
354 (def !find-data-vector-reffer/check-bounds %%data-vector-reffers/check-bounds%%))
356 ;;; Like DOVECTOR, but more magical -- can't use this on host.
357 (defmacro do-vector-data ((elt vector &optional result) &body body)
358 (multiple-value-bind (forms decls) (parse-body body :doc-string-allowed nil)
359 (with-unique-names (index vec start end ref)
360 `(with-array-data ((,vec ,vector)
363 :check-fill-pointer t)
364 (let ((,ref (%find-data-vector-reffer ,vec)))
365 (do ((,index ,start (1+ ,index)))
368 ,@(filter-dolist-declarations decls)
371 (let ((,elt (funcall ,ref ,vec ,index)))
373 (tagbody ,@forms))))))))
375 (macrolet ((%ref (accessor-getter extra-params)
376 `(funcall (,accessor-getter array) array index ,@extra-params))
377 (define (accessor-name slow-accessor-name accessor-getter
378 extra-params check-bounds)
380 (defun ,accessor-name (array index ,@extra-params)
381 (declare (optimize speed
382 ;; (SAFETY 0) is ok. All calls to
383 ;; these functions are generated by
384 ;; the compiler, so argument count
385 ;; checking isn't needed. Type checking
386 ;; is done implicitly via the widetag
389 (%ref ,accessor-getter ,extra-params))
390 (defun ,slow-accessor-name (array index ,@extra-params)
391 (declare (optimize speed (safety 0)))
392 (if (not (%array-displaced-p array))
393 ;; The reasonably quick path of non-displaced complex
395 (let ((array (%array-data-vector array)))
396 (%ref ,accessor-getter ,extra-params))
397 ;; The real slow path.
401 (declare (optimize (speed 1) (safety 1)))
402 (,@check-bounds index)))
405 (declare (ignore end))
406 (,accessor-name vector index ,@extra-params)))))))
407 (define hairy-data-vector-ref slow-hairy-data-vector-ref
408 %find-data-vector-reffer
410 (define hairy-data-vector-set slow-hairy-data-vector-set
411 !find-data-vector-setter
413 (define hairy-data-vector-ref/check-bounds
414 slow-hairy-data-vector-ref/check-bounds
415 !find-data-vector-reffer/check-bounds
416 nil (%check-bound array (array-dimension array 0)))
417 (define hairy-data-vector-set/check-bounds
418 slow-hairy-data-vector-set/check-bounds
419 !find-data-vector-setter/check-bounds
420 (new-value) (%check-bound array (array-dimension array 0))))
422 (defun hairy-ref-error (array index &optional new-value)
423 (declare (ignore index new-value))
426 :expected-type 'vector))
428 (macrolet ((define-reffer (saetp check-form)
429 (let* ((type (sb!vm:saetp-specifier saetp))
430 (atype `(simple-array ,type (*))))
431 `(named-lambda optimized-data-vector-ref (vector index)
432 (declare (optimize speed (safety 0)))
433 (data-vector-ref (the ,atype vector)
435 (declare (optimize (safety 1)))
437 (,@check-form index)))))))
438 (define-setter (saetp check-form)
439 (let* ((type (sb!vm:saetp-specifier saetp))
440 (atype `(simple-array ,type (*))))
441 `(named-lambda optimized-data-vector-set (vector index new-value)
442 (declare (optimize speed (safety 0)))
443 (data-vector-set (the ,atype vector)
445 (declare (optimize (safety 1)))
447 (,@check-form index)))
449 ;; SPEED 1 needed to avoid the compiler
450 ;; from downgrading the type check to
452 (declare (optimize (speed 1)
454 (the ,type new-value)))
455 ;; For specialized arrays, the return from
456 ;; data-vector-set would have to be reboxed to be a
457 ;; (Lisp) return value; instead, we use the
458 ;; already-boxed value as the return.
460 (define-reffers (symbol deffer check-form slow-path)
462 ;; FIXME/KLUDGE: can't just FILL here, because genesis doesn't
463 ;; preserve the binding, so re-initiaize as NS doesn't have
464 ;; the energy to figure out to change that right now.
465 (setf ,symbol (make-array (1+ sb!vm::widetag-mask)
466 :initial-element #'hairy-ref-error))
467 ,@(loop for widetag in '(sb!vm:complex-vector-widetag
468 sb!vm:complex-vector-nil-widetag
469 sb!vm:complex-bit-vector-widetag
470 #!+sb-unicode sb!vm:complex-character-string-widetag
471 sb!vm:complex-base-string-widetag
472 sb!vm:simple-array-widetag
473 sb!vm:complex-array-widetag)
474 collect `(setf (svref ,symbol ,widetag) ,slow-path))
475 ,@(loop for saetp across sb!vm:*specialized-array-element-type-properties*
476 for widetag = (sb!vm:saetp-typecode saetp)
477 collect `(setf (svref ,symbol ,widetag)
478 (,deffer ,saetp ,check-form))))))
479 (defun !hairy-data-vector-reffer-init ()
480 (define-reffers %%data-vector-reffers%% define-reffer
482 #'slow-hairy-data-vector-ref)
483 (define-reffers %%data-vector-setters%% define-setter
485 #'slow-hairy-data-vector-set)
486 (define-reffers %%data-vector-reffers/check-bounds%% define-reffer
487 (%check-bound vector (length vector))
488 #'slow-hairy-data-vector-ref/check-bounds)
489 (define-reffers %%data-vector-setters/check-bounds%% define-setter
490 (%check-bound vector (length vector))
491 #'slow-hairy-data-vector-set/check-bounds)))
493 ;;; (Ordinary DATA-VECTOR-REF usage compiles into a vop, but
494 ;;; DATA-VECTOR-REF is also FOLDABLE, and this ordinary function
495 ;;; definition is needed for the compiler to use in constant folding.)
496 (defun data-vector-ref (array index)
497 (hairy-data-vector-ref array index))
499 (defun data-vector-ref-with-offset (array index offset)
500 (hairy-data-vector-ref array (+ index offset)))
502 (defun invalid-array-p (array)
503 (and (array-header-p array)
504 (consp (%array-displaced-p array))))
506 (declaim (ftype (function (array) nil) invalid-array-error))
507 (defun invalid-array-error (array)
508 (aver (array-header-p array))
509 ;; Array invalidation stashes the original dimensions here...
510 (let ((dims (%array-displaced-p array))
511 (et (array-element-type array)))
512 (error 'invalid-array-error
517 `(vector ,et ,@dims)))))
519 (declaim (ftype (function (array integer integer &optional t) nil)
520 invalid-array-index-error))
521 (defun invalid-array-index-error (array index bound &optional axis)
522 (if (invalid-array-p array)
523 (invalid-array-error array)
524 (error 'invalid-array-index-error
528 :expected-type `(integer 0 (,bound)))))
530 ;;; SUBSCRIPTS has a dynamic-extent list structure and is destroyed
531 (defun %array-row-major-index (array subscripts
532 &optional (invalid-index-error-p t))
533 (declare (array array)
535 (let ((rank (array-rank array)))
536 (unless (= rank (length subscripts))
537 (error "wrong number of subscripts, ~W, for array of rank ~W"
538 (length subscripts) rank))
539 (if (array-header-p array)
540 (do ((subs (nreverse subscripts) (cdr subs))
541 (axis (1- (array-rank array)) (1- axis))
545 (declare (list subs) (fixnum axis chunk-size result))
546 (let ((index (car subs))
547 (dim (%array-dimension array axis)))
548 (declare (fixnum dim))
549 (unless (and (fixnump index) (< -1 index dim))
550 (if invalid-index-error-p
551 (invalid-array-index-error array index dim axis)
552 (return-from %array-row-major-index nil)))
553 (incf result (* chunk-size (the fixnum index)))
554 (setf chunk-size (* chunk-size dim))))
555 (let ((index (first subscripts))
556 (length (length (the (simple-array * (*)) array))))
557 (unless (and (fixnump index) (< -1 index length))
558 (if invalid-index-error-p
559 (invalid-array-index-error array index length)
560 (return-from %array-row-major-index nil)))
563 (defun array-in-bounds-p (array &rest subscripts)
565 "Return T if the SUBSCRIPTS are in bounds for the ARRAY, NIL otherwise."
566 (if (%array-row-major-index array subscripts nil)
569 (defun array-row-major-index (array &rest subscripts)
570 (declare (truly-dynamic-extent subscripts))
571 (%array-row-major-index array subscripts))
573 (defun aref (array &rest subscripts)
575 "Return the element of the ARRAY specified by the SUBSCRIPTS."
576 (declare (truly-dynamic-extent subscripts))
577 (row-major-aref array (%array-row-major-index array subscripts)))
579 ;;; (setf aref/bit/sbit) are implemented using setf-functions,
580 ;;; because they have to work with (setf (apply #'aref array subscripts))
581 ;;; All other setfs can be done using setf-functions too, but I
582 ;;; haven't found technical advantages or disatvantages for either
584 (defun (setf aref) (new-value array &rest subscripts)
585 (declare (truly-dynamic-extent subscripts)
587 (setf (row-major-aref array (%array-row-major-index array subscripts))
590 (defun row-major-aref (array index)
592 "Return the element of array corresponding to the row-major index. This is
594 (declare (optimize (safety 1)))
595 (row-major-aref array index))
597 (defun %set-row-major-aref (array index new-value)
598 (declare (optimize (safety 1)))
599 (setf (row-major-aref array index) new-value))
601 (defun svref (simple-vector index)
603 "Return the INDEXth element of the given Simple-Vector."
604 (declare (optimize (safety 1)))
605 (aref simple-vector index))
607 (defun %svset (simple-vector index new)
608 (declare (optimize (safety 1)))
609 (setf (aref simple-vector index) new))
611 (defun bit (bit-array &rest subscripts)
613 "Return the bit from the BIT-ARRAY at the specified SUBSCRIPTS."
614 (declare (type (array bit) bit-array)
615 (optimize (safety 1)))
616 (row-major-aref bit-array (%array-row-major-index bit-array subscripts)))
618 (defun (setf bit) (new-value bit-array &rest subscripts)
619 (declare (type (array bit) bit-array)
621 (optimize (safety 1)))
622 (setf (row-major-aref bit-array
623 (%array-row-major-index bit-array subscripts))
626 (defun sbit (simple-bit-array &rest subscripts)
628 "Return the bit from SIMPLE-BIT-ARRAY at the specified SUBSCRIPTS."
629 (declare (type (simple-array bit) simple-bit-array)
630 (optimize (safety 1)))
631 (row-major-aref simple-bit-array
632 (%array-row-major-index simple-bit-array subscripts)))
634 (defun (setf sbit) (new-value bit-array &rest subscripts)
635 (declare (type (simple-array bit) bit-array)
637 (optimize (safety 1)))
638 (setf (row-major-aref bit-array
639 (%array-row-major-index bit-array subscripts))
642 ;;;; miscellaneous array properties
644 (defun array-element-type (array)
646 "Return the type of the elements of the array"
647 (let ((widetag (widetag-of array)))
648 (macrolet ((pick-element-type (&rest stuff)
649 `(cond ,@(mapcar (lambda (stuff)
651 (let ((item (car stuff)))
660 `(= widetag ,item))))
663 #.`(pick-element-type
666 `(,(if (sb!vm:saetp-complex-typecode saetp)
667 (list (sb!vm:saetp-typecode saetp)
668 (sb!vm:saetp-complex-typecode saetp))
669 (sb!vm:saetp-typecode saetp))
670 ',(sb!vm:saetp-specifier saetp)))
671 sb!vm:*specialized-array-element-type-properties*)
672 ((sb!vm:simple-array-widetag
673 sb!vm:complex-vector-widetag
674 sb!vm:complex-array-widetag)
675 (with-array-data ((array array) (start) (end))
676 (declare (ignore start end))
677 (array-element-type array)))
679 (error 'type-error :datum array :expected-type 'array))))))
681 (defun array-rank (array)
683 "Return the number of dimensions of ARRAY."
684 (if (array-header-p array)
688 (defun array-dimension (array axis-number)
690 "Return the length of dimension AXIS-NUMBER of ARRAY."
691 (declare (array array) (type index axis-number))
692 (cond ((not (array-header-p array))
693 (unless (= axis-number 0)
694 (error "Vector axis is not zero: ~S" axis-number))
695 (length (the (simple-array * (*)) array)))
696 ((>= axis-number (%array-rank array))
697 (error "Axis number ~W is too big; ~S only has ~D dimension~:P."
698 axis-number array (%array-rank array)))
700 (%array-dimension array axis-number))))
702 (defun array-dimensions (array)
704 "Return a list whose elements are the dimensions of the array"
705 (declare (array array))
706 (if (array-header-p array)
707 (do ((results nil (cons (array-dimension array index) results))
708 (index (1- (array-rank array)) (1- index)))
709 ((minusp index) results))
710 (list (array-dimension array 0))))
712 (defun array-total-size (array)
714 "Return the total number of elements in the Array."
715 (declare (array array))
716 (if (array-header-p array)
717 (%array-available-elements array)
718 (length (the vector array))))
720 (defun array-displacement (array)
722 "Return the values of :DISPLACED-TO and :DISPLACED-INDEX-offset
723 options to MAKE-ARRAY, or NIL and 0 if not a displaced array."
724 (declare (type array array))
725 (if (and (array-header-p array) ; if unsimple and
726 (%array-displaced-p array)) ; displaced
727 (values (%array-data-vector array) (%array-displacement array))
730 (defun adjustable-array-p (array)
732 "Return T if (ADJUST-ARRAY ARRAY...) would return an array identical
733 to the argument, this happens for complex arrays."
734 (declare (array array))
735 ;; Note that this appears not to be a fundamental limitation.
736 ;; non-vector SIMPLE-ARRAYs are in fact capable of being adjusted,
737 ;; but in practice we test using ADJUSTABLE-ARRAY-P in ADJUST-ARRAY.
738 ;; -- CSR, 2004-03-01.
739 (not (typep array 'simple-array)))
741 ;;;; fill pointer frobbing stuff
743 (declaim (inline array-has-fill-pointer-p))
744 (defun array-has-fill-pointer-p (array)
746 "Return T if the given ARRAY has a fill pointer, or NIL otherwise."
747 (declare (array array))
748 (and (array-header-p array) (%array-fill-pointer-p array)))
750 (defun fill-pointer-error (vector arg)
752 (aver (array-has-fill-pointer-p vector))
753 (let ((max (%array-available-elements vector)))
754 (error 'simple-type-error
756 :expected-type (list 'integer 0 max)
757 :format-control "The new fill pointer, ~S, is larger than the length of the vector (~S.)"
758 :format-arguments (list arg max))))
760 (error 'simple-type-error
762 :expected-type '(and vector (satisfies array-has-fill-pointer-p))
763 :format-control "~S is not an array with a fill pointer."
764 :format-arguments (list vector)))))
766 (declaim (inline fill-pointer))
767 (defun fill-pointer (vector)
769 "Return the FILL-POINTER of the given VECTOR."
770 (if (array-has-fill-pointer-p vector)
771 (%array-fill-pointer vector)
772 (fill-pointer-error vector nil)))
774 (defun %set-fill-pointer (vector new)
776 (fill-pointer-error vector x)))
777 (if (array-has-fill-pointer-p vector)
778 (if (> new (%array-available-elements vector))
780 (setf (%array-fill-pointer vector) new))
783 ;;; FIXME: It'd probably make sense to use a MACROLET to share the
784 ;;; guts of VECTOR-PUSH between VECTOR-PUSH-EXTEND. Such a macro
785 ;;; should probably be based on the VECTOR-PUSH-EXTEND code (which is
786 ;;; new ca. sbcl-0.7.0) rather than the VECTOR-PUSH code (which dates
788 (defun vector-push (new-element array)
790 "Attempt to set the element of ARRAY designated by its fill pointer
791 to NEW-ELEMENT, and increment the fill pointer by one. If the fill pointer is
792 too large, NIL is returned, otherwise the index of the pushed element is
794 (let ((fill-pointer (fill-pointer array)))
795 (declare (fixnum fill-pointer))
796 (cond ((= fill-pointer (%array-available-elements array))
799 (locally (declare (optimize (safety 0)))
800 (setf (aref array fill-pointer) new-element))
801 (setf (%array-fill-pointer array) (1+ fill-pointer))
804 (defun vector-push-extend (new-element vector &optional min-extension)
805 (declare (type (or null fixnum) min-extension))
806 (let ((fill-pointer (fill-pointer vector)))
807 (declare (fixnum fill-pointer))
808 (when (= fill-pointer (%array-available-elements vector))
811 (let ((length (length vector)))
813 (- array-dimension-limit length))))))
814 (adjust-array vector (+ fill-pointer (max 1 min-extension)))))
815 ;; disable bounds checking
816 (locally (declare (optimize (safety 0)))
817 (setf (aref vector fill-pointer) new-element))
818 (setf (%array-fill-pointer vector) (1+ fill-pointer))
821 (defun vector-pop (array)
823 "Decrease the fill pointer by 1 and return the element pointed to by the
825 (let ((fill-pointer (fill-pointer array)))
826 (declare (fixnum fill-pointer))
827 (if (zerop fill-pointer)
828 (error "There is nothing left to pop.")
829 ;; disable bounds checking (and any fixnum test)
830 (locally (declare (optimize (safety 0)))
832 (setf (%array-fill-pointer array)
833 (1- fill-pointer)))))))
838 (defun adjust-array (array dimensions &key
839 (element-type (array-element-type array) element-type-p)
840 (initial-element nil initial-element-p)
841 (initial-contents nil initial-contents-p)
843 displaced-to displaced-index-offset)
845 "Adjust ARRAY's dimensions to the given DIMENSIONS and stuff."
846 (when (invalid-array-p array)
847 (invalid-array-error array))
848 (let ((dimensions (if (listp dimensions) dimensions (list dimensions))))
849 (cond ((/= (the fixnum (length (the list dimensions)))
850 (the fixnum (array-rank array)))
851 (error "The number of dimensions not equal to rank of array."))
853 (not (subtypep element-type (array-element-type array))))
854 (error "The new element type, ~S, is incompatible with old type."
856 ((and fill-pointer (not (array-has-fill-pointer-p array)))
859 :expected-type '(satisfies array-has-fill-pointer-p))))
860 (let ((array-rank (length (the list dimensions))))
861 (declare (fixnum array-rank))
862 (unless (= array-rank 1)
864 (error "Only vectors can have fill pointers.")))
865 (cond (initial-contents-p
866 ;; array former contents replaced by INITIAL-CONTENTS
867 (if (or initial-element-p displaced-to)
868 (error ":INITIAL-CONTENTS may not be specified with ~
869 the :INITIAL-ELEMENT or :DISPLACED-TO option."))
870 (let* ((array-size (apply #'* dimensions))
871 (array-data (data-vector-from-inits
872 dimensions array-size element-type nil
873 initial-contents initial-contents-p
874 initial-element initial-element-p)))
875 (if (adjustable-array-p array)
876 (set-array-header array array-data array-size
877 (get-new-fill-pointer array array-size
879 0 dimensions nil nil)
880 (if (array-header-p array)
881 ;; simple multidimensional or single dimensional array
882 (make-array dimensions
883 :element-type element-type
884 :initial-contents initial-contents)
887 ;; We already established that no INITIAL-CONTENTS was supplied.
888 (when initial-element
889 (error "The :INITIAL-ELEMENT option may not be specified ~
890 with :DISPLACED-TO."))
891 (unless (subtypep element-type (array-element-type displaced-to))
892 (error "can't displace an array of type ~S into another of ~
894 element-type (array-element-type displaced-to)))
895 (let ((displacement (or displaced-index-offset 0))
896 (array-size (apply #'* dimensions)))
897 (declare (fixnum displacement array-size))
898 (if (< (the fixnum (array-total-size displaced-to))
899 (the fixnum (+ displacement array-size)))
900 (error "The :DISPLACED-TO array is too small."))
901 (if (adjustable-array-p array)
902 ;; None of the original contents appear in adjusted array.
903 (set-array-header array displaced-to array-size
904 (get-new-fill-pointer array array-size
906 displacement dimensions t nil)
907 ;; simple multidimensional or single dimensional array
908 (make-array dimensions
909 :element-type element-type
910 :displaced-to displaced-to
911 :displaced-index-offset
912 displaced-index-offset))))
914 (let ((old-length (array-total-size array))
915 (new-length (car dimensions))
917 (declare (fixnum old-length new-length))
918 (with-array-data ((old-data array) (old-start)
919 (old-end old-length))
920 (cond ((or (and (array-header-p array)
921 (%array-displaced-p array))
922 (< old-length new-length))
924 (data-vector-from-inits
925 dimensions new-length element-type
926 (widetag-of old-data)
927 initial-contents initial-contents-p
928 initial-element initial-element-p))
929 ;; Provide :END1 to avoid full call to LENGTH
931 (replace new-data old-data
933 :start2 old-start :end2 old-end))
935 (shrink-vector old-data new-length))))
936 (if (adjustable-array-p array)
937 (set-array-header array new-data new-length
938 (get-new-fill-pointer array new-length
940 0 dimensions nil nil)
943 (let ((old-length (%array-available-elements array))
944 (new-length (apply #'* dimensions)))
945 (declare (fixnum old-length new-length))
946 (with-array-data ((old-data array) (old-start)
947 (old-end old-length))
948 (declare (ignore old-end))
949 (let ((new-data (if (or (and (array-header-p array)
950 (%array-displaced-p array))
951 (> new-length old-length))
952 (data-vector-from-inits
953 dimensions new-length
955 (widetag-of old-data) () nil
956 initial-element initial-element-p)
958 (if (or (zerop old-length) (zerop new-length))
959 (when initial-element-p (fill new-data initial-element))
960 (zap-array-data old-data (array-dimensions array)
962 new-data dimensions new-length
963 element-type initial-element
965 (if (adjustable-array-p array)
966 (set-array-header array new-data new-length
967 nil 0 dimensions nil nil)
970 sb!vm:simple-array-widetag array-rank)))
971 (set-array-header new-array new-data new-length
972 nil 0 dimensions nil t)))))))))))
975 (defun get-new-fill-pointer (old-array new-array-size fill-pointer)
976 (cond ((not fill-pointer)
977 (when (array-has-fill-pointer-p old-array)
978 (when (> (%array-fill-pointer old-array) new-array-size)
979 (error "cannot ADJUST-ARRAY an array (~S) to a size (~S) that is ~
980 smaller than its fill pointer (~S)"
981 old-array new-array-size (fill-pointer old-array)))
982 (%array-fill-pointer old-array)))
983 ((not (array-has-fill-pointer-p old-array))
984 (error "cannot supply a non-NIL value (~S) for :FILL-POINTER ~
985 in ADJUST-ARRAY unless the array (~S) was originally ~
986 created with a fill pointer"
989 ((numberp fill-pointer)
990 (when (> fill-pointer new-array-size)
991 (error "can't supply a value for :FILL-POINTER (~S) that is larger ~
992 than the new length of the vector (~S)"
993 fill-pointer new-array-size))
998 (error "bogus value for :FILL-POINTER in ADJUST-ARRAY: ~S"
1001 ;;; Destructively alter VECTOR, changing its length to NEW-LENGTH,
1002 ;;; which must be less than or equal to its current length. This can
1003 ;;; be called on vectors without a fill pointer but it is extremely
1004 ;;; dangerous to do so: shrinking the size of an object (as viewed by
1005 ;;; the gc) makes bounds checking unreliable in the face of interrupts
1006 ;;; or multi-threading. Call it only on provably local vectors.
1007 (defun %shrink-vector (vector new-length)
1008 (declare (vector vector))
1009 (unless (array-header-p vector)
1010 (macrolet ((frob (name &rest things)
1012 ((simple-array nil (*)) (error 'nil-array-accessed-error))
1013 ,@(mapcar (lambda (thing)
1014 (destructuring-bind (type-spec fill-value)
1017 (fill (truly-the ,type-spec ,name)
1019 :start new-length))))
1021 ;; Set the 'tail' of the vector to the appropriate type of zero,
1022 ;; "because in some cases we'll scavenge larger areas in one go,
1023 ;; like groups of pages that had triggered the write barrier, or
1024 ;; the whole static space" according to jsnell.
1028 `((simple-array ,(sb!vm:saetp-specifier saetp) (*))
1029 ,(if (or (eq (sb!vm:saetp-specifier saetp) 'character)
1031 (eq (sb!vm:saetp-specifier saetp) 'base-char))
1032 *default-init-char-form*
1033 (sb!vm:saetp-initial-element-default saetp))))
1035 #'sb!vm:saetp-specifier
1036 sb!vm:*specialized-array-element-type-properties*)))))
1037 ;; Only arrays have fill-pointers, but vectors have their length
1038 ;; parameter in the same place.
1039 (setf (%array-fill-pointer vector) new-length)
1042 (defun shrink-vector (vector new-length)
1043 (declare (vector vector))
1045 ((eq (length vector) new-length)
1047 ((array-has-fill-pointer-p vector)
1048 (setf (%array-fill-pointer vector) new-length)
1050 (t (subseq vector 0 new-length))))
1052 ;;; BIG THREAD SAFETY NOTE
1054 ;;; ADJUST-ARRAY/SET-ARRAY-HEADER, and its callees are very
1055 ;;; thread unsafe. They are nonatomic, and can mess with parallel
1056 ;;; code using the same arrays.
1058 ;;; A likely seeming fix is an additional level of indirection:
1059 ;;; ARRAY-HEADER -> ARRAY-INFO -> ... where ARRAY-HEADER would
1060 ;;; hold nothing but the pointer to ARRAY-INFO, and ARRAY-INFO
1061 ;;; would hold everything ARRAY-HEADER now holds. This allows
1062 ;;; consing up a new ARRAY-INFO and replacing it atomically in
1063 ;;; the ARRAY-HEADER.
1065 ;;; %WALK-DISPLACED-ARRAY-BACKPOINTERS is an especially nasty
1066 ;;; one: not only is it needed extremely rarely, which makes
1067 ;;; any thread safety bugs involving it look like rare random
1068 ;;; corruption, but because it walks the chain *upwards*, which
1069 ;;; may violate user expectations.
1071 (defun %save-displaced-array-backpointer (array data)
1072 (flet ((purge (pointers)
1073 (remove-if (lambda (value)
1074 (or (not value) (eq array value)))
1076 :key #'weak-pointer-value)))
1077 ;; Add backpointer to the new data vector if it has a header.
1078 (when (array-header-p data)
1079 (setf (%array-displaced-from data)
1080 (cons (make-weak-pointer array)
1081 (purge (%array-displaced-from data)))))
1082 ;; Remove old backpointer, if any.
1083 (let ((old-data (%array-data-vector array)))
1084 (when (and (neq data old-data) (array-header-p old-data))
1085 (setf (%array-displaced-from old-data)
1086 (purge (%array-displaced-from old-data)))))))
1088 (defun %walk-displaced-array-backpointers (array new-length)
1089 (dolist (p (%array-displaced-from array))
1090 (let ((from (weak-pointer-value p)))
1091 (when (and from (eq array (%array-data-vector from)))
1092 (let ((requires (+ (%array-available-elements from)
1093 (%array-displacement from))))
1094 (unless (>= new-length requires)
1095 ;; ANSI sayeth (ADJUST-ARRAY dictionary entry):
1097 ;; "If A is displaced to B, the consequences are unspecified if B is
1098 ;; adjusted in such a way that it no longer has enough elements to
1101 ;; since we're hanging on a weak pointer here, we can't signal an
1102 ;; error right now: the array that we're looking at might be
1103 ;; garbage. Instead, we set all dimensions to zero so that next
1104 ;; safe access to the displaced array will trap. Additionally, we
1105 ;; save the original dimensions, so we can signal a more
1106 ;; understandable error when the time comes.
1107 (%walk-displaced-array-backpointers from 0)
1108 (setf (%array-fill-pointer from) 0
1109 (%array-available-elements from) 0
1110 (%array-displaced-p from) (array-dimensions array))
1111 (dotimes (i (%array-rank from))
1112 (setf (%array-dimension from i) 0))))))))
1114 ;;; Fill in array header with the provided information, and return the array.
1115 (defun set-array-header (array data length fill-pointer displacement dimensions
1118 (setf (%array-displaced-from array) nil)
1119 (%walk-displaced-array-backpointers array length))
1121 (%save-displaced-array-backpointer array data))
1122 (setf (%array-data-vector array) data)
1123 (setf (%array-available-elements array) length)
1125 (setf (%array-fill-pointer array) fill-pointer)
1126 (setf (%array-fill-pointer-p array) t))
1128 (setf (%array-fill-pointer array) length)
1129 (setf (%array-fill-pointer-p array) nil)))
1130 (setf (%array-displacement array) displacement)
1131 (if (listp dimensions)
1132 (dotimes (axis (array-rank array))
1133 (declare (type index axis))
1134 (setf (%array-dimension array axis) (pop dimensions)))
1135 (setf (%array-dimension array 0) dimensions))
1136 (setf (%array-displaced-p array) displacedp)
1139 ;;; User visible extension
1140 (declaim (ftype (function (array) (values (simple-array * (*)) &optional))
1141 array-storage-vector))
1142 (defun array-storage-vector (array)
1143 "Returns the underlying storage vector of ARRAY, which must be a non-displaced array.
1145 In SBCL, if ARRAY is a of type \(SIMPLE-ARRAY * \(*)), it is its own storage
1146 vector. Multidimensional arrays, arrays with fill pointers, and adjustable
1147 arrays have an underlying storage vector with the same ARRAY-ELEMENT-TYPE as
1148 ARRAY, which this function returns.
1150 Important note: the underlying vector is an implementation detail. Even though
1151 this function exposes it, changes in the implementation may cause this
1152 function to be removed without further warning."
1153 ;; KLUDGE: Without TRULY-THE the system is not smart enough to figure out that
1154 ;; the return value is always of the known type.
1155 (truly-the (simple-array * (*))
1156 (if (array-header-p array)
1157 (if (%array-displaced-p array)
1158 (error "~S cannot be used with displaced arrays. Use ~S instead."
1159 'array-storage-vector 'array-displacement)
1160 (%array-data-vector array))
1164 ;;;; ZAP-ARRAY-DATA for ADJUST-ARRAY
1166 ;;; This does the grinding work for ADJUST-ARRAY. It zaps the data
1167 ;;; from the OLD-DATA in an arrangement specified by the OLD-DIMS to
1168 ;;; the NEW-DATA in an arrangement specified by the NEW-DIMS. OFFSET
1169 ;;; is a displaced offset to be added to computed indices of OLD-DATA.
1170 (defun zap-array-data (old-data old-dims offset new-data new-dims new-length
1171 element-type initial-element initial-element-p)
1172 (declare (list old-dims new-dims)
1173 (fixnum new-length))
1174 ;; OLD-DIMS comes from array-dimensions, which returns a fresh list
1175 ;; at least in SBCL.
1176 ;; NEW-DIMS comes from the user.
1177 (setf old-dims (nreverse old-dims)
1178 new-dims (reverse new-dims))
1179 (cond ((eq old-data new-data)
1180 ;; NEW-LENGTH, ELEMENT-TYPE, INITIAL-ELEMENT, and
1181 ;; INITIAL-ELEMENT-P are used when OLD-DATA and NEW-DATA are
1182 ;; EQ; in this case, a temporary must be used and filled
1183 ;; appropriately. specified initial-element.
1184 (when initial-element-p
1185 ;; FIXME: transforming this TYPEP to someting a bit faster
1186 ;; would be a win...
1187 (unless (typep initial-element element-type)
1188 (error "~S can't be used to initialize an array of type ~S."
1189 initial-element element-type)))
1190 (let ((temp (if initial-element-p
1191 (make-array new-length :initial-element initial-element)
1192 (make-array new-length))))
1193 (declare (simple-vector temp))
1194 (zap-array-data-aux old-data old-dims offset temp new-dims)
1195 (dotimes (i new-length)
1196 (setf (aref new-data i) (aref temp i)))
1197 ;; Kill the temporary vector to prevent garbage retention.
1198 (%shrink-vector temp 0)))
1200 ;; When OLD-DATA and NEW-DATA are not EQ, NEW-DATA has
1201 ;; already been filled with any
1202 (zap-array-data-aux old-data old-dims offset new-data new-dims))))
1204 (defun zap-array-data-aux (old-data old-dims offset new-data new-dims)
1205 (declare (fixnum offset))
1206 (let ((limits (mapcar (lambda (x y)
1207 (declare (fixnum x y))
1208 (1- (the fixnum (min x y))))
1209 old-dims new-dims)))
1210 (macrolet ((bump-index-list (index limits)
1211 `(do ((subscripts ,index (cdr subscripts))
1212 (limits ,limits (cdr limits)))
1213 ((null subscripts) :eof)
1214 (cond ((< (the fixnum (car subscripts))
1215 (the fixnum (car limits)))
1217 (1+ (the fixnum (car subscripts))))
1219 (t (rplaca subscripts 0))))))
1220 (do ((index (make-list (length old-dims) :initial-element 0)
1221 (bump-index-list index limits)))
1223 (setf (aref new-data (row-major-index-from-dims index new-dims))
1225 (+ (the fixnum (row-major-index-from-dims index old-dims))
1228 ;;; Figure out the row-major-order index of an array reference from a
1229 ;;; list of subscripts and a list of dimensions. This is for internal
1230 ;;; calls only, and the subscripts and dim-list variables are assumed
1231 ;;; to be reversed from what the user supplied.
1232 (defun row-major-index-from-dims (rev-subscripts rev-dim-list)
1233 (do ((rev-subscripts rev-subscripts (cdr rev-subscripts))
1234 (rev-dim-list rev-dim-list (cdr rev-dim-list))
1237 ((null rev-dim-list) result)
1238 (declare (fixnum chunk-size result))
1239 (setq result (+ result
1240 (the fixnum (* (the fixnum (car rev-subscripts))
1242 (setq chunk-size (* chunk-size (the fixnum (car rev-dim-list))))))
1246 (defun bit-array-same-dimensions-p (array1 array2)
1247 (declare (type (array bit) array1 array2))
1248 (and (= (array-rank array1)
1249 (array-rank array2))
1250 (dotimes (index (array-rank array1) t)
1251 (when (/= (array-dimension array1 index)
1252 (array-dimension array2 index))
1255 (defun pick-result-array (result-bit-array bit-array-1)
1256 (case result-bit-array
1258 ((nil) (make-array (array-dimensions bit-array-1)
1260 :initial-element 0))
1262 (unless (bit-array-same-dimensions-p bit-array-1
1264 (error "~S and ~S don't have the same dimensions."
1265 bit-array-1 result-bit-array))
1268 (defmacro def-bit-array-op (name function)
1269 `(defun ,name (bit-array-1 bit-array-2 &optional result-bit-array)
1272 "Perform a bit-wise ~A on the elements of BIT-ARRAY-1 and ~
1273 BIT-ARRAY-2,~% putting the results in RESULT-BIT-ARRAY. ~
1274 If RESULT-BIT-ARRAY is T,~% BIT-ARRAY-1 is used. If ~
1275 RESULT-BIT-ARRAY is NIL or omitted, a new array is~% created. ~
1276 All the arrays must have the same rank and dimensions."
1277 (symbol-name function))
1278 (declare (type (array bit) bit-array-1 bit-array-2)
1279 (type (or (array bit) (member t nil)) result-bit-array))
1280 (unless (bit-array-same-dimensions-p bit-array-1 bit-array-2)
1281 (error "~S and ~S don't have the same dimensions."
1282 bit-array-1 bit-array-2))
1283 (let ((result-bit-array (pick-result-array result-bit-array bit-array-1)))
1284 (if (and (simple-bit-vector-p bit-array-1)
1285 (simple-bit-vector-p bit-array-2)
1286 (simple-bit-vector-p result-bit-array))
1287 (locally (declare (optimize (speed 3) (safety 0)))
1288 (,name bit-array-1 bit-array-2 result-bit-array))
1289 (with-array-data ((data1 bit-array-1) (start1) (end1))
1290 (declare (ignore end1))
1291 (with-array-data ((data2 bit-array-2) (start2) (end2))
1292 (declare (ignore end2))
1293 (with-array-data ((data3 result-bit-array) (start3) (end3))
1294 (do ((index-1 start1 (1+ index-1))
1295 (index-2 start2 (1+ index-2))
1296 (index-3 start3 (1+ index-3)))
1297 ((>= index-3 end3) result-bit-array)
1298 (declare (type index index-1 index-2 index-3))
1299 (setf (sbit data3 index-3)
1300 (logand (,function (sbit data1 index-1)
1301 (sbit data2 index-2))
1304 (def-bit-array-op bit-and logand)
1305 (def-bit-array-op bit-ior logior)
1306 (def-bit-array-op bit-xor logxor)
1307 (def-bit-array-op bit-eqv logeqv)
1308 (def-bit-array-op bit-nand lognand)
1309 (def-bit-array-op bit-nor lognor)
1310 (def-bit-array-op bit-andc1 logandc1)
1311 (def-bit-array-op bit-andc2 logandc2)
1312 (def-bit-array-op bit-orc1 logorc1)
1313 (def-bit-array-op bit-orc2 logorc2)
1315 (defun bit-not (bit-array &optional result-bit-array)
1317 "Performs a bit-wise logical NOT on the elements of BIT-ARRAY,
1318 putting the results in RESULT-BIT-ARRAY. If RESULT-BIT-ARRAY is T,
1319 BIT-ARRAY is used. If RESULT-BIT-ARRAY is NIL or omitted, a new array is
1320 created. Both arrays must have the same rank and dimensions."
1321 (declare (type (array bit) bit-array)
1322 (type (or (array bit) (member t nil)) result-bit-array))
1323 (let ((result-bit-array (pick-result-array result-bit-array bit-array)))
1324 (if (and (simple-bit-vector-p bit-array)
1325 (simple-bit-vector-p result-bit-array))
1326 (locally (declare (optimize (speed 3) (safety 0)))
1327 (bit-not bit-array result-bit-array))
1328 (with-array-data ((src bit-array) (src-start) (src-end))
1329 (declare (ignore src-end))
1330 (with-array-data ((dst result-bit-array) (dst-start) (dst-end))
1331 (do ((src-index src-start (1+ src-index))
1332 (dst-index dst-start (1+ dst-index)))
1333 ((>= dst-index dst-end) result-bit-array)
1334 (declare (type index src-index dst-index))
1335 (setf (sbit dst dst-index)
1336 (logxor (sbit src src-index) 1))))))))
1338 ;;;; array type dispatching
1340 ;;; Given DISPATCH-FOO as the DISPATCH-NAME argument (unevaluated),
1341 ;;; defines the functions
1343 ;;; DISPATCH-FOO/SIMPLE-BASE-STRING
1344 ;;; DISPATCH-FOO/SIMPLE-CHARACTER-STRING
1345 ;;; DISPATCH-FOO/SIMPLE-ARRAY-SINGLE-FLOAT
1348 ;;; PARAMS are the function parameters in the definition of each
1349 ;;; specializer function. The array being specialized must be the
1350 ;;; first parameter in PARAMS. A type declaration for this parameter
1351 ;;; is automatically inserted into the body of each function.
1353 ;;; The dispatch table %%FOO-FUNS%% is defined and populated by these
1354 ;;; functions. The table is padded by the function
1355 ;;; HAIRY-FOO-DISPATCH-ERROR, also defined by DEFINE-ARRAY-DISPATCH.
1357 ;;; Finally, the DISPATCH-FOO macro is defined which does the actual
1358 ;;; dispatching when called. It expects arguments that match PARAMS.
1360 (defmacro define-array-dispatch (dispatch-name params &body body)
1361 (let ((table-name (symbolicate "%%" dispatch-name "-FUNS%%"))
1362 (error-name (symbolicate "HAIRY-" dispatch-name "-ERROR")))
1364 (eval-when (:compile-toplevel :load-toplevel :execute)
1365 (defun ,error-name (&rest args)
1368 :expected-type '(simple-array * (*)))))
1369 (defglobal ,table-name (make-array ,(1+ sb!vm:widetag-mask)
1370 :initial-element #',error-name))
1371 ,@(loop for info across sb!vm:*specialized-array-element-type-properties*
1372 for typecode = (sb!vm:saetp-typecode info)
1373 for specifier = (sb!vm:saetp-specifier info)
1374 for primitive-type-name = (sb!vm:saetp-primitive-type-name info)
1375 collect (let ((fun-name (symbolicate (string dispatch-name)
1376 "/" primitive-type-name)))
1378 (defun ,fun-name ,params
1379 (declare (type (simple-array ,specifier (*))
1382 (setf (svref ,table-name ,typecode) #',fun-name))))
1383 (defmacro ,dispatch-name (&rest args)
1384 (check-type (first args) symbol)
1385 (let ((tag (gensym "TAG")))
1389 (when (sb!vm::%other-pointer-p ,(first args))
1390 (setf ,tag (%other-pointer-widetag ,(first args))))
1391 (svref ,',table-name ,tag)))