[libc-commits] [libc] 5e22ef3 - [libc] move int conversion out of base template

Michael Jones via libc-commits libc-commits at lists.llvm.org
Thu Aug 11 16:56:36 PDT 2022


Author: Michael Jones
Date: 2022-08-11T16:56:27-07:00
New Revision: 5e22ef3198d1686f7978dd150a3eefad4f737bfc

URL: https://github.com/llvm/llvm-project/commit/5e22ef3198d1686f7978dd150a3eefad4f737bfc
DIFF: https://github.com/llvm/llvm-project/commit/5e22ef3198d1686f7978dd150a3eefad4f737bfc.diff

LOG: [libc] move int conversion out of base template

The convert_alpha_numeric function is intentionally non-templated so
that its code can be reused for different bases in code-size sensitive
cases. Previously it was inside the IntegerToString class which created
a different version for each base.

Reviewed By: sivachandra, lntue

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

Added: 
    

Modified: 
    libc/src/__support/integer_to_string.h

Removed: 
    


################################################################################
diff  --git a/libc/src/__support/integer_to_string.h b/libc/src/__support/integer_to_string.h
index 0826ce6242ee5..d37ae1a41cd46 100644
--- a/libc/src/__support/integer_to_string.h
+++ b/libc/src/__support/integer_to_string.h
@@ -16,6 +16,38 @@
 
 namespace __llvm_libc {
 
+template <typename T>
+inline constexpr cpp::StringView
+convert_alpha_numeric(T val, cpp::MutableArrayRef<char> &buffer, bool lowercase,
+                      const uint8_t conv_base) {
+  using UnsignedType = cpp::make_unsigned_t<T>;
+  UnsignedType uval = val < 0 ? UnsignedType(-val) : UnsignedType(val);
+
+  const char a = lowercase ? 'a' : 'A';
+
+  size_t len = 0;
+
+  size_t buffptr = buffer.size();
+  if (uval == 0) {
+    buffer[buffptr - 1] = '0';
+    --buffptr;
+  } else {
+    for (; uval > 0; --buffptr, uval /= conv_base) {
+      UnsignedType digit = (uval % conv_base);
+      buffer[buffptr - 1] = digit < 10 ? digit + '0' : digit + a - 10;
+    }
+  }
+  len = buffer.size() - buffptr;
+
+  if (val < 0) {
+    // This branch will be taken only for negative signed values.
+    ++len;
+    buffer[buffer.size() - len] = '-';
+  }
+  cpp::StringView buff_str(buffer.data() + buffer.size() - len, len);
+  return buff_str;
+}
+
 template <typename T, uint8_t BASE = 10,
           cpp::enable_if_t<2 <= BASE && BASE <= 36, int> = 0>
 class IntegerToString {
@@ -69,50 +101,18 @@ class IntegerToString {
   char strbuf[BUFSIZE] = {'\0'};
   cpp::StringView str_view;
 
-  static inline constexpr cpp::StringView
-  convert_alpha_numeric(T val, cpp::MutableArrayRef<char> &buffer,
-                        bool lowercase, const uint8_t conv_base) {
-    UnsignedType uval = val < 0 ? UnsignedType(-val) : UnsignedType(val);
-
-    const char a = lowercase ? 'a' : 'A';
-
-    size_t len = 0;
-
-    size_t buffptr = buffer.size();
-    if (uval == 0) {
-      buffer[buffptr - 1] = '0';
-      --buffptr;
-    } else {
-      for (; uval > 0; --buffptr, uval /= conv_base) {
-        UnsignedType digit = (uval % conv_base);
-        buffer[buffptr - 1] = digit < 10 ? digit + '0' : digit + a - 10;
-      }
-    }
-    len = buffer.size() - buffptr;
-
-    if (val < 0) {
-      // This branch will be taken only for negative signed values.
-      ++len;
-      buffer[buffer.size() - len] = '-';
-    }
-    cpp::StringView buff_str(buffer.data() + buffer.size() - len, len);
-    return buff_str;
-  }
-
-  // This function exists to check at compile time that the base is valid, as
-  // well as to convert the templated call into a non-templated call. This
-  // allows the compiler to decide to do strength reduction and constant folding
-  // on the base or not, depending on if size or performance is required.
+  // This function exists to convert the templated call into a non-templated
+  // call. This allows the compiler to decide to do strength reduction and
+  // constant folding on the base or not, depending on if size or performance is
+  // required.
   static inline constexpr cpp::StringView
   convert_internal(T val, cpp::MutableArrayRef<char> &buffer, bool lowercase) {
-    return convert_alpha_numeric(val, buffer, lowercase, BASE);
+    return convert_alpha_numeric<T>(val, buffer, lowercase, BASE);
   }
 
 public:
   static inline cpp::optional<cpp::StringView>
   convert(T val, cpp::MutableArrayRef<char> &buffer, bool lowercase) {
-    // If This function can actually be a constexpr, then the below "if" will be
-    // optimized out.
     if (buffer.size() < bufsize())
       return cpp::optional<cpp::StringView>();
     return cpp::optional<cpp::StringView>(


        


More information about the libc-commits mailing list