Initial revision
[sbcl.git] / src / compiler / bit-util.lisp
1 ;;;; bit-vector hacking utilities, potentially implementation-dependent
2 ;;;; for speed
3
4 ;;;; This software is part of the SBCL system. See the README file for
5 ;;;; more information.
6 ;;;;
7 ;;;; This software is derived from the CMU CL system, which was
8 ;;;; written at Carnegie Mellon University and released into the
9 ;;;; public domain. The software is in the public domain and is
10 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
11 ;;;; files for more information.
12
13 (in-package "SB!C")
14
15 (file-comment
16   "$Header$")
17
18 #!-sb-fluid
19 (declaim (inline clear-bit-vector set-bit-vector bit-vector-replace
20                  bit-vector-copy))
21
22 ;;; Clear a SIMPLE-BIT-VECTOR to zeros.
23 (defun clear-bit-vector (vec)
24   (declare (type simple-bit-vector vec))
25   (bit-xor vec vec t))
26
27 ;;; The old (pre-1999) code had a more-efficient-looking, but also
28 ;;; less-portable implementation of CLEAR-BIT-VECTOR:
29 ;;;  (do ((i sb!vm:vector-data-offset (1+ i))
30 ;;;       (end (+ sb!vm:vector-data-offset
31 ;;;            (ash (+ (length vec) (1- sb!vm:word-bits))
32 ;;;                 (- (1- (integer-length sb!vm:word-bits)))))))
33 ;;;      ((= i end) vec)
34 ;;;    (setf (sb!kernel:%raw-bits vec i) 0)))
35 ;;; We could use this in the target SBCL if the new version turns out to be a
36 ;;; bottleneck. I (WHN 19990321) will stick to the portable version for now.
37 ;;; And by the way, if we do revisit this file with efficiency on our mind, it
38 ;;; might be good to check whether it's really that helpful to implement
39 ;;; all these functions as INLINE. (How expensive can it be to call a
40 ;;; 1-argument function? How expensive is it to fill up our cache with
41 ;;; a bunch of redundant loop expansions?)
42 ;;;
43 ;;; FIXME: Perhaps do simple benchmarks against CMU CL to check this.
44
45 ;;; Fill a bit vector with ones.
46 (defun set-bit-vector (vec)
47   (declare (type simple-bit-vector vec))
48   (bit-orc2 vec vec t))
49
50 ;;; Replace the bits in To with the bits in From.
51 (defun bit-vector-replace (to from)
52   (declare (type simple-bit-vector to from))
53   (bit-ior from from to))
54
55 ;;; Copy a bit-vector.
56 (defun bit-vector-copy (vec)
57   (declare (type simple-bit-vector vec))
58   (bit-ior vec vec (make-array (length vec) :element-type 'bit)))