From 41a4a40879acdaa7e9a22f6574145715bef09e60 Mon Sep 17 00:00:00 2001 From: Johannes Date: Fri, 15 Feb 2019 19:10:40 -0600 Subject: [PATCH] libs/libc/stdio/nano_libvsprintf.c: Add long long support. CONFIG_LIBC_LONG_LONG needs at least CONFIG_NANO_PRINTLEVEL 2. Code size for compile without CONFIG_LIBC_LONG_LONG shouldn't be affected. --- libs/libc/stdio/Kconfig | 7 ++++--- libs/libc/stdio/nano_libvsprintf.c | 28 ++++++++++++++++++++++++---- libs/libc/stdio/nano_ultoa_invert.c | 4 ++++ libs/libc/stdio/nano_ultoa_invert.h | 8 +++++++- 4 files changed, 39 insertions(+), 8 deletions(-) diff --git a/libs/libc/stdio/Kconfig b/libs/libc/stdio/Kconfig index 05d8523ba4..a63bf8e165 100644 --- a/libs/libc/stdio/Kconfig +++ b/libs/libc/stdio/Kconfig @@ -118,7 +118,6 @@ config NANO_PRINTF bool "Use nano printf code" default y if DEFAULT_SMALL default n if !DEFAULT_SMALL - depends on !LIBC_LONG_LONG ---help--- Replace printf code with version from newlib-nano. Can be signifcantly smaller, especially if floating support is enabled. @@ -128,8 +127,10 @@ config NANO_PRINTLEVEL int "Nano printf support level" default 2 if !LIBC_FLOATINGPOINT default 3 if LIBC_FLOATINGPOINT - range 1 2 if !LIBC_FLOATINGPOINT - range 1 3 if LIBC_FLOATINGPOINT + range 1 2 if !LIBC_FLOATINGPOINT && !LIBC_LONG_LONG + range 1 3 if LIBC_FLOATINGPOINT && !LIBC_LONG_LONG + range 2 2 if !LIBC_FLOATINGPOINT && LIBC_LONG_LONG + range 2 3 if LIBC_FLOATINGPOINT && LIBC_LONG_LONG depends on NANO_PRINTF ---help--- Nano printf can be built into more than one support level. The diff --git a/libs/libc/stdio/nano_libvsprintf.c b/libs/libc/stdio/nano_libvsprintf.c index 98a9638f39..7aeea5bb3b 100644 --- a/libs/libc/stdio/nano_libvsprintf.c +++ b/libs/libc/stdio/nano_libvsprintf.c @@ -326,7 +326,11 @@ int lib_vsprintf(FAR struct lib_outstream_s *stream, int prec; union { +# ifdef CONFIG_LIBC_LONG_LONG + unsigned char __buf[22]; /* Size for -1 in octal, without '\0' */ +# else unsigned char __buf[11]; /* Size for -1 in octal, without '\0' */ +# endif # if PRINTF_LEVEL >= PRINTF_FLT struct dtoa_s __dtoa; # endif @@ -833,8 +837,16 @@ int lib_vsprintf(FAR struct lib_outstream_s *stream, if (c == 'd' || c == 'i') { long x; +#ifdef CONFIG_LIBC_LONG_LONG + long long x; - if (flags & FL_LONG) + if ((flags & FL_LONGLONG) != 0) + { + x = va_arg(ap, long long); + } + else +#endif + if ((flags & FL_LONG) != 0) { x = va_arg(ap, long); } @@ -865,10 +877,18 @@ int lib_vsprintf(FAR struct lib_outstream_s *stream, } else { - int base; unsigned long x; + int base; +#ifdef CONFIG_LIBC_LONG_LONG + unsigned long long x; - if (flags & FL_LONG) + if (flags & FL_LONGLONG) != 0) + { + x = va_arg(ap, unsigned long long); + } + else +#endif + if ((flags & FL_LONG) != 0) { x = va_arg(ap, unsigned long); } @@ -921,7 +941,7 @@ int lib_vsprintf(FAR struct lib_outstream_s *stream, } else { - c = __ultoa_invert(x, (char *)buf, base) - (char *)buf; + c = __ultoa_invert(x, (char *)buf, base) - (char *)buf; } flags &= ~FL_NEGATIVE; diff --git a/libs/libc/stdio/nano_ultoa_invert.c b/libs/libc/stdio/nano_ultoa_invert.c index 6bd28e9067..f538a5416f 100644 --- a/libs/libc/stdio/nano_ultoa_invert.c +++ b/libs/libc/stdio/nano_ultoa_invert.c @@ -43,7 +43,11 @@ * Public Functions ****************************************************************************/ +#ifdef CONFIG_LIBC_LONG_LONG +FAR char *__ultoa_invert(unsigned long long val, FAR char *str, int base) +#else FAR char *__ultoa_invert(unsigned long val, FAR char *str, int base) +#endif { int upper = 0; diff --git a/libs/libc/stdio/nano_ultoa_invert.h b/libs/libc/stdio/nano_ultoa_invert.h index 634d2eb88a..969ebeafd3 100644 --- a/libs/libc/stdio/nano_ultoa_invert.h +++ b/libs/libc/stdio/nano_ultoa_invert.h @@ -42,6 +42,8 @@ #include +#include + /**************************************************************************** * Pre-processor Definitions ****************************************************************************/ @@ -57,6 +59,10 @@ /* Internal function for use from `printf'. */ -FAR char *__ultoa_invert(unsigned long val, FAR char *s, int base); +#ifdef CONFIG_LIBC_LONG_LONG +FAR char *__ultoa_invert(unsigned long long val, FAR char *str, int base); +#else +FAR char *__ultoa_invert(unsigned long val, FAR char *str, int base); +#endif #endif /* __LIBS_LIBC_STDIO_NANO_ULTOA_INVERT_H */