[libc-commits] [libc] [libc][math] Implement issignaling macro. (PR #109615)

Shourya Goel via libc-commits libc-commits at lists.llvm.org
Mon Sep 23 08:16:53 PDT 2024


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

>From 511114f45dbf45b3b504a54fbd5c46874d57d15c Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Mon, 23 Sep 2024 12:21:11 +0530
Subject: [PATCH 1/6] implement issignaling

---
 .../llvm-libc-macros/math-function-macros.h   | 10 ++++
 libc/test/include/CMakeLists.txt              | 45 +++++++++++++++++
 libc/test/include/IsSignalingTest.h           | 49 +++++++++++++++++++
 libc/test/include/issignaling_test.c          | 25 ++++++++++
 libc/test/include/issignaling_test.cpp        | 12 +++++
 libc/test/include/issignalingf_test.cpp       | 12 +++++
 libc/test/include/issignalingl_test.cpp       | 12 +++++
 7 files changed, 165 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 f8cd9d8f4f24b1..8f581949db52a3 100644
--- a/libc/include/llvm-libc-macros/math-function-macros.h
+++ b/libc/include/llvm-libc-macros/math-function-macros.h
@@ -19,5 +19,15 @@
 #define fpclassify(x)                                                          \
   __builtin_fpclassify(FP_NAN, FP_INFINITE, FP_NORMAL, FP_SUBNORMAL, FP_ZERO, x)
 #define isnormal(x) __builtin_isnormal(x)
+#ifdef __cplusplus
+  #define issignaling(x)                                                       \
+      _Generic((x),                                                            \
+          float: __issignalingf,                                               \
+          double: __issignaling,                                               \
+          long double: __issignalingl                                          \
+      )(x)
+#else
+  #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 e500d2795c6365..92f55d68f4b9a3 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(
   isnormal_test
   SUITE
@@ -336,6 +366,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..d65d3a818083e0
--- /dev/null
+++ b/libc/test/include/issignaling_test.c
@@ -0,0 +1,25 @@
+//===-- 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>
+
+// check if macro is defined
+#ifndef issignaling
+#error "issignaling macro is not defined"
+#else
+int main(void) {
+  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);
+  return 0;
+}
+#endif
diff --git a/libc/test/include/issignaling_test.cpp b/libc/test/include/issignaling_test.cpp
new file mode 100644
index 00000000000000..f8e5d82b9e2b3f
--- /dev/null
+++ b/libc/test/include/issignaling_test.cpp
@@ -0,0 +1,12 @@
+//===-- 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"
+
+LIST_ISSIGNALING_TESTS(double, issignaling)
diff --git a/libc/test/include/issignalingf_test.cpp b/libc/test/include/issignalingf_test.cpp
new file mode 100644
index 00000000000000..2377bed0513f7d
--- /dev/null
+++ b/libc/test/include/issignalingf_test.cpp
@@ -0,0 +1,12 @@
+//===-- 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"
+
+LIST_ISSIGNALING_TESTS(float, issignaling)
diff --git a/libc/test/include/issignalingl_test.cpp b/libc/test/include/issignalingl_test.cpp
new file mode 100644
index 00000000000000..3d7e793f89755e
--- /dev/null
+++ b/libc/test/include/issignalingl_test.cpp
@@ -0,0 +1,12 @@
+//===-- 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"
+
+LIST_ISSIGNALING_TESTS(long double, issignaling)

>From 578af38b637f1a31550d6d1182bd1578d12cbc7a Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Mon, 23 Sep 2024 12:21:31 +0530
Subject: [PATCH 2/6] Ran fmt

---
 .../include/llvm-libc-macros/math-function-macros.h | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)

diff --git a/libc/include/llvm-libc-macros/math-function-macros.h b/libc/include/llvm-libc-macros/math-function-macros.h
index 8f581949db52a3..11b5555e43d1f0 100644
--- a/libc/include/llvm-libc-macros/math-function-macros.h
+++ b/libc/include/llvm-libc-macros/math-function-macros.h
@@ -20,14 +20,13 @@
   __builtin_fpclassify(FP_NAN, FP_INFINITE, FP_NORMAL, FP_SUBNORMAL, FP_ZERO, x)
 #define isnormal(x) __builtin_isnormal(x)
 #ifdef __cplusplus
-  #define issignaling(x)                                                       \
-      _Generic((x),                                                            \
-          float: __issignalingf,                                               \
-          double: __issignaling,                                               \
-          long double: __issignalingl                                          \
-      )(x)
+#define issignaling(x)                                                         \
+  _Generic((x),                                                                \
+      float: __issignalingf,                                                   \
+      double: __issignaling,                                                   \
+      long double: __issignalingl)(x)
 #else
-  #define issignaling(x) __builtin_issignaling(x)
+#define issignaling(x) __builtin_issignaling(x)
 #endif
 
 #endif // LLVM_LIBC_MACROS_MATH_FUNCTION_MACROS_H

