1;; Implementation of SRFI 60 "Integers as Bits" for Scheme 48, based on
2;; reference implementation.
3
4;; Copyright (C) Aubrey Jaffer (2004, 2005).
5;; Copyright (C) 2005 David Van Horn
6;; All Rights Reserved.
7;;
8;; Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
9;;
10;; The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
11;;
12;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
13
14;; Released under the same terms as the SRFI reference implementation
15;; included below.
16
17
18;; SRFI 60 defines several procedures which are already provided
19;; by Scheme48's bitwise structure, namely bit-count,
20;; bitwise-{ior,xor,and,not}, and arithmetic-shift, which are used
21;; for the implementation of this library, and exported by this
22;; structure.
23
24(define logior bitwise-ior)
25(define logxor bitwise-xor)
26(define logand bitwise-and)
27(define lognot bitwise-not)
28(define logcount bit-count)
29
30;; The reference implementation follows below and has been changed only
31;; by adding S-expression comments to definitions which are not needed,
32;; such as definitions implemented as Scheme 48 exact integer primitives.
33
34;;;; "logical.scm", bit access and operations for integers for Scheme
35;;; Copyright (C) 1991, 1993, 2001, 2003, 2005 Aubrey Jaffer
36;
37;Permission to copy this software, to modify it, to redistribute it,
38;to distribute modified versions, and to use it for any purpose is
39;granted, subject to the following restrictions and understandings.
40;
41;1.  Any copy made of this software must include this copyright notice
42;in full.
43;
44;2.  I have made no warranty or representation that the operation of
45;this software will be error-free, and I am under no obligation to
46;provide any services, by way of maintenance, update, or otherwise.
47;
48;3.  In conjunction with products arising from the use of this
49;material, there shall be no use of my name in any advertising,
50;promotional, or sales literature without prior written consent in
51;each case.
52
53; (define logical:boole-xor
54;  '#(#(0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15)
55;     #(1 0 3 2 5 4 7 6 9 8 11 10 13 12 15 14)
56;     #(2 3 0 1 6 7 4 5 10 11 8 9 14 15 12 13)
57;     #(3 2 1 0 7 6 5 4 11 10 9 8 15 14 13 12)
58;     #(4 5 6 7 0 1 2 3 12 13 14 15 8 9 10 11)
59;     #(5 4 7 6 1 0 3 2 13 12 15 14 9 8 11 10)
60;     #(6 7 4 5 2 3 0 1 14 15 12 13 10 11 8 9)
61;     #(7 6 5 4 3 2 1 0 15 14 13 12 11 10 9 8)
62;     #(8 9 10 11 12 13 14 15 0 1 2 3 4 5 6 7)
63;     #(9 8 11 10 13 12 15 14 1 0 3 2 5 4 7 6)
64;     #(10 11 8 9 14 15 12 13 2 3 0 1 6 7 4 5)
65;     #(11 10 9 8 15 14 13 12 3 2 1 0 7 6 5 4)
66;     #(12 13 14 15 8 9 10 11 4 5 6 7 0 1 2 3)
67;     #(13 12 15 14 9 8 11 10 5 4 7 6 1 0 3 2)
68;     #(14 15 12 13 10 11 8 9 6 7 4 5 2 3 0 1)
69;     #(15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0)))
70;
71; (define logical:boole-and
72;  '#(#(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)
73;     #(0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1)
74;     #(0 0 2 2 0 0 2 2 0 0 2 2 0 0 2 2)
75;     #(0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3)
76;     #(0 0 0 0 4 4 4 4 0 0 0 0 4 4 4 4)
77;     #(0 1 0 1 4 5 4 5 0 1 0 1 4 5 4 5)
78;     #(0 0 2 2 4 4 6 6 0 0 2 2 4 4 6 6)
79;     #(0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7)
80;     #(0 0 0 0 0 0 0 0 8 8 8 8 8 8 8 8)
81;     #(0 1 0 1 0 1 0 1 8 9 8 9 8 9 8 9)
82;     #(0 0 2 2 0 0 2 2 8 8 10 10 8 8 10 10)
83;     #(0 1 2 3 0 1 2 3 8 9 10 11 8 9 10 11)
84;     #(0 0 0 0 4 4 4 4 8 8 8 8 12 12 12 12)
85;     #(0 1 0 1 4 5 4 5 8 9 8 9 12 13 12 13)
86;     #(0 0 2 2 4 4 6 6 8 8 10 10 12 12 14 14)
87;     #(0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15)))
88
89(define (logical:ash-4 x)
90  (if (negative? x)
91      (+ -1 (quotient (+ 1 x) 16))
92      (quotient x 16)))
93
94; (define (logical:reduce op4 ident)
95;   (lambda args
96;     (do ((res ident (op4 res (car rgs) 1 0))
97; 	 (rgs args (cdr rgs)))
98; 	((null? rgs) res))))
99
100;
101; (define logand
102;   (letrec
103;       ((lgand
104; 	(lambda (n2 n1 scl acc)
105; 	  (cond ((= n1 n2) (+ acc (* scl n1)))
106; 		((zero? n2) acc)
107; 		((zero? n1) acc)
108; 		(else (lgand (logical:ash-4 n2)
109; 			     (logical:ash-4 n1)
110; 			     (* 16 scl)
111; 			     (+ (* (vector-ref (vector-ref logical:boole-and
112; 							   (modulo n1 16))
113; 					       (modulo n2 16))
114; 				   scl)
115; 				acc)))))))
116;     (logical:reduce lgand -1)))
117;
118; (define logior
119;   (letrec
120;       ((lgior
121; 	(lambda (n2 n1 scl acc)
122; 	  (cond ((= n1 n2) (+ acc (* scl n1)))
123; 		((zero? n2) (+ acc (* scl n1)))
124; 		((zero? n1) (+ acc (* scl n2)))
125; 		(else (lgior (logical:ash-4 n2)
126; 			     (logical:ash-4 n1)
127; 			     (* 16 scl)
128; 			     (+ (* (- 15 (vector-ref
129; 					  (vector-ref logical:boole-and
130; 						      (- 15 (modulo n1 16)))
131; 					  (- 15 (modulo n2 16))))
132; 				   scl)
133; 				acc)))))))
134;     (logical:reduce lgior 0)))
135;
136; (define logxor
137;   (letrec
138;       ((lgxor
139; 	(lambda (n2 n1 scl acc)
140; 	  (cond ((= n1 n2) acc)
141; 		((zero? n2) (+ acc (* scl n1)))
142; 		((zero? n1) (+ acc (* scl n2)))
143; 		(else (lgxor (logical:ash-4 n2)
144; 			     (logical:ash-4 n1)
145; 			     (* 16 scl)
146; 			     (+ (* (vector-ref (vector-ref logical:boole-xor
147; 							   (modulo n1 16))
148; 					       (modulo n2 16))
149; 				   scl)
150; 				acc)))))))
151;     (logical:reduce lgxor 0)))
152;
153; (define (lognot n) (- -1 n))
154
155(define (logtest n1 n2)
156  (not (zero? (logand n1 n2))))
157
158(define (logbit? index n)
159  (logtest (expt 2 index) n))
160
161(define (copy-bit index to bool)
162  (if bool
163      (logior to (arithmetic-shift 1 index))
164      (logand to (lognot (arithmetic-shift 1 index)))))
165
166(define (bitwise-if mask n0 n1)
167  (logior (logand mask n0)
168	  (logand (lognot mask) n1)))
169
170(define (bit-field n start end)
171  (logand (lognot (ash -1 (- end start)))
172	  (arithmetic-shift n (- start))))
173
174(define (copy-bit-field to from start end)
175  (bitwise-if (arithmetic-shift (lognot (ash -1 (- end start))) start)
176	      (arithmetic-shift from start)
177	      to))
178
179(define (rotate-bit-field n count start end)
180  (define width (- end start))
181  (set! count (modulo count width))
182  (let ((mask (lognot (ash -1 width))))
183    (define zn (logand mask (arithmetic-shift n (- start))))
184    (logior (arithmetic-shift
185	     (logior (logand mask (arithmetic-shift zn count))
186		     (arithmetic-shift zn (- count width)))
187	     start)
188	    (logand (lognot (ash mask start)) n))))
189
190; (define (arithmetic-shift n count)
191;   (if (negative? count)
192;       (let ((k (expt 2 (- count))))
193; 	(if (negative? n)
194; 	    (+ -1 (quotient (+ 1 n) k))
195; 	    (quotient n k)))
196;       (* (expt 2 count) n)))
197
198(define integer-length
199  (letrec ((intlen (lambda (n tot)
200		     (case n
201		       ((0 -1) (+ 0 tot))
202		       ((1 -2) (+ 1 tot))
203		       ((2 3 -3 -4) (+ 2 tot))
204		       ((4 5 6 7 -5 -6 -7 -8) (+ 3 tot))
205		       (else (intlen (logical:ash-4 n) (+ 4 tot)))))))
206    (lambda (n) (intlen n 0))))
207
208; (define logcount
209;   (letrec ((logcnt (lambda (n tot)
210; 		     (if (zero? n)
211; 			 tot
212; 			 (logcnt (quotient n 16)
213; 				 (+ (vector-ref
214; 				     '#(0 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4)
215; 				     (modulo n 16))
216; 				    tot))))))
217;     (lambda (n)
218;       (cond ((negative? n) (logcnt (lognot n) 0))
219; 	    ((positive? n) (logcnt n 0))
220; 	    (else 0)))))
221
222(define (log2-binary-factors n)
223  (+ -1 (integer-length (logand n (- n)))))
224
225(define (bit-reverse k n)
226  (do ((m (if (negative? n) (lognot n) n) (arithmetic-shift m -1))
227       (k (+ -1 k) (+ -1 k))
228       (rvs 0 (logior (arithmetic-shift rvs 1) (logand 1 m))))
229      ((negative? k) (if (negative? n) (lognot rvs) rvs))))
230
231(define (reverse-bit-field n start end)
232  (define width (- end start))
233  (let ((mask (lognot (ash -1 width))))
234    (define zn (logand mask (arithmetic-shift n (- start))))
235    (logior (arithmetic-shift (bit-reverse width zn) start)
236	    (logand (lognot (ash mask start)) n))))
237
238(define (integer->list k . len)
239  (if (null? len)
240      (do ((k k (arithmetic-shift k -1))
241	   (lst '() (cons (odd? k) lst)))
242	  ((<= k 0) lst))
243      (do ((idx (+ -1 (car len)) (+ -1 idx))
244	   (k k (arithmetic-shift k -1))
245	   (lst '() (cons (odd? k) lst)))
246	  ((negative? idx) lst))))
247
248(define (list->integer bools)
249  (do ((bs bools (cdr bs))
250       (acc 0 (+ acc acc (if (car bs) 1 0))))
251      ((null? bs) acc)))
252(define (booleans->integer . bools)
253  (list->integer bools))
254
255;;;; SRFI-60 aliases
256(define ash arithmetic-shift)
257; (define bitwise-ior logior)
258; (define bitwise-xor logxor)
259; (define bitwise-and logand)
260; (define bitwise-not lognot)
261; (define bit-count logcount)
262(define bit-set?   logbit?)
263(define any-bits-set? logtest)
264(define first-set-bit log2-binary-factors)
265(define bitwise-merge bitwise-if)
266
267;;; Legacy
268;;(define (logical:rotate k count len) (rotate-bit-field k count 0 len))
269;;(define (logical:ones deg) (lognot (ash -1 deg)))
270;;(define integer-expt expt)		; legacy name
271