source: trunk/j/src/org/armedbear/lisp/bit-array-ops.lisp @ 4267

Last change on this file since 4267 was 4267, checked in by piso, 18 years ago

Cleanup.

File size: 3.6 KB
Line 
1;;; bit-array-ops.lisp
2;;;
3;;; Copyright (C) 2003 Peter Graves
4;;; $Id: bit-array-ops.lisp,v 1.2 2003-10-09 16:51:08 piso Exp $
5;;;
6;;; This program is free software; you can redistribute it and/or
7;;; modify it under the terms of the GNU General Public License
8;;; as published by the Free Software Foundation; either version 2
9;;; of the License, or (at your option) any later version.
10;;;
11;;; This program is distributed in the hope that it will be useful,
12;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14;;; GNU General Public License for more details.
15;;;
16;;; You should have received a copy of the GNU General Public License
17;;; along with this program; if not, write to the Free Software
18;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20;;; Adapted from CMUCL.
21
22(in-package "SYSTEM")
23
24(defun bit-array-same-dimensions-p (array1 array2)
25  (declare (type (array bit) array1 array2))
26  (and (= (array-rank array1)
27    (array-rank array2))
28       (dotimes (index (array-rank array1) t)
29   (when (/= (array-dimension array1 index)
30       (array-dimension array2 index))
31     (return nil)))))
32
33(defun pick-result-array (result-bit-array bit-array-1)
34  (case result-bit-array
35    ((t) bit-array-1)
36    ((nil) (make-array (array-dimensions bit-array-1)
37           :element-type 'bit
38           :initial-element 0))
39    (t
40     (unless (bit-array-same-dimensions-p bit-array-1
41            result-bit-array)
42       (error 'program-error "~S and ~S do not have the same dimensions"
43              bit-array-1 result-bit-array))
44     result-bit-array)))
45
46(defmacro def-bit-array-op (name function)
47  `(defun ,name (bit-array-1 bit-array-2 &optional result-bit-array)
48     ,(format nil
49        "Perform a bit-wise ~A on the elements of BIT-ARRAY-1 and ~
50               BIT-ARRAY-2,~%  putting the results in RESULT-BIT-ARRAY.  ~
51               If RESULT-BIT-ARRAY is T,~%  BIT-ARRAY-1 is used.  If ~
52               RESULT-BIT-ARRAY is NIL or omitted, a new array is~%  created.  ~
53               All the arrays must have the same rank and dimensions."
54        (symbol-name function))
55     (unless (bit-array-same-dimensions-p bit-array-1 bit-array-2)
56       (error 'program-error "~S and ~S do not have the same dimensions"
57              bit-array-1 bit-array-2))
58     (let ((result-bit-array (pick-result-array result-bit-array bit-array-1)))
59       (dotimes (i (array-total-size result-bit-array) result-bit-array)
60         (setf (row-major-aref result-bit-array i)
61               (logand (,function (row-major-aref bit-array-1 i)
62                                  (row-major-aref bit-array-2 i))
63                       1))))))
64
65(def-bit-array-op bit-and   logand)
66(def-bit-array-op bit-ior   logior)
67(def-bit-array-op bit-xor   logxor)
68(def-bit-array-op bit-eqv   logeqv)
69(def-bit-array-op bit-nand  lognand)
70(def-bit-array-op bit-nor   lognor)
71(def-bit-array-op bit-andc1 logandc1)
72(def-bit-array-op bit-andc2 logandc2)
73(def-bit-array-op bit-orc1  logorc1)
74(def-bit-array-op bit-orc2  logorc2)
75
76(defun bit-not (bit-array &optional result-bit-array)
77  "Performs a bit-wise logical NOT on the elements of BIT-ARRAY,
78   putting the results in RESULT-BIT-ARRAY.  If RESULT-BIT-ARRAY is T,
79   BIT-ARRAY is used.  If RESULT-BIT-ARRAY is NIL or omitted, a new array is
80   created.  Both arrays must have the same rank and dimensions."
81  (let ((result-bit-array (pick-result-array result-bit-array bit-array)))
82    (dotimes (i (array-total-size result-bit-array) result-bit-array)
83      (setf (row-major-aref result-bit-array i)
84            (logxor (row-major-aref bit-array i) 1)))))
Note: See TracBrowser for help on using the repository browser.