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