SDL_cpuinfo.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 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 <SDL3/SDL_stdinc.h>
  26. #include <SDL3/SDL_begin_code.h>
  27. /* Set up for C function definitions, even when using C++ */
  28. #ifdef __cplusplus
  29. extern "C" {
  30. #endif
  31. /**
  32. * A guess for the cacheline size used for padding.
  33. *
  34. * Most x86 processors have a 64 byte cache line. The 64-bit PowerPC
  35. * processors have a 128 byte cache line. We use the larger value to be
  36. * generally safe.
  37. *
  38. * \since This macro is available since SDL 3.0.0.
  39. */
  40. #define SDL_CACHELINE_SIZE 128
  41. /**
  42. * Get the number of CPU cores available.
  43. *
  44. * \returns the total number of logical CPU cores. On CPUs that include
  45. * technologies such as hyperthreading, the number of logical cores
  46. * may be more than the number of physical cores.
  47. *
  48. * \since This function is available since SDL 3.0.0.
  49. */
  50. extern DECLSPEC int SDLCALL SDL_GetCPUCount(void);
  51. /**
  52. * Determine the L1 cache line size of the CPU.
  53. *
  54. * This is useful for determining multi-threaded structure padding or SIMD
  55. * prefetch sizes.
  56. *
  57. * \returns the L1 cache line size of the CPU, in bytes.
  58. *
  59. * \since This function is available since SDL 3.0.0.
  60. */
  61. extern DECLSPEC int SDLCALL SDL_GetCPUCacheLineSize(void);
  62. /**
  63. * Determine whether the CPU has AltiVec features.
  64. *
  65. * This always returns false on CPUs that aren't using PowerPC instruction
  66. * sets.
  67. *
  68. * \returns SDL_TRUE if the CPU has AltiVec features or SDL_FALSE if not.
  69. *
  70. * \since This function is available since SDL 3.0.0.
  71. */
  72. extern DECLSPEC SDL_bool SDLCALL SDL_HasAltiVec(void);
  73. /**
  74. * Determine whether the CPU has MMX features.
  75. *
  76. * This always returns false on CPUs that aren't using Intel instruction sets.
  77. *
  78. * \returns SDL_TRUE if the CPU has MMX features or SDL_FALSE if not.
  79. *
  80. * \since This function is available since SDL 3.0.0.
  81. */
  82. extern DECLSPEC SDL_bool SDLCALL SDL_HasMMX(void);
  83. /**
  84. * Determine whether the CPU has SSE features.
  85. *
  86. * This always returns false on CPUs that aren't using Intel instruction sets.
  87. *
  88. * \returns SDL_TRUE if the CPU has SSE features or SDL_FALSE if not.
  89. *
  90. * \since This function is available since SDL 3.0.0.
  91. *
  92. * \sa SDL_HasSSE2
  93. * \sa SDL_HasSSE3
  94. * \sa SDL_HasSSE41
  95. * \sa SDL_HasSSE42
  96. */
  97. extern DECLSPEC SDL_bool SDLCALL SDL_HasSSE(void);
  98. /**
  99. * Determine whether the CPU has SSE2 features.
  100. *
  101. * This always returns false on CPUs that aren't using Intel instruction sets.
  102. *
  103. * \returns SDL_TRUE if the CPU has SSE2 features or SDL_FALSE if not.
  104. *
  105. * \since This function is available since SDL 3.0.0.
  106. *
  107. * \sa SDL_HasSSE
  108. * \sa SDL_HasSSE3
  109. * \sa SDL_HasSSE41
  110. * \sa SDL_HasSSE42
  111. */
  112. extern DECLSPEC SDL_bool SDLCALL SDL_HasSSE2(void);
  113. /**
  114. * Determine whether the CPU has SSE3 features.
  115. *
  116. * This always returns false on CPUs that aren't using Intel instruction sets.
  117. *
  118. * \returns SDL_TRUE if the CPU has SSE3 features or SDL_FALSE if not.
  119. *
  120. * \since This function is available since SDL 3.0.0.
  121. *
  122. * \sa SDL_HasSSE
  123. * \sa SDL_HasSSE2
  124. * \sa SDL_HasSSE41
  125. * \sa SDL_HasSSE42
  126. */
  127. extern DECLSPEC SDL_bool SDLCALL SDL_HasSSE3(void);
  128. /**
  129. * Determine whether the CPU has SSE4.1 features.
  130. *
  131. * This always returns false on CPUs that aren't using Intel instruction sets.
  132. *
  133. * \returns SDL_TRUE if the CPU has SSE4.1 features or SDL_FALSE if not.
  134. *
  135. * \since This function is available since SDL 3.0.0.
  136. *
  137. * \sa SDL_HasSSE
  138. * \sa SDL_HasSSE2
  139. * \sa SDL_HasSSE3
  140. * \sa SDL_HasSSE42
  141. */
  142. extern DECLSPEC SDL_bool SDLCALL SDL_HasSSE41(void);
  143. /**
  144. * Determine whether the CPU has SSE4.2 features.
  145. *
  146. * This always returns false on CPUs that aren't using Intel instruction sets.
  147. *
  148. * \returns SDL_TRUE if the CPU has SSE4.2 features or SDL_FALSE if not.
  149. *
  150. * \since This function is available since SDL 3.0.0.
  151. *
  152. * \sa SDL_HasSSE
  153. * \sa SDL_HasSSE2
  154. * \sa SDL_HasSSE3
  155. * \sa SDL_HasSSE41
  156. */
  157. extern DECLSPEC SDL_bool SDLCALL SDL_HasSSE42(void);
  158. /**
  159. * Determine whether the CPU has AVX features.
  160. *
  161. * This always returns false on CPUs that aren't using Intel instruction sets.
  162. *
  163. * \returns SDL_TRUE if the CPU has AVX features or SDL_FALSE if not.
  164. *
  165. * \since This function is available since SDL 3.0.0.
  166. *
  167. * \sa SDL_HasAVX2
  168. * \sa SDL_HasAVX512F
  169. */
  170. extern DECLSPEC SDL_bool SDLCALL SDL_HasAVX(void);
  171. /**
  172. * Determine whether the CPU has AVX2 features.
  173. *
  174. * This always returns false on CPUs that aren't using Intel instruction sets.
  175. *
  176. * \returns SDL_TRUE if the CPU has AVX2 features or SDL_FALSE if not.
  177. *
  178. * \since This function is available since SDL 3.0.0.
  179. *
  180. * \sa SDL_HasAVX
  181. * \sa SDL_HasAVX512F
  182. */
  183. extern DECLSPEC SDL_bool SDLCALL SDL_HasAVX2(void);
  184. /**
  185. * Determine whether the CPU has AVX-512F (foundation) features.
  186. *
  187. * This always returns false on CPUs that aren't using Intel instruction sets.
  188. *
  189. * \returns SDL_TRUE if the CPU has AVX-512F features or SDL_FALSE if not.
  190. *
  191. * \since This function is available since SDL 3.0.0.
  192. *
  193. * \sa SDL_HasAVX
  194. * \sa SDL_HasAVX2
  195. */
  196. extern DECLSPEC SDL_bool SDLCALL SDL_HasAVX512F(void);
  197. /**
  198. * Determine whether the CPU has ARM SIMD (ARMv6) features.
  199. *
  200. * This is different from ARM NEON, which is a different instruction set.
  201. *
  202. * This always returns false on CPUs that aren't using ARM instruction sets.
  203. *
  204. * \returns SDL_TRUE if the CPU has ARM SIMD features or SDL_FALSE if not.
  205. *
  206. * \since This function is available since SDL 3.0.0.
  207. *
  208. * \sa SDL_HasNEON
  209. */
  210. extern DECLSPEC SDL_bool SDLCALL SDL_HasARMSIMD(void);
  211. /**
  212. * Determine whether the CPU has NEON (ARM SIMD) features.
  213. *
  214. * This always returns false on CPUs that aren't using ARM instruction sets.
  215. *
  216. * \returns SDL_TRUE if the CPU has ARM NEON features or SDL_FALSE if not.
  217. *
  218. * \since This function is available since SDL 3.0.0.
  219. */
  220. extern DECLSPEC SDL_bool SDLCALL SDL_HasNEON(void);
  221. /**
  222. * Determine whether the CPU has LSX (LOONGARCH SIMD) features.
  223. *
  224. * This always returns false on CPUs that aren't using LOONGARCH instruction
  225. * sets.
  226. *
  227. * \returns SDL_TRUE if the CPU has LOONGARCH LSX features or SDL_FALSE if
  228. * not.
  229. *
  230. * \since This function is available since SDL 3.0.0.
  231. */
  232. extern DECLSPEC SDL_bool SDLCALL SDL_HasLSX(void);
  233. /**
  234. * Determine whether the CPU has LASX (LOONGARCH SIMD) features.
  235. *
  236. * This always returns false on CPUs that aren't using LOONGARCH instruction
  237. * sets.
  238. *
  239. * \returns SDL_TRUE if the CPU has LOONGARCH LASX features or SDL_FALSE if
  240. * not.
  241. *
  242. * \since This function is available since SDL 3.0.0.
  243. */
  244. extern DECLSPEC SDL_bool SDLCALL SDL_HasLASX(void);
  245. /**
  246. * Get the amount of RAM configured in the system.
  247. *
  248. * \returns the amount of RAM configured in the system in MiB.
  249. *
  250. * \since This function is available since SDL 3.0.0.
  251. */
  252. extern DECLSPEC int SDLCALL SDL_GetSystemRAM(void);
  253. /**
  254. * Report the alignment this system needs for SIMD allocations.
  255. *
  256. * This will return the minimum number of bytes to which a pointer must be
  257. * aligned to be compatible with SIMD instructions on the current machine. For
  258. * example, if the machine supports SSE only, it will return 16, but if it
  259. * supports AVX-512F, it'll return 64 (etc). This only reports values for
  260. * instruction sets SDL knows about, so if your SDL build doesn't have
  261. * SDL_HasAVX512F(), then it might return 16 for the SSE support it sees and
  262. * not 64 for the AVX-512 instructions that exist but SDL doesn't know about.
  263. * Plan accordingly.
  264. *
  265. * \returns the alignment in bytes needed for available, known SIMD
  266. * instructions.
  267. *
  268. * \since This function is available since SDL 3.0.0.
  269. *
  270. * \sa SDL_aligned_alloc
  271. * \sa SDL_aligned_free
  272. */
  273. extern DECLSPEC size_t SDLCALL SDL_SIMDGetAlignment(void);
  274. /* Ends C function definitions when using C++ */
  275. #ifdef __cplusplus
  276. }
  277. #endif
  278. #include <SDL3/SDL_close_code.h>
  279. #endif /* SDL_cpuinfo_h_ */