SDL_intrin.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
  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 ARM NEON intrinsics.
  69. *
  70. * If this macro is defined, SDL will have already included `<armintr.h>`
  71. * `<arm_neon.h>`, `<arm64intr.h>`, and `<arm64_neon.h>`, as appropriate.
  72. *
  73. * \since This macro is available since 3.1.3.
  74. */
  75. #define SDL_NEON_INTRINSICS 1
  76. /**
  77. * Defined if (and only if) the compiler supports PowerPC Altivec intrinsics.
  78. *
  79. * If this macro is defined, SDL will have already included `<altivec.h>`
  80. *
  81. * \since This macro is available since 3.1.3.
  82. */
  83. #define SDL_ALTIVEC_INTRINSICS 1
  84. /**
  85. * Defined if (and only if) the compiler supports Intel MMX intrinsics.
  86. *
  87. * If this macro is defined, SDL will have already included `<mmintrin.h>`
  88. *
  89. * \since This macro is available since 3.1.3.
  90. *
  91. * \sa SDL_SSE_INTRINSICS
  92. */
  93. #define SDL_MMX_INTRINSICS 1
  94. /**
  95. * Defined if (and only if) the compiler supports Intel SSE intrinsics.
  96. *
  97. * If this macro is defined, SDL will have already included `<xmmintrin.h>`
  98. *
  99. * \since This macro is available since 3.1.3.
  100. *
  101. * \sa SDL_SSE2_INTRINSICS
  102. * \sa SDL_SSE3_INTRINSICS
  103. * \sa SDL_SSE4_1_INTRINSICS
  104. * \sa SDL_SSE4_2_INTRINSICS
  105. */
  106. #define SDL_SSE_INTRINSICS 1
  107. /**
  108. * Defined if (and only if) the compiler supports Intel SSE2 intrinsics.
  109. *
  110. * If this macro is defined, SDL will have already included `<emmintrin.h>`
  111. *
  112. * \since This macro is available since 3.1.3.
  113. *
  114. * \sa SDL_SSE_INTRINSICS
  115. * \sa SDL_SSE3_INTRINSICS
  116. * \sa SDL_SSE4_1_INTRINSICS
  117. * \sa SDL_SSE4_2_INTRINSICS
  118. */
  119. #define SDL_SSE2_INTRINSICS 1
  120. /**
  121. * Defined if (and only if) the compiler supports Intel SSE3 intrinsics.
  122. *
  123. * If this macro is defined, SDL will have already included `<pmmintrin.h>`
  124. *
  125. * \since This macro is available since 3.1.3.
  126. *
  127. * \sa SDL_SSE_INTRINSICS
  128. * \sa SDL_SSE2_INTRINSICS
  129. * \sa SDL_SSE4_1_INTRINSICS
  130. * \sa SDL_SSE4_2_INTRINSICS
  131. */
  132. #define SDL_SSE3_INTRINSICS 1
  133. /**
  134. * Defined if (and only if) the compiler supports Intel SSE4.1 intrinsics.
  135. *
  136. * If this macro is defined, SDL will have already included `<smmintrin.h>`
  137. *
  138. * \since This macro is available since 3.1.3.
  139. *
  140. * \sa SDL_SSE_INTRINSICS
  141. * \sa SDL_SSE2_INTRINSICS
  142. * \sa SDL_SSE3_INTRINSICS
  143. * \sa SDL_SSE4_2_INTRINSICS
  144. */
  145. #define SDL_SSE4_1_INTRINSICS 1
  146. /**
  147. * Defined if (and only if) the compiler supports Intel SSE4.2 intrinsics.
  148. *
  149. * If this macro is defined, SDL will have already included `<nmmintrin.h>`
  150. *
  151. * \since This macro is available since 3.1.3.
  152. *
  153. * \sa SDL_SSE_INTRINSICS
  154. * \sa SDL_SSE2_INTRINSICS
  155. * \sa SDL_SSE3_INTRINSICS
  156. * \sa SDL_SSE4_1_INTRINSICS
  157. */
  158. #define SDL_SSE4_2_INTRINSICS 1
  159. /**
  160. * Defined if (and only if) the compiler supports Intel AVX intrinsics.
  161. *
  162. * If this macro is defined, SDL will have already included `<immintrin.h>`
  163. *
  164. * \since This macro is available since 3.1.3.
  165. *
  166. * \sa SDL_AVX2_INTRINSICS
  167. * \sa SDL_AVX512F_INTRINSICS
  168. */
  169. #define SDL_AVX_INTRINSICS 1
  170. /**
  171. * Defined if (and only if) the compiler supports Intel AVX2 intrinsics.
  172. *
  173. * If this macro is defined, SDL will have already included `<immintrin.h>`
  174. *
  175. * \since This macro is available since 3.1.3.
  176. *
  177. * \sa SDL_AVX_INTRINSICS
  178. * \sa SDL_AVX512F_INTRINSICS
  179. */
  180. #define SDL_AVX2_INTRINSICS 1
  181. /**
  182. * Defined if (and only if) the compiler supports Intel AVX-512F intrinsics.
  183. *
  184. * AVX-512F is also sometimes referred to as "AVX-512 Foundation."
  185. *
  186. * If this macro is defined, SDL will have already included `<immintrin.h>`
  187. *
  188. * \since This macro is available since 3.1.3.
  189. *
  190. * \sa SDL_AVX_INTRINSICS
  191. * \sa SDL_AVX2_INTRINSICS
  192. */
  193. #define SDL_AVX512F_INTRINSICS 1
  194. #endif
  195. /* Need to do this here because intrin.h has C++ code in it */
  196. /* Visual Studio 2005 has a bug where intrin.h conflicts with winnt.h */
  197. #if defined(_MSC_VER) && (_MSC_VER >= 1500) && (defined(_M_IX86) || defined(_M_X64))
  198. #ifdef __clang__
  199. /* As of Clang 11, '_m_prefetchw' is conflicting with the winnt.h's version,
  200. so we define the needed '_m_prefetch' here as a pseudo-header, until the issue is fixed. */
  201. #ifndef __PRFCHWINTRIN_H
  202. #define __PRFCHWINTRIN_H
  203. static __inline__ void __attribute__((__always_inline__, __nodebug__))
  204. _m_prefetch(void *__P)
  205. {
  206. __builtin_prefetch (__P, 0, 3 /* _MM_HINT_T0 */);
  207. }
  208. #endif /* __PRFCHWINTRIN_H */
  209. #endif /* __clang__ */
  210. #include <intrin.h>
  211. #elif defined(__MINGW64_VERSION_MAJOR)
  212. #include <intrin.h>
  213. #if defined(__ARM_NEON) && !defined(SDL_DISABLE_NEON)
  214. # define SDL_NEON_INTRINSICS 1
  215. # include <arm_neon.h>
  216. #endif
  217. #else
  218. /* 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. */
  219. #if defined(__ALTIVEC__) && defined(SDL_ENABLE_ALTIVEC)
  220. #define SDL_ALTIVEC_INTRINSICS 1
  221. #include <altivec.h>
  222. #endif
  223. #ifndef SDL_DISABLE_NEON
  224. # ifdef __ARM_NEON
  225. # define SDL_NEON_INTRINSICS 1
  226. # include <arm_neon.h>
  227. # elif defined(SDL_PLATFORM_WINDOWS)
  228. /* Visual Studio doesn't define __ARM_ARCH, but _M_ARM (if set, always 7), and _M_ARM64 (if set, always 1). */
  229. # ifdef _M_ARM
  230. # define SDL_NEON_INTRINSICS 1
  231. # include <armintr.h>
  232. # include <arm_neon.h>
  233. # define __ARM_NEON 1 /* Set __ARM_NEON so that it can be used elsewhere, at compile time */
  234. # endif
  235. # if defined (_M_ARM64)
  236. # define SDL_NEON_INTRINSICS 1
  237. # include <arm64intr.h>
  238. # include <arm64_neon.h>
  239. # define __ARM_NEON 1 /* Set __ARM_NEON so that it can be used elsewhere, at compile time */
  240. # define __ARM_ARCH 8
  241. # endif
  242. # endif
  243. #endif
  244. #endif /* compiler version */
  245. #ifdef SDL_WIKI_DOCUMENTATION_SECTION
  246. /**
  247. * A macro to decide if the compiler supports `__attribute__((target))`.
  248. *
  249. * Even though this is defined in SDL's public headers, it is generally not
  250. * used directly by apps. Apps should probably just use SDL_TARGETING
  251. * directly, instead.
  252. *
  253. * \since This macro is available since SDL 3.1.3.
  254. *
  255. * \sa SDL_TARGETING
  256. */
  257. #define SDL_HAS_TARGET_ATTRIBS
  258. #elif defined(__clang__) && defined(__has_attribute)
  259. # if __has_attribute(target)
  260. # define SDL_HAS_TARGET_ATTRIBS
  261. # endif
  262. #elif defined(__GNUC__) && (__GNUC__ + (__GNUC_MINOR__ >= 9) > 4) /* gcc >= 4.9 */
  263. # define SDL_HAS_TARGET_ATTRIBS
  264. #elif defined(__ICC) && __ICC >= 1600
  265. # define SDL_HAS_TARGET_ATTRIBS
  266. #endif
  267. #ifdef SDL_WIKI_DOCUMENTATION_SECTION
  268. /**
  269. * A macro to tag a function as targeting a specific CPU architecture.
  270. *
  271. * This is a hint to the compiler that a function should be built with support
  272. * for a CPU instruction set that might be different than the rest of the
  273. * program.
  274. *
  275. * The particulars of this are explained in the GCC documentation:
  276. *
  277. * https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-target-function-attribute
  278. *
  279. * An example of using this feature is to turn on SSE2 support for a specific
  280. * function, even if the rest of the source code is not compiled to use SSE2
  281. * code:
  282. *
  283. * ```c
  284. * #ifdef SDL_SSE2_INTRINSICS
  285. * static void SDL_TARGETING("sse2") DoSomethingWithSSE2(char *x) {
  286. * ...use SSE2 intrinsic functions, etc...
  287. * }
  288. * #endif
  289. *
  290. * // later...
  291. * #ifdef SDL_SSE2_INTRINSICS
  292. * if (SDL_HasSSE2()) {
  293. * DoSomethingWithSSE2(str);
  294. * }
  295. * #endif
  296. * ```
  297. *
  298. * The application is, on a whole, built without SSE2 instructions, so it will
  299. * run on Intel machines that don't support SSE2. But then at runtime, it
  300. * checks if the system supports the instructions, and then calls into a
  301. * function that uses SSE2 opcodes. The ifdefs make sure that this code isn't
  302. * used on platforms that don't have SSE2 at all.
  303. *
  304. * On compilers without target support, this is defined to nothing.
  305. *
  306. * This symbol is used by SDL internally, but apps and other libraries are
  307. * welcome to use it for their own interfaces as well.
  308. *
  309. * \since This macro is available since SDL 3.1.3.
  310. */
  311. #define SDL_TARGETING(x) __attribute__((target(x)))
  312. #elif defined(SDL_HAS_TARGET_ATTRIBS)
  313. # define SDL_TARGETING(x) __attribute__((target(x)))
  314. #else
  315. # define SDL_TARGETING(x)
  316. #endif
  317. #ifdef __loongarch64
  318. # ifndef SDL_DISABLE_LSX
  319. # define SDL_LSX_INTRINSICS 1
  320. # include <lsxintrin.h>
  321. # endif
  322. # ifndef SDL_DISABLE_LASX
  323. # define SDL_LASX_INTRINSICS 1
  324. # include <lasxintrin.h>
  325. # endif
  326. #endif
  327. #if defined(__x86_64__) || defined(_M_X64) || defined(__i386__) || defined(_M_IX86)
  328. # if ((defined(_MSC_VER) && !defined(_M_X64)) || defined(__MMX__) || defined(SDL_HAS_TARGET_ATTRIBS)) && !defined(SDL_DISABLE_MMX)
  329. # define SDL_MMX_INTRINSICS 1
  330. # include <mmintrin.h>
  331. # endif
  332. # if (defined(_MSC_VER) || defined(__SSE__) || defined(SDL_HAS_TARGET_ATTRIBS)) && !defined(SDL_DISABLE_SSE)
  333. # define SDL_SSE_INTRINSICS 1
  334. # include <xmmintrin.h>
  335. # endif
  336. # if (defined(_MSC_VER) || defined(__SSE2__) || defined(SDL_HAS_TARGET_ATTRIBS)) && !defined(SDL_DISABLE_SSE2)
  337. # define SDL_SSE2_INTRINSICS 1
  338. # include <emmintrin.h>
  339. # endif
  340. # if (defined(_MSC_VER) || defined(__SSE3__) || defined(SDL_HAS_TARGET_ATTRIBS)) && !defined(SDL_DISABLE_SSE3)
  341. # define SDL_SSE3_INTRINSICS 1
  342. # include <pmmintrin.h>
  343. # endif
  344. # if (defined(_MSC_VER) || defined(__SSE4_1__) || defined(SDL_HAS_TARGET_ATTRIBS)) && !defined(SDL_DISABLE_SSE4_1)
  345. # define SDL_SSE4_1_INTRINSICS 1
  346. # include <smmintrin.h>
  347. # endif
  348. # if (defined(_MSC_VER) || defined(__SSE4_2__) || defined(SDL_HAS_TARGET_ATTRIBS)) && !defined(SDL_DISABLE_SSE4_2)
  349. # define SDL_SSE4_2_INTRINSICS 1
  350. # include <nmmintrin.h>
  351. # endif
  352. # if defined(__clang__) && (defined(_MSC_VER) || defined(__SCE__)) && !defined(__AVX__) && !defined(SDL_DISABLE_AVX)
  353. # define SDL_DISABLE_AVX /* see https://reviews.llvm.org/D20291 and https://reviews.llvm.org/D79194 */
  354. # endif
  355. # if (defined(_MSC_VER) || defined(__AVX__) || defined(SDL_HAS_TARGET_ATTRIBS)) && !defined(_M_ARM64EC) && !defined(SDL_DISABLE_AVX)
  356. # define SDL_AVX_INTRINSICS 1
  357. # include <immintrin.h>
  358. # endif
  359. # if defined(__clang__) && (defined(_MSC_VER) || defined(__SCE__)) && !defined(__AVX2__) && !defined(SDL_DISABLE_AVX2)
  360. # define SDL_DISABLE_AVX2 /* see https://reviews.llvm.org/D20291 and https://reviews.llvm.org/D79194 */
  361. # endif
  362. # if (defined(_MSC_VER) || defined(__AVX2__) || defined(SDL_HAS_TARGET_ATTRIBS)) && !defined(_M_ARM64EC) && !defined(SDL_DISABLE_AVX2)
  363. # define SDL_AVX2_INTRINSICS 1
  364. # include <immintrin.h>
  365. # endif
  366. # if defined(__clang__) && (defined(_MSC_VER) || defined(__SCE__)) && !defined(__AVX512F__) && !defined(SDL_DISABLE_AVX512F)
  367. # define SDL_DISABLE_AVX512F /* see https://reviews.llvm.org/D20291 and https://reviews.llvm.org/D79194 */
  368. # endif
  369. # if (defined(_MSC_VER) || defined(__AVX512F__) || defined(SDL_HAS_TARGET_ATTRIBS)) && !defined(_M_ARM64EC) && !defined(SDL_DISABLE_AVX512F)
  370. # define SDL_AVX512F_INTRINSICS 1
  371. # include <immintrin.h>
  372. # endif
  373. #endif /* defined(__x86_64__) || defined(_M_X64) || defined(__i386__) || defined(_M_IX86) */
  374. #endif /* SDL_intrin_h_ */