adapt to latest libfirm
[cparser] / adt / bitfiddle.h
1 /*
2  * This file is part of cparser.
3  * Copyright (C) 2007-2009 Matthias Braun <matze@braunis.de>
4  *
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.
9  *
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.
14  *
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
18  * 02111-1307, USA.
19  */
20
21 /**
22  * @file
23  * @date    28.9.2004
24  * @brief   Functions from hackers delight.
25  * @author  Sebastian Hack, Matthias Braun
26  */
27 #ifndef _FIRM_BITFIDDLE_H_
28 #define _FIRM_BITFIDDLE_H_
29
30 #include <limits.h>
31 #include "util.h"
32
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)
37
38 /**
39  * Add saturated.
40  * @param x Summand 1.
41  * @param y Summand 2.
42  * @return x + y or INT_MAX/INT_MIN if an overflow occurred and x,y was positive/negative.
43  *
44  * @note See hacker's delight, page 27.
45  */
46 static inline __attribute__((const))
47 int add_saturated(int x, int y)
48 {
49         int sum      = x + y;
50         /*
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);
55         */
56         int overflow = (x ^ sum) & (y ^ sum);
57
58         /*
59                 The infinity to use.
60                 Make a mask of the sign bit of x and y (they are the same if an
61                 overflow occurred).
62                 INT_MIN == ~INT_MAX, so if the sign was negative, INT_MAX becomes
63                 INT_MIN.
64         */
65         int inf = (x >> (sizeof(x) * 8 - 1)) ^ INT_MAX;
66
67         return overflow < 0 ? inf : sum;
68 }
69
70 /**
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.
74  */
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;
80         x += x >> 8;
81         x += x >> 16;
82         return x & 0x3f;
83 }
84
85 /**
86  * Compute the number of leading zeros in a word.
87  * @param x The word.
88  * @return The number of leading (from the most significant bit) zeros.
89  */
90 static inline __attribute__((const))
91 unsigned nlz(unsigned x) {
92 #ifdef USE_X86_ASSEMBLY
93         unsigned res;
94         if(x == 0)
95                 return 32;
96
97         __asm__("bsrl %1,%0"
98                         : "=r" (res)
99                         : "r" (x));
100         return 31 - res;
101 #else
102         x |= x >> 1;
103         x |= x >> 2;
104         x |= x >> 4;
105         x |= x >> 8;
106         x |= x >> 16;
107         return popcnt(~x);
108 #endif
109 }
110
111 /**
112  * Compute the number of trailing zeros in a word.
113  * @param x The word.
114  * @return The number of trailing zeros.
115  */
116 static inline __attribute__((const))
117 unsigned ntz(unsigned x) {
118 #ifdef USE_X86_ASSEMBLY
119         unsigned res;
120         if(x == 0)
121                 return 32;
122
123         __asm__("bsfl %1,%0"
124                         : "=r" (res)
125                         : "r" (x));
126         return  res;
127 #else
128         return HACKDEL_WORDSIZE - nlz(~x & (x - 1));
129 #endif
130 }
131
132 /**
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.
137  */
138 #define log2_floor(x) (HACKDEL_WORDSIZE - 1 - nlz(x))
139
140 /**
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.
145  */
146 #define log2_ceil(x) (HACKDEL_WORDSIZE - nlz((x) - 1))
147
148 /**
149  * Round up to the next multiple of a power of two.
150  * @param x A value.
151  * @param pot A power of two.
152  * @return x rounded up to the next multiple of pot.
153  */
154 #define round_up2(x,pot) (((x) + ((pot) - 1)) & (~((pot) - 1)))
155
156 /**
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)
159  */
160 static inline __attribute__((const))
161 unsigned floor_po2(unsigned x)
162 {
163 #ifdef USE_X86_ASSEMBLY // in this case nlz is fast
164         if(x == 0)
165                 return 0;
166         // note that x != 0 here, so nlz(x) < 32!
167         return 0x80000000U >> nlz(x);
168 #else
169         x |= x >> 1;
170         x |= x >> 2;
171         x |= x >> 4;
172         x |= x >> 8;
173         x |= x >> 16;
174         return x - (x >> 1);
175 #endif
176 }
177
178 /**
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
182  */
183 static inline __attribute__((const))
184 unsigned ceil_po2(unsigned x)
185 {
186         if(x == 0)
187                 return 0;
188         assert(x < (1U << 31));
189
190 #ifdef USE_X86_ASSEMBLY // in this case nlz is fast
191         // note that x != 0 here!
192         return 0x80000000U >> (nlz(x-1) - 1);
193 #else
194         x = x - 1;
195         x |= x >> 1;
196         x |= x >> 2;
197         x |= x >> 4;
198         x |= x >> 8;
199         x |= x >> 16;
200         return x + 1;
201 #endif
202 }
203
204 /**
205  * Tests whether @p x is a power of 2
206  */
207 static inline __attribute__((const))
208 int is_po2(unsigned x)
209 {
210         return (x & (x-1)) == 0;
211 }
212
213 #endif