// Like sizeof, but count how many bits a type takes. Pass type explicitly. template <typename T>
constexpr size_t BitSizeOf() {
static_assert(std::is_integral_v<T>, "T must be integral"); using unsigned_type = std::make_unsigned_t<T>;
static_assert(sizeof(T) == sizeof(unsigned_type), "Unexpected type size mismatch!");
static_assert(std::numeric_limits<unsigned_type>::radix == 2, "Unexpected radix!"); return std::numeric_limits<unsigned_type>::digits;
}
// Like sizeof, but count how many bits a type takes. Infers type from parameter. template <typename T>
constexpr size_t BitSizeOf(T /*x*/) { return BitSizeOf<T>();
}
// Similar to CLZ except that on zero input it returns bitwidth and supports signed integers. template<typename T>
constexpr int JAVASTYLE_CLZ(T x) {
static_assert(std::is_integral_v<T>, "T must be integral"); using unsigned_type = std::make_unsigned_t<T>; return (x == 0) ? BitSizeOf<T>() : CLZ(static_cast<unsigned_type>(x));
}
template<typename T>
constexpr int CTZ(T x) {
static_assert(std::is_integral_v<T>, "T must be integral"); // It is not unreasonable to ask for trailing zeros in a negative number. As such, do not check // that T is an unsigned type.
static_assert(sizeof(T) == sizeof(uint64_t) || sizeof(T) <= sizeof(uint32_t), "Unsupported sizeof(T)");
DCHECK_NE(x, static_cast<T>(0)); return (sizeof(T) == sizeof(uint64_t)) ? __builtin_ctzll(x) : __builtin_ctz(x);
}
// Similar to CTZ except that on zero input it returns bitwidth and supports signed integers. template<typename T>
constexpr int JAVASTYLE_CTZ(T x) {
static_assert(std::is_integral_v<T>, "T must be integral"); using unsigned_type = std::make_unsigned_t<T>; return (x == 0) ? BitSizeOf<T>() : CTZ(static_cast<unsigned_type>(x));
}
// Return the number of 1-bits in `x`. template<typename T>
constexpr int POPCOUNT(T x) { return (sizeof(T) == sizeof(uint32_t)) ? __builtin_popcount(x) : __builtin_popcountll(x);
}
// Find the bit position of the most significant bit (0-based), or -1 if there were no bits set. template <typename T>
constexpr ssize_t MostSignificantBit(T value) {
static_assert(std::is_integral_v<T>, "T must be integral");
static_assert(std::is_unsigned_v<T>, "T must be unsigned");
static_assert(std::numeric_limits<T>::radix == 2, "Unexpected radix!"); return (value == 0) ? -1 : std::numeric_limits<T>::digits - 1 - CLZ(value);
}
// Find the bit position of the least significant bit (0-based), or -1 if there were no bits set. template <typename T>
constexpr ssize_t LeastSignificantBit(T value) {
static_assert(std::is_integral_v<T>, "T must be integral");
static_assert(std::is_unsigned_v<T>, "T must be unsigned"); return (value == 0) ? -1 : CTZ(value);
}
// How many bits (minimally) does it take to store the constant 'value'? i.e. 1 for 1, 3 for 5, etc. template <typename T>
constexpr size_t MinimumBitsToStore(T value) { returnstatic_cast<size_t>(MostSignificantBit(value) + 1);
}
template <typename T>
constexpr T RoundUpToPowerOfTwo(T x) {
static_assert(std::is_integral_v<T>, "T must be integral");
static_assert(std::is_unsigned_v<T>, "T must be unsigned"); // NOTE: Undefined if x > (1 << (std::numeric_limits<T>::digits - 1)). return (x < 2u) ? x : static_cast<T>(1u) << (std::numeric_limits<T>::digits - CLZ(x - 1u));
}
// Return highest possible N - a power of two - such that val >= N. template <typename T>
constexpr T TruncToPowerOfTwo(T val) {
static_assert(std::is_integral_v<T>, "T must be integral");
static_assert(std::is_unsigned_v<T>, "T must be unsigned"); return (val != 0) ? static_cast<T>(1u) << (BitSizeOf<T>() - CLZ(val) - 1u) : 0;
}
template<typename T>
constexpr bool IsPowerOfTwo(T x) {
static_assert(std::is_integral_v<T>, "T must be integral"); // TODO: assert unsigned. There is currently many uses with signed values.
DCHECK_NE(x, static_cast<T>(0)); return (x & (x - 1)) == 0;
}
template<typename T>
constexpr int WhichPowerOf2(T x) {
static_assert(std::is_integral_v<T>, "T must be integral"); // TODO: assert unsigned. There is currently many uses with signed values.
DCHECK((x != 0) && IsPowerOfTwo(x)); return CTZ(x);
}
// For rounding integers. // Note: Omit the `n` from T type deduction, deduce only from the `x` argument. template<typename T>
constexpr T RoundDown(T x, typename Identity<T>::type n) WARN_UNUSED;
template <size_t kBits, typename T>
constexpr bool IsInt(T value) {
static_assert(kBits > 0, "kBits cannot be zero.");
static_assert(kBits <= BitSizeOf<T>(), "kBits must be <= max.");
static_assert(std::is_signed_v<T>, "Needs a signed type."); // Corner case for "use all bits." Can't use the limits, as they would overflow, but it is // trivially true. return (kBits == BitSizeOf<T>()) ? true :
(-GetIntLimit<T>(kBits) <= value) && (value < GetIntLimit<T>(kBits));
}
template <size_t kBits, typename T>
constexpr bool IsUint(T value) {
static_assert(kBits > 0, "kBits cannot be zero.");
static_assert(kBits <= BitSizeOf<T>(), "kBits must be <= max.");
static_assert(std::is_integral_v<T>, "Needs an integral type."); // Corner case for "use all bits." Can't use the limits, as they would overflow, but it is // trivially true. // NOTE: To avoid triggering assertion in GetIntLimit(kBits+1) if kBits+1==BitSizeOf<T>(), // use GetIntLimit(kBits)*2u. The unsigned arithmetic works well for us if it overflows. using unsigned_type = std::make_unsigned_t<T>; return (0 <= value) &&
(kBits == BitSizeOf<T>() ||
(static_cast<unsigned_type>(value) <= GetIntLimit<unsigned_type>(kBits) * 2u - 1u));
}
template <size_t kBits, typename T>
constexpr bool IsAbsoluteUint(T value) {
static_assert(kBits <= BitSizeOf<T>(), "kBits must be <= max.");
static_assert(std::is_integral_v<T>, "Needs an integral type."); using unsigned_type = std::make_unsigned_t<T>; return (kBits == BitSizeOf<T>())
? true
: IsUint<kBits>(value < 0
? static_cast<unsigned_type>(-1 - value) + 1u // Avoid overflow.
: static_cast<unsigned_type>(value));
}
// Generate maximum/minimum values for signed/unsigned n-bit integers template <typename T>
constexpr T MaxInt(size_t bits) {
DCHECK(std::is_unsigned_v<T> || bits > 0u) << "bits cannot be zero for signed.";
DCHECK_LE(bits, BitSizeOf<T>()); using unsigned_type = std::make_unsigned_t<T>; return bits == BitSizeOf<T>()
? std::numeric_limits<T>::max()
: std::is_signed_v<T>
? ((bits == 1u) ? 0 : static_cast<T>(MaxInt<unsigned_type>(bits - 1)))
: static_cast<T>(UINT64_C(1) << bits) - static_cast<T>(1);
}
// Returns value with bit set in lowest one-bit position or 0 if 0. (java.lang.X.lowestOneBit). template <typename kind> inlinestatic kind LowestOneBitValue(kind opnd) { // Hacker's Delight, Section 2-1 return opnd & -opnd;
}
// Returns value with bit set in hightest one-bit position or 0 if 0. (java.lang.X.highestOneBit). template <typename T> inlinestatic T HighestOneBitValue(T opnd) { using unsigned_type = std::make_unsigned_t<T>;
T res; if (opnd == 0) {
res = 0;
} else { int bit_position = BitSizeOf<T>() - (CLZ(static_cast<unsigned_type>(opnd)) + 1);
res = static_cast<T>(UINT64_C(1) << bit_position);
} return res;
}
// Rotate bits. template <typename T, bool left> inlinestatic T Rot(T opnd, int distance) { int mask = BitSizeOf<T>() - 1; int unsigned_right_shift = left ? (-distance & mask) : (distance & mask); int signed_left_shift = left ? (distance & mask) : (-distance & mask); using unsigned_type = std::make_unsigned_t<T>; return (static_cast<unsigned_type>(opnd) >> unsigned_right_shift) | (opnd << signed_left_shift);
}
// Create a mask for the least significant "bits" // The returned value is always unsigned to prevent undefined behavior for bitwise ops. // // Given 'bits', // Returns: // <--- bits ---> // +-----------------+------------+ // | 0 ............0 | 1.....1 | // +-----------------+------------+ // msb lsb template <typename T = size_t> inlinestatic constexpr std::make_unsigned_t<T> MaskLeastSignificant(size_t bits) {
DCHECK_GE(BitSizeOf<T>(), bits) << "Bits out of range for type T"; using unsigned_T = std::make_unsigned_t<T>; if (bits >= BitSizeOf<T>()) { return std::numeric_limits<unsigned_T>::max();
} else { auto kOne = static_cast<unsigned_T>(1); // Do not truncate for T>size_t. returnstatic_cast<unsigned_T>((kOne << bits) - kOne);
}
}
// Clears the bitfield starting at the least significant bit "lsb" with a bitwidth of 'width'. // (Equivalent of ARM BFC instruction). // // Given: // <-- width --> // +--------+------------+--------+ // | ABC... | bitfield | XYZ... + // +--------+------------+--------+ // lsb 0 // Returns: // <-- width --> // +--------+------------+--------+ // | ABC... | 0........0 | XYZ... + // +--------+------------+--------+ // lsb 0 template <typename T> inlinestatic constexpr T BitFieldClear(T value, size_t lsb, size_t width) {
DCHECK_GE(BitSizeOf(value), lsb + width) << "Bit field out of range for value"; constauto val = static_cast<std::make_unsigned_t<T>>(value); constauto mask = MaskLeastSignificant<T>(width);
returnstatic_cast<T>(val & ~(mask << lsb));
}
// Inserts the contents of 'data' into bitfield of 'value' starting // at the least significant bit "lsb" with a bitwidth of 'width'. // Note: data must be within range of [MinInt(width), MaxInt(width)]. // (Equivalent of ARM BFI instruction). // // Given (data): // <-- width --> // +--------+------------+--------+ // | ABC... | bitfield | XYZ... + // +--------+------------+--------+ // lsb 0 // Returns: // <-- width --> // +--------+------------+--------+ // | ABC... | 0...data | XYZ... + // +--------+------------+--------+ // lsb 0
template <typename T, typename T2> inlinestatic constexpr T BitFieldInsert(T value, T2 data, size_t lsb, size_t width) {
DCHECK_GE(BitSizeOf(value), lsb + width) << "Bit field out of range for value"; if (width != 0u) {
DCHECK_GE(MaxInt<T2>(width), data) << "Data out of range [too large] for bitwidth";
DCHECK_LE(MinInt<T2>(width), data) << "Data out of range [too small] for bitwidth";
} else {
DCHECK_EQ(static_cast<T2>(0), data) << "Data out of range [nonzero] for bitwidth 0";
} constauto data_mask = MaskLeastSignificant<T2>(width); constauto value_cleared = BitFieldClear(value, lsb, width);
// Extracts the bitfield starting at the least significant bit "lsb" with a bitwidth of 'width'. // Signed types are sign-extended during extraction. (Equivalent of ARM UBFX/SBFX instruction). // // Given: // <-- width --> // +--------+-------------+-------+ // | | bitfield | + // +--------+-------------+-------+ // lsb 0 // (Unsigned) Returns: // <-- width --> // +----------------+-------------+ // | 0... 0 | bitfield | // +----------------+-------------+ // 0 // (Signed) Returns: // <-- width --> // +----------------+-------------+ // | S... S | bitfield | // +----------------+-------------+ // 0 // where S is the highest bit in 'bitfield'. template <typename T> inlinestatic constexpr T BitFieldExtract(T value, size_t lsb, size_t width) {
DCHECK_GE(BitSizeOf(value), lsb + width) << "Bit field out of range for value"; constauto val = static_cast<std::make_unsigned_t<T>>(value);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.