SDL_cpuinfo.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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. #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 sets.
  158. *
  159. * \returns SDL_TRUE if the CPU has AltiVec features or SDL_FALSE if not.
  160. *
  161. * \sa SDL_Has3DNow
  162. * \sa SDL_HasAVX
  163. * \sa SDL_HasAVX2
  164. * \sa SDL_HasMMX
  165. * \sa SDL_HasRDTSC
  166. * \sa SDL_HasSSE
  167. * \sa SDL_HasSSE2
  168. * \sa SDL_HasSSE3
  169. * \sa SDL_HasSSE41
  170. * \sa SDL_HasSSE42
  171. */
  172. extern DECLSPEC SDL_bool SDLCALL SDL_HasAltiVec(void);
  173. /**
  174. * Determine whether the CPU has MMX features.
  175. *
  176. * This always returns false on CPUs that aren't using Intel instruction sets.
  177. *
  178. * \returns SDL_TRUE if the CPU has MMX features or SDL_FALSE if not.
  179. *
  180. * \sa SDL_Has3DNow
  181. * \sa SDL_HasAltiVec
  182. * \sa SDL_HasAVX
  183. * \sa SDL_HasAVX2
  184. * \sa SDL_HasRDTSC
  185. * \sa SDL_HasSSE
  186. * \sa SDL_HasSSE2
  187. * \sa SDL_HasSSE3
  188. * \sa SDL_HasSSE41
  189. * \sa SDL_HasSSE42
  190. */
  191. extern DECLSPEC SDL_bool SDLCALL SDL_HasMMX(void);
  192. /**
  193. * Determine whether the CPU has 3DNow! features.
  194. *
  195. * This always returns false on CPUs that aren't using AMD instruction sets.
  196. *
  197. * \returns SDL_TRUE if the CPU has 3DNow! features or SDL_FALSE if not.
  198. *
  199. * \sa SDL_HasAltiVec
  200. * \sa SDL_HasAVX
  201. * \sa SDL_HasAVX2
  202. * \sa SDL_HasMMX
  203. * \sa SDL_HasRDTSC
  204. * \sa SDL_HasSSE
  205. * \sa SDL_HasSSE2
  206. * \sa SDL_HasSSE3
  207. * \sa SDL_HasSSE41
  208. * \sa SDL_HasSSE42
  209. */
  210. extern DECLSPEC SDL_bool SDLCALL SDL_Has3DNow(void);
  211. /**
  212. * Determine whether the CPU has SSE features.
  213. *
  214. * This always returns false on CPUs that aren't using Intel instruction sets.
  215. *
  216. * \returns SDL_TRUE if the CPU has SSE features or SDL_FALSE if not.
  217. *
  218. * \sa SDL_Has3DNow
  219. * \sa SDL_HasAltiVec
  220. * \sa SDL_HasAVX
  221. * \sa SDL_HasAVX2
  222. * \sa SDL_HasMMX
  223. * \sa SDL_HasRDTSC
  224. * \sa SDL_HasSSE2
  225. * \sa SDL_HasSSE3
  226. * \sa SDL_HasSSE41
  227. * \sa SDL_HasSSE42
  228. */
  229. extern DECLSPEC SDL_bool SDLCALL SDL_HasSSE(void);
  230. /**
  231. * Determine whether the CPU has SSE2 features.
  232. *
  233. * This always returns false on CPUs that aren't using Intel instruction sets.
  234. *
  235. * \returns SDL_TRUE if the CPU has SSE2 features or SDL_FALSE if not.
  236. *
  237. * \sa SDL_Has3DNow
  238. * \sa SDL_HasAltiVec
  239. * \sa SDL_HasAVX
  240. * \sa SDL_HasAVX2
  241. * \sa SDL_HasMMX
  242. * \sa SDL_HasRDTSC
  243. * \sa SDL_HasSSE
  244. * \sa SDL_HasSSE3
  245. * \sa SDL_HasSSE41
  246. * \sa SDL_HasSSE42
  247. */
  248. extern DECLSPEC SDL_bool SDLCALL SDL_HasSSE2(void);
  249. /**
  250. * Determine whether the CPU has SSE3 features.
  251. *
  252. * This always returns false on CPUs that aren't using Intel instruction sets.
  253. *
  254. * \returns SDL_TRUE if the CPU has SSE3 features or SDL_FALSE if not.
  255. *
  256. * \sa SDL_Has3DNow
  257. * \sa SDL_HasAltiVec
  258. * \sa SDL_HasAVX
  259. * \sa SDL_HasAVX2
  260. * \sa SDL_HasMMX
  261. * \sa SDL_HasRDTSC
  262. * \sa SDL_HasSSE
  263. * \sa SDL_HasSSE2
  264. * \sa SDL_HasSSE41
  265. * \sa SDL_HasSSE42
  266. */
  267. extern DECLSPEC SDL_bool SDLCALL SDL_HasSSE3(void);
  268. /**
  269. * Determine whether the CPU has SSE4.1 features.
  270. *
  271. * This always returns false on CPUs that aren't using Intel instruction sets.
  272. *
  273. * \returns SDL_TRUE if the CPU has SSE4.1 features or SDL_FALSE if not.
  274. *
  275. * \sa SDL_Has3DNow
  276. * \sa SDL_HasAltiVec
  277. * \sa SDL_HasAVX
  278. * \sa SDL_HasAVX2
  279. * \sa SDL_HasMMX
  280. * \sa SDL_HasRDTSC
  281. * \sa SDL_HasSSE
  282. * \sa SDL_HasSSE2
  283. * \sa SDL_HasSSE3
  284. * \sa SDL_HasSSE42
  285. */
  286. extern DECLSPEC SDL_bool SDLCALL SDL_HasSSE41(void);
  287. /**
  288. * Determine whether the CPU has SSE4.2 features.
  289. *
  290. * This always returns false on CPUs that aren't using Intel instruction sets.
  291. *
  292. * \returns SDL_TRUE if the CPU has SSE4.2 features or SDL_FALSE if not.
  293. *
  294. * \sa SDL_Has3DNow
  295. * \sa SDL_HasAltiVec
  296. * \sa SDL_HasAVX
  297. * \sa SDL_HasAVX2
  298. * \sa SDL_HasMMX
  299. * \sa SDL_HasRDTSC
  300. * \sa SDL_HasSSE
  301. * \sa SDL_HasSSE2
  302. * \sa SDL_HasSSE3
  303. * \sa SDL_HasSSE41
  304. */
  305. extern DECLSPEC SDL_bool SDLCALL SDL_HasSSE42(void);
  306. /**
  307. * Determine whether the CPU has AVX features.
  308. *
  309. * This always returns false on CPUs that aren't using Intel instruction sets.
  310. *
  311. * \returns SDL_TRUE if the CPU has AVX features or SDL_FALSE if not.
  312. *
  313. * \since This function is available since SDL 2.0.2.
  314. *
  315. * \sa SDL_Has3DNow
  316. * \sa SDL_HasAltiVec
  317. * \sa SDL_HasAVX2
  318. * \sa SDL_HasMMX
  319. * \sa SDL_HasRDTSC
  320. * \sa SDL_HasSSE
  321. * \sa SDL_HasSSE2
  322. * \sa SDL_HasSSE3
  323. * \sa SDL_HasSSE41
  324. * \sa SDL_HasSSE42
  325. */
  326. extern DECLSPEC SDL_bool SDLCALL SDL_HasAVX(void);
  327. /**
  328. * Determine whether the CPU has AVX2 features.
  329. *
  330. * This always returns false on CPUs that aren't using Intel instruction sets.
  331. *
  332. * \returns SDL_TRUE if the CPU has AVX2 features or SDL_FALSE if not.
  333. *
  334. * \since This function is available since SDL 2.0.4.
  335. *
  336. * \sa SDL_Has3DNow
  337. * \sa SDL_HasAltiVec
  338. * \sa SDL_HasAVX
  339. * \sa SDL_HasMMX
  340. * \sa SDL_HasRDTSC
  341. * \sa SDL_HasSSE
  342. * \sa SDL_HasSSE2
  343. * \sa SDL_HasSSE3
  344. * \sa SDL_HasSSE41
  345. * \sa SDL_HasSSE42
  346. */
  347. extern DECLSPEC SDL_bool SDLCALL SDL_HasAVX2(void);
  348. /**
  349. * Determine whether the CPU has AVX-512F (foundation) features.
  350. *
  351. * This always returns false on CPUs that aren't using Intel instruction sets.
  352. *
  353. * \returns SDL_TRUE if the CPU has AVX-512F features or SDL_FALSE if not.
  354. *
  355. * \sa SDL_HasAVX
  356. */
  357. extern DECLSPEC SDL_bool SDLCALL SDL_HasAVX512F(void);
  358. /**
  359. * Determine whether the CPU has ARM SIMD (ARMv6) features.
  360. *
  361. * This is different from ARM NEON, which is a different instruction set.
  362. *
  363. * This always returns false on CPUs that aren't using ARM instruction sets.
  364. *
  365. * \returns SDL_TRUE if the CPU has ARM SIMD features or SDL_FALSE if not.
  366. *
  367. * \sa SDL_HasNEON
  368. */
  369. extern DECLSPEC SDL_bool SDLCALL SDL_HasARMSIMD(void);
  370. /**
  371. * Determine whether the CPU has NEON (ARM SIMD) features.
  372. *
  373. * This always returns false on CPUs that aren't using ARM instruction sets.
  374. *
  375. * \returns SDL_TRUE if the CPU has ARM NEON features or SDL_FALSE if not.
  376. */
  377. extern DECLSPEC SDL_bool SDLCALL SDL_HasNEON(void);
  378. /**
  379. * Get the amount of RAM configured in the system.
  380. *
  381. * \returns the amount of RAM configured in the system in MB.
  382. *
  383. * \since This function is available since SDL 2.0.1.
  384. */
  385. extern DECLSPEC int SDLCALL SDL_GetSystemRAM(void);
  386. /**
  387. * Report the alignment this system needs for SIMD allocations.
  388. *
  389. * This will return the minimum number of bytes to which a pointer must be
  390. * aligned to be compatible with SIMD instructions on the current machine.
  391. * For example, if the machine supports SSE only, it will return 16, but if
  392. * it supports AVX-512F, it'll return 64 (etc). This only reports values for
  393. * instruction sets SDL knows about, so if your SDL build doesn't have
  394. * SDL_HasAVX512F(), then it might return 16 for the SSE support it sees and
  395. * not 64 for the AVX-512 instructions that exist but SDL doesn't know about.
  396. * Plan accordingly.
  397. *
  398. * \returns Alignment in bytes needed for available, known SIMD instructions.
  399. */
  400. extern DECLSPEC size_t SDLCALL SDL_SIMDGetAlignment(void);
  401. /**
  402. * Allocate memory in a SIMD-friendly way.
  403. *
  404. * This will allocate a block of memory that is suitable for use with SIMD
  405. * instructions. Specifically, it will be properly aligned and padded for
  406. * the system's supported vector instructions.
  407. *
  408. * The memory returned will be padded such that it is safe to read or write
  409. * an incomplete vector at the end of the memory block. This can be useful
  410. * so you don't have to drop back to a scalar fallback at the end of your
  411. * SIMD processing loop to deal with the final elements without overflowing
  412. * the allocated buffer.
  413. *
  414. * You must free this memory with SDL_FreeSIMD(), not free() or SDL_free()
  415. * or delete[], etc.
  416. *
  417. * Note that SDL will only deal with SIMD instruction sets it is aware of;
  418. * for example, SDL 2.0.8 knows that SSE wants 16-byte vectors
  419. * (SDL_HasSSE()), and AVX2 wants 32 bytes (SDL_HasAVX2()), but doesn't
  420. * know that AVX-512 wants 64. To be clear: if you can't decide to use an
  421. * instruction set with an SDL_Has*() function, don't use that instruction
  422. * set with memory allocated through here.
  423. *
  424. * SDL_AllocSIMD(0) will return a non-NULL pointer, assuming the system isn't
  425. * out of memory, but you are not allowed to dereference it (because you only
  426. * own zero bytes of that buffer).
  427. *
  428. * \param len The length, in bytes, of the block to allocate. The actual
  429. * allocated block might be larger due to padding, etc.
  430. * \returns Pointer to newly-allocated block, NULL if out of memory.
  431. *
  432. * \sa SDL_SIMDAlignment
  433. * \sa SDL_SIMDRealloc
  434. * \sa SDL_SIMDFree
  435. */
  436. extern DECLSPEC void * SDLCALL SDL_SIMDAlloc(const size_t len);
  437. /**
  438. * Reallocate memory obtained from SDL_SIMDAlloc
  439. *
  440. * It is not valid to use this function on a pointer from anything but
  441. * SDL_SIMDAlloc(). It can't be used on pointers from malloc, realloc,
  442. * SDL_malloc, memalign, new[], etc.
  443. *
  444. * \param mem The pointer obtained from SDL_SIMDAlloc. This function also
  445. * accepts NULL, at which point this function is the same as
  446. * calling SDL_SIMDAlloc with a NULL pointer.
  447. * \param len The length, in bytes, of the block to allocated. The actual
  448. * allocated block might be larger due to padding, etc. Passing 0
  449. * will return a non-NULL pointer, assuming the system isn't out of
  450. * memory.
  451. * \returns Pointer to newly-reallocated block, NULL if out of memory.
  452. *
  453. * \sa SDL_SIMDAlignment
  454. * \sa SDL_SIMDAlloc
  455. * \sa SDL_SIMDFree
  456. */
  457. extern DECLSPEC void * SDLCALL SDL_SIMDRealloc(void *mem, const size_t len);
  458. /**
  459. * Deallocate memory obtained from SDL_SIMDAlloc
  460. *
  461. * It is not valid to use this function on a pointer from anything but
  462. * SDL_SIMDAlloc() or SDL_SIMDRealloc(). It can't be used on pointers from
  463. * malloc, realloc, SDL_malloc, memalign, new[], etc.
  464. *
  465. * However, SDL_SIMDFree(NULL) is a legal no-op.
  466. *
  467. * The memory pointed to by `ptr` is no longer valid for access upon return,
  468. * and may be returned to the system or reused by a future allocation.
  469. * The pointer passed to this function is no longer safe to dereference once
  470. * this function returns, and should be discarded.
  471. *
  472. * \param ptr The pointer, returned from SDL_SIMDAlloc or SDL_SIMDRealloc,
  473. * to deallocate. NULL is a legal no-op.
  474. *
  475. * \sa SDL_SIMDAlloc
  476. * \sa SDL_SIMDRealloc
  477. */
  478. extern DECLSPEC void SDLCALL SDL_SIMDFree(void *ptr);
  479. /* Ends C function definitions when using C++ */
  480. #ifdef __cplusplus
  481. }
  482. #endif
  483. #include "close_code.h"
  484. #endif /* SDL_cpuinfo_h_ */
  485. /* vi: set ts=4 sw=4 expandtab: */