Skip to content

Latest commit

 

History

History
1057 lines (969 loc) · 31.9 KB

SDL_cpuinfo.c

File metadata and controls

1057 lines (969 loc) · 31.9 KB
 
1
2
/*
Simple DirectMedia Layer
Jan 17, 2020
Jan 17, 2020
3
Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#ifdef TEST_MAIN
#include "SDL_config.h"
#else
#include "../SDL_internal.h"
#endif
Mar 19, 2019
Mar 19, 2019
27
#if defined(__WIN32__) || defined(__WINRT__)
28
29
#include "../core/windows/SDL_windows.h"
#endif
Aug 21, 2017
Aug 21, 2017
30
31
32
33
34
35
36
#if defined(__OS2__)
#define INCL_DOS
#include <os2.h>
#ifndef QSV_NUMPROCESSORS
#define QSV_NUMPROCESSORS 26
#endif
#endif
37
38
39
40
/* CPU feature detection for SDL */
#include "SDL_cpuinfo.h"
May 21, 2018
May 21, 2018
41
#include "SDL_assert.h"
42
43
44
45
46
47
48
49
50
51
#ifdef HAVE_SYSCONF
#include <unistd.h>
#endif
#ifdef HAVE_SYSCTLBYNAME
#include <sys/types.h>
#include <sys/sysctl.h>
#endif
#if defined(__MACOSX__) && (defined(__ppc__) || defined(__ppc64__))
#include <sys/sysctl.h> /* For AltiVec check */
Jul 11, 2020
Jul 11, 2020
52
#elif (defined(__OpenBSD__) || defined(__FreeBSD__)) && defined(__powerpc__)
53
54
55
56
57
58
59
60
#include <sys/param.h>
#include <sys/sysctl.h> /* For AltiVec check */
#include <machine/cpu.h>
#elif SDL_ALTIVEC_BLITTERS && HAVE_SETJMP
#include <signal.h>
#include <setjmp.h>
#endif
Aug 17, 2017
Aug 17, 2017
61
62
63
64
#if defined(__QNXNTO__)
#include <sys/syspage.h>
#endif
Nov 17, 2016
Nov 17, 2016
65
#if (defined(__LINUX__) || defined(__ANDROID__)) && defined(__ARM_ARCH)
Nov 17, 2016
Nov 17, 2016
66
67
68
69
/*#include <asm/hwcap.h>*/
#ifndef AT_HWCAP
#define AT_HWCAP 16
#endif
Oct 27, 2019
Oct 27, 2019
70
71
72
73
74
75
76
#ifndef AT_PLATFORM
#define AT_PLATFORM 15
#endif
/* Prevent compilation error when including elf.h would also try to define AT_* as an enum */
#ifndef AT_NULL
#define AT_NULL 0
#endif
Nov 17, 2016
Nov 17, 2016
77
78
79
#ifndef HWCAP_NEON
#define HWCAP_NEON (1 << 12)
#endif
Nov 17, 2016
Nov 17, 2016
80
81
82
#if defined HAVE_GETAUXVAL
#include <sys/auxv.h>
#else
Nov 17, 2016
Nov 17, 2016
83
84
#include <fcntl.h>
#endif
Nov 17, 2016
Nov 17, 2016
85
86
#endif
Dec 1, 2018
Dec 1, 2018
87
88
89
90
91
92
#if defined(__ANDROID__) && defined(__ARM_ARCH) && !defined(HAVE_GETAUXVAL)
#if __ARM_ARCH < 8
#include <cpu-features.h>
#endif
#endif
Jan 6, 2020
Jan 6, 2020
93
94
95
96
97
#ifdef __RISCOS__
#include <kernel.h>
#include <swis.h>
#endif
May 14, 2018
May 14, 2018
98
99
100
101
102
103
104
105
106
107
108
109
#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)
May 21, 2018
May 21, 2018
110
#define CPU_HAS_AVX512F (1 << 12)
Oct 25, 2019
Oct 25, 2019
111
#define CPU_HAS_ARM_SIMD (1 << 13)
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#if SDL_ALTIVEC_BLITTERS && HAVE_SETJMP && !__MACOSX__ && !__OpenBSD__
/* This is the brute force way of detecting instruction sets...
the idea is borrowed from the libmpeg2 library - thanks!
*/
static jmp_buf jmpbuf;
static void
illegal_instruction(int sig)
{
longjmp(jmpbuf, 1);
}
#endif /* HAVE_SETJMP */
static int
CPU_haveCPUID(void)
{
int has_CPUID = 0;
Nov 17, 2016
Nov 17, 2016
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/* *INDENT-OFF* */
#ifndef SDL_CPUINFO_DISABLED
#if defined(__GNUC__) && defined(i386)
__asm__ (
" pushfl # Get original EFLAGS \n"
" popl %%eax \n"
" movl %%eax,%%ecx \n"
" xorl $0x200000,%%eax # Flip ID bit in EFLAGS \n"
" pushl %%eax # Save new EFLAGS value on stack \n"
" popfl # Replace current EFLAGS value \n"
" pushfl # Get new EFLAGS \n"
" popl %%eax # Store new EFLAGS in EAX \n"
" xorl %%ecx,%%eax # Can not toggle ID bit, \n"
" jz 1f # Processor=80486 \n"
" movl $1,%0 # We have CPUID support \n"
"1: \n"
: "=m" (has_CPUID)
:
: "%eax", "%ecx"
);
#elif defined(__GNUC__) && defined(__x86_64__)
/* Technically, if this is being compiled under __x86_64__ then it has
CPUid by definition. But it's nice to be able to prove it. :) */
__asm__ (
" pushfq # Get original EFLAGS \n"
" popq %%rax \n"
" movq %%rax,%%rcx \n"
" xorl $0x200000,%%eax # Flip ID bit in EFLAGS \n"
" pushq %%rax # Save new EFLAGS value on stack \n"
" popfq # Replace current EFLAGS value \n"
" pushfq # Get new EFLAGS \n"
" popq %%rax # Store new EFLAGS in EAX \n"
" xorl %%ecx,%%eax # Can not toggle ID bit, \n"
" jz 1f # Processor=80486 \n"
" movl $1,%0 # We have CPUID support \n"
"1: \n"
: "=m" (has_CPUID)
:
: "%rax", "%rcx"
);
#elif (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
__asm {
pushfd ; Get original EFLAGS
pop eax
mov ecx, eax
xor eax, 200000h ; Flip ID bit in EFLAGS
push eax ; Save new EFLAGS value on stack
popfd ; Replace current EFLAGS value
pushfd ; Get new EFLAGS
pop eax ; Store new EFLAGS in EAX
xor eax, ecx ; Can not toggle ID bit,
jz done ; Processor=80486
mov has_CPUID,1 ; We have CPUID support
done:
}
#elif defined(_MSC_VER) && defined(_M_X64)
has_CPUID = 1;
#elif defined(__sun) && defined(__i386)
__asm (
" pushfl \n"
" popl %eax \n"
" movl %eax,%ecx \n"
" xorl $0x200000,%eax \n"
" pushl %eax \n"
" popfl \n"
" pushfl \n"
" popl %eax \n"
" xorl %ecx,%eax \n"
" jz 1f \n"
" movl $1,-8(%ebp) \n"
"1: \n"
);
#elif defined(__sun) && defined(__amd64)
__asm (
" pushfq \n"
" popq %rax \n"
" movq %rax,%rcx \n"
" xorl $0x200000,%eax \n"
" pushq %rax \n"
" popfq \n"
" pushfq \n"
" popq %rax \n"
" xorl %ecx,%eax \n"
" jz 1f \n"
" movl $1,-8(%rbp) \n"
"1: \n"
);
#endif
#endif
/* *INDENT-ON* */
return has_CPUID;
}
#if defined(__GNUC__) && defined(i386)
#define cpuid(func, a, b, c, d) \
__asm__ __volatile__ ( \
" pushl %%ebx \n" \
" xorl %%ecx,%%ecx \n" \
" cpuid \n" \
" movl %%ebx, %%esi \n" \
" popl %%ebx \n" : \
"=a" (a), "=S" (b), "=c" (c), "=d" (d) : "a" (func))
#elif defined(__GNUC__) && defined(__x86_64__)
#define cpuid(func, a, b, c, d) \
__asm__ __volatile__ ( \
" pushq %%rbx \n" \
" xorq %%rcx,%%rcx \n" \
" cpuid \n" \
" movq %%rbx, %%rsi \n" \
" popq %%rbx \n" : \
"=a" (a), "=S" (b), "=c" (c), "=d" (d) : "a" (func))
#elif (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
#define cpuid(func, a, b, c, d) \
__asm { \
__asm mov eax, func \
__asm xor ecx, ecx \
__asm cpuid \
__asm mov a, eax \
__asm mov b, ebx \
__asm mov c, ecx \
__asm mov d, edx \
}
#elif defined(_MSC_VER) && defined(_M_X64)
#define cpuid(func, a, b, c, d) \
{ \
int CPUInfo[4]; \
__cpuid(CPUInfo, func); \
a = CPUInfo[0]; \
b = CPUInfo[1]; \
c = CPUInfo[2]; \
d = CPUInfo[3]; \
}
#else
#define cpuid(func, a, b, c, d) \
Mar 3, 2017
Mar 3, 2017
264
do { a = b = c = d = 0; (void) a; (void) b; (void) c; (void) d; } while (0)
Nov 17, 2016
Nov 17, 2016
267
268
269
static int CPU_CPUIDFeatures[4];
static int CPU_CPUIDMaxFunction = 0;
static SDL_bool CPU_OSSavesYMM = SDL_FALSE;
May 21, 2018
May 21, 2018
270
static SDL_bool CPU_OSSavesZMM = SDL_FALSE;
Nov 17, 2016
Nov 17, 2016
272
273
static void
CPU_calcCPUIDFeatures(void)
Nov 17, 2016
Nov 17, 2016
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
static SDL_bool checked = SDL_FALSE;
if (!checked) {
checked = SDL_TRUE;
if (CPU_haveCPUID()) {
int a, b, c, d;
cpuid(0, a, b, c, d);
CPU_CPUIDMaxFunction = a;
if (CPU_CPUIDMaxFunction >= 1) {
cpuid(1, a, b, c, d);
CPU_CPUIDFeatures[0] = a;
CPU_CPUIDFeatures[1] = b;
CPU_CPUIDFeatures[2] = c;
CPU_CPUIDFeatures[3] = d;
/* Check to make sure we can call xgetbv */
if (c & 0x08000000) {
May 21, 2018
May 21, 2018
291
/* Call xgetbv to see if YMM (etc) register state is saved */
292
#if defined(__GNUC__) && (defined(i386) || defined(__x86_64__))
Mar 7, 2017
Mar 7, 2017
293
__asm__(".byte 0x0f, 0x01, 0xd0" : "=a" (a) : "c" (0) : "%edx");
294
#elif defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64)) && (_MSC_FULL_VER >= 160040219) /* VS2010 SP1 */
Nov 17, 2016
Nov 17, 2016
295
a = (int)_xgetbv(0);
296
#elif (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
Nov 17, 2016
Nov 17, 2016
297
298
299
300
301
302
__asm
{
xor ecx, ecx
_asm _emit 0x0f _asm _emit 0x01 _asm _emit 0xd0
mov a, eax
}
Nov 17, 2016
Nov 17, 2016
304
CPU_OSSavesYMM = ((a & 6) == 6) ? SDL_TRUE : SDL_FALSE;
May 21, 2018
May 21, 2018
305
CPU_OSSavesZMM = (CPU_OSSavesYMM && ((a & 0xe0) == 0xe0)) ? SDL_TRUE : SDL_FALSE;
Nov 17, 2016
Nov 17, 2016
306
307
308
}
}
}
309
310
311
312
313
314
315
316
}
}
static int
CPU_haveAltiVec(void)
{
volatile int altivec = 0;
#ifndef SDL_CPUINFO_DISABLED
Jul 11, 2020
Jul 11, 2020
317
#if (defined(__MACOSX__) && (defined(__ppc__) || defined(__ppc64__))) || (defined(__OpenBSD__) && defined(__powerpc__)) || (defined(__FreeBSD__) && defined(__powerpc__))
318
319
#ifdef __OpenBSD__
int selectors[2] = { CTL_MACHDEP, CPU_ALTIVEC };
Jul 11, 2020
Jul 11, 2020
320
321
#elif defined(__FreeBSD__)
int selectors[2] = { CTL_HW, PPC_FEATURE_HAS_ALTIVEC };
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#else
int selectors[2] = { CTL_HW, HW_VECTORUNIT };
#endif
int hasVectorUnit = 0;
size_t length = sizeof(hasVectorUnit);
int error = sysctl(selectors, 2, &hasVectorUnit, &length, NULL, 0);
if (0 == error)
altivec = (hasVectorUnit != 0);
#elif SDL_ALTIVEC_BLITTERS && HAVE_SETJMP
void (*handler) (int sig);
handler = signal(SIGILL, illegal_instruction);
if (setjmp(jmpbuf) == 0) {
asm volatile ("mtspr 256, %0\n\t" "vand %%v0, %%v0, %%v0"::"r" (-1));
altivec = 1;
}
signal(SIGILL, handler);
#endif
#endif
return altivec;
}
Feb 4, 2020
Feb 4, 2020
343
344
345
346
347
348
349
350
#if defined(__ARM_ARCH) && (__ARM_ARCH >= 6)
static int
CPU_haveARMSIMD(void)
{
return 1;
}
#elif !defined(__arm__)
Nov 13, 2019
Nov 13, 2019
351
352
353
354
355
static int
CPU_haveARMSIMD(void)
{
return 0;
}
Oct 25, 2019
Oct 25, 2019
356
Nov 13, 2019
Nov 13, 2019
357
#elif defined(__LINUX__)
Oct 25, 2019
Oct 25, 2019
358
359
360
361
362
363
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <elf.h>
Nov 13, 2019
Nov 13, 2019
364
static int
Oct 25, 2019
Oct 25, 2019
365
366
367
368
369
370
371
372
373
374
375
376
377
378
CPU_haveARMSIMD(void)
{
int arm_simd = 0;
int fd;
fd = open("/proc/self/auxv", O_RDONLY);
if (fd >= 0)
{
Elf32_auxv_t aux;
while (read(fd, &aux, sizeof aux) == sizeof aux)
{
if (aux.a_type == AT_PLATFORM)
{
const char *plat = (const char *) aux.a_un.a_val;
Nov 16, 2019
Nov 16, 2019
379
380
381
382
if (plat) {
arm_simd = strncmp(plat, "v6l", 3) == 0 ||
strncmp(plat, "v7l", 3) == 0;
}
Oct 25, 2019
Oct 25, 2019
383
384
385
386
387
388
389
}
}
close(fd);
}
return arm_simd;
}
Jan 6, 2020
Jan 6, 2020
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
#elif defined(__RISCOS__)
static int
CPU_haveARMSIMD(void)
{
_kernel_swi_regs regs;
regs.r[0] = 0;
if (_kernel_swi(OS_PlatformFeatures, &regs, &regs) != NULL)
return 0;
if (!(regs.r[0] & (1<<31)))
return 0;
regs.r[0] = 34;
regs.r[1] = 29;
if (_kernel_swi(OS_PlatformFeatures, &regs, &regs) != NULL)
return 0;
return regs.r[0];
}
Oct 25, 2019
Oct 25, 2019
411
#else
Nov 13, 2019
Nov 13, 2019
412
static int
Oct 25, 2019
Oct 25, 2019
413
414
CPU_haveARMSIMD(void)
{
Feb 4, 2020
Feb 4, 2020
415
416
#warning SDL_HasARMSIMD is not implemented for this ARM platform. Write me.
return 0;
Oct 25, 2019
Oct 25, 2019
417
418
419
}
#endif
Nov 18, 2019
Nov 18, 2019
420
#if defined(__LINUX__) && defined(__ARM_ARCH) && !defined(HAVE_GETAUXVAL)
Nov 17, 2016
Nov 17, 2016
421
static int
Nov 17, 2016
Nov 17, 2016
422
readProcAuxvForNeon(void)
Nov 17, 2016
Nov 17, 2016
423
424
{
int neon = 0;
Nov 17, 2016
Nov 17, 2016
425
426
int kv[2];
const int fd = open("/proc/self/auxv", O_RDONLY);
Nov 17, 2016
Nov 17, 2016
427
428
429
430
431
432
if (fd != -1) {
while (read(fd, kv, sizeof (kv)) == sizeof (kv)) {
if (kv[0] == AT_HWCAP) {
neon = ((kv[1] & HWCAP_NEON) == HWCAP_NEON);
break;
}
Nov 17, 2016
Nov 17, 2016
433
}
Nov 17, 2016
Nov 17, 2016
434
close(fd);
Nov 17, 2016
Nov 17, 2016
435
436
437
438
}
return neon;
}
#endif
Nov 17, 2016
Nov 17, 2016
439
Nov 17, 2016
Nov 17, 2016
440
441
442
static int
CPU_haveNEON(void)
{
Nov 17, 2016
Nov 17, 2016
443
444
/* The way you detect NEON is a privileged instruction on ARM, so you have
query the OS kernel in a platform-specific way. :/ */
Dec 4, 2018
Dec 4, 2018
445
446
447
448
449
450
451
452
453
454
#if defined(SDL_CPUINFO_DISABLED)
return 0; /* disabled */
#elif (defined(__WINDOWS__) || defined(__WINRT__)) && (defined(_M_ARM) || defined(_M_ARM64))
/* Visual Studio, for ARM, doesn't define __ARM_ARCH. Handle this first. */
/* Seems to have been removed */
# if !defined(PF_ARM_NEON_INSTRUCTIONS_AVAILABLE)
# define PF_ARM_NEON_INSTRUCTIONS_AVAILABLE 19
# endif
/* All WinRT ARM devices are required to support NEON, but just in case. */
return IsProcessorFeaturePresent(PF_ARM_NEON_INSTRUCTIONS_AVAILABLE) != 0;
Feb 4, 2020
Feb 4, 2020
455
#elif defined(__ARM_ARCH) && (__ARM_ARCH >= 8)
Nov 22, 2016
Nov 22, 2016
456
return 1; /* ARMv8 always has non-optional NEON support. */
Feb 4, 2020
Feb 4, 2020
457
#elif defined(__APPLE__) && defined(__ARM_ARCH) && (__ARM_ARCH >= 7)
Nov 22, 2016
Nov 22, 2016
458
459
/* (note that sysctlbyname("hw.optional.neon") doesn't work!) */
return 1; /* all Apple ARMv7 chips and later have NEON. */
Nov 17, 2016
Nov 17, 2016
460
#elif defined(__APPLE__)
Nov 12, 2017
Nov 12, 2017
461
return 0; /* assume anything else from Apple doesn't have NEON. */
Jun 23, 2020
Jun 23, 2020
462
463
#elif defined(__OpenBSD__)
return 1; /* OpenBSD only supports ARMv7 CPUs that have NEON. */
Feb 4, 2020
Feb 4, 2020
464
465
#elif !defined(__arm__)
return 0; /* not an ARM CPU at all. */
Aug 17, 2017
Aug 17, 2017
466
467
#elif defined(__QNXNTO__)
return SYSPAGE_ENTRY(cpuinfo)->flags & ARM_CPU_FLAG_NEON;
Nov 17, 2016
Nov 17, 2016
468
#elif (defined(__LINUX__) || defined(__ANDROID__)) && defined(HAVE_GETAUXVAL)
Nov 17, 2016
Nov 17, 2016
469
return ((getauxval(AT_HWCAP) & HWCAP_NEON) == HWCAP_NEON);
Dec 1, 2018
Dec 1, 2018
470
471
472
473
474
475
476
477
478
479
480
481
482
483
#elif defined(__LINUX__)
return readProcAuxvForNeon();
#elif defined(__ANDROID__)
/* Use NDK cpufeatures to read either /proc/self/auxv or /proc/cpuinfo */
{
AndroidCpuFamily cpu_family = android_getCpuFamily();
if (cpu_family == ANDROID_CPU_FAMILY_ARM) {
uint64_t cpu_features = android_getCpuFeatures();
if ((cpu_features & ANDROID_CPU_ARM_FEATURE_NEON) != 0) {
return 1;
}
}
return 0;
}
Jan 6, 2020
Jan 6, 2020
484
485
486
487
488
489
490
491
492
493
494
495
#elif defined(__RISCOS__)
/* Use the VFPSupport_Features SWI to access the MVFR registers */
{
_kernel_swi_regs regs;
regs.r[0] = 0;
if (_kernel_swi(VFPSupport_Features, &regs, &regs) == NULL) {
if ((regs.r[2] & 0xFFF000) == 0x111000) {
return 1;
}
}
return 0;
}
Nov 17, 2016
Nov 17, 2016
496
497
#else
#warning SDL_HasNEON is not implemented for this ARM platform. Write me.
Nov 17, 2016
Nov 17, 2016
498
return 0;
Nov 17, 2016
Nov 17, 2016
499
500
501
#endif
}
502
503
504
static int
CPU_have3DNow(void)
{
Nov 17, 2016
Nov 17, 2016
505
if (CPU_CPUIDMaxFunction > 0) { /* that is, do we have CPUID at all? */
506
507
508
509
510
511
512
513
514
515
int a, b, c, d;
cpuid(0x80000000, a, b, c, d);
if (a >= 0x80000001) {
cpuid(0x80000001, a, b, c, d);
return (d & 0x80000000);
}
}
return 0;
}
Nov 17, 2016
Nov 17, 2016
516
517
518
519
520
521
522
523
#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))
524
525
526
527
static int
CPU_haveAVX2(void)
{
Nov 17, 2016
Nov 17, 2016
528
if (CPU_OSSavesYMM && (CPU_CPUIDMaxFunction >= 7)) {
Nov 17, 2016
Nov 17, 2016
530
(void) a; (void) b; (void) c; (void) d; /* compiler warnings... */
Nov 17, 2016
Nov 17, 2016
531
532
cpuid(7, a, b, c, d);
return (b & 0x00000020);
533
534
535
536
}
return 0;
}
May 21, 2018
May 21, 2018
537
538
539
540
541
542
543
544
545
546
547
548
static int
CPU_haveAVX512F(void)
{
if (CPU_OSSavesZMM && (CPU_CPUIDMaxFunction >= 7)) {
int a, b, c, d;
(void) a; (void) b; (void) c; (void) d; /* compiler warnings... */
cpuid(7, a, b, c, d);
return (b & 0x00010000);
}
return 0;
}
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
static int SDL_CPUCount = 0;
int
SDL_GetCPUCount(void)
{
if (!SDL_CPUCount) {
#ifndef SDL_CPUINFO_DISABLED
#if defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN)
if (SDL_CPUCount <= 0) {
SDL_CPUCount = (int)sysconf(_SC_NPROCESSORS_ONLN);
}
#endif
#ifdef HAVE_SYSCTLBYNAME
if (SDL_CPUCount <= 0) {
size_t size = sizeof(SDL_CPUCount);
sysctlbyname("hw.ncpu", &SDL_CPUCount, &size, NULL, 0);
}
#endif
#ifdef __WIN32__
if (SDL_CPUCount <= 0) {
SYSTEM_INFO info;
GetSystemInfo(&info);
SDL_CPUCount = info.dwNumberOfProcessors;
}
#endif
Aug 21, 2017
Aug 21, 2017
574
575
576
577
578
579
#ifdef __OS2__
if (SDL_CPUCount <= 0) {
DosQuerySysInfo(QSV_NUMPROCESSORS, QSV_NUMPROCESSORS,
&SDL_CPUCount, sizeof(SDL_CPUCount) );
}
#endif
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
#endif
/* There has to be at least 1, right? :) */
if (SDL_CPUCount <= 0) {
SDL_CPUCount = 1;
}
}
return SDL_CPUCount;
}
/* Oh, such a sweet sweet trick, just not very useful. :) */
static const char *
SDL_GetCPUType(void)
{
static char SDL_CPUType[13];
if (!SDL_CPUType[0]) {
int i = 0;
Nov 17, 2016
Nov 17, 2016
598
599
CPU_calcCPUIDFeatures();
if (CPU_CPUIDMaxFunction > 0) { /* do we have CPUID at all? */
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
int a, b, c, d;
cpuid(0x00000000, a, b, c, d);
(void) a;
SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
SDL_CPUType[i++] = (char)(b & 0xff);
SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
SDL_CPUType[i++] = (char)(d & 0xff);
SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
SDL_CPUType[i++] = (char)(c & 0xff);
}
if (!SDL_CPUType[0]) {
SDL_strlcpy(SDL_CPUType, "Unknown", sizeof(SDL_CPUType));
}
}
return SDL_CPUType;
}
#ifdef TEST_MAIN /* !!! FIXME: only used for test at the moment. */
static const char *
SDL_GetCPUName(void)
{
static char SDL_CPUName[48];
if (!SDL_CPUName[0]) {
int i = 0;
int a, b, c, d;
Nov 17, 2016
Nov 17, 2016
636
637
CPU_calcCPUIDFeatures();
if (CPU_CPUIDMaxFunction > 0) { /* do we have CPUID at all? */
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
cpuid(0x80000000, a, b, c, d);
if (a >= 0x80000004) {
cpuid(0x80000002, a, b, c, d);
SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
cpuid(0x80000003, a, b, c, d);
SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
cpuid(0x80000004, a, b, c, d);
SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
}
}
if (!SDL_CPUName[0]) {
SDL_strlcpy(SDL_CPUName, "Unknown", sizeof(SDL_CPUName));
}
}
return SDL_CPUName;
}
#endif
int
SDL_GetCPUCacheLineSize(void)
{
const char *cpuType = SDL_GetCPUType();
int a, b, c, d;
(void) a; (void) b; (void) c; (void) d;
if (SDL_strcmp(cpuType, "GenuineIntel") == 0) {
cpuid(0x00000001, a, b, c, d);
return (((b >> 8) & 0xff) * 8);
May 15, 2019
May 15, 2019
710
} else if (SDL_strcmp(cpuType, "AuthenticAMD") == 0 || SDL_strcmp(cpuType, "HygonGenuine") == 0) {
711
712
713
714
715
716
717
718
719
cpuid(0x80000005, a, b, c, d);
return (c & 0xff);
} else {
/* Just make a guess here... */
return SDL_CACHELINE_SIZE;
}
}
static Uint32 SDL_CPUFeatures = 0xFFFFFFFF;
May 21, 2018
May 21, 2018
720
static Uint32 SDL_SIMDAlignment = 0xFFFFFFFF;
721
722
723
724
725
static Uint32
SDL_GetCPUFeatures(void)
{
if (SDL_CPUFeatures == 0xFFFFFFFF) {
Nov 17, 2016
Nov 17, 2016
726
CPU_calcCPUIDFeatures();
727
SDL_CPUFeatures = 0;
Oct 21, 2019
Oct 21, 2019
728
SDL_SIMDAlignment = sizeof(void *); /* a good safe base value */
729
730
731
732
733
if (CPU_haveRDTSC()) {
SDL_CPUFeatures |= CPU_HAS_RDTSC;
}
if (CPU_haveAltiVec()) {
SDL_CPUFeatures |= CPU_HAS_ALTIVEC;
May 21, 2018
May 21, 2018
734
SDL_SIMDAlignment = SDL_max(SDL_SIMDAlignment, 16);
735
736
737
}
if (CPU_haveMMX()) {
SDL_CPUFeatures |= CPU_HAS_MMX;
May 21, 2018
May 21, 2018
738
SDL_SIMDAlignment = SDL_max(SDL_SIMDAlignment, 8);
739
740
741
}
if (CPU_have3DNow()) {
SDL_CPUFeatures |= CPU_HAS_3DNOW;
May 21, 2018
May 21, 2018
742
SDL_SIMDAlignment = SDL_max(SDL_SIMDAlignment, 8);
743
744
745
}
if (CPU_haveSSE()) {
SDL_CPUFeatures |= CPU_HAS_SSE;
May 21, 2018
May 21, 2018
746
SDL_SIMDAlignment = SDL_max(SDL_SIMDAlignment, 16);
747
748
749
}
if (CPU_haveSSE2()) {
SDL_CPUFeatures |= CPU_HAS_SSE2;
May 21, 2018
May 21, 2018
750
SDL_SIMDAlignment = SDL_max(SDL_SIMDAlignment, 16);
751
752
753
}
if (CPU_haveSSE3()) {
SDL_CPUFeatures |= CPU_HAS_SSE3;
May 21, 2018
May 21, 2018
754
SDL_SIMDAlignment = SDL_max(SDL_SIMDAlignment, 16);
755
756
757
}
if (CPU_haveSSE41()) {
SDL_CPUFeatures |= CPU_HAS_SSE41;
May 21, 2018
May 21, 2018
758
SDL_SIMDAlignment = SDL_max(SDL_SIMDAlignment, 16);
759
760
761
}
if (CPU_haveSSE42()) {
SDL_CPUFeatures |= CPU_HAS_SSE42;
May 21, 2018
May 21, 2018
762
SDL_SIMDAlignment = SDL_max(SDL_SIMDAlignment, 16);
763
764
765
}
if (CPU_haveAVX()) {
SDL_CPUFeatures |= CPU_HAS_AVX;
May 21, 2018
May 21, 2018
766
SDL_SIMDAlignment = SDL_max(SDL_SIMDAlignment, 32);
767
768
769
}
if (CPU_haveAVX2()) {
SDL_CPUFeatures |= CPU_HAS_AVX2;
May 21, 2018
May 21, 2018
770
SDL_SIMDAlignment = SDL_max(SDL_SIMDAlignment, 32);
May 21, 2018
May 21, 2018
772
773
774
775
if (CPU_haveAVX512F()) {
SDL_CPUFeatures |= CPU_HAS_AVX512F;
SDL_SIMDAlignment = SDL_max(SDL_SIMDAlignment, 64);
}
Oct 25, 2019
Oct 25, 2019
776
777
778
779
if (CPU_haveARMSIMD()) {
SDL_CPUFeatures |= CPU_HAS_ARM_SIMD;
SDL_SIMDAlignment = SDL_max(SDL_SIMDAlignment, 16);
}
Nov 17, 2016
Nov 17, 2016
780
781
if (CPU_haveNEON()) {
SDL_CPUFeatures |= CPU_HAS_NEON;
May 21, 2018
May 21, 2018
782
SDL_SIMDAlignment = SDL_max(SDL_SIMDAlignment, 16);
Nov 17, 2016
Nov 17, 2016
783
}
784
785
786
787
}
return SDL_CPUFeatures;
}
Nov 17, 2016
Nov 17, 2016
788
789
790
#define CPU_FEATURE_AVAILABLE(f) ((SDL_GetCPUFeatures() & f) ? SDL_TRUE : SDL_FALSE)
SDL_bool SDL_HasRDTSC(void)
Nov 17, 2016
Nov 17, 2016
792
return CPU_FEATURE_AVAILABLE(CPU_HAS_RDTSC);
793
794
795
796
797
}
SDL_bool
SDL_HasAltiVec(void)
{
Nov 17, 2016
Nov 17, 2016
798
return CPU_FEATURE_AVAILABLE(CPU_HAS_ALTIVEC);
799
800
801
802
803
}
SDL_bool
SDL_HasMMX(void)
{
Nov 17, 2016
Nov 17, 2016
804
return CPU_FEATURE_AVAILABLE(CPU_HAS_MMX);
805
806
807
808
809
}
SDL_bool
SDL_Has3DNow(void)
{
Nov 17, 2016
Nov 17, 2016
810
return CPU_FEATURE_AVAILABLE(CPU_HAS_3DNOW);
811
812
813
814
815
}
SDL_bool
SDL_HasSSE(void)
{
Nov 17, 2016
Nov 17, 2016
816
return CPU_FEATURE_AVAILABLE(CPU_HAS_SSE);
817
818
819
820
821
}
SDL_bool
SDL_HasSSE2(void)
{
Nov 17, 2016
Nov 17, 2016
822
return CPU_FEATURE_AVAILABLE(CPU_HAS_SSE2);
823
824
825
826
827
}
SDL_bool
SDL_HasSSE3(void)
{
Nov 17, 2016
Nov 17, 2016
828
return CPU_FEATURE_AVAILABLE(CPU_HAS_SSE3);
829
830
831
832
833
}
SDL_bool
SDL_HasSSE41(void)
{
Nov 17, 2016
Nov 17, 2016
834
return CPU_FEATURE_AVAILABLE(CPU_HAS_SSE41);
835
836
837
838
839
}
SDL_bool
SDL_HasSSE42(void)
{
Nov 17, 2016
Nov 17, 2016
840
return CPU_FEATURE_AVAILABLE(CPU_HAS_SSE42);
841
842
843
844
845
}
SDL_bool
SDL_HasAVX(void)
{
Nov 17, 2016
Nov 17, 2016
846
return CPU_FEATURE_AVAILABLE(CPU_HAS_AVX);
847
848
849
850
851
}
SDL_bool
SDL_HasAVX2(void)
{
Nov 17, 2016
Nov 17, 2016
852
return CPU_FEATURE_AVAILABLE(CPU_HAS_AVX2);
May 21, 2018
May 21, 2018
855
856
857
858
859
860
SDL_bool
SDL_HasAVX512F(void)
{
return CPU_FEATURE_AVAILABLE(CPU_HAS_AVX512F);
}
Oct 25, 2019
Oct 25, 2019
861
862
863
864
865
866
SDL_bool
SDL_HasARMSIMD(void)
{
return CPU_FEATURE_AVAILABLE(CPU_HAS_ARM_SIMD);
}
Nov 17, 2016
Nov 17, 2016
867
868
869
870
871
872
SDL_bool
SDL_HasNEON(void)
{
return CPU_FEATURE_AVAILABLE(CPU_HAS_NEON);
}
873
874
875
876
877
878
879
880
881
882
883
884
885
886
static int SDL_SystemRAM = 0;
int
SDL_GetSystemRAM(void)
{
if (!SDL_SystemRAM) {
#ifndef SDL_CPUINFO_DISABLED
#if defined(HAVE_SYSCONF) && defined(_SC_PHYS_PAGES) && defined(_SC_PAGESIZE)
if (SDL_SystemRAM <= 0) {
SDL_SystemRAM = (int)((Sint64)sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE) / (1024*1024));
}
#endif
#ifdef HAVE_SYSCTLBYNAME
if (SDL_SystemRAM <= 0) {
Dec 29, 2015
Dec 29, 2015
887
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__)
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
#ifdef HW_REALMEM
int mib[2] = {CTL_HW, HW_REALMEM};
#else
/* might only report up to 2 GiB */
int mib[2] = {CTL_HW, HW_PHYSMEM};
#endif /* HW_REALMEM */
#else
int mib[2] = {CTL_HW, HW_MEMSIZE};
#endif /* __FreeBSD__ || __FreeBSD_kernel__ */
Uint64 memsize = 0;
size_t len = sizeof(memsize);
if (sysctl(mib, 2, &memsize, &len, NULL, 0) == 0) {
SDL_SystemRAM = (int)(memsize / (1024*1024));
}
}
#endif
#ifdef __WIN32__
if (SDL_SystemRAM <= 0) {
MEMORYSTATUSEX stat;
stat.dwLength = sizeof(stat);
if (GlobalMemoryStatusEx(&stat)) {
SDL_SystemRAM = (int)(stat.ullTotalPhys / (1024 * 1024));
}
}
#endif
Aug 21, 2017
Aug 21, 2017
914
915
916
917
918
919
920
#ifdef __OS2__
if (SDL_SystemRAM <= 0) {
Uint32 sysram = 0;
DosQuerySysInfo(QSV_TOTPHYSMEM, QSV_TOTPHYSMEM, &sysram, 4);
SDL_SystemRAM = (int) (sysram / 0x100000U);
}
#endif
Jan 6, 2020
Jan 6, 2020
921
922
923
924
925
926
927
928
929
#ifdef __RISCOS__
if (SDL_SystemRAM <= 0) {
_kernel_swi_regs regs;
regs.r[0] = 0x108;
if (_kernel_swi(OS_Memory, &regs, &regs) == NULL) {
SDL_SystemRAM = (int)(regs.r[1] * regs.r[2] / (1024 * 1024));
}
}
#endif
930
931
932
933
934
935
#endif
}
return SDL_SystemRAM;
}
May 21, 2018
May 21, 2018
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
size_t
SDL_SIMDGetAlignment(void)
{
if (SDL_SIMDAlignment == 0xFFFFFFFF) {
SDL_GetCPUFeatures(); /* make sure this has been calculated */
}
SDL_assert(SDL_SIMDAlignment != 0);
return SDL_SIMDAlignment;
}
void *
SDL_SIMDAlloc(const size_t len)
{
const size_t alignment = SDL_SIMDGetAlignment();
const size_t padding = alignment - (len % alignment);
const size_t padded = (padding != alignment) ? (len + padding) : len;
Uint8 *retval = NULL;
Uint8 *ptr = (Uint8 *) SDL_malloc(padded + alignment + sizeof (void *));
if (ptr) {
/* store the actual malloc pointer right before our aligned pointer. */
retval = ptr + sizeof (void *);
retval += alignment - (((size_t) retval) % alignment);
*(((void **) retval) - 1) = ptr;
}
return retval;
}
Jun 11, 2020
Jun 11, 2020
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
void *
SDL_SIMDRealloc(void *mem, const size_t len)
{
const size_t alignment = SDL_SIMDGetAlignment();
const size_t padding = alignment - (len % alignment);
const size_t padded = (padding != alignment) ? (len + padding) : len;
Uint8 *retval = (Uint8*) mem;
void *oldmem = mem;
size_t memdiff, ptrdiff;
Uint8 *ptr;
if (mem) {
void **realptr = (void **) mem;
realptr--;
mem = *(((void **) mem) - 1);
/* Check the delta between the real pointer and user pointer */
memdiff = ((size_t) oldmem) - ((size_t) mem);
}
ptr = (Uint8 *) SDL_realloc(mem, padded + alignment + sizeof (void *));
if (ptr == mem) {
return retval; /* Pointer didn't change, nothing to do */
}
if (ptr == NULL) {
return NULL; /* Out of memory, bail! */
}
/* Store the actual malloc pointer right before our aligned pointer. */
retval = ptr + sizeof (void *);
retval += alignment - (((size_t) retval) % alignment);
/* Make sure the delta is the same! */
if (mem) {
ptrdiff = ((size_t) retval) - ((size_t) ptr);
if (memdiff != ptrdiff) { /* Delta has changed, copy to new offset! */
oldmem = (void*) (((size_t) ptr) + memdiff);