| 1 |
;; SRFI-60 R6RS implementation |
| 2 |
;; |
| 3 |
;; Builds out the SRFI-60 specified bitwise operators using the set of bitwise |
| 4 |
;; operators that is part of the standard R6RS library. In some cases these |
| 5 |
;; could directly use Chez Scheme library procedures directly, but this library |
| 6 |
;; does not do that yet. |
| 7 |
;; |
| 8 |
;; Copyright (c) 2018 - 2020 Andrew W. Keep |
| 9 |
|
| 10 |
(library (srfi :60 integer-bits) |
| 11 |
(export logand bitwise-and logior bitwise-ior logxor bitwise-xor lognot |
| 12 |
bitwise-not bitwise-if bitwise-merge logtest any-bits-set? logcount |
| 13 |
bit-count integer-length log2-binary-factors first-set-bit logbit? |
| 14 |
bit-set? copy-bit bit-field copy-bit-field ash arithmetic-shift |
| 15 |
rotate-bit-field reverse-bit-field integer->list |
| 16 |
list->integer booleans->integer) |
| 17 |
(import (rnrs)) |
| 18 |
|
| 19 |
(define logand |
| 20 |
(case-lambda |
| 21 |
[() (bitwise-and)] |
| 22 |
[(i) i] |
| 23 |
[(i j) (bitwise-and i j)] |
| 24 |
[(i j k) (bitwise-and i j k)] |
| 25 |
[args (apply bitwise-and args)])) |
| 26 |
|
| 27 |
(define logior |
| 28 |
(case-lambda |
| 29 |
[() (bitwise-ior)] |
| 30 |
[(i) i] |
| 31 |
[(i j) (bitwise-ior i j)] |
| 32 |
[(i j k) (bitwise-ior i j k)] |
| 33 |
[args (apply bitwise-ior args)])) |
| 34 |
|
| 35 |
(define logxor |
| 36 |
(case-lambda |
| 37 |
[() (bitwise-xor)] |
| 38 |
[(i) i] |
| 39 |
[(i j) (bitwise-xor i j)] |
| 40 |
[(i j k) (bitwise-xor i j k)] |
| 41 |
[args (apply bitwise-xor args)])) |
| 42 |
|
| 43 |
(define lognot (lambda (n) (bitwise-not n))) |
| 44 |
|
| 45 |
(define bitwise-merge (lambda (m n0 n1) (bitwise-if m n0 n1))) |
| 46 |
|
| 47 |
(define logtest (lambda (j k) (not (zero? (logand j k))))) |
| 48 |
(define any-bits-set? (lambda (j k) (not (zero? (logand j k))))) |
| 49 |
|
| 50 |
(define logcount |
| 51 |
(lambda (n) |
| 52 |
(if (< n 0) |
| 53 |
(bitwise-bit-count (bitwise-not n)) |
| 54 |
(bitwise-bit-count n)))) |
| 55 |
|
| 56 |
(define bit-count |
| 57 |
(lambda (n) |
| 58 |
(if (< n 0) |
| 59 |
(bitwise-bit-count (bitwise-not n)) |
| 60 |
(bitwise-bit-count n)))) |
| 61 |
|
| 62 |
(define integer-length (lambda (n) (bitwise-length n))) |
| 63 |
|
| 64 |
(define log2-binary-factors (lambda (n) (bitwise-first-bit-set n))) |
| 65 |
(define first-set-bit (lambda (n) (bitwise-first-bit-set n))) |
| 66 |
|
| 67 |
(define logbit? (lambda (i n) (bitwise-bit-set? n i))) |
| 68 |
(define bit-set? (lambda (i n) (bitwise-bit-set? n i))) |
| 69 |
|
| 70 |
(define copy-bit (lambda (i n b) (bitwise-copy-bit n i (if b 1 0)))) |
| 71 |
|
| 72 |
(define bit-field (lambda (n s e) (bitwise-bit-field n s e))) |
| 73 |
|
| 74 |
(define copy-bit-field |
| 75 |
(lambda (to from s e) |
| 76 |
(bitwise-copy-bit-field to s e from))) |
| 77 |
|
| 78 |
(define ash (lambda (n count) (bitwise-arithmetic-shift n count))) |
| 79 |
(define arithmetic-shift |
| 80 |
(lambda (n count) |
| 81 |
(bitwise-arithmetic-shift n count))) |
| 82 |
|
| 83 |
(define rotate-bit-field |
| 84 |
(lambda (n count start end) |
| 85 |
(bitwise-rotate-bit-field n start end count))) |
| 86 |
|
| 87 |
(define reverse-bit-field |
| 88 |
(lambda (n start end) |
| 89 |
(bitwise-reverse-bit-field n start end))) |
| 90 |
|
| 91 |
(define integer->list |
| 92 |
(case-lambda |
| 93 |
[(k len) (let loop ([i len] [ls '()]) |
| 94 |
(if (fx=? i 0) |
| 95 |
ls |
| 96 |
(let ([i (fx- i 1)]) |
| 97 |
(loop i (cons (bitwise-bit-set? k i) ls)))))] |
| 98 |
[(k) (integer->list k (bitwise-length k))])) |
| 99 |
|
| 100 |
(define list->integer |
| 101 |
(lambda (ls) |
| 102 |
(let loop ([ls ls] [i 0] [n 0]) |
| 103 |
(if (null? ls) |
| 104 |
n |
| 105 |
(loop (cdr ls) (fx+ i 1) (bitwise-copy-bit n i (if (car ls) 1 0))))))) |
| 106 |
|
| 107 |
(define booleans->integer |
| 108 |
(lambda args |
| 109 |
(list->integer args)))) |