• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_sse_DEFINED
9 #define SkNx_sse_DEFINED
10 
11 #include "include/core/SkTypes.h"
12 
13 #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE41
14     #include <smmintrin.h>
15 #elif SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSSE3
16     #include <tmmintrin.h>
17 #else
18     #include <emmintrin.h>
19 #endif
20 
21 // This file may assume <= SSE2, but must check SK_CPU_SSE_LEVEL for anything more recent.
22 // If you do, make sure this is in a static inline function... anywhere else risks violating ODR.
23 
24 namespace {  // NOLINT(google-build-namespaces)
25 
26 // Emulate _mm_floor_ps() with SSE2:
27 //   - roundtrip through integers via truncation
28 //   - subtract 1 if that's too big (possible for negative values).
29 // This restricts the domain of our inputs to a maximum somehwere around 2^31.
30 // Seems plenty big.
emulate_mm_floor_ps(__m128 v)31 AI static __m128 emulate_mm_floor_ps(__m128 v) {
32     __m128 roundtrip = _mm_cvtepi32_ps(_mm_cvttps_epi32(v));
33     __m128 too_big = _mm_cmpgt_ps(roundtrip, v);
34     return _mm_sub_ps(roundtrip, _mm_and_ps(too_big, _mm_set1_ps(1.0f)));
35 }
36 
37 template <>
38 class SkNx<2, float> {
39 public:
SkNx(const __m128 & vec)40     AI SkNx(const __m128& vec) : fVec(vec) {}
41 
SkNx()42     AI SkNx() {}
SkNx(float val)43     AI SkNx(float val) : fVec(_mm_set1_ps(val)) {}
Load(const void * ptr)44     AI static SkNx Load(const void* ptr) {
45         return _mm_castsi128_ps(_mm_loadl_epi64((const __m128i*)ptr));
46     }
SkNx(float a,float b)47     AI SkNx(float a, float b) : fVec(_mm_setr_ps(a,b,0,0)) {}
48 
store(void * ptr)49     AI void store(void* ptr) const { _mm_storel_pi((__m64*)ptr, fVec); }
50 
Load2(const void * ptr,SkNx * x,SkNx * y)51     AI static void Load2(const void* ptr, SkNx* x, SkNx* y) {
52         const float* m = (const float*)ptr;
53         *x = SkNx{m[0], m[2]};
54         *y = SkNx{m[1], m[3]};
55     }
56 
Store2(void * dst,const SkNx & a,const SkNx & b)57     AI static void Store2(void* dst, const SkNx& a, const SkNx& b) {
58         auto vals = _mm_unpacklo_ps(a.fVec, b.fVec);
59         _mm_storeu_ps((float*)dst, vals);
60     }
61 
Store3(void * dst,const SkNx & a,const SkNx & b,const SkNx & c)62     AI static void Store3(void* dst, const SkNx& a, const SkNx& b, const SkNx& c) {
63         auto lo = _mm_setr_ps(a[0], b[0], c[0], a[1]),
64              hi = _mm_setr_ps(b[1], c[1],    0,    0);
65         _mm_storeu_ps((float*)dst, lo);
66         _mm_storel_pi(((__m64*)dst) + 2, hi);
67     }
68 
Store4(void * dst,const SkNx & a,const SkNx & b,const SkNx & c,const SkNx & d)69     AI static void Store4(void* dst, const SkNx& a, const SkNx& b, const SkNx& c, const SkNx& d) {
70         auto lo = _mm_setr_ps(a[0], b[0], c[0], d[0]),
71              hi = _mm_setr_ps(a[1], b[1], c[1], d[1]);
72         _mm_storeu_ps((float*)dst, lo);
73         _mm_storeu_ps(((float*)dst) + 4, hi);
74     }
75 
76     AI SkNx operator - () const { return _mm_xor_ps(_mm_set1_ps(-0.0f), fVec); }
77 
78     AI SkNx operator + (const SkNx& o) const { return _mm_add_ps(fVec, o.fVec); }
79     AI SkNx operator - (const SkNx& o) const { return _mm_sub_ps(fVec, o.fVec); }
80     AI SkNx operator * (const SkNx& o) const { return _mm_mul_ps(fVec, o.fVec); }
81     AI SkNx operator / (const SkNx& o) const { return _mm_div_ps(fVec, o.fVec); }
82 
83     AI SkNx operator == (const SkNx& o) const { return _mm_cmpeq_ps (fVec, o.fVec); }
84     AI SkNx operator != (const SkNx& o) const { return _mm_cmpneq_ps(fVec, o.fVec); }
85     AI SkNx operator  < (const SkNx& o) const { return _mm_cmplt_ps (fVec, o.fVec); }
86     AI SkNx operator  > (const SkNx& o) const { return _mm_cmpgt_ps (fVec, o.fVec); }
87     AI SkNx operator <= (const SkNx& o) const { return _mm_cmple_ps (fVec, o.fVec); }
88     AI SkNx operator >= (const SkNx& o) const { return _mm_cmpge_ps (fVec, o.fVec); }
89 
Min(const SkNx & l,const SkNx & r)90     AI static SkNx Min(const SkNx& l, const SkNx& r) { return _mm_min_ps(l.fVec, r.fVec); }
Max(const SkNx & l,const SkNx & r)91     AI static SkNx Max(const SkNx& l, const SkNx& r) { return _mm_max_ps(l.fVec, r.fVec); }
92 
abs()93     AI SkNx abs() const { return _mm_andnot_ps(_mm_set1_ps(-0.0f), fVec); }
floor()94     AI SkNx floor() const {
95     #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE41
96         return _mm_floor_ps(fVec);
97     #else
98         return emulate_mm_floor_ps(fVec);
99     #endif
100     }
101 
sqrt()102     AI SkNx   sqrt() const { return _mm_sqrt_ps (fVec);  }
103 
104     AI float operator[](int k) const {
105         SkASSERT(0 <= k && k < 2);
106         union { __m128 v; float fs[4]; } pun = {fVec};
107         return pun.fs[k&1];
108     }
109 
allTrue()110     AI bool allTrue() const { return 0b11 == (_mm_movemask_ps(fVec) & 0b11); }
anyTrue()111     AI bool anyTrue() const { return 0b00 != (_mm_movemask_ps(fVec) & 0b11); }
112 
thenElse(const SkNx & t,const SkNx & e)113     AI SkNx thenElse(const SkNx& t, const SkNx& e) const {
114     #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE41
115         return _mm_blendv_ps(e.fVec, t.fVec, fVec);
116     #else
117         return _mm_or_ps(_mm_and_ps   (fVec, t.fVec),
118                          _mm_andnot_ps(fVec, e.fVec));
119     #endif
120     }
121 
122     __m128 fVec;
123 };
124 
125 template <>
126 class SkNx<4, float> {
127 public:
SkNx(const __m128 & vec)128     AI SkNx(const __m128& vec) : fVec(vec) {}
129 
SkNx()130     AI SkNx() {}
SkNx(float val)131     AI SkNx(float val)           : fVec( _mm_set1_ps(val) ) {}
SkNx(float a,float b,float c,float d)132     AI SkNx(float a, float b, float c, float d) : fVec(_mm_setr_ps(a,b,c,d)) {}
133 
Load(const void * ptr)134     AI static SkNx Load(const void* ptr) { return _mm_loadu_ps((const float*)ptr); }
store(void * ptr)135     AI void store(void* ptr) const { _mm_storeu_ps((float*)ptr, fVec); }
136 
Load2(const void * ptr,SkNx * x,SkNx * y)137     AI static void Load2(const void* ptr, SkNx* x, SkNx* y) {
138         SkNx lo = SkNx::Load((const float*)ptr+0),
139              hi = SkNx::Load((const float*)ptr+4);
140         *x = SkNx{lo[0], lo[2], hi[0], hi[2]};
141         *y = SkNx{lo[1], lo[3], hi[1], hi[3]};
142     }
143 
Load4(const void * ptr,SkNx * r,SkNx * g,SkNx * b,SkNx * a)144     AI static void Load4(const void* ptr, SkNx* r, SkNx* g, SkNx* b, SkNx* a) {
145         __m128 v0 = _mm_loadu_ps(((float*)ptr) +  0),
146                v1 = _mm_loadu_ps(((float*)ptr) +  4),
147                v2 = _mm_loadu_ps(((float*)ptr) +  8),
148                v3 = _mm_loadu_ps(((float*)ptr) + 12);
149         _MM_TRANSPOSE4_PS(v0, v1, v2, v3);
150         *r = v0;
151         *g = v1;
152         *b = v2;
153         *a = v3;
154     }
Store4(void * dst,const SkNx & r,const SkNx & g,const SkNx & b,const SkNx & a)155     AI static void Store4(void* dst, const SkNx& r, const SkNx& g, const SkNx& b, const SkNx& a) {
156         __m128 v0 = r.fVec,
157                v1 = g.fVec,
158                v2 = b.fVec,
159                v3 = a.fVec;
160         _MM_TRANSPOSE4_PS(v0, v1, v2, v3);
161         _mm_storeu_ps(((float*) dst) +  0, v0);
162         _mm_storeu_ps(((float*) dst) +  4, v1);
163         _mm_storeu_ps(((float*) dst) +  8, v2);
164         _mm_storeu_ps(((float*) dst) + 12, v3);
165     }
166 
167     AI SkNx operator - () const { return _mm_xor_ps(_mm_set1_ps(-0.0f), fVec); }
168 
169     AI SkNx operator + (const SkNx& o) const { return _mm_add_ps(fVec, o.fVec); }
170     AI SkNx operator - (const SkNx& o) const { return _mm_sub_ps(fVec, o.fVec); }
171     AI SkNx operator * (const SkNx& o) const { return _mm_mul_ps(fVec, o.fVec); }
172     AI SkNx operator / (const SkNx& o) const { return _mm_div_ps(fVec, o.fVec); }
173 
174     AI SkNx operator == (const SkNx& o) const { return _mm_cmpeq_ps (fVec, o.fVec); }
175     AI SkNx operator != (const SkNx& o) const { return _mm_cmpneq_ps(fVec, o.fVec); }
176     AI SkNx operator  < (const SkNx& o) const { return _mm_cmplt_ps (fVec, o.fVec); }
177     AI SkNx operator  > (const SkNx& o) const { return _mm_cmpgt_ps (fVec, o.fVec); }
178     AI SkNx operator <= (const SkNx& o) const { return _mm_cmple_ps (fVec, o.fVec); }
179     AI SkNx operator >= (const SkNx& o) const { return _mm_cmpge_ps (fVec, o.fVec); }
180 
Min(const SkNx & l,const SkNx & r)181     AI static SkNx Min(const SkNx& l, const SkNx& r) { return _mm_min_ps(l.fVec, r.fVec); }
Max(const SkNx & l,const SkNx & r)182     AI static SkNx Max(const SkNx& l, const SkNx& r) { return _mm_max_ps(l.fVec, r.fVec); }
183 
abs()184     AI SkNx abs() const { return _mm_andnot_ps(_mm_set1_ps(-0.0f), fVec); }
floor()185     AI SkNx floor() const {
186     #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE41
187         return _mm_floor_ps(fVec);
188     #else
189         return emulate_mm_floor_ps(fVec);
190     #endif
191     }
192 
sqrt()193     AI SkNx   sqrt() const { return _mm_sqrt_ps (fVec);  }
194 
195     AI float operator[](int k) const {
196         SkASSERT(0 <= k && k < 4);
197         union { __m128 v; float fs[4]; } pun = {fVec};
198         return pun.fs[k&3];
199     }
200 
min()201     AI float min() const {
202         SkNx min = Min(*this, _mm_shuffle_ps(fVec, fVec, _MM_SHUFFLE(2,3,0,1)));
203         min = Min(min, _mm_shuffle_ps(min.fVec, min.fVec, _MM_SHUFFLE(0,1,2,3)));
204         return min[0];
205     }
206 
max()207     AI float max() const {
208         SkNx max = Max(*this, _mm_shuffle_ps(fVec, fVec, _MM_SHUFFLE(2,3,0,1)));
209         max = Max(max, _mm_shuffle_ps(max.fVec, max.fVec, _MM_SHUFFLE(0,1,2,3)));
210         return max[0];
211     }
212 
allTrue()213     AI bool allTrue() const { return 0b1111 == _mm_movemask_ps(fVec); }
anyTrue()214     AI bool anyTrue() const { return 0b0000 != _mm_movemask_ps(fVec); }
215 
thenElse(const SkNx & t,const SkNx & e)216     AI SkNx thenElse(const SkNx& t, const SkNx& e) const {
217     #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE41
218         return _mm_blendv_ps(e.fVec, t.fVec, fVec);
219     #else
220         return _mm_or_ps(_mm_and_ps   (fVec, t.fVec),
221                          _mm_andnot_ps(fVec, e.fVec));
222     #endif
223     }
224 
225     __m128 fVec;
226 };
227 
mullo32(__m128i a,__m128i b)228 AI static __m128i mullo32(__m128i a, __m128i b) {
229 #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE41
230     return _mm_mullo_epi32(a, b);
231 #else
232     __m128i mul20 = _mm_mul_epu32(a, b),
233             mul31 = _mm_mul_epu32(_mm_srli_si128(a, 4), _mm_srli_si128(b, 4));
234     return _mm_unpacklo_epi32(_mm_shuffle_epi32(mul20, _MM_SHUFFLE(0,0,2,0)),
235                               _mm_shuffle_epi32(mul31, _MM_SHUFFLE(0,0,2,0)));
236 #endif
237 }
238 
239 template <>
240 class SkNx<4, int32_t> {
241 public:
SkNx(const __m128i & vec)242     AI SkNx(const __m128i& vec) : fVec(vec) {}
243 
SkNx()244     AI SkNx() {}
SkNx(int32_t val)245     AI SkNx(int32_t val) : fVec(_mm_set1_epi32(val)) {}
Load(const void * ptr)246     AI static SkNx Load(const void* ptr) { return _mm_loadu_si128((const __m128i*)ptr); }
SkNx(int32_t a,int32_t b,int32_t c,int32_t d)247     AI SkNx(int32_t a, int32_t b, int32_t c, int32_t d) : fVec(_mm_setr_epi32(a,b,c,d)) {}
248 
store(void * ptr)249     AI void store(void* ptr) const { _mm_storeu_si128((__m128i*)ptr, fVec); }
250 
251     AI SkNx operator + (const SkNx& o) const { return _mm_add_epi32(fVec, o.fVec); }
252     AI SkNx operator - (const SkNx& o) const { return _mm_sub_epi32(fVec, o.fVec); }
253     AI SkNx operator * (const SkNx& o) const { return mullo32(fVec, o.fVec);       }
254 
255     AI SkNx operator & (const SkNx& o) const { return _mm_and_si128(fVec, o.fVec); }
256     AI SkNx operator | (const SkNx& o) const { return _mm_or_si128(fVec, o.fVec);  }
257     AI SkNx operator ^ (const SkNx& o) const { return _mm_xor_si128(fVec, o.fVec); }
258 
259     AI SkNx operator << (int bits) const { return _mm_slli_epi32(fVec, bits); }
260     AI SkNx operator >> (int bits) const { return _mm_srai_epi32(fVec, bits); }
261 
262     AI SkNx operator == (const SkNx& o) const { return _mm_cmpeq_epi32 (fVec, o.fVec); }
263     AI SkNx operator  < (const SkNx& o) const { return _mm_cmplt_epi32 (fVec, o.fVec); }
264     AI SkNx operator  > (const SkNx& o) const { return _mm_cmpgt_epi32 (fVec, o.fVec); }
265 
266     AI int32_t operator[](int k) const {
267         SkASSERT(0 <= k && k < 4);
268         union { __m128i v; int32_t is[4]; } pun = {fVec};
269         return pun.is[k&3];
270     }
271 
thenElse(const SkNx & t,const SkNx & e)272     AI SkNx thenElse(const SkNx& t, const SkNx& e) const {
273     #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE41
274         return _mm_blendv_epi8(e.fVec, t.fVec, fVec);
275     #else
276         return _mm_or_si128(_mm_and_si128   (fVec, t.fVec),
277                             _mm_andnot_si128(fVec, e.fVec));
278     #endif
279     }
280 
abs()281     AI SkNx abs() const {
282 #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSSE3
283         return _mm_abs_epi32(fVec);
284 #else
285         SkNx mask = (*this) >> 31;
286         return (mask ^ (*this)) - mask;
287 #endif
288     }
289 
Min(const SkNx & x,const SkNx & y)290     AI static SkNx Min(const SkNx& x, const SkNx& y) {
291 #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE41
292         return _mm_min_epi32(x.fVec, y.fVec);
293 #else
294         return (x < y).thenElse(x, y);
295 #endif
296     }
297 
Max(const SkNx & x,const SkNx & y)298     AI static SkNx Max(const SkNx& x, const SkNx& y) {
299 #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE41
300         return _mm_max_epi32(x.fVec, y.fVec);
301 #else
302         return (x > y).thenElse(x, y);
303 #endif
304     }
305 
306     __m128i fVec;
307 };
308 
309 template <>
310 class SkNx<2, uint32_t> {
311 public:
SkNx(const __m128i & vec)312     AI SkNx(const __m128i& vec) : fVec(vec) {}
313 
SkNx()314     AI SkNx() {}
SkNx(uint32_t val)315     AI SkNx(uint32_t val) : fVec(_mm_set1_epi32((int)val)) {}
Load(const void * ptr)316     AI static SkNx Load(const void* ptr) { return _mm_loadl_epi64((const __m128i*)ptr); }
SkNx(uint32_t a,uint32_t b)317     AI SkNx(uint32_t a, uint32_t b) : fVec(_mm_setr_epi32((int)a,(int)b,0,0)) {}
318 
store(void * ptr)319     AI void store(void* ptr) const { _mm_storel_epi64((__m128i*)ptr, fVec); }
320 
321     AI SkNx operator + (const SkNx& o) const { return _mm_add_epi32(fVec, o.fVec); }
322     AI SkNx operator - (const SkNx& o) const { return _mm_sub_epi32(fVec, o.fVec); }
323     AI SkNx operator * (const SkNx& o) const { return mullo32(fVec, o.fVec);       }
324 
325     AI SkNx operator & (const SkNx& o) const { return _mm_and_si128(fVec, o.fVec); }
326     AI SkNx operator | (const SkNx& o) const { return _mm_or_si128(fVec, o.fVec);  }
327     AI SkNx operator ^ (const SkNx& o) const { return _mm_xor_si128(fVec, o.fVec); }
328 
329     AI SkNx operator << (int bits) const { return _mm_slli_epi32(fVec, bits); }
330     AI SkNx operator >> (int bits) const { return _mm_srli_epi32(fVec, bits); }
331 
332     AI SkNx operator == (const SkNx& o) const { return _mm_cmpeq_epi32 (fVec, o.fVec); }
333     AI SkNx operator != (const SkNx& o) const { return (*this == o) ^ 0xffffffff; }
334     // operator < and > take a little extra fiddling to make work for unsigned ints.
335 
336     AI uint32_t operator[](int k) const {
337         SkASSERT(0 <= k && k < 2);
338         union { __m128i v; uint32_t us[4]; } pun = {fVec};
339         return pun.us[k&1];
340     }
341 
thenElse(const SkNx & t,const SkNx & e)342     AI SkNx thenElse(const SkNx& t, const SkNx& e) const {
343 #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE41
344         return _mm_blendv_epi8(e.fVec, t.fVec, fVec);
345 #else
346         return _mm_or_si128(_mm_and_si128   (fVec, t.fVec),
347                             _mm_andnot_si128(fVec, e.fVec));
348 #endif
349     }
350 
allTrue()351     AI bool allTrue() const { return 0xff == (_mm_movemask_epi8(fVec) & 0xff); }
352 
353     __m128i fVec;
354 };
355 
356 template <>
357 class SkNx<4, uint32_t> {
358 public:
SkNx(const __m128i & vec)359     AI SkNx(const __m128i& vec) : fVec(vec) {}
360 
SkNx()361     AI SkNx() {}
SkNx(uint32_t val)362     AI SkNx(uint32_t val) : fVec(_mm_set1_epi32((int)val)) {}
Load(const void * ptr)363     AI static SkNx Load(const void* ptr) { return _mm_loadu_si128((const __m128i*)ptr); }
SkNx(uint32_t a,uint32_t b,uint32_t c,uint32_t d)364     AI SkNx(uint32_t a, uint32_t b, uint32_t c, uint32_t d)
365         : fVec(_mm_setr_epi32((int)a,(int)b,(int)c,(int)d)) {}
366 
store(void * ptr)367     AI void store(void* ptr) const { _mm_storeu_si128((__m128i*)ptr, fVec); }
368 
369     AI SkNx operator + (const SkNx& o) const { return _mm_add_epi32(fVec, o.fVec); }
370     AI SkNx operator - (const SkNx& o) const { return _mm_sub_epi32(fVec, o.fVec); }
371     AI SkNx operator * (const SkNx& o) const { return mullo32(fVec, o.fVec);       }
372 
373     AI SkNx operator & (const SkNx& o) const { return _mm_and_si128(fVec, o.fVec); }
374     AI SkNx operator | (const SkNx& o) const { return _mm_or_si128(fVec, o.fVec);  }
375     AI SkNx operator ^ (const SkNx& o) const { return _mm_xor_si128(fVec, o.fVec); }
376 
377     AI SkNx operator << (int bits) const { return _mm_slli_epi32(fVec, bits); }
378     AI SkNx operator >> (int bits) const { return _mm_srli_epi32(fVec, bits); }
379 
380     AI SkNx operator == (const SkNx& o) const { return _mm_cmpeq_epi32 (fVec, o.fVec); }
381     AI SkNx operator != (const SkNx& o) const { return (*this == o) ^ 0xffffffff; }
382 
383     // operator < and > take a little extra fiddling to make work for unsigned ints.
384 
385     AI uint32_t operator[](int k) const {
386         SkASSERT(0 <= k && k < 4);
387         union { __m128i v; uint32_t us[4]; } pun = {fVec};
388         return pun.us[k&3];
389     }
390 
thenElse(const SkNx & t,const SkNx & e)391     AI SkNx thenElse(const SkNx& t, const SkNx& e) const {
392     #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE41
393         return _mm_blendv_epi8(e.fVec, t.fVec, fVec);
394     #else
395         return _mm_or_si128(_mm_and_si128   (fVec, t.fVec),
396                             _mm_andnot_si128(fVec, e.fVec));
397     #endif
398     }
399 
mulHi(SkNx m)400     AI SkNx mulHi(SkNx m) const {
401         SkNx v20{_mm_mul_epu32(m.fVec, fVec)};
402         SkNx v31{_mm_mul_epu32(_mm_srli_si128(m.fVec, 4), _mm_srli_si128(fVec, 4))};
403 
404         return SkNx{v20[1], v31[1], v20[3], v31[3]};
405     }
406 
407     __m128i fVec;
408 };
409 
410 template <>
411 class SkNx<4, uint16_t> {
412 public:
SkNx(const __m128i & vec)413     AI SkNx(const __m128i& vec) : fVec(vec) {}
414 
SkNx()415     AI SkNx() {}
SkNx(uint16_t val)416     AI SkNx(uint16_t val) : fVec(_mm_set1_epi16((short)val)) {}
SkNx(uint16_t a,uint16_t b,uint16_t c,uint16_t d)417     AI SkNx(uint16_t a, uint16_t b, uint16_t c, uint16_t d)
418         : fVec(_mm_setr_epi16((short)a,(short)b,(short)c,(short)d,0,0,0,0)) {}
419 
Load(const void * ptr)420     AI static SkNx Load(const void* ptr) { return _mm_loadl_epi64((const __m128i*)ptr); }
store(void * ptr)421     AI void store(void* ptr) const { _mm_storel_epi64((__m128i*)ptr, fVec); }
422 
Load4(const void * ptr,SkNx * r,SkNx * g,SkNx * b,SkNx * a)423     AI static void Load4(const void* ptr, SkNx* r, SkNx* g, SkNx* b, SkNx* a) {
424         __m128i lo = _mm_loadu_si128(((__m128i*)ptr) + 0),
425                 hi = _mm_loadu_si128(((__m128i*)ptr) + 1);
426         __m128i even = _mm_unpacklo_epi16(lo, hi),   // r0 r2 g0 g2 b0 b2 a0 a2
427                  odd = _mm_unpackhi_epi16(lo, hi);   // r1 r3 ...
428         __m128i rg = _mm_unpacklo_epi16(even, odd),  // r0 r1 r2 r3 g0 g1 g2 g3
429                 ba = _mm_unpackhi_epi16(even, odd);  // b0 b1 ...   a0 a1 ...
430         *r = rg;
431         *g = _mm_srli_si128(rg, 8);
432         *b = ba;
433         *a = _mm_srli_si128(ba, 8);
434     }
Load3(const void * ptr,SkNx * r,SkNx * g,SkNx * b)435     AI static void Load3(const void* ptr, SkNx* r, SkNx* g, SkNx* b) {
436         // The idea here is to get 4 vectors that are R G B _ _ _ _ _.
437         // The second load is at a funny location to make sure we don't read past
438         // the bounds of memory.  This is fine, we just need to shift it a little bit.
439         const uint8_t* ptr8 = (const uint8_t*) ptr;
440         __m128i rgb0 = _mm_loadu_si128((const __m128i*) (ptr8 + 0));
441         __m128i rgb1 = _mm_srli_si128(rgb0, 3*2);
442         __m128i rgb2 = _mm_srli_si128(_mm_loadu_si128((const __m128i*) (ptr8 + 4*2)), 2*2);
443         __m128i rgb3 = _mm_srli_si128(rgb2, 3*2);
444 
445         __m128i rrggbb01 = _mm_unpacklo_epi16(rgb0, rgb1);
446         __m128i rrggbb23 = _mm_unpacklo_epi16(rgb2, rgb3);
447         *r = _mm_unpacklo_epi32(rrggbb01, rrggbb23);
448         *g = _mm_srli_si128(r->fVec, 4*2);
449         *b = _mm_unpackhi_epi32(rrggbb01, rrggbb23);
450     }
Store4(void * dst,const SkNx & r,const SkNx & g,const SkNx & b,const SkNx & a)451     AI static void Store4(void* dst, const SkNx& r, const SkNx& g, const SkNx& b, const SkNx& a) {
452         __m128i rg = _mm_unpacklo_epi16(r.fVec, g.fVec);
453         __m128i ba = _mm_unpacklo_epi16(b.fVec, a.fVec);
454         __m128i lo = _mm_unpacklo_epi32(rg, ba);
455         __m128i hi = _mm_unpackhi_epi32(rg, ba);
456         _mm_storeu_si128(((__m128i*) dst) + 0, lo);
457         _mm_storeu_si128(((__m128i*) dst) + 1, hi);
458     }
459 
460     AI SkNx operator + (const SkNx& o) const { return _mm_add_epi16(fVec, o.fVec); }
461     AI SkNx operator - (const SkNx& o) const { return _mm_sub_epi16(fVec, o.fVec); }
462     AI SkNx operator * (const SkNx& o) const { return _mm_mullo_epi16(fVec, o.fVec); }
463     AI SkNx operator & (const SkNx& o) const { return _mm_and_si128(fVec, o.fVec); }
464     AI SkNx operator | (const SkNx& o) const { return _mm_or_si128(fVec, o.fVec); }
465 
466     AI SkNx operator << (int bits) const { return _mm_slli_epi16(fVec, bits); }
467     AI SkNx operator >> (int bits) const { return _mm_srli_epi16(fVec, bits); }
468 
469     AI uint16_t operator[](int k) const {
470         SkASSERT(0 <= k && k < 4);
471         union { __m128i v; uint16_t us[8]; } pun = {fVec};
472         return pun.us[k&3];
473     }
474 
475     __m128i fVec;
476 };
477 
478 template <>
479 class SkNx<8, uint16_t> {
480 public:
SkNx(const __m128i & vec)481     AI SkNx(const __m128i& vec) : fVec(vec) {}
482 
SkNx()483     AI SkNx() {}
SkNx(uint16_t val)484     AI SkNx(uint16_t val) : fVec(_mm_set1_epi16((short)val)) {}
SkNx(uint16_t a,uint16_t b,uint16_t c,uint16_t d,uint16_t e,uint16_t f,uint16_t g,uint16_t h)485     AI SkNx(uint16_t a, uint16_t b, uint16_t c, uint16_t d,
486             uint16_t e, uint16_t f, uint16_t g, uint16_t h)
487         : fVec(_mm_setr_epi16((short)a,(short)b,(short)c,(short)d,
488                               (short)e,(short)f,(short)g,(short)h)) {}
489 
Load(const void * ptr)490     AI static SkNx Load(const void* ptr) { return _mm_loadu_si128((const __m128i*)ptr); }
store(void * ptr)491     AI void store(void* ptr) const { _mm_storeu_si128((__m128i*)ptr, fVec); }
492 
Load4(const void * ptr,SkNx * r,SkNx * g,SkNx * b,SkNx * a)493     AI static void Load4(const void* ptr, SkNx* r, SkNx* g, SkNx* b, SkNx* a) {
494         __m128i _01 = _mm_loadu_si128(((__m128i*)ptr) + 0),
495                 _23 = _mm_loadu_si128(((__m128i*)ptr) + 1),
496                 _45 = _mm_loadu_si128(((__m128i*)ptr) + 2),
497                 _67 = _mm_loadu_si128(((__m128i*)ptr) + 3);
498 
499         __m128i _02 = _mm_unpacklo_epi16(_01, _23),  // r0 r2 g0 g2 b0 b2 a0 a2
500                 _13 = _mm_unpackhi_epi16(_01, _23),  // r1 r3 g1 g3 b1 b3 a1 a3
501                 _46 = _mm_unpacklo_epi16(_45, _67),
502                 _57 = _mm_unpackhi_epi16(_45, _67);
503 
504         __m128i rg0123 = _mm_unpacklo_epi16(_02, _13),  // r0 r1 r2 r3 g0 g1 g2 g3
505                 ba0123 = _mm_unpackhi_epi16(_02, _13),  // b0 b1 b2 b3 a0 a1 a2 a3
506                 rg4567 = _mm_unpacklo_epi16(_46, _57),
507                 ba4567 = _mm_unpackhi_epi16(_46, _57);
508 
509         *r = _mm_unpacklo_epi64(rg0123, rg4567);
510         *g = _mm_unpackhi_epi64(rg0123, rg4567);
511         *b = _mm_unpacklo_epi64(ba0123, ba4567);
512         *a = _mm_unpackhi_epi64(ba0123, ba4567);
513     }
Load3(const void * ptr,SkNx * r,SkNx * g,SkNx * b)514     AI static void Load3(const void* ptr, SkNx* r, SkNx* g, SkNx* b) {
515         const uint8_t* ptr8 = (const uint8_t*) ptr;
516         __m128i rgb0 = _mm_loadu_si128((const __m128i*) (ptr8 +  0*2));
517         __m128i rgb1 = _mm_srli_si128(rgb0, 3*2);
518         __m128i rgb2 = _mm_loadu_si128((const __m128i*) (ptr8 +  6*2));
519         __m128i rgb3 = _mm_srli_si128(rgb2, 3*2);
520         __m128i rgb4 = _mm_loadu_si128((const __m128i*) (ptr8 + 12*2));
521         __m128i rgb5 = _mm_srli_si128(rgb4, 3*2);
522         __m128i rgb6 = _mm_srli_si128(_mm_loadu_si128((const __m128i*) (ptr8 + 16*2)), 2*2);
523         __m128i rgb7 = _mm_srli_si128(rgb6, 3*2);
524 
525         __m128i rgb01 = _mm_unpacklo_epi16(rgb0, rgb1);
526         __m128i rgb23 = _mm_unpacklo_epi16(rgb2, rgb3);
527         __m128i rgb45 = _mm_unpacklo_epi16(rgb4, rgb5);
528         __m128i rgb67 = _mm_unpacklo_epi16(rgb6, rgb7);
529 
530         __m128i rg03 = _mm_unpacklo_epi32(rgb01, rgb23);
531         __m128i bx03 = _mm_unpackhi_epi32(rgb01, rgb23);
532         __m128i rg47 = _mm_unpacklo_epi32(rgb45, rgb67);
533         __m128i bx47 = _mm_unpackhi_epi32(rgb45, rgb67);
534 
535         *r = _mm_unpacklo_epi64(rg03, rg47);
536         *g = _mm_unpackhi_epi64(rg03, rg47);
537         *b = _mm_unpacklo_epi64(bx03, bx47);
538     }
Store4(void * ptr,const SkNx & r,const SkNx & g,const SkNx & b,const SkNx & a)539     AI static void Store4(void* ptr, const SkNx& r, const SkNx& g, const SkNx& b, const SkNx& a) {
540         __m128i rg0123 = _mm_unpacklo_epi16(r.fVec, g.fVec),  // r0 g0 r1 g1 r2 g2 r3 g3
541                 rg4567 = _mm_unpackhi_epi16(r.fVec, g.fVec),  // r4 g4 r5 g5 r6 g6 r7 g7
542                 ba0123 = _mm_unpacklo_epi16(b.fVec, a.fVec),
543                 ba4567 = _mm_unpackhi_epi16(b.fVec, a.fVec);
544 
545         _mm_storeu_si128((__m128i*)ptr + 0, _mm_unpacklo_epi32(rg0123, ba0123));
546         _mm_storeu_si128((__m128i*)ptr + 1, _mm_unpackhi_epi32(rg0123, ba0123));
547         _mm_storeu_si128((__m128i*)ptr + 2, _mm_unpacklo_epi32(rg4567, ba4567));
548         _mm_storeu_si128((__m128i*)ptr + 3, _mm_unpackhi_epi32(rg4567, ba4567));
549     }
550 
551     AI SkNx operator + (const SkNx& o) const { return _mm_add_epi16(fVec, o.fVec); }
552     AI SkNx operator - (const SkNx& o) const { return _mm_sub_epi16(fVec, o.fVec); }
553     AI SkNx operator * (const SkNx& o) const { return _mm_mullo_epi16(fVec, o.fVec); }
554     AI SkNx operator & (const SkNx& o) const { return _mm_and_si128(fVec, o.fVec); }
555     AI SkNx operator | (const SkNx& o) const { return _mm_or_si128(fVec, o.fVec); }
556 
557     AI SkNx operator << (int bits) const { return _mm_slli_epi16(fVec, bits); }
558     AI SkNx operator >> (int bits) const { return _mm_srli_epi16(fVec, bits); }
559 
Min(const SkNx & a,const SkNx & b)560     AI static SkNx Min(const SkNx& a, const SkNx& b) {
561         // No unsigned _mm_min_epu16, so we'll shift into a space where we can use the
562         // signed version, _mm_min_epi16, then shift back.
563         const uint16_t top = 0x8000; // Keep this separate from _mm_set1_epi16 or MSVC will whine.
564         const __m128i top_8x = _mm_set1_epi16((short)top);
565         return _mm_add_epi8(top_8x, _mm_min_epi16(_mm_sub_epi8(a.fVec, top_8x),
566                                                   _mm_sub_epi8(b.fVec, top_8x)));
567     }
568 
mulHi(const SkNx & m)569     AI SkNx mulHi(const SkNx& m) const {
570         return _mm_mulhi_epu16(fVec, m.fVec);
571     }
572 
thenElse(const SkNx & t,const SkNx & e)573     AI SkNx thenElse(const SkNx& t, const SkNx& e) const {
574         return _mm_or_si128(_mm_and_si128   (fVec, t.fVec),
575                             _mm_andnot_si128(fVec, e.fVec));
576     }
577 
578     AI uint16_t operator[](int k) const {
579         SkASSERT(0 <= k && k < 8);
580         union { __m128i v; uint16_t us[8]; } pun = {fVec};
581         return pun.us[k&7];
582     }
583 
584     __m128i fVec;
585 };
586 
587 template <>
588 class SkNx<4, uint8_t> {
589 public:
SkNx()590     AI SkNx() {}
SkNx(const __m128i & vec)591     AI SkNx(const __m128i& vec) : fVec(vec) {}
SkNx(uint8_t a,uint8_t b,uint8_t c,uint8_t d)592     AI SkNx(uint8_t a, uint8_t b, uint8_t c, uint8_t d)
593         : fVec(_mm_setr_epi8((char)a,(char)b,(char)c,(char)d, 0,0,0,0, 0,0,0,0, 0,0,0,0)) {}
594 
Load(const void * ptr)595     AI static SkNx Load(const void* ptr) { return _mm_cvtsi32_si128(*(const int*)ptr); }
store(void * ptr)596     AI void store(void* ptr) const { *(int*)ptr = _mm_cvtsi128_si32(fVec); }
597 
598     AI uint8_t operator[](int k) const {
599         SkASSERT(0 <= k && k < 4);
600         union { __m128i v; uint8_t us[16]; } pun = {fVec};
601         return pun.us[k&3];
602     }
603 
604     // TODO as needed
605 
606     __m128i fVec;
607 };
608 
609 template <>
610 class SkNx<8, uint8_t> {
611 public:
SkNx(const __m128i & vec)612     AI SkNx(const __m128i& vec) : fVec(vec) {}
613 
SkNx()614     AI SkNx() {}
SkNx(uint8_t val)615     AI SkNx(uint8_t val) : fVec(_mm_set1_epi8((char)val)) {}
Load(const void * ptr)616     AI static SkNx Load(const void* ptr) { return _mm_loadl_epi64((const __m128i*)ptr); }
SkNx(uint8_t a,uint8_t b,uint8_t c,uint8_t d,uint8_t e,uint8_t f,uint8_t g,uint8_t h)617     AI SkNx(uint8_t a, uint8_t b, uint8_t c, uint8_t d,
618             uint8_t e, uint8_t f, uint8_t g, uint8_t h)
619             : fVec(_mm_setr_epi8((char)a,(char)b,(char)c,(char)d,
620                                  (char)e,(char)f,(char)g,(char)h,
621                                  0,0,0,0, 0,0,0,0)) {}
622 
store(void * ptr)623     AI void store(void* ptr) const {_mm_storel_epi64((__m128i*)ptr, fVec);}
624 
saturatedAdd(const SkNx & o)625     AI SkNx saturatedAdd(const SkNx& o) const { return _mm_adds_epu8(fVec, o.fVec); }
626 
627     AI SkNx operator + (const SkNx& o) const { return _mm_add_epi8(fVec, o.fVec); }
628     AI SkNx operator - (const SkNx& o) const { return _mm_sub_epi8(fVec, o.fVec); }
629 
Min(const SkNx & a,const SkNx & b)630     AI static SkNx Min(const SkNx& a, const SkNx& b) { return _mm_min_epu8(a.fVec, b.fVec); }
631     AI SkNx operator < (const SkNx& o) const {
632         // There's no unsigned _mm_cmplt_epu8, so we flip the sign bits then use a signed compare.
633         auto flip = _mm_set1_epi8(char(0x80));
634         return _mm_cmplt_epi8(_mm_xor_si128(flip, fVec), _mm_xor_si128(flip, o.fVec));
635     }
636 
637     AI uint8_t operator[](int k) const {
638         SkASSERT(0 <= k && k < 16);
639         union { __m128i v; uint8_t us[16]; } pun = {fVec};
640         return pun.us[k&15];
641     }
642 
thenElse(const SkNx & t,const SkNx & e)643     AI SkNx thenElse(const SkNx& t, const SkNx& e) const {
644         return _mm_or_si128(_mm_and_si128   (fVec, t.fVec),
645                             _mm_andnot_si128(fVec, e.fVec));
646     }
647 
648     __m128i fVec;
649 };
650 
651 template <>
652 class SkNx<16, uint8_t> {
653 public:
SkNx(const __m128i & vec)654     AI SkNx(const __m128i& vec) : fVec(vec) {}
655 
SkNx()656     AI SkNx() {}
SkNx(uint8_t val)657     AI SkNx(uint8_t val) : fVec(_mm_set1_epi8((char)val)) {}
Load(const void * ptr)658     AI static SkNx Load(const void* ptr) { return _mm_loadu_si128((const __m128i*)ptr); }
SkNx(uint8_t a,uint8_t b,uint8_t c,uint8_t d,uint8_t e,uint8_t f,uint8_t g,uint8_t h,uint8_t i,uint8_t j,uint8_t k,uint8_t l,uint8_t m,uint8_t n,uint8_t o,uint8_t p)659     AI SkNx(uint8_t a, uint8_t b, uint8_t c, uint8_t d,
660             uint8_t e, uint8_t f, uint8_t g, uint8_t h,
661             uint8_t i, uint8_t j, uint8_t k, uint8_t l,
662             uint8_t m, uint8_t n, uint8_t o, uint8_t p)
663         : fVec(_mm_setr_epi8((char)a,(char)b,(char)c,(char)d,
664                              (char)e,(char)f,(char)g,(char)h,
665                              (char)i,(char)j,(char)k,(char)l,
666                              (char)m,(char)n,(char)o,(char)p)) {}
667 
store(void * ptr)668     AI void store(void* ptr) const { _mm_storeu_si128((__m128i*)ptr, fVec); }
669 
saturatedAdd(const SkNx & o)670     AI SkNx saturatedAdd(const SkNx& o) const { return _mm_adds_epu8(fVec, o.fVec); }
671 
672     AI SkNx operator + (const SkNx& o) const { return _mm_add_epi8(fVec, o.fVec); }
673     AI SkNx operator - (const SkNx& o) const { return _mm_sub_epi8(fVec, o.fVec); }
674     AI SkNx operator & (const SkNx& o) const { return _mm_and_si128(fVec, o.fVec); }
675 
Min(const SkNx & a,const SkNx & b)676     AI static SkNx Min(const SkNx& a, const SkNx& b) { return _mm_min_epu8(a.fVec, b.fVec); }
677     AI SkNx operator < (const SkNx& o) const {
678         // There's no unsigned _mm_cmplt_epu8, so we flip the sign bits then use a signed compare.
679         auto flip = _mm_set1_epi8(char(0x80));
680         return _mm_cmplt_epi8(_mm_xor_si128(flip, fVec), _mm_xor_si128(flip, o.fVec));
681     }
682 
683     AI uint8_t operator[](int k) const {
684         SkASSERT(0 <= k && k < 16);
685         union { __m128i v; uint8_t us[16]; } pun = {fVec};
686         return pun.us[k&15];
687     }
688 
thenElse(const SkNx & t,const SkNx & e)689     AI SkNx thenElse(const SkNx& t, const SkNx& e) const {
690         return _mm_or_si128(_mm_and_si128   (fVec, t.fVec),
691                             _mm_andnot_si128(fVec, e.fVec));
692     }
693 
694     __m128i fVec;
695 };
696 
697 template<> AI /*static*/ Sk4f SkNx_cast<float, int32_t>(const Sk4i& src) {
698     return _mm_cvtepi32_ps(src.fVec);
699 }
700 
701 template<> AI /*static*/ Sk4f SkNx_cast<float, uint32_t>(const Sk4u& src) {
702     return SkNx_cast<float>(Sk4i::Load(&src));
703 }
704 
705 template <> AI /*static*/ Sk4i SkNx_cast<int32_t, float>(const Sk4f& src) {
706     return _mm_cvttps_epi32(src.fVec);
707 }
708 
709 template<> AI /*static*/ Sk4h SkNx_cast<uint16_t, int32_t>(const Sk4i& src) {
710 #if 0 && SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE41
711     // TODO: This seems to be causing code generation problems.   Investigate?
712     return _mm_packus_epi32(src.fVec);
713 #elif SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSSE3
714     // With SSSE3, we can just shuffle the low 2 bytes from each lane right into place.
715     const int _ = ~0;
716     return _mm_shuffle_epi8(src.fVec, _mm_setr_epi8(0,1, 4,5, 8,9, 12,13, _,_,_,_,_,_,_,_));
717 #else
718     // With SSE2, we have to sign extend our input, making _mm_packs_epi32 do the pack we want.
719     __m128i x = _mm_srai_epi32(_mm_slli_epi32(src.fVec, 16), 16);
720     return _mm_packs_epi32(x,x);
721 #endif
722 }
723 
724 template<> AI /*static*/ Sk4h SkNx_cast<uint16_t, float>(const Sk4f& src) {
725     return SkNx_cast<uint16_t>(SkNx_cast<int32_t>(src));
726 }
727 
728 template<> AI /*static*/ Sk4b SkNx_cast<uint8_t, float>(const Sk4f& src) {
729     auto _32 = _mm_cvttps_epi32(src.fVec);
730 #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSSE3
731     const int _ = ~0;
732     return _mm_shuffle_epi8(_32, _mm_setr_epi8(0,4,8,12, _,_,_,_, _,_,_,_, _,_,_,_));
733 #else
734     auto _16 = _mm_packus_epi16(_32, _32);
735     return     _mm_packus_epi16(_16, _16);
736 #endif
737 }
738 
739 template<> AI /*static*/ Sk4u SkNx_cast<uint32_t, uint8_t>(const Sk4b& src) {
740 #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSSE3
741     const int _ = ~0;
742     return _mm_shuffle_epi8(src.fVec, _mm_setr_epi8(0,_,_,_, 1,_,_,_, 2,_,_,_, 3,_,_,_));
743 #else
744     auto _16 = _mm_unpacklo_epi8(src.fVec, _mm_setzero_si128());
745     return _mm_unpacklo_epi16(_16, _mm_setzero_si128());
746 #endif
747 }
748 
749 template<> AI /*static*/ Sk4i SkNx_cast<int32_t, uint8_t>(const Sk4b& src) {
750     return SkNx_cast<uint32_t>(src).fVec;
751 }
752 
753 template<> AI /*static*/ Sk4f SkNx_cast<float, uint8_t>(const Sk4b& src) {
754     return _mm_cvtepi32_ps(SkNx_cast<int32_t>(src).fVec);
755 }
756 
757 template<> AI /*static*/ Sk4f SkNx_cast<float, uint16_t>(const Sk4h& src) {
758     auto _32 = _mm_unpacklo_epi16(src.fVec, _mm_setzero_si128());
759     return _mm_cvtepi32_ps(_32);
760 }
761 
762 template<> AI /*static*/ Sk8b SkNx_cast<uint8_t, int32_t>(const Sk8i& src) {
763     Sk4i lo, hi;
764     SkNx_split(src, &lo, &hi);
765 
766     auto t = _mm_packs_epi32(lo.fVec, hi.fVec);
767     return _mm_packus_epi16(t, t);
768 }
769 
770 template<> AI /*static*/ Sk16b SkNx_cast<uint8_t, float>(const Sk16f& src) {
771     Sk8f ab, cd;
772     SkNx_split(src, &ab, &cd);
773 
774     Sk4f a,b,c,d;
775     SkNx_split(ab, &a, &b);
776     SkNx_split(cd, &c, &d);
777 
778     return _mm_packus_epi16(_mm_packus_epi16(_mm_cvttps_epi32(a.fVec),
779                                              _mm_cvttps_epi32(b.fVec)),
780                             _mm_packus_epi16(_mm_cvttps_epi32(c.fVec),
781                                              _mm_cvttps_epi32(d.fVec)));
782 }
783 
784 template<> AI /*static*/ Sk4h SkNx_cast<uint16_t, uint8_t>(const Sk4b& src) {
785     return _mm_unpacklo_epi8(src.fVec, _mm_setzero_si128());
786 }
787 
788 template<> AI /*static*/ Sk8h SkNx_cast<uint16_t, uint8_t>(const Sk8b& src) {
789     return _mm_unpacklo_epi8(src.fVec, _mm_setzero_si128());
790 }
791 
792 template<> AI /*static*/ Sk4b SkNx_cast<uint8_t, uint16_t>(const Sk4h& src) {
793     return _mm_packus_epi16(src.fVec, src.fVec);
794 }
795 
796 template<> AI /*static*/ Sk8b SkNx_cast<uint8_t, uint16_t>(const Sk8h& src) {
797     return _mm_packus_epi16(src.fVec, src.fVec);
798 }
799 
800 template<> AI /*static*/ Sk4i SkNx_cast<int32_t, uint16_t>(const Sk4h& src) {
801     return _mm_unpacklo_epi16(src.fVec, _mm_setzero_si128());
802 }
803 
804 
805 template<> AI /*static*/ Sk4b SkNx_cast<uint8_t, int32_t>(const Sk4i& src) {
806     return _mm_packus_epi16(_mm_packus_epi16(src.fVec, src.fVec), src.fVec);
807 }
808 
809 template<> AI /*static*/ Sk4b SkNx_cast<uint8_t, uint32_t>(const Sk4u& src) {
810     return _mm_packus_epi16(_mm_packus_epi16(src.fVec, src.fVec), src.fVec);
811 }
812 
813 template<> AI /*static*/ Sk4i SkNx_cast<int32_t, uint32_t>(const Sk4u& src) {
814     return src.fVec;
815 }
816 
Sk4f_round(const Sk4f & x)817 AI static Sk4i Sk4f_round(const Sk4f& x) {
818     return _mm_cvtps_epi32(x.fVec);
819 }
820 
821 }  // namespace
822 
823 #endif//SkNx_sse_DEFINED
824