-
Notifications
You must be signed in to change notification settings - Fork 180
/
Copy pathcpuid.c
82 lines (65 loc) · 1.79 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
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
/*
* Copyright Supranational LLC
* Licensed under the Apache License, Version 2.0, see LICENSE for details.
* SPDX-License-Identifier: Apache-2.0
*/
int __blst_platform_cap = 0;
#if defined(__x86_64__) || defined(__x86_64) || defined(_M_X64)
# if defined(__GNUC__) || defined(__clang__) || defined(__SUNPRO_C)
static void __cpuidex(int info[4], int func, int sub)
{
int eax, ebx, ecx, edx;
__asm__("cpuid" : "=a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx)
: "a"(func), "c"(sub));
info[0] = eax;
info[1] = ebx;
info[2] = ecx;
info[3] = edx;
}
# else
# include <intrin.h>
# endif
# if defined(__GNUC__) || defined(__clang__)
__attribute__((constructor))
# endif
int __blst_cpuid(void)
{
int info[4], cap = 0;
__cpuidex(info, 0, 0);
if (info[0] > 6) {
__cpuidex(info, 7, 0);
cap |= (info[1]>>19) & 1; /* ADX */
cap |= (info[1]>>28) & 2; /* SHA */
}
__blst_platform_cap = cap;
return 0;
}
# if defined(_MSC_VER) && !defined(__clang__)
# pragma section(".CRT$XCU",read)
__declspec(allocate(".CRT$XCU")) static int (*p)(void) = __blst_cpuid;
# elif defined(__SUNPRO_C)
# pragma init(__blst_cpuid)
# endif
#elif defined(__aarch64__) || defined(__aarch64)
# if defined(__linux__) && (defined(__GNUC__) || defined(__clang__))
extern unsigned long getauxval(unsigned long type) __attribute__ ((weak));
__attribute__((constructor))
int __blst_cpuid(void)
{
int cap = 0;
if (getauxval) {
unsigned long hwcap_ce = getauxval(16);
cap = (hwcap_ce>>6) & 1; /* SHA256 */
}
__blst_platform_cap = cap;
return 0;
}
# elif defined(__APPLE__) && (defined(__GNUC__) || defined(__clang__))
__attribute__((constructor))
int __blst_cpuid()
{
__blst_platform_cap = 1; /* SHA256 */
return 0;
}
# endif
#endif