SDL_cpuinfo.c 34 KB

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