2 * Copyright 2017 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * Various low-level, bit-manipulation routines.
20 * findFirstSet(x) [constexpr]
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)
24 * findLastSet(x) [constexpr]
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, findLastSet(x) == 1 + floor(log2(x))
29 * nextPowTwo(x) [constexpr]
30 * Finds the next power of two >= x.
32 * isPowTwo(x) [constexpr]
33 * return true iff x is a power of two
36 * return the number of 1 bits in x
39 * convert between native, big, and little endian representation
40 * Endian::big(x) big <-> native
41 * Endian::little(x) little <-> native
42 * Endian::swap(x) big <-> little
45 * Wrapper around an iterator over an integral type that iterates
46 * over its underlying bits in MSb to LSb order
48 * findFirstSet(BitIterator begin, BitIterator end)
49 * return a BitIterator pointing to the first 1 bit in [begin, end), or
50 * end if all bits in [begin, end) are 0
52 * @author Tudor Bosman (tudorb@fb.com)
57 // MSVC does not support intrinsics constexpr
59 #define FOLLY_INTRINSIC_CONSTEXPR const
61 #define FOLLY_INTRINSIC_CONSTEXPR constexpr
70 #include <type_traits>
72 #include <boost/iterator/iterator_adaptor.hpp>
74 #include <folly/Assume.h>
75 #include <folly/Likely.h>
76 #include <folly/Portability.h>
77 #include <folly/detail/BitIteratorDetail.h>
78 #include <folly/portability/Builtins.h>
82 // Generate overloads for findFirstSet as wrappers around
83 // appropriate ffs, ffsl, ffsll gcc builtins
85 inline FOLLY_INTRINSIC_CONSTEXPR
86 typename std::enable_if<
87 (std::is_integral<T>::value &&
88 std::is_unsigned<T>::value &&
89 sizeof(T) <= sizeof(unsigned int)),
92 return static_cast<unsigned int>(__builtin_ffs(static_cast<int>(x)));
96 inline FOLLY_INTRINSIC_CONSTEXPR
97 typename std::enable_if<
98 (std::is_integral<T>::value &&
99 std::is_unsigned<T>::value &&
100 sizeof(T) > sizeof(unsigned int) &&
101 sizeof(T) <= sizeof(unsigned long)),
104 return static_cast<unsigned int>(__builtin_ffsl(static_cast<long>(x)));
108 inline FOLLY_INTRINSIC_CONSTEXPR
109 typename std::enable_if<
110 (std::is_integral<T>::value &&
111 std::is_unsigned<T>::value &&
112 sizeof(T) > sizeof(unsigned long) &&
113 sizeof(T) <= sizeof(unsigned long long)),
116 return static_cast<unsigned int>(__builtin_ffsll(static_cast<long long>(x)));
120 inline FOLLY_INTRINSIC_CONSTEXPR
121 typename std::enable_if<
122 (std::is_integral<T>::value && std::is_signed<T>::value),
125 // Note that conversion from a signed type to the corresponding unsigned
126 // type is technically implementation-defined, but will likely work
127 // on any impementation that uses two's complement.
128 return findFirstSet(static_cast<typename std::make_unsigned<T>::type>(x));
131 // findLastSet: return the 1-based index of the highest bit set
132 // for x > 0, findLastSet(x) == 1 + floor(log2(x))
134 inline FOLLY_INTRINSIC_CONSTEXPR
135 typename std::enable_if<
136 (std::is_integral<T>::value &&
137 std::is_unsigned<T>::value &&
138 sizeof(T) <= sizeof(unsigned int)),
141 // If X is a power of two X - Y = ((X - 1) ^ Y) + 1. Doing this transformation
142 // allows GCC to remove its own xor that it adds to implement clz using bsr
143 return x ? ((8 * sizeof(unsigned int) - 1) ^ __builtin_clz(x)) + 1 : 0;
147 inline FOLLY_INTRINSIC_CONSTEXPR
148 typename std::enable_if<
149 (std::is_integral<T>::value &&
150 std::is_unsigned<T>::value &&
151 sizeof(T) > sizeof(unsigned int) &&
152 sizeof(T) <= sizeof(unsigned long)),
155 return x ? ((8 * sizeof(unsigned long) - 1) ^ __builtin_clzl(x)) + 1 : 0;
159 inline FOLLY_INTRINSIC_CONSTEXPR
160 typename std::enable_if<
161 (std::is_integral<T>::value &&
162 std::is_unsigned<T>::value &&
163 sizeof(T) > sizeof(unsigned long) &&
164 sizeof(T) <= sizeof(unsigned long long)),
167 return x ? ((8 * sizeof(unsigned long long) - 1) ^ __builtin_clzll(x)) + 1
172 inline FOLLY_INTRINSIC_CONSTEXPR
173 typename std::enable_if<
174 (std::is_integral<T>::value &&
175 std::is_signed<T>::value),
178 return findLastSet(static_cast<typename std::make_unsigned<T>::type>(x));
182 inline FOLLY_INTRINSIC_CONSTEXPR
183 typename std::enable_if<
184 std::is_integral<T>::value && std::is_unsigned<T>::value,
187 return v ? (T(1) << findLastSet(v - 1)) : 1;
191 inline FOLLY_INTRINSIC_CONSTEXPR typename std::
192 enable_if<std::is_integral<T>::value && std::is_unsigned<T>::value, T>::type
194 return v ? (T(1) << (findLastSet(v) - 1)) : 0;
198 inline constexpr typename std::enable_if<
199 std::is_integral<T>::value && std::is_unsigned<T>::value,
202 return (v != 0) && !(v & (v - 1));
209 inline typename std::enable_if<
210 (std::is_integral<T>::value &&
211 std::is_unsigned<T>::value &&
212 sizeof(T) <= sizeof(unsigned int)),
215 return size_t(__builtin_popcount(x));
219 inline typename std::enable_if<
220 (std::is_integral<T>::value &&
221 std::is_unsigned<T>::value &&
222 sizeof(T) > sizeof(unsigned int) &&
223 sizeof(T) <= sizeof(unsigned long long)),
226 return size_t(__builtin_popcountll(x));
230 * Endianness detection and manipulation primitives.
234 template <size_t Size>
235 struct uint_types_by_size;
237 #define FB_GEN(sz, fn) \
238 static inline uint##sz##_t byteswap_gen(uint##sz##_t v) { \
242 struct uint_types_by_size<sz / 8> { \
243 using type = uint##sz##_t; \
248 FB_GEN(64, _byteswap_uint64)
249 FB_GEN(32, _byteswap_ulong)
250 FB_GEN(16, _byteswap_ushort)
252 FB_GEN(64, __builtin_bswap64)
253 FB_GEN(32, __builtin_bswap32)
254 FB_GEN(16, __builtin_bswap16)
262 (std::is_integral<T>::value && !std::is_same<T, bool>::value) ||
263 std::is_floating_point<T>::value,
264 "template type parameter must be non-bool integral or floating point");
266 // we implement this with memcpy because that is defined behavior in C++
267 // we rely on compilers to optimize away the memcpy calls
268 constexpr auto s = sizeof(T);
269 using B = typename uint_types_by_size<s>::type;
271 std::memcpy(&b, &x, s);
273 std::memcpy(&x, &b, s);
277 return kIsLittleEndian ? EndianInt::swap(x) : x;
279 static T little(T x) {
280 return kIsBigEndian ? EndianInt::swap(x) : x;
284 } // namespace detail
286 // big* convert between native and big-endian representations
287 // little* convert between native and little-endian representations
288 // swap* convert between big-endian and little-endian representations
290 // ntohs, htons == big16
291 // ntohl, htonl == big32
292 #define FB_GEN1(fn, t, sz) \
293 static t fn##sz(t x) { return fn<t>(x); } \
295 #define FB_GEN2(t, sz) \
296 FB_GEN1(swap, t, sz) \
297 FB_GEN1(big, t, sz) \
298 FB_GEN1(little, t, sz)
301 FB_GEN2(uint##sz##_t, sz) \
302 FB_GEN2(int##sz##_t, sz)
306 enum class Order : uint8_t {
311 static constexpr Order order = kIsLittleEndian ? Order::LITTLE : Order::BIG;
313 template <class T> static T swap(T x) {
314 return folly::detail::EndianInt<T>::swap(x);
316 template <class T> static T big(T x) {
317 return folly::detail::EndianInt<T>::big(x);
319 template <class T> static T little(T x) {
320 return folly::detail::EndianInt<T>::little(x);
323 #if !defined(__ANDROID__)
336 * Fast bit iteration facility.
340 template <class BaseIter> class BitIterator;
341 template <class BaseIter>
342 BitIterator<BaseIter> findFirstSet(BitIterator<BaseIter>,
343 BitIterator<BaseIter>);
345 * Wrapper around an iterator over an integer type that iterates
346 * over its underlying bits in LSb to MSb order.
348 * BitIterator models the same iterator concepts as the base iterator.
350 template <class BaseIter>
352 : public bititerator_detail::BitIteratorBase<BaseIter>::type {
355 * Return the number of bits in an element of the underlying iterator.
357 static unsigned int bitsPerBlock() {
358 return std::numeric_limits<
359 typename std::make_unsigned<
360 typename std::iterator_traits<BaseIter>::value_type
366 * Construct a BitIterator that points at a given bit offset (default 0)
369 explicit BitIterator(const BaseIter& iter, size_t bitOff=0)
370 : bititerator_detail::BitIteratorBase<BaseIter>::type(iter),
372 assert(bitOffset_ < bitsPerBlock());
375 size_t bitOffset() const {
379 void advanceToNextBlock() {
381 ++this->base_reference();
384 BitIterator& operator=(const BaseIter& other) {
385 this->~BitIterator();
386 new (this) BitIterator(other);
391 friend class boost::iterator_core_access;
392 friend BitIterator findFirstSet<>(BitIterator, BitIterator);
394 typedef bititerator_detail::BitReference<
395 typename std::iterator_traits<BaseIter>::reference,
396 typename std::iterator_traits<BaseIter>::value_type
399 void advanceInBlock(size_t n) {
401 assert(bitOffset_ < bitsPerBlock());
404 BitRef dereference() const {
405 return BitRef(*this->base_reference(), bitOffset_);
408 void advance(ssize_t n) {
409 size_t bpb = bitsPerBlock();
410 ssize_t blocks = n / ssize_t(bpb);
411 bitOffset_ += n % bpb;
412 if (bitOffset_ >= bpb) {
416 this->base_reference() += blocks;
420 if (++bitOffset_ == bitsPerBlock()) {
421 advanceToNextBlock();
426 if (bitOffset_-- == 0) {
427 bitOffset_ = bitsPerBlock() - 1;
428 --this->base_reference();
432 bool equal(const BitIterator& other) const {
433 return (bitOffset_ == other.bitOffset_ &&
434 this->base_reference() == other.base_reference());
437 ssize_t distance_to(const BitIterator& other) const {
439 (other.base_reference() - this->base_reference()) * bitsPerBlock() +
440 other.bitOffset_ - bitOffset_);
447 * Helper function, so you can write
448 * auto bi = makeBitIterator(container.begin());
450 template <class BaseIter>
451 BitIterator<BaseIter> makeBitIterator(const BaseIter& iter) {
452 return BitIterator<BaseIter>(iter);
457 * Find first bit set in a range of bit iterators.
458 * 4.5x faster than the obvious std::find(begin, end, true);
460 template <class BaseIter>
461 BitIterator<BaseIter> findFirstSet(BitIterator<BaseIter> begin,
462 BitIterator<BaseIter> end) {
463 // shortcut to avoid ugly static_cast<>
464 static const typename BaseIter::value_type one = 1;
466 while (begin.base() != end.base()) {
467 typename BaseIter::value_type v = *begin.base();
468 // mask out the bits that don't matter (< begin.bitOffset)
469 v &= ~((one << begin.bitOffset()) - 1);
470 size_t firstSet = findFirstSet(v);
472 --firstSet; // now it's 0-based
473 assert(firstSet >= begin.bitOffset());
474 begin.advanceInBlock(firstSet - begin.bitOffset());
477 begin.advanceToNextBlock();
480 // now begin points to the same block as end
481 if (end.bitOffset() != 0) { // assume end is dereferenceable
482 typename BaseIter::value_type v = *begin.base();
483 // mask out the bits that don't matter (< begin.bitOffset)
484 v &= ~((one << begin.bitOffset()) - 1);
485 // mask out the bits that don't matter (>= end.bitOffset)
486 v &= (one << end.bitOffset()) - 1;
487 size_t firstSet = findFirstSet(v);
489 --firstSet; // now it's 0-based
490 assert(firstSet >= begin.bitOffset());
491 begin.advanceInBlock(firstSet - begin.bitOffset());
500 template <class T, class Enable=void> struct Unaligned;
503 * Representation of an unaligned value of a POD type.
509 typename std::enable_if<std::is_pod<T>::value>::type> {
510 Unaligned() = default; // uninitialized
511 /* implicit */ Unaligned(T v) : value(v) { }
517 * Read an unaligned value of type T and return it.
520 inline T loadUnaligned(const void* p) {
521 static_assert(sizeof(Unaligned<T>) == sizeof(T), "Invalid unaligned size");
522 static_assert(alignof(Unaligned<T>) == 1, "Invalid alignment");
523 if (kHasUnalignedAccess) {
524 return static_cast<const Unaligned<T>*>(p)->value;
527 memcpy(&value, p, sizeof(T));
533 * Write an unaligned value of type T.
536 inline void storeUnaligned(void* p, T value) {
537 static_assert(sizeof(Unaligned<T>) == sizeof(T), "Invalid unaligned size");
538 static_assert(alignof(Unaligned<T>) == 1, "Invalid alignment");
539 if (kHasUnalignedAccess) {
540 // Prior to C++14, the spec says that a placement new like this
541 // is required to check that p is not nullptr, and to do nothing
542 // if p is a nullptr. By assuming it's not a nullptr, we get a
543 // nice loud segfault in optimized builds if p is nullptr, rather
544 // than just silently doing nothing.
545 folly::assume(p != nullptr);
546 new (p) Unaligned<T>(value);
548 memcpy(p, &value, sizeof(T));