[libc-commits] [libc] [libc][math] Implement issubnormal macro. (PR #109572)

Shourya Goel via libc-commits libc-commits at lists.llvm.org
Sun Sep 22 02:03:32 PDT 2024


https://github.com/Sh0g0-1758 created https://github.com/llvm/llvm-project/pull/109572

#109201 

>From 164d5716e50e90574bea6e1fddd39b169f57f572 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Sun, 22 Sep 2024 14:29:19 +0530
Subject: [PATCH 1/2] Implement issubnormal

---
 .../llvm-libc-macros/math-function-macros.h   |  1 +
 libc/test/include/CMakeLists.txt              | 45 +++++++++++++++++
 libc/test/include/IsSubnormalTest.h           | 49 +++++++++++++++++++
 libc/test/include/issubnormal_test.c          | 24 +++++++++
 libc/test/include/issubnormal_test.cpp        | 12 +++++
 libc/test/include/issubnormalf_test.cpp       | 12 +++++
 libc/test/include/issubnormall_test.cpp       | 12 +++++
 7 files changed, 155 insertions(+)
 create mode 100644 libc/test/include/IsSubnormalTest.h
 create mode 100644 libc/test/include/issubnormal_test.c
 create mode 100644 libc/test/include/issubnormal_test.cpp
 create mode 100644 libc/test/include/issubnormalf_test.cpp
 create mode 100644 libc/test/include/issubnormall_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..3c75e933745756 100644
--- a/libc/include/llvm-libc-macros/math-function-macros.h
+++ b/libc/include/llvm-libc-macros/math-function-macros.h
@@ -19,5 +19,6 @@
 #define fpclassify(x)                                                          \
   __builtin_fpclassify(FP_NAN, FP_INFINITE, FP_NORMAL, FP_SUBNORMAL, FP_ZERO, x)
 #define isnormal(x) __builtin_isnormal(x)
+#define issubnormal(x) (__builtin_fpclassify(FP_NAN, FP_INFINITE, FP_NORMAL, FP_SUBNORMAL, FP_ZERO, x) == FP_SUBNORMAL)
 
 #endif // LLVM_LIBC_MACROS_MATH_FUNCTION_MACROS_H
diff --git a/libc/test/include/CMakeLists.txt b/libc/test/include/CMakeLists.txt
index e500d2795c6365..12692eed417c45 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(
+  issubnormal_test
+  SUITE
+    libc_include_tests
+  SRCS
+    issubnormal_test.cpp
+  DEPENDS
+    libc.include.llvm-libc-macros.math_function_macros
+)
+
+add_libc_test(
+  issubnormalf_test
+  SUITE
+    libc_include_tests
+  SRCS
+    issubnormalf_test.cpp
+  DEPENDS
+    libc.include.llvm-libc-macros.math_function_macros
+)
+
+add_libc_test(
+  issubnormall_test
+  SUITE
+    libc_include_tests
+  SRCS
+    issubnormall_test.cpp
+  DEPENDS
+    libc.include.llvm-libc-macros.math_function_macros
+)
+
 add_libc_test(
   isnormal_test
   SUITE
@@ -366,6 +396,21 @@ add_libc_test(
     libc.include.llvm-libc-macros.math_function_macros
 )
 
+add_libc_test(
+  issubnormal_c_test
+  C_TEST
+  UNIT_TEST_ONLY
+  SUITE
+    libc_include_tests
+  SRCS
+    issubnormal_test.c
+  COMPILE_OPTIONS
+    -Wall
+    -Werror
+  DEPENDS
+    libc.include.llvm-libc-macros.math_function_macros
+)
+
 add_libc_test(
   fpclassify_c_test
   C_TEST
diff --git a/libc/test/include/IsSubnormalTest.h b/libc/test/include/IsSubnormalTest.h
new file mode 100644
index 00000000000000..05d5e7ec72b5c7
--- /dev/null
+++ b/libc/test/include/IsSubnormalTest.h
@@ -0,0 +1,49 @@
+//===-- Utility class to test the issubnormal 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_ISSUBNORMAL_H
+#define LLVM_LIBC_TEST_INCLUDE_MATH_ISSUBNORMAL_H
+
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+
+#include "include/llvm-libc-macros/math-function-macros.h"
+
+template <typename T>
+class IsSubnormalTest : public LIBC_NAMESPACE::testing::Test {
+  DECLARE_SPECIAL_CONSTANTS(T)
+
+public:
+  typedef bool (*IsSubnormalFunc)(T);
+
+  void testSpecialNumbers(IsSubnormalFunc func) {
+    EXPECT_FALSE(func(aNaN));
+    EXPECT_FALSE(func(neg_aNaN));
+    EXPECT_FALSE(func(sNaN));
+    EXPECT_FALSE(func(neg_sNaN));
+    EXPECT_FALSE(func(inf));
+    EXPECT_FALSE(func(neg_inf));
+    EXPECT_FALSE(func(min_normal));
+    EXPECT_FALSE(func(max_normal));
+    EXPECT_FALSE(func(neg_max_normal));
+    EXPECT_TRUE(func(min_denormal));
+    EXPECT_TRUE(func(neg_min_denormal));
+    EXPECT_TRUE(func(max_denormal));
+    EXPECT_FALSE(func(zero));
+    EXPECT_FALSE(func(neg_zero));
+  }
+};
+
+#define LIST_ISSUBNORMAL_TESTS(T, func)                                         \
+  using LlvmLibcIsSubnormalTest = IsSubnormalTest<T>;                            \
+  TEST_F(LlvmLibcIsSubnormalTest, SpecialNumbers) {                             \
+    auto issubnormal_func = [](T x) { return func(x); };                        \
+    testSpecialNumbers(issubnormal_func);                                       \
+  }
+
+#endif // LLVM_LIBC_TEST_INCLUDE_MATH_ISSUBNORMAL_H
diff --git a/libc/test/include/issubnormal_test.c b/libc/test/include/issubnormal_test.c
new file mode 100644
index 00000000000000..8a4544305287aa
--- /dev/null
+++ b/libc/test/include/issubnormal_test.c
@@ -0,0 +1,24 @@
+//===-- Unittests for issubnormal 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 issubnormal
+#error "issubnormal macro is not defined"
+#else
+int main(void) {
+  assert(issubnormal(1.819f) == 0);
+  assert(issubnormal(-1.726) == 0);
+  assert(issubnormal(1.426L) == 0);
+  assert(issubnormal(1e-308) == 1);
+  assert(issubnormal(-1e-308) == 1);
+  return 0;
+}
+#endif
diff --git a/libc/test/include/issubnormal_test.cpp b/libc/test/include/issubnormal_test.cpp
new file mode 100644
index 00000000000000..ff57a1fa47e0dd
--- /dev/null
+++ b/libc/test/include/issubnormal_test.cpp
@@ -0,0 +1,12 @@
+//===-- Unittest for issubnormal[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 "IsSubnormalTest.h"
+#include "include/llvm-libc-macros/math-function-macros.h"
+
+LIST_ISSUBNORMAL_TESTS(double, issubnormal)
diff --git a/libc/test/include/issubnormalf_test.cpp b/libc/test/include/issubnormalf_test.cpp
new file mode 100644
index 00000000000000..7ffa07e4ab8da5
--- /dev/null
+++ b/libc/test/include/issubnormalf_test.cpp
@@ -0,0 +1,12 @@
+//===-- Unittest for issubnormal[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 "IsSubnormalTest.h"
+#include "include/llvm-libc-macros/math-function-macros.h"
+
+LIST_ISSUBNORMAL_TESTS(float, issubnormal)
diff --git a/libc/test/include/issubnormall_test.cpp b/libc/test/include/issubnormall_test.cpp
new file mode 100644
index 00000000000000..4546e2d8f54019
--- /dev/null
+++ b/libc/test/include/issubnormall_test.cpp
@@ -0,0 +1,12 @@
+//===-- Unittest for issubnormal[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 "IsSubnormalTest.h"
+#include "include/llvm-libc-macros/math-function-macros.h"
+
+LIST_ISSUBNORMAL_TESTS(long double, issubnormal)

>From ab7afd43b88af688d1594bccdf3596a918ba5d88 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Sun, 22 Sep 2024 14:31:11 +0530
Subject: [PATCH 2/2] ran git clang-format

---
 libc/include/llvm-libc-macros/math-function-macros.h |  4 +++-
 libc/test/include/IsSubnormalTest.h                  | 10 +++++-----
 2 files changed, 8 insertions(+), 6 deletions(-)

diff --git a/libc/include/llvm-libc-macros/math-function-macros.h b/libc/include/llvm-libc-macros/math-function-macros.h
index 3c75e933745756..699ec6b027f168 100644
--- a/libc/include/llvm-libc-macros/math-function-macros.h
+++ b/libc/include/llvm-libc-macros/math-function-macros.h
@@ -19,6 +19,8 @@
 #define fpclassify(x)                                                          \
   __builtin_fpclassify(FP_NAN, FP_INFINITE, FP_NORMAL, FP_SUBNORMAL, FP_ZERO, x)
 #define isnormal(x) __builtin_isnormal(x)
-#define issubnormal(x) (__builtin_fpclassify(FP_NAN, FP_INFINITE, FP_NORMAL, FP_SUBNORMAL, FP_ZERO, x) == FP_SUBNORMAL)
+#define issubnormal(x)                                                         \
+  (__builtin_fpclassify(FP_NAN, FP_INFINITE, FP_NORMAL, FP_SUBNORMAL, FP_ZERO, \
+                        x) == FP_SUBNORMAL)
 
 #endif // LLVM_LIBC_MACROS_MATH_FUNCTION_MACROS_H
diff --git a/libc/test/include/IsSubnormalTest.h b/libc/test/include/IsSubnormalTest.h
index 05d5e7ec72b5c7..f26d6d2f51b7f1 100644
--- a/libc/test/include/IsSubnormalTest.h
+++ b/libc/test/include/IsSubnormalTest.h
@@ -39,11 +39,11 @@ class IsSubnormalTest : public LIBC_NAMESPACE::testing::Test {
   }
 };
 
-#define LIST_ISSUBNORMAL_TESTS(T, func)                                         \
-  using LlvmLibcIsSubnormalTest = IsSubnormalTest<T>;                            \
-  TEST_F(LlvmLibcIsSubnormalTest, SpecialNumbers) {                             \
-    auto issubnormal_func = [](T x) { return func(x); };                        \
-    testSpecialNumbers(issubnormal_func);                                       \
+#define LIST_ISSUBNORMAL_TESTS(T, func)                                        \
+  using LlvmLibcIsSubnormalTest = IsSubnormalTest<T>;                          \
+  TEST_F(LlvmLibcIsSubnormalTest, SpecialNumbers) {                            \
+    auto issubnormal_func = [](T x) { return func(x); };                       \
+    testSpecialNumbers(issubnormal_func);                                      \
   }
 
 #endif // LLVM_LIBC_TEST_INCLUDE_MATH_ISSUBNORMAL_H



More information about the libc-commits mailing list