Implement C23 printf 'wf' length modifiers

wfN: Specifies that a following b, d, i, o, u, x, or X conversion specifier applies to a fastest minimum-width integer argument with a specific width where N is a positive decimal integer with no leading zeros (the argument will have been promoted according to the integer promotions, but its value shall be converted to the unpromoted type); or that a following n conversion specifier applies to a pointer to a fastest minimum-width integer type argument with a width of N bits. All fastest minimum-width integer types (7.22.1.3) defined in the header <stdint.h> shall be supported. Other supported values of N are implementation-defined.

Bug: b/271903607
Test: adb shell
Change-Id: Ida36d5a50af2a46fd04cb5fe039793d8872f9f3b
diff --git a/libc/stdio/printf_common.h b/libc/stdio/printf_common.h
index b0055f0..365728b 100644
--- a/libc/stdio/printf_common.h
+++ b/libc/stdio/printf_common.h
@@ -528,17 +528,29 @@
       case 'b':
         ADDUARG();
         break;
-      case 'w':
+      case 'w': {
         n = 0;
+        bool fast = false;
         ch = *fmt++;
+        if (ch == 'f') {
+          fast = true;
+          ch = *fmt++;
+        }
         while (is_digit(ch)) {
           APPEND_DIGIT(n, ch);
           ch = *fmt++;
         }
         if (n == 64) {
           flags |= LLONGINT;
+        } else {
+          if (n != 8 && fast) {
+#if defined(__LP64__)
+            flags |= LLONGINT;
+#endif
+          }
         }
         goto reswitch;
+      }
       default: /* "%?" prints ?, unless ? is NUL */
         if (ch == '\0') goto done;
         break;
@@ -824,4 +836,15 @@
     return convbuf;
   }
 
+  // Trasnlate a fixed size integer argument for the %w/%wf format to a
+  // flag representation. Supported sizes are 8, 16, 32, and 64 so far.
+  // See details in bionic/libc/include/stdint.h
+  static int w_to_flag(int size, bool fast) {
+    static constexpr int fast_size = sizeof(void*) == 8 ? LLONGINT : 0;
+    if (size == 8) return CHARINT;
+    if (size == 16) return fast ? fast_size : SHORTINT;
+    if (size == 32) return fast ? fast_size : 0;
+    if (size == 64) return LLONGINT;
+    __fortify_fatal("%%w%s%d is unsupported", fast ? "f" : "", size);
+  }
 };
diff --git a/libc/stdio/vfprintf.cpp b/libc/stdio/vfprintf.cpp
index b7c68dd..994269b 100644
--- a/libc/stdio/vfprintf.cpp
+++ b/libc/stdio/vfprintf.cpp
@@ -521,34 +521,21 @@
         _umax = UARG();
         base = DEC;
         goto nosign;
-      case 'w':
+      case 'w': {
         n = 0;
+        bool fast = false;
         ch = *fmt++;
+        if (ch == 'f') {
+          fast = true;
+          ch = *fmt++;
+        }
         while (is_digit(ch)) {
           APPEND_DIGIT(n, ch);
           ch = *fmt++;
         }
-        switch (n) {
-          case 8: {
-            flags |= CHARINT;
-            goto reswitch;
-          }
-          case 16: {
-            flags |= SHORTINT;
-            goto reswitch;
-          }
-          case 32: {
-            goto reswitch;
-          }
-          case 64: {
-            flags |= LLONGINT;
-            goto reswitch;
-          }
-          default: {
-            __fortify_fatal("%%w%d is unsupported", n);
-            break;
-          }
-        }
+        flags |= helpers::w_to_flag(n, fast);
+        goto reswitch;
+      }
       case 'X':
         xdigs = xdigs_upper;
         goto hex;
diff --git a/libc/stdio/vfwprintf.cpp b/libc/stdio/vfwprintf.cpp
index 52ae64b..0caeb2d 100644
--- a/libc/stdio/vfwprintf.cpp
+++ b/libc/stdio/vfwprintf.cpp
@@ -510,34 +510,21 @@
         _umax = UARG();
         base = DEC;
         goto nosign;
-      case 'w':
+      case 'w': {
         n = 0;
+        bool fast = false;
         ch = *fmt++;
+        if (ch == 'f') {
+          fast = true;
+          ch = *fmt++;
+        }
         while (is_digit(ch)) {
           APPEND_DIGIT(n, ch);
           ch = *fmt++;
         }
-        switch (n) {
-          case 8: {
-            flags |= CHARINT;
-            goto reswitch;
-          }
-          case 16: {
-            flags |= SHORTINT;
-            goto reswitch;
-          }
-          case 32: {
-            goto reswitch;
-          }
-          case 64: {
-            flags |= LLONGINT;
-            goto reswitch;
-          }
-          default: {
-            __fortify_fatal("%%w%d is unsupported", n);
-            break;
-          }
-        }
+        flags |= helpers::w_to_flag(n, fast);
+        goto reswitch;
+      }
       case 'X':
         xdigs = xdigs_upper;
         goto hex;
