SDL_intrin.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 Sam Lantinga <[email protected]>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. /* WIKI CATEGORY: Intrinsics */
  19. /**
  20. * # CategoryIntrinsics
  21. *
  22. * SDL does some preprocessor gymnastics to determine if any CPU-specific
  23. * compiler intrinsics are available, as this is not necessarily an easy thing
  24. * to calculate, and sometimes depends on quirks of a system, versions of
  25. * build tools, and other external forces.
  26. *
  27. * Apps including SDL's headers will be able to check consistent preprocessor
  28. * definitions to decide if it's safe to use compiler intrinsics for a
  29. * specific CPU architecture. This check only tells you that the compiler is
  30. * capable of using those intrinsics; at runtime, you should still check if
  31. * they are available on the current system with the
  32. * [CPU info functions](https://wiki.libsdl.org/SDL3/CategoryCPUInfo)
  33. * , such as SDL_HasSSE() or SDL_HasNEON(). Otherwise, the process might crash
  34. * for using an unsupported CPU instruction.
  35. *
  36. * SDL only sets preprocessor defines for CPU intrinsics if they are
  37. * supported, so apps should check with `#ifdef` and not `#if`.
  38. *
  39. * SDL will also include the appropriate instruction-set-specific support
  40. * headers, so if SDL decides to define SDL_SSE2_INTRINSICS, it will also
  41. * `#include <emmintrin.h>` as well.
  42. */
  43. #ifndef SDL_intrin_h_
  44. #define SDL_intrin_h_
  45. #include <SDL3/SDL_stdinc.h>
  46. #ifdef SDL_WIKI_DOCUMENTATION_SECTION
  47. /**
  48. * Defined if (and only if) the compiler supports Loongarch LSX intrinsics.
  49. *
  50. * If this macro is defined, SDL will have already included `<lsxintrin.h>`
  51. *
  52. * \since This macro is available since 3.1.3.
  53. *
  54. * \sa SDL_LASX_INTRINSICS
  55. */
  56. #define SDL_LSX_INTRINSICS 1
  57. /**
  58. * Defined if (and only if) the compiler supports Loongarch LSX intrinsics.
  59. *
  60. * If this macro is defined, SDL will have already included `<lasxintrin.h>`
  61. *
  62. * \since This macro is available since 3.1.3.
  63. *
  64. * \sa SDL_LASX_INTRINSICS
  65. */
  66. #define SDL_LASX_INTRINSICS 1
  67. /**
  68. * Defined if (and only if) the compiler supports Intel MMX intrinsics.
  69. *
  70. * If this macro is defined, SDL will have already included `<mmintrin.h>`
  71. *
  72. * \since This macro is available since 3.1.3.
  73. *
  74. * \sa SDL_SSE_INTRINSICS
  75. */
  76. #define SDL_MMX_INTRINSICS 1
  77. /**
  78. * Defined if (and only if) the compiler supports Intel SSE intrinsics.
  79. *
  80. * If this macro is defined, SDL will have already included `<xmmintrin.h>`
  81. *
  82. * \since This macro is available since 3.1.3.
  83. *
  84. * \sa SDL_SSE2_INTRINSICS
  85. * \sa SDL_SSE3_INTRINSICS
  86. * \sa SDL_SSE4_1_INTRINSICS
  87. * \sa SDL_SSE4_2_INTRINSICS
  88. */
  89. #define SDL_SSE_INTRINSICS 1
  90. /**
  91. * Defined if (and only if) the compiler supports Intel SSE2 intrinsics.
  92. *
  93. * If this macro is defined, SDL will have already included `<emmintrin.h>`
  94. *
  95. * \since This macro is available since 3.1.3.
  96. *
  97. * \sa SDL_SSE_INTRINSICS
  98. * \sa SDL_SSE3_INTRINSICS
  99. * \sa SDL_SSE4_1_INTRINSICS
  100. * \sa SDL_SSE4_2_INTRINSICS
  101. */
  102. #define SDL_SSE2_INTRINSICS 1
  103. /**
  104. * Defined if (and only if) the compiler supports Intel SSE3 intrinsics.
  105. *
  106. * If this macro is defined, SDL will have already included `<pmmintrin.h>`
  107. *
  108. * \since This macro is available since 3.1.3.
  109. *
  110. * \sa SDL_SSE_INTRINSICS
  111. * \sa SDL_SSE2_INTRINSICS
  112. * \sa SDL_SSE4_1_INTRINSICS
  113. * \sa SDL_SSE4_2_INTRINSICS
  114. */
  115. #define SDL_SSE3_INTRINSICS 1
  116. /**
  117. * Defined if (and only if) the compiler supports Intel SSE4.1 intrinsics.
  118. *
  119. * If this macro is defined, SDL will have already included `<smmintrin.h>`
  120. *
  121. * \since This macro is available since 3.1.3.
  122. *
  123. * \sa SDL_SSE_INTRINSICS
  124. * \sa SDL_SSE2_INTRINSICS
  125. * \sa SDL_SSE3_INTRINSICS
  126. * \sa SDL_SSE4_2_INTRINSICS
  127. */
  128. #define SDL_SSE4_1_INTRINSICS 1
  129. /**
  130. * Defined if (and only if) the compiler supports Intel SSE4.2 intrinsics.
  131. *
  132. * If this macro is defined, SDL will have already included `<nmmintrin.h>`
  133. *
  134. * \since This macro is available since 3.1.3.
  135. *
  136. * \sa SDL_SSE_INTRINSICS
  137. * \sa SDL_SSE2_INTRINSICS
  138. * \sa SDL_SSE3_INTRINSICS
  139. * \sa SDL_SSE4_1_INTRINSICS
  140. */
  141. #define SDL_SSE4_2_INTRINSICS 1
  142. /**
  143. * Defined if (and only if) the compiler supports Intel AVX intrinsics.
  144. *
  145. * If this macro is defined, SDL will have already included `<immintrin.h>`
  146. *
  147. * \since This macro is available since 3.1.3.
  148. *
  149. * \sa SDL_AVX2_INTRINSICS
  150. * \sa SDL_AVX512F_INTRINSICS
  151. */
  152. #define SDL_AVX_INTRINSICS 1
  153. /**
  154. * Defined if (and only if) the compiler supports Intel AVX2 intrinsics.
  155. *
  156. * If this macro is defined, SDL will have already included `<immintrin.h>`
  157. *
  158. * \since This macro is available since 3.1.3.
  159. *
  160. * \sa SDL_AVX_INTRINSICS
  161. * \sa SDL_AVX512F_INTRINSICS
  162. */
  163. #define SDL_AVX2_INTRINSICS 1
  164. /**
  165. * Defined if (and only if) the compiler supports Intel AVX-512F intrinsics.
  166. *
  167. * AVX-512F is also sometimes referred to as "AVX-512 Foundation."
  168. *
  169. * If this macro is defined, SDL will have already included `<immintrin.h>`
  170. *
  171. * \since This macro is available since 3.1.3.
  172. *
  173. * \sa SDL_AVX_INTRINSICS
  174. * \sa SDL_AVX2_INTRINSICS
  175. */
  176. #define SDL_AVX512F_INTRINSICS 1
  177. #endif
  178. /* Need to do this here because intrin.h has C++ code in it */
  179. /* Visual Studio 2005 has a bug where intrin.h conflicts with winnt.h */
  180. #if defined(_MSC_VER) && (_MSC_VER >= 1500) && (defined(_M_IX86) || defined(_M_X64))
  181. #ifdef __clang__
  182. /* As of Clang 11, '_m_prefetchw' is conflicting with the winnt.h's version,
  183. so we define the needed '_m_prefetch' here as a pseudo-header, until the issue is fixed. */
  184. #ifndef __PRFCHWINTRIN_H
  185. #define __PRFCHWINTRIN_H
  186. static __inline__ void __attribute__((__always_inline__, __nodebug__))
  187. _m_prefetch(void *__P)
  188. {
  189. __builtin_prefetch (__P, 0, 3 /* _MM_HINT_T0 */);
  190. }
  191. #endif /* __PRFCHWINTRIN_H */
  192. #endif /* __clang__ */
  193. #include <intrin.h>
  194. #elif defined(__MINGW64_VERSION_MAJOR)
  195. #include <intrin.h>
  196. #if defined(__ARM_NEON) && !defined(SDL_DISABLE_NEON)
  197. # define SDL_NEON_INTRINSICS 1
  198. # include <arm_neon.h>
  199. #endif
  200. #else
  201. /* altivec.h redefining bool causes a number of problems, see bugs 3993 and 4392, so you need to explicitly define SDL_ENABLE_ALTIVEC to have it included. */
  202. #if defined(__ALTIVEC__) && defined(SDL_ENABLE_ALTIVEC)
  203. #define SDL_ALTIVEC_INTRINSICS 1
  204. #include <altivec.h>
  205. #endif
  206. #ifndef SDL_DISABLE_NEON
  207. # ifdef __ARM_NEON
  208. # define SDL_NEON_INTRINSICS 1
  209. # include <arm_neon.h>
  210. # elif defined(SDL_PLATFORM_WINDOWS)
  211. /* Visual Studio doesn't define __ARM_ARCH, but _M_ARM (if set, always 7), and _M_ARM64 (if set, always 1). */
  212. # ifdef _M_ARM
  213. # define SDL_NEON_INTRINSICS 1
  214. # include <armintr.h>
  215. # include <arm_neon.h>
  216. # define __ARM_NEON 1 /* Set __ARM_NEON so that it can be used elsewhere, at compile time */
  217. # endif
  218. # if defined (_M_ARM64)
  219. # define SDL_NEON_INTRINSICS 1
  220. # include <arm64intr.h>
  221. # include <arm64_neon.h>
  222. # define __ARM_NEON 1 /* Set __ARM_NEON so that it can be used elsewhere, at compile time */
  223. # define __ARM_ARCH 8
  224. # endif
  225. # endif
  226. #endif
  227. #endif /* compiler version */
  228. #if defined(__clang__) && defined(__has_attribute)
  229. # if __has_attribute(target)
  230. # define SDL_HAS_TARGET_ATTRIBS
  231. # endif
  232. #elif defined(__GNUC__) && (__GNUC__ + (__GNUC_MINOR__ >= 9) > 4) /* gcc >= 4.9 */
  233. # define SDL_HAS_TARGET_ATTRIBS
  234. #elif defined(__ICC) && __ICC >= 1600
  235. # define SDL_HAS_TARGET_ATTRIBS
  236. #endif
  237. #ifdef SDL_HAS_TARGET_ATTRIBS
  238. # define SDL_TARGETING(x) __attribute__((target(x)))
  239. #else
  240. # define SDL_TARGETING(x)
  241. #endif
  242. #ifdef __loongarch64
  243. # ifndef SDL_DISABLE_LSX
  244. # define SDL_LSX_INTRINSICS 1
  245. # include <lsxintrin.h>
  246. # endif
  247. # ifndef SDL_DISABLE_LASX
  248. # define SDL_LASX_INTRINSICS 1
  249. # include <lasxintrin.h>
  250. # endif
  251. #endif
  252. #if defined(__x86_64__) || defined(_M_X64) || defined(__i386__) || defined(_M_IX86)
  253. # if ((defined(_MSC_VER) && !defined(_M_X64)) || defined(__MMX__) || defined(SDL_HAS_TARGET_ATTRIBS)) && !defined(SDL_DISABLE_MMX)
  254. # define SDL_MMX_INTRINSICS 1
  255. # include <mmintrin.h>
  256. # endif
  257. # if (defined(_MSC_VER) || defined(__SSE__) || defined(SDL_HAS_TARGET_ATTRIBS)) && !defined(SDL_DISABLE_SSE)
  258. # define SDL_SSE_INTRINSICS 1
  259. # include <xmmintrin.h>
  260. # endif
  261. # if (defined(_MSC_VER) || defined(__SSE2__) || defined(SDL_HAS_TARGET_ATTRIBS)) && !defined(SDL_DISABLE_SSE2)
  262. # define SDL_SSE2_INTRINSICS 1
  263. # include <emmintrin.h>
  264. # endif
  265. # if (defined(_MSC_VER) || defined(__SSE3__) || defined(SDL_HAS_TARGET_ATTRIBS)) && !defined(SDL_DISABLE_SSE3)
  266. # define SDL_SSE3_INTRINSICS 1
  267. # include <pmmintrin.h>
  268. # endif
  269. # if (defined(_MSC_VER) || defined(__SSE4_1__) || defined(SDL_HAS_TARGET_ATTRIBS)) && !defined(SDL_DISABLE_SSE4_1)
  270. # define SDL_SSE4_1_INTRINSICS 1
  271. # include <smmintrin.h>
  272. # endif
  273. # if (defined(_MSC_VER) || defined(__SSE4_2__) || defined(SDL_HAS_TARGET_ATTRIBS)) && !defined(SDL_DISABLE_SSE4_2)
  274. # define SDL_SSE4_2_INTRINSICS 1
  275. # include <nmmintrin.h>
  276. # endif
  277. # if defined(__clang__) && (defined(_MSC_VER) || defined(__SCE__)) && !defined(__AVX__) && !defined(SDL_DISABLE_AVX)
  278. # define SDL_DISABLE_AVX /* see https://reviews.llvm.org/D20291 and https://reviews.llvm.org/D79194 */
  279. # endif
  280. # if (defined(_MSC_VER) || defined(__AVX__) || defined(SDL_HAS_TARGET_ATTRIBS)) && !defined(_M_ARM64EC) && !defined(SDL_DISABLE_AVX)
  281. # define SDL_AVX_INTRINSICS 1
  282. # include <immintrin.h>
  283. # endif
  284. # if defined(__clang__) && (defined(_MSC_VER) || defined(__SCE__)) && !defined(__AVX2__) && !defined(SDL_DISABLE_AVX2)
  285. # define SDL_DISABLE_AVX2 /* see https://reviews.llvm.org/D20291 and https://reviews.llvm.org/D79194 */
  286. # endif
  287. # if (defined(_MSC_VER) || defined(__AVX2__) || defined(SDL_HAS_TARGET_ATTRIBS)) && !defined(_M_ARM64EC) && !defined(SDL_DISABLE_AVX2)
  288. # define SDL_AVX2_INTRINSICS 1
  289. # include <immintrin.h>
  290. # endif
  291. # if defined(__clang__) && (defined(_MSC_VER) || defined(__SCE__)) && !defined(__AVX512F__) && !defined(SDL_DISABLE_AVX512F)
  292. # define SDL_DISABLE_AVX512F /* see https://reviews.llvm.org/D20291 and https://reviews.llvm.org/D79194 */
  293. # endif
  294. # if (defined(_MSC_VER) || defined(__AVX512F__) || defined(SDL_HAS_TARGET_ATTRIBS)) && !defined(_M_ARM64EC) && !defined(SDL_DISABLE_AVX512F)
  295. # define SDL_AVX512F_INTRINSICS 1
  296. # include <immintrin.h>
  297. # endif
  298. #endif /* defined(__x86_64__) || defined(_M_X64) || defined(__i386__) || defined(_M_IX86) */
  299. #endif /* SDL_intrin_h_ */