linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Willy Tarreau <w@1wt.eu>
To: "Paul E . McKenney" <paulmck@kernel.org>
Cc: Mark Brown <broonie@kernel.org>,
	linux-kernel@vger.kernel.org, Willy Tarreau <w@1wt.eu>
Subject: [PATCH 15/42] tools/nolibc/stdlib: replace the ltoa() function with more efficient ones
Date: Mon,  7 Feb 2022 17:23:27 +0100	[thread overview]
Message-ID: <20220207162354.14293-16-w@1wt.eu> (raw)
In-Reply-To: <20220207162354.14293-1-w@1wt.eu>

The original ltoa() function and the reentrant one ltoa_r() present a
number of drawbacks. The divide by 10 generates calls to external code
from libgcc_s, and the number does not necessarily start at the beginning
of the buffer.

Let's rewrite these functions so that they do not involve a divide and
only use loops on powers of 10, and implement both signed and unsigned
variants, always starting from the buffer's first character. Instead of
using a static buffer for each function, we're now using a common one.

In order to avoid confusion with the ltoa() name, the new functions are
called itoa_r() and utoa_r() to distinguish the signed and unsigned
versions, and for convenience for their callers, these functions now
reutrn the number of characters emitted. The ltoa_r() function is just
an inline mapping to the signed one and which returns the buffer.

The functions are quite small (86 bytes on x86_64, 68 on armv7) and
do not depend anymore on external code.

Signed-off-by: Willy Tarreau <w@1wt.eu>
---
 tools/include/nolibc/stdlib.h | 107 +++++++++++++++++++++++++++-------
 1 file changed, 87 insertions(+), 20 deletions(-)

diff --git a/tools/include/nolibc/stdlib.h b/tools/include/nolibc/stdlib.h
index 84fc4353fb01..dbb45631c7ca 100644
--- a/tools/include/nolibc/stdlib.h
+++ b/tools/include/nolibc/stdlib.h
@@ -12,6 +12,13 @@
 #include "types.h"
 #include "sys.h"
 
+
+/* Buffer used to store int-to-ASCII conversions. Will only be implemented if
+ * any of the related functions is implemented. The area is large enough to
+ * store "18446744073709551615" or "-9223372036854775808" and the final zero.
+ */
+static __attribute__((unused)) char itoa_buffer[21];
+
 /*
  * As much as possible, please keep functions alphabetically sorted.
  */
@@ -45,36 +52,96 @@ int atoi(const char *s)
 	return atol(s);
 }
 
-/* performs the opposite of atol() using a user-fed buffer. The buffer must be
- * at least 21 bytes long (large enough for "-9223372036854775808").
+/* Converts the unsigned long 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 in 64-bit, 11 for
+ * 4294967295 in 32-bit). 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))
-const char *ltoa_r(long in, char *buffer)
+int utoa_r(unsigned long in, char *buffer)
 {
-	char       *pos = buffer + 21 - 1;
-	int         neg = in < 0;
-	unsigned long n = neg ? -in : in;
+	unsigned long lim;
+	int digits = 0;
+	int pos = (~0UL > 0xfffffffful) ? 19 : 9;
+	int dig;
 
-	*pos-- = '\0';
 	do {
-		*pos-- = '0' + n % 10;
-		n /= 10;
-		if (pos < buffer)
-			return pos + 1;
-	} while (n);
+		for (dig = 0, lim = 1; dig < pos; dig++)
+			lim *= 10;
 
-	if (neg)
-		*pos-- = '-';
-	return pos + 1;
+		if (digits || in >= lim || !pos) {
+			for (dig = 0; in >= lim; dig++)
+				in -= lim;
+			buffer[digits++] = '0' + dig;
+		}
+	} while (pos--);
+
+	buffer[digits] = 0;
+	return digits;
 }
 
-/* performs the opposite of atol() using a statically allocated buffer */
+/* Converts the signed long 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 in 64-bit, 12 for
+ * -2147483648 in 32-bit). The buffer is filled from the first byte, and the
+ * number of characters emitted (not counting the trailing zero) is returned.
+ */
 static __attribute__((unused))
