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
11 //#define SKNX_NO_SIMD
12
13 #include "SkScalar.h"
14 #include "SkTypes.h"
15 #include <math.h>
16
17 // The default implementations just fall back on a pair of size N/2.
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 /).
20 template <int N, typename T>
21 class SkNx {
22 public:
SkNx()23 SkNx() {}
SkNx(T val)24 SkNx(T val) : fLo(val), fHi(val) {}
25
26 typedef SkNx<N/2, T> Half;
SkNx(const Half & lo,const Half & hi)27 SkNx(const Half& lo, const Half& hi) : fLo(lo), fHi(hi) {}
28
SkNx(T a,T b)29 SkNx(T a, T b) : fLo(a), fHi(b) {}
SkNx(T a,T b,T c,T d)30 SkNx(T a, T b, T c, T d) : fLo(a,b), fHi(c,d) {}
SkNx(T a,T b,T c,T d,T e,T f,T g,T h)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) {}
SkNx(T a,T b,T c,T d,T e,T f,T g,T h,T i,T j,T k,T l,T m,T n,T o,T p)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
Load(const void * ptr)35 static SkNx Load(const void* ptr) {
36 auto vals = (const T*)ptr;
37 return SkNx(Half::Load(vals), Half::Load(vals+N/2));
38 }
39
store(void * ptr)40 void store(void* ptr) const {
41 auto vals = (T*)ptr;
42 fLo.store(vals);
43 fHi.store(vals+N/2);
44 }
45
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
51
52 #define OP(op) SkNx op() const { return {fLo.op(), fHi.op()}; }
53 OP(abs) OP(floor)
54 OP(sqrt) OP(rsqrt0) OP(rsqrt1) OP(rsqrt2)
55 OP(invert) OP(approxInvert)
56 #undef OP
57
58 SkNx operator << (int bits) const { return SkNx(fLo << bits, fHi << bits); }
59 SkNx operator >> (int bits) const { return SkNx(fLo >> bits, fHi >> bits); }
60
saturatedAdd(const SkNx & o)61 SkNx saturatedAdd(const SkNx& o) const {
62 return {fLo.saturatedAdd(o.fLo), fHi.saturatedAdd(o.fHi)};
63 }
64
Min(const SkNx & a,const SkNx & b)65 static SkNx Min(const SkNx& a, const SkNx& b) {
66 return {Half::Min(a.fLo, b.fLo), Half::Min(a.fHi, b.fHi)};
67 }
Max(const SkNx & a,const SkNx & b)68 static SkNx Max(const SkNx& a, const SkNx& b) {
69 return {Half::Max(a.fLo, b.fLo), Half::Max(a.fHi, b.fHi)};
70 }
71
72 T operator[](int k) const {
73 SkASSERT(0 <= k && k < N);
74 return k < N/2 ? fLo[k] : fHi[k-N/2];
75 }
76
allTrue()77 bool allTrue() const { return fLo.allTrue() && fHi.allTrue(); }
anyTrue()78 bool anyTrue() const { return fLo.anyTrue() || fHi.anyTrue(); }
thenElse(const SkNx & t,const SkNx & e)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));
81 }
82
83 protected:
84 static_assert(0 == (N & (N-1)), "N must be a power of 2.");
85
86 Half fLo, fHi;
87 };
88
89 // Bottom out the default implementations with scalars when nothing's been specialized.
90 template <typename T>
91 class SkNx<1, T> {
92 public:
SkNx()93 SkNx() {}
SkNx(T val)94 SkNx(T val) : fVal(val) {}
95
Load(const void * ptr)96 static SkNx Load(const void* ptr) {
97 auto vals = (const T*)ptr;
98 return SkNx(vals[0]);
99 }
100
store(void * ptr)101 void store(void* ptr) const {
102 auto vals = (T*) ptr;
103 vals[0] = fVal;
104 }
105
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
saturatedAdd(const SkNx & o)115 SkNx saturatedAdd(const SkNx& o) const {
116 SkASSERT((T)(~0) > 0); // TODO: support signed T?
117 T sum = fVal + o.fVal;
118 return sum < fVal ? (T)(~0) : sum;
119 }
120
Min(const SkNx & a,const SkNx & b)121 static SkNx Min(const SkNx& a, const SkNx& b) { return SkTMin(a.fVal, b.fVal); }
Max(const SkNx & a,const SkNx & b)122 static SkNx Max(const SkNx& a, const SkNx& b) { return SkTMax(a.fVal, b.fVal); }
123
abs()124 SkNx abs() const { return SkTAbs(fVal); }
floor()125 SkNx floor() const { return Floor(fVal); }
126
sqrt()127 SkNx sqrt () const { return Sqrt(fVal); }
rsqrt0()128 SkNx rsqrt0() const { return this->sqrt().invert(); }
rsqrt1()129 SkNx rsqrt1() const { return this->rsqrt0(); }
rsqrt2()130 SkNx rsqrt2() const { return this->rsqrt1(); }
131
invert()132 SkNx invert() const { return 1 / fVal; }
approxInvert()133 SkNx approxInvert() const { return this->invert(); }
134
135 T operator[](int k) const {
136 SkASSERT(0 == k);
137 return fVal;
138 }
139
allTrue()140 bool allTrue() const { return fVal != 0; }
anyTrue()141 bool anyTrue() const { return fVal != 0; }
thenElse(const SkNx & t,const SkNx & e)142 SkNx thenElse(const SkNx& t, const SkNx& e) const { return fVal != 0 ? t : e; }
143
144 protected:
Floor(double val)145 static double Floor(double val) { return ::floor (val); }
Floor(float val)146 static float Floor(float val) { return ::floorf(val); }
Sqrt(double val)147 static double Sqrt(double val) { return ::sqrt (val); }
Sqrt(float val)148 static float Sqrt(float val) { return ::sqrtf(val); }
149
150 T fVal;
151 };
152
153 // This generic shuffle can be called to create any valid SkNx<N,T>.
154 // Sk4f f(a,b,c,d);
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>
SkNx_shuffle(const SkNx<N,T> & src)158 static inline SkNx<sizeof...(Ix), T> SkNx_shuffle(const SkNx<N,T>& src) { return { src[Ix]... }; }
159
160 // This is a generic cast between two SkNx with the same number of elements N. E.g.
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>
SkNx_cast(const SkNx<2,S> & src)165 static inline SkNx<2,D> SkNx_cast(const SkNx<2,S>& src) {
166 return { (D)src[0], (D)src[1] };
167 }
168
169 template <typename D, typename S>
SkNx_cast(const SkNx<4,S> & src)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 }
173
174 template <typename D, typename S>
SkNx_cast(const SkNx<8,S> & src)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 }
179
180 template <typename D, typename S>
SkNx_cast(const SkNx<16,S> & src)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 }
187
188 typedef SkNx<2, float> Sk2f;
189 typedef SkNx<4, float> Sk4f;
190 typedef SkNx<2, SkScalar> Sk2s;
191 typedef SkNx<4, SkScalar> Sk4s;
192
193 typedef SkNx<4, uint8_t> Sk4b;
194 typedef SkNx<16, uint8_t> Sk16b;
195 typedef SkNx<4, uint16_t> Sk4h;
196 typedef SkNx<16, uint16_t> Sk16h;
197 typedef SkNx<4, int> Sk4i;
198
199 typedef SkNx<4, int> Sk4i;
200
201 // Include platform specific specializations if available.
202 #if !defined(SKNX_NO_SIMD) && SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE2
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
Sk4f_ToBytes(uint8_t p[16],const Sk4f & a,const Sk4f & b,const Sk4f & c,const Sk4f & d)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
216 #endif//SkNx_DEFINED
217