Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 28, 2025

Conversation

jdeguire
Copy link
Contributor

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

@llvmbot llvmbot added the libc label Mar 28, 2025
@llvmbot
Copy link
Member

llvmbot commented Mar 28, 2025

@llvm/pr-subscribers-libc

Author: Jesse D (jdeguire)

Changes

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

@lntue lntue changed the title Fix implicit conversion error in DyadicFloat::as_mantissa_type(). [libc] Fix implicit conversion error in DyadicFloat::as_mantissa_type(). Mar 28, 2025
@jdeguire
Copy link
Contributor Author

I don't have write access to the repo, so would somebody need to merge this for me? Thanks!

@lntue lntue merged commit f76254d into llvm:main Mar 28, 2025
18 checks passed
@jdeguire jdeguire deleted the dyadic_float_implicit_conversion_warning branch April 4, 2025 20:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants