SDL  2.0
SDL_cpuinfo.c File Reference
#include "../SDL_internal.h"
#include "SDL_simd.h"
#include "SDL_cpuinfo.h"
#include "SDL_assert.h"
+ Include dependency graph for SDL_cpuinfo.c:

Go to the source code of this file.

Macros

#define CPU_HAS_RDTSC   (1 << 0)
 
#define CPU_HAS_ALTIVEC   (1 << 1)
 
#define CPU_HAS_MMX   (1 << 2)
 
#define CPU_HAS_3DNOW   (1 << 3)
 
#define CPU_HAS_SSE   (1 << 4)
 
#define CPU_HAS_SSE2   (1 << 5)
 
#define CPU_HAS_SSE3   (1 << 6)
 
#define CPU_HAS_SSE41   (1 << 7)
 
#define CPU_HAS_SSE42   (1 << 8)
 
#define CPU_HAS_AVX   (1 << 9)
 
#define CPU_HAS_AVX2   (1 << 10)
 
#define CPU_HAS_NEON   (1 << 11)
 
#define CPU_HAS_AVX512F   (1 << 12)
 
#define CPU_HAS_MSA   (1 << 13)
 
#define CPU_HAS_LSX   (1 << 14)
 
#define CPU_HAS_LASX   (1 << 15)
 
#define CPU_CFG2   0x2
 
#define CPU_CFG2_LSX   (1 << 6)
 
#define CPU_CFG2_LASX   (1 << 7)
 
#define cpuid(func, a, b, c, d)   do { a = b = c = d = 0; (void) a; (void) b; (void) c; (void) d; } while (0)
 
#define CPU_haveLSX()   (CPU_readCPUCFG() & CPU_CFG2_LSX)
 
#define CPU_haveLASX()   (CPU_readCPUCFG() & CPU_CFG2_LASX)
 
#define CPU_haveRDTSC()   (CPU_CPUIDFeatures[3] & 0x00000010)
 
#define CPU_haveMMX()   (CPU_CPUIDFeatures[3] & 0x00800000)
 
#define CPU_haveSSE()   (CPU_CPUIDFeatures[3] & 0x02000000)
 
#define CPU_haveSSE2()   (CPU_CPUIDFeatures[3] & 0x04000000)
 
#define CPU_haveSSE3()   (CPU_CPUIDFeatures[2] & 0x00000001)
 
#define CPU_haveSSE41()   (CPU_CPUIDFeatures[2] & 0x00080000)
 
#define CPU_haveSSE42()   (CPU_CPUIDFeatures[2] & 0x00100000)
 
#define CPU_haveAVX()   (CPU_OSSavesYMM && (CPU_CPUIDFeatures[2] & 0x10000000))
 
#define CPU_FEATURE_AVAILABLE(f)   ((SDL_GetCPUFeatures() & f) ? SDL_TRUE : SDL_FALSE)
 

Functions

static int CPU_haveCPUID (void)
 
static void CPU_calcCPUIDFeatures (void)
 
static int CPU_haveAltiVec (void)
 
static int CPU_haveNEON (void)
 
static int CPU_haveMSA (void)
 
static int CPU_readCPUCFG (void)
 
static int CPU_have3DNow (void)
 
static int CPU_haveAVX2 (void)
 
static int CPU_haveAVX512F (void)
 
int SDL_GetCPUCount (void)
 
static const char * SDL_GetCPUType (void)
 
int SDL_GetCPUCacheLineSize (void)
 
static Uint32 SDL_GetCPUFeatures (void)
 
SDL_bool SDL_HasRDTSC (void)
 
SDL_bool SDL_HasAltiVec (void)
 
SDL_bool SDL_HasMMX (void)
 
SDL_bool SDL_Has3DNow (void)
 
SDL_bool SDL_HasSSE (void)
 
SDL_bool SDL_HasSSE2 (void)
 
SDL_bool SDL_HasSSE3 (void)
 
SDL_bool SDL_HasSSE41 (void)
 
SDL_bool SDL_HasSSE42 (void)
 
SDL_bool SDL_HasAVX (void)
 
SDL_bool SDL_HasAVX2 (void)
 
SDL_bool SDL_HasAVX512F (void)
 
SDL_bool SDL_HasNEON (void)
 
SDL_bool SDL_HasMSA (void)
 
SDL_bool SDL_HasLSX (void)
 
SDL_bool SDL_HasLASX (void)
 
int SDL_GetSystemRAM (void)
 
size_t SDL_SIMDGetAlignment (void)
 Report the alignment this system needs for SIMD allocations. More...
 
voidSDL_SIMDAlloc (const size_t len)
 Allocate memory in a SIMD-friendly way. More...
 
void SDL_SIMDFree (void *ptr)
 Deallocate memory obtained from SDL_SIMDAlloc. More...
 

Variables

static int CPU_CPUIDFeatures [4]
 
static int CPU_CPUIDMaxFunction = 0
 
static SDL_bool CPU_OSSavesYMM = SDL_FALSE
 
static SDL_bool CPU_OSSavesZMM = SDL_FALSE
 
static int SDL_CPUCount = 0
 
static Uint32 SDL_CPUFeatures = 0xFFFFFFFF
 
static Uint32 SDL_SIMDAlignment = 0xFFFFFFFF
 
static int SDL_SystemRAM = 0
 

Macro Definition Documentation

◆ CPU_CFG2

#define CPU_CFG2   0x2

Definition at line 98 of file SDL_cpuinfo.c.

Referenced by CPU_readCPUCFG().

◆ CPU_CFG2_LASX

#define CPU_CFG2_LASX   (1 << 7)

Definition at line 100 of file SDL_cpuinfo.c.

◆ CPU_CFG2_LSX

#define CPU_CFG2_LSX   (1 << 6)

Definition at line 99 of file SDL_cpuinfo.c.

◆ CPU_FEATURE_AVAILABLE

◆ CPU_HAS_3DNOW

#define CPU_HAS_3DNOW   (1 << 3)

Definition at line 84 of file SDL_cpuinfo.c.

Referenced by SDL_GetCPUFeatures(), and SDL_Has3DNow().

◆ CPU_HAS_ALTIVEC

#define CPU_HAS_ALTIVEC   (1 << 1)

Definition at line 82 of file SDL_cpuinfo.c.

Referenced by SDL_GetCPUFeatures(), and SDL_HasAltiVec().

◆ CPU_HAS_AVX

#define CPU_HAS_AVX   (1 << 9)

Definition at line 90 of file SDL_cpuinfo.c.

Referenced by SDL_GetCPUFeatures(), and SDL_HasAVX().

◆ CPU_HAS_AVX2

#define CPU_HAS_AVX2   (1 << 10)

Definition at line 91 of file SDL_cpuinfo.c.

Referenced by SDL_GetCPUFeatures(), and SDL_HasAVX2().

◆ CPU_HAS_AVX512F

#define CPU_HAS_AVX512F   (1 << 12)

Definition at line 93 of file SDL_cpuinfo.c.

Referenced by SDL_GetCPUFeatures(), and SDL_HasAVX512F().

◆ CPU_HAS_LASX

#define CPU_HAS_LASX   (1 << 15)

Definition at line 96 of file SDL_cpuinfo.c.

Referenced by SDL_GetCPUFeatures(), and SDL_HasLASX().

◆ CPU_HAS_LSX

#define CPU_HAS_LSX   (1 << 14)

Definition at line 95 of file SDL_cpuinfo.c.

Referenced by SDL_GetCPUFeatures(), and SDL_HasLSX().

◆ CPU_HAS_MMX

#define CPU_HAS_MMX   (1 << 2)

Definition at line 83 of file SDL_cpuinfo.c.

Referenced by SDL_GetCPUFeatures(), and SDL_HasMMX().

◆ CPU_HAS_MSA

#define CPU_HAS_MSA   (1 << 13)

Definition at line 94 of file SDL_cpuinfo.c.

Referenced by SDL_GetCPUFeatures(), and SDL_HasMSA().

◆ CPU_HAS_NEON

#define CPU_HAS_NEON   (1 << 11)

Definition at line 92 of file SDL_cpuinfo.c.

Referenced by SDL_GetCPUFeatures(), and SDL_HasNEON().

◆ CPU_HAS_RDTSC

#define CPU_HAS_RDTSC   (1 << 0)

Definition at line 81 of file SDL_cpuinfo.c.

Referenced by SDL_GetCPUFeatures(), and SDL_HasRDTSC().

◆ CPU_HAS_SSE

#define CPU_HAS_SSE   (1 << 4)

Definition at line 85 of file SDL_cpuinfo.c.

Referenced by SDL_GetCPUFeatures(), and SDL_HasSSE().

◆ CPU_HAS_SSE2

#define CPU_HAS_SSE2   (1 << 5)

Definition at line 86 of file SDL_cpuinfo.c.

Referenced by SDL_GetCPUFeatures(), and SDL_HasSSE2().

◆ CPU_HAS_SSE3

#define CPU_HAS_SSE3   (1 << 6)

Definition at line 87 of file SDL_cpuinfo.c.

Referenced by SDL_GetCPUFeatures(), and SDL_HasSSE3().

◆ CPU_HAS_SSE41

#define CPU_HAS_SSE41   (1 << 7)

Definition at line 88 of file SDL_cpuinfo.c.

Referenced by SDL_GetCPUFeatures(), and SDL_HasSSE41().

◆ CPU_HAS_SSE42

#define CPU_HAS_SSE42   (1 << 8)

Definition at line 89 of file SDL_cpuinfo.c.

Referenced by SDL_GetCPUFeatures(), and SDL_HasSSE42().

◆ CPU_haveAVX

#define CPU_haveAVX ( )    (CPU_OSSavesYMM && (CPU_CPUIDFeatures[2] & 0x10000000))

Definition at line 454 of file SDL_cpuinfo.c.

Referenced by SDL_GetCPUFeatures().

◆ CPU_haveLASX

#define CPU_haveLASX ( )    (CPU_readCPUCFG() & CPU_CFG2_LASX)

Definition at line 431 of file SDL_cpuinfo.c.

Referenced by SDL_GetCPUFeatures().

◆ CPU_haveLSX

#define CPU_haveLSX ( )    (CPU_readCPUCFG() & CPU_CFG2_LSX)

Definition at line 430 of file SDL_cpuinfo.c.

Referenced by SDL_GetCPUFeatures().

◆ CPU_haveMMX

#define CPU_haveMMX ( )    (CPU_CPUIDFeatures[3] & 0x00800000)

Definition at line 448 of file SDL_cpuinfo.c.

Referenced by SDL_GetCPUFeatures().

◆ CPU_haveRDTSC

#define CPU_haveRDTSC ( )    (CPU_CPUIDFeatures[3] & 0x00000010)

Definition at line 447 of file SDL_cpuinfo.c.

Referenced by SDL_GetCPUFeatures().

◆ CPU_haveSSE

#define CPU_haveSSE ( )    (CPU_CPUIDFeatures[3] & 0x02000000)

Definition at line 449 of file SDL_cpuinfo.c.

Referenced by SDL_GetCPUFeatures().

◆ CPU_haveSSE2

#define CPU_haveSSE2 ( )    (CPU_CPUIDFeatures[3] & 0x04000000)

Definition at line 450 of file SDL_cpuinfo.c.

Referenced by SDL_GetCPUFeatures().

◆ CPU_haveSSE3

#define CPU_haveSSE3 ( )    (CPU_CPUIDFeatures[2] & 0x00000001)

Definition at line 451 of file SDL_cpuinfo.c.

Referenced by SDL_GetCPUFeatures().

◆ CPU_haveSSE41

#define CPU_haveSSE41 ( )    (CPU_CPUIDFeatures[2] & 0x00080000)

Definition at line 452 of file SDL_cpuinfo.c.

Referenced by SDL_GetCPUFeatures().

◆ CPU_haveSSE42

#define CPU_haveSSE42 ( )    (CPU_CPUIDFeatures[2] & 0x00100000)

Definition at line 453 of file SDL_cpuinfo.c.

Referenced by SDL_GetCPUFeatures().

◆ cpuid

#define cpuid (   func,
  a,
  b,
  c,
  d 
)    do { a = b = c = d = 0; (void) a; (void) b; (void) c; (void) d; } while (0)

Function Documentation

◆ CPU_calcCPUIDFeatures()

static void CPU_calcCPUIDFeatures ( void  )
static

Definition at line 262 of file SDL_cpuinfo.c.

References CPU_CPUIDFeatures, CPU_CPUIDMaxFunction, CPU_haveCPUID(), CPU_OSSavesYMM, CPU_OSSavesZMM, cpuid, d, SDL_FALSE, and SDL_TRUE.

Referenced by SDL_GetCPUFeatures(), and SDL_GetCPUType().

263 {
264  static SDL_bool checked = SDL_FALSE;
265  if (!checked) {
266  checked = SDL_TRUE;
267  if (CPU_haveCPUID()) {
268  int a, b, c, d;
269  cpuid(0, a, b, c, d);
271  if (CPU_CPUIDMaxFunction >= 1) {
272  cpuid(1, a, b, c, d);
273  CPU_CPUIDFeatures[0] = a;
274  CPU_CPUIDFeatures[1] = b;
275  CPU_CPUIDFeatures[2] = c;
276  CPU_CPUIDFeatures[3] = d;
277 
278  /* Check to make sure we can call xgetbv */
279  if (c & 0x08000000) {
280  /* Call xgetbv to see if YMM (etc) register state is saved */
281 #if defined(__GNUC__) && (defined(i386) || defined(__x86_64__))
282  __asm__(".byte 0x0f, 0x01, 0xd0" : "=a" (a) : "c" (0) : "%edx");
283 #elif defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64)) && (_MSC_FULL_VER >= 160040219) /* VS2010 SP1 */
284  a = (int)_xgetbv(0);
285 #elif (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
286  __asm
287  {
288  xor ecx, ecx
289  _asm _emit 0x0f _asm _emit 0x01 _asm _emit 0xd0
290  mov a, eax
291  }
292 #endif
293  CPU_OSSavesYMM = ((a & 6) == 6) ? SDL_TRUE : SDL_FALSE;
294  CPU_OSSavesZMM = (CPU_OSSavesYMM && ((a & 0xe0) == 0xe0)) ? SDL_TRUE : SDL_FALSE;
295  }
296  }
297  }
298  }
299 }
static SDL_bool CPU_OSSavesYMM
Definition: SDL_cpuinfo.c:258
static int CPU_CPUIDMaxFunction
Definition: SDL_cpuinfo.c:257
static int CPU_haveCPUID(void)
Definition: SDL_cpuinfo.c:115
static SDL_bool CPU_OSSavesZMM
Definition: SDL_cpuinfo.c:259
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
const GLubyte * c
#define cpuid(func, a, b, c, d)
Definition: SDL_cpuinfo.c:252
SDL_bool
Definition: SDL_stdinc.h:161
static int CPU_CPUIDFeatures[4]
Definition: SDL_cpuinfo.c:256
GLboolean GLboolean GLboolean GLboolean a
GLboolean GLboolean GLboolean b

