13 #ifndef UTIL_MATH_MATHLIMITS_H__ 14 #define UTIL_MATH_MATHLIMITS_H__ 19 #include "base/integral_types.h" 31 typedef T UnsignedType;
33 static const bool kIsSigned;
35 static const bool kIsInteger;
37 static const Type kPosMin;
39 static const Type kPosMax;
41 static const Type kMin;
43 static const Type kMax;
46 static const Type kNegMin;
49 static const Type kNegMax;
51 static const int kMin10Exp;
53 static const int kMax10Exp;
55 static const Type kEpsilon;
60 static const Type kStdError;
63 static const int kPrecisionDigits;
66 static const Type kNaN;
69 static const Type kPosInf;
72 static const Type kNegInf;
81 static bool IsFinite(
const Type x);
82 static bool IsNaN(
const Type x);
83 static bool IsInf(
const Type x);
84 static bool IsPosInf(
const Type x);
85 static bool IsNegInf(
const Type x);
97 #define SIGNED_INT_MAX(Type) \ 98 (((Type(1) << (sizeof(Type)*8 - 2)) - 1) + (Type(1) << (sizeof(Type)*8 - 2))) 100 #define SIGNED_INT_MIN(Type) \ 101 (-(Type(1) << (sizeof(Type)*8 - 2)) - (Type(1) << (sizeof(Type)*8 - 2))) 103 #define UNSIGNED_INT_MAX(Type) \ 104 (((Type(1) << (sizeof(Type)*8 - 1)) - 1) + (Type(1) << (sizeof(Type)*8 - 1))) 107 #define SIGNED_MAX_10_EXP(Type) \ 108 (sizeof(Type) == 1 ? 2 : ( \ 109 sizeof(Type) == 2 ? 4 : ( \ 110 sizeof(Type) == 4 ? 9 : ( \ 111 sizeof(Type) == 8 ? 18 : -1)))) 113 #define UNSIGNED_MAX_10_EXP(Type) \ 114 (sizeof(Type) == 1 ? 2 : ( \ 115 sizeof(Type) == 2 ? 4 : ( \ 116 sizeof(Type) == 4 ? 9 : ( \ 117 sizeof(Type) == 8 ? 19 : -1)))) 119 #define DECL_INT_LIMIT_FUNCS \ 120 static bool IsFinite(const Type ) { return true; } \ 121 static bool IsNaN(const Type ) { return false; } \ 122 static bool IsInf(const Type ) { return false; } \ 123 static bool IsPosInf(const Type ) { return false; } \ 124 static bool IsNegInf(const Type ) { return false; } 126 #define DECL_SIGNED_INT_LIMITS(IntType, UnsignedIntType) \ 128 struct MathLimits<IntType> { \ 129 typedef IntType Type; \ 130 typedef UnsignedIntType UnsignedType; \ 131 static const bool kIsSigned = true; \ 132 static const bool kIsInteger = true; \ 133 static const Type kPosMin = 1; \ 134 static const Type kPosMax = SIGNED_INT_MAX(Type); \ 135 static const Type kMin = SIGNED_INT_MIN(Type); \ 136 static const Type kMax = kPosMax; \ 137 static const Type kNegMin = -1; \ 138 static const Type kNegMax = kMin; \ 139 static const int kMin10Exp = 0; \ 140 static const int kMax10Exp = SIGNED_MAX_10_EXP(Type); \ 141 static const Type kEpsilon = 1; \ 142 static const Type kStdError = 0; \ 143 DECL_INT_LIMIT_FUNCS \ 146 #define DECL_UNSIGNED_INT_LIMITS(IntType) \ 148 struct MathLimits<IntType> { \ 149 typedef IntType Type; \ 150 typedef IntType UnsignedType; \ 151 static const bool kIsSigned = false; \ 152 static const bool kIsInteger = true; \ 153 static const Type kPosMin = 1; \ 154 static const Type kPosMax = UNSIGNED_INT_MAX(Type); \ 155 static const Type kMin = 0; \ 156 static const Type kMax = kPosMax; \ 157 static const int kMin10Exp = 0; \ 158 static const int kMax10Exp = UNSIGNED_MAX_10_EXP(Type); \ 159 static const Type kEpsilon = 1; \ 160 static const Type kStdError = 0; \ 161 DECL_INT_LIMIT_FUNCS \ 164 DECL_SIGNED_INT_LIMITS(
signed char,
unsigned char)
165 DECL_SIGNED_INT_LIMITS(
signed short int,
unsigned short int)
166 DECL_SIGNED_INT_LIMITS(
signed int,
unsigned int)
167 DECL_SIGNED_INT_LIMITS(
signed long int,
unsigned long int)
168 DECL_SIGNED_INT_LIMITS(
signed long long int,
unsigned long long int)
169 DECL_UNSIGNED_INT_LIMITS(
unsigned char)
170 DECL_UNSIGNED_INT_LIMITS(
unsigned short int)
171 DECL_UNSIGNED_INT_LIMITS(
unsigned int)
172 DECL_UNSIGNED_INT_LIMITS(
unsigned long int)
173 DECL_UNSIGNED_INT_LIMITS(
unsigned long long int)
175 #undef DECL_SIGNED_INT_LIMITS 176 #undef DECL_UNSIGNED_INT_LIMITS 177 #undef SIGNED_INT_MAX 178 #undef SIGNED_INT_MIN 179 #undef UNSIGNED_INT_MAX 180 #undef SIGNED_MAX_10_EXP 181 #undef UNSIGNED_MAX_10_EXP 182 #undef DECL_INT_LIMIT_FUNCS 185 #ifdef WIN32 // Lacks built-in isnan() and std::isinf() 186 #define DECL_FP_LIMIT_FUNCS \ 187 static bool IsFinite(const Type x) { return _finite(x); } \ 188 static bool IsNaN(const Type x) { return _isnan(x); } \ 189 static bool IsInf(const Type x) { return (_fpclass(x) & (_FPCLASS_NINF | _FPCLASS_PINF)) != 0; } \ 190 static bool IsPosInf(const Type x) { return _fpclass(x) == _FPCLASS_PINF; } \ 191 static bool IsNegInf(const Type x) { return _fpclass(x) == _FPCLASS_NINF; } 193 #define DECL_FP_LIMIT_FUNCS \ 194 static bool IsFinite(const Type x) { return !std::isinf(x) && !std::isnan(x); } \ 195 static bool IsNaN(const Type x) { return std::isnan(x); } \ 196 static bool IsInf(const Type x) { return std::isinf(x); } \ 197 static bool IsPosInf(const Type x) { return std::isinf(x) && x > 0; } \ 198 static bool IsNegInf(const Type x) { return std::isinf(x) && x < 0; } 205 #define DECL_FP_LIMITS(FP_Type, PREFIX) \ 207 struct MathLimits<FP_Type> { \ 208 typedef FP_Type Type; \ 209 typedef FP_Type UnsignedType; \ 210 static const bool kIsSigned = true; \ 211 static const bool kIsInteger = false; \ 212 static const Type kPosMin; \ 213 static const Type kPosMax; \ 214 static const Type kMin; \ 215 static const Type kMax; \ 216 static const Type kNegMin; \ 217 static const Type kNegMax; \ 218 static const int kMin10Exp = PREFIX##_MIN_10_EXP; \ 219 static const int kMax10Exp = PREFIX##_MAX_10_EXP; \ 220 static const Type kEpsilon; \ 221 static const Type kStdError; \ 222 static const int kPrecisionDigits = PREFIX##_DIG; \ 223 static const Type kNaN; \ 224 static const Type kPosInf; \ 225 static const Type kNegInf; \ 226 DECL_FP_LIMIT_FUNCS \ 229 DECL_FP_LIMITS(
float, FLT)
230 DECL_FP_LIMITS(
double, DBL)
231 DECL_FP_LIMITS(
long double, LDBL)
233 #undef DECL_FP_LIMITS 234 #undef DECL_FP_LIMIT_FUNCS 238 #endif // UTIL_MATH_MATHLIMITS_H__