This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit c8e9b1f9b5f22b28cd4a5f6eba7dfc9e4b3f7a0d
Author: anjiahao <[email protected]>
AuthorDate: Thu Jul 2 20:27:10 2026 +0800

    libc/machine/arm/armv7-a: add 6 new arch string functions.
    
    Add ARM-state implementations of memcmp, strchr, strchrnul, strncmp,
    strnlen and strrchr. They scan a word at a time instead of a byte at
    a time, so long strings and buffers take fewer loop iterations than the
    generic C versions.
    
    Each function has its own Kconfig option that selects the matching
    LIBC_ARCH_* symbol, and ARMV7A_STRING_FUNCTION enables all of them.
    
    Signed-off-by: ganjing <[email protected]>
---
 libs/libc/machine/arm/armv7-a/CMakeLists.txt   |  24 ++++
 libs/libc/machine/arm/armv7-a/Kconfig          |  55 +++++++++
 libs/libc/machine/arm/armv7-a/Make.defs        |  24 ++++
 libs/libc/machine/arm/armv7-a/arch_memcmp.S    | 139 +++++++++++++++++++++++
 libs/libc/machine/arm/armv7-a/arch_strchr.S    | 123 ++++++++++++++++++++
 libs/libc/machine/arm/armv7-a/arch_strchrnul.S | 112 ++++++++++++++++++
 libs/libc/machine/arm/armv7-a/arch_strncmp.S   | 151 +++++++++++++++++++++++++
 libs/libc/machine/arm/armv7-a/arch_strnlen.S   | 101 +++++++++++++++++
 libs/libc/machine/arm/armv7-a/arch_strrchr.S   | 122 ++++++++++++++++++++
 9 files changed, 851 insertions(+)

diff --git a/libs/libc/machine/arm/armv7-a/CMakeLists.txt 
b/libs/libc/machine/arm/armv7-a/CMakeLists.txt
index c74e1fa1e01..aef9505a1b3 100644
--- a/libs/libc/machine/arm/armv7-a/CMakeLists.txt
+++ b/libs/libc/machine/arm/armv7-a/CMakeLists.txt
@@ -44,6 +44,30 @@ if(CONFIG_ARMV7A_STRLEN)
   list(APPEND SRCS arch_strlen.S)
 endif()
 
+if(CONFIG_ARMV7A_MEMCMP)
+  list(APPEND SRCS arch_memcmp.S)
+endif()
+
+if(CONFIG_ARMV7A_STRCHR)
+  list(APPEND SRCS arch_strchr.S)
+endif()
+
+if(CONFIG_ARMV7A_STRCHRNUL)
+  list(APPEND SRCS arch_strchrnul.S)
+endif()
+
+if(CONFIG_ARMV7A_STRNCMP)
+  list(APPEND SRCS arch_strncmp.S)
+endif()
+
+if(CONFIG_ARMV7A_STRNLEN)
+  list(APPEND SRCS arch_strnlen.S)
+endif()
+
+if(CONFIG_ARMV7A_STRRCHR)
+  list(APPEND SRCS arch_strrchr.S)
+endif()
+
 if(CONFIG_LIBC_ARCH_ELF)
   list(APPEND SRCS arch_elf.c)
 endif()
diff --git a/libs/libc/machine/arm/armv7-a/Kconfig 
b/libs/libc/machine/arm/armv7-a/Kconfig
index 42200d31326..48b90063ecc 100644
--- a/libs/libc/machine/arm/armv7-a/Kconfig
+++ b/libs/libc/machine/arm/armv7-a/Kconfig
@@ -29,11 +29,17 @@ config ARMV7A_STRING_FUNCTION
        default n
        depends on ARCH_TOOLCHAIN_GNU
        select ARMV7A_MEMCHR
+       select ARMV7A_MEMCMP
        select ARMV7A_MEMCPY
        select ARMV7A_MEMMOVE
        select ARMV7A_MEMSET
+       select ARMV7A_STRCHR
+       select ARMV7A_STRCHRNUL
        select ARMV7A_STRCMP
        select ARMV7A_STRLEN
+       select ARMV7A_STRNCMP
+       select ARMV7A_STRNLEN
+       select ARMV7A_STRRCHR
 
 config ARMV7A_MEMCHR
        bool "Enable optimized memchr() for ARMv7-A"
