commit 80badb8f12f8a362587d0cb1279f2d57bec18d9f parent 77f3276967897bd8e37befd299a225f1a186ee4e Author: vaplv <vaplv@free.fr> Date: Wed, 2 Jun 2021 15:58:57 +0200 Detect the supported SIMD instruction sets Diffstat:
| M | cmake/CMakeLists.txt | | | 41 | ++++++++++++++++++++++++++++++++++++----- |
| M | cmake/RSIMDConfig.cmake.in | | | 34 | ++++++++++++++++++++-------------- |
2 files changed, 56 insertions(+), 19 deletions(-)
diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt @@ -31,22 +31,53 @@ find_package(PkgConfig REQUIRED) find_package(Sleef REQUIRED) include_directories(${RSys_INCLUDE_DIR} ${Sleef_INCLUDE_DIR}) +include(CheckCCompilerFlag) set(CMAKE_MODULE_PATH ${RCMAKE_SOURCE_DIR}) include(rcmake) ################################################################################ # Check compiler features ################################################################################ +if(NOT CMAKE_SYSTEM_NAME MATCHES "Linux" +OR NOT CMAKE_COMPILER_IS_GNUCC) + message(STATUS ${CMAKE_SYSTEM_NAME}) + message(FATAL_ERROR "Unsupported platform") +endif() + +execute_process(COMMAND cat "/proc/cpuinfo" + OUTPUT_VARIABLE CPUINFO_OUT + ERROR_VARIABLE CPUINFO_ERR + RESULT_VARIABLE CPUINFO_RES) +if(NOT CPUINFO_RES EQUAL 0) + message(FATAL_ERROR "${CPUINFO_ERR}") +endif() + +string(REGEX MATCH "[ \t\r\n]+sse2[ \t\r\n]+" SSE2 ${CPUINFO_OUT}) +string(REGEX MATCH "[ \t\r\n]+sse4_1[ \t\r\n]+" SSE4_1 ${CPUINFO_OUT}) +string(REGEX MATCH "[ \t\r\n]+avx[ \t\r\n]+" AVX ${CPUINFO_OUT}) +string(REGEX MATCH "[ \t\r\n]+fma[ \t\r\n]+" FMA ${CPUINFO_OUT}) -if(CMAKE_COMPILER_IS_GNUCC) - include(CheckCCompilerFlag) +if(SSE2) + unset(SSE2) CHECK_C_COMPILER_FLAG("-msse2" SSE2) - if(NOT SSE2) - message(FATAL_ERROR "The SSE2 instruction set must be supported.") - endif() + message(STATUS "Use the SSE2 instruction set ") +else() + message(FATAL_ERROR "The SSE2 instruction set must be supported.") +endif() +if(SSE4_1) + unset(SSE4_1) CHECK_C_COMPILER_FLAG("-msse4.1" SSE4_1) + message(STATUS "Use the SSE4.1 instruction set") +endif() +if(AVX) + unset(AVX) CHECK_C_COMPILER_FLAG("-mavx" AVX) + message(STATUS "Use the AVX instruction set") +endif() +if(FMA) + unset(FMA) CHECK_C_COMPILER_FLAG("-mfma" FMA) + message(STATUS "Use the FMA instruction set") endif() ################################################################################ diff --git a/cmake/RSIMDConfig.cmake.in b/cmake/RSIMDConfig.cmake.in @@ -26,23 +26,29 @@ set(RSIMD_FMA @RSIMD_FMA@) # Check compiler features if(CMAKE_COMPILER_IS_GNUCC) include(CheckCCompilerFlag) - CHECK_C_COMPILER_FLAG("-msse2" SSE2) - CHECK_C_COMPILER_FLAG("-msse4.1" SSE4_1) - CHECK_C_COMPILER_FLAG("-mavx" AVX) - CHECK_C_COMPILER_FLAG("-mfma" FMA) - - if(SSE2 AND RSIMD_SSE2) - list(APPEND _compile_flags -msse2) + if(RSIMD_SSE2) + CHECK_C_COMPILER_FLAG("-msse2" SSE2) + if(SSE2) + list(APPEND _compile_flags -msse2) + endif() endif() - if(SSE4_1 AND RSIMD_SSE4_1) - list(APPEND _compile_flags -msse4.1) + if(RSIMD_SSE4_1) + CHECK_C_COMPILER_FLAG("-msse4.1" SSE4_1) + if(SSE4_1) + list(APPEND _compile_flags -msse4.1) + endif() endif() - if(AVX AND RSIMD_AVX) - list(APPEND _compile_flags -mavx) + if(RSIMD_AVX) + CHECK_C_COMPILER_FLAG("-mavx" AVX) + if(AVX) + list(APPEND _compile_flags -mavx) + endif() endif() - if(FMA AND RSIMD_FMA) - set(RSIMD_FMA ON) - list(APPEND _compile_flags -mfma) + if(RSIMD_FMA) + CHECK_C_COMPILER_FLAG("-mfma" FMA) + if(FMA) + list(APPEND _compile_flags -mfma) + endif() endif() endif()