[libc-commits] [libc] [libc][math] Implement isnormal macro. (PR #109547)

Shourya Goel via libc-commits libc-commits at lists.llvm.org
Sat Sep 21 13:03:15 PDT 2024


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

#109201 

>From a2d2ef7b3fa510d95cda514eed9ce2f7f4313894 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Sun, 22 Sep 2024 01:24:46 +0530
Subject: [PATCH 1/2] Add isnormal

---
 .../llvm-libc-macros/math-function-macros.h   |  1 +
 libc/test/include/CMakeLists.txt              | 45 +++++++++++++++++
 libc/test/include/IsNormalTest.h              | 49 +++++++++++++++++++
 libc/test/include/isnormal_test.c             | 25 ++++++++++
 libc/test/include/isnormal_test.cpp           | 12 +++++
 libc/test/include/isnormalf_test.cpp          | 12 +++++
 libc/test/include/isnormall_test.cpp          | 12 +++++
 7 files changed, 156 insertions(+)
 create mode 100644 libc/test/include/IsNormalTest.h
 create mode 100644 libc/test/include/isnormal_test.c
 create mode 100644 libc/test/include/isnormal_test.cpp
 create mode 100644 libc/test/include/isnormalf_test.cpp
 create mode 100644 libc/test/include/isnormall_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 4eaeb6958a5cb4..f8cd9d8f4f24b1 100644
--- a/libc/include/llvm-libc-macros/math-function-macros.h
+++ b/libc/include/llvm-libc-macros/math-function-macros.h
@@ -18,5 +18,6 @@
 #define iszero(x) (x == 0)
 #define fpclassify(x)                                                          \
   __builtin_fpclassify(FP_NAN, FP_INFINITE, FP_NORMAL, FP_SUBNORMAL, FP_ZERO, x)
+#define isnormal(x) __builtin_isnormal(x)
 
 #endif // LLVM_LIBC_MACROS_MATH_FUNCTION_MACROS_H
diff --git a/libc/test/include/CMakeLists.txt b/libc/test/include/CMakeLists.txt
index ca38b6fa48a236..e500d2795c6365 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(
+  isnormal_test
+  SUITE
+    libc_include_tests
+  SRCS
+    isnormal_test.cpp
+  DEPENDS
+    libc.include.llvm-libc-macros.math_function_macros
+)
+
+add_libc_test(
+  isnormalf_test
+  SUITE
+    libc_include_tests
+  SRCS
+    isnormalf_test.cpp
+  DEPENDS
+    libc.include.llvm-libc-macros.math_function_macros
+)
+
+add_libc_test(
+  isnormall_test
+  SUITE
+    libc_include_tests
+  SRCS
+    isnormall_test.cpp
+  DEPENDS
+    libc.include.llvm-libc-macros.math_function_macros
+)
+
 add_libc_test(
   fpclassify_test
   SUITE
@@ -291,6 +321,21 @@ add_libc_test(
     libc.include.llvm-libc-macros.math_function_macros
 )
 
+add_libc_test(
+  isnormal_c_test
+  C_TEST
+  UNIT_TEST_ONLY
+  SUITE
+    libc_include_tests
+  SRCS
+    isnormal_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/IsNormalTest.h b/libc/test/include/IsNormalTest.h
new file mode 100644
index 00000000000000..9dd24634421193
--- /dev/null
+++ b/libc/test/include/IsNormalTest.h
@@ -0,0 +1,49 @@
+//===-- Utility class to test the isnormal 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_ISNORMAL_H
+#define LLVM_LIBC_TEST_INCLUDE_MATH_ISNORMAL_H
+
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+
+#include "include/llvm-libc-macros/math-function-macros.h"
+
+template <typename T>
+class IsNormalTest : public LIBC_NAMESPACE::testing::Test {
+  DECLARE_SPECIAL_CONSTANTS(T)
+
+public:
+  typedef int (*IsNormalFunc)(T);
+
+  void testSpecialNumbers(IsNormalFunc func) {
+    EXPECT_EQ(func(aNaN), 0);
+    EXPECT_EQ(func(neg_aNaN), 0);
+    EXPECT_EQ(func(sNaN), 0);
+    EXPECT_EQ(func(neg_sNaN), 0);
+    EXPECT_EQ(func(inf), 0);
+    EXPECT_EQ(func(neg_inf), 0);
+    EXPECT_EQ(func(min_normal), 1);
+    EXPECT_EQ(func(max_normal), 1);
+    EXPECT_EQ(func(neg_max_normal), 1);
+    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_ISNORMAL_TESTS(T, func)                                           \
+  using LlvmLibcIsNormalTest = IsNormalTest<T>;                                \
+  TEST_F(LlvmLibcIsNormalTest, SpecialNumbers) {                               \
+    auto isnormal_func = [](T x) { return func(x); };                          \
+    testSpecialNumbers(isnormal_func);                                         \
+  }
+
+#endif // LLVM_LIBC_TEST_INCLUDE_MATH_ISNORMAL_H
diff --git a/libc/test/include/isnormal_test.c b/libc/test/include/isnormal_test.c
new file mode 100644
index 00000000000000..c076c5bfa2953a
--- /dev/null
+++ b/libc/test/include/isnormal_test.c
@@ -0,0 +1,25 @@
+//===-- Unittests for isnormal 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 isnormal
+#error "isnormal macro is not defined"
+#else
+int main(void) {
+  assert(isnormal(1.819f) == 1);
+  assert(isnormal(-1.726) == 1);
+  assert(isnormal(1.426L) == 1);
+  assert(isnormal(-0.0f) == 0);
+  assert(isnormal(0.0) == 0);
+  assert(isnormal(-0.0L) == 0);
+  return 0;
+}
+#endif
diff --git a/libc/test/include/isnormal_test.cpp b/libc/test/include/isnormal_test.cpp
new file mode 100644
index 00000000000000..da108507dfd19f
--- /dev/null
+++ b/libc/test/include/isnormal_test.cpp
@@ -0,0 +1,12 @@
+//===-- Unittest for isnormal[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 "IsNormalTest.h"
+#include "include/llvm-libc-macros/math-function-macros.h"
+
+LIST_ISNORMAL_TESTS(double, isnormal)
diff --git a/libc/test/include/isnormalf_test.cpp b/libc/test/include/isnormalf_test.cpp
new file mode 100644
index 00000000000000..59c090927795e1
--- /dev/null
+++ b/libc/test/include/isnormalf_test.cpp
@@ -0,0 +1,12 @@
+//===-- Unittest for isnormal[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 "IsNormalTest.h"
+#include "include/llvm-libc-macros/math-function-macros.h"
+
+LIST_ISNORMAL_TESTS(float, isnormal)
diff --git a/libc/test/include/isnormall_test.cpp b/libc/test/include/isnormall_test.cpp
new file mode 100644
index 00000000000000..a21f841a25d483
--- /dev/null
+++ b/libc/test/include/isnormall_test.cpp
@@ -0,0 +1,12 @@
+//===-- Unittest for isnormal[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 "IsNormalTest.h"
+#include "include/llvm-libc-macros/math-function-macros.h"
+
+LIST_ISNORMAL_TESTS(long double, isnormal)

>From fd8ced9668f127aa22a143a6992daf76ed579f62 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Sun, 22 Sep 2024 01:32:47 +0530
Subject: [PATCH 2/2] nit

---
 libc/test/include/IsNormalTest.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/test/include/IsNormalTest.h b/libc/test/include/IsNormalTest.h
index 9dd24634421193..5e74efa139e056 100644
--- a/libc/test/include/IsNormalTest.h
+++ b/libc/test/include/IsNormalTest.h
@@ -1,4 +1,4 @@
-//===-- Utility class to test the isnormal macro  -------------*- C++ -*-===//
+//===-- Utility class to test the isnormal 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.



More information about the libc-commits mailing list