-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[libc][math] Implement isnormal macro. #109547
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
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@llvm/pr-subscribers-libc Author: Shourya Goel (Sh0g0-1758) Changes#109201 Full diff: https://github.com/llvm/llvm-project/pull/109547.diff 7 Files Affected:
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..5e74efa139e056
--- /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)
|
6 tasks
lntue
approved these changes
Sep 22, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
#109201