@@ -81,3 +87,52 @@ config ARMV7A_STRLEN
        depends on ARCH_TOOLCHAIN_GNU
        ---help---
                Enable optimized ARMv7-A specific strlen() library function
+
+config ARMV7A_MEMCMP
+       bool "Enable optimized memcmp() for ARMv7-A"
+       default n
+       select LIBC_ARCH_MEMCMP
+       depends on ARCH_TOOLCHAIN_GNU
+       ---help---
+               Enable optimized ARMv7-A specific memcmp() library function
+
+config ARMV7A_STRCHR
+       bool "Enable optimized strchr() for ARMv7-A"
+       default n
+       select LIBC_ARCH_STRCHR
+       depends on ARCH_TOOLCHAIN_GNU
+       ---help---
+               Enable optimized ARMv7-A specific strchr() library function
+
+config ARMV7A_STRCHRNUL
+       bool "Enable optimized strchrnul() for ARMv7-A"
+       default n
+       select LIBC_ARCH_STRCHRNUL
+       depends on ARCH_TOOLCHAIN_GNU
+       ---help---
+               Enable optimized ARMv7-A specific strchrnul() library function
+
+config ARMV7A_STRNCMP
+       bool "Enable optimized strncmp() for ARMv7-A"
+       default n
+       select LIBC_ARCH_STRNCMP
+       depends on ARCH_TOOLCHAIN_GNU
+       ---help---
+               Enable optimized ARMv7-A specific strncmp() library function
+
+config ARMV7A_STRNLEN
+       bool "Enable optimized strnlen() for ARMv7-A"
+       default n
+       select LIBC_ARCH_STRNLEN
+       depends on ARCH_TOOLCHAIN_GNU
+       ---help---
+               Enable optimized ARMv7-A specific strnlen() library function
+
+config ARMV7A_STRRCHR
+       bool "Enable optimized strrchr() for ARMv7-A"
+       default n
+       select LIBC_ARCH_STRRCHR
+       depends on ARCH_TOOLCHAIN_GNU
+       ---help---
+               Enable optimized ARMv7-A specific strrchr() library function
+
diff --git a/libs/libc/machine/arm/armv7-a/Make.defs 
b/libs/libc/machine/arm/armv7-a/Make.defs
index 6d7ea5a9a35..e23bae79706 100644
--- a/libs/libc/machine/arm/armv7-a/Make.defs
+++ b/libs/libc/machine/arm/armv7-a/Make.defs
@@ -44,6 +44,30 @@ ifeq ($(CONFIG_ARMV7A_STRLEN),y)
 ASRCS += arch_strlen.S
 endif
 
+ifeq ($(CONFIG_ARMV7A_MEMCMP),y)
+ASRCS += arch_memcmp.S
+endif
+
+ifeq ($(CONFIG_ARMV7A_STRCHR),y)
+ASRCS += arch_strchr.S
+endif
+
+ifeq ($(CONFIG_ARMV7A_STRCHRNUL),y)
+ASRCS += arch_strchrnul.S
+endif
+
+ifeq ($(CONFIG_ARMV7A_STRNCMP),y)
+ASRCS += arch_strncmp.S
+endif
+
+ifeq ($(CONFIG_ARMV7A_STRNLEN),y)
+ASRCS += arch_strnlen.S
+endif
+
+ifeq ($(CONFIG_ARMV7A_STRRCHR),y)
+ASRCS += arch_strrchr.S
+endif
+
 ifeq ($(CONFIG_LIBC_ARCH_ELF),y)
 CSRCS += arch_elf.c
 endif
