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