[libc-commits] [libc] [libc][math] Implement fpclassify Macro (PR #109519)
via libc-commits
libc-commits at lists.llvm.org
Sat Sep 21 00:27:59 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libc
Author: Shourya Goel (Sh0g0-1758)
<details>
<summary>Changes</summary>
#<!-- -->109201
---
Full diff: https://github.com/llvm/llvm-project/pull/109519.diff
7 Files Affected:
- (modified) libc/include/llvm-libc-macros/math-function-macros.h (+1)
- (modified) libc/test/include/CMakeLists.txt (+45)
- (added) libc/test/include/FpClassifyTest.h (+52)
- (added) libc/test/include/fpclassify_test.c (+25)
- (added) libc/test/include/fpclassify_test.cpp (+12)
- (added) libc/test/include/fpclassifyf_test.cpp (+12)
- (added) libc/test/include/fpclassifyl_test.cpp (+12)
``````````diff
diff --git a/libc/include/llvm-libc-macros/math-function-macros.h b/libc/include/llvm-libc-macros/math-function-macros.h
index afbabd023203a2..0b7ee53a3a5a77 100644
--- a/libc/include/llvm-libc-macros/math-function-macros.h
+++ b/libc/include/llvm-libc-macros/math-function-macros.h
@@ -14,5 +14,6 @@
#define isnan(x) __builtin_isnan(x)
#define signbit(x) __builtin_signbit(x)
#define iszero(x) (x == 0)
+#define fpclassify(a, b, c, d, e, f) __builtin_fpclassify(a, b, c, d, e, f)
#endif // LLVM_LIBC_MACROS_MATH_FUNCTION_MACROS_H
diff --git a/libc/test/include/CMakeLists.txt b/libc/test/include/CMakeLists.txt
index 1a2f18731565c2..ca38b6fa48a236 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(
+ fpclassify_test
+ SUITE
+ libc_include_tests
+ SRCS
+ fpclassify_test.cpp
+ DEPENDS
+ libc.include.llvm-libc-macros.math_function_macros
+)
+
+add_libc_test(
+ fpclassifyf_test
+ SUITE
+ libc_include_tests
+ SRCS
+ fpclassifyf_test.cpp
+ DEPENDS
+ libc.include.llvm-libc-macros.math_function_macros
+)
+
+add_libc_test(
+ fpclassifyl_test
+ SUITE
+ libc_include_tests
+ SRCS
+ fpclassifyl_test.cpp
+ DEPENDS
+ libc.include.llvm-libc-macros.math_function_macros
+)
+
add_libc_test(
iszero_test
SUITE
@@ -291,6 +321,21 @@ add_libc_test(
libc.include.llvm-libc-macros.math_function_macros
)
+add_libc_test(
+ fpclassify_c_test
+ C_TEST
+ UNIT_TEST_ONLY
+ SUITE
+ libc_include_tests
+ SRCS
+ fpclassify_test.c
+ COMPILE_OPTIONS
+ -Wall
+ -Werror
+ DEPENDS
+ libc.include.llvm-libc-macros.math_function_macros
+)
+
add_libc_test(
iszero_c_test
C_TEST
diff --git a/libc/test/include/FpClassifyTest.h b/libc/test/include/FpClassifyTest.h
new file mode 100644
index 00000000000000..48d7c32ce256c9
--- /dev/null
+++ b/libc/test/include/FpClassifyTest.h
@@ -0,0 +1,52 @@
+//===-- Utility class to test the fpclassify 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_FPCLASSIFY_H
+#define LLVM_LIBC_TEST_INCLUDE_MATH_FPCLASSIFY_H
+
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+
+#include "include/llvm-libc-macros/math-function-macros.h"
+
+template <typename T>
+class FpClassifyTest : public LIBC_NAMESPACE::testing::Test {
+ DECLARE_SPECIAL_CONSTANTS(T)
+
+public:
+ typedef int (*FpClassifyFunc)(T, T, T, T, T, T);
+
+ void testSpecialNumbers(FpClassifyFunc func) {
+ EXPECT_EQ(func(1, 2, 3, 4, 5, aNaN), 1);
+ EXPECT_EQ(func(1, 2, 3, 4, 5, neg_aNaN), 1);
+ EXPECT_EQ(func(1, 2, 3, 4, 5, sNaN), 1);
+ EXPECT_EQ(func(1, 2, 3, 4, 5, neg_sNaN), 1);
+ EXPECT_EQ(func(1, 2, 3, 4, 5, inf), 2);
+ EXPECT_EQ(func(1, 2, 3, 4, 5, neg_inf), 2);
+ EXPECT_EQ(func(1, 2, 3, 4, 5, min_normal), 3);
+ EXPECT_EQ(func(1, 2, 3, 4, 5, max_normal), 3);
+ EXPECT_EQ(func(1, 2, 3, 4, 5, neg_max_normal), 3);
+ EXPECT_EQ(func(1, 2, 3, 4, 5, min_denormal), 4);
+ EXPECT_EQ(func(1, 2, 3, 4, 5, neg_min_denormal), 4);
+ EXPECT_EQ(func(1, 2, 3, 4, 5, max_denormal), 4);
+ EXPECT_EQ(func(1, 2, 3, 4, 5, zero), 5);
+ EXPECT_EQ(func(1, 2, 3, 4, 5, neg_zero), 5);
+ }
+};
+
+#define LIST_FPCLASSIFY_TESTS(T, func) \
+ using LlvmLibcFpClassifyTest = FpClassifyTest<T>; \
+ TEST_F(LlvmLibcFpClassifyTest, SpecialNumbers) { \
+ auto fpclassify_func = [](T a, T b, T c, T d, T e, T f) { \
+ return func(a, b, c, d, e, f); \
+ }; \
+ testSpecialNumbers(fpclassify_func); \
+ }
+
+#endif // LLVM_LIBC_TEST_INCLUDE_MATH_FPCLASSIFY_H
diff --git a/libc/test/include/fpclassify_test.c b/libc/test/include/fpclassify_test.c
new file mode 100644
index 00000000000000..ecfcf4eb178d19
--- /dev/null
+++ b/libc/test/include/fpclassify_test.c
@@ -0,0 +1,25 @@
+//===-- Unittests for fpclassify 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 fpclassify
+#error "fpclassify macro is not defined"
+#else
+int main(void) {
+ assert(fpclassify(1, 2, 3, 4, 5, 1.819f) == 3);
+ assert(fpclassify(1, 2, 3, 4, 5, -1.726) == 3);
+ assert(fpclassify(1, 2, 3, 4, 5, 1.426L) == 3);
+ assert(fpclassify(1, 2, 3, 4, 5, -0.0f) == 5);
+ assert(fpclassify(1, 2, 3, 4, 5, 0.0) == 5);
+ assert(fpclassify(1, 2, 3, 4, 5, -0.0L) == 5);
+ return 0;
+}
+#endif
diff --git a/libc/test/include/fpclassify_test.cpp b/libc/test/include/fpclassify_test.cpp
new file mode 100644
index 00000000000000..93c8e3c7c60533
--- /dev/null
+++ b/libc/test/include/fpclassify_test.cpp
@@ -0,0 +1,12 @@
+//===-- Unittest for fpclassify[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 "FpClassifyTest.h"
+#include "include/llvm-libc-macros/math-function-macros.h"
+
+LIST_FPCLASSIFY_TESTS(double, fpclassify)
diff --git a/libc/test/include/fpclassifyf_test.cpp b/libc/test/include/fpclassifyf_test.cpp
new file mode 100644
index 00000000000000..875482a6a233c7
--- /dev/null
+++ b/libc/test/include/fpclassifyf_test.cpp
@@ -0,0 +1,12 @@
+//===-- Unittest for fpclassify[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 "FpClassifyTest.h"
+#include "include/llvm-libc-macros/math-function-macros.h"
+
+LIST_FPCLASSIFY_TESTS(float, fpclassify)
diff --git a/libc/test/include/fpclassifyl_test.cpp b/libc/test/include/fpclassifyl_test.cpp
new file mode 100644
index 00000000000000..6627956a0d35b9
--- /dev/null
+++ b/libc/test/include/fpclassifyl_test.cpp
@@ -0,0 +1,12 @@
+//===-- Unittest for fpclassify[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 "FpClassifyTest.h"
+#include "include/llvm-libc-macros/math-function-macros.h"
+
+LIST_FPCLASSIFY_TESTS(long double, fpclassify)
``````````
</details>
https://github.com/llvm/llvm-project/pull/109519
More information about the libc-commits
mailing list