CMake: add option to enable compiler simd flags
@@ -80,6 +80,27 @@ if(CMAKE_COMPILER_IS_GNUCXX)
#symbols are only exported from libraries/modules explicitly
add_compile_options(-fvisibility=hidden)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility-inlines-hidden")
+
+ SET(ENABLE_SIMD_FLAGS "native" CACHE STRING "Set compiler SIMD flags")
+ SET_PROPERTY(CACHE ENABLE_SIMD_FLAGS PROPERTY STRINGS none native SSE3 SSE4.2 AVX AVX2)
+
+ if(${ENABLE_SIMD_FLAGS} MATCHES "native")
+ add_definitions(-march=native -mfpmath=both)
+ message(STATUS "Enabling native optimizations")
+ elseif(${ENABLE_SIMD_FLAGS} MATCHES "AVX2")
+ add_definitions(-mmmx -msse -msse2 -msse3 -mssse3 -msse4.1 -msse4.2 -mpopcnt -mavx -mfma -mavx2 -mfpmath=both)
+ message(STATUS "Enabling AVX2 instructions")
+ elseif(${ENABLE_SIMD_FLAGS} MATCHES "AVX")
+ add_definitions(-mmmx -msse -msse2 -msse3 -mssse3 -msse4.1 -msse4.2 -mpopcnt -mavx -mfpmath=both)
+ message(STATUS "Enabling AVX instructions")
+ elseif(${ENABLE_SIMD_FLAGS} MATCHES "SSE4.2")
+ add_definitions(-mmmx -msse -msse2 -msse3 -mssse3 -msse4.1 -msse4.2 -mpopcnt -mfpmath=both)
+ message(STATUS "Enabling SSE4.2 instructions")
+ elseif(${ENABLE_SIMD_FLAGS} MATCHES "SSE3")
+ add_definitions(-mmmx -msse -msse2 -msse3 -mfpmath=both)
+ message(STATUS "Enabling SSE3 instructions")
+ endif()
+
endif()
if (MSVC)
@@ -87,6 +108,20 @@ if (MSVC)
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
set( MSVC_MULTITHREAD_COMPILE_FLAGS "/MP")
set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${MSVC_MULTITHREAD_COMPILE_FLAGS}" )
+
+ SET(ENABLE_SIMD_FLAGS "SSE2" CACHE STRING "Set compiler SIMD flags")
+ SET_PROPERTY(CACHE ENABLE_SIMD_FLAGS PROPERTY STRINGS none SSE2 AVX AVX2)
+
+ if(${ENABLE_SIMD_FLAGS} MATCHES "AVX2")
+ add_definitions(/arch:AVX2)
+ message(STATUS "Enabling AVX2 instructions")
+ elseif(${ENABLE_SIMD_FLAGS} MATCHES "AVX")
+ add_definitions(/arch:AVX)
+ message(STATUS "Enabling AVX instructions")
+ elseif(${ENABLE_SIMD_FLAGS} MATCHES "SSE2")
+ add_definitions(/arch:SSE2)
+ message(STATUS "Enabling SSE2 instructions")
+ endif()
endif(MSVC)
if (APPLE)
