blob: 9d8f773a2e09c3342f53a8a1c27b5541fcf3fee4 [file] [log] [blame]
Mike Klein455c7472019-02-05 13:42:46 -05001/*
2 * Copyright 2019 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef SKVX_DEFINED
9#define SKVX_DEFINED
10
11// skvx::Vec<N,T> are SIMD vectors of N T's, a v1.5 successor to SkNx<N,T>.
12//
13// This time we're leaning a bit less on platform-specific intrinsics and a bit
14// more on Clang/GCC vector extensions, but still keeping the option open to
15// drop in platform-specific intrinsics, actually more easily than before.
16//
17// We've also fixed a few of the caveats that used to make SkNx awkward to work
18// with across translation units. skvx::Vec<N,T> always has N*sizeof(T) size
Mike Kleina1711092020-09-02 09:00:57 -050019// and alignment and is safe to use across translation units freely.
20// Ideally we'd only align to T, but that tanks ARMv7 NEON codegen.
Mike Klein455c7472019-02-05 13:42:46 -050021
Mike Klein7d3b27d2019-06-07 10:57:58 -050022// Please try to keep this file independent of Skia headers.
Mike Kleindcfc3ef2019-02-07 09:49:17 -050023#include <algorithm> // std::min, std::max
Mike Klein41b995c2019-02-27 10:24:55 -060024#include <cmath> // std::ceil, std::floor, std::trunc, std::round, std::sqrt, etc.
Mike Klein455c7472019-02-05 13:42:46 -050025#include <cstdint> // intXX_t
26#include <cstring> // memcpy()
Mike Klein455c7472019-02-05 13:42:46 -050027#include <initializer_list> // std::initializer_list
28
Mike Klein5caf7de2020-03-12 11:05:46 -050029#if defined(__SSE__) || defined(__AVX__) || defined(__AVX2__)
Mike Kleindcfc3ef2019-02-07 09:49:17 -050030 #include <immintrin.h>
Mike Klein7d3b27d2019-06-07 10:57:58 -050031#elif defined(__ARM_NEON)
Mike Kleindcfc3ef2019-02-07 09:49:17 -050032 #include <arm_neon.h>
Mike Kleina1711092020-09-02 09:00:57 -050033#elif defined(__wasm_simd128__)
Elliot Evansfe7e74b2020-06-30 16:08:44 -060034 #include <wasm_simd128.h>
35#endif
36
Mike Kleina1711092020-09-02 09:00:57 -050037// To avoid ODR violations, all methods must be force-inlined...
Mike Klein21ef0d52019-12-17 11:40:14 -060038#if defined(_MSC_VER)
39 #define SKVX_ALWAYS_INLINE __forceinline
40#else
41 #define SKVX_ALWAYS_INLINE __attribute__((always_inline))
42#endif
43
Mike Kleina1711092020-09-02 09:00:57 -050044// ... and all standalone functions must be static. Please use these helpers:
45#define SI static inline
46#define SIT template < typename T> SI
47#define SIN template <int N > SI
48#define SINT template <int N, typename T> SI
Mike Klein21ef0d52019-12-17 11:40:14 -060049#define SINTU template <int N, typename T, typename U, \
Mike Kleina1711092020-09-02 09:00:57 -050050 typename=std::enable_if_t<std::is_convertible<U,T>::value>> SI
Mike Klein41b995c2019-02-27 10:24:55 -060051
Mike Klein455c7472019-02-05 13:42:46 -050052namespace skvx {
53
54// All Vec have the same simple memory layout, the same as `T vec[N]`.
Mike Klein455c7472019-02-05 13:42:46 -050055template <int N, typename T>
Mike Kleina1711092020-09-02 09:00:57 -050056struct alignas(N*sizeof(T)) Vec {
Mike Klein96e4e532019-04-16 11:36:55 -050057 static_assert((N & (N-1)) == 0, "N must be a power of 2.");
58 static_assert(sizeof(T) >= alignof(T), "What kind of crazy T is this?");
Mike Klein455c7472019-02-05 13:42:46 -050059
Mike Kleindcfc3ef2019-02-07 09:49:17 -050060 Vec<N/2,T> lo, hi;
Mike Klein455c7472019-02-05 13:42:46 -050061
Mike Klein42925152019-02-06 11:56:58 -050062 // Methods belong here in the class declaration of Vec only if:
63 // - they must be here, like constructors or operator[];
64 // - they'll definitely never want a specialized implementation.
65 // Other operations on Vec should be defined outside the type.
66
Mike Klein21ef0d52019-12-17 11:40:14 -060067 SKVX_ALWAYS_INLINE Vec() = default;
Mike Kleinf4438d52019-03-14 13:30:42 -050068
Mike Kleina1711092020-09-02 09:00:57 -050069 template <typename U, typename=std::enable_if_t<std::is_convertible<U,T>::value>>
Mike Klein21ef0d52019-12-17 11:40:14 -060070 SKVX_ALWAYS_INLINE
Mike Kleinf4438d52019-03-14 13:30:42 -050071 Vec(U x) : lo(x), hi(x) {}
Mike Klein455c7472019-02-05 13:42:46 -050072
Mike Klein21ef0d52019-12-17 11:40:14 -060073 SKVX_ALWAYS_INLINE Vec(std::initializer_list<T> xs) {
Mike Kleindcfc3ef2019-02-07 09:49:17 -050074 T vals[N] = {0};
75 memcpy(vals, xs.begin(), std::min(xs.size(), (size_t)N)*sizeof(T));
Mike Klein455c7472019-02-05 13:42:46 -050076
Mike Kleindcfc3ef2019-02-07 09:49:17 -050077 lo = Vec<N/2,T>::Load(vals + 0);
78 hi = Vec<N/2,T>::Load(vals + N/2);
Mike Klein455c7472019-02-05 13:42:46 -050079 }
80
Mike Klein21ef0d52019-12-17 11:40:14 -060081 SKVX_ALWAYS_INLINE T operator[](int i) const { return i < N/2 ? lo[i] : hi[i-N/2]; }
82 SKVX_ALWAYS_INLINE T& operator[](int i) { return i < N/2 ? lo[i] : hi[i-N/2]; }
Mike Klein42925152019-02-06 11:56:58 -050083
Mike Klein21ef0d52019-12-17 11:40:14 -060084 SKVX_ALWAYS_INLINE static Vec Load(const void* ptr) {
Mike Klein42925152019-02-06 11:56:58 -050085 Vec v;
86 memcpy(&v, ptr, sizeof(Vec));
87 return v;
88 }
Mike Klein21ef0d52019-12-17 11:40:14 -060089 SKVX_ALWAYS_INLINE void store(void* ptr) const {
Mike Klein42925152019-02-06 11:56:58 -050090 memcpy(ptr, this, sizeof(Vec));
91 }
Mike Klein455c7472019-02-05 13:42:46 -050092};
93
Mike Kleindcfc3ef2019-02-07 09:49:17 -050094template <typename T>
95struct Vec<1,T> {
96 T val;
Mike Klein455c7472019-02-05 13:42:46 -050097
Mike Klein21ef0d52019-12-17 11:40:14 -060098 SKVX_ALWAYS_INLINE Vec() = default;
Mike Kleinf4438d52019-03-14 13:30:42 -050099
Mike Kleina1711092020-09-02 09:00:57 -0500100 template <typename U, typename=std::enable_if_t<std::is_convertible<U,T>::value>>
Mike Klein21ef0d52019-12-17 11:40:14 -0600101 SKVX_ALWAYS_INLINE
Mike Kleinf4438d52019-03-14 13:30:42 -0500102 Vec(U x) : val(x) {}
Mike Klein455c7472019-02-05 13:42:46 -0500103
Mike Klein21ef0d52019-12-17 11:40:14 -0600104 SKVX_ALWAYS_INLINE Vec(std::initializer_list<T> xs) : val(xs.size() ? *xs.begin() : 0) {}
Mike Kleindcfc3ef2019-02-07 09:49:17 -0500105
Mike Klein21ef0d52019-12-17 11:40:14 -0600106 SKVX_ALWAYS_INLINE T operator[](int) const { return val; }
107 SKVX_ALWAYS_INLINE T& operator[](int) { return val; }
Mike Kleindcfc3ef2019-02-07 09:49:17 -0500108
Mike Klein21ef0d52019-12-17 11:40:14 -0600109 SKVX_ALWAYS_INLINE static Vec Load(const void* ptr) {
Mike Kleindcfc3ef2019-02-07 09:49:17 -0500110 Vec v;
111 memcpy(&v, ptr, sizeof(Vec));
112 return v;
113 }
Mike Klein21ef0d52019-12-17 11:40:14 -0600114 SKVX_ALWAYS_INLINE void store(void* ptr) const {
Mike Kleindcfc3ef2019-02-07 09:49:17 -0500115 memcpy(ptr, this, sizeof(Vec));
116 }
117};
Mike Klein455c7472019-02-05 13:42:46 -0500118
Mike Kleindcfc3ef2019-02-07 09:49:17 -0500119template <typename D, typename S>
Mike Kleina1711092020-09-02 09:00:57 -0500120SI D unchecked_bit_pun(const S& s) {
Mike Kleindcfc3ef2019-02-07 09:49:17 -0500121 D d;
122 memcpy(&d, &s, sizeof(D));
123 return d;
124}
Mike Klein455c7472019-02-05 13:42:46 -0500125
Mike Klein5cb47d62020-07-10 15:46:46 -0500126template <typename D, typename S>
Mike Kleina1711092020-09-02 09:00:57 -0500127SI D bit_pun(const S& s) {
Mike Klein5cb47d62020-07-10 15:46:46 -0500128 static_assert(sizeof(D) == sizeof(S), "");
129 return unchecked_bit_pun<D>(s);
130}
131
Mike Klein455c7472019-02-05 13:42:46 -0500132// Translate from a value type T to its corresponding Mask, the result of a comparison.
Mike Kleincd9ef732019-02-09 13:48:54 -0500133template <typename T> struct Mask { using type = T; };
134template <> struct Mask<float > { using type = int32_t; };
135template <> struct Mask<double> { using type = int64_t; };
136template <typename T> using M = typename Mask<T>::type;
Mike Klein455c7472019-02-05 13:42:46 -0500137
Mike Kleindcfc3ef2019-02-07 09:49:17 -0500138// Join two Vec<N,T> into one Vec<2N,T>.
Mike Klein9a885b22019-04-16 12:07:23 -0500139SINT Vec<2*N,T> join(const Vec<N,T>& lo, const Vec<N,T>& hi) {
Mike Kleindcfc3ef2019-02-07 09:49:17 -0500140 Vec<2*N,T> v;
141 v.lo = lo;
142 v.hi = hi;
143 return v;
Mike Klein455c7472019-02-05 13:42:46 -0500144}
Mike Klein455c7472019-02-05 13:42:46 -0500145
146// We have two default strategies for implementing most operations:
147// 1) lean on Clang/GCC vector extensions when available;
Mike Kleindcfc3ef2019-02-07 09:49:17 -0500148// 2) recurse to scalar portable implementations when not.
149// At the end we can drop in platform-specific implementations that override either default.
Mike Klein455c7472019-02-05 13:42:46 -0500150
Mike Klein42925152019-02-06 11:56:58 -0500151#if !defined(SKNX_NO_SIMD) && (defined(__clang__) || defined(__GNUC__))
Mike Klein455c7472019-02-05 13:42:46 -0500152
153 // VExt<N,T> types have the same size as Vec<N,T> and support most operations directly.
154 // N.B. VExt<N,T> alignment is N*alignof(T), stricter than Vec<N,T>'s alignof(T).
Mike Klein455c7472019-02-05 13:42:46 -0500155 #if defined(__clang__)
156 template <int N, typename T>
157 using VExt = T __attribute__((ext_vector_type(N)));
158
159 #elif defined(__GNUC__)
160 template <int N, typename T>
161 struct VExtHelper {
162 typedef T __attribute__((vector_size(N*sizeof(T)))) type;
163 };
164
165 template <int N, typename T>
166 using VExt = typename VExtHelper<N,T>::type;
Mike Kleindcfc3ef2019-02-07 09:49:17 -0500167
168 // For some reason some (new!) versions of GCC cannot seem to deduce N in the generic
169 // to_vec<N,T>() below for N=4 and T=float. This workaround seems to help...
Mike Kleina1711092020-09-02 09:00:57 -0500170 SI Vec<4,float> to_vec(VExt<4,float> v) { return bit_pun<Vec<4,float>>(v); }
Mike Klein455c7472019-02-05 13:42:46 -0500171 #endif
172
Mike Klein9a885b22019-04-16 12:07:23 -0500173 SINT VExt<N,T> to_vext(const Vec<N,T>& v) { return bit_pun<VExt<N,T>>(v); }
174 SINT Vec <N,T> to_vec(const VExt<N,T>& v) { return bit_pun<Vec <N,T>>(v); }
Mike Klein455c7472019-02-05 13:42:46 -0500175
Mike Kleina1711092020-09-02 09:00:57 -0500176 SINT Vec<N,T> operator+(const Vec<N,T>& x, const Vec<N,T>& y) {
177 return to_vec<N,T>(to_vext(x) + to_vext(y));
178 }
179 SINT Vec<N,T> operator-(const Vec<N,T>& x, const Vec<N,T>& y) {
180 return to_vec<N,T>(to_vext(x) - to_vext(y));
181 }
182 SINT Vec<N,T> operator*(const Vec<N,T>& x, const Vec<N,T>& y) {
183 return to_vec<N,T>(to_vext(x) * to_vext(y));
184 }
185 SINT Vec<N,T> operator/(const Vec<N,T>& x, const Vec<N,T>& y) {
186 return to_vec<N,T>(to_vext(x) / to_vext(y));
187 }
Mike Klein455c7472019-02-05 13:42:46 -0500188
Mike Kleina1711092020-09-02 09:00:57 -0500189 SINT Vec<N,T> operator^(const Vec<N,T>& x, const Vec<N,T>& y) {
190 return to_vec<N,T>(to_vext(x) ^ to_vext(y));
191 }
192 SINT Vec<N,T> operator&(const Vec<N,T>& x, const Vec<N,T>& y) {
193 return to_vec<N,T>(to_vext(x) & to_vext(y));
194 }
195 SINT Vec<N,T> operator|(const Vec<N,T>& x, const Vec<N,T>& y) {
196 return to_vec<N,T>(to_vext(x) | to_vext(y));
197 }
Mike Klein455c7472019-02-05 13:42:46 -0500198
Mike Klein9a885b22019-04-16 12:07:23 -0500199 SINT Vec<N,T> operator!(const Vec<N,T>& x) { return to_vec<N,T>(!to_vext(x)); }
200 SINT Vec<N,T> operator-(const Vec<N,T>& x) { return to_vec<N,T>(-to_vext(x)); }
201 SINT Vec<N,T> operator~(const Vec<N,T>& x) { return to_vec<N,T>(~to_vext(x)); }
Mike Klein455c7472019-02-05 13:42:46 -0500202
Mike Kleina1711092020-09-02 09:00:57 -0500203 SINT Vec<N,T> operator<<(const Vec<N,T>& x, int k) { return to_vec<N,T>(to_vext(x) << k); }
204 SINT Vec<N,T> operator>>(const Vec<N,T>& x, int k) { return to_vec<N,T>(to_vext(x) >> k); }
Mike Klein455c7472019-02-05 13:42:46 -0500205
Mike Kleina1711092020-09-02 09:00:57 -0500206 SINT Vec<N,M<T>> operator==(const Vec<N,T>& x, const Vec<N,T>& y) {
207 return bit_pun<Vec<N,M<T>>>(to_vext(x) == to_vext(y));
208 }
209 SINT Vec<N,M<T>> operator!=(const Vec<N,T>& x, const Vec<N,T>& y) {
210 return bit_pun<Vec<N,M<T>>>(to_vext(x) != to_vext(y));
211 }
212 SINT Vec<N,M<T>> operator<=(const Vec<N,T>& x, const Vec<N,T>& y) {
213 return bit_pun<Vec<N,M<T>>>(to_vext(x) <= to_vext(y));
214 }
215 SINT Vec<N,M<T>> operator>=(const Vec<N,T>& x, const Vec<N,T>& y) {
216 return bit_pun<Vec<N,M<T>>>(to_vext(x) >= to_vext(y));
217 }
218 SINT Vec<N,M<T>> operator< (const Vec<N,T>& x, const Vec<N,T>& y) {
219 return bit_pun<Vec<N,M<T>>>(to_vext(x) < to_vext(y));
220 }
221 SINT Vec<N,M<T>> operator> (const Vec<N,T>& x, const Vec<N,T>& y) {
222 return bit_pun<Vec<N,M<T>>>(to_vext(x) > to_vext(y));
223 }
Mike Klein455c7472019-02-05 13:42:46 -0500224
225#else
226
227 // Either SKNX_NO_SIMD is defined, or Clang/GCC vector extensions are not available.
228 // We'll implement things portably, in a way that should be easily autovectorizable.
229
Mike Kleindcfc3ef2019-02-07 09:49:17 -0500230 // N == 1 scalar implementations.
Mike Klein9a885b22019-04-16 12:07:23 -0500231 SIT Vec<1,T> operator+(const Vec<1,T>& x, const Vec<1,T>& y) { return x.val + y.val; }
232 SIT Vec<1,T> operator-(const Vec<1,T>& x, const Vec<1,T>& y) { return x.val - y.val; }
233 SIT Vec<1,T> operator*(const Vec<1,T>& x, const Vec<1,T>& y) { return x.val * y.val; }
234 SIT Vec<1,T> operator/(const Vec<1,T>& x, const Vec<1,T>& y) { return x.val / y.val; }
Mike Klein455c7472019-02-05 13:42:46 -0500235
Mike Klein9a885b22019-04-16 12:07:23 -0500236 SIT Vec<1,T> operator^(const Vec<1,T>& x, const Vec<1,T>& y) { return x.val ^ y.val; }
237 SIT Vec<1,T> operator&(const Vec<1,T>& x, const Vec<1,T>& y) { return x.val & y.val; }
238 SIT Vec<1,T> operator|(const Vec<1,T>& x, const Vec<1,T>& y) { return x.val | y.val; }
Mike Klein455c7472019-02-05 13:42:46 -0500239
Mike Klein9a885b22019-04-16 12:07:23 -0500240 SIT Vec<1,T> operator!(const Vec<1,T>& x) { return !x.val; }
241 SIT Vec<1,T> operator-(const Vec<1,T>& x) { return -x.val; }
242 SIT Vec<1,T> operator~(const Vec<1,T>& x) { return ~x.val; }
Mike Klein455c7472019-02-05 13:42:46 -0500243
Mike Kleina1711092020-09-02 09:00:57 -0500244 SIT Vec<1,T> operator<<(const Vec<1,T>& x, int k) { return x.val << k; }
245 SIT Vec<1,T> operator>>(const Vec<1,T>& x, int k) { return x.val >> k; }
Mike Klein455c7472019-02-05 13:42:46 -0500246
Mike Kleina1711092020-09-02 09:00:57 -0500247 SIT Vec<1,M<T>> operator==(const Vec<1,T>& x, const Vec<1,T>& y) {
248 return x.val == y.val ? ~0 : 0;
249 }
250 SIT Vec<1,M<T>> operator!=(const Vec<1,T>& x, const Vec<1,T>& y) {
251 return x.val != y.val ? ~0 : 0;
252 }
253 SIT Vec<1,M<T>> operator<=(const Vec<1,T>& x, const Vec<1,T>& y) {
254 return x.val <= y.val ? ~0 : 0;
255 }
256 SIT Vec<1,M<T>> operator>=(const Vec<1,T>& x, const Vec<1,T>& y) {
257 return x.val >= y.val ? ~0 : 0;
258 }
259 SIT Vec<1,M<T>> operator< (const Vec<1,T>& x, const Vec<1,T>& y) {
260 return x.val < y.val ? ~0 : 0;
261 }
262 SIT Vec<1,M<T>> operator> (const Vec<1,T>& x, const Vec<1,T>& y) {
263 return x.val > y.val ? ~0 : 0;
264 }
Mike Kleindcfc3ef2019-02-07 09:49:17 -0500265
266 // All default N != 1 implementations just recurse on lo and hi halves.
Mike Kleina1711092020-09-02 09:00:57 -0500267 SINT Vec<N,T> operator+(const Vec<N,T>& x, const Vec<N,T>& y) {
268 return join(x.lo + y.lo, x.hi + y.hi);
269 }
270 SINT Vec<N,T> operator-(const Vec<N,T>& x, const Vec<N,T>& y) {
271 return join(x.lo - y.lo, x.hi - y.hi);
272 }
273 SINT Vec<N,T> operator*(const Vec<N,T>& x, const Vec<N,T>& y) {
274 return join(x.lo * y.lo, x.hi * y.hi);
275 }
276 SINT Vec<N,T> operator/(const Vec<N,T>& x, const Vec<N,T>& y) {
277 return join(x.lo / y.lo, x.hi / y.hi);
278 }
Mike Kleindcfc3ef2019-02-07 09:49:17 -0500279
Mike Kleina1711092020-09-02 09:00:57 -0500280 SINT Vec<N,T> operator^(const Vec<N,T>& x, const Vec<N,T>& y) {
281 return join(x.lo ^ y.lo, x.hi ^ y.hi);
282 }
283 SINT Vec<N,T> operator&(const Vec<N,T>& x, const Vec<N,T>& y) {
284 return join(x.lo & y.lo, x.hi & y.hi);
285 }
286 SINT Vec<N,T> operator|(const Vec<N,T>& x, const Vec<N,T>& y) {
287 return join(x.lo | y.lo, x.hi | y.hi);
288 }
Mike Kleindcfc3ef2019-02-07 09:49:17 -0500289
Mike Klein9a885b22019-04-16 12:07:23 -0500290 SINT Vec<N,T> operator!(const Vec<N,T>& x) { return join(!x.lo, !x.hi); }
291 SINT Vec<N,T> operator-(const Vec<N,T>& x) { return join(-x.lo, -x.hi); }
292 SINT Vec<N,T> operator~(const Vec<N,T>& x) { return join(~x.lo, ~x.hi); }
Mike Kleindcfc3ef2019-02-07 09:49:17 -0500293
Mike Kleina1711092020-09-02 09:00:57 -0500294 SINT Vec<N,T> operator<<(const Vec<N,T>& x, int k) { return join(x.lo << k, x.hi << k); }
295 SINT Vec<N,T> operator>>(const Vec<N,T>& x, int k) { return join(x.lo >> k, x.hi >> k); }
Mike Kleindcfc3ef2019-02-07 09:49:17 -0500296
Mike Kleina1711092020-09-02 09:00:57 -0500297 SINT Vec<N,M<T>> operator==(const Vec<N,T>& x, const Vec<N,T>& y) {
298 return join(x.lo == y.lo, x.hi == y.hi);
299 }
300 SINT Vec<N,M<T>> operator!=(const Vec<N,T>& x, const Vec<N,T>& y) {
301 return join(x.lo != y.lo, x.hi != y.hi);
302 }
303 SINT Vec<N,M<T>> operator<=(const Vec<N,T>& x, const Vec<N,T>& y) {
304 return join(x.lo <= y.lo, x.hi <= y.hi);
305 }
306 SINT Vec<N,M<T>> operator>=(const Vec<N,T>& x, const Vec<N,T>& y) {
307 return join(x.lo >= y.lo, x.hi >= y.hi);
308 }
309 SINT Vec<N,M<T>> operator< (const Vec<N,T>& x, const Vec<N,T>& y) {
310 return join(x.lo < y.lo, x.hi < y.hi);
311 }
312 SINT Vec<N,M<T>> operator> (const Vec<N,T>& x, const Vec<N,T>& y) {
313 return join(x.lo > y.lo, x.hi > y.hi);
314 }
Mike Klein455c7472019-02-05 13:42:46 -0500315#endif
316
Mike Kleindcfc3ef2019-02-07 09:49:17 -0500317// Some operations we want are not expressible with Clang/GCC vector
318// extensions, so we implement them using the recursive approach.
Mike Klein455c7472019-02-05 13:42:46 -0500319
Mike Kleindcfc3ef2019-02-07 09:49:17 -0500320// N == 1 scalar implementations.
Mike Klein9a885b22019-04-16 12:07:23 -0500321SIT Vec<1,T> if_then_else(const Vec<1,M<T>>& cond, const Vec<1,T>& t, const Vec<1,T>& e) {
Mike Klein5cb47d62020-07-10 15:46:46 -0500322 // In practice this scalar implementation is unlikely to be used. See if_then_else() below.
323 return bit_pun<Vec<1,T>>(( cond & bit_pun<Vec<1, M<T>>>(t)) |
324 (~cond & bit_pun<Vec<1, M<T>>>(e)) );
Mike Klein455c7472019-02-05 13:42:46 -0500325}
326
Mike Klein9a885b22019-04-16 12:07:23 -0500327SIT bool any(const Vec<1,T>& x) { return x.val != 0; }
328SIT bool all(const Vec<1,T>& x) { return x.val != 0; }
Mike Klein42925152019-02-06 11:56:58 -0500329
Mike Klein9a885b22019-04-16 12:07:23 -0500330SIT T min(const Vec<1,T>& x) { return x.val; }
331SIT T max(const Vec<1,T>& x) { return x.val; }
Mike Kleindcfc3ef2019-02-07 09:49:17 -0500332
Mike Klein9a885b22019-04-16 12:07:23 -0500333SIT Vec<1,T> min(const Vec<1,T>& x, const Vec<1,T>& y) { return std::min(x.val, y.val); }
334SIT Vec<1,T> max(const Vec<1,T>& x, const Vec<1,T>& y) { return std::max(x.val, y.val); }
Florin Malita3facc9c2020-05-04 09:26:15 -0400335SIT Vec<1,T> pow(const Vec<1,T>& x, const Vec<1,T>& y) { return std::pow(x.val, y.val); }
Mike Kleindcfc3ef2019-02-07 09:49:17 -0500336
Mike Reed8520e762020-04-30 12:06:23 -0400337SIT Vec<1,T> atan(const Vec<1,T>& x) { return std:: atan(x.val); }
Mike Klein9a885b22019-04-16 12:07:23 -0500338SIT Vec<1,T> ceil(const Vec<1,T>& x) { return std:: ceil(x.val); }
339SIT Vec<1,T> floor(const Vec<1,T>& x) { return std::floor(x.val); }
340SIT Vec<1,T> trunc(const Vec<1,T>& x) { return std::trunc(x.val); }
341SIT Vec<1,T> round(const Vec<1,T>& x) { return std::round(x.val); }
342SIT Vec<1,T> sqrt(const Vec<1,T>& x) { return std:: sqrt(x.val); }
343SIT Vec<1,T> abs(const Vec<1,T>& x) { return std:: abs(x.val); }
Mike Kleinc2160252020-04-29 09:56:56 -0500344SIT Vec<1,T> sin(const Vec<1,T>& x) { return std:: sin(x.val); }
345SIT Vec<1,T> cos(const Vec<1,T>& x) { return std:: cos(x.val); }
346SIT Vec<1,T> tan(const Vec<1,T>& x) { return std:: tan(x.val); }
Mike Kleindcfc3ef2019-02-07 09:49:17 -0500347
Mike Klein5caf7de2020-03-12 11:05:46 -0500348SIT Vec<1,int> lrint(const Vec<1,T>& x) { return (int)std::lrint(x.val); }
349
Mike Klein9a885b22019-04-16 12:07:23 -0500350SIT Vec<1,T> rcp(const Vec<1,T>& x) { return 1 / x.val; }
351SIT Vec<1,T> rsqrt(const Vec<1,T>& x) { return rcp(sqrt(x)); }
Mike Kleindcfc3ef2019-02-07 09:49:17 -0500352
353// All default N != 1 implementations just recurse on lo and hi halves.
Mike Klein9a885b22019-04-16 12:07:23 -0500354SINT Vec<N,T> if_then_else(const Vec<N,M<T>>& cond, const Vec<N,T>& t, const Vec<N,T>& e) {
Mike Klein5cb47d62020-07-10 15:46:46 -0500355 // Specializations inline here so they can generalize what types the apply to.
356 // (This header is used in C++14 contexts, so we have to kind of fake constexpr if.)
357#if defined(__AVX__)
Mike Kleinc3ad6a12020-09-15 15:26:22 -0500358 if /*constexpr*/ (N*sizeof(T) == 32) {
359 return unchecked_bit_pun<Vec<N,T>>(_mm256_blendv_epi8(unchecked_bit_pun<__m256i>(e),
360 unchecked_bit_pun<__m256i>(t),
361 unchecked_bit_pun<__m256i>(cond)));
Mike Klein5cb47d62020-07-10 15:46:46 -0500362 }
363#endif
364#if defined(__SSE4_1__)
Mike Kleinc3ad6a12020-09-15 15:26:22 -0500365 if /*constexpr*/ (N*sizeof(T) == 16) {
366 return unchecked_bit_pun<Vec<N,T>>(_mm_blendv_epi8(unchecked_bit_pun<__m128i>(e),
367 unchecked_bit_pun<__m128i>(t),
368 unchecked_bit_pun<__m128i>(cond)));
Mike Klein5cb47d62020-07-10 15:46:46 -0500369 }
370#endif
371#if defined(__ARM_NEON)
Mike Kleinc3ad6a12020-09-15 15:26:22 -0500372 if /*constexpr*/ (N*sizeof(T) == 16) {
373 return unchecked_bit_pun<Vec<N,T>>(vbslq_u8(unchecked_bit_pun<uint8x16_t>(cond),
374 unchecked_bit_pun<uint8x16_t>(t),
375 unchecked_bit_pun<uint8x16_t>(e)));
Mike Klein5cb47d62020-07-10 15:46:46 -0500376 }
377#endif
378 // Recurse for large vectors to try to hit the specializations above.
Mike Kleinc3ad6a12020-09-15 15:26:22 -0500379 if /*constexpr*/ (N*sizeof(T) > 16) {
Mike Klein5cb47d62020-07-10 15:46:46 -0500380 return join(if_then_else(cond.lo, t.lo, e.lo),
381 if_then_else(cond.hi, t.hi, e.hi));
382 }
383 // This default can lead to better code than the recursing onto scalars.
384 return bit_pun<Vec<N,T>>(( cond & bit_pun<Vec<N, M<T>>>(t)) |
385 (~cond & bit_pun<Vec<N, M<T>>>(e)) );
Mike Kleindcfc3ef2019-02-07 09:49:17 -0500386}
387
Mike Klein9a885b22019-04-16 12:07:23 -0500388SINT bool any(const Vec<N,T>& x) { return any(x.lo) || any(x.hi); }
389SINT bool all(const Vec<N,T>& x) { return all(x.lo) && all(x.hi); }
Mike Kleindcfc3ef2019-02-07 09:49:17 -0500390
Mike Klein9a885b22019-04-16 12:07:23 -0500391SINT T min(const Vec<N,T>& x) { return std::min(min(x.lo), min(x.hi)); }
392SINT T max(const Vec<N,T>& x) { return std::max(max(x.lo), max(x.hi)); }
Mike Kleindcfc3ef2019-02-07 09:49:17 -0500393
Mike Kleina1711092020-09-02 09:00:57 -0500394SINT Vec<N,T> min(const Vec<N,T>& x, const Vec<N,T>& y) {
395 return join(min(x.lo, y.lo), min(x.hi, y.hi));
396}
397SINT Vec<N,T> max(const Vec<N,T>& x, const Vec<N,T>& y) {
398 return join(max(x.lo, y.lo), max(x.hi, y.hi));
399}
400SINT Vec<N,T> pow(const Vec<N,T>& x, const Vec<N,T>& y) {
401 return join(pow(x.lo, y.lo), pow(x.hi, y.hi));
402}
Mike Kleindcfc3ef2019-02-07 09:49:17 -0500403
Mike Reed8520e762020-04-30 12:06:23 -0400404SINT Vec<N,T> atan(const Vec<N,T>& x) { return join( atan(x.lo), atan(x.hi)); }
Mike Klein9a885b22019-04-16 12:07:23 -0500405SINT Vec<N,T> ceil(const Vec<N,T>& x) { return join( ceil(x.lo), ceil(x.hi)); }
406SINT Vec<N,T> floor(const Vec<N,T>& x) { return join(floor(x.lo), floor(x.hi)); }
407SINT Vec<N,T> trunc(const Vec<N,T>& x) { return join(trunc(x.lo), trunc(x.hi)); }
408SINT Vec<N,T> round(const Vec<N,T>& x) { return join(round(x.lo), round(x.hi)); }
409SINT Vec<N,T> sqrt(const Vec<N,T>& x) { return join( sqrt(x.lo), sqrt(x.hi)); }
410SINT Vec<N,T> abs(const Vec<N,T>& x) { return join( abs(x.lo), abs(x.hi)); }
Mike Kleinc2160252020-04-29 09:56:56 -0500411SINT Vec<N,T> sin(const Vec<N,T>& x) { return join( sin(x.lo), sin(x.hi)); }
412SINT Vec<N,T> cos(const Vec<N,T>& x) { return join( cos(x.lo), cos(x.hi)); }
413SINT Vec<N,T> tan(const Vec<N,T>& x) { return join( tan(x.lo), tan(x.hi)); }
Mike Kleindcfc3ef2019-02-07 09:49:17 -0500414
Mike Klein5caf7de2020-03-12 11:05:46 -0500415SINT Vec<N,int> lrint(const Vec<N,T>& x) { return join(lrint(x.lo), lrint(x.hi)); }
416
Mike Klein9a885b22019-04-16 12:07:23 -0500417SINT Vec<N,T> rcp(const Vec<N,T>& x) { return join( rcp(x.lo), rcp(x.hi)); }
418SINT Vec<N,T> rsqrt(const Vec<N,T>& x) { return join(rsqrt(x.lo), rsqrt(x.hi)); }
Mike Kleindcfc3ef2019-02-07 09:49:17 -0500419
Mike Klein455c7472019-02-05 13:42:46 -0500420
Mike Klein42925152019-02-06 11:56:58 -0500421// Scalar/vector operations just splat the scalar to a vector...
Mike Klein9a885b22019-04-16 12:07:23 -0500422SINTU Vec<N,T> operator+ (U x, const Vec<N,T>& y) { return Vec<N,T>(x) + y; }
423SINTU Vec<N,T> operator- (U x, const Vec<N,T>& y) { return Vec<N,T>(x) - y; }
424SINTU Vec<N,T> operator* (U x, const Vec<N,T>& y) { return Vec<N,T>(x) * y; }
425SINTU Vec<N,T> operator/ (U x, const Vec<N,T>& y) { return Vec<N,T>(x) / y; }
426SINTU Vec<N,T> operator^ (U x, const Vec<N,T>& y) { return Vec<N,T>(x) ^ y; }
427SINTU Vec<N,T> operator& (U x, const Vec<N,T>& y) { return Vec<N,T>(x) & y; }
428SINTU Vec<N,T> operator| (U x, const Vec<N,T>& y) { return Vec<N,T>(x) | y; }
429SINTU Vec<N,M<T>> operator==(U x, const Vec<N,T>& y) { return Vec<N,T>(x) == y; }
430SINTU Vec<N,M<T>> operator!=(U x, const Vec<N,T>& y) { return Vec<N,T>(x) != y; }
431SINTU Vec<N,M<T>> operator<=(U x, const Vec<N,T>& y) { return Vec<N,T>(x) <= y; }
432SINTU Vec<N,M<T>> operator>=(U x, const Vec<N,T>& y) { return Vec<N,T>(x) >= y; }
433SINTU Vec<N,M<T>> operator< (U x, const Vec<N,T>& y) { return Vec<N,T>(x) < y; }
434SINTU Vec<N,M<T>> operator> (U x, const Vec<N,T>& y) { return Vec<N,T>(x) > y; }
435SINTU Vec<N,T> min(U x, const Vec<N,T>& y) { return min(Vec<N,T>(x), y); }
436SINTU Vec<N,T> max(U x, const Vec<N,T>& y) { return max(Vec<N,T>(x), y); }
Florin Malita3facc9c2020-05-04 09:26:15 -0400437SINTU Vec<N,T> pow(U x, const Vec<N,T>& y) { return pow(Vec<N,T>(x), y); }
Mike Kleindcfc3ef2019-02-07 09:49:17 -0500438
Mike Klein42925152019-02-06 11:56:58 -0500439// ... and same deal for vector/scalar operations.
Mike Klein9a885b22019-04-16 12:07:23 -0500440SINTU Vec<N,T> operator+ (const Vec<N,T>& x, U y) { return x + Vec<N,T>(y); }
441SINTU Vec<N,T> operator- (const Vec<N,T>& x, U y) { return x - Vec<N,T>(y); }
442SINTU Vec<N,T> operator* (const Vec<N,T>& x, U y) { return x * Vec<N,T>(y); }
443SINTU Vec<N,T> operator/ (const Vec<N,T>& x, U y) { return x / Vec<N,T>(y); }
444SINTU Vec<N,T> operator^ (const Vec<N,T>& x, U y) { return x ^ Vec<N,T>(y); }
445SINTU Vec<N,T> operator& (const Vec<N,T>& x, U y) { return x & Vec<N,T>(y); }
446SINTU Vec<N,T> operator| (const Vec<N,T>& x, U y) { return x | Vec<N,T>(y); }
447SINTU Vec<N,M<T>> operator==(const Vec<N,T>& x, U y) { return x == Vec<N,T>(y); }
448SINTU Vec<N,M<T>> operator!=(const Vec<N,T>& x, U y) { return x != Vec<N,T>(y); }
449SINTU Vec<N,M<T>> operator<=(const Vec<N,T>& x, U y) { return x <= Vec<N,T>(y); }
450SINTU Vec<N,M<T>> operator>=(const Vec<N,T>& x, U y) { return x >= Vec<N,T>(y); }
451SINTU Vec<N,M<T>> operator< (const Vec<N,T>& x, U y) { return x < Vec<N,T>(y); }
452SINTU Vec<N,M<T>> operator> (const Vec<N,T>& x, U y) { return x > Vec<N,T>(y); }
453SINTU Vec<N,T> min(const Vec<N,T>& x, U y) { return min(x, Vec<N,T>(y)); }
454SINTU Vec<N,T> max(const Vec<N,T>& x, U y) { return max(x, Vec<N,T>(y)); }
Florin Malita3facc9c2020-05-04 09:26:15 -0400455SINTU Vec<N,T> pow(const Vec<N,T>& x, U y) { return pow(x, Vec<N,T>(y)); }
Mike Kleindcfc3ef2019-02-07 09:49:17 -0500456
Mike Klein42925152019-02-06 11:56:58 -0500457// The various op= operators, for vectors...
Mike Klein9a885b22019-04-16 12:07:23 -0500458SINT Vec<N,T>& operator+=(Vec<N,T>& x, const Vec<N,T>& y) { return (x = x + y); }
459SINT Vec<N,T>& operator-=(Vec<N,T>& x, const Vec<N,T>& y) { return (x = x - y); }
460SINT Vec<N,T>& operator*=(Vec<N,T>& x, const Vec<N,T>& y) { return (x = x * y); }
461SINT Vec<N,T>& operator/=(Vec<N,T>& x, const Vec<N,T>& y) { return (x = x / y); }
462SINT Vec<N,T>& operator^=(Vec<N,T>& x, const Vec<N,T>& y) { return (x = x ^ y); }
463SINT Vec<N,T>& operator&=(Vec<N,T>& x, const Vec<N,T>& y) { return (x = x & y); }
464SINT Vec<N,T>& operator|=(Vec<N,T>& x, const Vec<N,T>& y) { return (x = x | y); }
Mike Kleindcfc3ef2019-02-07 09:49:17 -0500465
Mike Klein42925152019-02-06 11:56:58 -0500466// ... for scalars...
Mike Kleinf4438d52019-03-14 13:30:42 -0500467SINTU Vec<N,T>& operator+=(Vec<N,T>& x, U y) { return (x = x + Vec<N,T>(y)); }
468SINTU Vec<N,T>& operator-=(Vec<N,T>& x, U y) { return (x = x - Vec<N,T>(y)); }
469SINTU Vec<N,T>& operator*=(Vec<N,T>& x, U y) { return (x = x * Vec<N,T>(y)); }
470SINTU Vec<N,T>& operator/=(Vec<N,T>& x, U y) { return (x = x / Vec<N,T>(y)); }
471SINTU Vec<N,T>& operator^=(Vec<N,T>& x, U y) { return (x = x ^ Vec<N,T>(y)); }
472SINTU Vec<N,T>& operator&=(Vec<N,T>& x, U y) { return (x = x & Vec<N,T>(y)); }
473SINTU Vec<N,T>& operator|=(Vec<N,T>& x, U y) { return (x = x | Vec<N,T>(y)); }
Mike Kleindcfc3ef2019-02-07 09:49:17 -0500474
Mike Klein42925152019-02-06 11:56:58 -0500475// ... and for shifts.
Mike Kleindcfc3ef2019-02-07 09:49:17 -0500476SINT Vec<N,T>& operator<<=(Vec<N,T>& x, int bits) { return (x = x << bits); }
477SINT Vec<N,T>& operator>>=(Vec<N,T>& x, int bits) { return (x = x >> bits); }
Mike Klein455c7472019-02-05 13:42:46 -0500478
Mike Klein53a52982019-02-06 15:48:12 -0500479// cast() Vec<N,S> to Vec<N,D>, as if applying a C-cast to each lane.
Mike Kleindcfc3ef2019-02-07 09:49:17 -0500480template <typename D, typename S>
Mike Kleina1711092020-09-02 09:00:57 -0500481SI Vec<1,D> cast(const Vec<1,S>& src) { return (D)src.val; }
Mike Kleindcfc3ef2019-02-07 09:49:17 -0500482
Mike Klein42925152019-02-06 11:56:58 -0500483template <typename D, int N, typename S>
Mike Kleina1711092020-09-02 09:00:57 -0500484SI Vec<N,D> cast(const Vec<N,S>& src) {
Mike Klein42925152019-02-06 11:56:58 -0500485#if !defined(SKNX_NO_SIMD) && defined(__clang__)
Mike Kleinda7b0532019-04-10 12:40:31 -0500486 return to_vec(__builtin_convertvector(to_vext(src), VExt<N,D>));
Mike Klein42925152019-02-06 11:56:58 -0500487#else
Mike Kleindcfc3ef2019-02-07 09:49:17 -0500488 return join(cast<D>(src.lo), cast<D>(src.hi));
Mike Klein42925152019-02-06 11:56:58 -0500489#endif
490}
491
Mike Klein53a52982019-02-06 15:48:12 -0500492// Shuffle values from a vector pretty arbitrarily:
493// skvx::Vec<4,float> rgba = {R,G,B,A};
494// shuffle<2,1,0,3> (rgba) ~> {B,G,R,A}
495// shuffle<2,1> (rgba) ~> {B,G}
496// shuffle<2,1,2,1,2,1,2,1>(rgba) ~> {B,G,B,G,B,G,B,G}
497// shuffle<3,3,3,3> (rgba) ~> {A,A,A,A}
498// The only real restriction is that the output also be a legal N=power-of-two sknx::Vec.
499template <int... Ix, int N, typename T>
Mike Kleina1711092020-09-02 09:00:57 -0500500SI Vec<sizeof...(Ix),T> shuffle(const Vec<N,T>& x) {
Mike Klein3bad19c2019-04-11 14:14:16 -0500501#if !defined(SKNX_NO_SIMD) && defined(__clang__)
502 return to_vec<sizeof...(Ix),T>(__builtin_shufflevector(to_vext(x), to_vext(x), Ix...));
503#else
Mike Klein53a52982019-02-06 15:48:12 -0500504 return { x[Ix]... };
Mike Klein3bad19c2019-04-11 14:14:16 -0500505#endif
Mike Klein53a52982019-02-06 15:48:12 -0500506}
Mike Klein42925152019-02-06 11:56:58 -0500507
Mike Kleina1711092020-09-02 09:00:57 -0500508// fma() delivers a fused mul-add, even if that's really expensive.
509SI Vec<1,float> fma(const Vec<1,float>& x, const Vec<1,float>& y, const Vec<1,float>& z) {
Mike Kleinec370972020-03-05 10:15:35 -0600510 return std::fma(x.val, y.val, z.val);
511}
Mike Kleina1711092020-09-02 09:00:57 -0500512SIN Vec<N,float> fma(const Vec<N,float>& x, const Vec<N,float>& y, const Vec<N,float>& z) {
Mike Kleinec370972020-03-05 10:15:35 -0600513 return join(fma(x.lo, y.lo, z.lo),
514 fma(x.hi, y.hi, z.hi));
515}
516
Mike Kleina1711092020-09-02 09:00:57 -0500517SIN Vec<N,float> fract(const Vec<N,float>& x) {
Mike Reed8520e762020-04-30 12:06:23 -0400518 return x - floor(x);
519}
520
Mike Klein4d680cd2020-07-15 09:58:51 -0500521// The default cases for to_half/from_half are borrowed from skcms,
522// and assume inputs are finite and treat/flush denorm half floats as/to zero.
523// Key constants to watch for:
524// - a float is 32-bit, 1-8-23 sign-exponent-mantissa, with 127 exponent bias;
525// - a half is 16-bit, 1-5-10 sign-exponent-mantissa, with 15 exponent bias.
Mike Kleina1711092020-09-02 09:00:57 -0500526SIN Vec<N,uint16_t> to_half_finite_ftz(const Vec<N,float>& x) {
Mike Klein4d680cd2020-07-15 09:58:51 -0500527 Vec<N,uint32_t> sem = bit_pun<Vec<N,uint32_t>>(x),
528 s = sem & 0x8000'0000,
529 em = sem ^ s,
530 is_denorm = em < 0x3880'0000;
531 return cast<uint16_t>(if_then_else(is_denorm, Vec<N,uint32_t>(0)
532 , (s>>16) + (em>>13) - ((127-15)<<10)));
533}
Mike Kleina1711092020-09-02 09:00:57 -0500534SIN Vec<N,float> from_half_finite_ftz(const Vec<N,uint16_t>& x) {
Mike Klein4d680cd2020-07-15 09:58:51 -0500535 Vec<N,uint32_t> wide = cast<uint32_t>(x),
536 s = wide & 0x8000,
537 em = wide ^ s;
538 auto is_denorm = bit_pun<Vec<N,int32_t>>(em < 0x0400);
539 return if_then_else(is_denorm, Vec<N,float>(0)
540 , bit_pun<Vec<N,float>>( (s<<16) + (em<<13) + ((127-15)<<23) ));
541}
542
543// Like if_then_else(), these N=1 base cases won't actually be used unless explicitly called.
Mike Kleina1711092020-09-02 09:00:57 -0500544SI Vec<1,uint16_t> to_half(const Vec<1,float>& x) { return to_half_finite_ftz(x); }
545SI Vec<1,float> from_half(const Vec<1,uint16_t>& x) { return from_half_finite_ftz(x); }
Mike Klein4d680cd2020-07-15 09:58:51 -0500546
Mike Kleina1711092020-09-02 09:00:57 -0500547SIN Vec<N,uint16_t> to_half(const Vec<N,float>& x) {
Mike Klein4d680cd2020-07-15 09:58:51 -0500548#if defined(__F16C__)
549 if /*constexpr*/ (N == 8) {
550 return unchecked_bit_pun<Vec<N,uint16_t>>(_mm256_cvtps_ph(unchecked_bit_pun<__m256>(x),
551 _MM_FROUND_CUR_DIRECTION));
552 }
553#endif
554#if defined(__aarch64__)
555 if /*constexpr*/ (N == 4) {
556 return unchecked_bit_pun<Vec<N,uint16_t>>(vcvt_f16_f32(unchecked_bit_pun<float32x4_t>(x)));
557
558 }
559#endif
560 if /*constexpr*/ (N > 4) {
561 return join(to_half(x.lo),
562 to_half(x.hi));
563 }
564 return to_half_finite_ftz(x);
565}
566
Mike Kleina1711092020-09-02 09:00:57 -0500567SIN Vec<N,float> from_half(const Vec<N,uint16_t>& x) {
Mike Klein4d680cd2020-07-15 09:58:51 -0500568#if defined(__F16C__)
569 if /*constexpr*/ (N == 8) {
570 return unchecked_bit_pun<Vec<N,float>>(_mm256_cvtph_ps(unchecked_bit_pun<__m128i>(x)));
571 }
572#endif
573#if defined(__aarch64__)
574 if /*constexpr*/ (N == 4) {
Jose Dapena Pazdc4da5a2020-07-31 20:04:25 +0200575 return unchecked_bit_pun<Vec<N,float>>(vcvt_f32_f16(unchecked_bit_pun<float16x4_t>(x)));
Mike Klein4d680cd2020-07-15 09:58:51 -0500576 }
577#endif
578 if /*constexpr*/ (N > 4) {
579 return join(from_half(x.lo),
580 from_half(x.hi));
581 }
582 return from_half_finite_ftz(x);
583}
584
Mike Reed8520e762020-04-30 12:06:23 -0400585
Mike Klein4b44a0d2019-04-11 11:52:51 -0500586// div255(x) = (x + 127) / 255 is a bit-exact rounding divide-by-255, packing down to 8-bit.
Mike Kleina1711092020-09-02 09:00:57 -0500587SIN Vec<N,uint8_t> div255(const Vec<N,uint16_t>& x) {
Mike Klein4b44a0d2019-04-11 11:52:51 -0500588 return cast<uint8_t>( (x+127)/255 );
589}
590
591// approx_scale(x,y) approximates div255(cast<uint16_t>(x)*cast<uint16_t>(y)) within a bit,
592// and is always perfect when x or y is 0 or 255.
Mike Kleina1711092020-09-02 09:00:57 -0500593SIN Vec<N,uint8_t> approx_scale(const Vec<N,uint8_t>& x, const Vec<N,uint8_t>& y) {
Mike Klein4b44a0d2019-04-11 11:52:51 -0500594 // All of (x*y+x)/256, (x*y+y)/256, and (x*y+255)/256 meet the criteria above.
595 // We happen to have historically picked (x*y+x)/256.
596 auto X = cast<uint16_t>(x),
597 Y = cast<uint16_t>(y);
598 return cast<uint8_t>( (X*Y+X)/256 );
599}
600
Mike Klein7d3b27d2019-06-07 10:57:58 -0500601#if !defined(SKNX_NO_SIMD) && defined(__ARM_NEON)
Mike Klein9a885b22019-04-16 12:07:23 -0500602 // With NEON we can do eight u8*u8 -> u16 in one instruction, vmull_u8 (read, mul-long).
Mike Kleina1711092020-09-02 09:00:57 -0500603 SI Vec<8,uint16_t> mull(const Vec<8,uint8_t>& x,
604 const Vec<8,uint8_t>& y) {
Mike Klein9a885b22019-04-16 12:07:23 -0500605 return to_vec<8,uint16_t>(vmull_u8(to_vext(x),
606 to_vext(y)));
607 }
608
Mike Kleina1711092020-09-02 09:00:57 -0500609 SIN std::enable_if_t<(N < 8), Vec<N,uint16_t>> mull(const Vec<N,uint8_t>& x,
610 const Vec<N,uint8_t>& y) {
Mike Klein9a885b22019-04-16 12:07:23 -0500611 // N < 8 --> double up data until N == 8, returning the part we need.
612 return mull(join(x,x),
613 join(y,y)).lo;
614 }
615
Mike Kleina1711092020-09-02 09:00:57 -0500616 SIN std::enable_if_t<(N > 8), Vec<N,uint16_t>> mull(const Vec<N,uint8_t>& x,
617 const Vec<N,uint8_t>& y) {
Mike Klein9a885b22019-04-16 12:07:23 -0500618 // N > 8 --> usual join(lo,hi) strategy to recurse down to N == 8.
619 return join(mull(x.lo, y.lo),
620 mull(x.hi, y.hi));
621 }
622#else
623 // Nothing special when we don't have NEON... just cast up to 16-bit and multiply.
Mike Kleina1711092020-09-02 09:00:57 -0500624 SIN Vec<N,uint16_t> mull(const Vec<N,uint8_t>& x,
625 const Vec<N,uint8_t>& y) {
Mike Klein9a885b22019-04-16 12:07:23 -0500626 return cast<uint16_t>(x)
627 * cast<uint16_t>(y);
628 }
629#endif
630
Mike Klein41b995c2019-02-27 10:24:55 -0600631#if !defined(SKNX_NO_SIMD)
Mike Klein7d3b27d2019-06-07 10:57:58 -0500632
Mike Klein41b995c2019-02-27 10:24:55 -0600633 // Platform-specific specializations and overloads can now drop in here.
Mike Kleindcfc3ef2019-02-07 09:49:17 -0500634
Mike Klein5caf7de2020-03-12 11:05:46 -0500635 #if defined(__AVX__)
Mike Kleina1711092020-09-02 09:00:57 -0500636 SI Vec<8,float> sqrt(const Vec<8,float>& x) {
Mike Klein5caf7de2020-03-12 11:05:46 -0500637 return bit_pun<Vec<8,float>>(_mm256_sqrt_ps(bit_pun<__m256>(x)));
638 }
Mike Kleina1711092020-09-02 09:00:57 -0500639 SI Vec<8,float> rsqrt(const Vec<8,float>& x) {
Mike Klein5caf7de2020-03-12 11:05:46 -0500640 return bit_pun<Vec<8,float>>(_mm256_rsqrt_ps(bit_pun<__m256>(x)));
641 }
Mike Kleina1711092020-09-02 09:00:57 -0500642 SI Vec<8,float> rcp(const Vec<8,float>& x) {
Mike Klein5caf7de2020-03-12 11:05:46 -0500643 return bit_pun<Vec<8,float>>(_mm256_rcp_ps(bit_pun<__m256>(x)));
644 }
Mike Kleina1711092020-09-02 09:00:57 -0500645 SI Vec<8,int> lrint(const Vec<8,float>& x) {
Mike Klein5caf7de2020-03-12 11:05:46 -0500646 return bit_pun<Vec<8,int>>(_mm256_cvtps_epi32(bit_pun<__m256>(x)));
647 }
648 #endif
649
Mike Klein7d3b27d2019-06-07 10:57:58 -0500650 #if defined(__SSE__)
Mike Kleina1711092020-09-02 09:00:57 -0500651 SI Vec<4,float> sqrt(const Vec<4,float>& x) {
Mike Kleinda7b0532019-04-10 12:40:31 -0500652 return bit_pun<Vec<4,float>>(_mm_sqrt_ps(bit_pun<__m128>(x)));
653 }
Mike Kleina1711092020-09-02 09:00:57 -0500654 SI Vec<4,float> rsqrt(const Vec<4,float>& x) {
Mike Kleinda7b0532019-04-10 12:40:31 -0500655 return bit_pun<Vec<4,float>>(_mm_rsqrt_ps(bit_pun<__m128>(x)));
656 }
Mike Kleina1711092020-09-02 09:00:57 -0500657 SI Vec<4,float> rcp(const Vec<4,float>& x) {
Mike Kleinda7b0532019-04-10 12:40:31 -0500658 return bit_pun<Vec<4,float>>(_mm_rcp_ps(bit_pun<__m128>(x)));
659 }
Mike Kleina1711092020-09-02 09:00:57 -0500660 SI Vec<4,int> lrint(const Vec<4,float>& x) {
Mike Klein5caf7de2020-03-12 11:05:46 -0500661 return bit_pun<Vec<4,int>>(_mm_cvtps_epi32(bit_pun<__m128>(x)));
662 }
Mike Klein41b995c2019-02-27 10:24:55 -0600663
Mike Kleina1711092020-09-02 09:00:57 -0500664 SI Vec<2,float> sqrt(const Vec<2,float>& x) {
Mike Kleinda7b0532019-04-10 12:40:31 -0500665 return shuffle<0,1>( sqrt(shuffle<0,1,0,1>(x)));
666 }
Mike Kleina1711092020-09-02 09:00:57 -0500667 SI Vec<2,float> rsqrt(const Vec<2,float>& x) {
Mike Kleinda7b0532019-04-10 12:40:31 -0500668 return shuffle<0,1>(rsqrt(shuffle<0,1,0,1>(x)));
669 }
Mike Kleina1711092020-09-02 09:00:57 -0500670 SI Vec<2,float> rcp(const Vec<2,float>& x) {
Mike Kleinda7b0532019-04-10 12:40:31 -0500671 return shuffle<0,1>( rcp(shuffle<0,1,0,1>(x)));
672 }
Mike Kleina1711092020-09-02 09:00:57 -0500673 SI Vec<2,int> lrint(const Vec<2,float>& x) {
Mike Klein5caf7de2020-03-12 11:05:46 -0500674 return shuffle<0,1>(lrint(shuffle<0,1,0,1>(x)));
675 }
Mike Kleinda7b0532019-04-10 12:40:31 -0500676 #endif
Mike Klein41b995c2019-02-27 10:24:55 -0600677
Mike Kleinec370972020-03-05 10:15:35 -0600678 #if defined(__AVX2__)
Mike Kleina1711092020-09-02 09:00:57 -0500679 SI Vec<4,float> fma(const Vec<4,float>& x, const Vec<4,float>& y, const Vec<4,float>& z) {
Mike Kleinec370972020-03-05 10:15:35 -0600680 return bit_pun<Vec<4,float>>(_mm_fmadd_ps(bit_pun<__m128>(x),
681 bit_pun<__m128>(y),
682 bit_pun<__m128>(z)));
683 }
684
Mike Kleina1711092020-09-02 09:00:57 -0500685 SI Vec<8,float> fma(const Vec<8,float>& x, const Vec<8,float>& y, const Vec<8,float>& z) {
Mike Kleinec370972020-03-05 10:15:35 -0600686 return bit_pun<Vec<8,float>>(_mm256_fmadd_ps(bit_pun<__m256>(x),
687 bit_pun<__m256>(y),
688 bit_pun<__m256>(z)));
689 }
690 #elif defined(__aarch64__)
Mike Kleina1711092020-09-02 09:00:57 -0500691 SI Vec<4,float> fma(const Vec<4,float>& x, const Vec<4,float>& y, const Vec<4,float>& z) {
Mike Kleinec370972020-03-05 10:15:35 -0600692 // These instructions tend to work like z += xy, so the order here is z,x,y.
693 return bit_pun<Vec<4,float>>(vfmaq_f32(bit_pun<float32x4_t>(z),
694 bit_pun<float32x4_t>(x),
695 bit_pun<float32x4_t>(y)));
696 }
697 #endif
698
Elliot Evansfe7e74b2020-06-30 16:08:44 -0600699 // WASM SIMD compatible operations which are not automatically compiled to SIMD commands
700 // by emscripten:
701 #if defined __wasm_simd128__
Mike Kleina1711092020-09-02 09:00:57 -0500702 SI Vec<4, float> rcp (const Vec<4, float>& x) { return 1.0f / x; }
703 SI Vec<2,double> rcp (const Vec<2,double>& x) { return 1.0f / x; }
704 SI Vec<4, float> rsqrt(const Vec<4, float>& x) { return 1.0f / sqrt(x); }
705 SI Vec<2,double> rsqrt(const Vec<2,double>& x) { return 1.0f / sqrt(x); }
706
707 SI Vec<4,float> min(const Vec<4,float>& x, const Vec<4,float>& y) {
Elliot Evansfe7e74b2020-06-30 16:08:44 -0600708 return to_vec<4,float>(wasm_f32x4_min(to_vext(x), to_vext(y)));
709 }
Mike Kleina1711092020-09-02 09:00:57 -0500710 SI Vec<4,float> max(const Vec<4,float>& x, const Vec<4,float>& y) {
Elliot Evansfe7e74b2020-06-30 16:08:44 -0600711 return to_vec<4,float>(wasm_f32x4_max(to_vext(x), to_vext(y)));
712 }
Mike Kleina1711092020-09-02 09:00:57 -0500713 SI Vec<4,float> sqrt(const Vec<4,float>& x) {
Elliot Evansfe7e74b2020-06-30 16:08:44 -0600714 return to_vec<4,float>(wasm_f32x4_sqrt(to_vext(x)));
715 }
Mike Kleina1711092020-09-02 09:00:57 -0500716 SI Vec<4,float> abs(const Vec<4,float>& x) {
Elliot Evansfe7e74b2020-06-30 16:08:44 -0600717 return to_vec<4,float>(wasm_f32x4_abs(to_vext(x)));
718 }
Elliot Evansfe7e74b2020-06-30 16:08:44 -0600719
Mike Kleina1711092020-09-02 09:00:57 -0500720 SI Vec<2,double> min(const Vec<2,double>& x, const Vec<2,double>& y) {
Elliot Evansfe7e74b2020-06-30 16:08:44 -0600721 return to_vec<2,double>(wasm_f64x2_min(to_vext(x), to_vext(y)));
722 }
Mike Kleina1711092020-09-02 09:00:57 -0500723 SI Vec<2,double> max(const Vec<2,double>& x, const Vec<2,double>& y) {
Elliot Evansfe7e74b2020-06-30 16:08:44 -0600724 return to_vec<2,double>(wasm_f64x2_max(to_vext(x), to_vext(y)));
725 }
Mike Kleina1711092020-09-02 09:00:57 -0500726 SI Vec<2,double> sqrt(const Vec<2,double>& x) {
Elliot Evansfe7e74b2020-06-30 16:08:44 -0600727 return to_vec<2,double>(wasm_f64x2_sqrt(to_vext(x)));
728 }
Mike Kleina1711092020-09-02 09:00:57 -0500729 SI Vec<2,double> abs(const Vec<2,double>& x) {
Elliot Evansfe7e74b2020-06-30 16:08:44 -0600730 return to_vec<2,double>(wasm_f64x2_abs(to_vext(x)));
731 }
Elliot Evansfe7e74b2020-06-30 16:08:44 -0600732
Mike Kleina1711092020-09-02 09:00:57 -0500733 SI bool any(const Vec<4, int32_t>& x) { return wasm_i32x4_any_true(to_vext(x)); }
734 SI bool any(const Vec<4,uint32_t>& x) { return wasm_i32x4_any_true(to_vext(x)); }
735 SI bool all(const Vec<4, int32_t>& x) { return wasm_i32x4_all_true(to_vext(x)); }
736 SI bool all(const Vec<4,uint32_t>& x) { return wasm_i32x4_all_true(to_vext(x)); }
737
738 SI Vec<4,int32_t> min(const Vec<4,int32_t>& x, const Vec<4,int32_t>& y) {
Elliot Evansfe7e74b2020-06-30 16:08:44 -0600739 return to_vec<4,int32_t>(wasm_i32x4_min(to_vext(x), to_vext(y)));
740 }
Mike Kleina1711092020-09-02 09:00:57 -0500741 SI Vec<4,int32_t> max(const Vec<4,int32_t>& x, const Vec<4,int32_t>& y) {
Elliot Evansfe7e74b2020-06-30 16:08:44 -0600742 return to_vec<4,int32_t>(wasm_i32x4_max(to_vext(x), to_vext(y)));
743 }
Mike Kleina1711092020-09-02 09:00:57 -0500744 SI Vec<4,int32_t> abs(const Vec<4,int32_t>& x) {
Elliot Evansfe7e74b2020-06-30 16:08:44 -0600745 return to_vec<4,int32_t>(wasm_i32x4_abs(to_vext(x)));
746 }
747
Mike Kleina1711092020-09-02 09:00:57 -0500748 SI Vec<4,uint32_t> min(const Vec<4,uint32_t>& x, const Vec<4,uint32_t>& y) {
Elliot Evansfe7e74b2020-06-30 16:08:44 -0600749 return to_vec<4,uint32_t>(wasm_u32x4_min(to_vext(x), to_vext(y)));
750 }
Mike Kleina1711092020-09-02 09:00:57 -0500751 SI Vec<4,uint32_t> max(const Vec<4,uint32_t>& x, const Vec<4,uint32_t>& y) {
Elliot Evansfe7e74b2020-06-30 16:08:44 -0600752 return to_vec<4,uint32_t>(wasm_u32x4_max(to_vext(x), to_vext(y)));
753 }
754 #endif
755
Mike Klein7d3b27d2019-06-07 10:57:58 -0500756#endif // !defined(SKNX_NO_SIMD)
Mike Kleindcfc3ef2019-02-07 09:49:17 -0500757
758} // namespace skvx
759
Mike Kleinf4438d52019-03-14 13:30:42 -0500760#undef SINTU
Mike Kleindcfc3ef2019-02-07 09:49:17 -0500761#undef SINT
762#undef SIT
Mike Kleina1711092020-09-02 09:00:57 -0500763#undef SI
Mike Klein455c7472019-02-05 13:42:46 -0500764
765#endif//SKVX_DEFINED