[libc-commits] [libc] 3befa46 - [libc] Make printf decimal long doubles use hex

Michael Jones via libc-commits libc-commits at lists.llvm.org
Wed May 3 11:12:02 PDT 2023


Author: Michael Jones
Date: 2023-05-03T11:11:57-07:00
New Revision: 3befa46b912272e65a2d8aa0de956e389cfe3a32

URL: https://github.com/llvm/llvm-project/commit/3befa46b912272e65a2d8aa0de956e389cfe3a32
DIFF: https://github.com/llvm/llvm-project/commit/3befa46b912272e65a2d8aa0de956e389cfe3a32.diff

LOG: [libc] Make printf decimal long doubles use hex

Decimal long doubles are not commonly used, and aren't currently
supported by the algorithm used for decimal float conversions. To avoid
giving incorrect answers, this patch adds a temporary exception to print
long doubles in hexadecimal even when decimal is requested.

Reviewed By: sivachandra

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

Added: 
    

Modified: 
    libc/src/stdio/printf_core/converter.cpp
    libc/src/stdio/printf_core/float_hex_converter.h

Removed: 
    


################################################################################
diff  --git a/libc/src/stdio/printf_core/converter.cpp b/libc/src/stdio/printf_core/converter.cpp
index 950c246c247ed..ee9baf28ea782 100644
--- a/libc/src/stdio/printf_core/converter.cpp
+++ b/libc/src/stdio/printf_core/converter.cpp
@@ -28,6 +28,23 @@ int convert(Writer *writer, const FormatSection &to_conv) {
   if (!to_conv.has_conv)
     return writer->write(to_conv.raw_string);
 
+#ifndef LIBC_COPT_PRINTF_DISABLE_FLOAT
+  // TODO(michaelrj): Undo this once decimal long double support is done.
+  if (to_conv.length_modifier == LengthModifier::L) {
+    switch (to_conv.conv_name) {
+    case 'f':
+    case 'F':
+    case 'e':
+    case 'E':
+    case 'g':
+    case 'G':
+      return convert_float_hex_exp(writer, to_conv);
+    default:
+      break;
+    }
+  }
+#endif // LIBC_COPT_PRINTF_DISABLE_FLOAT
+
   switch (to_conv.conv_name) {
   case '%':
     return writer->write("%");

diff  --git a/libc/src/stdio/printf_core/float_hex_converter.h b/libc/src/stdio/printf_core/float_hex_converter.h
index faf97b5b5dd9e..a26a3e796e4df 100644
--- a/libc/src/stdio/printf_core/float_hex_converter.h
+++ b/libc/src/stdio/printf_core/float_hex_converter.h
@@ -29,9 +29,9 @@ using MantissaInt = fputil::FPBits<long double>::UIntType;
 LIBC_INLINE int convert_float_hex_exp(Writer *writer,
                                       const FormatSection &to_conv) {
   // All of the letters will be defined relative to variable a, which will be
-  // the appropriate case based on the name of the conversion.
-  // Since the name of the conversion is also 'a', we can just use it directly.
-  const char a = to_conv.conv_name;
+  // the appropriate case based on the name of the conversion. This converts any
+  // conversion name into the letter 'a' with the appropriate case.
+  const char a = (to_conv.conv_name & 32) | 'A';
 
   bool is_negative;
   int exponent;


        


More information about the libc-commits mailing list