casaroli commented on code in PR #22188:
URL: https://github.com/apache/nuttx/pull/22188#discussion_r4665502493


##########
libs/libc/machine/x86_64/arch_strlen_avx2.S:
##########
@@ -0,0 +1,312 @@
+/*********************************************************************************
+ * libs/libc/machine/x86_64/arch_strlen_avx2.S
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ * SPDX-FileCopyrightText: 2014, Intel Corporation
+ *
+ * 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 Intel Corporation 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 OWNER 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.
+  *
+ 
*********************************************************************************/
+
+/*********************************************************************************
+ * Pre-processor Definitions
+ 
*********************************************************************************/
+
+#ifndef USE_AS_STRCAT
+
+#ifndef STRLEN
+# define STRLEN                strlen

Review Comment:
   With `ARCH_X86_64_AVX` and `MM_KASAN` the link fails: `arch_libc.c` defines 
`strlen`, `memcpy`, `memmove`, `memcmp`, `strncmp` and this file defines them 
again (multiple definition), and `arch_strcmp`/`arch_strcpy` are undefined. The 
last commit fixes this for the scan functions only.
   
   Fix: use `ARCH_LIBCFUN()` for the names in all `_avx2` files (memmove + the 
memcpy alias, memcmp, memset, strlen, strcat, strcmp, strncmp, strcpy, strncpy, 
stpcpy, stpncpy).



