- Timestamp:
- 06/05/20 03:56:03 (8 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
release/5/bitwise-utils/trunk/bitwise-utils.scm
r38606 r38737 24 24 ;promotional, or sales literature without prior written consent in 25 25 ;each case. 26 27 #>28 /* Number of 1 bits */29 static unsigned int30 C_uword_bits( C_uword n )31 {32 # define TWO( c ) ( ((C_uword) 1u) << (c))33 # define MASK( c ) (((C_uword) -1) / (TWO( TWO( c ) ) + 1u))34 # define COUNT( x, c ) ((x) & MASK( c )) + (((x) >> (TWO( c ))) & MASK( c ))35 36 if (0 == n) return (unsigned int) 0;37 38 n = COUNT( n, 0 );39 n = COUNT( n, 1 );40 n = COUNT( n, 2 );41 n = COUNT( n, 3 );42 n = COUNT( n, 4 );43 # ifdef C_SIXTY_FOUR44 n = COUNT( n, 5 );45 # endif46 47 return (unsigned int) n;48 49 # undef COUNT50 # undef MASK51 # undef TWO52 }53 <#54 26 55 27 (module bitwise-utils … … 70 42 71 43 (import scheme) 72 (import (only (chicken base) sub1 add1 fixnum? foldl cut))44 (import (only (chicken base) declare sub1 add1 fixnum? foldl cut)) 73 45 (import (chicken type)) 74 46 (import (chicken foreign)) … … 148 120 (define *uword-size* (foreign-type-size "C_uword")) 149 121 122 #> 123 #define TWO( c ) ( ((C_uword) 1u) << (c)) 124 #define MASK( c ) (((C_uword) -1) / (TWO( TWO( c ) ) + 1u)) 125 #define COUNT( x, c ) ((x) & MASK( c )) + (((x) >> (TWO( c ))) & MASK( c )) 126 <# 127 150 128 (cond-expand 151 129 (64bit 152 130 (define uword-bitwise-count 153 (foreign-lambda* unsigned-int ((integer64 n)) 154 "return( C_uword_bits( (C_uword) n ) );")) ) 131 (foreign-lambda* unsigned-int ((integer64 n)) " 132 if (0 == n) C_return( (unsigned int) 0 ); 133 n = COUNT( n, 0 ); 134 n = COUNT( n, 1 ); 135 n = COUNT( n, 2 ); 136 n = COUNT( n, 3 ); 137 n = COUNT( n, 4 ); 138 C_return( (unsigned int) COUNT( n, 5 ) );")) ) 155 139 (else ;32bit 156 140 (define uword-bitwise-count 157 (foreign-lambda* unsigned-int ((integer32 n)) 158 "return( C_uword_bits( (C_uword) n ) );")) ) ) 141 (foreign-lambda* unsigned-int ((integer32 n)) " 142 if (0 == n) C_return( (unsigned int) 0 ); 143 n = COUNT( n, 0 ); 144 n = COUNT( n, 1 ); 145 n = COUNT( n, 2 ); 146 n = COUNT( n, 3 ); 147 C_return( (unsigned int) COUNT( n, 4 ) );")) ) ) 148 149 #| 150 #> 151 #undef COUNT 152 #undef MASK 153 #undef TWO 154 <# 155 |# 159 156 160 157 (define (integer->uwords n) (bitwise-split n (* 8 *uword-size*)))
Note: See TracChangeset
for help on using the changeset viewer.