2 * Copyright 2012 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.
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)
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))
30 * Finds the next power of two >= x.
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
39 * Wrapper around an iterator over an integral type that iterates
40 * over its underlying bits in MSb to LSb order
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
46 * @author Tudor Bosman (tudorb@fb.com)
52 #include "folly/Portability.h"
58 #include "folly/detail/BitIteratorDetail.h"
59 #include "folly/Likely.h"
64 #include <cstring> // for ffs, ffsl, ffsll
68 #include <type_traits>
69 #include <boost/iterator/iterator_adaptor.hpp>
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.
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)),
87 return ::ffs(static_cast<int>(x));
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)),
98 return ::ffsl(static_cast<long>(x));
101 #ifdef FOLLY_HAVE_FFSLL
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)),
111 return ::ffsll(static_cast<long long>(x));
117 typename std::enable_if<
118 (std::is_integral<T>::value &&
119 !std::is_signed<T>::value),
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));
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
139 } // namespace detail
143 // findLastSet: return the 1-based index of the highest bit set
144 // for x > 0, findLastSet(x) == 1 + floor(log2(x))
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)),
153 return x ? 8 * sizeof(unsigned int) - __builtin_clz(x) : 0;
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)),
166 return x ? 8 * sizeof(unsigned long) - __builtin_clzl(x) : 0;
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)),
179 return x ? 8 * sizeof(unsigned long long) - __builtin_clzll(x) : 0;
182 #else /* !__GNUC__ */
185 typename std::enable_if<
186 (std::is_integral<T>::value &&
187 std::is_unsigned<T>::value),
190 return detail:findLastSetPortable(x);
196 typename std::enable_if<
197 (std::is_integral<T>::value &&
198 std::is_signed<T>::value),
201 return findLastSet(static_cast<typename std::make_unsigned<T>::type>(x));
208 typename std::enable_if<
209 std::is_integral<T>::value && std::is_unsigned<T>::value,
211 nextPowTwoPortable(T v) {
212 if (UNLIKELY(v == 0)) {
217 for (uint32_t i = 1; i < sizeof(T) * 8; i <<= 8) {
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));
230 } // namespace detail
236 typename std::enable_if<
237 std::is_integral<T>::value && std::is_unsigned<T>::value,
240 if (UNLIKELY(v == 0)) {
243 return 1ul << findLastSet(v - 1);
250 typename std::enable_if<
251 std::is_integral<T>::value && std::is_unsigned<T>::value,
254 return detail::nextPowTwoPortable(v);
257 #endif /* __GNUC__ */
262 * Endianness detection and manipulation primitives.
267 struct EndianIntBase {
272 #define FB_GEN(t, fn) \
273 template<> inline t EndianIntBase<t>::swap(t x) { return fn(x); }
275 // fn(x) expands to (x) if the second argument is empty, which is exactly
276 // what we want for [u]int8_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)
288 #if __BYTE_ORDER == __LITTLE_ENDIAN
291 struct EndianInt : public detail::EndianIntBase<T> {
293 static T big(T x) { return EndianInt::swap(x); }
294 static T little(T x) { return x; }
297 #elif __BYTE_ORDER == __BIG_ENDIAN
300 struct EndianInt : public detail::EndianIntBase<T> {
302 static T big(T x) { return x; }
303 static T little(T x) { return EndianInt::swap(x); }
307 # error Your machine uses a weird endianness!
308 #endif /* __BYTE_ORDER */
310 } // namespace detail
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
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); } \
321 #define FB_GEN2(t, sz) \
322 FB_GEN1(swap, t, sz) \
323 FB_GEN1(big, t, sz) \
324 FB_GEN1(little, t, sz)
327 FB_GEN2(uint##sz##_t, sz) \
328 FB_GEN2(int##sz##_t, sz)
332 enum class Order : uint8_t {
337 static constexpr Order order =
338 #if __BYTE_ORDER == __LITTLE_ENDIAN
340 #elif __BYTE_ORDER == __BIG_ENDIAN
343 # error Your machine uses a weird endianness!
344 #endif /* __BYTE_ORDER */
346 template <class T> static T swap(T x) {
347 return detail::EndianInt<T>::swap(x);
349 template <class T> static T big(T x) {
350 return detail::EndianInt<T>::big(x);
352 template <class T> static T little(T x) {
353 return detail::EndianInt<T>::little(x);
367 * Fast bit iteration facility.
371 template <class BaseIter> class BitIterator;
372 template <class BaseIter>
373 BitIterator<BaseIter> findFirstSet(BitIterator<BaseIter>,
374 BitIterator<BaseIter>);
376 * Wrapper around an iterator over an integer type that iterates
377 * over its underlying bits in LSb to MSb order.
379 * BitIterator models the same iterator concepts as the base iterator.
381 template <class BaseIter>
383 : public bititerator_detail::BitIteratorBase<BaseIter>::type {
386 * Return the number of bits in an element of the underlying iterator.
388 static size_t bitsPerBlock() {
389 return std::numeric_limits<
390 typename std::make_unsigned<
391 typename std::iterator_traits<BaseIter>::value_type
397 * Construct a BitIterator that points at a given bit offset (default 0)
400 explicit BitIterator(const BaseIter& iter, size_t bitOffset=0)
401 : bititerator_detail::BitIteratorBase<BaseIter>::type(iter),
402 bitOffset_(bitOffset) {
403 assert(bitOffset_ < bitsPerBlock());
406 size_t bitOffset() const {
410 void advanceToNextBlock() {
412 ++this->base_reference();
415 BitIterator& operator=(const BaseIter& other) {
416 this->~BitIterator();
417 new (this) BitIterator(other);
422 friend class boost::iterator_core_access;
423 friend BitIterator findFirstSet<>(BitIterator, BitIterator);
425 typedef bititerator_detail::BitReference<
426 typename std::iterator_traits<BaseIter>::reference,
427 typename std::iterator_traits<BaseIter>::value_type
430 void advanceInBlock(size_t n) {
432 assert(bitOffset_ < bitsPerBlock());
435 BitRef dereference() const {
436 return BitRef(*this->base_reference(), bitOffset_);
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) {
447 this->base_reference() += blocks;
451 if (++bitOffset_ == bitsPerBlock()) {
452 advanceToNextBlock();
457 if (bitOffset_-- == 0) {
458 bitOffset_ = bitsPerBlock() - 1;
459 --this->base_reference();
463 bool equal(const BitIterator& other) const {
464 return (bitOffset_ == other.bitOffset_ &&
465 this->base_reference() == other.base_reference());
468 ssize_t distance_to(const BitIterator& other) const {
470 (other.base_reference() - this->base_reference()) * bitsPerBlock() +
471 (other.bitOffset_ - bitOffset_);
478 * Helper function, so you can write
479 * auto bi = makeBitIterator(container.begin());
481 template <class BaseIter>
482 BitIterator<BaseIter> makeBitIterator(const BaseIter& iter) {
483 return BitIterator<BaseIter>(iter);
488 * Find first bit set in a range of bit iterators.
489 * 4.5x faster than the obvious std::find(begin, end, true);
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;
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);
503 --firstSet; // now it's 0-based
504 assert(firstSet >= begin.bitOffset());
505 begin.advanceInBlock(firstSet - begin.bitOffset());
508 begin.advanceToNextBlock();
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);
520 --firstSet; // now it's 0-based
521 assert(firstSet >= begin.bitOffset());
522 begin.advanceInBlock(firstSet - begin.bitOffset());
531 template <class T, class Enable=void> struct Unaligned;
534 * Representation of an unaligned value of a POD type.
539 typename std::enable_if<std::is_pod<T>::value>::type> {
540 Unaligned() { } // uninitialized
541 /* implicit */ Unaligned(T v) : value(v) { }
543 } __attribute__((packed));
546 * Read an unaligned value of type T and return it.
549 inline T loadUnaligned(const void* p) {
550 static_assert(sizeof(Unaligned<T>) == sizeof(T), "Invalid unaligned size");
551 static_assert(alignof(Unaligned<T>) == 1, "Invalid alignment");
552 return static_cast<const Unaligned<T>*>(p)->value;
556 * Write an unaligned value of type T.
559 inline void storeUnaligned(void* p, T value) {
560 static_assert(sizeof(Unaligned<T>) == sizeof(T), "Invalid unaligned size");
561 static_assert(alignof(Unaligned<T>) == 1, "Invalid alignment");
562 new (p) Unaligned<T>(value);
567 #endif /* FOLLY_BITS_H_ */