srfi-151

Fork of the SRFI-151 reference implementation for CHICKEN

git clone https://git.8pit.net/srfi-151.git

 1;;;; chicken implementation of SRFI 151
 2(module srfi-151 ()
 3  (import scheme)
 4  (cond-expand
 5    (chicken-5
 6     (import (only (chicken base) include case-lambda when)))
 7    (chicken-6
 8     (import (only (scheme base) include when)
 9             (scheme case-lambda))))
10  (import (only (chicken module) export))
11
12  ;; Provides bitwise-not, bitwise-and, butwise-ior, bitwise-xor,
13  ;; arithmetic-shift, integer-length.  The remaining
14  ;; core function, bit-count, is provided in this file.
15  (import (chicken bitwise))
16
17  (export bitwise-not bitwise-and bitwise-ior bitwise-xor bitwise-eqv
18          bitwise-nand bitwise-nor bitwise-andc1 bitwise-andc2
19          bitwise-orc1 bitwise-orc2)
20  (export arithmetic-shift bit-count integer-length bitwise-if 
21          bit-set? copy-bit bit-swap any-bit-set? every-bit-set?  first-set-bit)
22  (export bit-field bit-field-any? bit-field-every?  bit-field-clear bit-field-set
23          bit-field-replace  bit-field-replace-same
24          bit-field-rotate bit-field-reverse)
25  (export bits->list list->bits bits->vector vector->bits bits
26          bitwise-fold bitwise-for-each bitwise-unfold make-bitwise-generator)
27
28  (define bit-count
29    (letrec ((logcnt (lambda (n tot)
30                       (if (zero? n)
31                           tot
32                           (logcnt (quotient n 16)
33                                   (+ (vector-ref
34                                       '#(0 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4)
35                                       (modulo n 16))
36                                      tot))))))
37      (lambda (n)
38        (cond ((negative? n) (logcnt (bitwise-not n) 0))
39              ((positive? n) (logcnt n 0))
40              (else 0)))))
41
42  (include "srfi-151/bitwise-33.scm")
43  (include "srfi-151/bitwise-60.scm")
44  (include "srfi-151/bitwise-other.scm")
45)