From 686a23585e2bc8a357842bd30ea3cae0cc5648da Mon Sep 17 00:00:00 2001 From: Vincent Palatin Date: Thu, 9 Apr 2015 12:28:49 -0700 Subject: [PATCH] optimize printf size without the common runtime When a platform is built without the common runtime to optimize for flash size, remove the 64-bit support in printf to save more space (mainly by getting rid of the uint64divmod helper). This saves 376 bytes of flash on Zinger/MiniMuffin. Signed-off-by: Vincent Palatin BRANCH=none BUG=none TEST=make buildall and compare flash size with and without the change. the common runtime binaries are identical excepted the version information. Change-Id: I1e7237e693df9ea23291c8c0fe414c3b5c716848 Reviewed-on: https://chromium-review.googlesource.com/265052 Trybot-Ready: Vincent Palatin Tested-by: Vincent Palatin Reviewed-by: Alec Berg Commit-Queue: Vincent Palatin --- common/printf.c | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/common/printf.c b/common/printf.c index 786d46711d..6e1a7401e9 100644 --- a/common/printf.c +++ b/common/printf.c @@ -13,6 +13,22 @@ static const char error_str[] = "ERROR"; #define MAX_FORMAT 1024 /* Maximum chars in a single format field */ +#ifdef CONFIG_COMMON_RUNTIME +static inline int divmod(uint64_t *n, int d) +{ + return uint64divmod(n, d); +} +#else /* !CONFIG_COMMON_RUNTIME */ +/* if we are optimizing for size, remove the 64-bit support */ +#define NO_UINT64_SUPPORT +static inline int divmod(uint32_t *n, int d) +{ + int r = *n % d; + *n /= d; + return r; +} +#endif + /** * Convert the lowest nibble of a number to hex * @@ -152,8 +168,13 @@ int vfnprintf(int (*addchar)(void *context, int c), void *context, continue; } else { - uint64_t v; int base = 10; +#ifdef NO_UINT64_SUPPORT + uint32_t v; + + v = va_arg(args, uint32_t); +#else /* NO_UINT64_SUPPORT */ + uint64_t v; /* Handle length */ if (c == 'l') { @@ -171,6 +192,7 @@ int vfnprintf(int (*addchar)(void *context, int c), void *context, } else { v = va_arg(args, uint32_t); } +#endif switch (c) { case 'd': @@ -224,7 +246,7 @@ int vfnprintf(int (*addchar)(void *context, int c), void *context, * numbers. */ for (vlen = 0; vlen < precision; vlen++) - *(--vstr) = '0' + uint64divmod(&v, 10); + *(--vstr) = '0' + divmod(&v, 10); if (precision) *(--vstr) = '.'; @@ -232,7 +254,7 @@ int vfnprintf(int (*addchar)(void *context, int c), void *context, *(--vstr) = '0'; while (v) { - int digit = uint64divmod(&v, base); + int digit = divmod(&v, base); if (digit < 10) *(--vstr) = '0' + digit; else if (c == 'X')