mtklein | 3e490b7 | 2015-03-20 06:33:02 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 SkNx_DEFINED |
| 9 | #define SkNx_DEFINED |
| 10 | |
mtklein | 6f37b4a | 2015-12-14 11:25:18 -0800 | [diff] [blame] | 11 | //#define SKNX_NO_SIMD |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 12 | |
| 13 | #include "SkScalar.h" |
| 14 | #include "SkTypes.h" |
| 15 | #include <math.h> |
mtklein | 082e329 | 2015-08-12 11:56:43 -0700 | [diff] [blame] | 16 | |
mtklein | 8fe8fff | 2015-04-14 11:49:14 -0700 | [diff] [blame] | 17 | // The default implementations just fall back on a pair of size N/2. |
mtklein | 6f37b4a | 2015-12-14 11:25:18 -0800 | [diff] [blame] | 18 | // These support the union of operations we might do to ints and floats, but |
| 19 | // platform specializations might support fewer (e.g. no float <<, no int /). |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 20 | template <int N, typename T> |
mtklein | 6c221b4 | 2015-11-20 13:53:19 -0800 | [diff] [blame] | 21 | class SkNx { |
mtklein | 115acee | 2015-04-14 14:02:52 -0700 | [diff] [blame] | 22 | public: |
mtklein | 6c221b4 | 2015-11-20 13:53:19 -0800 | [diff] [blame] | 23 | SkNx() {} |
mtklein | 6c221b4 | 2015-11-20 13:53:19 -0800 | [diff] [blame] | 24 | SkNx(T val) : fLo(val), fHi(val) {} |
mtklein | e4c0bee | 2016-02-09 10:35:27 -0800 | [diff] [blame] | 25 | |
| 26 | typedef SkNx<N/2, T> Half; |
| 27 | SkNx(const Half& lo, const Half& hi) : fLo(lo), fHi(hi) {} |
| 28 | |
| 29 | SkNx(T a, T b) : fLo(a), fHi(b) {} |
| 30 | SkNx(T a, T b, T c, T d) : fLo(a,b), fHi(c,d) {} |
| 31 | SkNx(T a, T b, T c, T d, T e, T f, T g, T h) : fLo(a,b,c,d), fHi(e,f,g,h) {} |
| 32 | SkNx(T a, T b, T c, T d, T e, T f, T g, T h, |
| 33 | T i, T j, T k, T l, T m, T n, T o, T p) : fLo(a,b,c,d, e,f,g,h), fHi(i,j,k,l, m,n,o,p) {} |
| 34 | |
mtklein | 507ef6d | 2016-01-31 08:02:47 -0800 | [diff] [blame] | 35 | static SkNx Load(const void* ptr) { |
| 36 | auto vals = (const T*)ptr; |
mtklein | e4c0bee | 2016-02-09 10:35:27 -0800 | [diff] [blame] | 37 | return SkNx(Half::Load(vals), Half::Load(vals+N/2)); |
mtklein | 115acee | 2015-04-14 14:02:52 -0700 | [diff] [blame] | 38 | } |
| 39 | |
mtklein | 507ef6d | 2016-01-31 08:02:47 -0800 | [diff] [blame] | 40 | void store(void* ptr) const { |
| 41 | auto vals = (T*)ptr; |
mtklein | 115acee | 2015-04-14 14:02:52 -0700 | [diff] [blame] | 42 | fLo.store(vals); |
| 43 | fHi.store(vals+N/2); |
| 44 | } |
| 45 | |
mtklein | e4c0bee | 2016-02-09 10:35:27 -0800 | [diff] [blame] | 46 | #define OP(op) SkNx operator op(const SkNx& o) const { return {fLo op o.fLo, fHi op o.fHi}; } |
| 47 | OP(+) OP(-) OP(*) OP(/) |
| 48 | OP(&) OP(|) OP(^) |
| 49 | OP(==) OP(!=) OP(<) OP(>) OP(<=) OP(>=) |
| 50 | #undef OP |
mtklein | 6cbf18c | 2015-05-12 15:48:09 -0700 | [diff] [blame] | 51 | |
mtklein | e4c0bee | 2016-02-09 10:35:27 -0800 | [diff] [blame] | 52 | #define OP(op) SkNx op() const { return {fLo.op(), fHi.op()}; } |
mtklein | 126626e | 2016-02-09 15:41:36 -0800 | [diff] [blame] | 53 | OP(abs) OP(floor) |
mtklein | e4c0bee | 2016-02-09 10:35:27 -0800 | [diff] [blame] | 54 | OP(sqrt) OP(rsqrt0) OP(rsqrt1) OP(rsqrt2) |
| 55 | OP(invert) OP(approxInvert) |
| 56 | #undef OP |
mtklein | 115acee | 2015-04-14 14:02:52 -0700 | [diff] [blame] | 57 | |
mtklein | 6c221b4 | 2015-11-20 13:53:19 -0800 | [diff] [blame] | 58 | SkNx operator << (int bits) const { return SkNx(fLo << bits, fHi << bits); } |
| 59 | SkNx operator >> (int bits) const { return SkNx(fLo >> bits, fHi >> bits); } |
mtklein | 115acee | 2015-04-14 14:02:52 -0700 | [diff] [blame] | 60 | |
mtklein | e4c0bee | 2016-02-09 10:35:27 -0800 | [diff] [blame] | 61 | SkNx saturatedAdd(const SkNx& o) const { |
| 62 | return {fLo.saturatedAdd(o.fLo), fHi.saturatedAdd(o.fHi)}; |
| 63 | } |
mtklein | 6f37b4a | 2015-12-14 11:25:18 -0800 | [diff] [blame] | 64 | |
mtklein | 6c221b4 | 2015-11-20 13:53:19 -0800 | [diff] [blame] | 65 | static SkNx Min(const SkNx& a, const SkNx& b) { |
mtklein | e4c0bee | 2016-02-09 10:35:27 -0800 | [diff] [blame] | 66 | return {Half::Min(a.fLo, b.fLo), Half::Min(a.fHi, b.fHi)}; |
mtklein | 27e517a | 2015-05-14 17:53:04 -0700 | [diff] [blame] | 67 | } |
mtklein | 6f37b4a | 2015-12-14 11:25:18 -0800 | [diff] [blame] | 68 | static SkNx Max(const SkNx& a, const SkNx& b) { |
mtklein | e4c0bee | 2016-02-09 10:35:27 -0800 | [diff] [blame] | 69 | return {Half::Max(a.fLo, b.fLo), Half::Max(a.fHi, b.fHi)}; |
mtklein | 6f37b4a | 2015-12-14 11:25:18 -0800 | [diff] [blame] | 70 | } |
| 71 | |
mtklein | e4c0bee | 2016-02-09 10:35:27 -0800 | [diff] [blame] | 72 | T operator[](int k) const { |
mtklein | 115acee | 2015-04-14 14:02:52 -0700 | [diff] [blame] | 73 | SkASSERT(0 <= k && k < N); |
mtklein | e4c0bee | 2016-02-09 10:35:27 -0800 | [diff] [blame] | 74 | return k < N/2 ? fLo[k] : fHi[k-N/2]; |
mtklein | 115acee | 2015-04-14 14:02:52 -0700 | [diff] [blame] | 75 | } |
| 76 | |
mtklein | b5e8611 | 2015-06-24 15:18:39 -0700 | [diff] [blame] | 77 | bool allTrue() const { return fLo.allTrue() && fHi.allTrue(); } |
| 78 | bool anyTrue() const { return fLo.anyTrue() || fHi.anyTrue(); } |
mtklein | 6c221b4 | 2015-11-20 13:53:19 -0800 | [diff] [blame] | 79 | SkNx thenElse(const SkNx& t, const SkNx& e) const { |
| 80 | return SkNx(fLo.thenElse(t.fLo, e.fLo), fHi.thenElse(t.fHi, e.fHi)); |
mtklein | b5e8611 | 2015-06-24 15:18:39 -0700 | [diff] [blame] | 81 | } |
| 82 | |
mtklein | d2ffd36 | 2015-05-12 06:11:21 -0700 | [diff] [blame] | 83 | protected: |
mtklein | e4c0bee | 2016-02-09 10:35:27 -0800 | [diff] [blame] | 84 | static_assert(0 == (N & (N-1)), "N must be a power of 2."); |
mtklein | 115acee | 2015-04-14 14:02:52 -0700 | [diff] [blame] | 85 | |
mtklein | e4c0bee | 2016-02-09 10:35:27 -0800 | [diff] [blame] | 86 | Half fLo, fHi; |
mtklein | 115acee | 2015-04-14 14:02:52 -0700 | [diff] [blame] | 87 | }; |
| 88 | |
mtklein | 8fe8fff | 2015-04-14 11:49:14 -0700 | [diff] [blame] | 89 | // Bottom out the default implementations with scalars when nothing's been specialized. |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 90 | template <typename T> |
mtklein | e4c0bee | 2016-02-09 10:35:27 -0800 | [diff] [blame] | 91 | class SkNx<1, T> { |
mtklein | 115acee | 2015-04-14 14:02:52 -0700 | [diff] [blame] | 92 | public: |
mtklein | 6c221b4 | 2015-11-20 13:53:19 -0800 | [diff] [blame] | 93 | SkNx() {} |
| 94 | SkNx(T val) : fVal(val) {} |
mtklein | e4c0bee | 2016-02-09 10:35:27 -0800 | [diff] [blame] | 95 | |
mtklein | 507ef6d | 2016-01-31 08:02:47 -0800 | [diff] [blame] | 96 | static SkNx Load(const void* ptr) { |
| 97 | auto vals = (const T*)ptr; |
| 98 | return SkNx(vals[0]); |
| 99 | } |
mtklein | 115acee | 2015-04-14 14:02:52 -0700 | [diff] [blame] | 100 | |
mtklein | 507ef6d | 2016-01-31 08:02:47 -0800 | [diff] [blame] | 101 | void store(void* ptr) const { |
| 102 | auto vals = (T*) ptr; |
| 103 | vals[0] = fVal; |
| 104 | } |
mtklein | 115acee | 2015-04-14 14:02:52 -0700 | [diff] [blame] | 105 | |
mtklein | e4c0bee | 2016-02-09 10:35:27 -0800 | [diff] [blame] | 106 | #define OP(op) SkNx operator op(const SkNx& o) const { return fVal op o.fVal; } |
| 107 | OP(+) OP(-) OP(*) OP(/) |
| 108 | OP(&) OP(|) OP(^) |
| 109 | OP(==) OP(!=) OP(<) OP(>) OP(<=) OP(>=) |
| 110 | #undef OP |
| 111 | |
| 112 | SkNx operator << (int bits) const { return fVal << bits; } |
| 113 | SkNx operator >> (int bits) const { return fVal >> bits; } |
| 114 | |
mtklein | 6c221b4 | 2015-11-20 13:53:19 -0800 | [diff] [blame] | 115 | SkNx saturatedAdd(const SkNx& o) const { |
mtklein | e4c0bee | 2016-02-09 10:35:27 -0800 | [diff] [blame] | 116 | SkASSERT((T)(~0) > 0); // TODO: support signed T? |
mtklein | 6cbf18c | 2015-05-12 15:48:09 -0700 | [diff] [blame] | 117 | T sum = fVal + o.fVal; |
mtklein | e4c0bee | 2016-02-09 10:35:27 -0800 | [diff] [blame] | 118 | return sum < fVal ? (T)(~0) : sum; |
mtklein | 6cbf18c | 2015-05-12 15:48:09 -0700 | [diff] [blame] | 119 | } |
| 120 | |
mtklein | e4c0bee | 2016-02-09 10:35:27 -0800 | [diff] [blame] | 121 | static SkNx Min(const SkNx& a, const SkNx& b) { return SkTMin(a.fVal, b.fVal); } |
| 122 | static SkNx Max(const SkNx& a, const SkNx& b) { return SkTMax(a.fVal, b.fVal); } |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 123 | |
mtklein | c33065a | 2016-01-15 12:16:40 -0800 | [diff] [blame] | 124 | SkNx abs() const { return SkTAbs(fVal); } |
mtklein | 126626e | 2016-02-09 15:41:36 -0800 | [diff] [blame] | 125 | SkNx floor() const { return Floor(fVal); } |
mtklein | c33065a | 2016-01-15 12:16:40 -0800 | [diff] [blame] | 126 | |
mtklein | e4c0bee | 2016-02-09 10:35:27 -0800 | [diff] [blame] | 127 | SkNx sqrt () const { return Sqrt(fVal); } |
| 128 | SkNx rsqrt0() const { return this->sqrt().invert(); } |
mtklein | 6c221b4 | 2015-11-20 13:53:19 -0800 | [diff] [blame] | 129 | SkNx rsqrt1() const { return this->rsqrt0(); } |
| 130 | SkNx rsqrt2() const { return this->rsqrt1(); } |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 131 | |
mtklein | e4c0bee | 2016-02-09 10:35:27 -0800 | [diff] [blame] | 132 | SkNx invert() const { return 1 / fVal; } |
| 133 | SkNx approxInvert() const { return this->invert(); } |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 134 | |
mtklein | e4c0bee | 2016-02-09 10:35:27 -0800 | [diff] [blame] | 135 | T operator[](int k) const { |
mtklein | 6f37b4a | 2015-12-14 11:25:18 -0800 | [diff] [blame] | 136 | SkASSERT(0 == k); |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 137 | return fVal; |
| 138 | } |
| 139 | |
mtklein | 6f37b4a | 2015-12-14 11:25:18 -0800 | [diff] [blame] | 140 | bool allTrue() const { return fVal != 0; } |
| 141 | bool anyTrue() const { return fVal != 0; } |
| 142 | SkNx thenElse(const SkNx& t, const SkNx& e) const { return fVal != 0 ? t : e; } |
mtklein | b5e8611 | 2015-06-24 15:18:39 -0700 | [diff] [blame] | 143 | |
mtklein | d2ffd36 | 2015-05-12 06:11:21 -0700 | [diff] [blame] | 144 | protected: |
mtklein | 126626e | 2016-02-09 15:41:36 -0800 | [diff] [blame] | 145 | static double Floor(double val) { return ::floor (val); } |
| 146 | static float Floor(float val) { return ::floorf(val); } |
mtklein | 6f37b4a | 2015-12-14 11:25:18 -0800 | [diff] [blame] | 147 | static double Sqrt(double val) { return ::sqrt (val); } |
| 148 | static float Sqrt(float val) { return ::sqrtf(val); } |
mtklein | b5e8611 | 2015-06-24 15:18:39 -0700 | [diff] [blame] | 149 | |
mtklein | 6f37b4a | 2015-12-14 11:25:18 -0800 | [diff] [blame] | 150 | T fVal; |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 151 | }; |
| 152 | |
mtklein | e4c0bee | 2016-02-09 10:35:27 -0800 | [diff] [blame] | 153 | // This generic shuffle can be called to create any valid SkNx<N,T>. |
mtklein | a1c0ee4 | 2015-09-10 14:16:07 -0700 | [diff] [blame] | 154 | // Sk4f f(a,b,c,d); |
mtklein | e4c0bee | 2016-02-09 10:35:27 -0800 | [diff] [blame] | 155 | // Sk2f t = SkNx_shuffle<2,1>(f); // ~~~> Sk2f(c,b) |
| 156 | // f = SkNx_shuffle<0,1,1,0>(t); // ~~~> Sk4f(c,b,b,c) |
| 157 | template <int... Ix, int N, typename T> |
| 158 | static inline SkNx<sizeof...(Ix), T> SkNx_shuffle(const SkNx<N,T>& src) { return { src[Ix]... }; } |
mtklein | 6c221b4 | 2015-11-20 13:53:19 -0800 | [diff] [blame] | 159 | |
| 160 | // This is a generic cast between two SkNx with the same number of elements N. E.g. |
mtklein | e4c0bee | 2016-02-09 10:35:27 -0800 | [diff] [blame] | 161 | // Sk4b bs = ...; // Load 4 bytes. |
| 162 | // Sk4f fs = SkNx_cast<float>(bs); // Cast each byte to a float. |
| 163 | // Sk4h hs = SkNx_cast<uint16_t>(fs); // Cast each float to uint16_t. |
| 164 | template <typename D, typename S> |
| 165 | static inline SkNx<2,D> SkNx_cast(const SkNx<2,S>& src) { |
| 166 | return { (D)src[0], (D)src[1] }; |
mtklein | 6c221b4 | 2015-11-20 13:53:19 -0800 | [diff] [blame] | 167 | } |
mtklein | a1c0ee4 | 2015-09-10 14:16:07 -0700 | [diff] [blame] | 168 | |
mtklein | e4c0bee | 2016-02-09 10:35:27 -0800 | [diff] [blame] | 169 | template <typename D, typename S> |
| 170 | static inline SkNx<4,D> SkNx_cast(const SkNx<4,S>& src) { |
| 171 | return { (D)src[0], (D)src[1], (D)src[2], (D)src[3] }; |
| 172 | } |
mtklein | 082e329 | 2015-08-12 11:56:43 -0700 | [diff] [blame] | 173 | |
mtklein | e4c0bee | 2016-02-09 10:35:27 -0800 | [diff] [blame] | 174 | template <typename D, typename S> |
| 175 | static inline SkNx<8,D> SkNx_cast(const SkNx<8,S>& src) { |
| 176 | return { (D)src[0], (D)src[1], (D)src[2], (D)src[3], |
| 177 | (D)src[4], (D)src[5], (D)src[6], (D)src[7] }; |
| 178 | } |
mtklein | fce612a | 2015-12-15 07:38:54 -0800 | [diff] [blame] | 179 | |
mtklein | e4c0bee | 2016-02-09 10:35:27 -0800 | [diff] [blame] | 180 | template <typename D, typename S> |
| 181 | static inline SkNx<16,D> SkNx_cast(const SkNx<16,S>& src) { |
| 182 | return { (D)src[ 0], (D)src[ 1], (D)src[ 2], (D)src[ 3], |
| 183 | (D)src[ 4], (D)src[ 5], (D)src[ 6], (D)src[ 7], |
| 184 | (D)src[ 8], (D)src[ 9], (D)src[10], (D)src[11], |
| 185 | (D)src[12], (D)src[13], (D)src[14], (D)src[15] }; |
| 186 | } |
mtklein | fce612a | 2015-12-15 07:38:54 -0800 | [diff] [blame] | 187 | |
mtklein | e4c0bee | 2016-02-09 10:35:27 -0800 | [diff] [blame] | 188 | typedef SkNx<2, float> Sk2f; |
| 189 | typedef SkNx<4, float> Sk4f; |
| 190 | typedef SkNx<2, SkScalar> Sk2s; |
| 191 | typedef SkNx<4, SkScalar> Sk4s; |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 192 | |
mtklein | e4c0bee | 2016-02-09 10:35:27 -0800 | [diff] [blame] | 193 | typedef SkNx<4, uint8_t> Sk4b; |
| 194 | typedef SkNx<16, uint8_t> Sk16b; |
| 195 | typedef SkNx<4, uint16_t> Sk4h; |
mtklein | 6c221b4 | 2015-11-20 13:53:19 -0800 | [diff] [blame] | 196 | typedef SkNx<16, uint16_t> Sk16h; |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 197 | typedef SkNx<4, int> Sk4i; |
mtklein | 6f37b4a | 2015-12-14 11:25:18 -0800 | [diff] [blame] | 198 | |
mtklein | ff9febf | 2016-02-16 14:33:08 -0800 | [diff] [blame] | 199 | typedef SkNx<4, int> Sk4i; |
| 200 | |
mtklein | 6f37b4a | 2015-12-14 11:25:18 -0800 | [diff] [blame] | 201 | // Include platform specific specializations if available. |
mtklein | 81bb79b | 2016-02-08 15:50:22 -0800 | [diff] [blame] | 202 | #if !defined(SKNX_NO_SIMD) && SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE2 |
mtklein | 6f37b4a | 2015-12-14 11:25:18 -0800 | [diff] [blame] | 203 | #include "../opts/SkNx_sse.h" |
| 204 | #elif !defined(SKNX_NO_SIMD) && defined(SK_ARM_HAS_NEON) |
| 205 | #include "../opts/SkNx_neon.h" |
| 206 | #else |
| 207 | static inline |
| 208 | void Sk4f_ToBytes(uint8_t p[16], const Sk4f& a, const Sk4f& b, const Sk4f& c, const Sk4f& d) { |
| 209 | SkNx_cast<uint8_t>(a).store(p+ 0); |
| 210 | SkNx_cast<uint8_t>(b).store(p+ 4); |
| 211 | SkNx_cast<uint8_t>(c).store(p+ 8); |
| 212 | SkNx_cast<uint8_t>(d).store(p+12); |
| 213 | } |
| 214 | #endif |
| 215 | |
mtklein | 3e490b7 | 2015-03-20 06:33:02 -0700 | [diff] [blame] | 216 | #endif//SkNx_DEFINED |