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