2 * This file is part of cparser.
3 * Copyright (C) 2007-2008 Matthias Braun <matze@braunis.de>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24 * @brief Functions from hackers delight.
25 * @author Sebastian Hack, Matthias Braun
28 #ifndef _FIRM_BITFIDDLE_H_
29 #define _FIRM_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.
47 static inline __attribute__((const))
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.
76 static inline __attribute__((const))
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.
91 static inline __attribute__((const))
92 unsigned nlz(unsigned x) {
93 #ifdef USE_X86_ASSEMBLY
113 * Compute the number of trailing zeros in a word.
115 * @return The number of trailing zeros.
117 static inline __attribute__((const))
118 unsigned ntz(unsigned x) {
119 #ifdef USE_X86_ASSEMBLY
129 return HACKDEL_WORDSIZE - nlz(~x & (x - 1));
134 * Compute the greatest power of 2 smaller or equal to a value.
135 * This is also known as the binary logarithm.
136 * @param x The value.
137 * @return The power of two.
139 #define log2_floor(x) (HACKDEL_WORDSIZE - 1 - nlz(x))
142 * Compute the smallest power of 2 greater or equal to a value.
143 * This is also known as the binary logarithm.
144 * @param x The value.
145 * @return The power of two.
147 #define log2_ceil(x) (HACKDEL_WORDSIZE - nlz((x) - 1))
150 * Round up to the next multiple of a power of two.
152 * @param pot A power of two.
153 * @return x rounded up to the next multiple of pot.
155 #define round_up2(x,pot) (((x) + ((pot) - 1)) & (~((pot) - 1)))
158 * Returns the biggest power of 2 that is equal or smaller than @p x
159 * (see hackers delight power-of-2 boundaries, page 48)
161 static inline __attribute__((const))
162 unsigned floor_po2(unsigned x)
164 #ifdef USE_X86_ASSEMBLY // in this case nlz is fast
167 // note that x != 0 here, so nlz(x) < 32!
168 return 0x80000000U >> nlz(x);
180 * Returns the smallest power of 2 that is equal or greater than x
181 * @remark x has to be <= 0x8000000 of course
182 * @note see hackers delight power-of-2 boundaries, page 48
184 static inline __attribute__((const))
185 unsigned ceil_po2(unsigned x)
189 assert(x < (1U << 31));
191 #ifdef USE_X86_ASSEMBLY // in this case nlz is fast
192 // note that x != 0 here!
193 return 0x80000000U >> (nlz(x-1) - 1);
206 * Tests whether @p x is a power of 2
208 static inline __attribute__((const))
209 int is_po2(unsigned x)
211 return (x & (x-1)) == 0;