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