28
28
#include <sys/auxv.h>
29
29
#include <asm/hwcap.h>
30
30
31
+ #ifdef __clang__
32
+ #pragma clang diagnostic push
33
+ #pragma clang diagnostic ignored "-Wunused-macros"
34
+ #endif
35
+
31
36
// Light-up for hardware capabilities that are not present in older headers used by the portable build.
32
37
#ifndef HWCAP_ASIMDRDM
33
38
#define HWCAP_ASIMDRDM (1 << 12)
45
50
#define HWCAP_SVE (1 << 22)
46
51
#endif
47
52
53
+ #ifndef XSTATE_MASK_AVX512
54
+ #define XSTATE_MASK_AVX512 (0xE0) /* 0b1110_0000 */
55
+ #endif // XSTATE_MASK_AVX512
56
+
57
+ #ifdef __clang__
58
+ #pragma clang diagnostic pop
59
+ #endif
60
+
48
61
#endif
49
62
50
63
#if HAVE_SYSCTLBYNAME
56
69
#if defined(HOST_UNIX )
57
70
#if defined(HOST_X86 ) || defined(HOST_AMD64 )
58
71
59
- static uint32_t xmmYmmStateSupport ()
72
+ static uint32_t xmmYmmStateSupport (void )
60
73
{
61
74
uint32_t eax ;
62
75
__asm(" xgetbv\n" \
@@ -68,11 +81,7 @@ static uint32_t xmmYmmStateSupport()
68
81
return ((eax & 0x06 ) == 0x06 ) ? 1 : 0 ;
69
82
}
70
83
71
- #ifndef XSTATE_MASK_AVX512
72
- #define XSTATE_MASK_AVX512 (0xE0) /* 0b1110_0000 */
73
- #endif // XSTATE_MASK_AVX512
74
-
75
- static uint32_t avx512StateSupport ()
84
+ static uint32_t avx512StateSupport (void )
76
85
{
77
86
#if defined(HOST_APPLE )
78
87
// MacOS has specialized behavior where it reports AVX512 support but doesnt
@@ -99,12 +108,12 @@ static uint32_t avx512StateSupport()
99
108
#endif
100
109
}
101
110
102
- static bool IsAvxEnabled ()
111
+ static bool IsAvxEnabled (void )
103
112
{
104
113
return true;
105
114
}
106
115
107
- static bool IsAvx512Enabled ()
116
+ static bool IsAvx512Enabled (void )
108
117
{
109
118
return true;
110
119
}
@@ -222,11 +231,11 @@ int minipal_getcpufeatures(void)
222
231
223
232
if (IsAvx512Enabled () && (avx512StateSupport () == 1 )) // XGETBV XRC0[7:5] == 111
224
233
{
225
- if (((cpuidInfo [CPUID_EBX ] & (1 << 16 )) != 0 ) && // AVX512F
226
- ((cpuidInfo [CPUID_EBX ] & (1 << 30 )) != 0 ) && // AVX512BW
227
- ((cpuidInfo [CPUID_EBX ] & (1 << 28 )) != 0 ) && // AVX512CD
228
- ((cpuidInfo [CPUID_EBX ] & (1 << 17 )) != 0 ) && // AVX512DQ
229
- ((cpuidInfo [CPUID_EBX ] & (1 << 31 )) != 0 )) // AVX512VL
234
+ if (((( uint32_t ) cpuidInfo [CPUID_EBX ] & (( uint32_t ) 1 << 16 )) != 0 ) && // AVX512F
235
+ ((( uint32_t ) cpuidInfo [CPUID_EBX ] & (( uint32_t ) 1 << 30 )) != 0 ) && // AVX512BW
236
+ ((( uint32_t ) cpuidInfo [CPUID_EBX ] & (( uint32_t ) 1 << 28 )) != 0 ) && // AVX512CD
237
+ ((( uint32_t ) cpuidInfo [CPUID_EBX ] & (( uint32_t ) 1 << 17 )) != 0 ) && // AVX512DQ
238
+ ((( uint32_t ) cpuidInfo [CPUID_EBX ] & (( uint32_t ) 1 << 31 )) != 0 )) // AVX512VL
230
239
{
231
240
// While the AVX-512 ISAs can be individually lit-up, they really
232
241
// need F, BW, CD, DQ, and VL to be fully functional without adding
@@ -301,12 +310,12 @@ int minipal_getcpufeatures(void)
301
310
}
302
311
}
303
312
304
- __cpuid (cpuidInfo , 0x80000000 );
313
+ __cpuid (cpuidInfo , ( int ) 0x80000000 );
305
314
uint32_t maxCpuIdEx = (uint32_t )cpuidInfo [CPUID_EAX ];
306
315
307
316
if (maxCpuIdEx >= 0x80000001 )
308
317
{
309
- __cpuid (cpuidInfo , 0x80000001 );
318
+ __cpuid (cpuidInfo , ( int ) 0x80000001 );
310
319
311
320
if ((cpuidInfo [CPUID_ECX ] & (1 << 5 )) != 0 ) // LZCNT
312
321
{
@@ -441,67 +450,58 @@ int minipal_getcpufeatures(void)
441
450
return result ;
442
451
}
443
452
444
- // Detect if the current process is running under the Apple Rosetta x64 emulator
445
- bool minipal_detect_rosetta (void )
453
+ static bool GetCpuBrand (char * brand , size_t bufferSize )
446
454
{
447
455
#if defined(HOST_AMD64 ) || defined(HOST_X86 )
448
456
// Check for CPU brand indicating emulation
449
457
int regs [4 ];
450
- char brand [49 ];
451
458
452
459
// Get the maximum value for extended function CPUID info
453
460
__cpuid (regs , (int )0x80000000 );
454
461
if ((unsigned int )regs [0 ] < 0x80000004 )
455
462
{
456
- return false; // Extended CPUID not supported
463
+ brand [0 ] = '\0' ; // Extended CPUID not supported, return empty string or handle error
464
+ return false;
457
465
}
458
466
459
- // Retrieve the CPU brand string
467
+ // Retrieve the CPU brand string directly into the caller-provided buffer
460
468
for (unsigned int i = 0x80000002 ; i <= 0x80000004 ; ++ i )
461
469
{
462
470
__cpuid (regs , (int )i );
463
471
memcpy (brand + (i - 0x80000002 ) * sizeof (regs ), regs , sizeof (regs ));
464
472
}
465
- brand [sizeof (brand ) - 1 ] = '\0' ;
466
473
467
- // Check if CPU brand indicates emulation
468
- if (strstr (brand , "VirtualApple" ) != NULL )
469
- {
470
- return true;
471
- }
472
- #endif // HOST_AMD64 || HOST_X86
474
+ brand [bufferSize - 1 ] = '\0' ;
473
475
476
+ return true;
477
+ #else
478
+ (void )brand ;
479
+ (void )bufferSize ;
474
480
return false;
481
+ #endif // HOST_AMD64 || HOST_X86
475
482
}
476
483
477
- bool minipal_detect_qemu (void )
484
+ // Detect if the current process is running under the Apple Rosetta x64 emulator
485
+ bool minipal_detect_rosetta (void )
478
486
{
479
- #if defined(HOST_AMD64 ) || defined(HOST_X86 )
480
- // Check for CPU brand indicating emulation
481
- unsigned int regs [4 ];
482
487
char brand [49 ];
483
488
484
- // Get the maximum value for extended function CPUID info
485
- __cpuid (0x80000000 , regs [0 ], regs [1 ], regs [2 ], regs [3 ]);
486
- if (regs [0 ] < 0x80000004 )
487
- {
488
- return false; // Extended CPUID not supported
489
- }
489
+ // Check if CPU brand indicates emulation
490
+ return GetCpuBrand (brand , sizeof (brand )) && (strstr (brand , "VirtualApple" ) != NULL );
491
+ }
490
492
491
- // Retrieve the CPU brand string
492
- for (unsigned int i = 0x80000002 ; i <= 0x80000004 ; ++ i )
493
- {
494
- __cpuid (i , regs [0 ], regs [1 ], regs [2 ], regs [3 ]);
495
- memcpy (brand + (i - 0x80000002 ) * sizeof (regs ), regs , sizeof (regs ));
496
- }
497
- brand [sizeof (brand ) - 1 ] = '\0' ;
493
+ #if !defined(HOST_WINDOWS )
494
+
495
+ // Detect if the current process is running under QEMU
496
+ bool minipal_detect_qemu (void )
497
+ {
498
+ char brand [49 ];
498
499
499
500
// Check if CPU brand indicates emulation
500
- if (strstr (brand , "QEMU" ) != NULL )
501
+ if (GetCpuBrand ( brand , sizeof ( brand )) && strstr (brand , "QEMU" ) != NULL )
501
502
{
502
503
return true;
503
504
}
504
- #endif
505
505
506
506
// Check for process name of PID 1 indicating emulation
507
507
char cmdline [256 ];
@@ -535,3 +535,5 @@ bool minipal_detect_qemu(void)
535
535
536
536
return false;
537
537
}
538
+
539
+ #endif // !HOST_WINDOWS
0 commit comments