mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-11-05 15:21:43 +00:00
33 lines
632 B
C++
33 lines
632 B
C++
#pragma once
|
|
|
|
#include <cstddef>
|
|
|
|
|
|
/** Returns log2 of number, rounded down.
|
|
* Compiles to single 'bsr' instruction on x86.
|
|
* For zero argument, result is unspecified.
|
|
*/
|
|
inline unsigned int bitScanReverse(unsigned int x)
|
|
{
|
|
return sizeof(unsigned int) * 8 - 1 - __builtin_clz(x);
|
|
}
|
|
|
|
|
|
/** For zero argument, result is zero.
|
|
* For arguments with most significand bit set, result is zero.
|
|
* For other arguments, returns value, rounded up to power of two.
|
|
*/
|
|
inline size_t roundUpToPowerOfTwoOrZero(size_t n)
|
|
{
|
|
--n;
|
|
n |= n >> 1;
|
|
n |= n >> 2;
|
|
n |= n >> 4;
|
|
n |= n >> 8;
|
|
n |= n >> 16;
|
|
n |= n >> 32;
|
|
++n;
|
|
|
|
return n;
|
|
}
|