diff --git a/tests/stdio_test.cpp b/tests/stdio_test.cpp
index 0e267c5..800732f 100644
--- a/tests/stdio_test.cpp
+++ b/tests/stdio_test.cpp
@@ -3415,4 +3415,172 @@
 #else
   GTEST_SKIP() << "no %w in glibc";
 #endif
+}
+
+TEST(STDIO_TEST, snprintf_wf_base) {
+#if defined(__BIONIC__)
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wconstant-conversion"
+#pragma clang diagnostic ignored "-Wformat"
+#pragma clang diagnostic ignored "-Wformat-invalid-specifier"
+  char buf[BUFSIZ];
+  int_fast8_t a = 0b101;
+  snprintf(buf, sizeof(buf), "<%wf8b>", a);
+  EXPECT_STREQ("<101>", buf);
+  int_fast8_t b = 0x12341234'12341234;
+  snprintf(buf, sizeof(buf), "<%wf8x>", b);
+  EXPECT_STREQ("<34>", buf);
+  uint_fast16_t c = 0x11111111'22222222;
+#if defined(__LP64__)
+  snprintf(buf, sizeof(buf), "<%wf16x>", c);
+  EXPECT_STREQ("<1111111122222222>", buf);
+#else
+  snprintf(buf, sizeof(buf), "<%wf16x>", c);
+  EXPECT_STREQ("<22222222>", buf);
+#endif
+  int_fast32_t d = 0x33333333'44444444;
+#if defined(__LP64__)
+  snprintf(buf, sizeof(buf), "<%wf32x>", d);
+  EXPECT_STREQ("<3333333344444444>", buf);
+#else
+  snprintf(buf, sizeof(buf), "<%wf32x>", d);
+  EXPECT_STREQ("<44444444>", buf);
+#endif
+  int_fast64_t e = 0xaaaaaaaa'aaaaaaaa;
+  snprintf(buf, sizeof(buf), "<%wf64x>", e);
+  EXPECT_STREQ("<aaaaaaaaaaaaaaaa>", buf);
+  snprintf(buf, sizeof(buf), "<%wf64X>", e);
+  EXPECT_STREQ("<AAAAAAAAAAAAAAAA>", buf);
+#pragma clang diagnostic pop
+#else
+  GTEST_SKIP() << "no %wf in glibc";
+#endif
+}
+
+TEST(STDIO_TEST, snprintf_wf_arguments_reordering) {
+#if defined(__BIONIC__)
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wconstant-conversion"
+#pragma clang diagnostic ignored "-Wformat"
+#pragma clang diagnostic ignored "-Wformat-extra-args"
+#pragma clang diagnostic ignored "-Wformat-invalid-specifier"
+  char buf[BUFSIZ];
+  int_fast16_t a = 0x11111111'22222222;
+  int_fast32_t b = 0x33333333'44444444;
+  int_fast32_t c = 0xaaaaaaaa'aaaaaaaa;
+#if defined(__LP64__)
+  snprintf(buf, sizeof(buf), "<%2$wf32x --- %1$wf32b>", c, b);
+  EXPECT_STREQ(
+      "<3333333344444444 --- 1010101010101010101010101010101010101010101010101010101010101010>",
+      buf);
+  snprintf(buf, sizeof(buf), "<%3$wf32b --- %1$wf16x --- %2$wf32x>", a, b, c);
+  EXPECT_STREQ(
+      "<1010101010101010101010101010101010101010101010101010101010101010 --- 1111111122222222 --- "
+      "3333333344444444>",
+      buf);
+#else
+  snprintf(buf, sizeof(buf), "<%2$wf32x --- %1$wf32b>", c, b);
+  EXPECT_STREQ("<44444444 --- 10101010101010101010101010101010>", buf);
+  snprintf(buf, sizeof(buf), "<%3$wf32b --- %1$wf16x --- %2$wf32x>", a, b, c);
+  EXPECT_STREQ("<10101010101010101010101010101010 --- 22222222 --- 44444444>", buf);
+#endif
+#pragma clang diagnostic pop
+#else
+  GTEST_SKIP() << "no %w in glibc";
+#endif
+}
+
+TEST(STDIO_TEST, snprintf_invalid_wf_width) {
+#if defined(__BIONIC__)
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wformat"
+#pragma clang diagnostic ignored "-Wformat-invalid-specifier"
+  char buf[BUFSIZ];
+  int_fast32_t a = 100;
+  EXPECT_DEATH(snprintf(buf, sizeof(buf), "%wf20d", &a), "%wf20 is unsupported");
+#pragma clang diagnostic pop
+#else
+  GTEST_SKIP() << "no %w in glibc";
+#endif
+}
+
+TEST(STDIO_TEST, swprintf_wf_base) {
+#if defined(__BIONIC__)
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wformat-invalid-specifier"
+#pragma clang diagnostic ignored "-Wconstant-conversion"
+  wchar_t buf[BUFSIZ];
+  int_fast8_t a = 0b101;
+  swprintf(buf, sizeof(buf), L"<%wf8b>", a);
+  EXPECT_EQ(std::wstring(L"<101>"), buf);
+  int_fast8_t b = 0x12341234'12341234;
+  swprintf(buf, sizeof(buf), L"<%wf8x>", b);
+  EXPECT_EQ(std::wstring(L"<34>"), buf);
+  uint_fast16_t c = 0x11111111'22222222;
+#if defined(__LP64__)
+  swprintf(buf, sizeof(buf), L"<%wf16x>", c);
+  EXPECT_EQ(std::wstring(L"<1111111122222222>"), buf);
+#else
+  swprintf(buf, sizeof(buf), L"<%wf16x>", c);
+  EXPECT_EQ(std::wstring(L"<22222222>"), buf);
+#endif
+  int_fast32_t d = 0x33333333'44444444;
+#if defined(__LP64__)
+  swprintf(buf, sizeof(buf), L"<%wf32x>", d);
+  EXPECT_EQ(std::wstring(L"<3333333344444444>"), buf);
+#else
+  swprintf(buf, sizeof(buf), L"<%wf32x>", d);
+  EXPECT_EQ(std::wstring(L"<44444444>"), buf);
+#endif
+  int_fast64_t e = 0xaaaaaaaa'aaaaaaaa;
+  swprintf(buf, sizeof(buf), L"<%wf64x>", e);
+  EXPECT_EQ(std::wstring(L"<aaaaaaaaaaaaaaaa>"), buf);
+  swprintf(buf, sizeof(buf), L"<%wf64X>", e);
+  EXPECT_EQ(std::wstring(L"<AAAAAAAAAAAAAAAA>"), buf);
+#else
+  GTEST_SKIP() << "no %w in glibc";
+#endif
+}
+
+TEST(STDIO_TEST, swprintf_wf_arguments_reordering) {
+#if defined(__BIONIC__)
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wformat-invalid-specifier"
+#pragma clang diagnostic ignored "-Wformat-extra-args"
+  wchar_t buf[BUFSIZ];
+  int_fast16_t a = 0x11111111'22222222;
+  int_fast32_t b = 0x33333333'44444444;
+  int_fast32_t c = 0xaaaaaaaa'aaaaaaaa;
+#if defined(__LP64__)
+  swprintf(buf, sizeof(buf), L"<%2$wf32x --- %1$wf32b>", c, b);
+  EXPECT_EQ(std::wstring(L"<3333333344444444 --- "
+                         L"1010101010101010101010101010101010101010101010101010101010101010>"),
+            buf);
+  swprintf(buf, sizeof(buf), L"<%3$wf32b --- %1$wf16x --- %2$wf32x>", a, b, c);
+  EXPECT_EQ(std::wstring(L"<1010101010101010101010101010101010101010101010101010101010101010 --- "
+                         L"1111111122222222 --- 3333333344444444>"),
+            buf);
+#else
+  swprintf(buf, sizeof(buf), L"<%2$wf32x --- %1$wf32b>", c, b);
+  EXPECT_EQ(std::wstring(L"<44444444 --- 10101010101010101010101010101010>"), buf);
+  swprintf(buf, sizeof(buf), L"<%3$wf32b --- %1$wf16x --- %2$wf32x>", a, b, c);
+  EXPECT_EQ(std::wstring(L"<10101010101010101010101010101010 --- 22222222 --- 44444444>"), buf);
+#endif
+#pragma clang diagnostic pop
+#else
+  GTEST_SKIP() << "no %w in glibc";
+#endif
+}
+
+TEST(STDIO_TEST, swprintf_invalid_wf_width) {
+#if defined(__BIONIC__)
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wformat-invalid-specifier"
+  wchar_t buf[BUFSIZ];
+  int_fast32_t a = 100;
+  EXPECT_DEATH(swprintf(buf, sizeof(buf), L"%wf20d", &a), "%wf20 is unsupported");
+#pragma clang diagnostic pop
+#else
+  GTEST_SKIP() << "no %w in glibc";
+#endif
 }
\ No newline at end of file