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 c71735d63187b9406701b27f48d2ef1e77dbab58
Author: ganjing <[email protected]>
AuthorDate: Fri Nov 20 19:04:22 2026 +0800

    libc/machine/arm/armv8-m: add 6 new arch string functions.
    
    Add Thumb-2 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 ARMV8M_STRING_FUNCTION enables all of them.
    
    Signed-off-by: ganjing <[email protected]>
---
 libs/libc/machine/arm/armv8-m/CMakeLists.txt   |  24 ++++
 libs/libc/machine/arm/armv8-m/Kconfig          |  54 ++++++++
 libs/libc/machine/arm/armv8-m/Make.defs        |  24 ++++
 libs/libc/machine/arm/armv8-m/arch_memcmp.S    | 163 +++++++++++++++++++++++
 libs/libc/machine/arm/armv8-m/arch_strchr.S    | 148 +++++++++++++++++++++
 libs/libc/machine/arm/armv8-m/arch_strchrnul.S | 131 +++++++++++++++++++
 libs/libc/machine/arm/armv8-m/arch_strncmp.S   | 174 +++++++++++++++++++++++++
 libs/libc/machine/arm/armv8-m/arch_strnlen.S   | 136 +++++++++++++++++++
 libs/libc/machine/arm/armv8-m/arch_strrchr.S   | 149 +++++++++++++++++++++
 9 files changed, 1003 insertions(+)

diff --git a/libs/libc/machine/arm/armv8-m/CMakeLists.txt 
b/libs/libc/machine/arm/armv8-m/CMakeLists.txt
index a2ec0b66fd9..71df888aa8d 100644
--- a/libs/libc/machine/arm/armv8-m/CMakeLists.txt
+++ b/libs/libc/machine/arm/armv8-m/CMakeLists.txt
@@ -22,6 +22,30 @@
 
 set(SRCS)
 
