blob: 94b458e1742b08643f59dc0d5bc44fd7c1bd6da1 [file] [log] [blame]
mtkleinc9adb052015-03-30 10:50:27 -07001/*
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
Mike Klein8ea971b2018-06-12 12:11:28 -040011#include "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
mtklein244a6532016-04-19 14:21:30 -070020
mtkleinc9adb052015-03-30 10:50:27 -070021// This file may assume <= SSE2, but must check SK_CPU_SSE_LEVEL for anything more recent.
mtkleine4c0bee2016-02-09 10:35:27 -080022// If you do, make sure this is in a static inline function... anywhere else risks violating ODR.
mtkleinaa999cb2015-05-22 17:18:21 -070023
Mike Kleinc33d6142018-12-12 08:47:54 -050024namespace { // NOLINT(google-build-namespaces)
Mike Klein1e764642016-10-14 17:09:03 -040025
Chris Dalton89c5e882018-06-08 11:46:42 -060026// 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.
31AI 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
mtkleinc9adb052015-03-30 10:50:27 -070037template <>
mtklein6c221b42015-11-20 13:53:19 -080038class SkNx<2, float> {
mtkleinc9adb052015-03-30 10:50:27 -070039public:
Mike Klein7c78f3a2016-10-19 09:21:11 -040040 AI SkNx(const __m128& vec) : fVec(vec) {}
mtkleinc9adb052015-03-30 10:50:27 -070041
Mike Klein7c78f3a2016-10-19 09:21:11 -040042 AI SkNx() {}
43 AI SkNx(float val) : fVec(_mm_set1_ps(val)) {}
44 AI static SkNx Load(const void* ptr) {
mtklein507ef6d2016-01-31 08:02:47 -080045 return _mm_castsi128_ps(_mm_loadl_epi64((const __m128i*)ptr));
mtkleinc9adb052015-03-30 10:50:27 -070046 }
Mike Klein7c78f3a2016-10-19 09:21:11 -040047 AI SkNx(float a, float b) : fVec(_mm_setr_ps(a,b,0,0)) {}
mtkleinc9adb052015-03-30 10:50:27 -070048
Mike Klein7c78f3a2016-10-19 09:21:11 -040049 AI void store(void* ptr) const { _mm_storel_pi((__m64*)ptr, fVec); }
mtkleinc9adb052015-03-30 10:50:27 -070050
Chris Dalton21f64372018-04-11 14:01:04 -060051 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
Chris Dalton42f02aa2018-04-08 23:58:43 -060057 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
Chris Dalton0cb75872017-12-01 13:23:05 -070062 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
Chris Dalton6f8fa4e2018-02-06 17:55:30 -070069 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
Chris Dalton7732f4f2017-08-28 14:45:40 -060076 AI SkNx operator - () const { return _mm_xor_ps(_mm_set1_ps(-0.0f), fVec); }
77
Mike Klein7c78f3a2016-10-19 09:21:11 -040078 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); }
mtkleinc9adb052015-03-30 10:50:27 -070082
Mike Klein7c78f3a2016-10-19 09:21:11 -040083 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); }
mtkleinc9adb052015-03-30 10:50:27 -070089
Mike Klein7c78f3a2016-10-19 09:21:11 -040090 AI static SkNx Min(const SkNx& l, const SkNx& r) { return _mm_min_ps(l.fVec, r.fVec); }
91 AI static SkNx Max(const SkNx& l, const SkNx& r) { return _mm_max_ps(l.fVec, r.fVec); }
mtkleinc9adb052015-03-30 10:50:27 -070092
Chris Dalton89c5e882018-06-08 11:46:42 -060093 AI SkNx abs() const { return _mm_andnot_ps(_mm_set1_ps(-0.0f), fVec); }
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
Mike Klein7c78f3a2016-10-19 09:21:11 -0400102 AI SkNx sqrt() const { return _mm_sqrt_ps (fVec); }
103 AI SkNx rsqrt() const { return _mm_rsqrt_ps(fVec); }
104 AI SkNx invert() const { return _mm_rcp_ps(fVec); }
mtkleinc9adb052015-03-30 10:50:27 -0700105
Mike Klein7c78f3a2016-10-19 09:21:11 -0400106 AI float operator[](int k) const {
mtkleinc9adb052015-03-30 10:50:27 -0700107 SkASSERT(0 <= k && k < 2);
108 union { __m128 v; float fs[4]; } pun = {fVec};
mtkleina156a8f2015-04-03 06:16:13 -0700109 return pun.fs[k&1];
mtkleinc9adb052015-03-30 10:50:27 -0700110 }
111
Mike Klein7c78f3a2016-10-19 09:21:11 -0400112 AI bool allTrue() const { return 0xff == (_mm_movemask_epi8(_mm_castps_si128(fVec)) & 0xff); }
113 AI bool anyTrue() const { return 0x00 != (_mm_movemask_epi8(_mm_castps_si128(fVec)) & 0xff); }
mtkleinb5e86112015-06-24 15:18:39 -0700114
Chris Dalton7732f4f2017-08-28 14:45:40 -0600115 AI SkNx thenElse(const SkNx& t, const SkNx& e) const {
116 #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE41
117 return _mm_blendv_ps(e.fVec, t.fVec, fVec);
118 #else
119 return _mm_or_ps(_mm_and_ps (fVec, t.fVec),
120 _mm_andnot_ps(fVec, e.fVec));
121 #endif
122 }
123
mtkleinc9adb052015-03-30 10:50:27 -0700124 __m128 fVec;
125};
126
127template <>
mtklein6c221b42015-11-20 13:53:19 -0800128class SkNx<4, float> {
mtkleinc9adb052015-03-30 10:50:27 -0700129public:
Mike Klein7c78f3a2016-10-19 09:21:11 -0400130 AI SkNx(const __m128& vec) : fVec(vec) {}
mtkleinc9adb052015-03-30 10:50:27 -0700131
Mike Klein7c78f3a2016-10-19 09:21:11 -0400132 AI SkNx() {}
133 AI SkNx(float val) : fVec( _mm_set1_ps(val) ) {}
134 AI SkNx(float a, float b, float c, float d) : fVec(_mm_setr_ps(a,b,c,d)) {}
mtkleinc9adb052015-03-30 10:50:27 -0700135
Mike Klein7c78f3a2016-10-19 09:21:11 -0400136 AI static SkNx Load(const void* ptr) { return _mm_loadu_ps((const float*)ptr); }
137 AI void store(void* ptr) const { _mm_storeu_ps((float*)ptr, fVec); }
mtklein9db43ac2015-12-01 07:10:21 -0800138
Mike Klein213d8212017-11-30 12:07:20 -0500139 AI static void Load2(const void* ptr, SkNx* x, SkNx* y) {
140 SkNx lo = SkNx::Load((const float*)ptr+0),
141 hi = SkNx::Load((const float*)ptr+4);
142 *x = SkNx{lo[0], lo[2], hi[0], hi[2]};
143 *y = SkNx{lo[1], lo[3], hi[1], hi[3]};
144 }
145
Mike Klein7c78f3a2016-10-19 09:21:11 -0400146 AI static void Load4(const void* ptr, SkNx* r, SkNx* g, SkNx* b, SkNx* a) {
Mike Klein33cbfd72016-10-06 11:09:27 -0400147 __m128 v0 = _mm_loadu_ps(((float*)ptr) + 0),
148 v1 = _mm_loadu_ps(((float*)ptr) + 4),
149 v2 = _mm_loadu_ps(((float*)ptr) + 8),
150 v3 = _mm_loadu_ps(((float*)ptr) + 12);
151 _MM_TRANSPOSE4_PS(v0, v1, v2, v3);
152 *r = v0;
153 *g = v1;
154 *b = v2;
155 *a = v3;
156 }
Mike Klein7c78f3a2016-10-19 09:21:11 -0400157 AI static void Store4(void* dst, const SkNx& r, const SkNx& g, const SkNx& b, const SkNx& a) {
Mike Klein33cbfd72016-10-06 11:09:27 -0400158 __m128 v0 = r.fVec,
159 v1 = g.fVec,
160 v2 = b.fVec,
161 v3 = a.fVec;
162 _MM_TRANSPOSE4_PS(v0, v1, v2, v3);
163 _mm_storeu_ps(((float*) dst) + 0, v0);
164 _mm_storeu_ps(((float*) dst) + 4, v1);
165 _mm_storeu_ps(((float*) dst) + 8, v2);
166 _mm_storeu_ps(((float*) dst) + 12, v3);
167 }
168
Chris Dalton7732f4f2017-08-28 14:45:40 -0600169 AI SkNx operator - () const { return _mm_xor_ps(_mm_set1_ps(-0.0f), fVec); }
170
Mike Klein7c78f3a2016-10-19 09:21:11 -0400171 AI SkNx operator + (const SkNx& o) const { return _mm_add_ps(fVec, o.fVec); }
172 AI SkNx operator - (const SkNx& o) const { return _mm_sub_ps(fVec, o.fVec); }
173 AI SkNx operator * (const SkNx& o) const { return _mm_mul_ps(fVec, o.fVec); }
174 AI SkNx operator / (const SkNx& o) const { return _mm_div_ps(fVec, o.fVec); }
mtkleinc9adb052015-03-30 10:50:27 -0700175
Mike Klein7c78f3a2016-10-19 09:21:11 -0400176 AI SkNx operator == (const SkNx& o) const { return _mm_cmpeq_ps (fVec, o.fVec); }
177 AI SkNx operator != (const SkNx& o) const { return _mm_cmpneq_ps(fVec, o.fVec); }
178 AI SkNx operator < (const SkNx& o) const { return _mm_cmplt_ps (fVec, o.fVec); }
179 AI SkNx operator > (const SkNx& o) const { return _mm_cmpgt_ps (fVec, o.fVec); }
180 AI SkNx operator <= (const SkNx& o) const { return _mm_cmple_ps (fVec, o.fVec); }
181 AI SkNx operator >= (const SkNx& o) const { return _mm_cmpge_ps (fVec, o.fVec); }
mtkleinc9adb052015-03-30 10:50:27 -0700182
Mike Klein7c78f3a2016-10-19 09:21:11 -0400183 AI static SkNx Min(const SkNx& l, const SkNx& r) { return _mm_min_ps(l.fVec, r.fVec); }
184 AI static SkNx Max(const SkNx& l, const SkNx& r) { return _mm_max_ps(l.fVec, r.fVec); }
mtkleinc9adb052015-03-30 10:50:27 -0700185
Mike Klein7c78f3a2016-10-19 09:21:11 -0400186 AI SkNx abs() const { return _mm_andnot_ps(_mm_set1_ps(-0.0f), fVec); }
187 AI SkNx floor() const {
mtklein5608e2e2016-07-11 09:59:21 -0700188 #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE41
189 return _mm_floor_ps(fVec);
190 #else
Chris Dalton89c5e882018-06-08 11:46:42 -0600191 return emulate_mm_floor_ps(fVec);
mtklein5608e2e2016-07-11 09:59:21 -0700192 #endif
mtklein244a6532016-04-19 14:21:30 -0700193 }
mtkleinc33065a2016-01-15 12:16:40 -0800194
Mike Klein7c78f3a2016-10-19 09:21:11 -0400195 AI SkNx sqrt() const { return _mm_sqrt_ps (fVec); }
196 AI SkNx rsqrt() const { return _mm_rsqrt_ps(fVec); }
197 AI SkNx invert() const { return _mm_rcp_ps(fVec); }
mtkleinc9adb052015-03-30 10:50:27 -0700198
Mike Klein7c78f3a2016-10-19 09:21:11 -0400199 AI float operator[](int k) const {
mtkleinc9adb052015-03-30 10:50:27 -0700200 SkASSERT(0 <= k && k < 4);
201 union { __m128 v; float fs[4]; } pun = {fVec};
mtkleina156a8f2015-04-03 06:16:13 -0700202 return pun.fs[k&3];
mtkleinc9adb052015-03-30 10:50:27 -0700203 }
204
Chris Daltone3fda932018-04-11 13:18:09 -0600205 AI float min() const {
206 SkNx min = Min(*this, _mm_shuffle_ps(fVec, fVec, _MM_SHUFFLE(2,3,0,1)));
207 min = Min(min, _mm_shuffle_ps(min.fVec, min.fVec, _MM_SHUFFLE(0,1,2,3)));
208 return min[0];
209 }
210
211 AI float max() const {
212 SkNx max = Max(*this, _mm_shuffle_ps(fVec, fVec, _MM_SHUFFLE(2,3,0,1)));
213 max = Max(max, _mm_shuffle_ps(max.fVec, max.fVec, _MM_SHUFFLE(0,1,2,3)));
214 return max[0];
215 }
216
Mike Klein7c78f3a2016-10-19 09:21:11 -0400217 AI bool allTrue() const { return 0xffff == _mm_movemask_epi8(_mm_castps_si128(fVec)); }
218 AI bool anyTrue() const { return 0x0000 != _mm_movemask_epi8(_mm_castps_si128(fVec)); }
mtkleinb5e86112015-06-24 15:18:39 -0700219
Mike Klein7c78f3a2016-10-19 09:21:11 -0400220 AI SkNx thenElse(const SkNx& t, const SkNx& e) const {
mtklein5608e2e2016-07-11 09:59:21 -0700221 #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE41
msarettd2809572016-06-20 06:07:45 -0700222 return _mm_blendv_ps(e.fVec, t.fVec, fVec);
mtklein5608e2e2016-07-11 09:59:21 -0700223 #else
mtklein2aab22a2015-06-26 10:46:31 -0700224 return _mm_or_ps(_mm_and_ps (fVec, t.fVec),
225 _mm_andnot_ps(fVec, e.fVec));
mtklein5608e2e2016-07-11 09:59:21 -0700226 #endif
mtklein2aab22a2015-06-26 10:46:31 -0700227 }
228
mtkleinc9adb052015-03-30 10:50:27 -0700229 __m128 fVec;
230};
231
Herb Derby0f96bb32017-09-13 16:46:05 -0400232AI static __m128i mullo32(__m128i a, __m128i b) {
233#if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE41
234 return _mm_mullo_epi32(a, b);
235#else
236 __m128i mul20 = _mm_mul_epu32(a, b),
237 mul31 = _mm_mul_epu32(_mm_srli_si128(a, 4), _mm_srli_si128(b, 4));
238 return _mm_unpacklo_epi32(_mm_shuffle_epi32(mul20, _MM_SHUFFLE(0,0,2,0)),
239 _mm_shuffle_epi32(mul31, _MM_SHUFFLE(0,0,2,0)));
240#endif
241}
242
mtklein115acee2015-04-14 14:02:52 -0700243template <>
mtkleind05a8752016-07-29 10:10:15 -0700244class SkNx<4, int32_t> {
mtklein8273ca42016-02-09 11:32:51 -0800245public:
Mike Klein7c78f3a2016-10-19 09:21:11 -0400246 AI SkNx(const __m128i& vec) : fVec(vec) {}
mtklein8273ca42016-02-09 11:32:51 -0800247
Mike Klein7c78f3a2016-10-19 09:21:11 -0400248 AI SkNx() {}
249 AI SkNx(int32_t val) : fVec(_mm_set1_epi32(val)) {}
250 AI static SkNx Load(const void* ptr) { return _mm_loadu_si128((const __m128i*)ptr); }
251 AI SkNx(int32_t a, int32_t b, int32_t c, int32_t d) : fVec(_mm_setr_epi32(a,b,c,d)) {}
mtklein8273ca42016-02-09 11:32:51 -0800252
Mike Klein7c78f3a2016-10-19 09:21:11 -0400253 AI void store(void* ptr) const { _mm_storeu_si128((__m128i*)ptr, fVec); }
mtklein8273ca42016-02-09 11:32:51 -0800254
Mike Klein7c78f3a2016-10-19 09:21:11 -0400255 AI SkNx operator + (const SkNx& o) const { return _mm_add_epi32(fVec, o.fVec); }
256 AI SkNx operator - (const SkNx& o) const { return _mm_sub_epi32(fVec, o.fVec); }
Herb Derby0f96bb32017-09-13 16:46:05 -0400257 AI SkNx operator * (const SkNx& o) const { return mullo32(fVec, o.fVec); }
mtklein8273ca42016-02-09 11:32:51 -0800258
Mike Klein7c78f3a2016-10-19 09:21:11 -0400259 AI SkNx operator & (const SkNx& o) const { return _mm_and_si128(fVec, o.fVec); }
Herb Derby0f96bb32017-09-13 16:46:05 -0400260 AI SkNx operator | (const SkNx& o) const { return _mm_or_si128(fVec, o.fVec); }
Mike Klein7c78f3a2016-10-19 09:21:11 -0400261 AI SkNx operator ^ (const SkNx& o) const { return _mm_xor_si128(fVec, o.fVec); }
mtklein64f061a2016-06-17 12:09:16 -0700262
Mike Klein7c78f3a2016-10-19 09:21:11 -0400263 AI SkNx operator << (int bits) const { return _mm_slli_epi32(fVec, bits); }
264 AI SkNx operator >> (int bits) const { return _mm_srai_epi32(fVec, bits); }
mtklein8273ca42016-02-09 11:32:51 -0800265
Mike Klein7c78f3a2016-10-19 09:21:11 -0400266 AI SkNx operator == (const SkNx& o) const { return _mm_cmpeq_epi32 (fVec, o.fVec); }
267 AI SkNx operator < (const SkNx& o) const { return _mm_cmplt_epi32 (fVec, o.fVec); }
268 AI SkNx operator > (const SkNx& o) const { return _mm_cmpgt_epi32 (fVec, o.fVec); }
mtklein58e389b2016-07-15 07:00:11 -0700269
Mike Klein7c78f3a2016-10-19 09:21:11 -0400270 AI int32_t operator[](int k) const {
mtklein8273ca42016-02-09 11:32:51 -0800271 SkASSERT(0 <= k && k < 4);
mtkleind05a8752016-07-29 10:10:15 -0700272 union { __m128i v; int32_t is[4]; } pun = {fVec};
mtklein8273ca42016-02-09 11:32:51 -0800273 return pun.is[k&3];
274 }
mtklein8273ca42016-02-09 11:32:51 -0800275
Mike Klein7c78f3a2016-10-19 09:21:11 -0400276 AI SkNx thenElse(const SkNx& t, const SkNx& e) const {
mtklein58e389b2016-07-15 07:00:11 -0700277 #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE41
278 return _mm_blendv_epi8(e.fVec, t.fVec, fVec);
279 #else
280 return _mm_or_si128(_mm_and_si128 (fVec, t.fVec),
281 _mm_andnot_si128(fVec, e.fVec));
282 #endif
283 }
284
Yuqian Li7da6ba22017-07-12 13:36:05 -0400285 AI SkNx abs() const {
286#if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSSE3
287 return _mm_abs_epi32(fVec);
288#else
289 SkNx mask = (*this) >> 31;
290 return (mask ^ (*this)) - mask;
291#endif
292 }
293
294 AI static SkNx Min(const SkNx& x, const SkNx& y) {
295#if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE41
296 return _mm_min_epi32(x.fVec, y.fVec);
297#else
Yuqian Lie94865e2017-07-14 10:17:04 -0400298 return (x < y).thenElse(x, y);
Yuqian Li7da6ba22017-07-12 13:36:05 -0400299#endif
300 }
301
302 AI static SkNx Max(const SkNx& x, const SkNx& y) {
303#if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE41
304 return _mm_max_epi32(x.fVec, y.fVec);
305#else
Yuqian Lie94865e2017-07-14 10:17:04 -0400306 return (x > y).thenElse(x, y);
Yuqian Li7da6ba22017-07-12 13:36:05 -0400307#endif
308 }
309
mtklein8273ca42016-02-09 11:32:51 -0800310 __m128i fVec;
311};
312
313template <>
Mike Reedd92a7392018-03-20 12:42:38 -0400314class SkNx<2, uint32_t> {
315public:
316 AI SkNx(const __m128i& vec) : fVec(vec) {}
317
318 AI SkNx() {}
319 AI SkNx(uint32_t val) : fVec(_mm_set1_epi32(val)) {}
320 AI static SkNx Load(const void* ptr) { return _mm_loadl_epi64((const __m128i*)ptr); }
321 AI SkNx(uint32_t a, uint32_t b) : fVec(_mm_setr_epi32(a,b,0,0)) {}
322
323 AI void store(void* ptr) const { _mm_storel_epi64((__m128i*)ptr, fVec); }
324
325 AI SkNx operator + (const SkNx& o) const { return _mm_add_epi32(fVec, o.fVec); }
326 AI SkNx operator - (const SkNx& o) const { return _mm_sub_epi32(fVec, o.fVec); }
327 AI SkNx operator * (const SkNx& o) const { return mullo32(fVec, o.fVec); }
328
329 AI SkNx operator & (const SkNx& o) const { return _mm_and_si128(fVec, o.fVec); }
330 AI SkNx operator | (const SkNx& o) const { return _mm_or_si128(fVec, o.fVec); }
331 AI SkNx operator ^ (const SkNx& o) const { return _mm_xor_si128(fVec, o.fVec); }
332
333 AI SkNx operator << (int bits) const { return _mm_slli_epi32(fVec, bits); }
334 AI SkNx operator >> (int bits) const { return _mm_srli_epi32(fVec, bits); }
335
336 AI SkNx operator == (const SkNx& o) const { return _mm_cmpeq_epi32 (fVec, o.fVec); }
337 AI SkNx operator != (const SkNx& o) const { return (*this == o) ^ 0xffffffff; }
338 // operator < and > take a little extra fiddling to make work for unsigned ints.
339
340 AI uint32_t operator[](int k) const {
341 SkASSERT(0 <= k && k < 2);
342 union { __m128i v; uint32_t us[4]; } pun = {fVec};
343 return pun.us[k&1];
344 }
345
346 AI SkNx thenElse(const SkNx& t, const SkNx& e) const {
347#if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE41
348 return _mm_blendv_epi8(e.fVec, t.fVec, fVec);
349#else
350 return _mm_or_si128(_mm_and_si128 (fVec, t.fVec),
351 _mm_andnot_si128(fVec, e.fVec));
352#endif
353 }
354
355 AI bool allTrue() const { return 0xff == (_mm_movemask_epi8(fVec) & 0xff); }
356
357 __m128i fVec;
358};
359
360template <>
mtkleind05a8752016-07-29 10:10:15 -0700361class SkNx<4, uint32_t> {
362public:
Mike Klein7c78f3a2016-10-19 09:21:11 -0400363 AI SkNx(const __m128i& vec) : fVec(vec) {}
mtkleind05a8752016-07-29 10:10:15 -0700364
Mike Klein7c78f3a2016-10-19 09:21:11 -0400365 AI SkNx() {}
366 AI SkNx(uint32_t val) : fVec(_mm_set1_epi32(val)) {}
367 AI static SkNx Load(const void* ptr) { return _mm_loadu_si128((const __m128i*)ptr); }
368 AI SkNx(uint32_t a, uint32_t b, uint32_t c, uint32_t d) : fVec(_mm_setr_epi32(a,b,c,d)) {}
mtkleind05a8752016-07-29 10:10:15 -0700369
Mike Klein7c78f3a2016-10-19 09:21:11 -0400370 AI void store(void* ptr) const { _mm_storeu_si128((__m128i*)ptr, fVec); }
mtkleind05a8752016-07-29 10:10:15 -0700371
Mike Klein7c78f3a2016-10-19 09:21:11 -0400372 AI SkNx operator + (const SkNx& o) const { return _mm_add_epi32(fVec, o.fVec); }
373 AI SkNx operator - (const SkNx& o) const { return _mm_sub_epi32(fVec, o.fVec); }
Herb Derby0f96bb32017-09-13 16:46:05 -0400374 AI SkNx operator * (const SkNx& o) const { return mullo32(fVec, o.fVec); }
mtkleind05a8752016-07-29 10:10:15 -0700375
Mike Klein7c78f3a2016-10-19 09:21:11 -0400376 AI SkNx operator & (const SkNx& o) const { return _mm_and_si128(fVec, o.fVec); }
Herb Derby0f96bb32017-09-13 16:46:05 -0400377 AI SkNx operator | (const SkNx& o) const { return _mm_or_si128(fVec, o.fVec); }
Mike Klein7c78f3a2016-10-19 09:21:11 -0400378 AI SkNx operator ^ (const SkNx& o) const { return _mm_xor_si128(fVec, o.fVec); }
mtkleind05a8752016-07-29 10:10:15 -0700379
Mike Klein7c78f3a2016-10-19 09:21:11 -0400380 AI SkNx operator << (int bits) const { return _mm_slli_epi32(fVec, bits); }
381 AI SkNx operator >> (int bits) const { return _mm_srli_epi32(fVec, bits); }
mtkleind05a8752016-07-29 10:10:15 -0700382
Mike Klein7c78f3a2016-10-19 09:21:11 -0400383 AI SkNx operator == (const SkNx& o) const { return _mm_cmpeq_epi32 (fVec, o.fVec); }
Mike Reedd92a7392018-03-20 12:42:38 -0400384 AI SkNx operator != (const SkNx& o) const { return (*this == o) ^ 0xffffffff; }
385
mtkleind05a8752016-07-29 10:10:15 -0700386 // operator < and > take a little extra fiddling to make work for unsigned ints.
387
Mike Klein7c78f3a2016-10-19 09:21:11 -0400388 AI uint32_t operator[](int k) const {
mtkleind05a8752016-07-29 10:10:15 -0700389 SkASSERT(0 <= k && k < 4);
390 union { __m128i v; uint32_t us[4]; } pun = {fVec};
391 return pun.us[k&3];
392 }
393
Mike Klein7c78f3a2016-10-19 09:21:11 -0400394 AI SkNx thenElse(const SkNx& t, const SkNx& e) const {
mtkleind05a8752016-07-29 10:10:15 -0700395 #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE41
396 return _mm_blendv_epi8(e.fVec, t.fVec, fVec);
397 #else
398 return _mm_or_si128(_mm_and_si128 (fVec, t.fVec),
399 _mm_andnot_si128(fVec, e.fVec));
400 #endif
401 }
402
Herb Derby5eb15282017-10-10 17:14:18 -0400403 AI SkNx mulHi(SkNx m) const {
404 SkNx v20{_mm_mul_epu32(m.fVec, fVec)};
405 SkNx v31{_mm_mul_epu32(_mm_srli_si128(m.fVec, 4), _mm_srli_si128(fVec, 4))};
406
407 return SkNx{v20[1], v31[1], v20[3], v31[3]};
408 }
409
mtkleind05a8752016-07-29 10:10:15 -0700410 __m128i fVec;
411};
412
mtkleind05a8752016-07-29 10:10:15 -0700413template <>
mtklein6c221b42015-11-20 13:53:19 -0800414class SkNx<4, uint16_t> {
mtklein115acee2015-04-14 14:02:52 -0700415public:
Mike Klein7c78f3a2016-10-19 09:21:11 -0400416 AI SkNx(const __m128i& vec) : fVec(vec) {}
mtklein115acee2015-04-14 14:02:52 -0700417
Mike Klein7c78f3a2016-10-19 09:21:11 -0400418 AI SkNx() {}
419 AI SkNx(uint16_t val) : fVec(_mm_set1_epi16(val)) {}
420 AI SkNx(uint16_t a, uint16_t b, uint16_t c, uint16_t d)
421 : fVec(_mm_setr_epi16(a,b,c,d,0,0,0,0)) {}
mtklein115acee2015-04-14 14:02:52 -0700422
Mike Klein7c78f3a2016-10-19 09:21:11 -0400423 AI static SkNx Load(const void* ptr) { return _mm_loadl_epi64((const __m128i*)ptr); }
424 AI void store(void* ptr) const { _mm_storel_epi64((__m128i*)ptr, fVec); }
mtklein115acee2015-04-14 14:02:52 -0700425
Mike Klein7c78f3a2016-10-19 09:21:11 -0400426 AI static void Load4(const void* ptr, SkNx* r, SkNx* g, SkNx* b, SkNx* a) {
Mike Klein33cbfd72016-10-06 11:09:27 -0400427 __m128i lo = _mm_loadu_si128(((__m128i*)ptr) + 0),
428 hi = _mm_loadu_si128(((__m128i*)ptr) + 1);
429 __m128i even = _mm_unpacklo_epi16(lo, hi), // r0 r2 g0 g2 b0 b2 a0 a2
430 odd = _mm_unpackhi_epi16(lo, hi); // r1 r3 ...
431 __m128i rg = _mm_unpacklo_epi16(even, odd), // r0 r1 r2 r3 g0 g1 g2 g3
432 ba = _mm_unpackhi_epi16(even, odd); // b0 b1 ... a0 a1 ...
433 *r = rg;
434 *g = _mm_srli_si128(rg, 8);
435 *b = ba;
436 *a = _mm_srli_si128(ba, 8);
437 }
Matt Sarett5bee0b62017-01-19 12:04:32 -0500438 AI static void Load3(const void* ptr, SkNx* r, SkNx* g, SkNx* b) {
439 // The idea here is to get 4 vectors that are R G B _ _ _ _ _.
440 // The second load is at a funny location to make sure we don't read past
441 // the bounds of memory. This is fine, we just need to shift it a little bit.
442 const uint8_t* ptr8 = (const uint8_t*) ptr;
443 __m128i rgb0 = _mm_loadu_si128((const __m128i*) (ptr8 + 0));
444 __m128i rgb1 = _mm_srli_si128(rgb0, 3*2);
445 __m128i rgb2 = _mm_srli_si128(_mm_loadu_si128((const __m128i*) (ptr8 + 4*2)), 2*2);
446 __m128i rgb3 = _mm_srli_si128(rgb2, 3*2);
447
448 __m128i rrggbb01 = _mm_unpacklo_epi16(rgb0, rgb1);
449 __m128i rrggbb23 = _mm_unpacklo_epi16(rgb2, rgb3);
450 *r = _mm_unpacklo_epi32(rrggbb01, rrggbb23);
451 *g = _mm_srli_si128(r->fVec, 4*2);
452 *b = _mm_unpackhi_epi32(rrggbb01, rrggbb23);
453 }
Mike Klein7c78f3a2016-10-19 09:21:11 -0400454 AI static void Store4(void* dst, const SkNx& r, const SkNx& g, const SkNx& b, const SkNx& a) {
Mike Klein33cbfd72016-10-06 11:09:27 -0400455 __m128i rg = _mm_unpacklo_epi16(r.fVec, g.fVec);
456 __m128i ba = _mm_unpacklo_epi16(b.fVec, a.fVec);
457 __m128i lo = _mm_unpacklo_epi32(rg, ba);
458 __m128i hi = _mm_unpackhi_epi32(rg, ba);
459 _mm_storeu_si128(((__m128i*) dst) + 0, lo);
460 _mm_storeu_si128(((__m128i*) dst) + 1, hi);
461 }
462
Mike Klein7c78f3a2016-10-19 09:21:11 -0400463 AI SkNx operator + (const SkNx& o) const { return _mm_add_epi16(fVec, o.fVec); }
464 AI SkNx operator - (const SkNx& o) const { return _mm_sub_epi16(fVec, o.fVec); }
465 AI SkNx operator * (const SkNx& o) const { return _mm_mullo_epi16(fVec, o.fVec); }
Matt Sarett379938e2017-01-12 18:34:29 -0500466 AI SkNx operator & (const SkNx& o) const { return _mm_and_si128(fVec, o.fVec); }
467 AI SkNx operator | (const SkNx& o) const { return _mm_or_si128(fVec, o.fVec); }
mtklein115acee2015-04-14 14:02:52 -0700468
Mike Klein7c78f3a2016-10-19 09:21:11 -0400469 AI SkNx operator << (int bits) const { return _mm_slli_epi16(fVec, bits); }
470 AI SkNx operator >> (int bits) const { return _mm_srli_epi16(fVec, bits); }
mtklein115acee2015-04-14 14:02:52 -0700471
Mike Klein7c78f3a2016-10-19 09:21:11 -0400472 AI uint16_t operator[](int k) const {
mtklein115acee2015-04-14 14:02:52 -0700473 SkASSERT(0 <= k && k < 4);
mtkleine4c0bee2016-02-09 10:35:27 -0800474 union { __m128i v; uint16_t us[8]; } pun = {fVec};
475 return pun.us[k&3];
mtklein115acee2015-04-14 14:02:52 -0700476 }
mtkleind2ffd362015-05-12 06:11:21 -0700477
mtklein115acee2015-04-14 14:02:52 -0700478 __m128i fVec;
479};
480
481template <>
mtklein6c221b42015-11-20 13:53:19 -0800482class SkNx<8, uint16_t> {
mtklein115acee2015-04-14 14:02:52 -0700483public:
Mike Klein7c78f3a2016-10-19 09:21:11 -0400484 AI SkNx(const __m128i& vec) : fVec(vec) {}
mtklein115acee2015-04-14 14:02:52 -0700485
Mike Klein7c78f3a2016-10-19 09:21:11 -0400486 AI SkNx() {}
487 AI SkNx(uint16_t val) : fVec(_mm_set1_epi16(val)) {}
488 AI SkNx(uint16_t a, uint16_t b, uint16_t c, uint16_t d,
489 uint16_t e, uint16_t f, uint16_t g, uint16_t h)
490 : fVec(_mm_setr_epi16(a,b,c,d,e,f,g,h)) {}
mtklein115acee2015-04-14 14:02:52 -0700491
Mike Klein7c78f3a2016-10-19 09:21:11 -0400492 AI static SkNx Load(const void* ptr) { return _mm_loadu_si128((const __m128i*)ptr); }
493 AI void store(void* ptr) const { _mm_storeu_si128((__m128i*)ptr, fVec); }
mtklein115acee2015-04-14 14:02:52 -0700494
Mike Klein7c78f3a2016-10-19 09:21:11 -0400495 AI static void Load4(const void* ptr, SkNx* r, SkNx* g, SkNx* b, SkNx* a) {
Mike Klein04adfda2016-10-12 09:52:55 -0400496 __m128i _01 = _mm_loadu_si128(((__m128i*)ptr) + 0),
497 _23 = _mm_loadu_si128(((__m128i*)ptr) + 1),
498 _45 = _mm_loadu_si128(((__m128i*)ptr) + 2),
499 _67 = _mm_loadu_si128(((__m128i*)ptr) + 3);
500
501 __m128i _02 = _mm_unpacklo_epi16(_01, _23), // r0 r2 g0 g2 b0 b2 a0 a2
502 _13 = _mm_unpackhi_epi16(_01, _23), // r1 r3 g1 g3 b1 b3 a1 a3
503 _46 = _mm_unpacklo_epi16(_45, _67),
504 _57 = _mm_unpackhi_epi16(_45, _67);
505
506 __m128i rg0123 = _mm_unpacklo_epi16(_02, _13), // r0 r1 r2 r3 g0 g1 g2 g3
507 ba0123 = _mm_unpackhi_epi16(_02, _13), // b0 b1 b2 b3 a0 a1 a2 a3
508 rg4567 = _mm_unpacklo_epi16(_46, _57),
509 ba4567 = _mm_unpackhi_epi16(_46, _57);
510
511 *r = _mm_unpacklo_epi64(rg0123, rg4567);
512 *g = _mm_unpackhi_epi64(rg0123, rg4567);
513 *b = _mm_unpacklo_epi64(ba0123, ba4567);
514 *a = _mm_unpackhi_epi64(ba0123, ba4567);
515 }
Matt Sarett5bee0b62017-01-19 12:04:32 -0500516 AI static void Load3(const void* ptr, SkNx* r, SkNx* g, SkNx* b) {
Matt Sarett5bee0b62017-01-19 12:04:32 -0500517 const uint8_t* ptr8 = (const uint8_t*) ptr;
518 __m128i rgb0 = _mm_loadu_si128((const __m128i*) (ptr8 + 0*2));
519 __m128i rgb1 = _mm_srli_si128(rgb0, 3*2);
520 __m128i rgb2 = _mm_loadu_si128((const __m128i*) (ptr8 + 6*2));
521 __m128i rgb3 = _mm_srli_si128(rgb2, 3*2);
522 __m128i rgb4 = _mm_loadu_si128((const __m128i*) (ptr8 + 12*2));
523 __m128i rgb5 = _mm_srli_si128(rgb4, 3*2);
524 __m128i rgb6 = _mm_srli_si128(_mm_loadu_si128((const __m128i*) (ptr8 + 16*2)), 2*2);
525 __m128i rgb7 = _mm_srli_si128(rgb6, 3*2);
526
527 __m128i rgb01 = _mm_unpacklo_epi16(rgb0, rgb1);
528 __m128i rgb23 = _mm_unpacklo_epi16(rgb2, rgb3);
529 __m128i rgb45 = _mm_unpacklo_epi16(rgb4, rgb5);
530 __m128i rgb67 = _mm_unpacklo_epi16(rgb6, rgb7);
531
532 __m128i rg03 = _mm_unpacklo_epi32(rgb01, rgb23);
533 __m128i bx03 = _mm_unpackhi_epi32(rgb01, rgb23);
534 __m128i rg47 = _mm_unpacklo_epi32(rgb45, rgb67);
535 __m128i bx47 = _mm_unpackhi_epi32(rgb45, rgb67);
536
537 *r = _mm_unpacklo_epi64(rg03, rg47);
538 *g = _mm_unpackhi_epi64(rg03, rg47);
539 *b = _mm_unpacklo_epi64(bx03, bx47);
540 }
Mike Klein7c78f3a2016-10-19 09:21:11 -0400541 AI static void Store4(void* ptr, const SkNx& r, const SkNx& g, const SkNx& b, const SkNx& a) {
Mike Klein04adfda2016-10-12 09:52:55 -0400542 __m128i rg0123 = _mm_unpacklo_epi16(r.fVec, g.fVec), // r0 g0 r1 g1 r2 g2 r3 g3
543 rg4567 = _mm_unpackhi_epi16(r.fVec, g.fVec), // r4 g4 r5 g5 r6 g6 r7 g7
544 ba0123 = _mm_unpacklo_epi16(b.fVec, a.fVec),
545 ba4567 = _mm_unpackhi_epi16(b.fVec, a.fVec);
546
547 _mm_storeu_si128((__m128i*)ptr + 0, _mm_unpacklo_epi32(rg0123, ba0123));
548 _mm_storeu_si128((__m128i*)ptr + 1, _mm_unpackhi_epi32(rg0123, ba0123));
549 _mm_storeu_si128((__m128i*)ptr + 2, _mm_unpacklo_epi32(rg4567, ba4567));
550 _mm_storeu_si128((__m128i*)ptr + 3, _mm_unpackhi_epi32(rg4567, ba4567));
551 }
552
Mike Klein7c78f3a2016-10-19 09:21:11 -0400553 AI SkNx operator + (const SkNx& o) const { return _mm_add_epi16(fVec, o.fVec); }
554 AI SkNx operator - (const SkNx& o) const { return _mm_sub_epi16(fVec, o.fVec); }
555 AI SkNx operator * (const SkNx& o) const { return _mm_mullo_epi16(fVec, o.fVec); }
Matt Sarett379938e2017-01-12 18:34:29 -0500556 AI SkNx operator & (const SkNx& o) const { return _mm_and_si128(fVec, o.fVec); }
557 AI SkNx operator | (const SkNx& o) const { return _mm_or_si128(fVec, o.fVec); }
mtklein115acee2015-04-14 14:02:52 -0700558
Mike Klein7c78f3a2016-10-19 09:21:11 -0400559 AI SkNx operator << (int bits) const { return _mm_slli_epi16(fVec, bits); }
560 AI SkNx operator >> (int bits) const { return _mm_srli_epi16(fVec, bits); }
mtklein115acee2015-04-14 14:02:52 -0700561
Mike Klein7c78f3a2016-10-19 09:21:11 -0400562 AI static SkNx Min(const SkNx& a, const SkNx& b) {
mtklein27e517a2015-05-14 17:53:04 -0700563 // No unsigned _mm_min_epu16, so we'll shift into a space where we can use the
564 // signed version, _mm_min_epi16, then shift back.
565 const uint16_t top = 0x8000; // Keep this separate from _mm_set1_epi16 or MSVC will whine.
566 const __m128i top_8x = _mm_set1_epi16(top);
567 return _mm_add_epi8(top_8x, _mm_min_epi16(_mm_sub_epi8(a.fVec, top_8x),
568 _mm_sub_epi8(b.fVec, top_8x)));
569 }
570
Herb Derbyd1b3c782017-11-02 13:18:38 -0400571 AI SkNx mulHi(const SkNx& m) const {
572 return _mm_mulhi_epu16(fVec, m.fVec);
573 }
574
Mike Klein7c78f3a2016-10-19 09:21:11 -0400575 AI SkNx thenElse(const SkNx& t, const SkNx& e) const {
mtklein4be181e2015-07-14 10:54:19 -0700576 return _mm_or_si128(_mm_and_si128 (fVec, t.fVec),
577 _mm_andnot_si128(fVec, e.fVec));
578 }
579
Mike Klein7c78f3a2016-10-19 09:21:11 -0400580 AI uint16_t operator[](int k) const {
mtklein115acee2015-04-14 14:02:52 -0700581 SkASSERT(0 <= k && k < 8);
mtkleine4c0bee2016-02-09 10:35:27 -0800582 union { __m128i v; uint16_t us[8]; } pun = {fVec};
583 return pun.us[k&7];
mtklein115acee2015-04-14 14:02:52 -0700584 }
mtkleind2ffd362015-05-12 06:11:21 -0700585
586 __m128i fVec;
587};
588
589template <>
mtklein6f37b4a2015-12-14 11:25:18 -0800590class SkNx<4, uint8_t> {
591public:
Mike Klein7c78f3a2016-10-19 09:21:11 -0400592 AI SkNx() {}
593 AI SkNx(const __m128i& vec) : fVec(vec) {}
594 AI SkNx(uint8_t a, uint8_t b, uint8_t c, uint8_t d)
herbfd5a2602016-03-01 07:01:23 -0800595 : fVec(_mm_setr_epi8(a,b,c,d, 0,0,0,0, 0,0,0,0, 0,0,0,0)) {}
596
Mike Klein7c78f3a2016-10-19 09:21:11 -0400597 AI static SkNx Load(const void* ptr) { return _mm_cvtsi32_si128(*(const int*)ptr); }
598 AI void store(void* ptr) const { *(int*)ptr = _mm_cvtsi128_si32(fVec); }
mtklein6f37b4a2015-12-14 11:25:18 -0800599
Mike Klein7c78f3a2016-10-19 09:21:11 -0400600 AI uint8_t operator[](int k) const {
herbfd5a2602016-03-01 07:01:23 -0800601 SkASSERT(0 <= k && k < 4);
602 union { __m128i v; uint8_t us[16]; } pun = {fVec};
603 return pun.us[k&3];
604 }
605
mtklein6f37b4a2015-12-14 11:25:18 -0800606 // TODO as needed
607
608 __m128i fVec;
609};
610
611template <>
Herb Derbyd1b3c782017-11-02 13:18:38 -0400612class SkNx<8, uint8_t> {
613public:
614 AI SkNx(const __m128i& vec) : fVec(vec) {}
615
616 AI SkNx() {}
617 AI SkNx(uint8_t val) : fVec(_mm_set1_epi8(val)) {}
Herb Derbyb8b30862017-11-16 16:04:42 -0500618 AI static SkNx Load(const void* ptr) { return _mm_loadl_epi64((const __m128i*)ptr); }
Herb Derbyd1b3c782017-11-02 13:18:38 -0400619 AI SkNx(uint8_t a, uint8_t b, uint8_t c, uint8_t d,
620 uint8_t e, uint8_t f, uint8_t g, uint8_t h)
621 : fVec(_mm_setr_epi8(a,b,c,d, e,f,g,h, 0,0,0,0, 0,0,0,0)) {}
622
623 AI void store(void* ptr) const {_mm_storel_epi64((__m128i*)ptr, fVec);}
624
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
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
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
651template <>
mtklein6c221b42015-11-20 13:53:19 -0800652class SkNx<16, uint8_t> {
mtkleind2ffd362015-05-12 06:11:21 -0700653public:
Mike Klein7c78f3a2016-10-19 09:21:11 -0400654 AI SkNx(const __m128i& vec) : fVec(vec) {}
mtkleind2ffd362015-05-12 06:11:21 -0700655
Mike Klein7c78f3a2016-10-19 09:21:11 -0400656 AI SkNx() {}
657 AI SkNx(uint8_t val) : fVec(_mm_set1_epi8(val)) {}
658 AI static SkNx Load(const void* ptr) { return _mm_loadu_si128((const __m128i*)ptr); }
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)
mtkleind2ffd362015-05-12 06:11:21 -0700663 : fVec(_mm_setr_epi8(a,b,c,d, e,f,g,h, i,j,k,l, m,n,o,p)) {}
664
Mike Klein7c78f3a2016-10-19 09:21:11 -0400665 AI void store(void* ptr) const { _mm_storeu_si128((__m128i*)ptr, fVec); }
mtkleind2ffd362015-05-12 06:11:21 -0700666
Mike Klein7c78f3a2016-10-19 09:21:11 -0400667 AI SkNx saturatedAdd(const SkNx& o) const { return _mm_adds_epu8(fVec, o.fVec); }
mtklein6cbf18c2015-05-12 15:48:09 -0700668
Mike Klein7c78f3a2016-10-19 09:21:11 -0400669 AI SkNx operator + (const SkNx& o) const { return _mm_add_epi8(fVec, o.fVec); }
670 AI SkNx operator - (const SkNx& o) const { return _mm_sub_epi8(fVec, o.fVec); }
Mike Klein7dfe6d92018-12-18 14:53:37 -0500671 AI SkNx operator & (const SkNx& o) const { return _mm_and_si128(fVec, o.fVec); }
mtkleind2ffd362015-05-12 06:11:21 -0700672
Mike Klein7c78f3a2016-10-19 09:21:11 -0400673 AI static SkNx Min(const SkNx& a, const SkNx& b) { return _mm_min_epu8(a.fVec, b.fVec); }
674 AI SkNx operator < (const SkNx& o) const {
mtkleinb5e86112015-06-24 15:18:39 -0700675 // There's no unsigned _mm_cmplt_epu8, so we flip the sign bits then use a signed compare.
676 auto flip = _mm_set1_epi8(char(0x80));
677 return _mm_cmplt_epi8(_mm_xor_si128(flip, fVec), _mm_xor_si128(flip, o.fVec));
678 }
mtklein27e517a2015-05-14 17:53:04 -0700679
Mike Klein7c78f3a2016-10-19 09:21:11 -0400680 AI uint8_t operator[](int k) const {
mtkleind2ffd362015-05-12 06:11:21 -0700681 SkASSERT(0 <= k && k < 16);
mtkleine4c0bee2016-02-09 10:35:27 -0800682 union { __m128i v; uint8_t us[16]; } pun = {fVec};
683 return pun.us[k&15];
mtkleind2ffd362015-05-12 06:11:21 -0700684 }
685
Mike Klein7c78f3a2016-10-19 09:21:11 -0400686 AI SkNx thenElse(const SkNx& t, const SkNx& e) const {
mtkleinb5e86112015-06-24 15:18:39 -0700687 return _mm_or_si128(_mm_and_si128 (fVec, t.fVec),
688 _mm_andnot_si128(fVec, e.fVec));
689 }
690
mtklein115acee2015-04-14 14:02:52 -0700691 __m128i fVec;
692};
mtkleinc9adb052015-03-30 10:50:27 -0700693
Mike Klein7c78f3a2016-10-19 09:21:11 -0400694template<> AI /*static*/ Sk4f SkNx_cast<float, int32_t>(const Sk4i& src) {
mtklein0cf795f2016-02-17 07:23:36 -0800695 return _mm_cvtepi32_ps(src.fVec);
696}
Herb Derbyd1b3c782017-11-02 13:18:38 -0400697
Mike Klein7c78f3a2016-10-19 09:21:11 -0400698template<> AI /*static*/ Sk4f SkNx_cast<float, uint32_t>(const Sk4u& src) {
mtkleind05a8752016-07-29 10:10:15 -0700699 return SkNx_cast<float>(Sk4i::Load(&src));
700}
mtklein0cf795f2016-02-17 07:23:36 -0800701
Mike Klein7c78f3a2016-10-19 09:21:11 -0400702template <> AI /*static*/ Sk4i SkNx_cast<int32_t, float>(const Sk4f& src) {
mtklein0cf795f2016-02-17 07:23:36 -0800703 return _mm_cvttps_epi32(src.fVec);
704}
mtklein6c221b42015-11-20 13:53:19 -0800705
Mike Klein7c78f3a2016-10-19 09:21:11 -0400706template<> AI /*static*/ Sk4h SkNx_cast<uint16_t, int32_t>(const Sk4i& src) {
mtklein036e1832016-07-15 07:45:53 -0700707#if 0 && SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE41
708 // TODO: This seems to be causing code generation problems. Investigate?
709 return _mm_packus_epi32(src.fVec);
710#elif SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSSE3
mtklein629f25a2016-02-08 05:54:38 -0800711 // With SSSE3, we can just shuffle the low 2 bytes from each lane right into place.
712 const int _ = ~0;
mtklein036e1832016-07-15 07:45:53 -0700713 return _mm_shuffle_epi8(src.fVec, _mm_setr_epi8(0,1, 4,5, 8,9, 12,13, _,_,_,_,_,_,_,_));
mtklein629f25a2016-02-08 05:54:38 -0800714#else
mtklein036e1832016-07-15 07:45:53 -0700715 // With SSE2, we have to sign extend our input, making _mm_packs_epi32 do the pack we want.
716 __m128i x = _mm_srai_epi32(_mm_slli_epi32(src.fVec, 16), 16);
717 return _mm_packs_epi32(x,x);
mtklein629f25a2016-02-08 05:54:38 -0800718#endif
mtklein2d340f22016-02-06 19:38:39 -0800719}
720
Mike Klein7c78f3a2016-10-19 09:21:11 -0400721template<> AI /*static*/ Sk4h SkNx_cast<uint16_t, float>(const Sk4f& src) {
mtkleind05a8752016-07-29 10:10:15 -0700722 return SkNx_cast<uint16_t>(SkNx_cast<int32_t>(src));
mtklein036e1832016-07-15 07:45:53 -0700723}
724
Mike Klein7c78f3a2016-10-19 09:21:11 -0400725template<> AI /*static*/ Sk4b SkNx_cast<uint8_t, float>(const Sk4f& src) {
mtklein97120a72016-02-12 14:19:06 -0800726 auto _32 = _mm_cvttps_epi32(src.fVec);
727#if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSSE3
728 const int _ = ~0;
729 return _mm_shuffle_epi8(_32, _mm_setr_epi8(0,4,8,12, _,_,_,_, _,_,_,_, _,_,_,_));
730#else
731 auto _16 = _mm_packus_epi16(_32, _32);
732 return _mm_packus_epi16(_16, _16);
733#endif
mtkleinc1eb3112016-02-11 08:10:22 -0800734}
735
Herb Derby5eb15282017-10-10 17:14:18 -0400736template<> AI /*static*/ Sk4u SkNx_cast<uint32_t, uint8_t>(const Sk4b& src) {
mtklein97120a72016-02-12 14:19:06 -0800737#if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSSE3
738 const int _ = ~0;
Mike Klein06a65e22016-11-17 12:39:09 -0500739 return _mm_shuffle_epi8(src.fVec, _mm_setr_epi8(0,_,_,_, 1,_,_,_, 2,_,_,_, 3,_,_,_));
mtklein97120a72016-02-12 14:19:06 -0800740#else
Mike Klein06a65e22016-11-17 12:39:09 -0500741 auto _16 = _mm_unpacklo_epi8(src.fVec, _mm_setzero_si128());
742 return _mm_unpacklo_epi16(_16, _mm_setzero_si128());
mtklein97120a72016-02-12 14:19:06 -0800743#endif
Mike Klein06a65e22016-11-17 12:39:09 -0500744}
745
Herb Derby5eb15282017-10-10 17:14:18 -0400746template<> AI /*static*/ Sk4i SkNx_cast<int32_t, uint8_t>(const Sk4b& src) {
747 return SkNx_cast<uint32_t>(src).fVec;
748}
749
Mike Klein06a65e22016-11-17 12:39:09 -0500750template<> AI /*static*/ Sk4f SkNx_cast<float, uint8_t>(const Sk4b& src) {
751 return _mm_cvtepi32_ps(SkNx_cast<int32_t>(src).fVec);
mtkleinc1eb3112016-02-11 08:10:22 -0800752}
mtklein97120a72016-02-12 14:19:06 -0800753
Mike Klein7c78f3a2016-10-19 09:21:11 -0400754template<> AI /*static*/ Sk4f SkNx_cast<float, uint16_t>(const Sk4h& src) {
mtklein97120a72016-02-12 14:19:06 -0800755 auto _32 = _mm_unpacklo_epi16(src.fVec, _mm_setzero_si128());
756 return _mm_cvtepi32_ps(_32);
mtklein2d340f22016-02-06 19:38:39 -0800757}
758
Herb Derbyd1b3c782017-11-02 13:18:38 -0400759template<> AI /*static*/ Sk8b SkNx_cast<uint8_t, int32_t>(const Sk8i& src) {
760 Sk4i lo, hi;
761 SkNx_split(src, &lo, &hi);
762
763 auto t = _mm_packs_epi32(lo.fVec, hi.fVec);
764 return _mm_packus_epi16(t, t);
765}
766
Mike Klein7c78f3a2016-10-19 09:21:11 -0400767template<> AI /*static*/ Sk16b SkNx_cast<uint8_t, float>(const Sk16f& src) {
mtkleinf8f90e42016-03-21 10:04:46 -0700768 Sk8f ab, cd;
769 SkNx_split(src, &ab, &cd);
770
771 Sk4f a,b,c,d;
772 SkNx_split(ab, &a, &b);
773 SkNx_split(cd, &c, &d);
774
775 return _mm_packus_epi16(_mm_packus_epi16(_mm_cvttps_epi32(a.fVec),
776 _mm_cvttps_epi32(b.fVec)),
777 _mm_packus_epi16(_mm_cvttps_epi32(c.fVec),
778 _mm_cvttps_epi32(d.fVec)));
mtklein97120a72016-02-12 14:19:06 -0800779}
780
Mike Klein7c78f3a2016-10-19 09:21:11 -0400781template<> AI /*static*/ Sk4h SkNx_cast<uint16_t, uint8_t>(const Sk4b& src) {
mtklein97120a72016-02-12 14:19:06 -0800782 return _mm_unpacklo_epi8(src.fVec, _mm_setzero_si128());
783}
784
Herb Derbyd1b3c782017-11-02 13:18:38 -0400785template<> AI /*static*/ Sk8h SkNx_cast<uint16_t, uint8_t>(const Sk8b& src) {
786 return _mm_unpacklo_epi8(src.fVec, _mm_setzero_si128());
787}
788
Mike Klein7c78f3a2016-10-19 09:21:11 -0400789template<> AI /*static*/ Sk4b SkNx_cast<uint8_t, uint16_t>(const Sk4h& src) {
mtklein97120a72016-02-12 14:19:06 -0800790 return _mm_packus_epi16(src.fVec, src.fVec);
mtklein550e9b02016-01-20 11:55:51 -0800791}
792
Herb Derbyd1b3c782017-11-02 13:18:38 -0400793template<> AI /*static*/ Sk8b SkNx_cast<uint8_t, uint16_t>(const Sk8h& src) {
794 return _mm_packus_epi16(src.fVec, src.fVec);
795}
796
Mike Klein7c78f3a2016-10-19 09:21:11 -0400797template<> AI /*static*/ Sk4i SkNx_cast<int32_t, uint16_t>(const Sk4h& src) {
mtklein58e389b2016-07-15 07:00:11 -0700798 return _mm_unpacklo_epi16(src.fVec, _mm_setzero_si128());
799}
800
Herb Derbyd1b3c782017-11-02 13:18:38 -0400801
Mike Klein7c78f3a2016-10-19 09:21:11 -0400802template<> AI /*static*/ Sk4b SkNx_cast<uint8_t, int32_t>(const Sk4i& src) {
msarett7d3ff712016-07-12 14:55:45 -0700803 return _mm_packus_epi16(_mm_packus_epi16(src.fVec, src.fVec), src.fVec);
804}
805
Herb Derby0f96bb32017-09-13 16:46:05 -0400806template<> AI /*static*/ Sk4b SkNx_cast<uint8_t, uint32_t>(const Sk4u& src) {
807 return _mm_packus_epi16(_mm_packus_epi16(src.fVec, src.fVec), src.fVec);
808}
809
Mike Klein7c78f3a2016-10-19 09:21:11 -0400810template<> AI /*static*/ Sk4i SkNx_cast<int32_t, uint32_t>(const Sk4u& src) {
msarett15ee3de2016-08-02 11:30:30 -0700811 return src.fVec;
812}
813
Mike Klein7c78f3a2016-10-19 09:21:11 -0400814AI static Sk4i Sk4f_round(const Sk4f& x) {
msarett7d3ff712016-07-12 14:55:45 -0700815 return _mm_cvtps_epi32(x.fVec);
816}
817
Mike Klein1e764642016-10-14 17:09:03 -0400818} // namespace
819
mtkleinc9adb052015-03-30 10:50:27 -0700820#endif//SkNx_sse_DEFINED