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

Jesse D via libc-commits libc-commits at lists.llvm.org
Thu Mar 27 23:49:59 PDT 2025


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

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

>From 96e24d70d6b1cfdc0ba0ef84cfab2f793f7c8876 Mon Sep 17 00:00:00 2001
From: Jesse DeGuire <jesse.a.deguire at gmail.com>
Date: Fri, 28 Mar 2025 01:46:55 -0500
Subject: [PATCH] Fix implicit conversion error in
 DyadicFloat::as_mantissa_type().

---
 libc/src/__support/FPUtil/dyadic_float.h | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

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()) {



More information about the libc-commits mailing list