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


The following commit(s) were added to refs/heads/master by this push:
     new c2e9beafd87 libc/machine/xtensa: Add optimized string and memory 
functions.
c2e9beafd87 is described below

commit c2e9beafd879a9d05bad006f47f5a4398c3ca2b3
Author: ganjing <[email protected]>
AuthorDate: Tue Jul 7 17:28:07 2026 +0800

    libc/machine/xtensa: Add optimized string and memory functions.
    
    Add assembly-optimized implementations for Xtensa architecture:
    memcmp, memchr, strchr, strchrnul, strrchr, strncmp, strnlen,
    stpcpy, stpncpy, strcat.
    
    Signed-off-by: Jing Gan <[email protected]>
    Signed-off-by: ganjing <[email protected]>
---
 libs/libc/machine/xtensa/CMakeLists.txt   |  40 +++++
 libs/libc/machine/xtensa/Kconfig          |  70 ++++++++
 libs/libc/machine/xtensa/Make.defs        |  40 +++++
 libs/libc/machine/xtensa/arch_memchr.S    | 157 ++++++++++++++++++
 libs/libc/machine/xtensa/arch_memcmp.S    | 257 ++++++++++++++++++++++++++++++
 libs/libc/machine/xtensa/arch_stpcpy.S    | 231 +++++++++++++++++++++++++++
 libs/libc/machine/xtensa/arch_stpncpy.S   | 100 ++++++++++++
 libs/libc/machine/xtensa/arch_strcat.S    | 176 ++++++++++++++++++++
 libs/libc/machine/xtensa/arch_strchr.S    | 225 ++++++++++++++++++++++++++
 libs/libc/machine/xtensa/arch_strchrnul.S |  24 +++
 libs/libc/machine/xtensa/arch_strncmp.S   | 203 +++++++++++++++++++++++
 libs/libc/machine/xtensa/arch_strnlen.S   | 149 +++++++++++++++++
 libs/libc/machine/xtensa/arch_strrchr.S   | 165 +++++++++++++++++++
 13 files changed, 1837 insertions(+)

diff --git a/libs/libc/machine/xtensa/CMakeLists.txt 
b/libs/libc/machine/xtensa/CMakeLists.txt
index 8d3dc9dfda6..9a07bdde25f 100644
--- a/libs/libc/machine/xtensa/CMakeLists.txt
+++ b/libs/libc/machine/xtensa/CMakeLists.txt
@@ -55,6 +55,46 @@ if(CONFIG_XTENSA_STRCMP)
   list(APPEND CSRCS arch_strcmp.S)
 endif()
 
+if(CONFIG_XTENSA_MEMCMP)
+  list(APPEND CSRCS arch_memcmp.S)
+endif()
+
+if(CONFIG_XTENSA_MEMCHR)
+  list(APPEND CSRCS arch_memchr.S)
+endif()
+
+if(CONFIG_XTENSA_STRCHR)
+  list(APPEND CSRCS arch_strchr.S)
+endif()
+
+if(CONFIG_XTENSA_STRRCHR)
+  list(APPEND CSRCS arch_strrchr.S)
+endif()
+
+if(CONFIG_XTENSA_STRNCMP)
+  list(APPEND CSRCS arch_strncmp.S)
+endif()
+
+if(CONFIG_XTENSA_STRNLEN)
+  list(APPEND CSRCS arch_strnlen.S)
+endif()
+
+if(CONFIG_XTENSA_STRCHRNUL)
+  list(APPEND CSRCS arch_strchrnul.S)
+endif()
+
+if(CONFIG_XTENSA_STPCPY)
+  list(APPEND CSRCS arch_stpcpy.S)
+endif()
+
+if(CONFIG_XTENSA_STPNCPY)
+  list(APPEND CSRCS arch_stpncpy.S)
+endif()
+
+if(CONFIG_XTENSA_STRCAT)
+  list(APPEND CSRCS arch_strcat.S)
+endif()
+
 if(NOT CONFIG_PROFILE_NONE)
   list(APPEND CSRCS arch_mcount.c)
 endif()
diff --git a/libs/libc/machine/xtensa/Kconfig b/libs/libc/machine/xtensa/Kconfig
index f72da5c6fab..ada53afab1a 100644
--- a/libs/libc/machine/xtensa/Kconfig
+++ b/libs/libc/machine/xtensa/Kconfig
@@ -30,10 +30,20 @@ config XTENSA_STRING_FUNCTION
        select XTENSA_MEMCPY
        select XTENSA_MEMMOVE
        select XTENSA_MEMSET
+       select XTENSA_MEMCMP
+       select XTENSA_MEMCHR
        select XTENSA_STRCMP
        select XTENSA_STRCPY
        select XTENSA_STRLEN
        select XTENSA_STRNCPY
+       select XTENSA_STRCHR
+       select XTENSA_STRRCHR
+       select XTENSA_STRNCMP
+       select XTENSA_STRNLEN
+       select XTENSA_STRCHRNUL
+       select XTENSA_STPCPY
+       select XTENSA_STPNCPY
+       select XTENSA_STRCAT
 
 config XTENSA_MEMCPY
        bool "Enable optimized memcpy() for XTENSA"
@@ -77,3 +87,63 @@ config XTENSA_STRNCPY
        ---help---
                Enable optimized XTENSA specific strncpy() library function
 
+config XTENSA_MEMCMP
+       bool "Enable optimized memcmp() for XTENSA"
+       select LIBC_ARCH_MEMCMP
+       ---help---
+               Enable optimized XTENSA specific memcmp() library function
+
+config XTENSA_MEMCHR
+       bool "Enable optimized memchr() for XTENSA"
+       select LIBC_ARCH_MEMCHR
+       ---help---
+               Enable optimized XTENSA specific memchr() library function
+
+config XTENSA_STRCHR
+       bool "Enable optimized strchr() for XTENSA"
+       select LIBC_ARCH_STRCHR
+       ---help---
+               Enable optimized XTENSA specific strchr() library function
+
+config XTENSA_STRRCHR
+       bool "Enable optimized strrchr() for XTENSA"
+       select LIBC_ARCH_STRRCHR
+       ---help---
+               Enable optimized XTENSA specific strrchr() library function
+
+config XTENSA_STRNCMP
+       bool "Enable optimized strncmp() for XTENSA"
+       select LIBC_ARCH_STRNCMP
+       ---help---
+               Enable optimized XTENSA specific strncmp() library function
+
+config XTENSA_STRNLEN
+       bool "Enable optimized strnlen() for XTENSA"
+       select LIBC_ARCH_STRNLEN
+       ---help---
+               Enable optimized XTENSA specific strnlen() library function
+
+config XTENSA_STRCHRNUL
+       bool "Enable optimized strchrnul() for XTENSA"
+       select LIBC_ARCH_STRCHRNUL
+       ---help---
+               Enable optimized XTENSA specific strchrnul() library function
+
+config XTENSA_STPCPY
+       bool "Enable optimized stpcpy() for XTENSA"
+       select LIBC_ARCH_STPCPY
+       ---help---
+               Enable optimized XTENSA specific stpcpy() library function
+
+config XTENSA_STPNCPY
+       bool "Enable optimized stpncpy() for XTENSA"
+       select LIBC_ARCH_STPNCPY
+       ---help---
+               Enable optimized XTENSA specific stpncpy() library function
+
+config XTENSA_STRCAT
+       bool "Enable optimized strcat() for XTENSA"
+       select LIBC_ARCH_STRCAT
+       ---help---
+               Enable optimized XTENSA specific strcat() library function
+
diff --git a/libs/libc/machine/xtensa/Make.defs 
b/libs/libc/machine/xtensa/Make.defs
index a1c1348ff39..0130b2769d1 100644
--- a/libs/libc/machine/xtensa/Make.defs
+++ b/libs/libc/machine/xtensa/Make.defs
@@ -56,6 +56,46 @@ ifeq ($(CONFIG_XTENSA_STRCMP),y)
 ASRCS += arch_strcmp.S
 endif
 
