24 #include "../SDL_internal.h" 27 #if defined(__WIN32__) || defined(__WINRT__) 28 #include "../core/windows/SDL_windows.h" 33 #ifndef QSV_NUMPROCESSORS 34 #define QSV_NUMPROCESSORS 26 46 #ifdef HAVE_SYSCTLBYNAME 47 #include <sys/types.h> 48 #include <sys/sysctl.h> 50 #if defined(__MACOSX__) && (defined(__ppc__) || defined(__ppc64__)) 51 #include <sys/sysctl.h> 52 #elif defined(__OpenBSD__) && defined(__powerpc__) 53 #include <sys/param.h> 54 #include <sys/sysctl.h> 55 #include <machine/cpu.h> 56 #elif SDL_ALTIVEC_BLITTERS && HAVE_SETJMP 61 #if defined(__QNXNTO__) 62 #include <sys/syspage.h> 65 #if (defined(__LINUX__) || defined(__ANDROID__)) && defined(__ARM_ARCH) 71 #define HWCAP_NEON (1 << 12) 73 #if defined HAVE_GETAUXVAL 80 #if defined(__ANDROID__) && defined(__ARM_ARCH) && !defined(HAVE_GETAUXVAL) 82 #include <cpu-features.h> 86 #define CPU_HAS_RDTSC (1 << 0) 87 #define CPU_HAS_ALTIVEC (1 << 1) 88 #define CPU_HAS_MMX (1 << 2) 89 #define CPU_HAS_3DNOW (1 << 3) 90 #define CPU_HAS_SSE (1 << 4) 91 #define CPU_HAS_SSE2 (1 << 5) 92 #define CPU_HAS_SSE3 (1 << 6) 93 #define CPU_HAS_SSE41 (1 << 7) 94 #define CPU_HAS_SSE42 (1 << 8) 95 #define CPU_HAS_AVX (1 << 9) 96 #define CPU_HAS_AVX2 (1 << 10) 97 #define CPU_HAS_NEON (1 << 11) 98 #define CPU_HAS_AVX512F (1 << 12) 100 #if SDL_ALTIVEC_BLITTERS && HAVE_SETJMP && !__MACOSX__ && !__OpenBSD__ 104 static jmp_buf jmpbuf;
106 illegal_instruction(
int sig)
118 #ifndef SDL_CPUINFO_DISABLED 119 #if defined(__GNUC__) && defined(i386) 121 " pushfl # Get original EFLAGS \n" 123 " movl %%eax,%%ecx \n" 124 " xorl $0x200000,%%eax # Flip ID bit in EFLAGS \n" 125 " pushl %%eax # Save new EFLAGS value on stack \n" 126 " popfl # Replace current EFLAGS value \n" 127 " pushfl # Get new EFLAGS \n" 128 " popl %%eax # Store new EFLAGS in EAX \n" 129 " xorl %%ecx,%%eax # Can not toggle ID bit, \n" 130 " jz 1f # Processor=80486 \n" 131 " movl $1,%0 # We have CPUID support \n" 137 #elif defined(__GNUC__) && defined(__x86_64__) 141 " pushfq # Get original EFLAGS \n" 143 " movq %%rax,%%rcx \n" 144 " xorl $0x200000,%%eax # Flip ID bit in EFLAGS \n" 145 " pushq %%rax # Save new EFLAGS value on stack \n" 146 " popfq # Replace current EFLAGS value \n" 147 " pushfq # Get new EFLAGS \n" 148 " popq %%rax # Store new EFLAGS in EAX \n" 149 " xorl %%ecx,%%eax # Can not toggle ID bit, \n" 150 " jz 1f # Processor=80486 \n" 151 " movl $1,%0 # We have CPUID support \n" 157 #elif (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__) 159 pushfd ; Get original EFLAGS
162 xor eax, 200000
h ; Flip ID bit
in EFLAGS
163 push eax ; Save
new EFLAGS
value on stack
164 popfd ; Replace current EFLAGS
value 165 pushfd ; Get
new EFLAGS
166 pop eax ; Store
new EFLAGS
in EAX
167 xor eax, ecx ; Can not toggle ID bit,
168 jz
done ; Processor=80486
169 mov has_CPUID,1 ; We have CPUID support
172 #elif defined(_MSC_VER) && defined(_M_X64) 174 #elif defined(__sun) && defined(__i386) 179 " xorl $0x200000,%eax \n" 186 " movl $1,-8(%ebp) \n" 189 #elif defined(__sun) && defined(__amd64) 194 " xorl $0x200000,%eax \n" 201 " movl $1,-8(%rbp) \n" 210 #if defined(__GNUC__) && defined(i386) 211 #define cpuid(func, a, b, c, d) \ 212 __asm__ __volatile__ ( \ 214 " xorl %%ecx,%%ecx \n" \ 216 " movl %%ebx, %%esi \n" \ 218 "=a" (a), "=S" (b), "=c" (c), "=d" (d) : "a" (func)) 219 #elif defined(__GNUC__) && defined(__x86_64__) 220 #define cpuid(func, a, b, c, d) \ 221 __asm__ __volatile__ ( \ 223 " xorq %%rcx,%%rcx \n" \ 225 " movq %%rbx, %%rsi \n" \ 227 "=a" (a), "=S" (b), "=c" (c), "=d" (d) : "a" (func)) 228 #elif (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__) 229 #define cpuid(func, a, b, c, d) \ 231 __asm mov eax, func \ 239 #elif defined(_MSC_VER) && defined(_M_X64) 240 #define cpuid(func, a, b, c, d) \ 243 __cpuid(CPUInfo, func); \ 250 #define cpuid(func, a, b, c, d) \ 251 do { a = b = c = d = 0; (void) a; (void) b; (void) c; (void) d; } while (0) 267 cpuid(0, a, b, c, d);
270 cpuid(1, a, b, c, d);
277 if (c & 0x08000000) {
279 #if defined(__GNUC__) && (defined(i386) || defined(__x86_64__)) 280 __asm__(
".byte 0x0f, 0x01, 0xd0" :
"=a" (a) :
"c" (0) :
"%edx");
281 #elif defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64)) && (_MSC_FULL_VER >= 160040219) 283 #elif (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__) 287 _asm _emit 0x0f _asm _emit 0x01 _asm _emit 0xd0
302 volatile int altivec = 0;
303 #ifndef SDL_CPUINFO_DISABLED 304 #if (defined(__MACOSX__) && (defined(__ppc__) || defined(__ppc64__))) || (defined(__OpenBSD__) && defined(__powerpc__)) 306 int selectors[2] = { CTL_MACHDEP, CPU_ALTIVEC };
308 int selectors[2] = { CTL_HW, HW_VECTORUNIT };
310 int hasVectorUnit = 0;
311 size_t length =
sizeof(hasVectorUnit);
312 int error = sysctl(selectors, 2, &hasVectorUnit, &length,
NULL, 0);
314 altivec = (hasVectorUnit != 0);
315 #elif SDL_ALTIVEC_BLITTERS && HAVE_SETJMP 316 void (*handler) (
int sig);
317 handler = signal(SIGILL, illegal_instruction);
318 if (setjmp(jmpbuf) == 0) {
319 asm volatile (
"mtspr 256, %0\n\t" "vand %%v0, %%v0, %%v0"::
"r" (-1));
322 signal(SIGILL, handler);
328 #if defined(__LINUX__) && defined(__ARM_ARCH) && !defined(HAVE_GETAUXVAL) 330 readProcAuxvForNeon(
void)
334 const int fd = open(
"/proc/self/auxv", O_RDONLY);
336 while (read(fd, kv,
sizeof (kv)) ==
sizeof (kv)) {
337 if (kv[0] == AT_HWCAP) {
338 neon = ((kv[1] & HWCAP_NEON) == HWCAP_NEON);
354 #if defined(SDL_CPUINFO_DISABLED) 356 #elif (defined(__WINDOWS__) || defined(__WINRT__)) && (defined(_M_ARM) || defined(_M_ARM64)) 359 # if !defined(PF_ARM_NEON_INSTRUCTIONS_AVAILABLE) 360 # define PF_ARM_NEON_INSTRUCTIONS_AVAILABLE 19 363 return IsProcessorFeaturePresent(PF_ARM_NEON_INSTRUCTIONS_AVAILABLE) != 0;
364 #elif !defined(__ARM_ARCH) 366 #elif __ARM_ARCH >= 8 368 #elif defined(__APPLE__) && (__ARM_ARCH >= 7) 371 #elif defined(__APPLE__) 373 #elif defined(__QNXNTO__) 374 return SYSPAGE_ENTRY(cpuinfo)->flags & ARM_CPU_FLAG_NEON;
375 #elif (defined(__LINUX__) || defined(__ANDROID__)) && defined(HAVE_GETAUXVAL) 376 return ((getauxval(AT_HWCAP) & HWCAP_NEON) == HWCAP_NEON);
377 #elif defined(__LINUX__) 378 return readProcAuxvForNeon();
379 #elif defined(__ANDROID__) 382 AndroidCpuFamily cpu_family = android_getCpuFamily();
383 if (cpu_family == ANDROID_CPU_FAMILY_ARM) {
384 uint64_t cpu_features = android_getCpuFeatures();
385 if ((cpu_features & ANDROID_CPU_ARM_FEATURE_NEON) != 0) {
392 #warning SDL_HasNEON is not implemented for this ARM platform. Write me. 402 cpuid(0x80000000, a, b, c, d);
403 if (a >= 0x80000001) {
404 cpuid(0x80000001, a, b, c, d);
405 return (d & 0x80000000);
411 #define CPU_haveRDTSC() (CPU_CPUIDFeatures[3] & 0x00000010) 412 #define CPU_haveMMX() (CPU_CPUIDFeatures[3] & 0x00800000) 413 #define CPU_haveSSE() (CPU_CPUIDFeatures[3] & 0x02000000) 414 #define CPU_haveSSE2() (CPU_CPUIDFeatures[3] & 0x04000000) 415 #define CPU_haveSSE3() (CPU_CPUIDFeatures[2] & 0x00000001) 416 #define CPU_haveSSE41() (CPU_CPUIDFeatures[2] & 0x00080000) 417 #define CPU_haveSSE42() (CPU_CPUIDFeatures[2] & 0x00100000) 418 #define CPU_haveAVX() (CPU_OSSavesYMM && (CPU_CPUIDFeatures[2] & 0x10000000)) 426 cpuid(7, a, b, c, d);
427 return (b & 0x00000020);
438 cpuid(7, a, b, c, d);
439 return (b & 0x00010000);
450 #ifndef SDL_CPUINFO_DISABLED 451 #if defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN) 456 #ifdef HAVE_SYSCTLBYNAME 465 GetSystemInfo(&info);
471 DosQuerySysInfo(QSV_NUMPROCESSORS, QSV_NUMPROCESSORS,
488 static char SDL_CPUType[13];
490 if (!SDL_CPUType[0]) {
496 cpuid(0x00000000, a, b, c, d);
498 SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
499 SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
500 SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
501 SDL_CPUType[i++] = (char)(b & 0xff);
503 SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
504 SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
505 SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
506 SDL_CPUType[i++] = (char)(d & 0xff);
508 SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
509 SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
510 SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
511 SDL_CPUType[i++] = (char)(c & 0xff);
513 if (!SDL_CPUType[0]) {
514 SDL_strlcpy(SDL_CPUType,
"Unknown",
sizeof(SDL_CPUType));
525 static char SDL_CPUName[48];
527 if (!SDL_CPUName[0]) {
533 cpuid(0x80000000, a, b, c, d);
534 if (a >= 0x80000004) {
535 cpuid(0x80000002, a, b, c, d);
536 SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
537 SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
538 SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
539 SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
540 SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
541 SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
542 SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
543 SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
544 SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
545 SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
546 SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
547 SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
548 SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
549 SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
550 SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
551 SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
552 cpuid(0x80000003, a, b, c, d);
553 SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
554 SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
555 SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
556 SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
557 SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
558 SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
559 SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
560 SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
561 SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
562 SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
563 SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
564 SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
565 SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
566 SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
567 SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
568 SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
569 cpuid(0x80000004, a, b, c, d);
570 SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
571 SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
572 SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
573 SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
574 SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
575 SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
576 SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
577 SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
578 SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
579 SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
580 SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
581 SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
582 SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
583 SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
584 SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
585 SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
588 if (!SDL_CPUName[0]) {
589 SDL_strlcpy(SDL_CPUName,
"Unknown",
sizeof(SDL_CPUName));
602 if (
SDL_strcmp(cpuType,
"GenuineIntel") == 0) {
603 cpuid(0x00000001, a, b, c, d);
604 return (((b >> 8) & 0xff) * 8);
605 }
else if (
SDL_strcmp(cpuType,
"AuthenticAMD") == 0) {
606 cpuid(0x80000005, a, b, c, d);
679 #define CPU_FEATURE_AVAILABLE(f) ((SDL_GetCPUFeatures() & f) ? SDL_TRUE : SDL_FALSE) 764 #ifndef SDL_CPUINFO_DISABLED 765 #if defined(HAVE_SYSCONF) && defined(_SC_PHYS_PAGES) && defined(_SC_PAGESIZE) 767 SDL_SystemRAM = (int)((
Sint64)sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE) / (1024*1024));
770 #ifdef HAVE_SYSCTLBYNAME 772 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) 774 int mib[2] = {CTL_HW, HW_REALMEM};
777 int mib[2] = {CTL_HW, HW_PHYSMEM};
780 int mib[2] = {CTL_HW, HW_MEMSIZE};
783 size_t len =
sizeof(memsize);
785 if (sysctl(mib, 2, &memsize, &len,
NULL, 0) == 0) {
793 stat.dwLength =
sizeof(stat);
794 if (GlobalMemoryStatusEx(&stat)) {
802 DosQuerySysInfo(QSV_TOTPHYSMEM, QSV_TOTPHYSMEM, &sysram, 4);
826 const size_t padding = alignment - (len % alignment);
827 const size_t padded = (padding != alignment) ? (len + padding) :
len;
832 retval = ptr +
sizeof (
void *);
833 retval += alignment - (((
size_t) retval) % alignment);
834 *(((
void **) retval) - 1) = ptr;
843 void **realptr = (
void **) ptr;
859 printf(
"CPU name: %s\n", SDL_GetCPUName());
void SDL_SIMDFree(void *ptr)
Deallocate memory obtained from SDL_SIMDAlloc.
static SDL_bool CPU_OSSavesYMM
SDL_bool SDL_HasSSE41(void)
static void CPU_calcCPUIDFeatures(void)
int SDL_GetCPUCount(void)
static const char * SDL_GetCPUType(void)
GLfloat GLfloat GLfloat GLfloat h
unsigned long long uint64_t
SDL_bool SDL_HasAVX512F(void)
SDL_bool SDL_HasSSE(void)
SDL_bool SDL_HasSSE3(void)
static int CPU_CPUIDMaxFunction
static int CPU_have3DNow(void)
void * SDL_SIMDAlloc(const size_t len)
Allocate memory in a SIMD-friendly way.
SDL_bool SDL_HasAltiVec(void)
static int CPU_haveCPUID(void)
SDL_bool SDL_HasSSE42(void)
SDL_bool SDL_HasMMX(void)
SDL_bool SDL_HasAVX2(void)
static int CPU_haveAltiVec(void)
static int CPU_haveAVX512F(void)
static SDL_bool CPU_OSSavesZMM
#define CPU_FEATURE_AVAILABLE(f)
SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char const char SDL_SCANF_FORMAT_STRING const char return SDL_ThreadFunction const char void return Uint32 return Uint32 SDL_AssertionHandler void SDL_SpinLock SDL_atomic_t int int return SDL_atomic_t return void void void return void return int return SDL_AudioSpec SDL_AudioSpec return int int return return int SDL_RWops int SDL_AudioSpec Uint8 ** d
SDL_bool SDL_HasNEON(void)
#define SDL_CACHELINE_SIZE
GLsizei const GLfloat * value
#define cpuid(func, a, b, c, d)
SDL_bool SDL_HasAVX(void)
static Uint32 SDL_CPUFeatures
static Uint32 SDL_GetCPUFeatures(void)
static int CPU_haveAVX2(void)
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
#define SDL_assert(condition)
static int CPU_haveNEON(void)
SDL_bool SDL_HasRDTSC(void)
SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char const char SDL_SCANF_FORMAT_STRING const char return SDL_ThreadFunction const char void return Uint32 return Uint32 void
int SDL_GetSystemRAM(void)
static Uint32 SDL_SIMDAlignment
size_t SDL_SIMDGetAlignment(void)
Report the alignment this system needs for SIMD allocations.
static int CPU_CPUIDFeatures[4]
GLuint GLsizei GLsizei * length
GLboolean GLboolean GLboolean GLboolean a
SDL_bool SDL_HasSSE2(void)
GLboolean GLboolean GLboolean b
SDL_bool SDL_Has3DNow(void)
int SDL_GetCPUCacheLineSize(void)