[compiler-rt] [compiler-rt] Implement __extendxftf2 and __trunctfxf2 for x86_64 (PR #66918)

Eli Friedman via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 29 13:48:11 PDT 2023


================
@@ -81,19 +94,37 @@ static __inline dst_t __extendXfYf2__(src_t a) {
     // bit (if needed) and right-aligning the rest of the trailing NaN
     // payload field.
     absResult = (dst_rep_t)dstInfExp << dstSigBits;
-    absResult |= (dst_rep_t)(aAbs & srcQNaN) << (dstSigBits - srcSigBits);
-    absResult |= (dst_rep_t)(aAbs & srcNaNCode) << (dstSigBits - srcSigBits);
+    absResult |= (dst_rep_t)(aAbs & srcQNaN)
+                 << (dstSigFracBits - srcSigFracBits);
+    absResult |= (dst_rep_t)(aAbs & srcNaNCode)
+                 << (dstSigFracBits - srcSigFracBits);
   }
 
   else if (aAbs) {
     // a is denormal.
-    // renormalize the significand and clear the leading bit, then insert
-    // the correct adjusted exponent in the destination type.
-    const int scale = src_rep_t_clz(aAbs) - src_rep_t_clz(srcMinNormal);
-    absResult = (dst_rep_t)aAbs << (dstSigBits - srcSigBits + scale);
-    absResult ^= dstMinNormal;
-    const int resultExponent = dstExpBias - srcExpBias - scale + 1;
-    absResult |= (dst_rep_t)resultExponent << dstSigBits;
+    if (srcExpBits == dstExpBits) {
+      // When we extend a denormal F80 to F128 this branch is always taken.
+      // In particular, we rely on the fact that both formats use 15-bit
+      // exponent. Therefore, the other branch is meaningless and marked with
+      // __builtin_unreachable.
+      // Insert the significand into the correct location in the destination
+      // type.
+      absResult = ((dst_rep_t)aAbs & srcSigFracMask)
+                  << (dstSigFracBits - srcSigFracBits);
+    } else {
+#ifndef src_rep_t_clz
+      // If src_rep_t_clz is not defined this branch must be unreachable.
+      __builtin_unreachable();
+#define src_rep_t_clz (int)
----------------
efriedma-quic wrote:

Can you just use `#else` here?  (Also, explicitly note that this codepath is currently unused for fp80.)

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


More information about the llvm-commits mailing list