SDL_cpuinfo.h 10 KB

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