#ifndef V8_BASE_BITS_H_
#define V8_BASE_BITS_H_
#include <stdint.h>
#include <type_traits>
#include "src/base/base-export.h"
#include "src/base/macros.h"
#if V8_CC_MSVC
#include <intrin.h>
#endif
#if V8_OS_WIN32
#include "src/base/win32-headers.h"
#endif
namespace v8 {
namespace base {
namespace bits {
template <typename T>
constexpr inline
typename std::enable_if<std::is_unsigned<T>::value && sizeof(T) <= 8,
unsigned>::type
CountPopulation(T value) {
STATIC_ASSERT(sizeof(T) <= 8);
#if V8_HAS_BUILTIN_POPCOUNT
return sizeof(T) == 8 ? __builtin_popcountll(static_cast<uint64_t>(value))
: __builtin_popcount(static_cast<uint32_t>(value));
#else
constexpr uint64_t mask[] = {0x5555555555555555, 0x3333333333333333,
0x0f0f0f0f0f0f0f0f};
value = ((value >> 1) & mask[0]) + (value & mask[0]);
value = ((value >> 2) & mask[1]) + (value & mask[1]);
value = ((value >> 4) & mask[2]) + (value & mask[2]);
if (sizeof(T) > 1) value = (value >> (sizeof(T) > 1 ? 8 : 0)) + value;
if (sizeof(T) > 2) value = (value >> (sizeof(T) > 2 ? 16 : 0)) + value;
if (sizeof(T) > 4) value = (value >> (sizeof(T) > 4 ? 32 : 0)) + value;
return static_cast<unsigned>(value & 0xff);
#endif
}
template <typename T>
T ReverseBits(T value) {
STATIC_ASSERT((sizeof(value) == 1) || (sizeof(value) == 2) ||
(sizeof(value) == 4) || (sizeof(value) == 8));
T result = 0;
for (unsigned i = 0; i < (sizeof(value) * 8); i++) {
result = (result << 1) | (value & 1);
value >>= 1;
}
return result;
}
template <typename T, unsigned bits = sizeof(T) * 8>
inline constexpr
typename std::enable_if<std::is_unsigned<T>::value && sizeof(T) <= 8,
unsigned>::type
CountLeadingZeros(T value) {
static_assert(bits > 0, "invalid instantiation");
#if V8_HAS_BUILTIN_CLZ
return value == 0
? bits
: bits == 64
? __builtin_clzll(static_cast<uint64_t>(value))
: __builtin_clz(static_cast<uint32_t>(value)) - (32 - bits);
#else
if (bits == 1) return static_cast<unsigned>(value) ^ 1;
T upper_half = value >> (bits / 2);
T next_value = upper_half != 0 ? upper_half : value;
unsigned add = upper_half != 0 ? 0 : bits / 2;
constexpr unsigned next_bits = bits == 1 ? 1 : bits / 2;
return CountLeadingZeros<T, next_bits>(next_value) + add;
#endif
}
inline constexpr unsigned CountLeadingZeros32(uint32_t value) {
return CountLeadingZeros(value);
}
inline constexpr unsigned CountLeadingZeros64(uint64_t value) {
return CountLeadingZeros(value);
}
template <typename T, unsigned bits = sizeof(T) * 8>
inline constexpr
typename std::enable_if<std::is_integral<T>::value && sizeof(T) <= 8,
unsigned>::type
CountTrailingZeros(T value) {
#if V8_HAS_BUILTIN_CTZ
return value == 0 ? bits
: bits == 64 ? __builtin_ctzll(static_cast<uint64_t>(value))
: __builtin_ctz(static_cast<uint32_t>(value));
#else
using U = typename std::make_unsigned<T>::type;
U u = value;
return CountPopulation(static_cast<U>(~u & (u - 1u)));
#endif
}
inline constexpr unsigned CountTrailingZeros32(uint32_t value) {
return CountTrailingZeros(value);
}
inline constexpr unsigned CountTrailingZeros64(uint64_t value) {
return CountTrailingZeros(value);
}
template <typename T, unsigned bits = sizeof(T) * 8>
inline constexpr
typename std::enable_if<std::is_integral<T>::value && sizeof(T) <= 8,
unsigned>::type
CountTrailingZerosNonZero(T value) {
DCHECK_NE(0, value);
#if V8_HAS_BUILTIN_CTZ
return bits == 64 ? __builtin_ctzll(static_cast<uint64_t>(value))
: __builtin_ctz(static_cast<uint32_t>(value));
#else
return CountTrailingZeros<T, bits>(value);
#endif
}
template <typename T,
typename = typename std::enable_if<std::is_integral<T>::value ||
std::is_enum<T>::value>::type>
constexpr inline bool IsPowerOfTwo(T value) {
return value > 0 && (value & (value - 1)) == 0;
}
template <typename T,
typename = typename std::enable_if<std::is_integral<T>::value>::type>
inline constexpr int WhichPowerOfTwo(T value) {
DCHECK(IsPowerOfTwo(value));
#if V8_HAS_BUILTIN_CTZ
STATIC_ASSERT(sizeof(T) <= 8);
return sizeof(T) == 8 ? __builtin_ctzll(static_cast<uint64_t>(value))
: __builtin_ctz(static_cast<uint32_t>(value));
#else
using U = typename std::make_unsigned<T>::type;
U u = value;
return CountPopulation(static_cast<U>(u - 1));
#endif
}
V8_BASE_EXPORT uint32_t RoundUpToPowerOfTwo32(uint32_t value);
V8_BASE_EXPORT uint64_t RoundUpToPowerOfTwo64(uint64_t value);
inline size_t RoundUpToPowerOfTwo(size_t value) {
if (sizeof(size_t) == sizeof(uint64_t)) {
return RoundUpToPowerOfTwo64(value);
} else {
return RoundUpToPowerOfTwo32(static_cast<uint32_t>(value));
}
}
inline uint32_t RoundDownToPowerOfTwo32(uint32_t value) {
if (value > 0x80000000u) return 0x80000000u;
uint32_t result = RoundUpToPowerOfTwo32(value);
if (result > value) result >>= 1;
return result;
}
inline constexpr uint32_t RotateRight32(uint32_t value, uint32_t shift) {
return (value >> shift) | (value << ((32 - shift) & 31));
}
inline constexpr uint32_t RotateLeft32(uint32_t value, uint32_t shift) {
return (value << shift) | (value >> ((32 - shift) & 31));
}
inline constexpr uint64_t RotateRight64(uint64_t value, uint64_t shift) {
return (value >> shift) | (value << ((64 - shift) & 63));
}
inline constexpr uint64_t RotateLeft64(uint64_t value, uint64_t shift) {
return (value << shift) | (value >> ((64 - shift) & 63));
}
inline bool SignedAddOverflow32(int32_t lhs, int32_t rhs, int32_t* val) {
#if V8_HAS_BUILTIN_SADD_OVERFLOW
return __builtin_sadd_overflow(lhs, rhs, val);
#else
uint32_t res = static_cast<uint32_t>(lhs) + static_cast<uint32_t>(rhs);
*val = bit_cast<int32_t>(res);
return ((res ^ lhs) & (res ^ rhs) & (1U << 31)) != 0;
#endif
}
inline bool SignedSubOverflow32(int32_t lhs, int32_t rhs, int32_t* val) {
#if V8_HAS_BUILTIN_SSUB_OVERFLOW
return __builtin_ssub_overflow(lhs, rhs, val);
#else
uint32_t res = static_cast<uint32_t>(lhs) - static_cast<uint32_t>(rhs);
*val = bit_cast<int32_t>(res);
return ((res ^ lhs) & (res ^ ~rhs) & (1U << 31)) != 0;
#endif
}
V8_BASE_EXPORT bool SignedMulOverflow32(int32_t lhs, int32_t rhs, int32_t* val);
inline bool SignedAddOverflow64(int64_t lhs, int64_t rhs, int64_t* val) {
uint64_t res = static_cast<uint64_t>(lhs) + static_cast<uint64_t>(rhs);
*val = bit_cast<int64_t>(res);
return ((res ^ lhs) & (res ^ rhs) & (1ULL << 63)) != 0;
}
inline bool SignedSubOverflow64(int64_t lhs, int64_t rhs, int64_t* val) {
uint64_t res = static_cast<uint64_t>(lhs) - static_cast<uint64_t>(rhs);
*val = bit_cast<int64_t>(res);
return ((res ^ lhs) & (res ^ ~rhs) & (1ULL << 63)) != 0;
}
V8_BASE_EXPORT int32_t SignedMulHigh32(int32_t lhs, int32_t rhs);
V8_BASE_EXPORT int32_t SignedMulHighAndAdd32(int32_t lhs, int32_t rhs,
int32_t acc);
V8_BASE_EXPORT int32_t SignedDiv32(int32_t lhs, int32_t rhs);
V8_BASE_EXPORT int32_t SignedMod32(int32_t lhs, int32_t rhs);
inline bool UnsignedAddOverflow32(uint32_t lhs, uint32_t rhs, uint32_t* val) {
#if V8_HAS_BUILTIN_SADD_OVERFLOW
return __builtin_uadd_overflow(lhs, rhs, val);
#else
*val = lhs + rhs;
return *val < (lhs | rhs);
#endif
}
inline uint32_t UnsignedDiv32(uint32_t lhs, uint32_t rhs) {
return rhs ? lhs / rhs : 0u;
}
inline uint32_t UnsignedMod32(uint32_t lhs, uint32_t rhs) {
return rhs ? lhs % rhs : 0u;
}
inline int32_t WraparoundAdd32(int32_t lhs, int32_t rhs) {
return static_cast<int32_t>(static_cast<uint32_t>(lhs) +
static_cast<uint32_t>(rhs));
}
inline int32_t WraparoundNeg32(int32_t x) {
return static_cast<int32_t>(-static_cast<uint32_t>(x));
}
V8_BASE_EXPORT int64_t SignedSaturatedAdd64(int64_t lhs, int64_t rhs);
V8_BASE_EXPORT int64_t SignedSaturatedSub64(int64_t lhs, int64_t rhs);
}
}
}
#endif