◆ CPU_have3DNow()

static int CPU_have3DNow ( void  )
static

Definition at line 434 of file SDL_cpuinfo.c.

References CPU_CPUIDMaxFunction, cpuid, and d.

Referenced by SDL_GetCPUFeatures().

435 {
436  if (CPU_CPUIDMaxFunction > 0) { /* that is, do we have CPUID at all? */
437  int a, b, c, d;
438  cpuid(0x80000000, a, b, c, d);
439  if (a >= 0x80000001) {
440  cpuid(0x80000001, a, b, c, d);
441  return (d & 0x80000000);
442  }
443  }
444  return 0;
445 }
static int CPU_CPUIDMaxFunction
Definition: SDL_cpuinfo.c:257
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
const GLubyte * c
#define cpuid(func, a, b, c, d)
Definition: SDL_cpuinfo.c:252
GLboolean GLboolean GLboolean GLboolean a
GLboolean GLboolean GLboolean b

◆ CPU_haveAltiVec()

static int CPU_haveAltiVec ( void  )
static

Definition at line 302 of file SDL_cpuinfo.c.

References NULL, and void.

Referenced by SDL_GetCPUFeatures().

303 {
304  volatile int altivec = 0;
305 #ifndef SDL_CPUINFO_DISABLED
306 #if (defined(__MACOSX__) && (defined(__ppc__) || defined(__ppc64__))) || (defined(__OpenBSD__) && defined(__powerpc__))
307 #ifdef __OpenBSD__
308  int selectors[2] = { CTL_MACHDEP, CPU_ALTIVEC };
309 #else
310  int selectors[2] = { CTL_HW, HW_VECTORUNIT };
311 #endif
312  int hasVectorUnit = 0;
313  size_t length = sizeof(hasVectorUnit);
314  int error = sysctl(selectors, 2, &hasVectorUnit, &length, NULL, 0);
315  if (0 == error)
316  altivec = (hasVectorUnit != 0);
317 #elif SDL_ALTIVEC_BLITTERS && HAVE_SETJMP
318  void (*handler) (int sig);
319  handler = signal(SIGILL, illegal_instruction);
320  if (setjmp(jmpbuf) == 0) {
321  asm volatile ("mtspr 256, %0\n\t" "vand %%v0, %%v0, %%v0"::"r" (-1));
322  altivec = 1;
323  }
324  signal(SIGILL, handler);
325 #endif
326 #endif
327  return altivec;
328 }
#define NULL
Definition: begin_code.h:164
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
GLuint GLsizei GLsizei * length

◆ CPU_haveAVX2()

static int CPU_haveAVX2 ( void  )
static

Definition at line 457 of file SDL_cpuinfo.c.

References CPU_CPUIDMaxFunction, CPU_OSSavesYMM, cpuid, d, and void.

Referenced by SDL_GetCPUFeatures().

458 {
459  if (CPU_OSSavesYMM && (CPU_CPUIDMaxFunction >= 7)) {
460  int a, b, c, d;
461  (void) a; (void) b; (void) c; (void) d; /* compiler warnings... */
462  cpuid(7, a, b, c, d);
463  return (b & 0x00000020);
464  }
465  return 0;
466 }
static SDL_bool CPU_OSSavesYMM
Definition: SDL_cpuinfo.c:258
static int CPU_CPUIDMaxFunction
Definition: SDL_cpuinfo.c:257
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
const GLubyte * c
#define cpuid(func, a, b, c, d)
Definition: SDL_cpuinfo.c:252
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
GLboolean GLboolean GLboolean GLboolean a
GLboolean GLboolean GLboolean b

◆ CPU_haveAVX512F()

static int CPU_haveAVX512F ( void  )
static

Definition at line 469 of file SDL_cpuinfo.c.

References CPU_CPUIDMaxFunction, CPU_OSSavesZMM, cpuid, d, and void.

Referenced by SDL_GetCPUFeatures().

470 {
471  if (CPU_OSSavesZMM && (CPU_CPUIDMaxFunction >= 7)) {
472  int a, b, c, d;
473  (void) a; (void) b; (void) c; (void) d; /* compiler warnings... */
474  cpuid(7, a, b, c, d);
475  return (b & 0x00010000);
476  }
477  return 0;
478 }
static int CPU_CPUIDMaxFunction
Definition: SDL_cpuinfo.c:257
static SDL_bool CPU_OSSavesZMM
Definition: SDL_cpuinfo.c:259
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
const GLubyte * c
#define cpuid(func, a, b, c, d)
Definition: SDL_cpuinfo.c:252
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
GLboolean GLboolean GLboolean GLboolean a
GLboolean GLboolean GLboolean b

◆ CPU_haveCPUID()

static int CPU_haveCPUID ( void  )
static

Definition at line 115 of file SDL_cpuinfo.c.

References done, and pop.

Referenced by CPU_calcCPUIDFeatures().

