-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdetect_simd.c
More file actions
98 lines (85 loc) · 2.48 KB
/
detect_simd.c
File metadata and controls
98 lines (85 loc) · 2.48 KB
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
88
89
90
91
92
93
94
95
96
97
98
#include <stdio.h>
#include <stdbool.h>
#ifdef _MSC_VER
#include <intrin.h>
#endif
#ifdef __GNUC__
void __cpuid(int* cpuinfo, int info)
{
__asm__ __volatile__(
"xchg %%ebx, %%edi;"
"cpuid;"
"xchg %%ebx, %%edi;"
:"=a" (cpuinfo[0]), "=D" (cpuinfo[1]), "=c" (cpuinfo[2]), "=d" (cpuinfo[3])
:"0" (info)
);
}
unsigned long long _xgetbv(unsigned int index)
{
unsigned int eax, edx;
__asm__ __volatile__(
"xgetbv;"
: "=a" (eax), "=d"(edx)
: "c" (index)
);
return ((unsigned long long)edx << 32) | eax;
}
#endif
int main(){
bool sseSupportted = false;
bool sse2Supportted = false;
bool sse3Supportted = false;
bool ssse3Supportted = false;
bool sse4_1Supportted = false;
bool sse4_2Supportted = false;
bool sse4aSupportted = false;
bool sse5Supportted = false;
bool avxSupportted = false;
bool avx2Supportted = false;
bool avx512Supportted = false;
int cpuinfo[4];
__cpuid(cpuinfo, 1);
// Check SSE, SSE2, SSE3, SSSE3, SSE4.1, and SSE4.2 support
sseSupportted = cpuinfo[3] & (1 << 25) || false;
sse2Supportted = cpuinfo[3] & (1 << 26) || false;
sse3Supportted = cpuinfo[2] & (1 << 0) || false;
ssse3Supportted = cpuinfo[2] & (1 << 9) || false;
sse4_1Supportted = cpuinfo[2] & (1 << 19) || false;
sse4_2Supportted = cpuinfo[2] & (1 << 20) || false;
// Check AVX support
avxSupportted = cpuinfo[2] & (1 << 28) || false;
bool osxsaveSupported = cpuinfo[2] & (1 << 27) || false;
if (osxsaveSupported && avxSupportted)
{
// _XCR_XFEATURE_ENABLED_MASK = 0
unsigned long long xcrFeatureMask = _xgetbv(0);
avxSupportted = (xcrFeatureMask & 0x6) == 0x6;
}
// Check AVX2 support
__cpuid(cpuinfo, 7);
avx2Supportted = cpuinfo[1] & (1 << 5) || false;
// Check AVX-512 support
__cpuid(cpuinfo, 0);
int numExtendedIds = cpuinfo[0];
if (numExtendedIds >= 0xD)
{
__cpuid(cpuinfo, 0xD);
avx512Supportted = cpuinfo[1] & (1 << 16) || false;
}
if(avx512Supportted){
printf("avx512f");
} else if(avxSupportted){
printf("avx");
} else if(sse4_2Supportted){
printf("sse4.2");
} else if(sse4_1Supportted){
printf("sse4.1");
} else if(sse3Supportted){
printf("sse3");
} else if(sse2Supportted){
printf("sse2");
} else if(sseSupportted){
printf("sse");
}
return 0;
}