[libc-commits] [PATCH] D107999: [libc] change internal strtoll to be templated

Michael Jones via Phabricator via libc-commits libc-commits at lists.llvm.org
Thu Aug 12 14:38:44 PDT 2021


michaelrj created this revision.
michaelrj added a reviewer: sivachandra.
Herald added subscribers: libc-commits, ecnelises, tschuett, mgorny.
Herald added a project: libc-project.
michaelrj requested review of this revision.

Uses the new Limits.h added in a previous commit for max and min values
for the template type. This makes implementing the other strto*
functions very simple.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D107999

Files:
  libc/src/__support/CMakeLists.txt
  libc/src/__support/str_conv_utils.h
  libc/src/stdlib/strtoll.cpp


Index: libc/src/stdlib/strtoll.cpp
===================================================================
--- libc/src/stdlib/strtoll.cpp
+++ libc/src/stdlib/strtoll.cpp
@@ -15,7 +15,7 @@
 LLVM_LIBC_FUNCTION(long long, strtoll,
                    (const char *__restrict str, char **__restrict str_end,
                     int base)) {
-  return internal::strtoll(str, str_end, base);
+  return internal::strtointeger<long long>(str, str_end, base);
 }
 
 } // namespace __llvm_libc
Index: libc/src/__support/str_conv_utils.h
===================================================================
--- libc/src/__support/str_conv_utils.h
+++ libc/src/__support/str_conv_utils.h
@@ -10,6 +10,7 @@
 #define LIBC_SRC_STDLIB_STDLIB_UTILS_H
 
 #include "src/__support/ctype_utils.h"
+#include "utils/CPP/Limits.h"
 #include <errno.h>
 #include <limits.h>
 
@@ -50,8 +51,9 @@
 // Takes a pointer to a string, a pointer to a string pointer, and the base to
 // convert to. This function is used as the backend for all of the string to int
 // functions.
-static inline long long strtoll(const char *__restrict src,
-                                char **__restrict str_end, int base) {
+template <class T>
+static inline T strtointeger(const char *__restrict src,
+                             char **__restrict str_end, int base) {
   unsigned long long result = 0;
 
   if (base < 0 || base == 1 || base > 36) {
@@ -73,9 +75,13 @@
     src = src + 2;
   }
 
+  unsigned long long const NEGATIVE_MAX =
+      (__llvm_libc::cpp::NumericLimits<T>::min() != 0)
+          ? __llvm_libc::cpp::NumericLimits<T>::min()
+          : __llvm_libc::cpp::NumericLimits<T>::max();
   unsigned long long const ABS_MAX =
-      (result_sign == '+' ? LLONG_MAX
-                          : static_cast<unsigned long long>(LLONG_MAX) + 1);
+      (result_sign == '+' ? __llvm_libc::cpp::NumericLimits<T>::max()
+                          : NEGATIVE_MAX);
   unsigned long long const ABS_MAX_DIV_BY_BASE = ABS_MAX / base;
   while (isalnum(*src)) {
     int cur_digit = b36_char_to_int(*src);
@@ -100,9 +106,9 @@
   if (str_end != nullptr)
     *str_end = const_cast<char *>(src);
   if (result_sign == '+')
-    return result;
+    return (T)(result);
   else
-    return -result;
+    return -(T)(result);
 }
 
 } // namespace internal
Index: libc/src/__support/CMakeLists.txt
===================================================================
--- libc/src/__support/CMakeLists.txt
+++ libc/src/__support/CMakeLists.txt
@@ -20,6 +20,7 @@
     .ctype_utils
     libc.include.errno
     libc.src.errno.__errno_location
+    libc.utils.CPP.standalone_cpp
 )
 
 add_header_library(


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D107999.366110.patch
Type: text/x-patch
Size: 2654 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libc-commits/attachments/20210812/2f9ea226/attachment.bin>


More information about the libc-commits mailing list