s/full\>/ful/.
[libfirm] / ir / adt / bitfiddle.h
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
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.
10  *
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.
14  *
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
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @date    28.9.2004
23  * @brief   Functions from hackers delight.
24  * @author  Sebastian Hack, Matthias Braun
25  * @version $Id$
26  */
27 #ifndef FIRM_ADT_BITFIDDLE_H
28 #define FIRM_ADT_BITFIDDLE_H
29
30 #include "compiler.h"
31
32 #include <limits.h>
33 #include <assert.h>
34
35 /* some functions here assume ints are 32 bit wide */
36 #define HACKDEL_WORDSIZE 32
37 COMPILETIME_ASSERT(sizeof(unsigned) == 4, unsignedsize)
38 COMPILETIME_ASSERT(UINT_MAX == 4294967295U, uintmax)
39
40 /**
41  * Add saturated.
42  * @param x Summand 1.
43  * @param y Summand 2.
44  * @return x + y or INT_MAX/INT_MIN if an overflow occurred and x,y was positive/negative.
45  *
46  * @note See hacker's delight, page 27.
47  */
48 static inline int add_saturated(int x, int y)
49 {
50         int sum      = x + y;
51         /*
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);
56         */
57         int overflow = (x ^ sum) & (y ^ sum);
58
59         /*
60                 The infinity to use.
61                 Make a mask of the sign bit of x and y (they are the same if an
62                 overflow occurred).
63                 INT_MIN == ~INT_MAX, so if the sign was negative, INT_MAX becomes
64                 INT_MIN.
65         */
66         int inf = (x >> (sizeof(x) * 8 - 1)) ^ INT_MAX;
67
68         return overflow < 0 ? inf : sum;
69 }
70
71 /**
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.
75  */
76 static inline unsigned popcount(unsigned x)
77 {
78 #if defined(__GNUC__) && __GNUC__ >= 4
79         return __builtin_popcount(x);
80 #else
81         x -= ((x >> 1) & 0x55555555);
82         x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
83         x = (x + (x >> 4)) & 0x0f0f0f0f;
84         x += x >> 8;
85         x += x >> 16;
86         return x & 0x3f;
87 #endif
88 }
89
90 /**
91  * Compute the number of leading zeros in a word.
92  * @param x The word.
93  * @return The number of leading (from the most significant bit) zeros.
94  */
95 static inline unsigned nlz(unsigned x)
96 {
97 #if defined(__GNUC__) && __GNUC__ >= 4
98         if(x == 0)
99                 return 32;
100         return __builtin_clz(x);
101 #else
102    unsigned y;
103    int n = 32;
104
105    y = x >>16;  if (y != 0) { n -= 16;  x = y; }
106    y = x >> 8;  if (y != 0) { n -=  8;  x = y; }
107    y = x >> 4;  if (y != 0) { n -=  4;  x = y; }
108    y = x >> 2;  if (y != 0) { n -=  2;  x = y; }
109    y = x >> 1;  if (y != 0) return n - 2;
110    return n - x;
111 #endif
112 }
113
114 /**
115  * Compute the number of trailing zeros in a word.
116  * @param x The word.
117  * @return The number of trailing zeros.
118  */
119 static inline unsigned ntz(unsigned x)
120 {
121 #if defined(__GNUC__) && __GNUC__ >= 4
122         if(x == 0)
123                 return 32;
124         return __builtin_ctz(x);
125 #else
126         return HACKDEL_WORDSIZE - nlz(~x & (x - 1));
127 #endif
128 }
129
130 /**
131  * Compute the greatest power of 2 smaller or equal to a value.
132  * This is also known as the binary logarithm.
133  * @param x The value.
134  * @return The power of two.
135  */
136 #define log2_floor(x) (HACKDEL_WORDSIZE - 1 - nlz(x))
137
138 /**
139  * Compute the smallest power of 2 greater or equal to a value.
140  * This is also known as the binary logarithm.
141  * @param x The value.
142  * @return The power of two.
143  */
144 #define log2_ceil(x) (HACKDEL_WORDSIZE - nlz((x) - 1))
145
146 /**
147  * Round up to the next multiple of a power of two.
148  * @param x A value.
149  * @param pot A power of two.
150  * @return x rounded up to the next multiple of pot.
151  */
152 #define round_up2(x,pot) (((x) + ((pot) - 1)) & (~((pot) - 1)))
153
154 /**
155  * Returns the biggest power of 2 that is equal or smaller than @p x
156  * (see hackers delight power-of-2 boundaries, page 48)
157  */
158 static inline unsigned floor_po2(unsigned x)
159 {
160 #if defined(__GNUC__) && __GNUC__ >= 4 // in this case nlz is fast
161         if(x == 0)
162                 return 0;
163         // note that x != 0 here, so nlz(x) < 32!
164         return 0x80000000U >> nlz(x);
165 #else
166         x |= x >> 1;
167         x |= x >> 2;
168         x |= x >> 4;
169         x |= x >> 8;
170         x |= x >> 16;
171         return x - (x >> 1);
172 #endif
173 }
174
175 /**
176  * Returns the smallest power of 2 that is equal or greater than x
177  * @remark x has to be <= 0x8000000 of course
178  * @note see hackers delight power-of-2 boundaries, page 48
179  */
180 static inline unsigned ceil_po2(unsigned x)
181 {
182         if(x == 0)
183                 return 0;
184         assert(x < (1U << 31));
185
186 #if defined(__GNUC__) && __GNUC__ >= 4 // in this case nlz is fast
187         // note that x != 0 here!
188         return 0x80000000U >> (nlz(x-1) - 1);
189 #else
190         x = x - 1;
191         x |= x >> 1;
192         x |= x >> 2;
193         x |= x >> 4;
194         x |= x >> 8;
195         x |= x >> 16;
196         return x + 1;
197 #endif
198 }
199
200 /**
201  * Tests whether @p x is a power of 2
202  */
203 static inline int is_po2(unsigned x)
204 {
205         return (x & (x-1)) == 0;
206 }
207
208 #endif