[libc++][math.h] move #undefs to the top and guard explicitly against MSVCRT instead
Reviewed By: ldionne, #libc
Spies: #libc_vendors, EricWF, libcxx-commits
Differential Revision: https://reviews.llvm.org/D137502
NOKEYCHECK=True
GitOrigin-RevId: ba1e13d5245977686d37b81ed31ad07a58e35721
diff --git a/include/math.h b/include/math.h
index 839be4d..b3990f3 100644
--- a/include/math.h
+++ b/include/math.h
@@ -312,11 +312,56 @@
#include <stdlib.h>
#include <type_traits>
-// signbit
+
+# ifdef fpclassify
+# undef fpclassify
+# endif
# ifdef signbit
# undef signbit
-# endif // signbit
+# endif
+
+# ifdef isfinite
+# undef isfinite
+# endif
+
+# ifdef isinf
+# undef isinf
+# endif
+
+# ifdef isnan
+# undef isnan
+# endif
+
+# ifdef isnormal
+# undef isnormal
+# endif
+
+# ifdef isgreater
+# undef isgreater
+# endif
+
+# ifdef isgreaterequal
+# undef isgreaterequal
+# endif
+
+# ifdef isless
+# undef isless
+# endif
+
+# ifdef islessequal
+# undef islessequal
+# endif
+
+# ifdef islessgreater
+# undef islessgreater
+# endif
+
+# ifdef isunordered
+# undef isunordered
+# endif
+
+// signbit
template <class _A1, std::__enable_if_t<std::is_floating_point<_A1>::value, int> = 0>
inline _LIBCPP_HIDE_FROM_ABI bool signbit(_A1 __x) _NOEXCEPT {
@@ -335,10 +380,6 @@
// fpclassify
-# ifdef fpclassify
-# undef fpclassify
-# endif // fpclassify
-
template <class _A1, std::__enable_if_t<std::is_floating_point<_A1>::value, int> = 0>
inline _LIBCPP_HIDE_FROM_ABI int fpclassify(_A1 __x) _NOEXCEPT {
return __builtin_fpclassify(FP_NAN, FP_INFINITE, FP_NORMAL, FP_SUBNORMAL, FP_ZERO, __x);
@@ -349,10 +390,10 @@
return __x == 0 ? FP_ZERO : FP_NORMAL;
}
-// isfinite
+// The MSVC runtime already provides these functions as templates
+#ifndef _LIBCPP_MSVCRT
-# ifdef isfinite
-# undef isfinite
+// isfinite
template <class _A1,
std::__enable_if_t<std::is_arithmetic<_A1>::value && std::numeric_limits<_A1>::has_infinity, int> = 0>
@@ -366,13 +407,8 @@
return true;
}
-# endif // isfinite
-
// isinf
-# ifdef isinf
-# undef isinf
-
template <class _A1,
std::__enable_if_t<std::is_arithmetic<_A1>::value && std::numeric_limits<_A1>::has_infinity, int> = 0>
inline _LIBCPP_HIDE_FROM_ABI bool isinf(_A1 __x) _NOEXCEPT {
@@ -394,13 +430,8 @@
inline _LIBCPP_HIDE_FROM_ABI bool isinf(long double __x) _NOEXCEPT { return __builtin_isinf(__x); }
# endif
-# endif // isinf
-
// isnan
-# ifdef isnan
-# undef isnan
-
template <class _A1, std::__enable_if_t<std::is_floating_point<_A1>::value, int> = 0>
inline _LIBCPP_HIDE_FROM_ABI bool isnan(_A1 __x) _NOEXCEPT {
return __builtin_isnan(__x);
@@ -419,13 +450,8 @@
inline _LIBCPP_HIDE_FROM_ABI bool isnan(long double __x) _NOEXCEPT { return __builtin_isnan(__x); }
# endif
-# endif // isnan
-
// isnormal
-# ifdef isnormal
-# undef isnormal
-
template <class _A1, std::__enable_if_t<std::is_floating_point<_A1>::value, int> = 0>
inline _LIBCPP_HIDE_FROM_ABI bool isnormal(_A1 __x) _NOEXCEPT {
return __builtin_isnormal(__x);
@@ -436,13 +462,8 @@
return __x != 0;
}
-# endif // isnormal
-
// isgreater
-# ifdef isgreater
-# undef isgreater
-
template <class _A1,
class _A2,
std::__enable_if_t<std::is_arithmetic<_A1>::value && std::is_arithmetic<_A2>::value, int> = 0>
@@ -451,13 +472,8 @@
return __builtin_isgreater((type)__x, (type)__y);
}
-# endif // isgreater
-
// isgreaterequal
-# ifdef isgreaterequal
-# undef isgreaterequal
-
template <class _A1,
class _A2,
std::__enable_if_t<std::is_arithmetic<_A1>::value && std::is_arithmetic<_A2>::value, int> = 0>
@@ -466,13 +482,8 @@
return __builtin_isgreaterequal((type)__x, (type)__y);
}
-# endif // isgreaterequal
-
// isless
-# ifdef isless
-# undef isless
-
template <class _A1,
class _A2,
std::__enable_if_t<std::is_arithmetic<_A1>::value && std::is_arithmetic<_A2>::value, int> = 0>
@@ -481,13 +492,8 @@
return __builtin_isless((type)__x, (type)__y);
}
-# endif // isless
-
// islessequal
-# ifdef islessequal
-# undef islessequal
-
template <class _A1,
class _A2,
std::__enable_if_t<std::is_arithmetic<_A1>::value && std::is_arithmetic<_A2>::value, int> = 0>
@@ -496,13 +502,8 @@
return __builtin_islessequal((type)__x, (type)__y);
}
-# endif // islessequal
-
// islessgreater
-# ifdef islessgreater
-# undef islessgreater
-
template <class _A1,
class _A2,
std::__enable_if_t<std::is_arithmetic<_A1>::value && std::is_arithmetic<_A2>::value, int> = 0>
@@ -511,13 +512,8 @@
return __builtin_islessgreater((type)__x, (type)__y);
}
-# endif // islessgreater
-
// isunordered
-# ifdef isunordered
-# undef isunordered
-
template <class _A1,
class _A2,
std::__enable_if_t<std::is_arithmetic<_A1>::value && std::is_arithmetic<_A2>::value, int> = 0>
@@ -526,7 +522,7 @@
return __builtin_isunordered((type)__x, (type)__y);
}
-# endif // isunordered
+#endif // _LIBCPP_MSVCRT
// abs
//