SDL_cpuinfo.c 27 KB

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