Skip to content
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

Merged
merged 1 commit into from
Nov 28, 2023

Conversation

nishantwrp
Copy link
Contributor

@nishantwrp nishantwrp commented Nov 27, 2023

Follow up to #72763 (comment).

Summary

  • Add unit tests for raising excepts in nextafter.
  • Fixed a bug in testing code for nexttoward.

cc: @lntue

@llvmbot llvmbot added the libc label Nov 27, 2023
@llvmbot
Copy link
Collaborator

llvmbot commented Nov 27, 2023

@llvm/pr-subscribers-libc

Author: Nishant Mittal (nishantwrp)

Changes

Follow up to #72763 (comment).

Summary

  • Add unit tests for raising excepts in nextafter.
  • Fixed a bug in testing code for nexttoward.

Full diff: https://github.com/llvm/llvm-project/pull/73556.diff

2 Files Affected:

  • (modified) libc/test/src/math/smoke/NextAfterTest.h (+25-14)
  • (modified) libc/test/src/math/smoke/NextTowardTest.h (+2-2)
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))
Copy link
Contributor Author

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)

Copy link
Contributor

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,

Copy link
Contributor Author

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!

@lntue lntue merged commit 18fd6df into llvm:main Nov 28, 2023
3 checks passed
@nishantwrp nishantwrp deleted the except-tests-nextafter branch November 30, 2023 18:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants