SDL 3.0
SDL_cpuinfo.h
Go to the documentation of this file.
1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2023 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/**
23 * \file SDL_cpuinfo.h
24 *
25 * CPU feature detection for SDL.
26 */
27
28#ifndef SDL_cpuinfo_h_
29#define SDL_cpuinfo_h_
30
31#include <SDL3/SDL_stdinc.h>
32
33#include <SDL3/SDL_begin_code.h>
34/* Set up for C function definitions, even when using C++ */
35#ifdef __cplusplus
36extern "C" {
37#endif
38
39/* This is a guess for the cacheline size used for padding.
40 * Most x86 processors have a 64 byte cache line.
41 * The 64-bit PowerPC processors have a 128 byte cache line.
42 * We'll use the larger value to be generally safe.
43 */
44#define SDL_CACHELINE_SIZE 128
45
46/**
47 * Get the number of 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 * \since This function is available since SDL 3.0.0.
54 */
55extern DECLSPEC int SDLCALL SDL_GetCPUCount(void);
56
57/**
58 * Determine the L1 cache line size of the CPU.
59 *
60 * This is useful for determining multi-threaded structure padding or SIMD
61 * prefetch sizes.
62 *
63 * \returns the L1 cache line size of the CPU, in bytes.
64 *
65 * \since This function is available since SDL 3.0.0.
66 */
67extern DECLSPEC int SDLCALL SDL_GetCPUCacheLineSize(void);
68
69/**
70 * Determine whether the CPU has the RDTSC instruction.
71 *
72 * This always returns false on CPUs that aren't using Intel instruction sets.
73 *
74 * \returns SDL_TRUE if the CPU has the RDTSC instruction or SDL_FALSE if not.
75 *
76 * \since This function is available since SDL 3.0.0.
77 *
78 * \sa SDL_HasAltiVec
79 * \sa SDL_HasAVX
80 * \sa SDL_HasAVX2
81 * \sa SDL_HasMMX
82 * \sa SDL_HasSSE
83 * \sa SDL_HasSSE2
84 * \sa SDL_HasSSE3
85 * \sa SDL_HasSSE41
86 * \sa SDL_HasSSE42
87 */
88extern DECLSPEC SDL_bool SDLCALL SDL_HasRDTSC(void);
89
90/**
91 * Determine whether the CPU has AltiVec features.
92 *
93 * This always returns false on CPUs that aren't using PowerPC instruction
94 * sets.
95 *
96 * \returns SDL_TRUE if the CPU has AltiVec features or SDL_FALSE if not.
97 *
98 * \since This function is available since SDL 3.0.0.
99 *
100 * \sa SDL_HasAVX
101 * \sa SDL_HasAVX2
102 * \sa SDL_HasMMX
103 * \sa SDL_HasRDTSC
104 * \sa SDL_HasSSE
105 * \sa SDL_HasSSE2
106 * \sa SDL_HasSSE3
107 * \sa SDL_HasSSE41
108 * \sa SDL_HasSSE42
109 */
110extern DECLSPEC SDL_bool SDLCALL SDL_HasAltiVec(void);
111
112/**
113 * Determine whether the CPU has MMX features.
114 *
115 * This always returns false on CPUs that aren't using Intel instruction sets.
116 *
117 * \returns SDL_TRUE if the CPU has MMX features or SDL_FALSE if not.
118 *
119 * \since This function is available since SDL 3.0.0.
120 *
121 * \sa SDL_HasAltiVec
122 * \sa SDL_HasAVX
123 * \sa SDL_HasAVX2
124 * \sa SDL_HasRDTSC
125 * \sa SDL_HasSSE
126 * \sa SDL_HasSSE2
127 * \sa SDL_HasSSE3
128 * \sa SDL_HasSSE41
129 * \sa SDL_HasSSE42
130 */
131extern DECLSPEC SDL_bool SDLCALL SDL_HasMMX(void);
132
133/**
134 * Determine whether the CPU has SSE features.
135 *
136 * This always returns false on CPUs that aren't using Intel instruction sets.
137 *
138 * \returns SDL_TRUE if the CPU has SSE features or SDL_FALSE if not.
139 *
140 * \since This function is available since SDL 3.0.0.
141 *
142 * \sa SDL_HasAltiVec
143 * \sa SDL_HasAVX
144 * \sa SDL_HasAVX2
145 * \sa SDL_HasMMX
146 * \sa SDL_HasRDTSC
147 * \sa SDL_HasSSE2
148 * \sa SDL_HasSSE3
149 * \sa SDL_HasSSE41
150 * \sa SDL_HasSSE42
151 */
152extern DECLSPEC SDL_bool SDLCALL SDL_HasSSE(void);
153
154/**
155 * Determine whether the CPU has SSE2 features.
156 *
157 * This always returns false on CPUs that aren't using Intel instruction sets.
158 *
159 * \returns SDL_TRUE if the CPU has SSE2 features or SDL_FALSE if not.
160 *
161 * \since This function is available since SDL 3.0.0.
162 *
163 * \sa SDL_HasAltiVec
164 * \sa SDL_HasAVX
165 * \sa SDL_HasAVX2
166 * \sa SDL_HasMMX
167 * \sa SDL_HasRDTSC
168 * \sa SDL_HasSSE
169 * \sa SDL_HasSSE3
170 * \sa SDL_HasSSE41
171 * \sa SDL_HasSSE42
172 */
173extern DECLSPEC SDL_bool SDLCALL SDL_HasSSE2(void);
174
175/**
176 * Determine whether the CPU has SSE3 features.
177 *
178 * This always returns false on CPUs that aren't using Intel instruction sets.
179 *
180 * \returns SDL_TRUE if the CPU has SSE3 features or SDL_FALSE if not.
181 *
182 * \since This function is available since SDL 3.0.0.
183 *
184 * \sa SDL_HasAltiVec
185 * \sa SDL_HasAVX
186 * \sa SDL_HasAVX2
187 * \sa SDL_HasMMX
188 * \sa SDL_HasRDTSC
189 * \sa SDL_HasSSE
190 * \sa SDL_HasSSE2
191 * \sa SDL_HasSSE41
192 * \sa SDL_HasSSE42
193 */
194extern DECLSPEC SDL_bool SDLCALL SDL_HasSSE3(void);
195
196/**
197 * Determine whether the CPU has SSE4.1 features.
198 *
199 * This always returns false on CPUs that aren't using Intel instruction sets.
200 *
201 * \returns SDL_TRUE if the CPU has SSE4.1 features or SDL_FALSE if not.
202 *
203 * \since This function is available since SDL 3.0.0.
204 *
205 * \sa SDL_HasAltiVec
206 * \sa SDL_HasAVX
207 * \sa SDL_HasAVX2
208 * \sa SDL_HasMMX
209 * \sa SDL_HasRDTSC
210 * \sa SDL_HasSSE
211 * \sa SDL_HasSSE2
212 * \sa SDL_HasSSE3
213 * \sa SDL_HasSSE42
214 */
215extern DECLSPEC SDL_bool SDLCALL SDL_HasSSE41(void);
216
217/**
218 * Determine whether the CPU has SSE4.2 features.
219 *
220 * This always returns false on CPUs that aren't using Intel instruction sets.
221 *
222 * \returns SDL_TRUE if the CPU has SSE4.2 features or SDL_FALSE if not.
223 *
224 * \since This function is available since SDL 3.0.0.
225 *
226 * \sa SDL_HasAltiVec
227 * \sa SDL_HasAVX
228 * \sa SDL_HasAVX2
229 * \sa SDL_HasMMX
230 * \sa SDL_HasRDTSC
231 * \sa SDL_HasSSE
232 * \sa SDL_HasSSE2
233 * \sa SDL_HasSSE3
234 * \sa SDL_HasSSE41
235 */
236extern DECLSPEC SDL_bool SDLCALL SDL_HasSSE42(void);
237
238/**
239 * Determine whether the CPU has AVX features.
240 *
241 * This always returns false on CPUs that aren't using Intel instruction sets.
242 *
243 * \returns SDL_TRUE if the CPU has AVX features or SDL_FALSE if not.
244 *
245 * \since This function is available since SDL 3.0.0.
246 *
247 * \sa SDL_HasAltiVec
248 * \sa SDL_HasAVX2
249 * \sa SDL_HasMMX
250 * \sa SDL_HasRDTSC
251 * \sa SDL_HasSSE
252 * \sa SDL_HasSSE2
253 * \sa SDL_HasSSE3
254 * \sa SDL_HasSSE41
255 * \sa SDL_HasSSE42
256 */
257extern DECLSPEC SDL_bool SDLCALL SDL_HasAVX(void);
258
259/**
260 * Determine whether the CPU has AVX2 features.
261 *
262 * This always returns false on CPUs that aren't using Intel instruction sets.
263 *
264 * \returns SDL_TRUE if the CPU has AVX2 features or SDL_FALSE if not.
265 *
266 * \since This function is available since SDL 3.0.0.
267 *
268 * \sa SDL_HasAltiVec
269 * \sa SDL_HasAVX
270 * \sa SDL_HasMMX
271 * \sa SDL_HasRDTSC
272 * \sa SDL_HasSSE
273 * \sa SDL_HasSSE2
274 * \sa SDL_HasSSE3
275 * \sa SDL_HasSSE41
276 * \sa SDL_HasSSE42
277 */
278extern DECLSPEC SDL_bool SDLCALL SDL_HasAVX2(void);
279
280/**
281 * Determine whether the CPU has AVX-512F (foundation) features.
282 *
283 * This always returns false on CPUs that aren't using Intel instruction sets.
284 *
285 * \returns SDL_TRUE if the CPU has AVX-512F features or SDL_FALSE if not.
286 *
287 * \since This function is available since SDL 3.0.0.
288 *
289 * \sa SDL_HasAVX
290 */
291extern DECLSPEC SDL_bool SDLCALL SDL_HasAVX512F(void);
292
293/**
294 * Determine whether the CPU has ARM SIMD (ARMv6) features.
295 *
296 * This is different from ARM NEON, which is a different instruction set.
297 *
298 * This always returns false on CPUs that aren't using ARM instruction sets.
299 *
300 * \returns SDL_TRUE if the CPU has ARM SIMD features or SDL_FALSE if not.
301 *
302 * \since This function is available since SDL 3.0.0.
303 *
304 * \sa SDL_HasNEON
305 */
306extern DECLSPEC SDL_bool SDLCALL SDL_HasARMSIMD(void);
307
308/**
309 * Determine whether the CPU has NEON (ARM SIMD) features.
310 *
311 * This always returns false on CPUs that aren't using ARM instruction sets.
312 *
313 * \returns SDL_TRUE if the CPU has ARM NEON features or SDL_FALSE if not.
314 *
315 * \since This function is available since SDL 3.0.0.
316 */
317extern DECLSPEC SDL_bool SDLCALL SDL_HasNEON(void);
318
319/**
320 * Determine whether the CPU has LSX (LOONGARCH SIMD) features.
321 *
322 * This always returns false on CPUs that aren't using LOONGARCH instruction
323 * sets.
324 *
325 * \returns SDL_TRUE if the CPU has LOONGARCH LSX features or SDL_FALSE if
326 * not.
327 *
328 * \since This function is available since SDL 3.0.0.
329 */
330extern DECLSPEC SDL_bool SDLCALL SDL_HasLSX(void);
331
332/**
333 * Determine whether the CPU has LASX (LOONGARCH SIMD) features.
334 *
335 * This always returns false on CPUs that aren't using LOONGARCH instruction
336 * sets.
337 *
338 * \returns SDL_TRUE if the CPU has LOONGARCH LASX features or SDL_FALSE if
339 * not.
340 *
341 * \since This function is available since SDL 3.0.0.
342 */
343extern DECLSPEC SDL_bool SDLCALL SDL_HasLASX(void);
344
345/**
346 * Get the amount of RAM configured in the system.
347 *
348 * \returns the amount of RAM configured in the system in MiB.
349 *
350 * \since This function is available since SDL 3.0.0.
351 */
352extern DECLSPEC int SDLCALL SDL_GetSystemRAM(void);
353
354/**
355 * Report the alignment this system needs for SIMD allocations.
356 *
357 * This will return the minimum number of bytes to which a pointer must be
358 * aligned to be compatible with SIMD instructions on the current machine. For
359 * example, if the machine supports SSE only, it will return 16, but if it
360 * supports AVX-512F, it'll return 64 (etc). This only reports values for
361 * instruction sets SDL knows about, so if your SDL build doesn't have
362 * SDL_HasAVX512F(), then it might return 16 for the SSE support it sees and
363 * not 64 for the AVX-512 instructions that exist but SDL doesn't know about.
364 * Plan accordingly.
365 *
366 * \returns the alignment in bytes needed for available, known SIMD
367 * instructions.
368 *
369 * \since This function is available since SDL 3.0.0.
370 *
371 * \sa SDL_aligned_alloc
372 * \sa SDL_aligned_free
373 */
374extern DECLSPEC size_t SDLCALL SDL_SIMDGetAlignment(void);
375
376/* Ends C function definitions when using C++ */
377#ifdef __cplusplus
378}
379#endif
380#include <SDL3/SDL_close_code.h>
381
382#endif /* SDL_cpuinfo_h_ */
SDL_bool SDL_HasSSE2(void)
SDL_bool SDL_HasSSE42(void)
SDL_bool SDL_HasARMSIMD(void)
SDL_bool SDL_HasNEON(void)
SDL_bool SDL_HasSSE3(void)
SDL_bool SDL_HasAVX2(void)
SDL_bool SDL_HasSSE41(void)
SDL_bool SDL_HasMMX(void)
SDL_bool SDL_HasAltiVec(void)
size_t SDL_SIMDGetAlignment(void)
SDL_bool SDL_HasLASX(void)
int SDL_GetSystemRAM(void)
SDL_bool SDL_HasAVX512F(void)
int SDL_GetCPUCount(void)
SDL_bool SDL_HasAVX(void)
int SDL_GetCPUCacheLineSize(void)
SDL_bool SDL_HasRDTSC(void)
SDL_bool SDL_HasSSE(void)
SDL_bool SDL_HasLSX(void)
SDL_bool
Definition: SDL_stdinc.h:130