[libcxx-commits] [libcxx] Provide overloads for cv-unqualified floating point types for `isnormal` (PR #104773)

Robin Caloudis via libcxx-commits libcxx-commits at lists.llvm.org
Mon Aug 19 05:51:30 PDT 2024


https://github.com/robincaloudis created https://github.com/llvm/llvm-project/pull/104773

This test shows that `isnormal()` does
not provide any overload for cv-unqualified
floating point type as actually defined
by the C++23 standard.

See §28.7.1/1.

>From 6c4468f671a537d73bc210e1776e4a8aa51e913c Mon Sep 17 00:00:00 2001
From: Robin Caloudis <robin.caloudis at gmx.de>
Date: Mon, 19 Aug 2024 14:42:03 +0200
Subject: [PATCH 1/2] Show current limitation by a test
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This test shows that `isnormal()` does
not provide any overload for cv-unqualified
floating point type as actually defined
by the C++23 standard.

See §28.7.1/1.
---
 libcxx/test/std/numerics/c.math/isnormal.pass.cpp | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/libcxx/test/std/numerics/c.math/isnormal.pass.cpp b/libcxx/test/std/numerics/c.math/isnormal.pass.cpp
index c3b8f31359f988..76c3d13520d996 100644
--- a/libcxx/test/std/numerics/c.math/isnormal.pass.cpp
+++ b/libcxx/test/std/numerics/c.math/isnormal.pass.cpp
@@ -62,9 +62,23 @@ struct TestInt {
   }
 };
 
+template <typename T>
+struct ConvertibleTo {
+  operator T() const { return T(1); }
+};
+
 int main(int, char**) {
   types::for_each(types::floating_point_types(), TestFloat());
   types::for_each(types::integral_types(), TestInt());
 
+  // Make sure we can call `std::isnormal` with convertible types. This checks
+  // whether overloads for all cv-unqualified floating-point types are working
+  // as expected.
+  {
+    assert(std::isnormal(ConvertibleTo<float>()));
+    assert(std::isnormal(ConvertibleTo<double>()));
+    assert(std::isnormal(ConvertibleTo<long double>()));
+  }
+
   return 0;
 }

>From 33567a2a26645508a5b094f3619f924e04ae2c88 Mon Sep 17 00:00:00 2001
From: Robin Caloudis <robin.caloudis at gmx.de>
Date: Mon, 19 Aug 2024 14:48:48 +0200
Subject: [PATCH 2/2] Provide overloads for cv-unqualified fp types

---
 libcxx/include/__math/traits.h | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/libcxx/include/__math/traits.h b/libcxx/include/__math/traits.h
index 35c283cc9e21ce..9fe1de66a80985 100644
--- a/libcxx/include/__math/traits.h
+++ b/libcxx/include/__math/traits.h
@@ -137,6 +137,18 @@ _LIBCPP_NODISCARD _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool isnor
   return __x != 0;
 }
 
+_LIBCPP_NODISCARD inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool isnormal(float __x) _NOEXCEPT {
+  return __builtin_isnormal(__x);
+}
+
+_LIBCPP_NODISCARD inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool isnormal(double __x) _NOEXCEPT {
+  return __builtin_isnormal(__x);
+}
+
+_LIBCPP_NODISCARD inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool isnormal(long double __x) _NOEXCEPT {
+  return __builtin_isnormal(__x);
+}
+
 // isgreater
 
 template <class _A1, class _A2, __enable_if_t<is_arithmetic<_A1>::value && is_arithmetic<_A2>::value, int> = 0>



More information about the libcxx-commits mailing list