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.0.0.
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 * \since This function is available since SDL 3.0.0.
64 */
65extern SDL_DECLSPEC int SDLCALL SDL_GetNumLogicalCPUCores(void);
66
67/**
68 * Determine the L1 cache line size of the CPU.
69 *
70 * This is useful for determining multi-threaded structure padding or SIMD
71 * prefetch sizes.
72 *
73 * \returns the L1 cache line size of the CPU, in bytes.
74 *
75 * \since This function is available since SDL 3.0.0.
76 */
77extern SDL_DECLSPEC int SDLCALL SDL_GetCPUCacheLineSize(void);
78
79/**
80 * Determine whether the CPU has AltiVec features.
81 *
82 * This always returns false on CPUs that aren't using PowerPC instruction
83 * sets.
84 *
85 * \returns SDL_TRUE if the CPU has AltiVec features or SDL_FALSE if not.
86 *
87 * \since This function is available since SDL 3.0.0.
88 */
89extern SDL_DECLSPEC SDL_bool SDLCALL SDL_HasAltiVec(void);
90
91/**
92 * Determine whether the CPU has MMX features.
93 *
94 * This always returns false on CPUs that aren't using Intel instruction sets.
95 *
96 * \returns SDL_TRUE if the CPU has MMX features or SDL_FALSE if not.
97 *
98 * \since This function is available since SDL 3.0.0.
99 */
100extern SDL_DECLSPEC SDL_bool SDLCALL SDL_HasMMX(void);
101
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 SDL_TRUE if the CPU has SSE features or SDL_FALSE if not.
108 *
109 * \since This function is available since SDL 3.0.0.
110 *
111 * \sa SDL_HasSSE2
112 * \sa SDL_HasSSE3
113 * \sa SDL_HasSSE41
114 * \sa SDL_HasSSE42
115 */
116extern SDL_DECLSPEC SDL_bool SDLCALL SDL_HasSSE(void);
117
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 SDL_TRUE if the CPU has SSE2 features or SDL_FALSE if not.
124 *
125 * \since This function is available since SDL 3.0.0.
126 *
127 * \sa SDL_HasSSE
128 * \sa SDL_HasSSE3
129 * \sa SDL_HasSSE41
130 * \sa SDL_HasSSE42
131 */
132extern SDL_DECLSPEC SDL_bool SDLCALL SDL_HasSSE2(void);
133
134/**
135 * Determine whether the CPU has SSE3 features.
136 *
137 * This always returns false on CPUs that aren't using Intel instruction sets.
138 *
139 * \returns SDL_TRUE if the CPU has SSE3 features or SDL_FALSE if not.
140 *
141 * \since This function is available since SDL 3.0.0.
142 *
143 * \sa SDL_HasSSE
144 * \sa SDL_HasSSE2
145 * \sa SDL_HasSSE41
146 * \sa SDL_HasSSE42
147 */
148extern SDL_DECLSPEC SDL_bool SDLCALL SDL_HasSSE3(void);
149
150/**
151 * Determine whether the CPU has SSE4.1 features.
152 *
153 * This always returns false on CPUs that aren't using Intel instruction sets.
154 *
155 * \returns SDL_TRUE if the CPU has SSE4.1 features or SDL_FALSE if not.
156 *
157 * \since This function is available since SDL 3.0.0.
158 *
159 * \sa SDL_HasSSE
160 * \sa SDL_HasSSE2
161 * \sa SDL_HasSSE3
162 * \sa SDL_HasSSE42
163 */
164extern SDL_DECLSPEC SDL_bool SDLCALL SDL_HasSSE41(void);
165
166/**
167 * Determine whether the CPU has SSE4.2 features.
168 *
169 * This always returns false on CPUs that aren't using Intel instruction sets.
170 *
171 * \returns SDL_TRUE if the CPU has SSE4.2 features or SDL_FALSE if not.
172 *
173 * \since This function is available since SDL 3.0.0.
174 *
175 * \sa SDL_HasSSE
176 * \sa SDL_HasSSE2
177 * \sa SDL_HasSSE3
178 * \sa SDL_HasSSE41
179 */
180extern SDL_DECLSPEC SDL_bool SDLCALL SDL_HasSSE42(void);
181
182/**
183 * Determine whether the CPU has AVX features.
184 *
185 * This always returns false on CPUs that aren't using Intel instruction sets.
186 *
187 * \returns SDL_TRUE if the CPU has AVX features or SDL_FALSE if not.
188 *
189 * \since This function is available since SDL 3.0.0.
190 *
191 * \sa SDL_HasAVX2
192 * \sa SDL_HasAVX512F
193 */
194extern SDL_DECLSPEC SDL_bool SDLCALL SDL_HasAVX(void);
195
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 SDL_TRUE if the CPU has AVX2 features or SDL_FALSE if not.
202 *
203 * \since This function is available since SDL 3.0.0.
204 *
205 * \sa SDL_HasAVX
206 * \sa SDL_HasAVX512F
207 */
208extern SDL_DECLSPEC SDL_bool SDLCALL SDL_HasAVX2(void);
209
210/**
211 * Determine whether the CPU has AVX-512F (foundation) features.
212 *
213 * This always returns false on CPUs that aren't using Intel instruction sets.
214 *
215 * \returns SDL_TRUE if the CPU has AVX-512F features or SDL_FALSE if not.
216 *
217 * \since This function is available since SDL 3.0.0.
218 *
219 * \sa SDL_HasAVX
220 * \sa SDL_HasAVX2
221 */
222extern SDL_DECLSPEC SDL_bool SDLCALL SDL_HasAVX512F(void);
223
224/**
225 * Determine whether the CPU has ARM SIMD (ARMv6) features.
226 *
227 * This is different from ARM NEON, which is a different instruction set.
228 *
229 * This always returns false on CPUs that aren't using ARM instruction sets.
230 *
231 * \returns SDL_TRUE if the CPU has ARM SIMD features or SDL_FALSE if not.
232 *
233 * \since This function is available since SDL 3.0.0.
234 *
235 * \sa SDL_HasNEON
236 */
237extern SDL_DECLSPEC SDL_bool SDLCALL SDL_HasARMSIMD(void);
238
239/**
240 * Determine whether the CPU has NEON (ARM SIMD) features.
241 *
242 * This always returns false on CPUs that aren't using ARM instruction sets.
243 *
244 * \returns SDL_TRUE if the CPU has ARM NEON features or SDL_FALSE if not.
245 *
246 * \since This function is available since SDL 3.0.0.
247 */
248extern SDL_DECLSPEC SDL_bool SDLCALL SDL_HasNEON(void);
249
250/**
251 * Determine whether the CPU has LSX (LOONGARCH SIMD) features.
252 *
253 * This always returns false on CPUs that aren't using LOONGARCH instruction
254 * sets.
255 *
256 * \returns SDL_TRUE if the CPU has LOONGARCH LSX features or SDL_FALSE if
257 * not.
258 *
259 * \since This function is available since SDL 3.0.0.
260 */
261extern SDL_DECLSPEC SDL_bool SDLCALL SDL_HasLSX(void);
262
263/**
264 * Determine whether the CPU has LASX (LOONGARCH SIMD) features.
265 *
266 * This always returns false on CPUs that aren't using LOONGARCH instruction
267 * sets.
268 *
269 * \returns SDL_TRUE if the CPU has LOONGARCH LASX features or SDL_FALSE if
270 * not.
271 *
272 * \since This function is available since SDL 3.0.0.
273 */
274extern SDL_DECLSPEC SDL_bool SDLCALL SDL_HasLASX(void);
275
276/**
277 * Get the amount of RAM configured in the system.
278 *
279 * \returns the amount of RAM configured in the system in MiB.
280 *
281 * \since This function is available since SDL 3.0.0.
282 */
283extern SDL_DECLSPEC int SDLCALL SDL_GetSystemRAM(void);
284
285/**
286 * Report the alignment this system needs for SIMD allocations.
287 *
288 * This will return the minimum number of bytes to which a pointer must be
289 * aligned to be compatible with SIMD instructions on the current machine. For
290 * example, if the machine supports SSE only, it will return 16, but if it
291 * supports AVX-512F, it'll return 64 (etc). This only reports values for
292 * instruction sets SDL knows about, so if your SDL build doesn't have
293 * SDL_HasAVX512F(), then it might return 16 for the SSE support it sees and
294 * not 64 for the AVX-512 instructions that exist but SDL doesn't know about.
295 * Plan accordingly.
296 *
297 * \returns the alignment in bytes needed for available, known SIMD
298 * instructions.
299 *
300 * \since This function is available since SDL 3.0.0.
301 *
302 * \sa SDL_aligned_alloc
303 * \sa SDL_aligned_free
304 */
305extern SDL_DECLSPEC size_t SDLCALL SDL_GetSIMDAlignment(void);
306
307/* Ends C function definitions when using C++ */
308#ifdef __cplusplus
309}
310#endif
311#include <SDL3/SDL_close_code.h>
312
313#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)
SDL_bool SDL_HasLASX(void)
int SDL_GetSystemRAM(void)
SDL_bool SDL_HasAVX512F(void)
SDL_bool SDL_HasAVX(void)
int SDL_GetCPUCacheLineSize(void)
SDL_bool SDL_HasSSE(void)
SDL_bool SDL_HasLSX(void)
size_t SDL_GetSIMDAlignment(void)
int SDL_GetNumLogicalCPUCores(void)
bool SDL_bool
Definition SDL_stdinc.h:301