110110# define SHOULD_PARSE_CPUID_L7S1
111111#endif
112112
113+ #if defined(SHOULD_PARSE_CPUID_L7S1 ) && !defined(SHOULD_PARSE_CPUID_L7 )
114+ #error "SHOULD_PARSE_CPUID_L7S1 requires SHOULD_PARSE_CPUID_L7"
115+ #endif
116+ #if defined(SHOULD_PARSE_CPUID_L7S0 ) && !defined(SHOULD_PARSE_CPUID_L7 )
117+ #error "SHOULD_PARSE_CPUID_L7S0 requires SHOULD_PARSE_CPUID_L7"
118+ #endif
119+
120+ #undef SHOULD_PARSE_CPUID_L1
121+
113122/*
114123 * Call __cpuid_count() or equivalent and get
115124 * its EAX, EBX, ECX and EDX output registers.
@@ -133,7 +142,8 @@ get_cpuid_info(uint32_t level /* input eax */,
133142#endif
134143}
135144
136- static uint64_t
145+ #if defined(HAS_XGETBV_SUPPORT ) && defined(SHOULD_PARSE_CPUID_L1 )
146+ static uint64_t /* should only be used after calling cpuid(1, 0, ...) */
137147get_xgetbv (uint32_t index )
138148{
139149 assert (index == 0 ); // only XCR0 is supported for now
@@ -148,6 +158,7 @@ get_xgetbv(uint32_t index)
148158 return 0 ;
149159#endif
150160}
161+ #endif
151162
152163/* Highest Function Parameter and Manufacturer ID (LEAF=0, SUBLEAF=0). */
153164static uint32_t
@@ -159,7 +170,8 @@ detect_cpuid_maxleaf(void)
159170}
160171
161172/* Processor Info and Feature Bits (LEAF=1, SUBLEAF=0). */
162- static void
173+ #ifdef SHOULD_PARSE_CPUID_L1
174+ static void /* should only be used after calling cpuid(1, 0, ...) */
163175detect_cpuid_features (_Py_cpuid_features * flags , uint32_t ecx , uint32_t edx )
164176{
165177 assert (flags -> ready == 0 );
@@ -203,9 +215,11 @@ detect_cpuid_features(_Py_cpuid_features *flags, uint32_t ecx, uint32_t edx)
203215 flags -> osxsave = CPUID_CHECK_REG (ecx , ECX_L1_OSXSAVE );
204216#endif
205217}
218+ #endif
206219
207220/* Extended Feature Bits (LEAF=7, SUBLEAF=0). */
208- static void
221+ #ifdef SHOULD_PARSE_CPUID_L7S0
222+ static void /* should only be used after calling cpuid(7, 0, ...) */
209223detect_cpuid_extended_features_L7S0 (_Py_cpuid_features * flags ,
210224 uint32_t ebx , uint32_t ecx , uint32_t edx )
211225{
@@ -278,9 +292,11 @@ detect_cpuid_extended_features_L7S0(_Py_cpuid_features *flags,
278292#endif
279293#endif // SIMD_AVX512_INSTRUCTIONS_DETECTION_GUARD
280294}
295+ #endif
281296
282297/* Extended Feature Bits (LEAF=7, SUBLEAF=1). */
283- static void
298+ #ifdef SHOULD_PARSE_CPUID_L7S1
299+ static void /* should only be used after calling cpuid(7, 1, ...) */
284300detect_cpuid_extended_features_L7S1 (_Py_cpuid_features * flags ,
285301 uint32_t eax ,
286302 uint32_t ebx ,
@@ -311,23 +327,24 @@ detect_cpuid_extended_features_L7S1(_Py_cpuid_features *flags,
311327#endif
312328#endif // SIMD_AVX_INSTRUCTIONS_DETECTION_GUARD
313329}
330+ #endif
314331
315- static void
332+ #ifdef SHOULD_PARSE_CPUID_L1
333+ static void /* should only be used after calling cpuid(1, 0, ...) */
316334detect_cpuid_xsave_state (_Py_cpuid_features * flags )
317335{
318336 assert (flags -> ready == 0 );
319337 assert (flags -> maxleaf >= 1 );
320338 (void )flags ;
321339 // Keep the ordering and newlines as they are declared in the structure.
322- #ifdef HAS_XGETBV_SUPPORT
323340 uint64_t xcr0 = flags -> osxsave ? get_xgetbv (0 ) : 0 ;
324341 flags -> xcr0_sse = XSAVE_CHECK_REG (xcr0 , XCR0_SSE );
325342 flags -> xcr0_avx = XSAVE_CHECK_REG (xcr0 , XCR0_AVX );
326343 flags -> xcr0_avx512_opmask = XSAVE_CHECK_REG (xcr0 , XCR0_AVX512_OPMASK );
327344 flags -> xcr0_avx512_zmm_hi256 = XSAVE_CHECK_REG (xcr0 , XCR0_AVX512_ZMM_HI256 );
328345 flags -> xcr0_avx512_hi16_zmm = XSAVE_CHECK_REG (xcr0 , XCR0_AVX512_HI16_ZMM );
329- #endif
330346}
347+ #endif
331348
332349static void
333350cpuid_features_finalize (_Py_cpuid_features * flags )
@@ -493,9 +510,7 @@ cpuid_detect_l1_features(_Py_cpuid_features *flags)
493510 uint32_t eax = 0 , ebx = 0 , ecx = 0 , edx = 0 ;
494511 get_cpuid_info (1 , 0 , & eax , & ebx , & ecx , & edx );
495512 detect_cpuid_features (flags , ecx , edx );
496- if (flags -> osxsave ) {
497- detect_cpuid_xsave_state (flags );
498- }
513+ detect_cpuid_xsave_state (flags );
499514 }
500515}
501516#else
@@ -551,15 +566,11 @@ _Py_cpuid_detect_features(_Py_cpuid_features *flags)
551566 return ;
552567 }
553568 _Py_cpuid_disable_features (flags );
554- #ifndef HAS_CPUID_SUPPORT
555- flags -> ready = 1 ;
556- #else
557569 flags -> maxleaf = detect_cpuid_maxleaf ();
558570 cpuid_detect_l1_features (flags );
559571 cpuid_detect_l7_features (flags );
560572 cpuid_features_finalize (flags );
561573 if (!_Py_cpuid_check_features (flags )) {
562574 _Py_cpuid_disable_features (flags );
563575 }
564- #endif // !HAS_CPUID_SUPPORT
565576}
0 commit comments