-const char *ltoa(long in)
+int itoa_r(long in, char *buffer)
 {
-	/* large enough for -9223372036854775808 */
-	static char buffer[21];
-	return ltoa_r(in, buffer);
+	char *ptr = buffer;
+	int len = 0;
+
+	if (in < 0) {
+		in = -in;
+		*(ptr++) = '-';
+		len++;
+	}
+	len += utoa_r(in, ptr);
+	return len;
+}
+
+/* for historical compatibility, same as above but returns the pointer to the
+ * buffer.
+ */
+static inline __attribute__((unused))
+char *ltoa_r(long in, char *buffer)
+{
+	itoa_r(in, buffer);
+	return buffer;
+}
+
+/* converts long integer <in> to a string using the static itoa_buffer and
+ * returns the pointer to that string.
+ */
+static inline __attribute__((unused))
+char *itoa(long in)
+{
+	itoa_r(in, itoa_buffer);
+	return itoa_buffer;
+}
+
+/* converts long integer <in> to a string using the static itoa_buffer and
+ * returns the pointer to that string. Same as above, for compatibility.
+ */
+static inline __attribute__((unused))
+char *ltoa(long in)
+{
+	itoa_r(in, itoa_buffer);
+	return itoa_buffer;
+}
+
+/* converts unsigned long integer <in> to a string using the static itoa_buffer
+ * and returns the pointer to that string.
+ */
+static inline __attribute__((unused))
+char *utoa(unsigned long in)
+{
+	utoa_r(in, itoa_buffer);
+	return itoa_buffer;
 }
 
 static __attribute__((unused))
-- 
2.35.1


  parent reply	other threads:[~2022-02-07 16:33 UTC|newest]

