From b2a0262c5939eee9ff84642329ae8bbe5262f46f Mon Sep 17 00:00:00 2001 From: Stefan Weil Date: Tue, 27 Dec 2016 10:21:50 +0100 Subject: [PATCH 1/2] lstm: Update AVX / SSE support * Fix compiler warning (see below) * Use Linux code for Mingw-w64, too * Simplify conditional code by using X86_BUILD instead of NONX86_BUILD * Remove unneeded call of __get_cpuid_max (already called by __get_cpuid) * Remove unneeded #undef statement gcc report: lstm/weightmatrix.cpp: In static member function 'static double tesseract::WeightMatrix::DotProduct(const double*, const double*, int)': weightmatrix.cpp:67:29: warning: 'ecx' may be used uninitialized in this function [-Wmaybe-uninitialized] avx_available_ = (ecx & 0x10000000) != 0; ^ lstm/weightmatrix.cpp:64:30: note: 'ecx' was declared here unsigned int eax, ebx, ecx, edx; ^ Signed-off-by: Stefan Weil --- lstm/weightmatrix.cpp | 41 +++++++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/lstm/weightmatrix.cpp b/lstm/weightmatrix.cpp index e0bed08ce..8c7aa4645 100644 --- a/lstm/weightmatrix.cpp +++ b/lstm/weightmatrix.cpp @@ -18,13 +18,18 @@ #include "weightmatrix.h" -#undef NONX86_BUILD -#if !defined(__x86_64__) && !defined(__i386__) && !defined(_WIN32) || defined(ANDROID_BUILD) -#define NONX86_BUILD 1 -#endif +#undef X86_BUILD +#if defined(__x86_64__) || defined(__i386__) || defined(_WIN32) +# if !defined(ANDROID_BUILD) +# define X86_BUILD 1 +# endif // !ANDROID_BUILD +#endif // x86 target -#if defined(__linux__) && !defined(NONX86_BUILD) -#include +#if defined(X86_BUILD) +# if defined(__linux__) || defined(__MINGW32__) +# include +# elif defined(_WIN32) +# endif #endif #include "dotproductavx.h" #include "dotproductsse.h" @@ -58,18 +63,20 @@ class SIMDDetect { // any other available SIMD equipment. void TestArchitecture() { SVAutoLock lock(&arch_mutex_); - if (arch_tested_) return; -#if defined(__linux__) && !defined(NONX86_BUILD) - if (__get_cpuid_max(0, NULL) >= 1) { + if (!arch_tested_) { +#if defined(X86_BUILD) +# if defined(__linux__) || defined(__MINGW32__) unsigned int eax, ebx, ecx, edx; - __get_cpuid(1, &eax, &ebx, &ecx, &edx); - sse_available_ = (ecx & 0x00080000) != 0; - avx_available_ = (ecx & 0x10000000) != 0; + if (__get_cpuid(1, &eax, &ebx, &ecx, &edx) != 0) { + sse_available_ = (ecx & 0x00080000) != 0; + avx_available_ = (ecx & 0x10000000) != 0; + } +# endif + if (avx_available_) tprintf("Found AVX\n"); + if (sse_available_) tprintf("Found SSE\n"); +#endif // X86_BUILD + arch_tested_ = true; } -#endif - if (avx_available_) tprintf("Found AVX\n"); - if (sse_available_) tprintf("Found SSE\n"); - arch_tested_ = true; } private: @@ -439,5 +446,3 @@ void WeightMatrix::MatrixDotVectorInternal(const GENERIC_2D_ARRAY& w, } } // namespace tesseract. - -#undef NONX86_BUILD From 5cfe0c700c27dd5834de5df09c8608cdcd976d1c Mon Sep 17 00:00:00 2001 From: Stefan Weil Date: Tue, 27 Dec 2016 11:14:15 +0100 Subject: [PATCH 2/2] lstm: Add AVX / SSE support for Windows Signed-off-by: Stefan Weil --- lstm/weightmatrix.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lstm/weightmatrix.cpp b/lstm/weightmatrix.cpp index 8c7aa4645..84dd9b77a 100644 --- a/lstm/weightmatrix.cpp +++ b/lstm/weightmatrix.cpp @@ -29,6 +29,7 @@ # if defined(__linux__) || defined(__MINGW32__) # include # elif defined(_WIN32) +# include # endif #endif #include "dotproductavx.h" @@ -71,6 +72,14 @@ class SIMDDetect { sse_available_ = (ecx & 0x00080000) != 0; avx_available_ = (ecx & 0x10000000) != 0; } +# elif defined(_WIN32) + int cpuInfo[4]; + __cpuid(cpuInfo, 0); + if (cpuInfo[0] >= 1) { + __cpuid(cpuInfo, 1); + sse_available_ = (cpuInfo[2] & 0x00080000) != 0; + avx_available_ = (cpuInfo[2] & 0x10000000) != 0; + } # endif if (avx_available_) tprintf("Found AVX\n"); if (sse_available_) tprintf("Found SSE\n");