1
0

simde-common.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  1. /* SPDX-License-Identifier: MIT
  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. * Copyright:
  24. * 2017-2020 Evan Nemerson <[email protected]>
  25. */
  26. #if !defined(SIMDE_COMMON_H)
  27. #define SIMDE_COMMON_H
  28. #include "hedley.h"
  29. #define SIMDE_VERSION_MAJOR 0
  30. #define SIMDE_VERSION_MINOR 5
  31. #define SIMDE_VERSION_MICRO 0
  32. #define SIMDE_VERSION \
  33. HEDLEY_VERSION_ENCODE(SIMDE_VERSION_MAJOR, SIMDE_VERSION_MINOR, \
  34. SIMDE_VERSION_MICRO)
  35. #include "simde-arch.h"
  36. #include "simde-features.h"
  37. #include "simde-diagnostic.h"
  38. #include <stddef.h>
  39. #include <stdint.h>
  40. #if HEDLEY_HAS_ATTRIBUTE(aligned) || HEDLEY_GCC_VERSION_CHECK(2, 95, 0) || \
  41. HEDLEY_CRAY_VERSION_CHECK(8, 4, 0) || \
  42. HEDLEY_IBM_VERSION_CHECK(11, 1, 0) || \
  43. HEDLEY_INTEL_VERSION_CHECK(13, 0, 0) || \
  44. HEDLEY_PGI_VERSION_CHECK(19, 4, 0) || \
  45. HEDLEY_ARM_VERSION_CHECK(4, 1, 0) || \
  46. HEDLEY_TINYC_VERSION_CHECK(0, 9, 24) || \
  47. HEDLEY_TI_VERSION_CHECK(8, 1, 0)
  48. #define SIMDE_ALIGN(alignment) __attribute__((aligned(alignment)))
  49. #elif defined(_MSC_VER) && !(defined(_M_ARM) && !defined(_M_ARM64))
  50. #define SIMDE_ALIGN(alignment) __declspec(align(alignment))
  51. #elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)
  52. #define SIMDE_ALIGN(alignment) _Alignas(alignment)
  53. #elif defined(__cplusplus) && (__cplusplus >= 201103L)
  54. #define SIMDE_ALIGN(alignment) alignas(alignment)
  55. #else
  56. #define SIMDE_ALIGN(alignment)
  57. #endif
  58. #if HEDLEY_GNUC_VERSION_CHECK(2, 95, 0) || \
  59. HEDLEY_ARM_VERSION_CHECK(4, 1, 0) || \
  60. HEDLEY_IBM_VERSION_CHECK(11, 1, 0)
  61. #define SIMDE_ALIGN_OF(T) (__alignof__(T))
  62. #elif (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)) || \
  63. HEDLEY_HAS_FEATURE(c11_alignof)
  64. #define SIMDE_ALIGN_OF(T) (_Alignof(T))
  65. #elif (defined(__cplusplus) && (__cplusplus >= 201103L)) || \
  66. HEDLEY_HAS_FEATURE(cxx_alignof)
  67. #define SIMDE_ALIGN_OF(T) (alignof(T))
  68. #endif
  69. #if defined(SIMDE_ALIGN_OF)
  70. #define SIMDE_ALIGN_AS(N, T) SIMDE_ALIGN(SIMDE_ALIGN_OF(T))
  71. #else
  72. #define SIMDE_ALIGN_AS(N, T) SIMDE_ALIGN(N)
  73. #endif
  74. #define simde_assert_aligned(alignment, val) \
  75. simde_assert_int(HEDLEY_REINTERPRET_CAST( \
  76. uintptr_t, HEDLEY_REINTERPRET_CAST( \
  77. const void *, (val))) % \
  78. (alignment), \
  79. ==, 0)
  80. #if HEDLEY_HAS_BUILTIN(__builtin_constant_p) || \
  81. HEDLEY_GCC_VERSION_CHECK(3, 4, 0) || \
  82. HEDLEY_INTEL_VERSION_CHECK(13, 0, 0) || \
  83. HEDLEY_TINYC_VERSION_CHECK(0, 9, 19) || \
  84. HEDLEY_ARM_VERSION_CHECK(4, 1, 0) || \
  85. HEDLEY_IBM_VERSION_CHECK(13, 1, 0) || \
  86. HEDLEY_TI_CL6X_VERSION_CHECK(6, 1, 0) || \
  87. (HEDLEY_SUNPRO_VERSION_CHECK(5, 10, 0) && !defined(__cplusplus)) || \
  88. HEDLEY_CRAY_VERSION_CHECK(8, 1, 0)
  89. #define SIMDE_CHECK_CONSTANT_(expr) (__builtin_constant_p(expr))
  90. #elif defined(__cplusplus) && (__cplusplus > 201703L)
  91. #include <type_traits>
  92. #define SIMDE_CHECK_CONSTANT_(expr) (std::is_constant_evaluated())
  93. #endif
  94. /* diagnose_if + __builtin_constant_p was broken until clang 9,
  95. * which is when __FILE_NAME__ was added. */
  96. #if defined(SIMDE_CHECK_CONSTANT_) && defined(__FILE_NAME__)
  97. #define SIMDE_REQUIRE_CONSTANT(arg) \
  98. HEDLEY_REQUIRE_MSG(SIMDE_CHECK_CONSTANT_(arg), \
  99. "`" #arg "' must be constant")
  100. #else
  101. #define SIMDE_REQUIRE_CONSTANT(arg)
  102. #endif
  103. #define SIMDE_REQUIRE_RANGE(arg, min, max) \
  104. HEDLEY_REQUIRE_MSG((((arg) >= (min)) && ((arg) <= (max))), \
  105. "'" #arg "' must be in [" #min ", " #max "]")
  106. #define SIMDE_REQUIRE_CONSTANT_RANGE(arg, min, max) \
  107. SIMDE_REQUIRE_CONSTANT(arg) \
  108. SIMDE_REQUIRE_RANGE(arg, min, max)
  109. /* SIMDE_ASSUME_ALIGNED allows you to (try to) tell the compiler
  110. * that a pointer is aligned to an `alignment`-byte boundary. */
  111. #if HEDLEY_HAS_BUILTIN(__builtin_assume_aligned) || \
  112. HEDLEY_GCC_VERSION_CHECK(4, 7, 0)
  113. #define SIMDE_ASSUME_ALIGNED(alignment, v) \
  114. HEDLEY_REINTERPRET_CAST(__typeof__(v), \
  115. __builtin_assume_aligned(v, alignment))
  116. #elif defined(__cplusplus) && (__cplusplus > 201703L)
  117. #define SIMDE_ASSUME_ALIGNED(alignment, v) std::assume_aligned<alignment>(v)
  118. #elif HEDLEY_INTEL_VERSION_CHECK(13, 0, 0)
  119. #define SIMDE_ASSUME_ALIGNED(alignment, v) \
  120. (__extension__({ \
  121. __typeof__(v) simde_assume_aligned_t_ = (v); \
  122. __assume_aligned(simde_assume_aligned_t_, alignment); \
  123. simde_assume_aligned_t_; \
  124. }))
  125. #else
  126. #define SIMDE_ASSUME_ALIGNED(alignment, v) (v)
  127. #endif
  128. /* SIMDE_ALIGN_CAST allows you to convert to a type with greater
  129. * aligment requirements without triggering a warning. */
  130. #if HEDLEY_HAS_WARNING("-Wcast-align")
  131. #define SIMDE_ALIGN_CAST(T, v) \
  132. (__extension__({ \
  133. HEDLEY_DIAGNOSTIC_PUSH \
  134. _Pragma("clang diagnostic ignored \"-Wcast-align\"") \
  135. T simde_r_ = HEDLEY_REINTERPRET_CAST(T, v); \
  136. HEDLEY_DIAGNOSTIC_POP \
  137. simde_r_; \
  138. }))
  139. #else
  140. #define SIMDE_ALIGN_CAST(T, v) HEDLEY_REINTERPRET_CAST(T, v)
  141. #endif
  142. #if (HEDLEY_HAS_ATTRIBUTE(may_alias) && !defined(HEDLEY_SUNPRO_VERSION)) || \
  143. HEDLEY_GCC_VERSION_CHECK(3, 3, 0) || \
  144. HEDLEY_INTEL_VERSION_CHECK(13, 0, 0) || \
  145. HEDLEY_IBM_VERSION_CHECK(13, 1, 0)
  146. #define SIMDE_MAY_ALIAS __attribute__((__may_alias__))
  147. #else
  148. #define SIMDE_MAY_ALIAS
  149. #endif
  150. /* Lots of compilers support GCC-style vector extensions, but many
  151. don't support all the features. Define different macros depending
  152. on support for
  153. * SIMDE_VECTOR - Declaring a vector.
  154. * SIMDE_VECTOR_OPS - basic operations (binary and unary).
  155. * SIMDE_VECTOR_SCALAR - For binary operators, the second argument
  156. can be a scalar, in which case the result is as if that scalar
  157. had been broadcast to all lanes of a vector.
  158. * SIMDE_VECTOR_SUBSCRIPT - Supports array subscript notation for
  159. extracting/inserting a single element.=
  160. SIMDE_VECTOR can be assumed if any others are defined, the
  161. others are independent. */
  162. #if !defined(SIMDE_NO_VECTOR)
  163. #if HEDLEY_GCC_VERSION_CHECK(4, 8, 0)
  164. #define SIMDE_VECTOR(size) __attribute__((__vector_size__(size)))
  165. #define SIMDE_VECTOR_OPS
  166. #define SIMDE_VECTOR_SCALAR
  167. #define SIMDE_VECTOR_SUBSCRIPT
  168. #elif HEDLEY_INTEL_VERSION_CHECK(16, 0, 0)
  169. #define SIMDE_VECTOR(size) __attribute__((__vector_size__(size)))
  170. #define SIMDE_VECTOR_OPS
  171. /* ICC only supports SIMDE_VECTOR_SCALAR for constants */
  172. #define SIMDE_VECTOR_SUBSCRIPT
  173. #elif HEDLEY_GCC_VERSION_CHECK(4, 1, 0) || HEDLEY_INTEL_VERSION_CHECK(13, 0, 0)
  174. #define SIMDE_VECTOR(size) __attribute__((__vector_size__(size)))
  175. #define SIMDE_VECTOR_OPS
  176. #elif HEDLEY_SUNPRO_VERSION_CHECK(5, 12, 0)
  177. #define SIMDE_VECTOR(size) __attribute__((__vector_size__(size)))
  178. #elif HEDLEY_HAS_ATTRIBUTE(vector_size)
  179. #define SIMDE_VECTOR(size) __attribute__((__vector_size__(size)))
  180. #define SIMDE_VECTOR_OPS
  181. #define SIMDE_VECTOR_SUBSCRIPT
  182. #if HEDLEY_HAS_ATTRIBUTE(diagnose_if) /* clang 4.0 */
  183. #define SIMDE_VECTOR_SCALAR
  184. #endif
  185. #endif
  186. /* GCC and clang have built-in functions to handle shuffling and
  187. converting of vectors, but the implementations are slightly
  188. different. This macro is just an abstraction over them. Note that
  189. elem_size is in bits but vec_size is in bytes. */
  190. #if !defined(SIMDE_NO_SHUFFLE_VECTOR) && defined(SIMDE_VECTOR_SUBSCRIPT)
  191. HEDLEY_DIAGNOSTIC_PUSH
  192. /* We don't care about -Wvariadic-macros; all compilers that support
  193. * shufflevector/shuffle support them. */
  194. #if HEDLEY_HAS_WARNING("-Wc++98-compat-pedantic")
  195. #pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
  196. #endif
  197. #if HEDLEY_HAS_WARNING("-Wvariadic-macros") || HEDLEY_GCC_VERSION_CHECK(4, 0, 0)
  198. #pragma GCC diagnostic ignored "-Wvariadic-macros"
  199. #endif
  200. #if HEDLEY_HAS_BUILTIN(__builtin_shufflevector)
  201. #define SIMDE_SHUFFLE_VECTOR_(elem_size, vec_size, a, b, ...) \
  202. __builtin_shufflevector(a, b, __VA_ARGS__)
  203. #elif HEDLEY_GCC_HAS_BUILTIN(__builtin_shuffle, 4, 7, 0) && \
  204. !defined(__INTEL_COMPILER)
  205. #define SIMDE_SHUFFLE_VECTOR_(elem_size, vec_size, a, b, ...) \
  206. (__extension__({ \
  207. int##elem_size##_t SIMDE_VECTOR(vec_size) \
  208. simde_shuffle_ = {__VA_ARGS__}; \
  209. __builtin_shuffle(a, b, simde_shuffle_); \
  210. }))
  211. #endif
  212. HEDLEY_DIAGNOSTIC_POP
  213. #endif
  214. /* TODO: this actually works on XL C/C++ without SIMDE_VECTOR_SUBSCRIPT
  215. but the code needs to be refactored a bit to take advantage. */
  216. #if !defined(SIMDE_NO_CONVERT_VECTOR) && defined(SIMDE_VECTOR_SUBSCRIPT)
  217. #if HEDLEY_HAS_BUILTIN(__builtin_convertvector) || \
  218. HEDLEY_GCC_VERSION_CHECK(9, 0, 0)
  219. #if HEDLEY_GCC_VERSION_CHECK(9, 0, 0) && !HEDLEY_GCC_VERSION_CHECK(9, 3, 0)
  220. /* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=93557 */
  221. #define SIMDE_CONVERT_VECTOR_(to, from) \
  222. ((to) = (__extension__({ \
  223. __typeof__(from) from_ = (from); \
  224. ((void)from_); \
  225. __builtin_convertvector(from_, __typeof__(to)); \
  226. })))
  227. #else
  228. #define SIMDE_CONVERT_VECTOR_(to, from) \
  229. ((to) = __builtin_convertvector((from), __typeof__(to)))
  230. #endif
  231. #endif
  232. #endif
  233. #endif
  234. /* Since we currently require SUBSCRIPT before using a vector in a
  235. union, we define these as dependencies of SUBSCRIPT. They are
  236. likely to disappear in the future, once SIMDe learns how to make
  237. use of vectors without using the union members. Do not use them
  238. in your code unless you're okay with it breaking when SIMDe
  239. changes. */
  240. #if defined(SIMDE_VECTOR_SUBSCRIPT)
  241. #if defined(SIMDE_VECTOR_OPS)
  242. #define SIMDE_VECTOR_SUBSCRIPT_OPS
  243. #endif
  244. #if defined(SIMDE_VECTOR_SCALAR)
  245. #define SIMDE_VECTOR_SUBSCRIPT_SCALAR
  246. #endif
  247. #endif
  248. #if !defined(SIMDE_ENABLE_OPENMP) && \
  249. ((defined(_OPENMP) && (_OPENMP >= 201307L)) || \
  250. (defined(_OPENMP_SIMD) && (_OPENMP_SIMD >= 201307L)))
  251. #define SIMDE_ENABLE_OPENMP
  252. #endif
  253. #if !defined(SIMDE_ENABLE_CILKPLUS) && \
  254. (defined(__cilk) || defined(HEDLEY_INTEL_VERSION))
  255. #define SIMDE_ENABLE_CILKPLUS
  256. #endif
  257. #if defined(SIMDE_ENABLE_OPENMP)
  258. #define SIMDE_VECTORIZE _Pragma("omp simd")
  259. #define SIMDE_VECTORIZE_SAFELEN(l) HEDLEY_PRAGMA(omp simd safelen(l))
  260. #define SIMDE_VECTORIZE_REDUCTION(r) HEDLEY_PRAGMA(omp simd reduction(r))
  261. #define SIMDE_VECTORIZE_ALIGNED(a) HEDLEY_PRAGMA(omp simd aligned(a))
  262. #elif defined(SIMDE_ENABLE_CILKPLUS)
  263. #define SIMDE_VECTORIZE _Pragma("simd")
  264. #define SIMDE_VECTORIZE_SAFELEN(l) HEDLEY_PRAGMA(simd vectorlength(l))
  265. #define SIMDE_VECTORIZE_REDUCTION(r) HEDLEY_PRAGMA(simd reduction(r))
  266. #define SIMDE_VECTORIZE_ALIGNED(a) HEDLEY_PRAGMA(simd aligned(a))
  267. #elif defined(__clang__) && !defined(HEDLEY_IBM_VERSION)
  268. #define SIMDE_VECTORIZE _Pragma("clang loop vectorize(enable)")
  269. #define SIMDE_VECTORIZE_SAFELEN(l) HEDLEY_PRAGMA(clang loop vectorize_width(l))
  270. #define SIMDE_VECTORIZE_REDUCTION(r) SIMDE_VECTORIZE
  271. #define SIMDE_VECTORIZE_ALIGNED(a)
  272. #elif HEDLEY_GCC_VERSION_CHECK(4, 9, 0)
  273. #define SIMDE_VECTORIZE _Pragma("GCC ivdep")
  274. #define SIMDE_VECTORIZE_SAFELEN(l) SIMDE_VECTORIZE
  275. #define SIMDE_VECTORIZE_REDUCTION(r) SIMDE_VECTORIZE
  276. #define SIMDE_VECTORIZE_ALIGNED(a)
  277. #elif HEDLEY_CRAY_VERSION_CHECK(5, 0, 0)
  278. #define SIMDE_VECTORIZE _Pragma("_CRI ivdep")
  279. #define SIMDE_VECTORIZE_SAFELEN(l) SIMDE_VECTORIZE
  280. #define SIMDE_VECTORIZE_REDUCTION(r) SIMDE_VECTORIZE
  281. #define SIMDE_VECTORIZE_ALIGNED(a)
  282. #else
  283. #define SIMDE_VECTORIZE
  284. #define SIMDE_VECTORIZE_SAFELEN(l)
  285. #define SIMDE_VECTORIZE_REDUCTION(r)
  286. #define SIMDE_VECTORIZE_ALIGNED(a)
  287. #endif
  288. #define SIMDE_MASK_NZ_(v, mask) (((v) & (mask)) | !((v) & (mask)))
  289. /* Intended for checking coverage, you should never use this in
  290. production. */
  291. #if defined(SIMDE_NO_INLINE)
  292. #define SIMDE_FUNCTION_ATTRIBUTES HEDLEY_NEVER_INLINE static
  293. #else
  294. #define SIMDE_FUNCTION_ATTRIBUTES HEDLEY_ALWAYS_INLINE static
  295. #endif
  296. #if HEDLEY_HAS_ATTRIBUTE(unused) || HEDLEY_GCC_VERSION_CHECK(2, 95, 0)
  297. #define SIMDE_FUNCTION_POSSIBLY_UNUSED_ __attribute__((__unused__))
  298. #else
  299. #define SIMDE_FUNCTION_POSSIBLY_UNUSED_
  300. #endif
  301. #if HEDLEY_HAS_WARNING("-Wused-but-marked-unused")
  302. #define SIMDE_DIAGNOSTIC_DISABLE_USED_BUT_MARKED_UNUSED \
  303. _Pragma("clang diagnostic ignored \"-Wused-but-marked-unused\"")
  304. #else
  305. #define SIMDE_DIAGNOSTIC_DISABLE_USED_BUT_MARKED_UNUSED
  306. #endif
  307. #if defined(_MSC_VER)
  308. #define SIMDE_BEGIN_DECLS_ \
  309. HEDLEY_DIAGNOSTIC_PUSH __pragma(warning(disable : 4996 4204)) \
  310. HEDLEY_BEGIN_C_DECLS
  311. #define SIMDE_END_DECLS_ HEDLEY_DIAGNOSTIC_POP HEDLEY_END_C_DECLS
  312. #else
  313. #define SIMDE_BEGIN_DECLS_ \
  314. HEDLEY_DIAGNOSTIC_PUSH \
  315. SIMDE_DIAGNOSTIC_DISABLE_USED_BUT_MARKED_UNUSED \
  316. HEDLEY_BEGIN_C_DECLS
  317. #define SIMDE_END_DECLS_ \
  318. HEDLEY_END_C_DECLS \
  319. HEDLEY_DIAGNOSTIC_POP
  320. #endif
  321. #if HEDLEY_HAS_WARNING("-Wpedantic")
  322. #define SIMDE_DIAGNOSTIC_DISABLE_INT128 \
  323. _Pragma("clang diagnostic ignored \"-Wpedantic\"")
  324. #elif defined(HEDLEY_GCC_VERSION)
  325. #define SIMDE_DIAGNOSTIC_DISABLE_INT128 \
  326. _Pragma("GCC diagnostic ignored \"-Wpedantic\"")
  327. #else
  328. #define SIMDE_DIAGNOSTIC_DISABLE_INT128
  329. #endif
  330. #if defined(__SIZEOF_INT128__)
  331. #define SIMDE_HAVE_INT128_
  332. HEDLEY_DIAGNOSTIC_PUSH
  333. SIMDE_DIAGNOSTIC_DISABLE_INT128
  334. typedef __int128 simde_int128;
  335. typedef unsigned __int128 simde_uint128;
  336. HEDLEY_DIAGNOSTIC_POP
  337. #endif
  338. #if !defined(SIMDE_ENDIAN_LITTLE)
  339. #define SIMDE_ENDIAN_LITTLE 1234
  340. #endif
  341. #if !defined(SIMDE_ENDIAN_BIG)
  342. #define SIMDE_ENDIAN_BIG 4321
  343. #endif
  344. #if !defined(SIMDE_ENDIAN_ORDER)
  345. /* GCC (and compilers masquerading as GCC) define __BYTE_ORDER__. */
  346. #if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && \
  347. (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
  348. #define SIMDE_ENDIAN_ORDER SIMDE_ENDIAN_LITTLE
  349. #elif defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \
  350. (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
  351. #define SIMDE_ENDIAN_ORDER SIMDE_ENDIAN_BIG
  352. /* TI defines _BIG_ENDIAN or _LITTLE_ENDIAN */
  353. #elif defined(_BIG_ENDIAN)
  354. #define SIMDE_ENDIAN_ORDER SIMDE_ENDIAN_BIG
  355. #elif defined(_LITTLE_ENDIAN)
  356. #define SIMDE_ENDIAN_ORDER SIMDE_ENDIAN_LITTLE
  357. /* We know the endianness of some common architectures. Common
  358. * architectures not listed (ARM, POWER, MIPS, etc.) here are
  359. * bi-endian. */
  360. #elif defined(__amd64) || defined(_M_X64) || defined(__i386) || defined(_M_IX86)
  361. #define SIMDE_ENDIAN_ORDER SIMDE_ENDIAN_LITTLE
  362. #elif defined(__s390x__) || defined(__zarch__)
  363. #define SIMDE_ENDIAN_ORDER SIMDE_ENDIAN_BIG
  364. /* Looks like we'll have to rely on the platform. If we're missing a
  365. * platform, please let us know. */
  366. #elif defined(_WIN32)
  367. #define SIMDE_ENDIAN_ORDER SIMDE_ENDIAN_LITTLE
  368. #elif defined(sun) || defined(__sun) /* Solaris */
  369. #include <sys/byteorder.h>
  370. #if defined(_LITTLE_ENDIAN)
  371. #define SIMDE_ENDIAN_ORDER SIMDE_ENDIAN_LITTLE
  372. #elif defined(_BIG_ENDIAN)
  373. #define SIMDE_ENDIAN_ORDER SIMDE_ENDIAN_BIG
  374. #endif
  375. #elif defined(__APPLE__)
  376. #include <libkern/OSByteOrder.h>
  377. #if defined(__LITTLE_ENDIAN__)
  378. #define SIMDE_ENDIAN_ORDER SIMDE_ENDIAN_LITTLE
  379. #elif defined(__BIG_ENDIAN__)
  380. #define SIMDE_ENDIAN_ORDER SIMDE_ENDIAN_BIG
  381. #endif
  382. #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || \
  383. defined(__bsdi__) || defined(__DragonFly__) || defined(BSD)
  384. #include <machine/endian.h>
  385. #if defined(__BYTE_ORDER) && (__BYTE_ORDER == __LITTLE_ENDIAN)
  386. #define SIMDE_ENDIAN_ORDER SIMDE_ENDIAN_LITTLE
  387. #elif defined(__BYTE_ORDER) && (__BYTE_ORDER == __BIG_ENDIAN)
  388. #define SIMDE_ENDIAN_ORDER SIMDE_ENDIAN_BIG
  389. #endif
  390. #elif defined(__linux__) || defined(__linux) || defined(__gnu_linux__)
  391. #include <endian.h>
  392. #if defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && \
  393. (__BYTE_ORDER == __LITTLE_ENDIAN)
  394. #define SIMDE_ENDIAN_ORDER SIMDE_ENDIAN_LITTLE
  395. #elif defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && \
  396. (__BYTE_ORDER == __BIG_ENDIAN)
  397. #define SIMDE_ENDIAN_ORDER SIMDE_ENDIAN_BIG
  398. #endif
  399. #endif
  400. #endif
  401. #if HEDLEY_HAS_BUILTIN(__builtin_bswap64) || \
  402. HEDLEY_GCC_VERSION_CHECK(4, 3, 0) || \
  403. HEDLEY_IBM_VERSION_CHECK(13, 1, 0) || \
  404. HEDLEY_INTEL_VERSION_CHECK(13, 0, 0)
  405. #define simde_bswap64(v) __builtin_bswap64(v)
  406. #elif HEDLEY_MSVC_VERSION_CHECK(13, 10, 0)
  407. #define simde_bswap64(v) _byteswap_uint64(v)
  408. #else
  409. SIMDE_FUNCTION_ATTRIBUTES
  410. uint64_t simde_bswap64(uint64_t v)
  411. {
  412. return ((v & (((uint64_t)0xff) << 56)) >> 56) |
  413. ((v & (((uint64_t)0xff) << 48)) >> 40) |
  414. ((v & (((uint64_t)0xff) << 40)) >> 24) |
  415. ((v & (((uint64_t)0xff) << 32)) >> 8) |
  416. ((v & (((uint64_t)0xff) << 24)) << 8) |
  417. ((v & (((uint64_t)0xff) << 16)) << 24) |
  418. ((v & (((uint64_t)0xff) << 8)) << 40) |
  419. ((v & (((uint64_t)0xff))) << 56);
  420. }
  421. #endif
  422. #if !defined(SIMDE_ENDIAN_ORDER)
  423. #error Unknown byte order; please file a bug
  424. #else
  425. #if SIMDE_ENDIAN_ORDER == SIMDE_ENDIAN_LITTLE
  426. #define simde_endian_bswap64_be(value) simde_bswap64(value)
  427. #define simde_endian_bswap64_le(value) (value)
  428. #elif SIMDE_ENDIAN_ORDER == SIMDE_ENDIAN_BIG
  429. #define simde_endian_bswap64_be(value) (value)
  430. #define simde_endian_bswap64_le(value) simde_bswap64(value)
  431. #endif
  432. #endif
  433. /* TODO: we should at least make an attempt to detect the correct
  434. types for simde_float32/float64 instead of just assuming float and
  435. double. */
  436. #if !defined(SIMDE_FLOAT32_TYPE)
  437. #define SIMDE_FLOAT32_TYPE float
  438. #define SIMDE_FLOAT32_C(value) value##f
  439. #else
  440. #define SIMDE_FLOAT32_C(value) ((SIMDE_FLOAT32_TYPE)value)
  441. #endif
  442. typedef SIMDE_FLOAT32_TYPE simde_float32;
  443. #if !defined(SIMDE_FLOAT64_TYPE)
  444. #define SIMDE_FLOAT64_TYPE double
  445. #define SIMDE_FLOAT64_C(value) value
  446. #else
  447. #define SIMDE_FLOAT32_C(value) ((SIMDE_FLOAT64_TYPE)value)
  448. #endif
  449. typedef SIMDE_FLOAT64_TYPE simde_float64;
  450. /* Whether to assume that the compiler can auto-vectorize reasonably
  451. well. This will cause SIMDe to attempt to compose vector
  452. operations using more simple vector operations instead of minimize
  453. serial work.
  454. As an example, consider the _mm_add_ss(a, b) function from SSE,
  455. which returns { a0 + b0, a1, a2, a3 }. This pattern is repeated
  456. for other operations (sub, mul, etc.).
  457. The naïve implementation would result in loading a0 and b0, adding
  458. them into a temporary variable, then splicing that value into a new
  459. vector with the remaining elements from a.
  460. On platforms which support vectorization, it's generally faster to
  461. simply perform the operation on the entire vector to avoid having
  462. to move data between SIMD registers and non-SIMD registers.
  463. Basically, instead of the temporary variable being (a0 + b0) it
  464. would be a vector of (a + b), which is then combined with a to form
  465. the result.
  466. By default, SIMDe will prefer the pure-vector versions if we detect
  467. a vector ISA extension, but this can be overridden by defining
  468. SIMDE_NO_ASSUME_VECTORIZATION. You can also define
  469. SIMDE_ASSUME_VECTORIZATION if you want to force SIMDe to use the
  470. vectorized version. */
  471. #if !defined(SIMDE_NO_ASSUME_VECTORIZATION) && \
  472. !defined(SIMDE_ASSUME_VECTORIZATION)
  473. #if defined(__SSE__) || defined(__ARM_NEON) || defined(__mips_msa) || \
  474. defined(__ALTIVEC__) || defined(__wasm_simd128__)
  475. #define SIMDE_ASSUME_VECTORIZATION
  476. #endif
  477. #endif
  478. #if HEDLEY_HAS_WARNING("-Wbad-function-cast")
  479. #define SIMDE_CONVERT_FTOI(T, v) \
  480. HEDLEY_DIAGNOSTIC_PUSH \
  481. _Pragma("clang diagnostic ignored \"-Wbad-function-cast\"") \
  482. HEDLEY_STATIC_CAST(T, (v)) HEDLEY_DIAGNOSTIC_POP
  483. #else
  484. #define SIMDE_CONVERT_FTOI(T, v) ((T)(v))
  485. #endif
  486. #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)
  487. #define SIMDE_CHECKED_REINTERPRET_CAST(to, from, value) \
  488. (_Generic((value), to : (value), from : ((to)(value))))
  489. #define SIMDE_CHECKED_STATIC_CAST(to, from, value) \
  490. (_Generic((value), to : (value), from : ((to)(value))))
  491. #else
  492. #define SIMDE_CHECKED_REINTERPRET_CAST(to, from, value) \
  493. HEDLEY_REINTERPRET_CAST(to, value)
  494. #define SIMDE_CHECKED_STATIC_CAST(to, from, value) HEDLEY_STATIC_CAST(to, value)
  495. #endif
  496. #if HEDLEY_HAS_WARNING("-Wfloat-equal")
  497. #define SIMDE_DIAGNOSTIC_DISABLE_FLOAT_EQUAL \
  498. _Pragma("clang diagnostic ignored \"-Wfloat-equal\"")
  499. #elif HEDLEY_GCC_VERSION_CHECK(3, 0, 0)
  500. #define SIMDE_DIAGNOSTIC_DISABLE_FLOAT_EQUAL \
  501. _Pragma("GCC diagnostic ignored \"-Wfloat-equal\"")
  502. #else
  503. #define SIMDE_DIAGNOSTIC_DISABLE_FLOAT_EQUAL
  504. #endif
  505. /* Some functions can trade accuracy for speed. For those functions
  506. you can control the trade-off using this macro. Possible values:
  507. 0: prefer speed
  508. 1: reasonable trade-offs
  509. 2: prefer accuracy */
  510. #if !defined(SIMDE_ACCURACY_PREFERENCE)
  511. #define SIMDE_ACCURACY_PREFERENCE 1
  512. #endif
  513. #if defined(__STDC_HOSTED__)
  514. #define SIMDE_STDC_HOSTED __STDC_HOSTED__
  515. #else
  516. #if defined(HEDLEY_PGI_VERSION_CHECK) || defined(HEDLEY_MSVC_VERSION_CHECK)
  517. #define SIMDE_STDC_HOSTED 1
  518. #else
  519. #define SIMDE_STDC_HOSTED 0
  520. #endif
  521. #endif
  522. /* Try to deal with environments without a standard library. */
  523. #if !defined(simde_memcpy) || !defined(simde_memset)
  524. #if !defined(SIMDE_NO_STRING_H) && defined(__has_include)
  525. #if __has_include(<string.h>)
  526. #include <string.h>
  527. #if !defined(simde_memcpy)
  528. #define simde_memcpy(dest, src, n) memcpy(dest, src, n)
  529. #endif
  530. #if !defined(simde_memset)
  531. #define simde_memset(s, c, n) memset(s, c, n)
  532. #endif
  533. #else
  534. #define SIMDE_NO_STRING_H
  535. #endif
  536. #endif
  537. #endif
  538. #if !defined(simde_memcpy) || !defined(simde_memset)
  539. #if !defined(SIMDE_NO_STRING_H) && (SIMDE_STDC_HOSTED == 1)
  540. #include <string.h>
  541. #if !defined(simde_memcpy)
  542. #define simde_memcpy(dest, src, n) memcpy(dest, src, n)
  543. #endif
  544. #if !defined(simde_memset)
  545. #define simde_memset(s, c, n) memset(s, c, n)
  546. #endif
  547. #elif (HEDLEY_HAS_BUILTIN(__builtin_memcpy) && \
  548. HEDLEY_HAS_BUILTIN(__builtin_memset)) || \
  549. HEDLEY_GCC_VERSION_CHECK(4, 2, 0)
  550. #if !defined(simde_memcpy)
  551. #define simde_memcpy(dest, src, n) __builtin_memcpy(dest, src, n)
  552. #endif
  553. #if !defined(simde_memset)
  554. #define simde_memset(s, c, n) __builtin_memset(s, c, n)
  555. #endif
  556. #else
  557. /* These are meant to be portable, not fast. If you're hitting them you
  558. * should think about providing your own (by defining the simde_memcpy
  559. * macro prior to including any SIMDe files) or submitting a patch to
  560. * SIMDe so we can detect your system-provided memcpy/memset, like by
  561. * adding your compiler to the checks for __builtin_memcpy and/or
  562. * __builtin_memset. */
  563. #if !defined(simde_memcpy)
  564. SIMDE_FUNCTION_ATTRIBUTES
  565. void simde_memcpy_(void *dest, const void *src, size_t len)
  566. {
  567. char *dest_ = HEDLEY_STATIC_CAST(char *, dest);
  568. char *src_ = HEDLEY_STATIC_CAST(const char *, src);
  569. for (size_t i = 0; i < len; i++) {
  570. dest_[i] = src_[i];
  571. }
  572. }
  573. #define simde_memcpy(dest, src, n) simde_memcpy_(dest, src, n)
  574. #endif
  575. #if !defined(simde_memset)
  576. SIMDE_FUNCTION_ATTRIBUTES
  577. void simde_memset_(void *s, int c, size_t len)
  578. {
  579. char *s_ = HEDLEY_STATIC_CAST(char *, s);
  580. char c_ = HEDLEY_STATIC_CAST(char, c);
  581. for (size_t i = 0; i < len; i++) {
  582. s_[i] = c_[i];
  583. }
  584. }
  585. #define simde_memset(s, c, n) simde_memset_(s, c, n)
  586. #endif
  587. #endif /* !defined(SIMDE_NO_STRING_H) && (SIMDE_STDC_HOSTED == 1) */
  588. #endif /* !defined(simde_memcpy) || !defined(simde_memset) */
  589. #include "simde-math.h"
  590. #if defined(FE_ALL_EXCEPT)
  591. #define SIMDE_HAVE_FENV_H
  592. #elif defined(__has_include)
  593. #if __has_include(<fenv.h>)
  594. #include <fenv.h>
  595. #define SIMDE_HAVE_FENV_H
  596. #endif
  597. #elif SIMDE_STDC_HOSTED == 1
  598. #include <fenv.h>
  599. #define SIMDE_HAVE_FENV_H
  600. #endif
  601. #if defined(EXIT_FAILURE)
  602. #define SIMDE_HAVE_STDLIB_H
  603. #elif defined(__has_include)
  604. #if __has_include(<stdlib.h>)
  605. #include <stdlib.h>
  606. #define SIMDE_HAVE_STDLIB_H
  607. #endif
  608. #elif SIMDE_STDC_HOSTED == 1
  609. #include <stdlib.h>
  610. #define SIMDE_HAVE_STDLIB_H
  611. #endif
  612. #if defined(__has_include)
  613. #if defined(__cplusplus) && (__cplusplus >= 201103L) && __has_include(<cfenv>)
  614. #include <cfenv>
  615. #elif __has_include(<fenv.h>)
  616. #include <fenv.h>
  617. #endif
  618. #if __has_include(<stdlib.h>)
  619. #include <stdlib.h>
  620. #endif
  621. #elif SIMDE_STDC_HOSTED == 1
  622. #include <stdlib.h>
  623. #include <fenv.h>
  624. #endif
  625. #include "check.h"
  626. /* Sometimes we run into problems with specific versions of compilers
  627. which make the native versions unusable for us. Often this is due
  628. to missing functions, sometimes buggy implementations, etc. These
  629. macros are how we check for specific bugs. As they are fixed we'll
  630. start only defining them for problematic compiler versions. */
  631. #if !defined(SIMDE_IGNORE_COMPILER_BUGS)
  632. #if defined(HEDLEY_GCC_VERSION)
  633. #if !HEDLEY_GCC_VERSION_CHECK(4, 9, 0)
  634. #define SIMDE_BUG_GCC_REV_208793
  635. #endif
  636. #if !HEDLEY_GCC_VERSION_CHECK(5, 0, 0)
  637. #define SIMDE_BUG_GCC_BAD_MM_SRA_EPI32 /* TODO: find relevant bug or commit */
  638. #endif
  639. #if !HEDLEY_GCC_VERSION_CHECK(4, 6, 0)
  640. #define SIMDE_BUG_GCC_BAD_MM_EXTRACT_EPI8 /* TODO: find relevant bug or commit */
  641. #endif
  642. #if !HEDLEY_GCC_VERSION_CHECK(8, 0, 0)
  643. #define SIMDE_BUG_GCC_REV_247851
  644. #endif
  645. #if !HEDLEY_GCC_VERSION_CHECK(10, 0, 0)
  646. #define SIMDE_BUG_GCC_REV_274313
  647. #define SIMDE_BUG_GCC_91341
  648. #endif
  649. #if !HEDLEY_GCC_VERSION_CHECK(9, 0, 0) && defined(SIMDE_ARCH_AARCH64)
  650. #define SIMDE_BUG_GCC_ARM_SHIFT_SCALAR
  651. #endif
  652. #if defined(SIMDE_ARCH_X86) && !defined(SIMDE_ARCH_AMD64)
  653. #define SIMDE_BUG_GCC_94482
  654. #endif
  655. #if !HEDLEY_GCC_VERSION_CHECK(9, 4, 0) && defined(SIMDE_ARCH_AARCH64)
  656. #define SIMDE_BUG_GCC_94488
  657. #endif
  658. #if defined(SIMDE_ARCH_POWER)
  659. #define SIMDE_BUG_GCC_95227
  660. #endif
  661. #define SIMDE_BUG_GCC_95399
  662. #elif defined(__clang__)
  663. #if defined(SIMDE_ARCH_AARCH64)
  664. #define SIMDE_BUG_CLANG_45541
  665. #endif
  666. #endif
  667. #if defined(HEDLEY_EMSCRIPTEN_VERSION)
  668. #define SIMDE_BUG_EMSCRIPTEN_MISSING_IMPL /* Placeholder for (as yet) unfiled issues. */
  669. #define SIMDE_BUG_EMSCRIPTEN_5242
  670. #endif
  671. #endif
  672. /* GCC and Clang both have the same issue:
  673. * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95144
  674. * https://bugs.llvm.org/show_bug.cgi?id=45931
  675. */
  676. #if HEDLEY_HAS_WARNING("-Wsign-conversion") || HEDLEY_GCC_VERSION_CHECK(4, 3, 0)
  677. #define SIMDE_BUG_IGNORE_SIGN_CONVERSION(expr) \
  678. (__extension__({ \
  679. HEDLEY_DIAGNOSTIC_PUSH \
  680. HEDLEY_DIAGNOSTIC_POP \
  681. _Pragma("GCC diagnostic ignored \"-Wsign-conversion\"") __typeof__( \
  682. expr) simde_bug_ignore_sign_conversion_v_ = (expr); \
  683. HEDLEY_DIAGNOSTIC_PUSH \
  684. simde_bug_ignore_sign_conversion_v_; \
  685. }))
  686. #else
  687. #define SIMDE_BUG_IGNORE_SIGN_CONVERSION(expr) (expr)
  688. #endif
  689. #endif /* !defined(SIMDE_COMMON_H) */