116 {
117  int has_CPUID = 0;
118 
119 /* *INDENT-OFF* */
120 #ifndef SDL_CPUINFO_DISABLED
121 #if defined(__GNUC__) && defined(i386)
122  __asm__ (
123 " pushfl # Get original EFLAGS \n"
124 " popl %%eax \n"
125 " movl %%eax,%%ecx \n"
126 " xorl $0x200000,%%eax # Flip ID bit in EFLAGS \n"
127 " pushl %%eax # Save new EFLAGS value on stack \n"
128 " popfl # Replace current EFLAGS value \n"
129 " pushfl # Get new EFLAGS \n"
130 " popl %%eax # Store new EFLAGS in EAX \n"
131 " xorl %%ecx,%%eax # Can not toggle ID bit, \n"
132 " jz 1f # Processor=80486 \n"
133 " movl $1,%0 # We have CPUID support \n"
134 "1: \n"
135  : "=m" (has_CPUID)
136  :
137  : "%eax", "%ecx"
138  );
139 #elif defined(__GNUC__) && defined(__x86_64__)
140 /* Technically, if this is being compiled under __x86_64__ then it has
141  CPUid by definition. But it's nice to be able to prove it. :) */
142  __asm__ (
143 " pushfq # Get original EFLAGS \n"
144 " popq %%rax \n"
145 " movq %%rax,%%rcx \n"
146 " xorl $0x200000,%%eax # Flip ID bit in EFLAGS \n"
147 " pushq %%rax # Save new EFLAGS value on stack \n"
148 " popfq # Replace current EFLAGS value \n"
149 " pushfq # Get new EFLAGS \n"
150 " popq %%rax # Store new EFLAGS in EAX \n"
151 " xorl %%ecx,%%eax # Can not toggle ID bit, \n"
152 " jz 1f # Processor=80486 \n"
153 " movl $1,%0 # We have CPUID support \n"
154 "1: \n"
155  : "=m" (has_CPUID)
156  :
157  : "%rax", "%rcx"
158  );
159 #elif (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
160  __asm {
161  pushfd ; Get original EFLAGS
162  pop eax
163  mov ecx, eax
164  xor eax, 200000h ; Flip ID bit in EFLAGS
165  push eax ; Save new EFLAGS value on stack
166  popfd ; Replace current EFLAGS value
167  pushfd ; Get new EFLAGS
168  pop eax ; Store new EFLAGS in EAX
169  xor eax, ecx ; Can not toggle ID bit,
170  jz done ; Processor=80486
171  mov has_CPUID,1 ; We have CPUID support
172 done:
173  }
174 #elif defined(_MSC_VER) && defined(_M_X64)
175  has_CPUID = 1;
176 #elif defined(__sun) && defined(__i386)
177  __asm (
178 " pushfl \n"
179 " popl %eax \n"
180 " movl %eax,%ecx \n"
181 " xorl $0x200000,%eax \n"
182 " pushl %eax \n"
183 " popfl \n"
184 " pushfl \n"
185 " popl %eax \n"
186 " xorl %ecx,%eax \n"
187 " jz 1f \n"
188 " movl $1,-8(%ebp) \n"
189 "1: \n"
190  );
191 #elif defined(__sun) && defined(__amd64)
192  __asm (
193 " pushfq \n"
194 " popq %rax \n"
195 " movq %rax,%rcx \n"
196 " xorl $0x200000,%eax \n"
197 " pushq %rax \n"
198 " popfq \n"
199 " pushfq \n"
200 " popq %rax \n"
201 " xorl %ecx,%eax \n"
202 " jz 1f \n"
203 " movl $1,-8(%rbp) \n"
204 "1: \n"
205  );
206 #endif
207 #endif
208 /* *INDENT-ON* */
209  return has_CPUID;
210 }
GLfloat GLfloat GLfloat GLfloat h
#define pop
Definition: SDL_qsort.c:192
int done
Definition: checkkeys.c:28
GLsizei const GLfloat * value
GLuint in

◆ CPU_haveMSA()

static int CPU_haveMSA ( void  )
static

Definition at line 381 of file SDL_cpuinfo.c.

References memset.

Referenced by SDL_GetCPUFeatures().

382 {
383  uint32_t flags = 0;
384  char buf[1024];
385 
386 # ifdef __linux__
387  FILE* fp = fopen("/proc/cpuinfo", "r");
388  if (!fp)
389  return flags;
390 
391  memset(buf, 0, sizeof(buf));
392  while (fgets(buf, sizeof(buf), fp)) {
393  if (!strncmp(buf, "cpu model", strlen("cpu model"))) {
394  /* In case of some kernel havn't add loongson extention in ASEs implemented,
395  * analysis cpu model is still needed.
396  */
397  if (strstr(buf, "Loongson-2K")) {
398  flags = 1;
399  break;
400  }
401  }
402  if (!strncmp(buf, "ASEs implemented", strlen("ASEs implemented"))) {
403  if (strstr(buf, "msa")) {
404  flags = 1;
405  }
406  break;
407  }
408  }
409 
410  fclose(fp);
411 # endif
412  return flags;
413 
414 }
#define memset
Definition: SDL_malloc.c:619
GLenum GLuint GLenum GLsizei const GLchar * buf
unsigned int uint32_t
GLbitfield flags

◆ CPU_haveNEON()

static int CPU_haveNEON ( void  )
static

Definition at line 352 of file SDL_cpuinfo.c.

Referenced by SDL_GetCPUFeatures().

353 {
354 /* The way you detect NEON is a privileged instruction on ARM, so you have
355  query the OS kernel in a platform-specific way. :/ */
356 #if defined(SDL_CPUINFO_DISABLED) || !defined(__ARM_ARCH)
357  return 0; /* disabled or not an ARM CPU at all. */
358 #elif __ARM_ARCH >= 8
359  return 1; /* ARMv8 always has non-optional NEON support. */
360 #elif defined(__APPLE__) && (__ARM_ARCH >= 7)
361  /* (note that sysctlbyname("hw.optional.neon") doesn't work!) */
362  return 1; /* all Apple ARMv7 chips and later have NEON. */
363 #elif defined(__APPLE__)
364  return 0; /* assume anything else from Apple doesn't have NEON. */
365 #elif defined(__QNXNTO__)
366  return SYSPAGE_ENTRY(cpuinfo)->flags & ARM_CPU_FLAG_NEON;
367 #elif (defined(__LINUX__) || defined(__ANDROID__)) && defined(HAVE_GETAUXVAL)
368  return ((getauxval(AT_HWCAP) & HWCAP_NEON) == HWCAP_NEON);
369 #elif (defined(__LINUX__) || defined(__ANDROID__))
370  return readProcAuxvForNeon(); /* Android offers a static library for this, but it just parses /proc/self/auxv */
371 #elif (defined(__WINDOWS__) || defined(__WINRT__)) && defined(_M_ARM)
372  /* All WinRT ARM devices are required to support NEON, but just in case. */
373  return IsProcessorFeaturePresent(PF_ARM_NEON_INSTRUCTIONS_AVAILABLE) != 0;
374 #else
375 #warning SDL_HasNEON is not implemented for this ARM platform. Write me.
376  return 0;
377 #endif
378 }

◆ CPU_readCPUCFG()

static int CPU_readCPUCFG ( void  )
static

Definition at line 417 of file SDL_cpuinfo.c.

References CPU_CFG2.

418 {
419  uint32_t cfg2 = 0;
420 #if defined __loongarch__
421  __asm__ volatile(
422  "cpucfg %0, %1 \n\t"
423  : "+&r"(cfg2)
424  : "r"(CPU_CFG2)
425  );
426 #endif
427  return cfg2;
428 }
#define CPU_CFG2
Definition: SDL_cpuinfo.c:98
unsigned int uint32_t

◆ SDL_GetCPUCacheLineSize()

int SDL_GetCPUCacheLineSize ( void  )

This function returns the L1 cache line size of the CPU

This is useful for determining multi-threaded structure padding or SIMD prefetch sizes.

Definition at line 633 of file SDL_cpuinfo.c.

References cpuid, d, SDL_CACHELINE_SIZE, SDL_GetCPUType(), SDL_strcmp, and void.

