[llvm] [Support][APInt] Fix sign extension, exponent and mantissa in APInt::roundToDouble (PR #192451)

via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 16 06:42:11 PDT 2026


github-actions[bot] wrote:

<!--LLVM CODE FORMAT COMMENT: {clang-format}-->


:warning: C/C++ code formatter, clang-format found issues in your code. :warning:

<details>
<summary>
You can test this locally with the following command:
</summary>

``````````bash
git-clang-format --diff origin/main HEAD --extensions cpp -- llvm/lib/Support/APInt.cpp llvm/unittests/ADT/APIntTest.cpp --diff_from_common_commit
``````````

:warning:
The reproduction instructions above might return results for more than one PR
in a stack if you are using a stacked PR workflow. You can limit the results by
changing `origin/main` to the base branch/commit you want to compare against.
:warning:

</details>

<details>
<summary>
View the diff from clang-format here.
</summary>

``````````diff
diff --git a/llvm/lib/Support/APInt.cpp b/llvm/lib/Support/APInt.cpp
index 1d1765a9e..1f21c82b8 100644
--- a/llvm/lib/Support/APInt.cpp
+++ b/llvm/lib/Support/APInt.cpp
@@ -922,7 +922,7 @@ double APInt::roundToDouble(bool isSigned) const {
   // bits we are using (minus 1 to account for the fact that the
   // exponent is on 2). Note that the sign bit is gone since we
   // constructed the absolute value.
-  uint64_t exp = n-1;
+  uint64_t exp = n - 1;
 
   // Return infinity for exponent overflow
   if (exp > 1023) {
@@ -949,7 +949,7 @@ double APInt::roundToDouble(bool isSigned) const {
   }
 
   // The leading bit of mantissa is implicit, so get rid of it.
-  mantissa &= ~(1ULL << std::min(n-1, 51U));
+  mantissa &= ~(1ULL << std::min(n - 1, 51U));
   uint64_t sign = isNeg ? (1ULL << (APINT_BITS_PER_WORD - 1)) : 0;
   uint64_t I = sign | (exp << 52) | mantissa;
   return bit_cast<double>(I);
diff --git a/llvm/unittests/ADT/APIntTest.cpp b/llvm/unittests/ADT/APIntTest.cpp
index d5fe5b30b..dd5ee556f 100644
--- a/llvm/unittests/ADT/APIntTest.cpp
+++ b/llvm/unittests/ADT/APIntTest.cpp
@@ -3986,28 +3986,35 @@ TEST(APIntTest, roundToDouble) {
   EXPECT_EQ(APInt(64, 0, false).roundToDouble(false), 0.0);
   EXPECT_EQ(APInt(64, 1, false).roundToDouble(false), 1.0);
   EXPECT_EQ(APInt(64, 2, false).roundToDouble(false), 2.0);
-  EXPECT_EQ(APInt(64, 1ULL << 63, false).roundToDouble(false), 9223372036854775808.0);
+  EXPECT_EQ(APInt(64, 1ULL << 63, false).roundToDouble(false),
+            9223372036854775808.0);
 
   // Single-word, negative
   EXPECT_EQ(APInt(64, 0, true).roundToDouble(true), 0.0);
   EXPECT_EQ(APInt(64, -1, true).roundToDouble(true), -1.0);
   EXPECT_EQ(APInt(64, -2, true).roundToDouble(true), -2.0);
-  EXPECT_EQ(APInt(64, 1ULL << 63, true).roundToDouble(true), -9223372036854775808.0);
+  EXPECT_EQ(APInt(64, 1ULL << 63, true).roundToDouble(true),
+            -9223372036854775808.0);
 
   // Multi-word, positive, active bits in first word
   EXPECT_EQ(APInt(65, 0, false).roundToDouble(false), 0.0);
   EXPECT_EQ(APInt(65, 1, false).roundToDouble(false), 1.0);
   EXPECT_EQ(APInt(65, 2, false).roundToDouble(false), 2.0);
-  EXPECT_EQ(APInt(65, 1ULL << 63, false).roundToDouble(true), 9223372036854775808.0);
+  EXPECT_EQ(APInt(65, 1ULL << 63, false).roundToDouble(true),
+            9223372036854775808.0);
 
   // Multi-word, positive, active bits outside first word
-  EXPECT_EQ(APInt(65, "18446744073709551616" /* 2^64 */, 10).roundToDouble(false), 18446744073709551616.0);
+  EXPECT_EQ(
+      APInt(65, "18446744073709551616" /* 2^64 */, 10).roundToDouble(false),
+      18446744073709551616.0);
 
   // Multi-word, negative
   EXPECT_EQ(APInt(65, 0, true).roundToDouble(true), 0.0);
 
   EXPECT_EQ(APInt(65, -1, true).roundToDouble(true), -1.0);
   EXPECT_EQ(APInt(65, -2, true).roundToDouble(true), -2.0);
-  EXPECT_EQ(APInt(65, "18446744073709551616" /* 2^64 */, 10).roundToDouble(true), -18446744073709551616.0);
+  EXPECT_EQ(
+      APInt(65, "18446744073709551616" /* 2^64 */, 10).roundToDouble(true),
+      -18446744073709551616.0);
 }
 } // end anonymous namespace

``````````

</details>


https://github.com/llvm/llvm-project/pull/192451


More information about the llvm-commits mailing list