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