>From 31bd5e020f77bcb3c8b616d51acfca0186be4021 Mon Sep 17 00:00:00 2001
From: Shourya Goel <shouryagoel10000 at gmail.com>
Date: Mon, 23 Sep 2024 12:41:44 +0530
Subject: [PATCH 3/6] Update CMakeLists.txt

---
 libc/test/include/CMakeLists.txt | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/libc/test/include/CMakeLists.txt b/libc/test/include/CMakeLists.txt
index dbaf3295348566..dd8f21bdd07aeb 100644
--- a/libc/test/include/CMakeLists.txt
+++ b/libc/test/include/CMakeLists.txt
@@ -82,31 +82,31 @@ add_libc_test(
 )
 
 add_libc_test(
-  stdckdint_test
+  issignaling_test
   SUITE
     libc_include_tests
   SRCS
-    stdckdint_test.cpp
+    issignaling_test.cpp
   DEPENDS
-    libc.include.llvm-libc-macros.stdckdint_macros
+    libc.include.llvm-libc-macros.math_function_macros
 )
 
 add_libc_test(
-  issignaling_test
+  issignalingf_test
   SUITE
     libc_include_tests
   SRCS
-    issignaling_test.cpp
+    issignalingf_test.cpp
   DEPENDS
     libc.include.llvm-libc-macros.math_function_macros
 )
 
 add_libc_test(
-  issignalingf_test
+  issignalingl_test
   SUITE
     libc_include_tests
   SRCS
-    issignalingf_test.cpp
+    issignalingl_test.cpp
   DEPENDS
     libc.include.llvm-libc-macros.math_function_macros
 )

>From da94b0712a43abeccec8557b03e6908b0bfc2374 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Mon, 23 Sep 2024 20:14:17 +0530
Subject: [PATCH 4/6] use builtin for the function

---
 libc/include/llvm-libc-macros/math-function-macros.h | 8 --------
 1 file changed, 8 deletions(-)

diff --git a/libc/include/llvm-libc-macros/math-function-macros.h b/libc/include/llvm-libc-macros/math-function-macros.h
index bb7a668d4b4d7f..8284f18ecca5a2 100644
--- a/libc/include/llvm-libc-macros/math-function-macros.h
+++ b/libc/include/llvm-libc-macros/math-function-macros.h
@@ -20,14 +20,6 @@
   __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)
-#ifdef __cplusplus
-#define issignaling(x)                                                         \
-  _Generic((x),                                                                \
-      float: __issignalingf,                                                   \
-      double: __issignaling,                                                   \
-      long double: __issignalingl)(x)
-#else
 #define issignaling(x) __builtin_issignaling(x)
-#endif
 
 #endif // LLVM_LIBC_MACROS_MATH_FUNCTION_MACROS_H

>From 3c19675b18dc864512c2dcbc34b5fab718f2c685 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Mon, 23 Sep 2024 20:45:52 +0530
Subject: [PATCH 5/6] Added guards for compiler version

---
 libc/include/llvm-libc-macros/math-function-macros.h | 4 +++-
 libc/test/include/issignaling_test.cpp               | 7 +++++++
 libc/test/include/issignalingf_test.cpp              | 7 +++++++
 libc/test/include/issignalingl_test.cpp              | 7 +++++++
 4 files changed, 24 insertions(+), 1 deletion(-)

diff --git a/libc/include/llvm-libc-macros/math-function-macros.h b/libc/include/llvm-libc-macros/math-function-macros.h
index 8284f18ecca5a2..288b52799b87b3 100644
--- a/libc/include/llvm-libc-macros/math-function-macros.h
+++ b/libc/include/llvm-libc-macros/math-function-macros.h
@@ -20,6 +20,8 @@
   __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)
-#define issignaling(x) __builtin_issignaling(x)
+#if defined(__clang__) && __clang_major__ >= 18
+  #define issignaling(x) __builtin_issignaling(x)
+#endif
 
 #endif // LLVM_LIBC_MACROS_MATH_FUNCTION_MACROS_H
diff --git a/libc/test/include/issignaling_test.cpp b/libc/test/include/issignaling_test.cpp
index f8e5d82b9e2b3f..d7eff3a85cb301 100644
--- a/libc/test/include/issignaling_test.cpp
+++ b/libc/test/include/issignaling_test.cpp
@@ -9,4 +9,11 @@
 #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
+#if (defined(__clang__) && __clang_major__ >= 18) || \
+    (defined(__GNUC__) && __GNUC__ >= 13)
+
 LIST_ISSIGNALING_TESTS(double, issignaling)
+
+#endif
diff --git a/libc/test/include/issignalingf_test.cpp b/libc/test/include/issignalingf_test.cpp
index 2377bed0513f7d..dce742d0cf0647 100644
--- a/libc/test/include/issignalingf_test.cpp
+++ b/libc/test/include/issignalingf_test.cpp
@@ -9,4 +9,11 @@
 #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
