SDL_cpuinfo.h 15 KB

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