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