Minor changes to folly/experimental/io
[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   enum class Order : uint8_t {
333     LITTLE,
334     BIG
335   };
336
337   static constexpr Order order =
338 #if __BYTE_ORDER == __LITTLE_ENDIAN
339     Order::LITTLE;
340 #elif __BYTE_ORDER == __BIG_ENDIAN
341     Order::BIG;
342 #else
343 # error Your machine uses a weird endianness!
344 #endif  /* __BYTE_ORDER */
345
346   template <class T> static T swap(T x) {
347     return detail::EndianInt<T>::swap(x);
348   }
349   template <class T> static T big(T x) {
350     return detail::EndianInt<T>::big(x);
351   }
352   template <class T> static T little(T x) {
353     return detail::EndianInt<T>::little(x);
354   }
355
356   FB_GEN(64)
357   FB_GEN(32)
358   FB_GEN(16)
359   FB_GEN(8)
360 };
361
362 #undef FB_GEN
363 #undef FB_GEN2
364 #undef FB_GEN1
365
366 /**
367  * Fast bit iteration facility.
368  */
369
370
371 template <class BaseIter> class BitIterator;
372 template <class BaseIter>
373 BitIterator<BaseIter> findFirstSet(BitIterator<BaseIter>,
374                                    BitIterator<BaseIter>);
375 /**
376  * Wrapper around an iterator over an integer type that iterates
377  * over its underlying bits in LSb to MSb order.
378  *
379  * BitIterator models the same iterator concepts as the base iterator.
380  */
381 template <class BaseIter>
382 class BitIterator
383   : public bititerator_detail::BitIteratorBase<BaseIter>::type {
384  public:
385   /**
386    * Return the number of bits in an element of the underlying iterator.
387    */
388   static size_t bitsPerBlock() {
389     return std::numeric_limits<
390       typename std::make_unsigned<
391         typename std::iterator_traits<BaseIter>::value_type
392       >::type
393     >::digits;
394   }
395
396   /**
397    * Construct a BitIterator that points at a given bit offset (default 0)
398    * in iter.
399    */
400   explicit BitIterator(const BaseIter& iter, size_t bitOffset=0)
401     : bititerator_detail::BitIteratorBase<BaseIter>::type(iter),
402       bitOffset_(bitOffset) {
403     assert(bitOffset_ < bitsPerBlock());
404   }
405
406   size_t bitOffset() const {
407     return bitOffset_;
408   }
409
410   void advanceToNextBlock() {
411     bitOffset_ = 0;
412     ++this->base_reference();
413   }
414
415   BitIterator& operator=(const BaseIter& other) {
416     this->~BitIterator();
417     new (this) BitIterator(other);
418     return *this;
419   }
420
421  private:
422   friend class boost::iterator_core_access;
423   friend BitIterator findFirstSet<>(BitIterator, BitIterator);
424
425   typedef bititerator_detail::BitReference<
426       typename std::iterator_traits<BaseIter>::reference,
427       typename std::iterator_traits<BaseIter>::value_type
428     > BitRef;
429
430   void advanceInBlock(size_t n) {
431     bitOffset_ += n;
432     assert(bitOffset_ < bitsPerBlock());
433   }
434
435   BitRef dereference() const {
436     return BitRef(*this->base_reference(), bitOffset_);
437   }
438
439   void advance(ssize_t n) {
440     size_t bpb = bitsPerBlock();
441     ssize_t blocks = n / bpb;
442     bitOffset_ += n % bpb;
443     if (bitOffset_ >= bpb) {
444       bitOffset_ -= bpb;
445       ++blocks;
446     }
447     this->base_reference() += blocks;
448   }
449
450   void increment() {
451     if (++bitOffset_ == bitsPerBlock()) {
452       advanceToNextBlock();
453     }
454   }
455
456   void decrement() {
457     if (bitOffset_-- == 0) {
458       bitOffset_ = bitsPerBlock() - 1;
459       --this->base_reference();
460     }
461   }
462
463   bool equal(const BitIterator& other) const {
464     return (bitOffset_ == other.bitOffset_ &&
465             this->base_reference() == other.base_reference());
466   }
467
468   ssize_t distance_to(const BitIterator& other) const {
469     return
470       (other.base_reference() - this->base_reference()) * bitsPerBlock() +
471       (other.bitOffset_ - bitOffset_);
472   }
473
474   ssize_t bitOffset_;
475 };
476
477 /**
478  * Helper function, so you can write
479  * auto bi = makeBitIterator(container.begin());
480  */
481 template <class BaseIter>
482 BitIterator<BaseIter> makeBitIterator(const BaseIter& iter) {
483   return BitIterator<BaseIter>(iter);
484 }
485
486
487 /**
488  * Find first bit set in a range of bit iterators.
489  * 4.5x faster than the obvious std::find(begin, end, true);
490  */
491 template <class BaseIter>
492 BitIterator<BaseIter> findFirstSet(BitIterator<BaseIter> begin,
493                                    BitIterator<BaseIter> end) {
494   // shortcut to avoid ugly static_cast<>
495   static const typename BaseIter::value_type one = 1;
496
497   while (begin.base() != end.base()) {
498     typename BaseIter::value_type v = *begin.base();
499     // mask out the bits that don't matter (< begin.bitOffset)
500     v &= ~((one << begin.bitOffset()) - 1);
501     size_t firstSet = findFirstSet(v);
502     if (firstSet) {
503       --firstSet;  // now it's 0-based
504       assert(firstSet >= begin.bitOffset());
505       begin.advanceInBlock(firstSet - begin.bitOffset());
506       return begin;
507     }
508     begin.advanceToNextBlock();
509   }
510
511   // now begin points to the same block as end
512   if (end.bitOffset() != 0) {  // assume end is dereferenceable
513     typename BaseIter::value_type v = *begin.base();
514     // mask out the bits that don't matter (< begin.bitOffset)
515     v &= ~((one << begin.bitOffset()) - 1);
516     // mask out the bits that don't matter (>= end.bitOffset)
517     v &= (one << end.bitOffset()) - 1;
518     size_t firstSet = findFirstSet(v);
519     if (firstSet) {
520       --firstSet;  // now it's 0-based
521       assert(firstSet >= begin.bitOffset());
522       begin.advanceInBlock(firstSet - begin.bitOffset());
523       return begin;
524     }
525   }
526
527   return end;
528 }
529
530
531 namespace detail {
532
533 template <class T, class Enable=void> struct Unaligned;
534
535 template <class T>
536 struct Unaligned<
537   T,
538   typename std::enable_if<std::is_pod<T>::value>::type> {
539   T value;
540 } __attribute__((packed));
541
542 }  // namespace detail
543
544 /**
545  * Read an unaligned value of type T and return it.
546  */
547 template <class T>
548 inline T loadUnaligned(const void* p) {
549   static_assert(alignof(detail::Unaligned<T>) == 1, "Invalid alignment");
550   return static_cast<const detail::Unaligned<T>*>(p)->value;
551 }
552
553 /**
554  * Write an unaligned value of type T.
555  */
556 template <class T>
557 inline void storeUnaligned(void* p, T value) {
558   static_assert(alignof(detail::Unaligned<T>) == 1, "Invalid alignment");
559   static_cast<detail::Unaligned<T>*>(p)->value = value;
560 }
561
562 }  // namespace folly
563
564 #endif /* FOLLY_BITS_H_ */
565