Changeset 38737 in project for release


Ignore:
Timestamp:
06/05/20 03:56:03 (2 months ago)
Author:
Kon Lovett
Message:

add 64bit uword-bitwise-count

File:
1 edited

Legend:

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

    r38606 r38737  
    2424;promotional, or sales literature without prior written consent in
    2525;each case.
    26 
    27 #>
    28 /* Number of 1 bits */
    29 static unsigned int
    30 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_FOUR
    44         n = COUNT( n, 5 );
    45 # endif
    46 
    47         return (unsigned int) n;
    48 
    49 # undef COUNT
    50 # undef MASK
    51 # undef TWO
    52 }
    53 <#
    5426
    5527(module bitwise-utils
     
    7042
    7143(import scheme)
    72 (import (only (chicken base) sub1 add1 fixnum? foldl cut))
     44(import (only (chicken base) declare sub1 add1 fixnum? foldl cut))
    7345(import (chicken type))
    7446(import (chicken foreign))
     
    148120(define *uword-size* (foreign-type-size "C_uword"))
    149121
     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
    150128(cond-expand
    151129  (64bit
    152130    (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 ) );")) )
    155139  (else ;32bit
    156140    (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|#
    159156
    160157(define (integer->uwords n) (bitwise-split n (* 8 *uword-size*)))
Note: See TracChangeset for help on using the changeset viewer.