/
util.hpp
58 lines (45 loc) · 900 Bytes
/
util.hpp
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
#ifndef __UTIL_H__
#define __UTIL_H__
#include <cstdlib>
#include <climits>
#define VERSION "1.0.1"
#ifdef __GNUC__
#define USE_GCC 1
#else
#define USE_GCC 0
#endif
#define GCC_VERSION (__GNUC__ * 10000 \
+ __GNUC_MINOR__ * 100 \
+ __GNUC_PATCHLEVEL)
#if USE_GCC && GCC_VERSION>40500
#define UNREACHABLE() __builtin_unreachable()
#else
#define UNREACHABLE() ((void) 0)
#endif
namespace {
#if USE_GCC && GCC_VERSION>40600
inline int clz(unsigned int v) {
return __builtin_clz(v);
}
inline int clz(unsigned long v) {
return __builtin_clzl(v);
}
inline int clz(unsigned long long v) {
return __builtin_clzll(v);
}
template <typename INT_TYPE>
int msb(INT_TYPE v) {
return sizeof(INT_TYPE)*CHAR_BIT - clz(v) - 1;
}
#else
template <typename INT_TYPE>
inline int msb(INT_TYPE v) {
int ret = 0;
while (v>>=1) {
++ret;
}
return ret;
}
#endif
}
#endif /* __UTIL_H__ */