From 2cae0f62908812b6cd925943ee70e7e0bef0387e Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 28 May 2015 13:54:56 +0000 Subject: [PATCH] py: Reduce size of mp_printf by eliminating unnecessary code. Saves around 120 bytes on Thumb2 archs. --- py/mpprint.c | 14 ++++++++++++-- py/mpprint.h | 1 - 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/py/mpprint.c b/py/mpprint.c index 78a0b7aa55..51c16f4e3d 100644 --- a/py/mpprint.c +++ b/py/mpprint.c @@ -120,9 +120,13 @@ int mp_print_strn(const mp_print_t *print, const char *str, mp_uint_t len, int f // We can use 16 characters for 32-bit and 32 characters for 64-bit #define INT_BUF_SIZE (sizeof(mp_int_t) * 4) -// This function is used by stmhal port to implement printf. +// Our mp_vprintf function below does not support the '#' format modifier to +// print the prefix of a non-base-10 number, so we don't need code for this. +#define SUPPORT_INT_BASE_PREFIX (0) + +// This function is used exclusively by mp_vprintf to format ints. // It needs to be a separate function to mp_print_mp_int, since converting to a mp_int looses the MSB. -int mp_print_int(const mp_print_t *print, mp_uint_t x, int sgn, int base, int base_char, int flags, char fill, int width) { +STATIC int mp_print_int(const mp_print_t *print, mp_uint_t x, int sgn, int base, int base_char, int flags, char fill, int width) { char sign = 0; if (sgn) { if ((mp_int_t)x < 0) { @@ -153,6 +157,7 @@ int mp_print_int(const mp_print_t *print, mp_uint_t x, int sgn, int base, int ba } while (b > buf && x != 0); } + #if SUPPORT_INT_BASE_PREFIX char prefix_char = '\0'; if (flags & PF_FLAG_SHOW_PREFIX) { @@ -164,6 +169,7 @@ int mp_print_int(const mp_print_t *print, mp_uint_t x, int sgn, int base, int ba prefix_char = base_char + 'x' - 'a'; } } + #endif int len = 0; if (flags & PF_FLAG_PAD_AFTER_SIGN) { @@ -171,16 +177,20 @@ int mp_print_int(const mp_print_t *print, mp_uint_t x, int sgn, int base, int ba len += mp_print_strn(print, &sign, 1, flags, fill, 1); width--; } + #if SUPPORT_INT_BASE_PREFIX if (prefix_char) { len += mp_print_strn(print, "0", 1, flags, fill, 1); len += mp_print_strn(print, &prefix_char, 1, flags, fill, 1); width -= 2; } + #endif } else { + #if SUPPORT_INT_BASE_PREFIX if (prefix_char && b > &buf[1]) { *(--b) = prefix_char; *(--b) = '0'; } + #endif if (sign && b > buf) { *(--b) = sign; } diff --git a/py/mpprint.h b/py/mpprint.h index de497ce90f..ceea3b978d 100644 --- a/py/mpprint.h +++ b/py/mpprint.h @@ -57,7 +57,6 @@ extern const mp_print_t mp_sys_stdout_print; int mp_print_str(const mp_print_t *print, const char *str); int mp_print_strn(const mp_print_t *print, const char *str, mp_uint_t len, int flags, char fill, int width); -int mp_print_int(const mp_print_t *print, mp_uint_t x, int sgn, int base, int base_char, int flags, char fill, int width); #if MICROPY_PY_BUILTINS_FLOAT int mp_print_float(const mp_print_t *print, mp_float_t f, char fmt, int flags, char fill, int width, int prec); #endif