-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[libc][math] implement signbit
and math macro unit tests
#97791
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
Conversation
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write If you have received no comments on your PR for a week, you can request a review If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
…d system. verifying 1 case for now
…ong double` types
@llvm/pr-subscribers-libc Author: Akiel Aries (akielaries) ChangesThis PR addresses #96322 and implements the Full diff: https://github.com/llvm/llvm-project/pull/97791.diff 5 Files Affected:
diff --git a/libc/include/llvm-libc-macros/CMakeLists.txt b/libc/include/llvm-libc-macros/CMakeLists.txt
index f6af11abd4dd7..666b6337ddf37 100644
--- a/libc/include/llvm-libc-macros/CMakeLists.txt
+++ b/libc/include/llvm-libc-macros/CMakeLists.txt
@@ -277,3 +277,9 @@ add_macro_header(
HDR
stdckdint-macros.h
)
+
+add_macro_header(
+ generic_math_macros
+ HDR
+ generic-math-macros.h
+)
diff --git a/libc/include/llvm-libc-macros/generic-math-macros.h b/libc/include/llvm-libc-macros/generic-math-macros.h
new file mode 100644
index 0000000000000..8fec2cc6c02ea
--- /dev/null
+++ b/libc/include/llvm-libc-macros/generic-math-macros.h
@@ -0,0 +1,20 @@
+//===-- Definition of macros from math.h ----------------------------------===//
+//
+// 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_MACROS_GENERIC_MATH_MACROS_H
+#define LLVM_LIBC_MACROS_GENERIC_MATH_MACROS_H
+
+#define isfinite(x) __builtin_isfinite(x)
+#define isinf(x) __builtin_isinf(x)
+#define isnan(x) __builtin_isnan(x)
+#define signbit(x) \
+ ((sizeof(x) == sizeof(float)) ? __builtin_signbitf(x) \
+ : (sizeof(x) == sizeof(double)) ? __builtin_signbit(x) \
+ : __builtin_signbitl(x))
+
+#endif // LLVM_LIBC_MACROS_GENERIC_MATH_MACROS_H
diff --git a/libc/include/llvm-libc-macros/math-macros.h b/libc/include/llvm-libc-macros/math-macros.h
index 47838969d59ae..bcda32a615b62 100644
--- a/libc/include/llvm-libc-macros/math-macros.h
+++ b/libc/include/llvm-libc-macros/math-macros.h
@@ -51,9 +51,4 @@
#define math_errhandling (MATH_ERRNO | MATH_ERREXCEPT)
#endif
-// TODO: Move generic functional math macros to a separate header file.
-#define isfinite(x) __builtin_isfinite(x)
-#define isinf(x) __builtin_isinf(x)
-#define isnan(x) __builtin_isnan(x)
-
#endif // LLVM_LIBC_MACROS_MATH_MACROS_H
diff --git a/libc/test/include/CMakeLists.txt b/libc/test/include/CMakeLists.txt
index 03c31855e352b..7a805fc3a2e66 100644
--- a/libc/test/include/CMakeLists.txt
+++ b/libc/test/include/CMakeLists.txt
@@ -79,3 +79,13 @@ add_libc_test(
DEPENDS
libc.include.llvm-libc-macros.stdckdint_macros
)
+
+add_libc_test(
+ generic_math_macros_test
+ SUITE
+ libc_include_tests
+ SRCS
+ generic-math-macros_test.cpp
+ DEPENDS
+ libc.include.llvm-libc-macros.generic_math_macros
+)
diff --git a/libc/test/include/generic-math-macros_test.cpp b/libc/test/include/generic-math-macros_test.cpp
new file mode 100644
index 0000000000000..d7b37c7ba4e8b
--- /dev/null
+++ b/libc/test/include/generic-math-macros_test.cpp
@@ -0,0 +1,87 @@
+//===-- Unittests for stdbit ----------------------------------------------===//
+//
+// 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 "test/UnitTest/Test.h"
+
+/*
+ * The intent of this test is validate that the generic math macros operate as
+ * intended
+ */
+#include "include/llvm-libc-macros/generic-math-macros.h"
+
+// INF can be defined as a number with zeroed out mantissa and 1s in the
+// exponent
+static uint32_t positive_infinity = 0x7F800000;
+static uint32_t negative_infinity = 0xFF800000;
+static const float pos_inf = *(float *) &positive_infinity;
+static const float neg_inf = *(float *) &negative_infinity;
+
+// NaN can be defined as a number with all 1s in the exponent
+static uint32_t positive_nan = 0x7F800001;
+static uint32_t negative_nan = 0xFF800001;
+static const float pos_nan = *(float *) &positive_nan;
+static const float neg_nan = *(float *) &negative_nan;
+
+#define PI 3.14159265358979323846
+#define CASE_DIV_BY_ZERO PI / 0.0
+#define CASE_DIV_BY_POS_INF PI / pos_inf
+#define CASE_DIV_BY_NEG_INF PI / neg_inf
+#define CASE_MULT_ZERO_BY_POS_INF 0 * pos_inf
+#define CASE_MULT_ZERO_BY_NEG_INF 0 * neg_inf
+
+/*
+ * As with IEEE 754-1985, the biased-exponent field is filled with all 1 bits
+ * to indicate either infinity (trailing significand field = 0) or a NaN
+ * (trailing significand field ≠ 0)
+ */
+
+TEST(LlvmLibcGenericMath, TypeGenericMacroMathIsfinite) {
+ EXPECT_EQ(isfinite(pos_inf), 0);
+ EXPECT_EQ(isfinite(neg_inf), 0);
+ EXPECT_EQ(isfinite(pos_nan), 0);
+ EXPECT_EQ(isfinite(neg_nan), 0);
+ EXPECT_EQ(isfinite(CASE_DIV_BY_ZERO), 0);
+ EXPECT_EQ(isfinite(PI), 1);
+}
+
+TEST(LlvmLibcGenericMath, TypeGenericMacroMathIsinf) {
+ EXPECT_EQ(isinf(PI), 0);
+ EXPECT_EQ(isinf(CASE_DIV_BY_POS_INF), 0);
+ EXPECT_EQ(isinf(CASE_DIV_BY_NEG_INF), 0);
+ EXPECT_EQ(isinf(CASE_MULT_ZERO_BY_POS_INF), 0);
+ EXPECT_EQ(isinf(CASE_MULT_ZERO_BY_NEG_INF), 0);
+ EXPECT_EQ(isinf(pos_inf), 1);
+ EXPECT_EQ(isinf(neg_inf), 1);
+ EXPECT_EQ(isinf(CASE_DIV_BY_ZERO), 1);
+}
+
+TEST(LlvmLibcGenericMath, TypeGenericMacroMathIsnan) {
+ EXPECT_EQ(isnan(PI), 0);
+ EXPECT_EQ(isnan(CASE_DIV_BY_ZERO), 0);
+ EXPECT_EQ(isnan(CASE_DIV_BY_POS_INF), 0);
+ EXPECT_EQ(isnan(CASE_DIV_BY_NEG_INF), 0);
+ EXPECT_EQ(isnan(pos_nan), 1);
+ EXPECT_EQ(isnan(neg_nan), 1);
+ EXPECT_EQ(isnan(CASE_MULT_ZERO_BY_POS_INF), 1);
+ EXPECT_EQ(isnan(CASE_MULT_ZERO_BY_NEG_INF), 1);
+ EXPECT_EQ(isnan(pos_inf / neg_inf), 1);
+}
+
+TEST(LlvmLibcGenericMath, TypeGenericMacroMathSignbit) {
+ EXPECT_EQ(signbit(static_cast<float>(PI)), 0);
+ EXPECT_EQ(signbit(static_cast<double>(PI)), 0);
+ EXPECT_EQ(signbit(static_cast<long double>(PI)), 0);
+ EXPECT_EQ(signbit(pos_inf), 0);
+ EXPECT_EQ(signbit(pos_nan), 0);
+
+ EXPECT_EQ(signbit(static_cast<float>(-PI)), 1);
+ EXPECT_EQ(signbit(static_cast<double>(-PI)), 1);
+ EXPECT_EQ(signbit(static_cast<long double>(-PI)), 1);
+ EXPECT_EQ(signbit(neg_inf), 1);
+ EXPECT_EQ(signbit(neg_nan), 1);
+}
|
… for macros signbit, isinf, isfinite, and isnan. curious on replicating this unit test abstraction piece for the C tests and for converting the PI define into a hex representation
signbit
signbit
and math macro unit tests
@akielaries Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested Please check whether problems have been caused by your change specifically, as How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/131/builds/2049 Here is the relevant piece of the build log for the reference:
|
I don't think the I think this change should have really just added tests for |
FYI I have a PR #98274 for the |
Makes sense. Original issue #96322 had mentioned
So I figured closing out the issue in one PR was reasonable. I had also planned to remove the TODO that was in |
So I think there are 2 different things: the |
//===-- Utility class to test the isnan macro [f|l] -------------*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license nanormation. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just noticed that this file has wrong copyright header.
This PR resolves llvm#96322 and implements the `signbit` macro under a new header `generic-math-macros.h`. This also removed the `TODO` in `math-macros.h` and moves `isfinite`, `isinf`, and `isnan` to the same generic maths header. Finally, a test file `generic-math-macros_test.cpp` that adds coverage to the above 4 macros. Fixes llvm#96322.
This PR resolves #96322 and implements the
signbit
macro under a new headergeneric-math-macros.h
. This also removed theTODO
inmath-macros.h
and movesisfinite
,isinf
, andisnan
to the same generic maths header. Finally, a test filegeneric-math-macros_test.cpp
that adds coverage to the above 4 macros.