SDL_cpuinfo.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2014 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__)
  24. #include "../core/windows/SDL_windows.h"
  25. #endif
  26. /* CPU feature detection for SDL */
  27. #include "SDL_cpuinfo.h"
  28. #ifdef HAVE_SYSCONF
  29. #include <unistd.h>
  30. #endif
  31. #ifdef HAVE_SYSCTLBYNAME
  32. #include <sys/types.h>
  33. #include <sys/sysctl.h>
  34. #endif
  35. #if defined(__MACOSX__) && (defined(__ppc__) || defined(__ppc64__))
  36. #include <sys/sysctl.h> /* For AltiVec check */
  37. #elif defined(__OpenBSD__) && defined(__powerpc__)
  38. #include <sys/param.h>
  39. #include <sys/sysctl.h> /* For AltiVec check */
  40. #include <machine/cpu.h>
  41. #elif SDL_ALTIVEC_BLITTERS && HAVE_SETJMP
  42. #include <signal.h>
  43. #include <setjmp.h>
  44. #endif
  45. #define CPU_HAS_RDTSC 0x00000001
  46. #define CPU_HAS_ALTIVEC 0x00000002
  47. #define CPU_HAS_MMX 0x00000004
  48. #define CPU_HAS_3DNOW 0x00000008
  49. #define CPU_HAS_SSE 0x00000010
  50. #define CPU_HAS_SSE2 0x00000020
  51. #define CPU_HAS_SSE3 0x00000040
  52. #define CPU_HAS_SSE41 0x00000100
  53. #define CPU_HAS_SSE42 0x00000200
  54. #define CPU_HAS_AVX 0x00000400
  55. #if SDL_ALTIVEC_BLITTERS && HAVE_SETJMP && !__MACOSX__ && !__OpenBSD__
  56. /* This is the brute force way of detecting instruction sets...
  57. the idea is borrowed from the libmpeg2 library - thanks!
  58. */
  59. static jmp_buf jmpbuf;
  60. static void
  61. illegal_instruction(int sig)
  62. {
  63. longjmp(jmpbuf, 1);
  64. }
  65. #endif /* HAVE_SETJMP */
  66. static SDL_INLINE int
  67. CPU_haveCPUID(void)
  68. {
  69. int has_CPUID = 0;
  70. /* *INDENT-OFF* */
  71. #if defined(__GNUC__) && defined(i386)
  72. __asm__ (
  73. " pushfl # Get original EFLAGS \n"
  74. " popl %%eax \n"
  75. " movl %%eax,%%ecx \n"
  76. " xorl $0x200000,%%eax # Flip ID bit in EFLAGS \n"
  77. " pushl %%eax # Save new EFLAGS value on stack \n"
  78. " popfl # Replace current EFLAGS value \n"
  79. " pushfl # Get new EFLAGS \n"
  80. " popl %%eax # Store new EFLAGS in EAX \n"
  81. " xorl %%ecx,%%eax # Can not toggle ID bit, \n"
  82. " jz 1f # Processor=80486 \n"
  83. " movl $1,%0 # We have CPUID support \n"
  84. "1: \n"
  85. : "=m" (has_CPUID)
  86. :
  87. : "%eax", "%ecx"
  88. );
  89. #elif defined(__GNUC__) && defined(__x86_64__)
  90. /* Technically, if this is being compiled under __x86_64__ then it has
  91. CPUid by definition. But it's nice to be able to prove it. :) */
  92. __asm__ (
  93. " pushfq # Get original EFLAGS \n"
  94. " popq %%rax \n"
  95. " movq %%rax,%%rcx \n"
  96. " xorl $0x200000,%%eax # Flip ID bit in EFLAGS \n"
  97. " pushq %%rax # Save new EFLAGS value on stack \n"
  98. " popfq # Replace current EFLAGS value \n"
  99. " pushfq # Get new EFLAGS \n"
  100. " popq %%rax # Store new EFLAGS in EAX \n"
  101. " xorl %%ecx,%%eax # Can not toggle ID bit, \n"
  102. " jz 1f # Processor=80486 \n"
  103. " movl $1,%0 # We have CPUID support \n"
  104. "1: \n"
  105. : "=m" (has_CPUID)
  106. :
  107. : "%rax", "%rcx"
  108. );
  109. #elif (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
  110. __asm {
  111. pushfd ; Get original EFLAGS
  112. pop eax
  113. mov ecx, eax
  114. xor eax, 200000h ; Flip ID bit in EFLAGS
  115. push eax ; Save new EFLAGS value on stack
  116. popfd ; Replace current EFLAGS value
  117. pushfd ; Get new EFLAGS
  118. pop eax ; Store new EFLAGS in EAX
  119. xor eax, ecx ; Can not toggle ID bit,
  120. jz done ; Processor=80486
  121. mov has_CPUID,1 ; We have CPUID support
  122. done:
  123. }
  124. #elif defined(_MSC_VER) && defined(_M_X64)
  125. has_CPUID = 1;
  126. #elif defined(__sun) && defined(__i386)
  127. __asm (
  128. " pushfl \n"
  129. " popl %eax \n"
  130. " movl %eax,%ecx \n"
  131. " xorl $0x200000,%eax \n"
  132. " pushl %eax \n"
  133. " popfl \n"
  134. " pushfl \n"
  135. " popl %eax \n"
  136. " xorl %ecx,%eax \n"
  137. " jz 1f \n"
  138. " movl $1,-8(%ebp) \n"
  139. "1: \n"
  140. );
  141. #elif defined(__sun) && defined(__amd64)
  142. __asm (
  143. " pushfq \n"
  144. " popq %rax \n"
  145. " movq %rax,%rcx \n"
  146. " xorl $0x200000,%eax \n"
  147. " pushq %rax \n"
  148. " popfq \n"
  149. " pushfq \n"
  150. " popq %rax \n"
  151. " xorl %ecx,%eax \n"
  152. " jz 1f \n"
  153. " movl $1,-8(%rbp) \n"
  154. "1: \n"
  155. );
  156. #endif
  157. /* *INDENT-ON* */
  158. return has_CPUID;
  159. }
  160. #if defined(__GNUC__) && defined(i386)
  161. #define cpuid(func, a, b, c, d) \
  162. __asm__ __volatile__ ( \
  163. " pushl %%ebx \n" \
  164. " cpuid \n" \
  165. " movl %%ebx, %%esi \n" \
  166. " popl %%ebx \n" : \
  167. "=a" (a), "=S" (b), "=c" (c), "=d" (d) : "a" (func))
  168. #elif defined(__GNUC__) && defined(__x86_64__)
  169. #define cpuid(func, a, b, c, d) \
  170. __asm__ __volatile__ ( \
  171. " pushq %%rbx \n" \
  172. " cpuid \n" \
  173. " movq %%rbx, %%rsi \n" \
  174. " popq %%rbx \n" : \
  175. "=a" (a), "=S" (b), "=c" (c), "=d" (d) : "a" (func))
  176. #elif (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
  177. #define cpuid(func, a, b, c, d) \
  178. __asm { \
  179. __asm mov eax, func \
  180. __asm cpuid \
  181. __asm mov a, eax \
  182. __asm mov b, ebx \
  183. __asm mov c, ecx \
  184. __asm mov d, edx \
  185. }
  186. #elif defined(_MSC_VER) && defined(_M_X64)
  187. #define cpuid(func, a, b, c, d) \
  188. { \
  189. int CPUInfo[4]; \
  190. __cpuid(CPUInfo, func); \
  191. a = CPUInfo[0]; \
  192. b = CPUInfo[1]; \
  193. c = CPUInfo[2]; \
  194. d = CPUInfo[3]; \
  195. }
  196. #else
  197. #define cpuid(func, a, b, c, d) \
  198. a = b = c = d = 0
  199. #endif
  200. static SDL_INLINE int
  201. CPU_getCPUIDFeatures(void)
  202. {
  203. int features = 0;
  204. int a, b, c, d;
  205. cpuid(0, a, b, c, d);
  206. if (a >= 1) {
  207. cpuid(1, a, b, c, d);
  208. features = d;
  209. }
  210. return features;
  211. }
  212. static SDL_INLINE int
  213. CPU_haveRDTSC(void)
  214. {
  215. if (CPU_haveCPUID()) {
  216. return (CPU_getCPUIDFeatures() & 0x00000010);
  217. }
  218. return 0;
  219. }
  220. static SDL_INLINE int
  221. CPU_haveAltiVec(void)
  222. {
  223. volatile int altivec = 0;
  224. #if (defined(__MACOSX__) && (defined(__ppc__) || defined(__ppc64__))) || (defined(__OpenBSD__) && defined(__powerpc__))
  225. #ifdef __OpenBSD__
  226. int selectors[2] = { CTL_MACHDEP, CPU_ALTIVEC };
  227. #else
  228. int selectors[2] = { CTL_HW, HW_VECTORUNIT };
  229. #endif
  230. int hasVectorUnit = 0;
  231. size_t length = sizeof(hasVectorUnit);
  232. int error = sysctl(selectors, 2, &hasVectorUnit, &length, NULL, 0);
  233. if (0 == error)
  234. altivec = (hasVectorUnit != 0);
  235. #elif SDL_ALTIVEC_BLITTERS && HAVE_SETJMP
  236. void (*handler) (int sig);
  237. handler = signal(SIGILL, illegal_instruction);
  238. if (setjmp(jmpbuf) == 0) {
  239. asm volatile ("mtspr 256, %0\n\t" "vand %%v0, %%v0, %%v0"::"r" (-1));
  240. altivec = 1;
  241. }
  242. signal(SIGILL, handler);
  243. #endif
  244. return altivec;
  245. }
  246. static SDL_INLINE int
  247. CPU_haveMMX(void)
  248. {
  249. if (CPU_haveCPUID()) {
  250. return (CPU_getCPUIDFeatures() & 0x00800000);
  251. }
  252. return 0;
  253. }
  254. static SDL_INLINE int
  255. CPU_have3DNow(void)
  256. {
  257. if (CPU_haveCPUID()) {
  258. int a, b, c, d;
  259. cpuid(0x80000000, a, b, c, d);
  260. if (a >= 0x80000001) {
  261. cpuid(0x80000001, a, b, c, d);
  262. return (d & 0x80000000);
  263. }
  264. }
  265. return 0;
  266. }
  267. static SDL_INLINE int
  268. CPU_haveSSE(void)
  269. {
  270. if (CPU_haveCPUID()) {
  271. return (CPU_getCPUIDFeatures() & 0x02000000);
  272. }
  273. return 0;
  274. }
  275. static SDL_INLINE int
  276. CPU_haveSSE2(void)
  277. {
  278. if (CPU_haveCPUID()) {
  279. return (CPU_getCPUIDFeatures() & 0x04000000);
  280. }
  281. return 0;
  282. }
  283. static SDL_INLINE int
  284. CPU_haveSSE3(void)
  285. {
  286. if (CPU_haveCPUID()) {
  287. int a, b, c, d;
  288. cpuid(0, a, b, c, d);
  289. if (a >= 1) {
  290. cpuid(1, a, b, c, d);
  291. return (c & 0x00000001);
  292. }
  293. }
  294. return 0;
  295. }
  296. static SDL_INLINE int
  297. CPU_haveSSE41(void)
  298. {
  299. if (CPU_haveCPUID()) {
  300. int a, b, c, d;
  301. cpuid(1, a, b, c, d);
  302. if (a >= 1) {
  303. cpuid(1, a, b, c, d);
  304. return (c & 0x00080000);
  305. }
  306. }
  307. return 0;
  308. }
  309. static SDL_INLINE int
  310. CPU_haveSSE42(void)
  311. {
  312. if (CPU_haveCPUID()) {
  313. int a, b, c, d;
  314. cpuid(1, a, b, c, d);
  315. if (a >= 1) {
  316. cpuid(1, a, b, c, d);
  317. return (c & 0x00100000);
  318. }
  319. }
  320. return 0;
  321. }
  322. static SDL_INLINE int
  323. CPU_haveAVX(void)
  324. {
  325. if (CPU_haveCPUID()) {
  326. int a, b, c, d;
  327. cpuid(1, a, b, c, d);
  328. if (a >= 1) {
  329. cpuid(1, a, b, c, d);
  330. return (c & 0x10000000);
  331. }
  332. }
  333. return 0;
  334. }
  335. static int SDL_CPUCount = 0;
  336. int
  337. SDL_GetCPUCount(void)
  338. {
  339. if (!SDL_CPUCount) {
  340. #if defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN)
  341. if (SDL_CPUCount <= 0) {
  342. SDL_CPUCount = (int)sysconf(_SC_NPROCESSORS_ONLN);
  343. }
  344. #endif
  345. #ifdef HAVE_SYSCTLBYNAME
  346. if (SDL_CPUCount <= 0) {
  347. size_t size = sizeof(SDL_CPUCount);
  348. sysctlbyname("hw.ncpu", &SDL_CPUCount, &size, NULL, 0);
  349. }
  350. #endif
  351. #ifdef __WIN32__
  352. if (SDL_CPUCount <= 0) {
  353. SYSTEM_INFO info;
  354. GetSystemInfo(&info);
  355. SDL_CPUCount = info.dwNumberOfProcessors;
  356. }
  357. #endif
  358. /* There has to be at least 1, right? :) */
  359. if (SDL_CPUCount <= 0) {
  360. SDL_CPUCount = 1;
  361. }
  362. }
  363. return SDL_CPUCount;
  364. }
  365. /* Oh, such a sweet sweet trick, just not very useful. :) */
  366. static const char *
  367. SDL_GetCPUType(void)
  368. {
  369. static char SDL_CPUType[13];
  370. if (!SDL_CPUType[0]) {
  371. int i = 0;
  372. int a, b, c, d;
  373. if (CPU_haveCPUID()) {
  374. cpuid(0x00000000, a, b, c, d);
  375. SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
  376. SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
  377. SDL_CPUType[i++] = (char)(b & 0xff); b >>= 8;
  378. SDL_CPUType[i++] = (char)(b & 0xff);
  379. SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
  380. SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
  381. SDL_CPUType[i++] = (char)(d & 0xff); d >>= 8;
  382. SDL_CPUType[i++] = (char)(d & 0xff);
  383. SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
  384. SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
  385. SDL_CPUType[i++] = (char)(c & 0xff); c >>= 8;
  386. SDL_CPUType[i++] = (char)(c & 0xff);
  387. }
  388. if (!SDL_CPUType[0]) {
  389. SDL_strlcpy(SDL_CPUType, "Unknown", sizeof(SDL_CPUType));
  390. }
  391. }
  392. return SDL_CPUType;
  393. }
  394. #ifdef TEST_MAIN /* !!! FIXME: only used for test at the moment. */
  395. static const char *
  396. SDL_GetCPUName(void)
  397. {
  398. static char SDL_CPUName[48];
  399. if (!SDL_CPUName[0]) {
  400. int i = 0;
  401. int a, b, c, d;
  402. if (CPU_haveCPUID()) {
  403. cpuid(0x80000000, a, b, c, d);
  404. if (a >= 0x80000004) {
  405. cpuid(0x80000002, a, b, c, d);
  406. SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
  407. SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
  408. SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
  409. SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
  410. SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
  411. SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
  412. SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
  413. SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
  414. SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
  415. SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
  416. SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
  417. SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
  418. SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
  419. SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
  420. SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
  421. SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
  422. cpuid(0x80000003, a, b, c, d);
  423. SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
  424. SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
  425. SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
  426. SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
  427. SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
  428. SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
  429. SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
  430. SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
  431. SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
  432. SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
  433. SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
  434. SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
  435. SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
  436. SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
  437. SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
  438. SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
  439. cpuid(0x80000004, a, b, c, d);
  440. SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
  441. SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
  442. SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
  443. SDL_CPUName[i++] = (char)(a & 0xff); a >>= 8;
  444. SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
  445. SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
  446. SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
  447. SDL_CPUName[i++] = (char)(b & 0xff); b >>= 8;
  448. SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
  449. SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
  450. SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
  451. SDL_CPUName[i++] = (char)(c & 0xff); c >>= 8;
  452. SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
  453. SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
  454. SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
  455. SDL_CPUName[i++] = (char)(d & 0xff); d >>= 8;
  456. }
  457. }
  458. if (!SDL_CPUName[0]) {
  459. SDL_strlcpy(SDL_CPUName, "Unknown", sizeof(SDL_CPUName));
  460. }
  461. }
  462. return SDL_CPUName;
  463. }
  464. #endif
  465. int
  466. SDL_GetCPUCacheLineSize(void)
  467. {
  468. const char *cpuType = SDL_GetCPUType();
  469. if (SDL_strcmp(cpuType, "GenuineIntel") == 0) {
  470. int a, b, c, d;
  471. cpuid(0x00000001, a, b, c, d);
  472. return (((b >> 8) & 0xff) * 8);
  473. } else if (SDL_strcmp(cpuType, "AuthenticAMD") == 0) {
  474. int a, b, c, d;
  475. cpuid(0x80000005, a, b, c, d);
  476. return (c & 0xff);
  477. } else {
  478. /* Just make a guess here... */
  479. return SDL_CACHELINE_SIZE;
  480. }
  481. }
  482. static Uint32 SDL_CPUFeatures = 0xFFFFFFFF;
  483. static Uint32
  484. SDL_GetCPUFeatures(void)
  485. {
  486. if (SDL_CPUFeatures == 0xFFFFFFFF) {
  487. SDL_CPUFeatures = 0;
  488. if (CPU_haveRDTSC()) {
  489. SDL_CPUFeatures |= CPU_HAS_RDTSC;
  490. }
  491. if (CPU_haveAltiVec()) {
  492. SDL_CPUFeatures |= CPU_HAS_ALTIVEC;
  493. }
  494. if (CPU_haveMMX()) {
  495. SDL_CPUFeatures |= CPU_HAS_MMX;
  496. }
  497. if (CPU_have3DNow()) {
  498. SDL_CPUFeatures |= CPU_HAS_3DNOW;
  499. }
  500. if (CPU_haveSSE()) {
  501. SDL_CPUFeatures |= CPU_HAS_SSE;
  502. }
  503. if (CPU_haveSSE2()) {
  504. SDL_CPUFeatures |= CPU_HAS_SSE2;
  505. }
  506. if (CPU_haveSSE3()) {
  507. SDL_CPUFeatures |= CPU_HAS_SSE3;
  508. }
  509. if (CPU_haveSSE41()) {
  510. SDL_CPUFeatures |= CPU_HAS_SSE41;
  511. }
  512. if (CPU_haveSSE42()) {
  513. SDL_CPUFeatures |= CPU_HAS_SSE42;
  514. }
  515. if (CPU_haveAVX()) {
  516. SDL_CPUFeatures |= CPU_HAS_AVX;
  517. }
  518. }
  519. return SDL_CPUFeatures;
  520. }
  521. SDL_bool
  522. SDL_HasRDTSC(void)
  523. {
  524. if (SDL_GetCPUFeatures() & CPU_HAS_RDTSC) {
  525. return SDL_TRUE;
  526. }
  527. return SDL_FALSE;
  528. }
  529. SDL_bool
  530. SDL_HasAltiVec(void)
  531. {
  532. if (SDL_GetCPUFeatures() & CPU_HAS_ALTIVEC) {
  533. return SDL_TRUE;
  534. }
  535. return SDL_FALSE;
  536. }
  537. SDL_bool
  538. SDL_HasMMX(void)
  539. {
  540. if (SDL_GetCPUFeatures() & CPU_HAS_MMX) {
  541. return SDL_TRUE;
  542. }
  543. return SDL_FALSE;
  544. }
  545. SDL_bool
  546. SDL_Has3DNow(void)
  547. {
  548. if (SDL_GetCPUFeatures() & CPU_HAS_3DNOW) {
  549. return SDL_TRUE;
  550. }
  551. return SDL_FALSE;
  552. }
  553. SDL_bool
  554. SDL_HasSSE(void)
  555. {
  556. if (SDL_GetCPUFeatures() & CPU_HAS_SSE) {
  557. return SDL_TRUE;
  558. }
  559. return SDL_FALSE;
  560. }
  561. SDL_bool
  562. SDL_HasSSE2(void)
  563. {
  564. if (SDL_GetCPUFeatures() & CPU_HAS_SSE2) {
  565. return SDL_TRUE;
  566. }
  567. return SDL_FALSE;
  568. }
  569. SDL_bool
  570. SDL_HasSSE3(void)
  571. {
  572. if (SDL_GetCPUFeatures() & CPU_HAS_SSE3) {
  573. return SDL_TRUE;
  574. }
  575. return SDL_FALSE;
  576. }
  577. SDL_bool
  578. SDL_HasSSE41(void)
  579. {
  580. if (SDL_GetCPUFeatures() & CPU_HAS_SSE41) {
  581. return SDL_TRUE;
  582. }
  583. return SDL_FALSE;
  584. }
  585. SDL_bool
  586. SDL_HasSSE42(void)
  587. {
  588. if (SDL_GetCPUFeatures() & CPU_HAS_SSE42) {
  589. return SDL_TRUE;
  590. }
  591. return SDL_FALSE;
  592. }
  593. SDL_bool
  594. SDL_HasAVX(void)
  595. {
  596. if (SDL_GetCPUFeatures() & CPU_HAS_AVX) {
  597. return SDL_TRUE;
  598. }
  599. return SDL_FALSE;
  600. }
  601. static int SDL_SystemRAM = 0;
  602. int
  603. SDL_GetSystemRAM(void)
  604. {
  605. if (!SDL_SystemRAM) {
  606. #if defined(HAVE_SYSCONF) && defined(_SC_PHYS_PAGES) && defined(_SC_PAGESIZE)
  607. if (SDL_SystemRAM <= 0) {
  608. SDL_SystemRAM = (int)((Sint64)sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE) / (1024*1024));
  609. }
  610. #endif
  611. #ifdef HAVE_SYSCTLBYNAME
  612. if (SDL_SystemRAM <= 0) {
  613. #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
  614. #ifdef HW_REALMEM
  615. int mib[2] = {CTL_HW, HW_REALMEM};
  616. #else
  617. /* might only report up to 2 GiB */
  618. int mib[2] = {CTL_HW, HW_PHYSMEM};
  619. #endif /* HW_REALMEM */
  620. #else
  621. int mib[2] = {CTL_HW, HW_MEMSIZE};
  622. #endif /* __FreeBSD__ || __FreeBSD_kernel__ */
  623. Uint64 memsize = 0;
  624. size_t len = sizeof(memsize);
  625. if (sysctl(mib, 2, &memsize, &len, NULL, 0) == 0) {
  626. SDL_SystemRAM = (int)(memsize / (1024*1024));
  627. }
  628. }
  629. #endif
  630. #ifdef __WIN32__
  631. if (SDL_SystemRAM <= 0) {
  632. MEMORYSTATUSEX stat;
  633. stat.dwLength = sizeof(stat);
  634. if (GlobalMemoryStatusEx(&stat)) {
  635. SDL_SystemRAM = (int)(stat.ullTotalPhys / (1024 * 1024));
  636. }
  637. }
  638. #endif
  639. }
  640. return SDL_SystemRAM;
  641. }
  642. #ifdef TEST_MAIN
  643. #include <stdio.h>
  644. int
  645. main()
  646. {
  647. printf("CPU count: %d\n", SDL_GetCPUCount());
  648. printf("CPU type: %s\n", SDL_GetCPUType());
  649. printf("CPU name: %s\n", SDL_GetCPUName());
  650. printf("CacheLine size: %d\n", SDL_GetCPUCacheLineSize());
  651. printf("RDTSC: %d\n", SDL_HasRDTSC());
  652. printf("Altivec: %d\n", SDL_HasAltiVec());
  653. printf("MMX: %d\n", SDL_HasMMX());
  654. printf("3DNow: %d\n", SDL_Has3DNow());
  655. printf("SSE: %d\n", SDL_HasSSE());
  656. printf("SSE2: %d\n", SDL_HasSSE2());
  657. printf("SSE3: %d\n", SDL_HasSSE3());
  658. printf("SSE4.1: %d\n", SDL_HasSSE41());
  659. printf("SSE4.2: %d\n", SDL_HasSSE42());
  660. printf("AVX: %d\n", SDL_HasAVX());
  661. printf("RAM: %d MB\n", SDL_GetSystemRAM());
  662. return 0;
  663. }
  664. #endif /* TEST_MAIN */
  665. /* vi: set ts=4 sw=4 expandtab: */