tools/nolibc/stdlib: add i64toa() and u64toa()
authorWilly Tarreau <w@1wt.eu>
Mon, 7 Feb 2022 16:23:28 +0000 (17:23 +0100)
committerPaul E. McKenney <paulmck@kernel.org>
Thu, 21 Apr 2022 00:05:44 +0000 (17:05 -0700)
These are 64-bit variants of the itoa() and utoa() functions. They also
support reentrant ones, and use the same itoa_buffer. The functions are
a bit larger than the previous ones in 32-bit mode (86 and 98 bytes on
x86_64 and armv7 respectively), which is why we continue to provide them
as separate functions.

Signed-off-by: Willy Tarreau <w@1wt.eu>
Signed-off-by: Paul E. McKenney <paulmck@kernel.org>
tools/include/nolibc/stdlib.h

index dbb4563..d972871 100644 (file)
@@ -144,6 +144,78 @@ char *utoa(unsigned long in)
        return itoa_buffer;
 }
 
+/* Converts the unsigned 64-bit integer <in> to its string representation into
+ * buffer <buffer>, which must be long enough to store the number and the
+ * trailing zero (21 bytes for 18446744073709551615). The buffer is filled from
+ * the first byte, and the number of characters emitted (not counting the
+ * trailing zero) is returned. The function is constructed in a way to optimize
+ * the code size and avoid any divide that could add a dependency on large
+ * external functions.
+ */
+static __attribute__((unused))
+int u64toa_r(uint64_t in, char *buffer)
+{
+       unsigned long long lim;
+       int digits = 0;
+       int pos = 19; /* start with the highest possible digit */
+       int dig;
+
+       do {
+               for (dig = 0, lim = 1; dig < pos; dig++)
+                       lim *= 10;
+
+               if (digits || in >= lim || !pos) {
+                       for (dig = 0; in >= lim; dig++)
+                               in -= lim;
+                       buffer[digits++] = '0' + dig;
+               }
+       } while (pos--);
+
+       buffer[digits] = 0;
+       return digits;
+}
+
+/* Converts the signed 64-bit integer <in> to its string representation into
+ * buffer <buffer>, which must be long enough to store the number and the
+ * trailing zero (21 bytes for -9223372036854775808). The buffer is filled from
+ * the first byte, and the number of characters emitted (not counting the
+ * trailing zero) is returned.
+ */
+static __attribute__((unused))
+int i64toa_r(int64_t in, char *buffer)
+{
+       char *ptr = buffer;
+       int len = 0;
+
+       if (in < 0) {
+               in = -in;
+               *(ptr++) = '-';
+               len++;
+       }
+       len += u64toa_r(in, ptr);
+       return len;
+}
+
+/* converts int64_t <in> to a string using the static itoa_buffer and returns
+ * the pointer to that string.
+ */
+static inline __attribute__((unused))
+char *i64toa(int64_t in)
+{
+       i64toa_r(in, itoa_buffer);
+       return itoa_buffer;
+}
+
+/* converts uint64_t <in> to a string using the static itoa_buffer and returns
+ * the pointer to that string.
+ */
+static inline __attribute__((unused))
+char *u64toa(uint64_t in)
+{
+       u64toa_r(in, itoa_buffer);
+       return itoa_buffer;
+}
+
 static __attribute__((unused))
 int msleep(unsigned int msecs)
 {