aboutsummaryrefslogtreecommitdiff
path: root/src/google/protobuf/stubs/mathlimits.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/google/protobuf/stubs/mathlimits.h')
-rw-r--r--src/google/protobuf/stubs/mathlimits.h46
1 files changed, 35 insertions, 11 deletions
diff --git a/src/google/protobuf/stubs/mathlimits.h b/src/google/protobuf/stubs/mathlimits.h
index d9846940..9c9d0e9a 100644
--- a/src/google/protobuf/stubs/mathlimits.h
+++ b/src/google/protobuf/stubs/mathlimits.h
@@ -43,12 +43,23 @@
#ifndef UTIL_MATH_MATHLIMITS_H__
#define UTIL_MATH_MATHLIMITS_H__
-// <math.h> lacks a lot of prototypes. However, this file needs <math.h> to
-// access old-fashioned isinf et al. Even worse more: this file must not
-// include <cmath> because that breaks the definition of isinf with gcc 4.9.
-//
-// TODO(mec): after C++11 everywhere, use <cmath> and std::isinf in this file.
+// Note that for Windows we do something different because it does not support
+// the plain isinf and isnan.
+#if __cplusplus >= 201103L
+// GCC 4.9 has a bug that makes isinf and isnan ambigious when both <math.h>
+// and <cmath> get pulled into the same translation unit. We use the ones in
+// std:: namespace explicitly for C++11
+#include <cmath>
+#define GOOGLE_PROTOBUF_USE_STD_CMATH
+#elif _GLIBCXX_USE_C99_MATH && !_GLIBCXX_USE_C99_FP_MACROS_DYNAMIC
+// libstdc++ <cmath> header undefines the global macros and put functions in
+// std:: namespace even before C++11. Use the ones in std:: instead too.
+#include <cmath>
+#define GOOGLE_PROTOBUF_USE_STD_CMATH
+#else
#include <math.h>
+#endif
+
#include <string.h>
#include <cfloat>
@@ -220,8 +231,19 @@ DECL_UNSIGNED_INT_LIMITS(unsigned long long int)
#undef UNSIGNED_MAX_10_EXP
#undef DECL_INT_LIMIT_FUNCS
+// For non-Windows builds we use the std:: versions of isinf and isnan if they
+// are available; see the comment about <cmath> at the top of this file for the
+// details on why we need to do this.
+#ifdef GOOGLE_PROTOBUF_USE_STD_CMATH
+#define ISINF std::isinf
+#define ISNAN std::isnan
+#else
+#define ISINF isinf
+#define ISNAN isnan
+#endif
+
// ========================================================================= //
-#ifdef WIN32 // Lacks built-in isnan() and isinf()
+#if WIN32 && !__MINGW32__ // Lacks built-in isnan() and isinf()
#define DECL_FP_LIMIT_FUNCS \
static bool IsFinite(const Type x) { return _finite(x); } \
static bool IsNaN(const Type x) { return _isnan(x); } \
@@ -230,11 +252,11 @@ DECL_UNSIGNED_INT_LIMITS(unsigned long long int)
static bool IsNegInf(const Type x) { return _fpclass(x) == _FPCLASS_NINF; }
#else
#define DECL_FP_LIMIT_FUNCS \
- static bool IsFinite(const Type x) { return !isinf(x) && !isnan(x); } \
- static bool IsNaN(const Type x) { return isnan(x); } \
- static bool IsInf(const Type x) { return isinf(x); } \
- static bool IsPosInf(const Type x) { return isinf(x) && x > 0; } \
- static bool IsNegInf(const Type x) { return isinf(x) && x < 0; }
+ static bool IsFinite(const Type x) { return !ISINF(x) && !ISNAN(x); } \
+ static bool IsNaN(const Type x) { return ISNAN(x); } \
+ static bool IsInf(const Type x) { return ISINF(x); } \
+ static bool IsPosInf(const Type x) { return ISINF(x) && x > 0; } \
+ static bool IsNegInf(const Type x) { return ISINF(x) && x < 0; }
#endif
// We can't put floating-point constant values in the header here because
@@ -269,6 +291,8 @@ DECL_FP_LIMITS(float, FLT)
DECL_FP_LIMITS(double, DBL)
DECL_FP_LIMITS(long double, LDBL)
+#undef ISINF
+#undef ISNAN
#undef DECL_FP_LIMITS
#undef DECL_FP_LIMIT_FUNCS