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 // <math.h> lacks a lot of prototypes. However, this file needs <math.h> to | 46 // GCC 4.9 has a bug that makes it impossible to use isinf and isnan when both |
47 // access old-fashioned isinf et al. Even worse more: this file must not | 47 // <math.h> and <cmath> get pulled into the same translation unit. |
48 // include <cmath> because that breaks the definition of isinf with gcc 4.9. | 48 // Unfortunately it is difficult to prevent this from happening, so to work |
49 // | 49 // around the problem we use std::isinf and std::isnan from <cmath> for C++11 |
50 // TODO(mec): after C++11 everywhere, use <cmath> and std::isinf in this file. | 50 // builds and otherwise use the plain isinf and isnan functions from <math.h>. |
| 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 |
51 #include <math.h> | 56 #include <math.h> |
| 57 #endif |
52 #include <string.h> | 58 #include <string.h> |
53 | 59 |
54 #include <cfloat> | 60 #include <cfloat> |
55 | 61 |
56 #include <google/protobuf/stubs/common.h> | 62 #include <google/protobuf/stubs/common.h> |
57 | 63 |
58 // ========================================================================= // | 64 // ========================================================================= // |
59 | 65 |
60 // Useful integer and floating point limits and type traits. | 66 // Useful integer and floating point limits and type traits. |
61 // This is just for the documentation; | 67 // This is just for the documentation; |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
213 | 219 |
214 #undef DECL_SIGNED_INT_LIMITS | 220 #undef DECL_SIGNED_INT_LIMITS |
215 #undef DECL_UNSIGNED_INT_LIMITS | 221 #undef DECL_UNSIGNED_INT_LIMITS |
216 #undef SIGNED_INT_MAX | 222 #undef SIGNED_INT_MAX |
217 #undef SIGNED_INT_MIN | 223 #undef SIGNED_INT_MIN |
218 #undef UNSIGNED_INT_MAX | 224 #undef UNSIGNED_INT_MAX |
219 #undef SIGNED_MAX_10_EXP | 225 #undef SIGNED_MAX_10_EXP |
220 #undef UNSIGNED_MAX_10_EXP | 226 #undef UNSIGNED_MAX_10_EXP |
221 #undef DECL_INT_LIMIT_FUNCS | 227 #undef DECL_INT_LIMIT_FUNCS |
222 | 228 |
| 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 |
223 // ========================================================================= // | 240 // ========================================================================= // |
224 #ifdef WIN32 // Lacks built-in isnan() and isinf() | 241 #ifdef WIN32 // Lacks built-in isnan() and isinf() |
225 #define DECL_FP_LIMIT_FUNCS \ | 242 #define DECL_FP_LIMIT_FUNCS \ |
226 static bool IsFinite(const Type x) { return _finite(x); } \ | 243 static bool IsFinite(const Type x) { return _finite(x); } \ |
227 static bool IsNaN(const Type x) { return _isnan(x); } \ | 244 static bool IsNaN(const Type x) { return _isnan(x); } \ |
228 static bool IsInf(const Type x) { return (_fpclass(x) & (_FPCLASS_NINF | _FPCL
ASS_PINF)) != 0; } \ | 245 static bool IsInf(const Type x) { return (_fpclass(x) & (_FPCLASS_NINF | _FPCL
ASS_PINF)) != 0; } \ |
229 static bool IsPosInf(const Type x) { return _fpclass(x) == _FPCLASS_PINF; } \ | 246 static bool IsPosInf(const Type x) { return _fpclass(x) == _FPCLASS_PINF; } \ |
230 static bool IsNegInf(const Type x) { return _fpclass(x) == _FPCLASS_NINF; } | 247 static bool IsNegInf(const Type x) { return _fpclass(x) == _FPCLASS_NINF; } |
231 #else | 248 #else |
232 #define DECL_FP_LIMIT_FUNCS \ | 249 #define DECL_FP_LIMIT_FUNCS \ |
233 static bool IsFinite(const Type x) { return !isinf(x) && !isnan(x); } \ | 250 static bool IsFinite(const Type x) { return !ISINF(x) && !ISNAN(x); } \ |
234 static bool IsNaN(const Type x) { return isnan(x); } \ | 251 static bool IsNaN(const Type x) { return ISNAN(x); } \ |
235 static bool IsInf(const Type x) { return isinf(x); } \ | 252 static bool IsInf(const Type x) { return ISINF(x); } \ |
236 static bool IsPosInf(const Type x) { return isinf(x) && x > 0; } \ | 253 static bool IsPosInf(const Type x) { return ISINF(x) && x > 0; } \ |
237 static bool IsNegInf(const Type x) { return isinf(x) && x < 0; } | 254 static bool IsNegInf(const Type x) { return ISINF(x) && x < 0; } |
238 #endif | 255 #endif |
239 | 256 |
240 // We can't put floating-point constant values in the header here because | 257 // We can't put floating-point constant values in the header here because |
241 // such constants are not considered to be primitive-type constants by gcc. | 258 // such constants are not considered to be primitive-type constants by gcc. |
242 // CAVEAT: Hence, they are going to be initialized only during | 259 // CAVEAT: Hence, they are going to be initialized only during |
243 // the global objects construction time. | 260 // the global objects construction time. |
244 #define DECL_FP_LIMITS(FP_Type, PREFIX) \ | 261 #define DECL_FP_LIMITS(FP_Type, PREFIX) \ |
245 template<> \ | 262 template<> \ |
246 struct LIBPROTOBUF_EXPORT MathLimits<FP_Type> { \ | 263 struct LIBPROTOBUF_EXPORT MathLimits<FP_Type> { \ |
247 typedef FP_Type Type; \ | 264 typedef FP_Type Type; \ |
(...skipping 14 matching lines...) Expand all Loading... |
262 static const Type kNaN; \ | 279 static const Type kNaN; \ |
263 static const Type kPosInf; \ | 280 static const Type kPosInf; \ |
264 static const Type kNegInf; \ | 281 static const Type kNegInf; \ |
265 DECL_FP_LIMIT_FUNCS \ | 282 DECL_FP_LIMIT_FUNCS \ |
266 }; | 283 }; |
267 | 284 |
268 DECL_FP_LIMITS(float, FLT) | 285 DECL_FP_LIMITS(float, FLT) |
269 DECL_FP_LIMITS(double, DBL) | 286 DECL_FP_LIMITS(double, DBL) |
270 DECL_FP_LIMITS(long double, LDBL) | 287 DECL_FP_LIMITS(long double, LDBL) |
271 | 288 |
| 289 #undef ISINF |
| 290 #undef ISNAN |
272 #undef DECL_FP_LIMITS | 291 #undef DECL_FP_LIMITS |
273 #undef DECL_FP_LIMIT_FUNCS | 292 #undef DECL_FP_LIMIT_FUNCS |
274 | 293 |
275 // ========================================================================= // | 294 // ========================================================================= // |
276 } // namespace protobuf | 295 } // namespace protobuf |
277 } // namespace google | 296 } // namespace google |
278 | 297 |
279 #endif // UTIL_MATH_MATHLIMITS_H__ | 298 #endif // UTIL_MATH_MATHLIMITS_H__ |
OLD | NEW |