Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
bit-util.h
Go to the documentation of this file.
1 // Copyright 2012 Cloudera Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 
16 #ifndef IMPALA_BIT_UTIL_H
17 #define IMPALA_BIT_UTIL_H
18 
19 #include <endian.h>
20 
21 #include "common/compiler-util.h"
22 #include "util/cpu-info.h"
23 #include "util/sse-util.h"
24 
25 namespace impala {
26 
29 class BitUtil {
30  public:
32  static inline int Ceil(int value, int divisor) {
33  return value / divisor + (value % divisor != 0);
34  }
35 
37  static inline int RoundUp(int value, int factor) {
38  return (value + (factor - 1)) / factor * factor;
39  }
40 
42  static inline int RoundDown(int value, int factor) {
43  return (value / factor) * factor;
44  }
45 
50  static inline int64_t NextPowerOfTwo(int64_t v) {
51  --v;
52  v |= v >> 1;
53  v |= v >> 2;
54  v |= v >> 4;
55  v |= v >> 8;
56  v |= v >> 16;
57  v |= v >> 32;
58  ++v;
59  return v;
60  }
61 
64  static inline int RoundUpToPowerOf2(int value, int factor) {
65  DCHECK((factor > 0) && ((factor & (factor - 1)) == 0));
66  return (value + (factor - 1)) & ~(factor - 1);
67  }
68 
69  static inline int RoundDownToPowerOf2(int value, int factor) {
70  DCHECK((factor > 0) && ((factor & (factor - 1)) == 0));
71  return value & ~(factor - 1);
72  }
73 
77  static inline uint32_t RoundUpNumBytes(uint32_t bits) {
78  return (bits + 7) >> 3;
79  }
80 
82  static inline uint32_t RoundDownNumBytes(uint32_t bits) {
83  return bits >> 3;
84  }
85 
87  static inline uint32_t RoundUpNumi32(uint32_t bits) {
88  return (bits + 31) >> 5;
89  }
90 
92  static inline uint32_t RoundDownNumi32(uint32_t bits) {
93  return bits >> 5;
94  }
95 
97  static inline uint32_t RoundUpNumi64(uint32_t bits) {
98  return (bits + 63) >> 6;
99  }
100 
102  static inline uint32_t RoundDownNumi64(uint32_t bits) {
103  return bits >> 6;
104  }
105 
109  static inline int PopcountNoHw(uint64_t x) {
110  int count = 0;
111  for (; x != 0; ++count) x &= x-1;
112  return count;
113  }
114 
116  static inline int Popcount(uint64_t x) {
118  return POPCNT_popcnt_u64(x);
119  } else {
120  return PopcountNoHw(x);
121  }
122  }
123 
125  static inline uint64_t TrailingBits(uint64_t v, int num_bits) {
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;
130  }
131 
135  static inline int Log2(uint64_t x) {
136  DCHECK_GT(x, 0);
137  if (x == 1) return 0;
138  // Compute result = ceil(log2(x))
139  // = floor(log2(x - 1)) + 1, for x > 1
140  // by finding the position of the most significant bit (1-indexed) of x - 1
141  // (floor(log2(n)) = MSB(n) (0-indexed))
142  --x;
143  int result = 1;
144  while (x >>= 1) ++result;
145  return result;
146  }
147 
149  static inline int64_t ByteSwap(int64_t value) {
150  return __builtin_bswap64(value);
151  }
152  static inline uint64_t ByteSwap(uint64_t value) {
153  return static_cast<uint64_t>(__builtin_bswap64(value));
154  }
155  static inline int32_t ByteSwap(int32_t value) {
156  return __builtin_bswap32(value);
157  }
158  static inline uint32_t ByteSwap(uint32_t value) {
159  return static_cast<uint32_t>(__builtin_bswap32(value));
160  }
161  static inline int16_t ByteSwap(int16_t value) {
162  return (((value >> 8) & 0xff) | ((value & 0xff) << 8));
163  }
164  static inline uint16_t ByteSwap(uint16_t value) {
165  return static_cast<uint16_t>(ByteSwap(static_cast<int16_t>(value)));
166  }
167 
169  static inline void ByteSwap(void* dst, const void* src, int len) {
170  switch (len) {
171  case 1:
172  *reinterpret_cast<int8_t*>(dst) = *reinterpret_cast<const int8_t*>(src);
173  return;
174  case 2:
175  *reinterpret_cast<int16_t*>(dst) =
176  ByteSwap(*reinterpret_cast<const int16_t*>(src));
177  return;
178  case 4:
179  *reinterpret_cast<int32_t*>(dst) =
180  ByteSwap(*reinterpret_cast<const int32_t*>(src));
181  return;
182  case 8:
183  *reinterpret_cast<int64_t*>(dst) =
184  ByteSwap(*reinterpret_cast<const int64_t*>(src));
185  return;
186  default: break;
187  }
188 
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];
193  }
194  }
195 
198 #if __BYTE_ORDER == __LITTLE_ENDIAN
199  static inline int64_t ToBigEndian(int64_t value) { return ByteSwap(value); }
200  static inline uint64_t ToBigEndian(uint64_t value) { return ByteSwap(value); }
201  static inline int32_t ToBigEndian(int32_t value) { return ByteSwap(value); }
202  static inline uint32_t ToBigEndian(uint32_t value) { return ByteSwap(value); }
203  static inline int16_t ToBigEndian(int16_t value) { return ByteSwap(value); }
204  static inline uint16_t ToBigEndian(uint16_t value) { return ByteSwap(value); }
205 #else
206  static inline int64_t ToBigEndian(int64_t val) { return val; }
207  static inline uint64_t ToBigEndian(uint64_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; }
212 #endif
213 
215 #if __BYTE_ORDER == __LITTLE_ENDIAN
216  static inline int64_t FromBigEndian(int64_t value) { return ByteSwap(value); }
217  static inline uint64_t FromBigEndian(uint64_t value) { return ByteSwap(value); }
218  static inline int32_t FromBigEndian(int32_t value) { return ByteSwap(value); }
219  static inline uint32_t FromBigEndian(uint32_t value) { return ByteSwap(value); }
220  static inline int16_t FromBigEndian(int16_t value) { return ByteSwap(value); }
221  static inline uint16_t FromBigEndian(uint16_t value) { return ByteSwap(value); }
222 #else
223  static inline int64_t FromBigEndian(int64_t val) { return val; }
224  static inline uint64_t FromBigEndian(uint64_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; }
229 #endif
230 
231 };
232 
233 }
234 
235 #endif
static uint16_t ToBigEndian(uint16_t value)
Definition: bit-util.h:204
static int16_t ByteSwap(int16_t value)
Definition: bit-util.h:161
static uint64_t FromBigEndian(uint64_t value)
Definition: bit-util.h:217
static uint32_t RoundDownNumi64(uint32_t bits)
Returns the rounded down to 64 multiple.
Definition: bit-util.h:102
static uint32_t ToBigEndian(uint32_t value)
Definition: bit-util.h:202
static int64_t POPCNT_popcnt_u64(uint64_t a)
Definition: sse-util.h:117
static int64_t ByteSwap(int64_t value)
Swaps the byte order (i.e. endianess)
Definition: bit-util.h:149
static int32_t FromBigEndian(int32_t value)
Definition: bit-util.h:218
static int16_t ToBigEndian(int16_t value)
Definition: bit-util.h:203
static uint32_t RoundUpNumi32(uint32_t bits)
Returns the rounded up to 32 multiple. Used for conversions of bits to i32.
Definition: bit-util.h:87
static int RoundDown(int value, int factor)
Returns 'value' rounded down to the nearest multiple of 'factor'.
Definition: bit-util.h:42
static uint16_t ByteSwap(uint16_t value)
Definition: bit-util.h:164
static void ByteSwap(void *dst, const void *src, int len)
Write the swapped bytes into dst. Src and st cannot overlap.
Definition: bit-util.h:169
static const int64_t POPCNT
Definition: cpu-info.h:35
static uint64_t TrailingBits(uint64_t v, int num_bits)
Returns the 'num_bits' least-significant bits of 'v'.
Definition: bit-util.h:125
static uint32_t RoundDownNumi32(uint32_t bits)
Returns the rounded up 32 multiple.
Definition: bit-util.h:92
static int PopcountNoHw(uint64_t x)
Definition: bit-util.h:109
static uint32_t RoundUpNumBytes(uint32_t bits)
Definition: bit-util.h:77
static uint64_t ToBigEndian(uint64_t value)
Definition: bit-util.h:200
static uint32_t ByteSwap(uint32_t value)
Definition: bit-util.h:158
static uint16_t FromBigEndian(uint16_t value)
Definition: bit-util.h:221
static int RoundDownToPowerOf2(int value, int factor)
Definition: bit-util.h:69
static int64_t FromBigEndian(int64_t value)
Converts from big endian format to the machine's native endian format.
Definition: bit-util.h:216
static int Ceil(int value, int divisor)
Returns the ceil of value/divisor.
Definition: bit-util.h:32
uint64_t count
static int32_t ByteSwap(int32_t value)
Definition: bit-util.h:155
static uint32_t FromBigEndian(uint32_t value)
Definition: bit-util.h:219
static int64_t ToBigEndian(int64_t value)
Definition: bit-util.h:199
static int64_t NextPowerOfTwo(int64_t v)
Definition: bit-util.h:50
#define UNLIKELY(expr)
Definition: compiler-util.h:33
static int32_t ToBigEndian(int32_t value)
Definition: bit-util.h:201
#define LIKELY(expr)
Definition: compiler-util.h:32
static uint64_t ByteSwap(uint64_t value)
Definition: bit-util.h:152
static uint32_t RoundUpNumi64(uint32_t bits)
Returns the rounded up to 64 multiple. Used for conversions of bits to i64.
Definition: bit-util.h:97
static int16_t FromBigEndian(int16_t value)
Definition: bit-util.h:220
static bool IsSupported(long flag)
Returns whether of not the cpu supports this flag.
Definition: cpu-info.h:58
static uint32_t RoundDownNumBytes(uint32_t bits)
Returns the rounded down number of bytes that fit the number of bits.
Definition: bit-util.h:82
static int Popcount(uint64_t x)
Returns the number of set bits in x.
Definition: bit-util.h:116
static int RoundUpToPowerOf2(int value, int factor)
Definition: bit-util.h:64
static int RoundUp(int value, int factor)
Returns 'value' rounded up to the nearest multiple of 'factor'.
Definition: bit-util.h:37
static int Log2(uint64_t x)
Definition: bit-util.h:135