-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
[libc][math] Add unit tests for raising excepts in nextafter #73556
Conversation
@llvm/pr-subscribers-libc Author: Nishant Mittal (nishantwrp) ChangesFollow up to #72763 (comment). Summary
Full diff: https://github.com/llvm/llvm-project/pull/73556.diff 2 Files Affected:
diff --git a/libc/test/src/math/smoke/NextAfterTest.h b/libc/test/src/math/smoke/NextAfterTest.h
index 4e450cf5bc1e9e7..69eb9446a9ffa16 100644
--- a/libc/test/src/math/smoke/NextAfterTest.h
+++ b/libc/test/src/math/smoke/NextAfterTest.h
@@ -17,6 +17,17 @@
#include "test/UnitTest/Test.h"
#include <math.h>
+#define ASSERT_FP_EQ_WITH_EXCEPTION(result, expected, expected_exception) \
+ ASSERT_FP_EQ(result, expected); \
+ ASSERT_FP_EXCEPTION(expected_exception); \
+ LIBC_NAMESPACE::fputil::clear_except(FE_ALL_EXCEPT)
+
+#define ASSERT_FP_EQ_WITH_UNDERFLOW(result, expected) \
+ ASSERT_FP_EQ_WITH_EXCEPTION(result, expected, (FE_INEXACT | FE_UNDERFLOW))
+
+#define ASSERT_FP_EQ_WITH_OVERFLOW(result, expected) \
+ ASSERT_FP_EQ_WITH_EXCEPTION(result, expected, (FE_INEXACT | FE_OVERFLOW))
+
template <typename T>
class NextAfterTestTemplate : public LIBC_NAMESPACE::testing::Test {
using FPBits = LIBC_NAMESPACE::fputil::FPBits<T>;
@@ -53,23 +64,23 @@ class NextAfterTestTemplate : public LIBC_NAMESPACE::testing::Test {
T result = func(x, T(1));
UIntType expected_bits = 1;
T expected = LIBC_NAMESPACE::cpp::bit_cast<T>(expected_bits);
- ASSERT_FP_EQ(result, expected);
+ ASSERT_FP_EQ_WITH_UNDERFLOW(result, expected);
result = func(x, T(-1));
expected_bits = (UIntType(1) << (BIT_WIDTH_OF_TYPE - 1)) + 1;
expected = LIBC_NAMESPACE::cpp::bit_cast<T>(expected_bits);
- ASSERT_FP_EQ(result, expected);
+ ASSERT_FP_EQ_WITH_UNDERFLOW(result, expected);
x = neg_zero;
result = func(x, 1);
expected_bits = 1;
expected = LIBC_NAMESPACE::cpp::bit_cast<T>(expected_bits);
- ASSERT_FP_EQ(result, expected);
+ ASSERT_FP_EQ_WITH_UNDERFLOW(result, expected);
result = func(x, -1);
expected_bits = (UIntType(1) << (BIT_WIDTH_OF_TYPE - 1)) + 1;
expected = LIBC_NAMESPACE::cpp::bit_cast<T>(expected_bits);
- ASSERT_FP_EQ(result, expected);
+ ASSERT_FP_EQ_WITH_UNDERFLOW(result, expected);
// 'from' is max subnormal value.
x = LIBC_NAMESPACE::cpp::bit_cast<T>(max_subnormal);
@@ -80,7 +91,7 @@ class NextAfterTestTemplate : public LIBC_NAMESPACE::testing::Test {
result = func(x, 0);
expected_bits = max_subnormal - 1;
expected = LIBC_NAMESPACE::cpp::bit_cast<T>(expected_bits);
- ASSERT_FP_EQ(result, expected);
+ ASSERT_FP_EQ_WITH_UNDERFLOW(result, expected);
x = -x;
@@ -93,30 +104,30 @@ class NextAfterTestTemplate : public LIBC_NAMESPACE::testing::Test {
expected_bits =
(UIntType(1) << (BIT_WIDTH_OF_TYPE - 1)) + max_subnormal - 1;
expected = LIBC_NAMESPACE::cpp::bit_cast<T>(expected_bits);
- ASSERT_FP_EQ(result, expected);
+ ASSERT_FP_EQ_WITH_UNDERFLOW(result, expected);
// 'from' is min subnormal value.
x = LIBC_NAMESPACE::cpp::bit_cast<T>(min_subnormal);
result = func(x, 1);
expected_bits = min_subnormal + 1;
expected = LIBC_NAMESPACE::cpp::bit_cast<T>(expected_bits);
- ASSERT_FP_EQ(result, expected);
- ASSERT_FP_EQ(func(x, 0), 0);
+ ASSERT_FP_EQ_WITH_UNDERFLOW(result, expected);
+ ASSERT_FP_EQ_WITH_UNDERFLOW(func(x, 0), 0);
x = -x;
result = func(x, -1);
expected_bits =
(UIntType(1) << (BIT_WIDTH_OF_TYPE - 1)) + min_subnormal + 1;
expected = LIBC_NAMESPACE::cpp::bit_cast<T>(expected_bits);
- ASSERT_FP_EQ(result, expected);
- ASSERT_FP_EQ(func(x, 0), T(-0.0));
+ ASSERT_FP_EQ_WITH_UNDERFLOW(result, expected);
+ ASSERT_FP_EQ_WITH_UNDERFLOW(func(x, 0), T(-0.0));
// 'from' is min normal.
x = LIBC_NAMESPACE::cpp::bit_cast<T>(min_normal);
result = func(x, 0);
expected_bits = max_subnormal;
expected = LIBC_NAMESPACE::cpp::bit_cast<T>(expected_bits);
- ASSERT_FP_EQ(result, expected);
+ ASSERT_FP_EQ_WITH_UNDERFLOW(result, expected);
result = func(x, inf);
expected_bits = min_normal + 1;
@@ -127,7 +138,7 @@ class NextAfterTestTemplate : public LIBC_NAMESPACE::testing::Test {
result = func(x, 0);
expected_bits = (UIntType(1) << (BIT_WIDTH_OF_TYPE - 1)) + max_subnormal;
expected = LIBC_NAMESPACE::cpp::bit_cast<T>(expected_bits);
- ASSERT_FP_EQ(result, expected);
+ ASSERT_FP_EQ_WITH_UNDERFLOW(result, expected);
result = func(x, -inf);
expected_bits = (UIntType(1) << (BIT_WIDTH_OF_TYPE - 1)) + min_normal + 1;
@@ -137,10 +148,10 @@ class NextAfterTestTemplate : public LIBC_NAMESPACE::testing::Test {
// 'from' is max normal and 'to' is infinity.
x = LIBC_NAMESPACE::cpp::bit_cast<T>(max_normal);
result = func(x, inf);
- ASSERT_FP_EQ(result, inf);
+ ASSERT_FP_EQ_WITH_OVERFLOW(result, inf);
result = func(-x, -inf);
- ASSERT_FP_EQ(result, -inf);
+ ASSERT_FP_EQ_WITH_OVERFLOW(result, -inf);
// 'from' is infinity.
x = inf;
diff --git a/libc/test/src/math/smoke/NextTowardTest.h b/libc/test/src/math/smoke/NextTowardTest.h
index 4d27592a11422bf..fc37a9d1fd3d95d 100644
--- a/libc/test/src/math/smoke/NextTowardTest.h
+++ b/libc/test/src/math/smoke/NextTowardTest.h
@@ -24,10 +24,10 @@
LIBC_NAMESPACE::fputil::clear_except(FE_ALL_EXCEPT)
#define ASSERT_FP_EQ_WITH_UNDERFLOW(result, expected) \
- ASSERT_FP_EQ_WITH_EXCEPTION(result, expected, FE_INEXACT | FE_UNDERFLOW)
+ ASSERT_FP_EQ_WITH_EXCEPTION(result, expected, (FE_INEXACT | FE_UNDERFLOW))
#define ASSERT_FP_EQ_WITH_OVERFLOW(result, expected) \
- ASSERT_FP_EQ_WITH_EXCEPTION(result, expected, FE_INEXACT | FE_OVERFLOW)
+ ASSERT_FP_EQ_WITH_EXCEPTION(result, expected, (FE_INEXACT | FE_OVERFLOW))
template <typename T>
class NextTowardTestTemplate : public LIBC_NAMESPACE::testing::Test {
|
@@ -24,10 +24,10 @@ | |||
LIBC_NAMESPACE::fputil::clear_except(FE_ALL_EXCEPT) | |||
|
|||
#define ASSERT_FP_EQ_WITH_UNDERFLOW(result, expected) \ | |||
ASSERT_FP_EQ_WITH_EXCEPTION(result, expected, FE_INEXACT | FE_UNDERFLOW) | |||
ASSERT_FP_EQ_WITH_EXCEPTION(result, expected, (FE_INEXACT | FE_UNDERFLOW)) |
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.
This macro is defined something like
#define ASSERT_FP_EXCEPT(expected) do {
// some code
LIBC_NAMESPACE::fputil::test_except(FE_ALL_EXCEPT) & expected
} while(0)
Using ASSERT_FP_EXCEPT(a | b)
generates something like LIBC_NAMESPACE::fputil::test_except(FE_ALL_EXCEPT) & a | b
which is not expected. Instead we want LIBC_NAMESPACE::fputil::test_except(FE_ALL_EXCEPT) & (a | b)
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.
Do you mind updating ASSERT_FP_EXCEPT(expected)
and similar macros to:
LIBC_NAMESPACE::fputil::test_except(FE_ALL_EXCEPT) & (expected)
Thanks,
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.
Yeah, that would be better. Done!
65f0647
to
c9aa14a
Compare
Follow up to #72763 (comment).
Summary
nextafter
.nexttoward
.cc: @lntue