[libc-commits] [libc] [libc] fix -Wconversion in float_to_string.h (PR #74369)

Nick Desaulniers via libc-commits libc-commits at lists.llvm.org
Mon Dec 4 13:06:49 PST 2023


https://github.com/nickdesaulniers created https://github.com/llvm/llvm-project/pull/74369

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


>From bc445a3b56696e8c6336a05da49416391c0ea811 Mon Sep 17 00:00:00 2001
From: Nick Desaulniers <ndesaulniers at google.com>
Date: Mon, 4 Dec 2023 13:04:30 -0800
Subject: [PATCH] [libc] fix -Wconversion in float_to_string.h
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

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
---
 libc/src/__support/float_to_string.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

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;



More information about the libc-commits mailing list