Skip to content

[libc][math] Fix division by infinity in fputil::div #152092

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 2 commits into from
Aug 5, 2025

Conversation

krishna2803
Copy link
Contributor

No description provided.

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

llvmbot commented Aug 5, 2025

@llvm/pr-subscribers-libc

Author: Krishna Pandey (krishna2803)

Changes

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

2 Files Affected:

  • (modified) libc/src/__support/FPUtil/generic/div.h (+1-1)
  • (modified) libc/test/src/math/smoke/DivTest.h (+5)
diff --git a/libc/src/__support/FPUtil/generic/div.h b/libc/src/__support/FPUtil/generic/div.h
index f0e405772e9fa..0891ae010ce20 100644
--- a/libc/src/__support/FPUtil/generic/div.h
+++ b/libc/src/__support/FPUtil/generic/div.h
@@ -78,7 +78,7 @@ div(InType x, InType y) {
     }
 
     if (y_bits.is_inf())
-      return OutFPBits::inf(result_sign).get_val();
+      return OutFPBits::zero(result_sign).get_val();
 
     if (y_bits.is_zero()) {
       if (x_bits.is_zero()) {
diff --git a/libc/test/src/math/smoke/DivTest.h b/libc/test/src/math/smoke/DivTest.h
index d8074797449e9..ff82f68827a12 100644
--- a/libc/test/src/math/smoke/DivTest.h
+++ b/libc/test/src/math/smoke/DivTest.h
@@ -47,6 +47,11 @@ class DivTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
     EXPECT_FP_EQ(neg_inf, func(in.neg_inf, in.zero));
     EXPECT_FP_EQ(neg_inf, func(in.inf, in.neg_zero));
     EXPECT_FP_EQ(inf, func(in.neg_inf, in.neg_zero));
+    EXPECT_FP_EQ(zero, func(in.min_normal, in.inf));
+    EXPECT_FP_EQ(zero, func(in.zero, in.inf));
+    EXPECT_FP_EQ(zero, func(in.neg_zero, in.neg_inf));
+    EXPECT_FP_EQ(neg_zero, func(in.min_normal, in.neg_inf));
+    EXPECT_FP_EQ(neg_zero, func(in.zero, in.neg_inf));
   }
 
   void test_division_by_zero(DivFunc func) {

@lntue lntue merged commit 52b7b22 into llvm:main Aug 5, 2025
21 checks passed
@krishna2803 krishna2803 deleted the fix-div branch August 5, 2025 12:44
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.

3 participants