1 ;;;; functions to implement arrays
3 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
12 (in-package "SB!IMPL")
15 (declaim (inline fill-pointer array-has-fill-pointer-p adjustable-array-p
18 ;;;; miscellaneous accessor functions
20 ;;; These functions are only needed by the interpreter, 'cause the
21 ;;; compiler inlines them.
22 (macrolet ((def (name)
26 (defun (setf ,name
) (value array
)
27 (setf (,name array
) value
)))))
28 (def %array-fill-pointer
)
29 (def %array-fill-pointer-p
)
30 (def %array-available-elements
)
31 (def %array-data-vector
)
32 (def %array-displacement
)
33 (def %array-displaced-p
))
35 (defun %array-rank
(array)
38 (defun %array-dimension
(array axis
)
39 (%array-dimension array axis
))
41 (defun %set-array-dimension
(array axis value
)
42 (%set-array-dimension array axis value
))
44 (defun %check-bound
(array bound index
)
45 (declare (type index bound
)
47 (%check-bound array bound index
))
49 (defun %with-array-data
/fp
(array start end
)
50 (%with-array-data-macro array start end
:check-bounds t
:check-fill-pointer t
))
52 (defun %with-array-data
(array start end
)
53 (%with-array-data-macro array start end
:check-bounds t
:check-fill-pointer nil
))
55 (defun %data-vector-and-index
(array index
)
56 (if (array-header-p array
)
57 (multiple-value-bind (vector index
)
58 (%with-array-data array index nil
)
59 (values vector index
))
60 (values array index
)))
63 (eval-when (:compile-toplevel
:execute
)
64 (sb!xc
:defmacro pick-vector-type
(type &rest specs
)
65 `(cond ,@(mapcar (lambda (spec)
66 `(,(if (eq (car spec
) t
)
68 `(subtypep ,type
',(car spec
)))
72 ;;; These functions are used in the implementation of MAKE-ARRAY for
73 ;;; complex arrays. There are lots of transforms to simplify
74 ;;; MAKE-ARRAY for various easy cases, but not for all reasonable
75 ;;; cases, so e.g. as of sbcl-0.6.6 we still make full calls to
76 ;;; MAKE-ARRAY for any non-simple array. Thus, there's some value to
77 ;;; making this somewhat efficient, at least not doing full calls to
78 ;;; SUBTYPEP in the easy cases.
79 (defun %vector-widetag-and-n-bits
(type)
81 ;; Pick off some easy common cases.
83 ;; (Perhaps we should make a much more exhaustive table of easy
84 ;; common cases here. Or perhaps the effort would be better spent
85 ;; on smarter compiler transforms which do the calculation once
86 ;; and for all in any reasonable user programs.)
88 (values #.sb
!vm
:simple-vector-widetag
#.sb
!vm
:n-word-bits
))
89 ((base-char standard-char
#!-sb-unicode character
)
90 (values #.sb
!vm
:simple-base-string-widetag
#.sb
!vm
:n-byte-bits
))
93 (values #.sb
!vm
:simple-character-string-widetag
#.sb
!vm
:n-word-bits
))
95 (values #.sb
!vm
:simple-bit-vector-widetag
1))
96 ;; OK, we have to wade into SUBTYPEPing after all.
98 #.
`(pick-vector-type type
101 `(,(sb!vm
:saetp-specifier saetp
)
102 (values ,(sb!vm
:saetp-typecode saetp
)
103 ,(sb!vm
:saetp-n-bits saetp
))))
104 sb
!vm
:*specialized-array-element-type-properties
*)))))
106 (defun %complex-vector-widetag
(type)
108 ;; Pick off some easy common cases.
110 #.sb
!vm
:complex-vector-widetag
)
111 ((base-char #!-sb-unicode character
)
112 #.sb
!vm
:complex-base-string-widetag
)
115 #.sb
!vm
:complex-character-string-widetag
)
117 #.sb
!vm
:complex-vector-nil-widetag
)
119 #.sb
!vm
:complex-bit-vector-widetag
)
120 ;; OK, we have to wade into SUBTYPEPing after all.
122 (pick-vector-type type
123 (nil #.sb
!vm
:complex-vector-nil-widetag
)
125 (character #.sb
!vm
:complex-base-string-widetag
)
127 (base-char #.sb
!vm
:complex-base-string-widetag
)
129 (character #.sb
!vm
:complex-character-string-widetag
)
130 (bit #.sb
!vm
:complex-bit-vector-widetag
)
131 (t #.sb
!vm
:complex-vector-widetag
)))))
133 (defun make-array (dimensions &key
135 (initial-element nil initial-element-p
)
136 (initial-contents nil initial-contents-p
)
137 adjustable fill-pointer
138 displaced-to displaced-index-offset
)
139 (let* ((dimensions (if (listp dimensions
) dimensions
(list dimensions
)))
140 (array-rank (length (the list dimensions
)))
141 (simple (and (null fill-pointer
)
143 (null displaced-to
))))
144 (declare (fixnum array-rank
))
145 (when (and displaced-index-offset
(null displaced-to
))
146 (error "can't specify :DISPLACED-INDEX-OFFSET without :DISPLACED-TO"))
147 (when (and displaced-to
148 (arrayp displaced-to
)
149 (not (equal (array-element-type displaced-to
)
150 (upgraded-array-element-type element-type
))))
151 (error "Array element type of :DISPLACED-TO array does not match specified element type"))
152 (if (and simple
(= array-rank
1))
153 ;; it's a (SIMPLE-ARRAY * (*))
154 (multiple-value-bind (type n-bits
)
155 (%vector-widetag-and-n-bits element-type
)
156 (declare (type (unsigned-byte 8) type
)
157 (type (integer 0 256) n-bits
))
158 (let* ((length (car dimensions
))
159 (array (allocate-vector
163 (* (if (or (= type sb
!vm
:simple-base-string-widetag
)
166 sb
!vm
:simple-character-string-widetag
))
170 sb
!vm
:n-word-bits
))))
171 (declare (type index length
))
172 (when initial-element-p
173 (fill array initial-element
))
174 (when initial-contents-p
175 (when initial-element-p
176 (error "can't specify both :INITIAL-ELEMENT and ~
178 (unless (= length
(length initial-contents
))
179 (error "There are ~W elements in the :INITIAL-CONTENTS, but ~
180 the vector length is ~W."
181 (length initial-contents
)
183 (replace array initial-contents
))
185 ;; it's either a complex array or a multidimensional array.
186 (let* ((total-size (reduce #'* dimensions
))
187 (data (or displaced-to
188 (data-vector-from-inits
189 dimensions total-size element-type
190 initial-contents initial-contents-p
191 initial-element initial-element-p
)))
192 (array (make-array-header
193 (cond ((= array-rank
1)
194 (%complex-vector-widetag element-type
))
195 (simple sb
!vm
:simple-array-widetag
)
196 (t sb
!vm
:complex-array-widetag
))
199 (unless (= array-rank
1)
200 (error "Only vectors can have fill pointers."))
201 (let ((length (car dimensions
)))
202 (declare (fixnum length
))
203 (setf (%array-fill-pointer array
)
204 (cond ((eq fill-pointer t
)
207 (unless (and (fixnump fill-pointer
)
209 (<= fill-pointer length
))
210 ;; FIXME: should be TYPE-ERROR?
211 (error "invalid fill-pointer ~W"
214 (setf (%array-fill-pointer-p array
) t
))
216 (setf (%array-fill-pointer array
) total-size
)
217 (setf (%array-fill-pointer-p array
) nil
)))
218 (setf (%array-available-elements array
) total-size
)
219 (setf (%array-data-vector array
) data
)
221 (when (or initial-element-p initial-contents-p
)
222 (error "Neither :INITIAL-ELEMENT nor :INITIAL-CONTENTS ~
223 can be specified along with :DISPLACED-TO"))
224 (let ((offset (or displaced-index-offset
0)))
225 (when (> (+ offset total-size
)
226 (array-total-size displaced-to
))
227 (error "~S doesn't have enough elements." displaced-to
))
228 (setf (%array-displacement array
) offset
)
229 (setf (%array-displaced-p array
) t
)))
231 (setf (%array-displaced-p array
) nil
)))
233 (dolist (dim dimensions
)
234 (setf (%array-dimension array axis
) dim
)
238 (defun make-static-vector (length &key
239 (element-type '(unsigned-byte 8))
240 (initial-contents nil initial-contents-p
)
241 (initial-element nil initial-element-p
))
242 "Allocate vector of LENGTH elements in static space. Only allocation
243 of specialized arrays is supported."
244 ;; STEP 1: check inputs fully
246 ;; This way of doing explicit checks before the vector is allocated
247 ;; is expensive, but probably worth the trouble as once we've allocated
248 ;; the vector we have no way to get rid of it anymore...
249 (when (eq t
(upgraded-array-element-type element-type
))
250 (error "Static arrays of type ~S not supported."
252 (when initial-contents-p
253 (when initial-element-p
254 (error "can't specify both :INITIAL-ELEMENT and :INITIAL-CONTENTS"))
255 (unless (= length
(length initial-contents
))
256 (error "There are ~W elements in the :INITIAL-CONTENTS, but the ~
257 vector length is ~W."
258 (length initial-contents
)
260 (unless (every (lambda (x) (typep x element-type
)) initial-contents
)
261 (error ":INITIAL-CONTENTS contains elements not of type ~S."
263 (when initial-element-p
264 (unless (typep initial-element element-type
)
265 (error ":INITIAL-ELEMENT ~S is not of type ~S."
266 initial-element element-type
)))
269 ;; Allocate and possibly initialize the vector.
270 (multiple-value-bind (type n-bits
)
271 (sb!impl
::%vector-widetag-and-n-bits element-type
)
273 (allocate-static-vector type length
274 (ceiling (* length n-bits
)
275 sb
!vm
:n-word-bits
))))
276 (cond (initial-element-p
277 (fill vector initial-element
))
279 (replace vector initial-contents
))
283 ;;; DATA-VECTOR-FROM-INITS returns a simple vector that has the
284 ;;; specified array characteristics. Dimensions is only used to pass
285 ;;; to FILL-DATA-VECTOR for error checking on the structure of
286 ;;; initial-contents.
287 (defun data-vector-from-inits (dimensions total-size element-type
288 initial-contents initial-contents-p
289 initial-element initial-element-p
)
290 (when (and initial-contents-p initial-element-p
)
291 (error "cannot supply both :INITIAL-CONTENTS and :INITIAL-ELEMENT to
292 either MAKE-ARRAY or ADJUST-ARRAY."))
293 (let ((data (if initial-element-p
294 (make-array total-size
295 :element-type element-type
296 :initial-element initial-element
)
297 (make-array total-size
298 :element-type element-type
))))
299 (cond (initial-element-p
300 (unless (simple-vector-p data
)
301 (unless (typep initial-element element-type
)
302 (error "~S cannot be used to initialize an array of type ~S."
303 initial-element element-type
))
304 (fill (the vector data
) initial-element
)))
306 (fill-data-vector data dimensions initial-contents
)))
309 (defun vector (&rest objects
)
311 "Construct a SIMPLE-VECTOR from the given objects."
312 (coerce (the list objects
) 'simple-vector
))
315 ;;;; accessor/setter functions
317 ;;; Dispatch to an optimized routine the data vector accessors for
318 ;;; each different specialized vector type. Do dispatching by looking
319 ;;; up the widetag in the array rather than with the typecases, which
320 ;;; as of 1.0.5 compiles to a naive sequence of linear TYPEPs. Also
321 ;;; provide separate versions where bounds checking has been moved
322 ;;; from the callee to the caller, since it's much cheaper to do once
323 ;;; the type information is available. Finally, for each of these
324 ;;; routines also provide a slow path, taken for arrays that are not
325 ;;; vectors or not simple.
326 (macrolet ((def (name table-name
)
329 (defmacro ,name
(array-var)
333 #.
(ecase sb
!c
:*backend-byte-order
*
335 (- sb
!vm
:other-pointer-lowtag
))
337 (- (1- sb
!vm
:n-word-bytes
) sb
!vm
:other-pointer-lowtag
)))))
338 ;; WIDETAG-OF needs extra code to handle LIST and
339 ;; FUNCTION lowtags. We're only dispatching on
340 ;; other pointers, so let's do the lowtag
341 ;; extraction manually.
342 (when (sb!vm
::%other-pointer-p
,array-var
)
344 (sb!sys
:sap-ref-8
(int-sap (get-lisp-obj-address ,array-var
))
346 ;; SYMBOL-GLOBAL-VALUE is a performance hack
347 ;; for threaded builds.
348 (svref (sb!vm
::symbol-global-value
',',table-name
) tag
)))))))
349 (def !find-data-vector-setter
*data-vector-setters
*)
350 (def !find-data-vector-setter
/check-bounds
*data-vector-setters
/check-bounds
*)
351 (def !find-data-vector-reffer
*data-vector-reffers
*)
352 (def !find-data-vector-reffer
/check-bounds
*data-vector-reffers
/check-bounds
*))
354 (macrolet ((%ref
(accessor-getter extra-params
)
355 `(funcall (,accessor-getter array
) array index
,@extra-params
))
356 (define (accessor-name slow-accessor-name accessor-getter
357 extra-params check-bounds
)
359 (defun ,accessor-name
(array index
,@extra-params
)
360 (declare (optimize speed
361 ;; (SAFETY 0) is ok. All calls to
362 ;; these functions are generated by
363 ;; the compiler, so argument count
364 ;; checking isn't needed. Type checking
365 ;; is done implicitly via the widetag
368 (%ref
,accessor-getter
,extra-params
))
369 (defun ,slow-accessor-name
(array index
,@extra-params
)
370 (declare (optimize speed
(safety 0)))
371 (if (not (%array-displaced-p array
))
372 ;; The reasonably quick path of non-displaced complex
374 (let ((array (%array-data-vector array
)))
375 (%ref
,accessor-getter
,extra-params
))
376 ;; The real slow path.
380 (declare (optimize (speed 1) (safety 1)))
381 (,@check-bounds index
)))
384 (declare (ignore end
))
385 (,accessor-name vector index
,@extra-params
)))))))
386 (define hairy-data-vector-ref slow-hairy-data-vector-ref
387 !find-data-vector-reffer
389 (define hairy-data-vector-set slow-hairy-data-vector-set
390 !find-data-vector-setter
392 (define hairy-data-vector-ref
/check-bounds
393 slow-hairy-data-vector-ref
/check-bounds
394 !find-data-vector-reffer
/check-bounds
395 nil
(%check-bound array
(array-dimension array
0)))
396 (define hairy-data-vector-set
/check-bounds
397 slow-hairy-data-vector-set
/check-bounds
398 !find-data-vector-setter
/check-bounds
399 (new-value) (%check-bound array
(array-dimension array
0))))
401 (defun hairy-ref-error (array index
&optional new-value
)
402 (declare (ignore index new-value
))
405 :expected-type
'vector
))
407 ;;; Populate the dispatch tables.
408 (macrolet ((define-reffer (saetp check-form
)
409 (let* ((type (sb!vm
:saetp-specifier saetp
))
410 (atype `(simple-array ,type
(*))))
411 `(named-lambda optimized-data-vector-ref
(vector index
)
412 (declare (optimize speed
(safety 0)))
413 (data-vector-ref (the ,atype vector
)
415 (declare (optimize (safety 1)))
417 (,@check-form index
)))))))
418 (define-setter (saetp check-form
)
419 (let* ((type (sb!vm
:saetp-specifier saetp
))
420 (atype `(simple-array ,type
(*))))
421 `(named-lambda optimized-data-vector-set
(vector index new-value
)
422 (declare (optimize speed
(safety 0)))
423 (data-vector-set (the ,atype vector
)
425 (declare (optimize (safety 1)))
427 (,@check-form index
)))
429 ;; SPEED 1 needed to avoid the compiler
430 ;; from downgrading the type check to
432 (declare (optimize (speed 1)
434 (the ,type new-value
)))
435 ;; For specialized arrays, the return from
436 ;; data-vector-set would have to be reboxed to be a
437 ;; (Lisp) return value; instead, we use the
438 ;; already-boxed value as the return.
440 (define-reffers (symbol deffer check-form slow-path
)
442 (setf ,symbol
(make-array sb
!vm
::widetag-mask
443 :initial-element
#'hairy-ref-error
))
444 ,@(loop for widetag in
'(sb!vm
:complex-vector-widetag
445 sb
!vm
:complex-vector-nil-widetag
446 sb
!vm
:complex-bit-vector-widetag
447 #!+sb-unicode sb
!vm
:complex-character-string-widetag
448 sb
!vm
:complex-base-string-widetag
449 sb
!vm
:simple-array-widetag
450 sb
!vm
:complex-array-widetag
)
451 collect
`(setf (svref ,symbol
,widetag
) ,slow-path
))
452 ,@(loop for saetp across sb
!vm
:*specialized-array-element-type-properties
*
453 for widetag
= (sb!vm
:saetp-typecode saetp
)
454 collect
`(setf (svref ,symbol
,widetag
)
455 (,deffer
,saetp
,check-form
))))))
456 (defun !hairy-data-vector-reffer-init
()
457 (define-reffers *data-vector-reffers
* define-reffer
459 #'slow-hairy-data-vector-ref
)
460 (define-reffers *data-vector-setters
* define-setter
462 #'slow-hairy-data-vector-set
)
463 (define-reffers *data-vector-reffers
/check-bounds
* define-reffer
464 (%check-bound vector
(length vector
))
465 #'slow-hairy-data-vector-ref
/check-bounds
)
466 (define-reffers *data-vector-setters
/check-bounds
* define-setter
467 (%check-bound vector
(length vector
))
468 #'slow-hairy-data-vector-set
/check-bounds
)))
470 ;;; (Ordinary DATA-VECTOR-REF usage compiles into a vop, but
471 ;;; DATA-VECTOR-REF is also FOLDABLE, and this ordinary function
472 ;;; definition is needed for the compiler to use in constant folding.)
473 (defun data-vector-ref (array index
)
474 (hairy-data-vector-ref array index
))
476 (defun data-vector-ref-with-offset (array index offset
)
477 (hairy-data-vector-ref array
(+ index offset
)))
479 (declaim (ftype (function (array integer integer
&optional t
) nil
) signal-invalid-array-index-error
))
480 (defun invalid-array-index-error (array index bound
&optional axis
)
481 (error 'invalid-array-index-error
485 :expected-type
`(integer 0 (,bound
))))
487 ;;; SUBSCRIPTS has a dynamic-extent list structure and is destroyed
488 (defun %array-row-major-index
(array subscripts
489 &optional
(invalid-index-error-p t
))
490 (declare (array array
)
492 (let ((rank (array-rank array
)))
493 (unless (= rank
(length subscripts
))
494 (error "wrong number of subscripts, ~W, for array of rank ~W"
495 (length subscripts
) rank
))
496 (if (array-header-p array
)
497 (do ((subs (nreverse subscripts
) (cdr subs
))
498 (axis (1- (array-rank array
)) (1- axis
))
502 (declare (list subs
) (fixnum axis chunk-size result
))
503 (let ((index (car subs
))
504 (dim (%array-dimension array axis
)))
505 (declare (fixnum dim
))
506 (unless (and (fixnump index
) (< -
1 index dim
))
507 (if invalid-index-error-p
508 (invalid-array-index-error array index dim axis
)
509 (return-from %array-row-major-index nil
)))
510 (incf result
(* chunk-size
(the fixnum index
)))
511 (setf chunk-size
(* chunk-size dim
))))
512 (let ((index (first subscripts
))
513 (length (length (the (simple-array * (*)) array
))))
514 (unless (and (fixnump index
) (< -
1 index length
))
515 (if invalid-index-error-p
516 (invalid-array-index-error array index length
)
517 (return-from %array-row-major-index nil
)))
520 (defun array-in-bounds-p (array &rest subscripts
)
522 "Return T if the SUBSCIPTS are in bounds for the ARRAY, NIL otherwise."
523 (if (%array-row-major-index array subscripts nil
)
526 (defun array-row-major-index (array &rest subscripts
)
527 (declare (truly-dynamic-extent subscripts
))
528 (%array-row-major-index array subscripts
))
530 (defun aref (array &rest subscripts
)
532 "Return the element of the ARRAY specified by the SUBSCRIPTS."
533 (declare (truly-dynamic-extent subscripts
))
534 (row-major-aref array
(%array-row-major-index array subscripts
)))
536 (defun %aset
(array &rest stuff
)
537 (declare (truly-dynamic-extent stuff
))
538 (let ((subscripts (butlast stuff
))
539 (new-value (car (last stuff
))))
540 (setf (row-major-aref array
(%array-row-major-index array subscripts
))
543 ;;; FIXME: What's supposed to happen with functions
544 ;;; like AREF when we (DEFUN (SETF FOO) ..) when
545 ;;; DEFSETF FOO is also defined? It seems as though the logical
546 ;;; thing to do would be to nuke the macro definition for (SETF FOO)
547 ;;; and replace it with the (SETF FOO) function, issuing a warning,
548 ;;; just as for ordinary functions
549 ;;; * (LISP-IMPLEMENTATION-VERSION)
550 ;;; "18a+ release x86-linux 2.4.7 6 November 1998 cvs"
551 ;;; * (DEFMACRO ZOO (X) `(+ ,X ,X))
553 ;;; * (DEFUN ZOO (X) (* 3 X))
554 ;;; Warning: ZOO previously defined as a macro.
556 ;;; But that doesn't seem to be what happens in CMU CL.
558 ;;; KLUDGE: this is probably because ANSI, in its wisdom (CLHS
559 ;;; 5.1.2.5) requires implementations to support
560 ;;; (SETF (APPLY #'AREF ...) ...)
561 ;;; [and also #'BIT and #'SBIT]. Yes, this is terrifying, and it's
562 ;;; also terrifying that this sequence of definitions causes it to
565 ;;; Also, it would be nice to make DESCRIBE FOO tell whether a symbol
566 ;;; has a setf expansion and/or a setf function defined.
568 #!-sb-fluid
(declaim (inline (setf aref
)))
569 (defun (setf aref
) (new-value array
&rest subscripts
)
570 (declare (truly-dynamic-extent subscripts
))
571 (declare (type array array
))
572 (setf (row-major-aref array
(%array-row-major-index array subscripts
))
575 (defun row-major-aref (array index
)
577 "Return the element of array corressponding to the row-major index. This is
579 (declare (optimize (safety 1)))
580 (row-major-aref array index
))
582 (defun %set-row-major-aref
(array index new-value
)
583 (declare (optimize (safety 1)))
584 (setf (row-major-aref array index
) new-value
))
586 (defun svref (simple-vector index
)
588 "Return the INDEX'th element of the given Simple-Vector."
589 (declare (optimize (safety 1)))
590 (aref simple-vector index
))
592 (defun %svset
(simple-vector index new
)
593 (declare (optimize (safety 1)))
594 (setf (aref simple-vector index
) new
))
596 (defun bit (bit-array &rest subscripts
)
598 "Return the bit from the BIT-ARRAY at the specified SUBSCRIPTS."
599 (declare (type (array bit
) bit-array
) (optimize (safety 1)))
600 (row-major-aref bit-array
(%array-row-major-index bit-array subscripts
)))
602 (defun %bitset
(bit-array &rest stuff
)
603 (declare (type (array bit
) bit-array
) (optimize (safety 1)))
604 (let ((subscripts (butlast stuff
))
605 (new-value (car (last stuff
))))
606 (setf (row-major-aref bit-array
607 (%array-row-major-index bit-array subscripts
))
610 #!-sb-fluid
(declaim (inline (setf bit
)))
611 (defun (setf bit
) (new-value bit-array
&rest subscripts
)
612 (declare (type (array bit
) bit-array
) (optimize (safety 1)))
613 (setf (row-major-aref bit-array
614 (%array-row-major-index bit-array subscripts
))
617 (defun sbit (simple-bit-array &rest subscripts
)
619 "Return the bit from SIMPLE-BIT-ARRAY at the specified SUBSCRIPTS."
620 (declare (type (simple-array bit
) simple-bit-array
) (optimize (safety 1)))
621 (row-major-aref simple-bit-array
622 (%array-row-major-index simple-bit-array subscripts
)))
624 ;;; KLUDGE: Not all these things (%SET-ROW-MAJOR-AREF, %SET-FILL-POINTER,
625 ;;; %SET-FDEFINITION, %SCHARSET, %SBITSET..) seem to deserve separate names.
626 ;;; Could we just DEFUN (SETF SBIT) etc. and get rid of the non-ANSI names?
628 (defun %sbitset
(simple-bit-array &rest stuff
)
629 (declare (type (simple-array bit
) simple-bit-array
) (optimize (safety 1)))
630 (let ((subscripts (butlast stuff
))
631 (new-value (car (last stuff
))))
632 (setf (row-major-aref simple-bit-array
633 (%array-row-major-index simple-bit-array subscripts
))
636 #!-sb-fluid
(declaim (inline (setf sbit
)))
637 (defun (setf sbit
) (new-value bit-array
&rest subscripts
)
638 (declare (type (simple-array bit
) bit-array
) (optimize (safety 1)))
639 (setf (row-major-aref bit-array
640 (%array-row-major-index bit-array subscripts
))
643 ;;;; miscellaneous array properties
645 (defun array-element-type (array)
647 "Return the type of the elements of the array"
648 (let ((widetag (widetag-of array
)))
649 (macrolet ((pick-element-type (&rest stuff
)
650 `(cond ,@(mapcar (lambda (stuff)
652 (let ((item (car stuff
)))
661 `(= widetag
,item
))))
664 #.
`(pick-element-type
667 `(,(if (sb!vm
:saetp-complex-typecode saetp
)
668 (list (sb!vm
:saetp-typecode saetp
)
669 (sb!vm
:saetp-complex-typecode saetp
))
670 (sb!vm
:saetp-typecode saetp
))
671 ',(sb!vm
:saetp-specifier saetp
)))
672 sb
!vm
:*specialized-array-element-type-properties
*)
673 ((sb!vm
:simple-array-widetag
674 sb
!vm
:complex-vector-widetag
675 sb
!vm
:complex-array-widetag
)
676 (with-array-data ((array array
) (start) (end))
677 (declare (ignore start end
))
678 (array-element-type array
)))
680 (error 'type-error
:datum array
:expected-type
'array
))))))
682 (defun array-rank (array)
684 "Return the number of dimensions of ARRAY."
685 (if (array-header-p array
)
689 (defun array-dimension (array axis-number
)
691 "Return the length of dimension AXIS-NUMBER of ARRAY."
692 (declare (array array
) (type index axis-number
))
693 (cond ((not (array-header-p array
))
694 (unless (= axis-number
0)
695 (error "Vector axis is not zero: ~S" axis-number
))
696 (length (the (simple-array * (*)) array
)))
697 ((>= axis-number
(%array-rank array
))
698 (error "Axis number ~W is too big; ~S only has ~D dimension~:P."
699 axis-number array
(%array-rank array
)))
701 ;; ANSI sayeth (ADJUST-ARRAY dictionary entry):
703 ;; "If A is displaced to B, the consequences are
704 ;; unspecified if B is adjusted in such a way that it no
705 ;; longer has enough elements to satisfy A.
707 ;; In situations where this matters we should be doing a
708 ;; bounds-check, which in turn uses ARRAY-DIMENSION -- so
709 ;; this seems like a good place to signal an error.
710 (multiple-value-bind (target offset
) (array-displacement array
)
712 (> (array-total-size array
)
713 (- (array-total-size target
) offset
)))
714 (error 'displaced-to-array-too-small-error
715 :format-control
"~@<The displaced-to array is too small. ~S ~
716 elements after offset required, ~S available.~:@>"
717 :format-arguments
(list (array-total-size array
)
718 (- (array-total-size target
) offset
))))
719 (%array-dimension array axis-number
)))))
721 (defun array-dimensions (array)
723 "Return a list whose elements are the dimensions of the array"
724 (declare (array array
))
725 (if (array-header-p array
)
726 (do ((results nil
(cons (array-dimension array index
) results
))
727 (index (1- (array-rank array
)) (1- index
)))
728 ((minusp index
) results
))
729 (list (array-dimension array
0))))
731 (defun array-total-size (array)
733 "Return the total number of elements in the Array."
734 (declare (array array
))
735 (if (array-header-p array
)
736 (%array-available-elements array
)
737 (length (the vector array
))))
739 (defun array-displacement (array)
741 "Return the values of :DISPLACED-TO and :DISPLACED-INDEX-offset
742 options to MAKE-ARRAY, or NIL and 0 if not a displaced array."
743 (declare (type array array
))
744 (if (and (array-header-p array
) ; if unsimple and
745 (%array-displaced-p array
)) ; displaced
746 (values (%array-data-vector array
) (%array-displacement array
))
749 (defun adjustable-array-p (array)
751 "Return T if (ADJUST-ARRAY ARRAY...) would return an array identical
752 to the argument, this happens for complex arrays."
753 (declare (array array
))
754 ;; Note that this appears not to be a fundamental limitation.
755 ;; non-vector SIMPLE-ARRAYs are in fact capable of being adjusted,
756 ;; but in practice we test using ADJUSTABLE-ARRAY-P in ADJUST-ARRAY.
757 ;; -- CSR, 2004-03-01.
758 (not (typep array
'simple-array
)))
760 ;;;; fill pointer frobbing stuff
762 (defun array-has-fill-pointer-p (array)
764 "Return T if the given ARRAY has a fill pointer, or NIL otherwise."
765 (declare (array array
))
766 (and (array-header-p array
) (%array-fill-pointer-p array
)))
768 (defun fill-pointer-error (vector arg
)
770 (aver (array-has-fill-pointer-p vector
))
771 (let ((max (%array-available-elements vector
)))
772 (error 'simple-type-error
774 :expected-type
(list 'integer
0 max
)
775 :format-control
"The new fill pointer, ~S, is larger than the length of the vector (~S.)"
776 :format-arguments
(list arg max
))))
778 (error 'simple-type-error
780 :expected-type
'(and vector
(satisfies array-has-fill-pointer-p
))
781 :format-control
"~S is not an array with a fill pointer."
782 :format-arguments
(list vector
)))))
784 (defun fill-pointer (vector)
786 "Return the FILL-POINTER of the given VECTOR."
787 (if (array-has-fill-pointer-p vector
)
788 (%array-fill-pointer vector
)
789 (fill-pointer-error vector nil
)))
791 (defun %set-fill-pointer
(vector new
)
793 (fill-pointer-error vector x
)))
794 (if (array-has-fill-pointer-p vector
)
795 (if (> new
(%array-available-elements vector
))
797 (setf (%array-fill-pointer vector
) new
))
800 ;;; FIXME: It'd probably make sense to use a MACROLET to share the
801 ;;; guts of VECTOR-PUSH between VECTOR-PUSH-EXTEND. Such a macro
802 ;;; should probably be based on the VECTOR-PUSH-EXTEND code (which is
803 ;;; new ca. sbcl-0.7.0) rather than the VECTOR-PUSH code (which dates
805 (defun vector-push (new-el array
)
807 "Attempt to set the element of ARRAY designated by its fill pointer
808 to NEW-EL, and increment the fill pointer by one. If the fill pointer is
809 too large, NIL is returned, otherwise the index of the pushed element is
811 (declare (vector array
))
812 (let ((fill-pointer (fill-pointer array
)))
813 (declare (fixnum fill-pointer
))
814 (cond ((= fill-pointer
(%array-available-elements array
))
817 (locally (declare (optimize (safety 0)))
818 (setf (aref array fill-pointer
) new-el
))
819 (setf (%array-fill-pointer array
) (1+ fill-pointer
))
822 (defun vector-push-extend (new-element
826 (let ((length (length vector
)))
828 (- array-dimension-limit length
)))))
829 (declare (vector vector
) (fixnum min-extension
))
830 (let ((fill-pointer (fill-pointer vector
)))
831 (declare (fixnum fill-pointer
))
832 (when (= fill-pointer
(%array-available-elements vector
))
833 (adjust-array vector
(+ fill-pointer
(max 1 min-extension
))))
834 ;; disable bounds checking
835 (locally (declare (optimize (safety 0)))
836 (setf (aref vector fill-pointer
) new-element
))
837 (setf (%array-fill-pointer vector
) (1+ fill-pointer
))
840 (defun vector-pop (array)
842 "Decrease the fill pointer by 1 and return the element pointed to by the
844 (declare (vector array
))
845 (let ((fill-pointer (fill-pointer array
)))
846 (declare (fixnum fill-pointer
))
847 (if (zerop fill-pointer
)
848 (error "There is nothing left to pop.")
849 ;; disable bounds checking (and any fixnum test)
850 (locally (declare (optimize (safety 0)))
852 (setf (%array-fill-pointer array
)
853 (1- fill-pointer
)))))))
858 (defun adjust-array (array dimensions
&key
859 (element-type (array-element-type array
))
860 (initial-element nil initial-element-p
)
861 (initial-contents nil initial-contents-p
)
863 displaced-to displaced-index-offset
)
865 "Adjust ARRAY's dimensions to the given DIMENSIONS and stuff."
866 (let ((dimensions (if (listp dimensions
) dimensions
(list dimensions
))))
867 (cond ((/= (the fixnum
(length (the list dimensions
)))
868 (the fixnum
(array-rank array
)))
869 (error "The number of dimensions not equal to rank of array."))
870 ((not (subtypep element-type
(array-element-type array
)))
871 (error "The new element type, ~S, is incompatible with old type."
873 ((and fill-pointer
(not (array-has-fill-pointer-p array
)))
876 :expected-type
'(satisfies array-has-fill-pointer-p
))))
877 (let ((array-rank (length (the list dimensions
))))
878 (declare (fixnum array-rank
))
879 (unless (= array-rank
1)
881 (error "Only vectors can have fill pointers.")))
882 (cond (initial-contents-p
883 ;; array former contents replaced by INITIAL-CONTENTS
884 (if (or initial-element-p displaced-to
)
885 (error "INITIAL-CONTENTS may not be specified with ~
886 the :INITIAL-ELEMENT or :DISPLACED-TO option."))
887 (let* ((array-size (apply #'* dimensions
))
888 (array-data (data-vector-from-inits
889 dimensions array-size element-type
890 initial-contents initial-contents-p
891 initial-element initial-element-p
)))
892 (if (adjustable-array-p array
)
893 (set-array-header array array-data array-size
894 (get-new-fill-pointer array array-size
897 (if (array-header-p array
)
898 ;; simple multidimensional or single dimensional array
899 (make-array dimensions
900 :element-type element-type
901 :initial-contents initial-contents
)
904 ;; We already established that no INITIAL-CONTENTS was supplied.
905 (when initial-element
906 (error "The :INITIAL-ELEMENT option may not be specified ~
907 with :DISPLACED-TO."))
908 (unless (subtypep element-type
(array-element-type displaced-to
))
909 (error "can't displace an array of type ~S into another of ~
911 element-type
(array-element-type displaced-to
)))
912 (let ((displacement (or displaced-index-offset
0))
913 (array-size (apply #'* dimensions
)))
914 (declare (fixnum displacement array-size
))
915 (if (< (the fixnum
(array-total-size displaced-to
))
916 (the fixnum
(+ displacement array-size
)))
917 (error "The :DISPLACED-TO array is too small."))
918 (if (adjustable-array-p array
)
919 ;; None of the original contents appear in adjusted array.
920 (set-array-header array displaced-to array-size
921 (get-new-fill-pointer array array-size
923 displacement dimensions t
)
924 ;; simple multidimensional or single dimensional array
925 (make-array dimensions
926 :element-type element-type
927 :displaced-to displaced-to
928 :displaced-index-offset
929 displaced-index-offset
))))
931 (let ((old-length (array-total-size array
))
932 (new-length (car dimensions
))
934 (declare (fixnum old-length new-length
))
935 (with-array-data ((old-data array
) (old-start)
936 (old-end old-length
))
937 (cond ((or (and (array-header-p array
)
938 (%array-displaced-p array
))
939 (< old-length new-length
))
941 (data-vector-from-inits
942 dimensions new-length element-type
943 initial-contents initial-contents-p
944 initial-element initial-element-p
))
945 (replace new-data old-data
946 :start2 old-start
:end2 old-end
))
948 (shrink-vector old-data new-length
))))
949 (if (adjustable-array-p array
)
950 (set-array-header array new-data new-length
951 (get-new-fill-pointer array new-length
956 (let ((old-length (%array-available-elements array
))
957 (new-length (apply #'* dimensions
)))
958 (declare (fixnum old-length new-length
))
959 (with-array-data ((old-data array
) (old-start)
960 (old-end old-length
))
961 (declare (ignore old-end
))
962 (let ((new-data (if (or (and (array-header-p array
)
963 (%array-displaced-p array
))
964 (> new-length old-length
))
965 (data-vector-from-inits
966 dimensions new-length
968 initial-element initial-element-p
)
970 (if (or (zerop old-length
) (zerop new-length
))
971 (when initial-element-p
(fill new-data initial-element
))
972 (zap-array-data old-data
(array-dimensions array
)
974 new-data dimensions new-length
975 element-type initial-element
977 (if (adjustable-array-p array
)
978 (set-array-header array new-data new-length
979 nil
0 dimensions nil
)
982 sb
!vm
:simple-array-widetag array-rank
)))
983 (set-array-header new-array new-data new-length
984 nil
0 dimensions nil
)))))))))))
987 (defun get-new-fill-pointer (old-array new-array-size fill-pointer
)
988 (cond ((not fill-pointer
)
989 (when (array-has-fill-pointer-p old-array
)
990 (when (> (%array-fill-pointer old-array
) new-array-size
)
991 (error "cannot ADJUST-ARRAY an array (~S) to a size (~S) that is ~
992 smaller than its fill pointer (~S)"
993 old-array new-array-size
(fill-pointer old-array
)))
994 (%array-fill-pointer old-array
)))
995 ((not (array-has-fill-pointer-p old-array
))
996 (error "cannot supply a non-NIL value (~S) for :FILL-POINTER ~
997 in ADJUST-ARRAY unless the array (~S) was originally ~
998 created with a fill pointer"
1001 ((numberp fill-pointer
)
1002 (when (> fill-pointer new-array-size
)
1003 (error "can't supply a value for :FILL-POINTER (~S) that is larger ~
1004 than the new length of the vector (~S)"
1005 fill-pointer new-array-size
))
1007 ((eq fill-pointer t
)
1010 (error "bogus value for :FILL-POINTER in ADJUST-ARRAY: ~S"
1013 ;;; Destructively alter VECTOR, changing its length to NEW-LENGTH,
1014 ;;; which must be less than or equal to its current length. This can
1015 ;;; be called on vectors without a fill pointer but it is extremely
1016 ;;; dangerous to do so: shrinking the size of an object (as viewed by
1017 ;;; the gc) makes bounds checking unreliable in the face of interrupts
1018 ;;; or multi-threading. Call it only on provably local vectors.
1019 (defun %shrink-vector
(vector new-length
)
1020 (declare (vector vector
))
1021 (unless (array-header-p vector
)
1022 (macrolet ((frob (name &rest things
)
1024 ((simple-array nil
(*)) (error 'nil-array-accessed-error
))
1025 ,@(mapcar (lambda (thing)
1026 (destructuring-bind (type-spec fill-value
)
1029 (fill (truly-the ,type-spec
,name
)
1031 :start new-length
))))
1033 ;; Set the 'tail' of the vector to the appropriate type of zero,
1034 ;; "because in some cases we'll scavenge larger areas in one go,
1035 ;; like groups of pages that had triggered the write barrier, or
1036 ;; the whole static space" according to jsnell.
1040 `((simple-array ,(sb!vm
:saetp-specifier saetp
) (*))
1041 ,(if (or (eq (sb!vm
:saetp-specifier saetp
) 'character
)
1043 (eq (sb!vm
:saetp-specifier saetp
) 'base-char
))
1044 *default-init-char-form
*
1045 (sb!vm
:saetp-initial-element-default saetp
))))
1047 #'sb
!vm
:saetp-specifier
1048 sb
!vm
:*specialized-array-element-type-properties
*)))))
1049 ;; Only arrays have fill-pointers, but vectors have their length
1050 ;; parameter in the same place.
1051 (setf (%array-fill-pointer vector
) new-length
)
1054 (defun shrink-vector (vector new-length
)
1055 (declare (vector vector
))
1057 ((eq (length vector
) new-length
)
1059 ((array-has-fill-pointer-p vector
)
1060 (setf (%array-fill-pointer vector
) new-length
)
1062 (t (subseq vector
0 new-length
))))
1064 ;;; Fill in array header with the provided information, and return the array.
1065 (defun set-array-header (array data length fill-pointer displacement dimensions
1066 &optional displacedp
)
1067 (setf (%array-data-vector array
) data
)
1068 (setf (%array-available-elements array
) length
)
1070 (setf (%array-fill-pointer array
) fill-pointer
)
1071 (setf (%array-fill-pointer-p array
) t
))
1073 (setf (%array-fill-pointer array
) length
)
1074 (setf (%array-fill-pointer-p array
) nil
)))
1075 (setf (%array-displacement array
) displacement
)
1076 (if (listp dimensions
)
1077 (dotimes (axis (array-rank array
))
1078 (declare (type index axis
))
1079 (setf (%array-dimension array axis
) (pop dimensions
)))
1080 (setf (%array-dimension array
0) dimensions
))
1081 (setf (%array-displaced-p array
) displacedp
)
1084 ;;; User visible extension
1085 (declaim (ftype (function (array) (values (simple-array * (*)) &optional
))
1086 array-storage-vector
))
1087 (defun array-storage-vector (array)
1088 "Returns the underlying storage vector of ARRAY, which must be a non-displaced array.
1090 In SBCL, if ARRAY is a of type \(SIMPLE-ARRAY * \(*)), it is its own storage
1091 vector. Multidimensional arrays, arrays with fill pointers, and adjustable
1092 arrays have an underlying storage vector with the same ARRAY-ELEMENT-TYPE as
1093 ARRAY, which this function returns.
1095 Important note: the underlying vector is an implementation detail. Even though
1096 this function exposes it, changes in the implementation may cause this
1097 function to be removed without further warning."
1098 ;; KLUDGE: Without TRULY-THE the system is not smart enough to figure out that
1099 ;; the return value is always of the known type.
1100 (truly-the (simple-array * (*))
1101 (if (array-header-p array
)
1102 (if (%array-displaced-p array
)
1103 (error "~S cannot be used with displaced arrays. Use ~S instead."
1104 'array-storage-vector
'array-displacement
)
1105 (%array-data-vector array
))
1110 ;;; temporary vector for stable sorting vectors, allocated for each new thread
1111 (defvar *merge-sort-temp-vector
* (vector))
1112 (declaim (simple-vector *merge-sort-temp-vector
*))
1114 ;;;; ZAP-ARRAY-DATA for ADJUST-ARRAY
1116 ;;; a temporary to be used when OLD-DATA and NEW-DATA are EQ.
1117 ;;; KLUDGE: Boy, DYNAMIC-EXTENT would be nice. This is rebound
1118 ;;; to length zero array in each new thread.
1120 ;;; DX is probably a bad idea, because a with a big array it would
1121 ;;; be fairly easy to blow the stack.
1122 (defvar *zap-array-data-temp
* (vector))
1123 (declaim (simple-vector *zap-array-data-temp
*))
1125 (defun zap-array-data-temp (length initial-element initial-element-p
)
1126 (declare (fixnum length
))
1127 (let ((tmp *zap-array-data-temp
*))
1128 (declare (simple-vector tmp
))
1129 (cond ((> length
(length tmp
))
1130 (setf *zap-array-data-temp
*
1131 (if initial-element-p
1132 (make-array length
:initial-element initial-element
)
1133 (make-array length
))))
1135 (fill tmp initial-element
:end length
))
1139 ;;; This does the grinding work for ADJUST-ARRAY. It zaps the data
1140 ;;; from the OLD-DATA in an arrangement specified by the OLD-DIMS to
1141 ;;; the NEW-DATA in an arrangement specified by the NEW-DIMS. OFFSET
1142 ;;; is a displaced offset to be added to computed indices of OLD-DATA.
1143 (defun zap-array-data (old-data old-dims offset new-data new-dims new-length
1144 element-type initial-element initial-element-p
)
1145 (declare (list old-dims new-dims
))
1146 ;; OLD-DIMS comes from array-dimensions, which returns a fresh list
1147 ;; at least in SBCL.
1148 ;; NEW-DIMS comes from the user.
1149 (setf old-dims
(nreverse old-dims
)
1150 new-dims
(reverse new-dims
))
1151 (cond ((eq old-data new-data
)
1152 ;; NEW-LENGTH, ELEMENT-TYPE, INITIAL-ELEMENT, and
1153 ;; INITIAL-ELEMENT-P are used when OLD-DATA and NEW-DATA are
1154 ;; EQ; in this case, a temporary must be used and filled
1155 ;; appropriately. specified initial-element.
1156 (when initial-element-p
1157 ;; FIXME: transforming this TYPEP to someting a bit faster
1158 ;; would be a win...
1159 (unless (typep initial-element element-type
)
1160 (error "~S can't be used to initialize an array of type ~S."
1161 initial-element element-type
)))
1162 (let ((temp (zap-array-data-temp new-length
1163 initial-element initial-element-p
)))
1164 (declare (simple-vector temp
))
1165 (zap-array-data-aux old-data old-dims offset temp new-dims
)
1166 (dotimes (i new-length
)
1167 (setf (aref new-data i
) (aref temp i
)
1168 ;; zero out any garbage right away
1171 ;; When OLD-DATA and NEW-DATA are not EQ, NEW-DATA has
1172 ;; already been filled with any
1173 (zap-array-data-aux old-data old-dims offset new-data new-dims
))))
1175 (defun zap-array-data-aux (old-data old-dims offset new-data new-dims
)
1176 (declare (fixnum offset
))
1177 (let ((limits (mapcar (lambda (x y
)
1178 (declare (fixnum x y
))
1179 (1- (the fixnum
(min x y
))))
1180 old-dims new-dims
)))
1181 (macrolet ((bump-index-list (index limits
)
1182 `(do ((subscripts ,index
(cdr subscripts
))
1183 (limits ,limits
(cdr limits
)))
1184 ((null subscripts
) :eof
)
1185 (cond ((< (the fixnum
(car subscripts
))
1186 (the fixnum
(car limits
)))
1188 (1+ (the fixnum
(car subscripts
))))
1190 (t (rplaca subscripts
0))))))
1191 (do ((index (make-list (length old-dims
) :initial-element
0)
1192 (bump-index-list index limits
)))
1194 (setf (aref new-data
(row-major-index-from-dims index new-dims
))
1196 (+ (the fixnum
(row-major-index-from-dims index old-dims
))
1199 ;;; Figure out the row-major-order index of an array reference from a
1200 ;;; list of subscripts and a list of dimensions. This is for internal
1201 ;;; calls only, and the subscripts and dim-list variables are assumed
1202 ;;; to be reversed from what the user supplied.
1203 (defun row-major-index-from-dims (rev-subscripts rev-dim-list
)
1204 (do ((rev-subscripts rev-subscripts
(cdr rev-subscripts
))
1205 (rev-dim-list rev-dim-list
(cdr rev-dim-list
))
1208 ((null rev-dim-list
) result
)
1209 (declare (fixnum chunk-size result
))
1210 (setq result
(+ result
1211 (the fixnum
(* (the fixnum
(car rev-subscripts
))
1213 (setq chunk-size
(* chunk-size
(the fixnum
(car rev-dim-list
))))))
1217 (defun bit-array-same-dimensions-p (array1 array2
)
1218 (declare (type (array bit
) array1 array2
))
1219 (and (= (array-rank array1
)
1220 (array-rank array2
))
1221 (dotimes (index (array-rank array1
) t
)
1222 (when (/= (array-dimension array1 index
)
1223 (array-dimension array2 index
))
1226 (defun pick-result-array (result-bit-array bit-array-1
)
1227 (case result-bit-array
1229 ((nil) (make-array (array-dimensions bit-array-1
)
1231 :initial-element
0))
1233 (unless (bit-array-same-dimensions-p bit-array-1
1235 (error "~S and ~S don't have the same dimensions."
1236 bit-array-1 result-bit-array
))
1239 (defmacro def-bit-array-op
(name function
)
1240 `(defun ,name
(bit-array-1 bit-array-2
&optional result-bit-array
)
1243 "Perform a bit-wise ~A on the elements of BIT-ARRAY-1 and ~
1244 BIT-ARRAY-2,~% putting the results in RESULT-BIT-ARRAY. ~
1245 If RESULT-BIT-ARRAY is T,~% BIT-ARRAY-1 is used. If ~
1246 RESULT-BIT-ARRAY is NIL or omitted, a new array is~% created. ~
1247 All the arrays must have the same rank and dimensions."
1248 (symbol-name function
))
1249 (declare (type (array bit
) bit-array-1 bit-array-2
)
1250 (type (or (array bit
) (member t nil
)) result-bit-array
))
1251 (unless (bit-array-same-dimensions-p bit-array-1 bit-array-2
)
1252 (error "~S and ~S don't have the same dimensions."
1253 bit-array-1 bit-array-2
))
1254 (let ((result-bit-array (pick-result-array result-bit-array bit-array-1
)))
1255 (if (and (simple-bit-vector-p bit-array-1
)
1256 (simple-bit-vector-p bit-array-2
)
1257 (simple-bit-vector-p result-bit-array
))
1258 (locally (declare (optimize (speed 3) (safety 0)))
1259 (,name bit-array-1 bit-array-2 result-bit-array
))
1260 (with-array-data ((data1 bit-array-1
) (start1) (end1))
1261 (declare (ignore end1
))
1262 (with-array-data ((data2 bit-array-2
) (start2) (end2))
1263 (declare (ignore end2
))
1264 (with-array-data ((data3 result-bit-array
) (start3) (end3))
1265 (do ((index-1 start1
(1+ index-1
))
1266 (index-2 start2
(1+ index-2
))
1267 (index-3 start3
(1+ index-3
)))
1268 ((>= index-3 end3
) result-bit-array
)
1269 (declare (type index index-1 index-2 index-3
))
1270 (setf (sbit data3 index-3
)
1271 (logand (,function
(sbit data1 index-1
)
1272 (sbit data2 index-2
))
1275 (def-bit-array-op bit-and logand
)
1276 (def-bit-array-op bit-ior logior
)
1277 (def-bit-array-op bit-xor logxor
)
1278 (def-bit-array-op bit-eqv logeqv
)
1279 (def-bit-array-op bit-nand lognand
)
1280 (def-bit-array-op bit-nor lognor
)
1281 (def-bit-array-op bit-andc1 logandc1
)
1282 (def-bit-array-op bit-andc2 logandc2
)
1283 (def-bit-array-op bit-orc1 logorc1
)
1284 (def-bit-array-op bit-orc2 logorc2
)
1286 (defun bit-not (bit-array &optional result-bit-array
)
1288 "Performs a bit-wise logical NOT on the elements of BIT-ARRAY,
1289 putting the results in RESULT-BIT-ARRAY. If RESULT-BIT-ARRAY is T,
1290 BIT-ARRAY is used. If RESULT-BIT-ARRAY is NIL or omitted, a new array is
1291 created. Both arrays must have the same rank and dimensions."
1292 (declare (type (array bit
) bit-array
)
1293 (type (or (array bit
) (member t nil
)) result-bit-array
))
1294 (let ((result-bit-array (pick-result-array result-bit-array bit-array
)))
1295 (if (and (simple-bit-vector-p bit-array
)
1296 (simple-bit-vector-p result-bit-array
))
1297 (locally (declare (optimize (speed 3) (safety 0)))
1298 (bit-not bit-array result-bit-array
))
1299 (with-array-data ((src bit-array
) (src-start) (src-end))
1300 (declare (ignore src-end
))
1301 (with-array-data ((dst result-bit-array
) (dst-start) (dst-end))
1302 (do ((src-index src-start
(1+ src-index
))
1303 (dst-index dst-start
(1+ dst-index
)))
1304 ((>= dst-index dst-end
) result-bit-array
)
1305 (declare (type index src-index dst-index
))
1306 (setf (sbit dst dst-index
)
1307 (logxor (sbit src src-index
) 1))))))))