>From 1f3b9db8c25bb5996ca5c5c2e988d1babcc5daa6 Mon Sep 17 00:00:00 2001 From: Szabolcs Nagy Date: Mon, 25 May 2026 08:35:41 +0000 Subject: [PATCH 1/4] string.h: use builtin memcpy and memset internally musl is compiled for a freestanding execution environment so the compiler does not make assumptions about standard API calls. If an API is internally used according to the public interface contract, i.e. not relying on musl specific behaviours, then enabling compiler builtin for it is safe. builtins supposed to improve code generation and most useful when a library call can be lowered to a few instructions. e.g. memcpy and memset with a fixed small size can be a few load/store/move ops. unfortunately gcc creates a mess on x86_64 of code like if (n < 64) __builtin_memcpy(d,s,n); if (n < 64) __builtin_memset(p,0,n); libc.so .text change: arch diff size x86_64: +4525 667424 riscv64: +724 613171 aarch64: -432 679747 arm: -152 658809 There are no CFLAGS to selectively enable builtins so function-like macro is used to turn memcpy calls into __builtin_memcpy and undef is used to suppress the macro. for the record, top functions called in musl that have gcc builtin, sorted by number of call sites, first supported gcc version noted: x86_64 aarch64 riscv64 arm sym version notes 173 174 163 186 memcpy gcc-2 * 109 112 111 110 free gcc-4.3 alloc 80 80 80 80 strlen gcc-2 54 81 60 115 memset gcc-2 * 51 53 51 51 malloc gcc-3.4 alloc 44 44 44 44 strchr gcc-3 35 35 35 35 strcmp gcc-2 34 34 34 34 iswalnum gcc-4 wchar 30 30 30 30 strncmp gcc-3 30 30 30 30 memcmp gcc-2 * 27 27 27 28 strnlen gcc-9 25 25 25 26 cos gcc-3 fp-inexact 23 26 24 25 snprintf gcc-3.3 fmt 22 22 22 23 fabs gcc-2 * 21 22 20 23 sin gcc-3 fp-inexact 20 20 20 20 realloc gcc-4.3 alloc 19 19 19 19 fabsf gcc-3 * 18 18 18 18 strcpy gcc-2 18 18 18 19 sqrt gcc-3 *eval-prec,errno 18 17 16 18 fprintf gcc-3 fmt 18 18 18 18 calloc gcc-3.4 alloc 17 17 17 17 towlower gcc-4 wchar 17 17 17 17 cosf gcc-3 fp-inexact 16 16 16 16 logf gcc-3.3 fp-inexact 16 17 17 17 log gcc-3.3 fp-inexact 15 13 14 16 scalbn gcc-3.4 15 15 14 15 copysignf gcc-3.4 * 15 15 15 16 copysign gcc-3.4 * 14 14 13 14 sinf gcc-3 fp-inexact 13 13 13 13 towupper gcc-4 wchar 12 2 2 0 logl gcc-3.3 fp-inexact 11 11 11 11 sprintf gcc-3.3 fmt 11 12 12 12 exp gcc-3.3 fp-inexact 10 10 10 10 sqrtf gcc-3 * 10 10 10 3 scalbnl gcc-3.4 10 10 10 10 memchr gcc-4.3 10 10 10 10 iswspace gcc-4 wchar 10 0 0 0 floorl gcc-3 10 6 6 1 fabsl gcc-3 * 10 10 10 10 expf gcc-3.3 fp-inexact --- src/include/string.h | 5 +++++ src/string/memcpy.c | 1 + src/string/memset.c | 1 + 3 files changed, 7 insertions(+) diff --git a/src/include/string.h b/src/include/string.h index 2133b5c1..997400b0 100644 --- a/src/include/string.h +++ b/src/include/string.h @@ -8,4 +8,9 @@ hidden char *__stpcpy(char *, const char *); hidden char *__stpncpy(char *, const char *, size_t); hidden char *__strchrnul(const char *, int); +#ifdef __GNUC__ +#define memcpy(x,y,z) __builtin_memcpy(x,y,z) +#define memset(x,y,z) __builtin_memset(x,y,z) +#endif + #endif diff --git a/src/string/memcpy.c b/src/string/memcpy.c index 06e88742..985529f7 100644 --- a/src/string/memcpy.c +++ b/src/string/memcpy.c @@ -2,6 +2,7 @@ #include #include +#undef memcpy void *memcpy(void *restrict dest, const void *restrict src, size_t n) { unsigned char *d = dest; diff --git a/src/string/memset.c b/src/string/memset.c index 5613a148..b3dcf585 100644 --- a/src/string/memset.c +++ b/src/string/memset.c @@ -1,6 +1,7 @@ #include #include +#undef memset void *memset(void *dest, int c, size_t n) { unsigned char *s = dest; -- 2.52.0