[libc-commits] [libc] 276e108 - [libc][NFC] clean int to string static templates

Michael Jones via libc-commits libc-commits at lists.llvm.org
Wed Aug 10 13:11:34 PDT 2022


Author: Michael Jones
Date: 2022-08-10T13:11:29-07:00
New Revision: 276e108bf9d8dd9507e37c9a25114a3dbd0ac0ef

URL: https://github.com/llvm/llvm-project/commit/276e108bf9d8dd9507e37c9a25114a3dbd0ac0ef
DIFF: https://github.com/llvm/llvm-project/commit/276e108bf9d8dd9507e37c9a25114a3dbd0ac0ef.diff

LOG: [libc][NFC] clean int to string static templates

Reviewed By: sivachandra

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

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 f9d5f24ddc74..0826ce6242ee 100644
--- a/libc/src/__support/integer_to_string.h
+++ b/libc/src/__support/integer_to_string.h
@@ -16,7 +16,9 @@
 
 namespace __llvm_libc {
 
-template <typename T, uint8_t BASE = 10> class IntegerToString {
+template <typename T, uint8_t BASE = 10,
+          cpp::enable_if_t<2 <= BASE && BASE <= 36, int> = 0>
+class IntegerToString {
 public:
   static constexpr inline size_t floor_log_2(size_t num) {
     size_t i = 0;
@@ -47,16 +49,16 @@ template <typename T, uint8_t BASE = 10> class IntegerToString {
   // two base, since the space needed is easy to calculate and it won't
   // overestimate by too much.
 
-  template <uint8_t STATIC_BASE> static constexpr size_t bufsize() {
-    constexpr size_t BITS_PER_DIGIT = floor_log_2(STATIC_BASE);
+  static constexpr size_t bufsize() {
+    constexpr size_t BITS_PER_DIGIT = floor_log_2(BASE);
     constexpr size_t BUFSIZE_COMMON =
         ((sizeof(T) * 8 + (BITS_PER_DIGIT - 1)) / BITS_PER_DIGIT);
     constexpr size_t BUFSIZE_BASE10 = (sizeof(T) * 5 + 1) / 2;
     return (cpp::is_signed<T>() ? 1 : 0) +
-           (STATIC_BASE == 10 ? BUFSIZE_BASE10 : BUFSIZE_COMMON);
+           (BASE == 10 ? BUFSIZE_BASE10 : BUFSIZE_COMMON);
   }
 
-  static constexpr size_t BUFSIZE = bufsize<BASE>();
+  static constexpr size_t BUFSIZE = bufsize();
 
 private:
   static_assert(cpp::is_integral_v<T>,
@@ -101,33 +103,30 @@ template <typename T, uint8_t BASE = 10> class IntegerToString {
   // 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.
-  template <uint8_t STATIC_BASE = BASE,
-            cpp::enable_if_t<2 <= STATIC_BASE && STATIC_BASE <= 36, int> = 0>
   static inline constexpr cpp::StringView
   convert_internal(T val, cpp::MutableArrayRef<char> &buffer, bool lowercase) {
-    return convert_alpha_numeric(val, buffer, lowercase, STATIC_BASE);
+    return convert_alpha_numeric(val, buffer, lowercase, BASE);
   }
 
 public:
-  template <uint8_t STATIC_BASE = BASE>
   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<STATIC_BASE>())
+    if (buffer.size() < bufsize())
       return cpp::optional<cpp::StringView>();
     return cpp::optional<cpp::StringView>(
-        convert_internal<STATIC_BASE>(val, buffer, lowercase));
+        convert_internal(val, buffer, lowercase));
   }
 
   constexpr explicit IntegerToString(T val) {
     cpp::MutableArrayRef<char> bufref(strbuf, BUFSIZE);
-    str_view = convert_internal<BASE>(val, bufref, true);
+    str_view = convert_internal(val, bufref, true);
   }
 
   constexpr explicit IntegerToString(T val, bool lowercase) {
     cpp::MutableArrayRef<char> bufref(strbuf, BUFSIZE);
-    str_view = convert_internal<BASE>(val, bufref, lowercase);
+    str_view = convert_internal(val, bufref, lowercase);
   }
 
   cpp::StringView str() const { return str_view; }


        


More information about the libc-commits mailing list