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

source code of include/SDL2/SDL_cpuinfo.h