SDL_cpuinfo.h 10 KB

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