Skip to content

Commit

Permalink
CMake: fix AVX detection on Windows and other SSE/AVX fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
rouault committed Oct 29, 2021
1 parent c50db0d commit 350d05e
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 17 deletions.
36 changes: 33 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -137,19 +137,49 @@ if ("${CMAKE_SYSTEM_PROCESSOR}" MATCHES "(x86|AMD64)")
check_compiler_machine_option(flag SSE)
if (NOT ${flag} EQUAL "")
set(HAVE_SSE_AT_COMPILE_TIME 1)
set(GDAL_SSE_FLAG "-msse")
if (NOT ${flag} EQUAL " ")
set(GDAL_SSE_FLAG ${flag})
endif ()
endif ()

check_compiler_machine_option(flag SSE2)
if (NOT ${flag} EQUAL "")
set(HAVE_SSE2_AT_COMPILE_TIME 1)
if (NOT ${flag} EQUAL " ")
set(GDAL_SSE2_FLAG ${flag})
endif ()
endif ()

check_compiler_machine_option(flag SSSE3)
if (NOT ${flag} EQUAL "")
set(HAVE_SSSE3_AT_COMPILE_TIME 1)
set(GDAL_SSSE3_FLAG "-mssse3")
if (NOT ${flag} EQUAL " ")
set(GDAL_SSSE3_FLAG ${flag})
endif ()
endif ()

check_compiler_machine_option(flag SSE4_1)
if (NOT ${flag} EQUAL "")
set(HAVE_SSE41_AT_COMPILE_TIME 1)
if (NOT ${flag} EQUAL " ")
set(GDAL_SSE41_FLAG ${flag})
endif ()
endif ()

check_compiler_machine_option(flag AVX)
if (NOT ${flag} EQUAL "")
set(HAVE_AVX_AT_COMPILE_TIME 1)
set(GDAL_AVX_FLAG "-mavx")
if (NOT ${flag} EQUAL " ")
set(GDAL_AVX_FLAG ${flag})
endif ()
endif ()

check_compiler_machine_option(flag AVX2)
if (NOT ${flag} EQUAL "")
set(HAVE_AVX2_AT_COMPILE_TIME 1)
if (NOT ${flag} EQUAL " ")
set(GDAL_AVX2_FLAG ${flag})
endif ()
endif ()

endif ()
Expand Down
12 changes: 9 additions & 3 deletions alg/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,16 @@ endif ()
if (HAVE_AVX_AT_COMPILE_TIME)
target_sources(alg PRIVATE gdalgridavx.cpp)
target_compile_definitions(alg PRIVATE -DHAVE_AVX_AT_COMPILE_TIME)
set_property(
get_property(
_compile_flags
SOURCE gdalgridavx.cpp
APPEND
PROPERTY COMPILE_FLAGS ${GDAL_AVX_FLAG})
PROPERTY COMPILE_FLAGS)
if ("${_compile_flags}" STREQUAL "" OR "${_compile_flags}" STREQUAL " ")
set(_compile_flags ${GDAL_AVX_FLAG})
else ()
list(APPEND _compile_flags ${GDAL_AVX_FLAG})
endif ()
set_property(SOURCE gdalgridavx.cpp PROPERTY COMPILE_FLAGS ${_compile_flags})
endif ()

