From e376ffc5fe9c87d2779bf839278e08c2c2dffe70 Mon Sep 17 00:00:00 2001 From: Egor Pugin Date: Thu, 23 Feb 2017 19:29:48 +0300 Subject: [PATCH] Fix avx on windows. --- CMakeLists.txt | 12 +++++++++++- arch/dotproductavx.cpp | 11 ++++++++++- cppan.yml | 16 +++++++++++----- 3 files changed, 32 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9c74d6090f..d227b7dc36 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -84,7 +84,6 @@ if (STATIC) endif() if (WIN32) - add_definitions(-D__SSE4_1__) if (MSVC) add_definitions(-D_CRT_SECURE_NO_WARNINGS) @@ -214,6 +213,17 @@ set(tesseract_src ${tesseract_src} api/pdfrenderer.cpp ) +if (WIN32) + set_source_files_properties( + ${CMAKE_CURRENT_SOURCE_DIR}/arch/dotproductsse.cpp + PROPERTIES COMPILE_DEFINITIONS __SSE4_1__) + if (MSVC) + set_source_files_properties( + ${CMAKE_CURRENT_SOURCE_DIR}/arch/dotproductavx.cpp + PROPERTIES COMPILE_FLAGS "/arch:AVX") + endif() +endif() + add_library (libtesseract ${LIBRARY_TYPE} ${tesseract_src} ${tesseract_hdr}) if (NOT STATIC) target_compile_definitions (libtesseract diff --git a/arch/dotproductavx.cpp b/arch/dotproductavx.cpp index 7ba784a7fc..9c84226439 100644 --- a/arch/dotproductavx.cpp +++ b/arch/dotproductavx.cpp @@ -90,8 +90,17 @@ double DotProductAVX(const double* u, const double* v, int n) { // _mm256_extract_f64 doesn't exist, but resist the temptation to use an sse // instruction, as that introduces a 70 cycle delay. All this casting is to // fool the instrinsics into thinking we are extracting the bottom int64. + auto cast_sum = _mm256_castpd_si256(sum); *(reinterpret_cast(&result)) = - _mm256_extract_epi64(_mm256_castpd_si256(sum), 0); +#ifndef _WIN32 + _mm256_extract_epi64(cast_sum, 0) +#else + // this is a very simple workaround that probably could be activated + // for all other platforms that do not have _mm256_extract_epi64 + // _mm256_extract_epi64(X, Y) == ((uint64_t*)&X)[Y] + ((uint64_t*)&cast_sum)[0] +#endif + ; while (offset < n) { result += u[offset] * v[offset]; ++offset; diff --git a/cppan.yml b/cppan.yml index b221ace1f4..dc0a1ffeda 100644 --- a/cppan.yml +++ b/cppan.yml @@ -108,13 +108,19 @@ projects: - _Bool pre_sources: | - # dummy config file - if (NOT EXISTS ${CMAKE_CURRENT_BINARY_DIR}/config_auto.h) - file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/config_auto.h) - endif() + file_write_once(${BDIR}/config_auto.h "") post_sources: | - if (NOT WIN32) + if (WIN32) + set_source_files_properties( + ${CMAKE_CURRENT_SOURCE_DIR}/arch/dotproductsse.cpp + PROPERTIES COMPILE_DEFINITIONS __SSE4_1__) + if (MSVC) + set_source_files_properties( + ${CMAKE_CURRENT_SOURCE_DIR}/arch/dotproductavx.cpp + PROPERTIES COMPILE_FLAGS "/arch:AVX") + endif() + else() remove_src_dir(vs2010/port/*) endif()