6d6ae8c6452f15d44da98e71bbd84ebd75fff73e
[sbcl.git] / src / code / array.lisp
1 ;;;; functions to implement arrays
2
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.
11
12 (in-package "SB!IMPL")
13
14 #!-sb-fluid
15 (declaim (inline fill-pointer array-has-fill-pointer-p adjustable-array-p
16                  array-displacement))
17 \f
18 ;;;; miscellaneous accessor functions
19
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
35 (defun %array-rank (array)
36   (%array-rank array))
37
38 (defun %array-dimension (array axis)
39   (%array-dimension array axis))
40
41 (defun %set-array-dimension (array axis value)
42   (%set-array-dimension array axis value))
43
44 (defun %check-bound (array bound index)
45   (declare (type index bound)
46            (fixnum index))
47   (%check-bound array bound index))
48
49 (defun %with-array-data (array start end)
50   (%with-array-data-macro array start end :fail-inline? t))
51
52 (defun %data-vector-and-index (array index)
53   (if (array-header-p array)
54       (multiple-value-bind (vector index)
55           (%with-array-data array index nil)
56         (values vector index))
57       (values array index)))
58
59 ;;; It'd waste space to expand copies of error handling in every
60 ;;; inline %WITH-ARRAY-DATA, so we have them call this function
61 ;;; instead. This is just a wrapper which is known never to return.
62 (defun failed-%with-array-data (array start end)
63   (declare (notinline %with-array-data))
64   (%with-array-data array start end)
65   (bug "called FAILED-%WITH-ARRAY-DATA with valid array parameters?"))
66 \f
67 ;;;; MAKE-ARRAY
68 (eval-when (:compile-toplevel :execute)
69   (sb!xc:defmacro pick-vector-type (type &rest specs)
70     `(cond ,@(mapcar (lambda (spec)
71                        `(,(if (eq (car spec) t)
72                               t
73                               `(subtypep ,type ',(car spec)))
74                          ,@(cdr spec)))
75                      specs))))
76
77 ;;; These functions are used in the implementation of MAKE-ARRAY for
78 ;;; complex arrays. There are lots of transforms to simplify
79 ;;; MAKE-ARRAY for various easy cases, but not for all reasonable
80 ;;; cases, so e.g. as of sbcl-0.6.6 we still make full calls to
81 ;;; MAKE-ARRAY for any non-simple array. Thus, there's some value to
82 ;;; making this somewhat efficient, at least not doing full calls to
83 ;;; SUBTYPEP in the easy cases.
84 (defun %vector-widetag-and-n-bits (type)
85   (case type
86     ;; Pick off some easy common cases.
87     ;;
88     ;; (Perhaps we should make a much more exhaustive table of easy
89     ;; common cases here. Or perhaps the effort would be better spent
90     ;; on smarter compiler transforms which do the calculation once
91     ;; and for all in any reasonable user programs.)
92     ((t)
93      (values #.sb!vm:simple-vector-widetag #.sb!vm:n-word-bits))
94     ((base-char standard-char #!-sb-unicode character)
95      (values #.sb!vm:simple-base-string-widetag #.sb!vm:n-byte-bits))
96     #!+sb-unicode
97     ((character)
98      (values #.sb!vm:simple-character-string-widetag #.sb!vm:n-word-bits))
99     ((bit)
100      (values #.sb!vm:simple-bit-vector-widetag 1))
101     ;; OK, we have to wade into SUBTYPEPing after all.
102     (t
103      #.`(pick-vector-type type
104          ,@(map 'list
105                 (lambda (saetp)
106                   `(,(sb!vm:saetp-specifier saetp)
107                     (values ,(sb!vm:saetp-typecode saetp)
108                             ,(sb!vm:saetp-n-bits saetp))))
109                 sb!vm:*specialized-array-element-type-properties*)))))
110
111 (defun %complex-vector-widetag (type)
112   (case type
113     ;; Pick off some easy common cases.
114     ((t)
115      #.sb!vm:complex-vector-widetag)
116     ((base-char #!-sb-unicode character)
117      #.sb!vm:complex-base-string-widetag)
118     #!+sb-unicode
119     ((character)
120      #.sb!vm:complex-character-string-widetag)
121     ((nil)
122      #.sb!vm:complex-vector-nil-widetag)
123     ((bit)
124      #.sb!vm:complex-bit-vector-widetag)
125     ;; OK, we have to wade into SUBTYPEPing after all.
126     (t
127      (pick-vector-type type
128        (nil #.sb!vm:complex-vector-nil-widetag)
129        #!-sb-unicode
130        (character #.sb!vm:complex-base-string-widetag)
131        #!+sb-unicode
132        (base-char #.sb!vm:complex-base-string-widetag)
133        #!+sb-unicode
134        (character #.sb!vm:complex-character-string-widetag)
135        (bit #.sb!vm:complex-bit-vector-widetag)
136        (t #.sb!vm:complex-vector-widetag)))))
137
138 (defun make-array (dimensions &key
139                               (element-type t)
140                               (initial-element nil initial-element-p)
141                               (initial-contents nil initial-contents-p)
142                               adjustable fill-pointer
143                               displaced-to displaced-index-offset)
144   (let* ((dimensions (if (listp dimensions) dimensions (list dimensions)))
145          (array-rank (length (the list dimensions)))
146          (simple (and (null fill-pointer)
147                       (not adjustable)
148                       (null displaced-to))))
149     (declare (fixnum array-rank))
150     (when (and displaced-index-offset (null displaced-to))
151       (error "can't specify :DISPLACED-INDEX-OFFSET without :DISPLACED-TO"))
152     (when (and displaced-to
153                (arrayp displaced-to)
154                (not (equal (array-element-type displaced-to)
155                            (upgraded-array-element-type element-type))))
156       (error "Array element type of :DISPLACED-TO array does not match specified element type"))
157     (if (and simple (= array-rank 1))
158         ;; it's a (SIMPLE-ARRAY * (*))
159         (multiple-value-bind (type n-bits)
160             (%vector-widetag-and-n-bits element-type)
161           (declare (type (unsigned-byte 8) type)
162                    (type (integer 0 256) n-bits))
163           (let* ((length (car dimensions))
164                  (array (allocate-vector
165                          type
166                          length
167                          (ceiling
168                           (* (if (or (= type sb!vm:simple-base-string-widetag)
169                                      #!+sb-unicode
170                                      (= type
171                                         sb!vm:simple-character-string-widetag))
172                                  (1+ length)
173                                  length)
174                              n-bits)
175                           sb!vm:n-word-bits))))
176             (declare (type index length))
177             (when initial-element-p
178               (fill array initial-element))
179             (when initial-contents-p
180               (when initial-element-p
181                 (error "can't specify both :INITIAL-ELEMENT and ~
182                        :INITIAL-CONTENTS"))
183               (unless (= length (length initial-contents))
184                 (error "There are ~W elements in the :INITIAL-CONTENTS, but ~
185                        the vector length is ~W."
186                        (length initial-contents)
187                        length))
188               (replace array initial-contents))
189             array))
190         ;; it's either a complex array or a multidimensional array.
191         (let* ((total-size (reduce #'* dimensions))
192                (data (or displaced-to
193                          (data-vector-from-inits
194                           dimensions total-size element-type
195                           initial-contents initial-contents-p
196                           initial-element initial-element-p)))
197                (array (make-array-header
198                        (cond ((= array-rank 1)
199                               (%complex-vector-widetag element-type))
200                              (simple sb!vm:simple-array-widetag)
201                              (t sb!vm:complex-array-widetag))
202                        array-rank)))
203           (cond (fill-pointer
204                  (unless (= array-rank 1)
205                    (error "Only vectors can have fill pointers."))
206                  (let ((length (car dimensions)))
207                    (declare (fixnum length))
208                    (setf (%array-fill-pointer array)
209                      (cond ((eq fill-pointer t)
210                             length)
211                            (t
212                             (unless (and (fixnump fill-pointer)
213                                          (>= fill-pointer 0)
214                                          (<= fill-pointer length))
215                               ;; FIXME: should be TYPE-ERROR?
216                               (error "invalid fill-pointer ~W"
217                                      fill-pointer))
218                             fill-pointer))))
219                  (setf (%array-fill-pointer-p array) t))
220                 (t
221                  (setf (%array-fill-pointer array) total-size)
222                  (setf (%array-fill-pointer-p array) nil)))
223           (setf (%array-available-elements array) total-size)
224           (setf (%array-data-vector array) data)
225           (cond (displaced-to
226                  (when (or initial-element-p initial-contents-p)
227                    (error "Neither :INITIAL-ELEMENT nor :INITIAL-CONTENTS ~
228                    can be specified along with :DISPLACED-TO"))
229                  (let ((offset (or displaced-index-offset 0)))
230                    (when (> (+ offset total-size)
231                             (array-total-size displaced-to))
232                      (error "~S doesn't have enough elements." displaced-to))
233                    (setf (%array-displacement array) offset)
234                    (setf (%array-displaced-p array) t)))
235                 (t
236                  (setf (%array-displaced-p array) nil)))
237           (let ((axis 0))
238             (dolist (dim dimensions)
239               (setf (%array-dimension array axis) dim)
240               (incf axis)))
241           array))))
242
243 (defun make-static-vector (length &key
244                            (element-type '(unsigned-byte 8))
245                            (initial-contents nil initial-contents-p)
246                            (initial-element nil initial-element-p))
247   "Allocate vector of LENGTH elements in static space. Only allocation
248 of specialized arrays is supported."
249   ;; STEP 1: check inputs fully
250   ;;
251   ;; This way of doing explicit checks before the vector is allocated
252   ;; is expensive, but probably worth the trouble as once we've allocated
253   ;; the vector we have no way to get rid of it anymore...
254   (when (eq t (upgraded-array-element-type element-type))
255     (error "Static arrays of type ~S not supported."
256            element-type))
257   (when initial-contents-p
258     (when initial-element-p
259       (error "can't specify both :INITIAL-ELEMENT and :INITIAL-CONTENTS"))
260     (unless (= length (length initial-contents))
261       (error "There are ~W elements in the :INITIAL-CONTENTS, but the ~
262               vector length is ~W."
263              (length initial-contents)
264              length))
265     (unless (every (lambda (x) (typep x element-type)) initial-contents)
266       (error ":INITIAL-CONTENTS contains elements not of type ~S."
267              element-type)))
268   (when initial-element-p
269     (unless (typep initial-element element-type)
270       (error ":INITIAL-ELEMENT ~S is not of type ~S."
271              initial-element element-type)))
272   ;; STEP 2
273   ;;
274   ;; Allocate and possibly initialize the vector.
275   (multiple-value-bind (type n-bits)
276       (sb!impl::%vector-widetag-and-n-bits element-type)
277     (let ((vector
278            (allocate-static-vector type length
279                                    (ceiling (* length n-bits)
280                                             sb!vm:n-word-bits))))
281       (cond (initial-element-p
282              (fill vector initial-element))
283             (initial-contents-p
284              (replace vector initial-contents))
285             (t
286              vector)))))
287
288 ;;; DATA-VECTOR-FROM-INITS returns a simple vector that has the
289 ;;; specified array characteristics. Dimensions is only used to pass
290 ;;; to FILL-DATA-VECTOR for error checking on the structure of
291 ;;; initial-contents.
292 (defun data-vector-from-inits (dimensions total-size element-type
293                                initial-contents initial-contents-p
294                                initial-element initial-element-p)
295   (when (and initial-contents-p initial-element-p)
296     (error "cannot supply both :INITIAL-CONTENTS and :INITIAL-ELEMENT to
297             either MAKE-ARRAY or ADJUST-ARRAY."))
298   (let ((data (if initial-element-p
299                   (make-array total-size
300                               :element-type element-type
301                               :initial-element initial-element)
302                   (make-array total-size
303                               :element-type element-type))))
304     (cond (initial-element-p
305            (unless (simple-vector-p data)
306              (unless (typep initial-element element-type)
307                (error "~S cannot be used to initialize an array of type ~S."
308                       initial-element element-type))
309              (fill (the vector data) initial-element)))
310           (initial-contents-p
311            (fill-data-vector data dimensions initial-contents)))
312     data))
313
314 (defun fill-data-vector (vector dimensions initial-contents)
315   (let ((index 0))
316     (labels ((frob (axis dims contents)
317                (cond ((null dims)
318                       (setf (aref vector index) contents)
319                       (incf index))
320                      (t
321                       (unless (typep contents 'sequence)
322                         (error "malformed :INITIAL-CONTENTS: ~S is not a ~
323                                 sequence, but ~W more layer~:P needed."
324                                contents
325                                (- (length dimensions) axis)))
326                       (unless (= (length contents) (car dims))
327                         (error "malformed :INITIAL-CONTENTS: Dimension of ~
328                                 axis ~W is ~W, but ~S is ~W long."
329                                axis (car dims) contents (length contents)))
330                       (if (listp contents)
331                           (dolist (content contents)
332                             (frob (1+ axis) (cdr dims) content))
333                           (dotimes (i (length contents))
334                             (frob (1+ axis) (cdr dims) (aref contents i))))))))
335       (frob 0 dimensions initial-contents))))
336
337 (defun vector (&rest objects)
338   #!+sb-doc
339   "Construct a SIMPLE-VECTOR from the given objects."
340   (coerce (the list objects) 'simple-vector))
341 \f
342 ;;;; accessor/setter functions
343 (defun hairy-data-vector-ref (array index)
344   (with-array-data ((vector array) (index index) (end))
345     (declare (ignore end))
346     (etypecase vector .
347                #.(map 'list
348                       (lambda (saetp)
349                         (let* ((type (sb!vm:saetp-specifier saetp))
350                                (atype `(simple-array ,type (*))))
351                           `(,atype
352                             (data-vector-ref (the ,atype vector) index))))
353                       (sort
354                        (copy-seq
355                         sb!vm:*specialized-array-element-type-properties*)
356                        #'> :key #'sb!vm:saetp-importance)))))
357
358 ;;; (Ordinary DATA-VECTOR-REF usage compiles into a vop, but
359 ;;; DATA-VECTOR-REF is also FOLDABLE, and this ordinary function
360 ;;; definition is needed for the compiler to use in constant folding.)
361 (defun data-vector-ref (array index)
362   (hairy-data-vector-ref array index))
363
364 (defun hairy-data-vector-set (array index new-value)
365   (with-array-data ((vector array) (index index) (end))
366     (declare (ignore end))
367     (etypecase vector .
368                #.(map 'list
369                       (lambda (saetp)
370                         (let* ((type (sb!vm:saetp-specifier saetp))
371                                (atype `(simple-array ,type (*))))
372                           `(,atype
373                             (data-vector-set (the ,atype vector) index
374                                              (the ,type new-value))
375                             ;; For specialized arrays, the return from
376                             ;; data-vector-set would have to be
377                             ;; reboxed to be a (Lisp) return value;
378                             ;; instead, we use the already-boxed value
379                             ;; as the return.
380                             new-value)))
381                       (sort
382                        (copy-seq
383                         sb!vm:*specialized-array-element-type-properties*)
384                        #'> :key #'sb!vm:saetp-importance)))))
385
386 ;;; SUBSCRIPTS has a dynamic-extent list structure and is destroyed
387 (defun %array-row-major-index (array subscripts
388                                      &optional (invalid-index-error-p t))
389   (declare (array array)
390            (list subscripts))
391   (let ((rank (array-rank array)))
392     (unless (= rank (length subscripts))
393       (error "wrong number of subscripts, ~W, for array of rank ~W"
394              (length subscripts) rank))
395     (if (array-header-p array)
396         (do ((subs (nreverse subscripts) (cdr subs))
397              (axis (1- (array-rank array)) (1- axis))
398              (chunk-size 1)
399              (result 0))
400             ((null subs) result)
401           (declare (list subs) (fixnum axis chunk-size result))
402           (let ((index (car subs))
403                 (dim (%array-dimension array axis)))
404             (declare (fixnum dim))
405             (unless (and (fixnump index) (< -1 index dim))
406               (if invalid-index-error-p
407                   (error 'simple-type-error
408                          :format-control "invalid index ~W~[~;~:; on axis ~:*~W~] in ~S"
409                          :format-arguments (list index axis array)
410                          :datum index
411                          :expected-type `(integer 0 (,dim)))
412                   (return-from %array-row-major-index nil)))
413             (incf result (* chunk-size (the fixnum index)))
414             (setf chunk-size (* chunk-size dim))))
415         (let ((index (first subscripts))
416               (length (length (the (simple-array * (*)) array))))
417           (unless (and (fixnump index) (< -1 index length))
418             (if invalid-index-error-p
419                 ;; FIXME: perhaps this should share a format-string
420                 ;; with INVALID-ARRAY-INDEX-ERROR or
421                 ;; INDEX-TOO-LARGE-ERROR?
422                 (error 'simple-type-error
423                        :format-control "invalid index ~W in ~S"
424                        :format-arguments (list index array)
425                        :datum index
426                        :expected-type `(integer 0 (,length)))
427                 (return-from %array-row-major-index nil)))
428           index))))
429
430 (defun array-in-bounds-p (array &rest subscripts)
431   #!+sb-doc
432   "Return T if the SUBSCIPTS are in bounds for the ARRAY, NIL otherwise."
433   (if (%array-row-major-index array subscripts nil)
434       t))
435
436 (defun array-row-major-index (array &rest subscripts)
437   (declare (dynamic-extent subscripts))
438   (%array-row-major-index array subscripts))
439
440 (defun aref (array &rest subscripts)
441   #!+sb-doc
442   "Return the element of the ARRAY specified by the SUBSCRIPTS."
443   (declare (dynamic-extent subscripts))
444   (row-major-aref array (%array-row-major-index array subscripts)))
445
446 (defun %aset (array &rest stuff)
447   (declare (dynamic-extent stuff))
448   (let ((subscripts (butlast stuff))
449         (new-value (car (last stuff))))
450     (setf (row-major-aref array (%array-row-major-index array subscripts))
451           new-value)))
452
453 ;;; FIXME: What's supposed to happen with functions
454 ;;; like AREF when we (DEFUN (SETF FOO) ..) when
455 ;;; DEFSETF FOO is also defined? It seems as though the logical
456 ;;; thing to do would be to nuke the macro definition for (SETF FOO)
457 ;;; and replace it with the (SETF FOO) function, issuing a warning,
458 ;;; just as for ordinary functions
459 ;;;  * (LISP-IMPLEMENTATION-VERSION)
460 ;;;  "18a+ release x86-linux 2.4.7 6 November 1998 cvs"
461 ;;;  * (DEFMACRO ZOO (X) `(+ ,X ,X))
462 ;;;  ZOO
463 ;;;  * (DEFUN ZOO (X) (* 3 X))
464 ;;;  Warning: ZOO previously defined as a macro.
465 ;;;  ZOO
466 ;;; But that doesn't seem to be what happens in CMU CL.
467 ;;;
468 ;;; KLUDGE: this is probably because ANSI, in its wisdom (CLHS
469 ;;; 5.1.2.5) requires implementations to support
470 ;;;   (SETF (APPLY #'AREF ...) ...)
471 ;;; [and also #'BIT and #'SBIT].  Yes, this is terrifying, and it's
472 ;;; also terrifying that this sequence of definitions causes it to
473 ;;; work.
474 ;;;
475 ;;; Also, it would be nice to make DESCRIBE FOO tell whether a symbol
476 ;;; has a setf expansion and/or a setf function defined.
477
478 #!-sb-fluid (declaim (inline (setf aref)))
479 (defun (setf aref) (new-value array &rest subscripts)
480   (declare (dynamic-extent subscripts))
481   (declare (type array array))
482   (setf (row-major-aref array (%array-row-major-index array subscripts))
483         new-value))
484
485 (defun row-major-aref (array index)
486   #!+sb-doc
487   "Return the element of array corressponding to the row-major index. This is
488    SETF'able."
489   (declare (optimize (safety 1)))
490   (row-major-aref array index))
491
492 (defun %set-row-major-aref (array index new-value)
493   (declare (optimize (safety 1)))
494   (setf (row-major-aref array index) new-value))
495
496 (defun svref (simple-vector index)
497   #!+sb-doc
498   "Return the INDEX'th element of the given Simple-Vector."
499   (declare (optimize (safety 1)))
500   (aref simple-vector index))
501
502 (defun %svset (simple-vector index new)
503   (declare (optimize (safety 1)))
504   (setf (aref simple-vector index) new))
505
506 (defun bit (bit-array &rest subscripts)
507   #!+sb-doc
508   "Return the bit from the BIT-ARRAY at the specified SUBSCRIPTS."
509   (declare (type (array bit) bit-array) (optimize (safety 1)))
510   (row-major-aref bit-array (%array-row-major-index bit-array subscripts)))
511
512 (defun %bitset (bit-array &rest stuff)
513   (declare (type (array bit) bit-array) (optimize (safety 1)))
514   (let ((subscripts (butlast stuff))
515         (new-value (car (last stuff))))
516     (setf (row-major-aref bit-array
517                           (%array-row-major-index bit-array subscripts))
518           new-value)))
519
520 #!-sb-fluid (declaim (inline (setf bit)))
521 (defun (setf bit) (new-value bit-array &rest subscripts)
522   (declare (type (array bit) bit-array) (optimize (safety 1)))
523   (setf (row-major-aref bit-array
524                         (%array-row-major-index bit-array subscripts))
525         new-value))
526
527 (defun sbit (simple-bit-array &rest subscripts)
528   #!+sb-doc
529   "Return the bit from SIMPLE-BIT-ARRAY at the specified SUBSCRIPTS."
530   (declare (type (simple-array bit) simple-bit-array) (optimize (safety 1)))
531   (row-major-aref simple-bit-array
532                   (%array-row-major-index simple-bit-array subscripts)))
533
534 ;;; KLUDGE: Not all these things (%SET-ROW-MAJOR-AREF, %SET-FILL-POINTER,
535 ;;; %SET-FDEFINITION, %SCHARSET, %SBITSET..) seem to deserve separate names.
536 ;;; Could we just DEFUN (SETF SBIT) etc. and get rid of the non-ANSI names?
537 ;;; -- WHN 19990911
538 (defun %sbitset (simple-bit-array &rest stuff)
539   (declare (type (simple-array bit) simple-bit-array) (optimize (safety 1)))
540   (let ((subscripts (butlast stuff))
541         (new-value (car (last stuff))))
542     (setf (row-major-aref simple-bit-array
543                           (%array-row-major-index simple-bit-array subscripts))
544           new-value)))
545
546 #!-sb-fluid (declaim (inline (setf sbit)))
547 (defun (setf sbit) (new-value bit-array &rest subscripts)
548   (declare (type (simple-array bit) bit-array) (optimize (safety 1)))
549   (setf (row-major-aref bit-array
550                         (%array-row-major-index bit-array subscripts))
551         new-value))
552 \f
553 ;;;; miscellaneous array properties
554
555 (defun array-element-type (array)
556   #!+sb-doc
557   "Return the type of the elements of the array"
558   (let ((widetag (widetag-of array)))
559     (macrolet ((pick-element-type (&rest stuff)
560                  `(cond ,@(mapcar (lambda (stuff)
561                                     (cons
562                                      (let ((item (car stuff)))
563                                        (cond ((eq item t)
564                                               t)
565                                              ((listp item)
566                                               (cons 'or
567                                                     (mapcar (lambda (x)
568                                                               `(= widetag ,x))
569                                                             item)))
570                                              (t
571                                               `(= widetag ,item))))
572                                      (cdr stuff)))
573                                   stuff))))
574       #.`(pick-element-type
575           ,@(map 'list
576                  (lambda (saetp)
577                    `(,(if (sb!vm:saetp-complex-typecode saetp)
578                           (list (sb!vm:saetp-typecode saetp)
579                                 (sb!vm:saetp-complex-typecode saetp))
580                           (sb!vm:saetp-typecode saetp))
581                      ',(sb!vm:saetp-specifier saetp)))
582                  sb!vm:*specialized-array-element-type-properties*)
583           ((sb!vm:simple-array-widetag
584             sb!vm:complex-vector-widetag
585             sb!vm:complex-array-widetag)
586            (with-array-data ((array array) (start) (end))
587              (declare (ignore start end))
588              (array-element-type array)))
589           (t
590            (error 'type-error :datum array :expected-type 'array))))))
591
592 (defun array-rank (array)
593   #!+sb-doc
594   "Return the number of dimensions of ARRAY."
595   (if (array-header-p array)
596       (%array-rank array)
597       1))
598
599 (defun array-dimension (array axis-number)
600   #!+sb-doc
601   "Return the length of dimension AXIS-NUMBER of ARRAY."
602   (declare (array array) (type index axis-number))
603   (cond ((not (array-header-p array))
604          (unless (= axis-number 0)
605            (error "Vector axis is not zero: ~S" axis-number))
606          (length (the (simple-array * (*)) array)))
607         ((>= axis-number (%array-rank array))
608          (error "Axis number ~W is too big; ~S only has ~D dimension~:P."
609                 axis-number array (%array-rank array)))
610         (t
611          ;; ANSI sayeth (ADJUST-ARRAY dictionary entry):
612          ;;
613          ;;   "If A is displaced to B, the consequences are
614          ;;   unspecified if B is adjusted in such a way that it no
615          ;;   longer has enough elements to satisfy A.
616          ;;
617          ;; In situations where this matters we should be doing a
618          ;; bounds-check, which in turn uses ARRAY-DIMENSION -- so
619          ;; this seems like a good place to signal an error.
620          (multiple-value-bind (target offset) (array-displacement array)
621            (when (and target
622                       (> (array-total-size array)
623                          (- (array-total-size target) offset)))
624                (error 'displaced-to-array-too-small-error
625                       :format-control "~@<The displaced-to array is too small. ~S ~
626                                       elements after offset required, ~S available.~:@>"
627                       :format-arguments (list (array-total-size array)
628                                               (- (array-total-size target) offset))))
629            (%array-dimension array axis-number)))))
630
631 (defun array-dimensions (array)
632   #!+sb-doc
633   "Return a list whose elements are the dimensions of the array"
634   (declare (array array))
635   (if (array-header-p array)
636       (do ((results nil (cons (array-dimension array index) results))
637            (index (1- (array-rank array)) (1- index)))
638           ((minusp index) results))
639       (list (array-dimension array 0))))
640
641 (defun array-total-size (array)
642   #!+sb-doc
643   "Return the total number of elements in the Array."
644   (declare (array array))
645   (if (array-header-p array)
646       (%array-available-elements array)
647       (length (the vector array))))
648
649 (defun array-displacement (array)
650   #!+sb-doc
651   "Return the values of :DISPLACED-TO and :DISPLACED-INDEX-offset
652    options to MAKE-ARRAY, or NIL and 0 if not a displaced array."
653   (declare (type array array))
654   (if (and (array-header-p array) ; if unsimple and
655            (%array-displaced-p array)) ; displaced
656       (values (%array-data-vector array) (%array-displacement array))
657       (values nil 0)))
658
659 (defun adjustable-array-p (array)
660   #!+sb-doc
661   "Return T if (ADJUST-ARRAY ARRAY...) would return an array identical
662    to the argument, this happens for complex arrays."
663   (declare (array array))
664   ;; Note that this appears not to be a fundamental limitation.
665   ;; non-vector SIMPLE-ARRAYs are in fact capable of being adjusted,
666   ;; but in practice we test using ADJUSTABLE-ARRAY-P in ADJUST-ARRAY.
667   ;; -- CSR, 2004-03-01.
668   (not (typep array 'simple-array)))
669 \f
670 ;;;; fill pointer frobbing stuff
671
672 (defun array-has-fill-pointer-p (array)
673   #!+sb-doc
674   "Return T if the given ARRAY has a fill pointer, or NIL otherwise."
675   (declare (array array))
676   (and (array-header-p array) (%array-fill-pointer-p array)))
677
678 (defun fill-pointer (vector)
679   #!+sb-doc
680   "Return the FILL-POINTER of the given VECTOR."
681   (declare (vector vector))
682   (if (and (array-header-p vector) (%array-fill-pointer-p vector))
683       (%array-fill-pointer vector)
684       (error 'simple-type-error
685              :datum vector
686              :expected-type '(and vector (satisfies array-has-fill-pointer-p))
687              :format-control "~S is not an array with a fill pointer."
688              :format-arguments (list vector))))
689
690 (defun %set-fill-pointer (vector new)
691   (declare (vector vector) (fixnum new))
692   (if (and (array-header-p vector) (%array-fill-pointer-p vector))
693       (if (> new (%array-available-elements vector))
694         (error
695          "The new fill pointer, ~S, is larger than the length of the vector."
696          new)
697         (setf (%array-fill-pointer vector) new))
698       (error 'simple-type-error
699              :datum vector
700              :expected-type '(and vector (satisfies array-has-fill-pointer-p))
701              :format-control "~S is not an array with a fill pointer."
702              :format-arguments (list vector))))
703
704 ;;; FIXME: It'd probably make sense to use a MACROLET to share the
705 ;;; guts of VECTOR-PUSH between VECTOR-PUSH-EXTEND. Such a macro
706 ;;; should probably be based on the VECTOR-PUSH-EXTEND code (which is
707 ;;; new ca. sbcl-0.7.0) rather than the VECTOR-PUSH code (which dates
708 ;;; back to CMU CL).
709 (defun vector-push (new-el array)
710   #!+sb-doc
711   "Attempt to set the element of ARRAY designated by its fill pointer
712    to NEW-EL, and increment the fill pointer by one. If the fill pointer is
713    too large, NIL is returned, otherwise the index of the pushed element is
714    returned."
715   (declare (vector array))
716   (let ((fill-pointer (fill-pointer array)))
717     (declare (fixnum fill-pointer))
718     (cond ((= fill-pointer (%array-available-elements array))
719            nil)
720           (t
721            (setf (aref array fill-pointer) new-el)
722            (setf (%array-fill-pointer array) (1+ fill-pointer))
723            fill-pointer))))
724
725 (defun vector-push-extend (new-element
726                            vector
727                            &optional
728                            (extension (1+ (length vector))))
729   (declare (vector vector) (fixnum extension))
730   (let ((fill-pointer (fill-pointer vector)))
731     (declare (fixnum fill-pointer))
732     (when (= fill-pointer (%array-available-elements vector))
733       (adjust-array vector (+ fill-pointer extension)))
734     ;; disable bounds checking
735     (locally (declare (optimize (safety 0)))
736       (setf (aref vector fill-pointer) new-element))
737     (setf (%array-fill-pointer vector) (1+ fill-pointer))
738     fill-pointer))
739
740 (defun vector-pop (array)
741   #!+sb-doc
742   "Decrease the fill pointer by 1 and return the element pointed to by the
743   new fill pointer."
744   (declare (vector array))
745   (let ((fill-pointer (fill-pointer array)))
746     (declare (fixnum fill-pointer))
747     (if (zerop fill-pointer)
748         (error "There is nothing left to pop.")
749         ;; disable bounds checking (and any fixnum test)
750         (locally (declare (optimize (safety 0)))
751           (aref array
752                 (setf (%array-fill-pointer array)
753                       (1- fill-pointer)))))))
754
755 \f
756 ;;;; ADJUST-ARRAY
757
758 (defun adjust-array (array dimensions &key
759                            (element-type (array-element-type array))
760                            (initial-element nil initial-element-p)
761                            (initial-contents nil initial-contents-p)
762                            fill-pointer
763                            displaced-to displaced-index-offset)
764   #!+sb-doc
765   "Adjust ARRAY's dimensions to the given DIMENSIONS and stuff."
766   (let ((dimensions (if (listp dimensions) dimensions (list dimensions))))
767     (cond ((/= (the fixnum (length (the list dimensions)))
768                (the fixnum (array-rank array)))
769            (error "The number of dimensions not equal to rank of array."))
770           ((not (subtypep element-type (array-element-type array)))
771            (error "The new element type, ~S, is incompatible with old type."
772                   element-type))
773           ((and fill-pointer (not (array-has-fill-pointer-p array)))
774            (error 'type-error
775                   :datum array
776                   :expected-type '(satisfies array-has-fill-pointer-p))))
777     (let ((array-rank (length (the list dimensions))))
778       (declare (fixnum array-rank))
779       (unless (= array-rank 1)
780         (when fill-pointer
781           (error "Only vectors can have fill pointers.")))
782       (cond (initial-contents-p
783              ;; array former contents replaced by INITIAL-CONTENTS
784              (if (or initial-element-p displaced-to)
785                  (error "INITIAL-CONTENTS may not be specified with ~
786                          the :INITIAL-ELEMENT or :DISPLACED-TO option."))
787              (let* ((array-size (apply #'* dimensions))
788                     (array-data (data-vector-from-inits
789                                  dimensions array-size element-type
790                                  initial-contents initial-contents-p
791                                  initial-element initial-element-p)))
792                (if (adjustable-array-p array)
793                    (set-array-header array array-data array-size
794                                  (get-new-fill-pointer array array-size
795                                                        fill-pointer)
796                                  0 dimensions nil)
797                    (if (array-header-p array)
798                        ;; simple multidimensional or single dimensional array
799                        (make-array dimensions
800                                    :element-type element-type
801                                    :initial-contents initial-contents)
802                        array-data))))
803             (displaced-to
804              ;; We already established that no INITIAL-CONTENTS was supplied.
805              (when initial-element
806                (error "The :INITIAL-ELEMENT option may not be specified ~
807                        with :DISPLACED-TO."))
808              (unless (subtypep element-type (array-element-type displaced-to))
809                (error "can't displace an array of type ~S into another of ~
810                        type ~S"
811                       element-type (array-element-type displaced-to)))
812              (let ((displacement (or displaced-index-offset 0))
813                    (array-size (apply #'* dimensions)))
814                (declare (fixnum displacement array-size))
815                (if (< (the fixnum (array-total-size displaced-to))
816                       (the fixnum (+ displacement array-size)))
817                    (error "The :DISPLACED-TO array is too small."))
818                (if (adjustable-array-p array)
819                    ;; None of the original contents appear in adjusted array.
820                    (set-array-header array displaced-to array-size
821                                      (get-new-fill-pointer array array-size
822                                                            fill-pointer)
823                                      displacement dimensions t)
824                    ;; simple multidimensional or single dimensional array
825                    (make-array dimensions
826                                :element-type element-type
827                                :displaced-to displaced-to
828                                :displaced-index-offset
829                                displaced-index-offset))))
830             ((= array-rank 1)
831              (let ((old-length (array-total-size array))
832                    (new-length (car dimensions))
833                    new-data)
834                (declare (fixnum old-length new-length))
835                (with-array-data ((old-data array) (old-start)
836                                  (old-end old-length))
837                  (cond ((or (and (array-header-p array)
838                                  (%array-displaced-p array))
839                             (< old-length new-length))
840                         (setf new-data
841                               (data-vector-from-inits
842                                dimensions new-length element-type
843                                initial-contents initial-contents-p
844                                initial-element initial-element-p))
845                         (replace new-data old-data
846                                  :start2 old-start :end2 old-end))
847                        (t (setf new-data
848                                 (shrink-vector old-data new-length))))
849                  (if (adjustable-array-p array)
850                      (set-array-header array new-data new-length
851                                        (get-new-fill-pointer array new-length
852                                                              fill-pointer)
853                                        0 dimensions nil)
854                      new-data))))
855             (t
856              (let ((old-length (%array-available-elements array))
857                    (new-length (apply #'* dimensions)))
858                (declare (fixnum old-length new-length))
859                (with-array-data ((old-data array) (old-start)
860                                  (old-end old-length))
861                  (declare (ignore old-end))
862                  (let ((new-data (if (or (and (array-header-p array)
863                                               (%array-displaced-p array))
864                                          (> new-length old-length))
865                                      (data-vector-from-inits
866                                       dimensions new-length
867                                       element-type () nil
868                                       initial-element initial-element-p)
869                                      old-data)))
870                    (if (or (zerop old-length) (zerop new-length))
871                        (when initial-element-p (fill new-data initial-element))
872                        (zap-array-data old-data (array-dimensions array)
873                                        old-start
874                                        new-data dimensions new-length
875                                        element-type initial-element
876                                        initial-element-p))
877                    (if (adjustable-array-p array)
878                        (set-array-header array new-data new-length
879                                          new-length 0 dimensions nil)
880                        (let ((new-array
881                               (make-array-header
882                                sb!vm:simple-array-widetag array-rank)))
883                          (set-array-header new-array new-data new-length
884                                            new-length 0 dimensions nil)))))))))))
885
886
887 (defun get-new-fill-pointer (old-array new-array-size fill-pointer)
888   (cond ((not fill-pointer)
889          (when (array-has-fill-pointer-p old-array)
890            (when (> (%array-fill-pointer old-array) new-array-size)
891              (error "cannot ADJUST-ARRAY an array (~S) to a size (~S) that is ~
892                      smaller than its fill pointer (~S)"
893                     old-array new-array-size (fill-pointer old-array)))
894            (%array-fill-pointer old-array)))
895         ((not (array-has-fill-pointer-p old-array))
896          (error "cannot supply a non-NIL value (~S) for :FILL-POINTER ~
897                  in ADJUST-ARRAY unless the array (~S) was originally ~
898                  created with a fill pointer"
899                 fill-pointer
900                 old-array))
901         ((numberp fill-pointer)
902          (when (> fill-pointer new-array-size)
903            (error "can't supply a value for :FILL-POINTER (~S) that is larger ~
904                    than the new length of the vector (~S)"
905                   fill-pointer new-array-size))
906          fill-pointer)
907         ((eq fill-pointer t)
908          new-array-size)
909         (t
910          (error "bogus value for :FILL-POINTER in ADJUST-ARRAY: ~S"
911                 fill-pointer))))
912
913 ;;; Destructively alter VECTOR, changing its length to NEW-LENGTH,
914 ;;; which must be less than or equal to its current length. This can
915 ;;; be called on vectors without a fill pointer but it is extremely
916 ;;; dangerous to do so: shrinking the size of an object (as viewed by
917 ;;; the gc) makes bounds checking unreliable in the face of interrupts
918 ;;; or multi-threading. Call it only on provably local vectors.
919 (defun %shrink-vector (vector new-length)
920   (declare (vector vector))
921   (unless (array-header-p vector)
922     (macrolet ((frob (name &rest things)
923                  `(etypecase ,name
924                     ((simple-array nil (*)) (error 'nil-array-accessed-error))
925                     ,@(mapcar (lambda (thing)
926                                 (destructuring-bind (type-spec fill-value)
927                                     thing
928                                   `(,type-spec
929                                     (fill (truly-the ,type-spec ,name)
930                                           ,fill-value
931                                           :start new-length))))
932                               things))))
933       ;; Set the 'tail' of the vector to the appropriate type of zero,
934       ;; "because in some cases we'll scavenge larger areas in one go,
935       ;; like groups of pages that had triggered the write barrier, or
936       ;; the whole static space" according to jsnell.
937       #.`(frob vector
938           ,@(map 'list
939                  (lambda (saetp)
940                    `((simple-array ,(sb!vm:saetp-specifier saetp) (*))
941                      ,(if (or (eq (sb!vm:saetp-specifier saetp) 'character)
942                               #!+sb-unicode
943                               (eq (sb!vm:saetp-specifier saetp) 'base-char))
944                           *default-init-char-form*
945                           (sb!vm:saetp-initial-element-default saetp))))
946                  (remove-if-not
947                   #'sb!vm:saetp-specifier
948                   sb!vm:*specialized-array-element-type-properties*)))))
949   ;; Only arrays have fill-pointers, but vectors have their length
950   ;; parameter in the same place.
951   (setf (%array-fill-pointer vector) new-length)
952   vector)
953
954 (defun shrink-vector (vector new-length)
955   (declare (vector vector))
956   (cond
957     ((eq (length vector) new-length)
958      vector)
959     ((array-has-fill-pointer-p vector)
960      (setf (%array-fill-pointer vector) new-length)
961      vector)
962     (t (subseq vector 0 new-length))))
963
964 ;;; Fill in array header with the provided information, and return the array.
965 (defun set-array-header (array data length fill-pointer displacement dimensions
966                          &optional displacedp)
967   (setf (%array-data-vector array) data)
968   (setf (%array-available-elements array) length)
969   (cond (fill-pointer
970          (setf (%array-fill-pointer array) fill-pointer)
971          (setf (%array-fill-pointer-p array) t))
972         (t
973          (setf (%array-fill-pointer array) length)
974          (setf (%array-fill-pointer-p array) nil)))
975   (setf (%array-displacement array) displacement)
976   (if (listp dimensions)
977       (dotimes (axis (array-rank array))
978         (declare (type index axis))
979         (setf (%array-dimension array axis) (pop dimensions)))
980       (setf (%array-dimension array 0) dimensions))
981   (setf (%array-displaced-p array) displacedp)
982   array)
983 \f
984 ;;;; ZAP-ARRAY-DATA for ADJUST-ARRAY
985
986 ;;; a temporary to be used when OLD-DATA and NEW-DATA are EQ.
987 ;;; KLUDGE: Boy, DYNAMIC-EXTENT would be nice. This is rebound
988 ;;; to length zero array in each new thread.
989 ;;;
990 ;;; DX is probably a bad idea, because a with a big array it would
991 ;;; be fairly easy to blow the stack.
992 ;;;
993 ;;; Rebound per thread.
994 (defvar *zap-array-data-temp* (make-array 1000 :initial-element t))
995
996 (defun zap-array-data-temp (length element-type initial-element
997                             initial-element-p)
998   (declare (fixnum length))
999   (when (> length (the fixnum (length *zap-array-data-temp*)))
1000     (setf *zap-array-data-temp*
1001           (make-array length :initial-element t)))
1002   (when initial-element-p
1003     (unless (typep initial-element element-type)
1004       (error "~S can't be used to initialize an array of type ~S."
1005              initial-element element-type))
1006     (fill (the simple-vector *zap-array-data-temp*) initial-element
1007           :end length))
1008   *zap-array-data-temp*)
1009
1010 ;;; This does the grinding work for ADJUST-ARRAY. It zaps the data
1011 ;;; from the OLD-DATA in an arrangement specified by the OLD-DIMS to
1012 ;;; the NEW-DATA in an arrangement specified by the NEW-DIMS. OFFSET
1013 ;;; is a displaced offset to be added to computed indices of OLD-DATA.
1014 ;;; NEW-LENGTH, ELEMENT-TYPE, INITIAL-ELEMENT, and INITIAL-ELEMENT-P
1015 ;;; are used when OLD-DATA and NEW-DATA are EQ; in this case, a
1016 ;;; temporary must be used and filled appropriately. When OLD-DATA and
1017 ;;; NEW-DATA are not EQ, NEW-DATA has already been filled with any
1018 ;;; specified initial-element.
1019 (defun zap-array-data (old-data old-dims offset new-data new-dims new-length
1020                        element-type initial-element initial-element-p)
1021   (declare (list old-dims new-dims))
1022   (setq old-dims (nreverse old-dims))
1023   (setq new-dims (reverse new-dims))
1024   (if (eq old-data new-data)
1025       (let ((temp (zap-array-data-temp new-length element-type
1026                                        initial-element initial-element-p)))
1027         (zap-array-data-aux old-data old-dims offset temp new-dims)
1028         (dotimes (i new-length) (setf (aref new-data i) (aref temp i))))
1029       (zap-array-data-aux old-data old-dims offset new-data new-dims)))
1030
1031 (defun zap-array-data-aux (old-data old-dims offset new-data new-dims)
1032   (declare (fixnum offset))
1033   (let ((limits (mapcar (lambda (x y)
1034                           (declare (fixnum x y))
1035                           (1- (the fixnum (min x y))))
1036                         old-dims new-dims)))
1037     (macrolet ((bump-index-list (index limits)
1038                  `(do ((subscripts ,index (cdr subscripts))
1039                        (limits ,limits (cdr limits)))
1040                       ((null subscripts) :eof)
1041                     (cond ((< (the fixnum (car subscripts))
1042                               (the fixnum (car limits)))
1043                            (rplaca subscripts
1044                                    (1+ (the fixnum (car subscripts))))
1045                            (return ,index))
1046                           (t (rplaca subscripts 0))))))
1047       (do ((index (make-list (length old-dims) :initial-element 0)
1048                   (bump-index-list index limits)))
1049           ((eq index :eof))
1050         (setf (aref new-data (row-major-index-from-dims index new-dims))
1051               (aref old-data
1052                     (+ (the fixnum (row-major-index-from-dims index old-dims))
1053                        offset)))))))
1054
1055 ;;; Figure out the row-major-order index of an array reference from a
1056 ;;; list of subscripts and a list of dimensions. This is for internal
1057 ;;; calls only, and the subscripts and dim-list variables are assumed
1058 ;;; to be reversed from what the user supplied.
1059 (defun row-major-index-from-dims (rev-subscripts rev-dim-list)
1060   (do ((rev-subscripts rev-subscripts (cdr rev-subscripts))
1061        (rev-dim-list rev-dim-list (cdr rev-dim-list))
1062        (chunk-size 1)
1063        (result 0))
1064       ((null rev-dim-list) result)
1065     (declare (fixnum chunk-size result))
1066     (setq result (+ result
1067                     (the fixnum (* (the fixnum (car rev-subscripts))
1068                                    chunk-size))))
1069     (setq chunk-size (* chunk-size (the fixnum (car rev-dim-list))))))
1070 \f
1071 ;;;; some bit stuff
1072
1073 (defun bit-array-same-dimensions-p (array1 array2)
1074   (declare (type (array bit) array1 array2))
1075   (and (= (array-rank array1)
1076           (array-rank array2))
1077        (dotimes (index (array-rank array1) t)
1078          (when (/= (array-dimension array1 index)
1079                    (array-dimension array2 index))
1080            (return nil)))))
1081
1082 (defun pick-result-array (result-bit-array bit-array-1)
1083   (case result-bit-array
1084     ((t) bit-array-1)
1085     ((nil) (make-array (array-dimensions bit-array-1)
1086                        :element-type 'bit
1087                        :initial-element 0))
1088     (t
1089      (unless (bit-array-same-dimensions-p bit-array-1
1090                                           result-bit-array)
1091        (error "~S and ~S don't have the same dimensions."
1092               bit-array-1 result-bit-array))
1093      result-bit-array)))
1094
1095 (defmacro def-bit-array-op (name function)
1096   `(defun ,name (bit-array-1 bit-array-2 &optional result-bit-array)
1097      #!+sb-doc
1098      ,(format nil
1099               "Perform a bit-wise ~A on the elements of BIT-ARRAY-1 and ~
1100                BIT-ARRAY-2,~%  putting the results in RESULT-BIT-ARRAY. ~
1101                If RESULT-BIT-ARRAY is T,~%  BIT-ARRAY-1 is used. If ~
1102                RESULT-BIT-ARRAY is NIL or omitted, a new array is~%  created. ~
1103                All the arrays must have the same rank and dimensions."
1104               (symbol-name function))
1105      (declare (type (array bit) bit-array-1 bit-array-2)
1106               (type (or (array bit) (member t nil)) result-bit-array))
1107      (unless (bit-array-same-dimensions-p bit-array-1 bit-array-2)
1108        (error "~S and ~S don't have the same dimensions."
1109               bit-array-1 bit-array-2))
1110      (let ((result-bit-array (pick-result-array result-bit-array bit-array-1)))
1111        (if (and (simple-bit-vector-p bit-array-1)
1112                 (simple-bit-vector-p bit-array-2)
1113                 (simple-bit-vector-p result-bit-array))
1114            (locally (declare (optimize (speed 3) (safety 0)))
1115              (,name bit-array-1 bit-array-2 result-bit-array))
1116            (with-array-data ((data1 bit-array-1) (start1) (end1))
1117              (declare (ignore end1))
1118              (with-array-data ((data2 bit-array-2) (start2) (end2))
1119                (declare (ignore end2))
1120                (with-array-data ((data3 result-bit-array) (start3) (end3))
1121                  (do ((index-1 start1 (1+ index-1))
1122                       (index-2 start2 (1+ index-2))
1123                       (index-3 start3 (1+ index-3)))
1124                      ((>= index-3 end3) result-bit-array)
1125                    (declare (type index index-1 index-2 index-3))
1126                    (setf (sbit data3 index-3)
1127                          (logand (,function (sbit data1 index-1)
1128                                             (sbit data2 index-2))
1129                                  1))))))))))
1130
1131 (def-bit-array-op bit-and logand)
1132 (def-bit-array-op bit-ior logior)
1133 (def-bit-array-op bit-xor logxor)
1134 (def-bit-array-op bit-eqv logeqv)
1135 (def-bit-array-op bit-nand lognand)
1136 (def-bit-array-op bit-nor lognor)
1137 (def-bit-array-op bit-andc1 logandc1)
1138 (def-bit-array-op bit-andc2 logandc2)
1139 (def-bit-array-op bit-orc1 logorc1)
1140 (def-bit-array-op bit-orc2 logorc2)
1141
1142 (defun bit-not (bit-array &optional result-bit-array)
1143   #!+sb-doc
1144   "Performs a bit-wise logical NOT on the elements of BIT-ARRAY,
1145   putting the results in RESULT-BIT-ARRAY. If RESULT-BIT-ARRAY is T,
1146   BIT-ARRAY is used. If RESULT-BIT-ARRAY is NIL or omitted, a new array is
1147   created. Both arrays must have the same rank and dimensions."
1148   (declare (type (array bit) bit-array)
1149            (type (or (array bit) (member t nil)) result-bit-array))
1150   (let ((result-bit-array (pick-result-array result-bit-array bit-array)))
1151     (if (and (simple-bit-vector-p bit-array)
1152              (simple-bit-vector-p result-bit-array))
1153         (locally (declare (optimize (speed 3) (safety 0)))
1154           (bit-not bit-array result-bit-array))
1155         (with-array-data ((src bit-array) (src-start) (src-end))
1156           (declare (ignore src-end))
1157           (with-array-data ((dst result-bit-array) (dst-start) (dst-end))
1158             (do ((src-index src-start (1+ src-index))
1159                  (dst-index dst-start (1+ dst-index)))
1160                 ((>= dst-index dst-end) result-bit-array)
1161               (declare (type index src-index dst-index))
1162               (setf (sbit dst dst-index)
1163                     (logxor (sbit src src-index) 1))))))))