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 293ceef142dc44c565bec44f4455303dc600ebe6 Author: ganjing <[email protected]> AuthorDate: Fri Nov 20 19:00:34 2026 +0800 libc/machine/arm/armv7-r: add 6 new arch string functions. Add ARM-state implementations of memcmp, strchr, strchrnul, strncmp, strnlen and strrchr, sharing the ARMv7-A sources. They scan a word at a time instead of a byte at a time. Each function has its own Kconfig option that selects the matching LIBC_ARCH_* symbol, and ARMV7R_STRING_FUNCTION enables all of them. Signed-off-by: ganjing <[email protected]> --- libs/libc/machine/arm/armv7-r/CMakeLists.txt | 24 ++++ libs/libc/machine/arm/armv7-r/Kconfig | 54 +++++++++ libs/libc/machine/arm/armv7-r/Make.defs | 24 ++++ libs/libc/machine/arm/armv7-r/arch_memcmp.S | 139 +++++++++++++++++++++++ libs/libc/machine/arm/armv7-r/arch_strchr.S | 123 ++++++++++++++++++++ libs/libc/machine/arm/armv7-r/arch_strchrnul.S | 112 ++++++++++++++++++ libs/libc/machine/arm/armv7-r/arch_strncmp.S | 151 +++++++++++++++++++++++++ libs/libc/machine/arm/armv7-r/arch_strnlen.S | 101 +++++++++++++++++ libs/libc/machine/arm/armv7-r/arch_strrchr.S | 122 ++++++++++++++++++++ 9 files changed, 850 insertions(+) diff --git a/libs/libc/machine/arm/armv7-r/CMakeLists.txt b/libs/libc/machine/arm/armv7-r/CMakeLists.txt index 9fe70da88ca..289512dbb0c 100644 --- a/libs/libc/machine/arm/armv7-r/CMakeLists.txt +++ b/libs/libc/machine/arm/armv7-r/CMakeLists.txt @@ -42,6 +42,30 @@ if(CONFIG_ARMV7R_STRLEN) list(APPEND SRCS arch_strlen.S) endif() +if(CONFIG_ARMV7R_MEMCMP) + list(APPEND SRCS arch_memcmp.S) +endif() + +if(CONFIG_ARMV7R_STRCHR) + list(APPEND SRCS arch_strchr.S) +endif() + +if(CONFIG_ARMV7R_STRCHRNUL) + list(APPEND SRCS arch_strchrnul.S) +endif() + +if(CONFIG_ARMV7R_STRNCMP) + list(APPEND SRCS arch_strncmp.S) +endif() + +if(CONFIG_ARMV7R_STRNLEN) + list(APPEND SRCS arch_strnlen.S) +endif() + +if(CONFIG_ARMV7R_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-r/Kconfig b/libs/libc/machine/arm/armv7-r/Kconfig index ac9085d0389..6cebb251bd3 100644 --- a/libs/libc/machine/arm/armv7-r/Kconfig +++ b/libs/libc/machine/arm/armv7-r/Kconfig @@ -29,11 +29,17 @@ config ARMV7R_STRING_FUNCTION default n depends on ARCH_TOOLCHAIN_GNU select ARMV7R_MEMCHR + select ARMV7R_MEMCMP select ARMV7R_MEMCPY select ARMV7R_MEMMOVE select ARMV7R_MEMSET + select ARMV7R_STRCHR + select ARMV7R_STRCHRNUL select ARMV7R_STRCMP select ARMV7R_STRLEN + select ARMV7R_STRNCMP + select ARMV7R_STRNLEN + select ARMV7R_STRRCHR config ARMV7R_MEMCHR bool "Enable optimized memchr() for ARMv7-R" @@ -82,3 +88,51 @@ config ARMV7R_STRLEN ---help--- Enable optimized ARMv7-R specific strlen() library function + +config ARMV7R_MEMCMP + bool "Enable optimized memcmp() for ARMv7-R" + default n + select LIBC_ARCH_MEMCMP + depends on ARCH_TOOLCHAIN_GNU + ---help--- + Enable optimized ARMv7-R specific memcmp() library function + +config ARMV7R_STRCHR + bool "Enable optimized strchr() for ARMv7-R" + default n + select LIBC_ARCH_STRCHR + depends on ARCH_TOOLCHAIN_GNU + ---help--- + Enable optimized ARMv7-R specific strchr() library function + +config ARMV7R_STRCHRNUL + bool "Enable optimized strchrnul() for ARMv7-R" + default n + select LIBC_ARCH_STRCHRNUL + depends on ARCH_TOOLCHAIN_GNU + ---help--- + Enable optimized ARMv7-R specific strchrnul() library function + +config ARMV7R_STRNCMP + bool "Enable optimized strncmp() for ARMv7-R" + default n + select LIBC_ARCH_STRNCMP + depends on ARCH_TOOLCHAIN_GNU + ---help--- + Enable optimized ARMv7-R specific strncmp() library function + +config ARMV7R_STRNLEN + bool "Enable optimized strnlen() for ARMv7-R" + default n + select LIBC_ARCH_STRNLEN + depends on ARCH_TOOLCHAIN_GNU + ---help--- + Enable optimized ARMv7-R specific strnlen() library function + +config ARMV7R_STRRCHR + bool "Enable optimized strrchr() for ARMv7-R" + default n + select LIBC_ARCH_STRRCHR + depends on ARCH_TOOLCHAIN_GNU + ---help--- + Enable optimized ARMv7-R specific strrchr() library function diff --git a/libs/libc/machine/arm/armv7-r/Make.defs b/libs/libc/machine/arm/armv7-r/Make.defs index 90d8e45af59..c598bbf96c4 100644 --- a/libs/libc/machine/arm/armv7-r/Make.defs +++ b/libs/libc/machine/arm/armv7-r/Make.defs @@ -44,6 +44,30 @@ ifeq ($(CONFIG_ARMV7R_STRLEN),y) ASRCS += arch_strlen.S endif +ifeq ($(CONFIG_ARMV7R_MEMCMP),y) +ASRCS += arch_memcmp.S +endif + +ifeq ($(CONFIG_ARMV7R_STRCHR),y) +ASRCS += arch_strchr.S +endif + +ifeq ($(CONFIG_ARMV7R_STRCHRNUL),y) +ASRCS += arch_strchrnul.S +endif + +ifeq ($(CONFIG_ARMV7R_STRNCMP),y) +ASRCS += arch_strncmp.S +endif + +ifeq ($(CONFIG_ARMV7R_STRNLEN),y) +ASRCS += arch_strnlen.S +endif + +ifeq ($(CONFIG_ARMV7R_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-r/arch_memcmp.S b/libs/libc/machine/arm/armv7-r/arch_memcmp.S new file mode 100644 index 00000000000..338a3457a96 --- /dev/null +++ b/libs/libc/machine/arm/armv7-r/arch_memcmp.S @@ -0,0 +1,139 @@ +/**************************************************************************** + * libs/libc/machine/arm/armv7-r/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-r/arch_strchr.S b/libs/libc/machine/arm/armv7-r/arch_strchr.S new file mode 100644 index 00000000000..9b47abd5d70 --- /dev/null +++ b/libs/libc/machine/arm/armv7-r/arch_strchr.S @@ -0,0 +1,123 @@ +/**************************************************************************** + * libs/libc/machine/arm/armv7-r/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-r/arch_strchrnul.S b/libs/libc/machine/arm/armv7-r/arch_strchrnul.S new file mode 100644 index 00000000000..adad08e44c1 --- /dev/null +++ b/libs/libc/machine/arm/armv7-r/arch_strchrnul.S @@ -0,0 +1,112 @@ +/**************************************************************************** + * libs/libc/machine/arm/armv7-r/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-r/arch_strncmp.S b/libs/libc/machine/arm/armv7-r/arch_strncmp.S new file mode 100644 index 00000000000..a4038b58511 --- /dev/null +++ b/libs/libc/machine/arm/armv7-r/arch_strncmp.S @@ -0,0 +1,151 @@ +/**************************************************************************** + * libs/libc/machine/arm/armv7-r/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-r/arch_strnlen.S b/libs/libc/machine/arm/armv7-r/arch_strnlen.S new file mode 100644 index 00000000000..7c0efb39d1d --- /dev/null +++ b/libs/libc/machine/arm/armv7-r/arch_strnlen.S @@ -0,0 +1,101 @@ +/**************************************************************************** + * libs/libc/machine/arm/armv7-r/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-r/arch_strrchr.S b/libs/libc/machine/arm/armv7-r/arch_strrchr.S new file mode 100644 index 00000000000..1ca69cb99bc --- /dev/null +++ b/libs/libc/machine/arm/armv7-r/arch_strrchr.S @@ -0,0 +1,122 @@ +/**************************************************************************** + * libs/libc/machine/arm/armv7-r/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
