[libc-commits] [libc] Fix implicit conversion error in DyadicFloat::as_mantissa_type(). (PR #133383)

via libc-commits libc-commits at lists.llvm.org
Thu Mar 27 23:50:30 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libc

Author: Jesse D (jdeguire)

<details>
<summary>Changes</summary>

This is the same fix that was recently applied to as_mantissa_type_rounded(), but for as_mantissa_type().

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


1 Files Affected:

- (modified) libc/src/__support/FPUtil/dyadic_float.h (+6-1) 


``````````diff
diff --git a/libc/src/__support/FPUtil/dyadic_float.h b/libc/src/__support/FPUtil/dyadic_float.h
index 65d55e16d95ec..2d181134bc2ae 100644
--- a/libc/src/__support/FPUtil/dyadic_float.h
+++ b/libc/src/__support/FPUtil/dyadic_float.h
@@ -434,7 +434,12 @@ template <size_t Bits> struct DyadicFloat {
     if (exponent > 0) {
       new_mant <<= exponent;
     } else {
-      new_mant >>= (-exponent);
+      // Cast the exponent to size_t before negating it, rather than after,
+      // to avoid undefined behavior negating INT_MIN as an integer (although
+      // exponents coming in to this function _shouldn't_ be that large). The
+      // result should always end up as a positive size_t.
+      size_t shift = -static_cast<size_t>(exponent);
+      new_mant >>= shift;
     }
 
     if (sign.is_neg()) {

``````````

</details>


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


More information about the libc-commits mailing list