SDL 2.0
SDL_cpuinfo.h
Go to the documentation of this file.
1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2025 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 number of logical CPU cores.
32 */
33
34#ifndef SDL_cpuinfo_h_
35#define SDL_cpuinfo_h_
36
37#include "SDL_stdinc.h"
38
39/* Need to do this here because intrin.h has C++ code in it */
40/* Visual Studio 2005 has a bug where intrin.h conflicts with winnt.h */
41#if defined(_MSC_VER) && (_MSC_VER >= 1500) && (defined(_M_IX86) || defined(_M_X64))
42#ifdef __clang__
43/* As of Clang 11, '_m_prefetchw' is conflicting with the winnt.h's version,
44 so we define the needed '_m_prefetch' here as a pseudo-header, until the issue is fixed. */
45
46#ifndef __PRFCHWINTRIN_H
47#define __PRFCHWINTRIN_H
48
49static __inline__ void __attribute__((__always_inline__, __nodebug__))
50_m_prefetch(void *__P)
51{
52 __builtin_prefetch (__P, 0, 3 /* _MM_HINT_T0 */);
53}
54
55#endif /* __PRFCHWINTRIN_H */
56#endif /* __clang__ */
57#include <intrin.h>
58#ifndef _WIN64
59#ifndef __MMX__
60#define __MMX__
61#endif
62/*
63#ifndef __3dNOW__
64#define __3dNOW__
65#endif
66*/
67#endif
68#ifndef __SSE__
69#define __SSE__
70#endif
71#ifndef __SSE2__
72#define __SSE2__
73#endif
74#ifndef __SSE3__
75#define __SSE3__
76#endif
77#elif defined(__MINGW64_VERSION_MAJOR)
78#include <intrin.h>
79#if !defined(SDL_DISABLE_ARM_NEON_H) && defined(__ARM_NEON)
80# include <arm_neon.h>
81#endif
82#else
83/* altivec.h redefining bool causes a number of problems, see bugs 3993 and 4392, so you need to explicitly define SDL_ENABLE_ALTIVEC_H to have it included. */
84#if defined(HAVE_ALTIVEC_H) && defined(__ALTIVEC__) && !defined(__APPLE_ALTIVEC__) && defined(SDL_ENABLE_ALTIVEC_H)
85#include <altivec.h>
86#endif
87#if !defined(SDL_DISABLE_ARM_NEON_H)
88# if defined(__ARM_NEON)
89# include <arm_neon.h>
90# elif defined(__WINDOWS__) || defined(__WINRT__) || defined(__GDK__)
91/* Visual Studio doesn't define __ARM_ARCH, but _M_ARM (if set, always 7), and _M_ARM64 (if set, always 1). */
92# if defined(_M_ARM)
93# include <armintr.h>
94# include <arm_neon.h>
95# define __ARM_NEON 1 /* Set __ARM_NEON so that it can be used elsewhere, at compile time */
96# endif
97# if defined (_M_ARM64)
98# include <arm64intr.h>
99# include <arm64_neon.h>
100# define __ARM_NEON 1 /* Set __ARM_NEON so that it can be used elsewhere, at compile time */
101# define __ARM_ARCH 8
102# endif
103# endif
104#endif
105#endif /* compiler version */
106
107#if defined(__3dNOW__) && !defined(SDL_DISABLE_MM3DNOW_H)
108#include <mm3dnow.h>
109#endif
110#if defined(__loongarch_sx) && !defined(SDL_DISABLE_LSX_H)
111#include <lsxintrin.h>
112#define __LSX__
113#endif
114#if defined(__loongarch_asx) && !defined(SDL_DISABLE_LASX_H)
115#include <lasxintrin.h>
116#define __LASX__
117#endif
118#if defined(HAVE_IMMINTRIN_H) && !defined(SDL_DISABLE_IMMINTRIN_H)
119#include <immintrin.h>
120#else
121#if defined(__MMX__) && !defined(SDL_DISABLE_MMINTRIN_H)
122#include <mmintrin.h>
123#endif
124#if defined(__SSE__) && !defined(SDL_DISABLE_XMMINTRIN_H)
125#include <xmmintrin.h>
126#endif
127#if defined(__SSE2__) && !defined(SDL_DISABLE_EMMINTRIN_H)
128#include <emmintrin.h>
129#endif
130#if defined(__SSE3__) && !defined(SDL_DISABLE_PMMINTRIN_H)
131#include <pmmintrin.h>
132#endif
133#endif /* HAVE_IMMINTRIN_H */
134
135#include "begin_code.h"
136/* Set up for C function definitions, even when using C++ */
137#ifdef __cplusplus
138extern "C" {
139#endif
140
141/* This is a guess for the cacheline size used for padding.
142 * Most x86 processors have a 64 byte cache line.
143 * The 64-bit PowerPC processors have a 128 byte cache line.
144 * We'll use the larger value to be generally safe.
145 */
146#define SDL_CACHELINE_SIZE 128
147
148/**
149 * Get the number of CPU cores available.
150 *
151 * \returns the total number of logical CPU cores. On CPUs that include
152 * technologies such as hyperthreading, the number of logical cores
153 * may be more than the number of physical cores.
154 *
155 * \since This function is available since SDL 2.0.0.
156 */
157extern DECLSPEC int SDLCALL SDL_GetCPUCount(void);
158
159/**
160 * Determine the L1 cache line size of the CPU.
161 *
162 * This is useful for determining multi-threaded structure padding or SIMD
163 * prefetch sizes.
164 *
165 * \returns the L1 cache line size of the CPU, in bytes.
166 *
167 * \since This function is available since SDL 2.0.0.
168 */
169extern DECLSPEC int SDLCALL SDL_GetCPUCacheLineSize(void);
170
171/**
172 * Determine whether the CPU has the RDTSC instruction.
173 *
174 * This always returns false on CPUs that aren't using Intel instruction sets.
175 *
176 * \returns SDL_TRUE if the CPU has the RDTSC instruction or SDL_FALSE if not.
177 *
178 * \since This function is available since SDL 2.0.0.
179 *
180 * \sa SDL_Has3DNow
181 * \sa SDL_HasAltiVec
182 * \sa SDL_HasAVX
183 * \sa SDL_HasAVX2
184 * \sa SDL_HasMMX
185 * \sa SDL_HasSSE
186 * \sa SDL_HasSSE2
187 * \sa SDL_HasSSE3
188 * \sa SDL_HasSSE41
189 * \sa SDL_HasSSE42
190 */
191extern DECLSPEC SDL_bool SDLCALL SDL_HasRDTSC(void);
192
193/**
194 * Determine whether the CPU has AltiVec features.
195 *
196 * This always returns false on CPUs that aren't using PowerPC instruction
197 * sets.
198 *
199 * \returns SDL_TRUE if the CPU has AltiVec features or SDL_FALSE if not.
200 *
201 * \since This function is available since SDL 2.0.0.
202 *
203 * \sa SDL_Has3DNow
204 * \sa SDL_HasAVX
205 * \sa SDL_HasAVX2
206 * \sa SDL_HasMMX
207 * \sa SDL_HasRDTSC
208 * \sa SDL_HasSSE
209 * \sa SDL_HasSSE2
210 * \sa SDL_HasSSE3
211 * \sa SDL_HasSSE41
212 * \sa SDL_HasSSE42
213 */
214extern DECLSPEC SDL_bool SDLCALL SDL_HasAltiVec(void);
215
216/**
217 * Determine whether the CPU has MMX features.
218 *
219 * This always returns false on CPUs that aren't using Intel instruction sets.
220 *
221 * \returns SDL_TRUE if the CPU has MMX features or SDL_FALSE if not.
222 *
223 * \since This function is available since SDL 2.0.0.
224 *
225 * \sa SDL_Has3DNow
226 * \sa SDL_HasAltiVec
227 * \sa SDL_HasAVX
228 * \sa SDL_HasAVX2
229 * \sa SDL_HasRDTSC
230 * \sa SDL_HasSSE
231 * \sa SDL_HasSSE2
232 * \sa SDL_HasSSE3
233 * \sa SDL_HasSSE41
234 * \sa SDL_HasSSE42
235 */
236extern DECLSPEC SDL_bool SDLCALL SDL_HasMMX(void);
237
238/**
239 * Determine whether the CPU has 3DNow! features.
240 *
241 * This always returns false on CPUs that aren't using AMD instruction sets.
242 *
243 * \returns SDL_TRUE if the CPU has 3DNow! features or SDL_FALSE if not.
244 *
245 * \since This function is available since SDL 2.0.0.
246 *
247 * \sa SDL_HasAltiVec
248 * \sa SDL_HasAVX
249 * \sa SDL_HasAVX2
250 * \sa SDL_HasMMX
251 * \sa SDL_HasRDTSC
252 * \sa SDL_HasSSE
253 * \sa SDL_HasSSE2
254 * \sa SDL_HasSSE3
255 * \sa SDL_HasSSE41
256 * \sa SDL_HasSSE42
257 */
258extern DECLSPEC SDL_bool SDLCALL SDL_Has3DNow(void);
259
260/**
261 * Determine whether the CPU has SSE features.
262 *
263 * This always returns false on CPUs that aren't using Intel instruction sets.
264 *
265 * \returns SDL_TRUE if the CPU has SSE features or SDL_FALSE if not.
266 *
267 * \since This function is available since SDL 2.0.0.
268 *
269 * \sa SDL_Has3DNow
270 * \sa SDL_HasAltiVec
271 * \sa SDL_HasAVX
272 * \sa SDL_HasAVX2
273 * \sa SDL_HasMMX
274 * \sa SDL_HasRDTSC
275 * \sa SDL_HasSSE2
276 * \sa SDL_HasSSE3
277 * \sa SDL_HasSSE41
278 * \sa SDL_HasSSE42
279 */
280extern DECLSPEC SDL_bool SDLCALL SDL_HasSSE(void);
281
282/**
283 * Determine whether the CPU has SSE2 features.
284 *
285 * This always returns false on CPUs that aren't using Intel instruction sets.
286 *
287 * \returns SDL_TRUE if the CPU has SSE2 features or SDL_FALSE if not.
288 *
289 * \since This function is available since SDL 2.0.0.
290 *
291 * \sa SDL_Has3DNow
292 * \sa SDL_HasAltiVec
293 * \sa SDL_HasAVX
294 * \sa SDL_HasAVX2
295 * \sa SDL_HasMMX
296 * \sa SDL_HasRDTSC
297 * \sa SDL_HasSSE
298 * \sa SDL_HasSSE3
299 * \sa SDL_HasSSE41
300 * \sa SDL_HasSSE42
301 */
302extern DECLSPEC SDL_bool SDLCALL SDL_HasSSE2(void);
303
304/**
305 * Determine whether the CPU has SSE3 features.
306 *
307 * This always returns false on CPUs that aren't using Intel instruction sets.
308 *
309 * \returns SDL_TRUE if the CPU has SSE3 features or SDL_FALSE if not.
310 *
311 * \since This function is available since SDL 2.0.0.
312 *
313 * \sa SDL_Has3DNow
314 * \sa SDL_HasAltiVec
315 * \sa SDL_HasAVX
316 * \sa SDL_HasAVX2
317 * \sa SDL_HasMMX
318 * \sa SDL_HasRDTSC
319 * \sa SDL_HasSSE
320 * \sa SDL_HasSSE2
321 * \sa SDL_HasSSE41
322 * \sa SDL_HasSSE42
323 */
324extern DECLSPEC SDL_bool SDLCALL SDL_HasSSE3(void);
325
326/**
327 * Determine whether the CPU has SSE4.1 features.
328 *
329 * This always returns false on CPUs that aren't using Intel instruction sets.
330 *
331 * \returns SDL_TRUE if the CPU has SSE4.1 features or SDL_FALSE if not.
332 *
333 * \since This function is available since SDL 2.0.0.
334 *
335 * \sa SDL_Has3DNow
336 * \sa SDL_HasAltiVec
337 * \sa SDL_HasAVX
338 * \sa SDL_HasAVX2
339 * \sa SDL_HasMMX
340 * \sa SDL_HasRDTSC
341 * \sa SDL_HasSSE
342 * \sa SDL_HasSSE2
343 * \sa SDL_HasSSE3
344 * \sa SDL_HasSSE42
345 */
346extern DECLSPEC SDL_bool SDLCALL SDL_HasSSE41(void);
347
348/**
349 * Determine whether the CPU has SSE4.2 features.
350 *
351 * This always returns false on CPUs that aren't using Intel instruction sets.
352 *
353 * \returns SDL_TRUE if the CPU has SSE4.2 features or SDL_FALSE if not.
354 *
355 * \since This function is available since SDL 2.0.0.
356 *
357 * \sa SDL_Has3DNow
358 * \sa SDL_HasAltiVec
359 * \sa SDL_HasAVX
360 * \sa SDL_HasAVX2
361 * \sa SDL_HasMMX
362 * \sa SDL_HasRDTSC
363 * \sa SDL_HasSSE
364 * \sa SDL_HasSSE2
365 * \sa SDL_HasSSE3
366 * \sa SDL_HasSSE41
367 */
368extern DECLSPEC SDL_bool SDLCALL SDL_HasSSE42(void);
369
370/**
371 * Determine whether the CPU has AVX features.
372 *
373 * This always returns false on CPUs that aren't using Intel instruction sets.
374 *
375 * \returns SDL_TRUE if the CPU has AVX features or SDL_FALSE if not.
376 *
377 * \since This function is available since SDL 2.0.2.
378 *
379 * \sa SDL_Has3DNow
380 * \sa SDL_HasAltiVec
381 * \sa SDL_HasAVX2
382 * \sa SDL_HasMMX
383 * \sa SDL_HasRDTSC
384 * \sa SDL_HasSSE
385 * \sa SDL_HasSSE2
386 * \sa SDL_HasSSE3
387 * \sa SDL_HasSSE41
388 * \sa SDL_HasSSE42
389 */
390extern DECLSPEC SDL_bool SDLCALL SDL_HasAVX(void);
391
392/**
393 * Determine whether the CPU has AVX2 features.
394 *
395 * This always returns false on CPUs that aren't using Intel instruction sets.
396 *
397 * \returns SDL_TRUE if the CPU has AVX2 features or SDL_FALSE if not.
398 *
399 * \since This function is available since SDL 2.0.4.
400 *
401 * \sa SDL_Has3DNow
402 * \sa SDL_HasAltiVec
403 * \sa SDL_HasAVX
404 * \sa SDL_HasMMX
405 * \sa SDL_HasRDTSC
406 * \sa SDL_HasSSE
407 * \sa SDL_HasSSE2
408 * \sa SDL_HasSSE3
409 * \sa SDL_HasSSE41
410 * \sa SDL_HasSSE42
411 */
412extern DECLSPEC SDL_bool SDLCALL SDL_HasAVX2(void);
413
414/**
415 * Determine whether the CPU has AVX-512F (foundation) features.
416 *
417 * This always returns false on CPUs that aren't using Intel instruction sets.
418 *
419 * \returns SDL_TRUE if the CPU has AVX-512F features or SDL_FALSE if not.
420 *
421 * \since This function is available since SDL 2.0.9.
422 *
423 * \sa SDL_HasAVX
424 */
425extern DECLSPEC SDL_bool SDLCALL SDL_HasAVX512F(void);
426
427/**
428 * Determine whether the CPU has ARM SIMD (ARMv6) features.
429 *
430 * This is different from ARM NEON, which is a different instruction set.
431 *
432 * This always returns false on CPUs that aren't using ARM instruction sets.
433 *
434 * \returns SDL_TRUE if the CPU has ARM SIMD features or SDL_FALSE if not.
435 *
436 * \since This function is available since SDL 2.0.12.
437 *
438 * \sa SDL_HasNEON
439 */
440extern DECLSPEC SDL_bool SDLCALL SDL_HasARMSIMD(void);
441
442/**
443 * Determine whether the CPU has NEON (ARM SIMD) features.
444 *
445 * This always returns false on CPUs that aren't using ARM instruction sets.
446 *
447 * \returns SDL_TRUE if the CPU has ARM NEON features or SDL_FALSE if not.
448 *
449 * \since This function is available since SDL 2.0.6.
450 */
451extern DECLSPEC SDL_bool SDLCALL SDL_HasNEON(void);
452
453/**
454 * Determine whether the CPU has LSX (LOONGARCH SIMD) features.
455 *
456 * This always returns false on CPUs that aren't using LOONGARCH instruction
457 * sets.
458 *
459 * \returns SDL_TRUE if the CPU has LOONGARCH LSX features or SDL_FALSE if
460 * not.
461 *
462 * \since This function is available since SDL 2.24.0.
463 */
464extern DECLSPEC SDL_bool SDLCALL SDL_HasLSX(void);
465
466/**
467 * Determine whether the CPU has LASX (LOONGARCH SIMD) features.
468 *
469 * This always returns false on CPUs that aren't using LOONGARCH instruction
470 * sets.
471 *
472 * \returns SDL_TRUE if the CPU has LOONGARCH LASX features or SDL_FALSE if
473 * not.
474 *
475 * \since This function is available since SDL 2.24.0.
476 */
477extern DECLSPEC SDL_bool SDLCALL SDL_HasLASX(void);
478
479/**
480 * Get the amount of RAM configured in the system.
481 *
482 * \returns the amount of RAM configured in the system in MiB.
483 *
484 * \since This function is available since SDL 2.0.1.
485 */
486extern DECLSPEC int SDLCALL SDL_GetSystemRAM(void);
487
488/**
489 * Report the alignment this system needs for SIMD allocations.
490 *
491 * This will return the minimum number of bytes to which a pointer must be
492 * aligned to be compatible with SIMD instructions on the current machine. For
493 * example, if the machine supports SSE only, it will return 16, but if it
494 * supports AVX-512F, it'll return 64 (etc). This only reports values for
495 * instruction sets SDL knows about, so if your SDL build doesn't have
496 * SDL_HasAVX512F(), then it might return 16 for the SSE support it sees and
497 * not 64 for the AVX-512 instructions that exist but SDL doesn't know about.
498 * Plan accordingly.
499 *
500 * \returns the alignment in bytes needed for available, known SIMD
501 * instructions.
502 *
503 * \since This function is available since SDL 2.0.10.
504 */
505extern DECLSPEC size_t SDLCALL SDL_SIMDGetAlignment(void);
506
507/**
508 * Allocate memory in a SIMD-friendly way.
509 *
510 * This will allocate a block of memory that is suitable for use with SIMD
511 * instructions. Specifically, it will be properly aligned and padded for the
512 * system's supported vector instructions.
513 *
514 * The memory returned will be padded such that it is safe to read or write an
515 * incomplete vector at the end of the memory block. This can be useful so you
516 * don't have to drop back to a scalar fallback at the end of your SIMD
517 * processing loop to deal with the final elements without overflowing the
518 * allocated buffer.
519 *
520 * You must free this memory with SDL_FreeSIMD(), not free() or SDL_free() or
521 * delete[], etc.
522 *
523 * Note that SDL will only deal with SIMD instruction sets it is aware of; for
524 * example, SDL 2.0.8 knows that SSE wants 16-byte vectors (SDL_HasSSE()), and
525 * AVX2 wants 32 bytes (SDL_HasAVX2()), but doesn't know that AVX-512 wants
526 * 64. To be clear: if you can't decide to use an instruction set with an
527 * SDL_Has*() function, don't use that instruction set with memory allocated
528 * through here.
529 *
530 * SDL_AllocSIMD(0) will return a non-NULL pointer, assuming the system isn't
531 * out of memory, but you are not allowed to dereference it (because you only
532 * own zero bytes of that buffer).
533 *
534 * \param len The length, in bytes, of the block to allocate. The actual
535 * allocated block might be larger due to padding, etc.
536 * \returns a pointer to the newly-allocated block, NULL if out of memory.
537 *
538 * \since This function is available since SDL 2.0.10.
539 *
540 * \sa SDL_SIMDGetAlignment
541 * \sa SDL_SIMDRealloc
542 * \sa SDL_SIMDFree
543 */
544extern DECLSPEC void * SDLCALL SDL_SIMDAlloc(const size_t len);
545
546/**
547 * Reallocate memory obtained from SDL_SIMDAlloc
548 *
549 * It is not valid to use this function on a pointer from anything but
550 * SDL_SIMDAlloc(). It can't be used on pointers from malloc, realloc,
551 * SDL_malloc, memalign, new[], etc.
552 *
553 * \param mem The pointer obtained from SDL_SIMDAlloc. This function also
554 * accepts NULL, at which point this function is the same as
555 * calling SDL_SIMDAlloc with a NULL pointer.
556 * \param len The length, in bytes, of the block to allocated. The actual
557 * allocated block might be larger due to padding, etc. Passing 0
558 * will return a non-NULL pointer, assuming the system isn't out of
559 * memory.
560 * \returns a pointer to the newly-reallocated block, NULL if out of memory.
561 *
562 * \since This function is available since SDL 2.0.14.
563 *
564 * \sa SDL_SIMDGetAlignment
565 * \sa SDL_SIMDAlloc
566 * \sa SDL_SIMDFree
567 */
568extern DECLSPEC void * SDLCALL SDL_SIMDRealloc(void *mem, const size_t len);
569
570/**
571 * Deallocate memory obtained from SDL_SIMDAlloc
572 *
573 * It is not valid to use this function on a pointer from anything but
574 * SDL_SIMDAlloc() or SDL_SIMDRealloc(). It can't be used on pointers from
575 * malloc, realloc, SDL_malloc, memalign, new[], etc.
576 *
577 * However, SDL_SIMDFree(NULL) is a legal no-op.
578 *
579 * The memory pointed to by `ptr` is no longer valid for access upon return,
580 * and may be returned to the system or reused by a future allocation. The
581 * pointer passed to this function is no longer safe to dereference once this
582 * function returns, and should be discarded.
583 *
584 * \param ptr The pointer, returned from SDL_SIMDAlloc or SDL_SIMDRealloc, to
585 * deallocate. NULL is a legal no-op.
586 *
587 * \since This function is available since SDL 2.0.10.
588 *
589 * \sa SDL_SIMDAlloc
590 * \sa SDL_SIMDRealloc
591 */
592extern DECLSPEC void SDLCALL SDL_SIMDFree(void *ptr);
593
594/* Ends C function definitions when using C++ */
595#ifdef __cplusplus
596}
597#endif
598#include "close_code.h"
599
600#endif /* SDL_cpuinfo_h_ */
601
602/* vi: set ts=4 sw=4 expandtab: */
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)
void * SDL_SIMDAlloc(const size_t len)
void SDL_SIMDFree(void *ptr)
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 SDL_Has3DNow(void)
void * SDL_SIMDRealloc(void *mem, const size_t len)
SDL_bool
Definition SDL_stdinc.h:187
#define __inline__
Definition begin_code.h:134