From 9c17e90c77ed813cc62eb1c6313fbdc4d6ffbc75 Mon Sep 17 00:00:00 2001 From: Bram Veenboer Date: Fri, 15 Aug 2025 10:30:57 +0200 Subject: [PATCH] Add AVX checks (#20) Co-authored-by: mancini --- CMakeLists.txt | 1 + benchmarks/CMakeLists.txt | 7 +++++-- cmake/FindAVX.cmake | 42 +++++++++++++++++++++++++++++++++++++++ src/CMakeLists.txt | 11 ++++++---- src/lookup_avx.cpp | 1 - tests/CMakeLists.txt | 8 +++++--- 6 files changed, 60 insertions(+), 10 deletions(-) create mode 100644 cmake/FindAVX.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index ef1df5e..2faf542 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,6 +12,7 @@ option(TRIGDX_BUILD_TESTS "Build tests" ON) option(TRIGDX_BUILD_BENCHMARKS "Build tests" ON) option(TRIGDX_BUILD_PYTHON "Build Python interface" ON) +list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake") configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/cmake/trigdx_config.hpp.in ${CMAKE_CURRENT_BINARY_DIR}/include/trigdx/trigdx_config.hpp @ONLY) diff --git a/benchmarks/CMakeLists.txt b/benchmarks/CMakeLists.txt index a44e7fc..95bc091 100644 --- a/benchmarks/CMakeLists.txt +++ b/benchmarks/CMakeLists.txt @@ -13,8 +13,11 @@ target_link_libraries(benchmark_reference PRIVATE trigdx benchmark::benchmark) add_executable(benchmark_lookup benchmark_lookup.cpp) target_link_libraries(benchmark_lookup PRIVATE trigdx benchmark::benchmark) -add_executable(benchmark_lookup_avx benchmark_lookup_avx.cpp) -target_link_libraries(benchmark_lookup_avx PRIVATE trigdx benchmark::benchmark) +if(HAVE_AVX) + add_executable(benchmark_lookup_avx benchmark_lookup_avx.cpp) + target_link_libraries(benchmark_lookup_avx PRIVATE trigdx + benchmark::benchmark) +endif() if(TRIGDX_USE_MKL) add_executable(benchmark_mkl benchmark_mkl.cpp) diff --git a/cmake/FindAVX.cmake b/cmake/FindAVX.cmake new file mode 100644 index 0000000..9fac219 --- /dev/null +++ b/cmake/FindAVX.cmake @@ -0,0 +1,42 @@ +include(CheckCXXSourceRuns) + +set(SUPPORTED_COMPILERS Clang;GNU;Intel;IntelLLVM) + +if(CMAKE_CXX_COMPILER_ID IN_LIST SUPPORTED_COMPILERS) + if(CMAKE_CXX_COMPILER_ID STREQUAL "Intel") + set(CMAKE_REQUIRED_FLAGS "-xHost") # ICC + elseif(CMAKE_CXX_COMPILER_ID STREQUAL "IntelLLVM") + set(CMAKE_REQUIRED_FLAGS "-march=native") # ICX + else() + set(CMAKE_REQUIRED_FLAGS "-march=native") # GCC/Clang + endif() +else() + message(FATAL_ERROR "Unsupported compiler: ${CMAKE_CXX_COMPILER_ID}.") +endif() + +# AVX check +check_cxx_source_runs( + " +#include +int main() { + __m256 a = _mm256_setzero_ps(); // AVX + (void) a; + return 0; +} +" + HAVE_AVX) + +if(HAVE_AVX) + # AVX2 check + check_cxx_source_runs( + " +#include +int main() { + __m256i a = _mm256_set1_epi32(-1); + __m256i b = _mm256_abs_epi32(a); // AVX2 + (void) b; + return 0; +} +" + HAVE_AVX2) +endif() diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f7eb2f2..09c01e9 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,10 +1,12 @@ include(FetchContent) - -add_library(trigdx reference.cpp lookup.cpp lookup_avx.cpp) +include(FindAVX) +add_library(trigdx reference.cpp lookup.cpp) target_include_directories(trigdx PUBLIC ${PROJECT_SOURCE_DIR}/include) -target_compile_options(trigdx PRIVATE -O3 -march=native) +if(HAVE_AVX) + target_sources(trigdx PRIVATE lookup_avx.cpp) +endif() if(TRIGDX_USE_MKL) find_package(MKL REQUIRED) @@ -22,7 +24,8 @@ if(TRIGDX_USE_GPU) endif() if(TRIGDX_USE_XSIMD) - find_package(xsimd QUIET) + # Requires XSIMD > 13 for architecture independent dispatching + find_package(xsimd 13 QUIET) if(NOT TARGET xsimd) FetchContent_Declare( xsimd diff --git a/src/lookup_avx.cpp b/src/lookup_avx.cpp index b3313ff..1effba7 100644 --- a/src/lookup_avx.cpp +++ b/src/lookup_avx.cpp @@ -160,7 +160,6 @@ template struct LookupAVXBackend::Impl { for (std::size_t i = 0; i < n; ++i) { std::size_t idx = static_cast(x[i] * SCALE) & MASK; std::size_t idx_cos = (idx + NR_SAMPLES / 4) & MASK; - s[i] = lookup[idx]; c[i] = lookup[idx_cos]; } #endif diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 69992d0..bd50093 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -10,9 +10,11 @@ target_link_libraries(test_lookup PRIVATE trigdx Catch2::Catch2WithMain) add_test(NAME test_lookup COMMAND test_lookup) # LookupAVX backend test -add_executable(test_lookup_avx test_lookup_avx.cpp) -target_link_libraries(test_lookup_avx PRIVATE trigdx Catch2::Catch2WithMain) -add_test(NAME test_lookup_avx COMMAND test_lookup_avx) +if(HAVE_AVX) + add_executable(test_lookup_avx test_lookup_avx.cpp) + target_link_libraries(test_lookup_avx PRIVATE trigdx Catch2::Catch2WithMain) + add_test(NAME test_lookup_avx COMMAND test_lookup_avx) +endif() # MKL backend test if(TRIGDX_USE_MKL)