##########
libs/libc/machine/x86_64/arch_strcmp_avx2.S:
##########
@@ -0,0 +1,1973 @@
+/*********************************************************************************
+ * libs/libc/machine/x86_64/arch_strcmp_avx2.S
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ * SPDX-FileCopyrightText: 2014, Intel Corporation
+ *
+ * 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 Intel Corporation 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 OWNER 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.
+  *
+ 
*********************************************************************************/
+
+/*********************************************************************************
+ * Pre-processor Definitions
+ 
*********************************************************************************/
+
+#ifdef USE_AS_STRNCMP
+/* Since the counter, %r11, is unsigned, we branch to strcmp_exitz
+   if the new counter > the old one or is 0.  */
+#define UPDATE_STRNCMP_COUNTER              \
+       /* calculate left number to compare */    \
+       lea     -16(%rcx, %r11), %r9;                 \
+       cmp     %r9, %r11;                            \
+       jb      L(strcmp_exitz);                      \
+       test    %r9, %r9;                           \
+       je      L(strcmp_exitz);                      \
+       mov     %r9, %r11
+
+#else
+#define UPDATE_STRNCMP_COUNTER
+#ifndef STRCMP
+#define STRCMP         strcmp
+#endif
+#endif
+
+#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
+#define RETURN ret
+
+/*********************************************************************************
+ * Public Functions
+ 
*********************************************************************************/
+
+       .section .text.avx2,"ax",@progbits
+ENTRY (STRCMP)
+/*
+ * This implementation uses SSE to compare up to 16 bytes at a time.
+ */
+#ifdef USE_AS_STRNCMP
+       test    %rdx, %rdx
+       je      L(strcmp_exitz)
+       cmp     $1, %rdx
+       je      L(Byte0)
+       mov     %rdx, %r11
+#endif
+       mov     %esi, %ecx
+       mov     %edi, %eax
+/* Use 64bit AND here to avoid long NOP padding.  */
+       and     $0x3f, %rcx             /* rsi alignment in cache line */
+       and     $0x3f, %rax             /* rdi alignment in cache line */
+       cmp     $0x30, %ecx
+       ja      L(crosscache)   /* rsi: 16-byte load will cross cache line */
+       cmp     $0x30, %eax
+       ja      L(crosscache)   /* rdi: 16-byte load will cross cache line */
+       movlpd  (%rdi), %xmm1
+       movlpd  (%rsi), %xmm2
+       movhpd  8(%rdi), %xmm1
+       movhpd  8(%rsi), %xmm2
+       vpxor %xmm0, %xmm0, %xmm0               /* clear %xmm0 for null char 
checks */
+       vpcmpeqb %xmm1, %xmm0, %xmm0            /* Any null chars? */
+       vpcmpeqb %xmm2, %xmm1, %xmm1            /* compare first 16 bytes for 
equality */
+       vpsubb %xmm0, %xmm1, %xmm1              /* packed sub of comparison 
results*/
+       vpmovmskb %xmm1, %edx
+       sub     $0xffff, %edx           /* if first 16 bytes are same, edx == 
0xffff */
+       jnz     L(less16bytes)  /* If not, find different value or null char */
+#ifdef USE_AS_STRNCMP
+       sub     $16, %r11
+       jbe     L(strcmp_exitz) /* finish comparison */
+#endif
+       add     $16, %rsi               /* prepare to search next 16 bytes */
+       add     $16, %rdi               /* prepare to search next 16 bytes */
+
+       /*
+        * Determine source and destination string offsets from 16-byte 
alignment.
+        * Use relative offset difference between the two to determine which 
case
+        * below to use.
+        */
+       .p2align 4
+L(crosscache):
+       and     $0xfffffffffffffff0, %rsi       /* force %rsi is 16 byte 
aligned */
+       and     $0xfffffffffffffff0, %rdi       /* force %rdi is 16 byte 
aligned */
+       mov     $0xffff, %edx                   /* for equivalent offset */
+       xor     %r8d, %r8d
+       and     $0xf, %ecx                      /* offset of rsi */
+       and     $0xf, %eax                      /* offset of rdi */
+       cmp     %eax, %ecx
+       je      L(ashr_0)                       /* rsi and rdi relative offset 
same */
+       ja      L(bigger)
+       mov     %edx, %r8d                      /* r8d is offset flag for exit 
tail */
+       xchg    %ecx, %eax
+       xchg    %rsi, %rdi
+L(bigger):
+       lea     15(%rax), %r9
+       sub     %rcx, %r9
+       lea     L(unaligned_table)(%rip), %r10
+       movslq  (%r10, %r9,4), %r9
+       lea     (%r10, %r9), %r10
+       jmp     *%r10                           /* jump to corresponding case */
+
+/*
+ * The following cases will be handled by ashr_0
+ *  rcx(offset of rsi)  rax(offset of rdi)  relative offset  corresponding case
+ *        n(0~15)            n(0~15)           15(15+ n-n)         ashr_0
+ */
+       .p2align 4
+L(ashr_0):
+
+       vmovdqa (%rsi), %xmm1
+       vpxor %xmm0, %xmm0, %xmm0                       /* clear %xmm0 for null 
char check */
+       vpcmpeqb %xmm1, %xmm0, %xmm0                    /* Any null chars? */
+       vpcmpeqb (%rdi), %xmm1, %xmm1                   /* compare 16 bytes for 
equality */
+       vpsubb %xmm0, %xmm1, %xmm1                      /* packed sub of 
comparison results*/
+       vpmovmskb %xmm1, %r9d
+       shr     %cl, %edx                       /* adjust 0xffff for offset */
+       shr     %cl, %r9d                       /* adjust for 16-byte offset */
+       sub     %r9d, %edx
+       /*
+        * edx must be the same with r9d if in left byte (16-rcx) is equal to
+        * the start from (16-rax) and no null char was seen.
+        */
+       jne     L(less32bytes)          /* mismatch or null char */
+       UPDATE_STRNCMP_COUNTER
+       mov     $16, %rcx
+       mov     $16, %r9
+       vpxor %xmm0, %xmm0, %xmm0                       /* clear xmm0, may have 
changed above */
+
+       /*
+        * Now both strings are aligned at 16-byte boundary. Loop over strings
+        * checking 32-bytes per iteration.
+        */
+       .p2align 4
+L(loop_ashr_0):
+       /* Compare 32 bytes per iteration with one 256-bit ymm step instead of
+        * two 128-bit xmm steps. ymm0 is the persistent zero vector (never
+        * written in this loop). ymm3 = (s2 byte == 0) null mask; ymm2 =
+        * (s1 == s2) equal mask; equal-minus-null leaves 0xff only for bytes
+        * that are equal AND non-NUL, so edx == 0xffffffff iff all 32 bytes
+        * match with no terminator. The critical-path ops (vpcmpeqb/vpsubb/
+        * vpmovmskb) keep 128-bit latency at 256-bit width, so widening is a
+        * net win (measured ~1.2x). This path (ashr_0) has r8d == 0 (no operand
+        * swap), so on a difference we can locate the byte and return
+        * s1[k]-s2[k] directly. vmovdqu: rsi/rdi are only 16-byte aligned. */
+#ifdef USE_AS_STRNCMP
+       /* strncmp: only take the 32-byte wide step when at least 32 bytes
+        * remain (r11), so any diff found inside the window is guaranteed to
+        * be within bounds (k < 32 <= r11) and the exit path needs no k-vs-n
+        * check. When fewer than 32 remain, hand off to a bounded scalar tail
+        * that respects n exactly. This whole block is compiled out for plain
+        * strcmp. */
+       cmp     $32, %r11
+       jb      L(ashr0_strncmp_tail)
+#endif
+       vmovdqu (%rsi, %rcx), %ymm1

Review Comment:
   This loop reads past the end of the string. The two pointers are only 
16-byte aligned (line 197), so the 32-byte `vmovdqu` loads the next 16 bytes 
even when the NUL is in the first 16, and they can be in the next page. On 
x86_64 Linux, `strcmp()` of two equal 32-character strings whose NUL is the 
last byte of a page, with the next page not mapped, faults here; 
`arch_strcmp.S` does not.
   
   Fix: keep the two aligned 16-byte loads of `arch_strcmp.S` in this loop, or 
take the 32-byte step only when `(%rsi, %rcx)` and `(%rdi, %rcx)` are 32-byte 
aligned or at least 32 bytes before the end of the page.



