16 #ifndef IMPALA_BIT_UTIL_H
17 #define IMPALA_BIT_UTIL_H
32 static inline int Ceil(
int value,
int divisor) {
33 return value / divisor + (value % divisor != 0);
37 static inline int RoundUp(
int value,
int factor) {
38 return (value + (factor - 1)) / factor * factor;
42 static inline int RoundDown(
int value,
int factor) {
43 return (value / factor) * factor;
65 DCHECK((factor > 0) && ((factor & (factor - 1)) == 0));
66 return (value + (factor - 1)) & ~(factor - 1);
70 DCHECK((factor > 0) && ((factor & (factor - 1)) == 0));
71 return value & ~(factor - 1);
78 return (bits + 7) >> 3;
88 return (bits + 31) >> 5;
98 return (bits + 63) >> 6;
111 for (; x != 0; ++
count) x &= x-1;
126 if (
UNLIKELY(num_bits == 0))
return 0;
127 if (
UNLIKELY(num_bits >= 64))
return v;
128 int n = 64 - num_bits;
129 return (v << n) >> n;
137 if (x == 1)
return 0;
144 while (x >>= 1) ++result;
150 return __builtin_bswap64(value);
153 return static_cast<uint64_t>(__builtin_bswap64(value));
156 return __builtin_bswap32(value);
159 return static_cast<uint32_t
>(__builtin_bswap32(value));
162 return (((value >> 8) & 0xff) | ((value & 0xff) << 8));
165 return static_cast<uint16_t
>(
ByteSwap(static_cast<int16_t>(value)));
169 static inline void ByteSwap(
void* dst,
const void* src,
int len) {
172 *
reinterpret_cast<int8_t*
>(dst) = *reinterpret_cast<const int8_t*>(src);
175 *
reinterpret_cast<int16_t*
>(dst) =
176 ByteSwap(*reinterpret_cast<const int16_t*>(src));
179 *
reinterpret_cast<int32_t*
>(dst) =
180 ByteSwap(*reinterpret_cast<const int32_t*>(src));
183 *
reinterpret_cast<int64_t*
>(dst) =
184 ByteSwap(*reinterpret_cast<const int64_t*>(src));
189 uint8_t* d =
reinterpret_cast<uint8_t*
>(dst);
190 const uint8_t* s =
reinterpret_cast<const uint8_t*
>(src);
191 for (
int i = 0; i < len; ++i) {
192 d[i] = s[len - i - 1];
198 #if __BYTE_ORDER == __LITTLE_ENDIAN
206 static inline int64_t
ToBigEndian(int64_t val) {
return val; }
208 static inline int32_t
ToBigEndian(int32_t val) {
return val; }
209 static inline uint32_t
ToBigEndian(uint32_t val) {
return val; }
210 static inline int16_t
ToBigEndian(int16_t val) {
return val; }
211 static inline uint16_t
ToBigEndian(uint16_t val) {
return val; }
215 #if __BYTE_ORDER == __LITTLE_ENDIAN
223 static inline int64_t
FromBigEndian(int64_t val) {
return val; }
225 static inline int32_t
FromBigEndian(int32_t val) {
return val; }
226 static inline uint32_t
FromBigEndian(uint32_t val) {
return val; }
227 static inline int16_t
FromBigEndian(int16_t val) {
return val; }
228 static inline uint16_t
FromBigEndian(uint16_t val) {
return val; }
static uint16_t ToBigEndian(uint16_t value)
static int16_t ByteSwap(int16_t value)
static uint64_t FromBigEndian(uint64_t value)
static uint32_t RoundDownNumi64(uint32_t bits)
Returns the rounded down to 64 multiple.
static uint32_t ToBigEndian(uint32_t value)
static int64_t POPCNT_popcnt_u64(uint64_t a)
static int64_t ByteSwap(int64_t value)
Swaps the byte order (i.e. endianess)
static int32_t FromBigEndian(int32_t value)
static int16_t ToBigEndian(int16_t value)
static uint32_t RoundUpNumi32(uint32_t bits)
Returns the rounded up to 32 multiple. Used for conversions of bits to i32.
static int RoundDown(int value, int factor)
Returns 'value' rounded down to the nearest multiple of 'factor'.
static uint16_t ByteSwap(uint16_t value)
static void ByteSwap(void *dst, const void *src, int len)
Write the swapped bytes into dst. Src and st cannot overlap.
static const int64_t POPCNT
static uint64_t TrailingBits(uint64_t v, int num_bits)
Returns the 'num_bits' least-significant bits of 'v'.
static uint32_t RoundDownNumi32(uint32_t bits)
Returns the rounded up 32 multiple.
static int PopcountNoHw(uint64_t x)
static uint32_t RoundUpNumBytes(uint32_t bits)
static uint64_t ToBigEndian(uint64_t value)
static uint32_t ByteSwap(uint32_t value)
static uint16_t FromBigEndian(uint16_t value)
static int RoundDownToPowerOf2(int value, int factor)
static int64_t FromBigEndian(int64_t value)
Converts from big endian format to the machine's native endian format.
static int Ceil(int value, int divisor)
Returns the ceil of value/divisor.
static int32_t ByteSwap(int32_t value)
static uint32_t FromBigEndian(uint32_t value)
static int64_t ToBigEndian(int64_t value)
static int64_t NextPowerOfTwo(int64_t v)
static int32_t ToBigEndian(int32_t value)
static uint64_t ByteSwap(uint64_t value)
static uint32_t RoundUpNumi64(uint32_t bits)
Returns the rounded up to 64 multiple. Used for conversions of bits to i64.
static int16_t FromBigEndian(int16_t value)
static bool IsSupported(long flag)
Returns whether of not the cpu supports this flag.
static uint32_t RoundDownNumBytes(uint32_t bits)
Returns the rounded down number of bytes that fit the number of bits.
static int Popcount(uint64_t x)
Returns the number of set bits in x.
static int RoundUpToPowerOf2(int value, int factor)
static int RoundUp(int value, int factor)
Returns 'value' rounded up to the nearest multiple of 'factor'.
static int Log2(uint64_t x)