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 fill-pointer array-has-fill-pointer-p 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))
35 (defun %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)
47 (%check-bound array bound index))
49 (defun %with-array-data (array start end)
50 (%with-array-data-macro array start end :fail-inline? t))
52 (defun %data-vector-and-index (array index)
53 (if (array-header-p array)
54 (multiple-value-bind (vector index)
55 (%with-array-data array index nil)
56 (values vector index))
57 (values array index)))
59 ;;; It'd waste space to expand copies of error handling in every
60 ;;; inline %WITH-ARRAY-DATA, so we have them call this function
61 ;;; instead. This is just a wrapper which is known never to return.
62 (defun failed-%with-array-data (array start end)
63 (declare (notinline %with-array-data))
64 (%with-array-data array start end)
65 (bug "called FAILED-%WITH-ARRAY-DATA with valid array parameters?"))
68 (eval-when (:compile-toplevel :execute)
69 (sb!xc:defmacro pick-vector-type (type &rest specs)
70 `(cond ,@(mapcar (lambda (spec)
71 `(,(if (eq (car spec) t)
73 `(subtypep ,type ',(car spec)))
77 ;;; These functions are used in the implementation of MAKE-ARRAY for
78 ;;; complex arrays. There are lots of transforms to simplify
79 ;;; MAKE-ARRAY for various easy cases, but not for all reasonable
80 ;;; cases, so e.g. as of sbcl-0.6.6 we still make full calls to
81 ;;; MAKE-ARRAY for any non-simple array. Thus, there's some value to
82 ;;; making this somewhat efficient, at least not doing full calls to
83 ;;; SUBTYPEP in the easy cases.
84 (defun %vector-widetag-and-n-bits (type)
86 ;; Pick off some easy common cases.
88 ;; (Perhaps we should make a much more exhaustive table of easy
89 ;; common cases here. Or perhaps the effort would be better spent
90 ;; on smarter compiler transforms which do the calculation once
91 ;; and for all in any reasonable user programs.)
93 (values #.sb!vm:simple-vector-widetag #.sb!vm:n-word-bits))
94 ((base-char standard-char #!-sb-unicode character)
95 (values #.sb!vm:simple-base-string-widetag #.sb!vm:n-byte-bits))
98 (values #.sb!vm:simple-character-string-widetag #.sb!vm:n-word-bits))
100 (values #.sb!vm:simple-bit-vector-widetag 1))
101 ;; OK, we have to wade into SUBTYPEPing after all.
103 #.`(pick-vector-type type
106 `(,(sb!vm:saetp-specifier saetp)
107 (values ,(sb!vm:saetp-typecode saetp)
108 ,(sb!vm:saetp-n-bits saetp))))
109 sb!vm:*specialized-array-element-type-properties*)))))
111 (defun %complex-vector-widetag (type)
113 ;; Pick off some easy common cases.
115 #.sb!vm:complex-vector-widetag)
116 ((base-char #!-sb-unicode character)
117 #.sb!vm:complex-base-string-widetag)
120 #.sb!vm:complex-character-string-widetag)
122 #.sb!vm:complex-vector-nil-widetag)
124 #.sb!vm:complex-bit-vector-widetag)
125 ;; OK, we have to wade into SUBTYPEPing after all.
127 (pick-vector-type type
128 (nil #.sb!vm:complex-vector-nil-widetag)
130 (character #.sb!vm:complex-base-string-widetag)
132 (base-char #.sb!vm:complex-base-string-widetag)
134 (character #.sb!vm:complex-character-string-widetag)
135 (bit #.sb!vm:complex-bit-vector-widetag)
136 (t #.sb!vm:complex-vector-widetag)))))
138 (defun make-array (dimensions &key
140 (initial-element nil initial-element-p)
141 (initial-contents nil initial-contents-p)
142 adjustable fill-pointer
143 displaced-to displaced-index-offset)
144 (let* ((dimensions (if (listp dimensions) dimensions (list dimensions)))
145 (array-rank (length (the list dimensions)))
146 (simple (and (null fill-pointer)
148 (null displaced-to))))
149 (declare (fixnum array-rank))
150 (when (and displaced-index-offset (null displaced-to))
151 (error "can't specify :DISPLACED-INDEX-OFFSET without :DISPLACED-TO"))
152 (when (and displaced-to
153 (arrayp displaced-to)
154 (not (equal (array-element-type displaced-to)
155 (upgraded-array-element-type element-type))))
156 (error "Array element type of :DISPLACED-TO array does not match specified element type"))
157 (if (and simple (= array-rank 1))
158 ;; it's a (SIMPLE-ARRAY * (*))
159 (multiple-value-bind (type n-bits)
160 (%vector-widetag-and-n-bits element-type)
161 (declare (type (unsigned-byte 8) type)
162 (type (integer 0 256) n-bits))
163 (let* ((length (car dimensions))
164 (array (allocate-vector
168 (* (if (or (= type sb!vm:simple-base-string-widetag)
171 sb!vm:simple-character-string-widetag))
175 sb!vm:n-word-bits))))
176 (declare (type index length))
177 (when initial-element-p
178 (fill array initial-element))
179 (when initial-contents-p
180 (when initial-element-p
181 (error "can't specify both :INITIAL-ELEMENT and ~
183 (unless (= length (length initial-contents))
184 (error "There are ~W elements in the :INITIAL-CONTENTS, but ~
185 the vector length is ~W."
186 (length initial-contents)
188 (replace array initial-contents))
190 ;; it's either a complex array or a multidimensional array.
191 (let* ((total-size (reduce #'* dimensions))
192 (data (or displaced-to
193 (data-vector-from-inits
194 dimensions total-size element-type
195 initial-contents initial-contents-p
196 initial-element initial-element-p)))
197 (array (make-array-header
198 (cond ((= array-rank 1)
199 (%complex-vector-widetag element-type))
200 (simple sb!vm:simple-array-widetag)
201 (t sb!vm:complex-array-widetag))
204 (unless (= array-rank 1)
205 (error "Only vectors can have fill pointers."))
206 (let ((length (car dimensions)))
207 (declare (fixnum length))
208 (setf (%array-fill-pointer array)
209 (cond ((eq fill-pointer t)
212 (unless (and (fixnump fill-pointer)
214 (<= fill-pointer length))
215 ;; FIXME: should be TYPE-ERROR?
216 (error "invalid fill-pointer ~W"
219 (setf (%array-fill-pointer-p array) t))
221 (setf (%array-fill-pointer array) total-size)
222 (setf (%array-fill-pointer-p array) nil)))
223 (setf (%array-available-elements array) total-size)
224 (setf (%array-data-vector array) data)
226 (when (or initial-element-p initial-contents-p)
227 (error "Neither :INITIAL-ELEMENT nor :INITIAL-CONTENTS ~
228 can be specified along with :DISPLACED-TO"))
229 (let ((offset (or displaced-index-offset 0)))
230 (when (> (+ offset total-size)
231 (array-total-size displaced-to))
232 (error "~S doesn't have enough elements." displaced-to))
233 (setf (%array-displacement array) offset)
234 (setf (%array-displaced-p array) t)))
236 (setf (%array-displaced-p array) nil)))
238 (dolist (dim dimensions)
239 (setf (%array-dimension array axis) dim)
243 (defun make-static-vector (length &key
244 (element-type '(unsigned-byte 8))
245 (initial-contents nil initial-contents-p)
246 (initial-element nil initial-element-p))
247 "Allocate vector of LENGTH elements in static space. Only allocation
248 of specialized arrays is supported."
249 ;; STEP 1: check inputs fully
251 ;; This way of doing explicit checks before the vector is allocated
252 ;; is expensive, but probably worth the trouble as once we've allocated
253 ;; the vector we have no way to get rid of it anymore...
254 (when (eq t (upgraded-array-element-type element-type))
255 (error "Static arrays of type ~S not supported."
257 (when initial-contents-p
258 (when initial-element-p
259 (error "can't specify both :INITIAL-ELEMENT and :INITIAL-CONTENTS"))
260 (unless (= length (length initial-contents))
261 (error "There are ~W elements in the :INITIAL-CONTENTS, but the ~
262 vector length is ~W."
263 (length initial-contents)
265 (unless (every (lambda (x) (typep x element-type)) initial-contents)
266 (error ":INITIAL-CONTENTS contains elements not of type ~S."
268 (when initial-element-p
269 (unless (typep initial-element element-type)
270 (error ":INITIAL-ELEMENT ~S is not of type ~S."
271 initial-element element-type)))
274 ;; Allocate and possibly initialize the vector.
275 (multiple-value-bind (type n-bits)
276 (sb!impl::%vector-widetag-and-n-bits element-type)
278 (allocate-static-vector type length
279 (ceiling (* length n-bits)
280 sb!vm:n-word-bits))))
281 (cond (initial-element-p
282 (fill vector initial-element))
284 (replace vector initial-contents))
288 ;;; DATA-VECTOR-FROM-INITS returns a simple vector that has the
289 ;;; specified array characteristics. Dimensions is only used to pass
290 ;;; to FILL-DATA-VECTOR for error checking on the structure of
291 ;;; initial-contents.
292 (defun data-vector-from-inits (dimensions total-size element-type
293 initial-contents initial-contents-p
294 initial-element initial-element-p)
295 (when (and initial-contents-p initial-element-p)
296 (error "cannot supply both :INITIAL-CONTENTS and :INITIAL-ELEMENT to
297 either MAKE-ARRAY or ADJUST-ARRAY."))
298 (let ((data (if initial-element-p
299 (make-array total-size
300 :element-type element-type
301 :initial-element initial-element)
302 (make-array total-size
303 :element-type element-type))))
304 (cond (initial-element-p
305 (unless (simple-vector-p data)
306 (unless (typep initial-element element-type)
307 (error "~S cannot be used to initialize an array of type ~S."
308 initial-element element-type))
309 (fill (the vector data) initial-element)))
311 (fill-data-vector data dimensions initial-contents)))
314 (defun vector (&rest objects)
316 "Construct a SIMPLE-VECTOR from the given objects."
317 (coerce (the list objects) 'simple-vector))
320 ;;;; accessor/setter functions
322 ;;; Dispatch to an optimized routine the data vector accessors for
323 ;;; each different specialized vector type. Do dispatching by looking
324 ;;; up the widetag in the array rather than with the typecases, which
325 ;;; as of 1.0.5 compiles to a naive sequence of linear TYPEPs. Also
326 ;;; provide separate versions where bounds checking has been moved
327 ;;; from the callee to the caller, since it's much cheaper to do once
328 ;;; the type information is available. Finally, for each of these
329 ;;; routines also provide a slow path, taken for arrays that are not
330 ;;; vectors or not simple.
331 (macrolet ((%define (table-name extra-params)
336 #.(ecase sb!c:*backend-byte-order*
338 (- sb!vm:other-pointer-lowtag))
340 (- (1- sb!vm:n-word-bytes) sb!vm:other-pointer-lowtag)))))
341 ;; WIDETAG-OF needs extra code to handle
342 ;; LIST and FUNCTION lowtags. We're only
343 ;; dispatching on other pointers, so let's
344 ;; do the lowtag extraction manually.
345 (when (sb!vm::%other-pointer-p array)
347 (sb!sys:sap-ref-8 (int-sap (get-lisp-obj-address array))
349 ;; SYMBOL-GLOBAL-VALUE is a performance hack
350 ;; for threaded builds.
351 (svref (sb!vm::symbol-global-value ',table-name) tag)))
352 array index ,@extra-params))
353 (define (accessor-name slow-accessor-name table-name extra-params
357 (defun ,accessor-name (array index ,@extra-params)
358 (declare (optimize speed
359 ;; (SAFETY 0) is ok. All calls to
360 ;; these functions are generated by
361 ;; the compiler, so argument count
362 ;; checking isn't needed. Type checking
363 ;; is done implicitly via the widetag
366 (%define ,table-name ,extra-params))
367 (defun ,slow-accessor-name (array index ,@extra-params)
368 (declare (optimize speed (safety 0)))
369 (if (not (%array-displaced-p array))
370 ;; The reasonably quick path of non-displaced complex
372 (let ((array (%array-data-vector array)))
373 (%define ,table-name ,extra-params))
374 ;; The real slow path.
378 (declare (optimize (speed 1) (safety 1)))
379 (,@check-bounds index)))
382 (declare (ignore end))
383 (,accessor-name vector index ,@extra-params)))))))
384 (define hairy-data-vector-ref slow-hairy-data-vector-ref
385 *data-vector-reffers* nil (progn))
386 (define hairy-data-vector-set slow-hairy-data-vector-set
387 *data-vector-setters* (new-value) (progn))
388 (define hairy-data-vector-ref/check-bounds
389 slow-hairy-data-vector-ref/check-bounds
390 *data-vector-reffers/check-bounds* nil
391 (%check-bound array (array-dimension array 0)))
392 (define hairy-data-vector-set/check-bounds
393 slow-hairy-data-vector-set/check-bounds
394 *data-vector-setters/check-bounds* (new-value)
395 (%check-bound array (array-dimension array 0))))
397 (defun hairy-ref-error (array index &optional new-value)
398 (declare (ignore index new-value))
401 :expected-type 'vector))
403 ;;; Populate the dispatch tables.
404 (macrolet ((define-reffer (saetp check-form)
405 (let* ((type (sb!vm:saetp-specifier saetp))
406 (atype `(simple-array ,type (*))))
407 `(named-lambda optimized-data-vector-ref (vector index)
408 (declare (optimize speed (safety 0)))
409 (data-vector-ref (the ,atype vector)
411 (declare (optimize (safety 1)))
413 (,@check-form index)))))))
414 (define-setter (saetp check-form)
415 (let* ((type (sb!vm:saetp-specifier saetp))
416 (atype `(simple-array ,type (*))))
417 `(named-lambda optimized-data-vector-set (vector index new-value)
418 (declare (optimize speed (safety 0)))
419 (data-vector-set (the ,atype vector)
421 (declare (optimize (safety 1)))
423 (,@check-form index)))
425 ;; SPEED 1 needed to avoid the compiler
426 ;; from downgrading the type check to
428 (declare (optimize (speed 1)
430 (the ,type new-value)))
431 ;; For specialized arrays, the return from
432 ;; data-vector-set would have to be reboxed to be a
433 ;; (Lisp) return value; instead, we use the
434 ;; already-boxed value as the return.
436 (define-reffers (symbol deffer check-form slow-path)
438 (setf ,symbol (make-array sb!vm::widetag-mask
439 :initial-element #'hairy-ref-error))
440 ,@(loop for widetag in '(sb!vm:complex-vector-widetag
441 sb!vm:complex-vector-nil-widetag
442 sb!vm:complex-bit-vector-widetag
443 #!+sb-unicode sb!vm:complex-character-string-widetag
444 sb!vm:complex-base-string-widetag
445 sb!vm:simple-array-widetag
446 sb!vm:complex-array-widetag)
447 collect `(setf (svref ,symbol ,widetag) ,slow-path))
448 ,@(loop for saetp across sb!vm:*specialized-array-element-type-properties*
449 for widetag = (sb!vm:saetp-typecode saetp)
450 collect `(setf (svref ,symbol ,widetag)
451 (,deffer ,saetp ,check-form))))))
452 (defun !hairy-data-vector-reffer-init ()
453 (define-reffers *data-vector-reffers* define-reffer
455 #'slow-hairy-data-vector-ref)
456 (define-reffers *data-vector-setters* define-setter
458 #'slow-hairy-data-vector-set)
459 (define-reffers *data-vector-reffers/check-bounds* define-reffer
460 (%check-bound vector (length vector))
461 #'slow-hairy-data-vector-ref/check-bounds)
462 (define-reffers *data-vector-setters/check-bounds* define-setter
463 (%check-bound vector (length vector))
464 #'slow-hairy-data-vector-set/check-bounds)))
466 ;;; (Ordinary DATA-VECTOR-REF usage compiles into a vop, but
467 ;;; DATA-VECTOR-REF is also FOLDABLE, and this ordinary function
468 ;;; definition is needed for the compiler to use in constant folding.)
469 (defun data-vector-ref (array index)
470 (hairy-data-vector-ref array index))
472 (defun data-vector-ref-with-offset (array index offset)
473 (hairy-data-vector-ref array (+ index offset)))
475 ;;; SUBSCRIPTS has a dynamic-extent list structure and is destroyed
476 (defun %array-row-major-index (array subscripts
477 &optional (invalid-index-error-p t))
478 (declare (array array)
480 (let ((rank (array-rank array)))
481 (unless (= rank (length subscripts))
482 (error "wrong number of subscripts, ~W, for array of rank ~W"
483 (length subscripts) rank))
484 (if (array-header-p array)
485 (do ((subs (nreverse subscripts) (cdr subs))
486 (axis (1- (array-rank array)) (1- axis))
490 (declare (list subs) (fixnum axis chunk-size result))
491 (let ((index (car subs))
492 (dim (%array-dimension array axis)))
493 (declare (fixnum dim))
494 (unless (and (fixnump index) (< -1 index dim))
495 (if invalid-index-error-p
496 (error 'simple-type-error
497 :format-control "invalid index ~W~[~;~:; on axis ~:*~W~] in ~S"
498 :format-arguments (list index axis array)
500 :expected-type `(integer 0 (,dim)))
501 (return-from %array-row-major-index nil)))
502 (incf result (* chunk-size (the fixnum index)))
503 (setf chunk-size (* chunk-size dim))))
504 (let ((index (first subscripts))
505 (length (length (the (simple-array * (*)) array))))
506 (unless (and (fixnump index) (< -1 index length))
507 (if invalid-index-error-p
508 ;; FIXME: perhaps this should share a format-string
509 ;; with INVALID-ARRAY-INDEX-ERROR or
510 ;; INDEX-TOO-LARGE-ERROR?
511 (error 'simple-type-error
512 :format-control "invalid index ~W in ~S"
513 :format-arguments (list index array)
515 :expected-type `(integer 0 (,length)))
516 (return-from %array-row-major-index nil)))
519 (defun array-in-bounds-p (array &rest subscripts)
521 "Return T if the SUBSCIPTS are in bounds for the ARRAY, NIL otherwise."
522 (if (%array-row-major-index array subscripts nil)
525 (defun array-row-major-index (array &rest subscripts)
526 (declare (dynamic-extent subscripts))
527 (%array-row-major-index array subscripts))
529 (defun aref (array &rest subscripts)
531 "Return the element of the ARRAY specified by the SUBSCRIPTS."
532 (declare (dynamic-extent subscripts))
533 (row-major-aref array (%array-row-major-index array subscripts)))
535 (defun %aset (array &rest stuff)
536 (declare (dynamic-extent stuff))
537 (let ((subscripts (butlast stuff))
538 (new-value (car (last stuff))))
539 (setf (row-major-aref array (%array-row-major-index array subscripts))
542 ;;; FIXME: What's supposed to happen with functions
543 ;;; like AREF when we (DEFUN (SETF FOO) ..) when
544 ;;; DEFSETF FOO is also defined? It seems as though the logical
545 ;;; thing to do would be to nuke the macro definition for (SETF FOO)
546 ;;; and replace it with the (SETF FOO) function, issuing a warning,
547 ;;; just as for ordinary functions
548 ;;; * (LISP-IMPLEMENTATION-VERSION)
549 ;;; "18a+ release x86-linux 2.4.7 6 November 1998 cvs"
550 ;;; * (DEFMACRO ZOO (X) `(+ ,X ,X))
552 ;;; * (DEFUN ZOO (X) (* 3 X))
553 ;;; Warning: ZOO previously defined as a macro.
555 ;;; But that doesn't seem to be what happens in CMU CL.
557 ;;; KLUDGE: this is probably because ANSI, in its wisdom (CLHS
558 ;;; 5.1.2.5) requires implementations to support
559 ;;; (SETF (APPLY #'AREF ...) ...)
560 ;;; [and also #'BIT and #'SBIT]. Yes, this is terrifying, and it's
561 ;;; also terrifying that this sequence of definitions causes it to
564 ;;; Also, it would be nice to make DESCRIBE FOO tell whether a symbol
565 ;;; has a setf expansion and/or a setf function defined.
567 #!-sb-fluid (declaim (inline (setf aref)))
568 (defun (setf aref) (new-value array &rest subscripts)
569 (declare (dynamic-extent subscripts))
570 (declare (type array array))
571 (setf (row-major-aref array (%array-row-major-index array subscripts))
574 (defun row-major-aref (array index)
576 "Return the element of array corressponding to the row-major index. This is
578 (declare (optimize (safety 1)))
579 (row-major-aref array index))
581 (defun %set-row-major-aref (array index new-value)
582 (declare (optimize (safety 1)))
583 (setf (row-major-aref array index) new-value))
585 (defun svref (simple-vector index)
587 "Return the INDEX'th element of the given Simple-Vector."
588 (declare (optimize (safety 1)))
589 (aref simple-vector index))
591 (defun %svset (simple-vector index new)
592 (declare (optimize (safety 1)))
593 (setf (aref simple-vector index) new))
595 (defun bit (bit-array &rest subscripts)
597 "Return the bit from the BIT-ARRAY at the specified SUBSCRIPTS."
598 (declare (type (array bit) bit-array) (optimize (safety 1)))
599 (row-major-aref bit-array (%array-row-major-index bit-array subscripts)))
601 (defun %bitset (bit-array &rest stuff)
602 (declare (type (array bit) bit-array) (optimize (safety 1)))
603 (let ((subscripts (butlast stuff))
604 (new-value (car (last stuff))))
605 (setf (row-major-aref bit-array
606 (%array-row-major-index bit-array subscripts))
609 #!-sb-fluid (declaim (inline (setf bit)))
610 (defun (setf bit) (new-value bit-array &rest subscripts)
611 (declare (type (array bit) bit-array) (optimize (safety 1)))
612 (setf (row-major-aref bit-array
613 (%array-row-major-index bit-array subscripts))
616 (defun sbit (simple-bit-array &rest subscripts)
618 "Return the bit from SIMPLE-BIT-ARRAY at the specified SUBSCRIPTS."
619 (declare (type (simple-array bit) simple-bit-array) (optimize (safety 1)))
620 (row-major-aref simple-bit-array
621 (%array-row-major-index simple-bit-array subscripts)))
623 ;;; KLUDGE: Not all these things (%SET-ROW-MAJOR-AREF, %SET-FILL-POINTER,
624 ;;; %SET-FDEFINITION, %SCHARSET, %SBITSET..) seem to deserve separate names.
625 ;;; Could we just DEFUN (SETF SBIT) etc. and get rid of the non-ANSI names?
627 (defun %sbitset (simple-bit-array &rest stuff)
628 (declare (type (simple-array bit) simple-bit-array) (optimize (safety 1)))
629 (let ((subscripts (butlast stuff))
630 (new-value (car (last stuff))))
631 (setf (row-major-aref simple-bit-array
632 (%array-row-major-index simple-bit-array subscripts))
635 #!-sb-fluid (declaim (inline (setf sbit)))
636 (defun (setf sbit) (new-value bit-array &rest subscripts)
637 (declare (type (simple-array bit) bit-array) (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 ;; ANSI sayeth (ADJUST-ARRAY dictionary entry):
702 ;; "If A is displaced to B, the consequences are
703 ;; unspecified if B is adjusted in such a way that it no
704 ;; longer has enough elements to satisfy A.
706 ;; In situations where this matters we should be doing a
707 ;; bounds-check, which in turn uses ARRAY-DIMENSION -- so
708 ;; this seems like a good place to signal an error.
709 (multiple-value-bind (target offset) (array-displacement array)
711 (> (array-total-size array)
712 (- (array-total-size target) offset)))
713 (error 'displaced-to-array-too-small-error
714 :format-control "~@<The displaced-to array is too small. ~S ~
715 elements after offset required, ~S available.~:@>"
716 :format-arguments (list (array-total-size array)
717 (- (array-total-size target) offset))))
718 (%array-dimension array axis-number)))))
720 (defun array-dimensions (array)
722 "Return a list whose elements are the dimensions of the array"
723 (declare (array array))
724 (if (array-header-p array)
725 (do ((results nil (cons (array-dimension array index) results))
726 (index (1- (array-rank array)) (1- index)))
727 ((minusp index) results))
728 (list (array-dimension array 0))))
730 (defun array-total-size (array)
732 "Return the total number of elements in the Array."
733 (declare (array array))
734 (if (array-header-p array)
735 (%array-available-elements array)
736 (length (the vector array))))
738 (defun array-displacement (array)
740 "Return the values of :DISPLACED-TO and :DISPLACED-INDEX-offset
741 options to MAKE-ARRAY, or NIL and 0 if not a displaced array."
742 (declare (type array array))
743 (if (and (array-header-p array) ; if unsimple and
744 (%array-displaced-p array)) ; displaced
745 (values (%array-data-vector array) (%array-displacement array))
748 (defun adjustable-array-p (array)
750 "Return T if (ADJUST-ARRAY ARRAY...) would return an array identical
751 to the argument, this happens for complex arrays."
752 (declare (array array))
753 ;; Note that this appears not to be a fundamental limitation.
754 ;; non-vector SIMPLE-ARRAYs are in fact capable of being adjusted,
755 ;; but in practice we test using ADJUSTABLE-ARRAY-P in ADJUST-ARRAY.
756 ;; -- CSR, 2004-03-01.
757 (not (typep array 'simple-array)))
759 ;;;; fill pointer frobbing stuff
761 (defun array-has-fill-pointer-p (array)
763 "Return T if the given ARRAY has a fill pointer, or NIL otherwise."
764 (declare (array array))
765 (and (array-header-p array) (%array-fill-pointer-p array)))
767 (defun fill-pointer (vector)
769 "Return the FILL-POINTER of the given VECTOR."
770 (declare (vector vector))
771 (if (and (array-header-p vector) (%array-fill-pointer-p vector))
772 (%array-fill-pointer vector)
773 (error 'simple-type-error
775 :expected-type '(and vector (satisfies array-has-fill-pointer-p))
776 :format-control "~S is not an array with a fill pointer."
777 :format-arguments (list vector))))
779 (defun %set-fill-pointer (vector new)
780 (declare (vector vector) (fixnum new))
781 (if (and (array-header-p vector) (%array-fill-pointer-p vector))
782 (if (> new (%array-available-elements vector))
784 "The new fill pointer, ~S, is larger than the length of the vector."
786 (setf (%array-fill-pointer vector) new))
787 (error 'simple-type-error
789 :expected-type '(and vector (satisfies array-has-fill-pointer-p))
790 :format-control "~S is not an array with a fill pointer."
791 :format-arguments (list vector))))
793 ;;; FIXME: It'd probably make sense to use a MACROLET to share the
794 ;;; guts of VECTOR-PUSH between VECTOR-PUSH-EXTEND. Such a macro
795 ;;; should probably be based on the VECTOR-PUSH-EXTEND code (which is
796 ;;; new ca. sbcl-0.7.0) rather than the VECTOR-PUSH code (which dates
798 (defun vector-push (new-el array)
800 "Attempt to set the element of ARRAY designated by its fill pointer
801 to NEW-EL, and increment the fill pointer by one. If the fill pointer is
802 too large, NIL is returned, otherwise the index of the pushed element is
804 (declare (vector array))
805 (let ((fill-pointer (fill-pointer array)))
806 (declare (fixnum fill-pointer))
807 (cond ((= fill-pointer (%array-available-elements array))
810 (setf (aref array fill-pointer) new-el)
811 (setf (%array-fill-pointer array) (1+ fill-pointer))
814 (defun vector-push-extend (new-element
817 (extension (1+ (length vector))))
818 (declare (vector vector) (fixnum extension))
819 (let ((fill-pointer (fill-pointer vector)))
820 (declare (fixnum fill-pointer))
821 (when (= fill-pointer (%array-available-elements vector))
822 (adjust-array vector (+ fill-pointer extension)))
823 ;; disable bounds checking
824 (locally (declare (optimize (safety 0)))
825 (setf (aref vector fill-pointer) new-element))
826 (setf (%array-fill-pointer vector) (1+ fill-pointer))
829 (defun vector-pop (array)
831 "Decrease the fill pointer by 1 and return the element pointed to by the
833 (declare (vector array))
834 (let ((fill-pointer (fill-pointer array)))
835 (declare (fixnum fill-pointer))
836 (if (zerop fill-pointer)
837 (error "There is nothing left to pop.")
838 ;; disable bounds checking (and any fixnum test)
839 (locally (declare (optimize (safety 0)))
841 (setf (%array-fill-pointer array)
842 (1- fill-pointer)))))))
847 (defun adjust-array (array dimensions &key
848 (element-type (array-element-type array))
849 (initial-element nil initial-element-p)
850 (initial-contents nil initial-contents-p)
852 displaced-to displaced-index-offset)
854 "Adjust ARRAY's dimensions to the given DIMENSIONS and stuff."
855 (let ((dimensions (if (listp dimensions) dimensions (list dimensions))))
856 (cond ((/= (the fixnum (length (the list dimensions)))
857 (the fixnum (array-rank array)))
858 (error "The number of dimensions not equal to rank of array."))
859 ((not (subtypep element-type (array-element-type array)))
860 (error "The new element type, ~S, is incompatible with old type."
862 ((and fill-pointer (not (array-has-fill-pointer-p array)))
865 :expected-type '(satisfies array-has-fill-pointer-p))))
866 (let ((array-rank (length (the list dimensions))))
867 (declare (fixnum array-rank))
868 (unless (= array-rank 1)
870 (error "Only vectors can have fill pointers.")))
871 (cond (initial-contents-p
872 ;; array former contents replaced by INITIAL-CONTENTS
873 (if (or initial-element-p displaced-to)
874 (error "INITIAL-CONTENTS may not be specified with ~
875 the :INITIAL-ELEMENT or :DISPLACED-TO option."))
876 (let* ((array-size (apply #'* dimensions))
877 (array-data (data-vector-from-inits
878 dimensions array-size element-type
879 initial-contents initial-contents-p
880 initial-element initial-element-p)))
881 (if (adjustable-array-p array)
882 (set-array-header array array-data array-size
883 (get-new-fill-pointer array array-size
886 (if (array-header-p array)
887 ;; simple multidimensional or single dimensional array
888 (make-array dimensions
889 :element-type element-type
890 :initial-contents initial-contents)
893 ;; We already established that no INITIAL-CONTENTS was supplied.
894 (when initial-element
895 (error "The :INITIAL-ELEMENT option may not be specified ~
896 with :DISPLACED-TO."))
897 (unless (subtypep element-type (array-element-type displaced-to))
898 (error "can't displace an array of type ~S into another of ~
900 element-type (array-element-type displaced-to)))
901 (let ((displacement (or displaced-index-offset 0))
902 (array-size (apply #'* dimensions)))
903 (declare (fixnum displacement array-size))
904 (if (< (the fixnum (array-total-size displaced-to))
905 (the fixnum (+ displacement array-size)))
906 (error "The :DISPLACED-TO array is too small."))
907 (if (adjustable-array-p array)
908 ;; None of the original contents appear in adjusted array.
909 (set-array-header array displaced-to array-size
910 (get-new-fill-pointer array array-size
912 displacement dimensions t)
913 ;; simple multidimensional or single dimensional array
914 (make-array dimensions
915 :element-type element-type
916 :displaced-to displaced-to
917 :displaced-index-offset
918 displaced-index-offset))))
920 (let ((old-length (array-total-size array))
921 (new-length (car dimensions))
923 (declare (fixnum old-length new-length))
924 (with-array-data ((old-data array) (old-start)
925 (old-end old-length))
926 (cond ((or (and (array-header-p array)
927 (%array-displaced-p array))
928 (< old-length new-length))
930 (data-vector-from-inits
931 dimensions new-length element-type
932 initial-contents initial-contents-p
933 initial-element initial-element-p))
934 (replace new-data old-data
935 :start2 old-start :end2 old-end))
937 (shrink-vector old-data new-length))))
938 (if (adjustable-array-p array)
939 (set-array-header array new-data new-length
940 (get-new-fill-pointer array new-length
945 (let ((old-length (%array-available-elements array))
946 (new-length (apply #'* dimensions)))
947 (declare (fixnum old-length new-length))
948 (with-array-data ((old-data array) (old-start)
949 (old-end old-length))
950 (declare (ignore old-end))
951 (let ((new-data (if (or (and (array-header-p array)
952 (%array-displaced-p array))
953 (> new-length old-length))
954 (data-vector-from-inits
955 dimensions new-length
957 initial-element initial-element-p)
959 (if (or (zerop old-length) (zerop new-length))
960 (when initial-element-p (fill new-data initial-element))
961 (zap-array-data old-data (array-dimensions array)
963 new-data dimensions new-length
964 element-type initial-element
966 (if (adjustable-array-p array)
967 (set-array-header array new-data new-length
968 new-length 0 dimensions nil)
971 sb!vm:simple-array-widetag array-rank)))
972 (set-array-header new-array new-data new-length
973 new-length 0 dimensions nil)))))))))))
976 (defun get-new-fill-pointer (old-array new-array-size fill-pointer)
977 (cond ((not fill-pointer)
978 (when (array-has-fill-pointer-p old-array)
979 (when (> (%array-fill-pointer old-array) new-array-size)
980 (error "cannot ADJUST-ARRAY an array (~S) to a size (~S) that is ~
981 smaller than its fill pointer (~S)"
982 old-array new-array-size (fill-pointer old-array)))
983 (%array-fill-pointer old-array)))
984 ((not (array-has-fill-pointer-p old-array))
985 (error "cannot supply a non-NIL value (~S) for :FILL-POINTER ~
986 in ADJUST-ARRAY unless the array (~S) was originally ~
987 created with a fill pointer"
990 ((numberp fill-pointer)
991 (when (> fill-pointer new-array-size)
992 (error "can't supply a value for :FILL-POINTER (~S) that is larger ~
993 than the new length of the vector (~S)"
994 fill-pointer new-array-size))
999 (error "bogus value for :FILL-POINTER in ADJUST-ARRAY: ~S"
1002 ;;; Destructively alter VECTOR, changing its length to NEW-LENGTH,
1003 ;;; which must be less than or equal to its current length. This can
1004 ;;; be called on vectors without a fill pointer but it is extremely
1005 ;;; dangerous to do so: shrinking the size of an object (as viewed by
1006 ;;; the gc) makes bounds checking unreliable in the face of interrupts
1007 ;;; or multi-threading. Call it only on provably local vectors.
1008 (defun %shrink-vector (vector new-length)
1009 (declare (vector vector))
1010 (unless (array-header-p vector)
1011 (macrolet ((frob (name &rest things)
1013 ((simple-array nil (*)) (error 'nil-array-accessed-error))
1014 ,@(mapcar (lambda (thing)
1015 (destructuring-bind (type-spec fill-value)
1018 (fill (truly-the ,type-spec ,name)
1020 :start new-length))))
1022 ;; Set the 'tail' of the vector to the appropriate type of zero,
1023 ;; "because in some cases we'll scavenge larger areas in one go,
1024 ;; like groups of pages that had triggered the write barrier, or
1025 ;; the whole static space" according to jsnell.
1029 `((simple-array ,(sb!vm:saetp-specifier saetp) (*))
1030 ,(if (or (eq (sb!vm:saetp-specifier saetp) 'character)
1032 (eq (sb!vm:saetp-specifier saetp) 'base-char))
1033 *default-init-char-form*
1034 (sb!vm:saetp-initial-element-default saetp))))
1036 #'sb!vm:saetp-specifier
1037 sb!vm:*specialized-array-element-type-properties*)))))
1038 ;; Only arrays have fill-pointers, but vectors have their length
1039 ;; parameter in the same place.
1040 (setf (%array-fill-pointer vector) new-length)
1043 (defun shrink-vector (vector new-length)
1044 (declare (vector vector))
1046 ((eq (length vector) new-length)
1048 ((array-has-fill-pointer-p vector)
1049 (setf (%array-fill-pointer vector) new-length)
1051 (t (subseq vector 0 new-length))))
1053 ;;; Fill in array header with the provided information, and return the array.
1054 (defun set-array-header (array data length fill-pointer displacement dimensions
1055 &optional displacedp)
1056 (setf (%array-data-vector array) data)
1057 (setf (%array-available-elements array) length)
1059 (setf (%array-fill-pointer array) fill-pointer)
1060 (setf (%array-fill-pointer-p array) t))
1062 (setf (%array-fill-pointer array) length)
1063 (setf (%array-fill-pointer-p array) nil)))
1064 (setf (%array-displacement array) displacement)
1065 (if (listp dimensions)
1066 (dotimes (axis (array-rank array))
1067 (declare (type index axis))
1068 (setf (%array-dimension array axis) (pop dimensions)))
1069 (setf (%array-dimension array 0) dimensions))
1070 (setf (%array-displaced-p array) displacedp)
1073 ;;;; ZAP-ARRAY-DATA for ADJUST-ARRAY
1075 ;;; a temporary to be used when OLD-DATA and NEW-DATA are EQ.
1076 ;;; KLUDGE: Boy, DYNAMIC-EXTENT would be nice. This is rebound
1077 ;;; to length zero array in each new thread.
1079 ;;; DX is probably a bad idea, because a with a big array it would
1080 ;;; be fairly easy to blow the stack.
1081 (defvar *zap-array-data-temp* (vector))
1082 (declaim (simple-vector *zap-array-data-temp*))
1084 (defun zap-array-data-temp (length initial-element initial-element-p)
1085 (declare (fixnum length))
1086 (let ((tmp *zap-array-data-temp*))
1087 (declare (simple-vector tmp))
1088 (cond ((> length (length tmp))
1089 (setf *zap-array-data-temp*
1090 (if initial-element-p
1091 (make-array length :initial-element initial-element)
1092 (make-array length))))
1094 (fill tmp initial-element :end length))
1098 ;;; This does the grinding work for ADJUST-ARRAY. It zaps the data
1099 ;;; from the OLD-DATA in an arrangement specified by the OLD-DIMS to
1100 ;;; the NEW-DATA in an arrangement specified by the NEW-DIMS. OFFSET
1101 ;;; is a displaced offset to be added to computed indices of OLD-DATA.
1102 (defun zap-array-data (old-data old-dims offset new-data new-dims new-length
1103 element-type initial-element initial-element-p)
1104 (declare (list old-dims new-dims))
1105 ;; OLD-DIMS comes from array-dimensions, which returns a fresh list
1106 ;; at least in SBCL.
1107 ;; NEW-DIMS comes from the user.
1108 (setf old-dims (nreverse old-dims)
1109 new-dims (reverse new-dims))
1110 (cond ((eq old-data new-data)
1111 ;; NEW-LENGTH, ELEMENT-TYPE, INITIAL-ELEMENT, and
1112 ;; INITIAL-ELEMENT-P are used when OLD-DATA and NEW-DATA are
1113 ;; EQ; in this case, a temporary must be used and filled
1114 ;; appropriately. specified initial-element.
1115 (when initial-element-p
1116 ;; FIXME: transforming this TYPEP to someting a bit faster
1117 ;; would be a win...
1118 (unless (typep initial-element element-type)
1119 (error "~S can't be used to initialize an array of type ~S."
1120 initial-element element-type)))
1122 ;; Need to disable interrupts while using the temp-vector.
1123 ;; An interrupt handler that also happened to call
1124 ;; ADJUST-ARRAY could otherwise stomp on our data here.
1125 (let ((temp (zap-array-data-temp new-length
1126 initial-element initial-element-p)))
1127 (declare (simple-vector temp))
1128 (zap-array-data-aux old-data old-dims offset temp new-dims)
1129 (dotimes (i new-length)
1130 (setf (aref new-data i) (aref temp i)
1131 ;; zero out any garbage right away
1132 (aref temp i) 0)))))
1134 ;; When OLD-DATA and NEW-DATA are not EQ, NEW-DATA has
1135 ;; already been filled with any
1136 (zap-array-data-aux old-data old-dims offset new-data new-dims))))
1138 (defun zap-array-data-aux (old-data old-dims offset new-data new-dims)
1139 (declare (fixnum offset))
1140 (let ((limits (mapcar (lambda (x y)
1141 (declare (fixnum x y))
1142 (1- (the fixnum (min x y))))
1143 old-dims new-dims)))
1144 (macrolet ((bump-index-list (index limits)
1145 `(do ((subscripts ,index (cdr subscripts))
1146 (limits ,limits (cdr limits)))
1147 ((null subscripts) :eof)
1148 (cond ((< (the fixnum (car subscripts))
1149 (the fixnum (car limits)))
1151 (1+ (the fixnum (car subscripts))))
1153 (t (rplaca subscripts 0))))))
1154 (do ((index (make-list (length old-dims) :initial-element 0)
1155 (bump-index-list index limits)))
1157 (setf (aref new-data (row-major-index-from-dims index new-dims))
1159 (+ (the fixnum (row-major-index-from-dims index old-dims))
1162 ;;; Figure out the row-major-order index of an array reference from a
1163 ;;; list of subscripts and a list of dimensions. This is for internal
1164 ;;; calls only, and the subscripts and dim-list variables are assumed
1165 ;;; to be reversed from what the user supplied.
1166 (defun row-major-index-from-dims (rev-subscripts rev-dim-list)
1167 (do ((rev-subscripts rev-subscripts (cdr rev-subscripts))
1168 (rev-dim-list rev-dim-list (cdr rev-dim-list))
1171 ((null rev-dim-list) result)
1172 (declare (fixnum chunk-size result))
1173 (setq result (+ result
1174 (the fixnum (* (the fixnum (car rev-subscripts))
1176 (setq chunk-size (* chunk-size (the fixnum (car rev-dim-list))))))
1180 (defun bit-array-same-dimensions-p (array1 array2)
1181 (declare (type (array bit) array1 array2))
1182 (and (= (array-rank array1)
1183 (array-rank array2))
1184 (dotimes (index (array-rank array1) t)
1185 (when (/= (array-dimension array1 index)
1186 (array-dimension array2 index))
1189 (defun pick-result-array (result-bit-array bit-array-1)
1190 (case result-bit-array
1192 ((nil) (make-array (array-dimensions bit-array-1)
1194 :initial-element 0))
1196 (unless (bit-array-same-dimensions-p bit-array-1
1198 (error "~S and ~S don't have the same dimensions."
1199 bit-array-1 result-bit-array))
1202 (defmacro def-bit-array-op (name function)
1203 `(defun ,name (bit-array-1 bit-array-2 &optional result-bit-array)
1206 "Perform a bit-wise ~A on the elements of BIT-ARRAY-1 and ~
1207 BIT-ARRAY-2,~% putting the results in RESULT-BIT-ARRAY. ~
1208 If RESULT-BIT-ARRAY is T,~% BIT-ARRAY-1 is used. If ~
1209 RESULT-BIT-ARRAY is NIL or omitted, a new array is~% created. ~
1210 All the arrays must have the same rank and dimensions."
1211 (symbol-name function))
1212 (declare (type (array bit) bit-array-1 bit-array-2)
1213 (type (or (array bit) (member t nil)) result-bit-array))
1214 (unless (bit-array-same-dimensions-p bit-array-1 bit-array-2)
1215 (error "~S and ~S don't have the same dimensions."
1216 bit-array-1 bit-array-2))
1217 (let ((result-bit-array (pick-result-array result-bit-array bit-array-1)))
1218 (if (and (simple-bit-vector-p bit-array-1)
1219 (simple-bit-vector-p bit-array-2)
1220 (simple-bit-vector-p result-bit-array))
1221 (locally (declare (optimize (speed 3) (safety 0)))
1222 (,name bit-array-1 bit-array-2 result-bit-array))
1223 (with-array-data ((data1 bit-array-1) (start1) (end1))
1224 (declare (ignore end1))
1225 (with-array-data ((data2 bit-array-2) (start2) (end2))
1226 (declare (ignore end2))
1227 (with-array-data ((data3 result-bit-array) (start3) (end3))
1228 (do ((index-1 start1 (1+ index-1))
1229 (index-2 start2 (1+ index-2))
1230 (index-3 start3 (1+ index-3)))
1231 ((>= index-3 end3) result-bit-array)
1232 (declare (type index index-1 index-2 index-3))
1233 (setf (sbit data3 index-3)
1234 (logand (,function (sbit data1 index-1)
1235 (sbit data2 index-2))
1238 (def-bit-array-op bit-and logand)
1239 (def-bit-array-op bit-ior logior)
1240 (def-bit-array-op bit-xor logxor)
1241 (def-bit-array-op bit-eqv logeqv)
1242 (def-bit-array-op bit-nand lognand)
1243 (def-bit-array-op bit-nor lognor)
1244 (def-bit-array-op bit-andc1 logandc1)
1245 (def-bit-array-op bit-andc2 logandc2)
1246 (def-bit-array-op bit-orc1 logorc1)
1247 (def-bit-array-op bit-orc2 logorc2)
1249 (defun bit-not (bit-array &optional result-bit-array)
1251 "Performs a bit-wise logical NOT on the elements of BIT-ARRAY,
1252 putting the results in RESULT-BIT-ARRAY. If RESULT-BIT-ARRAY is T,
1253 BIT-ARRAY is used. If RESULT-BIT-ARRAY is NIL or omitted, a new array is
1254 created. Both arrays must have the same rank and dimensions."
1255 (declare (type (array bit) bit-array)
1256 (type (or (array bit) (member t nil)) result-bit-array))
1257 (let ((result-bit-array (pick-result-array result-bit-array bit-array)))
1258 (if (and (simple-bit-vector-p bit-array)
1259 (simple-bit-vector-p result-bit-array))
1260 (locally (declare (optimize (speed 3) (safety 0)))
1261 (bit-not bit-array result-bit-array))
1262 (with-array-data ((src bit-array) (src-start) (src-end))
1263 (declare (ignore src-end))
1264 (with-array-data ((dst result-bit-array) (dst-start) (dst-end))
1265 (do ((src-index src-start (1+ src-index))
1266 (dst-index dst-start (1+ dst-index)))
1267 ((>= dst-index dst-end) result-bit-array)
1268 (declare (type index src-index dst-index))
1269 (setf (sbit dst dst-index)
1270 (logxor (sbit src src-index) 1))))))))