##########
libs/libc/machine/x86_64/Make.defs:
##########
@@ -29,51 +29,134 @@ ASRCS += arch_setjmp_x86_64.S
 endif
 
 ifeq ($(CONFIG_X86_64_MEMCMP),y)
-ASRCS += arch_memcmp.S
+  ifeq ($(CONFIG_ARCH_X86_64_AVX),y)
+    ASRCS += arch_memcmp_avx2.S
+  else
+    ASRCS += arch_memcmp.S
+  endif
 endif
 
 ifeq ($(CONFIG_X86_64_MEMMOVE),y)
-ASRCS += arch_memmove.S
+  ifeq ($(CONFIG_ARCH_X86_64_AVX),y)
+    ASRCS += arch_memmove_avx2.S
+  else
+    ASRCS += arch_memmove.S
+  endif
 endif
 
 ifeq ($(CONFIG_X86_64_MEMSET),y)
   ifeq ($(CONFIG_ARCH_X86_64_AVX),y)
     ASRCS += arch_memset_avx2.S
   else
-    ASRCS += arch_memset_sse2.S
+    ASRCS += arch_memset.S
   endif
 endif
 
 ifeq ($(CONFIG_X86_64_STPCPY),y)
-ASRCS += arch_stpcpy.S
+  ifeq ($(CONFIG_ARCH_X86_64_AVX),y)
+    ASRCS += arch_stpcpy_avx2.S
+  else
+    ASRCS += arch_stpcpy.S
+  endif
 endif
 
 ifeq ($(CONFIG_X86_64_STPNCPY),y)
-ASRCS += arch_stpncpy.S
+  ifeq ($(CONFIG_ARCH_X86_64_AVX),y)
+    ASRCS += arch_stpncpy_avx2.S
+  else
+    ASRCS += arch_stpncpy.S
+  endif
 endif
 
 ifeq ($(CONFIG_X86_64_STRCAT),y)
-ASRCS += arch_strcat.S
+  ifeq ($(CONFIG_ARCH_X86_64_AVX),y)
+    ASRCS += arch_strcat_avx2.S
+  else
+    ASRCS += arch_strcat.S
+  endif
 endif
 
 ifeq ($(CONFIG_X86_64_STRCMP),y)
-ASRCS += arch_strcmp.S
+  ifeq ($(CONFIG_ARCH_X86_64_AVX),y)
+    ASRCS += arch_strcmp_avx2.S
+  else
+    ASRCS += arch_strcmp.S
+  endif
 endif
 
 ifeq ($(CONFIG_X86_64_STRCPY),y)
-ASRCS += arch_strcpy.S
+  ifeq ($(CONFIG_ARCH_X86_64_AVX),y)
+    ASRCS += arch_strcpy_avx2.S
+  else
+    ASRCS += arch_strcpy.S
+  endif
 endif
 
 ifeq ($(CONFIG_X86_64_STRLEN),y)
-ASRCS += arch_strlen.S
+  ifeq ($(CONFIG_ARCH_X86_64_AVX),y)

Review Comment:
   `ARCH_X86_64_AVX` means AVX, and `intel64_check_capability.c:121` checks 
only the AVX bit. The `_avx2` files use AVX2 instructions (`vpcmpeqb`/`vpminub` 
on ymm, for example `arch_strlen_avx2.S:242`), which raise #UD on an AVX-only 
CPU.
   
   Fix: check `X86_64_CPUID_07_AVX2` at boot when these files are built, or 
select them with a separate AVX2 option.



##########
libs/libc/machine/x86_64/arch_memchr.S:
##########
@@ -0,0 +1,129 @@
+/****************************************************************************
+ * libs/libc/machine/x86_64/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.
+ *
+ ****************************************************************************/
+
+/* Page-safe SSE2 memchr (128-bit; used when AVX2 is not enabled). Bounded
+ * scan of exactly n bytes [s, s+n). 16-byte-aligned-down loads never cross
+ * into the next page; a match is only honored if its byte index is < n. The
+ * loop stops once the aligned pointer passes s+n.
+ *
+ * void *memchr(const void *s /rdi/, int c /esi/, size_t n /rdx/)
+ */
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#include "libc.h"
+
+#ifndef MEMCHR
+# define MEMCHR                ARCH_LIBCFUN(memchr)
+#endif
+
+#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
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+       .text
+ENTRY (MEMCHR)
+       test    %rdx, %rdx
+       jz      L(not_found)
+
+       movd    %esi, %xmm1
+       punpcklbw %xmm1, %xmm1
+       punpcklwd %xmm1, %xmm1
+       pshufd  $0, %xmm1, %xmm1
+
+       lea     (%rdi, %rdx), %r11      /* end = s + n (exclusive) */

Review Comment:
   `s + n` is not saturated as in `arch_strnlen.S:79-81`, so for `n` near 
`SIZE_MAX` the end wraps and `memchr()` returns NULL when the byte is present 
(tested with `n = SIZE_MAX`; the same in `arch_memchr_avx2.S:82`).
   
   Fix: saturate the end with the `add`/`sbb`/`or` sequence of `arch_strnlen.S`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to