Referenced by SDL_SIMDFree().

634 {
635  const char *cpuType = SDL_GetCPUType();
636  int a, b, c, d;
637  (void) a; (void) b; (void) c; (void) d;
638  if (SDL_strcmp(cpuType, "GenuineIntel") == 0) {
639  cpuid(0x00000001, a, b, c, d);
640  return (((b >> 8) & 0xff) * 8);
641  } else if (SDL_strcmp(cpuType, "AuthenticAMD") == 0) {
642  cpuid(0x80000005, a, b, c, d);
643  return (c & 0xff);
644  } else {
645  /* Just make a guess here... */
646  return SDL_CACHELINE_SIZE;
647  }
648 }
static const char * SDL_GetCPUType(void)
Definition: SDL_cpuinfo.c:522
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
#define SDL_CACHELINE_SIZE
Definition: SDL_cpuinfo.h:107
const GLubyte * c
#define cpuid(func, a, b, c, d)
Definition: SDL_cpuinfo.c:252
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
#define SDL_strcmp
GLboolean GLboolean GLboolean GLboolean a
GLboolean GLboolean GLboolean b

◆ SDL_GetCPUCount()

int SDL_GetCPUCount ( void  )

This function returns the number of CPU cores available.

Definition at line 483 of file SDL_cpuinfo.c.

References NULL, and SDL_CPUCount.

Referenced by SDL_SIMDFree().

484 {
485  if (!SDL_CPUCount) {
486 #ifndef SDL_CPUINFO_DISABLED
487 #if defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN)
488  if (SDL_CPUCount <= 0) {
489  SDL_CPUCount = (int)sysconf(_SC_NPROCESSORS_ONLN);
490  }
491 #endif
492 #ifdef HAVE_SYSCTLBYNAME
493  if (SDL_CPUCount <= 0) {
494  size_t size = sizeof(SDL_CPUCount);
495  sysctlbyname("hw.ncpu", &SDL_CPUCount, &size, NULL, 0);
496  }
497 #endif
498 #ifdef __WIN32__
499  if (SDL_CPUCount <= 0) {
500  SYSTEM_INFO info;
501  GetSystemInfo(&info);
502  SDL_CPUCount = info.dwNumberOfProcessors;
503  }
504 #endif
505 #ifdef __OS2__
506  if (SDL_CPUCount <= 0) {
507  DosQuerySysInfo(QSV_NUMPROCESSORS, QSV_NUMPROCESSORS,
508  &SDL_CPUCount, sizeof(SDL_CPUCount) );
509  }
510 #endif
511 #endif
512  /* There has to be at least 1, right? :) */
513  if (SDL_CPUCount <= 0) {
514  SDL_CPUCount = 1;
515  }
516  }
517  return SDL_CPUCount;
518 }
GLsizeiptr size
#define NULL
Definition: begin_code.h:164
static int SDL_CPUCount
Definition: SDL_cpuinfo.c:480

◆ SDL_GetCPUFeatures()

static Uint32 SDL_GetCPUFeatures ( void  )
static

Definition at line 654 of file SDL_cpuinfo.c.

References CPU_calcCPUIDFeatures(), CPU_HAS_3DNOW, CPU_HAS_ALTIVEC, CPU_HAS_AVX, CPU_HAS_AVX2, CPU_HAS_AVX512F, CPU_HAS_LASX, CPU_HAS_LSX, CPU_HAS_MMX, CPU_HAS_MSA, CPU_HAS_NEON, CPU_HAS_RDTSC, CPU_HAS_SSE, CPU_HAS_SSE2, CPU_HAS_SSE3, CPU_HAS_SSE41, CPU_HAS_SSE42, CPU_have3DNow(), CPU_haveAltiVec(), CPU_haveAVX, CPU_haveAVX2(), CPU_haveAVX512F(), CPU_haveLASX, CPU_haveLSX, CPU_haveMMX, CPU_haveMSA(), CPU_haveNEON(), CPU_haveRDTSC, CPU_haveSSE, CPU_haveSSE2, CPU_haveSSE3, CPU_haveSSE41, CPU_haveSSE42, SDL_CPUFeatures, SDL_max, and SDL_SIMDAlignment.

Referenced by SDL_SIMDGetAlignment().

655 {
656  if (SDL_CPUFeatures == 0xFFFFFFFF) {
658  SDL_CPUFeatures = 0;
659  SDL_SIMDAlignment = 4; /* a good safe base value */
660  if (CPU_haveRDTSC()) {
662  }
663  if (CPU_haveAltiVec()) {
666  }
667  if (CPU_haveMMX()) {
670  }
671  if (CPU_have3DNow()) {
674  }
675  if (CPU_haveSSE()) {
678  }
679  if (CPU_haveSSE2()) {
682  }
683  if (CPU_haveSSE3()) {
686  }
687  if (CPU_haveSSE41()) {
690  }
691  if (CPU_haveSSE42()) {
694  }
695  if (CPU_haveAVX()) {
698  }
699  if (CPU_haveAVX2()) {
702  }
703  if (CPU_haveAVX512F()) {
706  }
707  if (CPU_haveNEON()) {
710  }
711  if (CPU_haveMSA()) {
714  }
715  if (CPU_haveLSX()) {
718  }
719  if (CPU_haveLASX()) {
722  }
723  }
724  return SDL_CPUFeatures;
725 }
#define CPU_haveSSE2()
Definition: SDL_cpuinfo.c:450
#define CPU_haveAVX()
Definition: SDL_cpuinfo.c:454
#define CPU_HAS_SSE41
Definition: SDL_cpuinfo.c:88
#define CPU_HAS_RDTSC
Definition: SDL_cpuinfo.c:81
#define CPU_HAS_SSE3
Definition: SDL_cpuinfo.c:87
#define CPU_HAS_LSX
Definition: SDL_cpuinfo.c:95
static void CPU_calcCPUIDFeatures(void)
Definition: SDL_cpuinfo.c:262
#define CPU_haveLSX()
Definition: SDL_cpuinfo.c:430
#define CPU_HAS_SSE2
Definition: SDL_cpuinfo.c:86
#define CPU_HAS_AVX512F
Definition: SDL_cpuinfo.c:93
#define CPU_HAS_MSA
Definition: SDL_cpuinfo.c:94
static int CPU_have3DNow(void)
Definition: SDL_cpuinfo.c:434
#define CPU_haveRDTSC()
Definition: SDL_cpuinfo.c:447
static int CPU_haveMSA(void)
Definition: SDL_cpuinfo.c:381
#define CPU_HAS_ALTIVEC
Definition: SDL_cpuinfo.c:82
#define CPU_haveMMX()
Definition: SDL_cpuinfo.c:448
#define SDL_max(x, y)
Definition: SDL_stdinc.h:407
static int CPU_haveAltiVec(void)
Definition: SDL_cpuinfo.c:302
static int CPU_haveAVX512F(void)
Definition: SDL_cpuinfo.c:469
#define CPU_HAS_AVX
Definition: SDL_cpuinfo.c:90
#define CPU_haveSSE42()
Definition: SDL_cpuinfo.c:453
#define CPU_HAS_NEON
Definition: SDL_cpuinfo.c:92
#define CPU_haveSSE3()
Definition: SDL_cpuinfo.c:451
static Uint32 SDL_CPUFeatures
Definition: SDL_cpuinfo.c:650
#define CPU_HAS_SSE
Definition: SDL_cpuinfo.c:85
static int CPU_haveAVX2(void)
Definition: SDL_cpuinfo.c:457
#define CPU_HAS_AVX2
Definition: SDL_cpuinfo.c:91
static int CPU_haveNEON(void)
Definition: SDL_cpuinfo.c:352
#define CPU_haveSSE()
Definition: SDL_cpuinfo.c:449
#define CPU_HAS_MMX
Definition: SDL_cpuinfo.c:83
#define CPU_haveLASX()
Definition: SDL_cpuinfo.c:431
#define CPU_haveSSE41()
Definition: SDL_cpuinfo.c:452
#define CPU_HAS_SSE42
Definition: SDL_cpuinfo.c:89
static Uint32 SDL_SIMDAlignment
Definition: SDL_cpuinfo.c:651
#define CPU_HAS_LASX
Definition: SDL_cpuinfo.c:96
#define CPU_HAS_3DNOW
Definition: SDL_cpuinfo.c:84

