libs/libc/risc-v: Add optimized string and memory functions. - #19782
Merged
xiaoxiang781216 merged 3 commits intoAug 12, 2026
Conversation
Shanks0224
force-pushed
the
riscv-string-functions-v2
branch
from
August 11, 2026 05:27
aa5f207 to
1e0041d
Compare
Contributor
|
@Fishwaldo could you benchmark this patch on your hardware? |
Add assembly-optimized implementations for 14 string/memory functions using word-at-a-time techniques (DETECTNULL, broadcast+XOR) and XLEN-adaptive macros for both RV32 and RV64: - memmove: direction check + forward tail to memcpy, reverse path with 16xSZREG unroll and shift-merge for misaligned src. - memcmp: word-granularity compare when both pointers share alignment, bytewise fallback for mismatched pointers. - memchr: broadcast target byte, XOR with each word, DETECTNULL to find matches. Counter-based bounds (no pointer overflow). - strlen: DETECTNULL word loop, constants loaded from .srodata. - strnlen: strlen with counter-based length limit. - strcpy/strncpy: word loop with DETECTNULL, zero-fill remainder 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 char and null simultaneously. strchrnul reuses strchr via #define. - strrchr: forward scan recording last match position. - strncmp: word-at-a-time compare with null detection and counter. - strcat: strlen(dst) then strcpy(dst_end, src) word-at-a-time. Each function is independently selectable via CONFIG_RISCV_<FUNC>, or all enabled together with CONFIG_RISCV_STRING_FUNCTION=y. Assisted-by: Claude Opus 5 (1M context) <noreply@anthropic.com> Signed-off-by: ganjing <ganjing@xiaomi.com>
Reduce branch overhead in the memcmp main loop by comparing four words per iteration: XOR each pair, OR the four differences together, and branch once. On a mismatch the single-word loop locates the exact differing word within four words of the fault. Add a beqz guard at .Lbyte_cmp entry to handle the case where the 4-word loop consumes all remaining bytes exactly. Measured on QEMU RV32: memcmp(128) 313 -> 271 cycles (13% faster). Assisted-by: Claude Opus 5 (1M context) <noreply@anthropic.com> Signed-off-by: ganjing <ganjing@xiaomi.com>
Shanks0224
force-pushed
the
riscv-string-functions-v2
branch
from
August 11, 2026 08:08
47cc270 to
b56ea73
Compare
Shanks0224
requested review from
Donny9,
tmedicci and
xiaoxiang781216
as code owners
August 11, 2026 08:08
Add word-at-a-time strlcpy using DETECTNULL for both the copy phase and the strlen tail when truncated. The copy loop aligns src and processes a register at a time, falling to bytewise for the last word containing the terminator. When truncated, the remaining src length is measured with a second word-at-a-time loop. strlcpy has 46 call sites in a typical kernel image (more than strcpy) and is not covered by newlib OPTSPEED, making it a high-value target. Assisted-by: Claude Opus 5 (1M context) <noreply@anthropic.com> Signed-off-by: ganjing <ganjing@xiaomi.com>
Shanks0224
force-pushed
the
riscv-string-functions-v2
branch
from
August 11, 2026 09:27
b56ea73 to
b66728b
Compare
xiaoxiang781216
approved these changes
Aug 11, 2026
simbit18
approved these changes
Aug 11, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
with 16xSZREG unroll and shift-merge for misaligned source.
find matches. Counter-based bounds to avoid pointer overflow.
strncpy reuses strcpy via #define USE_AS_STRNCPY.
strchrnul reuses strchr via #define USE_AS_STRCHRNUL.
Each function is independently selectable via CONFIG_RISCV_, or
all enabled together with CONFIG_RISCV_STRING_FUNCTION=y.
Impact
Testing
I confirm that changes are verified on local setup and works as intended:
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):
Testing logs (optimized, all functions):