+if(CONFIG_ARMV8M_MEMCMP)
+  list(APPEND SRCS arch_memcmp.S)
+endif()
+
+if(CONFIG_ARMV8M_STRCHR)
+  list(APPEND SRCS arch_strchr.S)
+endif()
+
+if(CONFIG_ARMV8M_STRCHRNUL)
+  list(APPEND SRCS arch_strchrnul.S)
+endif()
+
+if(CONFIG_ARMV8M_STRNCMP)
+  list(APPEND SRCS arch_strncmp.S)
+endif()
+
+if(CONFIG_ARMV8M_STRNLEN)
+  list(APPEND SRCS arch_strnlen.S)
+endif()
+
+if(CONFIG_ARMV8M_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/armv8-m/Kconfig 
b/libs/libc/machine/arm/armv8-m/Kconfig
index 68fb40881d3..6be8cc1a72b 100644
--- a/libs/libc/machine/arm/armv8-m/Kconfig
+++ b/libs/libc/machine/arm/armv8-m/Kconfig
@@ -31,12 +31,18 @@ config ARMV8M_STRING_FUNCTION
        default n
        depends on ARCH_TOOLCHAIN_GNU
        select ARMV8M_MEMCHR
+       select ARMV8M_MEMCMP
        select ARMV8M_MEMCPY
        select ARMV8M_MEMSET
        select ARMV8M_MEMMOVE
+       select ARMV8M_STRCHR
+       select ARMV8M_STRCHRNUL
        select ARMV8M_STRCMP
        select ARMV8M_STRCPY
        select ARMV8M_STRLEN
+       select ARMV8M_STRNCMP
+       select ARMV8M_STRNLEN
+       select ARMV8M_STRRCHR
 
 config ARMV8M_MEMCHR
        bool "Enable optimized memchr() for ARMv8-M"
@@ -94,4 +100,52 @@ config ARMV8M_STRLEN
        ---help---
                Enable optimized ARMv8-M specific strlen() library function
 
+config ARMV8M_MEMCMP
+       bool "Enable optimized memcmp() for ARMv8-M"
+       default n
+       select LIBC_ARCH_MEMCMP
+       depends on ARCH_TOOLCHAIN_GNU
+       ---help---
+               Enable optimized ARMv8-M specific memcmp() library function
+
+config ARMV8M_STRCHR
+       bool "Enable optimized strchr() for ARMv8-M"
+       default n
+       select LIBC_ARCH_STRCHR
+       depends on ARCH_TOOLCHAIN_GNU
+       ---help---
+               Enable optimized ARMv8-M specific strchr() library function
+
+config ARMV8M_STRCHRNUL
+       bool "Enable optimized strchrnul() for ARMv8-M"
+       default n
+       select LIBC_ARCH_STRCHRNUL
+       depends on ARCH_TOOLCHAIN_GNU
+       ---help---
+               Enable optimized ARMv8-M specific strchrnul() library function
+
+config ARMV8M_STRNCMP
+       bool "Enable optimized strncmp() for ARMv8-M"
+       default n
+       select LIBC_ARCH_STRNCMP
+       depends on ARCH_TOOLCHAIN_GNU
+       ---help---
+               Enable optimized ARMv8-M specific strncmp() library function
+
+config ARMV8M_STRNLEN
+       bool "Enable optimized strnlen() for ARMv8-M"
+       default n
+       select LIBC_ARCH_STRNLEN
+       depends on ARCH_TOOLCHAIN_GNU
+       ---help---
+               Enable optimized ARMv8-M specific strnlen() library function
+
+config ARMV8M_STRRCHR
+       bool "Enable optimized strrchr() for ARMv8-M"
+       default n
+       select LIBC_ARCH_STRRCHR
+       depends on ARCH_TOOLCHAIN_GNU
+       ---help---
+               Enable optimized ARMv8-M specific strrchr() library function
+
 endif
diff --git a/libs/libc/machine/arm/armv8-m/Make.defs 
b/libs/libc/machine/arm/armv8-m/Make.defs
index c49aff3726a..2521ec24328 100644
--- a/libs/libc/machine/arm/armv8-m/Make.defs
+++ b/libs/libc/machine/arm/armv8-m/Make.defs
@@ -20,6 +20,30 @@
 #
 ############################################################################
 
+ifeq ($(CONFIG_ARMV8M_MEMCMP),y)
+ASRCS += arch_memcmp.S
+endif
+
+ifeq ($(CONFIG_ARMV8M_STRCHR),y)
+ASRCS += arch_strchr.S
+endif
+
+ifeq ($(CONFIG_ARMV8M_STRCHRNUL),y)
+ASRCS += arch_strchrnul.S
+endif
+
+ifeq ($(CONFIG_ARMV8M_STRNCMP),y)
+ASRCS += arch_strncmp.S
+endif
+
+ifeq ($(CONFIG_ARMV8M_STRNLEN),y)
+ASRCS += arch_strnlen.S
+endif
+
+ifeq ($(CONFIG_ARMV8M_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/armv8-m/arch_memcmp.S 
b/libs/libc/machine/arm/armv8-m/arch_memcmp.S
new file mode 100644
index 00000000000..9991b1881e8
--- /dev/null
+++ b/libs/libc/machine/arm/armv8-m/arch_memcmp.S
@@ -0,0 +1,163 @@
+/****************************************************************************
+ * libs/libc/machine/arm/armv8-m/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-M / ARMv8-M (Thumb-2), unaligned word accesses supported.
+ */
+
+#ifndef __ARM_FEATURE_BTI_DEFAULT
+#  define __ARM_FEATURE_BTI_DEFAULT 0
+#endif
+
+#ifndef __ARM_FEATURE_PAC_DEFAULT
+#  define __ARM_FEATURE_PAC_DEFAULT 0
+#endif
+
+       .macro def_fn f p2align=0
+       .text
+       .p2align \p2align
+       .global \f
+       .type \f, %function
+\f:
+       .endm
+
+/* Parameters and result.  */
+#define src1           r0
+#define src2           r1
+#define limit          r2
+#define result         r0
+
+/* Internal variables.  */
+#define data1          r3
+#define data2          r4
+#define tmp1           r12
+
+       .thumb
+       .syntax unified
+def_fn ARCH_LIBCFUN(memcmp)
+       .cfi_sections .debug_frame
+       .cfi_startproc
+#if __ARM_FEATURE_PAC_DEFAULT
+#  if __ARM_FEATURE_BTI_DEFAULT
+       pacbti  ip, lr, sp
+#  else
+       pac     ip, lr, sp
+#  endif
+#endif
+       push    {r4, lr}
+       .cfi_def_cfa_offset 8
+       .cfi_offset lr, -4
+       .cfi_offset r4, -8
+
+       cbz     limit, .Lret0
+
+       /* Align src1 to a 4-byte boundary, comparing the misaligned head
+          byte-by-byte.  src2 may stay unaligned (unaligned ldr is OK).  */
+       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:
+       /* Word loop: 4 bytes per iteration while at least 4 remain.  */
+.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
+
+       /* Remaining 0..3 bytes.  */
+.Lbyte_tail:
+       cbz     limit, .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
+
+       .p2align 2
+.Ldiff_word:
+       /* Two differing words (all previous words matched).  Reverse on
+          little-endian so the first byte (LSB) dominates the compare.  */
+#ifndef __ARM_BIG_ENDIAN
+       rev     data1, data1
+       rev     data2, data2
+#endif
+       cmp     data1, data2
+       blo     .Lret_minus1
+       b       .Lret_plus1
+
+.Lret_diff_byte:
+       /* data1/data2 hold the differing bytes (zero-extended).  */
+       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/armv8-m/arch_strchr.S 
b/libs/libc/machine/arm/armv8-m/arch_strchr.S
new file mode 100644
index 00000000000..961d80d59cd
--- /dev/null
+++ b/libs/libc/machine/arm/armv8-m/arch_strchr.S
@@ -0,0 +1,148 @@
+/****************************************************************************
+ * libs/libc/machine/arm/armv8-m/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-M / ARMv8-M (Thumb-2).
+ */
+
+#ifndef __ARM_FEATURE_BTI_DEFAULT
+#  define __ARM_FEATURE_BTI_DEFAULT 0
+#endif
+
+#ifndef __ARM_FEATURE_PAC_DEFAULT
+#  define __ARM_FEATURE_PAC_DEFAULT 0
+#endif
+
+       .macro def_fn f p2align=0
+       .text
+       .p2align \p2align
+       .global \f
+       .type \f, %function
+\f:
+       .endm
+
+#define src            r0
+#define ch             r1
+#define data           r2
+#define tmp            r3
+#define const_m1       r4
+#define const_h                r5
+#define repc           r6
+#define syndrome       r7
+
+       .thumb
+       .syntax unified
+def_fn ARCH_LIBCFUN(strchr)
+       .cfi_sections .debug_frame
+       .cfi_startproc
+#if __ARM_FEATURE_PAC_DEFAULT
+#  if __ARM_FEATURE_BTI_DEFAULT
+       pacbti  ip, lr, sp
+#  else
+       pac     ip, lr, sp
+#  endif
+#endif
+       push    {r4, r5, r6, r7, lr}
+       .cfi_def_cfa_offset 20
+       .cfi_offset lr, -4
+       .cfi_offset r4, -20
+       .cfi_offset r5, -16
+       .cfi_offset r6, -12
+       .cfi_offset r7, -8
+       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: syndrome = (data - const_m1) & ~data.  */
+       sub     syndrome, data, const_m1
+       bic     syndrome, syndrome, data
+       tst     syndrome, const_h
+       bne     .Lterm
+       /* Search-byte test: zero-byte test on data ^ repc.  */
+       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     src, #0
+#if __ARM_FEATURE_PAC_DEFAULT
+       aut     ip, lr, sp
+#endif
+       pop     {r4, r5, r6, r7, pc}
+.Lret_found:
+       sub     src, src, #1            /* src was post-incremented */
+#if __ARM_FEATURE_PAC_DEFAULT
+       aut     ip, lr, sp
+#endif
+       pop     {r4, r5, r6, r7, pc}
+
+       .cfi_endproc
+       .size ARCH_LIBCFUN(strchr), . - ARCH_LIBCFUN(strchr)
+
+#endif
diff --git a/libs/libc/machine/arm/armv8-m/arch_strchrnul.S 
b/libs/libc/machine/arm/armv8-m/arch_strchrnul.S
new file mode 100644
index 00000000000..02ea72f2bfd
--- /dev/null
+++ b/libs/libc/machine/arm/armv8-m/arch_strchrnul.S
@@ -0,0 +1,131 @@
+/****************************************************************************
+ * libs/libc/machine/arm/armv8-m/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-M / ARMv8-M (Thumb-2).
+ */
+
+#ifndef __ARM_FEATURE_BTI_DEFAULT
+#  define __ARM_FEATURE_BTI_DEFAULT 0
+#endif
+
+#ifndef __ARM_FEATURE_PAC_DEFAULT
+#  define __ARM_FEATURE_PAC_DEFAULT 0
+#endif
+
+       .macro def_fn f p2align=0
+       .text
+       .p2align \p2align
+       .global \f
+       .type \f, %function
+\f:
+       .endm
+
+#define src            r0
+#define ch             r1
+#define data           r2
+#define tmp            r3
+#define const_m1       r4
+#define const_h                r5
+#define repc           r6
+#define syndrome       r7
+
+       .thumb
+       .syntax unified
+def_fn ARCH_LIBCFUN(strchrnul)
+       .cfi_sections .debug_frame
+       .cfi_startproc
+#if __ARM_FEATURE_PAC_DEFAULT
+#  if __ARM_FEATURE_BTI_DEFAULT
+       pacbti  ip, lr, sp
+#  else
+       pac     ip, lr, sp
+#  endif
+#endif
+       push    {r4, r5, r6, r7, lr}
+       .cfi_def_cfa_offset 20
+       .cfi_offset lr, -4
+       .cfi_offset r4, -20
+       .cfi_offset r5, -16
+       .cfi_offset r6, -12
+       .cfi_offset r7, -8
+       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
+       cmp     data, #0
+       beq     .Lret
+       ands    tmp, src, #3
+       bne     .Lhead
+
+       .p2align 2
+.Lword_loop:
+       ldr     data, [src], #4
+       /* NUL test: syndrome = (data - const_m1) & ~data.  */
+       sub     syndrome, data, const_m1
+       bic     syndrome, syndrome, data
+       tst     syndrome, const_h
+       bne     .Lscan
+       /* Search-byte test: zero-byte test on data ^ repc.  */
+       eor     tmp, data, repc
+       sub     syndrome, tmp, const_m1
+       bic     syndrome, syndrome, tmp
+       tst     syndrome, const_h
+       bne     .Lscan
+       b       .Lword_loop
+
+.Lscan:
+       /* Rescan the word byte-by-byte to locate the match or NUL.  */
+       sub     src, src, #4
+.Lscan_loop:
+       ldrb    data, [src], #1
+       cmp     data, ch
+       beq     .Lret
+       cmp     data, #0
+       bne     .Lscan_loop
+
+.Lret:
+       sub     src, src, #1            /* src was post-incremented */
+#if __ARM_FEATURE_PAC_DEFAULT
+       aut     ip, lr, sp
+#endif
+       pop     {r4, r5, r6, r7, pc}
+
+       .cfi_endproc
+       .size ARCH_LIBCFUN(strchrnul), . - ARCH_LIBCFUN(strchrnul)
+
+#endif
diff --git a/libs/libc/machine/arm/armv8-m/arch_strncmp.S 
b/libs/libc/machine/arm/armv8-m/arch_strncmp.S
new file mode 100644
index 00000000000..7ddc65c842a
--- /dev/null
+++ b/libs/libc/machine/arm/armv8-m/arch_strncmp.S
@@ -0,0 +1,174 @@
+/****************************************************************************
+ * libs/libc/machine/arm/armv8-m/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-M / ARMv8-M (Thumb-2), 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.
+ */
+
+#ifndef __ARM_FEATURE_BTI_DEFAULT
+#  define __ARM_FEATURE_BTI_DEFAULT 0
+#endif
+
+#ifndef __ARM_FEATURE_PAC_DEFAULT
+#  define __ARM_FEATURE_PAC_DEFAULT 0
+#endif
+
+       .macro def_fn f p2align=0
+       .text
+       .p2align \p2align
+       .global \f
+       .type \f, %function
+\f:
+       .endm
+
+#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 */
+
+       .thumb
+       .syntax unified
+def_fn ARCH_LIBCFUN(strncmp)
+       .cfi_sections .debug_frame
+       .cfi_startproc
+#if __ARM_FEATURE_PAC_DEFAULT
+#  if __ARM_FEATURE_BTI_DEFAULT
+       pacbti  ip, lr, sp
+#  else
+       pac     ip, lr, sp
+#  endif
+#endif
+       push    {r4, r5, r6, r7, lr}
+       .cfi_def_cfa_offset 20
+       .cfi_offset lr, -4
+       .cfi_offset r7, -8
+       .cfi_offset r6, -12
+       .cfi_offset r5, -16
+       .cfi_offset r4, -20
+
+       cbz     limit, .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
+       cbz     data1, .Lret0
+       subs    limit, limit, #1
+       beq     .Lret0
+       ands    tmp1, src1, #3
+       bne     .Lhead_loop
+
+       .p2align 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:
+       cbz     limit, .Lret0
+.Lbyte_loop:
+       ldrb    data1, [src1], #1
+       ldrb    data2, [src2], #1
+       cmp     data1, data2
+       bne     .Ldiff_byte
+       cbz     data1, .Lret0
+       subs    limit, limit, #1
+       bne     .Lbyte_loop
+
+.Lret0:
+       mov     result, #0
+       pop     {r4, r5, r6, r7, pc}
+
+       .p2align 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}
+
+       .cfi_endproc
+       .size ARCH_LIBCFUN(strncmp), . - ARCH_LIBCFUN(strncmp)
+
+#endif
diff --git a/libs/libc/machine/arm/armv8-m/arch_strnlen.S 
b/libs/libc/machine/arm/armv8-m/arch_strnlen.S
new file mode 100644
index 00000000000..31b8dadffbb
--- /dev/null
+++ b/libs/libc/machine/arm/armv8-m/arch_strnlen.S
@@ -0,0 +1,136 @@
+/****************************************************************************
+ * libs/libc/machine/arm/armv8-m/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-M / ARMv8-M (Thumb-2).
+ */
+
+#ifndef __ARM_FEATURE_BTI_DEFAULT
+#  define __ARM_FEATURE_BTI_DEFAULT 0
+#endif
+
+#ifndef __ARM_FEATURE_PAC_DEFAULT
+#  define __ARM_FEATURE_PAC_DEFAULT 0
+#endif
+
+       .macro def_fn f p2align=0
+       .text
+       .p2align \p2align
+       .global \f
+       .type \f, %function
+\f:
+       .endm
+
+#define src            r0
+#define maxlen         r1
+#define data           r2
+#define tmp            r3
+#define const_m1       r4
+#define const_h                r5
+#define ptr            r6
+
+       .thumb
+       .syntax unified
+def_fn ARCH_LIBCFUN(strnlen)
+       .cfi_sections .debug_frame
+       .cfi_startproc
+#if __ARM_FEATURE_PAC_DEFAULT
+#  if __ARM_FEATURE_BTI_DEFAULT
+       pacbti  ip, lr, sp
+#  else
+       pac     ip, lr, sp
+#  endif
+#endif
+       push    {r4, r5, r6, lr}
+       .cfi_def_cfa_offset 16
+       .cfi_offset lr, -4
+       .cfi_offset r4, -16
+       .cfi_offset r5, -12
+       .cfi_offset r6, -8
+       movw    const_m1, #:lower16:0x01010101
+       movt    const_m1, #:upper16:0x01010101
+       movw    const_h, #:lower16:0x80808080
+       movt    const_h, #:upper16:0x80808080
+       mov     ptr, src                        /* ptr = src */
+       cbz     maxlen, .Lzero
+       /* Byte-scan the misaligned head until ptr is word-aligned.  */
+       ands    tmp, ptr, #3
+       beq     .Lcheck4
+.Lhead:
+       ldrb    data, [ptr]
+       cbz     data, .Ldone                    /* NUL found, ptr at NUL */
+       adds    ptr, #1
+       subs    maxlen, #1
+       beq     .Ldone                          /* maxlen exhausted */
+       ands    tmp, ptr, #3
+       bne     .Lhead
+.Lcheck4:
+       cmp     maxlen, #4
+       blo     .Lbyte_tail
+.Lwl:
+       ldr     data, [ptr]
+       sub     tmp, data, const_m1
+       bic     tmp, tmp, data
+       tst     tmp, const_h
+       bne     .Lnulword                       /* NUL in this word */
+       adds    ptr, #4
+       subs    maxlen, #4
+       cmp     maxlen, #4
+       bhs     .Lwl
+.Lbyte_tail:
+       cbz     maxlen, .Ldone
+.Lbloop:
+       ldrb    data, [ptr]
+       cbz     data, .Ldone
+       adds    ptr, #1
+       subs    maxlen, #1
+       bne     .Lbloop
+       b       .Ldone
+.Lnulword:
+       /* NUL in this word (ptr at word start): rescan bytes to locate it.  */
+.Lnul_scan:
+       ldrb    data, [ptr]
+       cbz     data, .Ldone
+       adds    ptr, #1
+       b       .Lnul_scan
+.Ldone:
+       sub     src, ptr, src                   /* len = ptr - src */
+#if __ARM_FEATURE_PAC_DEFAULT
+       aut     ip, lr, sp
+#endif
+       pop     {r4, r5, r6, pc}
+.Lzero:
+       mov     src, #0
+#if __ARM_FEATURE_PAC_DEFAULT
+       aut     ip, lr, sp
+#endif
+       pop     {r4, r5, r6, pc}
+
+       .cfi_endproc
+       .size ARCH_LIBCFUN(strnlen), . - ARCH_LIBCFUN(strnlen)
+
+#endif
diff --git a/libs/libc/machine/arm/armv8-m/arch_strrchr.S 
b/libs/libc/machine/arm/armv8-m/arch_strrchr.S
new file mode 100644
index 00000000000..5115d3bdbfa
--- /dev/null
+++ b/libs/libc/machine/arm/armv8-m/arch_strrchr.S
@@ -0,0 +1,149 @@
+/****************************************************************************
+ * libs/libc/machine/arm/armv8-m/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-M / ARMv8-M (Thumb-2).
+ */
+
+#ifndef __ARM_FEATURE_BTI_DEFAULT
+#  define __ARM_FEATURE_BTI_DEFAULT 0
+#endif
+
+#ifndef __ARM_FEATURE_PAC_DEFAULT
+#  define __ARM_FEATURE_PAC_DEFAULT 0
+#endif
+
+       .macro def_fn f p2align=0
+       .text
+       .p2align \p2align
+       .global \f
+       .type \f, %function
+\f:
+       .endm
+
+#define src            r0
+#define ch             r1
+#define data           r2
+#define tmp            r3
+#define const_m1       r4
+#define const_h                r5
+#define repc           r6
+#define last           r7
+
+       .thumb
+       .syntax unified
+def_fn ARCH_LIBCFUN(strrchr)
+       .cfi_sections .debug_frame
+       .cfi_startproc
+#if __ARM_FEATURE_PAC_DEFAULT
+#  if __ARM_FEATURE_BTI_DEFAULT
+       pacbti  ip, lr, sp
+#  else
+       pac     ip, lr, sp
+#  endif
+#endif
+       push    {r4, r5, r6, r7, lr}
+       .cfi_def_cfa_offset 20
+       .cfi_offset lr, -4
+       .cfi_offset r4, -20
+       .cfi_offset r5, -16
+       .cfi_offset r6, -12
+       .cfi_offset r7, -8
+       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      /* 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
+       bne     .Lh1
+       sub     last, src, #1           /* record match (src post-incremented) 
*/
+.Lh1:
+       cmp     data, #0
+       beq     .Lret
+       ands    tmp, src, #3
+       bne     .Lhead
+
+       .p2align 2
+.Lword_loop:
+       ldr     data, [src], #4
+       /* NUL test: syndrome = (data - const_m1) & ~data.  */
+       sub     tmp, data, const_m1
+       bic     tmp, tmp, data
+       tst     tmp, const_h
+       bne     .Lterm
+       /* Search-byte test: zero-byte test on data ^ repc.  */
+       eor     data, data, repc
+       sub     tmp, data, const_m1
+       bic     tmp, tmp, data
+       tst     tmp, const_h
+       bne     .Lmatch_word
+       b       .Lword_loop
+
+.Lmatch_word:
+       /* Match (no NUL): rescan the 4 bytes to update last.  */
+       sub     tmp, src, #4
+.Lmw_scan:
+       ldrb    data, [tmp], #1
+       cmp     data, ch
+       bne     .Lmw1
+       sub     last, tmp, #1
+.Lmw1:
+       cmp     tmp, src
+       bne     .Lmw_scan
+       b       .Lword_loop
+
+.Lterm:
+       /* NUL in this word: rescan bytes tracking last match until NUL.  */
+       sub     src, src, #4
+.Lterm_scan:
+       ldrb    data, [src], #1
+       cmp     data, ch
+       bne     .Lt1
+       sub     last, src, #1
+.Lt1:
+       cmp     data, #0
+       bne     .Lterm_scan
+
+.Lret:
+       mov     src, last
+#if __ARM_FEATURE_PAC_DEFAULT
+       aut     ip, lr, sp
+#endif
+       pop     {r4, r5, r6, r7, pc}
+
+       .cfi_endproc
+       .size ARCH_LIBCFUN(strrchr), . - ARCH_LIBCFUN(strrchr)
+
+#endif

Reply via email to