[libc-commits] [PATCH] D148179: [libc] Make printf decimal long doubles use hex

Michael Jones via Phabricator via libc-commits libc-commits at lists.llvm.org
Wed Apr 12 15:43:34 PDT 2023


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

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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D148179

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


Index: libc/src/stdio/printf_core/float_hex_converter.h
===================================================================
--- libc/src/stdio/printf_core/float_hex_converter.h
+++ libc/src/stdio/printf_core/float_hex_converter.h
@@ -30,8 +30,7 @@
                                       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;
+  const char a = (to_conv.conv_name & 32) | 'A';
 
   bool is_negative;
   int exponent;
Index: libc/src/stdio/printf_core/converter.cpp
===================================================================
--- libc/src/stdio/printf_core/converter.cpp
+++ libc/src/stdio/printf_core/converter.cpp
@@ -28,6 +28,23 @@
   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("%");


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D148179.512989.patch
Type: text/x-patch
Size: 1479 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libc-commits/attachments/20230412/0f40ac9c/attachment-0001.bin>


More information about the libc-commits mailing list