Skip to content

Commit 5d55730

Browse files
nzmichaelhnashif
authored andcommitted
libc: minimal: add size optimized string functions
The current implementations of memcpy and memset are optimized for performance and use a word based loop before the byte based loop. Add a config option that skips the word based loop. This saves 120 bytes on the Cortex-M0+ which is worthwhile on small apps like a bootloader. Enable by default if SIZE_OPTIMIZATIONS is set. Signed-off-by: Michael Hope <mlhx@google.com>
1 parent 395b0c3 commit 5d55730

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

lib/libc/Kconfig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,13 @@ config MINIMAL_LIBC_LL_PRINTF
138138
Build with long long printf enabled. This will increase the size of
139139
the image.
140140

141+
config MINIMAL_LIBC_OPTIMIZE_STRING_FOR_SIZE
142+
bool "Use size optimized string functions"
143+
default y if SIZE_OPTIMIZATIONS
144+
help
145+
Enable smaller but potentially slower implementations of memcpy and
146+
memset. On the Cortex-M0+ this reduces the total code size by 120 bytes.
147+
141148
endif # MINIMAL_LIBC
142149

143150
config STDOUT_CONSOLE

lib/libc/minimal/source/string/string.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,8 @@ void *memcpy(void *_MLIBC_RESTRICT d, const void *_MLIBC_RESTRICT s, size_t n)
301301

302302
unsigned char *d_byte = (unsigned char *)d;
303303
const unsigned char *s_byte = (const unsigned char *)s;
304+
305+
#if !defined(CONFIG_MINIMAL_LIBC_OPTIMIZE_STRING_FOR_SIZE)
304306
const uintptr_t mask = sizeof(mem_word_t) - 1;
305307

306308
if ((((uintptr_t)d ^ (uintptr_t)s_byte) & mask) == 0) {
@@ -328,6 +330,7 @@ void *memcpy(void *_MLIBC_RESTRICT d, const void *_MLIBC_RESTRICT s, size_t n)
328330
d_byte = (unsigned char *)d_word;
329331
s_byte = (unsigned char *)s_word;
330332
}
333+
#endif
331334

332335
/* do byte-sized copying until finished */
333336

@@ -353,6 +356,7 @@ void *memset(void *buf, int c, size_t n)
353356
unsigned char *d_byte = (unsigned char *)buf;
354357
unsigned char c_byte = (unsigned char)c;
355358

359+
#if !defined(CONFIG_MINIMAL_LIBC_OPTIMIZE_STRING_FOR_SIZE)
356360
while (((uintptr_t)d_byte) & (sizeof(mem_word_t) - 1)) {
357361
if (n == 0) {
358362
return buf;
@@ -380,6 +384,7 @@ void *memset(void *buf, int c, size_t n)
380384
/* do byte-sized initialization until finished */
381385

382386
d_byte = (unsigned char *)d_word;
387+
#endif
383388

384389
while (n > 0) {
385390
*(d_byte++) = c_byte;

0 commit comments

Comments
 (0)