SDL_cpuinfo.h 15 KB

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