-
Notifications
You must be signed in to change notification settings - Fork 370
/
bli_cpuid.c
1390 lines (1177 loc) · 40.8 KB
/
bli_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
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
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
BLIS
An object-based framework for developing high-performance BLAS-like
libraries.
Copyright (C) 2014, The University of Texas at Austin
Copyright (C) 2018-2020, Advanced Micro Devices, Inc.
Copyright (C) 2019, Dave Love, University of Manchester
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
- Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
- Neither the name(s) of the copyright holder(s) nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#if 0
// Used only during standalone testing of ARM support.
#include "bli_system.h"
#include "bli_type_defs.h"
#include "bli_cpuid.h"
#undef __x86_64__
#undef _M_X64
#undef __i386
#undef _M_IX86
#define __arm__
#endif
#ifdef BLIS_CONFIGURETIME_CPUID
// NOTE: If you need to make any changes to this cpp branch, it's probably
// the case that you also need to modify bli_arch.c, bli_cpuid.c, and
// bli_env.c. Don't forget to update these other files as needed!
// The BLIS_ENABLE_SYSTEM macro must be defined so that the correct cpp
// branch in bli_system.h is processed. (This macro is normally defined in
// bli_config.h.)
#define BLIS_ENABLE_SYSTEM
// Use C-style static inline functions for any static inline functions that
// happen to be defined by the headers below. (This macro is normally defined
// in bli_config_macro_defs.h.)
#define BLIS_INLINE static
// Since we're not building a shared library, we can forgo the use of the
// BLIS_EXPORT_BLIS annotations by #defining them to be nothing. (This macro
// is normally defined in bli_config_macro_defs.h.)
#define BLIS_EXPORT_BLIS
#include "bli_system.h"
#include "bli_type_defs.h"
#include "bli_arch.h"
#include "bli_cpuid.h"
//#include "bli_env.h"
#else
#include "blis.h"
#endif
// -----------------------------------------------------------------------------
#if defined(__x86_64__) || defined(_M_X64) || defined(__i386) || defined(_M_IX86)
#include "cpuid.h"
arch_t bli_cpuid_query_id( void )
{
uint32_t vendor, family, model, features;
// Call the CPUID instruction and parse its results into a family id,
// model id, and a feature bit field. The return value encodes the
// vendor.
vendor = bli_cpuid_query( &family, &model, &features );
#if 0
printf( "vendor = %s\n", vendor==1 ? "AMD": "INTEL" );
printf("family = %x\n", family );
printf( "model = %x\n", model );
printf( "features = %x\n", features );
#endif
if ( vendor == VENDOR_INTEL )
{
// Check for each Intel configuration that is enabled, check for that
// microarchitecture. We check from most recent to most dated.
#ifdef BLIS_CONFIG_SKX
if ( bli_cpuid_is_skx( family, model, features ) )
return BLIS_ARCH_SKX;
#endif
#ifdef BLIS_CONFIG_KNL
if ( bli_cpuid_is_knl( family, model, features ) )
return BLIS_ARCH_KNL;
#endif
#ifdef BLIS_CONFIG_HASWELL
if ( bli_cpuid_is_haswell( family, model, features ) )
return BLIS_ARCH_HASWELL;
#endif
#ifdef BLIS_CONFIG_SANDYBRIDGE
if ( bli_cpuid_is_sandybridge( family, model, features ) )
return BLIS_ARCH_SANDYBRIDGE;
#endif
#ifdef BLIS_CONFIG_PENRYN
if ( bli_cpuid_is_penryn( family, model, features ) )
return BLIS_ARCH_PENRYN;
#endif
// If none of the other sub-configurations were detected, return
// the 'generic' arch_t id value.
return BLIS_ARCH_GENERIC;
}
else if ( vendor == VENDOR_AMD )
{
// Check for each AMD configuration that is enabled, check for that
// microarchitecture. We check from most recent to most dated.
#ifdef BLIS_CONFIG_ZEN3
if ( bli_cpuid_is_zen3( family, model, features ) )
return BLIS_ARCH_ZEN3;
#endif
#ifdef BLIS_CONFIG_ZEN2
if ( bli_cpuid_is_zen2( family, model, features ) )
return BLIS_ARCH_ZEN2;
#endif
#ifdef BLIS_CONFIG_ZEN
if ( bli_cpuid_is_zen( family, model, features ) )
return BLIS_ARCH_ZEN;
#endif
#ifdef BLIS_CONFIG_EXCAVATOR
if ( bli_cpuid_is_excavator( family, model, features ) )
return BLIS_ARCH_EXCAVATOR;
#endif
#ifdef BLIS_CONFIG_STEAMROLLER
if ( bli_cpuid_is_steamroller( family, model, features ) )
return BLIS_ARCH_STEAMROLLER;
#endif
#ifdef BLIS_CONFIG_PILEDRIVER
if ( bli_cpuid_is_piledriver( family, model, features ) )
return BLIS_ARCH_PILEDRIVER;
#endif
#ifdef BLIS_CONFIG_BULLDOZER
if ( bli_cpuid_is_bulldozer( family, model, features ) )
return BLIS_ARCH_BULLDOZER;
#endif
// If none of the other sub-configurations were detected, return
// the 'generic' arch_t id value.
return BLIS_ARCH_GENERIC;
}
else if ( vendor == VENDOR_UNKNOWN )
{
return BLIS_ARCH_GENERIC;
}
return BLIS_ARCH_GENERIC;
}
// -----------------------------------------------------------------------------
bool bli_cpuid_is_skx
(
uint32_t family,
uint32_t model,
uint32_t features
)
{
// Check for expected CPU features.
const uint32_t expected = FEATURE_AVX |
FEATURE_FMA3 |
FEATURE_AVX2 |
FEATURE_AVX512F |
FEATURE_AVX512DQ |
FEATURE_AVX512BW |
FEATURE_AVX512VL ;
int nvpu = vpu_count();
if ( bli_cpuid_has_features( features, expected ) )
{
switch ( nvpu )
{
case 1:
bli_arch_log( "Hardware has 1 FMA unit; using 'haswell' (not 'skx') sub-config.\n" );
return FALSE;
case 2:
bli_arch_log( "Hardware has 2 FMA units; using 'skx' sub-config.\n" );
return TRUE;
default:
bli_arch_log( "Number of FMA units unknown; using 'haswell' (not 'skx') config.\n" );
return FALSE;
}
}
else
return FALSE;
return TRUE;
}
bool bli_cpuid_is_knl
(
uint32_t family,
uint32_t model,
uint32_t features
)
{
// Check for expected CPU features.
const uint32_t expected = FEATURE_AVX |
FEATURE_FMA3 |
FEATURE_AVX2 |
FEATURE_AVX512F |
FEATURE_AVX512PF;
if ( !bli_cpuid_has_features( features, expected ) ) return FALSE;
return TRUE;
}
bool bli_cpuid_is_haswell
(
uint32_t family,
uint32_t model,
uint32_t features
)
{
// Check for expected CPU features.
const uint32_t expected = FEATURE_AVX |
FEATURE_FMA3 |
FEATURE_AVX2;
if ( !bli_cpuid_has_features( features, expected ) ) return FALSE;
return TRUE;
}
bool bli_cpuid_is_sandybridge
(
uint32_t family,
uint32_t model,
uint32_t features
)
{
// Check for expected CPU features.
const uint32_t expected = FEATURE_AVX;
if ( !bli_cpuid_has_features( features, expected ) ) return FALSE;
return TRUE;
}
bool bli_cpuid_is_penryn
(
uint32_t family,
uint32_t model,
uint32_t features
)
{
// Check for expected CPU features.
const uint32_t expected = FEATURE_SSE3 |
FEATURE_SSSE3;
if ( !bli_cpuid_has_features( features, expected ) ) return FALSE;
return TRUE;
}
// -----------------------------------------------------------------------------
bool bli_cpuid_is_zen3
(
uint32_t family,
uint32_t model,
uint32_t features
)
{
// Check for expected CPU features.
const uint32_t expected = FEATURE_AVX |
FEATURE_FMA3 |
FEATURE_AVX2;
if ( !bli_cpuid_has_features( features, expected ) ) return FALSE;
// All Zen3 cores have a family of 0x19.
if ( family != 0x19 ) return FALSE;
// Finally, check for specific models:
// - 0x00 ~ 0xff
// NOTE: We accept any model because the family 25 (0x19) is unique.
const bool is_arch
=
( 0x00 <= model && model <= 0xff );
if ( !is_arch ) return FALSE;
return TRUE;
}
bool bli_cpuid_is_zen2
(
uint32_t family,
uint32_t model,
uint32_t features
)
{
// Check for expected CPU features.
const uint32_t expected = FEATURE_AVX |
FEATURE_FMA3 |
FEATURE_AVX2;
if ( !bli_cpuid_has_features( features, expected ) ) return FALSE;
// All Zen2 cores have a family of 0x17.
if ( family != 0x17 ) return FALSE;
// Finally, check for specific models:
// - 0x30 ~ 0xff
// NOTE: We must check model because the family 23 (0x17) is shared with
// zen.
const bool is_arch
=
( 0x30 <= model && model <= 0xff );
if ( !is_arch ) return FALSE;
return TRUE;
}
bool bli_cpuid_is_zen
(
uint32_t family,
uint32_t model,
uint32_t features
)
{
// Check for expected CPU features.
const uint32_t expected = FEATURE_AVX |
FEATURE_FMA3 |
FEATURE_AVX2;
if ( !bli_cpuid_has_features( features, expected ) ) return FALSE;
// All Zen cores have a family of 0x17.
if ( family != 0x17 ) return FALSE;
// Finally, check for specific models:
// - 0x00 ~ 0x2f
// NOTE: We must check model because the family 23 (0x17) is shared with
// zen2.
const bool is_arch
=
( 0x00 <= model && model <= 0x2f );
if ( !is_arch ) return FALSE;
return TRUE;
}
bool bli_cpuid_is_excavator
(
uint32_t family,
uint32_t model,
uint32_t features
)
{
// Check for expected CPU features.
const uint32_t expected = FEATURE_AVX |
FEATURE_FMA3 |
FEATURE_AVX2;
if ( !bli_cpuid_has_features( features, expected ) ) return FALSE;
// All Excavator cores have a family of 0x15.
if ( family != 0x15 ) return FALSE;
// Finally, check for specific models:
// - 0x60 ~ 0x7f
const bool is_arch
=
( 0x60 <= model && model <= 0x7f );
if ( !is_arch ) return FALSE;
return TRUE;
}
bool bli_cpuid_is_steamroller
(
uint32_t family,
uint32_t model,
uint32_t features
)
{
// Check for expected CPU features.
const uint32_t expected = FEATURE_AVX |
FEATURE_FMA3 |
FEATURE_FMA4;
if ( !bli_cpuid_has_features( features, expected ) ) return FALSE;
// All Steamroller cores have a family of 0x15.
if ( family != 0x15 ) return FALSE;
// Finally, check for specific models:
// - 0x30 ~ 0x3f
const bool is_arch
=
( 0x30 <= model && model <= 0x3f );
if ( !is_arch ) return FALSE;
return TRUE;
}
bool bli_cpuid_is_piledriver
(
uint32_t family,
uint32_t model,
uint32_t features
)
{
// Check for expected CPU features.
const uint32_t expected = FEATURE_AVX |
FEATURE_FMA3 |
FEATURE_FMA4;
if ( !bli_cpuid_has_features( features, expected ) ) return FALSE;
// All Piledriver cores have a family of 0x15.
if ( family != 0x15 ) return FALSE;
// Finally, check for specific models:
// - 0x02
// - 0x10 ~ 0x1f
const bool is_arch
=
model == 0x02 || ( 0x10 <= model && model <= 0x1f );
if ( !is_arch ) return FALSE;
return TRUE;
}
bool bli_cpuid_is_bulldozer
(
uint32_t family,
uint32_t model,
uint32_t features
)
{
// Check for expected CPU features.
const uint32_t expected = FEATURE_AVX |
FEATURE_FMA4;
if ( !bli_cpuid_has_features( features, expected ) ) return FALSE;
// All Bulldozer cores have a family of 0x15.
if ( family != 0x15 ) return FALSE;
// Finally, check for specific models:
// - 0x00
// - 0x01
const bool is_arch
=
( model == 0x00 || model == 0x01 );
if ( !is_arch ) return FALSE;
return TRUE;
}
#elif defined(__aarch64__) || defined(__arm__) || defined(_M_ARM) || defined(_ARCH_PPC)
// courtesy OpenBSD
#define ARM_CPU_PART_CORTEX_A5 0xc05
#define ARM_CPU_PART_CORTEX_A7 0xc07
#define ARM_CPU_PART_CORTEX_A8 0xc08
#define ARM_CPU_PART_CORTEX_A9 0xc09
#define ARM_CPU_PART_CORTEX_A12 0xc0d
#define ARM_CPU_PART_CORTEX_A15 0xc0f
#define ARM_CPU_PART_CORTEX_A17 0xc0e
#define ARM_CPU_PART_CORTEX_A32 0xd01
arch_t bli_cpuid_query_id( void )
{
uint32_t vendor, model, part, features;
vendor = bli_cpuid_query( &model, &part, &features );
#if 0
printf( "vendor = %u\n", vendor );
printf( "model = %u\n", model );
printf( "part = 0x%x\n", part );
printf( "features = %u\n", features );
#endif
if ( vendor == VENDOR_ARM )
{
if ( model == MODEL_ARMV8 )
{
return part;
// Check for each ARMv8 configuration that is enabled, check for that
// microarchitecture. We check from most recent to most dated.
// If none of the other sub-configurations were detected, return
// the 'generic' arch_t id value.
return BLIS_ARCH_GENERIC;
}
else if ( model == MODEL_ARMV7 )
{
// Check for each ARMv7 configuration that is enabled, check for that
// microarchitecture. We check from most recent to most dated.
#ifdef BLIS_CONFIG_CORTEXA15
if ( bli_cpuid_is_cortexa15( model, part, features ) )
return BLIS_ARCH_CORTEXA15;
#endif
#ifdef BLIS_CONFIG_CORTEXA9
if ( bli_cpuid_is_cortexa9( model, part, features ) )
return BLIS_ARCH_CORTEXA9;
#endif
// If none of the other sub-configurations were detected, return
// the 'generic' arch_t id value.
return BLIS_ARCH_GENERIC;
}
}
else if ( vendor == VENDOR_IBM )
{
if ( model == MODEL_POWER7)
return BLIS_ARCH_POWER7;
else if ( model == MODEL_POWER9)
return BLIS_ARCH_POWER9;
else if ( model == MODEL_POWER10)
return BLIS_ARCH_POWER10;
}
return BLIS_ARCH_GENERIC;
}
bool bli_cpuid_is_cortexa15
(
uint32_t model,
uint32_t part,
uint32_t features
)
{
// Check for expected CPU features.
const uint32_t expected = FEATURE_NEON;
return bli_cpuid_has_features( features, expected ) && part == ARM_CPU_PART_CORTEX_A15;
}
bool bli_cpuid_is_cortexa9
(
uint32_t model,
uint32_t part,
uint32_t features
)
{
// Check for expected CPU features.
const uint32_t expected = FEATURE_NEON;
return bli_cpuid_has_features( features, expected ) && part == ARM_CPU_PART_CORTEX_A9;
}
#endif
// -----------------------------------------------------------------------------
//
// This section of the file was based off of cpuid.cxx from TBLIS [1].
//
// [1] https://github.com/devinamatthews/tblis
//
/*
Copyright (C) 2017, The University of Texas at Austin
Copyright (C) 2017, Devin Matthews
Copyright (C) 2018 - 2019, Advanced Micro Devices, Inc.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
- Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
- Neither the name(s) of the copyright holder(s) nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#if defined(__x86_64__) || defined(_M_X64) || defined(__i386) || defined(_M_IX86)
enum
{
// input register(s) output register
FEATURE_MASK_SSE3 = (1u<< 0), // cpuid[eax=1] :ecx[0]
FEATURE_MASK_SSSE3 = (1u<< 9), // cpuid[eax=1] :ecx[9]
FEATURE_MASK_SSE41 = (1u<<19), // cpuid[eax=1] :ecx[19]
FEATURE_MASK_SSE42 = (1u<<20), // cpuid[eax=1] :ecx[20]
FEATURE_MASK_AVX = (1u<<28), // cpuid[eax=1] :ecx[28]
FEATURE_MASK_AVX2 = (1u<< 5), // cpuid[eax=7,ecx=0] :ebx[5]
FEATURE_MASK_FMA3 = (1u<<12), // cpuid[eax=1] :ecx[12]
FEATURE_MASK_FMA4 = (1u<<16), // cpuid[eax=0x80000001]:ecx[16]
FEATURE_MASK_AVX512F = (1u<<16), // cpuid[eax=7,ecx=0] :ebx[16]
FEATURE_MASK_AVX512DQ = (1u<<17), // cpuid[eax=7,ecx=0] :ebx[17]
FEATURE_MASK_AVX512PF = (1u<<26), // cpuid[eax=7,ecx=0] :ebx[26]
FEATURE_MASK_AVX512ER = (1u<<27), // cpuid[eax=7,ecx=0] :ebx[27]
FEATURE_MASK_AVX512CD = (1u<<28), // cpuid[eax=7,ecx=0] :ebx[28]
FEATURE_MASK_AVX512BW = (1u<<30), // cpuid[eax=7,ecx=0] :ebx[30]
FEATURE_MASK_AVX512VL = (1u<<31), // cpuid[eax=7,ecx=0] :ebx[31]
FEATURE_MASK_XGETBV = (1u<<26)|
(1u<<27), // cpuid[eax=1] :ecx[27:26]
XGETBV_MASK_XMM = 0x02u, // xcr0[1]
XGETBV_MASK_YMM = 0x04u, // xcr0[2]
XGETBV_MASK_ZMM = 0xe0u // xcr0[7:5]
};
uint32_t bli_cpuid_query
(
uint32_t* family,
uint32_t* model,
uint32_t* features
)
{
uint32_t eax, ebx, ecx, edx;
uint32_t old_model = 0;
uint32_t old_family = 0;
uint32_t ext_model = 0;
uint32_t ext_family = 0;
*family = 0;
*model = 0;
*features = 0;
//fprintf( stderr, "checking cpuid\n" );
uint32_t cpuid_max = __get_cpuid_max( 0, 0 );
uint32_t cpuid_max_ext = __get_cpuid_max( 0x80000000u, 0 );
//fprintf( stderr, "max cpuid leaf: %d\n", cpuid_max );
//fprintf( stderr, "max extended cpuid leaf: %08x\n", cpuid_max_ext );
if ( cpuid_max < 1 ) return VENDOR_UNKNOWN;
// The fourth '0' serves as the NULL-terminator for the vendor string.
uint32_t vendor_string[4] = { 0, 0, 0, 0 };
// This is actually a macro that modifies the last four operands,
// hence why they are not passed by address.
__cpuid( 0, eax, vendor_string[0],
vendor_string[2],
vendor_string[1] );
// Check extended feature bits for post-AVX2 features.
if ( cpuid_max >= 7 )
{
// This is actually a macro that modifies the last four operands,
// hence why they are not passed by address.
__cpuid_count( 7, 0, eax, ebx, ecx, edx );
//fprintf( stderr, "cpuid leaf 7:\n" );
//print_binary( eax );
//print_binary( ebx );
//print_binary( ecx );
//print_binary( edx );
if ( bli_cpuid_has_features( ebx, FEATURE_MASK_AVX2 ) ) *features |= FEATURE_AVX2;
if ( bli_cpuid_has_features( ebx, FEATURE_MASK_AVX512F ) ) *features |= FEATURE_AVX512F;
if ( bli_cpuid_has_features( ebx, FEATURE_MASK_AVX512DQ ) ) *features |= FEATURE_AVX512DQ;
if ( bli_cpuid_has_features( ebx, FEATURE_MASK_AVX512PF ) ) *features |= FEATURE_AVX512PF;
if ( bli_cpuid_has_features( ebx, FEATURE_MASK_AVX512ER ) ) *features |= FEATURE_AVX512ER;
if ( bli_cpuid_has_features( ebx, FEATURE_MASK_AVX512CD ) ) *features |= FEATURE_AVX512CD;
if ( bli_cpuid_has_features( ebx, FEATURE_MASK_AVX512BW ) ) *features |= FEATURE_AVX512BW;
if ( bli_cpuid_has_features( ebx, FEATURE_MASK_AVX512VL ) ) *features |= FEATURE_AVX512VL;
}
// Check extended processor info / features bits for AMD-specific features.
if ( cpuid_max_ext >= 0x80000001u )
{
// This is actually a macro that modifies the last four operands,
// hence why they are not passed by address.
__cpuid( 0x80000001u, eax, ebx, ecx, edx );
//fprintf(stderr, "extended cpuid leaf 0x80000001:\n");
//print_binary(eax);
//print_binary(ebx);
//print_binary(ecx);
//print_binary(edx);
if ( bli_cpuid_has_features( ecx, FEATURE_MASK_FMA4 ) ) *features |= FEATURE_FMA4;
}
// Unconditionally check processor info / features bits.
{
// This is actually a macro that modifies the last four operands,
// hence why they are not passed by address.
__cpuid( 1, eax, ebx, ecx, edx );
//fprintf(stderr, "cpuid leaf 1:\n");
//print_binary(eax);
//print_binary(ebx);
//print_binary(ecx);
//print_binary(edx);
/*
cpuid(eax=1): eax[27:0]
3: 0 - Stepping
7: 4 - Model
11: 8 - Family
13:12 - Processor Type
19:16 - Extended Model
27:20 - Extended Family
Intel and AMD have suggested applications to display the family of a
CPU as the sum of the "Family" and the "Extended Family" fields shown
above, and the model as the sum of the "Model" and the 4-bit
left-shifted "Extended Model" fields. If "Family" is different than
6 or 15, only the "Family" and "Model" fields should be used while the
"Extended Family" and "Extended Model" bits are reserved. If "Family"
is set to 15, then "Extended Family" and the 4-bit left-shifted
"Extended Model" should be added to the respective base values, and if
"Family" is set to 6, then only the 4-bit left-shifted "Extended Model"
should be added to "Model".
*/
old_model = ( eax >> 4 ) & ( 0xF ); // bits 7:4
old_family = ( eax >> 8 ) & ( 0xF ); // bits 11:8
ext_model = ( eax >> 16 ) & ( 0xF ); // bits 19:16
ext_family = ( eax >> 20 ) & ( 0xFF ); // bits 27:20
// Set the display model and family values based on the original family
// value. See explanation above.
if ( old_family == 6 )
{
*model = ( ext_model << 4 ) + old_model;
*family = old_family;
}
else if ( old_family == 15 )
{
*model = ( ext_model << 4 ) + old_model;
*family = ( ext_family ) + old_family;
}
else
{
*model = old_model;
*family = old_family;
}
// Check for SSE, AVX, and FMA3 features.
if ( bli_cpuid_has_features( ecx, FEATURE_MASK_SSE3 ) ) *features |= FEATURE_SSE3;
if ( bli_cpuid_has_features( ecx, FEATURE_MASK_SSSE3 ) ) *features |= FEATURE_SSSE3;
if ( bli_cpuid_has_features( ecx, FEATURE_MASK_SSE41 ) ) *features |= FEATURE_SSE41;
if ( bli_cpuid_has_features( ecx, FEATURE_MASK_SSE42 ) ) *features |= FEATURE_SSE42;
if ( bli_cpuid_has_features( ecx, FEATURE_MASK_AVX ) ) *features |= FEATURE_AVX;
if ( bli_cpuid_has_features( ecx, FEATURE_MASK_FMA3 ) ) *features |= FEATURE_FMA3;
// Check whether the hardware supports xsave/xrestor/xsetbv/xgetbv AND
// support for these is enabled by the OS. If so, then we proceed with
// checking that various register-state saving features are available.
if ( bli_cpuid_has_features( ecx, FEATURE_MASK_XGETBV ) )
{
uint32_t xcr = 0;
// Call xgetbv to get xcr0 (the extended control register) copied
// to [edx:eax]. This encodes whether software supports various
// register state-saving features.
__asm__ __volatile__
(
".byte 0x0F, 0x01, 0xD0"
: "=a" (eax),
"=d" (edx)
: "c" (xcr)
: "cc"
);
//fprintf(stderr, "xcr0:\n");
//print_binary(eax);
//print_binary(edx);
//fprintf(stderr, "xgetbv: xmm: %d\n", bli_cpuid_has_features(eax, XGETBV_MASK_XMM));
//fprintf(stderr, "xgetbv: ymm: %d\n", bli_cpuid_has_features(eax, XGETBV_MASK_XMM|
// XGETBV_MASK_YMM));
//fprintf(stderr, "xgetbv: zmm: %d\n", bli_cpuid_has_features(eax, XGETBV_MASK_XMM|
// XGETBV_MASK_YMM|
// XGETBV_MASK_ZMM));
// The OS can manage the state of 512-bit zmm (AVX-512) registers
// only if the xcr[7:5] bits are set. If they are not set, then
// clear all feature bits related to AVX-512.
if ( !bli_cpuid_has_features( eax, XGETBV_MASK_XMM |
XGETBV_MASK_YMM |
XGETBV_MASK_ZMM ) )
{
*features &= ~( FEATURE_AVX512F |
FEATURE_AVX512DQ |
FEATURE_AVX512PF |
FEATURE_AVX512ER |
FEATURE_AVX512CD |
FEATURE_AVX512BW |
FEATURE_AVX512VL );
}
// The OS can manage the state of 256-bit ymm (AVX) registers
// only if the xcr[2] bit is set. If it is not set, then
// clear all feature bits related to AVX.
if ( !bli_cpuid_has_features( eax, XGETBV_MASK_XMM |
XGETBV_MASK_YMM ) )
{
*features &= ~( FEATURE_AVX |
FEATURE_AVX2 |
FEATURE_FMA3 |
FEATURE_FMA4 );
}
// The OS can manage the state of 128-bit xmm (SSE) registers
// only if the xcr[1] bit is set. If it is not set, then
// clear all feature bits related to SSE (which means the
// entire bitfield is clear).
if ( !bli_cpuid_has_features( eax, XGETBV_MASK_XMM ) )
{
*features = 0;
}
}
else
{
// If the hardware does not support xsave/xrestor/xsetbv/xgetbv,
// OR these features are not enabled by the OS, then we clear
// the bitfield, because it means that not even xmm support is
// present.
//fprintf(stderr, "xgetbv: no\n");
features = 0;
}
}
//fprintf(stderr, "vendor: %12s\n", vendor_string);
//fprintf(stderr, "family: %d\n", family);
//fprintf(stderr, "model: %d\n", model);
//fprintf(stderr, "sse3: %d\n", bli_cpuid_has_features(features, FEATURE_SSE3));
//fprintf(stderr, "ssse3: %d\n", bli_cpuid_has_features(features, FEATURE_SSSE3));
//fprintf(stderr, "sse4.1: %d\n", bli_cpuid_has_features(features, FEATURE_SSE41));
//fprintf(stderr, "sse4.2: %d\n", bli_cpuid_has_features(features, FEATURE_SSE42));
//fprintf(stderr, "avx: %d\n", bli_cpuid_has_features(features, FEATURE_AVX));
//fprintf(stderr, "avx2: %d\n", bli_cpuid_has_features(features, FEATURE_AVX2));
//fprintf(stderr, "fma3: %d\n", bli_cpuid_has_features(features, FEATURE_FMA3));
//fprintf(stderr, "fma4: %d\n", bli_cpuid_has_features(features, FEATURE_FMA4));
//fprintf(stderr, "avx512f: %d\n", bli_cpuid_has_features(features, FEATURE_AVX512F));
//fprintf(stderr, "avx512pf: %d\n", bli_cpuid_has_features(features, FEATURE_AVX512PF));
//fprintf(stderr, "avx512dq: %d\n", bli_cpuid_has_features(features, FEATURE_AVX512DQ));
// Check the vendor string and return a value to indicate Intel or AMD.
if ( strcmp( ( char* )vendor_string, "AuthenticAMD" ) == 0 )
return VENDOR_AMD;
else if ( strcmp( ( char* )vendor_string, "GenuineIntel" ) == 0 )
return VENDOR_INTEL;
else
return VENDOR_UNKNOWN;
}
void get_cpu_name( char *cpu_name )
{
uint32_t eax, ebx, ecx, edx;
__cpuid( 0x80000002u, eax, ebx, ecx, edx );
//printf("%x %x %x %x\n", eax, ebx, ecx, edx);
*( uint32_t* )&cpu_name[0 + 0] = eax;
*( uint32_t* )&cpu_name[0 + 4] = ebx;
*( uint32_t* )&cpu_name[0 + 8] = ecx;
*( uint32_t* )&cpu_name[0 +12] = edx;
__cpuid( 0x80000003u, eax, ebx, ecx, edx );
//printf("%x %x %x %x\n", 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;
__cpuid( 0x80000004u, eax, ebx, ecx, edx );
//printf("%x %x %x %x\n", 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;
}
// Return the number of FMA units _assuming avx512 is supported_.
// This needs updating for new processor types, sigh.
// See https://ark.intel.com/content/www/us/en/ark.html#@Processors
// and also https://github.com/jeffhammond/vpu-count
int vpu_count( void )
{
char cpu_name[48] = {};
char* loc;
char model_num[5];
int sku;
get_cpu_name( cpu_name );
if ( strstr( cpu_name, "Intel(R) Xeon(R)" ) != NULL )
{
if (( loc = strstr( cpu_name, "Platinum" ) ))
return 2;
if ( loc == NULL )
loc = strstr( cpu_name, "Gold" ); // 1 or 2, tested below
if ( loc == NULL )
if (( loc = strstr( cpu_name, "Silver" ) ))
return 1;
if ( loc == NULL )
if (( loc = strstr( cpu_name, "Bronze" ) ))
return 1;
if ( loc == NULL )
loc = strstr( cpu_name, "W" );
if ( loc == NULL )
if (( loc = strstr( cpu_name, "D" ) ))
// Fixme: May be wrong
// <https://github.com/jeffhammond/vpu-count/issues/3#issuecomment-542044651>
return 1;
if ( loc == NULL )
return -1;
// We may have W-nnnn rather than, say, Gold nnnn
if ( 'W' == *loc && '-' == *(loc+1) )
loc++;
else
loc = strstr( loc+1, " " );
if ( loc == NULL )
return -1;
strncpy( model_num, loc+1, 4 );
model_num[4] = '\0'; // Things like i9-10900X matched above
sku = atoi( model_num );
// These were derived from ARK listings as of 2019-10-09, but
// may not be complete, especially as the ARK Skylake listing
// seems to be limited.
if ( 8199 >= sku && sku >= 8100 ) return 2;
else if ( 6199 >= sku && sku >= 6100 ) return 2;
else if ( sku == 5122 ) return 2;
else if ( 6299 >= sku && sku >= 6200 ) return 2; // Cascade Lake Gold
else if ( 5299 >= sku && sku >= 5200 ) return 1; // Cascade Lake Gold
else if ( 5199 >= sku && sku >= 5100 ) return 1;
else if ( 4199 >= sku && sku >= 4100 ) return 1;
else if ( 3199 >= sku && sku >= 3100 ) return 1;
else if ( 3299 >= sku && sku >= 3200 ) return 2; // Cascade Lake W
else if ( 2299 >= sku && sku >= 2200 ) return 2; // Cascade Lake W
else if ( 2199 >= sku && sku >= 2120 ) return 2;
else if ( 2102 == sku || sku == 2104 ) return 2; // Gold exceptions
else if ( 2119 >= sku && sku >= 2100 ) return 1;
else return -1;
}
else if ( strstr( cpu_name, "Intel(R) Core(TM)" ) != NULL )
return 2; // All i7/i9 with avx512?
else
{
return -1;
}
}
#elif defined(__aarch64__)