tesseract  4.00.00dev
simddetect.cpp
Go to the documentation of this file.
1 // File: simddetect.h
3 // Description: Architecture detector.
4 // Author: Stefan Weil (based on code from Ray Smith)
5 //
6 // (C) Copyright 2014, Google Inc.
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
17 
18 #include "simddetect.h"
19 #include "tprintf.h"
20 
21 #undef X86_BUILD
22 #if defined(__x86_64__) || defined(__i386__) || defined(_WIN32)
23 #if !defined(ANDROID_BUILD)
24 #define X86_BUILD 1
25 #endif // !ANDROID_BUILD
26 #endif // x86 target
27 
28 #if defined(X86_BUILD)
29 #if defined(__GNUC__)
30 #include <cpuid.h>
31 #elif defined(_WIN32)
32 #include <intrin.h>
33 #endif
34 #endif
35 
36 SIMDDetect SIMDDetect::detector;
37 
38 // If true, then AVX has been detected.
39 bool SIMDDetect::avx_available_;
40 // If true, then SSe4.1 has been detected.
41 bool SIMDDetect::sse_available_;
42 
43 // Constructor.
44 // Tests the architecture in a system-dependent way to detect AVX, SSE and
45 // any other available SIMD equipment.
46 // __GNUC__ is also defined by compilers that include GNU extensions such as
47 // clang.
48 SIMDDetect::SIMDDetect() {
49 #if defined(X86_BUILD)
50 #if defined(__GNUC__)
51  unsigned int eax, ebx, ecx, edx;
52  if (__get_cpuid(1, &eax, &ebx, &ecx, &edx) != 0) {
53  sse_available_ = (ecx & 0x00080000) != 0;
54  avx_available_ = (ecx & 0x10000000) != 0;
55  }
56 #elif defined(_WIN32)
57  int cpuInfo[4];
58  __cpuid(cpuInfo, 0);
59  if (cpuInfo[0] >= 1) {
60  __cpuid(cpuInfo, 1);
61  sse_available_ = (cpuInfo[2] & 0x00080000) != 0;
62  avx_available_ = (cpuInfo[2] & 0x10000000) != 0;
63  }
64 #else
65 #error "I don't know how to test for SIMD with this compiler"
66 #endif
67 #endif // X86_BUILD
68 }