nuttx/libs/libc/machine/risc-v/arch_strlen.S
ganjing 9b20c629fe libs/libc/risc-v: Add optimized string and memory functions.
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>
2026-08-12 10:23:47 +08:00

117 lines
2.5 KiB
ArmAsm

/****************************************************************************
* libs/libc/machine/risc-v/arch_strlen.S
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
#include "libc.h"
#ifdef LIBC_BUILD_STRLEN
#include "asm.h"
.text
.global ARCH_LIBCFUN(strlen)
.type ARCH_LIBCFUN(strlen), @function
.align 2
/************************************************************************************
* Name: strlen
*
* size_t strlen(const char *s)
*
* Word-at-a-time DETECTNULL scanning.
************************************************************************************/
ARCH_LIBCFUN(strlen):
.cfi_sections .debug_frame
.cfi_startproc
mv t0, a0
.Lbyte_head:
andi t1, t0, SZREG-1
beqz t1, .Laligned
lbu t2, 0(t0)
beqz t2, .Ldone
addi t0, t0, 1
j .Lbyte_head
.Laligned:
#if SZREG == 8
lla t3, .Lmask01
ld t3, 0(t3)
lla t4, .Lmask80
ld t4, 0(t4)
#else
li t3, 0x01010101
li t4, 0x80808080
#endif
.Lword_loop:
REG_L t1, 0(t0)
sub t2, t1, t3
not t5, t1
and t2, t2, t5
and t2, t2, t4
bnez t2, .Lfind_null
addi t0, t0, SZREG
j .Lword_loop
.Lfind_null:
lbu t2, 0(t0)
beqz t2, .Ldone
addi t0, t0, 1
lbu t2, 0(t0)
beqz t2, .Ldone
addi t0, t0, 1
lbu t2, 0(t0)
beqz t2, .Ldone
addi t0, t0, 1
#if SZREG == 8
lbu t2, 0(t0)
beqz t2, .Ldone
addi t0, t0, 1
lbu t2, 0(t0)
beqz t2, .Ldone
addi t0, t0, 1
lbu t2, 0(t0)
beqz t2, .Ldone
addi t0, t0, 1
lbu t2, 0(t0)
beqz t2, .Ldone
addi t0, t0, 1
#endif
.Ldone:
sub a0, t0, a0
ret
.cfi_endproc
.size ARCH_LIBCFUN(strlen), .-ARCH_LIBCFUN(strlen)
#if SZREG == 8
.section .srodata.cst8,"aM",@progbits,8
.align 3
.Lmask01:
.dword 0x0101010101010101
.Lmask80:
.dword 0x8080808080808080
#endif
#endif