Pull from FB rev 63ce89e2f2301e6bba44a111cc7d4218022156f6
[folly.git] / folly / Bits.h
1 /*
2  * Copyright 2012 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /**
18  * Various low-level, bit-manipulation routines.
19  *
20  * findFirstSet(x)
21  *    find first (least significant) bit set in a value of an integral type,
22  *    1-based (like ffs()).  0 = no bits are set (x == 0)
23  *
24  * findLastSet(x)
25  *    find last (most significant) bit set in a value of an integral type,
26  *    1-based.  0 = no bits are set (x == 0)
27  *    for x != 0, findFirstSet(x) == 1 + floor(log2(x))
28  *
29  * nextPowTwo(x)
30  *    Finds the next power of two >= x.
31  *
32  * Endian
33  *    convert between native, big, and little endian representation
34  *    Endian::big(x)      big <-> native
35  *    Endian::little(x)   little <-> native
36  *    Endian::swap(x)     big <-> little
37  *
38  * BitIterator
39  *    Wrapper around an iterator over an integral type that iterates
40  *    over its underlying bits in MSb to LSb order
41  *
42  * findFirstSet(BitIterator begin, BitIterator end)
43  *    return a BitIterator pointing to the first 1 bit in [begin, end), or
44  *    end if all bits in [begin, end) are 0
45  *
46  * @author Tudor Bosman (tudorb@fb.com)
47  */
48
49 #ifndef FOLLY_BITS_H_
50 #define FOLLY_BITS_H_
51
52 #include "folly/Portability.h"
53
54 #ifndef _GNU_SOURCE
55 #define _GNU_SOURCE 1
56 #endif
57
58 #include "folly/detail/BitIteratorDetail.h"
59 #include "folly/Likely.h"
60
61 #include <byteswap.h>
62 #include <cassert>
63 #include <cinttypes>
64 #include <cstring>  // for ffs, ffsl, ffsll
65 #include <endian.h>
66 #include <iterator>
67 #include <limits>
68 #include <type_traits>
69 #include <boost/iterator/iterator_adaptor.hpp>
70 #include <stdint.h>
71
72 namespace folly {
73
74 // Generate overloads for findFirstSet as wrappers around
75 // appropriate ffs, ffsl, ffsll functions from glibc.
76 // We first define these overloads for signed types (because ffs, ffsl, ffsll
77 // take int, long, and long long as arguments, respectively) and then
78 // define an overload for unsigned that forwards to the overload for the
79 // corresponding signed type.
80 template <class T>
81 typename std::enable_if<
82   (std::is_integral<T>::value &&
83    std::is_signed<T>::value &&
84    (std::numeric_limits<T>::digits <= std::numeric_limits<int>::digits)),
85   unsigned int>::type
86   findFirstSet(T x) {
87   return ::ffs(static_cast<int>(x));
88 }
89
90 template <class T>
91 typename std::enable_if<
92   (std::is_integral<T>::value &&
93    std::is_signed<T>::value &&
94    (std::numeric_limits<T>::digits > std::numeric_limits<int>::digits) &&
95    (std::numeric_limits<T>::digits <= std::numeric_limits<long>::digits)),
96   unsigned int>::type
97   findFirstSet(T x) {
98   return ::ffsl(static_cast<long>(x));
99 }
100
101 #ifdef FOLLY_HAVE_FFSLL
102
103 template <class T>
104 typename std::enable_if<
105   (std::is_integral<T>::value &&
106    std::is_signed<T>::value &&
107    (std::numeric_limits<T>::digits > std::numeric_limits<long>::digits) &&
108    (std::numeric_limits<T>::digits <= std::numeric_limits<long long>::digits)),
109   unsigned int>::type
110   findFirstSet(T x) {
111   return ::ffsll(static_cast<long long>(x));
112 }
113
114 #endif
115
116 template <class T>
117 typename std::enable_if<
118   (std::is_integral<T>::value &&
119    !std::is_signed<T>::value),
120   unsigned int>::type
121   findFirstSet(T x) {
122   // Note that conversion from an unsigned type to the corresponding signed
123   // type is technically implementation-defined, but will likely work
124   // on any impementation that uses two's complement.
125   return findFirstSet(static_cast<typename std::make_signed<T>::type>(x));
126 }
127
128 namespace detail {
129
130 // Portable, but likely slow...
131 inline unsigned int findLastSetPortable(uint64_t x) {
132   unsigned int r = (x != 0);  // 1-based index, except for x==0
133   while (x >>= 1) {
134     ++r;
135   }
136   return r;
137 }
138
139 }  // namespace detail
140
141 #ifdef __GNUC__
142
143 // findLastSet: return the 1-based index of the highest bit set
144 // for x > 0, findLastSet(x) == 1 + floor(log2(x))
145 template <class T>
146 typename std::enable_if<
147   (std::is_integral<T>::value &&
148    std::is_unsigned<T>::value &&
149    (std::numeric_limits<T>::digits <=
150     std::numeric_limits<unsigned int>::digits)),
151   unsigned int>::type
152   findLastSet(T x) {
153   return x ? 8 * sizeof(unsigned int) - __builtin_clz(x) : 0;
154 }
155
156 template <class T>
157 typename std::enable_if<
158   (std::is_integral<T>::value &&
159    std::is_unsigned<T>::value &&
160    (std::numeric_limits<T>::digits >
161     std::numeric_limits<unsigned int>::digits) &&
162    (std::numeric_limits<T>::digits <=
163     std::numeric_limits<unsigned long>::digits)),
164   unsigned int>::type
165   findLastSet(T x) {
166   return x ? 8 * sizeof(unsigned long) - __builtin_clzl(x) : 0;
167 }
168
169 template <class T>
170 typename std::enable_if<
171   (std::is_integral<T>::value &&
172    std::is_unsigned<T>::value &&
173    (std::numeric_limits<T>::digits >
174     std::numeric_limits<unsigned long>::digits) &&
175    (std::numeric_limits<T>::digits <=
176     std::numeric_limits<unsigned long long>::digits)),
177   unsigned int>::type
178   findLastSet(T x) {
179   return x ? 8 * sizeof(unsigned long long) - __builtin_clzll(x) : 0;
180 }
181
182 #else  /* !__GNUC__ */
183
184 template <class T>
185 typename std::enable_if<
186   (std::is_integral<T>::value &&
187    std::is_unsigned<T>::value),
188   unsigned int>::type
189   findLastSet(T x) {
190   return detail:findLastSetPortable(x);
191 }
192
193 #endif
194
195 template <class T>
196 typename std::enable_if<
197   (std::is_integral<T>::value &&
198    std::is_signed<T>::value),
199   unsigned int>::type
200   findLastSet(T x) {
201   return findLastSet(static_cast<typename std::make_unsigned<T>::type>(x));
202 }
203
204 namespace detail {
205
206 template <class T>
207 inline
208 typename std::enable_if<
209   std::is_integral<T>::value && std::is_unsigned<T>::value,
210   T>::type
211 nextPowTwoPortable(T v) {
212   if (UNLIKELY(v == 0)) {
213     return 1;
214   }
215
216   --v;
217   for (uint32_t i = 1; i < sizeof(T) * 8; i <<= 8) {
218     v |= (v >> i);
219     v |= (v >> (i << 1));
220     v |= (v >> (i << 2));
221     v |= (v >> (i << 3));
222     v |= (v >> (i << 4));
223     v |= (v >> (i << 5));
224     v |= (v >> (i << 6));
225     v |= (v >> (i << 7));
226   }
227   return v + 1;
228 }
229
230 }  // namespace detail
231
232 #ifdef __GNUC__
233
234 template <class T>
235 inline
236 typename std::enable_if<
237   std::is_integral<T>::value && std::is_unsigned<T>::value,
238   T>::type
239 nextPowTwo(T v) {
240   if (UNLIKELY(v == 0)) {
241     return 1;
242   }
243   return 1ul << findLastSet(v - 1);
244 }
245
246 #else /* __GNUC__ */
247
248 template <class T>
249 inline
250 typename std::enable_if<
251   std::is_integral<T>::value && std::is_unsigned<T>::value,
252   T>::type
253 nextPowTwo(T v) {
254   return detail::nextPowTwoPortable(v);
255 }
256
257 #endif /* __GNUC__ */
258
259
260
261 /**
262  * Endianness detection and manipulation primitives.
263  */
264 namespace detail {
265
266 template <class T>
267 struct EndianIntBase {
268  public:
269   static T swap(T x);
270 };
271
272 #define FB_GEN(t, fn) \
273 template<> inline t EndianIntBase<t>::swap(t x) { return fn(x); }
274
275 // fn(x) expands to (x) if the second argument is empty, which is exactly
276 // what we want for [u]int8_t
277 FB_GEN( int8_t,)
278 FB_GEN(uint8_t,)
279 FB_GEN( int64_t, bswap_64)
280 FB_GEN(uint64_t, bswap_64)
281 FB_GEN( int32_t, bswap_32)
282 FB_GEN(uint32_t, bswap_32)
283 FB_GEN( int16_t, bswap_16)
284 FB_GEN(uint16_t, bswap_16)
285
286 #undef FB_GEN
287
288 #if __BYTE_ORDER == __LITTLE_ENDIAN
289
290 template <class T>
291 struct EndianInt : public detail::EndianIntBase<T> {
292  public:
293   static T big(T x) { return EndianInt::swap(x); }
294   static T little(T x) { return x; }
295 };
296
297 #elif __BYTE_ORDER == __BIG_ENDIAN
298
299 template <class T>
300 struct EndianInt : public detail::EndianIntBase<T> {
301  public:
302   static T big(T x) { return x; }
303   static T little(T x) { return EndianInt::swap(x); }
304 };
305
306 #else
307 # error Your machine uses a weird endianness!
308 #endif  /* __BYTE_ORDER */
309
310 }  // namespace detail
311
312 // big* convert between native and big-endian representations
313 // little* convert between native and little-endian representations
314 // swap* convert between big-endian and little-endian representations
315 //
316 // ntohs, htons == big16
317 // ntohl, htonl == big32
318 #define FB_GEN1(fn, t, sz) \
319   static t fn##sz(t x) { return fn<t>(x); } \
320
321 #define FB_GEN2(t, sz) \
322   FB_GEN1(swap, t, sz) \
323   FB_GEN1(big, t, sz) \
324   FB_GEN1(little, t, sz)
325
326 #define FB_GEN(sz) \
327   FB_GEN2(uint##sz##_t, sz) \
328   FB_GEN2(int##sz##_t, sz)
329
330 class Endian {
331  public:
332   template <class T> static T swap(T x) {
333     return detail::EndianInt<T>::swap(x);
334   }
335   template <class T> static T big(T x) {
336     return detail::EndianInt<T>::big(x);
337   }
338   template <class T> static T little(T x) {
339     return detail::EndianInt<T>::little(x);
340   }
341
342   FB_GEN(64)
343   FB_GEN(32)
344   FB_GEN(16)
345   FB_GEN(8)
346 };
347
348 #undef FB_GEN
349 #undef FB_GEN2
350 #undef FB_GEN1
351
352 /**
353  * Fast bit iteration facility.
354  */
355
356
357 template <class BaseIter> class BitIterator;
358 template <class BaseIter>
359 BitIterator<BaseIter> findFirstSet(BitIterator<BaseIter>,
360                                    BitIterator<BaseIter>);
361 /**
362  * Wrapper around an iterator over an integer type that iterates
363  * over its underlying bits in LSb to MSb order.
364  *
365  * BitIterator models the same iterator concepts as the base iterator.
366  */
367 template <class BaseIter>
368 class BitIterator
369   : public bititerator_detail::BitIteratorBase<BaseIter>::type {
370  public:
371   /**
372    * Return the number of bits in an element of the underlying iterator.
373    */
374   static size_t bitsPerBlock() {
375     return std::numeric_limits<
376       typename std::make_unsigned<
377         typename std::iterator_traits<BaseIter>::value_type
378       >::type
379     >::digits;
380   }
381
382   /**
383    * Construct a BitIterator that points at a given bit offset (default 0)
384    * in iter.
385    */
386   explicit BitIterator(const BaseIter& iter, size_t bitOffset=0)
387     : bititerator_detail::BitIteratorBase<BaseIter>::type(iter),
388       bitOffset_(bitOffset) {
389     assert(bitOffset_ < bitsPerBlock());
390   }
391
392   size_t bitOffset() const {
393     return bitOffset_;
394   }
395
396   void advanceToNextBlock() {
397     bitOffset_ = 0;
398     ++this->base_reference();
399   }
400
401   BitIterator& operator=(const BaseIter& other) {
402     this->~BitIterator();
403     new (this) BitIterator(other);
404     return *this;
405   }
406
407  private:
408   friend class boost::iterator_core_access;
409   friend BitIterator findFirstSet<>(BitIterator, BitIterator);
410
411   typedef bititerator_detail::BitReference<
412       typename std::iterator_traits<BaseIter>::reference,
413       typename std::iterator_traits<BaseIter>::value_type
414     > BitRef;
415
416   void advanceInBlock(size_t n) {
417     bitOffset_ += n;
418     assert(bitOffset_ < bitsPerBlock());
419   }
420
421   BitRef dereference() const {
422     return BitRef(*this->base_reference(), bitOffset_);
423   }
424
425   void advance(ssize_t n) {
426     size_t bpb = bitsPerBlock();
427     ssize_t blocks = n / bpb;
428     bitOffset_ += n % bpb;
429     if (bitOffset_ >= bpb) {
430       bitOffset_ -= bpb;
431       ++blocks;
432     }
433     this->base_reference() += blocks;
434   }
435
436   void increment() {
437     if (++bitOffset_ == bitsPerBlock()) {
438       advanceToNextBlock();
439     }
440   }
441
442   void decrement() {
443     if (bitOffset_-- == 0) {
444       bitOffset_ = bitsPerBlock() - 1;
445       --this->base_reference();
446     }
447   }
448
449   bool equal(const BitIterator& other) const {
450     return (bitOffset_ == other.bitOffset_ &&
451             this->base_reference() == other.base_reference());
452   }
453
454   ssize_t distance_to(const BitIterator& other) const {
455     return
456       (other.base_reference() - this->base_reference()) * bitsPerBlock() +
457       (other.bitOffset_ - bitOffset_);
458   }
459
460   ssize_t bitOffset_;
461 };
462
463 /**
464  * Helper function, so you can write
465  * auto bi = makeBitIterator(container.begin());
466  */
467 template <class BaseIter>
468 BitIterator<BaseIter> makeBitIterator(const BaseIter& iter) {
469   return BitIterator<BaseIter>(iter);
470 }
471
472
473 /**
474  * Find first bit set in a range of bit iterators.
475  * 4.5x faster than the obvious std::find(begin, end, true);
476  */
477 template <class BaseIter>
478 BitIterator<BaseIter> findFirstSet(BitIterator<BaseIter> begin,
479                                    BitIterator<BaseIter> end) {
480   // shortcut to avoid ugly static_cast<>
481   static const typename BaseIter::value_type one = 1;
482
483   while (begin.base() != end.base()) {
484     typename BaseIter::value_type v = *begin.base();
485     // mask out the bits that don't matter (< begin.bitOffset)
486     v &= ~((one << begin.bitOffset()) - 1);
487     size_t firstSet = findFirstSet(v);
488     if (firstSet) {
489       --firstSet;  // now it's 0-based
490       assert(firstSet >= begin.bitOffset());
491       begin.advanceInBlock(firstSet - begin.bitOffset());
492       return begin;
493     }
494     begin.advanceToNextBlock();
495   }
496
497   // now begin points to the same block as end
498   if (end.bitOffset() != 0) {  // assume end is dereferenceable
499     typename BaseIter::value_type v = *begin.base();
500     // mask out the bits that don't matter (< begin.bitOffset)
501     v &= ~((one << begin.bitOffset()) - 1);
502     // mask out the bits that don't matter (>= end.bitOffset)
503     v &= (one << end.bitOffset()) - 1;
504     size_t firstSet = findFirstSet(v);
505     if (firstSet) {
506       --firstSet;  // now it's 0-based
507       assert(firstSet >= begin.bitOffset());
508       begin.advanceInBlock(firstSet - begin.bitOffset());
509       return begin;
510     }
511   }
512
513   return end;
514 }
515
516 }  // namespace folly
517
518 #endif /* FOLLY_BITS_H_ */
519