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 #if !defined(__clang__) && !(defined(_MSC_VER) && (_MSC_VER < 1900))
58 #define FOLLY_INTRINSIC_CONSTEXPR constexpr
60 // GCC and MSVC 2015+ are the only compilers with
61 // intrinsics constexpr.
62 #define FOLLY_INTRINSIC_CONSTEXPR const
65 #include <folly/Portability.h>
66 #include <folly/portability/Builtins.h>
68 #include <folly/Assume.h>
69 #include <folly/detail/BitsDetail.h>
70 #include <folly/detail/BitIteratorDetail.h>
71 #include <folly/Likely.h>
78 #include <type_traits>
79 #include <boost/iterator/iterator_adaptor.hpp>
84 // Generate overloads for findFirstSet as wrappers around
85 // appropriate ffs, ffsl, ffsll gcc builtins
87 inline FOLLY_INTRINSIC_CONSTEXPR
88 typename std::enable_if<
89 (std::is_integral<T>::value &&
90 std::is_unsigned<T>::value &&
91 sizeof(T) <= sizeof(unsigned int)),
94 return static_cast<unsigned int>(__builtin_ffs(static_cast<int>(x)));
98 inline FOLLY_INTRINSIC_CONSTEXPR
99 typename std::enable_if<
100 (std::is_integral<T>::value &&
101 std::is_unsigned<T>::value &&
102 sizeof(T) > sizeof(unsigned int) &&
103 sizeof(T) <= sizeof(unsigned long)),
106 return static_cast<unsigned int>(__builtin_ffsl(static_cast<long>(x)));
110 inline FOLLY_INTRINSIC_CONSTEXPR
111 typename std::enable_if<
112 (std::is_integral<T>::value &&
113 std::is_unsigned<T>::value &&
114 sizeof(T) > sizeof(unsigned long) &&
115 sizeof(T) <= sizeof(unsigned long long)),
118 return static_cast<unsigned int>(__builtin_ffsll(static_cast<long long>(x)));
122 inline FOLLY_INTRINSIC_CONSTEXPR
123 typename std::enable_if<
124 (std::is_integral<T>::value && std::is_signed<T>::value),
127 // Note that conversion from a signed type to the corresponding unsigned
128 // type is technically implementation-defined, but will likely work
129 // on any impementation that uses two's complement.
130 return findFirstSet(static_cast<typename std::make_unsigned<T>::type>(x));
133 // findLastSet: return the 1-based index of the highest bit set
134 // for x > 0, findLastSet(x) == 1 + floor(log2(x))
136 inline FOLLY_INTRINSIC_CONSTEXPR
137 typename std::enable_if<
138 (std::is_integral<T>::value &&
139 std::is_unsigned<T>::value &&
140 sizeof(T) <= sizeof(unsigned int)),
143 // If X is a power of two X - Y = ((X - 1) ^ Y) + 1. Doing this transformation
144 // allows GCC to remove its own xor that it adds to implement clz using bsr
145 return x ? ((8 * sizeof(unsigned int) - 1) ^ __builtin_clz(x)) + 1 : 0;
149 inline FOLLY_INTRINSIC_CONSTEXPR
150 typename std::enable_if<
151 (std::is_integral<T>::value &&
152 std::is_unsigned<T>::value &&
153 sizeof(T) > sizeof(unsigned int) &&
154 sizeof(T) <= sizeof(unsigned long)),
157 return x ? ((8 * sizeof(unsigned long) - 1) ^ __builtin_clzl(x)) + 1 : 0;
161 inline FOLLY_INTRINSIC_CONSTEXPR
162 typename std::enable_if<
163 (std::is_integral<T>::value &&
164 std::is_unsigned<T>::value &&
165 sizeof(T) > sizeof(unsigned long) &&
166 sizeof(T) <= sizeof(unsigned long long)),
169 return x ? ((8 * sizeof(unsigned long long) - 1) ^ __builtin_clzll(x)) + 1
174 inline FOLLY_INTRINSIC_CONSTEXPR
175 typename std::enable_if<
176 (std::is_integral<T>::value &&
177 std::is_signed<T>::value),
180 return findLastSet(static_cast<typename std::make_unsigned<T>::type>(x));
184 inline FOLLY_INTRINSIC_CONSTEXPR
185 typename std::enable_if<
186 std::is_integral<T>::value && std::is_unsigned<T>::value,
189 return v ? (T(1) << findLastSet(v - 1)) : 1;
193 inline FOLLY_INTRINSIC_CONSTEXPR typename std::
194 enable_if<std::is_integral<T>::value && std::is_unsigned<T>::value, T>::type
196 return v ? (T(1) << (findLastSet(v) - 1)) : 0;
200 inline constexpr typename std::enable_if<
201 std::is_integral<T>::value && std::is_unsigned<T>::value,
204 return (v != 0) && !(v & (v - 1));
211 inline typename std::enable_if<
212 (std::is_integral<T>::value &&
213 std::is_unsigned<T>::value &&
214 sizeof(T) <= sizeof(unsigned int)),
217 return size_t(detail::popcount(x));
221 inline typename std::enable_if<
222 (std::is_integral<T>::value &&
223 std::is_unsigned<T>::value &&
224 sizeof(T) > sizeof(unsigned int) &&
225 sizeof(T) <= sizeof(unsigned long long)),
228 return size_t(detail::popcountll(x));
232 * Endianness detection and manipulation primitives.
236 template <size_t Size>
237 struct uint_types_by_size;
239 #define FB_GEN(sz, fn) \
240 static inline uint##sz##_t byteswap_gen(uint##sz##_t v) { \
244 struct uint_types_by_size<sz / 8> { \
245 using type = uint##sz##_t; \
250 FB_GEN(64, _byteswap_uint64)
251 FB_GEN(32, _byteswap_ulong)
252 FB_GEN(16, _byteswap_ushort)
254 FB_GEN(64, __builtin_bswap64)
255 FB_GEN(32, __builtin_bswap32)
256 FB_GEN(16, __builtin_bswap16)
264 (std::is_integral<T>::value && !std::is_same<T, bool>::value) ||
265 std::is_floating_point<T>::value,
266 "template type parameter must be non-bool integral or floating point");
268 // we implement this with memcpy because that is defined behavior in C++
269 // we rely on compilers to optimize away the memcpy calls
270 constexpr auto s = sizeof(T);
271 using B = typename uint_types_by_size<s>::type;
273 std::memcpy(&b, &x, s);
275 std::memcpy(&x, &b, s);
279 return kIsLittleEndian ? EndianInt::swap(x) : x;
281 static T little(T x) {
282 return kIsBigEndian ? EndianInt::swap(x) : x;
286 } // namespace detail
288 // big* convert between native and big-endian representations
289 // little* convert between native and little-endian representations
290 // swap* convert between big-endian and little-endian representations
292 // ntohs, htons == big16
293 // ntohl, htonl == big32
294 #define FB_GEN1(fn, t, sz) \
295 static t fn##sz(t x) { return fn<t>(x); } \
297 #define FB_GEN2(t, sz) \
298 FB_GEN1(swap, t, sz) \
299 FB_GEN1(big, t, sz) \
300 FB_GEN1(little, t, sz)
303 FB_GEN2(uint##sz##_t, sz) \
304 FB_GEN2(int##sz##_t, sz)
308 enum class Order : uint8_t {
313 static constexpr Order order = kIsLittleEndian ? Order::LITTLE : Order::BIG;
315 template <class T> static T swap(T x) {
316 return folly::detail::EndianInt<T>::swap(x);
318 template <class T> static T big(T x) {
319 return folly::detail::EndianInt<T>::big(x);
321 template <class T> static T little(T x) {
322 return folly::detail::EndianInt<T>::little(x);
325 #if !defined(__ANDROID__)
338 * Fast bit iteration facility.
342 template <class BaseIter> class BitIterator;
343 template <class BaseIter>
344 BitIterator<BaseIter> findFirstSet(BitIterator<BaseIter>,
345 BitIterator<BaseIter>);
347 * Wrapper around an iterator over an integer type that iterates
348 * over its underlying bits in LSb to MSb order.
350 * BitIterator models the same iterator concepts as the base iterator.
352 template <class BaseIter>
354 : public bititerator_detail::BitIteratorBase<BaseIter>::type {
357 * Return the number of bits in an element of the underlying iterator.
359 static unsigned int bitsPerBlock() {
360 return std::numeric_limits<
361 typename std::make_unsigned<
362 typename std::iterator_traits<BaseIter>::value_type
368 * Construct a BitIterator that points at a given bit offset (default 0)
371 explicit BitIterator(const BaseIter& iter, size_t bitOff=0)
372 : bititerator_detail::BitIteratorBase<BaseIter>::type(iter),
374 assert(bitOffset_ < bitsPerBlock());
377 size_t bitOffset() const {
381 void advanceToNextBlock() {
383 ++this->base_reference();
386 BitIterator& operator=(const BaseIter& other) {
387 this->~BitIterator();
388 new (this) BitIterator(other);
393 friend class boost::iterator_core_access;
394 friend BitIterator findFirstSet<>(BitIterator, BitIterator);
396 typedef bititerator_detail::BitReference<
397 typename std::iterator_traits<BaseIter>::reference,
398 typename std::iterator_traits<BaseIter>::value_type
401 void advanceInBlock(size_t n) {
403 assert(bitOffset_ < bitsPerBlock());
406 BitRef dereference() const {
407 return BitRef(*this->base_reference(), bitOffset_);
410 void advance(ssize_t n) {
411 size_t bpb = bitsPerBlock();
412 ssize_t blocks = n / ssize_t(bpb);
413 bitOffset_ += n % bpb;
414 if (bitOffset_ >= bpb) {
418 this->base_reference() += blocks;
422 if (++bitOffset_ == bitsPerBlock()) {
423 advanceToNextBlock();
428 if (bitOffset_-- == 0) {
429 bitOffset_ = bitsPerBlock() - 1;
430 --this->base_reference();
434 bool equal(const BitIterator& other) const {
435 return (bitOffset_ == other.bitOffset_ &&
436 this->base_reference() == other.base_reference());
439 ssize_t distance_to(const BitIterator& other) const {
441 (other.base_reference() - this->base_reference()) * bitsPerBlock() +
442 other.bitOffset_ - bitOffset_);
449 * Helper function, so you can write
450 * auto bi = makeBitIterator(container.begin());
452 template <class BaseIter>
453 BitIterator<BaseIter> makeBitIterator(const BaseIter& iter) {
454 return BitIterator<BaseIter>(iter);
459 * Find first bit set in a range of bit iterators.
460 * 4.5x faster than the obvious std::find(begin, end, true);
462 template <class BaseIter>
463 BitIterator<BaseIter> findFirstSet(BitIterator<BaseIter> begin,
464 BitIterator<BaseIter> end) {
465 // shortcut to avoid ugly static_cast<>
466 static const typename BaseIter::value_type one = 1;
468 while (begin.base() != end.base()) {
469 typename BaseIter::value_type v = *begin.base();
470 // mask out the bits that don't matter (< begin.bitOffset)
471 v &= ~((one << begin.bitOffset()) - 1);
472 size_t firstSet = findFirstSet(v);
474 --firstSet; // now it's 0-based
475 assert(firstSet >= begin.bitOffset());
476 begin.advanceInBlock(firstSet - begin.bitOffset());
479 begin.advanceToNextBlock();
482 // now begin points to the same block as end
483 if (end.bitOffset() != 0) { // assume end is dereferenceable
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 // mask out the bits that don't matter (>= end.bitOffset)
488 v &= (one << end.bitOffset()) - 1;
489 size_t firstSet = findFirstSet(v);
491 --firstSet; // now it's 0-based
492 assert(firstSet >= begin.bitOffset());
493 begin.advanceInBlock(firstSet - begin.bitOffset());
502 template <class T, class Enable=void> struct Unaligned;
505 * Representation of an unaligned value of a POD type.
511 typename std::enable_if<std::is_pod<T>::value>::type> {
512 Unaligned() = default; // uninitialized
513 /* implicit */ Unaligned(T v) : value(v) { }
519 * Read an unaligned value of type T and return it.
522 inline T loadUnaligned(const void* p) {
523 static_assert(sizeof(Unaligned<T>) == sizeof(T), "Invalid unaligned size");
524 static_assert(alignof(Unaligned<T>) == 1, "Invalid alignment");
525 if (kHasUnalignedAccess) {
526 return static_cast<const Unaligned<T>*>(p)->value;
529 memcpy(&value, p, sizeof(T));
535 * Write an unaligned value of type T.
538 inline void storeUnaligned(void* p, T value) {
539 static_assert(sizeof(Unaligned<T>) == sizeof(T), "Invalid unaligned size");
540 static_assert(alignof(Unaligned<T>) == 1, "Invalid alignment");
541 if (kHasUnalignedAccess) {
542 // Prior to C++14, the spec says that a placement new like this
543 // is required to check that p is not nullptr, and to do nothing
544 // if p is a nullptr. By assuming it's not a nullptr, we get a
545 // nice loud segfault in optimized builds if p is nullptr, rather
546 // than just silently doing nothing.
547 folly::assume(p != nullptr);
548 new (p) Unaligned<T>(value);
550 memcpy(p, &value, sizeof(T));