SDL 3.0
SDL_cpuinfo.h
Go to the documentation of this file.
1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
4
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
20*/
21
22/* WIKI CATEGORY: CPUInfo */
23
24/**
25 * # CategoryCPUInfo
26 *
27 * CPU feature detection for SDL.
28 *
29 * These functions are largely concerned with reporting if the system has
30 * access to various SIMD instruction sets, but also has other important info
31 * to share, such as system RAM size and number of logical CPU cores.
32 */
33
34#ifndef SDL_cpuinfo_h_
35#define SDL_cpuinfo_h_
36
37#include <SDL3/SDL_stdinc.h>
38
39#include <SDL3/SDL_begin_code.h>
40/* Set up for C function definitions, even when using C++ */
41#ifdef __cplusplus
42extern "C" {
43#endif
44
45/**
46 * A guess for the cacheline size used for padding.
47 *
48 * Most x86 processors have a 64 byte cache line. The 64-bit PowerPC
49 * processors have a 128 byte cache line. We use the larger value to be
50 * generally safe.
51 *
52 * \since This macro is available since SDL 3.1.3.
53 */
54#define SDL_CACHELINE_SIZE 128
55
56/**
57 * Get the number of logical CPU cores available.
58 *
59 * \returns the total number of logical CPU cores. On CPUs that include
60 * technologies such as hyperthreading, the number of logical cores
61 * may be more than the number of physical cores.
62 *
63 * \threadsafety It is safe to call this function from any thread.
64 *
65 * \since This function is available since SDL 3.1.3.
66 */
67extern SDL_DECLSPEC int SDLCALL SDL_GetNumLogicalCPUCores(void);
68
69/**
70 * Determine the L1 cache line size of the CPU.
71 *
72 * This is useful for determining multi-threaded structure padding or SIMD
73 * prefetch sizes.
74 *
75 * \returns the L1 cache line size of the CPU, in bytes.
76 *
77 * \threadsafety It is safe to call this function from any thread.
78 *
79 * \since This function is available since SDL 3.1.3.
80 */
81extern SDL_DECLSPEC int SDLCALL SDL_GetCPUCacheLineSize(void);
82
83/**
84 * Determine whether the CPU has AltiVec features.
85 *
86 * This always returns false on CPUs that aren't using PowerPC instruction
87 * sets.
88 *
89 * \returns true if the CPU has AltiVec 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 */
95extern SDL_DECLSPEC bool SDLCALL SDL_HasAltiVec(void);
96
97/**
98 * Determine whether the CPU has MMX features.
99 *
100 * This always returns false on CPUs that aren't using Intel instruction sets.
101 *
102 * \returns true if the CPU has MMX features or false if not.
103 *
104 * \threadsafety It is safe to call this function from any thread.
105 *
106 * \since This function is available since SDL 3.1.3.
107 */
108extern SDL_DECLSPEC bool SDLCALL SDL_HasMMX(void);
109
110/**
111 * Determine whether the CPU has SSE features.
112 *
113 * This always returns false on CPUs that aren't using Intel instruction sets.
114 *
115 * \returns true if the CPU has SSE features or false if not.
116 *
117 * \threadsafety It is safe to call this function from any thread.
118 *
119 * \since This function is available since SDL 3.1.3.
120 *
121 * \sa SDL_HasSSE2
122 * \sa SDL_HasSSE3
123 * \sa SDL_HasSSE41
124 * \sa SDL_HasSSE42
125 */
126extern SDL_DECLSPEC bool SDLCALL SDL_HasSSE(void);
127
128/**
129 * Determine whether the CPU has SSE2 features.
130 *
131 * This always returns false on CPUs that aren't using Intel instruction sets.
132 *
133 * \returns true if the CPU has SSE2 features or false if not.
134 *
135 * \threadsafety It is safe to call this function from any thread.
136 *
137 * \since This function is available since SDL 3.1.3.
138 *
139 * \sa SDL_HasSSE
140 * \sa SDL_HasSSE3
141 * \sa SDL_HasSSE41
142 * \sa SDL_HasSSE42
143 */
144extern SDL_DECLSPEC bool SDLCALL SDL_HasSSE2(void);
145
146/**
147 * Determine whether the CPU has SSE3 features.
148 *
149 * This always returns false on CPUs that aren't using Intel instruction sets.
150 *
151 * \returns true if the CPU has SSE3 features or false if not.
152 *
153 * \threadsafety It is safe to call this function from any thread.
154 *
155 * \since This function is available since SDL 3.1.3.
156 *
157 * \sa SDL_HasSSE
158 * \sa SDL_HasSSE2
159 * \sa SDL_HasSSE41
160 * \sa SDL_HasSSE42
161 */
162extern SDL_DECLSPEC bool SDLCALL SDL_HasSSE3(void);
163
164/**
165 * Determine whether the CPU has SSE4.1 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.1 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_HasSSE42
179 */
180extern SDL_DECLSPEC bool SDLCALL SDL_HasSSE41(void);
181
182/**
183 * Determine whether the CPU has SSE4.2 features.
184 *
185 * This always returns false on CPUs that aren't using Intel instruction sets.
186 *
187 * \returns true if the CPU has SSE4.2 features or false if not.
188 *
189 * \threadsafety It is safe to call this function from any thread.
190 *
191 * \since This function is available since SDL 3.1.3.
192 *
193 * \sa SDL_HasSSE
194 * \sa SDL_HasSSE2
195 * \sa SDL_HasSSE3
196 * \sa SDL_HasSSE41
197 */
198extern SDL_DECLSPEC bool SDLCALL SDL_HasSSE42(void);
199
200/**
201 * Determine whether the CPU has AVX features.
202 *
203 * This always returns false on CPUs that aren't using Intel instruction sets.
204 *
205 * \returns true if the CPU has AVX features or false if not.
206 *
207 * \threadsafety It is safe to call this function from any thread.
208 *
209 * \since This function is available since SDL 3.1.3.
210 *
211 * \sa SDL_HasAVX2
212 * \sa SDL_HasAVX512F
213 */
214extern SDL_DECLSPEC bool SDLCALL SDL_HasAVX(void);
215
216/**
217 * Determine whether the CPU has AVX2 features.
218 *
219 * This always returns false on CPUs that aren't using Intel instruction sets.
220 *
221 * \returns true if the CPU has AVX2 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_HasAVX512F
229 */
230extern SDL_DECLSPEC bool SDLCALL SDL_HasAVX2(void);
231
232/**
233 * Determine whether the CPU has AVX-512F (foundation) features.
234 *
235 * This always returns false on CPUs that aren't using Intel instruction sets.
236 *
237 * \returns true if the CPU has AVX-512F features or false if not.
238 *
239 * \threadsafety It is safe to call this function from any thread.
240 *
241 * \since This function is available since SDL 3.1.3.
242 *
243 * \sa SDL_HasAVX
244 * \sa SDL_HasAVX2
245 */
246extern SDL_DECLSPEC bool SDLCALL SDL_HasAVX512F(void);
247
248/**
249 * Determine whether the CPU has ARM SIMD (ARMv6) features.
250 *
251 * This is different from ARM NEON, which is a different instruction set.
252 *
253 * This always returns false on CPUs that aren't using ARM instruction sets.
254 *
255 * \returns true if the CPU has ARM SIMD features or false if not.
256 *
257 * \threadsafety It is safe to call this function from any thread.
258 *
259 * \since This function is available since SDL 3.1.3.
260 *
261 * \sa SDL_HasNEON
262 */
263extern SDL_DECLSPEC bool SDLCALL SDL_HasARMSIMD(void);
264
265/**
266 * Determine whether the CPU has NEON (ARM SIMD) features.
267 *
268 * This always returns false on CPUs that aren't using ARM instruction sets.
269 *
270 * \returns true if the CPU has ARM NEON features or false if not.
271 *
272 * \threadsafety It is safe to call this function from any thread.
273 *
274 * \since This function is available since SDL 3.1.3.
275 */
276extern SDL_DECLSPEC bool SDLCALL SDL_HasNEON(void);
277
278/**
279 * Determine whether the CPU has LSX (LOONGARCH SIMD) features.
280 *
281 * This always returns false on CPUs that aren't using LOONGARCH instruction
282 * sets.
283 *
284 * \returns true if the CPU has LOONGARCH LSX features or false if not.
285 *
286 * \threadsafety It is safe to call this function from any thread.
287 *
288 * \since This function is available since SDL 3.1.3.
289 */
290extern SDL_DECLSPEC bool SDLCALL SDL_HasLSX(void);
291
292/**
293 * Determine whether the CPU has LASX (LOONGARCH SIMD) features.
294 *
295 * This always returns false on CPUs that aren't using LOONGARCH instruction
296 * sets.
297 *
298 * \returns true if the CPU has LOONGARCH LASX features or false if not.
299 *
300 * \threadsafety It is safe to call this function from any thread.
301 *
302 * \since This function is available since SDL 3.1.3.
303 */
304extern SDL_DECLSPEC bool SDLCALL SDL_HasLASX(void);
305
306/**
307 * Get the amount of RAM configured in the system.
308 *
309 * \returns the amount of RAM configured in the system in MiB.
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 */
315extern SDL_DECLSPEC int SDLCALL SDL_GetSystemRAM(void);
316
317/**
318 * Report the alignment this system needs for SIMD allocations.
319 *
320 * This will return the minimum number of bytes to which a pointer must be
321 * aligned to be compatible with SIMD instructions on the current machine. For
322 * example, if the machine supports SSE only, it will return 16, but if it
323 * supports AVX-512F, it'll return 64 (etc). This only reports values for
324 * instruction sets SDL knows about, so if your SDL build doesn't have
325 * SDL_HasAVX512F(), then it might return 16 for the SSE support it sees and
326 * not 64 for the AVX-512 instructions that exist but SDL doesn't know about.
327 * Plan accordingly.
328 *
329 * \returns the alignment in bytes needed for available, known SIMD
330 * instructions.
331 *
332 * \threadsafety It is safe to call this function from any thread.
333 *
334 * \since This function is available since SDL 3.1.3.
335 *
336 * \sa SDL_aligned_alloc
337 * \sa SDL_aligned_free
338 */
339extern SDL_DECLSPEC size_t SDLCALL SDL_GetSIMDAlignment(void);
340
341/* Ends C function definitions when using C++ */
342#ifdef __cplusplus
343}
344#endif
345#include <SDL3/SDL_close_code.h>
346
347#endif /* SDL_cpuinfo_h_ */
bool SDL_HasSSE2(void)
bool SDL_HasSSE42(void)
bool SDL_HasAltiVec(void)
bool SDL_HasAVX512F(void)
int SDL_GetSystemRAM(void)
bool SDL_HasSSE(void)
bool SDL_HasARMSIMD(void)
bool SDL_HasLSX(void)
bool SDL_HasNEON(void)
int SDL_GetCPUCacheLineSize(void)
bool SDL_HasLASX(void)
bool SDL_HasSSE3(void)
bool SDL_HasAVX(void)
bool SDL_HasAVX2(void)
bool SDL_HasSSE41(void)
size_t SDL_GetSIMDAlignment(void)
bool SDL_HasMMX(void)
int SDL_GetNumLogicalCPUCores(void)