SDL_cpuinfo.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2021 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. /**
  19. * \file SDL_cpuinfo.h
  20. *
  21. * CPU feature detection for SDL.
  22. */
  23. #ifndef SDL_cpuinfo_h_
  24. #define SDL_cpuinfo_h_
  25. #include "SDL_stdinc.h"
  26. /* Need to do this here because intrin.h has C++ code in it */
  27. /* Visual Studio 2005 has a bug where intrin.h conflicts with winnt.h */
  28. #if defined(_MSC_VER) && (_MSC_VER >= 1500) && (defined(_M_IX86) || defined(_M_X64))
  29. #ifdef __clang__
  30. /* Many of the intrinsics SDL uses are not implemented by clang with Visual Studio */
  31. #undef __MMX__
  32. #undef __SSE__
  33. #undef __SSE2__
  34. #else
  35. #include <intrin.h>
  36. #ifndef _WIN64
  37. #ifndef __MMX__
  38. #define __MMX__
  39. #endif
  40. #ifndef __3dNOW__
  41. #define __3dNOW__
  42. #endif
  43. #endif
  44. #ifndef __SSE__
  45. #define __SSE__
  46. #endif
  47. #ifndef __SSE2__
  48. #define __SSE2__
  49. #endif
  50. #endif /* __clang__ */
  51. #elif defined(__MINGW64_VERSION_MAJOR)
  52. #include <intrin.h>
  53. #if !defined(SDL_DISABLE_ARM_NEON_H) && defined(__ARM_NEON)
  54. # include <arm_neon.h>
  55. #endif
  56. #else
  57. /* altivec.h redefining bool causes a number of problems, see bugs 3993 and 4392, so you need to explicitly define SDL_ENABLE_ALTIVEC_H to have it included. */
  58. #if defined(HAVE_ALTIVEC_H) && defined(__ALTIVEC__) && !defined(__APPLE_ALTIVEC__) && defined(SDL_ENABLE_ALTIVEC_H)
  59. #include <altivec.h>
  60. #endif
  61. #if !defined(SDL_DISABLE_ARM_NEON_H)
  62. # if defined(__ARM_NEON)
  63. # include <arm_neon.h>
  64. # elif defined(__WINDOWS__) || defined(__WINRT__)
  65. /* Visual Studio doesn't define __ARM_ARCH, but _M_ARM (if set, always 7), and _M_ARM64 (if set, always 1). */
  66. # if defined(_M_ARM)
  67. # include <armintr.h>
  68. # include <arm_neon.h>
  69. # define __ARM_NEON 1 /* Set __ARM_NEON so that it can be used elsewhere, at compile time */
  70. # endif
  71. # if defined (_M_ARM64)
  72. # include <arm64intr.h>
  73. # include <arm64_neon.h>
  74. # define __ARM_NEON 1 /* Set __ARM_NEON so that it can be used elsewhere, at compile time */
  75. # endif
  76. # endif
  77. #endif
  78. #if defined(__3dNOW__) && !defined(SDL_DISABLE_MM3DNOW_H)
  79. #include <mm3dnow.h>
  80. #endif
  81. #if defined(HAVE_IMMINTRIN_H) && !defined(SDL_DISABLE_IMMINTRIN_H)
  82. #include <immintrin.h>
  83. #else
  84. #if defined(__MMX__) && !defined(SDL_DISABLE_MMINTRIN_H)
  85. #include <mmintrin.h>
  86. #endif
  87. #if defined(__SSE__) && !defined(SDL_DISABLE_XMMINTRIN_H)
  88. #include <xmmintrin.h>
  89. #endif
  90. #if defined(__SSE2__) && !defined(SDL_DISABLE_EMMINTRIN_H)
  91. #include <emmintrin.h>
  92. #endif
  93. #if defined(__SSE3__) && !defined(SDL_DISABLE_PMMINTRIN_H)
  94. #include <pmmintrin.h>
  95. #endif
  96. #endif /* HAVE_IMMINTRIN_H */
  97. #endif /* compiler version */
  98. #include "begin_code.h"
  99. /* Set up for C function definitions, even when using C++ */
  100. #ifdef __cplusplus
  101. extern "C" {
  102. #endif
  103. /* This is a guess for the cacheline size used for padding.
  104. * Most x86 processors have a 64 byte cache line.
  105. * The 64-bit PowerPC processors have a 128 byte cache line.
  106. * We'll use the larger value to be generally safe.
  107. */
  108. #define SDL_CACHELINE_SIZE 128
  109. /**
  110. * Get the number of CPU cores available.
  111. *
  112. * \returns the total number of logical CPU cores. On CPUs that include
  113. * technologies such as hyperthreading, the number of logical cores
  114. * may be more than the number of physical cores.
  115. *
  116. * \since This function is available since SDL 2.0.0.
  117. */
  118. extern DECLSPEC int SDLCALL SDL_GetCPUCount(void);
  119. /**
  120. * Determine the L1 cache line size of the CPU.
  121. *
  122. * This is useful for determining multi-threaded structure padding or SIMD
  123. * prefetch sizes.
  124. *
  125. * \returns the L1 cache line size of the CPU, in bytes.
  126. *
  127. * \since This function is available since SDL 2.0.0.
  128. */
  129. extern DECLSPEC int SDLCALL SDL_GetCPUCacheLineSize(void);
  130. /**
  131. * Determine whether the CPU has the RDTSC instruction.
  132. *
  133. * This always returns false on CPUs that aren't using Intel instruction sets.
  134. *
  135. * \returns SDL_TRUE if the CPU has the RDTSC instruction or SDL_FALSE if not.
  136. *
  137. * \sa SDL_Has3DNow
  138. * \sa SDL_HasAltiVec
  139. * \sa SDL_HasAVX
  140. * \sa SDL_HasAVX2
  141. * \sa SDL_HasMMX
  142. * \sa SDL_HasSSE
  143. * \sa SDL_HasSSE2
  144. * \sa SDL_HasSSE3
  145. * \sa SDL_HasSSE41
  146. * \sa SDL_HasSSE42
  147. */
  148. extern DECLSPEC SDL_bool SDLCALL SDL_HasRDTSC(void);
  149. /**
  150. * Determine whether the CPU has AltiVec features.
  151. *
  152. * This always returns false on CPUs that aren't using PowerPC instruction sets.
  153. *
  154. * \returns SDL_TRUE if the CPU has AltiVec features or SDL_FALSE if not.
  155. *
  156. * \sa SDL_Has3DNow
  157. * \sa SDL_HasAVX
  158. * \sa SDL_HasAVX2
  159. * \sa SDL_HasMMX
  160. * \sa SDL_HasRDTSC
  161. * \sa SDL_HasSSE
  162. * \sa SDL_HasSSE2
  163. * \sa SDL_HasSSE3
  164. * \sa SDL_HasSSE41
  165. * \sa SDL_HasSSE42
  166. */
  167. extern DECLSPEC SDL_bool SDLCALL SDL_HasAltiVec(void);
  168. /**
  169. * Determine whether the CPU has MMX features.
  170. *
  171. * This always returns false on CPUs that aren't using Intel instruction sets.
  172. *
  173. * \returns SDL_TRUE if the CPU has MMX features or SDL_FALSE if not.
  174. *
  175. * \sa SDL_Has3DNow
  176. * \sa SDL_HasAltiVec
  177. * \sa SDL_HasAVX
  178. * \sa SDL_HasAVX2
  179. * \sa SDL_HasRDTSC
  180. * \sa SDL_HasSSE
  181. * \sa SDL_HasSSE2
  182. * \sa SDL_HasSSE3
  183. * \sa SDL_HasSSE41
  184. * \sa SDL_HasSSE42
  185. */
  186. extern DECLSPEC SDL_bool SDLCALL SDL_HasMMX(void);
  187. /**
  188. * Determine whether the CPU has 3DNow! features.
  189. *
  190. * This always returns false on CPUs that aren't using AMD instruction sets.
  191. *
  192. * \returns SDL_TRUE if the CPU has 3DNow! features or SDL_FALSE if not.
  193. *
  194. * \sa SDL_HasAltiVec
  195. * \sa SDL_HasAVX
  196. * \sa SDL_HasAVX2
  197. * \sa SDL_HasMMX
  198. * \sa SDL_HasRDTSC
  199. * \sa SDL_HasSSE
  200. * \sa SDL_HasSSE2
  201. * \sa SDL_HasSSE3
  202. * \sa SDL_HasSSE41
  203. * \sa SDL_HasSSE42
  204. */
  205. extern DECLSPEC SDL_bool SDLCALL SDL_Has3DNow(void);
  206. /**
  207. * Determine whether the CPU has SSE features.
  208. *
  209. * This always returns false on CPUs that aren't using Intel instruction sets.
  210. *
  211. * \returns SDL_TRUE if the CPU has SSE features or SDL_FALSE if not.
  212. *
  213. * \sa SDL_Has3DNow
  214. * \sa SDL_HasAltiVec
  215. * \sa SDL_HasAVX
  216. * \sa SDL_HasAVX2
  217. * \sa SDL_HasMMX
  218. * \sa SDL_HasRDTSC
  219. * \sa SDL_HasSSE2
  220. * \sa SDL_HasSSE3
  221. * \sa SDL_HasSSE41
  222. * \sa SDL_HasSSE42
  223. */
  224. extern DECLSPEC SDL_bool SDLCALL SDL_HasSSE(void);
  225. /**
  226. * Determine whether the CPU has SSE2 features.
  227. *
  228. * This always returns false on CPUs that aren't using Intel instruction sets.
  229. *
  230. * \returns SDL_TRUE if the CPU has SSE2 features or SDL_FALSE if not.
  231. *
  232. * \sa SDL_Has3DNow
  233. * \sa SDL_HasAltiVec
  234. * \sa SDL_HasAVX
  235. * \sa SDL_HasAVX2
  236. * \sa SDL_HasMMX
  237. * \sa SDL_HasRDTSC
  238. * \sa SDL_HasSSE
  239. * \sa SDL_HasSSE3
  240. * \sa SDL_HasSSE41
  241. * \sa SDL_HasSSE42
  242. */
  243. extern DECLSPEC SDL_bool SDLCALL SDL_HasSSE2(void);
  244. /**
  245. * Determine whether the CPU has SSE3 features.
  246. *
  247. * This always returns false on CPUs that aren't using Intel instruction sets.
  248. *
  249. * \returns SDL_TRUE if the CPU has SSE3 features or SDL_FALSE if not.
  250. *
  251. * \sa SDL_Has3DNow
  252. * \sa SDL_HasAltiVec
  253. * \sa SDL_HasAVX
  254. * \sa SDL_HasAVX2
  255. * \sa SDL_HasMMX
  256. * \sa SDL_HasRDTSC
  257. * \sa SDL_HasSSE
  258. * \sa SDL_HasSSE2
  259. * \sa SDL_HasSSE41
  260. * \sa SDL_HasSSE42
  261. */
  262. extern DECLSPEC SDL_bool SDLCALL SDL_HasSSE3(void);
  263. /**
  264. * Determine whether the CPU has SSE4.1 features.
  265. *
  266. * This always returns false on CPUs that aren't using Intel instruction sets.
  267. *
  268. * \returns SDL_TRUE if the CPU has SSE4.1 features or SDL_FALSE if not.
  269. *
  270. * \sa SDL_Has3DNow
  271. * \sa SDL_HasAltiVec
  272. * \sa SDL_HasAVX
  273. * \sa SDL_HasAVX2
  274. * \sa SDL_HasMMX
  275. * \sa SDL_HasRDTSC
  276. * \sa SDL_HasSSE
  277. * \sa SDL_HasSSE2
  278. * \sa SDL_HasSSE3
  279. * \sa SDL_HasSSE42
  280. */
  281. extern DECLSPEC SDL_bool SDLCALL SDL_HasSSE41(void);
  282. /**
  283. * Determine whether the CPU has SSE4.2 features.
  284. *
  285. * This always returns false on CPUs that aren't using Intel instruction sets.
  286. *
  287. * \returns SDL_TRUE if the CPU has SSE4.2 features or SDL_FALSE if not.
  288. *
  289. * \sa SDL_Has3DNow
  290. * \sa SDL_HasAltiVec
  291. * \sa SDL_HasAVX
  292. * \sa SDL_HasAVX2
  293. * \sa SDL_HasMMX
  294. * \sa SDL_HasRDTSC
  295. * \sa SDL_HasSSE
  296. * \sa SDL_HasSSE2
  297. * \sa SDL_HasSSE3
  298. * \sa SDL_HasSSE41
  299. */
  300. extern DECLSPEC SDL_bool SDLCALL SDL_HasSSE42(void);
  301. /**
  302. * Determine whether the CPU has AVX features.
  303. *
  304. * This always returns false on CPUs that aren't using Intel instruction sets.
  305. *
  306. * \returns SDL_TRUE if the CPU has AVX features or SDL_FALSE if not.
  307. *
  308. * \since This function is available since SDL 2.0.2.
  309. *
  310. * \sa SDL_Has3DNow
  311. * \sa SDL_HasAltiVec
  312. * \sa SDL_HasAVX2
  313. * \sa SDL_HasMMX
  314. * \sa SDL_HasRDTSC
  315. * \sa SDL_HasSSE
  316. * \sa SDL_HasSSE2
  317. * \sa SDL_HasSSE3
  318. * \sa SDL_HasSSE41
  319. * \sa SDL_HasSSE42
  320. */
  321. extern DECLSPEC SDL_bool SDLCALL SDL_HasAVX(void);
  322. /**
  323. * Determine whether the CPU has AVX2 features.
  324. *
  325. * This always returns false on CPUs that aren't using Intel instruction sets.
  326. *
  327. * \returns SDL_TRUE if the CPU has AVX2 features or SDL_FALSE if not.
  328. *
  329. * \since This function is available since SDL 2.0.4.
  330. *
  331. * \sa SDL_Has3DNow
  332. * \sa SDL_HasAltiVec
  333. * \sa SDL_HasAVX
  334. * \sa SDL_HasMMX
  335. * \sa SDL_HasRDTSC
  336. * \sa SDL_HasSSE
  337. * \sa SDL_HasSSE2
  338. * \sa SDL_HasSSE3
  339. * \sa SDL_HasSSE41
  340. * \sa SDL_HasSSE42
  341. */
  342. extern DECLSPEC SDL_bool SDLCALL SDL_HasAVX2(void);
  343. /**
  344. * Determine whether the CPU has AVX-512F (foundation) features.
  345. *
  346. * This always returns false on CPUs that aren't using Intel instruction sets.
  347. *
  348. * \returns SDL_TRUE if the CPU has AVX-512F features or SDL_FALSE if not.
  349. *
  350. * \sa SDL_HasAVX
  351. */
  352. extern DECLSPEC SDL_bool SDLCALL SDL_HasAVX512F(void);
  353. /**
  354. * Determine whether the CPU has ARM SIMD (ARMv6) features.
  355. *
  356. * This is different from ARM NEON, which is a different instruction set.
  357. *
  358. * This always returns false on CPUs that aren't using ARM instruction sets.
  359. *
  360. * \returns SDL_TRUE if the CPU has ARM SIMD features or SDL_FALSE if not.
  361. *
  362. * \sa SDL_HasNEON
  363. */
  364. extern DECLSPEC SDL_bool SDLCALL SDL_HasARMSIMD(void);
  365. /**
  366. * Determine whether the CPU has NEON (ARM SIMD) features.
  367. *
  368. * This always returns false on CPUs that aren't using ARM instruction sets.
  369. *
  370. * \returns SDL_TRUE if the CPU has ARM NEON features or SDL_FALSE if not.
  371. */
  372. extern DECLSPEC SDL_bool SDLCALL SDL_HasNEON(void);
  373. /**
  374. * Get the amount of RAM configured in the system.
  375. *
  376. * \returns the amount of RAM configured in the system in MB.
  377. *
  378. * \since This function is available since SDL 2.0.1.
  379. */
  380. extern DECLSPEC int SDLCALL SDL_GetSystemRAM(void);
  381. /**
  382. * Report the alignment this system needs for SIMD allocations.
  383. *
  384. * This will return the minimum number of bytes to which a pointer must be
  385. * aligned to be compatible with SIMD instructions on the current machine.
  386. * For example, if the machine supports SSE only, it will return 16, but if
  387. * it supports AVX-512F, it'll return 64 (etc). This only reports values for
  388. * instruction sets SDL knows about, so if your SDL build doesn't have
  389. * SDL_HasAVX512F(), then it might return 16 for the SSE support it sees and
  390. * not 64 for the AVX-512 instructions that exist but SDL doesn't know about.
  391. * Plan accordingly.
  392. *
  393. * \returns Alignment in bytes needed for available, known SIMD instructions.
  394. */
  395. extern DECLSPEC size_t SDLCALL SDL_SIMDGetAlignment(void);
  396. /**
  397. * Allocate memory in a SIMD-friendly way.
  398. *
  399. * This will allocate a block of memory that is suitable for use with SIMD
  400. * instructions. Specifically, it will be properly aligned and padded for
  401. * the system's supported vector instructions.
  402. *
  403. * The memory returned will be padded such that it is safe to read or write
  404. * an incomplete vector at the end of the memory block. This can be useful
  405. * so you don't have to drop back to a scalar fallback at the end of your
  406. * SIMD processing loop to deal with the final elements without overflowing
  407. * the allocated buffer.
  408. *
  409. * You must free this memory with SDL_FreeSIMD(), not free() or SDL_free()
  410. * or delete[], etc.
  411. *
  412. * Note that SDL will only deal with SIMD instruction sets it is aware of;
  413. * for example, SDL 2.0.8 knows that SSE wants 16-byte vectors
  414. * (SDL_HasSSE()), and AVX2 wants 32 bytes (SDL_HasAVX2()), but doesn't
  415. * know that AVX-512 wants 64. To be clear: if you can't decide to use an
  416. * instruction set with an SDL_Has*() function, don't use that instruction
  417. * set with memory allocated through here.
  418. *
  419. * SDL_AllocSIMD(0) will return a non-NULL pointer, assuming the system isn't
  420. * out of memory, but you are not allowed to dereference it (because you only
  421. * own zero bytes of that buffer).
  422. *
  423. * \param len The length, in bytes, of the block to allocate. The actual
  424. * allocated block might be larger due to padding, etc.
  425. * \returns Pointer to newly-allocated block, NULL if out of memory.
  426. *
  427. * \sa SDL_SIMDAlignment
  428. * \sa SDL_SIMDRealloc
  429. * \sa SDL_SIMDFree
  430. */
  431. extern DECLSPEC void * SDLCALL SDL_SIMDAlloc(const size_t len);
  432. /**
  433. * Reallocate memory obtained from SDL_SIMDAlloc
  434. *
  435. * It is not valid to use this function on a pointer from anything but
  436. * SDL_SIMDAlloc(). It can't be used on pointers from malloc, realloc,
  437. * SDL_malloc, memalign, new[], etc.
  438. *
  439. * \param mem The pointer obtained from SDL_SIMDAlloc. This function also
  440. * accepts NULL, at which point this function is the same as
  441. * calling SDL_SIMDAlloc with a NULL pointer.
  442. * \param len The length, in bytes, of the block to allocated. The actual
  443. * allocated block might be larger due to padding, etc. Passing 0
  444. * will return a non-NULL pointer, assuming the system isn't out of
  445. * memory.
  446. * \returns Pointer to newly-reallocated block, NULL if out of memory.
  447. *
  448. * \sa SDL_SIMDAlignment
  449. * \sa SDL_SIMDAlloc
  450. * \sa SDL_SIMDFree
  451. */
  452. extern DECLSPEC void * SDLCALL SDL_SIMDRealloc(void *mem, const size_t len);
  453. /**
  454. * Deallocate memory obtained from SDL_SIMDAlloc
  455. *
  456. * It is not valid to use this function on a pointer from anything but
  457. * SDL_SIMDAlloc() or SDL_SIMDRealloc(). It can't be used on pointers from
  458. * malloc, realloc, SDL_malloc, memalign, new[], etc.
  459. *
  460. * However, SDL_SIMDFree(NULL) is a legal no-op.
  461. *
  462. * The memory pointed to by `ptr` is no longer valid for access upon return,
  463. * and may be returned to the system or reused by a future allocation.
  464. * The pointer passed to this function is no longer safe to dereference once
  465. * this function returns, and should be discarded.
  466. *
  467. * \param ptr The pointer, returned from SDL_SIMDAlloc or SDL_SIMDRealloc,
  468. * to deallocate. NULL is a legal no-op.
  469. *
  470. * \sa SDL_SIMDAlloc
  471. * \sa SDL_SIMDRealloc
  472. */
  473. extern DECLSPEC void SDLCALL SDL_SIMDFree(void *ptr);
  474. /* Ends C function definitions when using C++ */
  475. #ifdef __cplusplus
  476. }
  477. #endif
  478. #include "close_code.h"
  479. #endif /* SDL_cpuinfo_h_ */
  480. /* vi: set ts=4 sw=4 expandtab: */