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.
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)
58 #include "folly/Portability.h"
68 #include "folly/detail/BitsDetail.h"
69 #include "folly/detail/BitIteratorDetail.h"
70 #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
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 __builtin_ffs(x);
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 __builtin_ffsl(x);
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 __builtin_ffsll(x);
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))
137 typename std::enable_if<
138 (std::is_integral<T>::value &&
139 std::is_unsigned<T>::value &&
140 sizeof(T) <= sizeof(unsigned int)),
143 return x ? 8 * sizeof(unsigned int) - __builtin_clz(x) : 0;
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) - __builtin_clzl(x) : 0;
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) - __builtin_clzll(x) : 0;
172 typename std::enable_if<
173 (std::is_integral<T>::value &&
174 std::is_signed<T>::value),
177 return findLastSet(static_cast<typename std::make_unsigned<T>::type>(x));
182 typename std::enable_if<
183 std::is_integral<T>::value && std::is_unsigned<T>::value,
186 return v ? (1ul << findLastSet(v - 1)) : 1;
191 typename std::enable_if<
192 std::is_integral<T>::value && std::is_unsigned<T>::value,
195 return (v != 0) && !(v & (v - 1));
202 inline typename std::enable_if<
203 (std::is_integral<T>::value &&
204 std::is_unsigned<T>::value &&
205 sizeof(T) <= sizeof(unsigned int)),
208 return detail::popcount(x);
212 inline typename std::enable_if<
213 (std::is_integral<T>::value &&
214 std::is_unsigned<T>::value &&
215 sizeof(T) > sizeof(unsigned int) &&
216 sizeof(T) <= sizeof(unsigned long long)),
219 return detail::popcountll(x);
223 * Endianness detection and manipulation primitives.
228 struct EndianIntBase {
233 #define FB_GEN(t, fn) \
234 template<> inline t EndianIntBase<t>::swap(t x) { return fn(x); }
236 // fn(x) expands to (x) if the second argument is empty, which is exactly
237 // what we want for [u]int8_t
240 FB_GEN( int64_t, bswap_64)
241 FB_GEN(uint64_t, bswap_64)
242 FB_GEN( int32_t, bswap_32)
243 FB_GEN(uint32_t, bswap_32)
244 FB_GEN( int16_t, bswap_16)
245 FB_GEN(uint16_t, bswap_16)
249 #if __BYTE_ORDER == __LITTLE_ENDIAN
252 struct EndianInt : public detail::EndianIntBase<T> {
254 static T big(T x) { return EndianInt::swap(x); }
255 static T little(T x) { return x; }
258 #elif __BYTE_ORDER == __BIG_ENDIAN
261 struct EndianInt : public detail::EndianIntBase<T> {
263 static T big(T x) { return x; }
264 static T little(T x) { return EndianInt::swap(x); }
268 # error Your machine uses a weird endianness!
269 #endif /* __BYTE_ORDER */
271 } // namespace detail
273 // big* convert between native and big-endian representations
274 // little* convert between native and little-endian representations
275 // swap* convert between big-endian and little-endian representations
277 // ntohs, htons == big16
278 // ntohl, htonl == big32
279 #define FB_GEN1(fn, t, sz) \
280 static t fn##sz(t x) { return fn<t>(x); } \
282 #define FB_GEN2(t, sz) \
283 FB_GEN1(swap, t, sz) \
284 FB_GEN1(big, t, sz) \
285 FB_GEN1(little, t, sz)
288 FB_GEN2(uint##sz##_t, sz) \
289 FB_GEN2(int##sz##_t, sz)
293 enum class Order : uint8_t {
298 static constexpr Order order =
299 #if __BYTE_ORDER == __LITTLE_ENDIAN
301 #elif __BYTE_ORDER == __BIG_ENDIAN
304 # error Your machine uses a weird endianness!
305 #endif /* __BYTE_ORDER */
307 template <class T> static T swap(T x) {
308 return detail::EndianInt<T>::swap(x);
310 template <class T> static T big(T x) {
311 return detail::EndianInt<T>::big(x);
313 template <class T> static T little(T x) {
314 return detail::EndianInt<T>::little(x);
328 * Fast bit iteration facility.
332 template <class BaseIter> class BitIterator;
333 template <class BaseIter>
334 BitIterator<BaseIter> findFirstSet(BitIterator<BaseIter>,
335 BitIterator<BaseIter>);
337 * Wrapper around an iterator over an integer type that iterates
338 * over its underlying bits in LSb to MSb order.
340 * BitIterator models the same iterator concepts as the base iterator.
342 template <class BaseIter>
344 : public bititerator_detail::BitIteratorBase<BaseIter>::type {
347 * Return the number of bits in an element of the underlying iterator.
349 static size_t bitsPerBlock() {
350 return std::numeric_limits<
351 typename std::make_unsigned<
352 typename std::iterator_traits<BaseIter>::value_type
358 * Construct a BitIterator that points at a given bit offset (default 0)
361 explicit BitIterator(const BaseIter& iter, size_t bitOffset=0)
362 : bititerator_detail::BitIteratorBase<BaseIter>::type(iter),
363 bitOffset_(bitOffset) {
364 assert(bitOffset_ < bitsPerBlock());
367 size_t bitOffset() const {
371 void advanceToNextBlock() {
373 ++this->base_reference();
376 BitIterator& operator=(const BaseIter& other) {
377 this->~BitIterator();
378 new (this) BitIterator(other);
383 friend class boost::iterator_core_access;
384 friend BitIterator findFirstSet<>(BitIterator, BitIterator);
386 typedef bititerator_detail::BitReference<
387 typename std::iterator_traits<BaseIter>::reference,
388 typename std::iterator_traits<BaseIter>::value_type
391 void advanceInBlock(size_t n) {
393 assert(bitOffset_ < bitsPerBlock());
396 BitRef dereference() const {
397 return BitRef(*this->base_reference(), bitOffset_);
400 void advance(ssize_t n) {
401 size_t bpb = bitsPerBlock();
402 ssize_t blocks = n / bpb;
403 bitOffset_ += n % bpb;
404 if (bitOffset_ >= bpb) {
408 this->base_reference() += blocks;
412 if (++bitOffset_ == bitsPerBlock()) {
413 advanceToNextBlock();
418 if (bitOffset_-- == 0) {
419 bitOffset_ = bitsPerBlock() - 1;
420 --this->base_reference();
424 bool equal(const BitIterator& other) const {
425 return (bitOffset_ == other.bitOffset_ &&
426 this->base_reference() == other.base_reference());
429 ssize_t distance_to(const BitIterator& other) const {
431 (other.base_reference() - this->base_reference()) * bitsPerBlock() +
432 (other.bitOffset_ - bitOffset_);
439 * Helper function, so you can write
440 * auto bi = makeBitIterator(container.begin());
442 template <class BaseIter>
443 BitIterator<BaseIter> makeBitIterator(const BaseIter& iter) {
444 return BitIterator<BaseIter>(iter);
449 * Find first bit set in a range of bit iterators.
450 * 4.5x faster than the obvious std::find(begin, end, true);
452 template <class BaseIter>
453 BitIterator<BaseIter> findFirstSet(BitIterator<BaseIter> begin,
454 BitIterator<BaseIter> end) {
455 // shortcut to avoid ugly static_cast<>
456 static const typename BaseIter::value_type one = 1;
458 while (begin.base() != end.base()) {
459 typename BaseIter::value_type v = *begin.base();
460 // mask out the bits that don't matter (< begin.bitOffset)
461 v &= ~((one << begin.bitOffset()) - 1);
462 size_t firstSet = findFirstSet(v);
464 --firstSet; // now it's 0-based
465 assert(firstSet >= begin.bitOffset());
466 begin.advanceInBlock(firstSet - begin.bitOffset());
469 begin.advanceToNextBlock();
472 // now begin points to the same block as end
473 if (end.bitOffset() != 0) { // assume end is dereferenceable
474 typename BaseIter::value_type v = *begin.base();
475 // mask out the bits that don't matter (< begin.bitOffset)
476 v &= ~((one << begin.bitOffset()) - 1);
477 // mask out the bits that don't matter (>= end.bitOffset)
478 v &= (one << end.bitOffset()) - 1;
479 size_t firstSet = findFirstSet(v);
481 --firstSet; // now it's 0-based
482 assert(firstSet >= begin.bitOffset());
483 begin.advanceInBlock(firstSet - begin.bitOffset());
492 template <class T, class Enable=void> struct Unaligned;
495 * Representation of an unaligned value of a POD type.
500 typename std::enable_if<std::is_pod<T>::value>::type> {
501 Unaligned() { } // uninitialized
502 /* implicit */ Unaligned(T v) : value(v) { }
504 } __attribute__((packed));
507 * Read an unaligned value of type T and return it.
510 inline T loadUnaligned(const void* p) {
511 static_assert(sizeof(Unaligned<T>) == sizeof(T), "Invalid unaligned size");
512 static_assert(alignof(Unaligned<T>) == 1, "Invalid alignment");
513 return static_cast<const Unaligned<T>*>(p)->value;
517 * Write an unaligned value of type T.
520 inline void storeUnaligned(void* p, T value) {
521 static_assert(sizeof(Unaligned<T>) == sizeof(T), "Invalid unaligned size");
522 static_assert(alignof(Unaligned<T>) == 1, "Invalid alignment");
523 new (p) Unaligned<T>(value);
528 #endif /* FOLLY_BITS_H_ */