SDL_cpuinfo.c 37 KB

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