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, findFirstSet(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 template <class T> static T swap(T x) {
333 return detail::EndianInt<T>::swap(x);
335 template <class T> static T big(T x) {
336 return detail::EndianInt<T>::big(x);
338 template <class T> static T little(T x) {
339 return detail::EndianInt<T>::little(x);
353 * Fast bit iteration facility.
357 template <class BaseIter> class BitIterator;
358 template <class BaseIter>
359 BitIterator<BaseIter> findFirstSet(BitIterator<BaseIter>,
360 BitIterator<BaseIter>);
362 * Wrapper around an iterator over an integer type that iterates
363 * over its underlying bits in LSb to MSb order.
365 * BitIterator models the same iterator concepts as the base iterator.
367 template <class BaseIter>
369 : public bititerator_detail::BitIteratorBase<BaseIter>::type {
372 * Return the number of bits in an element of the underlying iterator.
374 static size_t bitsPerBlock() {
375 return std::numeric_limits<
376 typename std::make_unsigned<
377 typename std::iterator_traits<BaseIter>::value_type
383 * Construct a BitIterator that points at a given bit offset (default 0)
386 explicit BitIterator(const BaseIter& iter, size_t bitOffset=0)
387 : bititerator_detail::BitIteratorBase<BaseIter>::type(iter),
388 bitOffset_(bitOffset) {
389 assert(bitOffset_ < bitsPerBlock());
392 size_t bitOffset() const {
396 void advanceToNextBlock() {
398 ++this->base_reference();
401 BitIterator& operator=(const BaseIter& other) {
402 this->~BitIterator();
403 new (this) BitIterator(other);
408 friend class boost::iterator_core_access;
409 friend BitIterator findFirstSet<>(BitIterator, BitIterator);
411 typedef bititerator_detail::BitReference<
412 typename std::iterator_traits<BaseIter>::reference,
413 typename std::iterator_traits<BaseIter>::value_type
416 void advanceInBlock(size_t n) {
418 assert(bitOffset_ < bitsPerBlock());
421 BitRef dereference() const {
422 return BitRef(*this->base_reference(), bitOffset_);
425 void advance(ssize_t n) {
426 size_t bpb = bitsPerBlock();
427 ssize_t blocks = n / bpb;
428 bitOffset_ += n % bpb;
429 if (bitOffset_ >= bpb) {
433 this->base_reference() += blocks;
437 if (++bitOffset_ == bitsPerBlock()) {
438 advanceToNextBlock();
443 if (bitOffset_-- == 0) {
444 bitOffset_ = bitsPerBlock() - 1;
445 --this->base_reference();
449 bool equal(const BitIterator& other) const {
450 return (bitOffset_ == other.bitOffset_ &&
451 this->base_reference() == other.base_reference());
454 ssize_t distance_to(const BitIterator& other) const {
456 (other.base_reference() - this->base_reference()) * bitsPerBlock() +
457 (other.bitOffset_ - bitOffset_);
464 * Helper function, so you can write
465 * auto bi = makeBitIterator(container.begin());
467 template <class BaseIter>
468 BitIterator<BaseIter> makeBitIterator(const BaseIter& iter) {
469 return BitIterator<BaseIter>(iter);
474 * Find first bit set in a range of bit iterators.
475 * 4.5x faster than the obvious std::find(begin, end, true);
477 template <class BaseIter>
478 BitIterator<BaseIter> findFirstSet(BitIterator<BaseIter> begin,
479 BitIterator<BaseIter> end) {
480 // shortcut to avoid ugly static_cast<>
481 static const typename BaseIter::value_type one = 1;
483 while (begin.base() != end.base()) {
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 size_t firstSet = findFirstSet(v);
489 --firstSet; // now it's 0-based
490 assert(firstSet >= begin.bitOffset());
491 begin.advanceInBlock(firstSet - begin.bitOffset());
494 begin.advanceToNextBlock();
497 // now begin points to the same block as end
498 if (end.bitOffset() != 0) { // assume end is dereferenceable
499 typename BaseIter::value_type v = *begin.base();
500 // mask out the bits that don't matter (< begin.bitOffset)
501 v &= ~((one << begin.bitOffset()) - 1);
502 // mask out the bits that don't matter (>= end.bitOffset)
503 v &= (one << end.bitOffset()) - 1;
504 size_t firstSet = findFirstSet(v);
506 --firstSet; // now it's 0-based
507 assert(firstSet >= begin.bitOffset());
508 begin.advanceInBlock(firstSet - begin.bitOffset());
519 template <class T, class Enable=void> struct Unaligned;
524 typename std::enable_if<std::is_pod<T>::value>::type> {
526 } __attribute__((packed));
528 } // namespace detail
531 * Read an unaligned value of type T and return it.
534 inline T loadUnaligned(const void* p) {
535 static_assert(alignof(detail::Unaligned<T>) == 1, "Invalid alignment");
536 return static_cast<const detail::Unaligned<T>*>(p)->value;
540 * Write an unaligned value of type T.
543 inline void storeUnaligned(void* p, T value) {
544 static_assert(alignof(detail::Unaligned<T>) == 1, "Invalid alignment");
545 static_cast<detail::Unaligned<T>*>(p)->value = value;
550 #endif /* FOLLY_BITS_H_ */