Skip to content

[libc] Add support for C23 binary notation in sprintf #80820

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

Rajveer100
Copy link
Member

Resolves Issue #80727

Implementation for representation of binary numbers using b specifier, similar to other languages.

Reference

@llvmbot llvmbot added the libc label Feb 6, 2024
@llvmbot
Copy link
Member

llvmbot commented Feb 6, 2024

@llvm/pr-subscribers-libc

Author: Rajveer Singh Bharadwaj (Rajveer100)

Changes

Resolves Issue #80727

Implementation for representation of binary numbers using b specifier, similar to other languages.

Reference


Full diff: https://github.com/llvm/llvm-project/pull/80820.diff

5 Files Affected:

  • (modified) libc/src/stdio/printf_core/converter.cpp (+3)
  • (modified) libc/src/stdio/printf_core/converter_atlas.h (+3)
  • (added) libc/src/stdio/printf_core/decimal_binary_converter.h (+8)
  • (modified) libc/src/stdio/printf_core/int_converter.h (+1)
  • (modified) libc/src/stdio/printf_core/parser.h (+8)
diff --git a/libc/src/stdio/printf_core/converter.cpp b/libc/src/stdio/printf_core/converter.cpp
index 74a36cbf7432f..39465e0ef7011 100644
--- a/libc/src/stdio/printf_core/converter.cpp
+++ b/libc/src/stdio/printf_core/converter.cpp
@@ -53,6 +53,9 @@ int convert(Writer *writer, const FormatSection &to_conv) {
   case 's':
     return convert_string(writer, to_conv);
   case 'd':
+  case 'b':
+  case 'B':
+    return convert_decimal_binary(writer, to_conv);
   case 'i':
   case 'u':
   case 'o':
diff --git a/libc/src/stdio/printf_core/converter_atlas.h b/libc/src/stdio/printf_core/converter_atlas.h
index 6471f3f2955b7..83f1837537b88 100644
--- a/libc/src/stdio/printf_core/converter_atlas.h
+++ b/libc/src/stdio/printf_core/converter_atlas.h
@@ -22,6 +22,9 @@
 // defines convert_int
 #include "src/stdio/printf_core/int_converter.h"
 
+// defines convert_decimal_binary
+#include "src/stdio/printf_core/decimal_binary_converter.h"
+
 #ifndef LIBC_COPT_PRINTF_DISABLE_FLOAT
 // defines convert_float_decimal
 // defines convert_float_dec_exp
diff --git a/libc/src/stdio/printf_core/decimal_binary_converter.h b/libc/src/stdio/printf_core/decimal_binary_converter.h
new file mode 100644
index 0000000000000..9f75d636753ce
--- /dev/null
+++ b/libc/src/stdio/printf_core/decimal_binary_converter.h
@@ -0,0 +1,8 @@
+//===-- Decimal Binary Converter for printf -----------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
diff --git a/libc/src/stdio/printf_core/int_converter.h b/libc/src/stdio/printf_core/int_converter.h
index 7744d801cbc18..b2e54e4e4f138 100644
--- a/libc/src/stdio/printf_core/int_converter.h
+++ b/libc/src/stdio/printf_core/int_converter.h
@@ -33,6 +33,7 @@ using HexFmt = IntegerToString<uintmax_t, radix::Hex>;
 using HexFmtUppercase = IntegerToString<uintmax_t, radix::Hex::Uppercase>;
 using OctFmt = IntegerToString<uintmax_t, radix::Oct>;
 using DecFmt = IntegerToString<uintmax_t>;
+using BinFmt = IntegerToString<Bin>
 
 LIBC_INLINE constexpr size_t num_buf_size() {
   constexpr auto max = [](size_t a, size_t b) -> size_t {
diff --git a/libc/src/stdio/printf_core/parser.h b/libc/src/stdio/printf_core/parser.h
index ab491655275fb..7ff77c5a6bae2 100644
--- a/libc/src/stdio/printf_core/parser.h
+++ b/libc/src/stdio/printf_core/parser.h
@@ -154,6 +154,10 @@ template <typename ArgProvider> class Parser {
         WRITE_ARG_VAL_SIMPLEST(section.conv_val_raw, int, conv_index);
         break;
       case ('d'):
+      case ('b'):
+      case ('B'):
+        WRITE_ARG_VAL_SIMPLEST(section.conv_val_raw, char *, conv_index);
+        break;
       case ('i'):
       case ('o'):
       case ('x'):
@@ -479,6 +483,10 @@ template <typename ArgProvider> class Parser {
           conv_size = type_desc_from_type<int>();
           break;
         case ('d'):
+        case ('b'):
+        case ('B'):
+          conv_size = type_desc_from_type<void *>();
+          break;
         case ('i'):
         case ('o'):
         case ('x'):

@Rajveer100 Rajveer100 force-pushed the branch-for-issue-80727 branch from ca92677 to 2449a30 Compare February 6, 2024 10:29
@Rajveer100
Copy link
Member Author

@SchrodingerZhu @michaelrj-google

I haven't yet modified decimal_binary_converter.h, for the core functionality itself as I wasn't quite sure about the snippets that needed to be added similar to other headers.

While the rest is implemented, although let me know if I missed out.

@Rajveer100
Copy link
Member Author

Closing this one, @agentcooper has opened a PR which is more close to complete.

@Rajveer100 Rajveer100 closed this Feb 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants