2018-10-05 11:32:39 +02:00
|
|
|
# Check for SIMD extensions.
|
2017-10-07 23:15:11 +02:00
|
|
|
include(CMakePushCheckState)
|
2016-07-08 01:57:02 +02:00
|
|
|
|
2017-02-03 16:47:59 +01:00
|
|
|
function(webp_check_compiler_flag WEBP_SIMD_FLAG ENABLE_SIMD)
|
|
|
|
if(NOT ENABLE_SIMD)
|
|
|
|
message(STATUS "Disabling ${WEBP_SIMD_FLAG} optimization.")
|
|
|
|
set(WEBP_HAVE_${WEBP_SIMD_FLAG} 0 PARENT_SCOPE)
|
|
|
|
return()
|
|
|
|
endif()
|
2016-08-04 04:55:18 +02:00
|
|
|
unset(WEBP_HAVE_FLAG_${WEBP_SIMD_FLAG} CACHE)
|
2017-10-07 23:15:11 +02:00
|
|
|
cmake_push_check_state()
|
|
|
|
set(CMAKE_REQUIRED_INCLUDES ${CMAKE_CURRENT_SOURCE_DIR})
|
2016-07-08 01:57:02 +02:00
|
|
|
check_c_source_compiles("
|
|
|
|
#include \"${CMAKE_CURRENT_LIST_DIR}/../src/dsp/dsp.h\"
|
|
|
|
int main(void) {
|
|
|
|
#if !defined(WEBP_USE_${WEBP_SIMD_FLAG})
|
|
|
|
this is not valid code
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
2018-10-05 11:32:39 +02:00
|
|
|
" WEBP_HAVE_FLAG_${WEBP_SIMD_FLAG})
|
2017-10-07 23:15:11 +02:00
|
|
|
cmake_pop_check_state()
|
2016-08-04 04:55:18 +02:00
|
|
|
if(WEBP_HAVE_FLAG_${WEBP_SIMD_FLAG})
|
2016-07-08 01:57:02 +02:00
|
|
|
set(WEBP_HAVE_${WEBP_SIMD_FLAG} 1 PARENT_SCOPE)
|
|
|
|
else()
|
|
|
|
set(WEBP_HAVE_${WEBP_SIMD_FLAG} 0 PARENT_SCOPE)
|
|
|
|
endif()
|
|
|
|
endfunction()
|
|
|
|
|
|
|
|
# those are included in the names of WEBP_USE_* in c++ code.
|
2018-10-04 17:25:42 +02:00
|
|
|
set(WEBP_SIMD_FLAGS "SSE41;SSE2;MIPS32;MIPS_DSP_R2;NEON;MSA")
|
2018-10-05 11:32:39 +02:00
|
|
|
set(WEBP_SIMD_FILE_EXTENSIONS
|
2018-10-04 17:25:42 +02:00
|
|
|
"_sse41.c;_sse2.c;_mips32.c;_mips_dsp_r2.c;_neon.c;_msa.c")
|
2016-07-07 20:21:51 +02:00
|
|
|
if(MSVC)
|
2018-10-04 17:25:42 +02:00
|
|
|
# MSVC does not have a SSE4 flag but AVX support implies SSE4 support.
|
|
|
|
set(SIMD_ENABLE_FLAGS "/arch:AVX;/arch:SSE2;;;;")
|
2016-07-07 20:21:51 +02:00
|
|
|
set(SIMD_DISABLE_FLAGS)
|
|
|
|
else()
|
2018-10-05 11:32:39 +02:00
|
|
|
set(SIMD_ENABLE_FLAGS
|
2018-10-04 17:25:42 +02:00
|
|
|
"-msse4.1;-msse2;-mips32;-mdspr2;-mfpu=neon;-mmsa")
|
2018-10-05 11:32:39 +02:00
|
|
|
set(SIMD_DISABLE_FLAGS
|
2018-10-04 17:25:42 +02:00
|
|
|
"-mno-sse4.1;-mno-sse2;;-mno-dspr2;;-mno-msa")
|
2016-07-07 20:21:51 +02:00
|
|
|
endif()
|
|
|
|
|
|
|
|
set(WEBP_SIMD_FILES_TO_NOT_INCLUDE)
|
|
|
|
set(WEBP_SIMD_FILES_TO_INCLUDE)
|
|
|
|
set(WEBP_SIMD_FLAGS_TO_INCLUDE)
|
|
|
|
|
2016-07-08 01:57:02 +02:00
|
|
|
if(${ANDROID})
|
|
|
|
if(${ANDROID_ABI} STREQUAL "armeabi-v7a")
|
2018-10-05 11:32:39 +02:00
|
|
|
# This is because Android studio uses the configuration "-march=armv7-a
|
|
|
|
# -mfloat-abi=softfp -mfpu=vfpv3-d16" that does not trigger neon
|
|
|
|
# optimizations but should (as this configuration does not exist anymore).
|
2016-07-08 01:57:02 +02:00
|
|
|
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mfpu=neon ")
|
|
|
|
endif()
|
|
|
|
endif()
|
|
|
|
|
2016-07-07 20:21:51 +02:00
|
|
|
list(LENGTH WEBP_SIMD_FLAGS WEBP_SIMD_FLAGS_LENGTH)
|
|
|
|
math(EXPR WEBP_SIMD_FLAGS_RANGE "${WEBP_SIMD_FLAGS_LENGTH} - 1")
|
2018-10-04 17:25:42 +02:00
|
|
|
unset(HIGHEST_SSE_FLAG)
|
2016-07-07 20:21:51 +02:00
|
|
|
|
|
|
|
foreach(I_SIMD RANGE ${WEBP_SIMD_FLAGS_RANGE})
|
|
|
|
list(GET WEBP_SIMD_FLAGS ${I_SIMD} WEBP_SIMD_FLAG)
|
|
|
|
|
2016-07-08 01:57:02 +02:00
|
|
|
# First try with no extra flag added as the compiler might have default flags
|
|
|
|
# (especially on Android).
|
|
|
|
unset(WEBP_HAVE_${WEBP_SIMD_FLAG} CACHE)
|
2017-07-07 10:33:48 +02:00
|
|
|
cmake_push_check_state()
|
2016-07-08 01:57:02 +02:00
|
|
|
set(CMAKE_REQUIRED_FLAGS)
|
2017-02-03 16:47:59 +01:00
|
|
|
webp_check_compiler_flag(${WEBP_SIMD_FLAG} ${WEBP_ENABLE_SIMD})
|
2016-07-08 01:57:02 +02:00
|
|
|
if(NOT WEBP_HAVE_${WEBP_SIMD_FLAG})
|
|
|
|
list(GET SIMD_ENABLE_FLAGS ${I_SIMD} SIMD_COMPILE_FLAG)
|
|
|
|
set(CMAKE_REQUIRED_FLAGS ${SIMD_COMPILE_FLAG})
|
2017-02-03 16:47:59 +01:00
|
|
|
webp_check_compiler_flag(${WEBP_SIMD_FLAG} ${WEBP_ENABLE_SIMD})
|
2016-07-08 01:57:02 +02:00
|
|
|
else()
|
2018-10-04 17:25:42 +02:00
|
|
|
if(MSVC)
|
|
|
|
list(GET SIMD_ENABLE_FLAGS ${I_SIMD} SIMD_COMPILE_FLAG)
|
|
|
|
else()
|
|
|
|
set(SIMD_COMPILE_FLAG " ")
|
|
|
|
endif()
|
2016-07-08 01:57:02 +02:00
|
|
|
endif()
|
2016-07-07 20:21:51 +02:00
|
|
|
# Check which files we should include or not.
|
|
|
|
list(GET WEBP_SIMD_FILE_EXTENSIONS ${I_SIMD} WEBP_SIMD_FILE_EXTENSION)
|
|
|
|
file(GLOB SIMD_FILES "${CMAKE_CURRENT_LIST_DIR}/../"
|
2018-10-05 11:32:39 +02:00
|
|
|
"src/dsp/*${WEBP_SIMD_FILE_EXTENSION}")
|
2016-07-07 20:21:51 +02:00
|
|
|
if(WEBP_HAVE_${WEBP_SIMD_FLAG})
|
2018-10-04 17:25:42 +02:00
|
|
|
if(${I_SIMD} LESS 2 AND NOT HIGHEST_SSE_FLAG)
|
|
|
|
set(HIGHEST_SSE_FLAG ${SIMD_COMPILE_FLAG})
|
|
|
|
endif()
|
2016-07-07 20:21:51 +02:00
|
|
|
# Memorize the file and flags.
|
|
|
|
foreach(FILE ${SIMD_FILES})
|
|
|
|
list(APPEND WEBP_SIMD_FILES_TO_INCLUDE ${FILE})
|
2018-10-04 17:25:42 +02:00
|
|
|
if(${I_SIMD} LESS 2)
|
|
|
|
list(APPEND WEBP_SIMD_FLAGS_TO_INCLUDE ${HIGHEST_SSE_FLAG})
|
|
|
|
else()
|
|
|
|
list(APPEND WEBP_SIMD_FLAGS_TO_INCLUDE ${SIMD_COMPILE_FLAG})
|
|
|
|
endif()
|
2016-07-07 20:21:51 +02:00
|
|
|
endforeach()
|
|
|
|
else()
|
|
|
|
# Remove the file from the list.
|
|
|
|
foreach(FILE ${SIMD_FILES})
|
|
|
|
list(APPEND WEBP_SIMD_FILES_NOT_TO_INCLUDE ${FILE})
|
|
|
|
endforeach()
|
|
|
|
# Explicitly disable SIMD.
|
|
|
|
if(SIMD_DISABLE_FLAGS)
|
|
|
|
list(GET SIMD_DISABLE_FLAGS ${I_SIMD} SIMD_COMPILE_FLAG)
|
|
|
|
include(CheckCCompilerFlag)
|
2016-07-08 01:57:02 +02:00
|
|
|
if(SIMD_COMPILE_FLAG)
|
|
|
|
unset(HAS_COMPILE_FLAG CACHE)
|
|
|
|
check_c_compiler_flag(${SIMD_COMPILE_FLAG} HAS_COMPILE_FLAG)
|
2016-12-19 17:07:33 +01:00
|
|
|
if(HAS_COMPILE_FLAG)
|
|
|
|
# Do one more check for Clang to circumvent CMake issue 13194.
|
|
|
|
if(COMMAND check_compiler_flag_common_patterns)
|
|
|
|
# Only in CMake 3.0 and above.
|
|
|
|
check_compiler_flag_common_patterns(COMMON_PATTERNS)
|
|
|
|
else()
|
|
|
|
set(COMMON_PATTERNS)
|
|
|
|
endif()
|
|
|
|
set(CMAKE_REQUIRED_DEFINITIONS ${SIMD_COMPILE_FLAG})
|
2017-06-29 04:10:48 +02:00
|
|
|
check_c_source_compiles("int main(void) {return 0;}"
|
2018-10-05 11:32:39 +02:00
|
|
|
FLAG_${SIMD_COMPILE_FLAG}
|
|
|
|
FAIL_REGEX
|
|
|
|
"warning: argument unused during compilation:"
|
|
|
|
${COMMON_PATTERNS})
|
2017-06-29 04:10:48 +02:00
|
|
|
if(NOT FLAG_${SIMD_COMPILE_FLAG})
|
2016-12-19 17:07:33 +01:00
|
|
|
unset(HAS_COMPILE_FLAG CACHE)
|
|
|
|
endif()
|
|
|
|
endif()
|
2016-07-08 01:57:02 +02:00
|
|
|
if(HAS_COMPILE_FLAG)
|
|
|
|
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${SIMD_COMPILE_FLAG}")
|
|
|
|
endif()
|
2016-07-07 20:21:51 +02:00
|
|
|
endif()
|
|
|
|
endif()
|
|
|
|
endif()
|
2017-07-07 10:33:48 +02:00
|
|
|
cmake_pop_check_state()
|
2016-07-07 20:21:51 +02:00
|
|
|
endforeach()
|