SDL_cpuinfo.c 29 KB

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