Debian Codegraph @

%3a151/bitwise-operations.sls

raw
1 ;; SRFI-151 r6rs library implementation
2 ;;
3 ;; The following contains a combination of R6RS implementation along with
4 ;; implementations pulled from the SRFI-151 implementation.
5 ;;
6 ;; The R6RS wrappers and simple functions are:
7 ;; Copyright (c) 2018 - 2020 Andrew W. Keep
8 ;;
9 ;; The bitwise-eqv implementation is based on the Olin Shiver's SRFI-33
10 ;; implementation from the SRFI-151 example library:
11 ;; Olin Shivers is the sole author of this code, and he has placed it in the
12 ;; public domain.
13 ;;
14 ;; The bitwise-fold, bitwise-for-each, bitwise-unfold, and
15 ;; make-bitwise-generator functions are pulled from John Cowan's SRFI-151
16 ;; implementation and are:
17 ;; Copyright John Cowan 2017
18
19 (library (srfi :151 bitwise-operations)
20 (export
21 bitwise-not
22 bitwise-and bitwise-ior
23 bitwise-xor bitwise-eqv
24 bitwise-nand bitwise-nor
25 bitwise-andc1 bitwise-andc2
26 bitwise-orc1 bitwise-orc2
27
28 arithmetic-shift bit-count
29 integer-length bitwise-if
30
31 bit-set? copy-bit bit-swap
32 any-bit-set? every-bit-set?
33 first-set-bit
34
35 bit-field bit-field-any? bit-field-every?
36 bit-field-clear bit-field-set
37 bit-field-replace bit-field-replace-same
38 bit-field-rotate bit-field-reverse
39
40 bits->list list->bits bits->vector vector->bits
41 bits
42 bitwise-fold bitwise-for-each bitwise-unfold
43 make-bitwise-generator)
44 (import (rnrs))
45
46 ;;; The bitwise-eqv implmentation is based on the one in the SRFI-151
47 ;;; implementation, which extracted it from the SRFI-33 implementation which
48 ;;; carried the following copyright information:
49 ;;;
50 ;;; Olin Shivers is the sole author of this code, and he has placed it in
51 ;;; the public domain.
52 ;;;
53 ;;; A good implementation might choose to provide direct compiler/interpreter
54 ;;; support for these derived functions, or might simply define them to be
55 ;;; integrable -- i.e., inline-expanded.
56 ;;;
57 ;;; This is a general definition, but less than efficient. It should also
58 ;;; receive primitive compiler/interpreter support so that the expensive
59 ;;; n-ary mechanism is not invoked in the standard cases -- that is,
60 ;;; an application of BITWISE-EQV should be rewritten into an equivalent
61 ;;; tree applying some two-argument primitive to the arguments, in the
62 ;;; same manner that statically-known n-ary applications of associative
63 ;;; operations such as + and * are handled efficiently:
64 ;;; (bitwise-eqv) => -1
65 ;;; (bitwise-eqv i) => i
66 ;;; (bitwise-eqv i j) => (%bitwise-eqv i j)
67 ;;; (bitwise-eqv i j k) => (%bitwise-eqv (%bitwise-eqv i j) k)
68 ;;; (bitwise-eqv i j k l) => (%bitwise-eqv (%bitwise-eqv (%bitwise-eqv i j) k) l)
69 ;;;
70 ;;; Note: this implementation takes the advice of the comment avove and
71 ;;; implemets this using case lambade, though Chez Scheme's source optimizer
72 ;;; is relied upon to produce the constants.
73 (define bitwise-eqv
74 (case-lambda
75 [() (bitwise-not (bitwise-xor))]
76 [(i) (bitwise-not (bitwise-xor i))]
77 [(i j) (bitwise-not (bitwise-xor i j))]
78 [(i . more)
79 (let loop ([i i] [more more])
80 (if (null? more)
81 i
82 (loop (bitwise-not (bitwise-xor i (car more))) (cdr more))))]))
83
84 (define bitwise-nand (lambda (i j) (bitwise-not (bitwise-and i j))))
85 (define bitwise-nor (lambda (i j) (bitwise-not (bitwise-ior i j))))
86 (define bitwise-andc1 (lambda (i j) (bitwise-and (bitwise-not i) j)))
87 (define bitwise-andc2 (lambda (i j) (bitwise-and i (bitwise-not j))))
88 (define bitwise-orc1 (lambda (i j) (bitwise-ior (bitwise-not i) j)))
89 (define bitwise-orc2 (lambda (i j) (bitwise-ior i (bitwise-not j))))
90
91 (define arithmetic-shift (lambda (i c) (bitwise-arithmetic-shift i c)))
92
93 (define bit-count
94 (lambda (i)
95 (if (< i 0)
96 (bitwise-bit-count (bitwise-not i))
97 (bitwise-bit-count i))))
98
99 (define integer-length (lambda (i) (bitwise-length i)))
100
101 (define bit-set? (lambda (index i) (bitwise-bit-set? i index)))
102
103 (define copy-bit
104 (lambda (index i bool)
105 (bitwise-copy-bit i index (if bool 1 0))))
106
107 (define bit-swap
108 (lambda (idx1 idx2 i)
109 (if (bitwise-bit-set? i idx1)
110 (if (bitwise-bit-set? i idx2)
111 i
112 (bitwise-copy-bit (bitwise-copy-bit i idx2 1) idx1 0))
113 (if (bitwise-bit-set? i idx2)
114 (bitwise-copy-bit (bitwise-copy-bit i idx2 0) idx1 1)
115 i))))
116
117 (define any-bit-set? (lambda (bits i) (not (zero? (bitwise-and bits i)))))
118 (define every-bit-set? (lambda (bits i) (= (bitwise-and bits i) bits)))
119
120 (define first-set-bit (lambda (i) (bitwise-first-bit-set i)))
121
122 (define bit-field (lambda (i s e) (bitwise-bit-field i s e)))
123
124 (define bit-field-any?
125 (lambda (i s e)
126 (not (zero? (bitwise-bit-field i s e)))))
127
128 (define bit-field-every?
129 (lambda (i s e)
130 (= (bitwise-bit-field i s e) (- (expt 2 (- e s)) 1))))
131
132 (define bit-field-clear
133 (lambda (i s e)
134 (bitwise-copy-bit-field i s e 0)))
135
136 (define bit-field-set
137 (lambda (i s e)
138 (bitwise-copy-bit-field i s e -1)))
139
140 (define bit-field-replace
141 (lambda (dest src s e)
142 (bitwise-copy-bit-field dest s e src)))
143
144 (define bit-field-replace-same
145 (lambda (dest src s e)
146 (bitwise-copy-bit-field dest s e (bitwise-bit-field src s e))))
147
148 (define bit-field-rotate
149 (lambda (i count start end)
150 (if (negative? count)
151 (bitwise-rotate-bit-field i start end (fx+ count (fx- end start)))
152 (bitwise-rotate-bit-field i start end count))))
153
154 (define bit-field-reverse
155 (lambda (i start end)
156 (bitwise-reverse-bit-field i start end)))
157
158 (define bits->list
159 (case-lambda
160 [(i)
161 (if (< i 0)
162 (bits->list i (integer-length i))
163 (let f ([i i])
164 (if (zero? i)
165 '()
166 (cons (odd? i) (f (bitwise-arithmetic-shift-right i 1))))))]
167 [(i n)
168 (let loop ([n n] [ls '()])
169 (if (fx=? n 0)
170 ls
171 (let ([n (fx- n 1)])
172 (loop n (cons (bitwise-bit-set? i n) ls)))))]))
173
174 (define bits->vector
175 (case-lambda
176 [(i)
177 (if (< i 0)
178 (bits->vector i (integer-length i))
179 (let f ([i i] [c 0])
180 (if (fx=? i 0)
181 (make-vector c)
182 (let ([v (f (bitwise-arithmetic-shift-right i 1) (fx+ c 1))])
183 (vector-set! v c (odd? i))))))]
184 [(i n)
185 (let ([v (make-vector n)])
186 (let loop ([n n])
187 (unless (fx=? n 0)
188 (let ([n (fx- n 1)])
189 (vector-set! v n (bitwise-bit-set? i n))
190 (loop n))))
191 v)]))
192
193 (define list->bits
194 (lambda (ls)
195 (let loop ([ls ls] [idx 0] [i 0])
196 (if (null? ls)
197 i
198 (loop (cdr ls) (fx+ idx 1)
199 (bitwise-copy-bit i idx (if (car ls) 1 0)))))))
200
201 (define vector->bits
202 (lambda (v)
203 (let loop ([n (vector-length v)] [i 0])
204 (if (fx=? n 0)
205 i
206 (let ([n (fx- n 1)])
207 (loop n (bitwise-copy-bit i n (if (vector-ref v n) 1 0))))))))
208
209 (define bits (lambda args (list->bits args)))
210
211 ;; ---- from SRFI-151 other ---
212 ;; The following functions: bitwise-fold, bitwise-for-each, bitwise-unfold,
213 ;; and make-bitwise-generator are taken from John Cowan's implemtation
214 ;; functions from the SRFI-151.
215 ;;
216 ;; Copyright John Cowan 2017
217 ;;
218 (define bitwise-fold
219 (lambda (proc seed i)
220 (let ([n (integer-length i)])
221 (let loop ([idx 0] [r seed])
222 (if (fx=? idx n)
223 r
224 (loop (fx+ idx 1) (proc (bitwise-bit-set? i idx) r)))))))
225
226 (define bitwise-for-each
227 (lambda (proc i)
228 (let ([n (integer-length i)])
229 (let loop ([idx 0])
230 (unless (fx=? idx n)
231 (proc (bitwise-bit-set? i idx))
232 (loop (fx+ idx 1)))))))
233
234 (define bitwise-unfold
235 (lambda (stop? mapper successor seed)
236 (do ([state seed (successor state)]
237 [idx 0 (fx+ idx 1)]
238 [i 0 (bitwise-copy-bit i idx (if (mapper state) 1 0))])
239 ((stop? state) i))))
240
241 (define make-bitwise-generator
242 (lambda (i)
243 (let ([idx 0])
244 (lambda ()
245 (let ([r (bitwise-bit-set? i idx)])
246 (set! idx (fx+ idx 1))
247 r)))))
248 ;; ---- END from SRFI-151 other ---
249 )