forked from IAIK/Picnic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpu.c
120 lines (96 loc) · 2.32 KB
/
cpu.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
* This file is part of the optimized implementation of the Picnic signature scheme.
* See the accompanying documentation for complete details.
*
* The code is provided under the MIT license, see LICENSE for
* more details.
* SPDX-License-Identifier: MIT
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#else
/* If cmake checks were not run, define some known values. */
#if !defined(HAVE_SYS_AUXV_H) && defined(__linux__)
#define HAVE_SYS_AUXV_H
#endif
#if !defined(HAVE_ASM_HWCAP_H) && defined(__linux__) && defined(__arm__)
#define HAVE_ASM_HWCAP_H
#endif
#endif
#include "cpu.h"
#if !defined(BUILTIN_CPU_SUPPORTED) || defined(BUILTIN_CPU_SUPPORTED_BROKEN_BMI2)
#if defined(__arm__) && defined(HAVE_SYS_AUXV_H) && defined(HAVE_ASM_HWCAP_H)
#include <asm/hwcap.h>
#include <sys/auxv.h>
static unsigned int init_caps(void) {
unsigned int caps = 0;
if (getauxval(AT_HWCAP) & HWCAP_NEON) {
caps |= CPU_CAP_NEON;
}
return caps;
}
#elif (defined(__x86_64__) || defined(__i386__) || defined(_M_IX86) || defined(_M_AMD64)) && (defined(__GNUC__) || defined(_MSC_VER))
#ifdef _MSC_VER
#include <intrin.h>
static unsigned init_caps(void) {
unsigned int caps = 0;
union {
struct {
unsigned int eax, ebx, ecx, edx;
};
int data[4];
} regs = {0};
__cpuid(regs.data, 0);
unsigned int max = regs.eax;
if (max >= 1) {
__cpuid(regs.data, 0);
if (regs.edx & (1 << 26)) {
caps |= CPU_CAP_SSE2;
}
}
if (max >= 7) {
__cpuidex(regs.data, 7, 0);
if (regs.ebx & (1 << 5)) {
caps |= CPU_CAP_AVX2;
}
if (regs.ebx & (1 << 8)) {
caps |= CPU_CAP_BMI2;
}
}
return caps;
}
#else
#include <cpuid.h>
static unsigned init_caps(void) {
unsigned int caps = 0;
unsigned int eax, ebx, ecx, edx;
if (__get_cpuid(1, &eax, &ebx, &ecx, &edx)) {
if (edx & (1 << 26)) {
caps |= CPU_CAP_SSE2;
}
}
if (__get_cpuid(7, &eax, &ebx, &ecx, &edx)) {
if (ebx & (1 << 5)) {
caps |= CPU_CAP_AVX2;
}
if (ebx & (1 << 8)) {
caps |= CPU_CAP_BMI2;
}
}
return caps;
}
#endif
#else
static unsigned init_caps(void) {
return 0;
}
#endif
#include <limits.h>
static unsigned int cpu_caps = UINT_MAX;
bool cpu_supports(unsigned int caps) {
if (cpu_caps == UINT_MAX) {
cpu_caps = init_caps();
}
return (cpu_caps & caps) == caps;
}
#endif