Commit 6aa05bb9 authored by Philip Pronin's avatar Philip Pronin Committed by Jordan DeLong

constexpr-ize folly/Bits.h

Summary:
Want to be able to call them from other constexpr functions
and from within static_assert()s.

Test Plan: compiled and ran folly tests

Reviewed By: tudorb@fb.com

FB internal diff: D627413
parent 91d0ef0e
......@@ -17,24 +17,24 @@
/**
* Various low-level, bit-manipulation routines.
*
* findFirstSet(x)
* findFirstSet(x) [constexpr]
* find first (least significant) bit set in a value of an integral type,
* 1-based (like ffs()). 0 = no bits are set (x == 0)
*
* findLastSet(x)
* findLastSet(x) [constexpr]
* find last (most significant) bit set in a value of an integral type,
* 1-based. 0 = no bits are set (x == 0)
* for x != 0, findLastSet(x) == 1 + floor(log2(x))
*
* popcount(x)
* return the number of 1 bits in x
*
* nextPowTwo(x)
* nextPowTwo(x) [constexpr]
* Finds the next power of two >= x.
*
* isPowTwo(x)
* isPowTwo(x) [constexpr]
* return true iff x is a power of two
*
* popcount(x)
* return the number of 1 bits in x
*
* Endian
* convert between native, big, and little endian representation
* Endian::big(x) big <-> native
......@@ -84,6 +84,7 @@ namespace folly {
// Generate overloads for findFirstSet as wrappers around
// appropriate ffs, ffsl, ffsll gcc builtins
template <class T>
inline constexpr
typename std::enable_if<
(std::is_integral<T>::value &&
std::is_unsigned<T>::value &&
......@@ -94,6 +95,7 @@ typename std::enable_if<
}
template <class T>
inline constexpr
typename std::enable_if<
(std::is_integral<T>::value &&
std::is_unsigned<T>::value &&
......@@ -105,6 +107,7 @@ typename std::enable_if<
}
template <class T>
inline constexpr
typename std::enable_if<
(std::is_integral<T>::value &&
std::is_unsigned<T>::value &&
......@@ -116,6 +119,7 @@ typename std::enable_if<
}
template <class T>
inline constexpr
typename std::enable_if<
(std::is_integral<T>::value && std::is_signed<T>::value),
unsigned int>::type
......@@ -129,6 +133,7 @@ typename std::enable_if<
// findLastSet: return the 1-based index of the highest bit set
// for x > 0, findLastSet(x) == 1 + floor(log2(x))
template <class T>
inline constexpr
typename std::enable_if<
(std::is_integral<T>::value &&
std::is_unsigned<T>::value &&
......@@ -139,6 +144,7 @@ typename std::enable_if<
}
template <class T>
inline constexpr
typename std::enable_if<
(std::is_integral<T>::value &&
std::is_unsigned<T>::value &&
......@@ -150,6 +156,7 @@ typename std::enable_if<
}
template <class T>
inline constexpr
typename std::enable_if<
(std::is_integral<T>::value &&
std::is_unsigned<T>::value &&
......@@ -161,6 +168,7 @@ typename std::enable_if<
}
template <class T>
inline constexpr
typename std::enable_if<
(std::is_integral<T>::value &&
std::is_signed<T>::value),
......@@ -170,24 +178,21 @@ typename std::enable_if<
}
template <class T>
inline
inline constexpr
typename std::enable_if<
std::is_integral<T>::value && std::is_unsigned<T>::value,
T>::type
nextPowTwo(T v) {
if (UNLIKELY(v == 0)) {
return 1;
}
return 1ul << findLastSet(v - 1);
return v ? (1ul << findLastSet(v - 1)) : 1;
}
template <class T>
inline
inline constexpr
typename std::enable_if<
std::is_integral<T>::value && std::is_unsigned<T>::value,
bool>::type
isPowTwo(T v) {
return ((v != 0) && !(v & (v-1))); // yes, this is endian-agnostic
return (v != 0) && !(v & (v - 1));
}
/**
......
......@@ -23,6 +23,12 @@
using namespace folly;
// Test constexpr-ness.
static_assert(findFirstSet(2u) == 2, "findFirstSet");
static_assert(findLastSet(2u) == 2, "findLastSet");
static_assert(nextPowTwo(2u) == 2, "nextPowTwo");
static_assert(isPowTwo(2u), "isPowTwo");
namespace {
template <class INT>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment