simplify hungarian interface
[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
49 int add_saturated(int x, int y)
50 {
51         int sum      = x + y;
52         /*
53                 An overflow occurs, if the sign of the both summands is equal
54                 and the one of the sum is different from the summand's one.
55                 The sign bit is 1, if an overflow occurred, 0 otherwise.
56                 int overflow = ~(x ^ y) & (sum ^ x);
57         */
58         int overflow = (x ^ sum) & (y ^ sum);
59
60         /*
61                 The infinity to use.
62                 Make a mask of the sign bit of x and y (they are the same if an
63                 overflow occurred).
64                 INT_MIN == ~INT_MAX, so if the sign was negative, INT_MAX becomes
65                 INT_MIN.
66         */
67         int inf = (x >> (sizeof(x) * 8 - 1)) ^ INT_MAX;
68
69         return overflow < 0 ? inf : sum;
70 }
71
72 /**
73  * Compute the count of set bits in a 32-bit word.
74  * @param x A 32-bit word.
75  * @return The number of bits set in x.
76  */
77 static inline
78 unsigned popcnt(unsigned x) {
79         x -= ((x >> 1) & 0x55555555);
80         x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
81         x = (x + (x >> 4)) & 0x0f0f0f0f;
82         x += x >> 8;
83         x += x >> 16;
84         return x & 0x3f;
85 }
86
87 /**
88  * Compute the number of leading zeros in a word.
89  * @param x The word.
90  * @return The number of leading (from the most significant bit) zeros.
91  */
92 static inline
93 unsigned nlz(unsigned x) {
94 #ifdef USE_X86_ASSEMBLY
95         unsigned res;
96         if(x == 0)
97                 return 32;
98
99         __asm__("bsrl %1,%0"
100                         : "=r" (res)
101                         : "r" (x));
102         return 31 - res;
103 #else
104    unsigned y;
105    int n = 32;
106
107    y = x >>16;  if (y != 0) { n -= 16;  x = y; }
108    y = x >> 8;  if (y != 0) { n -=  8;  x = y; }
109    y = x >> 4;  if (y != 0) { n -=  4;  x = y; }
110    y = x >> 2;  if (y != 0) { n -=  2;  x = y; }
111    y = x >> 1;  if (y != 0) return n - 2;
112    return n - x;
113 #endif
114 }
115
116 /**
117  * Compute the number of trailing zeros in a word.
118  * @param x The word.
119  * @return The number of trailing zeros.
120  */
121 static inline
122 unsigned ntz(unsigned x) {
123 #ifdef USE_X86_ASSEMBLY
124         unsigned res;
125         if(x == 0)
126                 return 32;
127
128         __asm__("bsfl %1,%0"
129                         : "=r" (res)
130                         : "r" (x));
131         return  res;
132 #else
133         return HACKDEL_WORDSIZE - nlz(~x & (x - 1));
134 #endif
135 }
136
137 /**
138  * Compute the greatest power of 2 smaller or equal to a value.
139  * This is also known as the binary logarithm.
140  * @param x The value.
141  * @return The power of two.
142  */
143 #define log2_floor(x) (HACKDEL_WORDSIZE - 1 - nlz(x))
144
145 /**
146  * Compute the smallest power of 2 greater or equal to a value.
147  * This is also known as the binary logarithm.
148  * @param x The value.
149  * @return The power of two.
150  */
151 #define log2_ceil(x) (HACKDEL_WORDSIZE - nlz((x) - 1))
152
153 /**
154  * Round up to the next multiple of a power of two.
155  * @param x A value.
156  * @param pot A power of two.
157  * @return x rounded up to the next multiple of pot.
158  */
159 #define round_up2(x,pot) (((x) + ((pot) - 1)) & (~((pot) - 1)))
160
161 /**
162  * Returns the biggest power of 2 that is equal or smaller than @p x
163  * (see hackers delight power-of-2 boundaries, page 48)
164  */
165 static inline
166 unsigned floor_po2(unsigned x)
167 {
168 #ifdef USE_X86_ASSEMBLY // in this case nlz is fast
169         if(x == 0)
170                 return 0;
171         // note that x != 0 here, so nlz(x) < 32!
172         return 0x80000000U >> nlz(x);
173 #else
174         x |= x >> 1;
175         x |= x >> 2;
176         x |= x >> 4;
177         x |= x >> 8;
178         x |= x >> 16;
179         return x - (x >> 1);
180 #endif
181 }
182
183 /**
184  * Returns the smallest power of 2 that is equal or greater than x
185  * @remark x has to be <= 0x8000000 of course
186  * @note see hackers delight power-of-2 boundaries, page 48
187  */
188 static inline
189 unsigned ceil_po2(unsigned x)
190 {
191         if(x == 0)
192                 return 0;
193         assert(x < (1U << 31));
194
195 #ifdef USE_X86_ASSEMBLY // in this case nlz is fast
196         // note that x != 0 here!
197         return 0x80000000U >> (nlz(x-1) - 1);
198 #else
199         x = x - 1;
200         x |= x >> 1;
201         x |= x >> 2;
202         x |= x >> 4;
203         x |= x >> 8;
204         x |= x >> 16;
205         return x + 1;
206 #endif
207 }
208
209 /**
210  * Tests whether @p x is a power of 2
211  */
212 static inline
213 int is_po2(unsigned x)
214 {
215         return (x & (x-1)) == 0;
216 }
217
218 #endif