1 | /* |
2 | Copyright (c) 2005-2019 Intel Corporation |
3 | |
4 | Licensed under the Apache License, Version 2.0 (the "License"); |
5 | you may not use this file except in compliance with the License. |
6 | You may obtain a copy of the License at |
7 | |
8 | http://www.apache.org/licenses/LICENSE-2.0 |
9 | |
10 | Unless required by applicable law or agreed to in writing, software |
11 | distributed under the License is distributed on an "AS IS" BASIS, |
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | See the License for the specific language governing permissions and |
14 | limitations under the License. |
15 | */ |
16 | |
17 | // Header that sets HAVE_m128/HAVE_m256 if vector types (__m128/__m256) are available |
18 | |
19 | //! Class for testing safety of using vector types. |
20 | /** Uses circuitous logic forces compiler to put __m128/__m256 objects on stack while |
21 | executing various methods, and thus tempt it to use aligned loads and stores |
22 | on the stack. */ |
23 | // Do not create file-scope objects of the class, because MinGW (as of May 2010) |
24 | // did not always provide proper stack alignment in destructors of such objects. |
25 | |
26 | #if (_MSC_VER>=1600) |
27 | //TODO: handle /arch:AVX in the right way. |
28 | #pragma warning (push) |
29 | #pragma warning (disable: 4752) |
30 | #endif |
31 | |
32 | #if __TBB_GCC_WARNING_SUPPRESSION_PRESENT && __TBB_GCC_WARNING_IGNORED_ATTRIBUTES_PRESENT |
33 | #pragma GCC diagnostic push |
34 | #pragma GCC diagnostic ignored "-Wignored-attributes" |
35 | #endif |
36 | |
37 | |
38 | template<typename __Mvec> |
39 | class ClassWithVectorType { |
40 | static const int n = 16; |
41 | static const int F = sizeof(__Mvec)/sizeof(float); |
42 | __Mvec field[n]; |
43 | void init( int start ); |
44 | public: |
45 | ClassWithVectorType() {init(-n);} |
46 | ClassWithVectorType( int i ) {init(i);} |
47 | void operator=( const ClassWithVectorType& src ) { |
48 | __Mvec stack[n]; |
49 | for( int i=0; i<n; ++i ) |
50 | stack[i^5] = src.field[i]; |
51 | for( int i=0; i<n; ++i ) |
52 | field[i^5] = stack[i]; |
53 | } |
54 | ~ClassWithVectorType() {init(-2*n);} |
55 | friend bool operator==( const ClassWithVectorType& x, const ClassWithVectorType& y ) { |
56 | for( int i=0; i<F*n; ++i ) |
57 | if( ((const float*)x.field)[i]!=((const float*)y.field)[i] ) |
58 | return false; |
59 | return true; |
60 | } |
61 | friend bool operator!=( const ClassWithVectorType& x, const ClassWithVectorType& y ) { |
62 | return !(x==y); |
63 | } |
64 | }; |
65 | |
66 | template<typename __Mvec> |
67 | void ClassWithVectorType<__Mvec>::init( int start ) { |
68 | __Mvec stack[n]; |
69 | for( int i=0; i<n; ++i ) { |
70 | // Declaring value as a one-element array instead of a scalar quites |
71 | // gratuitous warnings about possible use of "value" before it was set. |
72 | __Mvec value[1]; |
73 | for( int j=0; j<F; ++j ) |
74 | ((float*)value)[j] = float(n*start+F*i+j); |
75 | stack[i^5] = value[0]; |
76 | } |
77 | for( int i=0; i<n; ++i ) |
78 | field[i^5] = stack[i]; |
79 | } |
80 | |
81 | #if (__AVX__ || (_MSC_VER>=1600 && _M_X64)) && !defined(__sun) |
82 | #include <immintrin.h> |
83 | #define HAVE_m256 1 |
84 | typedef ClassWithVectorType<__m256> ClassWithAVX; |
85 | #if _MSC_VER |
86 | #include <intrin.h> // for __cpuid |
87 | #endif |
88 | bool have_AVX() { |
89 | bool result = false; |
90 | const int avx_mask = 1<<28; |
91 | #if _MSC_VER || __INTEL_COMPILER |
92 | int info[4] = {0,0,0,0}; |
93 | const int ECX = 2; |
94 | __cpuid(info, 1); |
95 | result = (info[ECX] & avx_mask)!=0; |
96 | #elif __GNUC__ |
97 | int ECX; |
98 | __asm__( "cpuid" |
99 | : "=c" (ECX) |
100 | : "a" (1) |
101 | : "ebx" , "edx" ); |
102 | result = (ECX & avx_mask); |
103 | #endif |
104 | return result; |
105 | } |
106 | #endif /* __AVX__ etc */ |
107 | |
108 | #if (__SSE__ || _M_IX86_FP || _M_X64) && !defined(__sun) |
109 | #include <xmmintrin.h> |
110 | #define HAVE_m128 1 |
111 | typedef ClassWithVectorType<__m128> ClassWithSSE; |
112 | #endif |
113 | |
114 | #if __TBB_GCC_WARNING_SUPPRESSION_PRESENT && __TBB_GCC_WARNING_IGNORED_ATTRIBUTES_PRESENT |
115 | #pragma GCC diagnostic pop |
116 | #endif |
117 | |
118 | #if (_MSC_VER>=1600) |
119 | #pragma warning (pop) |
120 | #endif |
121 | |