◆ SDL_GetCPUType()

static const char* SDL_GetCPUType ( void  )
static

Definition at line 522 of file SDL_cpuinfo.c.

References CPU_calcCPUIDFeatures(), CPU_CPUIDMaxFunction, cpuid, d, i, SDL_strlcpy, and void.

Referenced by SDL_GetCPUCacheLineSize(), and SDL_SIMDFree().

523 {
524  static char SDL_CPUType[13];
525 
526  if (!SDL_CPUType[0]) {
527  int i = 0;
528 
530  if (CPU_CPUIDMaxFunction > 0) { /* do we have CPUID at all? */
531  int a, b, c, d;
532  cpuid(0x00000000, a, b, c, d);
533  (void) a;
534  SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
535  SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
536  SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
537  SDL_CPUType[i++] = (char)(b & 0xff);
538 
539  SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
540  SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
541  SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
542  SDL_CPUType[i++] = (char)(d & 0xff);
543 
544  SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
545  SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
546  SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
547  SDL_CPUType[i++] = (char)(c & 0xff);
548  }
549  if (!SDL_CPUType[0]) {
550  SDL_strlcpy(SDL_CPUType, "Unknown", sizeof(SDL_CPUType));
551  }
552  }
553  return SDL_CPUType;
554 }
#define SDL_strlcpy
static void CPU_calcCPUIDFeatures(void)
Definition: SDL_cpuinfo.c:262
static int CPU_CPUIDMaxFunction
Definition: SDL_cpuinfo.c:257
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
const GLubyte * c
#define cpuid(func, a, b, c, d)
Definition: SDL_cpuinfo.c:252
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)
Definition: SDL_x11sym.h:50
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
GLboolean GLboolean GLboolean GLboolean a
GLboolean GLboolean GLboolean b

◆ SDL_GetSystemRAM()

int SDL_GetSystemRAM ( void  )

This function returns the amount of RAM configured in the system, in MB.

Definition at line 827 of file SDL_cpuinfo.c.

References NULL, and SDL_SystemRAM.

Referenced by SDL_SIMDFree().

828 {
829  if (!SDL_SystemRAM) {
830 #ifndef SDL_CPUINFO_DISABLED
831 #if defined(HAVE_SYSCONF) && defined(_SC_PHYS_PAGES) && defined(_SC_PAGESIZE)
832  if (SDL_SystemRAM <= 0) {
833  SDL_SystemRAM = (int)((Sint64)sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE) / (1024*1024));
834  }
835 #endif
836 #ifdef HAVE_SYSCTLBYNAME
837  if (SDL_SystemRAM <= 0) {
838 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__)
839 #ifdef HW_REALMEM
840  int mib[2] = {CTL_HW, HW_REALMEM};
841 #else
842  /* might only report up to 2 GiB */
843  int mib[2] = {CTL_HW, HW_PHYSMEM};
844 #endif /* HW_REALMEM */
845 #else
846  int mib[2] = {CTL_HW, HW_MEMSIZE};
847 #endif /* __FreeBSD__ || __FreeBSD_kernel__ */
848  Uint64 memsize = 0;
849  size_t len = sizeof(memsize);
850 
851  if (sysctl(mib, 2, &memsize, &len, NULL, 0) == 0) {
852  SDL_SystemRAM = (int)(memsize / (1024*1024));
853  }
854  }
855 #endif
856 #ifdef __WIN32__
857  if (SDL_SystemRAM <= 0) {
858  MEMORYSTATUSEX stat;
859  stat.dwLength = sizeof(stat);
860  if (GlobalMemoryStatusEx(&stat)) {
861  SDL_SystemRAM = (int)(stat.ullTotalPhys / (1024 * 1024));
862  }
863  }
864 #endif
865 #ifdef __OS2__
866  if (SDL_SystemRAM <= 0) {
867  Uint32 sysram = 0;
868  DosQuerySysInfo(QSV_TOTPHYSMEM, QSV_TOTPHYSMEM, &sysram, 4);
869  SDL_SystemRAM = (int) (sysram / 0x100000U);
870  }
871 #endif
872 #endif
873  }
874  return SDL_SystemRAM;
875 }
uint32_t Uint32
Definition: SDL_stdinc.h:203
static int SDL_SystemRAM
Definition: SDL_cpuinfo.c:824
uint64_t Uint64
Definition: SDL_stdinc.h:216
GLenum GLsizei len
#define NULL
Definition: begin_code.h:164
int64_t Sint64
Definition: SDL_stdinc.h:210

◆ SDL_Has3DNow()

SDL_bool SDL_Has3DNow ( void  )

This function returns true if the CPU has 3DNow! features.

Definition at line 747 of file SDL_cpuinfo.c.

References CPU_FEATURE_AVAILABLE, and CPU_HAS_3DNOW.

Referenced by SDL_SIMDFree().

748 {
750 }
#define CPU_FEATURE_AVAILABLE(f)
Definition: SDL_cpuinfo.c:727
#define CPU_HAS_3DNOW
Definition: SDL_cpuinfo.c:84

◆ SDL_HasAltiVec()

SDL_bool SDL_HasAltiVec ( void  )

This function returns true if the CPU has AltiVec features.

Definition at line 735 of file SDL_cpuinfo.c.

References CPU_FEATURE_AVAILABLE, and CPU_HAS_ALTIVEC.

Referenced by SDL_SIMDFree().

736 {
738 }
#define CPU_HAS_ALTIVEC
Definition: SDL_cpuinfo.c:82
#define CPU_FEATURE_AVAILABLE(f)
Definition: SDL_cpuinfo.c:727

◆ SDL_HasAVX()

SDL_bool SDL_HasAVX ( void  )

This function returns true if the CPU has AVX features.

Definition at line 783 of file SDL_cpuinfo.c.

References CPU_FEATURE_AVAILABLE, and CPU_HAS_AVX.

