1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
|
/*
* Copyright 2015 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#ifndef SkNx_DEFINED
#define SkNx_DEFINED
#define SKNX_NO_SIMDx // Remove the x to disable SIMD for all SkNx types.
#include "SkScalar.h"
#include "SkTypes.h"
#include <math.h>
#define REQUIRE(x) static_assert(x, #x)
// This file may be included multiple times by .cpp files with different flags, leading
// to different definitions. Usually that doesn't matter because it's all inlined, but
// in Debug modes the compilers may not inline everything. So wrap everything in an
// anonymous namespace to give each includer their own silo of this code (or the linker
// will probably pick one randomly for us, which is rarely correct).
namespace {
// The default implementations just fall back on a pair of size N/2.
// SkNb is a _very_ minimal class representing a vector of bools returned by comparison operators.
// We pass along the byte size of the compared types (Bytes) to help platform specializations.
template <int N, int Bytes>
class SkNb {
public:
SkNb() {}
SkNb(const SkNb<N/2, Bytes>& lo, const SkNb<N/2, Bytes>& hi) : fLo(lo), fHi(hi) {}
bool allTrue() const { return fLo.allTrue() && fHi.allTrue(); }
bool anyTrue() const { return fLo.anyTrue() || fHi.anyTrue(); }
protected:
REQUIRE(0 == (N & (N-1)));
SkNb<N/2, Bytes> fLo, fHi;
};
template <int N, typename T>
class SkNi {
public:
SkNi() {}
SkNi(const SkNi<N/2, T>& lo, const SkNi<N/2, T>& hi) : fLo(lo), fHi(hi) {}
explicit SkNi(T val) : fLo(val), fHi(val) {}
static SkNi Load(const T vals[N]) {
return SkNi(SkNi<N/2,T>::Load(vals), SkNi<N/2,T>::Load(vals+N/2));
}
SkNi(T a, T b) : fLo(a), fHi(b) { REQUIRE(N==2); }
SkNi(T a, T b, T c, T d) : fLo(a,b), fHi(c,d) { REQUIRE(N==4); }
SkNi(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) { REQUIRE(N==8); }
SkNi(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)
: fLo(a,b,c,d, e,f,g,h), fHi(i,j,k,l, m,n,o,p) { REQUIRE(N==16); }
void store(T vals[N]) const {
fLo.store(vals);
fHi.store(vals+N/2);
}
SkNi saturatedAdd(const SkNi& o) const {
return SkNi(fLo.saturatedAdd(o.fLo), fHi.saturatedAdd(o.fHi));
}
SkNi operator + (const SkNi& o) const { return SkNi(fLo + o.fLo, fHi + o.fHi); }
SkNi operator - (const SkNi& o) const { return SkNi(fLo - o.fLo, fHi - o.fHi); }
SkNi operator * (const SkNi& o) const { return SkNi(fLo * o.fLo, fHi * o.fHi); }
SkNi operator << (int bits) const { return SkNi(fLo << bits, fHi << bits); }
SkNi operator >> (int bits) const { return SkNi(fLo >> bits, fHi >> bits); }
static SkNi Min(const SkNi& a, const SkNi& b) {
return SkNi(SkNi<N/2, T>::Min(a.fLo, b.fLo), SkNi<N/2, T>::Min(a.fHi, b.fHi));
}
// TODO: comparisons, max?
template <int k> T kth() const {
SkASSERT(0 <= k && k < N);
return k < N/2 ? fLo.template kth<k>() : fHi.template kth<k-N/2>();
}
protected:
REQUIRE(0 == (N & (N-1)));
SkNi<N/2, T> fLo, fHi;
};
template <int N, typename T>
class SkNf {
typedef SkNb<N, sizeof(T)> Nb;
static int32_t MyNi(float);
static int64_t MyNi(double);
typedef SkNi<N, decltype(MyNi(T()))> Ni;
public:
SkNf() {}
explicit SkNf(T val) : fLo(val), fHi(val) {}
static SkNf Load(const T vals[N]) {
return SkNf(SkNf<N/2,T>::Load(vals), SkNf<N/2,T>::Load(vals+N/2));
}
SkNf(T a, T b) : fLo(a), fHi(b) { REQUIRE(N==2); }
SkNf(T a, T b, T c, T d) : fLo(a,b), fHi(c,d) { REQUIRE(N==4); }
SkNf(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) { REQUIRE(N==8); }
void store(T vals[N]) const {
fLo.store(vals);
fHi.store(vals+N/2);
}
Ni castTrunc() const { return Ni(fLo.castTrunc(), fHi.castTrunc()); }
SkNf operator + (const SkNf& o) const { return SkNf(fLo + o.fLo, fHi + o.fHi); }
SkNf operator - (const SkNf& o) const { return SkNf(fLo - o.fLo, fHi - o.fHi); }
SkNf operator * (const SkNf& o) const { return SkNf(fLo * o.fLo, fHi * o.fHi); }
SkNf operator / (const SkNf& o) const { return SkNf(fLo / o.fLo, fHi / o.fHi); }
Nb operator == (const SkNf& o) const { return Nb(fLo == o.fLo, fHi == o.fHi); }
Nb operator != (const SkNf& o) const { return Nb(fLo != o.fLo, fHi != o.fHi); }
Nb operator < (const SkNf& o) const { return Nb(fLo < o.fLo, fHi < o.fHi); }
Nb operator > (const SkNf& o) const { return Nb(fLo > o.fLo, fHi > o.fHi); }
Nb operator <= (const SkNf& o) const { return Nb(fLo <= o.fLo, fHi <= o.fHi); }
Nb operator >= (const SkNf& o) const { return Nb(fLo >= o.fLo, fHi >= o.fHi); }
static SkNf Min(const SkNf& l, const SkNf& r) {
return SkNf(SkNf<N/2,T>::Min(l.fLo, r.fLo), SkNf<N/2,T>::Min(l.fHi, r.fHi));
}
static SkNf Max(const SkNf& l, const SkNf& r) {
return SkNf(SkNf<N/2,T>::Max(l.fLo, r.fLo), SkNf<N/2,T>::Max(l.fHi, r.fHi));
}
SkNf sqrt() const { return SkNf(fLo. sqrt(), fHi. sqrt()); }
// Generally, increasing precision, increasing cost.
SkNf rsqrt0() const { return SkNf(fLo.rsqrt0(), fHi.rsqrt0()); }
SkNf rsqrt1() const { return SkNf(fLo.rsqrt1(), fHi.rsqrt1()); }
SkNf rsqrt2() const { return SkNf(fLo.rsqrt2(), fHi.rsqrt2()); }
SkNf invert() const { return SkNf(fLo. invert(), fHi. invert()); }
SkNf approxInvert() const { return SkNf(fLo.approxInvert(), fHi.approxInvert()); }
template <int k> T kth() const {
SkASSERT(0 <= k && k < N);
return k < N/2 ? fLo.template kth<k>() : fHi.template kth<k-N/2>();
}
protected:
REQUIRE(0 == (N & (N-1)));
SkNf(const SkNf<N/2, T>& lo, const SkNf<N/2, T>& hi) : fLo(lo), fHi(hi) {}
SkNf<N/2, T> fLo, fHi;
};
// Bottom out the default implementations with scalars when nothing's been specialized.
template <int Bytes>
class SkNb<1, Bytes> {
public:
SkNb() {}
explicit SkNb(bool val) : fVal(val) {}
bool allTrue() const { return fVal; }
bool anyTrue() const { return fVal; }
protected:
bool fVal;
};
template <typename T>
class SkNi<1,T> {
public:
SkNi() {}
explicit SkNi(T val) : fVal(val) {}
static SkNi Load(const T vals[1]) { return SkNi(vals[0]); }
void store(T vals[1]) const { vals[0] = fVal; }
SkNi saturatedAdd(const SkNi& o) const {
SkASSERT((T)(~0) > 0); // TODO: support signed T
T sum = fVal + o.fVal;
return SkNi(sum > fVal ? sum : (T)(~0));
}
SkNi operator + (const SkNi& o) const { return SkNi(fVal + o.fVal); }
SkNi operator - (const SkNi& o) const { return SkNi(fVal - o.fVal); }
SkNi operator * (const SkNi& o) const { return SkNi(fVal * o.fVal); }
SkNi operator << (int bits) const { return SkNi(fVal << bits); }
SkNi operator >> (int bits) const { return SkNi(fVal >> bits); }
static SkNi Min(const SkNi& a, const SkNi& b) { return SkNi(SkTMin(a.fVal, b.fVal)); }
template <int k> T kth() const {
SkASSERT(0 == k);
return fVal;
}
protected:
T fVal;
};
template <typename T>
class SkNf<1,T> {
typedef SkNb<1, sizeof(T)> Nb;
static int32_t MyNi(float);
static int64_t MyNi(double);
typedef SkNi<1, decltype(MyNi(T()))> Ni;
public:
SkNf() {}
explicit SkNf(T val) : fVal(val) {}
static SkNf Load(const T vals[1]) { return SkNf(vals[0]); }
void store(T vals[1]) const { vals[0] = fVal; }
Ni castTrunc() const { return Ni(fVal); }
SkNf operator + (const SkNf& o) const { return SkNf(fVal + o.fVal); }
SkNf operator - (const SkNf& o) const { return SkNf(fVal - o.fVal); }
SkNf operator * (const SkNf& o) const { return SkNf(fVal * o.fVal); }
SkNf operator / (const SkNf& o) const { return SkNf(fVal / o.fVal); }
Nb operator == (const SkNf& o) const { return Nb(fVal == o.fVal); }
Nb operator != (const SkNf& o) const { return Nb(fVal != o.fVal); }
Nb operator < (const SkNf& o) const { return Nb(fVal < o.fVal); }
Nb operator > (const SkNf& o) const { return Nb(fVal > o.fVal); }
Nb operator <= (const SkNf& o) const { return Nb(fVal <= o.fVal); }
Nb operator >= (const SkNf& o) const { return Nb(fVal >= o.fVal); }
static SkNf Min(const SkNf& l, const SkNf& r) { return SkNf(SkTMin(l.fVal, r.fVal)); }
static SkNf Max(const SkNf& l, const SkNf& r) { return SkNf(SkTMax(l.fVal, r.fVal)); }
SkNf sqrt() const { return SkNf(Sqrt(fVal)); }
SkNf rsqrt0() const { return SkNf((T)1 / Sqrt(fVal)); }
SkNf rsqrt1() const { return this->rsqrt0(); }
SkNf rsqrt2() const { return this->rsqrt1(); }
SkNf invert() const { return SkNf((T)1 / fVal); }
SkNf approxInvert() const { return this->invert(); }
template <int k> T kth() const {
SkASSERT(k == 0);
return fVal;
}
protected:
// We do double sqrts natively, or via floats for any other type.
template <typename U>
static U Sqrt(U val) { return (U) ::sqrtf((float)val); }
static double Sqrt(double val) { return ::sqrt ( val); }
T fVal;
};
} // namespace
// Include platform specific specializations if available.
#ifndef SKNX_NO_SIMD
#if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE2
#include "../opts/SkNx_sse.h"
#elif defined(SK_ARM_HAS_NEON)
#include "../opts/SkNx_neon.h"
#endif
#endif
#undef REQUIRE
typedef SkNf<2, float> Sk2f;
typedef SkNf<2, double> Sk2d;
typedef SkNf<2, SkScalar> Sk2s;
typedef SkNf<4, float> Sk4f;
typedef SkNf<4, double> Sk4d;
typedef SkNf<4, SkScalar> Sk4s;
typedef SkNi<4, uint16_t> Sk4h;
typedef SkNi<8, uint16_t> Sk8h;
typedef SkNi<16, uint16_t> Sk16h;
typedef SkNi<16, uint8_t> Sk16b;
typedef SkNi<4, int32_t> Sk4i;
typedef SkNi<4, uint32_t> Sk4u;
#endif//SkNx_DEFINED
|