[libc-commits] [libc] 44bf4c8 - [libc] Fix float to string conversion in 32-bit systems

Mikhail R. Gadelha via libc-commits libc-commits at lists.llvm.org
Wed Aug 2 05:51:16 PDT 2023


Author: Mikhail R. Gadelha
Date: 2023-08-02T09:50:45-03:00
New Revision: 44bf4c8944f5496b626958ffe9f29da182f4d9d8

URL: https://github.com/llvm/llvm-project/commit/44bf4c8944f5496b626958ffe9f29da182f4d9d8
DIFF: https://github.com/llvm/llvm-project/commit/44bf4c8944f5496b626958ffe9f29da182f4d9d8.diff

LOG: [libc] Fix float to string conversion in 32-bit systems

In 32-bit systems, sizeof(size_t) is 4, so we fail to build an 128-bit
integer in mul_shift_mod_1e9, which ends up ignoring the top bits in the
mantissa.

This patch fixes the issue by calling the Uint constructor directly. If
it's a system that supports 128-bit integers, the constructor that takes
a value will be called, if the system doesn't support 128-bit integers
(like rv32), mantissa is already a UInt.

Reviewed By: lntue, michaelrj

Differential Revision: https://reviews.llvm.org/D156813

Added: 
    

Modified: 
    libc/src/__support/float_to_string.h

Removed: 
    


################################################################################
diff  --git a/libc/src/__support/float_to_string.h b/libc/src/__support/float_to_string.h
index a729cbf6c49e99..404303a7edc057 100644
--- a/libc/src/__support/float_to_string.h
+++ b/libc/src/__support/float_to_string.h
@@ -387,15 +387,9 @@ LIBC_INLINE uint32_t mul_shift_mod_1e9(const MantissaInt mantissa,
                                        const int32_t shift_amount) {
   constexpr size_t MANT_INT_SIZE = sizeof(MantissaInt) * 8;
   cpp::UInt<MID_INT_SIZE + MANT_INT_SIZE> val(large);
-  // TODO: Find a better way to force __uint128_t to be UInt<128>
-  cpp::UInt<MANT_INT_SIZE> wide_mant(0);
-  wide_mant[0] = static_cast<size_t>(mantissa & (uint64_t(-1)));
-  wide_mant[1] = static_cast<size_t>(mantissa >> 64);
-  val = (val * wide_mant) >> shift_amount;
-
+  val = (val * mantissa) >> shift_amount;
   return static_cast<uint32_t>(
       val.div_uint32_times_pow_2(1000000000, 0).value());
-  // return fast_uint_mod_1e9(val);
 }
 
 } // namespace internal


        


More information about the libc-commits mailing list