diff mbox

[U-Boot,4/4,v2] lib/tiny-printf.c: Support numbers bigger than 0xffff and misc updates

Message ID 1447683994-25930-4-git-send-email-sr@denx.de
State Accepted
Delegated to: Tom Rini
Headers show

Commit Message

Stefan Roese Nov. 16, 2015, 2:26 p.m. UTC
With this patch now, the tiny printf() function also supports numbers
bigger than 0xffff. Additionally the code is simplified a bit and
some static variables are moved to function parameters. Also the
upper case hex variable output support is removed, as its not really
needed in this simple printf version. And removing it reduces the
complexity and the code size again a bit.

Here the new numbers, again on the db-mv784mp-gp (Armada XP):

Without this patch:
  56542   18536    1956   77034   12cea ./spl/u-boot-spl

With this patch:
  56446   18536    1936   76918   12c76 ./spl/u-boot-spl

Signed-off-by: Stefan Roese <sr@denx.de>
Cc: Simon Glass <sjg@chromium.org>
Cc: Hans de Goede <hdegoede@redhat.com>
Cc: Tom Rini <trini@konsulko.com>
Cc: Albert Aribaud <albert.u.boot@aribaud.net>
---
v2:
- new patch

 lib/tiny-printf.c | 32 ++++++++++++--------------------
 1 file changed, 12 insertions(+), 20 deletions(-)

Comments

Tom Rini Nov. 23, 2015, 10:44 p.m. UTC | #1
On Mon, Nov 16, 2015 at 03:26:34PM +0100, Stefan Roese wrote:

> With this patch now, the tiny printf() function also supports numbers
> bigger than 0xffff. Additionally the code is simplified a bit and
> some static variables are moved to function parameters. Also the
> upper case hex variable output support is removed, as its not really
> needed in this simple printf version. And removing it reduces the
> complexity and the code size again a bit.
> 
> Here the new numbers, again on the db-mv784mp-gp (Armada XP):
> 
> Without this patch:
>   56542   18536    1956   77034   12cea ./spl/u-boot-spl
> 
> With this patch:
>   56446   18536    1936   76918   12c76 ./spl/u-boot-spl
> 
> Signed-off-by: Stefan Roese <sr@denx.de>
> Cc: Simon Glass <sjg@chromium.org>
> Cc: Hans de Goede <hdegoede@redhat.com>
> Cc: Tom Rini <trini@konsulko.com>
> Cc: Albert Aribaud <albert.u.boot@aribaud.net>

Applied to u-boot/master, thanks!
diff mbox

Patch

diff --git a/lib/tiny-printf.c b/lib/tiny-printf.c
index d743a36..6766a8f 100644
--- a/lib/tiny-printf.c
+++ b/lib/tiny-printf.c
@@ -14,9 +14,6 @@ 
 #include <serial.h>
 
 static char *bf;
-static char buf[12];
-static unsigned int num;
-static char uc;
 static char zs;
 
 static void out(char c)
@@ -26,17 +23,16 @@  static void out(char c)
 
 static void out_dgt(char dgt)
 {
-	out(dgt + (dgt < 10 ? '0' : (uc ? 'A' : 'a') - 10));
+	out(dgt + (dgt < 10 ? '0' : 'a' - 10));
 	zs = 1;
 }
 
-static void div_out(unsigned int div)
+static void div_out(unsigned int *num, unsigned int div)
 {
 	unsigned char dgt = 0;
 
-	num &= 0xffff; /* just for testing the code with 32 bit ints */
-	while (num >= div) {
-		num -= div;
+	while (*num >= div) {
+		*num -= div;
 		dgt++;
 	}
 
@@ -49,6 +45,9 @@  int printf(const char *fmt, ...)
 	va_list va;
 	char ch;
 	char *p;
+	unsigned int num;
+	char buf[12];
+	unsigned int div;
 
 	va_start(va, fmt);
 
@@ -68,7 +67,7 @@  int printf(const char *fmt, ...)
 			if (ch >= '0' && ch <= '9') {
 				w = 0;
 				while (ch >= '0' && ch <= '9') {
-					w = (((w << 2) + w) << 1) + ch - '0';
+					w = (w * 10) + ch - '0';
 					ch = *fmt++;
 				}
 			}
@@ -86,20 +85,13 @@  int printf(const char *fmt, ...)
 					num = -(int)num;
 					out('-');
 				}
-				div_out(10000);
-				div_out(1000);
-				div_out(100);
-				div_out(10);
-				out_dgt(num);
+				for (div = 1000000000; div; div /= 10)
+					div_out(&num, div);
 				break;
 			case 'x':
-			case 'X':
-				uc = ch == 'X';
 				num = va_arg(va, unsigned int);
-				div_out(0x1000);
-				div_out(0x100);
-				div_out(0x10);
-				out_dgt(num);
+				for (div = 0x10000000; div; div /= 0x10)
+					div_out(&num, div);
 				break;
 			case 'c':
 				out((char)(va_arg(va, int)));