nuttx/libs/libc/string
Justin Hammond afc42d0aa2 libs/libc/string: Copy and compare by words when pointers agree on alignment.
The BSD string functions take a word path only when both pointers are
aligned, and a byte path otherwise.  A pair at the same offset from a
boundary takes the byte path even though copying or comparing a few leading
bytes aligns both at once, since aligning one aligns the other.

Add MISALIGNED(), which asks whether two pointers disagree about where a
boundary falls, and walk an agreeing pair up to the boundary before the
existing path selection.  MISALIGNED4() does the same for the 4-byte path,
so a pair that is 4-byte but not 8-byte aligned reaches the wide path
instead of the middle one.  No existing line changes: the walk is a new step
ahead of the current decisions.  A pair at differing offsets still takes the
byte path, since no single boundary serves both.

Measured on an EIC7700 EVB (EIC7700X, RV64GC, 1.4GHz) with the BSD string
functions selected and the RISC-V assembly ones disabled, using the
benchmark in apps#3706, medians of 3 runs in MB/s at its largest size:

                equal offset            aligned
  memcpy     414 -> 4148  10.0x    4214 -> 4208
  memcmp      41 ->  361   8.8x     362 ->  360
  strncmp     28 ->  202   7.4x     207 ->  207
  strcmp      42 ->  273   6.5x     278 ->  276
  strncpy    377 -> 1676   4.5x    1824 -> 1748
  stpncpy    376 -> 1654   4.4x    1843 -> 1724
  stpcpy     551 -> 1833   3.3x    1970 -> 1939
  memccpy    650 -> 2012   3.1x    2478 -> 2016
  strcpy     636 -> 1837   2.9x    1678 -> 1965

Cases the walk never runs for move in both directions by up to a third, the
largest being memccpy at differing offsets, 648 -> 414.  Their code is
unchanged, so that is code placement rather than an effect of the change.

The change is architecture independent but has only been measured on
RV64GC.  Word size, alignment cost and byte loop codegen all differ
elsewhere, so the balance wants measuring on other architectures.

