Skip to content

[libc] fix -Wconversion in float_to_string.h #74369

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 2 commits into from
Dec 4, 2023

Conversation

nickdesaulniers
Copy link
Member

Fixes:
libc/src/__support/float_to_string.h:551:48: error: conversion from ‘long
unsigned int’ to ‘int32_t’ {aka ‘int’} may change value [-Werror=conversion]
551 | const int32_t shift_amount = SHIFT_CONST + (-exponent - IDX_SIZE * idx);
| ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Observed in gcc fullbuilds.

IDX_SIZE is a size_t (aka 'long unsigned int'), but has the value 128, so the
expression is undergoing implicit promotion.

Link: https://lab.llvm.org/buildbot/#/builders/250/builds/14891

Fixes:
libc/src/__support/float_to_string.h:551:48: error: conversion from ‘long
unsigned int’ to ‘int32_t’ {aka ‘int’} may change value [-Werror=conversion]
  551 |       const int32_t shift_amount = SHIFT_CONST + (-exponent - IDX_SIZE * idx);
      |                                    ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Observed in gcc fullbuilds.

IDX_SIZE is a size_t (aka 'long unsigned int'), but has the value 128, so the
expression is undergoing implicit promotion.

Link: https://lab.llvm.org/buildbot/#/builders/250/builds/14891
@llvmbot llvmbot added the libc label Dec 4, 2023
@llvmbot
Copy link
Member

llvmbot commented Dec 4, 2023

@llvm/pr-subscribers-libc

Author: Nick Desaulniers (nickdesaulniers)

Changes

Fixes:
libc/src/__support/float_to_string.h:551:48: error: conversion from ‘long
unsigned int’ to ‘int32_t’ {aka ‘int’} may change value [-Werror=conversion]
551 | const int32_t shift_amount = SHIFT_CONST + (-exponent - IDX_SIZE * idx);
| ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Observed in gcc fullbuilds.

IDX_SIZE is a size_t (aka 'long unsigned int'), but has the value 128, so the
expression is undergoing implicit promotion.

Link: https://lab.llvm.org/buildbot/#/builders/250/builds/14891


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

1 Files Affected:

  • (modified) libc/src/__support/float_to_string.h (+1-1)
diff --git a/libc/src/__support/float_to_string.h b/libc/src/__support/float_to_string.h
index 1bb4e5c5b9246..f1471d0710277 100644
--- a/libc/src/__support/float_to_string.h
+++ b/libc/src/__support/float_to_string.h
@@ -548,7 +548,7 @@ class FloatToString {
 
       val = POW10_SPLIT_2[p];
 #endif
-      const int32_t shift_amount = SHIFT_CONST + (-exponent - IDX_SIZE * idx);
+      const int32_t shift_amount = SHIFT_CONST + (-exponent - static_cast<int>(IDX_SIZE) * idx);
       uint32_t digits =
           internal::mul_shift_mod_1e9(mantissa, val, shift_amount);
       return digits;

Copy link

github-actions bot commented Dec 4, 2023

✅ With the latest revision this PR passed the C/C++ code formatter.

@nickdesaulniers nickdesaulniers merged commit 0d59cfc into llvm:main Dec 4, 2023
@nickdesaulniers nickdesaulniers deleted the f2s branch December 4, 2023 21:12
michaelrj-google added a commit to michaelrj-google/llvm-project that referenced this pull request Dec 4, 2023
Patch llvm#74369 fixed one of the fixed in the previous commit. This rebases
on top of that commit and also adjusts the casts in that file to better
match.
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