26#ifndef MYSQL_HARNESS_STDX_BIT_H_
27#define MYSQL_HARNESS_STDX_BIT_H_
89constexpr std::enable_if_t<
sizeof(T) == 1, T>
bswap(T t)
noexcept {
94constexpr std::enable_if_t<
sizeof(T) == 2, T>
bswap(T t)
noexcept {
96 return __builtin_bswap16(t);
98 return (t & UINT16_C(0x00ff)) << (1 * 8) | (t & UINT16_C(0xff00)) >> (1 * 8);
107constexpr std::enable_if_t<
sizeof(T) == 4, T>
bswap(T t)
noexcept {
109 return __builtin_bswap32(t);
111 return (t & UINT32_C(0x0000'00ff)) << (3 * 8) |
112 (t & UINT32_C(0x0000'ff00)) << (1 * 8) |
113 (t & UINT32_C(0x00ff'0000)) >> (1 * 8) |
114 (t & UINT32_C(0xff00'0000)) >> (3 * 8);
123constexpr std::enable_if_t<
sizeof(T) == 8, T>
bswap(T t)
noexcept {
125 return __builtin_bswap64(t);
127 return (t & UINT64_C(0x0000'0000'0000'00ff)) << (7 * 8) |
128 (t & UINT64_C(0x0000'0000'0000'ff00)) << (5 * 8) |
129 (t & UINT64_C(0x0000'0000'00ff'0000)) << (3 * 8) |
130 (t & UINT64_C(0x0000'0000'ff00'0000)) << (1 * 8) |
131 (t & UINT64_C(0x0000'00ff'0000'0000)) >> (1 * 8) |
132 (t & UINT64_C(0x0000'ff00'0000'0000)) >> (3 * 8) |
133 (t & UINT64_C(0x00ff'0000'0000'0000)) >> (5 * 8) |
134 (t & UINT64_C(0xff00'0000'0000'0000)) >> (7 * 8);
140template <
class IntegerType>
141std::enable_if_t<std::is_integral<IntegerType>::value,
142 IntegerType>
constexpr byteswap(IntegerType t)
noexcept {
143 return impl::bswap(
static_cast<std::make_unsigned_t<IntegerType>
>(t));
148constexpr std::enable_if_t<std::is_unsigned<T>::value, T>
rotl(T x,
153 if (0 ==
r)
return x;
155 return r > 0 ? ((x << r) | x >> (
N -
r)) :
rotr(x, -
r);
159constexpr std::enable_if_t<std::is_unsigned<T>::value, T>
rotr(T x,
164 if (0 ==
r)
return x;
166 return r > 0 ? ((x >>
r) | x << (
N -
r)) :
rotl(x, -
r);
171inline constexpr std::enable_if_t<std::is_unsigned<T>::value,
int>
177 if (x == 0)
return N;
180 for (; x != 0; ++
r) {
188inline constexpr std::enable_if_t<std::is_unsigned<T>::value,
int>
214 if (x == 0)
return N;
217 T
mask = ~static_cast<T>(0);
218 int shiftr =
sizeof(T) * 4;
220 for (; shiftr; shiftr >>= 1) {
222 if ((x &
mask) == 0) {
232inline constexpr std::enable_if_t<std::is_unsigned<T>::value,
int>
234#if defined(__GNUC__) || defined(__clang__)
239 if (
sizeof(T) ==
sizeof(
unsigned long long)) {
240 return __builtin_clzll(x);
241 }
else if (
sizeof(T) ==
sizeof(
unsigned long)) {
242 return __builtin_clzl(x);
243 }
else if (
sizeof(T) ==
sizeof(
unsigned int)) {
244 return __builtin_clz(x);
253inline constexpr std::enable_if_t<std::is_unsigned<T>::value,
int>
266 if (x == 0)
return N;
269 for (; x != 0; ++
r) {
277inline constexpr std::enable_if_t<std::is_unsigned<T>::value,
int>
282 if (x == 0)
return N;
306 T
mask = ~static_cast<T>(0);
307 int shiftr =
sizeof(T) * 4;
310 for (; shiftr; shiftr >>= 1) {
312 if ((x &
mask) == 0) {
322inline constexpr std::enable_if_t<std::is_unsigned<T>::value,
int>
327 if (x == 0)
return N;
328#if defined(__GNUC__) || defined(__clang__)
329 if (
sizeof(T) ==
sizeof(
unsigned long long)) {
330 return __builtin_ctzll(x);
331 }
else if (
sizeof(T) ==
sizeof(
unsigned long)) {
332 return __builtin_ctzl(x);
333 }
else if (
sizeof(T) ==
sizeof(
unsigned int)) {
334 return __builtin_ctz(x);
350constexpr std::enable_if_t<std::is_unsigned<T>::value,
int> popcount_linear(
365constexpr std::enable_if_t<std::is_unsigned<T>::value,
int> popcount_linear_kr(
388 static_assert(
sizeof(T) <= 16,
389 "implementation of popcount works for up to 128 bit only");
392 const T bit_pattern_all = ~(
static_cast<T
>(0));
393 const T bit_pattern_5555 = bit_pattern_all / 3;
394 const T bit_pattern_3333 = bit_pattern_all / 0x0f * 3;
395 const T bit_pattern_0f0f = bit_pattern_all / 0xff * 0x0f;
396 const T bit_pattern_0101 = bit_pattern_all / 0xff;
398 v = v - ((v >> 1) & bit_pattern_5555);
399 v = (v & bit_pattern_3333) + ((v >> 2) & bit_pattern_3333);
400 v = (v + (v >> 4)) & bit_pattern_0f0f;
401 return static_cast<T
>(v * bit_pattern_0101) >> (
sizeof(T) - 1) * CHAR_BIT;
420#if defined(__clang__) || defined(__GNUC__)
421 if (
sizeof(T) ==
sizeof(
unsigned long long)) {
422 return __builtin_popcountll(v);
423 }
else if (
sizeof(T) ==
sizeof(
unsigned long)) {
424 return __builtin_popcountl(v);
426 return __builtin_popcount(v);
435constexpr std::enable_if_t<std::is_unsigned<T>::value,
int>
popcount(
448inline constexpr std::enable_if_t<std::is_unsigned<T>::value,
int>
countl_zero(
450#if (defined(__clang_major__) && (__clang_major__ >= 9))
463constexpr std::enable_if_t<std::is_unsigned<T>::value,
int>
countr_zero(
465#if (defined(__clang_major__) && (__clang_major__ >= 9))
478constexpr std::enable_if_t<std::is_unsigned<T>::value,
int>
countr_one(
489constexpr std::enable_if_t<std::is_unsigned<T>::value,
int>
countl_one(
static mi_bit_type mask[]
Definition: mi_packrec.cc:141
std::atomic< Type > N
Definition: ut0counter.h:225
Definition: authentication.cc:36
constexpr std::enable_if_t< std::is_unsigned< T >::value, int > countr_zero_linear(T x) noexcept
Definition: bit.h:254
constexpr std::enable_if_t< std::is_unsigned< T >::value, int > countl_zero_logarithmic(T x) noexcept
Definition: bit.h:189
constexpr std::enable_if_t< std::is_unsigned< T >::value, int > countl_zero_linear(T x) noexcept
Definition: bit.h:172
constexpr std::enable_if_t< std::is_unsigned< T >::value, int > popcount_constant(T v) noexcept
popcount.
Definition: bit.h:382
constexpr std::enable_if_t< std::is_unsigned< T >::value, int > popcount_builtin(T v) noexcept
popcount.
Definition: bit.h:418
constexpr std::enable_if_t< std::is_unsigned< T >::value, int > countr_zero_builtin(T x) noexcept
Definition: bit.h:323
constexpr std::enable_if_t< sizeof(T)==1, T > bswap(T t) noexcept
Definition: bit.h:89
constexpr std::enable_if_t< std::is_unsigned< T >::value, int > countl_zero_builtin(T x) noexcept
Definition: bit.h:233
constexpr std::enable_if_t< std::is_unsigned< T >::value, int > countr_zero_logarithmic(T x) noexcept
Definition: bit.h:278
constexpr std::enable_if_t< std::is_unsigned< T >::value, T > rotr(T x, int s) noexcept
Definition: bit.h:159
constexpr std::enable_if_t< std::is_unsigned< T >::value, int > countr_zero(T x) noexcept
consecutive 0-bits starting from LSB (right).
Definition: bit.h:463
constexpr std::enable_if_t< std::is_unsigned< T >::value, int > countr_one(T x) noexcept
consecutive 1-bits starting from LSB (right).
Definition: bit.h:478
constexpr std::enable_if_t< std::is_unsigned< T >::value, T > rotl(T x, int s) noexcept
Definition: bit.h:148
constexpr std::enable_if_t< std::is_unsigned< T >::value, int > countl_one(T x) noexcept
consecutive 1-bits starting from LSB (right).
Definition: bit.h:489
constexpr std::enable_if_t< std::is_integral< IntegerType >::value, IntegerType > byteswap(IntegerType t) noexcept
Definition: bit.h:142
constexpr std::enable_if_t< std::is_unsigned< T >::value, int > countl_zero(T x) noexcept
consecutive 0-bits starting from MSB.
Definition: bit.h:448
constexpr std::enable_if_t< std::is_unsigned< T >::value, int > popcount(T v) noexcept
Definition: bit.h:435
const mysql_service_registry_t * r
Definition: pfs_example_plugin_employee.cc:86
static const char digits[]
Definition: stacktrace.cc:598