diff --git a/libs/libc/machine/arm/armv7-a/arch_memcmp.S 
b/libs/libc/machine/arm/armv7-a/arch_memcmp.S
new file mode 100644
index 00000000000..990481fae1f
--- /dev/null
+++ b/libs/libc/machine/arm/armv7-a/arch_memcmp.S
@@ -0,0 +1,139 @@
+/****************************************************************************
+ * libs/libc/machine/arm/armv7-a/arch_memcmp.S
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ * SPDX-FileCopyrightText: 2018 Linaro Limited, 2017 ARM Ltd
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ *    * Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *    * Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ *    * Neither the name of Linaro nor the names of its
+ *      contributors may be used to endorse or promote products derived from
+ *      this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
+ * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ ****************************************************************************/
+
+#include "libc.h"
+
+#ifdef LIBC_BUILD_MEMCMP
+
+/* Assumptions:
+ *
+ * ARMv7-A / ARMv7-R (ARM state), unaligned word accesses supported.
+ */
+
+#define src1           r0
+#define src2           r1
+#define limit          r2
+#define result         r0
+#define data1          r3
+#define data2          r4
+#define tmp1           r12
+
+       .syntax unified
+       .text
+       .align 2
+       .global ARCH_LIBCFUN(memcmp)
+       .arm
+       .type ARCH_LIBCFUN(memcmp), %function
+
+ARCH_LIBCFUN(memcmp):
+       .cfi_sections .debug_frame
+       .cfi_startproc
+       push    {r4, lr}
+       .cfi_def_cfa_offset 8
+       .cfi_offset lr, -4
+       .cfi_offset r4, -8
+
+       cmp     limit, #0
+       beq     .Lret0
+
+       /* Align src1 to a 4-byte boundary, comparing the misaligned head
+          byte-by-byte.  src2 may stay unaligned.  */
+       ands    tmp1, src1, #3
+       beq     .Laligned
+
+.Lhead_loop:
+       ldrb    data1, [src1], #1
+       ldrb    data2, [src2], #1
+       cmp     data1, data2
+       bne     .Lret_diff_byte
+       subs    limit, limit, #1
+       beq     .Lret0
+       ands    tmp1, src1, #3
+       bne     .Lhead_loop
+
+.Laligned:
+.Lword_loop:
+       cmp     limit, #4
+       blo     .Lbyte_tail
+       ldr     data1, [src1], #4
+       ldr     data2, [src2], #4
+       subs    limit, limit, #4
+       cmp     data1, data2
+       bne     .Ldiff_word
+       b       .Lword_loop
+
+.Lbyte_tail:
+       cmp     limit, #0
+       beq     .Lret0
+.Lbyte_loop:
+       ldrb    data1, [src1], #1
+       ldrb    data2, [src2], #1
+       cmp     data1, data2
+       bne     .Lret_diff_byte
+       subs    limit, limit, #1
+       bne     .Lbyte_loop
+       b       .Lret0
+
+       .align 2
+.Ldiff_word:
+#ifndef __ARM_BIG_ENDIAN
+       rev     data1, data1
+       rev     data2, data2
+#endif
+       cmp     data1, data2
+       blo     .Lret_minus1
+       b       .Lret_plus1
+
+.Lret_diff_byte:
+       cmp     data1, data2
+       blo     .Lret_minus1
+       b       .Lret_plus1
+
+.Lret_minus1:
+       mvn     result, #0              /* -1 */
+       pop     {r4, pc}
+
+.Lret_plus1:
+       mov     result, #1
+       pop     {r4, pc}
+
+.Lret0:
+       mov     result, #0
+       pop     {r4, pc}
+
+       .cfi_endproc
+       .size ARCH_LIBCFUN(memcmp), . - ARCH_LIBCFUN(memcmp)
+
+#endif
diff --git a/libs/libc/machine/arm/armv7-a/arch_strchr.S 
b/libs/libc/machine/arm/armv7-a/arch_strchr.S
new file mode 100644
index 00000000000..ba7e5e45e7b
--- /dev/null
+++ b/libs/libc/machine/arm/armv7-a/arch_strchr.S
@@ -0,0 +1,123 @@
+/****************************************************************************
+ * libs/libc/machine/arm/armv7-a/arch_strchr.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_STRCHR
+
+/* Assumptions:
+ *
+ * ARMv7-A / ARMv7-R (ARM state), unaligned word accesses supported.
+ *
+ * Word-at-a-time scan: the hot loop tests one 4-byte word per iteration
+ * for both a NUL terminator and the search byte using the
+ * 0x01010101/0x80808080 trick.  On a hit the word is re-scanned
+ * byte-by-byte to locate the exact position.
+ */
+
+#define src            r0
+#define ch             r1
+#define result         r0
+#define data           r2
+#define repc           r3
+#define syndrome       r12
+#define tmp            r4
+#define const_m1       r5              /* 0x01010101 */
+#define const_h                r6              /* 0x80808080 */
+
+       .syntax unified
+       .text
+       .align 2
+       .global ARCH_LIBCFUN(strchr)
+       .arm
+       .type ARCH_LIBCFUN(strchr), %function
+
+ARCH_LIBCFUN(strchr):
+       push    {r4, r5, r6, lr}
+       and     ch, ch, #0xff
+
+       movw    const_m1, #:lower16:0x01010101
+       movt    const_m1, #:upper16:0x01010101
+       movw    const_h, #:lower16:0x80808080
+       movt    const_h, #:upper16:0x80808080
+       mul     repc, ch, const_m1      /* ch replicated to all 4 bytes */
+
+       /* Byte-scan the misaligned head until src is word-aligned.  */
+       ands    tmp, src, #3
+       beq     .Lword_loop
+.Lhead:
+       ldrb    data, [src], #1
+       cmp     data, ch
+       beq     .Lret_found
+       cmp     data, #0
+       beq     .Lret_null
+       ands    tmp, src, #3
+       bne     .Lhead
+
+       .p2align 2
+.Lword_loop:
+       ldr     data, [src], #4
+       /* NUL test.  */
+       sub     syndrome, data, const_m1
+       bic     syndrome, syndrome, data
+       tst     syndrome, const_h
+       bne     .Lterm
+       /* Search-byte test (XOR then zero-byte test).  */
+       eor     tmp, data, repc
+       sub     syndrome, tmp, const_m1
+       bic     syndrome, syndrome, tmp
+       tst     syndrome, const_h
+       bne     .Lfound
+       b       .Lword_loop
+
+.Lfound:
+       /* Match present, no NUL in this word: rescan bytes to locate it.  */
+       sub     src, src, #4
+.Lfound_scan:
+       ldrb    data, [src], #1
+       cmp     data, ch
+       beq     .Lret_found
+       cmp     data, #0
+       bne     .Lfound_scan
+       b       .Lret_null
+
+.Lterm:
+       /* NUL present: rescan bytes to see if the match precedes the NUL.  */
+       sub     src, src, #4
+.Lterm_scan:
+       ldrb    data, [src], #1
+       cmp     data, ch
+       beq     .Lret_found
+       cmp     data, #0
+       bne     .Lterm_scan
+
+.Lret_null:
+       mov     result, #0
+       pop     {r4, r5, r6, pc}
+
+.Lret_found:
+       sub     result, src, #1         /* src was post-incremented */
+       pop     {r4, r5, r6, pc}
+
+       .size ARCH_LIBCFUN(strchr), . - ARCH_LIBCFUN(strchr)
+
+#endif
diff --git a/libs/libc/machine/arm/armv7-a/arch_strchrnul.S 
b/libs/libc/machine/arm/armv7-a/arch_strchrnul.S
new file mode 100644
index 00000000000..6515691ec49
--- /dev/null
+++ b/libs/libc/machine/arm/armv7-a/arch_strchrnul.S
@@ -0,0 +1,112 @@
+/****************************************************************************
+ * libs/libc/machine/arm/armv7-a/arch_strchrnul.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_STRCHRNUL
+
+/* Assumptions:
+ *
+ * ARMv7-A / ARMv7-R (ARM state), unaligned word accesses supported.
+ *
+ * Like arch_strchr.S but returns a pointer to the NUL terminator when the
+ * byte is not found (never returns NULL).
+ */
+
+#define src            r0
+#define ch             r1
+#define result         r0
+#define data           r2
+#define repc           r3
+#define syndrome       r12
+#define tmp            r4
+#define const_m1       r5
+#define const_h                r6
+
+       .syntax unified
+       .text
+       .align 2
+       .global ARCH_LIBCFUN(strchrnul)
+       .arm
+       .type ARCH_LIBCFUN(strchrnul), %function
+
+ARCH_LIBCFUN(strchrnul):
+       push    {r4, r5, r6, lr}
+       and     ch, ch, #0xff
+
+       movw    const_m1, #:lower16:0x01010101
+       movt    const_m1, #:upper16:0x01010101
+       movw    const_h, #:lower16:0x80808080
+       movt    const_h, #:upper16:0x80808080
+       mul     repc, ch, const_m1
+
+       ands    tmp, src, #3
+       beq     .Lword_loop
+.Lhead:
+       ldrb    data, [src], #1
+       cmp     data, ch
+       beq     .Lret
+       cmp     data, #0
+       beq     .Lret
+       ands    tmp, src, #3
+       bne     .Lhead
+
+       .p2align 2
+.Lword_loop:
+       ldr     data, [src], #4
+       sub     syndrome, data, const_m1
+       bic     syndrome, syndrome, data
+       tst     syndrome, const_h
+       bne     .Lterm
+       eor     tmp, data, repc
+       sub     syndrome, tmp, const_m1
+       bic     syndrome, syndrome, tmp
+       tst     syndrome, const_h
+       bne     .Lfound
+       b       .Lword_loop
+
+.Lfound:
+       sub     src, src, #4
+.Lfound_scan:
+       ldrb    data, [src], #1
+       cmp     data, ch
+       beq     .Lret
+       cmp     data, #0
+       bne     .Lfound_scan
+       b       .Lret
+
+.Lterm:
+       sub     src, src, #4
+.Lterm_scan:
+       ldrb    data, [src], #1
+       cmp     data, ch
+       beq     .Lret
+       cmp     data, #0
+       bne     .Lterm_scan
+
+.Lret:
+       sub     result, src, #1
+       pop     {r4, r5, r6, pc}
+
+       .size ARCH_LIBCFUN(strchrnul), . - ARCH_LIBCFUN(strchrnul)
+
+#endif
diff --git a/libs/libc/machine/arm/armv7-a/arch_strncmp.S 
b/libs/libc/machine/arm/armv7-a/arch_strncmp.S
new file mode 100644
index 00000000000..02d727c8d60
--- /dev/null
+++ b/libs/libc/machine/arm/armv7-a/arch_strncmp.S
@@ -0,0 +1,151 @@
+/****************************************************************************
+ * libs/libc/machine/arm/armv7-a/arch_strncmp.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_STRNCMP
+
+/* Assumptions:
+ *
+ * ARMv7-A / ARMv7-R (ARM state), unaligned word accesses supported.
+ *
+ * Word-at-a-time comparison: 4 bytes per loop iteration using the
+ * 0x01010101/0x80808080 NUL-detection trick, with a byte loop for the
+ * misaligned head and the sub-word tail.
+ */
+
+#define src1           r0
+#define src2           r1
+#define limit          r2
+#define result         r0
+#define data1          r3
+#define data2          r4
+#define syndrome       r5
+#define tmp1           r12
+#define const_m1       r6              /* 0x01010101 */
+#define const_h                r7              /* 0x80808080 */
+
+       .syntax unified
+       .text
+       .align 2
+       .global ARCH_LIBCFUN(strncmp)
+       .arm
+       .type ARCH_LIBCFUN(strncmp), %function
+
+ARCH_LIBCFUN(strncmp):
+       push    {r4, r5, r6, r7, lr}
+
+       cmp     limit, #0
+       beq     .Lret0
+
+       movw    const_m1, #:lower16:0x01010101
+       movt    const_m1, #:upper16:0x01010101
+       movw    const_h, #:lower16:0x80808080
+       movt    const_h, #:upper16:0x80808080
+
+       /* Misaligned head: compare byte-by-byte until src1 is aligned or
+          the limit is exhausted.  */
+       ands    tmp1, src1, #3
+       beq     .Lword_loop
+
+.Lhead_loop:
+       ldrb    data1, [src1], #1
+       ldrb    data2, [src2], #1
+       cmp     data1, data2
+       bne     .Ldiff_byte
+       cmp     data1, #0
+       beq     .Lret0
+       subs    limit, limit, #1
+       beq     .Lret0
+       ands    tmp1, src1, #3
+       bne     .Lhead_loop
+
+       .align 2
+.Lword_loop:
+       cmp     limit, #4
+       blo     .Lbyte_tail
+       ldr     data1, [src1], #4
+       ldr     data2, [src2], #4
+       /* NUL check first: a NUL may end the string before any differing
+          byte within this word.  */
+       sub     syndrome, data1, const_m1
+       bic     syndrome, syndrome, data1
+       tst     syndrome, const_h
+       bne     .Lword_nul
+       /* No NUL: a difference here is a real difference.  */
+       cmp     data1, data2
+       bne     .Ldiff_word
+       subs    limit, limit, #4
+       bne     .Lword_loop
+       b       .Lret0          /* limit exhausted, equal */
+
+.Lword_nul:
+       /* NUL within this word: back up and finish byte-by-byte so that a
+          NUL preceding a difference correctly yields equality.  */
+       sub     src1, src1, #4
+       sub     src2, src2, #4
+
+.Lbyte_tail:
+       cmp     limit, #0
+       beq     .Lret0
+.Lbyte_loop:
+       ldrb    data1, [src1], #1
+       ldrb    data2, [src2], #1
+       cmp     data1, data2
+       bne     .Ldiff_byte
+       cmp     data1, #0
+       beq     .Lret0
+       subs    limit, limit, #1
+       bne     .Lbyte_loop
+
+.Lret0:
+       mov     result, #0
+       pop     {r4, r5, r6, r7, pc}
+
+       .align 2
+.Ldiff_word:
+       /* Find the first differing byte.  Reverse on little-endian so the
+          first byte dominates the compare.  */
+#ifndef __ARM_BIG_ENDIAN
+       rev     data1, data1
+       rev     data2, data2
+#endif
+       cmp     data1, data2
+       blo     .Lret_minus1
+       b       .Lret_plus1
+
+.Ldiff_byte:
+       cmp     data1, data2
+       blo     .Lret_minus1
+       b       .Lret_plus1
+
+.Lret_minus1:
+       mvn     result, #0
+       pop     {r4, r5, r6, r7, pc}
+
+.Lret_plus1:
+       mov     result, #1
+       pop     {r4, r5, r6, r7, pc}
+
+       .size ARCH_LIBCFUN(strncmp), . - ARCH_LIBCFUN(strncmp)
+
+#endif
diff --git a/libs/libc/machine/arm/armv7-a/arch_strnlen.S 
b/libs/libc/machine/arm/armv7-a/arch_strnlen.S
new file mode 100644
index 00000000000..4f79ba7e02e
--- /dev/null
+++ b/libs/libc/machine/arm/armv7-a/arch_strnlen.S
@@ -0,0 +1,101 @@
+/****************************************************************************
+ * libs/libc/machine/arm/armv7-a/arch_strnlen.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_STRNLEN
+
+/* Assumptions:
+ *
+ * ARMv7-A / ARMv7-R (ARM state).
+ */
+
+       .syntax unified
+       .text
+       .align 2
+       .global ARCH_LIBCFUN(strnlen)
+       .arm
+       .type ARCH_LIBCFUN(strnlen), %function
+
+ARCH_LIBCFUN(strnlen):
+       push    {r4, r5, r6, lr}
+       movw    r4, #0x0101
+       movt    r4, #0x0101             /* const_m1 = 0x01010101 */
+       movw    r5, #0x8080
+       movt    r5, #0x8080             /* const_h  = 0x80808080 */
+       mov     r6, r0                  /* ptr = src */
+       cmp     r1, #0
+       beq     .Lzero
+       /* Byte-scan the misaligned head until ptr is word-aligned.  */
+       ands    r3, r6, #3
+       beq     .Lcheck4
+.Lhead:
+       ldrb    r2, [r6]
+       cmp     r2, #0
+       beq     .Ldone          /* NUL found, ptr at NUL */
+       add     r6, #1
+       subs    r1, #1
+       beq     .Ldone                  /* maxlen exhausted */
+       ands    r3, r6, #3
+       bne     .Lhead
+.Lcheck4:
+       cmp     r1, #4
+       blo     .Lbyte_tail
+.Lwl:
+       ldr     r2, [r6]                /* aligned word */
+       sub     r3, r2, r4
+       bic     r3, r3, r2
+       tst     r3, r5
+       bne     .Lnulword               /* NUL in this word */
+       add     r6, #4
+       subs    r1, #4
+       cmp     r1, #4
+       bhs     .Lwl
+.Lbyte_tail:
+       cmp     r1, #0
+       beq     .Ldone
+.Lbloop:
+       ldrb    r2, [r6]
+       cmp     r2, #0
+       beq     .Ldone
+       add     r6, #1
+       subs    r1, #1
+       bne     .Lbloop
+       b       .Ldone
+.Lnulword:
+       /* NUL in this word (ptr at word start): rescan bytes to locate it.  */
+.Lnul_scan:
+       ldrb    r2, [r6]
+       cmp     r2, #0
+       beq     .Ldone
+       add     r6, #1
+       b       .Lnul_scan
+.Ldone:
+       sub     r0, r6, r0              /* len = ptr - src */
+       pop     {r4, r5, r6, pc}
+.Lzero:
+       mov     r0, #0
+       pop     {r4, r5, r6, pc}
+
+       .size ARCH_LIBCFUN(strnlen), . - ARCH_LIBCFUN(strnlen)
+
+#endif
diff --git a/libs/libc/machine/arm/armv7-a/arch_strrchr.S 
b/libs/libc/machine/arm/armv7-a/arch_strrchr.S
new file mode 100644
index 00000000000..859e586d2e3
--- /dev/null
+++ b/libs/libc/machine/arm/armv7-a/arch_strrchr.S
@@ -0,0 +1,122 @@
+/****************************************************************************
+ * libs/libc/machine/arm/armv7-a/arch_strrchr.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_STRRCHR
+
+/* Assumptions:
+ *
+ * ARMv7-A / ARMv7-R (ARM state), unaligned word accesses supported.
+ *
+ * Word-at-a-time scan: the hot loop tests one word per iteration for a
+ * NUL terminator and for the search byte.  The position of the last
+ * match seen is tracked; on reaching NUL that position (or NULL) is
+ * returned.
+ */
+
+#define src            r0
+#define ch             r1
+#define result         r0
+#define data           r2
+#define repc           r3
+#define last           r4              /* last match pointer (or 0) */
+#define syndrome       r12
+#define tmp            r5
+#define const_m1       r6
+#define const_h                r7
+
+       .syntax unified
+       .text
+       .align 2
+       .global ARCH_LIBCFUN(strrchr)
+       .arm
+       .type ARCH_LIBCFUN(strrchr), %function
+
+ARCH_LIBCFUN(strrchr):
+       push    {r4, r5, r6, r7, lr}
+       and     ch, ch, #0xff
+       mov     last, #0
+
+       movw    const_m1, #:lower16:0x01010101
+       movt    const_m1, #:upper16:0x01010101
+       movw    const_h, #:lower16:0x80808080
+       movt    const_h, #:upper16:0x80808080
+       mul     repc, ch, const_m1
+
+       /* Byte-scan the misaligned head.  */
+       ands    tmp, src, #3
+       beq     .Lword_loop
+.Lhead:
+       ldrb    data, [src], #1
+       cmp     data, ch
+       subeq   last, src, #1           /* record match position */
+       cmp     data, #0
+       beq     .Lret
+       ands    tmp, src, #3
+       bne     .Lhead
+
+       .p2align 2
+.Lword_loop:
+       ldr     data, [src], #4
+       /* NUL test.  */
+       sub     syndrome, data, const_m1
+       bic     syndrome, syndrome, data
+       tst     syndrome, const_h
+       bne     .Lterm
+       /* Search-byte test.  */
+       eor     tmp, data, repc
+       sub     syndrome, tmp, const_m1
+       bic     syndrome, syndrome, tmp
+       tst     syndrome, const_h
+       bne     .Lmatch_word
+       b       .Lword_loop
+
+.Lmatch_word:
+       /* A match exists in this word (no NUL): byte-scan it to update the
+          last-match pointer.  src points one past the word.  */
+       sub     tmp, src, #4
+.Lmatch_scan:
+       ldrb    data, [tmp], #1
+       cmp     data, ch
+       subeq   last, tmp, #1
+       cmp     tmp, src
+       bne     .Lmatch_scan
+       b       .Lword_loop
+
+.Lterm:
+       /* NUL in this word: byte-scan it tracking the last match until NUL.  */
+       sub     src, src, #4
+.Lterm_scan:
+       ldrb    data, [src], #1
+       cmp     data, ch
+       subeq   last, src, #1
+       cmp     data, #0
+       bne     .Lterm_scan
+
+.Lret:
+       mov     result, last
+       pop     {r4, r5, r6, r7, pc}
+
+       .size ARCH_LIBCFUN(strrchr), . - ARCH_LIBCFUN(strrchr)
+
+#endif

Reply via email to