Referenced by SDL_SIMDFree().

784 {
786 }
#define CPU_HAS_AVX
Definition: SDL_cpuinfo.c:90
#define CPU_FEATURE_AVAILABLE(f)
Definition: SDL_cpuinfo.c:727

◆ SDL_HasAVX2()

SDL_bool SDL_HasAVX2 ( void  )

This function returns true if the CPU has AVX2 features.

Definition at line 789 of file SDL_cpuinfo.c.

References CPU_FEATURE_AVAILABLE, and CPU_HAS_AVX2.

Referenced by SDL_SIMDFree().

790 {
792 }
#define CPU_FEATURE_AVAILABLE(f)
Definition: SDL_cpuinfo.c:727
#define CPU_HAS_AVX2
Definition: SDL_cpuinfo.c:91

◆ SDL_HasAVX512F()

SDL_bool SDL_HasAVX512F ( void  )

This function returns true if the CPU has AVX-512F (foundation) features.

Definition at line 795 of file SDL_cpuinfo.c.

References CPU_FEATURE_AVAILABLE, and CPU_HAS_AVX512F.

Referenced by SDL_SIMDFree().

796 {
798 }
#define CPU_HAS_AVX512F
Definition: SDL_cpuinfo.c:93
#define CPU_FEATURE_AVAILABLE(f)
Definition: SDL_cpuinfo.c:727

◆ SDL_HasLASX()

SDL_bool SDL_HasLASX ( void  )

This function returns true if the CPU has LASX (LOONGARCH SIMD) features.

Definition at line 819 of file SDL_cpuinfo.c.

References CPU_FEATURE_AVAILABLE, and CPU_HAS_LASX.

Referenced by SDL_SIMDFree().

820 {
822 }
#define CPU_FEATURE_AVAILABLE(f)
Definition: SDL_cpuinfo.c:727
#define CPU_HAS_LASX
Definition: SDL_cpuinfo.c:96

◆ SDL_HasLSX()

SDL_bool SDL_HasLSX ( void  )

This function returns true if the CPU has LSX (LOONGARCH SIMD) features.

Definition at line 813 of file SDL_cpuinfo.c.

References CPU_FEATURE_AVAILABLE, and CPU_HAS_LSX.

Referenced by SDL_SIMDFree(), and yuv_rgb_lsx().

814 {
816 }
#define CPU_HAS_LSX
Definition: SDL_cpuinfo.c:95
#define CPU_FEATURE_AVAILABLE(f)
Definition: SDL_cpuinfo.c:727

◆ SDL_HasMMX()

SDL_bool SDL_HasMMX ( void  )

This function returns true if the CPU has MMX features.

Definition at line 741 of file SDL_cpuinfo.c.

References CPU_FEATURE_AVAILABLE, and CPU_HAS_MMX.

Referenced by SDL_SIMDFree().

742 {
744 }
#define CPU_FEATURE_AVAILABLE(f)
Definition: SDL_cpuinfo.c:727
#define CPU_HAS_MMX
Definition: SDL_cpuinfo.c:83

◆ SDL_HasMSA()

SDL_bool SDL_HasMSA ( void  )

This function returns true if the CPU has MSA (MIPS SIMD) features.

Definition at line 807 of file SDL_cpuinfo.c.

References CPU_FEATURE_AVAILABLE, and CPU_HAS_MSA.

Referenced by SDL_SIMDFree().

808 {
810 }
#define CPU_HAS_MSA
Definition: SDL_cpuinfo.c:94
#define CPU_FEATURE_AVAILABLE(f)
Definition: SDL_cpuinfo.c:727

◆ SDL_HasNEON()

SDL_bool SDL_HasNEON ( void  )

This function returns true if the CPU has NEON (ARM SIMD) features.

Definition at line 801 of file SDL_cpuinfo.c.

References CPU_FEATURE_AVAILABLE, and CPU_HAS_NEON.

Referenced by SDL_SIMDFree().

802 {
804 }
#define CPU_FEATURE_AVAILABLE(f)
Definition: SDL_cpuinfo.c:727
#define CPU_HAS_NEON
Definition: SDL_cpuinfo.c:92

◆ SDL_HasRDTSC()

SDL_bool SDL_HasRDTSC ( void  )

This function returns true if the CPU has the RDTSC instruction.

Definition at line 729 of file SDL_cpuinfo.c.

References CPU_FEATURE_AVAILABLE, and CPU_HAS_RDTSC.

Referenced by SDL_SIMDFree().

730 {
732 }
#define CPU_HAS_RDTSC
Definition: SDL_cpuinfo.c:81
#define CPU_FEATURE_AVAILABLE(f)
Definition: SDL_cpuinfo.c:727

◆ SDL_HasSSE()

SDL_bool SDL_HasSSE ( void  )

This function returns true if the CPU has SSE features.

Definition at line 753 of file SDL_cpuinfo.c.

References CPU_FEATURE_AVAILABLE, and CPU_HAS_SSE.

Referenced by SDL_SIMDFree().

754 {
756 }
#define CPU_FEATURE_AVAILABLE(f)
Definition: SDL_cpuinfo.c:727
#define CPU_HAS_SSE
Definition: SDL_cpuinfo.c:85

◆ SDL_HasSSE2()

SDL_bool SDL_HasSSE2 ( void  )

This function returns true if the CPU has SSE2 features.

Definition at line 759 of file SDL_cpuinfo.c.

References CPU_FEATURE_AVAILABLE, and CPU_HAS_SSE2.

Referenced by SDL_SIMDFree().

760 {
762 }
#define CPU_HAS_SSE2
Definition: SDL_cpuinfo.c:86
#define CPU_FEATURE_AVAILABLE(f)
Definition: SDL_cpuinfo.c:727

◆ SDL_HasSSE3()

SDL_bool SDL_HasSSE3 ( void  )

This function returns true if the CPU has SSE3 features.

Definition at line 765 of file SDL_cpuinfo.c.

References CPU_FEATURE_AVAILABLE, and CPU_HAS_SSE3.

Referenced by SDL_SIMDFree().

766 {
768 }
#define CPU_HAS_SSE3
Definition: SDL_cpuinfo.c:87
#define CPU_FEATURE_AVAILABLE(f)
Definition: SDL_cpuinfo.c:727

◆ SDL_HasSSE41()

SDL_bool SDL_HasSSE41 ( void  )

This function returns true if the CPU has SSE4.1 features.

Definition at line 771 of file SDL_cpuinfo.c.

References CPU_FEATURE_AVAILABLE, and CPU_HAS_SSE41.

Referenced by SDL_SIMDFree().

772 {
774 }
#define CPU_HAS_SSE41
Definition: SDL_cpuinfo.c:88
#define CPU_FEATURE_AVAILABLE(f)
Definition: SDL_cpuinfo.c:727

◆ SDL_HasSSE42()

SDL_bool SDL_HasSSE42 ( void  )

This function returns true if the CPU has SSE4.2 features.

Definition at line 777 of file SDL_cpuinfo.c.

References CPU_FEATURE_AVAILABLE, and CPU_HAS_SSE42.

