From 050b0581b183ab7a0a6ccaf6a2e947700d8a93ee Mon Sep 17 00:00:00 2001 From: Sam Li Date: Tue, 14 Apr 2026 21:48:06 -0700 Subject: [PATCH] automatically detect and enable/disable AVX2 --- CMakeLists.txt | 14 +++++++++++++- src/sperr_helper.cpp | 4 ---- test_scripts/CMakeLists.txt | 6 ------ 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 161fdf4a..1fc367f1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -32,8 +32,20 @@ option( BUILD_SHARED_LIBS "Build shared SPERR library" ON ) option( BUILD_UNIT_TESTS "Build unit tests using GoogleTest" ON ) option( BUILD_CLI_UTILITIES "Build a set of command line utilities" ON ) option( USE_OMP "Use OpenMP parallelization on 3D volumes" OFF ) -option( ENABLE_AVX2 "Enable AVX2 instruction set compilation" ON ) option( SPERR_PREFER_RPATH "Set RPATH; this can fight with package managers so turn off when building for them" ON ) + +# +# AVX2 auto-detection: default ON for x86, OFF otherwise. +# +if(NOT DEFINED ENABLE_AVX2) + if(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|AMD64|i[3-9]86") + option( ENABLE_AVX2 "Enable AVX2 instruction set compilation" ON ) + else() + option( ENABLE_AVX2 "Enable AVX2 instruction set compilation" OFF ) + endif() +else() + option( ENABLE_AVX2 "Enable AVX2 instruction set compilation" ${ENABLE_AVX2} ) +endif() mark_as_advanced(FORCE SPERR_PREFER_RPATH) if(USE_OMP) diff --git a/src/sperr_helper.cpp b/src/sperr_helper.cpp index b72aae57..421b6c87 100644 --- a/src/sperr_helper.cpp +++ b/src/sperr_helper.cpp @@ -11,10 +11,6 @@ #include #endif -#ifdef __AVX2__ -#include -#endif - #ifdef USE_OMP #include #endif diff --git a/test_scripts/CMakeLists.txt b/test_scripts/CMakeLists.txt index d329dae6..0dbf6c50 100644 --- a/test_scripts/CMakeLists.txt +++ b/test_scripts/CMakeLists.txt @@ -1,12 +1,6 @@ add_executable( sperr_helper sperr_helper_unit_test.cpp ) target_link_libraries( sperr_helper PUBLIC SPERR GTest::gtest_main ) -if(ENABLE_AVX2) - target_compile_options(sperr_helper PRIVATE - "$<$:/arch:AVX2>" - "$<$,$>:-mavx2;-mfma>") -endif() - add_executable( bitstream bitstream_unit_test.cpp ) target_link_libraries( bitstream PUBLIC SPERR GTest::gtest_main )