1 /* 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 19 // and alignment[1][2] and is safe to use across translation units freely. 20 // 21 // [1] Ideally we'd only align to T, but that tanks ARMv7 NEON codegen. 22 // [2] Some compilers barf if we try to use N*sizeof(T), so instead we leave them at T. 23 24 // Please try to keep this file independent of Skia headers. 25 #include <algorithm> // std::min, std::max 26 #include <cmath> // std::ceil, std::floor, std::trunc, std::round, std::sqrt, etc. 27 #include <cstdint> // intXX_t 28 #include <cstring> // memcpy() 29 #include <initializer_list> // std::initializer_list 30 31 #if defined(__SSE__) 32 #include <immintrin.h> 33 #elif defined(__ARM_NEON) 34 #include <arm_neon.h> 35 #endif 36 37 #if !defined(__clang__) && defined(__GNUC__) && defined(__mips64) 38 // GCC 7 hits an internal compiler error when targeting MIPS64. 39 #define SKVX_ALIGNMENT 40 #elif !defined(__clang__) && defined(_MSC_VER) && defined(_M_IX86) 41 // Our SkVx unit tests fail when built by MSVC for 32-bit x86. 42 #define SKVX_ALIGNMENT 43 #else 44 #define SKVX_ALIGNMENT alignas(N * sizeof(T)) 45 #endif 46 47 48 namespace skvx { 49 50 // All Vec have the same simple memory layout, the same as `T vec[N]`. 51 // This gives Vec a consistent ABI, letting them pass between files compiled with 52 // different instruction sets (e.g. SSE2 and AVX2) without fear of ODR violation. 53 template <int N, typename T> 54 struct SKVX_ALIGNMENT Vec { 55 static_assert((N & (N-1)) == 0, "N must be a power of 2."); 56 static_assert(sizeof(T) >= alignof(T), "What kind of crazy T is this?"); 57 58 Vec<N/2,T> lo, hi; 59 60 // Methods belong here in the class declaration of Vec only if: 61 // - they must be here, like constructors or operator[]; 62 // - they'll definitely never want a specialized implementation. 63 // Other operations on Vec should be defined outside the type. 64 65 Vec() = default; 66 67 template <typename U, 68 typename=typename std::enable_if<std::is_convertible<U,T>::value>::type> VecVec69 Vec(U x) : lo(x), hi(x) {} 70 VecVec71 Vec(std::initializer_list<T> xs) { 72 T vals[N] = {0}; 73 memcpy(vals, xs.begin(), std::min(xs.size(), (size_t)N)*sizeof(T)); 74 75 lo = Vec<N/2,T>::Load(vals + 0); 76 hi = Vec<N/2,T>::Load(vals + N/2); 77 } 78 79 T operator[](int i) const { return i < N/2 ? lo[i] : hi[i-N/2]; } 80 T& operator[](int i) { return i < N/2 ? lo[i] : hi[i-N/2]; } 81 LoadVec82 static Vec Load(const void* ptr) { 83 Vec v; 84 memcpy(&v, ptr, sizeof(Vec)); 85 return v; 86 } storeVec87 void store(void* ptr) const { 88 memcpy(ptr, this, sizeof(Vec)); 89 } 90 }; 91 92 template <typename T> 93 struct Vec<1,T> { 94 T val; 95 96 Vec() = default; 97 98 template <typename U, 99 typename=typename std::enable_if<std::is_convertible<U,T>::value>::type> 100 Vec(U x) : val(x) {} 101 102 Vec(std::initializer_list<T> xs) : val(xs.size() ? *xs.begin() : 0) {} 103 104 T operator[](int) const { return val; } 105 T& operator[](int) { return val; } 106 107 static Vec Load(const void* ptr) { 108 Vec v; 109 memcpy(&v, ptr, sizeof(Vec)); 110 return v; 111 } 112 void store(void* ptr) const { 113 memcpy(ptr, this, sizeof(Vec)); 114 } 115 }; 116 117 #if defined(__GNUC__) && !defined(__clang__) && defined(__SSE__) 118 // GCC warns about ABI changes when returning >= 32 byte vectors when -mavx is not enabled. 119 // This only happens for types like VExt whose ABI we don't care about, not for Vec itself. 120 #pragma GCC diagnostic ignored "-Wpsabi" 121 #endif 122 123 // Helps tamp down on the repetitive boilerplate. 124 #define SIT template < typename T> static inline 125 #define SINT template <int N, typename T> static inline 126 #define SINTU template <int N, typename T, typename U, \ 127 typename=typename std::enable_if<std::is_convertible<U,T>::value>::type> \ 128 static inline 129 130 template <typename D, typename S> 131 static inline D bit_pun(const S& s) { 132 static_assert(sizeof(D) == sizeof(S), ""); 133 D d; 134 memcpy(&d, &s, sizeof(D)); 135 return d; 136 } 137 138 // Translate from a value type T to its corresponding Mask, the result of a comparison. 139 template <typename T> struct Mask { using type = T; }; 140 template <> struct Mask<float > { using type = int32_t; }; 141 template <> struct Mask<double> { using type = int64_t; }; 142 template <typename T> using M = typename Mask<T>::type; 143 144 // Join two Vec<N,T> into one Vec<2N,T>. 145 SINT Vec<2*N,T> join(const Vec<N,T>& lo, const Vec<N,T>& hi) { 146 Vec<2*N,T> v; 147 v.lo = lo; 148 v.hi = hi; 149 return v; 150 } 151 152 // We have two default strategies for implementing most operations: 153 // 1) lean on Clang/GCC vector extensions when available; 154 // 2) recurse to scalar portable implementations when not. 155 // At the end we can drop in platform-specific implementations that override either default. 156 157 #if !defined(SKNX_NO_SIMD) && (defined(__clang__) || defined(__GNUC__)) 158 159 // VExt<N,T> types have the same size as Vec<N,T> and support most operations directly. 160 // N.B. VExt<N,T> alignment is N*alignof(T), stricter than Vec<N,T>'s alignof(T). 161 #if defined(__clang__) 162 template <int N, typename T> 163 using VExt = T __attribute__((ext_vector_type(N))); 164 165 #elif defined(__GNUC__) 166 template <int N, typename T> 167 struct VExtHelper { 168 typedef T __attribute__((vector_size(N*sizeof(T)))) type; 169 }; 170 171 template <int N, typename T> 172 using VExt = typename VExtHelper<N,T>::type; 173 174 // For some reason some (new!) versions of GCC cannot seem to deduce N in the generic 175 // to_vec<N,T>() below for N=4 and T=float. This workaround seems to help... 176 static inline Vec<4,float> to_vec(VExt<4,float> v) { return bit_pun<Vec<4,float>>(v); } 177 #endif 178 179 SINT VExt<N,T> to_vext(const Vec<N,T>& v) { return bit_pun<VExt<N,T>>(v); } 180 SINT Vec <N,T> to_vec(const VExt<N,T>& v) { return bit_pun<Vec <N,T>>(v); } 181 182 SINT Vec<N,T> operator+(const Vec<N,T>& x, const Vec<N,T>& y) { return to_vec<N,T>(to_vext(x) + to_vext(y)); } 183 SINT Vec<N,T> operator-(const Vec<N,T>& x, const Vec<N,T>& y) { return to_vec<N,T>(to_vext(x) - to_vext(y)); } 184 SINT Vec<N,T> operator*(const Vec<N,T>& x, const Vec<N,T>& y) { return to_vec<N,T>(to_vext(x) * to_vext(y)); } 185 SINT Vec<N,T> operator/(const Vec<N,T>& x, const Vec<N,T>& y) { return to_vec<N,T>(to_vext(x) / to_vext(y)); } 186 187 SINT Vec<N,T> operator^(const Vec<N,T>& x, const Vec<N,T>& y) { return to_vec<N,T>(to_vext(x) ^ to_vext(y)); } 188 SINT Vec<N,T> operator&(const Vec<N,T>& x, const Vec<N,T>& y) { return to_vec<N,T>(to_vext(x) & to_vext(y)); } 189 SINT Vec<N,T> operator|(const Vec<N,T>& x, const Vec<N,T>& y) { return to_vec<N,T>(to_vext(x) | to_vext(y)); } 190 191 SINT Vec<N,T> operator!(const Vec<N,T>& x) { return to_vec<N,T>(!to_vext(x)); } 192 SINT Vec<N,T> operator-(const Vec<N,T>& x) { return to_vec<N,T>(-to_vext(x)); } 193 SINT Vec<N,T> operator~(const Vec<N,T>& x) { return to_vec<N,T>(~to_vext(x)); } 194 195 SINT Vec<N,T> operator<<(const Vec<N,T>& x, int bits) { return to_vec<N,T>(to_vext(x) << bits); } 196 SINT Vec<N,T> operator>>(const Vec<N,T>& x, int bits) { return to_vec<N,T>(to_vext(x) >> bits); } 197 198 SINT Vec<N,M<T>> operator==(const Vec<N,T>& x, const Vec<N,T>& y) { return bit_pun<Vec<N,M<T>>>(to_vext(x) == to_vext(y)); } 199 SINT Vec<N,M<T>> operator!=(const Vec<N,T>& x, const Vec<N,T>& y) { return bit_pun<Vec<N,M<T>>>(to_vext(x) != to_vext(y)); } 200 SINT Vec<N,M<T>> operator<=(const Vec<N,T>& x, const Vec<N,T>& y) { return bit_pun<Vec<N,M<T>>>(to_vext(x) <= to_vext(y)); } 201 SINT Vec<N,M<T>> operator>=(const Vec<N,T>& x, const Vec<N,T>& y) { return bit_pun<Vec<N,M<T>>>(to_vext(x) >= to_vext(y)); } 202 SINT Vec<N,M<T>> operator< (const Vec<N,T>& x, const Vec<N,T>& y) { return bit_pun<Vec<N,M<T>>>(to_vext(x) < to_vext(y)); } 203 SINT Vec<N,M<T>> operator> (const Vec<N,T>& x, const Vec<N,T>& y) { return bit_pun<Vec<N,M<T>>>(to_vext(x) > to_vext(y)); } 204 205 #else 206 207 // Either SKNX_NO_SIMD is defined, or Clang/GCC vector extensions are not available. 208 // We'll implement things portably, in a way that should be easily autovectorizable. 209 210 // N == 1 scalar implementations. 211 SIT Vec<1,T> operator+(const Vec<1,T>& x, const Vec<1,T>& y) { return x.val + y.val; } 212 SIT Vec<1,T> operator-(const Vec<1,T>& x, const Vec<1,T>& y) { return x.val - y.val; } 213 SIT Vec<1,T> operator*(const Vec<1,T>& x, const Vec<1,T>& y) { return x.val * y.val; } 214 SIT Vec<1,T> operator/(const Vec<1,T>& x, const Vec<1,T>& y) { return x.val / y.val; } 215 216 SIT Vec<1,T> operator^(const Vec<1,T>& x, const Vec<1,T>& y) { return x.val ^ y.val; } 217 SIT Vec<1,T> operator&(const Vec<1,T>& x, const Vec<1,T>& y) { return x.val & y.val; } 218 SIT Vec<1,T> operator|(const Vec<1,T>& x, const Vec<1,T>& y) { return x.val | y.val; } 219 220 SIT Vec<1,T> operator!(const Vec<1,T>& x) { return !x.val; } 221 SIT Vec<1,T> operator-(const Vec<1,T>& x) { return -x.val; } 222 SIT Vec<1,T> operator~(const Vec<1,T>& x) { return ~x.val; } 223 224 SIT Vec<1,T> operator<<(const Vec<1,T>& x, int bits) { return x.val << bits; } 225 SIT Vec<1,T> operator>>(const Vec<1,T>& x, int bits) { return x.val >> bits; } 226 227 SIT Vec<1,M<T>> operator==(const Vec<1,T>& x, const Vec<1,T>& y) { return x.val == y.val ? ~0 : 0; } 228 SIT Vec<1,M<T>> operator!=(const Vec<1,T>& x, const Vec<1,T>& y) { return x.val != y.val ? ~0 : 0; } 229 SIT Vec<1,M<T>> operator<=(const Vec<1,T>& x, const Vec<1,T>& y) { return x.val <= y.val ? ~0 : 0; } 230 SIT Vec<1,M<T>> operator>=(const Vec<1,T>& x, const Vec<1,T>& y) { return x.val >= y.val ? ~0 : 0; } 231 SIT Vec<1,M<T>> operator< (const Vec<1,T>& x, const Vec<1,T>& y) { return x.val < y.val ? ~0 : 0; } 232 SIT Vec<1,M<T>> operator> (const Vec<1,T>& x, const Vec<1,T>& y) { return x.val > y.val ? ~0 : 0; } 233 234 // All default N != 1 implementations just recurse on lo and hi halves. 235 SINT Vec<N,T> operator+(const Vec<N,T>& x, const Vec<N,T>& y) { return join(x.lo + y.lo, x.hi + y.hi); } 236 SINT Vec<N,T> operator-(const Vec<N,T>& x, const Vec<N,T>& y) { return join(x.lo - y.lo, x.hi - y.hi); } 237 SINT Vec<N,T> operator*(const Vec<N,T>& x, const Vec<N,T>& y) { return join(x.lo * y.lo, x.hi * y.hi); } 238 SINT Vec<N,T> operator/(const Vec<N,T>& x, const Vec<N,T>& y) { return join(x.lo / y.lo, x.hi / y.hi); } 239 240 SINT Vec<N,T> operator^(const Vec<N,T>& x, const Vec<N,T>& y) { return join(x.lo ^ y.lo, x.hi ^ y.hi); } 241 SINT Vec<N,T> operator&(const Vec<N,T>& x, const Vec<N,T>& y) { return join(x.lo & y.lo, x.hi & y.hi); } 242 SINT Vec<N,T> operator|(const Vec<N,T>& x, const Vec<N,T>& y) { return join(x.lo | y.lo, x.hi | y.hi); } 243 244 SINT Vec<N,T> operator!(const Vec<N,T>& x) { return join(!x.lo, !x.hi); } 245 SINT Vec<N,T> operator-(const Vec<N,T>& x) { return join(-x.lo, -x.hi); } 246 SINT Vec<N,T> operator~(const Vec<N,T>& x) { return join(~x.lo, ~x.hi); } 247 248 SINT Vec<N,T> operator<<(const Vec<N,T>& x, int bits) { return join(x.lo << bits, x.hi << bits); } 249 SINT Vec<N,T> operator>>(const Vec<N,T>& x, int bits) { return join(x.lo >> bits, x.hi >> bits); } 250 251 SINT Vec<N,M<T>> operator==(const Vec<N,T>& x, const Vec<N,T>& y) { return join(x.lo == y.lo, x.hi == y.hi); } 252 SINT Vec<N,M<T>> operator!=(const Vec<N,T>& x, const Vec<N,T>& y) { return join(x.lo != y.lo, x.hi != y.hi); } 253 SINT Vec<N,M<T>> operator<=(const Vec<N,T>& x, const Vec<N,T>& y) { return join(x.lo <= y.lo, x.hi <= y.hi); } 254 SINT Vec<N,M<T>> operator>=(const Vec<N,T>& x, const Vec<N,T>& y) { return join(x.lo >= y.lo, x.hi >= y.hi); } 255 SINT Vec<N,M<T>> operator< (const Vec<N,T>& x, const Vec<N,T>& y) { return join(x.lo < y.lo, x.hi < y.hi); } 256 SINT Vec<N,M<T>> operator> (const Vec<N,T>& x, const Vec<N,T>& y) { return join(x.lo > y.lo, x.hi > y.hi); } 257 #endif 258 259 // Some operations we want are not expressible with Clang/GCC vector 260 // extensions, so we implement them using the recursive approach. 261 262 // N == 1 scalar implementations. 263 SIT Vec<1,T> if_then_else(const Vec<1,M<T>>& cond, const Vec<1,T>& t, const Vec<1,T>& e) { 264 auto t_bits = bit_pun<M<T>>(t), 265 e_bits = bit_pun<M<T>>(e); 266 return bit_pun<T>( (cond.val & t_bits) | (~cond.val & e_bits) ); 267 } 268 269 SIT bool any(const Vec<1,T>& x) { return x.val != 0; } 270 SIT bool all(const Vec<1,T>& x) { return x.val != 0; } 271 272 SIT T min(const Vec<1,T>& x) { return x.val; } 273 SIT T max(const Vec<1,T>& x) { return x.val; } 274 275 SIT Vec<1,T> min(const Vec<1,T>& x, const Vec<1,T>& y) { return std::min(x.val, y.val); } 276 SIT Vec<1,T> max(const Vec<1,T>& x, const Vec<1,T>& y) { return std::max(x.val, y.val); } 277 278 SIT Vec<1,T> ceil(const Vec<1,T>& x) { return std:: ceil(x.val); } 279 SIT Vec<1,T> floor(const Vec<1,T>& x) { return std::floor(x.val); } 280 SIT Vec<1,T> trunc(const Vec<1,T>& x) { return std::trunc(x.val); } 281 SIT Vec<1,T> round(const Vec<1,T>& x) { return std::round(x.val); } 282 SIT Vec<1,T> sqrt(const Vec<1,T>& x) { return std:: sqrt(x.val); } 283 SIT Vec<1,T> abs(const Vec<1,T>& x) { return std:: abs(x.val); } 284 285 SIT Vec<1,T> rcp(const Vec<1,T>& x) { return 1 / x.val; } 286 SIT Vec<1,T> rsqrt(const Vec<1,T>& x) { return rcp(sqrt(x)); } 287 SIT Vec<1,T> mad(const Vec<1,T>& f, 288 const Vec<1,T>& m, 289 const Vec<1,T>& a) { return f*m+a; } 290 291 // All default N != 1 implementations just recurse on lo and hi halves. 292 SINT Vec<N,T> if_then_else(const Vec<N,M<T>>& cond, const Vec<N,T>& t, const Vec<N,T>& e) { 293 return join(if_then_else(cond.lo, t.lo, e.lo), 294 if_then_else(cond.hi, t.hi, e.hi)); 295 } 296 297 SINT bool any(const Vec<N,T>& x) { return any(x.lo) || any(x.hi); } 298 SINT bool all(const Vec<N,T>& x) { return all(x.lo) && all(x.hi); } 299 300 SINT T min(const Vec<N,T>& x) { return std::min(min(x.lo), min(x.hi)); } 301 SINT T max(const Vec<N,T>& x) { return std::max(max(x.lo), max(x.hi)); } 302 303 SINT Vec<N,T> min(const Vec<N,T>& x, const Vec<N,T>& y) { return join(min(x.lo, y.lo), min(x.hi, y.hi)); } 304 SINT Vec<N,T> max(const Vec<N,T>& x, const Vec<N,T>& y) { return join(max(x.lo, y.lo), max(x.hi, y.hi)); } 305 306 SINT Vec<N,T> ceil(const Vec<N,T>& x) { return join( ceil(x.lo), ceil(x.hi)); } 307 SINT Vec<N,T> floor(const Vec<N,T>& x) { return join(floor(x.lo), floor(x.hi)); } 308 SINT Vec<N,T> trunc(const Vec<N,T>& x) { return join(trunc(x.lo), trunc(x.hi)); } 309 SINT Vec<N,T> round(const Vec<N,T>& x) { return join(round(x.lo), round(x.hi)); } 310 SINT Vec<N,T> sqrt(const Vec<N,T>& x) { return join( sqrt(x.lo), sqrt(x.hi)); } 311 SINT Vec<N,T> abs(const Vec<N,T>& x) { return join( abs(x.lo), abs(x.hi)); } 312 313 SINT Vec<N,T> rcp(const Vec<N,T>& x) { return join( rcp(x.lo), rcp(x.hi)); } 314 SINT Vec<N,T> rsqrt(const Vec<N,T>& x) { return join(rsqrt(x.lo), rsqrt(x.hi)); } 315 SINT Vec<N,T> mad(const Vec<N,T>& f, 316 const Vec<N,T>& m, 317 const Vec<N,T>& a) { return join(mad(f.lo, m.lo, a.lo), mad(f.hi, m.hi, a.hi)); } 318 319 320 // Scalar/vector operations just splat the scalar to a vector... 321 SINTU Vec<N,T> operator+ (U x, const Vec<N,T>& y) { return Vec<N,T>(x) + y; } 322 SINTU Vec<N,T> operator- (U x, const Vec<N,T>& y) { return Vec<N,T>(x) - y; } 323 SINTU Vec<N,T> operator* (U x, const Vec<N,T>& y) { return Vec<N,T>(x) * y; } 324 SINTU Vec<N,T> operator/ (U x, const Vec<N,T>& y) { return Vec<N,T>(x) / y; } 325 SINTU Vec<N,T> operator^ (U x, const Vec<N,T>& y) { return Vec<N,T>(x) ^ y; } 326 SINTU Vec<N,T> operator& (U x, const Vec<N,T>& y) { return Vec<N,T>(x) & y; } 327 SINTU Vec<N,T> operator| (U x, const Vec<N,T>& y) { return Vec<N,T>(x) | y; } 328 SINTU Vec<N,M<T>> operator==(U x, const Vec<N,T>& y) { return Vec<N,T>(x) == y; } 329 SINTU Vec<N,M<T>> operator!=(U x, const Vec<N,T>& y) { return Vec<N,T>(x) != y; } 330 SINTU Vec<N,M<T>> operator<=(U x, const Vec<N,T>& y) { return Vec<N,T>(x) <= y; } 331 SINTU Vec<N,M<T>> operator>=(U x, const Vec<N,T>& y) { return Vec<N,T>(x) >= y; } 332 SINTU Vec<N,M<T>> operator< (U x, const Vec<N,T>& y) { return Vec<N,T>(x) < y; } 333 SINTU Vec<N,M<T>> operator> (U x, const Vec<N,T>& y) { return Vec<N,T>(x) > y; } 334 SINTU Vec<N,T> min(U x, const Vec<N,T>& y) { return min(Vec<N,T>(x), y); } 335 SINTU Vec<N,T> max(U x, const Vec<N,T>& y) { return max(Vec<N,T>(x), y); } 336 337 // ... and same deal for vector/scalar operations. 338 SINTU Vec<N,T> operator+ (const Vec<N,T>& x, U y) { return x + Vec<N,T>(y); } 339 SINTU Vec<N,T> operator- (const Vec<N,T>& x, U y) { return x - Vec<N,T>(y); } 340 SINTU Vec<N,T> operator* (const Vec<N,T>& x, U y) { return x * Vec<N,T>(y); } 341 SINTU Vec<N,T> operator/ (const Vec<N,T>& x, U y) { return x / Vec<N,T>(y); } 342 SINTU Vec<N,T> operator^ (const Vec<N,T>& x, U y) { return x ^ Vec<N,T>(y); } 343 SINTU Vec<N,T> operator& (const Vec<N,T>& x, U y) { return x & Vec<N,T>(y); } 344 SINTU Vec<N,T> operator| (const Vec<N,T>& x, U y) { return x | Vec<N,T>(y); } 345 SINTU Vec<N,M<T>> operator==(const Vec<N,T>& x, U y) { return x == Vec<N,T>(y); } 346 SINTU Vec<N,M<T>> operator!=(const Vec<N,T>& x, U y) { return x != Vec<N,T>(y); } 347 SINTU Vec<N,M<T>> operator<=(const Vec<N,T>& x, U y) { return x <= Vec<N,T>(y); } 348 SINTU Vec<N,M<T>> operator>=(const Vec<N,T>& x, U y) { return x >= Vec<N,T>(y); } 349 SINTU Vec<N,M<T>> operator< (const Vec<N,T>& x, U y) { return x < Vec<N,T>(y); } 350 SINTU Vec<N,M<T>> operator> (const Vec<N,T>& x, U y) { return x > Vec<N,T>(y); } 351 SINTU Vec<N,T> min(const Vec<N,T>& x, U y) { return min(x, Vec<N,T>(y)); } 352 SINTU Vec<N,T> max(const Vec<N,T>& x, U y) { return max(x, Vec<N,T>(y)); } 353 354 // All vector/scalar combinations for mad() with at least one vector. 355 SINTU Vec<N,T> mad(U f, const Vec<N,T>& m, const Vec<N,T>& a) { return Vec<N,T>(f)*m + a; } 356 SINTU Vec<N,T> mad(const Vec<N,T>& f, U m, const Vec<N,T>& a) { return f*Vec<N,T>(m) + a; } 357 SINTU Vec<N,T> mad(const Vec<N,T>& f, const Vec<N,T>& m, U a) { return f*m + Vec<N,T>(a); } 358 SINTU Vec<N,T> mad(const Vec<N,T>& f, U m, U a) { return f*Vec<N,T>(m) + Vec<N,T>(a); } 359 SINTU Vec<N,T> mad(U f, const Vec<N,T>& m, U a) { return Vec<N,T>(f)*m + Vec<N,T>(a); } 360 SINTU Vec<N,T> mad(U f, U m, const Vec<N,T>& a) { return Vec<N,T>(f)*Vec<N,T>(m) + a; } 361 362 // The various op= operators, for vectors... 363 SINT Vec<N,T>& operator+=(Vec<N,T>& x, const Vec<N,T>& y) { return (x = x + y); } 364 SINT Vec<N,T>& operator-=(Vec<N,T>& x, const Vec<N,T>& y) { return (x = x - y); } 365 SINT Vec<N,T>& operator*=(Vec<N,T>& x, const Vec<N,T>& y) { return (x = x * y); } 366 SINT Vec<N,T>& operator/=(Vec<N,T>& x, const Vec<N,T>& y) { return (x = x / y); } 367 SINT Vec<N,T>& operator^=(Vec<N,T>& x, const Vec<N,T>& y) { return (x = x ^ y); } 368 SINT Vec<N,T>& operator&=(Vec<N,T>& x, const Vec<N,T>& y) { return (x = x & y); } 369 SINT Vec<N,T>& operator|=(Vec<N,T>& x, const Vec<N,T>& y) { return (x = x | y); } 370 371 // ... for scalars... 372 SINTU Vec<N,T>& operator+=(Vec<N,T>& x, U y) { return (x = x + Vec<N,T>(y)); } 373 SINTU Vec<N,T>& operator-=(Vec<N,T>& x, U y) { return (x = x - Vec<N,T>(y)); } 374 SINTU Vec<N,T>& operator*=(Vec<N,T>& x, U y) { return (x = x * Vec<N,T>(y)); } 375 SINTU Vec<N,T>& operator/=(Vec<N,T>& x, U y) { return (x = x / Vec<N,T>(y)); } 376 SINTU Vec<N,T>& operator^=(Vec<N,T>& x, U y) { return (x = x ^ Vec<N,T>(y)); } 377 SINTU Vec<N,T>& operator&=(Vec<N,T>& x, U y) { return (x = x & Vec<N,T>(y)); } 378 SINTU Vec<N,T>& operator|=(Vec<N,T>& x, U y) { return (x = x | Vec<N,T>(y)); } 379 380 // ... and for shifts. 381 SINT Vec<N,T>& operator<<=(Vec<N,T>& x, int bits) { return (x = x << bits); } 382 SINT Vec<N,T>& operator>>=(Vec<N,T>& x, int bits) { return (x = x >> bits); } 383 384 // cast() Vec<N,S> to Vec<N,D>, as if applying a C-cast to each lane. 385 template <typename D, typename S> 386 static inline Vec<1,D> cast(const Vec<1,S>& src) { return (D)src.val; } 387 388 template <typename D, int N, typename S> 389 static inline Vec<N,D> cast(const Vec<N,S>& src) { 390 #if !defined(SKNX_NO_SIMD) && defined(__clang__) 391 return to_vec(__builtin_convertvector(to_vext(src), VExt<N,D>)); 392 #else 393 return join(cast<D>(src.lo), cast<D>(src.hi)); 394 #endif 395 } 396 397 // Shuffle values from a vector pretty arbitrarily: 398 // skvx::Vec<4,float> rgba = {R,G,B,A}; 399 // shuffle<2,1,0,3> (rgba) ~> {B,G,R,A} 400 // shuffle<2,1> (rgba) ~> {B,G} 401 // shuffle<2,1,2,1,2,1,2,1>(rgba) ~> {B,G,B,G,B,G,B,G} 402 // shuffle<3,3,3,3> (rgba) ~> {A,A,A,A} 403 // The only real restriction is that the output also be a legal N=power-of-two sknx::Vec. 404 template <int... Ix, int N, typename T> 405 static inline Vec<sizeof...(Ix),T> shuffle(const Vec<N,T>& x) { 406 #if !defined(SKNX_NO_SIMD) && defined(__clang__) 407 return to_vec<sizeof...(Ix),T>(__builtin_shufflevector(to_vext(x), to_vext(x), Ix...)); 408 #else 409 return { x[Ix]... }; 410 #endif 411 } 412 413 // div255(x) = (x + 127) / 255 is a bit-exact rounding divide-by-255, packing down to 8-bit. 414 template <int N> 415 static inline Vec<N,uint8_t> div255(const Vec<N,uint16_t>& x) { 416 return cast<uint8_t>( (x+127)/255 ); 417 } 418 419 // approx_scale(x,y) approximates div255(cast<uint16_t>(x)*cast<uint16_t>(y)) within a bit, 420 // and is always perfect when x or y is 0 or 255. 421 template <int N> 422 static inline Vec<N,uint8_t> approx_scale(const Vec<N,uint8_t>& x, const Vec<N,uint8_t>& y) { 423 // All of (x*y+x)/256, (x*y+y)/256, and (x*y+255)/256 meet the criteria above. 424 // We happen to have historically picked (x*y+x)/256. 425 auto X = cast<uint16_t>(x), 426 Y = cast<uint16_t>(y); 427 return cast<uint8_t>( (X*Y+X)/256 ); 428 } 429 430 #if !defined(SKNX_NO_SIMD) && defined(__ARM_NEON) 431 // With NEON we can do eight u8*u8 -> u16 in one instruction, vmull_u8 (read, mul-long). 432 static inline Vec<8,uint16_t> mull(const Vec<8,uint8_t>& x, 433 const Vec<8,uint8_t>& y) { 434 return to_vec<8,uint16_t>(vmull_u8(to_vext(x), 435 to_vext(y))); 436 } 437 438 template <int N> 439 static inline typename std::enable_if<(N < 8), 440 Vec<N,uint16_t>>::type mull(const Vec<N,uint8_t>& x, 441 const Vec<N,uint8_t>& y) { 442 // N < 8 --> double up data until N == 8, returning the part we need. 443 return mull(join(x,x), 444 join(y,y)).lo; 445 } 446 447 template <int N> 448 static inline typename std::enable_if<(N > 8), 449 Vec<N,uint16_t>>::type mull(const Vec<N,uint8_t>& x, 450 const Vec<N,uint8_t>& y) { 451 // N > 8 --> usual join(lo,hi) strategy to recurse down to N == 8. 452 return join(mull(x.lo, y.lo), 453 mull(x.hi, y.hi)); 454 } 455 #else 456 // Nothing special when we don't have NEON... just cast up to 16-bit and multiply. 457 template <int N> 458 static inline Vec<N,uint16_t> mull(const Vec<N,uint8_t>& x, 459 const Vec<N,uint8_t>& y) { 460 return cast<uint16_t>(x) 461 * cast<uint16_t>(y); 462 } 463 #endif 464 465 #if !defined(SKNX_NO_SIMD) 466 467 // Platform-specific specializations and overloads can now drop in here. 468 469 #if defined(__SSE__) 470 static inline Vec<4,float> sqrt(const Vec<4,float>& x) { 471 return bit_pun<Vec<4,float>>(_mm_sqrt_ps(bit_pun<__m128>(x))); 472 } 473 static inline Vec<4,float> rsqrt(const Vec<4,float>& x) { 474 return bit_pun<Vec<4,float>>(_mm_rsqrt_ps(bit_pun<__m128>(x))); 475 } 476 static inline Vec<4,float> rcp(const Vec<4,float>& x) { 477 return bit_pun<Vec<4,float>>(_mm_rcp_ps(bit_pun<__m128>(x))); 478 } 479 480 static inline Vec<2,float> sqrt(const Vec<2,float>& x) { 481 return shuffle<0,1>( sqrt(shuffle<0,1,0,1>(x))); 482 } 483 static inline Vec<2,float> rsqrt(const Vec<2,float>& x) { 484 return shuffle<0,1>(rsqrt(shuffle<0,1,0,1>(x))); 485 } 486 static inline Vec<2,float> rcp(const Vec<2,float>& x) { 487 return shuffle<0,1>( rcp(shuffle<0,1,0,1>(x))); 488 } 489 #endif 490 491 #if defined(__SSE4_1__) 492 static inline Vec<4,float> if_then_else(const Vec<4,int >& c, 493 const Vec<4,float>& t, 494 const Vec<4,float>& e) { 495 return bit_pun<Vec<4,float>>(_mm_blendv_ps(bit_pun<__m128>(e), 496 bit_pun<__m128>(t), 497 bit_pun<__m128>(c))); 498 } 499 #elif defined(__SSE__) 500 static inline Vec<4,float> if_then_else(const Vec<4,int >& c, 501 const Vec<4,float>& t, 502 const Vec<4,float>& e) { 503 return bit_pun<Vec<4,float>>(_mm_or_ps(_mm_and_ps (bit_pun<__m128>(c), 504 bit_pun<__m128>(t)), 505 _mm_andnot_ps(bit_pun<__m128>(c), 506 bit_pun<__m128>(e)))); 507 } 508 #elif defined(__ARM_NEON) 509 static inline Vec<4,float> if_then_else(const Vec<4,int >& c, 510 const Vec<4,float>& t, 511 const Vec<4,float>& e) { 512 return bit_pun<Vec<4,float>>(vbslq_f32(bit_pun<uint32x4_t> (c), 513 bit_pun<float32x4_t>(t), 514 bit_pun<float32x4_t>(e))); 515 } 516 #endif 517 518 #endif // !defined(SKNX_NO_SIMD) 519 520 } // namespace skvx 521 522 #undef SINTU 523 #undef SINT 524 #undef SIT 525 #undef SKVX_ALIGNMENT 526 527 #endif//SKVX_DEFINED 528