-
Notifications
You must be signed in to change notification settings - Fork 3
/
vpu-count.c
791 lines (657 loc) · 25.1 KB
/
vpu-count.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
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
//
// MIT License
//
// Copyright (c) 2017 Intel Corporation
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// This code was written by Jeff Hammond <[email protected]>.
//
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <cpuid.h>
#if (__STDC_VERSION__ < 199901L)
#error You need to enable C99 or remove vararg debug printing by hand.
#endif
#ifdef DEBUG
#define PDEBUG(...) printf(__VA_ARGS__)
#else
#define PDEBUG(...)
#endif
void get_cpu_name32(char cpu_name[32])
{
uint32_t eax, ebx, ecx, edx;
__cpuid(0x80000002u, eax, ebx, ecx, edx);
*(uint32_t *)&cpu_name[0] = eax;
*(uint32_t *)&cpu_name[4] = ebx;
*(uint32_t *)&cpu_name[8] = ecx;
*(uint32_t *)&cpu_name[12] = edx;
__cpuid(0x80000003u, eax, ebx, ecx, edx);
*(uint32_t *)&cpu_name[16+0] = eax;
*(uint32_t *)&cpu_name[16+4] = ebx;
*(uint32_t *)&cpu_name[16+8] = ecx;
*(uint32_t *)&cpu_name[16+12] = edx;
}
void get_cpu_name48(char cpu_name[48])
{
uint32_t eax, ebx, ecx, edx;
__cpuid(0x80000002u, eax, ebx, ecx, edx);
*(uint32_t *)&cpu_name[0] = eax;
*(uint32_t *)&cpu_name[4] = ebx;
*(uint32_t *)&cpu_name[8] = ecx;
*(uint32_t *)&cpu_name[12] = edx;
PDEBUG("%x %x %x %x\n", eax, ebx, ecx, edx);
__cpuid(0x80000003u, eax, ebx, ecx, edx);
*(uint32_t *)&cpu_name[16+0] = eax;
*(uint32_t *)&cpu_name[16+4] = ebx;
*(uint32_t *)&cpu_name[16+8] = ecx;
*(uint32_t *)&cpu_name[16+12] = edx;
PDEBUG("%x %x %x %x\n", eax, ebx, ecx, edx);
__cpuid(0x80000004u, eax, ebx, ecx, edx);
*(uint32_t *)&cpu_name[32+0] = eax;
*(uint32_t *)&cpu_name[32+4] = ebx;
*(uint32_t *)&cpu_name[32+8] = ecx;
*(uint32_t *)&cpu_name[32+12] = edx;
PDEBUG("%x %x %x %x\n", eax, ebx, ecx, edx);
PDEBUG("0x80000002u: \"%.*s\"\n", 16, &cpu_name[0]);
PDEBUG("0x80000003u: \"%.*s\"\n", 16, &cpu_name[16]);
PDEBUG("0x80000004u: \"%.*s\"\n", 16, &cpu_name[32]);
}
bool is_intel(void)
{
/* leaf 0 - Architecture */
uint32_t leaf0[4]={0x0,0x0,0x0,0x0};
__cpuid(leaf0[0], leaf0[0], leaf0[1], leaf0[2], leaf0[3]);
PDEBUG("0x0: %x,%x,%x,%x\n", leaf0[0], leaf0[1], leaf0[2], leaf0[3]);
bool intel = (leaf0[1] == 0x756e6547) && (leaf0[2] == 0x6c65746e) && (leaf0[3] == 0x49656e69);
PDEBUG("Intel? %s\n", intel ? "yes" : "no");
return (intel);
}
bool is_haswell(void)
{
/* leaf 1 - Model, Family, etc. */
uint32_t leaf1[4]={0x1,0x0,0x0,0x0};
__cpuid(leaf1[0], leaf1[0], leaf1[1], leaf1[2], leaf1[3]);
PDEBUG("0x1: %x,%x,%x,%x\n", leaf1[0], leaf1[1], leaf1[2], leaf1[3]);
//uint32_t stepping = (leaf1[0] ) & 0x0f;
uint32_t model = (leaf1[0] >> 4) & 0x0f;
uint32_t family = (leaf1[0] >> 8) & 0x0f;
//uint32_t proctype = (leaf1[0] >> 12) & 0x03;
uint32_t xmodel = (leaf1[0] >> 16) & 0x0f;
//uint32_t xfamily = (leaf1[0] >> 20) & 0xff;
if (family == 0x06) {
model += (xmodel << 4);
}
else if (family == 0x0f) {
model += (xmodel << 4);
//family += xfamily;
}
PDEBUG("signature: %#08x\n", (leaf1[0]) );
//PDEBUG("stepping: %#04x=%d\n", stepping, stepping);
PDEBUG("model: %#04x=%d\n", model, model);
PDEBUG("family: %#04x=%d\n", family, family);
//PDEBUG("proc type: %#04x=%d\n", proctype, proctype);
PDEBUG("ext model: %#04x=%d\n", xmodel, xmodel);
//PDEBUG("ext family: %#08x=%d\n", xfamily, xfamily);
/* https://en.wikichip.org/wiki/intel/cpuid */
bool haswell = ( (model == 0x3c) || /* 60 in binary (S) */
(model == 0x45) || /* 69 in binary (ULT) */
(model == 0x46) || /* 70 in binary (GT3E) */
(model == 0x3f) ); /* 63 in binary (server) */
PDEBUG("Haswell? %s\n", haswell ? "yes" : "no");
return (haswell);
}
bool is_skylake_server(void)
{
/* leaf 1 - Model, Family, etc. */
uint32_t leaf1[4]={0x1,0x0,0x0,0x0};
__cpuid(leaf1[0], leaf1[0], leaf1[1], leaf1[2], leaf1[3]);
PDEBUG("0x1: %x,%x,%x,%x\n", leaf1[0], leaf1[1], leaf1[2], leaf1[3]);
//uint32_t stepping = (leaf1[0] ) & 0x0f;
uint32_t model = (leaf1[0] >> 4) & 0x0f;
uint32_t family = (leaf1[0] >> 8) & 0x0f;
//uint32_t proctype = (leaf1[0] >> 12) & 0x03;
uint32_t xmodel = (leaf1[0] >> 16) & 0x0f;
//uint32_t xfamily = (leaf1[0] >> 20) & 0xff;
if (family == 0x06) {
model += (xmodel << 4);
}
else if (family == 0x0f) {
model += (xmodel << 4);
//family += xfamily;
}
PDEBUG("signature: %#08x\n", (leaf1[0]) );
//PDEBUG("stepping: %#04x=%d\n", stepping, stepping);
PDEBUG("model: %#04x=%d\n", model, model);
PDEBUG("family: %#04x=%d\n", family, family);
//PDEBUG("proc type: %#04x=%d\n", proctype, proctype);
PDEBUG("ext model: %#04x=%d\n", xmodel, xmodel);
//PDEBUG("ext family: %#08x=%d\n", xfamily, xfamily);
bool skylake_server = (model == 0x55); /* 85 in binary */
PDEBUG("Skylake server? %s\n", skylake_server ? "yes" : "no");
return (skylake_server);
}
bool is_cannonlake_client(void)
{
/* leaf 1 - Model, Family, etc. */
uint32_t leaf1[4]={0x1,0x0,0x0,0x0};
__cpuid(leaf1[0], leaf1[0], leaf1[1], leaf1[2], leaf1[3]);
PDEBUG("0x1: %x,%x,%x,%x\n", leaf1[0], leaf1[1], leaf1[2], leaf1[3]);
//uint32_t stepping = (leaf1[0] ) & 0x0f;
uint32_t model = (leaf1[0] >> 4) & 0x0f;
uint32_t family = (leaf1[0] >> 8) & 0x0f;
//uint32_t proctype = (leaf1[0] >> 12) & 0x03;
uint32_t xmodel = (leaf1[0] >> 16) & 0x0f;
//uint32_t xfamily = (leaf1[0] >> 20) & 0xff;
if (family == 0x06) {
model += (xmodel << 4);
}
else if (family == 0x0f) {
model += (xmodel << 4);
//family += xfamily;
}
PDEBUG("signature: %#08x\n", (leaf1[0]) );
//PDEBUG("stepping: %#04x=%d\n", stepping, stepping);
PDEBUG("model: %#04x=%d\n", model, model);
PDEBUG("family: %#04x=%d\n", family, family);
//PDEBUG("proc type: %#04x=%d\n", proctype, proctype);
PDEBUG("ext model: %#04x=%d\n", xmodel, xmodel);
//PDEBUG("ext family: %#08x=%d\n", xfamily, xfamily);
/* from https://en.wikichip.org/wiki/intel/cpuid */
bool cannonlake = (model == 0x66); /* 102 in binary */
PDEBUG("Cannon Lake client? %s\n", cannonlake ? "yes" : "no");
return (cannonlake);
}
bool is_icelake_client(void)
{
/* leaf 1 - Model, Family, etc. */
uint32_t leaf1[4]={0x1,0x0,0x0,0x0};
__cpuid(leaf1[0], leaf1[0], leaf1[1], leaf1[2], leaf1[3]);
PDEBUG("0x1: %x,%x,%x,%x\n", leaf1[0], leaf1[1], leaf1[2], leaf1[3]);
//uint32_t stepping = (leaf1[0] ) & 0x0f;
uint32_t model = (leaf1[0] >> 4) & 0x0f;
uint32_t family = (leaf1[0] >> 8) & 0x0f;
//uint32_t proctype = (leaf1[0] >> 12) & 0x03;
uint32_t xmodel = (leaf1[0] >> 16) & 0x0f;
//uint32_t xfamily = (leaf1[0] >> 20) & 0xff;
if (family == 0x06) {
model += (xmodel << 4);
}
else if (family == 0x0f) {
model += (xmodel << 4);
//family += xfamily;
}
PDEBUG("signature: %#08x\n", (leaf1[0]) );
//PDEBUG("stepping: %#04x=%d\n", stepping, stepping);
PDEBUG("model: %#04x=%d\n", model, model);
PDEBUG("family: %#04x=%d\n", family, family);
//PDEBUG("proc type: %#04x=%d\n", proctype, proctype);
PDEBUG("ext model: %#04x=%d\n", xmodel, xmodel);
//PDEBUG("ext family: %#08x=%d\n", xfamily, xfamily);
/* from https://en.wikichip.org/wiki/intel/cpuid */
bool icelake = ( (model == 0x7d) || /* 125 in binary (Y) */
(model == 0x7e) ); /* 126 in binary (U) */
PDEBUG("Ice Lake client? %s\n", icelake ? "yes" : "no");
return (icelake);
}
bool is_icelake_server(void)
{
/* leaf 1 - Model, Family, etc. */
uint32_t leaf1[4]={0x1,0x0,0x0,0x0};
__cpuid(leaf1[0], leaf1[0], leaf1[1], leaf1[2], leaf1[3]);
PDEBUG("0x1: %x,%x,%x,%x\n", leaf1[0], leaf1[1], leaf1[2], leaf1[3]);
//uint32_t stepping = (leaf1[0] ) & 0x0f;
uint32_t model = (leaf1[0] >> 4) & 0x0f;
uint32_t family = (leaf1[0] >> 8) & 0x0f;
//uint32_t proctype = (leaf1[0] >> 12) & 0x03;
uint32_t xmodel = (leaf1[0] >> 16) & 0x0f;
//uint32_t xfamily = (leaf1[0] >> 20) & 0xff;
if (family == 0x06) {
model += (xmodel << 4);
}
else if (family == 0x0f) {
model += (xmodel << 4);
//family += xfamily;
}
PDEBUG("signature: %#08x\n", (leaf1[0]) );
//PDEBUG("stepping: %#04x=%d\n", stepping, stepping);
PDEBUG("model: %#04x=%d\n", model, model);
PDEBUG("family: %#04x=%d\n", family, family);
//PDEBUG("proc type: %#04x=%d\n", proctype, proctype);
PDEBUG("ext model: %#04x=%d\n", xmodel, xmodel);
//PDEBUG("ext family: %#08x=%d\n", xfamily, xfamily);
bool icelake = ( (model == 0x6a) || /* 106 in binary */
(model == 0x6c) ); /* 108 in binary */
PDEBUG("Ice Lake server? %s\n", icelake ? "yes" : "no");
return (icelake);
}
bool is_sapphire_rapids(void)
{
/* leaf 1 - Model, Family, etc. */
uint32_t leaf1[4]={0x1,0x0,0x0,0x0};
__cpuid(leaf1[0], leaf1[0], leaf1[1], leaf1[2], leaf1[3]);
PDEBUG("0x1: %x,%x,%x,%x\n", leaf1[0], leaf1[1], leaf1[2], leaf1[3]);
//uint32_t stepping = (leaf1[0] ) & 0x0f;
uint32_t model = (leaf1[0] >> 4) & 0x0f;
uint32_t family = (leaf1[0] >> 8) & 0x0f;
//uint32_t proctype = (leaf1[0] >> 12) & 0x03;
uint32_t xmodel = (leaf1[0] >> 16) & 0x0f;
//uint32_t xfamily = (leaf1[0] >> 20) & 0xff;
if (family == 0x06) {
model += (xmodel << 4);
}
else if (family == 0x0f) {
model += (xmodel << 4);
//family += xfamily;
}
PDEBUG("signature: %#08x\n", (leaf1[0]) );
//PDEBUG("stepping: %#04x=%d\n", stepping, stepping);
PDEBUG("model: %#04x=%d\n", model, model);
PDEBUG("family: %#04x=%d\n", family, family);
//PDEBUG("proc type: %#04x=%d\n", proctype, proctype);
PDEBUG("ext model: %#04x=%d\n", xmodel, xmodel);
//PDEBUG("ext family: %#08x=%d\n", xfamily, xfamily);
// https://en.wikichip.org/wiki/intel/cpuid says 143
bool sapphire_rapids = (model == 0x8f); /* 143 in binary */
PDEBUG("Sapphire Rapids server? %s\n", sapphire_rapids ? "yes" : "no");
return (sapphire_rapids);
}
bool is_tigerlake_client(void)
{
/* leaf 1 - Model, Family, etc. */
uint32_t leaf1[4]={0x1,0x0,0x0,0x0};
__cpuid(leaf1[0], leaf1[0], leaf1[1], leaf1[2], leaf1[3]);
PDEBUG("0x1: %x,%x,%x,%x\n", leaf1[0], leaf1[1], leaf1[2], leaf1[3]);
//uint32_t stepping = (leaf1[0] ) & 0x0f;
uint32_t model = (leaf1[0] >> 4) & 0x0f;
uint32_t family = (leaf1[0] >> 8) & 0x0f;
//uint32_t proctype = (leaf1[0] >> 12) & 0x03;
uint32_t xmodel = (leaf1[0] >> 16) & 0x0f;
//uint32_t xfamily = (leaf1[0] >> 20) & 0xff;
if (family == 0x06) {
model += (xmodel << 4);
}
else if (family == 0x0f) {
model += (xmodel << 4);
//family += xfamily;
}
PDEBUG("signature: %#08x\n", (leaf1[0]) );
//PDEBUG("stepping: %#04x=%d\n", stepping, stepping);
PDEBUG("model: %#04x=%d\n", model, model);
PDEBUG("family: %#04x=%d\n", family, family);
//PDEBUG("proc type: %#04x=%d\n", proctype, proctype);
PDEBUG("ext model: %#04x=%d\n", xmodel, xmodel);
//PDEBUG("ext family: %#08x=%d\n", xfamily, xfamily);
bool tigerlake = (model == 0x8c); /* 140 in binary */
PDEBUG("Tiger Lake client? %s\n", tigerlake ? "yes" : "no");
return (tigerlake);
}
bool is_knl(void)
{
/* leaf 7 - AVX-512 features */
uint32_t leaf7[4]={0x7,0x0,0x0,0x0};
__cpuid_count(leaf7[0], leaf7[2], leaf7[0], leaf7[1], leaf7[2], leaf7[3]);
PDEBUG("0x7: %x,%x,%x,%x\n", leaf7[0], leaf7[1], leaf7[2], leaf7[3]);
bool knl = (leaf7[1] & 1u<<16) && /* AVX-512F */
(leaf7[1] & 1u<<28) && /* AVX-512CD */
(leaf7[1] & 1u<<26) && /* AVX-512PF */
(leaf7[1] & 1u<<27); /* AVX-512ER */
PDEBUG("KNL uarch? %s\n", knl ? "yes" : "no");
return (knl);
}
bool is_knm(void)
{
/* leaf 7 - AVX-512 features */
uint32_t leaf7[4]={0x7,0x0,0x0,0x0};
__cpuid_count(leaf7[0], leaf7[2], leaf7[0], leaf7[1], leaf7[2], leaf7[3]);
PDEBUG("0x7: %x,%x,%x,%x\n", leaf7[0], leaf7[1], leaf7[2], leaf7[3]);
bool knl = (leaf7[1] & 1u<<16) && /* AVX-512F */
(leaf7[1] & 1u<<28) && /* AVX-512CD */
(leaf7[1] & 1u<<26) && /* AVX-512PF */
(leaf7[1] & 1u<<27); /* AVX-512ER */
/* KNM is a superset of KNL, at least architecturally */
bool knm = knl && (leaf7[2] & 1u<<14); /* AVX-512VPOPCNTDQ */
PDEBUG("KNM uarch? %s\n", knl ? "yes" : "no");
return (knm);
}
/* detect the FMA3 set */
bool has_fma3(void)
{
/* leaf 1 - basic features */
uint32_t leaf1[4]={0x1,0x0,0x0,0x0};
__cpuid_count(leaf1[0], leaf1[2], leaf1[0], leaf1[1], leaf1[2], leaf1[3]);
PDEBUG("0x1: %x,%x,%x,%x\n", leaf1[0], leaf1[1], leaf1[2], leaf1[3]);
bool fma = (leaf1[2] & 1u<<12); /* FMA3 */
PDEBUG("FMA3? %s\n", fma ? "yes" : "no");
return (fma);
}
/* detect the FMA4 set */
bool has_fma4(void)
{
/* leaf 80000001h - extended features */
uint32_t leaf1[4]={0x80000001,0x0,0x0,0x0};
__cpuid_count(leaf1[0], leaf1[2], leaf1[0], leaf1[1], leaf1[2], leaf1[3]);
PDEBUG("0x1: %x,%x,%x,%x\n", leaf1[0], leaf1[1], leaf1[2], leaf1[3]);
bool fma = (leaf1[2] & 1u<<16); /* FMA4 */
PDEBUG("FMA4? %s\n", fma ? "yes" : "no");
return (fma);
}
/* detect the AVX2 set */
bool has_avx2(void)
{
/* leaf 7 - AVX features */
uint32_t leaf7[4]={0x7,0x0,0x0,0x0};
__cpuid_count(leaf7[0], leaf7[2], leaf7[0], leaf7[1], leaf7[2], leaf7[3]);
PDEBUG("0x7: %x,%x,%x,%x\n", leaf7[0], leaf7[1], leaf7[2], leaf7[3]);
bool avx2 = (leaf7[1] & 1u<<5); /* AVX2 */
PDEBUG("AVX2? %s\n", avx2 ? "yes" : "no");
return (avx2);
}
/* detect the AVX-512 set added in Skylake server:
* AVX-512 F,DQ,CD,BW,VL */
bool has_avx512_skx(void)
{
/* leaf 7 - AVX-512 features */
uint32_t leaf7[4]={0x7,0x0,0x0,0x0};
__cpuid_count(leaf7[0], leaf7[2], leaf7[0], leaf7[1], leaf7[2], leaf7[3]);
PDEBUG("0x7: %x,%x,%x,%x\n", leaf7[0], leaf7[1], leaf7[2], leaf7[3]);
bool skx = (leaf7[1] & 1u<<16) && /* AVX-512F */
(leaf7[1] & 1u<<17) && /* AVX-512DQ */
(leaf7[1] & 1u<<28) && /* AVX-512CD */
(leaf7[1] & 1u<<30) && /* AVX-512BW */
(leaf7[1] & 1u<<31); /* AVX-512VL */
return (skx);
}
/* AVX-512 VNNI first appeared in CLX */
bool has_avx512_vnni(void)
{
/* leaf 7 - AVX-512 features */
uint32_t leaf7[4]={0x7,0x0,0x0,0x0};
__cpuid_count(leaf7[0], leaf7[2], leaf7[0], leaf7[1], leaf7[2], leaf7[3]);
PDEBUG("0x7: %x,%x,%x,%x\n", leaf7[0], leaf7[1], leaf7[2], leaf7[3]);
bool vnni = (leaf7[2] & 1u<<11); /* AVX-512VNNI */
return (vnni);
}
/* AVX-512 VPOPCNTDQ first appeared in KNM and SNC */
bool has_avx512_vpopcntdq(void)
{
/* leaf 7 - AVX-512 features */
uint32_t leaf7[4]={0x7,0x0,0x0,0x0};
__cpuid_count(leaf7[0], leaf7[2], leaf7[0], leaf7[1], leaf7[2], leaf7[3]);
PDEBUG("0x7: %x,%x,%x,%x\n", leaf7[0], leaf7[1], leaf7[2], leaf7[3]);
bool vpopcntdq = (leaf7[2] & 1u<<14); /* AVX-512VPOPCNTDQ */
return (vpopcntdq);
}
/* detect the AVX-512 set added in CNL:
* AVX-512 VBMI,IFMA */
bool has_avx512_cnl(void)
{
/* leaf 7 - AVX-512 features */
uint32_t leaf7[4]={0x7,0x0,0x0,0x0};
__cpuid_count(leaf7[0], leaf7[2], leaf7[0], leaf7[1], leaf7[2], leaf7[3]);
PDEBUG("0x7: %x,%x,%x,%x\n", leaf7[0], leaf7[1], leaf7[2], leaf7[3]);
bool cnl = (leaf7[1] & 1u<<21) && /* AVX-512IFMA */
(leaf7[2] & 1u<< 1); /* AVX-512VBMI */
return (cnl);
}
/* detect the AVX-512 set added in SNC:
* AVX-512 VBMI2,GFNI,VAES,VPCLMULQDQ,BITALG */
bool has_avx512_snc(void)
{
/* leaf 7 - AVX-512 features */
uint32_t leaf7[4]={0x7,0x0,0x0,0x0};
__cpuid_count(leaf7[0], leaf7[2], leaf7[0], leaf7[1], leaf7[2], leaf7[3]);
PDEBUG("0x7: %x,%x,%x,%x\n", leaf7[0], leaf7[1], leaf7[2], leaf7[3]);
bool snc = (leaf7[2] & 1u<< 6) && /* AVX-512VBMI2 */
(leaf7[2] & 1u<< 8) && /* AVX-512GFNI */
(leaf7[2] & 1u<< 9) && /* AVX-512VAES */
(leaf7[2] & 1u<<10) && /* AVX-512VPCLMULQDQ */
(leaf7[2] & 1u<<12); /* AVX-512BITALG */
return (snc);
}
bool has_avx512_bf16(void)
{
/* based on https://software.intel.com/sites/default/files/managed/c5/15/architecture-instruction-set-extensions-programming-reference.pdf */
/* leaf 7 - AVX-512 features */
uint32_t leaf7[4]={0x7,0x0,0x1,0x0};
__cpuid_count(leaf7[0], leaf7[2], leaf7[0], leaf7[1], leaf7[2], leaf7[3]);
PDEBUG("0x7010: %x,%x,%x,%x\n", leaf7[0], leaf7[1], leaf7[2], leaf7[3]);
bool bf16 = (leaf7[0] & 1u<<5); /* AVX-512BF16 */
return (bf16);
}
int vpu_count(void)
{
/* I have no idea what to do for non-Intel CPUs. */
if ( !is_intel() ) {
fprintf(stderr, "Intel CPU required\n");
return 0;
}
/* Note that Skylake, Cascade Lake and Cooper Lake all share the same
* CPUID bits for model and extended model. */
if ( is_skylake_server() && has_avx512_skx() ) {
char cpu_name[48] = {0};
get_cpu_name48(cpu_name);
PDEBUG("Skylake AVX-512 detected\n");
PDEBUG("cpu_name = %s\n", cpu_name);
PDEBUG("cpu_name[9] = %c\n", cpu_name[9]);
PDEBUG("cpu_name[17] = %c\n", cpu_name[17]);
/* Skylake-X series: "Intel(R) Core (TM)..." */
/* Cascade Lake-X series: "Intel(R) Core (TM)..." */
if (cpu_name[9] == 'C') {
return 2;
}
else if (cpu_name[9] == 'X') {
/* Cooper Lake: Intel(R) Xeon(R) Platinum _3__H... */
if (cpu_name[23] == '3') {
#ifdef DEBUG
fprintf(stderr,"Cooper Lake detected, BF16 %s\n", has_avx512_bf16() ? "found" : "not found" );
#endif
return 2;
}
/* Xeon Scalable series: "Intel(R) Xeon(R) Platinum..." */
if (cpu_name[17] == 'P') {
return 2;
}
/* Xeon Scalable series: "Intel(R) Xeon(R) Gold..." */
else if (cpu_name[17] == 'G') {
/* 61xx */
if (cpu_name[22] == '6') {
return 2;
/* 5122 */
} else if (cpu_name[22] == '5' && cpu_name[24] == '2' && cpu_name[25] == '2') {
return 2;
/* 51xx */
} else {
return 1;
}
}
/* Xeon Scalable series: "Intel(R) Xeon(R) Silver..." */
else if (cpu_name[17] == 'S') {
return 1;
}
/* Xeon Scalable series: "Intel(R) Xeon(R) Bronze..." */
else if (cpu_name[17] == 'B') {
return 1;
}
/* Xeon W series: "Intel(R) Xeon(R) W..." */
else if (cpu_name[17] == 'W') {
/* 2102 or 2104 are Skylake X w/ 1 FMA */
if (cpu_name[21] == '0') {
return 1;
}
/* 212x and higher are Skylake-X w/ 2 FMAs */
/* 22xx and 32xx are Cascade Lake-X w/ 2 FMAs */
else {
return 2;
}
}
/* Xeon D series: "Intel(R) Xeon(R) D-2xxx..." */
else if (cpu_name[17] == 'D') {
/* 21xx series is Skylake-X */
if (cpu_name[19] == '2' && cpu_name[20] == '1') {
return 1;
}
}
}
/* Pre-production parts: Genuine Intel(R) CPU 0000 */
else if (cpu_name[0] == 'G' && cpu_name[21] == '0' && cpu_name[22] == '0' && cpu_name[23] == '0' && cpu_name[24] == '0') {
return 2;
}
/* Default to 2 FMAs and hope for the best... */
else {
char cpu_name[48] = {0};
get_cpu_name48(cpu_name);
fprintf(stderr,"UNSUPPORTED SKU! cpu_name = %s\n", cpu_name);
return 2;
}
}
#ifdef SUPPORT_SAPPHIRE_RAPIDS
else if ( is_sapphire_rapids() ) {
PDEBUG("Sapphire Rapids server detected\n");
#ifdef DEBUG
char cpu_name[48] = {0};
get_cpu_name48(cpu_name);
PDEBUG("cpu_name = %s\n", cpu_name);
if ( has_avx512_skx() ) {
PDEBUG("AVX-512 F,CD,DQ,BW,VL detected\n");
}
if ( has_avx512_vpopcntdq() ) {
PDEBUG("AVX-512 VPOPCNTDQ detected\n");
}
if ( has_avx512_snc() ) {
PDEBUG("AVX-512 VBMI2,GFNI,VAES,VPCLMULQDQ,BITALG detected\n");
}
#endif
return 2; // assume same as Ice Lake server
}
#endif /* SUPPORT_SAPPHIRE_RAPIDS */
#ifdef SUPPORT_TIGERLAKE
else if ( is_tigerlake_client() ) {
PDEBUG("Tiger Lake client detected\n");
#ifdef DEBUG
char cpu_name[48] = {0};
get_cpu_name48(cpu_name);
PDEBUG("cpu_name = %s\n", cpu_name);
if ( has_avx512_skx() ) {
PDEBUG("AVX-512 F,CD,DQ,BW,VL detected\n");
}
if ( has_avx512_vpopcntdq() ) {
PDEBUG("AVX-512 VPOPCNTDQ detected\n");
}
if ( has_avx512_snc() ) {
PDEBUG("AVX-512 VBMI2,GFNI,VAES,VPCLMULQDQ,BITALG detected\n");
}
#endif
return 1;
}
#endif /* SUPPORT_TIGERLAKE */
#ifdef SUPPORT_ICELAKE
else if ( is_icelake_client() ) {
PDEBUG("Ice Lake client detected\n");
#ifdef DEBUG
char cpu_name[48] = {0};
get_cpu_name48(cpu_name);
PDEBUG("cpu_name = %s\n", cpu_name);
if ( has_avx512_skx() ) {
PDEBUG("AVX-512 F,CD,DQ,BW,VL detected\n");
}
if ( has_avx512_vpopcntdq() ) {
PDEBUG("AVX-512 VPOPCNTDQ detected\n");
}
if ( has_avx512_snc() ) {
PDEBUG("AVX-512 VBMI2,GFNI,VAES,VPCLMULQDQ,BITALG detected\n");
}
#endif
/* https://software.intel.com/sites/default/files/managed/9e/bc/64-ia-32-architectures-optimization-manual.pdf says:
* "All processors based on Ice Lake Client microarchitecture contain a single 512-bit FMA unit,
* whereas some of the processors based on Skylake Server microarchitecture contain two such units." */
return 1;
}
#endif /* SUPPORT_ICELAKE */
#ifdef SUPPORT_ICELAKE_SERVER
else if ( is_icelake_server() ) {
PDEBUG("Ice Lake server detected\n");
#ifdef DEBUG
char cpu_name[48] = {0};
get_cpu_name48(cpu_name);
PDEBUG("cpu_name = %s\n", cpu_name);
if ( has_avx512_skx() ) {
PDEBUG("AVX-512 F,CD,DQ,BW,VL detected\n");
}
if ( has_avx512_vpopcntdq() ) {
PDEBUG("AVX-512 VPOPCNTDQ detected\n");
}
if ( has_avx512_snc() ) {
PDEBUG("AVX-512 VBMI2,GFNI,VAES,VPCLMULQDQ,BITALG detected\n");
}
#endif
return 2;
}
#endif /* SUPPORT_ICELAKE_SERVER */
#ifdef SUPPORT_CANNONLAKE
else if ( is_cannonlake_client() ) {
PDEBUG("Cannon Lake client detected\n");
#ifdef DEBUG
char cpu_name[48] = {0};
get_cpu_name48(cpu_name);
PDEBUG("cpu_name = %s\n", cpu_name);
if ( has_avx512_skx() ) {
PDEBUG("AVX-512 F,CD,DQ,BW,VL detected\n");
}
if ( has_avx512_cnl() ) {
PDEBUG("AVX-512 VBMI,IFMA detected\n");
}
#endif
return 1;
}
#endif /* SUPPORT_CANNONLAKE */
#ifdef SUPPORT_XEON_PHI
else if ( is_knl() || is_knm() ) {
return 2;
}
#endif /* SUPPORT_XEON_PHI */
#ifdef SUPPORT_PRE_AVX512
else if ( has_avx2() ) {
PDEBUG("AVX2 detected\n");
#ifdef DEBUG
char cpu_name[48] = {0};
get_cpu_name48(cpu_name);
PDEBUG("cpu_name = %s\n", cpu_name);
if ( has_fma3() ) {
PDEBUG("FMA3 detected\n");
}
if ( has_fma4() ) {
PDEBUG("FMA4 detected\n");
}
#endif
return 0;
}
#endif /* SUPPORT_PRE_AVX512 */
#ifdef DEBUG
else {
char cpu_name[48] = {0};
get_cpu_name48(cpu_name);
fprintf(stderr,"UNSUPPORTED CPU! cpu_name = %s\n", cpu_name);
return 0;
}
#endif
return 0;
}