Speed up vector extension in VECTOR-PUSH-EXTEND.
[sbcl.git] / src / code / array.lisp
blobaafe4a89b67da84ecce61f277cdde7babd4a8d23
1 ;;;; functions to implement arrays
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
12 (in-package "SB!IMPL")
14 #!-sb-fluid
15 (declaim (inline adjustable-array-p
16 array-displacement))
18 ;;;; miscellaneous accessor functions
20 ;;; These functions are only needed by the interpreter, 'cause the
21 ;;; compiler inlines them.
22 (macrolet ((def (name)
23 `(progn
24 (defun ,name (array)
25 (,name array))
26 (defun (setf ,name) (value array)
27 (setf (,name array) value)))))
28 (def %array-fill-pointer)
29 (def %array-fill-pointer-p)
30 (def %array-available-elements)
31 (def %array-data-vector)
32 (def %array-displacement)
33 (def %array-displaced-p)
34 (def %array-displaced-from))
36 (defun %array-rank (array)
37 (%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)
47 (fixnum index))
48 (%check-bound array bound index))
50 (defun check-bound (array bound index)
51 (declare (type index bound)
52 (fixnum index))
53 (%check-bound array bound index)
54 index)
56 (defun %with-array-data/fp (array start end)
57 (%with-array-data-macro array start end :check-bounds t :check-fill-pointer t))
59 (defun %with-array-data (array start end)
60 (%with-array-data-macro array start end :check-bounds t :check-fill-pointer nil))
62 (defun %data-vector-and-index (array index)
63 (if (array-header-p array)
64 (multiple-value-bind (vector index)
65 (%with-array-data array index nil)
66 (values vector index))
67 (values array index)))
69 ;;;; MAKE-ARRAY
70 (defun %integer-vector-widetag-and-n-bits (signed high)
71 (let ((unsigned-table
72 #.(let ((map (make-array (1+ sb!vm:n-word-bits))))
73 (loop for saetp across
74 (reverse sb!vm:*specialized-array-element-type-properties*)
75 for ctype = (sb!vm:saetp-ctype saetp)
76 when (and (numeric-type-p ctype)
77 (eq (numeric-type-class ctype) 'integer)
78 (zerop (numeric-type-low ctype)))
79 do (fill map (cons (sb!vm:saetp-typecode saetp)
80 (sb!vm:saetp-n-bits saetp))
81 :end (1+ (integer-length (numeric-type-high ctype)))))
82 map))
83 (signed-table
84 #.(let ((map (make-array (1+ sb!vm:n-word-bits))))
85 (loop for saetp across
86 (reverse sb!vm:*specialized-array-element-type-properties*)
87 for ctype = (sb!vm:saetp-ctype saetp)
88 when (and (numeric-type-p ctype)
89 (eq (numeric-type-class ctype) 'integer)
90 (minusp (numeric-type-low ctype)))
91 do (fill map (cons (sb!vm:saetp-typecode saetp)
92 (sb!vm:saetp-n-bits saetp))
93 :end (+ (integer-length (numeric-type-high ctype)) 2)))
94 map)))
95 (cond ((> high sb!vm:n-word-bits)
96 (values #.sb!vm:simple-vector-widetag #.sb!vm:n-word-bits))
97 (signed
98 (let ((x (aref signed-table high)))
99 (values (car x) (cdr x))))
101 (let ((x (aref unsigned-table high)))
102 (values (car x) (cdr x)))))))
104 ;;; This is a bit complicated, but calling subtypep over all
105 ;;; specialized types is exceedingly slow
106 (defun %vector-widetag-and-n-bits (type)
107 (macrolet ((with-parameters ((arg-type &key intervals)
108 (&rest args) &body body)
109 (let ((type-sym (gensym)))
110 `(let (,@(loop for arg in args
111 collect `(,arg '*)))
112 (declare (ignorable ,@args))
113 (when (consp type)
114 (let ((,type-sym (cdr type)))
115 (block nil
116 ,@(loop for arg in args
117 collect
118 `(cond ((consp ,type-sym)
119 (let ((value (pop ,type-sym)))
120 (if (or (eq value '*)
121 (typep value ',arg-type)
122 ,(if intervals
123 `(and (consp value)
124 (null (cdr value))
125 (typep (car value)
126 ',arg-type))))
127 (setf ,arg value)
128 (ill-type))))
129 ((null ,type-sym)
130 (return))
132 (ill-type)))))
133 (when ,type-sym
134 (ill-type))))
135 ,@body)))
136 (result (widetag)
137 (let ((value (symbol-value widetag)))
138 `(values ,value
139 ,(sb!vm:saetp-n-bits
140 (find value
141 sb!vm:*specialized-array-element-type-properties*
142 :key #'sb!vm:saetp-typecode))))))
143 (flet ((ill-type ()
144 (error "Invalid type specifier: ~s" type))
145 (integer-interval-widetag (low high)
146 (if (minusp low)
147 (%integer-vector-widetag-and-n-bits
149 (1+ (max (integer-length low) (integer-length high))))
150 (%integer-vector-widetag-and-n-bits
152 (max (integer-length low) (integer-length high))))))
153 (let* ((consp (consp type))
154 (type-name (if consp
155 (car type)
156 type)))
157 (case type-name
158 ((t)
159 (when consp
160 (ill-type))
161 (result sb!vm:simple-vector-widetag))
162 ((base-char standard-char #!-sb-unicode character)
163 (when consp
164 (ill-type))
165 (result sb!vm:simple-base-string-widetag))
166 #!+sb-unicode
167 ((character extended-char)
168 (when consp
169 (ill-type))
170 (result sb!vm:simple-character-string-widetag))
171 (bit
172 (when consp
173 (ill-type))
174 (result sb!vm:simple-bit-vector-widetag))
175 (fixnum
176 (when consp
177 (ill-type))
178 (result sb!vm:simple-array-fixnum-widetag))
179 (unsigned-byte
180 (with-parameters ((integer 1)) (high)
181 (if (eq high '*)
182 (result sb!vm:simple-vector-widetag)
183 (%integer-vector-widetag-and-n-bits nil high))))
184 (signed-byte
185 (with-parameters ((integer 1)) (high)
186 (if (eq high '*)
187 (result sb!vm:simple-vector-widetag)
188 (%integer-vector-widetag-and-n-bits t high))))
189 (double-float
190 (with-parameters (double-float :intervals t) (low high)
191 (if (and (not (eq low '*))
192 (not (eq high '*))
193 (if (or (consp low) (consp high))
194 (>= (type-bound-number low) (type-bound-number high))
195 (> low high)))
196 (result sb!vm:simple-array-nil-widetag)
197 (result sb!vm:simple-array-double-float-widetag))))
198 (single-float
199 (with-parameters (single-float :intervals t) (low high)
200 (if (and (not (eq low '*))
201 (not (eq high '*))
202 (if (or (consp low) (consp high))
203 (>= (type-bound-number low) (type-bound-number high))
204 (> low high)))
205 (result sb!vm:simple-array-nil-widetag)
206 (result sb!vm:simple-array-single-float-widetag))))
207 (mod
208 (if (and (consp type)
209 (consp (cdr type))
210 (null (cddr type))
211 (typep (cadr type) '(integer 1)))
212 (%integer-vector-widetag-and-n-bits
213 nil (integer-length (1- (cadr type))))
214 (ill-type)))
215 #!+long-float
216 (long-float
217 (with-parameters (long-float :intervals t) (low high)
218 (if (and (not (eq low '*))
219 (not (eq high '*))
220 (if (or (consp low) (consp high))
221 (>= (type-bound-number low) (type-bound-number high))
222 (> low high)))
223 (result sb!vm:simple-array-nil-widetag)
224 (result sb!vm:simple-array-long-float-widetag))))
225 (integer
226 (with-parameters (integer :intervals t) (low high)
227 (let ((low (if (consp low)
228 (1+ (car low))
229 low))
230 (high (if (consp high)
231 (1- (car high))
232 high)))
233 (cond ((or (eq high '*)
234 (eq low '*))
235 (result sb!vm:simple-vector-widetag))
236 ((> low high)
237 (result sb!vm:simple-array-nil-widetag))
239 (integer-interval-widetag low high))))))
240 (complex
241 (with-parameters (t) (subtype)
242 (if (eq subtype '*)
243 (result sb!vm:simple-vector-widetag)
244 (let ((ctype (specifier-type type)))
245 (cond ((eq ctype *empty-type*)
246 (result sb!vm:simple-array-nil-widetag))
247 ((union-type-p ctype)
248 (cond ((csubtypep ctype (specifier-type '(complex double-float)))
249 (result
250 sb!vm:simple-array-complex-double-float-widetag))
251 ((csubtypep ctype (specifier-type '(complex single-float)))
252 (result
253 sb!vm:simple-array-complex-single-float-widetag))
254 #!+long-float
255 ((csubtypep ctype (specifier-type '(complex long-float)))
256 (result
257 sb!vm:simple-array-complex-long-float-widetag))
259 (result sb!vm:simple-vector-widetag))))
261 (case (numeric-type-format ctype)
262 (double-float
263 (result
264 sb!vm:simple-array-complex-double-float-widetag))
265 (single-float
266 (result
267 sb!vm:simple-array-complex-single-float-widetag))
268 #!+long-float
269 (long-float
270 (result
271 sb!vm:simple-array-complex-long-float-widetag))
273 (result sb!vm:simple-vector-widetag)))))))))
274 ((nil)
275 (result sb!vm:simple-array-nil-widetag))
277 (block nil
278 (let ((ctype (type-or-nil-if-unknown type)))
279 (unless ctype
280 (return (result sb!vm:simple-vector-widetag)))
281 (typecase ctype
282 (union-type
283 (let ((types (union-type-types ctype)))
284 (cond ((not (every #'numeric-type-p types))
285 (result sb!vm:simple-vector-widetag))
286 ((csubtypep ctype (specifier-type 'integer))
287 (integer-interval-widetag
288 (reduce #'min types :key #'numeric-type-low)
289 (reduce #'max types :key #'numeric-type-high)))
290 ((csubtypep ctype (specifier-type 'double-float))
291 (result sb!vm:simple-array-double-float-widetag))
292 ((csubtypep ctype (specifier-type 'single-float))
293 (result sb!vm:simple-array-single-float-widetag))
294 #!+long-float
295 ((csubtypep ctype (specifier-type 'long-float))
296 (result sb!vm:simple-array-long-float-widetag))
298 (result sb!vm:simple-vector-widetag)))))
299 (character-set-type
300 #!-sb-unicode (result sb!vm:simple-base-string-widetag)
301 #!+sb-unicode
302 (if (loop for (start . end)
303 in (character-set-type-pairs ctype)
304 always (and (< start base-char-code-limit)
305 (< end base-char-code-limit)))
306 (result sb!vm:simple-base-string-widetag)
307 (result sb!vm:simple-character-string-widetag)))
309 (let ((expansion (type-specifier ctype)))
310 (if (equal expansion type)
311 (result sb!vm:simple-vector-widetag)
312 (%vector-widetag-and-n-bits expansion)))))))))))))
314 (defun %complex-vector-widetag (widetag)
315 (macrolet ((make-case ()
316 `(case widetag
317 ,@(loop for saetp across sb!vm:*specialized-array-element-type-properties*
318 for complex = (sb!vm:saetp-complex-typecode saetp)
319 when complex
320 collect (list (sb!vm:saetp-typecode saetp) complex))
322 #.sb!vm:complex-vector-widetag))))
323 (make-case)))
325 (defglobal %%simple-array-n-bits%% (make-array (1+ sb!vm:widetag-mask)))
326 #.(loop for info across sb!vm:*specialized-array-element-type-properties*
327 collect `(setf (aref %%simple-array-n-bits%% ,(sb!vm:saetp-typecode info))
328 ,(sb!vm:saetp-n-bits info)) into forms
329 finally (return `(progn ,@forms)))
331 (declaim (type (simple-vector #.(1+ sb!vm:widetag-mask)) %%simple-array-n-bits%%))
333 (defun allocate-vector-with-widetag (widetag length &optional n-bits)
334 (declare (type (unsigned-byte 8) widetag)
335 (type index length))
336 (let ((n-bits (or n-bits (aref %%simple-array-n-bits%% widetag))))
337 (declare (type (integer 0 256) n-bits))
338 (allocate-vector widetag length
339 (ceiling
340 (* (if (or (= widetag sb!vm:simple-base-string-widetag)
341 #!+sb-unicode
342 (= widetag
343 sb!vm:simple-character-string-widetag))
344 (1+ length)
345 length)
346 n-bits)
347 sb!vm:n-word-bits))))
349 (defun array-underlying-widetag (array)
350 (macrolet ((make-case ()
351 `(case widetag
352 ,@(loop for saetp across sb!vm:*specialized-array-element-type-properties*
353 for complex = (sb!vm:saetp-complex-typecode saetp)
354 when complex
355 collect (list complex (sb!vm:saetp-typecode saetp)))
356 ((,sb!vm:simple-array-widetag
357 ,sb!vm:complex-vector-widetag
358 ,sb!vm:complex-array-widetag)
359 (with-array-data ((array array) (start) (end))
360 (declare (ignore start end))
361 (%other-pointer-widetag array)))
363 widetag))))
364 (let ((widetag (%other-pointer-widetag array)))
365 (make-case))))
367 (defun make-vector-like (vector length)
368 (allocate-vector-with-widetag (array-underlying-widetag vector) length))
370 ;; Complain in various ways about wrong :INITIAL-foo arguments,
371 ;; returning the two initialization arguments needed for DATA-VECTOR-FROM-INITS.
372 (defun validate-array-initargs (element-p element contents-p contents displaced)
373 (cond ((and displaced (or element-p contents-p))
374 (if (and element-p contents-p)
375 (error "Neither :INITIAL-ELEMENT nor :INITIAL-CONTENTS ~
376 may be specified with the :DISPLACED-TO option")
377 (error "~S may not be specified with the :DISPLACED-TO option"
378 (if element-p :initial-element :initial-contents))))
379 ((and element-p contents-p)
380 (error "Can't specify both :INITIAL-ELEMENT and :INITIAL-CONTENTS"))
381 (element-p (values :initial-element element))
382 (contents-p (values :initial-contents contents))
383 (t (values nil nil))))
385 (declaim (inline %save-displaced-array-backpointer))
386 (defun %save-displaced-array-backpointer (array data)
387 (flet ((purge (pointers)
388 (remove-if (lambda (value)
389 (or (not value) (eq array value)))
390 pointers
391 :key #'weak-pointer-value)))
392 ;; Add backpointer to the new data vector if it has a header.
393 (when (array-header-p data)
394 (setf (%array-displaced-from data)
395 (cons (make-weak-pointer array)
396 (purge (%array-displaced-from data)))))
397 ;; Remove old backpointer, if any.
398 (let ((old-data (%array-data-vector array)))
399 (when (and (neq data old-data) (array-header-p old-data))
400 (setf (%array-displaced-from old-data)
401 (purge (%array-displaced-from old-data)))))))
403 ;;; Widetag is the widetag of the underlying vector,
404 ;;; it'll be the same as the resulting array widetag only for simple vectors
405 (defun %make-array (dimensions widetag n-bits
406 &key
407 element-type
408 (initial-element nil initial-element-p)
409 (initial-contents nil initial-contents-p)
410 adjustable fill-pointer
411 displaced-to displaced-index-offset)
412 (declare (ignore element-type))
413 (binding* (((array-rank dimension-0)
414 (if (listp dimensions)
415 (values (length dimensions)
416 (if dimensions (car dimensions) 1))
417 (values 1 dimensions)))
418 ((initialize initial-data)
419 (validate-array-initargs initial-element-p initial-element
420 initial-contents-p initial-contents
421 displaced-to))
422 (simple (and (null fill-pointer)
423 (not adjustable)
424 (null displaced-to))))
425 (declare (type array-rank array-rank))
426 (declare (type index dimension-0))
427 (cond ((and displaced-index-offset (null displaced-to))
428 (error "can't specify :DISPLACED-INDEX-OFFSET without :DISPLACED-TO"))
429 ((and simple (= array-rank 1))
430 (let ((vector ; a (SIMPLE-ARRAY * (*))
431 (allocate-vector-with-widetag widetag dimension-0 n-bits)))
432 ;; presence of at most one :INITIAL-thing keyword was ensured above
433 (cond (initial-element-p
434 (fill vector initial-element))
435 (initial-contents-p
436 (let ((content-length (length initial-contents)))
437 (unless (= dimension-0 content-length)
438 (error "There are ~W elements in the :INITIAL-CONTENTS, but ~
439 the vector length is ~W."
440 content-length dimension-0)))
441 (replace vector initial-contents)))
442 vector))
443 ((and (arrayp displaced-to)
444 (/= (array-underlying-widetag displaced-to) widetag))
445 (error "Array element type of :DISPLACED-TO array does not match specified element type"))
447 ;; it's non-simple or multidimensional, or both.
448 (when fill-pointer
449 (unless (= array-rank 1)
450 (error "Only vectors can have fill pointers."))
451 (when (and (integerp fill-pointer) (> fill-pointer dimension-0))
452 ;; FIXME: should be TYPE-ERROR?
453 (error "invalid fill-pointer ~W" fill-pointer)))
454 (let* ((total-size
455 (if (consp dimensions)
456 (the index (reduce (lambda (a b) (* a (the index b)))
457 dimensions))
458 ;; () is considered to have dimension-0 = 1.
459 ;; It avoids the REDUCE lambda being called with no args.
460 dimension-0))
461 (data (or displaced-to
462 (data-vector-from-inits
463 dimensions total-size nil widetag n-bits
464 initialize initial-data)))
465 (array (make-array-header
466 (cond ((= array-rank 1)
467 (%complex-vector-widetag widetag))
468 (simple sb!vm:simple-array-widetag)
469 (t sb!vm:complex-array-widetag))
470 array-rank)))
471 (if fill-pointer
472 (setf (%array-fill-pointer-p array) t
473 (%array-fill-pointer array)
474 (if (eq fill-pointer t) dimension-0 fill-pointer))
475 (setf (%array-fill-pointer-p array) nil
476 (%array-fill-pointer array) total-size))
477 (setf (%array-available-elements array) total-size)
478 ;; Terrible name for this slot - we displace to the
479 ;; target array's header, if any, not the "ultimate"
480 ;; vector in the chain of displacements.
481 (setf (%array-data-vector array) data)
482 (setf (%array-displaced-from array) nil)
483 (cond (displaced-to
484 (let ((offset (or displaced-index-offset 0)))
485 (when (> (+ offset total-size)
486 (array-total-size displaced-to))
487 (error "~S doesn't have enough elements." displaced-to))
488 (setf (%array-displacement array) offset)
489 (setf (%array-displaced-p array) t)
490 (%save-displaced-array-backpointer array data)))
492 (setf (%array-displaced-p array) nil)))
493 (if (listp dimensions)
494 (let ((dims dimensions)) ; avoid "prevents use of assertion"
495 (dotimes (axis array-rank)
496 (setf (%array-dimension array axis) (pop dims))))
497 (setf (%array-dimension array 0) dimension-0))
498 array)))))
500 (defun make-array (dimensions &rest args
501 &key (element-type t)
502 initial-element initial-contents
503 adjustable
504 fill-pointer
505 displaced-to
506 displaced-index-offset)
507 (declare (ignore initial-element
508 initial-contents adjustable
509 fill-pointer displaced-to displaced-index-offset))
510 (declare (explicit-check))
511 (multiple-value-bind (widetag n-bits) (%vector-widetag-and-n-bits element-type)
512 (apply #'%make-array dimensions widetag n-bits args)))
514 (defun make-static-vector (length &key
515 (element-type '(unsigned-byte 8))
516 (initial-contents nil initial-contents-p)
517 (initial-element nil initial-element-p))
518 #!+sb-doc
519 "Allocate vector of LENGTH elements in static space. Only allocation
520 of specialized arrays is supported."
521 ;; STEP 1: check inputs fully
523 ;; This way of doing explicit checks before the vector is allocated
524 ;; is expensive, but probably worth the trouble as once we've allocated
525 ;; the vector we have no way to get rid of it anymore...
526 (when (eq t (upgraded-array-element-type element-type))
527 (error "Static arrays of type ~S not supported."
528 element-type))
529 (validate-array-initargs initial-element-p initial-element
530 initial-contents-p initial-contents nil) ; for effect
531 (when initial-contents-p
532 (unless (= length (length initial-contents))
533 (error "There are ~W elements in the :INITIAL-CONTENTS, but the ~
534 vector length is ~W."
535 (length initial-contents)
536 length))
537 (unless (every (lambda (x) (typep x element-type)) initial-contents)
538 (error ":INITIAL-CONTENTS contains elements not of type ~S."
539 element-type)))
540 (when initial-element-p
541 (unless (typep initial-element element-type)
542 (error ":INITIAL-ELEMENT ~S is not of type ~S."
543 initial-element element-type)))
544 ;; STEP 2
546 ;; Allocate and possibly initialize the vector.
547 (multiple-value-bind (type n-bits)
548 (%vector-widetag-and-n-bits element-type)
549 (let ((vector
550 (allocate-static-vector type length
551 (ceiling (* length n-bits)
552 sb!vm:n-word-bits))))
553 (cond (initial-element-p
554 (fill vector initial-element))
555 (initial-contents-p
556 (replace vector initial-contents))
558 vector)))))
560 ;;; DATA-VECTOR-FROM-INITS returns a simple vector that has the
561 ;;; specified array characteristics. Dimensions is only used to pass
562 ;;; to FILL-DATA-VECTOR for error checking on the structure of
563 ;;; initial-contents.
564 (defun data-vector-from-inits (dimensions total-size
565 element-type widetag n-bits
566 initialize initial-data)
567 ;; FIXME: element-type can be NIL when widetag is non-nil,
568 ;; and FILL will check the type, although the error will be not as nice.
569 ;; (cond (typep initial-element element-type)
570 ;; (error "~S cannot be used to initialize an array of type ~S."
571 ;; initial-element element-type))
572 (let ((data (if widetag
573 (allocate-vector-with-widetag widetag total-size n-bits)
574 (make-array total-size :element-type element-type))))
575 (ecase initialize
576 (:initial-element
577 (fill (the vector data) initial-data))
578 (:initial-contents
579 ;; DIMENSIONS can be supplied as a list or integer now
580 (dx-let ((list-of-dims (list dimensions))) ; ok if already a list
581 (fill-data-vector data
582 (if (listp dimensions) dimensions list-of-dims)
583 initial-data)))
584 ((nil)))
585 data))
587 (defun vector (&rest objects)
588 #!+sb-doc
589 "Construct a SIMPLE-VECTOR from the given objects."
590 (let ((v (make-array (length objects))))
591 (do-rest-arg ((x i) objects 0 v)
592 (setf (aref v i) x))))
595 ;;;; accessor/setter functions
597 ;;; Dispatch to an optimized routine the data vector accessors for
598 ;;; each different specialized vector type. Do dispatching by looking
599 ;;; up the widetag in the array rather than with the typecases, which
600 ;;; as of 1.0.5 compiles to a naive sequence of linear TYPEPs. Also
601 ;;; provide separate versions where bounds checking has been moved
602 ;;; from the callee to the caller, since it's much cheaper to do once
603 ;;; the type information is available. Finally, for each of these
604 ;;; routines also provide a slow path, taken for arrays that are not
605 ;;; vectors or not simple.
606 (macrolet ((def (name table-name)
607 `(progn
608 (defglobal ,table-name (make-array ,(1+ sb!vm:widetag-mask)))
609 (declaim (type (simple-array function (,(1+ sb!vm:widetag-mask)))
610 ,table-name))
611 (defmacro ,name (array-var)
612 `(the function
613 (let ((tag 0))
614 (when (sb!vm::%other-pointer-p ,array-var)
615 (setf tag (%other-pointer-widetag ,array-var)))
616 (svref ,',table-name tag)))))))
617 (def !find-data-vector-setter %%data-vector-setters%%)
618 (def !find-data-vector-setter/check-bounds %%data-vector-setters/check-bounds%%)
619 ;; Used by DO-VECTOR-DATA -- which in turn appears in DOSEQUENCE expansion,
620 ;; meaning we can have post-build dependences on this.
621 (def %find-data-vector-reffer %%data-vector-reffers%%)
622 (def !find-data-vector-reffer/check-bounds %%data-vector-reffers/check-bounds%%))
624 ;;; Like DOVECTOR, but more magical -- can't use this on host.
625 (defmacro do-vector-data ((elt vector &optional result) &body body)
626 (multiple-value-bind (forms decls) (parse-body body nil)
627 (with-unique-names (index vec start end ref)
628 `(with-array-data ((,vec ,vector)
629 (,start)
630 (,end)
631 :check-fill-pointer t)
632 (let ((,ref (%find-data-vector-reffer ,vec)))
633 (declare (function ,ref))
634 (do ((,index ,start (1+ ,index)))
635 ((>= ,index ,end)
636 (let ((,elt nil))
637 ,@(filter-dolist-declarations decls)
638 ,elt
639 ,result))
640 (let ((,elt (funcall ,ref ,vec ,index)))
641 ,@decls
642 (tagbody ,@forms))))))))
644 (macrolet ((%ref (accessor-getter extra-params)
645 `(funcall (,accessor-getter array) array index ,@extra-params))
646 (define (accessor-name slow-accessor-name accessor-getter
647 extra-params check-bounds)
648 `(progn
649 (defun ,accessor-name (array index ,@extra-params)
650 (declare (explicit-check))
651 (declare (optimize speed
652 ;; (SAFETY 0) is ok. All calls to
653 ;; these functions are generated by
654 ;; the compiler, so argument count
655 ;; checking isn't needed. Type checking
656 ;; is done implicitly via the widetag
657 ;; dispatch.
658 (safety 0)))
659 (%ref ,accessor-getter ,extra-params))
660 (defun ,slow-accessor-name (array index ,@extra-params)
661 (declare (optimize speed (safety 0)))
662 (if (not (%array-displaced-p array))
663 ;; The reasonably quick path of non-displaced complex
664 ;; arrays.
665 (let ((array (%array-data-vector array)))
666 (%ref ,accessor-getter ,extra-params))
667 ;; The real slow path.
668 (with-array-data
669 ((vector array)
670 (index (locally
671 (declare (optimize (speed 1) (safety 1)))
672 (,@check-bounds index)))
673 (end)
674 :force-inline t)
675 (declare (ignore end))
676 (,accessor-name vector index ,@extra-params)))))))
677 (define hairy-data-vector-ref slow-hairy-data-vector-ref
678 %find-data-vector-reffer
679 nil (progn))
680 (define hairy-data-vector-set slow-hairy-data-vector-set
681 !find-data-vector-setter
682 (new-value) (progn))
683 (define hairy-data-vector-ref/check-bounds
684 slow-hairy-data-vector-ref/check-bounds
685 !find-data-vector-reffer/check-bounds
686 nil (check-bound array (array-dimension array 0)))
687 (define hairy-data-vector-set/check-bounds
688 slow-hairy-data-vector-set/check-bounds
689 !find-data-vector-setter/check-bounds
690 (new-value) (check-bound array (array-dimension array 0))))
692 (defun hairy-ref-error (array index &optional new-value)
693 (declare (ignore index new-value))
694 (error 'type-error
695 :datum array
696 :expected-type 'vector))
698 (macrolet ((define-reffer (saetp check-form)
699 (let* ((type (sb!vm:saetp-specifier saetp))
700 (atype `(simple-array ,type (*))))
701 `(named-lambda (optimized-data-vector-ref ,type) (vector index)
702 (declare (optimize speed (safety 0))
703 ;; Obviously these all coerce raw words to lispobjs
704 ;; so don't keep spewing notes about it.
705 (muffle-conditions compiler-note)
706 (ignorable index))
707 ,(if type
708 `(data-vector-ref (the ,atype vector)
709 (locally
710 (declare (optimize (safety 1)))
711 (the index
712 (,@check-form index))))
713 `(data-nil-vector-ref (the ,atype vector) index)))))
714 (define-setter (saetp check-form)
715 (let* ((type (sb!vm:saetp-specifier saetp))
716 (atype `(simple-array ,type (*))))
717 `(named-lambda (optimized-data-vector-set ,type) (vector index new-value)
718 (declare (optimize speed (safety 0)))
719 ;; Impossibly setting an elt of an (ARRAY NIL)
720 ;; returns no value. And nobody cares.
721 (declare (muffle-conditions compiler-note))
722 (data-vector-set (the ,atype vector)
723 (locally
724 (declare (optimize (safety 1)))
725 (the index
726 (,@check-form index)))
727 (locally
728 ;; SPEED 1 needed to avoid the compiler
729 ;; from downgrading the type check to
730 ;; a cheaper one.
731 (declare (optimize (speed 1)
732 (safety 1)))
733 (the ,type new-value)))
734 ;; For specialized arrays, the return from
735 ;; data-vector-set would have to be reboxed to be a
736 ;; (Lisp) return value; instead, we use the
737 ;; already-boxed value as the return.
738 new-value)))
739 (define-reffers (symbol deffer check-form slow-path)
740 `(progn
741 ;; FIXME/KLUDGE: can't just FILL here, because genesis doesn't
742 ;; preserve the binding, so re-initiaize as NS doesn't have
743 ;; the energy to figure out to change that right now.
744 (setf ,symbol (make-array (1+ sb!vm::widetag-mask)
745 :initial-element #'hairy-ref-error))
746 ,@(loop for widetag in '(sb!vm:complex-vector-widetag
747 sb!vm:complex-vector-nil-widetag
748 sb!vm:complex-bit-vector-widetag
749 #!+sb-unicode sb!vm:complex-character-string-widetag
750 sb!vm:complex-base-string-widetag
751 sb!vm:simple-array-widetag
752 sb!vm:complex-array-widetag)
753 collect `(setf (svref ,symbol ,widetag) ,slow-path))
754 ,@(loop for saetp across sb!vm:*specialized-array-element-type-properties*
755 for widetag = (sb!vm:saetp-typecode saetp)
756 collect `(setf (svref ,symbol ,widetag)
757 (,deffer ,saetp ,check-form))))))
758 (defun !hairy-data-vector-reffer-init ()
759 (define-reffers %%data-vector-reffers%% define-reffer
760 (progn)
761 #'slow-hairy-data-vector-ref)
762 (define-reffers %%data-vector-setters%% define-setter
763 (progn)
764 #'slow-hairy-data-vector-set)
765 (define-reffers %%data-vector-reffers/check-bounds%% define-reffer
766 (check-bound vector (length vector))
767 #'slow-hairy-data-vector-ref/check-bounds)
768 (define-reffers %%data-vector-setters/check-bounds%% define-setter
769 (check-bound vector (length vector))
770 #'slow-hairy-data-vector-set/check-bounds)))
772 ;;; (Ordinary DATA-VECTOR-REF usage compiles into a vop, but
773 ;;; DATA-VECTOR-REF is also FOLDABLE, and this ordinary function
774 ;;; definition is needed for the compiler to use in constant folding.)
775 (defun data-vector-ref (array index)
776 (declare (explicit-check))
777 (hairy-data-vector-ref array index))
779 (defun data-vector-ref-with-offset (array index offset)
780 (declare (explicit-check))
781 (hairy-data-vector-ref array (+ index offset)))
783 (defun invalid-array-p (array)
784 (and (array-header-p array)
785 (consp (%array-displaced-p array))))
787 (declaim (ftype (function (array) nil) invalid-array-error))
788 (defun invalid-array-error (array)
789 (aver (array-header-p array))
790 ;; Array invalidation stashes the original dimensions here...
791 (let ((dims (%array-displaced-p array))
792 (et (array-element-type array)))
793 (error 'invalid-array-error
794 :datum array
795 :expected-type
796 (if (cdr dims)
797 `(array ,et ,dims)
798 `(vector ,et ,@dims)))))
800 (declaim (ftype (function (array t integer &optional t) nil)
801 invalid-array-index-error))
802 (defun invalid-array-index-error (array index bound &optional axis)
803 (if (invalid-array-p array)
804 (invalid-array-error array)
805 (error 'invalid-array-index-error
806 :array array
807 :axis axis
808 :datum index
809 :expected-type `(integer 0 (,bound)))))
811 ;;; SUBSCRIPTS has a dynamic-extent list structure and is destroyed
812 (defun %array-row-major-index (array &rest subscripts)
813 (declare (truly-dynamic-extent subscripts)
814 (array array))
815 (let ((length (length subscripts)))
816 (cond ((array-header-p array)
817 (let ((rank (%array-rank array)))
818 (unless (= rank length)
819 (error "wrong number of subscripts, ~W, for array of rank ~W."
820 length rank))
821 (do ((axis (1- rank) (1- axis))
822 (chunk-size 1)
823 (result 0))
824 ((minusp axis) result)
825 (declare (fixnum axis chunk-size result))
826 (let ((index (fast-&rest-nth axis subscripts))
827 (dim (%array-dimension array axis)))
828 (unless (and (fixnump index) (< -1 index dim))
829 (invalid-array-index-error array index dim axis))
830 (setf result
831 (truly-the fixnum
832 (+ result
833 (truly-the fixnum (* chunk-size index))))
834 chunk-size (truly-the fixnum (* chunk-size dim)))))))
835 ((/= length 1)
836 (error "Wrong number of subscripts, ~W, for array of rank 1."
837 length))
839 (let ((index (fast-&rest-nth 0 subscripts))
840 (length (length (the (simple-array * (*)) array))))
841 (unless (and (fixnump index) (< -1 index length))
842 (invalid-array-index-error array index length))
843 index)))))
845 (defun array-in-bounds-p (array &rest subscripts)
846 #!+sb-doc
847 "Return T if the SUBSCRIPTS are in bounds for the ARRAY, NIL otherwise."
848 (declare (truly-dynamic-extent subscripts))
849 (let ((length (length subscripts)))
850 (cond ((array-header-p array)
851 (let ((rank (%array-rank array)))
852 (unless (= rank length)
853 (error "Wrong number of subscripts, ~W, for array of rank ~W."
854 length rank))
855 (loop for i below length
856 for s = (fast-&rest-nth i subscripts)
857 always (and (typep s '(and fixnum unsigned-byte))
858 (< s (%array-dimension array i))))))
859 ((/= length 1)
860 (error "Wrong number of subscripts, ~W, for array of rank 1."
861 length))
863 (let ((subscript (fast-&rest-nth 0 subscripts)))
864 (and (typep subscript '(and fixnum unsigned-byte))
865 (< subscript
866 (length (truly-the (simple-array * (*)) array)))))))))
868 (defun array-row-major-index (array &rest subscripts)
869 (declare (truly-dynamic-extent subscripts))
870 (apply #'%array-row-major-index array subscripts))
872 (defun aref (array &rest subscripts)
873 #!+sb-doc
874 "Return the element of the ARRAY specified by the SUBSCRIPTS."
875 (declare (truly-dynamic-extent subscripts))
876 (row-major-aref array (apply #'%array-row-major-index array subscripts)))
878 ;;; (setf aref/bit/sbit) are implemented using setf-functions,
879 ;;; because they have to work with (setf (apply #'aref array subscripts))
880 ;;; All other setfs can be done using setf-functions too, but I
881 ;;; haven't found technical advantages or disadvantages for either
882 ;;; scheme.
883 (defun (setf aref) (new-value array &rest subscripts)
884 (declare (truly-dynamic-extent subscripts)
885 (type array array))
886 (setf (row-major-aref array (apply #'%array-row-major-index array subscripts))
887 new-value))
889 (defun row-major-aref (array index)
890 #!+sb-doc
891 "Return the element of array corresponding to the row-major index. This is
892 SETFable."
893 (declare (optimize (safety 1)))
894 (row-major-aref array index))
896 (defun %set-row-major-aref (array index new-value)
897 (declare (optimize (safety 1)))
898 (setf (row-major-aref array index) new-value))
900 (defun svref (simple-vector index)
901 #!+sb-doc
902 "Return the INDEXth element of the given Simple-Vector."
903 (declare (optimize (safety 1)))
904 (aref simple-vector index))
906 (defun %svset (simple-vector index new)
907 (declare (optimize (safety 1)))
908 (setf (aref simple-vector index) new))
910 (defun bit (bit-array &rest subscripts)
911 #!+sb-doc
912 "Return the bit from the BIT-ARRAY at the specified SUBSCRIPTS."
913 (declare (type (array bit) bit-array)
914 (truly-dynamic-extent subscripts)
915 (optimize (safety 1)))
916 (row-major-aref bit-array (apply #'%array-row-major-index bit-array subscripts)))
918 (defun (setf bit) (new-value bit-array &rest subscripts)
919 (declare (type (array bit) bit-array)
920 (type bit new-value)
921 (truly-dynamic-extent subscripts)
922 (optimize (safety 1)))
923 (setf (row-major-aref bit-array
924 (apply #'%array-row-major-index bit-array subscripts))
925 new-value))
927 (defun sbit (simple-bit-array &rest subscripts)
928 #!+sb-doc
929 "Return the bit from SIMPLE-BIT-ARRAY at the specified SUBSCRIPTS."
930 (declare (type (simple-array bit) simple-bit-array)
931 (truly-dynamic-extent subscripts)
932 (optimize (safety 1)))
933 (row-major-aref simple-bit-array
934 (apply #'%array-row-major-index simple-bit-array subscripts)))
936 (defun (setf sbit) (new-value bit-array &rest subscripts)
937 (declare (type (simple-array bit) bit-array)
938 (type bit new-value)
939 (truly-dynamic-extent subscripts)
940 (optimize (safety 1)))
941 (setf (row-major-aref bit-array
942 (apply #'%array-row-major-index bit-array subscripts))
943 new-value))
945 ;;;; miscellaneous array properties
947 (defun array-element-type (array)
948 #!+sb-doc
949 "Return the type of the elements of the array"
950 (let ((widetag (%other-pointer-widetag array))
951 (table (load-time-value
952 (let ((table (make-array 256 :initial-element nil)))
953 (dotimes (i (length sb!vm:*specialized-array-element-type-properties*) table)
954 (let* ((saetp (aref sb!vm:*specialized-array-element-type-properties* i))
955 (typecode (sb!vm:saetp-typecode saetp))
956 (complex-typecode (sb!vm:saetp-complex-typecode saetp))
957 (specifier (sb!vm:saetp-specifier saetp)))
958 (aver (typep specifier '(or list symbol)))
959 (setf (aref table typecode) specifier)
960 (when complex-typecode
961 (setf (aref table complex-typecode) specifier)))))
962 t)))
963 (let ((result (aref table widetag)))
964 (if result
965 (truly-the (or list symbol) result)
966 ;; (MAKE-ARRAY :ELEMENT-TYPE NIL) goes to this branch, but
967 ;; gets the right answer in the end
968 (with-array-data ((array array) (start) (end))
969 (declare (ignore start end))
970 (truly-the (or list symbol) (aref table (%other-pointer-widetag array))))))))
972 (defun array-rank (array)
973 #!+sb-doc
974 "Return the number of dimensions of ARRAY."
975 (if (array-header-p array)
976 (%array-rank array)
979 (defun array-dimension (array axis-number)
980 #!+sb-doc
981 "Return the length of dimension AXIS-NUMBER of ARRAY."
982 (declare (array array) (type index axis-number))
983 (cond ((not (array-header-p array))
984 (unless (= axis-number 0)
985 (error "Vector axis is not zero: ~S" axis-number))
986 (length (the (simple-array * (*)) array)))
987 ((>= axis-number (%array-rank array))
988 (error "Axis number ~W is too big; ~S only has ~D dimension~:P."
989 axis-number array (%array-rank array)))
991 (%array-dimension array axis-number))))
993 (defun array-dimensions (array)
994 #!+sb-doc
995 "Return a list whose elements are the dimensions of the array"
996 (declare (array array))
997 (if (array-header-p array)
998 (do ((results nil (cons (array-dimension array index) results))
999 (index (1- (array-rank array)) (1- index)))
1000 ((minusp index) results))
1001 (list (array-dimension array 0))))
1003 (defun array-total-size (array)
1004 #!+sb-doc
1005 "Return the total number of elements in the Array."
1006 (declare (array array))
1007 (if (array-header-p array)
1008 (%array-available-elements array)
1009 (length (the vector array))))
1011 (defun array-displacement (array)
1012 #!+sb-doc
1013 "Return the values of :DISPLACED-TO and :DISPLACED-INDEX-offset
1014 options to MAKE-ARRAY, or NIL and 0 if not a displaced array."
1015 (declare (type array array))
1016 (if (and (array-header-p array) ; if unsimple and
1017 (%array-displaced-p array)) ; displaced
1018 (values (%array-data-vector array) (%array-displacement array))
1019 (values nil 0)))
1021 (defun adjustable-array-p (array)
1022 #!+sb-doc
1023 "Return T if (ADJUST-ARRAY ARRAY...) would return an array identical
1024 to the argument, this happens for complex arrays."
1025 (declare (array array))
1026 ;; Note that this appears not to be a fundamental limitation.
1027 ;; non-vector SIMPLE-ARRAYs are in fact capable of being adjusted,
1028 ;; but in practice we test using ADJUSTABLE-ARRAY-P in ADJUST-ARRAY.
1029 ;; -- CSR, 2004-03-01.
1030 (not (typep array 'simple-array)))
1032 ;;;; fill pointer frobbing stuff
1034 (declaim (inline array-has-fill-pointer-p))
1035 (defun array-has-fill-pointer-p (array)
1036 #!+sb-doc
1037 "Return T if the given ARRAY has a fill pointer, or NIL otherwise."
1038 (declare (array array))
1039 (and (array-header-p array) (%array-fill-pointer-p array)))
1041 (defun fill-pointer-error (vector &optional arg)
1042 (cond (arg
1043 (aver (array-has-fill-pointer-p vector))
1044 (let ((max (%array-available-elements vector)))
1045 (error 'simple-type-error
1046 :datum arg
1047 :expected-type (list 'integer 0 max)
1048 :format-control "The new fill pointer, ~S, is larger than the length of the vector (~S.)"
1049 :format-arguments (list arg max))))
1051 (error 'simple-type-error
1052 :datum vector
1053 :expected-type '(and vector (satisfies array-has-fill-pointer-p))
1054 :format-control "~S is not an array with a fill pointer."
1055 :format-arguments (list vector)))))
1057 (declaim (inline fill-pointer))
1058 (defun fill-pointer (vector)
1059 #!+sb-doc
1060 "Return the FILL-POINTER of the given VECTOR."
1061 (declare (explicit-check))
1062 (if (array-has-fill-pointer-p vector)
1063 (%array-fill-pointer vector)
1064 (fill-pointer-error vector)))
1066 (defun %set-fill-pointer (vector new)
1067 (declare (explicit-check))
1068 (flet ((oops (x)
1069 (fill-pointer-error vector x)))
1070 (cond ((not (array-has-fill-pointer-p vector))
1071 (oops nil))
1072 ((> new (%array-available-elements vector))
1073 (oops new))
1075 (setf (%array-fill-pointer vector) new)))))
1077 ;;; FIXME: It'd probably make sense to use a MACROLET to share the
1078 ;;; guts of VECTOR-PUSH between VECTOR-PUSH-EXTEND. Such a macro
1079 ;;; should probably be based on the VECTOR-PUSH-EXTEND code (which is
1080 ;;; new ca. sbcl-0.7.0) rather than the VECTOR-PUSH code (which dates
1081 ;;; back to CMU CL).
1082 (defun vector-push (new-element array)
1083 #!+sb-doc
1084 "Attempt to set the element of ARRAY designated by its fill pointer
1085 to NEW-ELEMENT, and increment the fill pointer by one. If the fill pointer is
1086 too large, NIL is returned, otherwise the index of the pushed element is
1087 returned."
1088 (declare (explicit-check))
1089 (let ((fill-pointer (fill-pointer array)))
1090 (cond ((= fill-pointer (%array-available-elements array))
1091 nil)
1093 (locally (declare (optimize (safety 0)))
1094 (setf (aref array fill-pointer) new-element))
1095 (setf (%array-fill-pointer array) (1+ fill-pointer))
1096 fill-pointer))))
1098 ;;; Widetags of FROM and TO should be equal
1099 (defun copy-vector-data (from to start end n-bits)
1100 (declare (vector from to)
1101 (index start end)
1102 ((integer 0 255) n-bits))
1103 (let ((from-length (length from)))
1104 (cond ((simple-vector-p from)
1105 (replace (truly-the simple-vector to)
1106 (truly-the simple-vector from)
1107 :start2 start :end2 end))
1108 ;; Vector sizes are double-word aligned and have zeros in
1109 ;; the extra word so it's safe to copy when the boundaries
1110 ;; are matching the whole vector.
1111 ;; A more generic routine is left for another time, even if
1112 ;; only handling aligned data since it will avoid consing
1113 ;; floats or word bignums.
1114 ((and (= start 0)
1115 (= end from-length))
1116 (let ((words (ceiling (* from-length n-bits)
1117 sb!vm:n-word-bits)))
1118 (loop for i below words
1119 do (setf
1120 (%vector-raw-bits to i)
1121 (%vector-raw-bits from i)))))
1123 (replace to
1124 from
1125 :start2 start :end2 end)))
1126 to))
1128 (defun extend-vector (vector min-extension)
1129 (declare (optimize speed)
1130 (vector vector))
1131 (let* ((old-length (length vector))
1132 (min-extension (or min-extension
1133 (min old-length
1134 (- array-dimension-limit old-length))))
1135 (new-length (the index (+ old-length
1136 (max 1 min-extension))))
1137 (fill-pointer (1+ old-length)))
1138 (declare (fixnum new-length min-extension))
1139 (with-array-data ((old-data vector) (old-start)
1140 (old-end old-length))
1141 (let* ((widetag (%other-pointer-widetag old-data))
1142 (n-bits (aref %%simple-array-n-bits%% widetag))
1143 (new-data
1144 (allocate-vector-with-widetag widetag new-length n-bits)))
1145 (copy-vector-data old-data new-data old-start old-end n-bits)
1146 (setf (%array-data-vector vector) new-data
1147 (%array-available-elements vector) new-length
1148 (%array-fill-pointer vector) fill-pointer
1149 (%array-displacement vector) 0
1150 (%array-dimension vector 0) new-length
1151 (%array-displaced-p vector) nil)
1152 vector))))
1154 (defun vector-push-extend (new-element vector &optional min-extension)
1155 (declare (type (or null (and index (integer 1))) min-extension))
1156 (declare (explicit-check))
1157 (let* ((fill-pointer (fill-pointer vector))
1158 (new-fill-pointer (1+ fill-pointer)))
1159 (if (= fill-pointer (%array-available-elements vector))
1160 (extend-vector vector min-extension)
1161 (setf (%array-fill-pointer vector) new-fill-pointer))
1162 ;; disable bounds checking
1163 (locally (declare (optimize (safety 0)))
1164 (setf (aref vector fill-pointer) new-element))
1165 fill-pointer))
1167 (defun vector-pop (array)
1168 #!+sb-doc
1169 "Decrease the fill pointer by 1 and return the element pointed to by the
1170 new fill pointer."
1171 (declare (explicit-check))
1172 (let ((fill-pointer (fill-pointer array)))
1173 (if (zerop fill-pointer)
1174 (error "There is nothing left to pop.")
1175 ;; disable bounds checking (and any fixnum test)
1176 (locally (declare (optimize (safety 0)))
1177 (aref array
1178 (setf (%array-fill-pointer array)
1179 (1- fill-pointer)))))))
1182 ;;;; ADJUST-ARRAY
1184 (defun adjust-array (array dimensions &key
1185 (element-type (array-element-type array) element-type-p)
1186 (initial-element nil initial-element-p)
1187 (initial-contents nil initial-contents-p)
1188 fill-pointer
1189 displaced-to displaced-index-offset)
1190 #!+sb-doc
1191 "Adjust ARRAY's dimensions to the given DIMENSIONS and stuff."
1192 (when (invalid-array-p array)
1193 (invalid-array-error array))
1194 (binding* ((dimensions (ensure-list dimensions))
1195 (array-rank (array-rank array))
1197 (unless (= (length dimensions) array-rank)
1198 (error "The number of dimensions not equal to rank of array.")))
1199 ((initialize initial-data)
1200 (validate-array-initargs initial-element-p initial-element
1201 initial-contents-p initial-contents
1202 displaced-to)))
1203 (cond ((and element-type-p
1204 (not (subtypep element-type (array-element-type array))))
1205 ;; This is weird. Should check upgraded type against actual
1206 ;; array element type I think. See lp#1331299. CLHS says that
1207 ;; "consequences are unspecified" so current behavior isn't wrong.
1208 (error "The new element type, ~S, is incompatible with old type."
1209 element-type))
1210 ((and fill-pointer (/= array-rank 1))
1211 (error "Only vectors can have fill pointers."))
1212 ((and fill-pointer (not (array-has-fill-pointer-p array)))
1213 ;; This case always struck me as odd. It seems like it might mean
1214 ;; that the user asks that the array gain a fill-pointer if it didn't
1215 ;; have one, yet CLHS is clear that the argument array must have a
1216 ;; fill-pointer or else signal a type-error.
1217 (fill-pointer-error array)))
1218 (cond (initial-contents-p
1219 ;; array former contents replaced by INITIAL-CONTENTS
1220 (let* ((array-size (apply #'* dimensions))
1221 (array-data (data-vector-from-inits
1222 dimensions array-size element-type nil nil
1223 initialize initial-data)))
1224 (cond ((adjustable-array-p array)
1225 (set-array-header array array-data array-size
1226 (get-new-fill-pointer array array-size
1227 fill-pointer)
1228 0 dimensions nil nil))
1229 ((array-header-p array)
1230 ;; simple multidimensional or single dimensional array
1231 (make-array dimensions
1232 :element-type element-type
1233 :initial-contents initial-contents))
1235 array-data))))
1236 (displaced-to
1237 ;; We already established that no INITIAL-CONTENTS was supplied.
1238 (unless (or (eql element-type (array-element-type displaced-to))
1239 (subtypep element-type (array-element-type displaced-to)))
1240 ;; See lp#1331299 again. Require exact match on upgraded type?
1241 (error "can't displace an array of type ~S into another of ~
1242 type ~S"
1243 element-type (array-element-type displaced-to)))
1244 (let ((displacement (or displaced-index-offset 0))
1245 (array-size (apply #'* dimensions)))
1246 (declare (fixnum displacement array-size))
1247 (if (< (the fixnum (array-total-size displaced-to))
1248 (the fixnum (+ displacement array-size)))
1249 (error "The :DISPLACED-TO array is too small."))
1250 (if (adjustable-array-p array)
1251 ;; None of the original contents appear in adjusted array.
1252 (set-array-header array displaced-to array-size
1253 (get-new-fill-pointer array array-size
1254 fill-pointer)
1255 displacement dimensions t nil)
1256 ;; simple multidimensional or single dimensional array
1257 (make-array dimensions
1258 :element-type element-type
1259 :displaced-to displaced-to
1260 :displaced-index-offset
1261 displaced-index-offset))))
1262 ((= array-rank 1)
1263 (let ((old-length (array-total-size array))
1264 (new-length (car dimensions))
1265 new-data)
1266 (declare (fixnum old-length new-length))
1267 (with-array-data ((old-data array) (old-start)
1268 (old-end old-length))
1269 (cond ((or (and (array-header-p array)
1270 (%array-displaced-p array))
1271 (< old-length new-length))
1272 (setf new-data
1273 (data-vector-from-inits
1274 dimensions new-length element-type
1275 (%other-pointer-widetag old-data) nil
1276 initialize initial-data))
1277 ;; Provide :END1 to avoid full call to LENGTH
1278 ;; inside REPLACE.
1279 (replace new-data old-data
1280 :end1 new-length
1281 :start2 old-start :end2 old-end))
1282 (t (setf new-data
1283 (shrink-vector old-data new-length))))
1284 (if (adjustable-array-p array)
1285 (set-array-header array new-data new-length
1286 (get-new-fill-pointer array new-length
1287 fill-pointer)
1288 0 dimensions nil nil)
1289 new-data))))
1291 (let ((old-length (%array-available-elements array))
1292 (new-length (apply #'* dimensions)))
1293 (declare (fixnum old-length new-length))
1294 (with-array-data ((old-data array) (old-start)
1295 (old-end old-length))
1296 (declare (ignore old-end))
1297 (let ((new-data (if (or (and (array-header-p array)
1298 (%array-displaced-p array))
1299 (> new-length old-length)
1300 (not (adjustable-array-p array)))
1301 (data-vector-from-inits
1302 dimensions new-length
1303 element-type
1304 (%other-pointer-widetag old-data) nil
1305 (if initial-element-p :initial-element)
1306 initial-element)
1307 old-data)))
1308 (if (or (zerop old-length) (zerop new-length))
1309 (when initial-element-p (fill new-data initial-element))
1310 (zap-array-data old-data (array-dimensions array)
1311 old-start
1312 new-data dimensions new-length
1313 element-type initial-element
1314 initial-element-p))
1315 (if (adjustable-array-p array)
1316 (set-array-header array new-data new-length
1317 nil 0 dimensions nil nil)
1318 (let ((new-array
1319 (make-array-header
1320 sb!vm:simple-array-widetag array-rank)))
1321 (set-array-header new-array new-data new-length
1322 nil 0 dimensions nil t))))))))))
1325 (defun get-new-fill-pointer (old-array new-array-size fill-pointer)
1326 (declare (fixnum new-array-size))
1327 (typecase fill-pointer
1328 (null
1329 ;; "The consequences are unspecified if array is adjusted to a
1330 ;; size smaller than its fill pointer ..."
1331 (when (array-has-fill-pointer-p old-array)
1332 (when (> (%array-fill-pointer old-array) new-array-size)
1333 (error "cannot ADJUST-ARRAY an array (~S) to a size (~S) that is ~
1334 smaller than its fill pointer (~S)"
1335 old-array new-array-size (fill-pointer old-array)))
1336 (%array-fill-pointer old-array)))
1337 ((eql t)
1338 new-array-size)
1339 (fixnum
1340 (when (> fill-pointer new-array-size)
1341 (error "can't supply a value for :FILL-POINTER (~S) that is larger ~
1342 than the new length of the vector (~S)"
1343 fill-pointer new-array-size))
1344 fill-pointer)))
1346 ;;; Destructively alter VECTOR, changing its length to NEW-LENGTH,
1347 ;;; which must be less than or equal to its current length. This can
1348 ;;; be called on vectors without a fill pointer but it is extremely
1349 ;;; dangerous to do so: shrinking the size of an object (as viewed by
1350 ;;; the gc) makes bounds checking unreliable in the face of interrupts
1351 ;;; or multi-threading. Call it only on provably local vectors.
1352 (defun %shrink-vector (vector new-length)
1353 (declare (vector vector))
1354 (unless (array-header-p vector)
1355 (macrolet ((frob (name &rest things)
1356 `(etypecase ,name
1357 ((simple-array nil (*)) (error 'nil-array-accessed-error))
1358 ,@(mapcar (lambda (thing)
1359 (destructuring-bind (type-spec fill-value)
1360 thing
1361 `(,type-spec
1362 (fill (truly-the ,type-spec ,name)
1363 ,fill-value
1364 :start new-length))))
1365 things))))
1366 ;; Set the 'tail' of the vector to the appropriate type of zero,
1367 ;; "because in some cases we'll scavenge larger areas in one go,
1368 ;; like groups of pages that had triggered the write barrier, or
1369 ;; the whole static space" according to jsnell.
1370 #.`(frob vector
1371 ,@(map 'list
1372 (lambda (saetp)
1373 `((simple-array ,(sb!vm:saetp-specifier saetp) (*))
1374 ,(if (or (eq (sb!vm:saetp-specifier saetp) 'character)
1375 #!+sb-unicode
1376 (eq (sb!vm:saetp-specifier saetp) 'base-char))
1377 *default-init-char-form*
1378 (sb!vm:saetp-initial-element-default saetp))))
1379 (remove-if-not
1380 #'sb!vm:saetp-specifier
1381 sb!vm:*specialized-array-element-type-properties*)))))
1382 ;; Only arrays have fill-pointers, but vectors have their length
1383 ;; parameter in the same place.
1384 (setf (%array-fill-pointer vector) new-length)
1385 vector)
1387 (defun shrink-vector (vector new-length)
1388 (declare (vector vector))
1389 (cond
1390 ((eq (length vector) new-length)
1391 vector)
1392 ((array-has-fill-pointer-p vector)
1393 (setf (%array-fill-pointer vector) new-length)
1394 vector)
1395 (t (subseq vector 0 new-length))))
1397 ;;; BIG THREAD SAFETY NOTE
1399 ;;; ADJUST-ARRAY/SET-ARRAY-HEADER, and its callees are very
1400 ;;; thread unsafe. They are nonatomic, and can mess with parallel
1401 ;;; code using the same arrays.
1403 ;;; A likely seeming fix is an additional level of indirection:
1404 ;;; ARRAY-HEADER -> ARRAY-INFO -> ... where ARRAY-HEADER would
1405 ;;; hold nothing but the pointer to ARRAY-INFO, and ARRAY-INFO
1406 ;;; would hold everything ARRAY-HEADER now holds. This allows
1407 ;;; consing up a new ARRAY-INFO and replacing it atomically in
1408 ;;; the ARRAY-HEADER.
1410 ;;; %WALK-DISPLACED-ARRAY-BACKPOINTERS is an especially nasty
1411 ;;; one: not only is it needed extremely rarely, which makes
1412 ;;; any thread safety bugs involving it look like rare random
1413 ;;; corruption, but because it walks the chain *upwards*, which
1414 ;;; may violate user expectations.
1416 ;;; Fill in array header with the provided information, and return the array.
1417 (defun set-array-header (array data length fill-pointer displacement dimensions
1418 displacedp newp)
1419 (labels ((%walk-displaced-array-backpointers (array new-length)
1420 (dolist (p (%array-displaced-from array))
1421 (let ((from (weak-pointer-value p)))
1422 (when (and from (eq array (%array-data-vector from)))
1423 (let ((requires (+ (%array-available-elements from)
1424 (%array-displacement from))))
1425 (unless (>= new-length requires)
1426 ;; ANSI sayeth (ADJUST-ARRAY dictionary entry):
1428 ;; "If A is displaced to B, the consequences are unspecified if B is
1429 ;; adjusted in such a way that it no longer has enough elements to
1430 ;; satisfy A.
1432 ;; since we're hanging on a weak pointer here, we can't signal an
1433 ;; error right now: the array that we're looking at might be
1434 ;; garbage. Instead, we set all dimensions to zero so that next
1435 ;; safe access to the displaced array will trap. Additionally, we
1436 ;; save the original dimensions, so we can signal a more
1437 ;; understandable error when the time comes.
1438 (%walk-displaced-array-backpointers from 0)
1439 (setf (%array-fill-pointer from) 0
1440 (%array-available-elements from) 0
1441 (%array-displaced-p from) (array-dimensions array))
1442 (dotimes (i (%array-rank from))
1443 (setf (%array-dimension from i) 0)))))))))
1444 (if newp
1445 (setf (%array-displaced-from array) nil)
1446 (%walk-displaced-array-backpointers array length))
1447 (when displacedp
1448 (%save-displaced-array-backpointer array data))
1449 (setf (%array-data-vector array) data)
1450 (setf (%array-available-elements array) length)
1451 (cond (fill-pointer
1452 (setf (%array-fill-pointer array) fill-pointer)
1453 (setf (%array-fill-pointer-p array) t))
1455 (setf (%array-fill-pointer array) length)
1456 (setf (%array-fill-pointer-p array) nil)))
1457 (setf (%array-displacement array) displacement)
1458 (if (listp dimensions)
1459 (dotimes (axis (array-rank array))
1460 (declare (type index axis))
1461 (setf (%array-dimension array axis) (pop dimensions)))
1462 (setf (%array-dimension array 0) dimensions))
1463 (setf (%array-displaced-p array) displacedp)
1464 array))
1466 ;;; User visible extension
1467 (declaim (ftype (sfunction (array) (simple-array * (*))) array-storage-vector))
1468 (defun array-storage-vector (array)
1469 #!+sb-doc
1470 "Returns the underlying storage vector of ARRAY, which must be a non-displaced array.
1472 In SBCL, if ARRAY is a of type \(SIMPLE-ARRAY * \(*)), it is its own storage
1473 vector. Multidimensional arrays, arrays with fill pointers, and adjustable
1474 arrays have an underlying storage vector with the same ARRAY-ELEMENT-TYPE as
1475 ARRAY, which this function returns.
1477 Important note: the underlying vector is an implementation detail. Even though
1478 this function exposes it, changes in the implementation may cause this
1479 function to be removed without further warning."
1480 ;; KLUDGE: Without TRULY-THE the system is not smart enough to figure out that
1481 ;; the return value is always of the known type.
1482 (truly-the (simple-array * (*))
1483 (cond ((not (array-header-p array))
1484 array)
1485 ((%array-displaced-p array)
1486 (error "~S cannot be used with displaced arrays. Use ~S instead."
1487 'array-storage-vector 'array-displacement))
1489 (%array-data-vector array)))))
1492 ;;;; ZAP-ARRAY-DATA for ADJUST-ARRAY
1494 ;;; This does the grinding work for ADJUST-ARRAY. It zaps the data
1495 ;;; from the OLD-DATA in an arrangement specified by the OLD-DIMS to
1496 ;;; the NEW-DATA in an arrangement specified by the NEW-DIMS. OFFSET
1497 ;;; is a displaced offset to be added to computed indices of OLD-DATA.
1498 (defun zap-array-data (old-data old-dims offset new-data new-dims new-length
1499 element-type initial-element initial-element-p)
1500 (declare (list old-dims new-dims)
1501 (fixnum new-length))
1502 ;; OLD-DIMS comes from array-dimensions, which returns a fresh list
1503 ;; at least in SBCL.
1504 ;; NEW-DIMS comes from the user.
1505 (setf old-dims (nreverse old-dims)
1506 new-dims (reverse new-dims))
1507 (cond ((eq old-data new-data)
1508 ;; NEW-LENGTH, ELEMENT-TYPE, INITIAL-ELEMENT, and
1509 ;; INITIAL-ELEMENT-P are used when OLD-DATA and NEW-DATA are
1510 ;; EQ; in this case, a temporary must be used and filled
1511 ;; appropriately. specified initial-element.
1512 ;; FIXME: transforming this TYPEP to someting a bit faster
1513 ;; would be a win...
1514 (unless (or (not initial-element-p)
1515 (typep initial-element element-type))
1516 (error "~S can't be used to initialize an array of type ~S."
1517 initial-element element-type))
1518 (let ((temp (if initial-element-p
1519 (make-array new-length :initial-element initial-element)
1520 (make-array new-length))))
1521 (declare (simple-vector temp))
1522 (zap-array-data-aux old-data old-dims offset temp new-dims)
1523 (dotimes (i new-length)
1524 (setf (aref new-data i) (aref temp i)))
1525 ;; Kill the temporary vector to prevent garbage retention.
1526 (%shrink-vector temp 0)))
1528 ;; When OLD-DATA and NEW-DATA are not EQ, NEW-DATA has
1529 ;; already been filled with any
1530 (zap-array-data-aux old-data old-dims offset new-data new-dims))))
1532 (defun zap-array-data-aux (old-data old-dims offset new-data new-dims)
1533 (declare (fixnum offset))
1534 (let ((limits (mapcar (lambda (x y)
1535 (declare (fixnum x y))
1536 (1- (the fixnum (min x y))))
1537 old-dims new-dims)))
1538 (macrolet ((bump-index-list (index limits)
1539 `(do ((subscripts ,index (cdr subscripts))
1540 (limits ,limits (cdr limits)))
1541 ((null subscripts) :eof)
1542 (cond ((< (the fixnum (car subscripts))
1543 (the fixnum (car limits)))
1544 (rplaca subscripts
1545 (1+ (the fixnum (car subscripts))))
1546 (return ,index))
1547 (t (rplaca subscripts 0))))))
1548 (do ((index (make-list (length old-dims) :initial-element 0)
1549 (bump-index-list index limits)))
1550 ((eq index :eof))
1551 (setf (aref new-data (row-major-index-from-dims index new-dims))
1552 (aref old-data
1553 (+ (the fixnum (row-major-index-from-dims index old-dims))
1554 offset)))))))
1556 ;;; Figure out the row-major-order index of an array reference from a
1557 ;;; list of subscripts and a list of dimensions. This is for internal
1558 ;;; calls only, and the subscripts and dim-list variables are assumed
1559 ;;; to be reversed from what the user supplied.
1560 (defun row-major-index-from-dims (rev-subscripts rev-dim-list)
1561 (do ((rev-subscripts rev-subscripts (cdr rev-subscripts))
1562 (rev-dim-list rev-dim-list (cdr rev-dim-list))
1563 (chunk-size 1)
1564 (result 0))
1565 ((null rev-dim-list) result)
1566 (declare (fixnum chunk-size result))
1567 (setq result (+ result
1568 (the fixnum (* (the fixnum (car rev-subscripts))
1569 chunk-size))))
1570 (setq chunk-size (* chunk-size (the fixnum (car rev-dim-list))))))
1572 ;;;; some bit stuff
1574 (defun bit-array-same-dimensions-p (array1 array2)
1575 (declare (type (array bit) array1 array2))
1576 (and (= (array-rank array1)
1577 (array-rank array2))
1578 (dotimes (index (array-rank array1) t)
1579 (when (/= (array-dimension array1 index)
1580 (array-dimension array2 index))
1581 (return nil)))))
1583 (defun pick-result-array (result-bit-array bit-array-1)
1584 (case result-bit-array
1585 ((t) bit-array-1)
1586 ((nil) (make-array (array-dimensions bit-array-1)
1587 :element-type 'bit
1588 :initial-element 0))
1590 (unless (bit-array-same-dimensions-p bit-array-1
1591 result-bit-array)
1592 (error "~S and ~S don't have the same dimensions."
1593 bit-array-1 result-bit-array))
1594 result-bit-array)))
1596 (defmacro def-bit-array-op (name function)
1597 `(defun ,name (bit-array-1 bit-array-2 &optional result-bit-array)
1598 #!+sb-doc
1599 ,(format nil
1600 "Perform a bit-wise ~A on the elements of BIT-ARRAY-1 and ~
1601 BIT-ARRAY-2,~% putting the results in RESULT-BIT-ARRAY. ~
1602 If RESULT-BIT-ARRAY is T,~% BIT-ARRAY-1 is used. If ~
1603 RESULT-BIT-ARRAY is NIL or omitted, a new array is~% created. ~
1604 All the arrays must have the same rank and dimensions."
1605 (symbol-name function))
1606 (declare (type (array bit) bit-array-1 bit-array-2)
1607 (type (or (array bit) (member t nil)) result-bit-array))
1608 (unless (bit-array-same-dimensions-p bit-array-1 bit-array-2)
1609 (error "~S and ~S don't have the same dimensions."
1610 bit-array-1 bit-array-2))
1611 (let ((result-bit-array (pick-result-array result-bit-array bit-array-1)))
1612 (if (and (simple-bit-vector-p bit-array-1)
1613 (simple-bit-vector-p bit-array-2)
1614 (simple-bit-vector-p result-bit-array))
1615 (locally (declare (optimize (speed 3) (safety 0)))
1616 (,name bit-array-1 bit-array-2 result-bit-array))
1617 (with-array-data ((data1 bit-array-1) (start1) (end1))
1618 (declare (ignore end1))
1619 (with-array-data ((data2 bit-array-2) (start2) (end2))
1620 (declare (ignore end2))
1621 (with-array-data ((data3 result-bit-array) (start3) (end3))
1622 (do ((index-1 start1 (1+ index-1))
1623 (index-2 start2 (1+ index-2))
1624 (index-3 start3 (1+ index-3)))
1625 ((>= index-3 end3) result-bit-array)
1626 (declare (type index index-1 index-2 index-3))
1627 (setf (sbit data3 index-3)
1628 (logand (,function (sbit data1 index-1)
1629 (sbit data2 index-2))
1630 1))))))))))
1632 (def-bit-array-op bit-and logand)
1633 (def-bit-array-op bit-ior logior)
1634 (def-bit-array-op bit-xor logxor)
1635 (def-bit-array-op bit-eqv logeqv)
1636 (def-bit-array-op bit-nand lognand)
1637 (def-bit-array-op bit-nor lognor)
1638 (def-bit-array-op bit-andc1 logandc1)
1639 (def-bit-array-op bit-andc2 logandc2)
1640 (def-bit-array-op bit-orc1 logorc1)
1641 (def-bit-array-op bit-orc2 logorc2)
1643 (defun bit-not (bit-array &optional result-bit-array)
1644 #!+sb-doc
1645 "Performs a bit-wise logical NOT on the elements of BIT-ARRAY,
1646 putting the results in RESULT-BIT-ARRAY. If RESULT-BIT-ARRAY is T,
1647 BIT-ARRAY is used. If RESULT-BIT-ARRAY is NIL or omitted, a new array is
1648 created. Both arrays must have the same rank and dimensions."
1649 (declare (type (array bit) bit-array)
1650 (type (or (array bit) (member t nil)) result-bit-array))
1651 (let ((result-bit-array (pick-result-array result-bit-array bit-array)))
1652 (if (and (simple-bit-vector-p bit-array)
1653 (simple-bit-vector-p result-bit-array))
1654 (locally (declare (optimize (speed 3) (safety 0)))
1655 (bit-not bit-array result-bit-array))
1656 (with-array-data ((src bit-array) (src-start) (src-end))
1657 (declare (ignore src-end))
1658 (with-array-data ((dst result-bit-array) (dst-start) (dst-end))
1659 (do ((src-index src-start (1+ src-index))
1660 (dst-index dst-start (1+ dst-index)))
1661 ((>= dst-index dst-end) result-bit-array)
1662 (declare (type index src-index dst-index))
1663 (setf (sbit dst dst-index)
1664 (logxor (sbit src src-index) 1))))))))
1666 ;;;; array type dispatching
1668 ;;; Given DISPATCH-FOO as the DISPATCH-NAME argument (unevaluated),
1669 ;;; defines the functions
1671 ;;; DISPATCH-FOO/SIMPLE-BASE-STRING
1672 ;;; DISPATCH-FOO/SIMPLE-CHARACTER-STRING
1673 ;;; DISPATCH-FOO/SIMPLE-ARRAY-SINGLE-FLOAT
1674 ;;; ...
1676 ;;; PARAMS are the function parameters in the definition of each
1677 ;;; specializer function. The array being specialized must be the
1678 ;;; first parameter in PARAMS. A type declaration for this parameter
1679 ;;; is automatically inserted into the body of each function.
1681 ;;; The dispatch table %%FOO-FUNS%% is defined and populated by these
1682 ;;; functions. The table is padded by the function
1683 ;;; HAIRY-FOO-DISPATCH-ERROR, also defined by DEFINE-ARRAY-DISPATCH.
1685 ;;; Finally, the DISPATCH-FOO macro is defined which does the actual
1686 ;;; dispatching when called. It expects arguments that match PARAMS.
1688 (defmacro !define-array-dispatch (dispatch-name params &body body)
1689 (let ((table-name (symbolicate "%%" dispatch-name "-FUNS%%"))
1690 (error-name (symbolicate "HAIRY-" dispatch-name "-ERROR")))
1691 `(progn
1692 (eval-when (:compile-toplevel :load-toplevel :execute)
1693 (defun ,error-name (&rest args)
1694 (error 'type-error
1695 :datum (first args)
1696 :expected-type '(simple-array * (*)))))
1697 (!defglobal ,table-name (make-array ,(1+ sb!vm:widetag-mask)
1698 :initial-element #',error-name))
1699 ,@(loop for info across sb!vm:*specialized-array-element-type-properties*
1700 for typecode = (sb!vm:saetp-typecode info)
1701 for specifier = (sb!vm:saetp-specifier info)
1702 for primitive-type-name = (sb!vm:saetp-primitive-type-name info)
1703 collect (let ((fun-name (symbolicate (string dispatch-name)
1704 "/" primitive-type-name)))
1705 `(progn
1706 (defun ,fun-name ,params
1707 (declare (type (simple-array ,specifier (*))
1708 ,(first params)))
1709 ,@body)
1710 (setf (svref ,table-name ,typecode) #',fun-name))))
1711 (defmacro ,dispatch-name (&rest args)
1712 (check-type (first args) symbol)
1713 (let ((tag (gensym "TAG")))
1714 `(funcall
1715 (the function
1716 (let ((,tag 0))
1717 (when (sb!vm::%other-pointer-p ,(first args))
1718 (setf ,tag (%other-pointer-widetag ,(first args))))
1719 (svref ,',table-name ,tag)))
1720 ,@args))))))