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