1 | // -*- Mode: C -*-
|
---|
2 | //
|
---|
3 | // Cforall Version 1.0.0 Copyright (C) 2020 University of Waterloo
|
---|
4 | //
|
---|
5 | // The contents of this file are covered under the licence agreement in the
|
---|
6 | // file "LICENCE" distributed with Cforall.
|
---|
7 | //
|
---|
8 | // bitmanip.hfa --
|
---|
9 | //
|
---|
10 | // Author : Peter A. Buhr
|
---|
11 | // Created On : Sat Mar 14 18:12:27 2020
|
---|
12 | // Last Modified By : Peter A. Buhr
|
---|
13 | // Last Modified On : Sat Oct 8 08:28:15 2022
|
---|
14 | // Update Count : 142
|
---|
15 | //
|
---|
16 |
|
---|
17 | #pragma once
|
---|
18 |
|
---|
19 | // Reference: Bit Twiddling Hacks: http://graphics.stanford.edu/%7Eseander/bithacks.html#CountBitsSetNaive
|
---|
20 |
|
---|
21 | // Bits are numbered 1-N.
|
---|
22 |
|
---|
23 | #define __bitsizeof( n ) (sizeof(n) * __CHAR_BIT__)
|
---|
24 |
|
---|
25 | static inline __attribute__((always_inline)) {
|
---|
26 | // Count leading 0 bits.
|
---|
27 | unsigned int leading0s( unsigned char n ) { return n != 0 ? __builtin_clz( n ) - (__bitsizeof(unsigned int) - __bitsizeof(n)) : __bitsizeof(n); }
|
---|
28 | unsigned int leading0s( unsigned short int n ) { return n != 0 ? __builtin_clz( n ) - (__bitsizeof(unsigned int) - __bitsizeof(n)) : __bitsizeof(n); }
|
---|
29 | unsigned int leading0s( unsigned int n ) { return n != 0 ? __builtin_clz( n ) : __bitsizeof(n); }
|
---|
30 | unsigned int leading0s( unsigned long int n ) { return n != 0 ? __builtin_clzl( n ) : __bitsizeof(n); }
|
---|
31 | unsigned int leading0s( unsigned long long int n ) { return n != 0 ? __builtin_clzll( n ) : __bitsizeof(n); }
|
---|
32 |
|
---|
33 | // Count trailing 0 bits.
|
---|
34 | unsigned int trailing0s( unsigned char n ) { return n != 0 ? __builtin_ctz( n ) : __bitsizeof(n); }
|
---|
35 | unsigned int trailing0s( unsigned short int n ) { return n != 0 ? __builtin_ctz( n ) : __bitsizeof(n); }
|
---|
36 | unsigned int trailing0s( unsigned int n ) { return n != 0 ? __builtin_ctz( n ) : __bitsizeof(n); }
|
---|
37 | unsigned int trailing0s( unsigned long int n ) { return n != 0 ? __builtin_ctzl( n ) : __bitsizeof(n); }
|
---|
38 | unsigned int trailing0s( unsigned long long int n ) { return n != 0 ? __builtin_ctzll( n ) : __bitsizeof(n); }
|
---|
39 |
|
---|
40 | // Count all 1 bits.
|
---|
41 | unsigned int all1s( unsigned char n ) { return __builtin_popcount( n ); }
|
---|
42 | unsigned int all1s( unsigned short int n ) { return __builtin_popcount( n ); }
|
---|
43 | unsigned int all1s( unsigned int n ) { return __builtin_popcount( n ); }
|
---|
44 | unsigned int all1s( unsigned long int n ) { return __builtin_popcountl( n ); }
|
---|
45 | unsigned int all1s( unsigned long long int n ) { return __builtin_popcountll( n ); }
|
---|
46 |
|
---|
47 | // Count all 0 bits.
|
---|
48 | unsigned int all0s( unsigned char n ) { return __builtin_popcount( (typeof(n))~n ); }
|
---|
49 | unsigned int all0s( unsigned short int n ) { return __builtin_popcount( (typeof(n))~n ); }
|
---|
50 | unsigned int all0s( unsigned int n ) { return __builtin_popcount( ~n ); }
|
---|
51 | unsigned int all0s( unsigned long int n ) { return __builtin_popcountl( ~n ); }
|
---|
52 | unsigned int all0s( unsigned long long int n ) { return __builtin_popcountll( ~n ); }
|
---|
53 |
|
---|
54 | // Find least significiant zero bit. (ffs)
|
---|
55 | unsigned int low0( unsigned char n ) { return __builtin_ffs( (typeof(n))~n ); }
|
---|
56 | unsigned int low0( unsigned short int n ) { return __builtin_ffs( (typeof(n))~n ); }
|
---|
57 | unsigned int low0( unsigned int n ) { return __builtin_ffs( ~n ); }
|
---|
58 | unsigned int low0( unsigned long int n ) { return __builtin_ffsl( ~n ); }
|
---|
59 | unsigned int low0( unsigned long long int n ) { return __builtin_ffsll( ~n ); }
|
---|
60 |
|
---|
61 | // Find least significiant one bit.
|
---|
62 | unsigned int low1( unsigned int n ) { return __builtin_ffs( n ); }
|
---|
63 | unsigned int low1( unsigned long int n ) { return __builtin_ffsl( n ); }
|
---|
64 | unsigned int low1( unsigned long long int n ) { return __builtin_ffsll( n ); }
|
---|
65 |
|
---|
66 | // Find most significiant zero bit.
|
---|
67 | unsigned int high0( unsigned char n ) { return n == (typeof(n))-1 ? 0 : __bitsizeof(unsigned int) - __builtin_clz( (typeof(n))~n ); }
|
---|
68 | unsigned int high0( unsigned short int n ) { return n == (typeof(n))-1 ? 0 : __bitsizeof(unsigned int) - __builtin_clz( (typeof(n))~n ); }
|
---|
69 | unsigned int high0( unsigned int n ) { return n == -1 ? 0 : __bitsizeof(n) - __builtin_clz( ~n ); }
|
---|
70 | unsigned int high0( unsigned long int n ) { return n == -1 ? 0 : __bitsizeof(n) - __builtin_clzl( ~n ); }
|
---|
71 | unsigned int high0( unsigned long long int n ) { return n == -1 ? 0 : __bitsizeof(n) - __builtin_clzll( ~n ); }
|
---|
72 |
|
---|
73 | // Find most significiant one bit.
|
---|
74 | unsigned int high1( unsigned char n ) { return n == 0 ? 0 : __bitsizeof(unsigned int) - __builtin_clz( n ); }
|
---|
75 | unsigned int high1( unsigned short int n ) { return n == 0 ? 0 : __bitsizeof(unsigned int) - __builtin_clz( n ); }
|
---|
76 | unsigned int high1( unsigned int n ) { return n == 0 ? 0 : __bitsizeof(n) - __builtin_clz( n ); }
|
---|
77 | unsigned int high1( unsigned long int n ) { return n == 0 ? 0 : __bitsizeof(n) - __builtin_clzl( n ); }
|
---|
78 | unsigned int high1( unsigned long long int n ) { return n == 0 ? 0 : __bitsizeof(n) - __builtin_clzll( n ); }
|
---|
79 |
|
---|
80 | // 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
|
---|
81 | // but this computation returns true because of the two's complement, so it is a special case.
|
---|
82 | bool is_pow2( unsigned char n ) { return n == 0 ? false : (n & (n - 1)) == 0; }
|
---|
83 | bool is_pow2( unsigned short int n ) { return n == 0 ? false : (n & (n - 1)) == 0; }
|
---|
84 | bool is_pow2( unsigned int n ) { return n == 0 ? false : (n & (n - 1)) == 0; }
|
---|
85 | bool is_pow2( unsigned long int n ) { return n == 0 ? false : (n & (n - 1)) == 0; }
|
---|
86 | bool is_pow2( unsigned long long int n ) { return n == 0 ? false : (n & (n - 1)) == 0; }
|
---|
87 |
|
---|
88 | // Returns n aligned at the floor of align, clear bits above or equal to align, giving n % align.
|
---|
89 | signed char floor2( signed char n, signed char align ) { verify( is_pow2( align ) ); return n & -align; }
|
---|
90 | unsigned char floor2( unsigned char n, unsigned char align ) { verify( is_pow2( align ) ); return n & -align; }
|
---|
91 | short int floor2( short int n, short int align ) { verify( is_pow2( align ) ); return n & -align; }
|
---|
92 | unsigned short int floor2( unsigned short int n, unsigned short int align ) { verify( is_pow2( align ) ); return n & -align; }
|
---|
93 | int floor2( int n, int align ) { verify( is_pow2( align ) ); return n & -align; }
|
---|
94 | unsigned int floor2( unsigned int n, unsigned int align ) { verify( is_pow2( align ) ); return n & -align; }
|
---|
95 | long int floor2( long int n, long int align ) { verify( is_pow2( align ) ); return n & -align; }
|
---|
96 | unsigned long int floor2( unsigned long int n, unsigned long int align ) { verify( is_pow2( align ) ); return n & -align; }
|
---|
97 | long long int floor2( long long int n, long long int align ) { verify( is_pow2( align ) ); return n & -align; }
|
---|
98 | unsigned long long int floor2( unsigned long long int n, unsigned long long int align ) { verify( is_pow2( align ) ); return n & -align; }
|
---|
99 |
|
---|
100 | // forall( T | { T ?&?( T, T ); T -?( T ); } )
|
---|
101 | // T floor2( T n, T align ) { verify( is_pow2( align ) ); return n & -align; }
|
---|
102 |
|
---|
103 | // Returns n aligned at the ceiling of align, negate, round down, negate is the same as round up.
|
---|
104 | signed char ceiling2( signed char n, signed char align ) { verify( is_pow2( align ) ); return -floor2( -n, align ); }
|
---|
105 | unsigned char ceiling2( unsigned char n, unsigned char align ) { verify( is_pow2( align ) ); return -floor2( -n, align ); }
|
---|
106 | short int ceiling2( short int n, short int align ) { verify( is_pow2( align ) ); return -floor2( -n, align ); }
|
---|
107 | unsigned short int ceiling2( unsigned short int n, unsigned short int align ) { verify( is_pow2( align ) ); return -floor2( -n, align ); }
|
---|
108 | int ceiling2( int n, int align ) { verify( is_pow2( align ) ); return -floor2( -n, align ); }
|
---|
109 | unsigned int ceiling2( unsigned int n, unsigned int align ) { verify( is_pow2( align ) ); return -floor2( -n, align ); }
|
---|
110 | long int ceiling2( long int n, long int align ) { verify( is_pow2( align ) ); return -floor2( -n, align ); }
|
---|
111 | unsigned long int ceiling2( unsigned long int n, unsigned long int align ) { verify( is_pow2( align ) ); return -floor2( -n, align ); }
|
---|
112 | long long int ceiling2( long long int n, long long int align ) { verify( is_pow2( align ) ); return -floor2( -n, align ); }
|
---|
113 | unsigned long long int ceiling2( unsigned long long int n, unsigned long long int align ) { verify( is_pow2( align ) ); return -floor2( -n, align ); }
|
---|
114 |
|
---|
115 | // forall( T | { T floor2( T, T ); T -?( T ); } )
|
---|
116 | // T ceiling2( T n, T align ) { verify( is_pow2( align ) ); return -floor2( -n, align ); }
|
---|
117 | } // distribution
|
---|
118 |
|
---|
119 | // Local Variables: //
|
---|
120 | // tab-width: 4 //
|
---|
121 | // End: //
|
---|