SDL  2.0
SDL_cpuinfo.h
Go to the documentation of this file.
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 
43 static __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
118 extern "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  */
137 extern 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  */
149 extern 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  */
169 extern 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
175  * sets.
176  *
177  * \returns SDL_TRUE if the CPU has AltiVec features or SDL_FALSE if not.
178  *
179  * \sa SDL_Has3DNow
180  * \sa SDL_HasAVX
181  * \sa SDL_HasAVX2
182  * \sa SDL_HasMMX
183  * \sa SDL_HasRDTSC
184  * \sa SDL_HasSSE
185  * \sa SDL_HasSSE2
186  * \sa SDL_HasSSE3
187  * \sa SDL_HasSSE41
188  * \sa SDL_HasSSE42
189  */
190 extern DECLSPEC SDL_bool SDLCALL SDL_HasAltiVec(void);
191 
192 /**
193  * Determine whether the CPU has MMX features.
194  *
195  * This always returns false on CPUs that aren't using Intel instruction sets.
196  *
197  * \returns SDL_TRUE if the CPU has MMX features or SDL_FALSE if not.
198  *
199  * \sa SDL_Has3DNow
200  * \sa SDL_HasAltiVec
201  * \sa SDL_HasAVX
202  * \sa SDL_HasAVX2
203  * \sa SDL_HasRDTSC
204  * \sa SDL_HasSSE
205  * \sa SDL_HasSSE2
206  * \sa SDL_HasSSE3
207  * \sa SDL_HasSSE41
208  * \sa SDL_HasSSE42
209  */
210 extern DECLSPEC SDL_bool SDLCALL SDL_HasMMX(void);
211 
212 /**
213  * Determine whether the CPU has 3DNow! features.
214  *
215  * This always returns false on CPUs that aren't using AMD instruction sets.
216  *
217  * \returns SDL_TRUE if the CPU has 3DNow! features or SDL_FALSE if not.
218  *
219  * \sa SDL_HasAltiVec
220  * \sa SDL_HasAVX
221  * \sa SDL_HasAVX2
222  * \sa SDL_HasMMX
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  */
230 extern DECLSPEC SDL_bool SDLCALL SDL_Has3DNow(void);
231 
232 /**
233  * Determine whether the CPU has SSE features.
234  *
235  * This always returns false on CPUs that aren't using Intel instruction sets.
236  *
237  * \returns SDL_TRUE if the CPU has SSE features or SDL_FALSE if not.
238  *
239  * \sa SDL_Has3DNow
240  * \sa SDL_HasAltiVec
241  * \sa SDL_HasAVX
242  * \sa SDL_HasAVX2
243  * \sa SDL_HasMMX
244  * \sa SDL_HasRDTSC
245  * \sa SDL_HasSSE2
246  * \sa SDL_HasSSE3
247  * \sa SDL_HasSSE41
248  * \sa SDL_HasSSE42
249  */
250 extern DECLSPEC SDL_bool SDLCALL SDL_HasSSE(void);
251 
252 /**
253  * Determine whether the CPU has SSE2 features.
254  *
255  * This always returns false on CPUs that aren't using Intel instruction sets.
256  *
257  * \returns SDL_TRUE if the CPU has SSE2 features or SDL_FALSE if not.
258  *
259  * \sa SDL_Has3DNow
260  * \sa SDL_HasAltiVec
261  * \sa SDL_HasAVX
262  * \sa SDL_HasAVX2
263  * \sa SDL_HasMMX
264  * \sa SDL_HasRDTSC
265  * \sa SDL_HasSSE
266  * \sa SDL_HasSSE3
267  * \sa SDL_HasSSE41
268  * \sa SDL_HasSSE42
269  */
270 extern DECLSPEC SDL_bool SDLCALL SDL_HasSSE2(void);
271 
272 /**
273  * Determine whether the CPU has SSE3 features.
274  *
275  * This always returns false on CPUs that aren't using Intel instruction sets.
276  *
277  * \returns SDL_TRUE if the CPU has SSE3 features or SDL_FALSE if not.
278  *
279  * \sa SDL_Has3DNow
280  * \sa SDL_HasAltiVec
281  * \sa SDL_HasAVX
282  * \sa SDL_HasAVX2
283  * \sa SDL_HasMMX
284  * \sa SDL_HasRDTSC
285  * \sa SDL_HasSSE
286  * \sa SDL_HasSSE2
287  * \sa SDL_HasSSE41
288  * \sa SDL_HasSSE42
289  */
290 extern DECLSPEC SDL_bool SDLCALL SDL_HasSSE3(void);
291 
292 /**
293  * Determine whether the CPU has SSE4.1 features.
294  *
295  * This always returns false on CPUs that aren't using Intel instruction sets.
296  *
297  * \returns SDL_TRUE if the CPU has SSE4.1 features or SDL_FALSE if not.
298  *
299  * \sa SDL_Has3DNow
300  * \sa SDL_HasAltiVec
301  * \sa SDL_HasAVX
302  * \sa SDL_HasAVX2
303  * \sa SDL_HasMMX
304  * \sa SDL_HasRDTSC
305  * \sa SDL_HasSSE
306  * \sa SDL_HasSSE2
307  * \sa SDL_HasSSE3
308  * \sa SDL_HasSSE42
309  */
310 extern DECLSPEC SDL_bool SDLCALL SDL_HasSSE41(void);
311 
312 /**
313  * Determine whether the CPU has SSE4.2 features.
314  *
315  * This always returns false on CPUs that aren't using Intel instruction sets.
316  *
317  * \returns SDL_TRUE if the CPU has SSE4.2 features or SDL_FALSE if not.
318  *
319  * \sa SDL_Has3DNow
320  * \sa SDL_HasAltiVec
321  * \sa SDL_HasAVX
322  * \sa SDL_HasAVX2
323  * \sa SDL_HasMMX
324  * \sa SDL_HasRDTSC
325  * \sa SDL_HasSSE
326  * \sa SDL_HasSSE2
327  * \sa SDL_HasSSE3
328  * \sa SDL_HasSSE41
329  */
330 extern DECLSPEC SDL_bool SDLCALL SDL_HasSSE42(void);
331 
332 /**
333  * Determine whether the CPU has AVX features.
334  *
335  * This always returns false on CPUs that aren't using Intel instruction sets.
336  *
337  * \returns SDL_TRUE if the CPU has AVX features or SDL_FALSE if not.
338  *
339  * \since This function is available since SDL 2.0.2.
340  *
341  * \sa SDL_Has3DNow
342  * \sa SDL_HasAltiVec
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  * \sa SDL_HasSSE42
351  */
352 extern DECLSPEC SDL_bool SDLCALL SDL_HasAVX(void);
353 
354 /**
355  * Determine whether the CPU has AVX2 features.
356  *
357  * This always returns false on CPUs that aren't using Intel instruction sets.
358  *
359  * \returns SDL_TRUE if the CPU has AVX2 features or SDL_FALSE if not.
360  *
361  * \since This function is available since SDL 2.0.4.
362  *
363  * \sa SDL_Has3DNow
364  * \sa SDL_HasAltiVec
365  * \sa SDL_HasAVX
366  * \sa SDL_HasMMX
367  * \sa SDL_HasRDTSC
368  * \sa SDL_HasSSE
369  * \sa SDL_HasSSE2
370  * \sa SDL_HasSSE3
371  * \sa SDL_HasSSE41
372  * \sa SDL_HasSSE42
373  */
374 extern DECLSPEC SDL_bool SDLCALL SDL_HasAVX2(void);
375 
376 /**
377  * Determine whether the CPU has AVX-512F (foundation) features.
378  *
379  * This always returns false on CPUs that aren't using Intel instruction sets.
380  *
381  * \returns SDL_TRUE if the CPU has AVX-512F features or SDL_FALSE if not.
382  *
383  * \sa SDL_HasAVX
384  */
385 extern DECLSPEC SDL_bool SDLCALL SDL_HasAVX512F(void);
386 
387 /**
388  * Determine whether the CPU has ARM SIMD (ARMv6) features.
389  *
390  * This is different from ARM NEON, which is a different instruction set.
391  *
392  * This always returns false on CPUs that aren't using ARM instruction sets.
393  *
394  * \returns SDL_TRUE if the CPU has ARM SIMD features or SDL_FALSE if not.
395  *
396  * \sa SDL_HasNEON
397  */
398 extern DECLSPEC SDL_bool SDLCALL SDL_HasARMSIMD(void);
399 
400 /**
401  * Determine whether the CPU has NEON (ARM SIMD) features.
402  *
403  * This always returns false on CPUs that aren't using ARM instruction sets.
404  *
405  * \returns SDL_TRUE if the CPU has ARM NEON features or SDL_FALSE if not.
406  */
407 extern DECLSPEC SDL_bool SDLCALL SDL_HasNEON(void);
408 
409 /**
410  * Get the amount of RAM configured in the system.
411  *
412  * \returns the amount of RAM configured in the system in MB.
413  *
414  * \since This function is available since SDL 2.0.1.
415  */
416 extern DECLSPEC int SDLCALL SDL_GetSystemRAM(void);
417 
418 /**
419  * Report the alignment this system needs for SIMD allocations.
420  *
421  * This will return the minimum number of bytes to which a pointer must be
422  * aligned to be compatible with SIMD instructions on the current machine. For
423  * example, if the machine supports SSE only, it will return 16, but if it
424  * supports AVX-512F, it'll return 64 (etc). This only reports values for
425  * instruction sets SDL knows about, so if your SDL build doesn't have
426  * SDL_HasAVX512F(), then it might return 16 for the SSE support it sees and
427  * not 64 for the AVX-512 instructions that exist but SDL doesn't know about.
428  * Plan accordingly.
429  *
430  * \returns the alignment in bytes needed for available, known SIMD
431  * instructions.
432  */
433 extern DECLSPEC size_t SDLCALL SDL_SIMDGetAlignment(void);
434 
435 /**
436  * Allocate memory in a SIMD-friendly way.
437  *
438  * This will allocate a block of memory that is suitable for use with SIMD
439  * instructions. Specifically, it will be properly aligned and padded for the
440  * system's supported vector instructions.
441  *
442  * The memory returned will be padded such that it is safe to read or write an
443  * incomplete vector at the end of the memory block. This can be useful so you
444  * don't have to drop back to a scalar fallback at the end of your SIMD
445  * processing loop to deal with the final elements without overflowing the
446  * allocated buffer.
447  *
448  * You must free this memory with SDL_FreeSIMD(), not free() or SDL_free() or
449  * delete[], etc.
450  *
451  * Note that SDL will only deal with SIMD instruction sets it is aware of; for
452  * example, SDL 2.0.8 knows that SSE wants 16-byte vectors (SDL_HasSSE()), and
453  * AVX2 wants 32 bytes (SDL_HasAVX2()), but doesn't know that AVX-512 wants
454  * 64. To be clear: if you can't decide to use an instruction set with an
455  * SDL_Has*() function, don't use that instruction set with memory allocated
456  * through here.
457  *
458  * SDL_AllocSIMD(0) will return a non-NULL pointer, assuming the system isn't
459  * out of memory, but you are not allowed to dereference it (because you only
460  * own zero bytes of that buffer).
461  *
462  * \param len The length, in bytes, of the block to allocate. The actual
463  * allocated block might be larger due to padding, etc.
464  * \returns a pointer to thenewly-allocated block, NULL if out of memory.
465  *
466  * \sa SDL_SIMDAlignment
467  * \sa SDL_SIMDRealloc
468  * \sa SDL_SIMDFree
469  */
470 extern DECLSPEC void * SDLCALL SDL_SIMDAlloc(const size_t len);
471 
472 /**
473  * Reallocate memory obtained from SDL_SIMDAlloc
474  *
475  * It is not valid to use this function on a pointer from anything but
476  * SDL_SIMDAlloc(). It can't be used on pointers from malloc, realloc,
477  * SDL_malloc, memalign, new[], etc.
478  *
479  * \param mem The pointer obtained from SDL_SIMDAlloc. This function also
480  * accepts NULL, at which point this function is the same as
481  * calling SDL_SIMDAlloc with a NULL pointer.
482  * \param len The length, in bytes, of the block to allocated. The actual
483  * allocated block might be larger due to padding, etc. Passing 0
484  * will return a non-NULL pointer, assuming the system isn't out of
485  * memory.
486  * \returns a pointer to the newly-reallocated block, NULL if out of memory.
487  *
488  * \sa SDL_SIMDAlignment
489  * \sa SDL_SIMDAlloc
490  * \sa SDL_SIMDFree
491  */
492 extern DECLSPEC void * SDLCALL SDL_SIMDRealloc(void *mem, const size_t len);
493 
494 /**
495  * Deallocate memory obtained from SDL_SIMDAlloc
496  *
497  * It is not valid to use this function on a pointer from anything but
498  * SDL_SIMDAlloc() or SDL_SIMDRealloc(). It can't be used on pointers from
499  * malloc, realloc, SDL_malloc, memalign, new[], etc.
500  *
501  * However, SDL_SIMDFree(NULL) is a legal no-op.
502  *
503  * The memory pointed to by `ptr` is no longer valid for access upon return,
504  * and may be returned to the system or reused by a future allocation. The
505  * pointer passed to this function is no longer safe to dereference once this
506  * function returns, and should be discarded.
507  *
508  * \param ptr The pointer, returned from SDL_SIMDAlloc or SDL_SIMDRealloc, to
509  * deallocate. NULL is a legal no-op.
510  *
511  * \sa SDL_SIMDAlloc
512  * \sa SDL_SIMDRealloc
513  */
514 extern DECLSPEC void SDLCALL SDL_SIMDFree(void *ptr);
515 
516 /* Ends C function definitions when using C++ */
517 #ifdef __cplusplus
518 }
519 #endif
520 #include "close_code.h"
521 
522 #endif /* SDL_cpuinfo_h_ */
523 
524 /* vi: set ts=4 sw=4 expandtab: */
int SDL_GetCPUCount(void)
#define __inline__
Definition: begin_code.h:132
SDL_bool SDL_Has3DNow(void)
size_t SDL_SIMDGetAlignment(void)
SDL_bool SDL_HasRDTSC(void)
SDL_bool SDL_HasNEON(void)
SDL_bool SDL_HasAVX2(void)
SDL_bool
Definition: SDL_stdinc.h:167
void * SDL_SIMDAlloc(const size_t len)
SDL_bool SDL_HasSSE41(void)
SDL_bool SDL_HasSSE42(void)
SDL_bool SDL_HasSSE3(void)
SDL_bool SDL_HasSSE(void)
SDL_bool SDL_HasAVX512F(void)
void SDL_SIMDFree(void *ptr)
SDL_bool SDL_HasAVX(void)
SDL_bool SDL_HasAltiVec(void)
SDL_bool SDL_HasMMX(void)
SDL_bool SDL_HasARMSIMD(void)
SDL_bool SDL_HasSSE2(void)
int SDL_GetSystemRAM(void)
void * SDL_SIMDRealloc(void *mem, const size_t len)
int SDL_GetCPUCacheLineSize(void)