+ifeq ($(CONFIG_XTENSA_MEMCMP),y)
+ASRCS += arch_memcmp.S
+endif
+
+ifeq ($(CONFIG_XTENSA_MEMCHR),y)
+ASRCS += arch_memchr.S
+endif
+
+ifeq ($(CONFIG_XTENSA_STRCHR),y)
+ASRCS += arch_strchr.S
+endif
+
+ifeq ($(CONFIG_XTENSA_STRRCHR),y)
+ASRCS += arch_strrchr.S
+endif
+
+ifeq ($(CONFIG_XTENSA_STRNCMP),y)
+ASRCS += arch_strncmp.S
+endif
+
+ifeq ($(CONFIG_XTENSA_STRNLEN),y)
+ASRCS += arch_strnlen.S
+endif
+
+ifeq ($(CONFIG_XTENSA_STRCHRNUL),y)
+ASRCS += arch_strchrnul.S
+endif
+
+ifeq ($(CONFIG_XTENSA_STPCPY),y)
+ASRCS += arch_stpcpy.S
+endif
+
+ifeq ($(CONFIG_XTENSA_STPNCPY),y)
+ASRCS += arch_stpncpy.S
+endif
+
+ifeq ($(CONFIG_XTENSA_STRCAT),y)
+ASRCS += arch_strcat.S
+endif
+
 ifeq ($(CONFIG_PROFILE_NONE),)
 CSRCS += arch_mcount.c
 endif
