SDL_cpuinfo.h 10.0 KB

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