+#if (defined(__clang__) && __clang_major__ >= 18) || \
+    (defined(__GNUC__) && __GNUC__ >= 13)
+
 LIST_ISSIGNALING_TESTS(float, issignaling)
+
+#endif
diff --git a/libc/test/include/issignalingl_test.cpp b/libc/test/include/issignalingl_test.cpp
index 3d7e793f89755e..0e2b2b8679dcd9 100644
--- a/libc/test/include/issignalingl_test.cpp
+++ b/libc/test/include/issignalingl_test.cpp
@@ -9,4 +9,11 @@
 #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
+#if (defined(__clang__) && __clang_major__ >= 18) || \
+    (defined(__GNUC__) && __GNUC__ >= 13)
+
 LIST_ISSIGNALING_TESTS(long double, issignaling)
+
+#endif

>From 4beb87bfa53bd137643b28cd40dcc744d116ef86 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Mon, 23 Sep 2024 20:46:34 +0530
Subject: [PATCH 6/6] Ran Fmt

---
 libc/include/llvm-libc-macros/math-function-macros.h | 2 +-
 libc/test/include/issignaling_test.cpp               | 4 ++--
 libc/test/include/issignalingf_test.cpp              | 4 ++--
 libc/test/include/issignalingl_test.cpp              | 4 ++--
 4 files changed, 7 insertions(+), 7 deletions(-)

diff --git a/libc/include/llvm-libc-macros/math-function-macros.h b/libc/include/llvm-libc-macros/math-function-macros.h
index 288b52799b87b3..4a64c5b2cfdc50 100644
--- a/libc/include/llvm-libc-macros/math-function-macros.h
+++ b/libc/include/llvm-libc-macros/math-function-macros.h
@@ -21,7 +21,7 @@
 #define isnormal(x) __builtin_isnormal(x)
 #define issubnormal(x) (fpclassify(x) == FP_SUBNORMAL)
 #if defined(__clang__) && __clang_major__ >= 18
-  #define issignaling(x) __builtin_issignaling(x)
+#define issignaling(x) __builtin_issignaling(x)
 #endif
 
 #endif // LLVM_LIBC_MACROS_MATH_FUNCTION_MACROS_H
diff --git a/libc/test/include/issignaling_test.cpp b/libc/test/include/issignaling_test.cpp
index d7eff3a85cb301..2924337c81b2ae 100644
--- a/libc/test/include/issignaling_test.cpp
+++ b/libc/test/include/issignaling_test.cpp
@@ -9,9 +9,9 @@
 #include "IsSignalingTest.h"
 #include "include/llvm-libc-macros/math-function-macros.h"
 
-// TODO: enable the test unconditionally when issignaling macro is fixed for 
+// TODO: enable the test unconditionally when issignaling macro is fixed for
 //       older compiler
-#if (defined(__clang__) && __clang_major__ >= 18) || \
+#if (defined(__clang__) && __clang_major__ >= 18) ||                           \
     (defined(__GNUC__) && __GNUC__ >= 13)
 
 LIST_ISSIGNALING_TESTS(double, issignaling)
diff --git a/libc/test/include/issignalingf_test.cpp b/libc/test/include/issignalingf_test.cpp
index dce742d0cf0647..267a7c1fd16f01 100644
--- a/libc/test/include/issignalingf_test.cpp
+++ b/libc/test/include/issignalingf_test.cpp
@@ -9,9 +9,9 @@
 #include "IsSignalingTest.h"
 #include "include/llvm-libc-macros/math-function-macros.h"
 
-// TODO: enable the test unconditionally when issignaling macro is fixed for 
+// TODO: enable the test unconditionally when issignaling macro is fixed for
 //       older compiler
-#if (defined(__clang__) && __clang_major__ >= 18) || \
+#if (defined(__clang__) && __clang_major__ >= 18) ||                           \
     (defined(__GNUC__) && __GNUC__ >= 13)
 
 LIST_ISSIGNALING_TESTS(float, issignaling)
diff --git a/libc/test/include/issignalingl_test.cpp b/libc/test/include/issignalingl_test.cpp
index 0e2b2b8679dcd9..8f83b3fca04f09 100644
--- a/libc/test/include/issignalingl_test.cpp
+++ b/libc/test/include/issignalingl_test.cpp
@@ -9,9 +9,9 @@
 #include "IsSignalingTest.h"
 #include "include/llvm-libc-macros/math-function-macros.h"
 
-// TODO: enable the test unconditionally when issignaling macro is fixed for 
+// TODO: enable the test unconditionally when issignaling macro is fixed for
 //       older compiler
-#if (defined(__clang__) && __clang_major__ >= 18) || \
+#if (defined(__clang__) && __clang_major__ >= 18) ||                           \
     (defined(__GNUC__) && __GNUC__ >= 13)
 
 LIST_ISSIGNALING_TESTS(long double, issignaling)



More information about the libc-commits mailing list