-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathCheckSimd.cmake
87 lines (76 loc) · 2.52 KB
/
CheckSimd.cmake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
include(CheckCXXSourceRuns)
# save old configuration
set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
# set flags
set(AVX_FLAGS)
set(AVX2_FLAGS)
set(AVX512_FLAGS)
if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "MSVC")
set(AVX_FLAGS "/arch:AVX")
set(AVX2_FLAGS "/arch:AVX2")
# set(AVX512_FLAGS "/arch:AVX512") found no option for msvc
endif()
if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU" OR "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
set(AVX_FLAGS "-mavx")
set(AVX2_FLAGS "-mavx2")
set(AVX512_FLAGS "-mavx512f" "-mavx512cd") # xeon processors have more flags
endif()
# check for AVX
set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS} ${AVX_FLAGS})
CHECK_CXX_SOURCE_RUNS("
#include <immintrin.h>
int main(){
const float src[8] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f };
float dst[8];
__m256 a = _mm256_loadu_ps( src );
__m256 b = _mm256_add_ps( a, a );
_mm256_storeu_ps( dst, b );
for( int i = 0; i < 8; i++ )
if( ( src[i] + src[i] ) != dst[i] ) return -1;
return 0;
}"
AVX_ENABLED)
# remove flags if cpu does not support AVX
if(NOT AVX_ENABLED)
set(AVX_FLAGS "")
endif()
# check for AVX2
set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS} ${AVX2_FLAGS})
CHECK_CXX_SOURCE_RUNS("
#include <immintrin.h>
int main(){
const int src[8] = { 1, 2, 3, 4, 5, 6, 7, 8 };
int dst[8];
__m256i a = _mm256_loadu_si256( (__m256i*)src );
__m256i b = _mm256_add_epi32( a, a );
_mm256_storeu_si256( (__m256i*)dst, b );
for( int i = 0; i < 8; i++ )
if( ( src[i] + src[i] ) != dst[i] ) return -1;
return 0;
}"
AVX2_ENABLED)
# remove flags if cpu does not support AVX
if(NOT AVX2_ENABLED)
set(AVX2_FLAGS "")
endif()
# check for AVX512
set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS} ${AVX512_FLAGS})
CHECK_CXX_SOURCE_RUNS("
#include <zmmintrin.h>
int main(){
const int src[16] = { 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8 };
int dst[16];
__m512i a = _mm512_loadu_si512( (__m512i*)src );
__m512i b = _mm512_add_epi32( a, a );
_mm512_storeu_si512( (__m512i*)dst, b );
for( int i = 0; i < 16; i++ )
if( ( src[i] + src[i] ) != dst[i] ) return -1;
return 0;
}"
AVX512_ENABLED)
# remove flags if cpu does not support AVX
if(NOT AVX512_ENABLED)
set(AVX512_FLAGS "")
endif()
# restore previous state of cmake variable
set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS})