Changeset 66f3bae


Ignore:
Timestamp:
Apr 19, 2020, 10:34:26 PM (4 years ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
5847d35, 61248a4
Parents:
692e6f1
Message:

formatting

File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/bitmanip.hfa

    r692e6f1 r66f3bae  
    1111// Created On       : Sat Mar 14 18:12:27 2020
    1212// Last Modified By : Peter A. Buhr
    13 // Last Modified On : Thu Apr 16 18:09:34 2020
    14 // Update Count     : 120
     13// Last Modified On : Sun Apr 19 22:29:58 2020
     14// Update Count     : 121
    1515//
    1616
     
    2626
    2727static inline {
    28     // Count leading 0 bits.
    29     unsigned int leading0s( unsigned char n ) { return n != 0 ? __builtin_clz( n ) - (__bitsizeof(unsigned int) - __bitsizeof(n)) : __bitsizeof(n); }
    30     unsigned int leading0s( unsigned short int n ) { return n != 0 ? __builtin_clz( n ) - (__bitsizeof(unsigned int) - __bitsizeof(n)) : __bitsizeof(n); }
    31     unsigned int leading0s( unsigned int n ) { return n != 0 ? __builtin_clz( n ) : __bitsizeof(n); }
    32     unsigned int leading0s( unsigned long int n ) { return n != 0 ? __builtin_clzl( n ) : __bitsizeof(n); }
    33     unsigned int leading0s( unsigned long long int n ) { return n != 0 ? __builtin_clzll( n ) : __bitsizeof(n); }
     28        // Count leading 0 bits.
     29        unsigned int leading0s( unsigned char n ) { return n != 0 ? __builtin_clz( n ) - (__bitsizeof(unsigned int) - __bitsizeof(n)) : __bitsizeof(n); }
     30        unsigned int leading0s( unsigned short int n ) { return n != 0 ? __builtin_clz( n ) - (__bitsizeof(unsigned int) - __bitsizeof(n)) : __bitsizeof(n); }
     31        unsigned int leading0s( unsigned int n ) { return n != 0 ? __builtin_clz( n ) : __bitsizeof(n); }
     32        unsigned int leading0s( unsigned long int n ) { return n != 0 ? __builtin_clzl( n ) : __bitsizeof(n); }
     33        unsigned int leading0s( unsigned long long int n ) { return n != 0 ? __builtin_clzll( n ) : __bitsizeof(n); }
    3434
    35     // Count trailing 0 bits.
    36     unsigned int trailing0s( unsigned char n ) { return n != 0 ? __builtin_ctz( n ) : __bitsizeof(n); }
    37     unsigned int trailing0s( unsigned short int n ) { return n != 0 ? __builtin_ctz( n ) : __bitsizeof(n); }
    38     unsigned int trailing0s( unsigned int n ) { return n != 0 ? __builtin_ctz( n ) : __bitsizeof(n); }
    39     unsigned int trailing0s( unsigned long int n ) { return n != 0 ? __builtin_ctzl( n ) : __bitsizeof(n); }
    40     unsigned int trailing0s( unsigned long long int n ) { return n != 0 ? __builtin_ctzll( n ) : __bitsizeof(n); }
     35        // Count trailing 0 bits.
     36        unsigned int trailing0s( unsigned char n ) { return n != 0 ? __builtin_ctz( n ) : __bitsizeof(n); }
     37        unsigned int trailing0s( unsigned short int n ) { return n != 0 ? __builtin_ctz( n ) : __bitsizeof(n); }
     38        unsigned int trailing0s( unsigned int n ) { return n != 0 ? __builtin_ctz( n ) : __bitsizeof(n); }
     39        unsigned int trailing0s( unsigned long int n ) { return n != 0 ? __builtin_ctzl( n ) : __bitsizeof(n); }
     40        unsigned int trailing0s( unsigned long long int n ) { return n != 0 ? __builtin_ctzll( n ) : __bitsizeof(n); }
    4141
    42     // Count all 1 bits.
    43     unsigned int all1s( unsigned char n ) { return __builtin_popcount( n ); }
    44     unsigned int all1s( unsigned short int n ) { return __builtin_popcount( n ); }
    45     unsigned int all1s( unsigned int n ) { return __builtin_popcount( n ); }
    46     unsigned int all1s( unsigned long int n ) { return __builtin_popcountl( n ); }
    47     unsigned int all1s( unsigned long long int n ) { return __builtin_popcountll( n ); }
     42        // Count all 1 bits.
     43        unsigned int all1s( unsigned char n ) { return __builtin_popcount( n ); }
     44        unsigned int all1s( unsigned short int n ) { return __builtin_popcount( n ); }
     45        unsigned int all1s( unsigned int n ) { return __builtin_popcount( n ); }
     46        unsigned int all1s( unsigned long int n ) { return __builtin_popcountl( n ); }
     47        unsigned int all1s( unsigned long long int n ) { return __builtin_popcountll( n ); }
    4848
    49     // Count all 0 bits.
    50     unsigned int all0s( unsigned char n ) { return __bitsizeof(n) - __builtin_popcount( n ); }
    51     unsigned int all0s( unsigned short int n ) { return __bitsizeof(n) - __builtin_popcount( n ); }
    52     unsigned int all0s( unsigned int n ) { return __bitsizeof(n) - __builtin_popcount( n ); }
    53     unsigned int all0s( unsigned long int n ) { return __bitsizeof(n) - __builtin_popcountl( n ); }
    54     unsigned int all0s( unsigned long long int n ) { return __bitsizeof(n) - __builtin_popcountll( n ); }
     49        // Count all 0 bits.
     50        unsigned int all0s( unsigned char n ) { return __bitsizeof(n) - __builtin_popcount( n ); }
     51        unsigned int all0s( unsigned short int n ) { return __bitsizeof(n) - __builtin_popcount( n ); }
     52        unsigned int all0s( unsigned int n ) { return __bitsizeof(n) - __builtin_popcount( n ); }
     53        unsigned int all0s( unsigned long int n ) { return __bitsizeof(n) - __builtin_popcountl( n ); }
     54        unsigned int all0s( unsigned long long int n ) { return __bitsizeof(n) - __builtin_popcountll( n ); }
    5555
    56     // Find least significiant zero bit. (ffs)
    57     unsigned int low0( unsigned char n ) { return __builtin_ffs( (typeof(n))~n ); }
    58     unsigned int low0( unsigned short int n ) { return __builtin_ffs( (typeof(n))~n ); }
    59     unsigned int low0( unsigned int n ) { return __builtin_ffs( ~n ); }
    60     unsigned int low0( unsigned long int n ) { return __builtin_ffsl( ~n ); }
    61     unsigned int low0( unsigned long long int n ) { return __builtin_ffsll( ~n ); }
     56        // Find least significiant zero bit. (ffs)
     57        unsigned int low0( unsigned char n ) { return __builtin_ffs( (typeof(n))~n ); }
     58        unsigned int low0( unsigned short int n ) { return __builtin_ffs( (typeof(n))~n ); }
     59        unsigned int low0( unsigned int n ) { return __builtin_ffs( ~n ); }
     60        unsigned int low0( unsigned long int n ) { return __builtin_ffsl( ~n ); }
     61        unsigned int low0( unsigned long long int n ) { return __builtin_ffsll( ~n ); }
    6262
    63     // Find least significiant one bit.
    64     unsigned int low1( unsigned int n ) { return __builtin_ffs( n ); }
    65     unsigned int low1( unsigned long int n ) { return __builtin_ffsl( n ); }
    66     unsigned int low1( unsigned long long int n ) { return __builtin_ffsll( n ); }
     63        // Find least significiant one bit.
     64        unsigned int low1( unsigned int n ) { return __builtin_ffs( n ); }
     65        unsigned int low1( unsigned long int n ) { return __builtin_ffsl( n ); }
     66        unsigned int low1( unsigned long long int n ) { return __builtin_ffsll( n ); }
    6767
    68     // Find most significiant zero bit.
    69     unsigned int high0( unsigned char n ) { return n == (typeof(n))-1 ? 0 : __bitsizeof(unsigned int) - __builtin_clz( (typeof(n))~n ); }
    70     unsigned int high0( unsigned short int n ) { return n == (typeof(n))-1 ? 0 : __bitsizeof(unsigned int) - __builtin_clz( (typeof(n))~n ); }
    71     unsigned int high0( unsigned int n ) { return n == -1 ? 0 : __bitsizeof(n) - __builtin_clz( ~n ); }
    72     unsigned int high0( unsigned long int n ) { return n == -1 ? 0 : __bitsizeof(n) - __builtin_clzl( ~n ); }
    73     unsigned int high0( unsigned long long int n ) { return n == -1 ? 0 : __bitsizeof(n) - __builtin_clzll( ~n ); }
     68        // Find most significiant zero bit.
     69        unsigned int high0( unsigned char n ) { return n == (typeof(n))-1 ? 0 : __bitsizeof(unsigned int) - __builtin_clz( (typeof(n))~n ); }
     70        unsigned int high0( unsigned short int n ) { return n == (typeof(n))-1 ? 0 : __bitsizeof(unsigned int) - __builtin_clz( (typeof(n))~n ); }
     71        unsigned int high0( unsigned int n ) { return n == -1 ? 0 : __bitsizeof(n) - __builtin_clz( ~n ); }
     72        unsigned int high0( unsigned long int n ) { return n == -1 ? 0 : __bitsizeof(n) - __builtin_clzl( ~n ); }
     73        unsigned int high0( unsigned long long int n ) { return n == -1 ? 0 : __bitsizeof(n) - __builtin_clzll( ~n ); }
    7474
    75     // Find most significiant one bit.
    76     unsigned int high1( unsigned char n ) { return n == 0 ? 0 : __bitsizeof(unsigned int) - __builtin_clz( n ); }
    77     unsigned int high1( unsigned short int n ) { return n == 0 ? 0 : __bitsizeof(unsigned int) - __builtin_clz( n ); }
    78     unsigned int high1( unsigned int n ) { return n == 0 ? 0 : __bitsizeof(n) - __builtin_clz( n ); }
    79     unsigned int high1( unsigned long int n ) { return n == 0 ? 0 : __bitsizeof(n) - __builtin_clzl( n ); }
    80     unsigned int high1( unsigned long long int n ) { return n == 0 ? 0 : __bitsizeof(n) - __builtin_clzll( n ); }
     75        // Find most significiant one bit.
     76        unsigned int high1( unsigned char n ) { return n == 0 ? 0 : __bitsizeof(unsigned int) - __builtin_clz( n ); }
     77        unsigned int high1( unsigned short int n ) { return n == 0 ? 0 : __bitsizeof(unsigned int) - __builtin_clz( n ); }
     78        unsigned int high1( unsigned int n ) { return n == 0 ? 0 : __bitsizeof(n) - __builtin_clz( n ); }
     79        unsigned int high1( unsigned long int n ) { return n == 0 ? 0 : __bitsizeof(n) - __builtin_clzl( n ); }
     80        unsigned int high1( unsigned long long int n ) { return n == 0 ? 0 : __bitsizeof(n) - __builtin_clzll( n ); }
    8181
    82     // Check for power of 2, clears bits below n, rounding down to the next lower multiple of n.
    83     bool is_pow2( unsigned char n ) { return n == 0 ? false : (n & (n - 1)) == 0; }
    84     bool is_pow2( unsigned short int n ) { return n == 0 ? false : (n & (n - 1)) == 0; }
    85     bool is_pow2( unsigned int n ) { return n == 0 ? false : (n & (n - 1)) == 0; }
    86     bool is_pow2( unsigned long int n ) { return n == 0 ? false : (n & (n - 1)) == 0; }
    87     bool is_pow2( unsigned long long int n ) { return n == 0 ? false : (n & (n - 1)) == 0; }
     82        // Check for power of 2, clears bits below n, rounding down to the next lower multiple of n.  0 is not a power of 2
     83        // but this computation returns true because of the two's complement, so it is a special case.
     84        bool is_pow2( unsigned char n ) { return n == 0 ? false : (n & (n - 1)) == 0; }
     85        bool is_pow2( unsigned short int n ) { return n == 0 ? false : (n & (n - 1)) == 0; }
     86        bool is_pow2( unsigned int n ) { return n == 0 ? false : (n & (n - 1)) == 0; }
     87        bool is_pow2( unsigned long int n ) { return n == 0 ? false : (n & (n - 1)) == 0; }
     88        bool is_pow2( unsigned long long int n ) { return n == 0 ? false : (n & (n - 1)) == 0; }
    8889
    89     // Returns n aligned at the floor of align, clear bits above or equal to align, giving n % align.
    90     signed char floor2( signed char n, char align ) { /*assert( is_pow2( align ) );*/ return n & -align; }
    91     unsigned char floor2( unsigned char n, unsigned char align ) { /*assert( is_pow2( align ) );*/ return n & -align; }
    92     short int floor2( short int n, short int align ) { /*assert( is_pow2( align ) );*/ return n & -align; }
    93     unsigned short int floor2( unsigned short int n, unsigned short int align ) { /*assert( is_pow2( align ) );*/ return n & -align; }
    94     int floor2( int n, int align ) { /*assert( is_pow2( align ) );*/ return n & -align; }
    95     unsigned int floor2( unsigned int n, unsigned int align ) { /*assert( is_pow2( align ) );*/ return n & -align; }
    96     long int floor2( long int n, long int align ) { /*assert( is_pow2( align ) );*/ return n & -align; }
    97     unsigned long int floor2( unsigned long int n, unsigned long int align ) { /*assert( is_pow2( align ) );*/ return n & -align; }
     90        // Returns n aligned at the floor of align, clear bits above or equal to align, giving n % align.
     91        signed char floor2( signed char n, char align ) { /*assert( is_pow2( align ) );*/ return n & -align; }
     92        unsigned char floor2( unsigned char n, unsigned char align ) { /*assert( is_pow2( align ) );*/ return n & -align; }
     93        short int floor2( short int n, short int align ) { /*assert( is_pow2( align ) );*/ return n & -align; }
     94        unsigned short int floor2( unsigned short int n, unsigned short int align ) { /*assert( is_pow2( align ) );*/ return n & -align; }
     95        int floor2( int n, int align ) { /*assert( is_pow2( align ) );*/ return n & -align; }
     96        unsigned int floor2( unsigned int n, unsigned int align ) { /*assert( is_pow2( align ) );*/ return n & -align; }
     97        long int floor2( long int n, long int align ) { /*assert( is_pow2( align ) );*/ return n & -align; }
     98        unsigned long int floor2( unsigned long int n, unsigned long int align ) { /*assert( is_pow2( align ) );*/ return n & -align; }
    9899        long long int floor2( long long int n, long long int align ) { /*assert( is_pow2( align ) );*/ return n & -align; }
    99     unsigned long long int floor2( unsigned long long int n, unsigned long long int align ) { /*assert( is_pow2( align ) );*/ return n & -align; }
     100        unsigned long long int floor2( unsigned long long int n, unsigned long long int align ) { /*assert( is_pow2( align ) );*/ return n & -align; }
    100101
    101102        // forall( otype T | { T ?&?( T, T ); T -?( T ); } )
    102103        // T floor2( T n, T align ) { /* assert( is_pow2( align ) ); */ return n & -align; }
    103104
    104     signed char floor( signed char n, char align ) { return n / align * align; }
    105     unsigned char floor( unsigned char n, unsigned char align ) { return n / align * align; }
    106     short int floor( short int n, short int align ) { return n / align * align; }
    107     unsigned short int floor( unsigned short int n, unsigned short int align ) { return n / align * align; }
    108     int floor( int n, int align ) { return n / align * align; }
    109     unsigned int floor( unsigned int n, unsigned int align ) { return n / align * align; }
    110     long int floor( long int n, long int align ) { return n / align * align; }
    111     unsigned long int floor( unsigned long int n, unsigned long int align ) { return n / align * align; }
     105        signed char floor( signed char n, char align ) { return n / align * align; }
     106        unsigned char floor( unsigned char n, unsigned char align ) { return n / align * align; }
     107        short int floor( short int n, short int align ) { return n / align * align; }
     108        unsigned short int floor( unsigned short int n, unsigned short int align ) { return n / align * align; }
     109        int floor( int n, int align ) { return n / align * align; }
     110        unsigned int floor( unsigned int n, unsigned int align ) { return n / align * align; }
     111        long int floor( long int n, long int align ) { return n / align * align; }
     112        unsigned long int floor( unsigned long int n, unsigned long int align ) { return n / align * align; }
    112113        long long int floor( long long int n, long long int align ) { return n / align * align; }
    113     unsigned long long int floor( unsigned long long int n, unsigned long long int align ) { return n / align * align; }
     114        unsigned long long int floor( unsigned long long int n, unsigned long long int align ) { return n / align * align; }
    114115
    115116        // forall( otype T | { T ?/?( T, T ); T ?*?( T, T ); } )
    116117        // T floor( T n, T align ) { return n / align * align; }
    117118
    118     // Returns n aligned at the ceiling of align, negate, round down, negate is the same as round up.
    119     signed char ceiling2( signed char n, char align ) { /*assert( is_pow2( align ) );*/ return -floor2( -n, align ); }
    120     unsigned char ceiling2( unsigned char n, unsigned char align ) { /*assert( is_pow2( align ) );*/ return -floor2( -n, align ); }
    121     short int ceiling2( short int n, short int align ) { /*assert( is_pow2( align ) );*/ return -floor2( -n, align ); }
    122     unsigned short int ceiling2( unsigned short int n, unsigned short int align ) { /*assert( is_pow2( align ) );*/ return -floor2( -n, align ); }
    123     int ceiling2( int n, int align ) { /*assert( is_pow2( align ) );*/ return -floor2( -n, align ); }
    124     unsigned int ceiling2( unsigned int n, unsigned int align ) { /*assert( is_pow2( align ) );*/ return -floor2( -n, align ); }
    125     long int ceiling2( long int n, long int align ) { /*assert( is_pow2( align ) );*/ return -floor2( -n, align ); }
    126     unsigned long int ceiling2( unsigned long int n, unsigned long int align ) { /*assert( is_pow2( align ) );*/ return -floor2( -n, align ); }
     119        // Returns n aligned at the ceiling of align, negate, round down, negate is the same as round up.
     120        signed char ceiling2( signed char n, char align ) { /*assert( is_pow2( align ) );*/ return -floor2( -n, align ); }
     121        unsigned char ceiling2( unsigned char n, unsigned char align ) { /*assert( is_pow2( align ) );*/ return -floor2( -n, align ); }
     122        short int ceiling2( short int n, short int align ) { /*assert( is_pow2( align ) );*/ return -floor2( -n, align ); }
     123        unsigned short int ceiling2( unsigned short int n, unsigned short int align ) { /*assert( is_pow2( align ) );*/ return -floor2( -n, align ); }
     124        int ceiling2( int n, int align ) { /*assert( is_pow2( align ) );*/ return -floor2( -n, align ); }
     125        unsigned int ceiling2( unsigned int n, unsigned int align ) { /*assert( is_pow2( align ) );*/ return -floor2( -n, align ); }
     126        long int ceiling2( long int n, long int align ) { /*assert( is_pow2( align ) );*/ return -floor2( -n, align ); }
     127        unsigned long int ceiling2( unsigned long int n, unsigned long int align ) { /*assert( is_pow2( align ) );*/ return -floor2( -n, align ); }
    127128        long long int ceiling2( long long int n, long long int align ) { /*assert( is_pow2( align ) );*/ return -floor2( -n, align ); }
    128     unsigned long long int ceiling2( unsigned long long int n, unsigned long long int align ) { /*assert( is_pow2( align ) );*/ return -floor2( -n, align ); }
     129        unsigned long long int ceiling2( unsigned long long int n, unsigned long long int align ) { /*assert( is_pow2( align ) );*/ return -floor2( -n, align ); }
    129130
    130131        // forall( otype T | { T floor2( T, T ); T -?( T ); } )
     
    132133
    133134        signed char ceiling( signed char n, char align ) { return (n + (align - 1)) / align; }
    134     unsigned char ceiling( unsigned char n, unsigned char align ) { return (n + (align - 1)) / align; }
    135     short int ceiling( short int n, short int align ) { return (n + (align - 1)) / align; }
    136     unsigned short int ceiling( unsigned short int n, unsigned short int align ) { return (n + (align - 1)) / align; }
    137     int ceiling( int n, int align ) { return (n + (align - 1)) / align; }
    138     unsigned int ceiling( unsigned int n, unsigned int align ) { return (n + (align - 1)) / align; }
    139     long int ceiling( long int n, long int align ) { return (n + (align - 1)) / align; }
    140     unsigned long int ceiling( unsigned long int n, unsigned long int align ) { return (n + (align - 1)) / align; }
     135        unsigned char ceiling( unsigned char n, unsigned char align ) { return (n + (align - 1)) / align; }
     136        short int ceiling( short int n, short int align ) { return (n + (align - 1)) / align; }
     137        unsigned short int ceiling( unsigned short int n, unsigned short int align ) { return (n + (align - 1)) / align; }
     138        int ceiling( int n, int align ) { return (n + (align - 1)) / align; }
     139        unsigned int ceiling( unsigned int n, unsigned int align ) { return (n + (align - 1)) / align; }
     140        long int ceiling( long int n, long int align ) { return (n + (align - 1)) / align; }
     141        unsigned long int ceiling( unsigned long int n, unsigned long int align ) { return (n + (align - 1)) / align; }
    141142        long long int ceiling( long long int n, long long int align ) { return (n + (align - 1)) / align; }
    142     unsigned long long int ceiling( unsigned long long int n, unsigned long long int align ) { return (n + (align - 1)) / align; }
     143        unsigned long long int ceiling( unsigned long long int n, unsigned long long int align ) { return (n + (align - 1)) / align; }
    143144
    144145        // forall( otype T | { void ?{}( T &, one_t ); T ?+?( T, T ); T ?-?( T, T ); T ?/?( T, T ); } )
Note: See TracChangeset for help on using the changeset viewer.