aboutsummaryrefslogtreecommitdiffstats
path: root/util/system/cpu_id.cpp
blob: a13ebbc018415ffd2d37a0a4f937dd243c8529d7 (plain) (blame)
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#include "cpu_id.h" 
#include "types.h" 
#include "platform.h" 

#include <util/generic/singleton.h> 
 
#if defined(_win_)
    #include <intrin.h> 
    #include <immintrin.h> 
#elif defined(_x86_)
    #include <cpuid.h> 
#endif

#include <string.h> 

#if defined(_x86_) && !defined(_win_)
static ui64 _xgetbv(ui32 xcr) {
    ui32 eax;
    ui32 edx;
    __asm__ volatile( 
        "xgetbv" 
        : "=a"(eax), "=d"(edx) 
        : "c"(xcr)); 
    return (static_cast<ui64>(edx) << 32) | eax;
}
#endif

bool NX86::CpuId(ui32 op, ui32 subOp, ui32* res) noexcept {
#if defined(_x86_)
    #if defined(_MSC_VER) 
    static_assert(sizeof(int) == sizeof(ui32), "ups, something wrong here");
    __cpuidex((int*)res, op, subOp);
    #else 
    __cpuid_count(op, subOp, res[0], res[1], res[2], res[3]);
    #endif 
    return true;
#else
    (void)op;
    (void)subOp;

    memset(res, 0, 4 * sizeof(ui32));

    return false;
#endif
}

bool NX86::CpuId(ui32 op, ui32* res) noexcept {
#if defined(_x86_) 
    #if defined(_MSC_VER) 
    static_assert(sizeof(int) == sizeof(ui32), "ups, something wrong here"); 
    __cpuid((int*)res, op); 
    #else 
    __cpuid(op, res[0], res[1], res[2], res[3]);
    #endif 
    return true; 
#else 
    (void)op; 
 
    memset(res, 0, 4 * sizeof(ui32)); 
 
    return false; 
#endif 
}

namespace { 
    union TX86CpuInfo { 
        ui32 Info[4]; 
 
        struct { 
            ui32 EAX; 
            ui32 EBX; 
            ui32 ECX; 
            ui32 EDX; 
        }; 
 
        inline TX86CpuInfo(ui32 op) noexcept {
            NX86::CpuId(op, Info); 
        } 

        inline TX86CpuInfo(ui32 op, ui32 subOp) noexcept {
            NX86::CpuId(op, subOp, Info);
        }
    };

    static_assert(sizeof(TX86CpuInfo) == 16, "please, fix me"); 
}

// https://en.wikipedia.org/wiki/CPUID 
bool NX86::HaveRDTSCP() noexcept { 
    return (TX86CpuInfo(0x80000001).EDX >> 27) & 1u; 
} 
 
bool NX86::HaveSSE() noexcept {
    return (TX86CpuInfo(0x1).EDX >> 25) & 1u; 
} 
 
bool NX86::HaveSSE2() noexcept {
    return (TX86CpuInfo(0x1).EDX >> 26) & 1u; 
}

bool NX86::HaveSSE3() noexcept {
    return TX86CpuInfo(0x1).ECX & 1u; 
} 
 
bool NX86::HavePCLMUL() noexcept {
    return (TX86CpuInfo(0x1).ECX >> 1) & 1u;
}

bool NX86::HaveSSSE3() noexcept {
    return (TX86CpuInfo(0x1).ECX >> 9) & 1u; 
} 
 
bool NX86::HaveSSE41() noexcept {
    return (TX86CpuInfo(0x1).ECX >> 19) & 1u; 
} 
 
bool NX86::HaveSSE42() noexcept {
    return (TX86CpuInfo(0x1).ECX >> 20) & 1u; 
}

bool NX86::HaveF16C() noexcept {
    return (TX86CpuInfo(0x1).ECX >> 29) & 1u;
}

bool NX86::HavePOPCNT() noexcept {
    return (TX86CpuInfo(0x1).ECX >> 23) & 1u; 
} 
 
bool NX86::HaveAES() noexcept {
    return (TX86CpuInfo(0x1).ECX >> 25) & 1u; 
} 
 
bool NX86::HaveXSAVE() noexcept {
    return (TX86CpuInfo(0x1).ECX >> 26) & 1u;
}

bool NX86::HaveOSXSAVE() noexcept {
    return (TX86CpuInfo(0x1).ECX >> 27) & 1u;
}

bool NX86::HaveAVX() noexcept {
#if defined(_x86_)
    // http://www.intel.com/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-optimization-manual.pdf
    // https://bugs.chromium.org/p/chromium/issues/detail?id=375968
    return HaveOSXSAVE()                           // implies HaveXSAVE() 
           && (_xgetbv(0) & 6u) == 6u              // XMM state and YMM state are enabled by OS 
           && ((TX86CpuInfo(0x1).ECX >> 28) & 1u); // AVX bit 
#else
    return false;
#endif
} 
 
bool NX86::HaveFMA() noexcept {
    return HaveAVX() && ((TX86CpuInfo(0x1).ECX >> 12) & 1u);
}

