5 #ifndef BASE_NUMERICS_SAFE_MATH_H_ 6 #define BASE_NUMERICS_SAFE_MATH_H_ 11 #include <type_traits> 13 #include "base/logging.h" 62 template <
typename Src>
67 template <
typename Src>
74 template <
typename Src>
78 static_assert(std::numeric_limits<Src>::is_specialized,
"Argument must be numeric.");
83 template <
typename Src>
85 : state_(static_cast<Src>(value))
97 return state_.value();
110 static_assert(std::numeric_limits<T>::is_iec559,
"Argument must be float.");
133 template <
typename Src>
135 template <
typename Src>
137 template <
typename Src>
139 template <
typename Src>
141 template <
typename Src>
149 if (std::numeric_limits<T>::is_iec559)
161 if (std::numeric_limits<T>::is_iec559)
206 template <
typename Src>
209 typename std::enable_if<std::numeric_limits<Src>::is_specialized,
int>::type = 0)
214 template <
typename Src>
225 template <
typename NumericType>
226 struct UnderlyingType
228 using type = NumericType;
231 template <
typename NumericType>
232 struct UnderlyingType<CheckedNumeric<NumericType>>
234 using type = NumericType;
247 #define BASE_NUMERIC_ARITHMETIC_OPERATORS(NAME, OP, COMPOUND_OP) \ 249 template <typename T> \ 250 CheckedNumeric<typename ArithmeticPromotion<T>::type> operator OP( \ 251 const CheckedNumeric<T> &lhs, const CheckedNumeric<T> &rhs) \ 253 typedef typename ArithmeticPromotion<T>::type Promotion; \ 255 if (std::numeric_limits<T>::is_iec559) \ 256 return CheckedNumeric<T>(lhs.ValueUnsafe() OP rhs.ValueUnsafe()); \ 257 if (IsIntegerArithmeticSafe<Promotion, T, T>::value) \ 258 return CheckedNumeric<Promotion>(lhs.ValueUnsafe() OP rhs.ValueUnsafe(), \ 259 GetRangeConstraint(rhs.validity() | lhs.validity())); \ 260 RangeConstraint validity = RANGE_VALID; \ 262 static_cast<T>(Checked##NAME(static_cast<Promotion>(lhs.ValueUnsafe()), \ 263 static_cast<Promotion>(rhs.ValueUnsafe()), &validity)); \ 264 return CheckedNumeric<Promotion>( \ 265 result, GetRangeConstraint(validity | lhs.validity() | rhs.validity())); \ 268 template <typename T> \ 269 template <typename Src> \ 270 CheckedNumeric<T> &CheckedNumeric<T>::operator COMPOUND_OP(Src rhs) \ 272 *this = CheckedNumeric<T>::cast(*this) \ 273 OP CheckedNumeric<typename UnderlyingType<Src>::type>::cast(rhs); \ 277 template <typename T, typename Src> \ 278 CheckedNumeric<typename ArithmeticPromotion<T, Src>::type> operator OP( \ 279 const CheckedNumeric<Src> &lhs, const CheckedNumeric<T> &rhs) \ 281 typedef typename ArithmeticPromotion<T, Src>::type Promotion; \ 282 if (IsIntegerArithmeticSafe<Promotion, T, Src>::value) \ 283 return CheckedNumeric<Promotion>(lhs.ValueUnsafe() OP rhs.ValueUnsafe(), \ 284 GetRangeConstraint(rhs.validity() | lhs.validity())); \ 285 return CheckedNumeric<Promotion>::cast(lhs) OP CheckedNumeric<Promotion>::cast(rhs); \ 288 template <typename T, typename Src, \ 289 typename std::enable_if<std::is_arithmetic<Src>::value>::type * = nullptr> \ 290 CheckedNumeric<typename ArithmeticPromotion<T, Src>::type> operator OP( \ 291 const CheckedNumeric<T> &lhs, Src rhs) \ 293 typedef typename ArithmeticPromotion<T, Src>::type Promotion; \ 294 if (IsIntegerArithmeticSafe<Promotion, T, Src>::value) \ 295 return CheckedNumeric<Promotion>(lhs.ValueUnsafe() OP rhs, lhs.validity()); \ 296 return CheckedNumeric<Promotion>::cast(lhs) OP CheckedNumeric<Promotion>::cast(rhs); \ 299 template <typename T, typename Src, \ 300 typename std::enable_if<std::is_arithmetic<Src>::value>::type * = nullptr> \ 301 CheckedNumeric<typename ArithmeticPromotion<T, Src>::type> operator OP( \ 302 Src lhs, const CheckedNumeric<T> &rhs) \ 304 typedef typename ArithmeticPromotion<T, Src>::type Promotion; \ 305 if (IsIntegerArithmeticSafe<Promotion, T, Src>::value) \ 306 return CheckedNumeric<Promotion>(lhs OP rhs.ValueUnsafe(), rhs.validity()); \ 307 return CheckedNumeric<Promotion>::cast(lhs) OP CheckedNumeric<Promotion>::cast(rhs); \ 316 #undef BASE_NUMERIC_ARITHMETIC_OPERATORS 320 using internal::CheckedNumeric;
324 #endif // BASE_NUMERICS_SAFE_MATH_H_ RangeConstraint
Definition: safe_conversions_impl.h:95
CheckedNumeric & operator++()
Definition: safe_math.h:177
CheckedNumeric operator++(int)
Definition: safe_math.h:183
OPENSSL_EXPORT pem_password_cb void * u
Definition: pem.h:398
#define BASE_NUMERIC_ARITHMETIC_OPERATORS(NAME, OP, COMPOUND_OP)
Definition: safe_math.h:247
T ValueOrDefault(T default_value) const
Definition: safe_math.h:103
CheckedNumeric< typename UnsignedOrFloatForSize< T >::type > UnsignedAbs() const
Definition: safe_math.h:171
CheckedNumeric & operator+=(Src rhs)
CheckedNumeric operator-() const
Definition: safe_math.h:144
T ValueFloating() const
Definition: safe_math.h:108
CheckedNumeric operator--(int)
Definition: safe_math.h:196
std::enable_if< std::numeric_limits< T >::is_integer &&std::numeric_limits< T >::is_signed, T >::type CheckedAbs(T value, RangeConstraint *validity)
Definition: safe_math_impl.h:326
std::enable_if< std::numeric_limits< T >::is_integer &&std::numeric_limits< T >::is_signed, T >::type CheckedNeg(T value, RangeConstraint *validity)
Definition: safe_math_impl.h:306
TestSubObjConstructor T
Definition: TestTypedefs.idl:84
CheckedNumeric & operator-=(Src rhs)
CheckedNumeric Abs() const
Definition: safe_math.h:156
static const CheckedNumeric< T > & cast(const CheckedNumeric< T > &u)
Definition: safe_math.h:222
EGLAttrib * value
Definition: eglext.h:120
CheckedNumeric(StrictNumeric< Src > value)
Definition: safe_math.h:84
Definition: safe_math.h:52
std::enable_if< std::numeric_limits< T >::is_integer &&std::numeric_limits< T >::is_signed, typename UnsignedIntegerForSize< T >::type >::type CheckedUnsignedAbs(T value)
Definition: safe_math_impl.h:345
T ValueOrDie() const
Definition: safe_math.h:94
static CheckedNumeric< T > cast(const CheckedNumeric< Src > &u, typename std::enable_if<!std::is_same< Src, T >::value, int >::type=0)
Definition: safe_math.h:215
CheckedNumeric(const CheckedNumeric< Src > &rhs)
Definition: safe_math.h:63
Definition: safe_conversions.h:138
CheckedNumeric()
Definition: safe_math.h:59
Definition: safe_conversions.h:16
Definition: document.h:393
RangeConstraint validity() const
Definition: safe_math.h:121
T ValueUnsafe() const
Definition: safe_math.h:130
CheckedNumeric & operator/=(Src rhs)
T type
Definition: safe_math.h:54
#define CHECK(x)
Definition: dynbench.cpp:46
bool IsValid() const
Definition: safe_math.h:90
CheckedNumeric(Src value)
Definition: safe_math.h:75
CheckedNumeric(Src value, RangeConstraint validity)
Definition: safe_math.h:68
static CheckedNumeric< T > cast(Src u, typename std::enable_if< std::numeric_limits< Src >::is_specialized, int >::type=0)
Definition: safe_math.h:207
Definition: safe_conversions_impl.h:97
CheckedNumeric & operator*=(Src rhs)
CheckedNumeric & operator--()
Definition: safe_math.h:190
constexpr RangeConstraint GetRangeConstraint(int integer_range_constraint)
Definition: safe_conversions_impl.h:104
CheckedNumeric & operator%=(Src rhs)