tesseract  4.0.0-1-g2a2b
simddetect.cpp
Go to the documentation of this file.
1 // File: simddetect.cpp
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 
20 #undef X86_BUILD
21 #if defined(__x86_64__) || defined(__i386__) || defined(_WIN32)
22 #if !defined(ANDROID_BUILD)
23 #define X86_BUILD 1
24 #endif // !ANDROID_BUILD
25 #endif // x86 target
26 
27 #if defined(X86_BUILD)
28 #if defined(__GNUC__)
29 #include <cpuid.h>
30 #elif defined(_WIN32)
31 #include <intrin.h>
32 #endif
33 #endif
34 
35 SIMDDetect SIMDDetect::detector;
36 
37 // If true, then AVX has been detected.
38 bool SIMDDetect::avx_available_;
39 bool SIMDDetect::avx2_available_;
40 bool SIMDDetect::avx512F_available_;
41 bool SIMDDetect::avx512BW_available_;
42 // If true, then SSe4.1 has been detected.
43 bool SIMDDetect::sse_available_;
44 
45 // Constructor.
46 // Tests the architecture in a system-dependent way to detect AVX, SSE and
47 // any other available SIMD equipment.
48 // __GNUC__ is also defined by compilers that include GNU extensions such as
49 // clang.
50 SIMDDetect::SIMDDetect() {
51 #if defined(X86_BUILD)
52 #if defined(__GNUC__)
53  unsigned int eax, ebx, ecx, edx;
54  if (__get_cpuid(1, &eax, &ebx, &ecx, &edx) != 0) {
55  // Note that these tests all use hex because the older compilers don't have
56  // the newer flags.
57  sse_available_ = (ecx & 0x00080000) != 0;
58  avx_available_ = (ecx & 0x10000000) != 0;
59  if (avx_available_) {
60  // There is supposed to be a __get_cpuid_count function, but this is all
61  // there is in my cpuid.h. It is a macro for an asm statement and cannot
62  // be used inside an if.
63  __cpuid_count(7, 0, eax, ebx, ecx, edx);
64  avx2_available_ = (ebx & 0x00000020) != 0;
65  avx512F_available_ = (ebx & 0x00010000) != 0;
66  avx512BW_available_ = (ebx & 0x40000000) != 0;
67  }
68  }
69 #elif defined(_WIN32)
70  int cpuInfo[4];
71  __cpuid(cpuInfo, 0);
72  if (cpuInfo[0] >= 1) {
73  __cpuid(cpuInfo, 1);
74  sse_available_ = (cpuInfo[2] & 0x00080000) != 0;
75  avx_available_ = (cpuInfo[2] & 0x10000000) != 0;
76  }
77 #else
78 #error "I don't know how to test for SIMD with this compiler"
79 #endif
80 #endif // X86_BUILD
81 }