Shanks0224 opened a new pull request, #19782:
URL: https://github.com/apache/nuttx/pull/19782
## libs/libc/risc-v: Add optimized string and memory functions.
### Summary
Add assembly-optimized implementations for 14 string/memory functions
using word-at-a-time techniques and XLEN-adaptive macros for both RV32
and RV64. The generic C library processes these functions byte by byte;
these replacements work a register width at a time (4 bytes on RV32,
8 on RV64) after aligning the pointers.
Functions added:
- memmove: direction check + tail to memcpy for forward, reverse path
with 16xSZREG unroll and shift-merge for misaligned source.
- memcmp: word-granularity compare when both pointers share alignment.
- memchr: broadcast target byte, XOR with each word, DETECTNULL to
find matches. Counter-based bounds to avoid pointer overflow.
- strlen, strnlen: DETECTNULL word loop with constants from .srodata.
- strcpy, strncpy: word loop with DETECTNULL, zero-fill for strncpy.
strncpy reuses strcpy via #define USE_AS_STRNCPY.
- stpcpy, stpncpy: reuse strcpy/strncpy via #define USE_AS_STPCPY.
- strchr, strchrnul: broadcast+XOR detecting both target and null.
strchrnul reuses strchr via #define USE_AS_STRCHRNUL.
- strrchr: forward scan recording last match position.
- strncmp: word-at-a-time compare with null detection and counter.
- strcat: strlen(dst) then word-at-a-time copy from src.
Each function is independently selectable via CONFIG_RISCV_<FUNC>, or
all enabled together with CONFIG_RISCV_STRING_FUNCTION=y.
### Impact
- Is new feature added? YES. 14 new arch-optimized string functions behind
Kconfig options (default n).
- Impact on user? NO. Must opt-in via CONFIG_RISCV_STRING_FUNCTION=y.
- Impact on build? NO. New source files compiled only when selected.
- Impact on hardware? NO.
- Impact on documentation? NO.
- Impact on security? NO.
- Impact on compatibility? NO. Existing behavior unchanged when options are
off.
### Testing
I confirm that changes are verified on local setup and works as intended:
- Build Host: Linux x86_64, riscv-none-elf-gcc 13.2.1
- Target(s): QEMU rv-virt RV32 (rv-virt:nsh), QEMU rv-virt RV64
(rv-virt:nsh64)
Correctness: arch_libctest reports PASSED for all 16 functions across
alignments 0-7 and boundary sizes 0-128, including overlap tests for
memmove.
Performance (QEMU RV32, rdcycle, 128 bytes unless noted, 100 iterations avg):
```
baseline optimized speedup
memmove(128) 564 423 1.33x
memcmp(128) 686 316 2.17x
memchr(128) 305 264 1.16x
strlen(128) 474 274 1.73x
strnlen(128) 579 320 1.81x
strcmp(128) 621 305 2.04x
strcpy(128) 441 300 1.47x
strncpy(128) 599 360 1.66x
stpcpy(128) 479 315 1.52x
strchr(128) 341 258 1.32x
strchrnul(128) 270 241 1.12x
strrchr(128) 557 523 1.07x
strncmp(128) 675 367 1.84x
strcat(64) 418 267 1.57x
```
Testing logs (optimized, all functions):
```
nsh> arch_libctest
Testing memcpy...
memcpy: PASSED
memcpy(128) avg cycles: 676
Testing memmove...
memmove: PASSED
memmove(128) avg cycles: 423
Testing memset...
memset: PASSED
memset(128) avg cycles: 237
Testing memcmp...
memcmp: PASSED
memcmp(128) avg cycles: 316
Testing memchr...
memchr: PASSED
memchr(128) avg cycles: 264
Testing strlen...
strlen: PASSED
strlen(128) avg cycles: 274
Testing strcmp...
strcmp: PASSED
strcmp(128) avg cycles: 305
Testing strcpy...
strcpy: PASSED
strcpy(128) avg cycles: 300
Testing strchr...
strchr: PASSED
strchr(128) avg cycles: 258
Testing strncmp...
strncmp: PASSED
strncmp(128) avg cycles: 367
Testing strnlen...
strnlen: PASSED
strnlen(128) avg cycles: 320
Testing strncpy...
strncpy: PASSED
strncpy(128) avg cycles: 360
Testing stpcpy...
stpcpy: PASSED
stpcpy(128) avg cycles: 315
Testing strcat...
strcat: PASSED
strcat(64) avg cycles: 267
Testing strrchr...
strrchr: PASSED
strrchr(128) avg cycles: 523
Testing strchrnul...
strchrnul: PASSED
strchrnul(128) avg cycles: 241
arch_libc_test Passed
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]