diff --git a/libs/libc/machine/xtensa/arch_memchr.S 
b/libs/libc/machine/xtensa/arch_memchr.S
new file mode 100644
index 00000000000..117665edfcc
--- /dev/null
+++ b/libs/libc/machine/xtensa/arch_memchr.S
@@ -0,0 +1,157 @@
+/****************************************************************************
+ * libs/libc/machine/xtensa/arch_memchr.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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include "xtensa_asm.h"
+
+#include <arch/chip/core-isa.h>
+#include <arch/xtensa/xtensa_abi.h>
+
+#include "libc.h"
+
+#ifdef LIBC_BUILD_MEMCHR
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+  .section .text
+  .begin schedule
+  .align  4
+  .literal_position
+  .global ARCH_LIBCFUN(memchr)
+  .type ARCH_LIBCFUN(memchr), @function
+ARCH_LIBCFUN(memchr):
+  ENTRY(16)
+  /* a2 = s, a3 = c, a4 = n */
+
+  /* Return NULL if n == 0 */
+
+  beqz  a4, .Lret_null
+
+  /* Broadcast c to 4 bytes: a5 = pattern */
+
+  extui a3, a3, 0, 8   /* c &= 0xff */
+  slli  a5, a3, 8
+  or  a5, a3, a5        /* 0x00cc -> 0xcccc */
+  slli  a8, a5, 16
+  or  a5, a5, a8        /* 0xcccc -> 0xcccccccc */
+
+  /* Byte-by-byte until s is word-aligned */
+
+  bbci.l  a2, 0, .Lcheck_align2
+  l8ui  a8, a2, 0
+  beq a8, a3, .Lret_found
+  addi  a2, a2, 1
+  addi  a4, a4, -1
+  beqz  a4, .Lret_null
+.Lcheck_align2:
+  bbci.l  a2, 1, .Lword_setup
+  beqz  a4, .Lret_null
+  l8ui  a8, a2, 0
+  beq a8, a3, .Lret_found
+  addi  a2, a2, 1
+  addi  a4, a4, -1
+  beqz  a4, .Lret_null
+  l8ui  a8, a2, 0
+  beq a8, a3, .Lret_found
+  addi  a2, a2, 1
+  addi  a4, a4, -1
+  beqz  a4, .Lret_null
+
+  /* s is now word-aligned. Set up MASKs for word-at-a-time loop. */
+
+.Lword_setup:
+  movi  a6, MASK0
+  movi  a7, MASK1
+  movi  a8, MASK2
+  movi  a9, MASK3
+
+  /* Word-at-a-time main loop */
+
+.Lword_loop:
+  bltui a4, 4, .Ltail
+#if XCHAL_HAVE_LOOPS
+  srli  a10, a4, 2      /* a10 = n / 4 = loop count */
+  loopnez a10, .Lword_loop_end
+#endif
+.Lword_loop_body:
+  l32i  a10, a2, 0
+  xor a11, a10, a5      /* XOR with pattern: matching bytes become 0 */
+  bnone a11, a6, .Lfound0
+  bnone a11, a7, .Lfound1
+  bnone a11, a8, .Lfound2
+  bnone a11, a9, .Lfound3
+  addi  a2, a2, 4
+  addi  a4, a4, -4
+#if XCHAL_HAVE_LOOPS
+.Lword_loop_end:
+#else
+  bgeui a4, 4, .Lword_loop_body
+#endif
+
+  /* Tail: byte-by-byte for remaining */
+
+.Ltail:
+  beqz  a4, .Lret_null
+.Ltail_loop:
+  l8ui  a10, a2, 0
+  beq a10, a3, .Lret_found
+  addi  a2, a2, 1
+  addi  a4, a4, -1
+  bnez  a4, .Ltail_loop
+
+.Lret_null:
+  movi  a2, 0
+  RET(16)
+
+.Lret_found:
+  /* a2 already points to the found byte */
+  RET(16)
+
+.Lfound0:
+  /* byte0 of word matches, s+0 */
+  RET(16)
+
+.Lfound1:
+  /* byte1 of word matches, s+1 */
+  addi  a2, a2, 1
+  RET(16)
+
+.Lfound2:
+  /* byte2 of word matches, s+2 */
+  addi  a2, a2, 2
+  RET(16)
+
+.Lfound3:
+  /* byte3 of word matches, s+3 */
+  addi  a2, a2, 3
+  RET(16)
+
+  .end schedule
+
+  .size ARCH_LIBCFUN(memchr), . - ARCH_LIBCFUN(memchr)
+
+#endif
diff --git a/libs/libc/machine/xtensa/arch_memcmp.S 
b/libs/libc/machine/xtensa/arch_memcmp.S
new file mode 100644
index 00000000000..c8a8fd5775a
--- /dev/null
+++ b/libs/libc/machine/xtensa/arch_memcmp.S
@@ -0,0 +1,257 @@
+/****************************************************************************
+ * libs/libc/machine/xtensa/arch_memcmp.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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include "xtensa_asm.h"
+
+#include <arch/chip/core-isa.h>
+#include <arch/xtensa/xtensa_abi.h>
+
+#include "libc.h"
+
+#ifdef LIBC_BUILD_MEMCMP
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+  .section .text
+  .begin schedule
+  .align  4
+  .literal_position
+  .global ARCH_LIBCFUN(memcmp)
+  .type ARCH_LIBCFUN(memcmp), @function
+ARCH_LIBCFUN(memcmp):
+  ENTRY(16)
+  /* a2 = s1, a3 = s2, a4 = n */
+
+  /* Return 0 if n == 0 */
+
+  beqz  a4, .Lret_zero
+
+  /* Byte compare until s1 is word-aligned */
+
+  bbci.l  a2, 0, .Lalign2
+  l8ui  a8, a2, 0
+  l8ui  a9, a3, 0
+  addi  a2, a2, 1
+  addi  a3, a3, 1
+  addi  a4, a4, -1
+  bne a8, a9, .Ldiff_byte
+  beqz  a4, .Lret_zero
+.Lalign2:
+  bbci.l  a2, 1, .Laligned
+  beqz  a4, .Lret_zero
+  l8ui  a8, a2, 0
+  l8ui  a9, a3, 0
+  addi  a2, a2, 1
+  addi  a3, a3, 1
+  addi  a4, a4, -1
+  bne a8, a9, .Ldiff_byte
+  beqz  a4, .Lret_zero
+  l8ui  a8, a2, 0
+  l8ui  a9, a3, 0
+  addi  a2, a2, 1
+  addi  a3, a3, 1
+  addi  a4, a4, -1
+  bne a8, a9, .Ldiff_byte
+  beqz  a4, .Lret_zero
+
+  /* s1 is word-aligned. Check if s2 is also word-aligned. */
+
+.Laligned:
+  movi  a5, MASK0
+  movi  a6, MASK1
+  movi  a7, MASK2
+
+  /* Check s2 alignment */
+
+  bbsi.l  a3, 0, .Lunaligned
+  bbsi.l  a3, 1, .Lunaligned
+
+  /* Both s1 and s2 are word-aligned. Word-at-a-time compare. */
+
+.Laligned_loop:
+  bltui a4, 4, .Ltail
+#if XCHAL_HAVE_LOOPS
+  srli  a8, a4, 2       /* a8 = n / 4 = loop count */
+  loopnez a8, .Laligned_loop_end
+#endif
+.Laligned_loop_body:
+  l32i  a8, a2, 0
+  l32i  a9, a3, 0
+  addi  a2, a2, 4
+  addi  a3, a3, 4
+  addi  a4, a4, -4
+  bne a8, a9, .Lfind_diff
+#if XCHAL_HAVE_LOOPS
+.Laligned_loop_end:
+  j .Ltail
+#else
+  bgeui a4, 4, .Laligned_loop_body
+  j .Ltail
+#endif
+
+  /* s1 aligned, s2 unaligned. Use funnel shift for s2. */
+
+.Lunaligned:
+  bltui a4, 4, .Ltail
+
+  /* Set up funnel shift */
+
+  ssa8  a3
+  movi  a10, ~3
+  and a3, a3, a10   /* align s2 pointer down */
+  l32i  a11, a3, 0  /* load first word of s2 */
+
+.Lunaligned_loop:
+  bltui a4, 4, .Ltail_unaligned
+#if XCHAL_HAVE_LOOPS
+  srli  a8, a4, 2
+  loopnez a8, .Lunaligned_loop_end
+#endif
+.Lunaligned_loop_body:
+  l32i  a9, a3, 4   /* load next aligned word from s2 */
+  src_b a10, a11, a9 /* funnel shift to get unaligned word */
+  l32i  a8, a2, 0   /* load aligned word from s1 */
+  addi  a2, a2, 4
+  addi  a3, a3, 4
+  addi  a4, a4, -4
+  mov a11, a9        /* save for next iteration */
+  bne a8, a10, .Lfind_diff_ua
+#if XCHAL_HAVE_LOOPS
+.Lunaligned_loop_end:
+#else
+  bgeui a4, 4, .Lunaligned_loop_body
+#endif
+
+.Ltail_unaligned:
+  /* Restore s2 byte pointer from aligned pointer + SAR offset */
+
+  rsr a9, SAR
+  srli  a9, a9, 3   /* offset = SAR / 8 */
+  add a3, a3, a9    /* a3 = real unaligned s2 position */
+
+.Ltail:
+  /* Byte-by-byte compare for remaining n bytes */
+
+  beqz  a4, .Lret_zero
+.Ltail_loop:
+  l8ui  a8, a2, 0
+  l8ui  a9, a3, 0
+  addi  a2, a2, 1
+  addi  a3, a3, 1
+  bne a8, a9, .Ldiff_byte
+  addi  a4, a4, -1
+  bnez  a4, .Ltail_loop
+
+.Lret_zero:
+  movi  a2, 0
+  RET(16)
+
+  /* Found difference in word compare (both aligned) */
+
+.Lfind_diff:
+  /* a8 = w1, a9 = w2, they differ. Find first different byte. */
+
+  xor a10, a8, a9
+  bany  a10, a5, .Ldiff0  /* byte0 differs */
+  bany  a10, a6, .Ldiff1  /* byte1 differs */
+  bany  a10, a7, .Ldiff2  /* byte2 differs */
+  /* byte3 must differ */
+#if XCHAL_HAVE_BE
+  extui a8, a8, 0, 8
+  extui a9, a9, 0, 8
+#else
+  extui a8, a8, 24, 8
+  extui a9, a9, 24, 8
+#endif
+  sub a2, a8, a9
+  RET(16)
+
+.Ldiff0:
+#if XCHAL_HAVE_BE
+  extui a8, a8, 24, 8
+  extui a9, a9, 24, 8
+#else
+  extui a8, a8, 0, 8
+  extui a9, a9, 0, 8
+#endif
+  sub a2, a8, a9
+  RET(16)
+
+.Ldiff1:
+#if XCHAL_HAVE_BE
+  extui a8, a8, 16, 8
+  extui a9, a9, 16, 8
+#else
+  extui a8, a8, 8, 8
+  extui a9, a9, 8, 8
+#endif
+  sub a2, a8, a9
+  RET(16)
+
+.Ldiff2:
+#if XCHAL_HAVE_BE
+  extui a8, a8, 8, 8
+  extui a9, a9, 8, 8
+#else
+  extui a8, a8, 16, 8
+  extui a9, a9, 16, 8
+#endif
+  sub a2, a8, a9
+  RET(16)
+
+  /* Found difference in unaligned path */
+
+.Lfind_diff_ua:
+  /* a8 = w1 (from s1), a10 = w2 (funnel shifted from s2) */
+
+  mov a9, a10
+  xor a10, a8, a9
+  bany  a10, a5, .Ldiff0
+  bany  a10, a6, .Ldiff1
+  bany  a10, a7, .Ldiff2
+#if XCHAL_HAVE_BE
+  extui a8, a8, 0, 8
+  extui a9, a9, 0, 8
+#else
+  extui a8, a8, 24, 8
+  extui a9, a9, 24, 8
+#endif
+  sub a2, a8, a9
+  RET(16)
+
+  /* Byte difference return */
+
+.Ldiff_byte:
+  sub a2, a8, a9
+  RET(16)
+
+  .end schedule
+
+  .size ARCH_LIBCFUN(memcmp), . - ARCH_LIBCFUN(memcmp)
+
+#endif
diff --git a/libs/libc/machine/xtensa/arch_stpcpy.S 
b/libs/libc/machine/xtensa/arch_stpcpy.S
new file mode 100644
index 00000000000..c76b53cc7bf
--- /dev/null
+++ b/libs/libc/machine/xtensa/arch_stpcpy.S
@@ -0,0 +1,231 @@
+/****************************************************************************
+ * libs/libc/machine/xtensa/arch_stpcpy.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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include "xtensa_asm.h"
+
+#include <arch/chip/core-isa.h>
+#include <arch/xtensa/xtensa_abi.h>
+
+#include "libc.h"
+
+#ifdef LIBC_BUILD_STPCPY
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+  .section .text
+  .begin schedule
+  .align  4
+  .literal_position
+  .global ARCH_LIBCFUN(stpcpy)
+  .type ARCH_LIBCFUN(stpcpy), @function
+ARCH_LIBCFUN(stpcpy):
+  ENTRY(16)
+  /* a2 = dst, a3 = src */
+  /* Returns pointer to the null terminator written in dst */
+
+  mov a10, a2           /* a10 = dst working pointer */
+  movi  a4, MASK0
+  movi  a5, MASK1
+  movi  a6, MASK2
+  movi  a7, MASK3
+
+  /* Align src to word boundary with byte copies */
+
+  bbsi.l  a3, 0, .Lsrc1mod2
+  bbsi.l  a3, 1, .Lsrc2mod4
+  j .Lsrcaligned
+
+.Lsrc1mod2:
+  l8ui  a8, a3, 0
+  addi  a3, a3, 1
+  s8i a8, a10, 0
+  beqz  a8, .Lret_null_pos
+  addi  a10, a10, 1
+  bbci.l  a3, 1, .Lsrcaligned
+
+.Lsrc2mod4:
+  l8ui  a8, a3, 0
+  s8i a8, a10, 0
+  beqz  a8, .Lret_null_pos
+  l8ui  a8, a3, 1
+  addi  a3, a3, 2
+  s8i a8, a10, 1
+  addi  a10, a10, 2
+  beqz  a8, .Lret_null_pos_minus1
+
+.Lsrcaligned:
+  /* src is word-aligned. Check if dst is also word-aligned. */
+
+  movi  a9, 3
+  bnone a10, a9, .Lboth_aligned
+
+  /* src aligned, dst NOT aligned. Word-read src, byte-write dst. */
+
+.Lsrc_aligned_only:
+  l32i  a8, a3, 0
+  addi  a3, a3, 4
+
+  /* Check and store byte0 */
+
+#if XCHAL_HAVE_BE
+  extui a9, a8, 24, 8
+#else
+  extui a9, a8, 0, 8
+#endif
+  s8i a9, a10, 0
+  beqz  a9, .Lret_null_pos
+
+  /* byte1 */
+
+#if XCHAL_HAVE_BE
+  extui a9, a8, 16, 8
+#else
+  extui a9, a8, 8, 8
+#endif
+  s8i a9, a10, 1
+  beqz  a9, .Lret_null_pos_plus1
+
+  /* byte2 */
+
+#if XCHAL_HAVE_BE
+  extui a9, a8, 8, 8
+#else
+  extui a9, a8, 16, 8
+#endif
+  s8i a9, a10, 2
+  beqz  a9, .Lret_null_pos_plus2
+
+  /* byte3 */
+
+#if XCHAL_HAVE_BE
+  extui a9, a8, 0, 8
+#else
+  extui a9, a8, 24, 8
+#endif
+  s8i a9, a10, 3
+  addi  a10, a10, 4
+  beqz  a9, .Lret_null_pos_minus1
+  j .Lsrc_aligned_only
+
+  /* Both src and dst are word-aligned. Fast path. */
+
+.Lboth_aligned:
+#if XCHAL_HAVE_LOOPS
+  _movi.n a8, 0
+  loop  a8, .Lloop_end
+#endif
+.Lloop:
+  l32i  a8, a3, 0
+  addi  a3, a3, 4
+
+  bnone a8, a4, .Lfound0
+  bnone a8, a5, .Lfound1
+  bnone a8, a6, .Lfound2
+  bnone a8, a7, .Lfound3
+
+  s32i  a8, a10, 0
+  addi  a10, a10, 4
+#if XCHAL_HAVE_LOOPS
+.Lloop_end:
+  j .Lloop
+#else
+  j .Lloop
+#endif
+
+  /* Found null at byte0 of word */
+
+.Lfound0:
+  s8i a8, a10, 0
+  mov a2, a10
+  RET(16)
+
+  /* Found null at byte1 */
+
+.Lfound1:
+#if XCHAL_HAVE_BE
+  extui a9, a8, 24, 8
+  s8i a9, a10, 0
+  s8i a8, a10, 1
+#else
+  s8i a8, a10, 0
+  extui a9, a8, 8, 8
+  s8i a9, a10, 1
+#endif
+  addi  a2, a10, 1
+  RET(16)
+
+  /* Found null at byte2 */
+
+.Lfound2:
+#if XCHAL_HAVE_BE
+  extui a9, a8, 24, 8
+  s8i a9, a10, 0
+  extui a9, a8, 16, 8
+  s8i a9, a10, 1
+  extui a9, a8, 8, 8
+  s8i a9, a10, 2
+#else
+  s8i a8, a10, 0
+  extui a9, a8, 8, 8
+  s8i a9, a10, 1
+  extui a9, a8, 16, 8
+  s8i a9, a10, 2
+#endif
+  addi  a2, a10, 2
+  RET(16)
+
+  /* Found null at byte3 - all 4 bytes valid, store whole word */
+
+.Lfound3:
+  s32i  a8, a10, 0
+  addi  a2, a10, 3
+  RET(16)
+
+  /* Return: a10 points to the null byte written */
+
+.Lret_null_pos:
+  mov a2, a10
+  RET(16)
+
+.Lret_null_pos_plus1:
+  addi  a2, a10, 1
+  RET(16)
+
+.Lret_null_pos_plus2:
+  addi  a2, a10, 2
+  RET(16)
+
+.Lret_null_pos_minus1:
+  addi  a2, a10, -1
+  RET(16)
+
+  .end schedule
+
+  .size ARCH_LIBCFUN(stpcpy), . - ARCH_LIBCFUN(stpcpy)
+
+#endif
diff --git a/libs/libc/machine/xtensa/arch_stpncpy.S 
b/libs/libc/machine/xtensa/arch_stpncpy.S
new file mode 100644
index 00000000000..9f531d14e2f
--- /dev/null
+++ b/libs/libc/machine/xtensa/arch_stpncpy.S
@@ -0,0 +1,100 @@
+/****************************************************************************
+ * libs/libc/machine/xtensa/arch_stpncpy.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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include "xtensa_asm.h"
+
+#include <arch/chip/core-isa.h>
+#include <arch/xtensa/xtensa_abi.h>
+
+#include "libc.h"
+
+#ifdef LIBC_BUILD_STPNCPY
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+  .section .text
+  .begin schedule
+  .align  4
+  .literal_position
+  .global ARCH_LIBCFUN(stpncpy)
+  .type ARCH_LIBCFUN(stpncpy), @function
+ARCH_LIBCFUN(stpncpy):
+  ENTRY(16)
+  /* a2 = dst, a3 = src, a4 = n */
+  /* Returns pointer to the byte after the last non-null byte written,
+     or dst+n if src has n or more characters. */
+
+  /* If n == 0, return dst */
+
+  beqz  a4, .Lret
+
+  /* Copy up to n bytes from src, stopping at null */
+
+.Lcopy_loop:
+  l8ui  a8, a3, 0
+  s8i a8, a2, 0
+  beqz  a8, .Lfill_zero
+  addi  a2, a2, 1
+  addi  a3, a3, 1
+  addi  a4, a4, -1
+  bnez  a4, .Lcopy_loop
+
+  /* Copied n bytes without hitting null. Return dst+n (current a2). */
+
+  RET(16)
+
+  /* Hit null in src. a2 points to the null written.
+     Save return value, then zero-fill remaining. */
+
+.Lfill_zero:
+  mov a10, a2         /* return value = pointer to first null written */
+  addi  a4, a4, -1   /* we already wrote the null, decrement n */
+  beqz  a4, .Lret_a10
+
+  /* Zero-fill remaining bytes */
+
+  addi  a2, a2, 1
+  movi  a8, 0
+.Lfill_loop:
+  s8i a8, a2, 0
+  addi  a2, a2, 1
+  addi  a4, a4, -1
+  bnez  a4, .Lfill_loop
+
+.Lret_a10:
+  mov a2, a10
+  RET(16)
+
+.Lret:
+  RET(16)
+
+  .end schedule
+
+  .size ARCH_LIBCFUN(stpncpy), . - ARCH_LIBCFUN(stpncpy)
+
+#endif
diff --git a/libs/libc/machine/xtensa/arch_strcat.S 
b/libs/libc/machine/xtensa/arch_strcat.S
new file mode 100644
index 00000000000..9af347ac2b9
--- /dev/null
+++ b/libs/libc/machine/xtensa/arch_strcat.S
@@ -0,0 +1,176 @@
+/****************************************************************************
+ * libs/libc/machine/xtensa/arch_strcat.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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include "xtensa_asm.h"
+
+#include <arch/chip/core-isa.h>
+#include <arch/xtensa/xtensa_abi.h>
+
+#include "libc.h"
+
+#ifdef LIBC_BUILD_STRCAT
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+  .section .text
+  .begin schedule
+  .align  4
+  .literal_position
+  .global ARCH_LIBCFUN(strcat)
+  .type ARCH_LIBCFUN(strcat), @function
+ARCH_LIBCFUN(strcat):
+  ENTRY(16)
+  /* a2 = dst, a3 = src */
+
+  /* Save dst for return value */
+
+  mov a10, a2
+
+  /* --- Phase 1: Find end of dst (inline strlen) --- */
+
+  /* Byte-by-byte until dst is word-aligned */
+
+  bbci.l  a2, 0, .Lstrlen_check2
+  l8ui  a8, a2, 0
+  beqz  a8, .Lstrlen_done
+  addi  a2, a2, 1
+.Lstrlen_check2:
+  bbci.l  a2, 1, .Lstrlen_word_setup
+  l8ui  a8, a2, 0
+  beqz  a8, .Lstrlen_done
+  addi  a2, a2, 1
+  l8ui  a8, a2, 0
+  beqz  a8, .Lstrlen_done
+  addi  a2, a2, 1
+
+.Lstrlen_word_setup:
+  movi  a4, MASK0
+  movi  a5, MASK1
+  movi  a6, MASK2
+  movi  a7, MASK3
+
+.Lstrlen_loop:
+  l32i  a8, a2, 0
+  bnone a8, a4, .Lstrlen_done
+  bnone a8, a5, .Lstrlen_z1
+  bnone a8, a6, .Lstrlen_z2
+  bnone a8, a7, .Lstrlen_z3
+  addi  a2, a2, 4
+  j .Lstrlen_loop
+
+.Lstrlen_z1:
+  addi  a2, a2, 1
+  j .Lstrlen_done
+.Lstrlen_z2:
+  addi  a2, a2, 2
+  j .Lstrlen_done
+.Lstrlen_z3:
+  addi  a2, a2, 3
+
+.Lstrlen_done:
+  /* a2 now points to the null terminator of dst */
+
+  /* --- Phase 2: Copy src to end of dst --- */
+  /* Word-read src, byte-write dst (dst may not be aligned) */
+
+  mov a11, a2           /* a11 = dst write pointer */
+
+  /* Align src first */
+
+  bbsi.l  a3, 0, .Lcpy_src1mod2
+  bbsi.l  a3, 1, .Lcpy_src2mod4
+  j .Lcpy_srcaligned
+
+.Lcpy_src1mod2:
+  l8ui  a8, a3, 0
+  addi  a3, a3, 1
+  s8i a8, a11, 0
+  beqz  a8, .Lret
+  addi  a11, a11, 1
+  bbci.l  a3, 1, .Lcpy_srcaligned
+
+.Lcpy_src2mod4:
+  l8ui  a8, a3, 0
+  s8i a8, a11, 0
+  beqz  a8, .Lret
+  l8ui  a8, a3, 1
+  addi  a3, a3, 2
+  s8i a8, a11, 1
+  addi  a11, a11, 2
+  beqz  a8, .Lret
+
+.Lcpy_srcaligned:
+  /* src is word-aligned. Word-read, byte-write. */
+
+.Lcpy_word_loop:
+  l32i  a8, a3, 0
+  addi  a3, a3, 4
+
+#if XCHAL_HAVE_BE
+  extui a9, a8, 24, 8
+#else
+  extui a9, a8, 0, 8
+#endif
+  s8i a9, a11, 0
+  beqz  a9, .Lret
+
+#if XCHAL_HAVE_BE
+  extui a9, a8, 16, 8
+#else
+  extui a9, a8, 8, 8
+#endif
+  s8i a9, a11, 1
+  beqz  a9, .Lret
+
+#if XCHAL_HAVE_BE
+  extui a9, a8, 8, 8
+#else
+  extui a9, a8, 16, 8
+#endif
+  s8i a9, a11, 2
+  beqz  a9, .Lret
+
+#if XCHAL_HAVE_BE
+  extui a9, a8, 0, 8
+#else
+  extui a9, a8, 24, 8
+#endif
+  s8i a9, a11, 3
+  addi  a11, a11, 4
+  beqz  a9, .Lret
+  j .Lcpy_word_loop
+
+.Lret:
+  mov a2, a10   /* return original dst */
+  RET(16)
+
+  .end schedule
+
+  .size ARCH_LIBCFUN(strcat), . - ARCH_LIBCFUN(strcat)
+
+#endif
diff --git a/libs/libc/machine/xtensa/arch_strchr.S 
b/libs/libc/machine/xtensa/arch_strchr.S
new file mode 100644
index 00000000000..1071e5fdbe0
--- /dev/null
+++ b/libs/libc/machine/xtensa/arch_strchr.S
@@ -0,0 +1,225 @@
+/****************************************************************************
+ * libs/libc/machine/xtensa/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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include "xtensa_asm.h"
+
+#include <arch/chip/core-isa.h>
+#include <arch/xtensa/xtensa_abi.h>
+
+#include "libc.h"
+
+#ifdef USE_AS_STRCHRNUL
+#  ifdef LIBC_BUILD_STRCHRNUL
+#    define STRCHR_FUNC strchrnul
+#  else
+#    undef USE_AS_STRCHRNUL
+#  endif
+#endif
+
+#ifndef USE_AS_STRCHRNUL
+#  ifdef LIBC_BUILD_STRCHR
+#    define STRCHR_FUNC strchr
+#  else
+     /* Neither guard is set, skip compilation */
+#    define SKIP_COMPILATION
+#  endif
+#endif
+
+#ifndef SKIP_COMPILATION
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+  .section .text
+  .begin schedule
+  .align  4
+  .literal_position
+  .global ARCH_LIBCFUN(STRCHR_FUNC)
+  .type ARCH_LIBCFUN(STRCHR_FUNC), @function
+ARCH_LIBCFUN(STRCHR_FUNC):
+  ENTRY(16)
+  /* a2 = s, a3 = c */
+
+  /* Broadcast c to 4 bytes: a5 = pattern */
+
+  extui a5, a3, 0, 8   /* c &= 0xff */
+  slli  a8, a5, 8
+  or  a5, a5, a8        /* 0xcc -> 0xcccc */
+  slli  a8, a5, 16
+  or  a5, a5, a8        /* 0xcccc -> 0xcccccccc */
+
+  /* Also keep single byte c in a3 for byte comparisons */
+
+  extui a3, a3, 0, 8
+
+  /* Byte-by-byte until s is word-aligned */
+
+  bbci.l  a2, 0, .Lcheck_align2
+  l8ui  a8, a2, 0
+  beq a8, a3, .Lret_found
+  beqz  a8, .Lret_null_or_pos
+  addi  a2, a2, 1
+.Lcheck_align2:
+  bbci.l  a2, 1, .Lword_setup
+  l8ui  a8, a2, 0
+  beq a8, a3, .Lret_found
+  beqz  a8, .Lret_null_or_pos
+  addi  a2, a2, 1
+  l8ui  a8, a2, 0
+  beq a8, a3, .Lret_found
+  beqz  a8, .Lret_null_or_pos
+  addi  a2, a2, 1
+
+  /* s is now word-aligned. Set up MASKs. */
+
+.Lword_setup:
+  movi  a6, MASK0
+  movi  a7, MASK1
+  movi  a8, MASK2
+  movi  a9, MASK3
+
+  /* Word-at-a-time main loop.
+     For each word, check both for null and for matching char.
+     The byte order determines which comes first. */
+
+#if XCHAL_HAVE_LOOPS
+  _movi.n a10, 0
+  loop  a10, .Lloop_end   /* infinite loop */
+#endif
+.Lloop:
+  l32i  a10, a2, 0
+  xor a11, a10, a5      /* xored: matching bytes become 0 */
+
+  /* Check byte0 */
+
+  bnone a10, a6, .Lnull0  /* byte0 of word is null */
+  bnone a11, a6, .Lfound0 /* byte0 matches c */
+
+  /* Check byte1 */
+
+  bnone a10, a7, .Lnull1
+  bnone a11, a7, .Lfound1
+
+  /* Check byte2 */
+
+  bnone a10, a8, .Lnull2
+  bnone a11, a8, .Lfound2
+
+  /* Check byte3 */
+
+  bnone a10, a9, .Lnull3
+  bnone a11, a9, .Lfound3
+
+  addi  a2, a2, 4
+#if XCHAL_HAVE_LOOPS
+.Lloop_end:
+  j .Lloop
+#else
+  j .Lloop
+#endif
+
+  /* Found matching character */
+
+.Lfound0:
+  RET(16)
+
+.Lfound1:
+  addi  a2, a2, 1
+  RET(16)
+
+.Lfound2:
+  addi  a2, a2, 2
+  RET(16)
+
+.Lfound3:
+  addi  a2, a2, 3
+  RET(16)
+
+  /* Found null terminator before c */
+
+.Lnull0:
+  /* Check if c == '\0' (i.e., null IS the char we're looking for) */
+  bnone a11, a6, .Lfound0
+#ifdef USE_AS_STRCHRNUL
+  /* strchrnul: return pointer to the null byte */
+  RET(16)
+#else
+  movi  a2, 0
+  RET(16)
+#endif
+
+.Lnull1:
+  bnone a11, a7, .Lfound1
+#ifdef USE_AS_STRCHRNUL
+  addi  a2, a2, 1
+  RET(16)
+#else
+  movi  a2, 0
+  RET(16)
+#endif
+
+.Lnull2:
+  bnone a11, a8, .Lfound2
+#ifdef USE_AS_STRCHRNUL
+  addi  a2, a2, 2
+  RET(16)
+#else
+  movi  a2, 0
+  RET(16)
+#endif
+
+.Lnull3:
+  bnone a11, a9, .Lfound3
+#ifdef USE_AS_STRCHRNUL
+  addi  a2, a2, 3
+  RET(16)
+#else
+  movi  a2, 0
+  RET(16)
+#endif
+
+  /* Return found (byte loop) */
+
+.Lret_found:
+  RET(16)
+
+  /* Return null or position (byte loop hit null) */
+
+.Lret_null_or_pos:
+#ifdef USE_AS_STRCHRNUL
+  /* strchrnul returns pointer to null */
+  RET(16)
+#else
+  movi  a2, 0
+  RET(16)
+#endif
+
+  .end schedule
+
+  .size ARCH_LIBCFUN(STRCHR_FUNC), . - ARCH_LIBCFUN(STRCHR_FUNC)
+
+#endif /* !SKIP_COMPILATION */
diff --git a/libs/libc/machine/xtensa/arch_strchrnul.S 
b/libs/libc/machine/xtensa/arch_strchrnul.S
new file mode 100644
index 00000000000..2bec385f01f
--- /dev/null
+++ b/libs/libc/machine/xtensa/arch_strchrnul.S
@@ -0,0 +1,24 @@
+/****************************************************************************
+ * libs/libc/machine/xtensa/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.
+ *
+ ****************************************************************************/
+
+#define USE_AS_STRCHRNUL
+#include "arch_strchr.S"
diff --git a/libs/libc/machine/xtensa/arch_strncmp.S 
b/libs/libc/machine/xtensa/arch_strncmp.S
new file mode 100644
index 00000000000..542c9cba6d1
--- /dev/null
+++ b/libs/libc/machine/xtensa/arch_strncmp.S
@@ -0,0 +1,203 @@
+/****************************************************************************
+ * libs/libc/machine/xtensa/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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include "xtensa_asm.h"
+
+#include <arch/chip/core-isa.h>
+#include <arch/xtensa/xtensa_abi.h>
+
+#include "libc.h"
+
+#ifdef LIBC_BUILD_STRNCMP
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+  .section .text
+  .begin schedule
+  .align  4
+  .literal_position
+  .global ARCH_LIBCFUN(strncmp)
+  .type ARCH_LIBCFUN(strncmp), @function
+ARCH_LIBCFUN(strncmp):
+  ENTRY(16)
+  /* a2 = s1, a3 = s2, a4 = n */
+
+  /* Return 0 if n == 0 */
+
+  beqz  a4, .Lret_zero
+
+  /* Byte compare until s1 is word-aligned, also handle short n */
+
+.Lbyte_head:
+  bbci.l  a2, 0, .Lcheck2
+  l8ui  a8, a2, 0
+  l8ui  a9, a3, 0
+  addi  a2, a2, 1
+  addi  a3, a3, 1
+  addi  a4, a4, -1
+  bne a8, a9, .Ldiff_byte
+  beqz  a8, .Lret_zero  /* null terminator */
+  beqz  a4, .Lret_zero
+.Lcheck2:
+  bbci.l  a2, 1, .Lcheck_word
+  l8ui  a8, a2, 0
+  l8ui  a9, a3, 0
+  addi  a2, a2, 1
+  addi  a3, a3, 1
+  addi  a4, a4, -1
+  bne a8, a9, .Ldiff_byte
+  beqz  a8, .Lret_zero
+  beqz  a4, .Lret_zero
+  l8ui  a8, a2, 0
+  l8ui  a9, a3, 0
+  addi  a2, a2, 1
+  addi  a3, a3, 1
+  addi  a4, a4, -1
+  bne a8, a9, .Ldiff_byte
+  beqz  a8, .Lret_zero
+  beqz  a4, .Lret_zero
+
+  /* s1 is word-aligned. Check if s2 is also aligned. */
+
+.Lcheck_word:
+  bbsi.l  a3, 0, .Ltail_loop
+  bbsi.l  a3, 1, .Ltail_loop
+
+  /* Both aligned. Word-at-a-time loop with null and diff detection. */
+
+  movi  a5, MASK0
+  movi  a6, MASK1
+  movi  a7, MASK2
+
+.Laligned_loop:
+  bltui a4, 4, .Ltail_loop
+  l32i  a8, a2, 0
+  l32i  a9, a3, 0
+  addi  a2, a2, 4
+  addi  a3, a3, 4
+  addi  a4, a4, -4
+  bne a8, a9, .Lfind_diff
+  /* Check for null in w1 */
+  bnone a8, a5, .Lret_zero  /* byte0 is null */
+  bnone a8, a6, .Lret_zero  /* byte1 is null */
+  bnone a8, a7, .Lret_zero  /* byte2 is null */
+  movi  a10, MASK3
+  bnone a8, a10, .Lret_zero /* byte3 is null */
+  j .Laligned_loop
+
+  /* Words differ. Find first diff, but null before diff → equal. */
+
+.Lfind_diff:
+  xor a10, a8, a9
+
+  /* byte0 */
+  bnone a8, a5, .Lret_zero    /* null at byte0 → equal */
+  bany  a10, a5, .Ldiff0      /* diff at byte0 */
+
+  /* byte1 */
+  bnone a8, a6, .Lret_zero
+  bany  a10, a6, .Ldiff1
+
+  /* byte2 */
+  bnone a8, a7, .Lret_zero
+  bany  a10, a7, .Ldiff2
+
+  /* byte3: null check */
+  movi  a11, MASK3
+  bnone a8, a11, .Lret_zero
+
+  /* byte3 differs */
+#if XCHAL_HAVE_BE
+  extui a8, a8, 0, 8
+  extui a9, a9, 0, 8
+#else
+  extui a8, a8, 24, 8
+  extui a9, a9, 24, 8
+#endif
+  sub a2, a8, a9
+  RET(16)
+
+.Ldiff0:
+#if XCHAL_HAVE_BE
+  extui a8, a8, 24, 8
+  extui a9, a9, 24, 8
+#else
+  extui a8, a8, 0, 8
+  extui a9, a9, 0, 8
+#endif
+  sub a2, a8, a9
+  RET(16)
+
+.Ldiff1:
+#if XCHAL_HAVE_BE
+  extui a8, a8, 16, 8
+  extui a9, a9, 16, 8
+#else
+  extui a8, a8, 8, 8
+  extui a9, a9, 8, 8
+#endif
+  sub a2, a8, a9
+  RET(16)
+
+.Ldiff2:
+#if XCHAL_HAVE_BE
+  extui a8, a8, 8, 8
+  extui a9, a9, 8, 8
+#else
+  extui a8, a8, 16, 8
+  extui a9, a9, 16, 8
+#endif
+  sub a2, a8, a9
+  RET(16)
+
+  /* Tail: byte-by-byte for unaligned s2 or remaining bytes */
+
+.Ltail_loop:
+  beqz  a4, .Lret_zero
+  l8ui  a8, a2, 0
+  l8ui  a9, a3, 0
+  addi  a2, a2, 1
+  addi  a3, a3, 1
+  bne a8, a9, .Ldiff_byte
+  beqz  a8, .Lret_zero
+  addi  a4, a4, -1
+  bnez  a4, .Ltail_loop
+
+.Lret_zero:
+  movi  a2, 0
+  RET(16)
+
+.Ldiff_byte:
+  sub a2, a8, a9
+  RET(16)
+
+  .end schedule
+
+  .size ARCH_LIBCFUN(strncmp), . - ARCH_LIBCFUN(strncmp)
+
+#endif
diff --git a/libs/libc/machine/xtensa/arch_strnlen.S 
b/libs/libc/machine/xtensa/arch_strnlen.S
new file mode 100644
index 00000000000..0b960d4a9b8
--- /dev/null
+++ b/libs/libc/machine/xtensa/arch_strnlen.S
@@ -0,0 +1,149 @@
+/****************************************************************************
+ * libs/libc/machine/xtensa/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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include "xtensa_asm.h"
+
+#include <arch/chip/core-isa.h>
+#include <arch/xtensa/xtensa_abi.h>
+
+#include "libc.h"
+
+#ifdef LIBC_BUILD_STRNLEN
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+  .section .text
+  .begin schedule
+  .align  4
+  .literal_position
+  .global ARCH_LIBCFUN(strnlen)
+  .type ARCH_LIBCFUN(strnlen), @function
+ARCH_LIBCFUN(strnlen):
+  ENTRY(16)
+  /* a2 = s, a3 = maxlen */
+
+  /* Return 0 if maxlen == 0 */
+
+  beqz  a3, .Lret_zero
+
+  /* Save start pointer */
+
+  mov a10, a2           /* a10 = start */
+
+  /* Byte-by-byte until s is word-aligned */
+
+  bbci.l  a2, 0, .Lcheck2
+  l8ui  a8, a2, 0
+  beqz  a8, .Lret_len
+  addi  a2, a2, 1
+  addi  a3, a3, -1
+  beqz  a3, .Lret_maxlen_a2
+.Lcheck2:
+  bbci.l  a2, 1, .Lword_setup
+  l8ui  a8, a2, 0
+  beqz  a8, .Lret_len
+  addi  a2, a2, 1
+  addi  a3, a3, -1
+  beqz  a3, .Lret_maxlen_a2
+  l8ui  a8, a2, 0
+  beqz  a8, .Lret_len
+  addi  a2, a2, 1
+  addi  a3, a3, -1
+  beqz  a3, .Lret_maxlen_a2
+
+  /* s is word-aligned. Set up MASKs. */
+
+.Lword_setup:
+  movi  a4, MASK0
+  movi  a5, MASK1
+  movi  a6, MASK2
+  movi  a7, MASK3
+
+  /* Word-at-a-time loop using count (a3) */
+
+.Lword_loop:
+  bltui a3, 4, .Ltail   /* less than 4 bytes remaining */
+  l32i  a8, a2, 0
+  bnone a8, a4, .Lz0
+  bnone a8, a5, .Lz1
+  bnone a8, a6, .Lz2
+  bnone a8, a7, .Lz3
+  addi  a2, a2, 4
+  addi  a3, a3, -4
+  j .Lword_loop
+
+  /* Found null byte in word */
+
+.Lz0:
+  sub a2, a2, a10
+  RET(16)
+
+.Lz1:
+  addi  a2, a2, 1
+  sub a2, a2, a10
+  RET(16)
+
+.Lz2:
+  addi  a2, a2, 2
+  sub a2, a2, a10
+  RET(16)
+
+.Lz3:
+  addi  a2, a2, 3
+  sub a2, a2, a10
+  RET(16)
+
+  /* Tail: byte-by-byte for remaining bytes */
+
+.Ltail:
+  beqz  a3, .Lret_maxlen_a2
+.Ltail_loop:
+  l8ui  a8, a2, 0
+  beqz  a8, .Lret_len
+  addi  a2, a2, 1
+  addi  a3, a3, -1
+  bnez  a3, .Ltail_loop
+
+.Lret_maxlen_a2:
+  /* Return s - start = total bytes scanned = original maxlen */
+  sub a2, a2, a10
+  RET(16)
+
+.Lret_zero:
+  movi  a2, 0
+  RET(16)
+
+.Lret_len:
+  sub a2, a2, a10
+  RET(16)
+
+  .end schedule
+
+  .size ARCH_LIBCFUN(strnlen), . - ARCH_LIBCFUN(strnlen)
+
+#endif
diff --git a/libs/libc/machine/xtensa/arch_strrchr.S 
b/libs/libc/machine/xtensa/arch_strrchr.S
new file mode 100644
index 00000000000..be25fe81297
--- /dev/null
+++ b/libs/libc/machine/xtensa/arch_strrchr.S
@@ -0,0 +1,165 @@
+/****************************************************************************
+ * libs/libc/machine/xtensa/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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include "xtensa_asm.h"
+
+#include <arch/chip/core-isa.h>
+#include <arch/xtensa/xtensa_abi.h>
+
+#include "libc.h"
+
+#ifdef LIBC_BUILD_STRRCHR
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+  .section .text
+  .begin schedule
+  .align  4
+  .literal_position
+  .global ARCH_LIBCFUN(strrchr)
+  .type ARCH_LIBCFUN(strrchr), @function
+ARCH_LIBCFUN(strrchr):
+  ENTRY(16)
+  /* a2 = s, a3 = c */
+
+  extui a3, a3, 0, 8   /* c &= 0xff */
+  movi  a10, 0         /* a10 = last_found = NULL */
+
+  /* Broadcast c to 4 bytes: a5 = pattern */
+
+  slli  a5, a3, 8
+  or  a5, a3, a5
+  slli  a8, a5, 16
+  or  a5, a5, a8        /* a5 = pattern = c * 0x01010101 */
+
+  /* Byte-by-byte until s is word-aligned */
+
+  bbci.l  a2, 0, .Lcheck2
+  l8ui  a8, a2, 0
+  beq a8, a3, .Lupdate0
+.Lafter0:
+  beqz  a8, .Lret_last
+  addi  a2, a2, 1
+.Lcheck2:
+  bbci.l  a2, 1, .Lword_setup
+  l8ui  a8, a2, 0
+  beq a8, a3, .Lupdate1
+.Lafter1:
+  beqz  a8, .Lret_last
+  addi  a2, a2, 1
+  l8ui  a8, a2, 0
+  beq a8, a3, .Lupdate2
+.Lafter2:
+  beqz  a8, .Lret_last
+  addi  a2, a2, 1
+
+  /* s is word-aligned. Set up MASKs. */
+
+.Lword_setup:
+  movi  a6, MASK0
+  movi  a7, MASK1
+  movi  a8, MASK2
+  movi  a9, MASK3
+
+  /* Word-at-a-time loop: scan forward, update last_found on match,
+     stop at null. Order: check match first, then check null. */
+
+.Lword_loop:
+  l32i  a11, a2, 0
+  xor a4, a11, a5       /* xored: matching bytes become 0 */
+
+  /* byte0: check match, then null */
+
+  bnone a4, a6, .Lmatch0
+.Lm0_done:
+  bnone a11, a6, .Lret_last
+
+  /* byte1 */
+
+  bnone a4, a7, .Lmatch1
+.Lm1_done:
+  bnone a11, a7, .Lret_last
+
+  /* byte2 */
+
+  bnone a4, a8, .Lmatch2
+.Lm2_done:
+  bnone a11, a8, .Lret_last
+
+  /* byte3 */
+
+  bnone a4, a9, .Lmatch3
+.Lm3_done:
+  bnone a11, a9, .Lret_last
+
+  addi  a2, a2, 4
+  j .Lword_loop
+
+  /* Match handlers: update last_found and continue checking */
+
+.Lmatch0:
+  mov a10, a2
+  j .Lm0_done
+
+.Lmatch1:
+  addi  a10, a2, 1
+  j .Lm1_done
+
+.Lmatch2:
+  addi  a10, a2, 2
+  j .Lm2_done
+
+.Lmatch3:
+  addi  a10, a2, 3
+  j .Lm3_done
+
+  /* Byte loop match handlers */
+
+.Lupdate0:
+  mov a10, a2
+  j .Lafter0
+
+.Lupdate1:
+  mov a10, a2
+  j .Lafter1
+
+.Lupdate2:
+  mov a10, a2
+  j .Lafter2
+
+  /* Return last found position (or NULL) */
+
+.Lret_last:
+  mov a2, a10
+  RET(16)
+
+  .end schedule
+
+  .size ARCH_LIBCFUN(strrchr), . - ARCH_LIBCFUN(strrchr)
+
+#endif

Reply via email to