🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

559 lines
16 KiB

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