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