5#ifndef V8_BASE_OVERFLOWING_MATH_H_
6#define V8_BASE_OVERFLOWING_MATH_H_
20#define ASSERT_SIGNED_INTEGER_TYPE(Type) \
21 static_assert(std::is_integral<Type>::value && std::is_signed<Type>::value, \
22 "use this for signed integer types");
23#define OP_WITH_WRAPAROUND(Name, OP) \
24 template <typename signed_type> \
25 inline signed_type Name##WithWraparound(signed_type a, signed_type b) { \
26 ASSERT_SIGNED_INTEGER_TYPE(signed_type); \
27 using unsigned_type = typename std::make_unsigned<signed_type>::type; \
28 unsigned_type a_unsigned = static_cast<unsigned_type>(a); \
29 unsigned_type b_unsigned = static_cast<unsigned_type>(b); \
30 unsigned_type result = a_unsigned OP b_unsigned; \
31 return static_cast<signed_type>(result); \
42 uint32_t a_unsigned =
static_cast<uint32_t
>(
a);
43 uint32_t b_unsigned =
static_cast<uint32_t
>(b);
44 uint32_t
result = a_unsigned * b_unsigned;
45 return static_cast<int16_t
>(
static_cast<uint16_t
>(
result));
48#undef OP_WITH_WRAPAROUND
50template <
typename signed_type>
53 if (a == std::numeric_limits<signed_type>::min())
return a;
57template <
typename signed_type>
60 using unsigned_type =
typename std::make_unsigned<signed_type>::type;
61 const unsigned_type kMask = (
sizeof(
a) * 8) - 1;
62 return static_cast<signed_type
>(
static_cast<unsigned_type
>(
a) << (b & kMask));
65#undef ASSERT_SIGNED_INTEGER_TYPE
70 if (
y != 0)
return x /
y;
71 if (
x == 0 ||
x !=
x)
return std::numeric_limits<T>::quiet_NaN();
72 if ((
x >= 0) == (std::signbit(
y) == 0)) {
73 return std::numeric_limits<T>::infinity();
75 return -std::numeric_limits<T>::infinity();
81 if (a != 0)
return 1.0f / std::sqrt(a);
82 if (std::signbit(a) == 0)
return std::numeric_limits<float>::infinity();
83 return -std::numeric_limits<float>::infinity();
std::optional< TNode< JSArray > > a
ZoneVector< RpoNumber > & result
int16_t MulWithWraparound(int16_t a, int16_t b)
signed_type NegateWithWraparound(signed_type a)
signed_type ShlWithWraparound(signed_type a, signed_type b)
#define OP_WITH_WRAPAROUND(Name, OP)
#define ASSERT_SIGNED_INTEGER_TYPE(Type)