-
Notifications
You must be signed in to change notification settings - Fork 17
/
intrinsics.hpp
101 lines (80 loc) · 2.14 KB
/
intrinsics.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
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
#pragma once
#include <stdint.h>
#include "succinct_config.hpp"
#if SUCCINCT_USE_INTRINSICS
#include <xmmintrin.h>
#if defined(__GNUC__) || defined(__clang__)
# define __INTRIN_INLINE inline __attribute__((__always_inline__))
#elif defined(_MSC_VER)
# define __INTRIN_INLINE inline __forceinline
#else
# define __INTRIN_INLINE inline
#endif
#endif
#if SUCCINCT_USE_POPCNT
# if !SUCCINCT_USE_INTRINSICS
# error "Intrinsics support needed for popcnt"
# endif
#include <smmintrin.h>
#endif
namespace succinct { namespace intrinsics {
#if SUCCINCT_USE_INTRINSICS
__INTRIN_INLINE uint64_t byteswap64(uint64_t value)
{
#if defined(__GNUC__) || defined(__clang__)
return __builtin_bswap64(value);
#elif defined(_MSC_VER)
return _byteswap_uint64(value);
#else
# error Unsupported platform
#endif
}
__INTRIN_INLINE bool bsf64(unsigned long* const index, const uint64_t mask)
{
#if defined(__GNUC__) || defined(__clang__)
if (mask) {
*index = (unsigned long)__builtin_ctzll(mask);
return true;
} else {
return false;
}
#elif defined(_MSC_VER)
return _BitScanForward64(index, mask) != 0;
#else
# error Unsupported platform
#endif
}
__INTRIN_INLINE bool bsr64(unsigned long* const index, const uint64_t mask)
{
#if defined(__GNUC__) || defined(__clang__)
if (mask) {
*index = (unsigned long)(63 - __builtin_clzll(mask));
return true;
} else {
return false;
}
#elif defined(_MSC_VER)
return _BitScanReverse64(index, mask) != 0;
#else
# error Unsupported platform
#endif
}
template <typename T>
__INTRIN_INLINE void prefetch(T const* ptr)
{
_mm_prefetch((const char*)ptr, _MM_HINT_T0);
}
#else /* SUCCINCT_USE_INTRINSICS */
template <typename T>
inline void prefetch(T const* /* ptr */)
{
/* do nothing */
}
#endif /* SUCCINCT_USE_INTRINSICS */
#if SUCCINCT_USE_POPCNT
__INTRIN_INLINE uint64_t popcount(uint64_t x)
{
return uint64_t(_mm_popcnt_u64(x));
}
#endif /* SUCCINCT_USE_POPCNT */
}}