Changeset 39177 in project


Ignore:
Timestamp:
11/08/20 20:21:39 (4 weeks ago)
Author:
Kon Lovett
Message:

add bitwise-split/join not inverse under sign test, reflow

Location:
release/5/bitwise-utils/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • release/5/bitwise-utils/trunk/bitwise-utils.scm

    r39174 r39177  
    8383(define (bitwise-abs n) (if (negative? n) (bitwise-not n) n))
    8484
    85 (define (bitwise-drop-right n w)
    86   (bitwise-and (arithmetic-shift-right n w) (bitwise-ones (- (integer-length n) w))) )
    87 
    88 (define (bitwise-cons a b)
    89   (bitwise-ior (logical-shift-left a (integer-length b)) b) )
     85(define (bitwise-drop-right n w) (bitwise-and (arithmetic-shift-right n w)
     86                                              (bitwise-ones (- (integer-length n) w))))
     87
     88(define (bitwise-cons a b) (bitwise-ior (logical-shift-left a (integer-length b)) b))
    9089
    9190;5 #t => +0...011111
    9291;5 #f => -1...100000
    93 (define (bitwise-mask b #!optional (on? #t))
    94   (if on? (bitwise-ones b)
    95     (bitwise-zeros b) ) )
     92(define (bitwise-mask b #!optional (on? #t)) (if on? (bitwise-ones b) (bitwise-zeros b)))
    9693
    9794;preserves sign - doesn't sign extend
     
    103100
    104101;#b10 #b0000001 #b101 => #b101101
    105 (define (bitwise-join n . ns)
    106   (foldl (cut bitwise-cons <> <>) n ns) )
     102(define (bitwise-join n . ns) (foldl bitwise-cons n ns))
    107103
    108104;babcdef 2 => ba bc de f
     
    163159
    164160(define (bitwise-merge mask n0 n1)
    165   (bitwise-ior
    166     (bitwise-and mask n0)
    167     (bitwise-and (bitwise-not mask) n1)) )
    168 
    169 (define (bitwise-nth? index n)
    170   (bit->boolean n index) )
    171 
    172 (define (bitwise-any? n1 n2)
    173   (not (zero? (bitwise-and n1 n2))) )
    174 
    175 (define (bitwise-first-set n)
    176   (sub1 (integer-length (bitwise-and n (- n)))) )
     161  (bitwise-ior  (bitwise-and mask n0)
     162                (bitwise-and (bitwise-not mask) n1)) )
     163
     164(define (bitwise-nth? index n) (bit->boolean n index))
     165
     166(define (bitwise-any? n1 n2) (not (zero? (bitwise-and n1 n2))))
     167
     168(define (bitwise-first-set n) (sub1 (integer-length (bitwise-and n (- n)))))
    177169
    178170(define (bitwise-reverse n k)
     
    182174      ((negative? k) (if (negative? n) (bitwise-not rvs) rvs))))
    183175
    184 (define (bitwise-rotate k count len)
    185   (bitwise-field-rotate k count 0 len) )
     176(define (bitwise-rotate k count len) (bitwise-field-rotate k count 0 len))
    186177
    187178(define (bitwise-set-nth to index on?)
    188   (if on?
    189     (bitwise-ior to (arithmetic-shift 1 index))
     179  (if on? (bitwise-ior to (arithmetic-shift 1 index))
    190180    (bitwise-and to (bitwise-not (arithmetic-shift 1 index))) ) )
    191181
    192182(define (bitwise-field n start end)
    193   (bitwise-and
    194     (bitwise-not (arithmetic-shift -1 (- end start)))
    195           (arithmetic-shift-right n start)) )
     183  (bitwise-and  (bitwise-not (arithmetic-shift -1 (- end start)))
     184                (arithmetic-shift-right n start)) )
    196185
    197186(define (bitwise-field-copy to from start end)
     
    202191
    203192(define (bitwise-field-reverse n start end)
    204   (let* (
    205     (width (- end start))
    206     (mask (bitwise-ones width))
    207     (zn (bitwise-and mask (arithmetic-shift-right n start))) )
    208     (bitwise-ior
    209       (arithmetic-shift (bitwise-reverse zn width) start)
    210             (bitwise-and (bitwise-not (arithmetic-shift mask start)) n)) ) )
     193  (let* ((width (- end start))
     194         (mask (bitwise-ones width))
     195         (zn (bitwise-and mask (arithmetic-shift-right n start))) )
     196    (bitwise-ior  (arithmetic-shift (bitwise-reverse zn width) start)
     197                        (bitwise-and (bitwise-not (arithmetic-shift mask start)) n)) ) )
    211198
    212199(define (bitwise-field-rotate n count start end)
    213   (let* (
    214     (width (- end start))
    215     (count (modulo count width))
    216     (mask (bitwise-ones width))
    217     (zn (bitwise-and mask (arithmetic-shift-right n start))) )
     200  (let* ((width (- end start))
     201         (count (modulo count width))
     202         (mask (bitwise-ones width))
     203         (zn (bitwise-and mask (arithmetic-shift-right n start))) )
    218204    (bitwise-ior
    219205      (arithmetic-shift
    220         (bitwise-ior
    221           (bitwise-and mask (arithmetic-shift zn count))
    222           (arithmetic-shift zn (- count width)))
     206        (bitwise-ior  (bitwise-and mask (arithmetic-shift zn count))
     207                      (arithmetic-shift zn (- count width)))
    223208        start)
    224209            (bitwise-and (bitwise-not (arithmetic-shift mask start)) n)) ) )
  • release/5/bitwise-utils/trunk/tests/bitwise-utils-test.scm

    r38599 r39177  
    1010(import bitwise-utils)
    1111(import (chicken bitwise))
     12
     13(define (hex->number x) (string->number x 16))
     14(define (number->hex x) (number->string x 16))
     15(define (negate x) (* -1 x))
    1216
    1317;(bitwise-join #t 23 '())
     
    5155(test '(0) (bitwise-split 0 0))
    5256
    53 (test '("a" "b" "c" "d" "e" "f") (map (cut number->string <> 16) (bitwise-split #xabcdef 4)))
    54 (test '("-a" "-b" "-c" "-d" "-e" "-f") (map (cut number->string <> 16) (bitwise-split #x-abcdef 4)))
     57(test '("a" "b" "c" "d" "e" "f") (map number->hex (bitwise-split #xabcdef 4)))
     58(test '("-a" "-b" "-c" "-d" "-e" "-f") (map number->hex (bitwise-split #x-abcdef 4)))
    5559
    56 (test '("b0000000" "0" "110000deadbeef") (map (cut number->string <> 16) (bitwise-split #xb0000000000000000000000000110000deadbeef 64)))
    57 (test '("-b0000000" "0" "-110000deadbeef") (map (cut number->string <> 16) (bitwise-split #x-b0000000000000000000000000110000deadbeef 64)))
     60;NOTE not bitwise-join <inverse> bitwise-split due to sign
     61(test #xabcdef (apply bitwise-join (map hex->number '("a" "b" "c" "d" "e" "f"))))
     62(test #x-abcdef (negate (apply bitwise-join (map (o negate hex->number) '("-a" "-b" "-c" "-d" "-e" "-f")))))
     63
     64(test '("b0000000" "0" "110000deadbeef") (map number->hex (bitwise-split #xb0000000000000000000000000110000deadbeef 64)))
     65(test '("-b0000000" "0" "-110000deadbeef") (map number->hex (bitwise-split #x-b0000000000000000000000000110000deadbeef 64)))
    5866
    5967(test 29 (bitwise-count #xb0000000000000000000000000110000deadbeef))
Note: See TracChangeset for help on using the changeset viewer.