Assisted-by: Claude:claude-opus-5
Signed-off-by: Justin Hammond <justin@dynam.ac>
2026-08-24 12:02:02 +08:00
..
CMakeLists.txt libc/string: Add BSD-optimized memset implementation. 2026-02-04 03:02:04 +08:00
Kconfig libc: Rename LIBC_STRING_OPTIMIZE to LIBC_NEWLIB_OPTSPEED 2025-01-20 22:27:19 +08:00
lib_bsdmemccpy.c libs/libc/string: Copy and compare by words when pointers agree on alignment. 2026-08-24 12:02:02 +08:00
lib_bsdmemchr.c libs/libc/string: Add 4-byte alignment middle path for BSD functions. 2026-08-17 10:47:33 +02:00
lib_bsdmemcmp.c libs/libc/string: Copy and compare by words when pointers agree on alignment. 2026-08-24 12:02:02 +08:00
lib_bsdmemcpy.c libs/libc/string: Copy and compare by words when pointers agree on alignment. 2026-08-24 12:02:02 +08:00
lib_bsdmemrchr.c libs/libc/string: Add 4-byte alignment middle path for BSD functions. 2026-08-17 10:47:33 +02:00
lib_bsdmemset.c libs/libc/string: Add 4-byte alignment middle path for BSD functions. 2026-08-17 10:47:33 +02:00
lib_bsdstpcpy.c libs/libc/string: Copy and compare by words when pointers agree on alignment. 2026-08-24 12:02:02 +08:00
lib_bsdstpncpy.c libs/libc/string: Copy and compare by words when pointers agree on alignment. 2026-08-24 12:02:02 +08:00
lib_bsdstrcat.c libs/libc/string: Use long long to speed up the BSD string functions. 2026-08-17 10:47:33 +02:00
lib_bsdstrchr.c libs/libc/string: Use long long to speed up the BSD string functions. 2026-08-17 10:47:33 +02:00
lib_bsdstrchrnul.c libc_bsd:add missing SPDX-License 2026-02-04 03:02:04 +08:00
lib_bsdstrcmp.c libs/libc/string: Copy and compare by words when pointers agree on alignment. 2026-08-24 12:02:02 +08:00
lib_bsdstrcpy.c libs/libc/string: Copy and compare by words when pointers agree on alignment. 2026-08-24 12:02:02 +08:00
lib_bsdstrlen.c libs/libc/string: Use long long to speed up the BSD string functions. 2026-08-17 10:47:33 +02:00
lib_bsdstrncmp.c libs/libc/string: Copy and compare by words when pointers agree on alignment. 2026-08-24 12:02:02 +08:00
lib_bsdstrncpy.c libs/libc/string: Copy and compare by words when pointers agree on alignment. 2026-08-24 12:02:02 +08:00
lib_bsdstrrchr.c libc_bsd:add missing SPDX-License 2026-02-04 03:02:04 +08:00
lib_explicit_bzero.c libs/libc: migrate to SPDX identifier 2024-10-01 12:25:52 +08:00
lib_ffs.c libs/libc: migrate to SPDX identifier 2024-10-01 12:25:52 +08:00
lib_ffsl.c libs/libc: migrate to SPDX identifier 2024-10-01 12:25:52 +08:00
lib_ffsll.c !compiler: drop CONFIG_HAVE_LONG_LONG and require long long support 2026-05-19 16:21:28 +08:00
lib_fls.c libs/libc: migrate to SPDX identifier 2024-10-01 12:25:52 +08:00
lib_flsl.c libs/libc: migrate to SPDX identifier 2024-10-01 12:25:52 +08:00
lib_flsll.c !compiler: drop CONFIG_HAVE_LONG_LONG and require long long support 2026-05-19 16:21:28 +08:00
lib_index.c libc: remove reference to non-existent readme 2025-10-11 17:07:51 -04:00
lib_isbasedigit.c libs/libc: migrate to SPDX identifier 2024-10-01 12:25:52 +08:00
lib_memccpy.c libc: remove reference to non-existent readme 2025-10-11 17:07:51 -04:00
lib_memchr.c string:use builtin function to optimize libc function 2025-12-20 10:38:53 +08:00
lib_memcmp.c libc: remove reference to non-existent readme 2025-10-11 17:07:51 -04:00
lib_memcpy.c libc: remove reference to non-existent readme 2025-10-11 17:07:51 -04:00
lib_memmem.c libc: remove reference to non-existent readme 2025-10-11 17:07:51 -04:00
lib_memmove.c libc: remove reference to non-existent readme 2025-10-11 17:07:51 -04:00
lib_mempcpy.c libc: remove reference to non-existent readme 2025-10-11 17:07:51 -04:00
lib_memrchr.c libc: remove reference to non-existent readme 2025-10-11 17:07:51 -04:00
lib_memset.c libs/libc/string: Fix sign extension in memset word fill pattern. 2026-08-17 10:47:33 +02:00
lib_popcount.c libs/libc: migrate to SPDX identifier 2024-10-01 12:25:52 +08:00
lib_popcountl.c libs/libc: migrate to SPDX identifier 2024-10-01 12:25:52 +08:00
lib_popcountll.c libs/libc: migrate to SPDX identifier 2024-10-01 12:25:52 +08:00
lib_rawmemchr.c libs/libc: migrate to SPDX identifier 2024-10-01 12:25:52 +08:00
lib_rindex.c libc: remove reference to non-existent readme 2025-10-11 17:07:51 -04:00
lib_skipspace.c libs/libc: migrate to SPDX identifier 2024-10-01 12:25:52 +08:00
lib_stpcpy.c libc: remove reference to non-existent readme 2025-10-11 17:07:51 -04:00
lib_stpncpy.c libc: remove reference to non-existent readme 2025-10-11 17:07:51 -04:00
lib_strcasecmp.c libc: remove reference to non-existent readme 2025-10-11 17:07:51 -04:00
lib_strcasestr.c libc: remove reference to non-existent readme 2025-10-11 17:07:51 -04:00
lib_strcat.c string:use builtin function to optimize libc function 2025-12-20 10:38:53 +08:00
lib_strchr.c string:use builtin function to optimize libc function 2025-12-20 10:38:53 +08:00
lib_strchrnul.c libc: remove reference to non-existent readme 2025-10-11 17:07:51 -04:00
lib_strcmp.c libc: remove reference to non-existent readme 2025-10-11 17:07:51 -04:00
lib_strcoll.c Merge the newlibc string into NuttX. 2024-10-17 22:52:42 +08:00
lib_strcpy.c string:use builtin function to optimize libc function 2025-12-20 10:38:53 +08:00
lib_strcspn.c string:use builtin function to optimize libc function 2025-12-20 10:38:53 +08:00
lib_strdup.c libc: remove reference to non-existent readme 2025-10-11 17:07:51 -04:00
lib_strerror.c libs/libc: migrate to SPDX identifier 2024-10-01 12:25:52 +08:00
lib_strerrorr.c libs/libc: migrate to SPDX identifier 2024-10-01 12:25:52 +08:00
lib_strlcat.c libc: remove reference to non-existent readme 2025-10-11 17:07:51 -04:00
lib_strlcpy.c libc: remove reference to non-existent readme 2025-10-11 17:07:51 -04:00
lib_strlen.c string:use builtin function to optimize libc function 2025-12-20 10:38:53 +08:00
lib_strncasecmp.c libc: remove reference to non-existent readme 2025-10-11 17:07:51 -04:00
lib_strncat.c libc: remove reference to non-existent readme 2025-10-11 17:07:51 -04:00
lib_strncmp.c string:use builtin function to optimize libc function 2025-12-20 10:38:53 +08:00
lib_strncpy.c libc: remove reference to non-existent readme 2025-10-11 17:07:51 -04:00
lib_strndup.c libc: remove reference to non-existent readme 2025-10-11 17:07:51 -04:00
lib_strnlen.c libc: remove reference to non-existent readme 2025-10-11 17:07:51 -04:00
lib_strpbrk.c libc: remove reference to non-existent readme 2025-10-11 17:07:51 -04:00
lib_strrchr.c string:use builtin function to optimize libc function 2025-12-20 10:38:53 +08:00
lib_strsep.c libc: remove reference to non-existent readme 2025-10-11 17:07:51 -04:00
lib_strsignal.c libs/libc: migrate to SPDX identifier 2024-10-01 12:25:52 +08:00
lib_strspn.c libc: remove reference to non-existent readme 2025-10-11 17:07:51 -04:00
lib_strstr.c string:use builtin function to optimize libc function 2025-12-20 10:38:53 +08:00
lib_strtok.c libc: remove reference to non-existent readme 2025-10-11 17:07:51 -04:00
lib_strtokr.c libc: remove reference to non-existent readme 2025-10-11 17:07:51 -04:00
lib_strverscmp.c libs/libc: migrate to SPDX identifier 2024-10-01 12:25:52 +08:00
lib_strxfrm.c libs/libc: migrate to SPDX identifier 2024-10-01 12:25:52 +08:00
lib_timingsafe_bcmp.c libs/libc: migrate to SPDX identifier 2024-10-01 12:25:52 +08:00
lib_vikmemcpy.c !compiler: drop CONFIG_HAVE_LONG_LONG and require long long support 2026-05-19 16:21:28 +08:00
Make.defs libc/string: Add BSD-optimized memset implementation. 2026-02-04 03:02:04 +08:00