[libcxx-commits] [libcxx] [libc++] Fix acceptance of convertible-to-{float, double, long double} in std::isfinite() (PR #98841)
via libcxx-commits
libcxx-commits at lists.llvm.org
Sun Jul 14 15:14:10 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libcxx
Author: Robin Caloudis (robincaloudis)
<details>
<summary>Changes</summary>
Closes https://github.com/llvm/llvm-project/issues/98816.
---
Full diff: https://github.com/llvm/llvm-project/pull/98841.diff
2 Files Affected:
- (modified) libcxx/include/__math/traits.h (+14)
- (modified) libcxx/test/std/numerics/c.math/isfinite.pass.cpp (+33)
``````````diff
diff --git a/libcxx/include/__math/traits.h b/libcxx/include/__math/traits.h
index a448266797557..07e5a74ac2362 100644
--- a/libcxx/include/__math/traits.h
+++ b/libcxx/include/__math/traits.h
@@ -55,6 +55,20 @@ _LIBCPP_NODISCARD _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool isfin
return true;
}
+#ifdef _LIBCPP_PREFERRED_OVERLOAD
+_LIBCPP_NODISCARD inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool isfinite(float __x) _NOEXCEPT {
+ return __builtin_isfinite(__x);
+}
+
+_LIBCPP_NODISCARD inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI _LIBCPP_PREFERRED_OVERLOAD bool
+isfinite(double __x) _NOEXCEPT {
+ return __builtin_isfinite(__x);
+}
+
+_LIBCPP_NODISCARD inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool isfinite(long double __x) _NOEXCEPT {
+ return __builtin_isfinite(__x);
+}
+#endif
// isinf
template <class _A1, __enable_if_t<is_arithmetic<_A1>::value && numeric_limits<_A1>::has_infinity, int> = 0>
diff --git a/libcxx/test/std/numerics/c.math/isfinite.pass.cpp b/libcxx/test/std/numerics/c.math/isfinite.pass.cpp
index 6bbc3aaac6d13..56d9550348a9b 100644
--- a/libcxx/test/std/numerics/c.math/isfinite.pass.cpp
+++ b/libcxx/test/std/numerics/c.math/isfinite.pass.cpp
@@ -62,9 +62,42 @@ struct TestInt {
}
};
+struct ConvertibleFloat {
+ int value;
+
+ ConvertibleFloat(int v) : value(v) {}
+
+ operator float() const {
+ return static_cast<float>(value);
+ }
+};
+
+struct ConvertibleDouble {
+ int value;
+
+ ConvertibleDouble(int v) : value(v) {}
+
+ operator double() const {
+ return static_cast<double>(value);
+ }
+};
+
+struct ConvertibleLongDouble {
+ int value;
+
+ ConvertibleLongDouble(int v) : value(v) {}
+
+ operator long double() const {
+ return static_cast<long double>(value);
+ }
+};
+
int main(int, char**) {
types::for_each(types::floating_point_types(), TestFloat());
types::for_each(types::integral_types(), TestInt());
+ assert(std::isfinite(ConvertibleFloat(0)));
+ assert(std::isfinite(ConvertibleDouble(0)));
+ assert(std::isfinite(ConvertibleLongDouble(0)));
return 0;
}
``````````
</details>
https://github.com/llvm/llvm-project/pull/98841
More information about the libcxx-commits
mailing list