4 * @brief Functions from hackers delight.
5 * @author Sebastian Hack, Matthias Braun
8 #ifndef _FIRM_BITFIDDLE_H_
9 #define _FIRM_BITFIDDLE_H_
14 /* some functions here assume ints are 32 bit wide */
15 #define HACKDEL_WORDSIZE 32
16 COMPILETIME_ASSERT(sizeof(unsigned) == 4, unsignedsize)
17 COMPILETIME_ASSERT(UINT_MAX == 4294967295U, uintmax)
23 * @return x + y or INT_MAX/INT_MIN if an overflow occurred and x,y was positive/negative.
25 * @note See hacker's delight, page 27.
27 static inline __attribute__((const))
28 int add_saturated(int x, int y)
32 An overflow occurs, if the sign of the both summands is equal
33 and the one of the sum is different from the summand's one.
34 The sign bit is 1, if an overflow occurred, 0 otherwise.
35 int overflow = ~(x ^ y) & (sum ^ x);
37 int overflow = (x ^ sum) & (y ^ sum);
41 Make a mask of the sign bit of x and y (they are the same if an
43 INT_MIN == ~INT_MAX, so if the sign was negative, INT_MAX becomes
46 int inf = (x >> (sizeof(x) * 8 - 1)) ^ INT_MAX;
48 return overflow < 0 ? inf : sum;
52 * Compute the count of set bits in a 32-bit word.
53 * @param x A 32-bit word.
54 * @return The number of bits set in x.
56 static inline __attribute__((const))
57 unsigned popcnt(unsigned x) {
58 x -= ((x >> 1) & 0x55555555);
59 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
60 x = (x + (x >> 4)) & 0x0f0f0f0f;
67 * Compute the number of leading zeros in a word.
69 * @return The number of leading (from the most significant bit) zeros.
71 static inline __attribute__((const))
72 unsigned nlz(unsigned x) {
73 #ifdef USE_X86_ASSEMBLY
93 * Compute the number of trailing zeros in a word.
95 * @return The number of trailing zeros.
97 static inline __attribute__((const))
98 unsigned ntz(unsigned x) {
99 #ifdef USE_X86_ASSEMBLY
109 return HACKDEL_WORDSIZE - nlz(~x & (x - 1));
114 * Compute the greatest power of 2 smaller or equal to a value.
115 * This is also known as the binary logarithm.
116 * @param x The value.
117 * @return The power of two.
119 #define log2_floor(x) (HACKDEL_WORDSIZE - 1 - nlz(x))
122 * Compute the smallest power of 2 greater or equal to a value.
123 * This is also known as the binary logarithm.
124 * @param x The value.
125 * @return The power of two.
127 #define log2_ceil(x) (HACKDEL_WORDSIZE - nlz((x) - 1))
130 * Round up to the next multiple of a power of two.
132 * @param pot A power of two.
133 * @return x rounded up to the next multiple of pot.
135 #define round_up2(x,pot) (((x) + ((pot) - 1)) & (~((pot) - 1)))
138 * Returns the biggest power of 2 that is equal or smaller than @p x
139 * (see hackers delight power-of-2 boundaries, page 48)
141 static inline __attribute__((const))
142 unsigned floor_po2(unsigned x)
144 #ifdef USE_X86_ASSEMBLY // in this case nlz is fast
147 // note that x != 0 here, so nlz(x) < 32!
148 return 0x80000000U >> nlz(x);
160 * Returns the smallest power of 2 that is equal or greater than x
161 * @remark x has to be <= 0x8000000 of course
162 * @note see hackers delight power-of-2 boundaries, page 48
164 static inline __attribute__((const))
165 unsigned ceil_po2(unsigned x)
169 assert(x < (1U << 31));
171 #ifdef USE_X86_ASSEMBLY // in this case nlz is fast
172 // note that x != 0 here!
173 return 0x80000000U >> (nlz(x-1) - 1);
186 * Tests whether @p x is a power of 2
188 static inline __attribute__((const))
189 int is_po2(unsigned x)
191 return (x & (x-1)) == 0;