include(TargetPublicHeader)
Expand Down
8 changes: 4 additions & 4 deletions autotest/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ register_test(
if ("${CMAKE_SYSTEM_PROCESSOR}" MATCHES "(x86_64|AMD64)")
gdal_test_target(testsse2 testsse.cpp)
gdal_test_target(testsse2_emulation testsse.cpp)
target_compile_options(testsse2 PRIVATE ${GDAL_SSE2_FLAG})
target_compile_definitions(testsse2_emulation PRIVATE -DUSE_SSE2_EMULATION -DNO_WARN_USE_SSE2_EMULATION)
target_compile_options(testsse2_emulation PRIVATE -msse2)
register_test(test-sse2 testsse2)
register_test(test-sse2-emulation testsse2_emulation)
endif ()
Expand All @@ -197,9 +197,9 @@ if (HAVE_AVX_AT_COMPILE_TIME)
gdal_test_target(testssse3 testsse.cpp)
gdal_test_target(testsse4_1 testsse.cpp)
gdal_test_target(testavx2 testsse.cpp)
target_compile_options(testssse3 PRIVATE -mssse3)
target_compile_options(testsse4_1 PRIVATE -mssse3 -msse4.1)
target_compile_options(testavx2 PRIVATE -mavx2)
target_compile_options(testssse3 PRIVATE ${GDAL_SSSE3_FLAG})
target_compile_options(testsse4_1 PRIVATE ${GDAL_SSSE3_FLAG} ${GDAL_SSSE41_FLAG})
target_compile_options(testavx2 PRIVATE ${GDAL_AVX2_FLAG})
register_test_as_custom_target(test-ssse3 testssse3)
register_test_as_custom_target(test-sse41 testsse4_1)
register_test_as_custom_target(test-avx2 testavx2)
Expand Down
16 changes: 9 additions & 7 deletions cmake/helpers/CheckCompilerMachineOption.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,30 @@ include(CheckIncludeFileCXX)
include(CheckIncludeFile)

function(check_compiler_machine_option outvar feature)
macro(__check_compiler_flag _flag _result)
macro(__check_compiler_flag _flag _result)
if(CMAKE_CXX_COMPILER_LOADED)
check_cxx_compiler_flag("${_flag}" ${_result})
elseif(CMAKE_C_COMPILER_LOADED)
check_c_compiler_flag("${_flag}" ${_result})
endif()
endmacro()

set(_FLAGS)
if("${CMAKE_SYSTEM_PROCESSOR}" MATCHES "(x86|AMD64)")
if(MSVC)
if(MSVC AND (${feature} MATCHES "SSE"))
# SSE2 and SSE are default on
set(_FLAGS " ")
elseif(MSVC)
# Only Visual Studio 2017 version 15.3 / Visual C++ 19.11 & up have support for AVX-512.
# https://blogs.msdn.microsoft.com/vcblog/2017/07/11/microsoft-visual-studio-2017-supports-intel-avx-512/
# SSE2 and SSE are default on
set(MSVC_FLAG_MAP AVX512 "-arch:AVX512" AVX2 "-arch:AVX2" AVX "-arch:AVX")
list(FIND MSVC_FLAG_MAP ${feature} _found)
if(_found GREATER -1)
math(EXPR index "${_found}+1")
list(GET MSVC_FLAG_MAP ${index} _flag)
__check_compiler_flag("${_flag}" test_${feature})
if(test_${feature})
set(_FLAGS "${flag}")
set(_FLAGS "${_flag}")
endif()
endif()
elseif(CMAKE_CXX_COMPILER MATCHES "/(icpc|icc)$") # ICC (on Linux)
Expand All @@ -62,6 +65,7 @@ function(check_compiler_machine_option outvar feature)
endif()
else() # not MSVC and not ICC => GCC, Clang, Open64
string(TOLOWER ${feature} _flag)
string(REPLACE "_" "." _flag "${_flag}")
__check_compiler_flag("-m${_flag}" test_${_flag})
if(test_${_flag})
set(header_table "sse3" "pmmintrin.h" "ssse3" "tmmintrin.h" "sse4.1" "smmintrin.h"
Expand All @@ -87,9 +91,7 @@ function(check_compiler_machine_option outvar feature)
endif()
endif()
endif()
set(${outvar} ${_FLAGS} PARENT_SCOPE)
elseif("${CMAKE_SYSTEM_PROCESSOR}" MATCHES "(ARM|aarch64)")
set(_FLAGS)
if(MSVC)
# TODO implement me
elseif(CMAKE_CXX_COMPILER MATCHES "/(icpc|icc)$") # ICC (on Linux)
Expand All @@ -101,6 +103,6 @@ function(check_compiler_machine_option outvar feature)
set(_FLAGS "-m${_flag}")
endif()
endif()
set(${outvar} "${_FLAGS}" PARENT_SCOPE)
endif()
set(${outvar} "${_FLAGS}" PARENT_SCOPE)
endfunction()

0 comments on commit 350d05e

Please sign in to comment.