|
1 |
| -/////////////////////////////////////////////////////////////////////// |
2 |
| -// 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. |
16 |
| -/////////////////////////////////////////////////////////////////////// |
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 |
| -bool SIMDDetect::avx2_available_; |
41 |
| -bool SIMDDetect::avx512F_available_; |
42 |
| -bool SIMDDetect::avx512BW_available_; |
43 |
| -// If true, then SSe4.1 has been detected. |
44 |
| -bool SIMDDetect::sse_available_; |
45 |
| - |
46 |
| -// Constructor. |
47 |
| -// Tests the architecture in a system-dependent way to detect AVX, SSE and |
48 |
| -// any other available SIMD equipment. |
49 |
| -// __GNUC__ is also defined by compilers that include GNU extensions such as |
50 |
| -// clang. |
51 |
| -SIMDDetect::SIMDDetect() { |
52 |
| -#if defined(X86_BUILD) |
53 |
| -#if defined(__GNUC__) |
54 |
| - unsigned int eax, ebx, ecx, edx; |
55 |
| - if (__get_cpuid(1, &eax, &ebx, &ecx, &edx) != 0) { |
56 |
| - // Note that these tests all use hex because the older compilers don't have |
57 |
| - // the newer flags. |
58 |
| - sse_available_ = (ecx & 0x00080000) != 0; |
59 |
| - avx_available_ = (ecx & 0x10000000) != 0; |
60 |
| - if (avx_available_) { |
61 |
| - // There is supposed to be a __get_cpuid_count function, but this is all |
62 |
| - // there is in my cpuid.h. It is a macro for an asm statement and cannot |
63 |
| - // be used inside an if. |
64 |
| - __cpuid_count(7, 0, eax, ebx, ecx, edx); |
65 |
| - avx2_available_ = (ebx & 0x00000020) != 0; |
66 |
| - avx512F_available_ = (ebx & 0x00010000) != 0; |
67 |
| - avx512BW_available_ = (ebx & 0x40000000) != 0; |
68 |
| - } |
69 |
| - } |
70 |
| -#elif defined(_WIN32) |
71 |
| - int cpuInfo[4]; |
72 |
| - __cpuid(cpuInfo, 0); |
73 |
| - if (cpuInfo[0] >= 1) { |
74 |
| - __cpuid(cpuInfo, 1); |
75 |
| - sse_available_ = (cpuInfo[2] & 0x00080000) != 0; |
76 |
| - avx_available_ = (cpuInfo[2] & 0x10000000) != 0; |
77 |
| - } |
78 |
| -#else |
79 |
| -#error "I don't know how to test for SIMD with this compiler" |
80 |
| -#endif |
81 |
| -#endif // X86_BUILD |
82 |
| -} |
| 1 | +/////////////////////////////////////////////////////////////////////// |
| 2 | +// 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. |
| 16 | +/////////////////////////////////////////////////////////////////////// |
| 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 | +bool SIMDDetect::avx2_available_; |
| 41 | +bool SIMDDetect::avx512F_available_; |
| 42 | +bool SIMDDetect::avx512BW_available_; |
| 43 | +// If true, then SSe4.1 has been detected. |
| 44 | +bool SIMDDetect::sse_available_; |
| 45 | + |
| 46 | +// Constructor. |
| 47 | +// Tests the architecture in a system-dependent way to detect AVX, SSE and |
| 48 | +// any other available SIMD equipment. |
| 49 | +// __GNUC__ is also defined by compilers that include GNU extensions such as |
| 50 | +// clang. |
| 51 | +SIMDDetect::SIMDDetect() { |
| 52 | +#if defined(X86_BUILD) |
| 53 | +#if defined(__GNUC__) |
| 54 | + unsigned int eax, ebx, ecx, edx; |
| 55 | + if (__get_cpuid(1, &eax, &ebx, &ecx, &edx) != 0) { |
| 56 | + // Note that these tests all use hex because the older compilers don't have |
| 57 | + // the newer flags. |
| 58 | + sse_available_ = (ecx & 0x00080000) != 0; |
| 59 | + avx_available_ = (ecx & 0x10000000) != 0; |
| 60 | + if (avx_available_) { |
| 61 | + // There is supposed to be a __get_cpuid_count function, but this is all |
| 62 | + // there is in my cpuid.h. It is a macro for an asm statement and cannot |
| 63 | + // be used inside an if. |
| 64 | + __cpuid_count(7, 0, eax, ebx, ecx, edx); |
| 65 | + avx2_available_ = (ebx & 0x00000020) != 0; |
| 66 | + avx512F_available_ = (ebx & 0x00010000) != 0; |
| 67 | + avx512BW_available_ = (ebx & 0x40000000) != 0; |
| 68 | + } |
| 69 | + } |
| 70 | +#elif defined(_WIN32) |
| 71 | + int cpuInfo[4]; |
| 72 | + __cpuid(cpuInfo, 0); |
| 73 | + if (cpuInfo[0] >= 1) { |
| 74 | + __cpuid(cpuInfo, 1); |
| 75 | + sse_available_ = (cpuInfo[2] & 0x00080000) != 0; |
| 76 | + avx_available_ = (cpuInfo[2] & 0x10000000) != 0; |
| 77 | + } |
| 78 | +#else |
| 79 | +#error "I don't know how to test for SIMD with this compiler" |
| 80 | +#endif |
| 81 | +#endif // X86_BUILD |
| 82 | +} |
0 commit comments