Thread overview: 51+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-07 16:23 [PATCH 00/42] nolibc: update to resync with out-of-tree project Willy Tarreau
2022-02-07 16:23 ` [PATCH 01/42] tools/nolibc: use pselect6 on RISCV Willy Tarreau
2022-02-07 16:23 ` [PATCH 02/42] tools/nolibc: guard the main file against multiple inclusion Willy Tarreau
2022-02-07 16:23 ` [PATCH 03/42] tools/nolibc/std: move the standard type definitions to std.h Willy Tarreau
2022-02-07 16:23 ` [PATCH 04/42] tools/nolibc/types: split syscall-specific definitions into their own files Willy Tarreau
2022-02-07 16:23 ` [PATCH 05/42] tools/nolibc/arch: split arch-specific code into individual files Willy Tarreau
2022-02-07 16:23 ` [PATCH 06/42] tools/nolibc/sys: split the syscall definitions into their own file Willy Tarreau
2022-02-07 16:23 ` [PATCH 07/42] tools/nolibc/stdlib: extract the stdlib-specific functions to " Willy Tarreau
2022-02-07 16:23 ` [PATCH 08/42] tools/nolibc/string: split the string functions into string.h Willy Tarreau
2022-02-07 16:23 ` [PATCH 09/42] tools/nolibc/ctype: split the is* functions to ctype.h Willy Tarreau
2022-02-07 16:23 ` [PATCH 10/42] tools/nolibc/ctype: add the missing is* functions Willy Tarreau
2022-02-07 16:23 ` [PATCH 11/42] tools/nolibc/types: move the FD_* functions to macros in types.h Willy Tarreau
2022-02-07 17:05   ` David Laight
2022-02-08  5:14     ` Willy Tarreau
2022-02-13  8:52   ` [PATCH v2 " Willy Tarreau
2022-02-07 16:23 ` [PATCH 12/42] tools/nolibc/types: make FD_SETSIZE configurable Willy Tarreau
2022-02-13  8:53   ` [PATCH v2 " Willy Tarreau
2022-02-14 20:17     ` Paul E. McKenney
2022-02-07 16:23 ` [PATCH 13/42] tools/nolibc/types: move makedev to types.h and make it a macro Willy Tarreau
2022-02-07 16:23 ` [PATCH 14/42] tools/nolibc/stdlib: move ltoa() to stdlib.h Willy Tarreau
2022-02-07 16:23 ` Willy Tarreau [this message]
2022-02-07 16:23 ` [PATCH 16/42] tools/nolibc/stdlib: add i64toa() and u64toa() Willy Tarreau
2022-02-07 16:23 ` [PATCH 17/42] tools/nolibc/stdlib: add utoh() and u64toh() Willy Tarreau
2022-02-07 16:23 ` [PATCH 18/42] tools/nolibc/stdio: add a minimal set of stdio functions Willy Tarreau
2022-02-07 16:23 ` [PATCH 19/42] tools/nolibc/stdio: add stdin/stdout/stderr and fget*/fput* functions Willy Tarreau
2022-02-07 16:23 ` [PATCH 20/42] tools/nolibc/stdio: add fwrite() to stdio Willy Tarreau
2022-02-07 16:23 ` [PATCH 21/42] tools/nolibc/stdio: add a minimal [vf]printf() implementation Willy Tarreau
2022-02-07 16:23 ` [PATCH 22/42] tools/nolibc/types: define EXIT_SUCCESS and EXIT_FAILURE Willy Tarreau
2022-02-07 16:23 ` [PATCH 23/42] tools/nolibc/stdio: add perror() to report the errno value Willy Tarreau
2022-02-07 16:23 ` [PATCH 24/42] tools/nolibc/sys: make open() take a vararg on the 3rd argument Willy Tarreau
2022-02-07 16:23 ` [PATCH 25/42] tools/nolibc/stdlib: avoid a 64-bit shift in u64toh_r() Willy Tarreau
2022-02-07 16:23 ` [PATCH 26/42] tools/nolibc/stdlib: make raise() use the lower level syscalls only Willy Tarreau
2022-02-07 16:23 ` [PATCH 27/42] tools/nolibc/sys: make getpgrp(), getpid(), gettid() not set errno Willy Tarreau
2022-02-07 16:23 ` [PATCH 28/42] tools/nolibc/string: use unidirectional variants for memcpy() Willy Tarreau
2022-02-07 16:23 ` [PATCH 29/42] tools/nolibc/string: slightly simplify memmove() Willy Tarreau
2022-02-07 16:23 ` [PATCH 30/42] tools/nolibc/string: add strncpy() and strlcpy() Willy Tarreau
2022-02-07 16:23 ` [PATCH 31/42] tools/nolibc/string: add tiny versions of strncat() and strlcat() Willy Tarreau
2022-02-07 16:23 ` [PATCH 32/42] tools/nolibc: move exported functions to their own section Willy Tarreau
2022-02-07 16:23 ` [PATCH 33/42] tools/nolibc/arch: mark the _start symbol as weak Willy Tarreau
2022-02-07 16:23 ` [PATCH 34/42] tools/nolibc/types: define PATH_MAX and MAXPATHLEN Willy Tarreau
2022-02-07 16:23 ` [PATCH 35/42] tools/nolibc/string: export memset() and memmove() Willy Tarreau
2022-02-07 16:23 ` [PATCH 36/42] tools/nolibc/errno: extract errno.h from sys.h Willy Tarreau
2022-02-07 16:23 ` [PATCH 37/42] tools/nolibc/unistd: extract msleep(), sleep(), tcsetpgrp() to unistd.h Willy Tarreau
2022-02-07 16:23 ` [PATCH 38/42] tools/nolibc/unistd: add usleep() Willy Tarreau
2022-02-07 16:23 ` [PATCH 39/42] tools/nolibc/signal: move raise() to signal.h Willy Tarreau
2022-02-07 16:23 ` [PATCH 40/42] tools/nolibc/time: create time.h with time() Willy Tarreau
2022-02-07 16:23 ` [PATCH 41/42] tools/nolibc: also mention how to build by just setting the include path Willy Tarreau
2022-02-07 16:23 ` [PATCH 42/42] tools/nolibc/stdlib: implement abort() Willy Tarreau
2022-02-08  0:00 ` [PATCH 00/42] nolibc: update to resync with out-of-tree project Paul E. McKenney
2022-02-08  4:41   ` Willy Tarreau
2022-02-08  5:10     ` Paul E. McKenney

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220207162354.14293-16-w@1wt.eu \
    --to=w@1wt.eu \
    --cc=broonie@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=paulmck@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).