OLD | NEW |
1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
4 // | 4 // |
5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
7 // met: | 7 // met: |
8 // | 8 // |
9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
(...skipping 25 matching lines...) Expand all Loading... |
36 // | 36 // |
37 // This partially replaces/duplictes numeric_limits<> from <limits>. | 37 // This partially replaces/duplictes numeric_limits<> from <limits>. |
38 // We get a Google-style class that we have a greater control over | 38 // We get a Google-style class that we have a greater control over |
39 // and thus can add new features to it or fix whatever happens to be broken in | 39 // and thus can add new features to it or fix whatever happens to be broken in |
40 // numeric_limits for the compilers we use. | 40 // numeric_limits for the compilers we use. |
41 // | 41 // |
42 | 42 |
43 #ifndef UTIL_MATH_MATHLIMITS_H__ | 43 #ifndef UTIL_MATH_MATHLIMITS_H__ |
44 #define UTIL_MATH_MATHLIMITS_H__ | 44 #define UTIL_MATH_MATHLIMITS_H__ |
45 | 45 |
46 // GCC 4.9 has a bug that makes it impossible to use isinf and isnan when both | 46 // <math.h> lacks a lot of prototypes. However, this file needs <math.h> to |
47 // <math.h> and <cmath> get pulled into the same translation unit. | 47 // access old-fashioned isinf et al. Even worse more: this file must not |
48 // Unfortunately it is difficult to prevent this from happening, so to work | 48 // include <cmath> because that breaks the definition of isinf with gcc 4.9. |
49 // around the problem we use std::isinf and std::isnan from <cmath> for C++11 | 49 // |
50 // builds and otherwise use the plain isinf and isnan functions from <math.h>. | 50 // TODO(mec): after C++11 everywhere, use <cmath> and std::isinf in this file. |
51 // Note that for Windows we do something different because it does not support | |
52 // the plain isinf and isnan. | |
53 #if __cplusplus >= 201103L | |
54 #include <cmath> | |
55 #else | |
56 #include <math.h> | 51 #include <math.h> |
57 #endif | |
58 #include <string.h> | 52 #include <string.h> |
59 | 53 |
60 #include <cfloat> | 54 #include <cfloat> |
61 | 55 |
62 #include <google/protobuf/stubs/common.h> | 56 #include <google/protobuf/stubs/common.h> |
63 | 57 |
64 // ========================================================================= // | 58 // ========================================================================= // |
65 | 59 |
66 // Useful integer and floating point limits and type traits. | 60 // Useful integer and floating point limits and type traits. |
67 // This is just for the documentation; | 61 // This is just for the documentation; |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
219 | 213 |
220 #undef DECL_SIGNED_INT_LIMITS | 214 #undef DECL_SIGNED_INT_LIMITS |
221 #undef DECL_UNSIGNED_INT_LIMITS | 215 #undef DECL_UNSIGNED_INT_LIMITS |
222 #undef SIGNED_INT_MAX | 216 #undef SIGNED_INT_MAX |
223 #undef SIGNED_INT_MIN | 217 #undef SIGNED_INT_MIN |
224 #undef UNSIGNED_INT_MAX | 218 #undef UNSIGNED_INT_MAX |
225 #undef SIGNED_MAX_10_EXP | 219 #undef SIGNED_MAX_10_EXP |
226 #undef UNSIGNED_MAX_10_EXP | 220 #undef UNSIGNED_MAX_10_EXP |
227 #undef DECL_INT_LIMIT_FUNCS | 221 #undef DECL_INT_LIMIT_FUNCS |
228 | 222 |
229 // For non-Windows builds we use the std:: versions of isinf and isnan if they | |
230 // are available; see the comment about <cmath> at the top of this file for the | |
231 // details on why we need to do this. | |
232 #if __cplusplus >= 201103L | |
233 #define ISINF std::isinf | |
234 #define ISNAN std::isnan | |
235 #else | |
236 #define ISINF isinf | |
237 #define ISNAN isnan | |
238 #endif | |
239 | |
240 // ========================================================================= // | 223 // ========================================================================= // |
241 #ifdef WIN32 // Lacks built-in isnan() and isinf() | 224 #ifdef WIN32 // Lacks built-in isnan() and isinf() |
242 #define DECL_FP_LIMIT_FUNCS \ | 225 #define DECL_FP_LIMIT_FUNCS \ |
243 static bool IsFinite(const Type x) { return _finite(x); } \ | 226 static bool IsFinite(const Type x) { return _finite(x); } \ |
244 static bool IsNaN(const Type x) { return _isnan(x); } \ | 227 static bool IsNaN(const Type x) { return _isnan(x); } \ |
245 static bool IsInf(const Type x) { return (_fpclass(x) & (_FPCLASS_NINF | _FPCL
ASS_PINF)) != 0; } \ | 228 static bool IsInf(const Type x) { return (_fpclass(x) & (_FPCLASS_NINF | _FPCL
ASS_PINF)) != 0; } \ |
246 static bool IsPosInf(const Type x) { return _fpclass(x) == _FPCLASS_PINF; } \ | 229 static bool IsPosInf(const Type x) { return _fpclass(x) == _FPCLASS_PINF; } \ |
247 static bool IsNegInf(const Type x) { return _fpclass(x) == _FPCLASS_NINF; } | 230 static bool IsNegInf(const Type x) { return _fpclass(x) == _FPCLASS_NINF; } |
248 #else | 231 #else |
249 #define DECL_FP_LIMIT_FUNCS \ | 232 #define DECL_FP_LIMIT_FUNCS \ |
250 static bool IsFinite(const Type x) { return !ISINF(x) && !ISNAN(x); } \ | 233 static bool IsFinite(const Type x) { return !isinf(x) && !isnan(x); } \ |
251 static bool IsNaN(const Type x) { return ISNAN(x); } \ | 234 static bool IsNaN(const Type x) { return isnan(x); } \ |
252 static bool IsInf(const Type x) { return ISINF(x); } \ | 235 static bool IsInf(const Type x) { return isinf(x); } \ |
253 static bool IsPosInf(const Type x) { return ISINF(x) && x > 0; } \ | 236 static bool IsPosInf(const Type x) { return isinf(x) && x > 0; } \ |
254 static bool IsNegInf(const Type x) { return ISINF(x) && x < 0; } | 237 static bool IsNegInf(const Type x) { return isinf(x) && x < 0; } |
255 #endif | 238 #endif |
256 | 239 |
257 // We can't put floating-point constant values in the header here because | 240 // We can't put floating-point constant values in the header here because |
258 // such constants are not considered to be primitive-type constants by gcc. | 241 // such constants are not considered to be primitive-type constants by gcc. |
259 // CAVEAT: Hence, they are going to be initialized only during | 242 // CAVEAT: Hence, they are going to be initialized only during |
260 // the global objects construction time. | 243 // the global objects construction time. |
261 #define DECL_FP_LIMITS(FP_Type, PREFIX) \ | 244 #define DECL_FP_LIMITS(FP_Type, PREFIX) \ |
262 template<> \ | 245 template<> \ |
263 struct LIBPROTOBUF_EXPORT MathLimits<FP_Type> { \ | 246 struct LIBPROTOBUF_EXPORT MathLimits<FP_Type> { \ |
264 typedef FP_Type Type; \ | 247 typedef FP_Type Type; \ |
(...skipping 14 matching lines...) Expand all Loading... |
279 static const Type kNaN; \ | 262 static const Type kNaN; \ |
280 static const Type kPosInf; \ | 263 static const Type kPosInf; \ |
281 static const Type kNegInf; \ | 264 static const Type kNegInf; \ |
282 DECL_FP_LIMIT_FUNCS \ | 265 DECL_FP_LIMIT_FUNCS \ |
283 }; | 266 }; |
284 | 267 |
285 DECL_FP_LIMITS(float, FLT) | 268 DECL_FP_LIMITS(float, FLT) |
286 DECL_FP_LIMITS(double, DBL) | 269 DECL_FP_LIMITS(double, DBL) |
287 DECL_FP_LIMITS(long double, LDBL) | 270 DECL_FP_LIMITS(long double, LDBL) |
288 | 271 |
289 #undef ISINF | |
290 #undef ISNAN | |
291 #undef DECL_FP_LIMITS | 272 #undef DECL_FP_LIMITS |
292 #undef DECL_FP_LIMIT_FUNCS | 273 #undef DECL_FP_LIMIT_FUNCS |
293 | 274 |
294 // ========================================================================= // | 275 // ========================================================================= // |
295 } // namespace protobuf | 276 } // namespace protobuf |
296 } // namespace google | 277 } // namespace google |
297 | 278 |
298 #endif // UTIL_MATH_MATHLIMITS_H__ | 279 #endif // UTIL_MATH_MATHLIMITS_H__ |
OLD | NEW |