5 #include "src/base/bits.h" 9 #include "src/base/logging.h" 10 #include "src/base/safe_math.h" 20 #if V8_HAS_BUILTIN_CLZ || V8_CC_MSVC 21 return 1u << (32 - CountLeadingZeros(value));
32 uint64_t RoundUpToPowerOfTwo64(uint64_t value) {
33 DCHECK_LE(value, uint64_t{1} << 63);
36 #if V8_HAS_BUILTIN_CLZ 37 return uint64_t{1} << (64 - CountLeadingZeros(value));
50 int32_t SignedMulHigh32(int32_t lhs, int32_t rhs) {
51 int64_t const value =
static_cast<int64_t>(lhs) * static_cast<int64_t>(rhs);
52 return bit_cast<int32_t,
uint32_t>(bit_cast<uint64_t>(value) >> 32u);
56 int32_t SignedMulHighAndAdd32(int32_t lhs, int32_t rhs, int32_t acc) {
57 return bit_cast<int32_t>(bit_cast<
uint32_t>(acc) +
58 bit_cast<uint32_t>(SignedMulHigh32(lhs, rhs)));
62 int32_t SignedDiv32(int32_t lhs, int32_t rhs) {
63 if (rhs == 0)
return 0;
64 if (rhs == -1)
return -lhs;
69 int32_t SignedMod32(int32_t lhs, int32_t rhs) {
70 if (rhs == 0 || rhs == -1)
return 0;
75 int64_t FromCheckedNumeric(
const internal::CheckedNumeric<int64_t> value) {
77 return value.ValueUnsafe();
83 int64_t limit = std::numeric_limits<int64_t>::max();
84 if (value.validity() == internal::RANGE_UNDERFLOW)
86 return value.ValueOrDefault(limit);
91 internal::CheckedNumeric<int64_t> rv(lhs);
93 return FromCheckedNumeric(rv);
98 internal::CheckedNumeric<int64_t> rv(lhs);
100 return FromCheckedNumeric(rv);
103 bool SignedMulOverflow32(int32_t lhs, int32_t rhs, int32_t* val) {
104 internal::CheckedNumeric<int32_t> rv(lhs);
106 int32_t limit = std::numeric_limits<int32_t>::max();
107 *val = rv.ValueOrDefault(limit);
108 return !rv.IsValid();
112 internal::CheckedNumeric<int64_t> rv(lhs);
114 int64_t limit = std::numeric_limits<int64_t>::max();
115 *val = rv.ValueOrDefault(limit);
116 return !rv.IsValid();