bool NX86::HaveAVX2() noexcept {
    return HaveAVX() && ((TX86CpuInfo(0x7, 0).EBX >> 5) & 1u);
}

bool NX86::HaveBMI1() noexcept {
    return (TX86CpuInfo(0x7, 0).EBX >> 3) & 1u;
}

bool NX86::HaveBMI2() noexcept {
    return (TX86CpuInfo(0x7, 0).EBX >> 8) & 1u;
}

bool NX86::HaveAVX512F() noexcept {
#if defined(_x86_)
    // https://software.intel.com/en-us/articles/how-to-detect-knl-instruction-support
    return HaveOSXSAVE()                           // implies HaveXSAVE() 
           && (_xgetbv(0) & 6u) == 6u              // XMM state and YMM state are enabled by OS 
           && ((_xgetbv(0) >> 5) & 7u) == 7u       // ZMM state is enabled by OS 
           && TX86CpuInfo(0x0).EAX >= 0x7          // leaf 7 is present 
           && ((TX86CpuInfo(0x7).EBX >> 16) & 1u); // AVX512F bit 
#else
    return false;
#endif
}

bool NX86::HaveAVX512DQ() noexcept {
    return HaveAVX512F() && ((TX86CpuInfo(0x7, 0).EBX >> 17) & 1u);
}

bool NX86::HaveRDSEED() noexcept {
    return TX86CpuInfo(0x0).EAX >= 0x7 && ((TX86CpuInfo(0x7, 0).EBX >> 18) & 1u);
}

bool NX86::HaveADX() noexcept {
    return TX86CpuInfo(0x0).EAX >= 0x7 && ((TX86CpuInfo(0x7, 0).EBX >> 19) & 1u);
}

bool NX86::HaveAVX512IFMA() noexcept {
    return HaveAVX512F() && ((TX86CpuInfo(0x7, 0).EBX >> 21) & 1u);
}

bool NX86::HavePCOMMIT() noexcept {
    return TX86CpuInfo(0x0).EAX >= 0x7 && ((TX86CpuInfo(0x7, 0).EBX >> 22) & 1u);
}

bool NX86::HaveCLFLUSHOPT() noexcept {
    return TX86CpuInfo(0x0).EAX >= 0x7 && ((TX86CpuInfo(0x7, 0).EBX >> 23) & 1u);
}

bool NX86::HaveCLWB() noexcept {
    return TX86CpuInfo(0x0).EAX >= 0x7 && ((TX86CpuInfo(0x7, 0).EBX >> 24) & 1u);
}

bool NX86::HaveAVX512PF() noexcept {
    return HaveAVX512F() && ((TX86CpuInfo(0x7, 0).EBX >> 26) & 1u);
}

bool NX86::HaveAVX512ER() noexcept {
    return HaveAVX512F() && ((TX86CpuInfo(0x7, 0).EBX >> 27) & 1u);
}

bool NX86::HaveAVX512CD() noexcept {
    return HaveAVX512F() && ((TX86CpuInfo(0x7, 0).EBX >> 28) & 1u);
}

bool NX86::HaveSHA() noexcept {
    return TX86CpuInfo(0x0).EAX >= 0x7 && ((TX86CpuInfo(0x7, 0).EBX >> 29) & 1u);
}

bool NX86::HaveAVX512BW() noexcept {
    return HaveAVX512F() && ((TX86CpuInfo(0x7, 0).EBX >> 30) & 1u);
}

bool NX86::HaveAVX512VL() noexcept {
    return HaveAVX512F() && ((TX86CpuInfo(0x7, 0).EBX >> 31) & 1u);
}

bool NX86::HavePREFETCHWT1() noexcept {
    return TX86CpuInfo(0x0).EAX >= 0x7 && ((TX86CpuInfo(0x7, 0).ECX >> 0) & 1u);
}

bool NX86::HaveAVX512VBMI() noexcept {
    return HaveAVX512F() && ((TX86CpuInfo(0x7, 0).ECX >> 1) & 1u);
}

bool NX86::HaveRDRAND() noexcept {
    return TX86CpuInfo(0x0).EAX >= 0x7 && ((TX86CpuInfo(0x1).ECX >> 30) & 1u);
} 
 
const char* CpuBrand(ui32* store) noexcept {
    memset(store, 0, 12 * sizeof(*store)); 
 
#if defined(_x86_) 
    NX86::CpuId(0x80000002, store); 
    NX86::CpuId(0x80000003, store + 4); 
    NX86::CpuId(0x80000004, store + 8); 
#endif 
 
    return (const char*)store; 
}
 
#define Y_DEF_NAME(X)                                               \ 
    bool NX86::CachedHave##X() noexcept {                           \ 
        return SingletonWithPriority<TFlagsCache, 0>()->Have##X##_; \ 
    } 
Y_CPU_ID_ENUMERATE_OUTLINED_CACHED_DEFINE(Y_DEF_NAME)
#undef Y_DEF_NAME