8 #ifndef V8_BASE_SAFE_MATH_H_
9 #define V8_BASE_SAFE_MATH_H_
55 template <
typename Src>
59 template <
typename Src>
65 template <
typename Src>
127 if (std::numeric_limits<T>::is_iec559)
138 if (std::numeric_limits<T>::is_iec559)
170 template <
typename Src>
173 typename enable_if<std::numeric_limits<Src>::is_specialized,
int>::
type =
178 template <
typename Src>
198 #define BASE_NUMERIC_ARITHMETIC_OPERATORS(NAME, OP, COMPOUND_OP) \
200 template <typename T> \
201 CheckedNumeric<typename ArithmeticPromotion<T>::type> operator OP( \
202 const CheckedNumeric<T>& lhs, const CheckedNumeric<T>& rhs) { \
203 typedef typename ArithmeticPromotion<T>::type Promotion; \
205 if (std::numeric_limits<T>::is_iec559) \
206 return CheckedNumeric<T>(lhs.ValueUnsafe() OP rhs.ValueUnsafe()); \
207 if (IsIntegerArithmeticSafe<Promotion, T, T>::value) \
208 return CheckedNumeric<Promotion>( \
209 lhs.ValueUnsafe() OP rhs.ValueUnsafe(), \
210 GetRangeConstraint(rhs.validity() | lhs.validity())); \
211 RangeConstraint validity = RANGE_VALID; \
212 T result = Checked##NAME(static_cast<Promotion>(lhs.ValueUnsafe()), \
213 static_cast<Promotion>(rhs.ValueUnsafe()), \
215 return CheckedNumeric<Promotion>( \
217 GetRangeConstraint(validity | lhs.validity() | rhs.validity())); \
220 template <typename T> \
221 template <typename Src> \
222 CheckedNumeric<T>& CheckedNumeric<T>::operator COMPOUND_OP(Src rhs) { \
223 *this = CheckedNumeric<T>::cast(*this) OP CheckedNumeric<Src>::cast(rhs); \
227 template <typename T, typename Src> \
228 CheckedNumeric<typename ArithmeticPromotion<T, Src>::type> operator OP( \
229 const CheckedNumeric<Src>& lhs, const CheckedNumeric<T>& rhs) { \
230 typedef typename ArithmeticPromotion<T, Src>::type Promotion; \
231 if (IsIntegerArithmeticSafe<Promotion, T, Src>::value) \
232 return CheckedNumeric<Promotion>( \
233 lhs.ValueUnsafe() OP rhs.ValueUnsafe(), \
234 GetRangeConstraint(rhs.validity() | lhs.validity())); \
235 return CheckedNumeric<Promotion>::cast(lhs) \
236 OP CheckedNumeric<Promotion>::cast(rhs); \
239 template <typename T, typename Src> \
240 CheckedNumeric<typename ArithmeticPromotion<T, Src>::type> operator OP( \
241 const CheckedNumeric<T>& lhs, Src rhs) { \
242 typedef typename ArithmeticPromotion<T, Src>::type Promotion; \
243 if (IsIntegerArithmeticSafe<Promotion, T, Src>::value) \
244 return CheckedNumeric<Promotion>(lhs.ValueUnsafe() OP rhs, \
246 return CheckedNumeric<Promotion>::cast(lhs) \
247 OP CheckedNumeric<Promotion>::cast(rhs); \
250 template <typename T, typename Src> \
251 CheckedNumeric<typename ArithmeticPromotion<T, Src>::type> operator OP( \
252 Src lhs, const CheckedNumeric<T>& rhs) { \
253 typedef typename ArithmeticPromotion<T, Src>::type Promotion; \
254 if (IsIntegerArithmeticSafe<Promotion, T, Src>::value) \
255 return CheckedNumeric<Promotion>(lhs OP rhs.ValueUnsafe(), \
257 return CheckedNumeric<Promotion>::cast(lhs) \
258 OP CheckedNumeric<Promotion>::cast(rhs); \
267 #undef BASE_NUMERIC_ARITHMETIC_OPERATORS
271 using internal::CheckedNumeric;
static CheckedNumeric< T > cast(const CheckedNumeric< Src > &u, typename enable_if<!is_same< Src, T >::value, int >::type=0)
CheckedNumeric operator--(int)
CheckedNumeric & operator*=(Src rhs)
CheckedNumeric & operator%=(Src rhs)
static CheckedNumeric< T > cast(Src u, typename enable_if< std::numeric_limits< Src >::is_specialized, int >::type=0)
CheckedNumeric(Src value, RangeConstraint validity)
CheckedNumeric operator++(int)
CheckedNumeric operator-() const
CheckedNumeric & operator-=(Src rhs)
CheckedNumeric & operator/=(Src rhs)
T ValueOrDefault(T default_value) const
static const CheckedNumeric< T > & cast(const CheckedNumeric< T > &u)
RangeConstraint validity() const
CheckedNumericState< T > state_
CheckedNumeric(const CheckedNumeric< Src > &rhs)
CheckedNumeric & operator+=(Src rhs)
CheckedNumeric Abs() const
CheckedNumeric & operator++()
CheckedNumeric & operator--()
CheckedNumeric(Src value)
#define STATIC_ASSERT(test)
enable_if< std::numeric_limits< T >::is_integer &&std::numeric_limits< T >::is_signed, T >::type CheckedAbs(T value, RangeConstraint *validity)
RangeConstraint GetRangeConstraint(int integer_range_constraint)
enable_if< std::numeric_limits< T >::is_integer &&std::numeric_limits< T >::is_signed, T >::type CheckedNeg(T value, RangeConstraint *validity)
Debugger support for the V8 JavaScript engine.
#define BASE_NUMERIC_ARITHMETIC_OPERATORS(NAME, OP, COMPOUND_OP)
#define T(name, string, precedence)