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

via libc-commits libc-commits at lists.llvm.org
Sun Sep 22 21:50:50 PDT 2024


Author: Shourya Goel
Date: 2024-09-23T00:50:46-04:00
New Revision: ba5e19580913d0b4cac249a81f0aabf7a0a3317f

URL: https://github.com/llvm/llvm-project/commit/ba5e19580913d0b4cac249a81f0aabf7a0a3317f
DIFF: https://github.com/llvm/llvm-project/commit/ba5e19580913d0b4cac249a81f0aabf7a0a3317f.diff

LOG: [libc][math] Implement issubnormal macro. (#109572)

#109201

Added: 
    libc/test/include/IsSubnormalTest.h
    libc/test/include/issubnormal_test.c
    libc/test/include/issubnormal_test.cpp
    libc/test/include/issubnormalf_test.cpp
    libc/test/include/issubnormall_test.cpp

Modified: 
    libc/include/llvm-libc-macros/math-function-macros.h
    libc/test/include/CMakeLists.txt

Removed: 
    


################################################################################
diff  --git a/libc/include/llvm-libc-macros/math-function-macros.h b/libc/include/llvm-libc-macros/math-function-macros.h
index f8cd9d8f4f24b1..68f9ff9d1c0330 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) (fpclassify(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..f26d6d2f51b7f1
--- /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)


        


More information about the libc-commits mailing list