2 * Copyright (C) 1995-2007 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * @brief Functions from hackers delight.
24 * @author Sebastian Hack, Matthias Braun
27 #ifndef FIRM_ADT_BITFIDDLE_H
28 #define FIRM_ADT_BITFIDDLE_H
34 /* some functions here assume ints are 32 bit wide */
35 #define HACKDEL_WORDSIZE 32
36 COMPILETIME_ASSERT(sizeof(unsigned) == 4, unsignedsize)
37 COMPILETIME_ASSERT(UINT_MAX == 4294967295U, uintmax)
43 * @return x + y or INT_MAX/INT_MIN if an overflow occurred and x,y was positive/negative.
45 * @note See hacker's delight, page 27.
48 int add_saturated(int x, int y)
52 An overflow occurs, if the sign of the both summands is equal
53 and the one of the sum is different from the summand's one.
54 The sign bit is 1, if an overflow occurred, 0 otherwise.
55 int overflow = ~(x ^ y) & (sum ^ x);
57 int overflow = (x ^ sum) & (y ^ sum);
61 Make a mask of the sign bit of x and y (they are the same if an
63 INT_MIN == ~INT_MAX, so if the sign was negative, INT_MAX becomes
66 int inf = (x >> (sizeof(x) * 8 - 1)) ^ INT_MAX;
68 return overflow < 0 ? inf : sum;
72 * Compute the count of set bits in a 32-bit word.
73 * @param x A 32-bit word.
74 * @return The number of bits set in x.
77 unsigned popcnt(unsigned x) {
78 x -= ((x >> 1) & 0x55555555);
79 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
80 x = (x + (x >> 4)) & 0x0f0f0f0f;
87 * Compute the number of leading zeros in a word.
89 * @return The number of leading (from the most significant bit) zeros.
92 unsigned nlz(unsigned x) {
93 #ifdef USE_X86_ASSEMBLY
106 y = x >>16; if (y != 0) { n -= 16; x = y; }
107 y = x >> 8; if (y != 0) { n -= 8; x = y; }
108 y = x >> 4; if (y != 0) { n -= 4; x = y; }
109 y = x >> 2; if (y != 0) { n -= 2; x = y; }
110 y = x >> 1; if (y != 0) return n - 2;
116 * Compute the number of trailing zeros in a word.
118 * @return The number of trailing zeros.
121 unsigned ntz(unsigned x) {
122 #ifdef USE_X86_ASSEMBLY
132 return HACKDEL_WORDSIZE - nlz(~x & (x - 1));
137 * Compute the greatest power of 2 smaller or equal to a value.
138 * This is also known as the binary logarithm.
139 * @param x The value.
140 * @return The power of two.
142 #define log2_floor(x) (HACKDEL_WORDSIZE - 1 - nlz(x))
145 * Compute the smallest power of 2 greater or equal to a value.
146 * This is also known as the binary logarithm.
147 * @param x The value.
148 * @return The power of two.
150 #define log2_ceil(x) (HACKDEL_WORDSIZE - nlz((x) - 1))
153 * Round up to the next multiple of a power of two.
155 * @param pot A power of two.
156 * @return x rounded up to the next multiple of pot.
158 #define round_up2(x,pot) (((x) + ((pot) - 1)) & (~((pot) - 1)))
161 * Returns the biggest power of 2 that is equal or smaller than @p x
162 * (see hackers delight power-of-2 boundaries, page 48)
165 unsigned floor_po2(unsigned x)
167 #ifdef USE_X86_ASSEMBLY // in this case nlz is fast
170 // note that x != 0 here, so nlz(x) < 32!
171 return 0x80000000U >> nlz(x);
183 * Returns the smallest power of 2 that is equal or greater than x
184 * @remark x has to be <= 0x8000000 of course
185 * @note see hackers delight power-of-2 boundaries, page 48
188 unsigned ceil_po2(unsigned x)
192 assert(x < (1U << 31));
194 #ifdef USE_X86_ASSEMBLY // in this case nlz is fast
195 // note that x != 0 here!
196 return 0x80000000U >> (nlz(x-1) - 1);
209 * Tests whether @p x is a power of 2
212 int is_po2(unsigned x)
214 return (x & (x-1)) == 0;