[llvm] [APFloat] Add APFloat::format (PR #99088)

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 8 00:28:54 PDT 2024


================
@@ -5217,6 +5249,50 @@ bool DoubleAPFloat::getExactInverse(APFloat *inv) const {
   return Ret;
 }
 
+bool DoubleAPFloat::decomposeToIntegerAndPowerOf2(APInt &I, int &exp) const {
+  APInt a, b;
+  int aExp, bExp;
+  bool aOK = Floats[0].decomposeToIntegerAndPowerOf2(a, aExp);
+  bool bOK = Floats[1].decomposeToIntegerAndPowerOf2(b, bExp);
+
+  if (aOK && bOK) {
+    if (aExp > bExp) {
+      int deltaExp = aExp - bExp;
+      // shift a left to reduce its exponent to be the same as b
+      a = a.zext(a.getBitWidth() + deltaExp);
+      a <<= deltaExp;
+      exp = bExp;
+    } else if (bExp > aExp) {
+      int deltaExp = bExp - aExp;
+      // shift b left to reduce its exponent to be the same as a
+      b = b.zext(b.getBitWidth() + deltaExp);
+      b <<= deltaExp;
+      exp = aExp;
+    } else {
+      exp = aExp;
+    }
+    // now do the addition
+
+    // need to extend the operands to be the same length,
+    // and allow a bit for a possible carry (hence the +1).
+    // Note: the +1 means that the new length is greater
+    //       than the lengths of both a and b.
+    //       That means that we can use zext safely.
+    int bw = 1 + std::max(a.getBitWidth(), b.getBitWidth());
+    a = a.zext(bw);
+    b = b.zext(bw);
+    I = a + b;
+
+    // now check for trailing zeros
----------------
arsenm wrote:

```suggestion
    // Now check for trailing zeros
```

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


More information about the llvm-commits mailing list