[libc-commits] [libc] [libc][math] Reapply and fix issignaling macro. (PR #110011)

Shourya Goel via libc-commits libc-commits at lists.llvm.org
Wed Sep 25 09:49:31 PDT 2024


https://github.com/Sh0g0-1758 updated https://github.com/llvm/llvm-project/pull/110011

>From b52f7574b069f066e22b8fe67e2c019d539d643e Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Wed, 25 Sep 2024 22:03:22 +0530
Subject: [PATCH 1/3] Reapply "[libc][math] Implement issignaling macro."
 (#109992)

This reverts commit 808c498f52c8ff7724f762dab351600864023098.
---
 .../llvm-libc-macros/math-function-macros.h   |  4 ++
 libc/test/include/CMakeLists.txt              | 45 +++++++++++++++++
 libc/test/include/IsSignalingTest.h           | 49 +++++++++++++++++++
 libc/test/include/issignaling_test.c          | 24 +++++++++
 libc/test/include/issignaling_test.cpp        | 18 +++++++
 libc/test/include/issignalingf_test.cpp       | 18 +++++++
 libc/test/include/issignalingl_test.cpp       | 18 +++++++
 7 files changed, 176 insertions(+)
 create mode 100644 libc/test/include/IsSignalingTest.h
 create mode 100644 libc/test/include/issignaling_test.c
 create mode 100644 libc/test/include/issignaling_test.cpp
 create mode 100644 libc/test/include/issignalingf_test.cpp
 create mode 100644 libc/test/include/issignalingl_test.cpp

diff --git a/libc/include/llvm-libc-macros/math-function-macros.h b/libc/include/llvm-libc-macros/math-function-macros.h
index 68f9ff9d1c0330..c740eb2d188259 100644
--- a/libc/include/llvm-libc-macros/math-function-macros.h
+++ b/libc/include/llvm-libc-macros/math-function-macros.h
@@ -20,5 +20,9 @@
   __builtin_fpclassify(FP_NAN, FP_INFINITE, FP_NORMAL, FP_SUBNORMAL, FP_ZERO, x)
 #define isnormal(x) __builtin_isnormal(x)
 #define issubnormal(x) (fpclassify(x) == FP_SUBNORMAL)
+#if (defined(__clang__) && __clang_major__ >= 18) ||                           \
+    (defined(__GNUC__) && __GNUC__ >= 13)
+#define issignaling(x) __builtin_issignaling(x)
+#endif
 
 #endif // LLVM_LIBC_MACROS_MATH_FUNCTION_MACROS_H
diff --git a/libc/test/include/CMakeLists.txt b/libc/test/include/CMakeLists.txt
index 12692eed417c45..dd8f21bdd07aeb 100644
--- a/libc/test/include/CMakeLists.txt
+++ b/libc/test/include/CMakeLists.txt
@@ -81,6 +81,36 @@ add_libc_test(
     libc.include.llvm-libc-macros.stdckdint_macros
 )
 
+add_libc_test(
+  issignaling_test
+  SUITE
+    libc_include_tests
+  SRCS
+    issignaling_test.cpp
+  DEPENDS
+    libc.include.llvm-libc-macros.math_function_macros
+)
+
+add_libc_test(
+  issignalingf_test
+  SUITE
+    libc_include_tests
+  SRCS
+    issignalingf_test.cpp
+  DEPENDS
+    libc.include.llvm-libc-macros.math_function_macros
+)
+
+add_libc_test(
+  issignalingl_test
+  SUITE
+    libc_include_tests
+  SRCS
+    issignalingl_test.cpp
+  DEPENDS
+    libc.include.llvm-libc-macros.math_function_macros
+)
+
 add_libc_test(
   issubnormal_test
   SUITE
@@ -366,6 +396,21 @@ add_libc_test(
     libc.include.llvm-libc-macros.math_function_macros
 )
 
+add_libc_test(
+  issignaling_c_test
+  C_TEST
+  UNIT_TEST_ONLY
+  SUITE
+    libc_include_tests
+  SRCS
+    issignaling_test.c
+  COMPILE_OPTIONS
+    -Wall
+    -Werror
+  DEPENDS
+    libc.include.llvm-libc-macros.math_function_macros
+)
+
 add_libc_test(
   isinf_c_test
   C_TEST
diff --git a/libc/test/include/IsSignalingTest.h b/libc/test/include/IsSignalingTest.h
new file mode 100644
index 00000000000000..c369cfe090ed30
--- /dev/null
+++ b/libc/test/include/IsSignalingTest.h
@@ -0,0 +1,49 @@
+//===-- Utility class to test the issignaling macro  ------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_TEST_INCLUDE_MATH_ISSIGNALING_H
+#define LLVM_LIBC_TEST_INCLUDE_MATH_ISSIGNALING_H
+
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+
+#include "include/llvm-libc-macros/math-function-macros.h"
+
+template <typename T>
+class IsSignalingTest : public LIBC_NAMESPACE::testing::Test {
+  DECLARE_SPECIAL_CONSTANTS(T)
+
+public:
+  typedef int (*IsSignalingFunc)(T);
+
+  void testSpecialNumbers(IsSignalingFunc func) {
+    EXPECT_EQ(func(aNaN), 0);
+    EXPECT_EQ(func(neg_aNaN), 0);
+    EXPECT_EQ(func(sNaN), 1);
+    EXPECT_EQ(func(neg_sNaN), 1);
+    EXPECT_EQ(func(inf), 0);
+    EXPECT_EQ(func(neg_inf), 0);
+    EXPECT_EQ(func(min_normal), 0);
+    EXPECT_EQ(func(max_normal), 0);
+    EXPECT_EQ(func(neg_max_normal), 0);
+    EXPECT_EQ(func(min_denormal), 0);
+    EXPECT_EQ(func(neg_min_denormal), 0);
+    EXPECT_EQ(func(max_denormal), 0);
+    EXPECT_EQ(func(zero), 0);
+    EXPECT_EQ(func(neg_zero), 0);
+  }
+};
+
+#define LIST_ISSIGNALING_TESTS(T, func)                                        \
+  using LlvmLibcIsSignalingTest = IsSignalingTest<T>;                          \
+  TEST_F(LlvmLibcIsSignalingTest, SpecialNumbers) {                            \
+    auto issignaling_func = [](T x) { return func(x); };                       \
+    testSpecialNumbers(issignaling_func);                                      \
+  }
+
+#endif // LLVM_LIBC_TEST_INCLUDE_MATH_ISSIGNALING_H
diff --git a/libc/test/include/issignaling_test.c b/libc/test/include/issignaling_test.c
new file mode 100644
index 00000000000000..2c080696404aee
--- /dev/null
+++ b/libc/test/include/issignaling_test.c
@@ -0,0 +1,24 @@
+//===-- Unittests for issignaling macro -----------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDSList-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+#include "include/llvm-libc-macros/math-function-macros.h"
+
+#include <assert.h>
+
+// TODO: enable the test unconditionally when issignaling macro is fixed for
+//       older compiler
+int main(void) {
+#ifdef issignaling
+  assert(issignaling(__builtin_nans("")) == 1);
+  assert(issignaling(__builtin_nansf("")) == 1);
+  assert(issignaling(__builtin_nansl("")) == 1);
+  assert(issignaling(1.819f) == 0);
+  assert(issignaling(-1.726) == 0);
+  assert(issignaling(1.426L) == 0);
+#endif
+  return 0;
+}
diff --git a/libc/test/include/issignaling_test.cpp b/libc/test/include/issignaling_test.cpp
new file mode 100644
index 00000000000000..ef007feb0a6338
--- /dev/null
+++ b/libc/test/include/issignaling_test.cpp
@@ -0,0 +1,18 @@
+//===-- Unittest for issignaling[d] macro ---------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDSList-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "IsSignalingTest.h"
+#include "include/llvm-libc-macros/math-function-macros.h"
+
+// TODO: enable the test unconditionally when issignaling macro is fixed for
+//       older compiler
+#ifdef issignaling
+LIST_ISSIGNALING_TESTS(double, issignaling)
+#else
+int main() { return 0; }
+#endif
diff --git a/libc/test/include/issignalingf_test.cpp b/libc/test/include/issignalingf_test.cpp
new file mode 100644
index 00000000000000..9b236f2bb84d75
--- /dev/null
+++ b/libc/test/include/issignalingf_test.cpp
@@ -0,0 +1,18 @@
+//===-- Unittest for issignaling[f] macro ---------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDSList-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "IsSignalingTest.h"
+#include "include/llvm-libc-macros/math-function-macros.h"
+
+// TODO: enable the test unconditionally when issignaling macro is fixed for
+//       older compiler
+#ifdef issignaling
+LIST_ISSIGNALING_TESTS(float, issignaling)
+#else
+int main() { return 0; }
+#endif
diff --git a/libc/test/include/issignalingl_test.cpp b/libc/test/include/issignalingl_test.cpp
new file mode 100644
index 00000000000000..35482cb4b02029
--- /dev/null
+++ b/libc/test/include/issignalingl_test.cpp
@@ -0,0 +1,18 @@
+//===-- Unittest for issignaling[l] macro ---------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDSList-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "IsSignalingTest.h"
+#include "include/llvm-libc-macros/math-function-macros.h"
+
+// TODO: enable the test unconditionally when issignaling macro is fixed for
+//       older compiler
+#ifdef issignaling
+LIST_ISSIGNALING_TESTS(long double, issignaling)
+#else
+int main() { return 0; }
+#endif

>From 932c8727d2b4b047f190a6abd8585b5ab72babb9 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Wed, 25 Sep 2024 22:12:41 +0530
Subject: [PATCH 2/3] fix

---
 libc/test/include/issignaling_test.cpp  | 2 +-
 libc/test/include/issignalingf_test.cpp | 2 +-
 libc/test/include/issignalingl_test.cpp | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/libc/test/include/issignaling_test.cpp b/libc/test/include/issignaling_test.cpp
index ef007feb0a6338..ac73eca3cf00b9 100644
--- a/libc/test/include/issignaling_test.cpp
+++ b/libc/test/include/issignaling_test.cpp
@@ -14,5 +14,5 @@
 #ifdef issignaling
 LIST_ISSIGNALING_TESTS(double, issignaling)
 #else
-int main() { return 0; }
+TEST(LlvmLibcIsSignalingTest, Skip) { }
 #endif
diff --git a/libc/test/include/issignalingf_test.cpp b/libc/test/include/issignalingf_test.cpp
index 9b236f2bb84d75..a577f8e06fe1bd 100644
--- a/libc/test/include/issignalingf_test.cpp
+++ b/libc/test/include/issignalingf_test.cpp
@@ -14,5 +14,5 @@
 #ifdef issignaling
 LIST_ISSIGNALING_TESTS(float, issignaling)
 #else
-int main() { return 0; }
+TEST(LlvmLibcIsSignalingTest, Skip) { }
 #endif
diff --git a/libc/test/include/issignalingl_test.cpp b/libc/test/include/issignalingl_test.cpp
index 35482cb4b02029..abe052410b0b5f 100644
--- a/libc/test/include/issignalingl_test.cpp
+++ b/libc/test/include/issignalingl_test.cpp
@@ -14,5 +14,5 @@
 #ifdef issignaling
 LIST_ISSIGNALING_TESTS(long double, issignaling)
 #else
-int main() { return 0; }
+TEST(LlvmLibcIsSignalingTest, Skip) { }
 #endif

>From c8c20a8ee3868f7797c176939e7ed1726e8f9c3a Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Wed, 25 Sep 2024 22:19:17 +0530
Subject: [PATCH 3/3] fmt

---
 libc/test/include/issignaling_test.cpp  | 2 +-
 libc/test/include/issignalingf_test.cpp | 2 +-
 libc/test/include/issignalingl_test.cpp | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/libc/test/include/issignaling_test.cpp b/libc/test/include/issignaling_test.cpp
index ac73eca3cf00b9..3d25ea394c835f 100644
--- a/libc/test/include/issignaling_test.cpp
+++ b/libc/test/include/issignaling_test.cpp
@@ -14,5 +14,5 @@
 #ifdef issignaling
 LIST_ISSIGNALING_TESTS(double, issignaling)
 #else
-TEST(LlvmLibcIsSignalingTest, Skip) { }
+TEST(LlvmLibcIsSignalingTest, Skip) {}
 #endif
diff --git a/libc/test/include/issignalingf_test.cpp b/libc/test/include/issignalingf_test.cpp
index a577f8e06fe1bd..02426ceb24ac84 100644
--- a/libc/test/include/issignalingf_test.cpp
+++ b/libc/test/include/issignalingf_test.cpp
@@ -14,5 +14,5 @@
 #ifdef issignaling
 LIST_ISSIGNALING_TESTS(float, issignaling)
 #else
-TEST(LlvmLibcIsSignalingTest, Skip) { }
+TEST(LlvmLibcIsSignalingTest, Skip) {}
 #endif
diff --git a/libc/test/include/issignalingl_test.cpp b/libc/test/include/issignalingl_test.cpp
index abe052410b0b5f..9897647fb1077f 100644
--- a/libc/test/include/issignalingl_test.cpp
+++ b/libc/test/include/issignalingl_test.cpp
@@ -14,5 +14,5 @@
 #ifdef issignaling
 LIST_ISSIGNALING_TESTS(long double, issignaling)
 #else
-TEST(LlvmLibcIsSignalingTest, Skip) { }
+TEST(LlvmLibcIsSignalingTest, Skip) {}
 #endif



More information about the libc-commits mailing list