5 #ifndef BASE_NUMERICS_SAFE_MATH_IMPL_H_ 6 #define BASE_NUMERICS_SAFE_MATH_IMPL_H_ 15 #include <type_traits> 29 template <
size_t Size,
bool IsSigned>
76 template <
typename Integer>
80 typename std::enable_if<std::numeric_limits<Integer>::is_integer,
85 template <
typename Integer>
89 typename std::enable_if<std::numeric_limits<Integer>::is_integer,
94 template <
typename Integer>
97 typedef typename std::enable_if<
98 std::numeric_limits<Integer>::is_integer,
103 template <
typename Integer>
106 static const typename std::enable_if<std::numeric_limits<Integer>::is_integer,
size_t>
::type 107 value = CHAR_BIT *
sizeof(Integer) - 1;
114 template <
typename Numeric,
115 bool IsInteger = std::numeric_limits<Numeric>::is_integer,
116 bool IsFloat = std::numeric_limits<Numeric>::is_iec559>
119 template <
typename Numeric>
125 template <
typename Numeric>
133 template <
typename T>
142 template <
typename T>
145 return static_cast<T>(~x);
152 template <
typename T>
153 typename std::enable_if<std::numeric_limits<T>::is_integer,
T>
::type 159 UnsignedDst ux =
static_cast<UnsignedDst
>(
x);
160 UnsignedDst uy =
static_cast<UnsignedDst
>(
y);
161 UnsignedDst uresult =
static_cast<UnsignedDst
>(ux + uy);
164 if (std::numeric_limits<T>::is_signed)
179 return static_cast<T>(uresult);
182 template <
typename T>
183 typename std::enable_if<std::numeric_limits<T>::is_integer,
T>
::type 189 UnsignedDst ux =
static_cast<UnsignedDst
>(
x);
190 UnsignedDst uy =
static_cast<UnsignedDst
>(
y);
191 UnsignedDst uresult =
static_cast<UnsignedDst
>(ux - uy);
194 if (std::numeric_limits<T>::is_signed)
209 return static_cast<T>(uresult);
216 template <
typename T>
217 typename std::enable_if<std::numeric_limits<T>::is_integer &&
sizeof(
T) * 2 <=
sizeof(
uintmax_t),
222 IntermediateType tmp =
static_cast<IntermediateType
>(
x) * static_cast<IntermediateType>(y);
223 *validity = DstRangeRelationToSrcRange<T>(tmp);
224 return static_cast<T>(tmp);
227 template <
typename T>
228 typename std::enable_if<std::numeric_limits<T>::is_integer && std::numeric_limits<T>::is_signed &&
237 return static_cast<T>(0);
254 return static_cast<T>(x *
y);
257 template <
typename T>
258 typename std::enable_if<std::numeric_limits<T>::is_integer && !std::numeric_limits<T>::is_signed &&
264 return static_cast<T>(x *
y);
268 template <
typename T>
272 typename std::enable_if<std::numeric_limits<T>::is_integer,
int>::
type = 0)
282 return static_cast<T>(x /
y);
285 template <
typename T>
286 typename std::enable_if<std::numeric_limits<T>::is_integer && std::numeric_limits<T>::is_signed,
291 return static_cast<T>(x %
y);
294 template <
typename T>
295 typename std::enable_if<std::numeric_limits<T>::is_integer && !std::numeric_limits<T>::is_signed,
300 return static_cast<T>(x %
y);
303 template <
typename T>
304 typename std::enable_if<std::numeric_limits<T>::is_integer && std::numeric_limits<T>::is_signed,
310 return static_cast<T>(-
value);
313 template <
typename T>
314 typename std::enable_if<std::numeric_limits<T>::is_integer && !std::numeric_limits<T>::is_signed,
323 template <
typename T>
324 typename std::enable_if<std::numeric_limits<T>::is_integer && std::numeric_limits<T>::is_signed,
332 template <
typename T>
333 typename std::enable_if<std::numeric_limits<T>::is_integer && !std::numeric_limits<T>::is_signed,
342 template <
typename T>
343 typename std::enable_if<std::numeric_limits<T>::is_integer && std::numeric_limits<T>::is_signed,
350 : static_cast<UnsignedT>(
std::abs(value));
353 template <
typename T>
354 typename std::enable_if<std::numeric_limits<T>::is_integer && !std::numeric_limits<T>::is_signed,
359 return static_cast<T>(
value);
364 #define BASE_FLOAT_ARITHMETIC_STUBS(NAME) \ 365 template <typename T> \ 366 typename std::enable_if<std::numeric_limits<T>::is_iec559, T>::type Checked##NAME( \ 367 T, T, RangeConstraint *) \ 370 return static_cast<T>(0); \ 379 #undef BASE_FLOAT_ARITHMETIC_STUBS 381 template <
typename T>
385 return static_cast<T>(-
value);
388 template <
typename T>
405 template <
typename NumericType>
409 std::numeric_limits<NumericType>::is_integer
420 template <
typename T>
428 template <
typename Src, NumericRepresentation type>
433 template <
typename Src>
435 : value_(static_cast<
T>(value)),
438 static_assert(std::numeric_limits<Src>::is_specialized,
"Argument must be numeric.");
442 template <
typename Src>
444 : value_(static_cast<
T>(rhs.
value())),
449 template <
typename Src>
452 typename std::enable_if<std::numeric_limits<Src>::is_specialized,
int>::
type = 0)
462 template <
typename T>
469 template <
typename Src, NumericRepresentation type>
474 template <
typename Src>
478 typename std::enable_if<std::numeric_limits<Src>::is_integer,
int>::
type = 0)
480 switch (DstRangeRelationToSrcRange<T>(value))
483 value_ =
static_cast<T>(
value);
487 value_ = -std::numeric_limits<T>::infinity();
491 value_ = std::numeric_limits<T>::infinity();
495 value_ = std::numeric_limits<T>::quiet_NaN();
503 template <
typename Src>
506 typename std::enable_if<std::numeric_limits<Src>::is_specialized,
int>::
type = 0)
507 : value_(static_cast<
T>(value))
512 template <
typename Src>
533 template <
typename Lhs,
540 template <
typename Lhs,
typename Rhs>
546 template <
typename Lhs,
typename Rhs>
556 template <
typename T,
typename Lhs,
typename Rhs>
560 !std::numeric_limits<T>::is_iec559 &&
562 sizeof(
T) >= (2 *
sizeof(Lhs)) &&
564 sizeof(
T) >= (2 *
sizeof(Rhs));
570 #endif // BASE_NUMERICS_SAFE_MATH_IMPL_H_ Definition: safe_conversions_impl.h:98
Definition: safe_math_impl.h:104
std::enable_if< std::numeric_limits< T >::is_integer, T >::type CheckedSub(T x, T y, RangeConstraint *validity)
Definition: safe_math_impl.h:184
#define BASE_FLOAT_ARITHMETIC_STUBS(NAME)
Definition: safe_math_impl.h:364
RangeConstraint
Definition: safe_conversions_impl.h:95
unsigned long long uint64_t
Definition: ptypes.h:120
Definition: safe_math_impl.h:415
uint64_t type
Definition: safe_math_impl.h:69
constexpr RangeConstraint DstRangeRelationToSrcRange(Src value)
Definition: safe_conversions_impl.h:259
int32_t type
Definition: safe_math_impl.h:54
signed int int32_t
Definition: ptypes.h:101
Definition: safe_math_impl.h:401
unsigned int uint32_t
Definition: ptypes.h:105
CheckedNumericState(Src value, typename std::enable_if< std::numeric_limits< Src >::is_specialized, int >::type=0)
Definition: safe_math_impl.h:450
#define NOTREACHED()
Definition: logging.h:19
std::enable_if< std::numeric_limits< T >::is_integer &&std::numeric_limits< T >::is_signed, T >::type CheckedMod(T x, T y, RangeConstraint *validity)
Definition: safe_math_impl.h:288
std::enable_if< std::numeric_limits< T >::is_integer &&!std::numeric_limits< T >::is_signed &&(sizeof(T) *2 > sizeof(uintmax_t)), T >::type CheckedMul(T x, T y, RangeConstraint *validity)
Definition: safe_math_impl.h:261
else * validity
Definition: safe_math_impl.h:251
NumericRepresentation
Definition: safe_math_impl.h:398
return static_cast< T >(x *y)
bool IsInteger(TBasicType type)
Definition: BaseTypes.h:279
T CheckedDiv(T x, T y, RangeConstraint *validity, typename std::enable_if< std::numeric_limits< T >::is_integer, int >::type=0)
Definition: safe_math_impl.h:269
signed long long int64_t
Definition: ptypes.h:112
std::enable_if< std::numeric_limits< T >::is_integer, T >::type CheckedAdd(T x, T y, RangeConstraint *validity)
Definition: safe_math_impl.h:154
signed short int16_t
Definition: ptypes.h:93
int16_t type
Definition: safe_math_impl.h:44
uint8_t type
Definition: safe_math_impl.h:39
RangeConstraint validity() const
Definition: safe_math_impl.h:517
Definition: safe_math_impl.h:402
Definition: safe_math_impl.h:30
CheckedNumericState(Src value, typename std::enable_if< std::numeric_limits< Src >::is_specialized, int >::type=0)
Definition: safe_math_impl.h:504
RangeConstraint validity() const
Definition: safe_math_impl.h:457
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
CheckedNumericState(const CheckedNumericState< Src > &rhs)
Definition: safe_math_impl.h:513
EGLSurface EGLint x
Definition: eglext.h:950
Definition: safe_conversions_impl.h:23
EGLAttrib * value
Definition: eglext.h:120
std::enable_if< std::numeric_limits< Integer >::is_integer, typename IntegerForSizeAndSign< sizeof(Integer), true >::type >::type type
Definition: safe_math_impl.h:91
unsigned char uint8_t
Definition: ptypes.h:89
Definition: safe_math_impl.h:406
Definition: safe_math_impl.h:400
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
unsigned short uint16_t
Definition: ptypes.h:97
uint32_t type
Definition: safe_math_impl.h:59
UnsignedIntegerForSize< Numeric >::type type
Definition: safe_math_impl.h:122
EGLSurface EGLint EGLint y
Definition: eglext.h:950
Definition: safe_math_impl.h:86
#define true
Definition: float-mm.c:6
uint16_t type
Definition: safe_math_impl.h:49
Definition: safe_math_impl.h:530
CheckedNumericState()
Definition: safe_math_impl.h:472
Definition: safe_conversions.h:16
EGLenum type
Definition: eglext.h:63
Definition: document.h:393
Definition: safe_math_impl.h:557
double max
Definition: DeviceProximityEvent.idl:32
#define min(a, b)
Definition: user_environment.h:66
uint64_t uintmax_t
Definition: stdint.h:166
CheckedNumericState(Src value, RangeConstraint validity)
Definition: safe_math_impl.h:434
#define false
Definition: float-mm.c:5
int8_t type
Definition: safe_math_impl.h:34
constexpr bool HasSignBit(T x)
Definition: safe_math_impl.h:134
T value() const
Definition: safe_math_impl.h:522
Definition: safe_math_impl.h:117
Definition: safe_conversions_impl.h:46
Definition: safe_conversions_impl.h:97
signed char int8_t
Definition: ptypes.h:85
Definition: safe_conversions_impl.h:100
Definition: safe_conversions_impl.h:99
CheckedNumericState(Src value, RangeConstraint validity, typename std::enable_if< std::numeric_limits< Src >::is_integer, int >::type=0)
Definition: safe_math_impl.h:475
constexpr T BinaryComplement(T x)
Definition: safe_math_impl.h:143
T value() const
Definition: safe_math_impl.h:458
Definition: safe_math_impl.h:77
constexpr RangeConstraint GetRangeConstraint(int integer_range_constraint)
Definition: safe_conversions_impl.h:104
int64_t type
Definition: safe_math_impl.h:64
ArithmeticPromotionCategory
Definition: safe_math_impl.h:527
std::enable_if< std::numeric_limits< Integer >::is_integer, typename IntegerForSizeAndSign< sizeof(Integer) *2, std::numeric_limits< Integer >::is_signed >::type >::type type
Definition: safe_math_impl.h:100
#define abs(a)
Definition: device_info_impl.cc:19
Definition: safe_conversions_impl.h:60
Numeric type
Definition: safe_math_impl.h:128
#define T(a)
Definition: row_common.cc:1964
std::enable_if< std::numeric_limits< Integer >::is_integer, typename IntegerForSizeAndSign< sizeof(Integer), false >::type >::type type
Definition: safe_math_impl.h:82
CheckedNumericState()
Definition: safe_math_impl.h:431
Definition: safe_math_impl.h:529
CheckedNumericState(const CheckedNumericState< Src > &rhs)
Definition: safe_math_impl.h:443
Definition: safe_math_impl.h:95