Skip to content

Commit 2cae0f6

Browse files
committed
py: Reduce size of mp_printf by eliminating unnecessary code.
Saves around 120 bytes on Thumb2 archs.
1 parent 78744c4 commit 2cae0f6

2 files changed

Lines changed: 12 additions & 3 deletions

File tree

py/mpprint.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,13 @@ int mp_print_strn(const mp_print_t *print, const char *str, mp_uint_t len, int f
120120
// We can use 16 characters for 32-bit and 32 characters for 64-bit
121121
#define INT_BUF_SIZE (sizeof(mp_int_t) * 4)
122122

123-
// This function is used by stmhal port to implement printf.
123+
// Our mp_vprintf function below does not support the '#' format modifier to
124+
// print the prefix of a non-base-10 number, so we don't need code for this.
125+
#define SUPPORT_INT_BASE_PREFIX (0)
126+
127+
// This function is used exclusively by mp_vprintf to format ints.
124128
// It needs to be a separate function to mp_print_mp_int, since converting to a mp_int looses the MSB.
125-
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) {
129+
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) {
126130
char sign = 0;
127131
if (sgn) {
128132
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
153157
} while (b > buf && x != 0);
154158
}
155159

160+
#if SUPPORT_INT_BASE_PREFIX
156161
char prefix_char = '\0';
157162

158163
if (flags & PF_FLAG_SHOW_PREFIX) {
@@ -164,23 +169,28 @@ int mp_print_int(const mp_print_t *print, mp_uint_t x, int sgn, int base, int ba
164169
prefix_char = base_char + 'x' - 'a';
165170
}
166171
}
172+
#endif
167173

168174
int len = 0;
169175
if (flags & PF_FLAG_PAD_AFTER_SIGN) {
170176
if (sign) {
171177
len += mp_print_strn(print, &sign, 1, flags, fill, 1);
172178
width--;
173179
}
180+
#if SUPPORT_INT_BASE_PREFIX
174181
if (prefix_char) {
175182
len += mp_print_strn(print, "0", 1, flags, fill, 1);
176183
len += mp_print_strn(print, &prefix_char, 1, flags, fill, 1);
177184
width -= 2;
178185
}
186+
#endif
179187
} else {
188+
#if SUPPORT_INT_BASE_PREFIX
180189
if (prefix_char && b > &buf[1]) {
181190
*(--b) = prefix_char;
182191
*(--b) = '0';
183192
}
193+
#endif
184194
if (sign && b > buf) {
185195
*(--b) = sign;
186196
}

py/mpprint.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ extern const mp_print_t mp_sys_stdout_print;
5757

5858
int mp_print_str(const mp_print_t *print, const char *str);
5959
int mp_print_strn(const mp_print_t *print, const char *str, mp_uint_t len, int flags, char fill, int width);
60-
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);
6160
#if MICROPY_PY_BUILTINS_FLOAT
6261
int mp_print_float(const mp_print_t *print, mp_float_t f, char fmt, int flags, char fill, int width, int prec);
6362
#endif

0 commit comments

Comments
 (0)