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