-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathewahutil.h
235 lines (201 loc) · 5.51 KB
/
ewahutil.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/**
* This code is released under the
* Apache License Version 2.0 http://www.apache.org/licenses/.
*
* (c) Daniel Lemire, http://lemire.me/en/
*
* Some code from the public domain tuklib.
*/
#ifndef EWAHUTIL_H
#define EWAHUTIL_H
#include <string.h>
#include <stdlib.h>
#include <iso646.h> // mostly for Microsoft compilers
#include <limits.h>
#include <stdint.h> // part of Visual Studio 2010 and better
#include <cassert>
#include <iostream>
#include <vector>
#include <string>
#include <stdexcept>
#include <cstddef>
#include <algorithm>
#include <sstream>
// taken from stackoverflow
#ifndef NDEBUG
#define ASSERT(condition, message) \
do { \
if (!(condition)) { \
std::cerr << "Assertion `" #condition "` failed in " << __FILE__ \
<< " line " << __LINE__ << ": " << message << std::endl; \
std::exit(EXIT_FAILURE); \
} \
} while (false)
#else
#define ASSERT(condition, message) \
do { \
} while (false)
#endif
static inline uint32_t ctz64(uint64_t n) {
#if defined(__GNUC__) && UINT_MAX >= UINT32_MAX && ULLONG_MAX >= UINT64_MAX
return static_cast<uint32_t>(__builtin_ctzll(n));
#elif defined(_WIN64) && defined(_MSC_VER) && _MSC_VER >= 1400 && ULONG_MAX >= UINT64_MAX
uint32_t i;
_BitScanForward64((unsigned long *)&i, n);
return i;
#else
uint32_t i = 1;
if ((n & static_cast<uint64_t>(4294967295)) == 0) {
n >>= 32;
i += 32;
}
if ((n & static_cast<uint64_t>(0x0000FFFFUL)) == 0) {
n >>= 16;
i += 16;
}
if ((n & static_cast<uint64_t>(0x000000FFUL)) == 0) {
n >>= 8;
i += 8;
}
if ((n & static_cast<uint64_t>(0x0000000FUL)) == 0) {
n >>= 4;
i += 4;
}
if ((n & static_cast<uint64_t>(0x00000003UL)) == 0) {
n >>= 2;
i += 2;
}
i -= (n & 0x1);
return i;
#endif
}
static inline uint32_t ctz32(uint32_t n) {
#if defined(__GNUC__) && UINT_MAX >= UINT32_MAX
return static_cast<uint32_t>(__builtin_ctz(n));
#elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
uint32_t i;
__asm__("bsfl %1, %0" : "=r"(i) : "rm"(n));
return i;
#elif defined(_MSC_VER) && _MSC_VER >= 1400
uint32_t i;
_BitScanForward((unsigned long *)&i, n);
return i;
#else
uint32_t i = 1;
if ((n & static_cast<uint32_t>(0x0000FFFF)) == 0) {
n >>= 16;
i += 16;
}
if ((n & static_cast<uint32_t>(0x000000FF)) == 0) {
n >>= 8;
i += 8;
}
if ((n & static_cast<uint32_t>(0x0000000F)) == 0) {
n >>= 4;
i += 4;
}
if ((n & static_cast<uint32_t>(0x00000003)) == 0) {
n >>= 2;
i += 2;
}
i -= (n & 1);
return i;
#endif
}
static inline uint32_t ctz16(uint16_t n) {
#if defined(__GNUC__) && UINT_MAX >= UINT32_MAX
return static_cast<uint32_t>(__builtin_ctz(n));
#elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
uint32_t i;
__asm__("bsfl %1, %0" : "=r"(i) : "rm"(n));
return i;
#elif defined(_MSC_VER) && _MSC_VER >= 1400
uint32_t i;
_BitScanForward((unsigned long *)&i, n);
return i;
#else
uint32_t i = 1;
if ((n & static_cast<uint16_t>(0x000000FF)) == 0) {
n >>= 8;
i += 8;
}
if ((n & static_cast<uint16_t>(0x0000000F)) == 0) {
n >>= 4;
i += 4;
}
if ((n & static_cast<uint16_t>(0x00000003)) == 0) {
n >>= 2;
i += 2;
}
i -= (n & 1);
return i;
#endif
}
#ifdef __GNUC__
/**
* count the number of bits set to one (32 bit version)
*/
inline uint32_t countOnes(uint32_t x) {
return static_cast<uint32_t>(__builtin_popcount(x));
}
#elif defined(_MSC_VER) && _MSC_VER >= 1400
inline uint32_t countOnes(uint32_t x) { return __popcnt(x); }
#else
inline uint32_t countOnes(uint32_t v) {
v = v - ((v >> 1) & 0x55555555);
v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
return static_cast<uint32_t>((((v + (v >> 4)) & 0x0F0F0F0F) * 0x01010101) >>
24);
}
#endif
#ifdef __GNUC__
/**
* count the number of bits set to one (64 bit version)
*/
inline uint32_t countOnes(uint64_t x) {
return static_cast<uint32_t>(__builtin_popcountll(x));
}
#elif defined(_WIN64) && defined(_MSC_VER) && _MSC_VER >= 1400
inline uint32_t countOnes(uint64_t x) {
return static_cast<uint32_t>(__popcnt64(static_cast<__int64>(x)));
}
#else
inline uint32_t countOnes(uint64_t v) {
v = v - ((v >> 1) & 0x5555555555555555);
v = (v & 0x3333333333333333) + ((v >> 2) & 0x3333333333333333);
v = ((v + (v >> 4)) & 0x0F0F0F0F0F0F0F0F);
return static_cast<uint32_t>((v * (0x0101010101010101)) >> 56);
}
#endif
inline uint32_t countOnes(uint16_t v) {
return countOnes(static_cast<uint32_t>(v));
}
inline uint32_t numberOfTrailingZeros(uint32_t x) {
if (x == 0)
return 32;
return ctz32(x);
}
inline uint32_t numberOfTrailingZeros(uint64_t x) {
if (x == 0)
return 64;
return ctz64(x);
}
inline uint32_t numberOfTrailingZeros(uint16_t x) {
if (x == 0)
return 16;
return ctz16(x);
}
/**
* Returns the binary representation of a binary word.
*/
template <class uword> std::string toBinaryString(const uword w) {
std::ostringstream convert;
for (uint32_t k = 0; k < sizeof(uword) * 8; ++k) {
if (w & (static_cast<uword>(1) << k))
convert << "1";
else
convert << "0";
}
return convert.str();
}
#endif