diff options
Diffstat (limited to 'third_party/base/numerics/safe_math_impl.h')
-rw-r--r-- | third_party/base/numerics/safe_math_impl.h | 111 |
1 files changed, 57 insertions, 54 deletions
diff --git a/third_party/base/numerics/safe_math_impl.h b/third_party/base/numerics/safe_math_impl.h index 4209446694..f219cf52bb 100644 --- a/third_party/base/numerics/safe_math_impl.h +++ b/third_party/base/numerics/safe_math_impl.h @@ -10,10 +10,10 @@ #include <cmath> #include <cstdlib> #include <limits> +#include <type_traits> #include "safe_conversions.h" #include "third_party/base/macros.h" -#include "third_party/base/template_util.h" namespace pdfium { namespace base { @@ -65,21 +65,21 @@ struct IntegerForSizeAndSign<8, false> { template <typename Integer> struct UnsignedIntegerForSize { - typedef typename enable_if< + typedef typename std::enable_if< std::numeric_limits<Integer>::is_integer, typename IntegerForSizeAndSign<sizeof(Integer), false>::type>::type type; }; template <typename Integer> struct SignedIntegerForSize { - typedef typename enable_if< + typedef typename std::enable_if< std::numeric_limits<Integer>::is_integer, typename IntegerForSizeAndSign<sizeof(Integer), true>::type>::type type; }; template <typename Integer> struct TwiceWiderInteger { - typedef typename enable_if< + typedef typename std::enable_if< std::numeric_limits<Integer>::is_integer, typename IntegerForSizeAndSign< sizeof(Integer) * 2, @@ -88,8 +88,9 @@ struct TwiceWiderInteger { template <typename Integer> struct PositionOfSignBit { - static const typename enable_if<std::numeric_limits<Integer>::is_integer, - size_t>::type value = 8 * sizeof(Integer) - 1; + static const typename std::enable_if<std::numeric_limits<Integer>::is_integer, + size_t>::type value = + 8 * sizeof(Integer) - 1; }; // Helper templates for integer manipulations. @@ -112,7 +113,7 @@ T BinaryComplement(T x) { // way to coalesce things into the CheckedNumericState specializations below. template <typename T> -typename enable_if<std::numeric_limits<T>::is_integer, T>::type +typename std::enable_if<std::numeric_limits<T>::is_integer, T>::type CheckedAdd(T x, T y, RangeConstraint* validity) { // Since the value of x+y is undefined if we have a signed type, we compute // it using the unsigned type of the same size. @@ -135,7 +136,7 @@ CheckedAdd(T x, T y, RangeConstraint* validity) { } template <typename T> -typename enable_if<std::numeric_limits<T>::is_integer, T>::type +typename std::enable_if<std::numeric_limits<T>::is_integer, T>::type CheckedSub(T x, T y, RangeConstraint* validity) { // Since the value of x+y is undefined if we have a signed type, we compute // it using the unsigned type of the same size. @@ -162,9 +163,9 @@ CheckedSub(T x, T y, RangeConstraint* validity) { // slow case we need to manually check that the result won't be truncated by // checking with division against the appropriate bound. template <typename T> -typename enable_if< - std::numeric_limits<T>::is_integer && sizeof(T) * 2 <= sizeof(uintmax_t), - T>::type +typename std::enable_if<std::numeric_limits<T>::is_integer && + sizeof(T) * 2 <= sizeof(uintmax_t), + T>::type CheckedMul(T x, T y, RangeConstraint* validity) { typedef typename TwiceWiderInteger<T>::type IntermediateType; IntermediateType tmp = @@ -174,9 +175,10 @@ CheckedMul(T x, T y, RangeConstraint* validity) { } template <typename T> -typename enable_if<std::numeric_limits<T>::is_integer&& std::numeric_limits< - T>::is_signed&&(sizeof(T) * 2 > sizeof(uintmax_t)), - T>::type +typename 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) { // If either side is zero then the result will be zero. if (!x || !y) { @@ -203,10 +205,10 @@ CheckedMul(T x, T y, RangeConstraint* validity) { } template <typename T> -typename enable_if<std::numeric_limits<T>::is_integer && - !std::numeric_limits<T>::is_signed && - (sizeof(T) * 2 > sizeof(uintmax_t)), - T>::type +typename 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) { *validity = (y == 0 || x <= std::numeric_limits<T>::max() / y) ? RANGE_VALID @@ -216,11 +218,11 @@ CheckedMul(T x, T y, RangeConstraint* validity) { // Division just requires a check for an invalid negation on signed min/-1. template <typename T> -T CheckedDiv( - T x, - T y, - RangeConstraint* validity, - typename enable_if<std::numeric_limits<T>::is_integer, int>::type = 0) { +T CheckedDiv(T x, + T y, + RangeConstraint* validity, + typename std::enable_if<std::numeric_limits<T>::is_integer, + int>::type = 0) { if (std::numeric_limits<T>::is_signed && x == std::numeric_limits<T>::min() && y == static_cast<T>(-1)) { *validity = RANGE_OVERFLOW; @@ -232,27 +234,27 @@ T CheckedDiv( } template <typename T> -typename enable_if< - std::numeric_limits<T>::is_integer&& std::numeric_limits<T>::is_signed, - T>::type +typename std::enable_if<std::numeric_limits<T>::is_integer && + std::numeric_limits<T>::is_signed, + T>::type CheckedMod(T x, T y, RangeConstraint* validity) { *validity = y > 0 ? RANGE_VALID : RANGE_INVALID; return x % y; } template <typename T> -typename enable_if< - std::numeric_limits<T>::is_integer && !std::numeric_limits<T>::is_signed, - T>::type +typename std::enable_if<std::numeric_limits<T>::is_integer && + !std::numeric_limits<T>::is_signed, + T>::type CheckedMod(T x, T y, RangeConstraint* validity) { *validity = RANGE_VALID; return x % y; } template <typename T> -typename enable_if< - std::numeric_limits<T>::is_integer&& std::numeric_limits<T>::is_signed, - T>::type +typename std::enable_if<std::numeric_limits<T>::is_integer && + std::numeric_limits<T>::is_signed, + T>::type CheckedNeg(T value, RangeConstraint* validity) { *validity = value != std::numeric_limits<T>::min() ? RANGE_VALID : RANGE_OVERFLOW; @@ -261,9 +263,9 @@ CheckedNeg(T value, RangeConstraint* validity) { } template <typename T> -typename enable_if< - std::numeric_limits<T>::is_integer && !std::numeric_limits<T>::is_signed, - T>::type +typename std::enable_if<std::numeric_limits<T>::is_integer && + !std::numeric_limits<T>::is_signed, + T>::type CheckedNeg(T value, RangeConstraint* validity) { // The only legal unsigned negation is zero. *validity = value ? RANGE_UNDERFLOW : RANGE_VALID; @@ -272,9 +274,9 @@ CheckedNeg(T value, RangeConstraint* validity) { } template <typename T> -typename enable_if< - std::numeric_limits<T>::is_integer&& std::numeric_limits<T>::is_signed, - T>::type +typename std::enable_if<std::numeric_limits<T>::is_integer && + std::numeric_limits<T>::is_signed, + T>::type CheckedAbs(T value, RangeConstraint* validity) { *validity = value != std::numeric_limits<T>::min() ? RANGE_VALID : RANGE_OVERFLOW; @@ -282,9 +284,9 @@ CheckedAbs(T value, RangeConstraint* validity) { } template <typename T> -typename enable_if< - std::numeric_limits<T>::is_integer && !std::numeric_limits<T>::is_signed, - T>::type +typename std::enable_if<std::numeric_limits<T>::is_integer && + !std::numeric_limits<T>::is_signed, + T>::type CheckedAbs(T value, RangeConstraint* validity) { // Absolute value of a positive is just its identiy. *validity = RANGE_VALID; @@ -293,12 +295,12 @@ CheckedAbs(T value, RangeConstraint* validity) { // These are the floating point stubs that the compiler needs to see. Only the // negation operation is ever called. -#define BASE_FLOAT_ARITHMETIC_STUBS(NAME) \ - template <typename T> \ - typename enable_if<std::numeric_limits<T>::is_iec559, T>::type \ - Checked##NAME(T, T, RangeConstraint*) { \ - NOTREACHED(); \ - return 0; \ +#define BASE_FLOAT_ARITHMETIC_STUBS(NAME) \ + template <typename T> \ + typename std::enable_if<std::numeric_limits<T>::is_iec559, T>::type \ + Checked##NAME(T, T, RangeConstraint*) { \ + NOTREACHED(); \ + return 0; \ } BASE_FLOAT_ARITHMETIC_STUBS(Add) @@ -310,14 +312,14 @@ BASE_FLOAT_ARITHMETIC_STUBS(Mod) #undef BASE_FLOAT_ARITHMETIC_STUBS template <typename T> -typename enable_if<std::numeric_limits<T>::is_iec559, T>::type CheckedNeg( +typename std::enable_if<std::numeric_limits<T>::is_iec559, T>::type CheckedNeg( T value, RangeConstraint*) { return -value; } template <typename T> -typename enable_if<std::numeric_limits<T>::is_iec559, T>::type CheckedAbs( +typename std::enable_if<std::numeric_limits<T>::is_iec559, T>::type CheckedAbs( T value, RangeConstraint*) { return std::abs(value); @@ -377,8 +379,8 @@ class CheckedNumericState<T, NUMERIC_INTEGER> { template <typename Src> explicit CheckedNumericState( Src value, - typename enable_if<std::numeric_limits<Src>::is_specialized, int>::type = - 0) + typename std::enable_if<std::numeric_limits<Src>::is_specialized, + int>::type = 0) : value_(static_cast<T>(value)), validity_(DstRangeRelationToSrcRange<T>(value)) {} @@ -402,7 +404,8 @@ class CheckedNumericState<T, NUMERIC_FLOATING> { CheckedNumericState( Src value, RangeConstraint validity, - typename enable_if<std::numeric_limits<Src>::is_integer, int>::type = 0) { + typename std::enable_if<std::numeric_limits<Src>::is_integer, int>::type = + 0) { switch (DstRangeRelationToSrcRange<T>(value)) { case RANGE_VALID: value_ = static_cast<T>(value); @@ -428,8 +431,8 @@ class CheckedNumericState<T, NUMERIC_FLOATING> { template <typename Src> explicit CheckedNumericState( Src value, - typename enable_if<std::numeric_limits<Src>::is_specialized, int>::type = - 0) + typename std::enable_if<std::numeric_limits<Src>::is_specialized, + int>::type = 0) : value_(static_cast<T>(value)) {} // Copy constructor. |