simde-common.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /* Copyright (c) 2017-2019 Evan Nemerson <[email protected]>
  2. *
  3. * Permission is hereby granted, free of charge, to any person
  4. * obtaining a copy of this software and associated documentation
  5. * files (the "Software"), to deal in the Software without
  6. * restriction, including without limitation the rights to use, copy,
  7. * modify, merge, publish, distribute, sublicense, and/or sell copies
  8. * of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be
  12. * included in all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  18. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  19. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. * SOFTWARE.
  22. */
  23. #if !defined(SIMDE_COMMON_H)
  24. #define SIMDE_COMMON_H
  25. #include "hedley.h"
  26. #include "check.h"
  27. #include "simde-arch.h"
  28. #if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L)
  29. #define SIMDE_ALIGN(alignment) _Alignas(alignment)
  30. #elif (defined(__cplusplus) && (__cplusplus >= 201103L))
  31. #define SIMDE_ALIGN(alignment) alignas(alignment)
  32. #elif HEDLEY_GCC_VERSION_CHECK(2, 95, 0) || \
  33. HEDLEY_CRAY_VERSION_CHECK(8, 4, 0) || \
  34. HEDLEY_IBM_VERSION_CHECK(11, 1, 0) || \
  35. HEDLEY_INTEL_VERSION_CHECK(13, 0, 0) || \
  36. HEDLEY_PGI_VERSION_CHECK(19, 4, 0) || \
  37. HEDLEY_ARM_VERSION_CHECK(4, 1, 0) || \
  38. HEDLEY_TINYC_VERSION_CHECK(0, 9, 24) || \
  39. HEDLEY_TI_VERSION_CHECK(8, 1, 0)
  40. #define SIMDE_ALIGN(alignment) __attribute__((aligned(alignment)))
  41. #elif defined(_MSC_VER) && (!defined(_M_IX86) || defined(_M_AMD64))
  42. #define SIMDE_ALIGN(alignment) __declspec(align(alignment))
  43. #else
  44. #define SIMDE_ALIGN(alignment)
  45. #endif
  46. #define simde_assert_aligned(alignment, val) \
  47. simde_assert_int(((uintptr_t)(val)) % (alignment), ==, 0)
  48. #if HEDLEY_GCC_HAS_ATTRIBUTE(vector_size, 4, 6, 0)
  49. #define SIMDE__ENABLE_GCC_VEC_EXT
  50. #endif
  51. #if !defined(SIMDE_ENABLE_OPENMP) && \
  52. ((defined(_OPENMP) && (_OPENMP >= 201307L)) || \
  53. (defined(_OPENMP_SIMD) && (_OPENMP_SIMD >= 201307L)))
  54. #define SIMDE_ENABLE_OPENMP
  55. #endif
  56. #if !defined(SIMDE_ENABLE_CILKPLUS) && defined(__cilk)
  57. #define SIMDE_ENABLE_CILKPLUS
  58. #endif
  59. #if defined(SIMDE_ENABLE_OPENMP)
  60. #define SIMDE__VECTORIZE _Pragma("omp simd")
  61. #define SIMDE__VECTORIZE_SAFELEN(l) HEDLEY_PRAGMA(omp simd safelen(l))
  62. #define SIMDE__VECTORIZE_REDUCTION(r) HEDLEY_PRAGMA(omp simd reduction(r))
  63. #define SIMDE__VECTORIZE_ALIGNED(a) HEDLEY_PRAGMA(omp simd aligned(a))
  64. #elif defined(SIMDE_ENABLE_CILKPLUS)
  65. #define SIMDE__VECTORIZE _Pragma("simd")
  66. #define SIMDE__VECTORIZE_SAFELEN(l) HEDLEY_PRAGMA(simd vectorlength(l))
  67. #define SIMDE__VECTORIZE_REDUCTION(r) HEDLEY_PRAGMA(simd reduction(r))
  68. #define SIMDE__VECTORIZE_ALIGNED(a) HEDLEY_PRAGMA(simd aligned(a))
  69. #elif defined(__INTEL_COMPILER)
  70. #define SIMDE__VECTORIZE _Pragma("simd")
  71. #define SIMDE__VECTORIZE_SAFELEN(l) HEDLEY_PRAGMA(simd vectorlength(l))
  72. #define SIMDE__VECTORIZE_REDUCTION(r) HEDLEY_PRAGMA(simd reduction(r))
  73. #define SIMDE__VECTORIZE_ALIGNED(a)
  74. #elif defined(__clang__)
  75. #define SIMDE__VECTORIZE _Pragma("clang loop vectorize(enable)")
  76. #define SIMDE__VECTORIZE_SAFELEN(l) HEDLEY_PRAGMA(clang loop vectorize_width(l))
  77. #define SIMDE__VECTORIZE_REDUCTION(r) SIMDE__VECTORIZE
  78. #define SIMDE__VECTORIZE_ALIGNED(a)
  79. #elif HEDLEY_GCC_VERSION_CHECK(4, 9, 0)
  80. #define SIMDE__VECTORIZE _Pragma("GCC ivdep")
  81. #define SIMDE__VECTORIZE_SAFELEN(l) SIMDE__VECTORIZE
  82. #define SIMDE__VECTORIZE_REDUCTION(r) SIMDE__VECTORIZE
  83. #define SIMDE__VECTORIZE_ALIGNED(a)
  84. #elif HEDLEY_CRAY_VERSION_CHECK(5, 0, 0)
  85. #define SIMDE__VECTORIZE _Pragma("_CRI ivdep")
  86. #define SIMDE__VECTORIZE_SAFELEN(l) SIMDE__VECTORIZE
  87. #define SIMDE__VECTORIZE_REDUCTION(r) SIMDE__VECTORIZE
  88. #define SIMDE__VECTORIZE_ALIGNED(a)
  89. #else
  90. #define SIMDE__VECTORIZE
  91. #define SIMDE__VECTORIZE_SAFELEN(l)
  92. #define SIMDE__VECTORIZE_REDUCTION(r)
  93. #define SIMDE__VECTORIZE_ALIGNED(a)
  94. #endif
  95. #if HEDLEY_GCC_HAS_ATTRIBUTE(unused, 3, 1, 0)
  96. #define SIMDE__UNUSED __attribute__((__unused__))
  97. #else
  98. #define SIMDE__UNUSED
  99. #endif
  100. #if HEDLEY_GCC_HAS_ATTRIBUTE(artificial, 4, 3, 0)
  101. #define SIMDE__ARTIFICIAL __attribute__((__artificial__))
  102. #else
  103. #define SIMDE__ARTIFICIAL
  104. #endif
  105. /* Intended for checking coverage, you should never use this in
  106. production. */
  107. #if defined(SIMDE_NO_INLINE)
  108. #define SIMDE__FUNCTION_ATTRIBUTES HEDLEY_NEVER_INLINE SIMDE__UNUSED static
  109. #else
  110. #define SIMDE__FUNCTION_ATTRIBUTES HEDLEY_INLINE SIMDE__ARTIFICIAL static
  111. #endif
  112. #if defined(_MSC_VER)
  113. #define SIMDE__BEGIN_DECLS \
  114. HEDLEY_DIAGNOSTIC_PUSH __pragma(warning(disable : 4996 4204)) \
  115. HEDLEY_BEGIN_C_DECLS
  116. #define SIMDE__END_DECLS HEDLEY_DIAGNOSTIC_POP HEDLEY_END_C_DECLS
  117. #else
  118. #define SIMDE__BEGIN_DECLS HEDLEY_BEGIN_C_DECLS
  119. #define SIMDE__END_DECLS HEDLEY_END_C_DECLS
  120. #endif
  121. #if defined(__SIZEOF_INT128__)
  122. #define SIMDE__HAVE_INT128
  123. typedef __int128 simde_int128;
  124. typedef unsigned __int128 simde_uint128;
  125. #endif
  126. /* TODO: we should at least make an attempt to detect the correct
  127. types for simde_float32/float64 instead of just assuming float and
  128. double. */
  129. #if !defined(SIMDE_FLOAT32_TYPE)
  130. #define SIMDE_FLOAT32_TYPE float
  131. #define SIMDE_FLOAT32_C(value) value##f
  132. #else
  133. #define SIMDE_FLOAT32_C(value) ((SIMDE_FLOAT32_TYPE)value)
  134. #endif
  135. typedef SIMDE_FLOAT32_TYPE simde_float32;
  136. HEDLEY_STATIC_ASSERT(sizeof(simde_float32) == 4,
  137. "Unable to find 32-bit floating-point type.");
  138. #if !defined(SIMDE_FLOAT64_TYPE)
  139. #define SIMDE_FLOAT64_TYPE double
  140. #define SIMDE_FLOAT64_C(value) value
  141. #else
  142. #define SIMDE_FLOAT32_C(value) ((SIMDE_FLOAT64_TYPE)value)
  143. #endif
  144. typedef SIMDE_FLOAT64_TYPE simde_float64;
  145. HEDLEY_STATIC_ASSERT(sizeof(simde_float64) == 8,
  146. "Unable to find 64-bit floating-point type.");
  147. /* Whether to assume that the compiler can auto-vectorize reasonably
  148. well. This will cause SIMDe to attempt to compose vector
  149. operations using more simple vector operations instead of minimize
  150. serial work.
  151. As an example, consider the _mm_add_ss(a, b) function from SSE,
  152. which returns { a0 + b0, a1, a2, a3 }. This pattern is repeated
  153. for other operations (sub, mul, etc.).
  154. The naïve implementation would result in loading a0 and b0, adding
  155. them into a temporary variable, then splicing that value into a new
  156. vector with the remaining elements from a.
  157. On platforms which support vectorization, it's generally faster to
  158. simply perform the operation on the entire vector to avoid having
  159. to move data between SIMD registers and non-SIMD registers.
  160. Basically, instead of the temporary variable being (a0 + b0) it
  161. would be a vector of (a + b), which is then combined with a to form
  162. the result.
  163. By default, SIMDe will prefer the pure-vector versions if we detect
  164. a vector ISA extension, but this can be overridden by defining
  165. SIMDE_NO_ASSUME_VECTORIZATION. You can also define
  166. SIMDE_ASSUME_VECTORIZATION if you want to force SIMDe to use the
  167. vectorized version. */
  168. #if !defined(SIMDE_NO_ASSUME_VECTORIZATION) && \
  169. !defined(SIMDE_ASSUME_VECTORIZATION)
  170. #if defined(__SSE__) || defined(__ARM_NEON) || defined(__mips_msa) || \
  171. defined(__ALTIVEC__)
  172. #define SIMDE_ASSUME_VECTORIZATION
  173. #endif
  174. #endif
  175. /* GCC and clang have built-in functions to handle shuffling of
  176. vectors, but the implementations are slightly different. This
  177. macro is just an abstraction over them. Note that elem_size is in
  178. bits but vec_size is in bytes. */
  179. #if HEDLEY_CLANG_HAS_BUILTIN(__builtin_shufflevector)
  180. #define SIMDE__SHUFFLE_VECTOR(elem_size, vec_size, a, b, ...) \
  181. __builtin_shufflevector(a, b, __VA_ARGS__)
  182. #elif HEDLEY_GCC_HAS_BUILTIN(__builtin_shuffle, 4, 7, 0) && \
  183. !defined(__INTEL_COMPILER)
  184. #define SIMDE__SHUFFLE_VECTOR(elem_size, vec_size, a, b, ...) \
  185. __builtin_shuffle(a, b, \
  186. (int##elem_size##_t __attribute__( \
  187. (__vector_size__(vec_size)))){__VA_ARGS__})
  188. #endif
  189. /* Some algorithms are iterative, and fewer iterations means less
  190. accuracy. Lower values here will result in faster, but less
  191. accurate, calculations for some functions. */
  192. #if !defined(SIMDE_ACCURACY_ITERS)
  193. #define SIMDE_ACCURACY_ITERS 2
  194. #endif
  195. /* This will probably move into Hedley at some point, but I'd like to
  196. more thoroughly check for other compilers which define __GNUC__
  197. first. */
  198. #if defined(SIMDE__REALLY_GCC)
  199. #undef SIMDE__REALLY_GCC
  200. #endif
  201. #if !defined(__GNUC__) || defined(__clang__) || defined(__INTEL_COMPILER)
  202. #define SIMDE__REALLY_GCC 0
  203. #else
  204. #define SIMDE__REALLY_GCC 1
  205. #endif
  206. #if defined(SIMDE__ASSUME_ALIGNED)
  207. #undef SIMDE__ASSUME_ALIGNED
  208. #endif
  209. #if HEDLEY_INTEL_VERSION_CHECK(9, 0, 0)
  210. #define SIMDE__ASSUME_ALIGNED(ptr, align) __assume_aligned(ptr, align)
  211. #elif HEDLEY_MSVC_VERSION_CHECK(13, 10, 0)
  212. #define SIMDE__ASSUME_ALIGNED(ptr, align) \
  213. __assume((((char *)ptr) - ((char *)0)) % (align) == 0)
  214. #elif HEDLEY_GCC_HAS_BUILTIN(__builtin_assume_aligned, 4, 7, 0)
  215. #define SIMDE__ASSUME_ALIGNED(ptr, align) \
  216. (ptr = (__typeof__(ptr))__builtin_assume_aligned((ptr), align))
  217. #elif HEDLEY_CLANG_HAS_BUILTIN(__builtin_assume)
  218. #define SIMDE__ASSUME_ALIGNED(ptr, align) \
  219. __builtin_assume((((char *)ptr) - ((char *)0)) % (align) == 0)
  220. #elif HEDLEY_GCC_HAS_BUILTIN(__builtin_unreachable, 4, 5, 0)
  221. #define SIMDE__ASSUME_ALIGNED(ptr, align) \
  222. ((((char *)ptr) - ((char *)0)) % (align) == 0) \
  223. ? (1) \
  224. : (__builtin_unreachable(), 0)
  225. #else
  226. #define SIMDE__ASSUME_ALIGNED(ptr, align)
  227. #endif
  228. /* Sometimes we run into problems with specific versions of compilers
  229. which make the native versions unusable for us. Often this is due
  230. to missing functions, sometimes buggy implementations, etc. These
  231. macros are how we check for specific bugs. As they are fixed we'll
  232. start only defining them for problematic compiler versions. */
  233. #if !defined(SIMDE_IGNORE_COMPILER_BUGS)
  234. #if SIMDE__REALLY_GCC
  235. #if !HEDLEY_GCC_VERSION_CHECK(4, 9, 0)
  236. #define SIMDE_BUG_GCC_REV_208793
  237. #endif
  238. #if !HEDLEY_GCC_VERSION_CHECK(5, 0, 0)
  239. #define SIMDE_BUG_GCC_BAD_MM_SRA_EPI32 /* TODO: find relevant bug or commit */
  240. #endif
  241. #if !HEDLEY_GCC_VERSION_CHECK(4, 6, 0)
  242. #define SIMDE_BUG_GCC_BAD_MM_EXTRACT_EPI8 /* TODO: find relevant bug or commit */
  243. #endif
  244. #endif
  245. #if defined(__EMSCRIPTEN__)
  246. #define SIMDE_BUG_EMSCRIPTEN_MISSING_IMPL /* Placeholder for (as yet) unfiled issues. */
  247. #define SIMDE_BUG_EMSCRIPTEN_5242
  248. #endif
  249. #endif
  250. #endif /* !defined(SIMDE_COMMON_H) */