Referenced by SDL_SIMDFree().

778 {
780 }
#define CPU_FEATURE_AVAILABLE(f)
Definition: SDL_cpuinfo.c:727
#define CPU_HAS_SSE42
Definition: SDL_cpuinfo.c:89

◆ SDL_SIMDAlloc()

void* SDL_SIMDAlloc ( const size_t  len)

Allocate memory in a SIMD-friendly way.

This will allocate a block of memory that is suitable for use with SIMD instructions. Specifically, it will be properly aligned and padded for the system's supported vector instructions.

The memory returned will be padded such that it is safe to read or write an incomplete vector at the end of the memory block. This can be useful so you don't have to drop back to a scalar fallback at the end of your SIMD processing loop to deal with the final elements without overflowing the allocated buffer.

You must free this memory with SDL_FreeSIMD(), not free() or SDL_free() or delete[], etc.

Note that SDL will only deal with SIMD instruction sets it is aware of; for example, SDL 2.0.8 knows that SSE wants 16-byte vectors (SDL_HasSSE()), and AVX2 wants 32 bytes (SDL_HasAVX2()), but doesn't know that AVX-512 wants 64. To be clear: if you can't decide to use an instruction set with an SDL_Has*() function, don't use that instruction set with memory allocated through here.

SDL_AllocSIMD(0) will return a non-NULL pointer, assuming the system isn't out of memory.

Parameters
lenThe length, in bytes, of the block to allocated. The actual allocated block might be larger due to padding, etc.
Returns
Pointer to newly-allocated block, NULL if out of memory.
See also
SDL_SIMDAlignment
SDL_SIMDFree

Definition at line 889 of file SDL_cpuinfo.c.

References NULL, retval, SDL_malloc, and SDL_SIMDGetAlignment().

890 {
891  const size_t alignment = SDL_SIMDGetAlignment();
892  const size_t padding = alignment - (len % alignment);
893  const size_t padded = (padding != alignment) ? (len + padding) : len;
894  Uint8 *retval = NULL;
895  Uint8 *ptr = (Uint8 *) SDL_malloc(padded + alignment + sizeof (void *));
896  if (ptr) {
897  /* store the actual malloc pointer right before our aligned pointer. */
898  retval = ptr + sizeof (void *);
899  retval += alignment - (((size_t) retval) % alignment);
900  *(((void **) retval) - 1) = ptr;
901  }
902  return retval;
903 }
uint8_t Uint8
Definition: SDL_stdinc.h:179
GLenum GLsizei len
unsigned int size_t
SDL_bool retval
#define NULL
Definition: begin_code.h:164
#define SDL_malloc
size_t SDL_SIMDGetAlignment(void)
Report the alignment this system needs for SIMD allocations.
Definition: SDL_cpuinfo.c:879

◆ SDL_SIMDFree()

void SDL_SIMDFree ( void ptr)

Deallocate memory obtained from SDL_SIMDAlloc.

It is not valid to use this function on a pointer from anything but SDL_SIMDAlloc(). It can't be used on pointers from malloc, realloc, SDL_malloc, memalign, new[], etc.

However, SDL_SIMDFree(NULL) is a legal no-op.

See also
SDL_SIMDAlloc

Definition at line 906 of file SDL_cpuinfo.c.

References main, SDL_free, SDL_GetCPUCacheLineSize(), SDL_GetCPUCount(), SDL_GetCPUType(), SDL_GetSystemRAM(), SDL_Has3DNow(), SDL_HasAltiVec(), SDL_HasAVX(), SDL_HasAVX2(), SDL_HasAVX512F(), SDL_HasLASX(), SDL_HasLSX(), SDL_HasMMX(), SDL_HasMSA(), SDL_HasNEON(), SDL_HasRDTSC(), SDL_HasSSE(), SDL_HasSSE2(), SDL_HasSSE3(), SDL_HasSSE41(), and SDL_HasSSE42().

907 {
908  if (ptr) {
909  void **realptr = (void **) ptr;
910  realptr--;
911  SDL_free(*(((void **) ptr) - 1));
912  }
913 }
#define SDL_free

◆ SDL_SIMDGetAlignment()

size_t SDL_SIMDGetAlignment ( void  )

Report the alignment this system needs for SIMD allocations.

This will return the minimum number of bytes to which a pointer must be aligned to be compatible with SIMD instructions on the current machine. For example, if the machine supports SSE only, it will return 16, but if it supports AVX-512F, it'll return 64 (etc). This only reports values for instruction sets SDL knows about, so if your SDL build doesn't have SDL_HasAVX512F(), then it might return 16 for the SSE support it sees and not 64 for the AVX-512 instructions that exist but SDL doesn't know about. Plan accordingly.

Definition at line 879 of file SDL_cpuinfo.c.

References SDL_assert, SDL_GetCPUFeatures(), and SDL_SIMDAlignment.

Referenced by SDL_SIMDAlloc().

880 {
881  if (SDL_SIMDAlignment == 0xFFFFFFFF) {
882  SDL_GetCPUFeatures(); /* make sure this has been calculated */
883  }
885  return SDL_SIMDAlignment;
886 }
static Uint32 SDL_GetCPUFeatures(void)
Definition: SDL_cpuinfo.c:654
#define SDL_assert(condition)
Definition: SDL_assert.h:169
static Uint32 SDL_SIMDAlignment
Definition: SDL_cpuinfo.c:651

Variable Documentation

◆ CPU_CPUIDFeatures

int CPU_CPUIDFeatures[4]
static

Definition at line 256 of file SDL_cpuinfo.c.

Referenced by CPU_calcCPUIDFeatures().

◆ CPU_CPUIDMaxFunction

int CPU_CPUIDMaxFunction = 0
static

◆ CPU_OSSavesYMM

SDL_bool CPU_OSSavesYMM = SDL_FALSE
static

Definition at line 258 of file SDL_cpuinfo.c.

Referenced by CPU_calcCPUIDFeatures(), and CPU_haveAVX2().

◆ CPU_OSSavesZMM

SDL_bool CPU_OSSavesZMM = SDL_FALSE
static

Definition at line 259 of file SDL_cpuinfo.c.

Referenced by CPU_calcCPUIDFeatures(), and CPU_haveAVX512F().

◆ SDL_CPUCount

int SDL_CPUCount = 0
static

Definition at line 480 of file SDL_cpuinfo.c.

Referenced by SDL_GetCPUCount().

◆ SDL_CPUFeatures

Uint32 SDL_CPUFeatures = 0xFFFFFFFF
static

Definition at line 650 of file SDL_cpuinfo.c.

Referenced by SDL_GetCPUFeatures().

◆ SDL_SIMDAlignment

Uint32 SDL_SIMDAlignment = 0xFFFFFFFF
static

Definition at line 651 of file SDL_cpuinfo.c.

Referenced by SDL_GetCPUFeatures(), and SDL_SIMDGetAlignment().

◆ SDL_SystemRAM

int SDL_SystemRAM = 0
static

Definition at line 824 of file SDL_cpuinfo.c.

Referenced by SDL_GetSystemRAM().