-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathcpuid.c
51 lines (41 loc) · 1.04 KB
/
cpuid.c
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
/**
* cpuid.c
*
* Checks if CPU has support of SHA instructions
*
* @author [email protected]
* @version 4.0
*/
#ifndef SILENT
#include <stdio.h>
#endif
#if defined(__clang__) || defined(__GNUC__) || defined(__INTEL_COMPILER)
#include <cpuid.h>
static int supports_sha_ni(void)
{
unsigned int CPUInfo[4];
__cpuid(0, CPUInfo[0], CPUInfo[1], CPUInfo[2], CPUInfo[3]);
if (CPUInfo[0] < 7)
return 0;
__cpuid_count(7, 0, CPUInfo[0], CPUInfo[1], CPUInfo[2], CPUInfo[3]);
return CPUInfo[1] & (1 << 29); /* SHA */
}
#else /* defined(__clang__) || defined(__GNUC__) */
static int supports_sha_ni(void)
{
unsigned int CPUInfo[4];
__cpuid(CPUInfo, 0);
if (CPUInfo[0] < 7)
return 0;
__cpuidex(CPUInfo, 7, 0);
return CPUInfo[1] & (1 << 29); /* Check SHA */
}
#endif /* defined(__clang__) || defined(__GNUC__) */
int main(int argc, char ** argv)
{
const int res = !supports_sha_ni();
#ifndef SILENT
printf("This CPU %s SHA-NI\n", res ? "does not support" : "supports");
#endif
return res;
}