SDL_cpuinfo.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2023 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__) || defined(__GDK__)
  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(__powerpc__)
  47. #include <sys/types.h>
  48. #include <sys/sysctl.h> /* For AltiVec check */
  49. #include <machine/cpu.h>
  50. #elif defined(__FreeBSD__) && defined(__powerpc__)
  51. #include <machine/cpu.h>
  52. #include <sys/auxv.h>
  53. #elif SDL_ALTIVEC_BLITTERS && HAVE_SETJMP
  54. #include <signal.h>
  55. #include <setjmp.h>
  56. #endif
  57. #if defined(__QNXNTO__)
  58. #include <sys/syspage.h>
  59. #endif
  60. #if (defined(__LINUX__) || defined(__ANDROID__)) && defined(__arm__)
  61. #include <unistd.h>
  62. #include <sys/types.h>
  63. #include <sys/stat.h>
  64. #include <fcntl.h>
  65. #include <elf.h>
  66. /*#include <asm/hwcap.h>*/
  67. #ifndef AT_HWCAP
  68. #define AT_HWCAP 16
  69. #endif
  70. #ifndef AT_PLATFORM
  71. #define AT_PLATFORM 15
  72. #endif
  73. #ifndef HWCAP_NEON
  74. #define HWCAP_NEON (1 << 12)
  75. #endif
  76. #endif
  77. #if defined(__ANDROID__) && defined(__arm__) && !defined(HAVE_GETAUXVAL)
  78. #include <cpu-features.h>
  79. #endif
  80. #if defined(HAVE_GETAUXVAL) || defined(HAVE_ELF_AUX_INFO)
  81. #include <sys/auxv.h>
  82. #endif
  83. #ifdef __RISCOS__
  84. #include <kernel.h>
  85. #include <swis.h>
  86. #endif
  87. #ifdef __PS2__
  88. #include <kernel.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. #define CPU_HAS_LSX (1 << 14)
  105. #define CPU_HAS_LASX (1 << 15)
  106. #define CPU_CFG2 0x2
  107. #define CPU_CFG2_LSX (1 << 6)
  108. #define CPU_CFG2_LASX (1 << 7)
  109. #if SDL_ALTIVEC_BLITTERS && HAVE_SETJMP && !__MACOSX__ && !__OpenBSD__ && !__FreeBSD__
  110. /* This is the brute force way of detecting instruction sets...
  111. the idea is borrowed from the libmpeg2 library - thanks!
  112. */
  113. static jmp_buf jmpbuf;
  114. static void illegal_instruction(int sig)
  115. {
  116. longjmp(jmpbuf, 1);
  117. }
  118. #endif /* HAVE_SETJMP */
  119. static int CPU_haveCPUID(void)
  120. {
  121. int has_CPUID = 0;
  122. /* *INDENT-OFF* */ /* clang-format off */
  123. #ifndef SDL_CPUINFO_DISABLED
  124. #if (defined(__GNUC__) || defined(__llvm__)) && defined(__i386__)
  125. __asm__ (
  126. " pushfl # Get original EFLAGS \n"
  127. " popl %%eax \n"
  128. " movl %%eax,%%ecx \n"
  129. " xorl $0x200000,%%eax # Flip ID bit in EFLAGS \n"
  130. " pushl %%eax # Save new EFLAGS value on stack \n"
  131. " popfl # Replace current EFLAGS value \n"
  132. " pushfl # Get new EFLAGS \n"
  133. " popl %%eax # Store new EFLAGS in EAX \n"
  134. " xorl %%ecx,%%eax # Can not toggle ID bit, \n"
  135. " jz 1f # Processor=80486 \n"
  136. " movl $1,%0 # We have CPUID support \n"
  137. "1: \n"
  138. : "=m" (has_CPUID)
  139. :
  140. : "%eax", "%ecx"
  141. );
  142. #elif (defined(__GNUC__) || defined(__llvm__)) && defined(__x86_64__)
  143. /* Technically, if this is being compiled under __x86_64__ then it has
  144. CPUid by definition. But it's nice to be able to prove it. :) */
  145. __asm__ (
  146. " pushfq # Get original EFLAGS \n"
  147. " popq %%rax \n"
  148. " movq %%rax,%%rcx \n"
  149. " xorl $0x200000,%%eax # Flip ID bit in EFLAGS \n"
  150. " pushq %%rax # Save new EFLAGS value on stack \n"
  151. " popfq # Replace current EFLAGS value \n"
  152. " pushfq # Get new EFLAGS \n"
  153. " popq %%rax # Store new EFLAGS in EAX \n"
  154. " xorl %%ecx,%%eax # Can not toggle ID bit, \n"
  155. " jz 1f # Processor=80486 \n"
  156. " movl $1,%0 # We have CPUID support \n"
  157. "1: \n"
  158. : "=m" (has_CPUID)
  159. :
  160. : "%rax", "%rcx"
  161. );
  162. #elif (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
  163. __asm {
  164. pushfd ; Get original EFLAGS
  165. pop eax
  166. mov ecx, eax
  167. xor eax, 200000h ; Flip ID bit in EFLAGS
  168. push eax ; Save new EFLAGS value on stack
  169. popfd ; Replace current EFLAGS value
  170. pushfd ; Get new EFLAGS
  171. pop eax ; Store new EFLAGS in EAX
  172. xor eax, ecx ; Can not toggle ID bit,
  173. jz done ; Processor=80486
  174. mov has_CPUID,1 ; We have CPUID support
  175. done:
  176. }
  177. #elif defined(_MSC_VER) && defined(_M_X64)
  178. has_CPUID = 1;
  179. #elif defined(__sun) && defined(__i386)
  180. __asm (
  181. " pushfl \n"
  182. " popl %eax \n"
  183. " movl %eax,%ecx \n"
  184. " xorl $0x200000,%eax \n"
  185. " pushl %eax \n"
  186. " popfl \n"
  187. " pushfl \n"
  188. " popl %eax \n"
  189. " xorl %ecx,%eax \n"
  190. " jz 1f \n"
  191. " movl $1,-8(%ebp) \n"
  192. "1: \n"
  193. );
  194. #elif defined(__sun) && defined(__amd64)
  195. __asm (
  196. " pushfq \n"
  197. " popq %rax \n"
  198. " movq %rax,%rcx \n"
  199. " xorl $0x200000,%eax \n"
  200. " pushq %rax \n"
  201. " popfq \n"
  202. " pushfq \n"
  203. " popq %rax \n"
  204. " xorl %ecx,%eax \n"
  205. " jz 1f \n"
  206. " movl $1,-8(%rbp) \n"
  207. "1: \n"
  208. );
  209. #endif
  210. #endif
  211. /* *INDENT-ON* */ /* clang-format on */
  212. return has_CPUID;
  213. }
  214. #if (defined(__GNUC__) || defined(__llvm__)) && defined(__i386__)
  215. #define cpuid(func, a, b, c, d) \
  216. __asm__ __volatile__( \
  217. " pushl %%ebx \n" \
  218. " xorl %%ecx,%%ecx \n" \
  219. " cpuid \n" \
  220. " movl %%ebx, %%esi \n" \
  221. " popl %%ebx \n" \
  222. : "=a"(a), "=S"(b), "=c"(c), "=d"(d) \
  223. : "a"(func))
  224. #elif (defined(__GNUC__) || defined(__llvm__)) && defined(__x86_64__)
  225. #define cpuid(func, a, b, c, d) \
  226. __asm__ __volatile__( \
  227. " pushq %%rbx \n" \
  228. " xorq %%rcx,%%rcx \n" \
  229. " cpuid \n" \
  230. " movq %%rbx, %%rsi \n" \
  231. " popq %%rbx \n" \
  232. : "=a"(a), "=S"(b), "=c"(c), "=d"(d) \
  233. : "a"(func))
  234. #elif (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
  235. #define cpuid(func, a, b, c, d) \
  236. __asm { \
  237. __asm mov eax, func \
  238. __asm xor ecx, ecx \
  239. __asm cpuid \
  240. __asm mov a, eax \
  241. __asm mov b, ebx \
  242. __asm mov c, ecx \
  243. __asm mov d, edx \
  244. }
  245. #elif (defined(_MSC_VER) && defined(_M_X64))
  246. /* Use __cpuidex instead of __cpuid because ICL does not clear ecx register */
  247. #define cpuid(func, a, b, c, d) \
  248. { \
  249. int CPUInfo[4]; \
  250. __cpuidex(CPUInfo, func, 0); \
  251. a = CPUInfo[0]; \
  252. b = CPUInfo[1]; \
  253. c = CPUInfo[2]; \
  254. d = CPUInfo[3]; \
  255. }
  256. #else
  257. #define cpuid(func, a, b, c, d) \
  258. do { \
  259. a = b = c = d = 0; \
  260. (void)a; \
  261. (void)b; \
  262. (void)c; \
  263. (void)d; \
  264. } while (0)
  265. #endif
  266. static int CPU_CPUIDFeatures[4];
  267. static int CPU_CPUIDMaxFunction = 0;
  268. static SDL_bool CPU_OSSavesYMM = SDL_FALSE;
  269. static SDL_bool CPU_OSSavesZMM = SDL_FALSE;
  270. static void CPU_calcCPUIDFeatures(void)
  271. {
  272. static SDL_bool checked = SDL_FALSE;
  273. if (!checked) {
  274. checked = SDL_TRUE;
  275. if (CPU_haveCPUID()) {
  276. int a, b, c, d;
  277. cpuid(0, a, b, c, d);
  278. CPU_CPUIDMaxFunction = a;
  279. if (CPU_CPUIDMaxFunction >= 1) {
  280. cpuid(1, a, b, c, d);
  281. CPU_CPUIDFeatures[0] = a;
  282. CPU_CPUIDFeatures[1] = b;
  283. CPU_CPUIDFeatures[2] = c;
  284. CPU_CPUIDFeatures[3] = d;
  285. /* Check to make sure we can call xgetbv */
  286. if (c & 0x08000000) {
  287. /* Call xgetbv to see if YMM (etc) register state is saved */
  288. #if (defined(__GNUC__) || defined(__llvm__)) && (defined(__i386__) || defined(__x86_64__))
  289. __asm__(".byte 0x0f, 0x01, 0xd0"
  290. : "=a"(a)
  291. : "c"(0)
  292. : "%edx");
  293. #elif defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64)) && (_MSC_FULL_VER >= 160040219) /* VS2010 SP1 */
  294. a = (int)_xgetbv(0);
  295. #elif (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
  296. __asm
  297. {
  298. xor ecx, ecx
  299. _asm _emit 0x0f _asm _emit 0x01 _asm _emit 0xd0
  300. mov a, eax
  301. }
  302. #endif
  303. CPU_OSSavesYMM = ((a & 6) == 6) ? SDL_TRUE : SDL_FALSE;
  304. CPU_OSSavesZMM = (CPU_OSSavesYMM && ((a & 0xe0) == 0xe0)) ? SDL_TRUE : SDL_FALSE;
  305. }
  306. }
  307. }
  308. }
  309. }
  310. static int CPU_haveAltiVec(void)
  311. {
  312. volatile int altivec = 0;
  313. #ifndef SDL_CPUINFO_DISABLED
  314. #if (defined(__MACOSX__) && (defined(__ppc__) || defined(__ppc64__))) || (defined(__OpenBSD__) && defined(__powerpc__))
  315. #ifdef __OpenBSD__
  316. int selectors[2] = { CTL_MACHDEP, CPU_ALTIVEC };
  317. #else
  318. int selectors[2] = { CTL_HW, HW_VECTORUNIT };
  319. #endif
  320. int hasVectorUnit = 0;
  321. size_t length = sizeof(hasVectorUnit);
  322. int error = sysctl(selectors, 2, &hasVectorUnit, &length, NULL, 0);
  323. if (0 == error) {
  324. altivec = (hasVectorUnit != 0);
  325. }
  326. #elif defined(__FreeBSD__) && defined(__powerpc__)
  327. unsigned long cpufeatures = 0;
  328. elf_aux_info(AT_HWCAP, &cpufeatures, sizeof(cpufeatures));
  329. altivec = cpufeatures & PPC_FEATURE_HAS_ALTIVEC;
  330. return altivec;
  331. #elif SDL_ALTIVEC_BLITTERS && HAVE_SETJMP
  332. void (*handler)(int sig);
  333. handler = signal(SIGILL, illegal_instruction);
  334. if (setjmp(jmpbuf) == 0) {
  335. asm volatile("mtspr 256, %0\n\t"
  336. "vand %%v0, %%v0, %%v0" ::"r"(-1));
  337. altivec = 1;
  338. }
  339. signal(SIGILL, handler);
  340. #endif
  341. #endif
  342. return altivec;
  343. }
  344. #if (defined(__ARM_ARCH) && (__ARM_ARCH >= 6)) || defined(__aarch64__)
  345. static int CPU_haveARMSIMD(void)
  346. {
  347. return 1;
  348. }
  349. #elif !defined(__arm__)
  350. static int CPU_haveARMSIMD(void)
  351. {
  352. return 0;
  353. }
  354. #elif defined(__LINUX__)
  355. static int CPU_haveARMSIMD(void)
  356. {
  357. int arm_simd = 0;
  358. int fd;
  359. fd = open("/proc/self/auxv", O_RDONLY | O_CLOEXEC);
  360. if (fd >= 0) {
  361. Elf32_auxv_t aux;
  362. while (read(fd, &aux, sizeof(aux)) == sizeof(aux)) {
  363. if (aux.a_type == AT_PLATFORM) {
  364. const char *plat = (const char *)aux.a_un.a_val;
  365. if (plat) {
  366. arm_simd = SDL_strncmp(plat, "v6l", 3) == 0 ||
  367. SDL_strncmp(plat, "v7l", 3) == 0;
  368. }
  369. }
  370. }
  371. close(fd);
  372. }
  373. return arm_simd;
  374. }
  375. #elif defined(__RISCOS__)
  376. static int CPU_haveARMSIMD(void)
  377. {
  378. _kernel_swi_regs regs;
  379. regs.r[0] = 0;
  380. if (_kernel_swi(OS_PlatformFeatures, &regs, &regs) != NULL) {
  381. return 0;
  382. }
  383. if (!(regs.r[0] & (1 << 31))) {
  384. return 0;
  385. }
  386. regs.r[0] = 34;
  387. regs.r[1] = 29;
  388. if (_kernel_swi(OS_PlatformFeatures, &regs, &regs) != NULL) {
  389. return 0;
  390. }
  391. return regs.r[0];
  392. }
  393. #else
  394. static int CPU_haveARMSIMD(void)
  395. {
  396. #warning SDL_HasARMSIMD is not implemented for this ARM platform. Write me.
  397. return 0;
  398. }
  399. #endif
  400. #if defined(__LINUX__) && defined(__arm__) && !defined(HAVE_GETAUXVAL)
  401. static int readProcAuxvForNeon(void)
  402. {
  403. int neon = 0;
  404. int fd;
  405. fd = open("/proc/self/auxv", O_RDONLY | O_CLOEXEC);
  406. if (fd >= 0) {
  407. Elf32_auxv_t aux;
  408. while (read(fd, &aux, sizeof(aux)) == sizeof(aux)) {
  409. if (aux.a_type == AT_HWCAP) {
  410. neon = (aux.a_un.a_val & HWCAP_NEON) == HWCAP_NEON;
  411. break;
  412. }
  413. }
  414. close(fd);
  415. }
  416. return neon;
  417. }
  418. #endif
  419. static int CPU_haveNEON(void)
  420. {
  421. /* The way you detect NEON is a privileged instruction on ARM, so you have
  422. query the OS kernel in a platform-specific way. :/ */
  423. #if defined(SDL_CPUINFO_DISABLED)
  424. return 0; /* disabled */
  425. #elif (defined(__WINDOWS__) || defined(__WINRT__) || defined(__GDK__)) && (defined(_M_ARM) || defined(_M_ARM64))
  426. /* Visual Studio, for ARM, doesn't define __ARM_ARCH. Handle this first. */
  427. /* Seems to have been removed */
  428. #if !defined(PF_ARM_NEON_INSTRUCTIONS_AVAILABLE)
  429. #define PF_ARM_NEON_INSTRUCTIONS_AVAILABLE 19
  430. #endif
  431. /* All WinRT ARM devices are required to support NEON, but just in case. */
  432. return IsProcessorFeaturePresent(PF_ARM_NEON_INSTRUCTIONS_AVAILABLE) != 0;
  433. #elif (defined(__ARM_ARCH) && (__ARM_ARCH >= 8)) || defined(__aarch64__)
  434. return 1; /* ARMv8 always has non-optional NEON support. */
  435. #elif __VITA__
  436. return 1;
  437. #elif __3DS__
  438. return 0;
  439. #elif defined(__APPLE__) && defined(__ARM_ARCH) && (__ARM_ARCH >= 7)
  440. /* (note that sysctlbyname("hw.optional.neon") doesn't work!) */
  441. return 1; /* all Apple ARMv7 chips and later have NEON. */
  442. #elif defined(__APPLE__)
  443. return 0; /* assume anything else from Apple doesn't have NEON. */
  444. #elif !defined(__arm__)
  445. return 0; /* not an ARM CPU at all. */
  446. #elif defined(__OpenBSD__)
  447. return 1; /* OpenBSD only supports ARMv7 CPUs that have NEON. */
  448. #elif defined(HAVE_ELF_AUX_INFO)
  449. unsigned long hasneon = 0;
  450. if (elf_aux_info(AT_HWCAP, (void *)&hasneon, (int)sizeof(hasneon)) != 0) {
  451. return 0;
  452. }
  453. return ((hasneon & HWCAP_NEON) == HWCAP_NEON);
  454. #elif defined(__QNXNTO__)
  455. return SYSPAGE_ENTRY(cpuinfo)->flags & ARM_CPU_FLAG_NEON;
  456. #elif (defined(__LINUX__) || defined(__ANDROID__)) && defined(HAVE_GETAUXVAL)
  457. return (getauxval(AT_HWCAP) & HWCAP_NEON) == HWCAP_NEON;
  458. #elif defined(__LINUX__)
  459. return readProcAuxvForNeon();
  460. #elif defined(__ANDROID__)
  461. /* Use NDK cpufeatures to read either /proc/self/auxv or /proc/cpuinfo */
  462. {
  463. AndroidCpuFamily cpu_family = android_getCpuFamily();
  464. if (cpu_family == ANDROID_CPU_FAMILY_ARM) {
  465. uint64_t cpu_features = android_getCpuFeatures();
  466. if (cpu_features & ANDROID_CPU_ARM_FEATURE_NEON) {
  467. return 1;
  468. }
  469. }
  470. return 0;
  471. }
  472. #elif defined(__RISCOS__)
  473. /* Use the VFPSupport_Features SWI to access the MVFR registers */
  474. {
  475. _kernel_swi_regs regs;
  476. regs.r[0] = 0;
  477. if (_kernel_swi(VFPSupport_Features, &regs, &regs) == NULL) {
  478. if ((regs.r[2] & 0xFFF000) == 0x111000) {
  479. return 1;
  480. }
  481. }
  482. return 0;
  483. }
  484. #else
  485. #warning SDL_HasNEON is not implemented for this ARM platform. Write me.
  486. return 0;
  487. #endif
  488. }
  489. static int CPU_readCPUCFG(void)
  490. {
  491. uint32_t cfg2 = 0;
  492. #if defined __loongarch__
  493. __asm__ volatile(
  494. "cpucfg %0, %1 \n\t"
  495. : "+&r"(cfg2)
  496. : "r"(CPU_CFG2));
  497. #endif
  498. return cfg2;
  499. }
  500. #define CPU_haveLSX() (CPU_readCPUCFG() & CPU_CFG2_LSX)
  501. #define CPU_haveLASX() (CPU_readCPUCFG() & CPU_CFG2_LASX)
  502. #if defined(__e2k__)
  503. inline int
  504. CPU_have3DNow(void)
  505. {
  506. #if defined(__3dNOW__)
  507. return 1;
  508. #else
  509. return 0;
  510. #endif
  511. }
  512. #else
  513. static int CPU_have3DNow(void)
  514. {
  515. if (CPU_CPUIDMaxFunction > 0) { /* that is, do we have CPUID at all? */
  516. int a, b, c, d;
  517. cpuid(0x80000000, a, b, c, d);
  518. if (a >= 0x80000001) {
  519. cpuid(0x80000001, a, b, c, d);
  520. return d & 0x80000000;
  521. }
  522. }
  523. return 0;
  524. }
  525. #endif
  526. #if defined(__e2k__)
  527. #define CPU_haveRDTSC() (0)
  528. #if defined(__MMX__)
  529. #define CPU_haveMMX() (1)
  530. #else
  531. #define CPU_haveMMX() (0)
  532. #endif
  533. #if defined(__SSE__)
  534. #define CPU_haveSSE() (1)
  535. #else
  536. #define CPU_haveSSE() (0)
  537. #endif
  538. #if defined(__SSE2__)
  539. #define CPU_haveSSE2() (1)
  540. #else
  541. #define CPU_haveSSE2() (0)
  542. #endif
  543. #if defined(__SSE3__)
  544. #define CPU_haveSSE3() (1)
  545. #else
  546. #define CPU_haveSSE3() (0)
  547. #endif
  548. #if defined(__SSE4_1__)
  549. #define CPU_haveSSE41() (1)
  550. #else
  551. #define CPU_haveSSE41() (0)
  552. #endif
  553. #if defined(__SSE4_2__)
  554. #define CPU_haveSSE42() (1)
  555. #else
  556. #define CPU_haveSSE42() (0)
  557. #endif
  558. #if defined(__AVX__)
  559. #define CPU_haveAVX() (1)
  560. #else
  561. #define CPU_haveAVX() (0)
  562. #endif
  563. #else
  564. #define CPU_haveRDTSC() (CPU_CPUIDFeatures[3] & 0x00000010)
  565. #define CPU_haveMMX() (CPU_CPUIDFeatures[3] & 0x00800000)
  566. #define CPU_haveSSE() (CPU_CPUIDFeatures[3] & 0x02000000)
  567. #define CPU_haveSSE2() (CPU_CPUIDFeatures[3] & 0x04000000)
  568. #define CPU_haveSSE3() (CPU_CPUIDFeatures[2] & 0x00000001)
  569. #define CPU_haveSSE41() (CPU_CPUIDFeatures[2] & 0x00080000)
  570. #define CPU_haveSSE42() (CPU_CPUIDFeatures[2] & 0x00100000)
  571. #define CPU_haveAVX() (CPU_OSSavesYMM && (CPU_CPUIDFeatures[2] & 0x10000000))
  572. #endif
  573. #if defined(__e2k__)
  574. inline int
  575. CPU_haveAVX2(void)
  576. {
  577. #if defined(__AVX2__)
  578. return 1;
  579. #else
  580. return 0;
  581. #endif
  582. }
  583. #else
  584. static int CPU_haveAVX2(void)
  585. {
  586. if (CPU_OSSavesYMM && (CPU_CPUIDMaxFunction >= 7)) {
  587. int a, b, c, d;
  588. (void)a;
  589. (void)b;
  590. (void)c;
  591. (void)d; /* compiler warnings... */
  592. cpuid(7, a, b, c, d);
  593. return b & 0x00000020;
  594. }
  595. return 0;
  596. }
  597. #endif
  598. #if defined(__e2k__)
  599. inline int
  600. CPU_haveAVX512F(void)
  601. {
  602. return 0;
  603. }
  604. #else
  605. static int CPU_haveAVX512F(void)
  606. {
  607. if (CPU_OSSavesZMM && (CPU_CPUIDMaxFunction >= 7)) {
  608. int a, b, c, d;
  609. (void)a;
  610. (void)b;
  611. (void)c;
  612. (void)d; /* compiler warnings... */
  613. cpuid(7, a, b, c, d);
  614. return b & 0x00010000;
  615. }
  616. return 0;
  617. }
  618. #endif
  619. static int SDL_CPUCount = 0;
  620. int SDL_GetCPUCount(void)
  621. {
  622. if (!SDL_CPUCount) {
  623. #ifndef SDL_CPUINFO_DISABLED
  624. #if defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN)
  625. if (SDL_CPUCount <= 0) {
  626. SDL_CPUCount = (int)sysconf(_SC_NPROCESSORS_ONLN);
  627. }
  628. #endif
  629. #ifdef HAVE_SYSCTLBYNAME
  630. if (SDL_CPUCount <= 0) {
  631. size_t size = sizeof(SDL_CPUCount);
  632. sysctlbyname("hw.ncpu", &SDL_CPUCount, &size, NULL, 0);
  633. }
  634. #endif
  635. #if defined(__WIN32__) || defined(__GDK__)
  636. if (SDL_CPUCount <= 0) {
  637. SYSTEM_INFO info;
  638. GetSystemInfo(&info);
  639. SDL_CPUCount = info.dwNumberOfProcessors;
  640. }
  641. #endif
  642. #ifdef __OS2__
  643. if (SDL_CPUCount <= 0) {
  644. DosQuerySysInfo(QSV_NUMPROCESSORS, QSV_NUMPROCESSORS,
  645. &SDL_CPUCount, sizeof(SDL_CPUCount) );
  646. }
  647. #endif
  648. #endif
  649. /* There has to be at least 1, right? :) */
  650. if (SDL_CPUCount <= 0) {
  651. SDL_CPUCount = 1;
  652. }
  653. }
  654. return SDL_CPUCount;
  655. }
  656. #if defined(__e2k__)
  657. inline const char *
  658. SDL_GetCPUType(void)
  659. {
  660. static char SDL_CPUType[13];
  661. SDL_strlcpy(SDL_CPUType, "E2K MACHINE", sizeof(SDL_CPUType));
  662. return SDL_CPUType;
  663. }
  664. #else
  665. /* Oh, such a sweet sweet trick, just not very useful. :) */
  666. static const char *SDL_GetCPUType(void)
  667. {
  668. static char SDL_CPUType[13];
  669. if (!SDL_CPUType[0]) {
  670. int i = 0;
  671. CPU_calcCPUIDFeatures();
  672. if (CPU_CPUIDMaxFunction > 0) { /* do we have CPUID at all? */
  673. int a, b, c, d;
  674. cpuid(0x00000000, a, b, c, d);
  675. (void)a;
  676. SDL_CPUType[i++] = (char)(b & 0xff);
  677. b >>= 8;
  678. SDL_CPUType[i++] = (char)(b & 0xff);
  679. b >>= 8;
  680. SDL_CPUType[i++] = (char)(b & 0xff);
  681. b >>= 8;
  682. SDL_CPUType[i++] = (char)(b & 0xff);
  683. SDL_CPUType[i++] = (char)(d & 0xff);
  684. d >>= 8;
  685. SDL_CPUType[i++] = (char)(d & 0xff);
  686. d >>= 8;
  687. SDL_CPUType[i++] = (char)(d & 0xff);
  688. d >>= 8;
  689. SDL_CPUType[i++] = (char)(d & 0xff);
  690. SDL_CPUType[i++] = (char)(c & 0xff);
  691. c >>= 8;
  692. SDL_CPUType[i++] = (char)(c & 0xff);
  693. c >>= 8;
  694. SDL_CPUType[i++] = (char)(c & 0xff);
  695. c >>= 8;
  696. SDL_CPUType[i++] = (char)(c & 0xff);
  697. }
  698. if (!SDL_CPUType[0]) {
  699. SDL_strlcpy(SDL_CPUType, "Unknown", sizeof(SDL_CPUType));
  700. }
  701. }
  702. return SDL_CPUType;
  703. }
  704. #endif
  705. #ifdef TEST_MAIN /* !!! FIXME: only used for test at the moment. */
  706. #if defined(__e2k__)
  707. inline const char *
  708. SDL_GetCPUName(void)
  709. {
  710. static char SDL_CPUName[48];
  711. SDL_strlcpy(SDL_CPUName, __builtin_cpu_name(), sizeof(SDL_CPUName));
  712. return SDL_CPUName;
  713. }
  714. #else
  715. static const char *SDL_GetCPUName(void)
  716. {
  717. static char SDL_CPUName[48];
  718. if (!SDL_CPUName[0]) {
  719. int i = 0;
  720. int a, b, c, d;
  721. CPU_calcCPUIDFeatures();
  722. if (CPU_CPUIDMaxFunction > 0) { /* do we have CPUID at all? */
  723. cpuid(0x80000000, a, b, c, d);
  724. if (a >= 0x80000004) {
  725. cpuid(0x80000002, a, b, c, d);
  726. SDL_CPUName[i++] = (char)(a & 0xff);
  727. a >>= 8;
  728. SDL_CPUName[i++] = (char)(a & 0xff);
  729. a >>= 8;
  730. SDL_CPUName[i++] = (char)(a & 0xff);
  731. a >>= 8;
  732. SDL_CPUName[i++] = (char)(a & 0xff);
  733. a >>= 8;
  734. SDL_CPUName[i++] = (char)(b & 0xff);
  735. b >>= 8;
  736. SDL_CPUName[i++] = (char)(b & 0xff);
  737. b >>= 8;
  738. SDL_CPUName[i++] = (char)(b & 0xff);
  739. b >>= 8;
  740. SDL_CPUName[i++] = (char)(b & 0xff);
  741. b >>= 8;
  742. SDL_CPUName[i++] = (char)(c & 0xff);
  743. c >>= 8;
  744. SDL_CPUName[i++] = (char)(c & 0xff);
  745. c >>= 8;
  746. SDL_CPUName[i++] = (char)(c & 0xff);
  747. c >>= 8;
  748. SDL_CPUName[i++] = (char)(c & 0xff);
  749. c >>= 8;
  750. SDL_CPUName[i++] = (char)(d & 0xff);
  751. d >>= 8;
  752. SDL_CPUName[i++] = (char)(d & 0xff);
  753. d >>= 8;
  754. SDL_CPUName[i++] = (char)(d & 0xff);
  755. d >>= 8;
  756. SDL_CPUName[i++] = (char)(d & 0xff);
  757. d >>= 8;
  758. cpuid(0x80000003, a, b, c, d);
  759. SDL_CPUName[i++] = (char)(a & 0xff);
  760. a >>= 8;
  761. SDL_CPUName[i++] = (char)(a & 0xff);
  762. a >>= 8;
  763. SDL_CPUName[i++] = (char)(a & 0xff);
  764. a >>= 8;
  765. SDL_CPUName[i++] = (char)(a & 0xff);
  766. a >>= 8;
  767. SDL_CPUName[i++] = (char)(b & 0xff);
  768. b >>= 8;
  769. SDL_CPUName[i++] = (char)(b & 0xff);
  770. b >>= 8;
  771. SDL_CPUName[i++] = (char)(b & 0xff);
  772. b >>= 8;
  773. SDL_CPUName[i++] = (char)(b & 0xff);
  774. b >>= 8;
  775. SDL_CPUName[i++] = (char)(c & 0xff);
  776. c >>= 8;
  777. SDL_CPUName[i++] = (char)(c & 0xff);
  778. c >>= 8;
  779. SDL_CPUName[i++] = (char)(c & 0xff);
  780. c >>= 8;
  781. SDL_CPUName[i++] = (char)(c & 0xff);
  782. c >>= 8;
  783. SDL_CPUName[i++] = (char)(d & 0xff);
  784. d >>= 8;
  785. SDL_CPUName[i++] = (char)(d & 0xff);
  786. d >>= 8;
  787. SDL_CPUName[i++] = (char)(d & 0xff);
  788. d >>= 8;
  789. SDL_CPUName[i++] = (char)(d & 0xff);
  790. d >>= 8;
  791. cpuid(0x80000004, a, b, c, d);
  792. SDL_CPUName[i++] = (char)(a & 0xff);
  793. a >>= 8;
  794. SDL_CPUName[i++] = (char)(a & 0xff);
  795. a >>= 8;
  796. SDL_CPUName[i++] = (char)(a & 0xff);
  797. a >>= 8;
  798. SDL_CPUName[i++] = (char)(a & 0xff);
  799. a >>= 8;
  800. SDL_CPUName[i++] = (char)(b & 0xff);
  801. b >>= 8;
  802. SDL_CPUName[i++] = (char)(b & 0xff);
  803. b >>= 8;
  804. SDL_CPUName[i++] = (char)(b & 0xff);
  805. b >>= 8;
  806. SDL_CPUName[i++] = (char)(b & 0xff);
  807. b >>= 8;
  808. SDL_CPUName[i++] = (char)(c & 0xff);
  809. c >>= 8;
  810. SDL_CPUName[i++] = (char)(c & 0xff);
  811. c >>= 8;
  812. SDL_CPUName[i++] = (char)(c & 0xff);
  813. c >>= 8;
  814. SDL_CPUName[i++] = (char)(c & 0xff);
  815. c >>= 8;
  816. SDL_CPUName[i++] = (char)(d & 0xff);
  817. d >>= 8;
  818. SDL_CPUName[i++] = (char)(d & 0xff);
  819. d >>= 8;
  820. SDL_CPUName[i++] = (char)(d & 0xff);
  821. d >>= 8;
  822. SDL_CPUName[i++] = (char)(d & 0xff);
  823. d >>= 8;
  824. }
  825. }
  826. if (!SDL_CPUName[0]) {
  827. SDL_strlcpy(SDL_CPUName, "Unknown", sizeof(SDL_CPUName));
  828. }
  829. }
  830. return SDL_CPUName;
  831. }
  832. #endif
  833. #endif
  834. int SDL_GetCPUCacheLineSize(void)
  835. {
  836. const char *cpuType = SDL_GetCPUType();
  837. int a, b, c, d;
  838. (void)a;
  839. (void)b;
  840. (void)c;
  841. (void)d;
  842. if (SDL_strcmp(cpuType, "GenuineIntel") == 0 || SDL_strcmp(cpuType, "CentaurHauls") == 0 || SDL_strcmp(cpuType, " Shanghai ") == 0) {
  843. cpuid(0x00000001, a, b, c, d);
  844. return ((b >> 8) & 0xff) * 8;
  845. } else if (SDL_strcmp(cpuType, "AuthenticAMD") == 0 || SDL_strcmp(cpuType, "HygonGenuine") == 0) {
  846. cpuid(0x80000005, a, b, c, d);
  847. return c & 0xff;
  848. } else {
  849. /* Just make a guess here... */
  850. return SDL_CACHELINE_SIZE;
  851. }
  852. }
  853. static Uint32 SDL_CPUFeatures = 0xFFFFFFFF;
  854. static Uint32 SDL_SIMDAlignment = 0xFFFFFFFF;
  855. static Uint32 SDL_GetCPUFeatures(void)
  856. {
  857. if (SDL_CPUFeatures == 0xFFFFFFFF) {
  858. CPU_calcCPUIDFeatures();
  859. SDL_CPUFeatures = 0;
  860. SDL_SIMDAlignment = sizeof(void *); /* a good safe base value */
  861. if (CPU_haveRDTSC()) {
  862. SDL_CPUFeatures |= CPU_HAS_RDTSC;
  863. }
  864. if (CPU_haveAltiVec()) {
  865. SDL_CPUFeatures |= CPU_HAS_ALTIVEC;
  866. SDL_SIMDAlignment = SDL_max(SDL_SIMDAlignment, 16);
  867. }
  868. if (CPU_haveMMX()) {
  869. SDL_CPUFeatures |= CPU_HAS_MMX;
  870. SDL_SIMDAlignment = SDL_max(SDL_SIMDAlignment, 8);
  871. }
  872. if (CPU_have3DNow()) {
  873. SDL_CPUFeatures |= CPU_HAS_3DNOW;
  874. SDL_SIMDAlignment = SDL_max(SDL_SIMDAlignment, 8);
  875. }
  876. if (CPU_haveSSE()) {
  877. SDL_CPUFeatures |= CPU_HAS_SSE;
  878. SDL_SIMDAlignment = SDL_max(SDL_SIMDAlignment, 16);
  879. }
  880. if (CPU_haveSSE2()) {
  881. SDL_CPUFeatures |= CPU_HAS_SSE2;
  882. SDL_SIMDAlignment = SDL_max(SDL_SIMDAlignment, 16);
  883. }
  884. if (CPU_haveSSE3()) {
  885. SDL_CPUFeatures |= CPU_HAS_SSE3;
  886. SDL_SIMDAlignment = SDL_max(SDL_SIMDAlignment, 16);
  887. }
  888. if (CPU_haveSSE41()) {
  889. SDL_CPUFeatures |= CPU_HAS_SSE41;
  890. SDL_SIMDAlignment = SDL_max(SDL_SIMDAlignment, 16);
  891. }
  892. if (CPU_haveSSE42()) {
  893. SDL_CPUFeatures |= CPU_HAS_SSE42;
  894. SDL_SIMDAlignment = SDL_max(SDL_SIMDAlignment, 16);
  895. }
  896. if (CPU_haveAVX()) {
  897. SDL_CPUFeatures |= CPU_HAS_AVX;
  898. SDL_SIMDAlignment = SDL_max(SDL_SIMDAlignment, 32);
  899. }
  900. if (CPU_haveAVX2()) {
  901. SDL_CPUFeatures |= CPU_HAS_AVX2;
  902. SDL_SIMDAlignment = SDL_max(SDL_SIMDAlignment, 32);
  903. }
  904. if (CPU_haveAVX512F()) {
  905. SDL_CPUFeatures |= CPU_HAS_AVX512F;
  906. SDL_SIMDAlignment = SDL_max(SDL_SIMDAlignment, 64);
  907. }
  908. if (CPU_haveARMSIMD()) {
  909. SDL_CPUFeatures |= CPU_HAS_ARM_SIMD;
  910. SDL_SIMDAlignment = SDL_max(SDL_SIMDAlignment, 16);
  911. }
  912. if (CPU_haveNEON()) {
  913. SDL_CPUFeatures |= CPU_HAS_NEON;
  914. SDL_SIMDAlignment = SDL_max(SDL_SIMDAlignment, 16);
  915. }
  916. if (CPU_haveLSX()) {
  917. SDL_CPUFeatures |= CPU_HAS_LSX;
  918. SDL_SIMDAlignment = SDL_max(SDL_SIMDAlignment, 16);
  919. }
  920. if (CPU_haveLASX()) {
  921. SDL_CPUFeatures |= CPU_HAS_LASX;
  922. SDL_SIMDAlignment = SDL_max(SDL_SIMDAlignment, 32);
  923. }
  924. }
  925. return SDL_CPUFeatures;
  926. }
  927. #define CPU_FEATURE_AVAILABLE(f) ((SDL_GetCPUFeatures() & f) ? SDL_TRUE : SDL_FALSE)
  928. SDL_bool SDL_HasRDTSC(void)
  929. {
  930. return CPU_FEATURE_AVAILABLE(CPU_HAS_RDTSC);
  931. }
  932. SDL_bool
  933. SDL_HasAltiVec(void)
  934. {
  935. return CPU_FEATURE_AVAILABLE(CPU_HAS_ALTIVEC);
  936. }
  937. SDL_bool
  938. SDL_HasMMX(void)
  939. {
  940. return CPU_FEATURE_AVAILABLE(CPU_HAS_MMX);
  941. }
  942. SDL_bool
  943. SDL_Has3DNow(void)
  944. {
  945. return CPU_FEATURE_AVAILABLE(CPU_HAS_3DNOW);
  946. }
  947. SDL_bool
  948. SDL_HasSSE(void)
  949. {
  950. return CPU_FEATURE_AVAILABLE(CPU_HAS_SSE);
  951. }
  952. SDL_bool
  953. SDL_HasSSE2(void)
  954. {
  955. return CPU_FEATURE_AVAILABLE(CPU_HAS_SSE2);
  956. }
  957. SDL_bool
  958. SDL_HasSSE3(void)
  959. {
  960. return CPU_FEATURE_AVAILABLE(CPU_HAS_SSE3);
  961. }
  962. SDL_bool
  963. SDL_HasSSE41(void)
  964. {
  965. return CPU_FEATURE_AVAILABLE(CPU_HAS_SSE41);
  966. }
  967. SDL_bool
  968. SDL_HasSSE42(void)
  969. {
  970. return CPU_FEATURE_AVAILABLE(CPU_HAS_SSE42);
  971. }
  972. SDL_bool
  973. SDL_HasAVX(void)
  974. {
  975. return CPU_FEATURE_AVAILABLE(CPU_HAS_AVX);
  976. }
  977. SDL_bool
  978. SDL_HasAVX2(void)
  979. {
  980. return CPU_FEATURE_AVAILABLE(CPU_HAS_AVX2);
  981. }
  982. SDL_bool
  983. SDL_HasAVX512F(void)
  984. {
  985. return CPU_FEATURE_AVAILABLE(CPU_HAS_AVX512F);
  986. }
  987. SDL_bool
  988. SDL_HasARMSIMD(void)
  989. {
  990. return CPU_FEATURE_AVAILABLE(CPU_HAS_ARM_SIMD);
  991. }
  992. SDL_bool
  993. SDL_HasNEON(void)
  994. {
  995. return CPU_FEATURE_AVAILABLE(CPU_HAS_NEON);
  996. }
  997. SDL_bool
  998. SDL_HasLSX(void)
  999. {
  1000. return CPU_FEATURE_AVAILABLE(CPU_HAS_LSX);
  1001. }
  1002. SDL_bool
  1003. SDL_HasLASX(void)
  1004. {
  1005. return CPU_FEATURE_AVAILABLE(CPU_HAS_LASX);
  1006. }
  1007. static int SDL_SystemRAM = 0;
  1008. int SDL_GetSystemRAM(void)
  1009. {
  1010. if (!SDL_SystemRAM) {
  1011. #ifndef SDL_CPUINFO_DISABLED
  1012. #if defined(HAVE_SYSCONF) && defined(_SC_PHYS_PAGES) && defined(_SC_PAGESIZE)
  1013. if (SDL_SystemRAM <= 0) {
  1014. SDL_SystemRAM = (int)((Sint64)sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE) / (1024 * 1024));
  1015. }
  1016. #endif
  1017. #ifdef HAVE_SYSCTLBYNAME
  1018. if (SDL_SystemRAM <= 0) {
  1019. #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__DragonFly__)
  1020. #ifdef HW_REALMEM
  1021. int mib[2] = { CTL_HW, HW_REALMEM };
  1022. #else
  1023. /* might only report up to 2 GiB */
  1024. int mib[2] = { CTL_HW, HW_PHYSMEM };
  1025. #endif /* HW_REALMEM */
  1026. #else
  1027. int mib[2] = { CTL_HW, HW_MEMSIZE };
  1028. #endif /* __FreeBSD__ || __FreeBSD_kernel__ */
  1029. Uint64 memsize = 0;
  1030. size_t len = sizeof(memsize);
  1031. if (sysctl(mib, 2, &memsize, &len, NULL, 0) == 0) {
  1032. SDL_SystemRAM = (int)(memsize / (1024 * 1024));
  1033. }
  1034. }
  1035. #endif
  1036. #if defined(__WIN32__) || defined(__GDK__)
  1037. if (SDL_SystemRAM <= 0) {
  1038. MEMORYSTATUSEX stat;
  1039. stat.dwLength = sizeof(stat);
  1040. if (GlobalMemoryStatusEx(&stat)) {
  1041. SDL_SystemRAM = (int)(stat.ullTotalPhys / (1024 * 1024));
  1042. }
  1043. }
  1044. #endif
  1045. #ifdef __OS2__
  1046. if (SDL_SystemRAM <= 0) {
  1047. Uint32 sysram = 0;
  1048. DosQuerySysInfo(QSV_TOTPHYSMEM, QSV_TOTPHYSMEM, &sysram, 4);
  1049. SDL_SystemRAM = (int) (sysram / 0x100000U);
  1050. }
  1051. #endif
  1052. #ifdef __RISCOS__
  1053. if (SDL_SystemRAM <= 0) {
  1054. _kernel_swi_regs regs;
  1055. regs.r[0] = 0x108;
  1056. if (_kernel_swi(OS_Memory, &regs, &regs) == NULL) {
  1057. SDL_SystemRAM = (int)(regs.r[1] * regs.r[2] / (1024 * 1024));
  1058. }
  1059. }
  1060. #endif
  1061. #ifdef __VITA__
  1062. if (SDL_SystemRAM <= 0) {
  1063. /* Vita has 512MiB on SoC, that's split into 256MiB(+109MiB in extended memory mode) for app
  1064. +26MiB of physically continuous memory, +112MiB of CDRAM(VRAM) + system reserved memory. */
  1065. SDL_SystemRAM = 536870912;
  1066. }
  1067. #endif
  1068. #ifdef __PS2__
  1069. if (SDL_SystemRAM <= 0) {
  1070. /* PlayStation 2 has 32MiB however there are some special models with 64 and 128 */
  1071. SDL_SystemRAM = GetMemorySize();
  1072. }
  1073. #endif
  1074. #endif
  1075. }
  1076. return SDL_SystemRAM;
  1077. }
  1078. size_t
  1079. SDL_SIMDGetAlignment(void)
  1080. {
  1081. if (SDL_SIMDAlignment == 0xFFFFFFFF) {
  1082. SDL_GetCPUFeatures(); /* make sure this has been calculated */
  1083. }
  1084. SDL_assert(SDL_SIMDAlignment != 0);
  1085. return SDL_SIMDAlignment;
  1086. }
  1087. void *
  1088. SDL_SIMDAlloc(const size_t len)
  1089. {
  1090. const size_t alignment = SDL_SIMDGetAlignment();
  1091. const size_t padding = (alignment - (len % alignment)) % alignment;
  1092. Uint8 *retval = NULL;
  1093. Uint8 *ptr;
  1094. size_t to_allocate;
  1095. /* alignment + padding + sizeof(void *) is bounded (a few hundred
  1096. * bytes max), so no need to check for overflow within that argument */
  1097. if (SDL_size_add_overflow(len, alignment + padding + sizeof(void *), &to_allocate)) {
  1098. return NULL;
  1099. }
  1100. ptr = (Uint8 *)SDL_malloc(to_allocate);
  1101. if (ptr) {
  1102. /* store the actual allocated pointer right before our aligned pointer. */
  1103. retval = ptr + sizeof(void *);
  1104. retval += alignment - (((size_t)retval) % alignment);
  1105. *(((void **)retval) - 1) = ptr;
  1106. }
  1107. return retval;
  1108. }
  1109. void *
  1110. SDL_SIMDRealloc(void *mem, const size_t len)
  1111. {
  1112. const size_t alignment = SDL_SIMDGetAlignment();
  1113. const size_t padding = (alignment - (len % alignment)) % alignment;
  1114. Uint8 *retval = (Uint8 *)mem;
  1115. void *oldmem = mem;
  1116. size_t memdiff = 0, ptrdiff;
  1117. Uint8 *ptr;
  1118. size_t to_allocate;
  1119. /* alignment + padding + sizeof(void *) is bounded (a few hundred
  1120. * bytes max), so no need to check for overflow within that argument */
  1121. if (SDL_size_add_overflow(len, alignment + padding + sizeof(void *), &to_allocate)) {
  1122. return NULL;
  1123. }
  1124. if (mem) {
  1125. mem = *(((void **)mem) - 1);
  1126. /* Check the delta between the real pointer and user pointer */
  1127. memdiff = ((size_t)oldmem) - ((size_t)mem);
  1128. }
  1129. ptr = (Uint8 *)SDL_realloc(mem, to_allocate);
  1130. if (ptr == NULL) {
  1131. return NULL; /* Out of memory, bail! */
  1132. }
  1133. /* Store the actual allocated pointer right before our aligned pointer. */
  1134. retval = ptr + sizeof(void *);
  1135. retval += alignment - (((size_t)retval) % alignment);
  1136. /* Make sure the delta is the same! */
  1137. if (mem) {
  1138. ptrdiff = ((size_t)retval) - ((size_t)ptr);
  1139. if (memdiff != ptrdiff) { /* Delta has changed, copy to new offset! */
  1140. oldmem = (void *)(((uintptr_t)ptr) + memdiff);
  1141. /* Even though the data past the old `len` is undefined, this is the
  1142. * only length value we have, and it guarantees that we copy all the
  1143. * previous memory anyhow.
  1144. */
  1145. SDL_memmove(retval, oldmem, len);
  1146. }
  1147. }
  1148. /* Actually store the allocated pointer, finally. */
  1149. *(((void **)retval) - 1) = ptr;
  1150. return retval;
  1151. }
  1152. void SDL_SIMDFree(void *ptr)
  1153. {
  1154. if (ptr) {
  1155. SDL_free(*(((void **)ptr) - 1));
  1156. }
  1157. }
  1158. #ifdef TEST_MAIN
  1159. #include <stdio.h>
  1160. int main()
  1161. {
  1162. printf("CPU count: %d\n", SDL_GetCPUCount());
  1163. printf("CPU type: %s\n", SDL_GetCPUType());
  1164. printf("CPU name: %s\n", SDL_GetCPUName());
  1165. printf("CacheLine size: %d\n", SDL_GetCPUCacheLineSize());
  1166. printf("RDTSC: %d\n", SDL_HasRDTSC());
  1167. printf("Altivec: %d\n", SDL_HasAltiVec());
  1168. printf("MMX: %d\n", SDL_HasMMX());
  1169. printf("3DNow: %d\n", SDL_Has3DNow());
  1170. printf("SSE: %d\n", SDL_HasSSE());
  1171. printf("SSE2: %d\n", SDL_HasSSE2());
  1172. printf("SSE3: %d\n", SDL_HasSSE3());
  1173. printf("SSE4.1: %d\n", SDL_HasSSE41());
  1174. printf("SSE4.2: %d\n", SDL_HasSSE42());
  1175. printf("AVX: %d\n", SDL_HasAVX());
  1176. printf("AVX2: %d\n", SDL_HasAVX2());
  1177. printf("AVX-512F: %d\n", SDL_HasAVX512F());
  1178. printf("ARM SIMD: %d\n", SDL_HasARMSIMD());
  1179. printf("NEON: %d\n", SDL_HasNEON());
  1180. printf("LSX: %d\n", SDL_HasLSX());
  1181. printf("LASX: %d\n", SDL_HasLASX());
  1182. printf("RAM: %d MB\n", SDL_GetSystemRAM());
  1183. return 0;
  1184. }
  1185. #endif /* TEST_MAIN */
  1186. /* vi: set ts=4 sw=4 expandtab: */