24 #include "../SDL_internal.h"
27 #if defined(__WIN32__)
28 #include "../core/windows/SDL_windows.h"
33 #ifndef QSV_NUMPROCESSORS
34 #define QSV_NUMPROCESSORS 26
45 #ifdef HAVE_SYSCTLBYNAME
46 #include <sys/types.h>
47 #include <sys/sysctl.h>
49 #if defined(__MACOSX__) && (defined(__ppc__) || defined(__ppc64__))
50 #include <sys/sysctl.h>
51 #elif defined(__OpenBSD__) && defined(__powerpc__)
52 #include <sys/param.h>
53 #include <sys/sysctl.h>
54 #include <machine/cpu.h>
55 #elif SDL_ALTIVEC_BLITTERS && HAVE_SETJMP
60 #if defined(__QNXNTO__)
61 #include <sys/syspage.h>
64 #if (defined(__LINUX__) || defined(__ANDROID__)) && defined(__ARM_ARCH)
70 #define HWCAP_NEON (1 << 12)
72 #if defined HAVE_GETAUXVAL
79 #define CPU_HAS_RDTSC 0x00000001
80 #define CPU_HAS_ALTIVEC 0x00000002
81 #define CPU_HAS_MMX 0x00000004
82 #define CPU_HAS_3DNOW 0x00000008
83 #define CPU_HAS_SSE 0x00000010
84 #define CPU_HAS_SSE2 0x00000020
85 #define CPU_HAS_SSE3 0x00000040
86 #define CPU_HAS_SSE41 0x00000100
87 #define CPU_HAS_SSE42 0x00000200
88 #define CPU_HAS_AVX 0x00000400
89 #define CPU_HAS_AVX2 0x00000800
90 #define CPU_HAS_NEON 0x00001000
92 #if SDL_ALTIVEC_BLITTERS && HAVE_SETJMP && !__MACOSX__ && !__OpenBSD__
96 static jmp_buf jmpbuf;
98 illegal_instruction(
int sig)
110 #ifndef SDL_CPUINFO_DISABLED
111 #if defined(__GNUC__) && defined(i386)
113 " pushfl # Get original EFLAGS \n"
115 " movl %%eax,%%ecx \n"
116 " xorl $0x200000,%%eax # Flip ID bit in EFLAGS \n"
117 " pushl %%eax # Save new EFLAGS value on stack \n"
118 " popfl # Replace current EFLAGS value \n"
119 " pushfl # Get new EFLAGS \n"
120 " popl %%eax # Store new EFLAGS in EAX \n"
121 " xorl %%ecx,%%eax # Can not toggle ID bit, \n"
122 " jz 1f # Processor=80486 \n"
123 " movl $1,%0 # We have CPUID support \n"
129 #elif defined(__GNUC__) && defined(__x86_64__)
133 " pushfq # Get original EFLAGS \n"
135 " movq %%rax,%%rcx \n"
136 " xorl $0x200000,%%eax # Flip ID bit in EFLAGS \n"
137 " pushq %%rax # Save new EFLAGS value on stack \n"
138 " popfq # Replace current EFLAGS value \n"
139 " pushfq # Get new EFLAGS \n"
140 " popq %%rax # Store new EFLAGS in EAX \n"
141 " xorl %%ecx,%%eax # Can not toggle ID bit, \n"
142 " jz 1f # Processor=80486 \n"
143 " movl $1,%0 # We have CPUID support \n"
149 #elif (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
151 pushfd ; Get original EFLAGS
154 xor eax, 200000
h ; Flip ID bit
in EFLAGS
155 push eax ; Save
new EFLAGS
value on stack
156 popfd ; Replace current EFLAGS
value
157 pushfd ; Get
new EFLAGS
158 pop eax ; Store
new EFLAGS
in EAX
159 xor eax, ecx ; Can not toggle ID bit,
160 jz
done ; Processor=80486
161 mov has_CPUID,1 ; We have CPUID support
164 #elif defined(_MSC_VER) && defined(_M_X64)
166 #elif defined(__sun) && defined(__i386)
171 " xorl $0x200000,%eax \n"
178 " movl $1,-8(%ebp) \n"
181 #elif defined(__sun) && defined(__amd64)
186 " xorl $0x200000,%eax \n"
193 " movl $1,-8(%rbp) \n"
202 #if defined(__GNUC__) && defined(i386)
203 #define cpuid(func, a, b, c, d) \
204 __asm__ __volatile__ ( \
206 " xorl %%ecx,%%ecx \n" \
208 " movl %%ebx, %%esi \n" \
210 "=a" (a), "=S" (b), "=c" (c), "=d" (d) : "a" (func))
211 #elif defined(__GNUC__) && defined(__x86_64__)
212 #define cpuid(func, a, b, c, d) \
213 __asm__ __volatile__ ( \
215 " xorq %%rcx,%%rcx \n" \
217 " movq %%rbx, %%rsi \n" \
219 "=a" (a), "=S" (b), "=c" (c), "=d" (d) : "a" (func))
220 #elif (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
221 #define cpuid(func, a, b, c, d) \
223 __asm mov eax, func \
231 #elif defined(_MSC_VER) && defined(_M_X64)
232 #define cpuid(func, a, b, c, d) \
235 __cpuid(CPUInfo, func); \
242 #define cpuid(func, a, b, c, d) \
243 do { a = b = c = d = 0; (void) a; (void) b; (void) c; (void) d; } while (0)
258 cpuid(0, a, b, c, d);
261 cpuid(1, a, b, c, d);
268 if (c & 0x08000000) {
270 #if defined(__GNUC__) && (defined(i386) || defined(__x86_64__))
271 __asm__(
".byte 0x0f, 0x01, 0xd0" :
"=a" (a) :
"c" (0) :
"%edx");
272 #elif defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64)) && (_MSC_FULL_VER >= 160040219)
274 #elif (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
278 _asm _emit 0x0f _asm _emit 0x01 _asm _emit 0xd0
292 volatile int altivec = 0;
293 #ifndef SDL_CPUINFO_DISABLED
294 #if (defined(__MACOSX__) && (defined(__ppc__) || defined(__ppc64__))) || (defined(__OpenBSD__) && defined(__powerpc__))
296 int selectors[2] = { CTL_MACHDEP, CPU_ALTIVEC };
298 int selectors[2] = { CTL_HW, HW_VECTORUNIT };
300 int hasVectorUnit = 0;
301 size_t length =
sizeof(hasVectorUnit);
302 int error = sysctl(selectors, 2, &hasVectorUnit, &length,
NULL, 0);
304 altivec = (hasVectorUnit != 0);
305 #elif SDL_ALTIVEC_BLITTERS && HAVE_SETJMP
306 void (*handler) (
int sig);
307 handler = signal(SIGILL, illegal_instruction);
308 if (setjmp(jmpbuf) == 0) {
309 asm volatile (
"mtspr 256, %0\n\t" "vand %%v0, %%v0, %%v0"::
"r" (-1));
312 signal(SIGILL, handler);
318 #if (defined(__LINUX__) || defined(__ANDROID__)) && defined(__ARM_ARCH) && !defined(HAVE_GETAUXVAL)
320 readProcAuxvForNeon(
void)
324 const int fd = open(
"/proc/self/auxv", O_RDONLY);
326 while (read(fd, kv,
sizeof (kv)) ==
sizeof (kv)) {
327 if (kv[0] == AT_HWCAP) {
328 neon = ((kv[1] & HWCAP_NEON) == HWCAP_NEON);
344 #if defined(SDL_CPUINFO_DISABLED) || !defined(__ARM_ARCH)
346 #elif __ARM_ARCH >= 8
348 #elif defined(__APPLE__) && (__ARM_ARCH >= 7)
351 #elif defined(__APPLE__)
353 #elif defined(__QNXNTO__)
354 return SYSPAGE_ENTRY(cpuinfo)->flags & ARM_CPU_FLAG_NEON;
355 #elif (defined(__LINUX__) || defined(__ANDROID__)) && defined(HAVE_GETAUXVAL)
356 return ((getauxval(AT_HWCAP) & HWCAP_NEON) == HWCAP_NEON);
357 #elif (defined(__LINUX__) || defined(__ANDROID__))
358 return readProcAuxvForNeon();
359 #elif (defined(__WINDOWS__) || defined(__WINRT__)) && defined(_M_ARM)
361 return IsProcessorFeaturePresent(PF_ARM_NEON_INSTRUCTIONS_AVAILABLE) != 0;
363 #warning SDL_HasNEON is not implemented for this ARM platform. Write me.
373 cpuid(0x80000000, a, b, c, d);
374 if (a >= 0x80000001) {
375 cpuid(0x80000001, a, b, c, d);
376 return (d & 0x80000000);
382 #define CPU_haveRDTSC() (CPU_CPUIDFeatures[3] & 0x00000010)
383 #define CPU_haveMMX() (CPU_CPUIDFeatures[3] & 0x00800000)
384 #define CPU_haveSSE() (CPU_CPUIDFeatures[3] & 0x02000000)
385 #define CPU_haveSSE2() (CPU_CPUIDFeatures[3] & 0x04000000)
386 #define CPU_haveSSE3() (CPU_CPUIDFeatures[2] & 0x00000001)
387 #define CPU_haveSSE41() (CPU_CPUIDFeatures[2] & 0x00080000)
388 #define CPU_haveSSE42() (CPU_CPUIDFeatures[2] & 0x00100000)
389 #define CPU_haveAVX() (CPU_OSSavesYMM && (CPU_CPUIDFeatures[2] & 0x10000000))
397 cpuid(7, a, b, c, d);
398 return (b & 0x00000020);
409 #ifndef SDL_CPUINFO_DISABLED
410 #if defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN)
411 if (SDL_CPUCount <= 0) {
412 SDL_CPUCount = (int)sysconf(_SC_NPROCESSORS_ONLN);
415 #ifdef HAVE_SYSCTLBYNAME
416 if (SDL_CPUCount <= 0) {
418 sysctlbyname(
"hw.ncpu", &SDL_CPUCount, &size,
NULL, 0);
422 if (SDL_CPUCount <= 0) {
424 GetSystemInfo(&info);
425 SDL_CPUCount = info.dwNumberOfProcessors;
429 if (SDL_CPUCount <= 0) {
430 DosQuerySysInfo(QSV_NUMPROCESSORS, QSV_NUMPROCESSORS,
431 &SDL_CPUCount,
sizeof(SDL_CPUCount) );
436 if (SDL_CPUCount <= 0) {
447 static char SDL_CPUType[13];
449 if (!SDL_CPUType[0]) {
455 cpuid(0x00000000, a, b, c, d);
457 SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
458 SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
459 SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
460 SDL_CPUType[i++] = (char)(b & 0xff);
462 SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
463 SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
464 SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
465 SDL_CPUType[i++] = (char)(d & 0xff);
467 SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
468 SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
469 SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
470 SDL_CPUType[i++] = (char)(c & 0xff);
472 if (!SDL_CPUType[0]) {
473 SDL_strlcpy(SDL_CPUType,
"Unknown",
sizeof(SDL_CPUType));
484 static char SDL_CPUName[48];
486 if (!SDL_CPUName[0]) {
492 cpuid(0x80000000, a, b, c, d);
493 if (a >= 0x80000004) {
494 cpuid(0x80000002, a, b, c, d);
495 SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
496 SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
497 SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
498 SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
499 SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
500 SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
501 SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
502 SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
503 SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
504 SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
505 SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
506 SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
507 SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
508 SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
509 SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
510 SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
511 cpuid(0x80000003, a, b, c, d);
512 SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
513 SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
514 SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
515 SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
516 SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
517 SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
518 SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
519 SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
520 SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
521 SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
522 SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
523 SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
524 SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
525 SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
526 SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
527 SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
528 cpuid(0x80000004, a, b, c, d);
529 SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
530 SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
531 SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
532 SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
533 SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
534 SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
535 SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
536 SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
537 SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
538 SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
539 SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
540 SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
541 SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
542 SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
543 SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
544 SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
547 if (!SDL_CPUName[0]) {
548 SDL_strlcpy(SDL_CPUName,
"Unknown",
sizeof(SDL_CPUName));
561 if (
SDL_strcmp(cpuType,
"GenuineIntel") == 0) {
562 cpuid(0x00000001, a, b, c, d);
563 return (((b >> 8) & 0xff) * 8);
564 }
else if (
SDL_strcmp(cpuType,
"AuthenticAMD") == 0) {
565 cpuid(0x80000005, a, b, c, d);
578 if (SDL_CPUFeatures == 0xFFFFFFFF) {
621 #define CPU_FEATURE_AVAILABLE(f) ((SDL_GetCPUFeatures() & f) ? SDL_TRUE : SDL_FALSE)
699 if (!SDL_SystemRAM) {
700 #ifndef SDL_CPUINFO_DISABLED
701 #if defined(HAVE_SYSCONF) && defined(_SC_PHYS_PAGES) && defined(_SC_PAGESIZE)
702 if (SDL_SystemRAM <= 0) {
703 SDL_SystemRAM = (int)((
Sint64)sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE) / (1024*1024));
706 #ifdef HAVE_SYSCTLBYNAME
707 if (SDL_SystemRAM <= 0) {
708 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__)
710 int mib[2] = {CTL_HW, HW_REALMEM};
713 int mib[2] = {CTL_HW, HW_PHYSMEM};
716 int mib[2] = {CTL_HW, HW_MEMSIZE};
719 size_t len =
sizeof(memsize);
721 if (sysctl(mib, 2, &memsize, &len,
NULL, 0) == 0) {
722 SDL_SystemRAM = (int)(memsize / (1024*1024));
727 if (SDL_SystemRAM <= 0) {
729 stat.dwLength =
sizeof(stat);
730 if (GlobalMemoryStatusEx(&stat)) {
731 SDL_SystemRAM = (int)(stat.ullTotalPhys / (1024 * 1024));
736 if (SDL_SystemRAM <= 0) {
738 DosQuerySysInfo(QSV_TOTPHYSMEM, QSV_TOTPHYSMEM, &sysram, 4);
739 SDL_SystemRAM = (int) (sysram / 0x100000U);
757 printf(
"CPU name: %s\n", SDL_GetCPUName());
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
SDL_bool SDL_HasSSE(void)
SDL_bool SDL_HasSSE3(void)
static int CPU_CPUIDMaxFunction
static int CPU_have3DNow(void)
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)
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 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)
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 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)