Skip to content

[libc][math] Fix subtraction of infinity in fputil::add_or_sub #152017

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

Merged
merged 4 commits into from
Aug 4, 2025

Conversation

krishna2803
Copy link
Contributor

No description provided.

@llvmbot llvmbot added the libc label Aug 4, 2025
@llvmbot
Copy link
Member

llvmbot commented Aug 4, 2025

@llvm/pr-subscribers-libc

Author: Krishna Pandey (krishna2803)

Changes

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

2 Files Affected:

  • (modified) libc/src/__support/FPUtil/generic/add_sub.h (+6-2)
  • (modified) libc/test/src/math/smoke/SubTest.h (+2)
diff --git a/libc/src/__support/FPUtil/generic/add_sub.h b/libc/src/__support/FPUtil/generic/add_sub.h
index 7205d8d41f5bd..d4a4129496640 100644
--- a/libc/src/__support/FPUtil/generic/add_sub.h
+++ b/libc/src/__support/FPUtil/generic/add_sub.h
@@ -87,8 +87,12 @@ add_or_sub(InType x, InType y) {
       return OutFPBits::inf(x_bits.sign()).get_val();
     }
 
-    if (y_bits.is_inf())
-      return OutFPBits::inf(y_bits.sign()).get_val();
+    if (y_bits.is_inf()) {
+      if constexpr (IsSub)
+        return OutFPBits::inf(y_bits.sign().negate()).get_val();
+      else
+        return OutFPBits::inf(y_bits.sign()).get_val();
+    }
 
     if (x_bits.is_zero()) {
       if (y_bits.is_zero()) {
diff --git a/libc/test/src/math/smoke/SubTest.h b/libc/test/src/math/smoke/SubTest.h
index c344db28b7fc6..5fafde00778c0 100644
--- a/libc/test/src/math/smoke/SubTest.h
+++ b/libc/test/src/math/smoke/SubTest.h
@@ -47,6 +47,8 @@ class SubTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
     EXPECT_FP_EQ(neg_inf, func(in.neg_inf, in.zero));
     EXPECT_FP_EQ(inf, func(in.inf, in.neg_zero));
     EXPECT_FP_EQ(neg_inf, func(in.neg_inf, in.neg_zero));
+    EXPECT_FP_EQ(neg_inf, func(in.zero, in.inf));
+    EXPECT_FP_EQ(inf, func(in.zero, in.neg_inf));
   }
 
   void test_invalid_operations(SubFunc func) {

@overmighty overmighty self-requested a review August 4, 2025 19:26
@overmighty overmighty changed the title [libc][math] Fix subtraction of infinity [libc][math] Fix subtraction of infinity in fputil::add_or_sub Aug 4, 2025
@lntue lntue merged commit 771d9ab into llvm:main Aug 4, 2025
19 checks passed
@krishna2803 krishna2803 deleted the fix-addsub branch August 5, 2025 05:25
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.

4 participants