srfi-151

Fork of the SRFI-151 reference implementation for CHICKEN

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

  1;;;; bitwise-33 - Olin Shivers's code from SRFI-33 with modified names
  2
  3;;; Olin Shivers is the sole author of this code, and he has placed it in
  4;;; the public domain.
  5;;; 
  6;;; A good implementation might choose to provide direct compiler/interpreter
  7;;; support for these derived functions, or might simply define them to be
  8;;; integrable -- i.e., inline-expanded.
  9;;; 
 10;;; The seven non-trivial boolean functions in terms
 11;;; of not, and, or & xor.
 12
 13(define (bitwise-nand  i j)  (bitwise-not (bitwise-and i j)))
 14(define (bitwise-nor   i j)  (bitwise-not (bitwise-ior i j)))
 15(define (bitwise-andc1 i j)  (bitwise-and (bitwise-not i) j))	
 16(define (bitwise-andc2 i j)  (bitwise-and i (bitwise-not j)))	
 17(define (bitwise-orc1  i j)  (bitwise-ior (bitwise-not i) j))
 18(define (bitwise-orc2  i j)  (bitwise-ior i (bitwise-not j)))
 19
 20;;; This is a general definition, but less than efficient.  It should also
 21;;; receive primitive compiler/interpreter support so that the expensive
 22;;; n-ary mechanism is not invoked in the standard cases -- that is,
 23;;; an application of BITWISE-EQV should be rewritten into an equivalent
 24;;; tree applying some two-argument primitive to the arguments, in the
 25;;; same manner that statically-known n-ary applications of associative
 26;;; operations such as + and * are handled efficiently:
 27;;;   (bitwise-eqv)         => -1
 28;;;   (bitwise-eqv i)       => i
 29;;;   (bitwise-eqv i j)     => (%bitwise-eqv i j)
 30;;;   (bitwise-eqv i j k)   => (%bitwise-eqv (%bitwise-eqv i j) k)
 31;;;   (bitwise-eqv i j k l) => (%bitwise-eqv (%bitwise-eqv (%bitwise-eqv i j) k) l)
 32
 33(define (bitwise-eqv . args)
 34  (let lp ((args args) (ans -1))
 35    (if (pair? args)
 36        (lp (cdr args) (bitwise-not (bitwise-xor ans (car args))))
 37	ans)))
 38
 39;;; Helper function -- make a mask of SIZE 1-bits, e.g. (%MASK 3) = #b111.
 40;;; Suppose your Scheme's fixnums are N bits wide (counting the sign bit,
 41;;; not counting any tag bits). This version, due to Marc Feeley, will 
 42;;; handle SIZE in the range [0,N-1] without overflowing to bignums. 
 43;;; (For SIZE >= N, the correct bignum value is also produced.)
 44
 45(define (mask start end) (bitwise-not (arithmetic-shift -1 (- end start))))
 46
 47;;; This alternate, mathematically-equivalent expression
 48;;;     (- (arithmetic-shift 1 size) 1)
 49;;; is not as good -- it only handles SIZE in the range [0,N-2] without
 50;;; overflowing to bignums.
 51;;;
 52;;; Finally, note that even Feeley's expression can't build an N-bit mask
 53;;; without bignum help. This is fundamental, since the interpretation
 54;;; of fixed-size fixnum bit patterns as semi-infinite-bit-strings is that
 55;;; you replicate the high bit out to infinity. So you have to have a
 56;;; zero "stop bit" appearing after that highest one bit to turn off the
 57;;; replication of the ones.
 58
 59(define (bit-set? index n) 
 60  (not (zero? (bitwise-and (arithmetic-shift 1 index) n))))
 61
 62(define (any-bit-set? test-bits n) (not (zero? (bitwise-and test-bits n))))
 63
 64(define (every-bit-set? test-bits n) (= test-bits (bitwise-and test-bits n)))
 65
 66;;; Bit-field ops
 67
 68(define (bit-field n start end)
 69  (bitwise-and (mask start end) (arithmetic-shift n (- start))))
 70
 71(define (bit-field-any? n start end)
 72  (not (zero? (bitwise-and (arithmetic-shift n (- start)) (mask start end)))))
 73
 74;; Part of Olin's late revisions; code by John Cowan; public domain.
 75(define (bit-field-every? n start end)
 76  (let ((m (mask start end)))
 77    (eqv? m (bitwise-and (arithmetic-shift n (- start)) m))))
 78
 79;; Integrating i-b-f reduces nicely.
 80(define (bit-field-clear n start end)
 81  (bit-field-replace n 0 start end))
 82
 83;; Counterpart to above, not in SRFI 33, written by John Cowan, public domain
 84(define (bit-field-set n start end)
 85  (bit-field-replace n -1 start end))
 86
 87;;; Oops -- intermediate ARITHMETIC-SHIFT can fixnum-overflow on fixnum args.
 88;(define (bit-field-replace newfield n start end)
 89;  (bit-field-replace-same (arithmetic-shift newfield start) n start end))
 90
 91;;; This three-line version won't fixnum-overflow on fixnum args.
 92(define (bit-field-replace n newfield start end)
 93  (let ((m (mask start end)))
 94    (bitwise-ior (bitwise-and n (bitwise-not (arithmetic-shift m start)))
 95		 (arithmetic-shift (bitwise-and newfield m) start))))
 96
 97(define (bit-field-replace-same to from start end)
 98  (bitwise-if (arithmetic-shift (mask start end) start) from to))
 99
100;; Simple definition
101;(define (first-set-bit i)
102;  (and (not (zero? i))
103;       (let lp ((j 0) (i start))
104;         (if (bit-set? i 0) j
105;             (lp (+ j 1) (arithmetic-shift i 1))))))
106
107;;; Clever definition, assuming you have a fast BIT-COUNT.
108(define (first-set-bit i) (- (bit-count (bitwise-xor i (- i 1))) 1))
109