Open Broadcaster Software
Free, open source software for live streaming and recording
simde-common.h
Go to the documentation of this file.
1 /* Copyright (c) 2017-2019 Evan Nemerson <evan@nemerson.com>
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 
24 #if !defined(SIMDE_COMMON_H)
25 #define SIMDE_COMMON_H
26 
27 #include "hedley.h"
28 #include "check.h"
29 #include "simde-arch.h"
30 
31 #if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L)
32 #define SIMDE_ALIGN(alignment) _Alignas(alignment)
33 #elif (defined(__cplusplus) && (__cplusplus >= 201103L))
34 #define SIMDE_ALIGN(alignment) alignas(alignment)
35 #elif HEDLEY_GCC_VERSION_CHECK(2, 95, 0) || \
36  HEDLEY_CRAY_VERSION_CHECK(8, 4, 0) || \
37  HEDLEY_IBM_VERSION_CHECK(11, 1, 0) || \
38  HEDLEY_INTEL_VERSION_CHECK(13, 0, 0) || \
39  HEDLEY_PGI_VERSION_CHECK(19, 4, 0) || \
40  HEDLEY_ARM_VERSION_CHECK(4, 1, 0) || \
41  HEDLEY_TINYC_VERSION_CHECK(0, 9, 24) || \
42  HEDLEY_TI_VERSION_CHECK(8, 1, 0)
43 #define SIMDE_ALIGN(alignment) __attribute__((aligned(alignment)))
44 #elif defined(_MSC_VER) && (!defined(_M_IX86) || defined(_M_AMD64))
45 #define SIMDE_ALIGN(alignment) __declspec(align(alignment))
46 #else
47 #define SIMDE_ALIGN(alignment)
48 #endif
49 
50 #define simde_assert_aligned(alignment, val) \
51  simde_assert_int(((uintptr_t)(val)) % (alignment), ==, 0)
52 
53 #if HEDLEY_GCC_HAS_ATTRIBUTE(vector_size, 4, 6, 0)
54 #define SIMDE__ENABLE_GCC_VEC_EXT
55 #endif
56 
57 #if !defined(SIMDE_ENABLE_OPENMP) && \
58  ((defined(_OPENMP) && (_OPENMP >= 201307L)) || \
59  (defined(_OPENMP_SIMD) && (_OPENMP_SIMD >= 201307L)))
60 #define SIMDE_ENABLE_OPENMP
61 #endif
62 
63 #if !defined(SIMDE_ENABLE_CILKPLUS) && defined(__cilk)
64 #define SIMDE_ENABLE_CILKPLUS
65 #endif
66 
67 #if defined(SIMDE_ENABLE_OPENMP)
68 #define SIMDE__VECTORIZE _Pragma("omp simd")
69 #define SIMDE__VECTORIZE_SAFELEN(l) HEDLEY_PRAGMA(omp simd safelen(l))
70 #define SIMDE__VECTORIZE_REDUCTION(r) HEDLEY_PRAGMA(omp simd reduction(r))
71 #define SIMDE__VECTORIZE_ALIGNED(a) HEDLEY_PRAGMA(omp simd aligned(a))
72 #elif defined(SIMDE_ENABLE_CILKPLUS)
73 #define SIMDE__VECTORIZE _Pragma("simd")
74 #define SIMDE__VECTORIZE_SAFELEN(l) HEDLEY_PRAGMA(simd vectorlength(l))
75 #define SIMDE__VECTORIZE_REDUCTION(r) HEDLEY_PRAGMA(simd reduction(r))
76 #define SIMDE__VECTORIZE_ALIGNED(a) HEDLEY_PRAGMA(simd aligned(a))
77 #elif defined(__INTEL_COMPILER)
78 #define SIMDE__VECTORIZE _Pragma("simd")
79 #define SIMDE__VECTORIZE_SAFELEN(l) HEDLEY_PRAGMA(simd vectorlength(l))
80 #define SIMDE__VECTORIZE_REDUCTION(r) HEDLEY_PRAGMA(simd reduction(r))
81 #define SIMDE__VECTORIZE_ALIGNED(a)
82 #elif defined(__clang__)
83 #define SIMDE__VECTORIZE _Pragma("clang loop vectorize(enable)")
84 #define SIMDE__VECTORIZE_SAFELEN(l) HEDLEY_PRAGMA(clang loop vectorize_width(l))
85 #define SIMDE__VECTORIZE_REDUCTION(r) SIMDE__VECTORIZE
86 #define SIMDE__VECTORIZE_ALIGNED(a)
87 #elif HEDLEY_GCC_VERSION_CHECK(4, 9, 0)
88 #define SIMDE__VECTORIZE _Pragma("GCC ivdep")
89 #define SIMDE__VECTORIZE_SAFELEN(l) SIMDE__VECTORIZE
90 #define SIMDE__VECTORIZE_REDUCTION(r) SIMDE__VECTORIZE
91 #define SIMDE__VECTORIZE_ALIGNED(a)
92 #elif HEDLEY_CRAY_VERSION_CHECK(5, 0, 0)
93 #define SIMDE__VECTORIZE _Pragma("_CRI ivdep")
94 #define SIMDE__VECTORIZE_SAFELEN(l) SIMDE__VECTORIZE
95 #define SIMDE__VECTORIZE_REDUCTION(r) SIMDE__VECTORIZE
96 #define SIMDE__VECTORIZE_ALIGNED(a)
97 #else
98 #define SIMDE__VECTORIZE
99 #define SIMDE__VECTORIZE_SAFELEN(l)
100 #define SIMDE__VECTORIZE_REDUCTION(r)
101 #define SIMDE__VECTORIZE_ALIGNED(a)
102 #endif
103 
104 #if HEDLEY_GCC_HAS_ATTRIBUTE(unused, 3, 1, 0)
105 #define SIMDE__UNUSED __attribute__((__unused__))
106 #else
107 #define SIMDE__UNUSED
108 #endif
109 
110 #if HEDLEY_GCC_HAS_ATTRIBUTE(artificial, 4, 3, 0)
111 #define SIMDE__ARTIFICIAL __attribute__((__artificial__))
112 #else
113 #define SIMDE__ARTIFICIAL
114 #endif
115 
116 /* Intended for checking coverage, you should never use this in
117  production. */
118 #if defined(SIMDE_NO_INLINE)
119 #define SIMDE__FUNCTION_ATTRIBUTES HEDLEY_NEVER_INLINE SIMDE__UNUSED static
120 #else
121 #define SIMDE__FUNCTION_ATTRIBUTES HEDLEY_INLINE SIMDE__ARTIFICIAL static
122 #endif
123 
124 #if defined(_MSC_VER)
125 #define SIMDE__BEGIN_DECLS \
126  HEDLEY_DIAGNOSTIC_PUSH __pragma(warning(disable : 4996 4204)) \
127  HEDLEY_BEGIN_C_DECLS
128 #define SIMDE__END_DECLS HEDLEY_DIAGNOSTIC_POP HEDLEY_END_C_DECLS
129 #else
130 #define SIMDE__BEGIN_DECLS HEDLEY_BEGIN_C_DECLS
131 #define SIMDE__END_DECLS HEDLEY_END_C_DECLS
132 #endif
133 
134 #if defined(__SIZEOF_INT128__)
135 #define SIMDE__HAVE_INT128
136 typedef __int128 simde_int128;
137 typedef unsigned __int128 simde_uint128;
138 #endif
139 
140 /* TODO: we should at least make an attempt to detect the correct
141  types for simde_float32/float64 instead of just assuming float and
142  double. */
143 
144 #if !defined(SIMDE_FLOAT32_TYPE)
145 #define SIMDE_FLOAT32_TYPE float
146 #define SIMDE_FLOAT32_C(value) value##f
147 #else
148 #define SIMDE_FLOAT32_C(value) ((SIMDE_FLOAT32_TYPE)value)
149 #endif
152  "Unable to find 32-bit floating-point type.");
153 
154 #if !defined(SIMDE_FLOAT64_TYPE)
155 #define SIMDE_FLOAT64_TYPE double
156 #define SIMDE_FLOAT64_C(value) value
157 #else
158 #define SIMDE_FLOAT32_C(value) ((SIMDE_FLOAT64_TYPE)value)
159 #endif
162  "Unable to find 64-bit floating-point type.");
163 
164 /* Whether to assume that the compiler can auto-vectorize reasonably
165  well. This will cause SIMDe to attempt to compose vector
166  operations using more simple vector operations instead of minimize
167  serial work.
168 
169  As an example, consider the _mm_add_ss(a, b) function from SSE,
170  which returns { a0 + b0, a1, a2, a3 }. This pattern is repeated
171  for other operations (sub, mul, etc.).
172 
173  The naïve implementation would result in loading a0 and b0, adding
174  them into a temporary variable, then splicing that value into a new
175  vector with the remaining elements from a.
176 
177  On platforms which support vectorization, it's generally faster to
178  simply perform the operation on the entire vector to avoid having
179  to move data between SIMD registers and non-SIMD registers.
180  Basically, instead of the temporary variable being (a0 + b0) it
181  would be a vector of (a + b), which is then combined with a to form
182  the result.
183 
184  By default, SIMDe will prefer the pure-vector versions if we detect
185  a vector ISA extension, but this can be overridden by defining
186  SIMDE_NO_ASSUME_VECTORIZATION. You can also define
187  SIMDE_ASSUME_VECTORIZATION if you want to force SIMDe to use the
188  vectorized version. */
189 #if !defined(SIMDE_NO_ASSUME_VECTORIZATION) && \
190  !defined(SIMDE_ASSUME_VECTORIZATION)
191 #if defined(__SSE__) || defined(__ARM_NEON) || defined(__mips_msa) || \
192  defined(__ALTIVEC__)
193 #define SIMDE_ASSUME_VECTORIZATION
194 #endif
195 #endif
196 
197 /* GCC and clang have built-in functions to handle shuffling of
198  vectors, but the implementations are slightly different. This
199  macro is just an abstraction over them. Note that elem_size is in
200  bits but vec_size is in bytes. */
201 #if HEDLEY_CLANG_HAS_BUILTIN(__builtin_shufflevector)
202 #define SIMDE__SHUFFLE_VECTOR(elem_size, vec_size, a, b, ...) \
203  __builtin_shufflevector(a, b, __VA_ARGS__)
204 #elif HEDLEY_GCC_HAS_BUILTIN(__builtin_shuffle, 4, 7, 0) && \
205  !defined(__INTEL_COMPILER)
206 #define SIMDE__SHUFFLE_VECTOR(elem_size, vec_size, a, b, ...) \
207  __builtin_shuffle(a, b, \
208  (int##elem_size##_t __attribute__( \
209  (__vector_size__(vec_size)))){__VA_ARGS__})
210 #endif
211 
212 /* Some algorithms are iterative, and fewer iterations means less
213  accuracy. Lower values here will result in faster, but less
214  accurate, calculations for some functions. */
215 #if !defined(SIMDE_ACCURACY_ITERS)
216 #define SIMDE_ACCURACY_ITERS 2
217 #endif
218 
219 /* This will probably move into Hedley at some point, but I'd like to
220  more thoroughly check for other compilers which define __GNUC__
221  first. */
222 #if defined(SIMDE__REALLY_GCC)
223 #undef SIMDE__REALLY_GCC
224 #endif
225 #if !defined(__GNUC__) || defined(__clang__) || defined(__INTEL_COMPILER)
226 #define SIMDE__REALLY_GCC 0
227 #else
228 #define SIMDE__REALLY_GCC 1
229 #endif
230 
231 #if defined(SIMDE__ASSUME_ALIGNED)
232 #undef SIMDE__ASSUME_ALIGNED
233 #endif
234 #if HEDLEY_INTEL_VERSION_CHECK(9, 0, 0)
235 #define SIMDE__ASSUME_ALIGNED(ptr, align) __assume_aligned(ptr, align)
236 #elif HEDLEY_MSVC_VERSION_CHECK(13, 10, 0)
237 #define SIMDE__ASSUME_ALIGNED(ptr, align) \
238  __assume((((char *)ptr) - ((char *)0)) % (align) == 0)
239 #elif HEDLEY_GCC_HAS_BUILTIN(__builtin_assume_aligned, 4, 7, 0)
240 #define SIMDE__ASSUME_ALIGNED(ptr, align) \
241  (ptr = (__typeof__(ptr))__builtin_assume_aligned((ptr), align))
242 #elif HEDLEY_CLANG_HAS_BUILTIN(__builtin_assume)
243 #define SIMDE__ASSUME_ALIGNED(ptr, align) \
244  __builtin_assume((((char *)ptr) - ((char *)0)) % (align) == 0)
245 #elif HEDLEY_GCC_HAS_BUILTIN(__builtin_unreachable, 4, 5, 0)
246 #define SIMDE__ASSUME_ALIGNED(ptr, align) \
247  ((((char *)ptr) - ((char *)0)) % (align) == 0) \
248  ? (1) \
249  : (__builtin_unreachable(), 0)
250 #else
251 #define SIMDE__ASSUME_ALIGNED(ptr, align)
252 #endif
253 
254 /* Sometimes we run into problems with specific versions of compilers
255  which make the native versions unusable for us. Often this is due
256  to missing functions, sometimes buggy implementations, etc. These
257  macros are how we check for specific bugs. As they are fixed we'll
258  start only defining them for problematic compiler versions. */
259 
260 #if !defined(SIMDE_IGNORE_COMPILER_BUGS)
261 #if SIMDE__REALLY_GCC
262 #if !HEDLEY_GCC_VERSION_CHECK(4, 9, 0)
263 #define SIMDE_BUG_GCC_REV_208793
264 #endif
265 #if !HEDLEY_GCC_VERSION_CHECK(5, 0, 0)
266 #define SIMDE_BUG_GCC_BAD_MM_SRA_EPI32 /* TODO: find relevant bug or commit */
267 #endif
268 #if !HEDLEY_GCC_VERSION_CHECK(4, 6, 0)
269 #define SIMDE_BUG_GCC_BAD_MM_EXTRACT_EPI8 /* TODO: find relevant bug or commit */
270 #endif
271 #endif
272 #if defined(__EMSCRIPTEN__)
273 #define SIMDE_BUG_EMSCRIPTEN_MISSING_IMPL /* Placeholder for (as yet) unfiled issues. */
274 #define SIMDE_BUG_EMSCRIPTEN_5242
275 #endif
276 #endif
277 
278 #endif /* !defined(SIMDE_COMMON_H) */
check.h
hedley.h
simde_float64
SIMDE_FLOAT64_TYPE simde_float64
Definition: simde-common.h:160
simde_float32
SIMDE_FLOAT32_TYPE simde_float32
Definition: simde-common.h:150
simde-arch.h
SIMDE_FLOAT32_TYPE
#define SIMDE_FLOAT32_TYPE
Definition: simde-common.h:145
HEDLEY_STATIC_ASSERT
HEDLEY_STATIC_ASSERT(sizeof(simde_float32)==4, "Unable to find 32-bit floating-point type.")
SIMDE_FLOAT64_TYPE
#define SIMDE_FLOAT64_TYPE
Definition: simde-common.h:155