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 0e2affc7510ee38508c92a4175549f69a9298053
Author: ganjing <[email protected]>
AuthorDate: Tue Nov 24 12:19:38 2026 +0800

    arch/x86_64: Add the ARCH_X86_64_AVX2 option.
    
    ARCH_X86_64_AVX only checks the AVX bit at boot, but code that uses
    AVX2 instructions, such as the AVX2 string functions of libc, raises #UD
    on a CPU that has AVX without AVX2 (Sandy Bridge and Ivy Bridge).  Add
    an option for AVX2: it depends on ARCH_X86_64_AVX, checks the AVX2 bit
    of CPUID leaf 7 at boot and passes -mavx2 to the compiler, as the other
    instruction set options do.
    
    Signed-off-by: ganjing <[email protected]>
---
 arch/x86_64/src/cmake/Toolchain.cmake              | 4 ++++
 arch/x86_64/src/common/Kconfig                     | 8 ++++++++
 arch/x86_64/src/common/Toolchain.defs              | 4 ++++
 arch/x86_64/src/intel64/intel64_check_capability.c | 6 ++++++
 4 files changed, 22 insertions(+)

diff --git a/arch/x86_64/src/cmake/Toolchain.cmake 
b/arch/x86_64/src/cmake/Toolchain.cmake
index e3e551da318..3412e144948 100644
--- a/arch/x86_64/src/cmake/Toolchain.cmake
+++ b/arch/x86_64/src/cmake/Toolchain.cmake
@@ -195,6 +195,10 @@ if(CONFIG_ARCH_X86_64_AVX)
   add_compile_options(-mavx)
 endif()
 
+if(CONFIG_ARCH_X86_64_AVX2)
+  add_compile_options(-mavx2)
+endif()
+
 if(CONFIG_ARCH_X86_64_AVX512)
   add_compile_options(-mavx512f)
 endif()
diff --git a/arch/x86_64/src/common/Kconfig b/arch/x86_64/src/common/Kconfig
index f37400016bb..7b9af69418a 100644
--- a/arch/x86_64/src/common/Kconfig
+++ b/arch/x86_64/src/common/Kconfig
@@ -145,6 +145,14 @@ config ARCH_X86_64_AVX
        depends on ARCH_HAVE_AVX
        default n
 
+config ARCH_X86_64_AVX2
+       bool "AVX2 support"
+       depends on ARCH_X86_64_AVX
+       default n
+       ---help---
+               Enable the use of AVX2 instructions.  The CPU is checked for
+               AVX2 at boot, as it is for AVX.
+
 config ARCH_X86_64_AVX512
        bool "AVX512 support"
        depends on ARCH_HAVE_AVX512
diff --git a/arch/x86_64/src/common/Toolchain.defs 
b/arch/x86_64/src/common/Toolchain.defs
index 514382af4a9..98327ef5b3b 100644
--- a/arch/x86_64/src/common/Toolchain.defs
+++ b/arch/x86_64/src/common/Toolchain.defs
@@ -225,6 +225,10 @@ ifeq ($(CONFIG_ARCH_X86_64_AVX),y)
   ARCHCPUFLAGS += -mavx
 endif
 
+ifeq ($(CONFIG_ARCH_X86_64_AVX2),y)
+  ARCHCPUFLAGS += -mavx2
+endif
+
 ifeq ($(CONFIG_ARCH_X86_64_AVX512),y)
   ARCHCPUFLAGS += -mavx512f
 endif
diff --git a/arch/x86_64/src/intel64/intel64_check_capability.c 
b/arch/x86_64/src/intel64/intel64_check_capability.c
index 2bebe3cdac1..883d0391176 100644
--- a/arch/x86_64/src/intel64/intel64_check_capability.c
+++ b/arch/x86_64/src/intel64/intel64_check_capability.c
@@ -142,6 +142,12 @@ void x86_64_check_and_enable_capability(void)
 
   require = 0;
 
+  /* Check AVX2 instructions availability */
+
+#ifdef CONFIG_ARCH_X86_64_AVX2
+  require |= X86_64_CPUID_07_AVX2;
+#endif
+
   /* Check AVX512 Foundation instructions availability */
 
 #ifdef CONFIG_ARCH_X86_64_AVX512

Reply via email to