8#ifndef V8_BASE_NUMERICS_CHECKED_MATH_H_
9#define V8_BASE_NUMERICS_CHECKED_MATH_H_
24 requires std::is_arithmetic_v<T>
32 template <
typename Src>
38 template <
typename Src>
39 requires(std::is_arithmetic_v<Src>)
45 template <
typename Src>
48 :
state_(static_cast<Src>(value)) {}
53 template <
typename Dst = T>
55 return state_.is_valid() &&
62 template <
typename Dst>
63#if defined(__clang__) || defined(__GNUC__)
64 __attribute__((warn_unused_result))
65#elif defined(_MSC_VER)
85 template <
typename Dst = T,
class CheckHandler = CheckOnFailure>
88 return static_cast<Dst
>(
state_.value());
90 return CheckHandler::template HandleFailure<Dst>();
99 template <
typename Dst = T,
typename Src>
102 return static_cast<Dst
>(
state_.value());
110 template <
typename Dst>
118 template <
typename U>
122 template <
typename Src>
124 template <
typename Src>
126 template <
typename Src>
128 template <
typename Src>
130 template <
typename Src>
132 template <
typename Src>
134 template <
typename Src>
136 template <
typename Src>
138 template <
typename Src>
140 template <
typename Src>
145 if (!std::is_constant_evaluated() && std::is_signed_v<T> &&
146 std::is_floating_point_v<T>) {
150 const bool is_valid =
152 (!std::is_signed_v<T> || std::is_floating_point_v<T> ||
166 template <
typename U>
169 return CheckMax(*
this, rhs);
172 template <
typename U>
175 return CheckMin(*
this, rhs);
211 template <
template <
typename,
typename>
class M,
typename L,
typename R>
215 const bool is_valid =
222 template <
template <
typename,
typename>
class M,
typename R>
226 const bool is_valid =
234 template <
typename U>
235 requires std::is_arithmetic_v<U>
246 template <
typename Src>
248 :
state_(value, is_valid) {}
252 template <
typename Src>
254 static constexpr bool is_valid(Src) {
return true; }
258 template <
typename Src>
264 return v.state_.value();
268 template <
typename Src>
272 return static_cast<Src
>(v);
281template <
typename Dst,
typename Src>
283 return value.template IsValid<Dst>();
286template <
typename Dst,
typename Src>
289 return value.template ValueOrDie<Dst>();
292template <
typename Dst,
typename Src,
typename Default>
294 Default default_value) {
295 return value.template ValueOrDefault<Dst>(default_value);
306template <
template <
typename,
typename>
class M,
typename L,
typename R>
315template <
template <
typename,
typename>
class M,
typename L,
typename R,
337template <typename L, typename R>
339 const uintptr_t
result = CheckAdd(
reinterpret_cast<uintptr_t
>(lhs),
340 CheckMul(
sizeof(L),
static_cast<R
>(rhs)))
341 .template ValueOrDie<uintptr_t>();
342 return reinterpret_cast<L*
>(
result);
345template <
typename L,
typename R>
347 const uintptr_t
result = CheckSub(
reinterpret_cast<uintptr_t
>(lhs),
348 CheckMul(
sizeof(L),
static_cast<R
>(rhs)))
349 .template ValueOrDie<uintptr_t>();
350 return reinterpret_cast<L*
>(
result);
355using internal::CheckAdd;
356using internal::CheckAnd;
357using internal::CheckDiv;
359using internal::CheckLsh;
360using internal::CheckMax;
361using internal::CheckMin;
362using internal::CheckMod;
363using internal::CheckMul;
364using internal::CheckOr;
365using internal::CheckRsh;
366using internal::CheckSub;
367using internal::CheckXor;
constexpr CheckedNumeric & operator%=(const Src rhs)
CheckedNumeric FastRuntimeNegate() const
constexpr CheckedNumeric(const CheckedNumeric< Src > &rhs)
friend class CheckedNumeric
constexpr CheckedNumeric & operator--()
constexpr CheckedNumeric & MathOp(R rhs)
constexpr CheckedNumeric operator--(int)
constexpr CheckedNumeric & operator&=(const Src rhs)
constexpr CheckedNumeric & operator++()
constexpr CheckedNumeric & operator/=(const Src rhs)
constexpr CheckedNumeric & operator<<=(const Src rhs)
constexpr StrictNumeric< Dst > ValueOrDefault(Src default_value) const
constexpr CheckedNumeric & operator>>=(const Src rhs)
constexpr CheckedNumeric< typename MathWrapper< CheckedMaxOp, T, U >::type > Max(U rhs) const
constexpr CheckedNumeric operator-() const
constexpr CheckedNumeric(Src value, bool is_valid)
constexpr CheckedNumeric operator++(int)
constexpr bool IsValid() const
constexpr bool AssignIfValid(Dst *result) const
constexpr CheckedNumeric & operator|=(const Src rhs)
constexpr CheckedNumeric< UnderlyingType< Dst > > Cast() const
constexpr CheckedNumeric & operator-=(const Src rhs)
constexpr CheckedNumeric(StrictNumeric< Src > value)
constexpr CheckedNumeric & operator^=(const Src rhs)
constexpr CheckedNumeric< typename UnsignedOrFloatForSize< T >::type > UnsignedAbs() const
friend U GetNumericValueForTest(const CheckedNumeric< U > &src)
constexpr StrictNumeric< Dst > ValueOrDie() const
constexpr CheckedNumeric & operator+=(const Src rhs)
static constexpr CheckedNumeric MathOp(L lhs, R rhs)
CheckedNumericState< T > state_
constexpr CheckedNumeric(Src value)
constexpr CheckedNumeric operator~() const
constexpr CheckedNumeric Abs() const
constexpr CheckedNumeric()=default
constexpr CheckedNumeric< typename MathWrapper< CheckedMinOp, T, U >::type > Min(U rhs) const
constexpr CheckedNumeric & operator*=(const Src rhs)
ZoneVector< RpoNumber > & result
constexpr auto SafeUnsignedAbs(T value)
constexpr bool IsValueNegative(T value)
constexpr T NegateWrapper(T value)
constexpr std::make_unsigned< T >::type InvertWrapper(T value)
constexpr StrictNumeric< Dst > ValueOrDieForType(const CheckedNumeric< Src > value)
constexpr CheckedNumeric< typename MathWrapper< M, L, R >::type > CheckMathOp(L lhs, R rhs)
constexpr CheckedNumeric< UnderlyingType< T > > MakeCheckedNum(T value)
constexpr StrictNumeric< Dst > ValueOrDefaultForType(CheckedNumeric< Src > value, Default default_value)
CheckedNumeric(T) -> CheckedNumeric< T >
constexpr Dst checked_cast(Src value)
constexpr bool IsValueInRangeForNumericType(Src value)
constexpr bool IsValidForType(const CheckedNumeric< Src > value)
L * operator-(L *lhs, StrictNumeric< R > rhs)
V8_BASE_EXPORT int const char va_list args
#define BASE_NUMERIC_ARITHMETIC_OPERATORS(CLASS, CL_ABBR, OP_NAME, OP, CMP_OP)
#define BASE_NUMERIC_ARITHMETIC_VARIADIC(CLASS, CL_ABBR, OP_NAME)
static constexpr bool is_valid(CheckedNumeric< Src > v)
static constexpr Src value(CheckedNumeric< Src > v)
static constexpr Src value(StrictNumeric< Src > v)
static constexpr bool is_valid(StrictNumeric< Src >)
static constexpr bool is_valid(Src)
static constexpr Src value(Src value)
std::unique_ptr< ValueMirror > value