[libc-commits] [PATCH] D156813: [libc] Fix float to string conversion in 32-bit systems

Mikhail Ramalho via Phabricator via libc-commits libc-commits at lists.llvm.org
Tue Aug 1 10:24:38 PDT 2023


mikhail.ramalho created this revision.
mikhail.ramalho added reviewers: lntue, sivachandra, michaelrj.
Herald added subscribers: libc-commits, asb.
Herald added projects: libc-project, All.
mikhail.ramalho requested review of this revision.

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.

For example, the call __llvm_libc::sprintf(buff, "%f", 1.0), where the
mantissa of 1.0 is 0x10000000000000 which requires 64-bits. If only
32-bits were used in this case, we see errors such as:

  /home/root/llvm-project/libc/test/src/stdio/sprintf_test.cpp:870: FAILURE
        Expected: buff
        Which is: 0.000000
  To be equal to: "1.000000"
        Which is: 1.000000


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D156813

Files:
  libc/src/__support/float_to_string.h


Index: libc/src/__support/float_to_string.h
===================================================================
--- libc/src/__support/float_to_string.h
+++ libc/src/__support/float_to_string.h
@@ -389,8 +389,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);
+  wide_mant[0] = static_cast<uint64_t>(mantissa & UINT64_MAX);
+  wide_mant[1] = static_cast<uint64_t>(mantissa >> 64);
   val = (val * wide_mant) >> shift_amount;
 
   return static_cast<uint32_t>(


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D156813.546121.patch
Type: text/x-patch
Size: 699 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libc-commits/attachments/20230801/80ca2cb1/attachment.bin>


More information about the libc-commits mailing list