2 * This file is part of cparser.
3 * Copyright (C) 2007-2009 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
27 #ifndef _FIRM_BITFIDDLE_H_
28 #define _FIRM_BITFIDDLE_H_
33 /* some functions here assume ints are 32 bit wide */
34 #define HACKDEL_WORDSIZE 32
35 COMPILETIME_ASSERT(sizeof(unsigned) == 4, unsignedsize)
36 COMPILETIME_ASSERT(UINT_MAX == 4294967295U, uintmax)
42 * @return x + y or INT_MAX/INT_MIN if an overflow occurred and x,y was positive/negative.
44 * @note See hacker's delight, page 27.
46 static inline __attribute__((const))
47 int add_saturated(int x, int y)
51 An overflow occurs, if the sign of the both summands is equal
52 and the one of the sum is different from the summand's one.
53 The sign bit is 1, if an overflow occurred, 0 otherwise.
54 int overflow = ~(x ^ y) & (sum ^ x);
56 int overflow = (x ^ sum) & (y ^ sum);
60 Make a mask of the sign bit of x and y (they are the same if an
62 INT_MIN == ~INT_MAX, so if the sign was negative, INT_MAX becomes
65 int inf = (x >> (sizeof(x) * 8 - 1)) ^ INT_MAX;
67 return overflow < 0 ? inf : sum;
71 * Compute the count of set bits in a 32-bit word.
72 * @param x A 32-bit word.
73 * @return The number of bits set in x.
75 static inline __attribute__((const))
76 unsigned popcnt(unsigned x) {
77 x -= ((x >> 1) & 0x55555555);
78 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
79 x = (x + (x >> 4)) & 0x0f0f0f0f;
86 * Compute the number of leading zeros in a word.
88 * @return The number of leading (from the most significant bit) zeros.
90 static inline __attribute__((const))
91 unsigned nlz(unsigned x) {
92 #ifdef USE_X86_ASSEMBLY
112 * Compute the number of trailing zeros in a word.
114 * @return The number of trailing zeros.
116 static inline __attribute__((const))
117 unsigned ntz(unsigned x) {
118 #ifdef USE_X86_ASSEMBLY
128 return HACKDEL_WORDSIZE - nlz(~x & (x - 1));
133 * Compute the greatest power of 2 smaller or equal to a value.
134 * This is also known as the binary logarithm.
135 * @param x The value.
136 * @return The power of two.
138 #define log2_floor(x) (HACKDEL_WORDSIZE - 1 - nlz(x))
141 * Compute the smallest power of 2 greater or equal to a value.
142 * This is also known as the binary logarithm.
143 * @param x The value.
144 * @return The power of two.
146 #define log2_ceil(x) (HACKDEL_WORDSIZE - nlz((x) - 1))
149 * Round up to the next multiple of a power of two.
151 * @param pot A power of two.
152 * @return x rounded up to the next multiple of pot.
154 #define round_up2(x,pot) (((x) + ((pot) - 1)) & (~((pot) - 1)))
157 * Returns the biggest power of 2 that is equal or smaller than @p x
158 * (see hackers delight power-of-2 boundaries, page 48)
160 static inline __attribute__((const))
161 unsigned floor_po2(unsigned x)
163 #ifdef USE_X86_ASSEMBLY // in this case nlz is fast
166 // note that x != 0 here, so nlz(x) < 32!
167 return 0x80000000U >> nlz(x);
179 * Returns the smallest power of 2 that is equal or greater than x
180 * @remark x has to be <= 0x8000000 of course
181 * @note see hackers delight power-of-2 boundaries, page 48
183 static inline __attribute__((const))
184 unsigned ceil_po2(unsigned x)
188 assert(x < (1U << 31));
190 #ifdef USE_X86_ASSEMBLY // in this case nlz is fast
191 // note that x != 0 here!
192 return 0x80000000U >> (nlz(x-1) - 1);
205 * Tests whether @p x is a power of 2
207 static inline __attribute__((const))
208 int is_po2(unsigned x)
210 return (x & (x-1)) == 0;