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 103f4767e93f5bb3d276c5c91d8f5f43d348df92 Author: ligd <[email protected]> AuthorDate: Thu May 7 15:14:25 2026 +0800 libc/machine/x86_64: Add an optimized strnlen(). Add arch_strnlen using repnz scasb instruction for x86_64. Controlled by CONFIG_X86_64_STRNLEN which selects LIBC_ARCH_STRNLEN. Signed-off-by: ligd <[email protected]> Signed-off-by: ganjing <[email protected]> --- libs/libc/machine/x86_64/CMakeLists.txt | 4 ++ libs/libc/machine/x86_64/Kconfig | 7 +++ libs/libc/machine/x86_64/Make.defs | 4 ++ libs/libc/machine/x86_64/arch_strnlen.S | 95 +++++++++++++++++++++++++++++++++ 4 files changed, 110 insertions(+) diff --git a/libs/libc/machine/x86_64/CMakeLists.txt b/libs/libc/machine/x86_64/CMakeLists.txt index e904299ff01..df7c8c22a88 100644 --- a/libs/libc/machine/x86_64/CMakeLists.txt +++ b/libs/libc/machine/x86_64/CMakeLists.txt @@ -70,6 +70,10 @@ if(CONFIG_X86_64_STRLEN) list(APPEND SRCS arch_strlen.S) endif() +if(CONFIG_X86_64_STRNLEN) + list(APPEND SRCS arch_strnlen.S) +endif() + if(CONFIG_X86_64_STRNCPY) list(APPEND SRCS arch_strncpy.S) endif() diff --git a/libs/libc/machine/x86_64/Kconfig b/libs/libc/machine/x86_64/Kconfig index 86928bff330..56dc6cb1174 100644 --- a/libs/libc/machine/x86_64/Kconfig +++ b/libs/libc/machine/x86_64/Kconfig @@ -99,6 +99,13 @@ config X86_64_STRLEN ---help--- Enable optimized X86_64 specific strlen() library function +config X86_64_STRNLEN + bool "Enable optimized strnlen() for X86_64" + default n + select LIBC_ARCH_STRNLEN + ---help--- + Enable optimized X86_64 specific strnlen() library function + config X86_64_STRNCPY bool "Enable optimized strncpy() for X86_64" default n diff --git a/libs/libc/machine/x86_64/Make.defs b/libs/libc/machine/x86_64/Make.defs index 4184b7a74fc..0f40d7a25c6 100644 --- a/libs/libc/machine/x86_64/Make.defs +++ b/libs/libc/machine/x86_64/Make.defs @@ -68,6 +68,10 @@ ifeq ($(CONFIG_X86_64_STRLEN),y) ASRCS += arch_strlen.S endif +ifeq ($(CONFIG_X86_64_STRNLEN),y) +ASRCS += arch_strnlen.S +endif + ifeq ($(CONFIG_X86_64_STRNCPY),y) ASRCS += arch_strncpy.S endif diff --git a/libs/libc/machine/x86_64/arch_strnlen.S b/libs/libc/machine/x86_64/arch_strnlen.S new file mode 100644 index 00000000000..459292290be --- /dev/null +++ b/libs/libc/machine/x86_64/arch_strnlen.S @@ -0,0 +1,95 @@ +/**************************************************************************** + * libs/libc/machine/x86_64/arch_strnlen.S + * + * 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. + * + ****************************************************************************/ + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#include "libc.h" + +#ifndef L +# define L(label) .L##label +#endif + +#ifndef cfi_startproc +# define cfi_startproc .cfi_startproc +#endif + +#ifndef cfi_endproc +# define cfi_endproc .cfi_endproc +#endif + +#ifndef ENTRY +# define ENTRY(name) \ + .type name, @function; \ + .globl name; \ + .p2align 4; \ +name: \ + cfi_startproc +#endif + +#ifndef END +# define END(name) \ + cfi_endproc; \ + .size name, .-name +#endif + +#ifndef STRNLEN +# define STRNLEN ARCH_LIBCFUN(strnlen) +#endif + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/* size_t arch_strnlen(const char *s, size_t maxlen) + * + * Returns the length of string s, but at most maxlen. + * + * Input: + * %rdi - pointer to string s + * %rsi - maxlen + * + * Output: + * %rax - min(strlen(s), maxlen) + */ + + .section .text,"ax",@progbits +ENTRY (STRNLEN) + xor %eax, %eax /* rax = 0, also null byte to search */ + mov %rsi, %rcx /* rcx = maxlen (counter for repnz) */ + test %rcx, %rcx /* if maxlen == 0 */ + jz L(ret) /* return 0 */ + mov %rdi, %rdx /* save start pointer */ + repnz scasb /* scan for null byte, up to rcx bytes */ + jnz L(not_found) /* ZF=0: count exhausted, no null found */ + + /* Null found: %rdi points one past the null byte */ + + sub %rdx, %rdi /* rdi = bytes consumed (including null) */ + lea -1(%rdi), %rax /* length = bytes consumed - 1 */ + ret + +L(not_found): + mov %rsi, %rax /* return maxlen */ +L(ret): + ret + +END (STRNLEN)
