Skip to content

[libc][math] Implement issubnormal macro. #109572

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 23, 2024
Merged

Conversation

Sh0g0-1758
Copy link
Member

@llvmbot llvmbot added the libc label Sep 22, 2024
@llvmbot
Copy link
Member

llvmbot commented Sep 22, 2024

@llvm/pr-subscribers-libc

Author: Shourya Goel (Sh0g0-1758)

Changes

#109201


Full diff: https://github.com/llvm/llvm-project/pull/109572.diff

7 Files Affected:

  • (modified) libc/include/llvm-libc-macros/math-function-macros.h (+3)
  • (modified) libc/test/include/CMakeLists.txt (+45)
  • (added) libc/test/include/IsSubnormalTest.h (+49)
  • (added) libc/test/include/issubnormal_test.c (+24)
  • (added) libc/test/include/issubnormal_test.cpp (+12)
  • (added) libc/test/include/issubnormalf_test.cpp (+12)
  • (added) libc/test/include/issubnormall_test.cpp (+12)
diff --git a/libc/include/llvm-libc-macros/math-function-macros.h b/libc/include/llvm-libc-macros/math-function-macros.h
index f8cd9d8f4f24b1..699ec6b027f168 100644
--- a/libc/include/llvm-libc-macros/math-function-macros.h
+++ b/libc/include/llvm-libc-macros/math-function-macros.h
@@ -19,5 +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)
 
 #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)

@lntue lntue merged commit ba5e195 into llvm:main Sep 23, 2024
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants