Clean up nested IFs.
[sbcl.git] / src / code / array.lisp
blobb0d15f649ed9c018bfa7045e4fb20316c6c81944
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))
55 (defun %with-array-data/fp (array start end)
56 (%with-array-data-macro array start end :check-bounds t :check-fill-pointer t))
58 (defun %with-array-data (array start end)
59 (%with-array-data-macro array start end :check-bounds t :check-fill-pointer nil))
61 (defun %data-vector-and-index (array index)
62 (if (array-header-p array)
63 (multiple-value-bind (vector index)
64 (%with-array-data array index nil)
65 (values vector index))
66 (values array index)))
68 ;;;; MAKE-ARRAY
69 (defun %integer-vector-widetag-and-n-bits (signed high)
70 (let ((unsigned-table
71 #.(let ((map (make-array (1+ sb!vm:n-word-bits))))
72 (loop for saetp across
73 (reverse sb!vm:*specialized-array-element-type-properties*)
74 for ctype = (sb!vm:saetp-ctype saetp)
75 when (and (numeric-type-p ctype)
76 (eq (numeric-type-class ctype) 'integer)
77 (zerop (numeric-type-low ctype)))
78 do (fill map (cons (sb!vm:saetp-typecode saetp)
79 (sb!vm:saetp-n-bits saetp))
80 :end (1+ (integer-length (numeric-type-high ctype)))))
81 map))
82 (signed-table
83 #.(let ((map (make-array (1+ sb!vm:n-word-bits))))
84 (loop for saetp across
85 (reverse sb!vm:*specialized-array-element-type-properties*)
86 for ctype = (sb!vm:saetp-ctype saetp)
87 when (and (numeric-type-p ctype)
88 (eq (numeric-type-class ctype) 'integer)
89 (minusp (numeric-type-low ctype)))
90 do (fill map (cons (sb!vm:saetp-typecode saetp)
91 (sb!vm:saetp-n-bits saetp))
92 :end (+ (integer-length (numeric-type-high ctype)) 2)))
93 map)))
94 (cond ((> high sb!vm:n-word-bits)
95 (values #.sb!vm:simple-vector-widetag #.sb!vm:n-word-bits))
96 (signed
97 (let ((x (aref signed-table high)))
98 (values (car x) (cdr x))))
100 (let ((x (aref unsigned-table high)))
101 (values (car x) (cdr x)))))))
103 ;;; This is a bit complicated, but calling subtypep over all
104 ;;; specialized types is exceedingly slow
105 (defun %vector-widetag-and-n-bits (type)
106 (macrolet ((with-parameters ((arg-type &key intervals)
107 (&rest args) &body body)
108 (let ((type-sym (gensym)))
109 `(let (,@(loop for arg in args
110 collect `(,arg '*)))
111 (declare (ignorable ,@args))
112 (when (consp type)
113 (let ((,type-sym (cdr type)))
114 (block nil
115 ,@(loop for arg in args
116 collect
117 `(cond ((consp ,type-sym)
118 (let ((value (pop ,type-sym)))
119 (if (or (eq value '*)
120 (typep value ',arg-type)
121 ,(if intervals
122 `(and (consp value)
123 (null (cdr value))
124 (typep (car value)
125 ',arg-type))))
126 (setf ,arg value)
127 (ill-type))))
128 ((null ,type-sym)
129 (return))
131 (ill-type)))))
132 (when ,type-sym
133 (ill-type))))
134 ,@body)))
135 (result (widetag)
136 (let ((value (symbol-value widetag)))
137 `(values ,value
138 ,(sb!vm:saetp-n-bits
139 (find value
140 sb!vm:*specialized-array-element-type-properties*
141 :key #'sb!vm:saetp-typecode))))))
142 (flet ((ill-type ()
143 (error "Invalid type specifier: ~s" type))
144 (integer-interval-widetag (low high)
145 (if (minusp low)
146 (%integer-vector-widetag-and-n-bits
148 (1+ (max (integer-length low) (integer-length high))))
149 (%integer-vector-widetag-and-n-bits
151 (max (integer-length low) (integer-length high))))))
152 (let* ((consp (consp type))
153 (type-name (if consp
154 (car type)
155 type)))
156 (case type-name
157 ((t)
158 (when consp
159 (ill-type))
160 (result sb!vm:simple-vector-widetag))
161 ((base-char standard-char #!-sb-unicode character)
162 (when consp
163 (ill-type))
164 (result sb!vm:simple-base-string-widetag))
165 #!+sb-unicode
166 ((character extended-char)
167 (when consp
168 (ill-type))
169 (result sb!vm:simple-character-string-widetag))
170 (bit
171 (when consp
172 (ill-type))
173 (result sb!vm:simple-bit-vector-widetag))
174 (fixnum
175 (when consp
176 (ill-type))
177 (result sb!vm:simple-array-fixnum-widetag))
178 (unsigned-byte
179 (with-parameters ((integer 1)) (high)
180 (if (eq high '*)
181 (result sb!vm:simple-vector-widetag)
182 (%integer-vector-widetag-and-n-bits nil high))))
183 (signed-byte
184 (with-parameters ((integer 1)) (high)
185 (if (eq high '*)
186 (result sb!vm:simple-vector-widetag)
187 (%integer-vector-widetag-and-n-bits t high))))
188 (double-float
189 (with-parameters (double-float :intervals t) (low high)
190 (if (and (not (eq low '*))
191 (not (eq high '*))
192 (if (or (consp low) (consp high))
193 (>= (type-bound-number low) (type-bound-number high))
194 (> low high)))
195 (result sb!vm:simple-array-nil-widetag)
196 (result sb!vm:simple-array-double-float-widetag))))
197 (single-float
198 (with-parameters (single-float :intervals t) (low high)
199 (if (and (not (eq low '*))
200 (not (eq high '*))
201 (if (or (consp low) (consp high))
202 (>= (type-bound-number low) (type-bound-number high))
203 (> low high)))
204 (result sb!vm:simple-array-nil-widetag)
205 (result sb!vm:simple-array-single-float-widetag))))
206 (mod
207 (if (and (consp type)
208 (consp (cdr type))
209 (null (cddr type))
210 (typep (cadr type) '(integer 1)))
211 (%integer-vector-widetag-and-n-bits
212 nil (integer-length (1- (cadr type))))
213 (ill-type)))
214 #!+long-float
215 (long-float
216 (with-parameters (long-float :intervals t) (low high)
217 (if (and (not (eq low '*))
218 (not (eq high '*))
219 (if (or (consp low) (consp high))
220 (>= (type-bound-number low) (type-bound-number high))
221 (> low high)))
222 (result sb!vm:simple-array-nil-widetag)
223 (result sb!vm:simple-array-long-float-widetag))))
224 (integer
225 (with-parameters (integer :intervals t) (low high)
226 (let ((low (if (consp low)
227 (1+ (car low))
228 low))
229 (high (if (consp high)
230 (1- (car high))
231 high)))
232 (cond ((or (eq high '*)
233 (eq low '*))
234 (result sb!vm:simple-vector-widetag))
235 ((> low high)
236 (result sb!vm:simple-array-nil-widetag))
238 (integer-interval-widetag low high))))))
239 (complex
240 (with-parameters (t) (subtype)
241 (if (eq subtype '*)
242 (result sb!vm:simple-vector-widetag)
243 (let ((ctype (specifier-type type)))
244 (cond ((eq ctype *empty-type*)
245 (result sb!vm:simple-array-nil-widetag))
246 ((union-type-p ctype)
247 (cond ((csubtypep ctype (specifier-type '(complex double-float)))
248 (result
249 sb!vm:simple-array-complex-double-float-widetag))
250 ((csubtypep ctype (specifier-type '(complex single-float)))
251 (result
252 sb!vm:simple-array-complex-single-float-widetag))
253 #!+long-float
254 ((csubtypep ctype (specifier-type '(complex long-float)))
255 (result
256 sb!vm:simple-array-complex-long-float-widetag))
258 (result sb!vm:simple-vector-widetag))))
260 (case (numeric-type-format ctype)
261 (double-float
262 (result
263 sb!vm:simple-array-complex-double-float-widetag))
264 (single-float
265 (result
266 sb!vm:simple-array-complex-single-float-widetag))
267 #!+long-float
268 (long-float
269 (result
270 sb!vm:simple-array-complex-long-float-widetag))
272 (result sb!vm:simple-vector-widetag)))))))))
273 ((nil)
274 (result sb!vm:simple-array-nil-widetag))
276 (block nil
277 (let ((ctype
278 (handler-case (specifier-type type)
279 (parse-unknown-type ()
280 (return (result sb!vm:simple-vector-widetag))))))
281 (typecase ctype
282 (union-type ; FIXME: forward ref
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 ; FIXME: forward ref
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 (let ((widetag (array-underlying-widetag vector)))
369 (%make-array length widetag (aref %%simple-array-n-bits%% widetag))))
371 ;; Complain in various ways about wrong :INITIAL-foo arguments,
372 ;; returning the two initialization arguments needed for DATA-VECTOR-FROM-INITS.
373 (defun validate-array-initargs (element-p element contents-p contents displaced)
374 (cond ((and displaced (or element-p contents-p))
375 (if (and element-p contents-p)
376 (error "Neither :INITIAL-ELEMENT nor :INITIAL-CONTENTS ~
377 may be specified with the :DISPLACED-TO option")
378 (error "~S may not be specified with the :DISPLACED-TO option"
379 (if element-p :initial-element :initial-contents))))
380 ((and element-p contents-p)
381 (error "Can't specify both :INITIAL-ELEMENT and :INITIAL-CONTENTS"))
382 (element-p (values :initial-element element))
383 (contents-p (values :initial-contents contents))
384 (t (values nil nil))))
386 (declaim (inline %save-displaced-array-backpointer))
387 (defun %save-displaced-array-backpointer (array data)
388 (flet ((purge (pointers)
389 (remove-if (lambda (value)
390 (or (not value) (eq array value)))
391 pointers
392 :key #'weak-pointer-value)))
393 ;; Add backpointer to the new data vector if it has a header.
394 (when (array-header-p data)
395 (setf (%array-displaced-from data)
396 (cons (make-weak-pointer array)
397 (purge (%array-displaced-from data)))))
398 ;; Remove old backpointer, if any.
399 (let ((old-data (%array-data-vector array)))
400 (when (and (neq data old-data) (array-header-p old-data))
401 (setf (%array-displaced-from old-data)
402 (purge (%array-displaced-from old-data)))))))
404 ;;; Widetag is the widetag of the underlying vector,
405 ;;; it'll be the same as the resulting array widetag only for simple vectors
406 (defun %make-array (dimensions widetag n-bits
407 &key
408 element-type
409 (initial-element nil initial-element-p)
410 (initial-contents nil initial-contents-p)
411 adjustable fill-pointer
412 displaced-to displaced-index-offset)
413 (declare (ignore element-type))
414 (binding* (((array-rank dimension-0)
415 (if (listp dimensions)
416 (values (length dimensions)
417 (if dimensions (car dimensions) 1))
418 (values 1 dimensions)))
419 ((initialize initial-data)
420 (validate-array-initargs initial-element-p initial-element
421 initial-contents-p initial-contents
422 displaced-to))
423 (simple (and (null fill-pointer)
424 (not adjustable)
425 (null displaced-to))))
426 (declare (type array-rank array-rank))
427 (declare (type index dimension-0))
428 (cond ((and displaced-index-offset (null displaced-to))
429 (error "can't specify :DISPLACED-INDEX-OFFSET without :DISPLACED-TO"))
430 ((and simple (= array-rank 1))
431 (let ((vector ; a (SIMPLE-ARRAY * (*))
432 (allocate-vector-with-widetag widetag dimension-0 n-bits)))
433 ;; presence of at most one :INITIAL-thing keyword was ensured above
434 (cond (initial-element-p
435 (fill vector initial-element))
436 (initial-contents-p
437 (let ((content-length (length initial-contents)))
438 (unless (= dimension-0 content-length)
439 (error "There are ~W elements in the :INITIAL-CONTENTS, but ~
440 the vector length is ~W."
441 content-length dimension-0)))
442 (replace vector initial-contents)))
443 vector))
444 ((and (arrayp displaced-to)
445 (/= (array-underlying-widetag displaced-to) widetag))
446 (error "Array element type of :DISPLACED-TO array does not match specified element type"))
448 ;; it's non-simple or multidimensional, or both.
449 (when fill-pointer
450 (unless (= array-rank 1)
451 (error "Only vectors can have fill pointers."))
452 (when (and (integerp fill-pointer) (> fill-pointer dimension-0))
453 ;; FIXME: should be TYPE-ERROR?
454 (error "invalid fill-pointer ~W" fill-pointer)))
455 (let* ((total-size
456 (if (consp dimensions)
457 (the index (reduce (lambda (a b) (* a (the index b)))
458 dimensions))
459 ;; () is considered to have dimension-0 = 1.
460 ;; It avoids the REDUCE lambda being called with no args.
461 dimension-0))
462 (data (or displaced-to
463 (data-vector-from-inits
464 dimensions total-size nil widetag n-bits
465 initialize initial-data)))
466 (array (make-array-header
467 (cond ((= array-rank 1)
468 (%complex-vector-widetag widetag))
469 (simple sb!vm:simple-array-widetag)
470 (t sb!vm:complex-array-widetag))
471 array-rank)))
472 (if fill-pointer
473 (setf (%array-fill-pointer-p array) t
474 (%array-fill-pointer array)
475 (if (eq fill-pointer t) dimension-0 fill-pointer))
476 (setf (%array-fill-pointer-p array) nil
477 (%array-fill-pointer array) total-size))
478 (setf (%array-available-elements array) total-size)
479 ;; Terrible name for this slot - we displace to the
480 ;; target array's header, if any, not the "ultimate"
481 ;; vector in the chain of displacements.
482 (setf (%array-data-vector array) data)
483 (setf (%array-displaced-from array) nil)
484 (cond (displaced-to
485 (let ((offset (or displaced-index-offset 0)))
486 (when (> (+ offset total-size)
487 (array-total-size displaced-to))
488 (error "~S doesn't have enough elements." displaced-to))
489 (setf (%array-displacement array) offset)
490 (setf (%array-displaced-p array) t)
491 (%save-displaced-array-backpointer array data)))
493 (setf (%array-displaced-p array) nil)))
494 (if (listp dimensions)
495 (let ((dims dimensions)) ; avoid "prevents use of assertion"
496 (dotimes (axis array-rank)
497 (setf (%array-dimension array axis) (pop dims))))
498 (setf (%array-dimension array 0) dimension-0))
499 array)))))
501 (defun make-array (dimensions &rest args
502 &key (element-type t)
503 initial-element initial-contents
504 adjustable
505 fill-pointer
506 displaced-to
507 displaced-index-offset)
508 (declare (ignore initial-element
509 initial-contents adjustable
510 fill-pointer displaced-to displaced-index-offset))
511 (declare (explicit-check))
512 (multiple-value-bind (widetag n-bits) (%vector-widetag-and-n-bits element-type)
513 (apply #'%make-array dimensions widetag n-bits args)))
515 (defun make-static-vector (length &key
516 (element-type '(unsigned-byte 8))
517 (initial-contents nil initial-contents-p)
518 (initial-element nil initial-element-p))
519 #!+sb-doc
520 "Allocate vector of LENGTH elements in static space. Only allocation
521 of specialized arrays is supported."
522 ;; STEP 1: check inputs fully
524 ;; This way of doing explicit checks before the vector is allocated
525 ;; is expensive, but probably worth the trouble as once we've allocated
526 ;; the vector we have no way to get rid of it anymore...
527 (when (eq t (upgraded-array-element-type element-type))
528 (error "Static arrays of type ~S not supported."
529 element-type))
530 (validate-array-initargs initial-element-p initial-element
531 initial-contents-p initial-contents nil) ; for effect
532 (when initial-contents-p
533 (unless (= length (length initial-contents))
534 (error "There are ~W elements in the :INITIAL-CONTENTS, but the ~
535 vector length is ~W."
536 (length initial-contents)
537 length))
538 (unless (every (lambda (x) (typep x element-type)) initial-contents)
539 (error ":INITIAL-CONTENTS contains elements not of type ~S."
540 element-type)))
541 (when initial-element-p
542 (unless (typep initial-element element-type)
543 (error ":INITIAL-ELEMENT ~S is not of type ~S."
544 initial-element element-type)))
545 ;; STEP 2
547 ;; Allocate and possibly initialize the vector.
548 (multiple-value-bind (type n-bits)
549 (%vector-widetag-and-n-bits element-type)
550 (let ((vector
551 (allocate-static-vector type length
552 (ceiling (* length n-bits)
553 sb!vm:n-word-bits))))
554 (cond (initial-element-p
555 (fill vector initial-element))
556 (initial-contents-p
557 (replace vector initial-contents))
559 vector)))))
561 ;;; DATA-VECTOR-FROM-INITS returns a simple vector that has the
562 ;;; specified array characteristics. Dimensions is only used to pass
563 ;;; to FILL-DATA-VECTOR for error checking on the structure of
564 ;;; initial-contents.
565 (defun data-vector-from-inits (dimensions total-size
566 element-type widetag n-bits
567 initialize initial-data)
568 ;; FIXME: element-type can be NIL when widetag is non-nil,
569 ;; and FILL will check the type, although the error will be not as nice.
570 ;; (cond (typep initial-element element-type)
571 ;; (error "~S cannot be used to initialize an array of type ~S."
572 ;; initial-element element-type))
573 (let ((data (if widetag
574 (allocate-vector-with-widetag widetag total-size n-bits)
575 (make-array total-size :element-type element-type))))
576 (ecase initialize
577 (:initial-element
578 (fill (the vector data) initial-data))
579 (:initial-contents
580 ;; DIMENSIONS can be supplied as a list or integer now
581 (dx-let ((list-of-dims (list dimensions))) ; ok if already a list
582 (fill-data-vector data
583 (if (listp dimensions) dimensions list-of-dims)
584 initial-data)))
585 ((nil)))
586 data))
588 (defun vector (&rest objects)
589 #!+sb-doc
590 "Construct a SIMPLE-VECTOR from the given objects."
591 (let ((v (make-array (length objects))))
592 (do-rest-arg ((x i) objects 0 v)
593 (setf (aref v i) x))))
596 ;;;; accessor/setter functions
598 ;;; Dispatch to an optimized routine the data vector accessors for
599 ;;; each different specialized vector type. Do dispatching by looking
600 ;;; up the widetag in the array rather than with the typecases, which
601 ;;; as of 1.0.5 compiles to a naive sequence of linear TYPEPs. Also
602 ;;; provide separate versions where bounds checking has been moved
603 ;;; from the callee to the caller, since it's much cheaper to do once
604 ;;; the type information is available. Finally, for each of these
605 ;;; routines also provide a slow path, taken for arrays that are not
606 ;;; vectors or not simple.
607 (macrolet ((def (name table-name)
608 `(progn
609 (defglobal ,table-name (make-array ,(1+ sb!vm:widetag-mask)))
610 (declaim (type (simple-array function (,(1+ sb!vm:widetag-mask)))
611 ,table-name))
612 (defmacro ,name (array-var)
613 `(the function
614 (let ((tag 0))
615 (when (sb!vm::%other-pointer-p ,array-var)
616 (setf tag (%other-pointer-widetag ,array-var)))
617 (svref ,',table-name tag)))))))
618 (def !find-data-vector-setter %%data-vector-setters%%)
619 (def !find-data-vector-setter/check-bounds %%data-vector-setters/check-bounds%%)
620 ;; Used by DO-VECTOR-DATA -- which in turn appears in DOSEQUENCE expansion,
621 ;; meaning we can have post-build dependences on this.
622 (def %find-data-vector-reffer %%data-vector-reffers%%)
623 (def !find-data-vector-reffer/check-bounds %%data-vector-reffers/check-bounds%%))
625 ;;; Like DOVECTOR, but more magical -- can't use this on host.
626 (defmacro do-vector-data ((elt vector &optional result) &body body)
627 (multiple-value-bind (forms decls) (parse-body body nil)
628 (with-unique-names (index vec start end ref)
629 `(with-array-data ((,vec ,vector)
630 (,start)
631 (,end)
632 :check-fill-pointer t)
633 (let ((,ref (%find-data-vector-reffer ,vec)))
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 (defun vector-push-extend (new-element vector &optional min-extension)
1099 (declare (type (or null (and index (integer 1))) min-extension))
1100 (declare (explicit-check))
1101 (let ((fill-pointer (fill-pointer vector)))
1102 (when (= fill-pointer (%array-available-elements vector))
1103 (let ((min-extension
1104 (or min-extension
1105 (let ((length (length vector)))
1106 (min length
1107 (- array-dimension-limit length))))))
1108 (adjust-array vector (+ fill-pointer (max 1 min-extension)))))
1109 ;; disable bounds checking
1110 (locally (declare (optimize (safety 0)))
1111 (setf (aref vector fill-pointer) new-element))
1112 (setf (%array-fill-pointer vector) (1+ fill-pointer))
1113 fill-pointer))
1115 (defun vector-pop (array)
1116 #!+sb-doc
1117 "Decrease the fill pointer by 1 and return the element pointed to by the
1118 new fill pointer."
1119 (declare (explicit-check))
1120 (let ((fill-pointer (fill-pointer array)))
1121 (if (zerop fill-pointer)
1122 (error "There is nothing left to pop.")
1123 ;; disable bounds checking (and any fixnum test)
1124 (locally (declare (optimize (safety 0)))
1125 (aref array
1126 (setf (%array-fill-pointer array)
1127 (1- fill-pointer)))))))
1130 ;;;; ADJUST-ARRAY
1132 (defun adjust-array (array dimensions &key
1133 (element-type (array-element-type array) element-type-p)
1134 (initial-element nil initial-element-p)
1135 (initial-contents nil initial-contents-p)
1136 fill-pointer
1137 displaced-to displaced-index-offset)
1138 #!+sb-doc
1139 "Adjust ARRAY's dimensions to the given DIMENSIONS and stuff."
1140 (when (invalid-array-p array)
1141 (invalid-array-error array))
1142 (binding* ((dimensions (ensure-list dimensions))
1143 (array-rank (array-rank array))
1145 (unless (= (length dimensions) array-rank)
1146 (error "The number of dimensions not equal to rank of array.")))
1147 ((initialize initial-data)
1148 (validate-array-initargs initial-element-p initial-element
1149 initial-contents-p initial-contents
1150 displaced-to)))
1151 (cond ((and element-type-p
1152 (not (subtypep element-type (array-element-type array))))
1153 ;; This is weird. Should check upgraded type against actual
1154 ;; array element type I think. See lp#1331299. CLHS says that
1155 ;; "consequences are unspecified" so current behavior isn't wrong.
1156 (error "The new element type, ~S, is incompatible with old type."
1157 element-type))
1158 ((and fill-pointer (/= array-rank 1))
1159 (error "Only vectors can have fill pointers."))
1160 ((and fill-pointer (not (array-has-fill-pointer-p array)))
1161 ;; This case always struck me as odd. It seems like it might mean
1162 ;; that the user asks that the array gain a fill-pointer if it didn't
1163 ;; have one, yet CLHS is clear that the argument array must have a
1164 ;; fill-pointer or else signal a type-error.
1165 (fill-pointer-error array)))
1166 (cond (initial-contents-p
1167 ;; array former contents replaced by INITIAL-CONTENTS
1168 (let* ((array-size (apply #'* dimensions))
1169 (array-data (data-vector-from-inits
1170 dimensions array-size element-type nil nil
1171 initialize initial-data)))
1172 (cond ((adjustable-array-p array)
1173 (set-array-header array array-data array-size
1174 (get-new-fill-pointer array array-size
1175 fill-pointer)
1176 0 dimensions nil nil))
1177 ((array-header-p array)
1178 ;; simple multidimensional or single dimensional array
1179 (make-array dimensions
1180 :element-type element-type
1181 :initial-contents initial-contents))
1183 array-data))))
1184 (displaced-to
1185 ;; We already established that no INITIAL-CONTENTS was supplied.
1186 (unless (or (eql element-type (array-element-type displaced-to))
1187 (subtypep element-type (array-element-type displaced-to)))
1188 ;; See lp#1331299 again. Require exact match on upgraded type?
1189 (error "can't displace an array of type ~S into another of ~
1190 type ~S"
1191 element-type (array-element-type displaced-to)))
1192 (let ((displacement (or displaced-index-offset 0))
1193 (array-size (apply #'* dimensions)))
1194 (declare (fixnum displacement array-size))
1195 (if (< (the fixnum (array-total-size displaced-to))
1196 (the fixnum (+ displacement array-size)))
1197 (error "The :DISPLACED-TO array is too small."))
1198 (if (adjustable-array-p array)
1199 ;; None of the original contents appear in adjusted array.
1200 (set-array-header array displaced-to array-size
1201 (get-new-fill-pointer array array-size
1202 fill-pointer)
1203 displacement dimensions t nil)
1204 ;; simple multidimensional or single dimensional array
1205 (make-array dimensions
1206 :element-type element-type
1207 :displaced-to displaced-to
1208 :displaced-index-offset
1209 displaced-index-offset))))
1210 ((= array-rank 1)
1211 (let ((old-length (array-total-size array))
1212 (new-length (car dimensions))
1213 new-data)
1214 (declare (fixnum old-length new-length))
1215 (with-array-data ((old-data array) (old-start)
1216 (old-end old-length))
1217 (cond ((or (and (array-header-p array)
1218 (%array-displaced-p array))
1219 (< old-length new-length))
1220 (setf new-data
1221 (data-vector-from-inits
1222 dimensions new-length element-type
1223 (%other-pointer-widetag old-data) nil
1224 initialize initial-data))
1225 ;; Provide :END1 to avoid full call to LENGTH
1226 ;; inside REPLACE.
1227 (replace new-data old-data
1228 :end1 new-length
1229 :start2 old-start :end2 old-end))
1230 (t (setf new-data
1231 (shrink-vector old-data new-length))))
1232 (if (adjustable-array-p array)
1233 (set-array-header array new-data new-length
1234 (get-new-fill-pointer array new-length
1235 fill-pointer)
1236 0 dimensions nil nil)
1237 new-data))))
1239 (let ((old-length (%array-available-elements array))
1240 (new-length (apply #'* dimensions)))
1241 (declare (fixnum old-length new-length))
1242 (with-array-data ((old-data array) (old-start)
1243 (old-end old-length))
1244 (declare (ignore old-end))
1245 (let ((new-data (if (or (and (array-header-p array)
1246 (%array-displaced-p array))
1247 (> new-length old-length)
1248 (not (adjustable-array-p array)))
1249 (data-vector-from-inits
1250 dimensions new-length
1251 element-type
1252 (%other-pointer-widetag old-data) nil
1253 (if initial-element-p :initial-element)
1254 initial-element)
1255 old-data)))
1256 (if (or (zerop old-length) (zerop new-length))
1257 (when initial-element-p (fill new-data initial-element))
1258 (zap-array-data old-data (array-dimensions array)
1259 old-start
1260 new-data dimensions new-length
1261 element-type initial-element
1262 initial-element-p))
1263 (if (adjustable-array-p array)
1264 (set-array-header array new-data new-length
1265 nil 0 dimensions nil nil)
1266 (let ((new-array
1267 (make-array-header
1268 sb!vm:simple-array-widetag array-rank)))
1269 (set-array-header new-array new-data new-length
1270 nil 0 dimensions nil t))))))))))
1273 (defun get-new-fill-pointer (old-array new-array-size fill-pointer)
1274 (declare (fixnum new-array-size))
1275 (typecase fill-pointer
1276 (null
1277 ;; "The consequences are unspecified if array is adjusted to a
1278 ;; size smaller than its fill pointer ..."
1279 (when (array-has-fill-pointer-p old-array)
1280 (when (> (%array-fill-pointer old-array) new-array-size)
1281 (error "cannot ADJUST-ARRAY an array (~S) to a size (~S) that is ~
1282 smaller than its fill pointer (~S)"
1283 old-array new-array-size (fill-pointer old-array)))
1284 (%array-fill-pointer old-array)))
1285 ((eql t)
1286 new-array-size)
1287 (fixnum
1288 (when (> fill-pointer new-array-size)
1289 (error "can't supply a value for :FILL-POINTER (~S) that is larger ~
1290 than the new length of the vector (~S)"
1291 fill-pointer new-array-size))
1292 fill-pointer)))
1294 ;;; Destructively alter VECTOR, changing its length to NEW-LENGTH,
1295 ;;; which must be less than or equal to its current length. This can
1296 ;;; be called on vectors without a fill pointer but it is extremely
1297 ;;; dangerous to do so: shrinking the size of an object (as viewed by
1298 ;;; the gc) makes bounds checking unreliable in the face of interrupts
1299 ;;; or multi-threading. Call it only on provably local vectors.
1300 (defun %shrink-vector (vector new-length)
1301 (declare (vector vector))
1302 (unless (array-header-p vector)
1303 (macrolet ((frob (name &rest things)
1304 `(etypecase ,name
1305 ((simple-array nil (*)) (error 'nil-array-accessed-error))
1306 ,@(mapcar (lambda (thing)
1307 (destructuring-bind (type-spec fill-value)
1308 thing
1309 `(,type-spec
1310 (fill (truly-the ,type-spec ,name)
1311 ,fill-value
1312 :start new-length))))
1313 things))))
1314 ;; Set the 'tail' of the vector to the appropriate type of zero,
1315 ;; "because in some cases we'll scavenge larger areas in one go,
1316 ;; like groups of pages that had triggered the write barrier, or
1317 ;; the whole static space" according to jsnell.
1318 #.`(frob vector
1319 ,@(map 'list
1320 (lambda (saetp)
1321 `((simple-array ,(sb!vm:saetp-specifier saetp) (*))
1322 ,(if (or (eq (sb!vm:saetp-specifier saetp) 'character)
1323 #!+sb-unicode
1324 (eq (sb!vm:saetp-specifier saetp) 'base-char))
1325 *default-init-char-form*
1326 (sb!vm:saetp-initial-element-default saetp))))
1327 (remove-if-not
1328 #'sb!vm:saetp-specifier
1329 sb!vm:*specialized-array-element-type-properties*)))))
1330 ;; Only arrays have fill-pointers, but vectors have their length
1331 ;; parameter in the same place.
1332 (setf (%array-fill-pointer vector) new-length)
1333 vector)
1335 (defun shrink-vector (vector new-length)
1336 (declare (vector vector))
1337 (cond
1338 ((eq (length vector) new-length)
1339 vector)
1340 ((array-has-fill-pointer-p vector)
1341 (setf (%array-fill-pointer vector) new-length)
1342 vector)
1343 (t (subseq vector 0 new-length))))
1345 ;;; BIG THREAD SAFETY NOTE
1347 ;;; ADJUST-ARRAY/SET-ARRAY-HEADER, and its callees are very
1348 ;;; thread unsafe. They are nonatomic, and can mess with parallel
1349 ;;; code using the same arrays.
1351 ;;; A likely seeming fix is an additional level of indirection:
1352 ;;; ARRAY-HEADER -> ARRAY-INFO -> ... where ARRAY-HEADER would
1353 ;;; hold nothing but the pointer to ARRAY-INFO, and ARRAY-INFO
1354 ;;; would hold everything ARRAY-HEADER now holds. This allows
1355 ;;; consing up a new ARRAY-INFO and replacing it atomically in
1356 ;;; the ARRAY-HEADER.
1358 ;;; %WALK-DISPLACED-ARRAY-BACKPOINTERS is an especially nasty
1359 ;;; one: not only is it needed extremely rarely, which makes
1360 ;;; any thread safety bugs involving it look like rare random
1361 ;;; corruption, but because it walks the chain *upwards*, which
1362 ;;; may violate user expectations.
1364 ;;; Fill in array header with the provided information, and return the array.
1365 (defun set-array-header (array data length fill-pointer displacement dimensions
1366 displacedp newp)
1367 (labels ((%walk-displaced-array-backpointers (array new-length)
1368 (dolist (p (%array-displaced-from array))
1369 (let ((from (weak-pointer-value p)))
1370 (when (and from (eq array (%array-data-vector from)))
1371 (let ((requires (+ (%array-available-elements from)
1372 (%array-displacement from))))
1373 (unless (>= new-length requires)
1374 ;; ANSI sayeth (ADJUST-ARRAY dictionary entry):
1376 ;; "If A is displaced to B, the consequences are unspecified if B is
1377 ;; adjusted in such a way that it no longer has enough elements to
1378 ;; satisfy A.
1380 ;; since we're hanging on a weak pointer here, we can't signal an
1381 ;; error right now: the array that we're looking at might be
1382 ;; garbage. Instead, we set all dimensions to zero so that next
1383 ;; safe access to the displaced array will trap. Additionally, we
1384 ;; save the original dimensions, so we can signal a more
1385 ;; understandable error when the time comes.
1386 (%walk-displaced-array-backpointers from 0)
1387 (setf (%array-fill-pointer from) 0
1388 (%array-available-elements from) 0
1389 (%array-displaced-p from) (array-dimensions array))
1390 (dotimes (i (%array-rank from))
1391 (setf (%array-dimension from i) 0)))))))))
1392 (if newp
1393 (setf (%array-displaced-from array) nil)
1394 (%walk-displaced-array-backpointers array length))
1395 (when displacedp
1396 (%save-displaced-array-backpointer array data))
1397 (setf (%array-data-vector array) data)
1398 (setf (%array-available-elements array) length)
1399 (cond (fill-pointer
1400 (setf (%array-fill-pointer array) fill-pointer)
1401 (setf (%array-fill-pointer-p array) t))
1403 (setf (%array-fill-pointer array) length)
1404 (setf (%array-fill-pointer-p array) nil)))
1405 (setf (%array-displacement array) displacement)
1406 (if (listp dimensions)
1407 (dotimes (axis (array-rank array))
1408 (declare (type index axis))
1409 (setf (%array-dimension array axis) (pop dimensions)))
1410 (setf (%array-dimension array 0) dimensions))
1411 (setf (%array-displaced-p array) displacedp)
1412 array))
1414 ;;; User visible extension
1415 (declaim (ftype (function (array) (values (simple-array * (*)) &optional))
1416 array-storage-vector))
1417 (defun array-storage-vector (array)
1418 #!+sb-doc
1419 "Returns the underlying storage vector of ARRAY, which must be a non-displaced array.
1421 In SBCL, if ARRAY is a of type \(SIMPLE-ARRAY * \(*)), it is its own storage
1422 vector. Multidimensional arrays, arrays with fill pointers, and adjustable
1423 arrays have an underlying storage vector with the same ARRAY-ELEMENT-TYPE as
1424 ARRAY, which this function returns.
1426 Important note: the underlying vector is an implementation detail. Even though
1427 this function exposes it, changes in the implementation may cause this
1428 function to be removed without further warning."
1429 ;; KLUDGE: Without TRULY-THE the system is not smart enough to figure out that
1430 ;; the return value is always of the known type.
1431 (truly-the (simple-array * (*))
1432 (cond ((not (array-header-p array))
1433 array)
1434 ((%array-displaced-p array)
1435 (error "~S cannot be used with displaced arrays. Use ~S instead."
1436 'array-storage-vector 'array-displacement))
1438 (%array-data-vector array)))))
1441 ;;;; ZAP-ARRAY-DATA for ADJUST-ARRAY
1443 ;;; This does the grinding work for ADJUST-ARRAY. It zaps the data
1444 ;;; from the OLD-DATA in an arrangement specified by the OLD-DIMS to
1445 ;;; the NEW-DATA in an arrangement specified by the NEW-DIMS. OFFSET
1446 ;;; is a displaced offset to be added to computed indices of OLD-DATA.
1447 (defun zap-array-data (old-data old-dims offset new-data new-dims new-length
1448 element-type initial-element initial-element-p)
1449 (declare (list old-dims new-dims)
1450 (fixnum new-length))
1451 ;; OLD-DIMS comes from array-dimensions, which returns a fresh list
1452 ;; at least in SBCL.
1453 ;; NEW-DIMS comes from the user.
1454 (setf old-dims (nreverse old-dims)
1455 new-dims (reverse new-dims))
1456 (cond ((eq old-data new-data)
1457 ;; NEW-LENGTH, ELEMENT-TYPE, INITIAL-ELEMENT, and
1458 ;; INITIAL-ELEMENT-P are used when OLD-DATA and NEW-DATA are
1459 ;; EQ; in this case, a temporary must be used and filled
1460 ;; appropriately. specified initial-element.
1461 ;; FIXME: transforming this TYPEP to someting a bit faster
1462 ;; would be a win...
1463 (unless (or (not initial-element-p)
1464 (typep initial-element element-type))
1465 (error "~S can't be used to initialize an array of type ~S."
1466 initial-element element-type))
1467 (let ((temp (if initial-element-p
1468 (make-array new-length :initial-element initial-element)
1469 (make-array new-length))))
1470 (declare (simple-vector temp))
1471 (zap-array-data-aux old-data old-dims offset temp new-dims)
1472 (dotimes (i new-length)
1473 (setf (aref new-data i) (aref temp i)))
1474 ;; Kill the temporary vector to prevent garbage retention.
1475 (%shrink-vector temp 0)))
1477 ;; When OLD-DATA and NEW-DATA are not EQ, NEW-DATA has
1478 ;; already been filled with any
1479 (zap-array-data-aux old-data old-dims offset new-data new-dims))))
1481 (defun zap-array-data-aux (old-data old-dims offset new-data new-dims)
1482 (declare (fixnum offset))
1483 (let ((limits (mapcar (lambda (x y)
1484 (declare (fixnum x y))
1485 (1- (the fixnum (min x y))))
1486 old-dims new-dims)))
1487 (macrolet ((bump-index-list (index limits)
1488 `(do ((subscripts ,index (cdr subscripts))
1489 (limits ,limits (cdr limits)))
1490 ((null subscripts) :eof)
1491 (cond ((< (the fixnum (car subscripts))
1492 (the fixnum (car limits)))
1493 (rplaca subscripts
1494 (1+ (the fixnum (car subscripts))))
1495 (return ,index))
1496 (t (rplaca subscripts 0))))))
1497 (do ((index (make-list (length old-dims) :initial-element 0)
1498 (bump-index-list index limits)))
1499 ((eq index :eof))
1500 (setf (aref new-data (row-major-index-from-dims index new-dims))
1501 (aref old-data
1502 (+ (the fixnum (row-major-index-from-dims index old-dims))
1503 offset)))))))
1505 ;;; Figure out the row-major-order index of an array reference from a
1506 ;;; list of subscripts and a list of dimensions. This is for internal
1507 ;;; calls only, and the subscripts and dim-list variables are assumed
1508 ;;; to be reversed from what the user supplied.
1509 (defun row-major-index-from-dims (rev-subscripts rev-dim-list)
1510 (do ((rev-subscripts rev-subscripts (cdr rev-subscripts))
1511 (rev-dim-list rev-dim-list (cdr rev-dim-list))
1512 (chunk-size 1)
1513 (result 0))
1514 ((null rev-dim-list) result)
1515 (declare (fixnum chunk-size result))
1516 (setq result (+ result
1517 (the fixnum (* (the fixnum (car rev-subscripts))
1518 chunk-size))))
1519 (setq chunk-size (* chunk-size (the fixnum (car rev-dim-list))))))
1521 ;;;; some bit stuff
1523 (defun bit-array-same-dimensions-p (array1 array2)
1524 (declare (type (array bit) array1 array2))
1525 (and (= (array-rank array1)
1526 (array-rank array2))
1527 (dotimes (index (array-rank array1) t)
1528 (when (/= (array-dimension array1 index)
1529 (array-dimension array2 index))
1530 (return nil)))))
1532 (defun pick-result-array (result-bit-array bit-array-1)
1533 (case result-bit-array
1534 ((t) bit-array-1)
1535 ((nil) (make-array (array-dimensions bit-array-1)
1536 :element-type 'bit
1537 :initial-element 0))
1539 (unless (bit-array-same-dimensions-p bit-array-1
1540 result-bit-array)
1541 (error "~S and ~S don't have the same dimensions."
1542 bit-array-1 result-bit-array))
1543 result-bit-array)))
1545 (defmacro def-bit-array-op (name function)
1546 `(defun ,name (bit-array-1 bit-array-2 &optional result-bit-array)
1547 #!+sb-doc
1548 ,(format nil
1549 "Perform a bit-wise ~A on the elements of BIT-ARRAY-1 and ~
1550 BIT-ARRAY-2,~% putting the results in RESULT-BIT-ARRAY. ~
1551 If RESULT-BIT-ARRAY is T,~% BIT-ARRAY-1 is used. If ~
1552 RESULT-BIT-ARRAY is NIL or omitted, a new array is~% created. ~
1553 All the arrays must have the same rank and dimensions."
1554 (symbol-name function))
1555 (declare (type (array bit) bit-array-1 bit-array-2)
1556 (type (or (array bit) (member t nil)) result-bit-array))
1557 (unless (bit-array-same-dimensions-p bit-array-1 bit-array-2)
1558 (error "~S and ~S don't have the same dimensions."
1559 bit-array-1 bit-array-2))
1560 (let ((result-bit-array (pick-result-array result-bit-array bit-array-1)))
1561 (if (and (simple-bit-vector-p bit-array-1)
1562 (simple-bit-vector-p bit-array-2)
1563 (simple-bit-vector-p result-bit-array))
1564 (locally (declare (optimize (speed 3) (safety 0)))
1565 (,name bit-array-1 bit-array-2 result-bit-array))
1566 (with-array-data ((data1 bit-array-1) (start1) (end1))
1567 (declare (ignore end1))
1568 (with-array-data ((data2 bit-array-2) (start2) (end2))
1569 (declare (ignore end2))
1570 (with-array-data ((data3 result-bit-array) (start3) (end3))
1571 (do ((index-1 start1 (1+ index-1))
1572 (index-2 start2 (1+ index-2))
1573 (index-3 start3 (1+ index-3)))
1574 ((>= index-3 end3) result-bit-array)
1575 (declare (type index index-1 index-2 index-3))
1576 (setf (sbit data3 index-3)
1577 (logand (,function (sbit data1 index-1)
1578 (sbit data2 index-2))
1579 1))))))))))
1581 (def-bit-array-op bit-and logand)
1582 (def-bit-array-op bit-ior logior)
1583 (def-bit-array-op bit-xor logxor)
1584 (def-bit-array-op bit-eqv logeqv)
1585 (def-bit-array-op bit-nand lognand)
1586 (def-bit-array-op bit-nor lognor)
1587 (def-bit-array-op bit-andc1 logandc1)
1588 (def-bit-array-op bit-andc2 logandc2)
1589 (def-bit-array-op bit-orc1 logorc1)
1590 (def-bit-array-op bit-orc2 logorc2)
1592 (defun bit-not (bit-array &optional result-bit-array)
1593 #!+sb-doc
1594 "Performs a bit-wise logical NOT on the elements of BIT-ARRAY,
1595 putting the results in RESULT-BIT-ARRAY. If RESULT-BIT-ARRAY is T,
1596 BIT-ARRAY is used. If RESULT-BIT-ARRAY is NIL or omitted, a new array is
1597 created. Both arrays must have the same rank and dimensions."
1598 (declare (type (array bit) bit-array)
1599 (type (or (array bit) (member t nil)) result-bit-array))
1600 (let ((result-bit-array (pick-result-array result-bit-array bit-array)))
1601 (if (and (simple-bit-vector-p bit-array)
1602 (simple-bit-vector-p result-bit-array))
1603 (locally (declare (optimize (speed 3) (safety 0)))
1604 (bit-not bit-array result-bit-array))
1605 (with-array-data ((src bit-array) (src-start) (src-end))
1606 (declare (ignore src-end))
1607 (with-array-data ((dst result-bit-array) (dst-start) (dst-end))
1608 (do ((src-index src-start (1+ src-index))
1609 (dst-index dst-start (1+ dst-index)))
1610 ((>= dst-index dst-end) result-bit-array)
1611 (declare (type index src-index dst-index))
1612 (setf (sbit dst dst-index)
1613 (logxor (sbit src src-index) 1))))))))
1615 ;;;; array type dispatching
1617 ;;; Given DISPATCH-FOO as the DISPATCH-NAME argument (unevaluated),
1618 ;;; defines the functions
1620 ;;; DISPATCH-FOO/SIMPLE-BASE-STRING
1621 ;;; DISPATCH-FOO/SIMPLE-CHARACTER-STRING
1622 ;;; DISPATCH-FOO/SIMPLE-ARRAY-SINGLE-FLOAT
1623 ;;; ...
1625 ;;; PARAMS are the function parameters in the definition of each
1626 ;;; specializer function. The array being specialized must be the
1627 ;;; first parameter in PARAMS. A type declaration for this parameter
1628 ;;; is automatically inserted into the body of each function.
1630 ;;; The dispatch table %%FOO-FUNS%% is defined and populated by these
1631 ;;; functions. The table is padded by the function
1632 ;;; HAIRY-FOO-DISPATCH-ERROR, also defined by DEFINE-ARRAY-DISPATCH.
1634 ;;; Finally, the DISPATCH-FOO macro is defined which does the actual
1635 ;;; dispatching when called. It expects arguments that match PARAMS.
1637 (defmacro !define-array-dispatch (dispatch-name params &body body)
1638 (let ((table-name (symbolicate "%%" dispatch-name "-FUNS%%"))
1639 (error-name (symbolicate "HAIRY-" dispatch-name "-ERROR")))
1640 `(progn
1641 (eval-when (:compile-toplevel :load-toplevel :execute)
1642 (defun ,error-name (&rest args)
1643 (error 'type-error
1644 :datum (first args)
1645 :expected-type '(simple-array * (*)))))
1646 (!defglobal ,table-name (make-array ,(1+ sb!vm:widetag-mask)
1647 :initial-element #',error-name))
1648 ,@(loop for info across sb!vm:*specialized-array-element-type-properties*
1649 for typecode = (sb!vm:saetp-typecode info)
1650 for specifier = (sb!vm:saetp-specifier info)
1651 for primitive-type-name = (sb!vm:saetp-primitive-type-name info)
1652 collect (let ((fun-name (symbolicate (string dispatch-name)
1653 "/" primitive-type-name)))
1654 `(progn
1655 (defun ,fun-name ,params
1656 (declare (type (simple-array ,specifier (*))
1657 ,(first params)))
1658 ,@body)
1659 (setf (svref ,table-name ,typecode) #',fun-name))))
1660 (defmacro ,dispatch-name (&rest args)
1661 (check-type (first args) symbol)
1662 (let ((tag (gensym "TAG")))
1663 `(funcall
1664 (the function
1665 (let ((,tag 0))
1666 (when (sb!vm::%other-pointer-p ,(first args))
1667 (setf ,tag (%other-pointer-widetag ,(first args))))
1668 (svref ,',table-name ,tag)))
1669 ,@args))))))