[libc-commits] [libc] aaa637d - [libc][math] Implement isnormal macro. (#109547)
via libc-commits
libc-commits at lists.llvm.org
Sat Sep 21 19:27:00 PDT 2024
Author: Shourya Goel
Date: 2024-09-21T22:26:56-04:00
New Revision: aaa637d8d04694827e0e7d68c68971ff26791823
URL: https://github.com/llvm/llvm-project/commit/aaa637d8d04694827e0e7d68c68971ff26791823
DIFF: https://github.com/llvm/llvm-project/commit/aaa637d8d04694827e0e7d68c68971ff26791823.diff
LOG: [libc][math] Implement isnormal macro. (#109547)
#109201
Added:
libc/test/include/IsNormalTest.h
libc/test/include/isnormal_test.c
libc/test/include/isnormal_test.cpp
libc/test/include/isnormalf_test.cpp
libc/test/include/isnormall_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 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)
More information about the libc-commits
mailing list