blob: a4594115e0ecff4e4223606da03c4ccba8fcbcd8 [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
mtkleine18fa442016-06-09 13:40:56 -070011#include <immintrin.h>
mtklein244a6532016-04-19 14:21:30 -070012
mtkleinc9adb052015-03-30 10:50:27 -070013// This file may assume <= SSE2, but must check SK_CPU_SSE_LEVEL for anything more recent.
mtkleine4c0bee2016-02-09 10:35:27 -080014// If you do, make sure this is in a static inline function... anywhere else risks violating ODR.
mtkleinaa999cb2015-05-22 17:18:21 -070015
fmalitac2e0ac42015-12-03 09:15:25 -080016#define SKNX_IS_FAST
17
Mike Klein511ea172016-10-15 00:13:29 -040018template <> struct SkNx_abi<4,float> { __m128 vec; };
Mike Klein5ec55902016-10-15 01:18:45 -040019#if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_AVX2
20 template <> struct SkNx_abi<8,float> { __m256 vec; };
21#endif
Mike Klein511ea172016-10-15 00:13:29 -040022
Mike Klein1e764642016-10-14 17:09:03 -040023namespace {
24
mtkleinc9adb052015-03-30 10:50:27 -070025template <>
mtklein6c221b42015-11-20 13:53:19 -080026class SkNx<2, float> {
mtkleinc9adb052015-03-30 10:50:27 -070027public:
Mike Klein7c78f3a2016-10-19 09:21:11 -040028 AI SkNx(const __m128& vec) : fVec(vec) {}
mtkleinc9adb052015-03-30 10:50:27 -070029
Mike Klein7c78f3a2016-10-19 09:21:11 -040030 AI SkNx() {}
31 AI SkNx(float val) : fVec(_mm_set1_ps(val)) {}
32 AI static SkNx Load(const void* ptr) {
mtklein507ef6d2016-01-31 08:02:47 -080033 return _mm_castsi128_ps(_mm_loadl_epi64((const __m128i*)ptr));
mtkleinc9adb052015-03-30 10:50:27 -070034 }
Mike Klein7c78f3a2016-10-19 09:21:11 -040035 AI SkNx(float a, float b) : fVec(_mm_setr_ps(a,b,0,0)) {}
mtkleinc9adb052015-03-30 10:50:27 -070036
Mike Klein7c78f3a2016-10-19 09:21:11 -040037 AI void store(void* ptr) const { _mm_storel_pi((__m64*)ptr, fVec); }
mtkleinc9adb052015-03-30 10:50:27 -070038
Mike Klein7c78f3a2016-10-19 09:21:11 -040039 AI SkNx operator + (const SkNx& o) const { return _mm_add_ps(fVec, o.fVec); }
40 AI SkNx operator - (const SkNx& o) const { return _mm_sub_ps(fVec, o.fVec); }
41 AI SkNx operator * (const SkNx& o) const { return _mm_mul_ps(fVec, o.fVec); }
42 AI SkNx operator / (const SkNx& o) const { return _mm_div_ps(fVec, o.fVec); }
mtkleinc9adb052015-03-30 10:50:27 -070043
Mike Klein7c78f3a2016-10-19 09:21:11 -040044 AI SkNx operator == (const SkNx& o) const { return _mm_cmpeq_ps (fVec, o.fVec); }
45 AI SkNx operator != (const SkNx& o) const { return _mm_cmpneq_ps(fVec, o.fVec); }
46 AI SkNx operator < (const SkNx& o) const { return _mm_cmplt_ps (fVec, o.fVec); }
47 AI SkNx operator > (const SkNx& o) const { return _mm_cmpgt_ps (fVec, o.fVec); }
48 AI SkNx operator <= (const SkNx& o) const { return _mm_cmple_ps (fVec, o.fVec); }
49 AI SkNx operator >= (const SkNx& o) const { return _mm_cmpge_ps (fVec, o.fVec); }
mtkleinc9adb052015-03-30 10:50:27 -070050
Mike Klein7c78f3a2016-10-19 09:21:11 -040051 AI static SkNx Min(const SkNx& l, const SkNx& r) { return _mm_min_ps(l.fVec, r.fVec); }
52 AI static SkNx Max(const SkNx& l, const SkNx& r) { return _mm_max_ps(l.fVec, r.fVec); }
mtkleinc9adb052015-03-30 10:50:27 -070053
Mike Klein7c78f3a2016-10-19 09:21:11 -040054 AI SkNx sqrt() const { return _mm_sqrt_ps (fVec); }
55 AI SkNx rsqrt() const { return _mm_rsqrt_ps(fVec); }
56 AI SkNx invert() const { return _mm_rcp_ps(fVec); }
mtkleinc9adb052015-03-30 10:50:27 -070057
Mike Klein7c78f3a2016-10-19 09:21:11 -040058 AI float operator[](int k) const {
mtkleinc9adb052015-03-30 10:50:27 -070059 SkASSERT(0 <= k && k < 2);
60 union { __m128 v; float fs[4]; } pun = {fVec};
mtkleina156a8f2015-04-03 06:16:13 -070061 return pun.fs[k&1];
mtkleinc9adb052015-03-30 10:50:27 -070062 }
63
Mike Klein7c78f3a2016-10-19 09:21:11 -040064 AI bool allTrue() const { return 0xff == (_mm_movemask_epi8(_mm_castps_si128(fVec)) & 0xff); }
65 AI bool anyTrue() const { return 0x00 != (_mm_movemask_epi8(_mm_castps_si128(fVec)) & 0xff); }
mtkleinb5e86112015-06-24 15:18:39 -070066
mtkleinc9adb052015-03-30 10:50:27 -070067 __m128 fVec;
68};
69
70template <>
mtklein6c221b42015-11-20 13:53:19 -080071class SkNx<4, float> {
mtkleinc9adb052015-03-30 10:50:27 -070072public:
Mike Klein7c78f3a2016-10-19 09:21:11 -040073 AI SkNx(const __m128& vec) : fVec(vec) {}
mtkleinc9adb052015-03-30 10:50:27 -070074
Mike Klein7c78f3a2016-10-19 09:21:11 -040075 AI SkNx() {}
76 AI SkNx(float val) : fVec( _mm_set1_ps(val) ) {}
77 AI SkNx(float a, float b, float c, float d) : fVec(_mm_setr_ps(a,b,c,d)) {}
mtkleinc9adb052015-03-30 10:50:27 -070078
Mike Klein7c78f3a2016-10-19 09:21:11 -040079 AI SkNx(const SkNx_abi<4,float>& a) : fVec(a.vec) {}
80 AI operator SkNx_abi<4,float>() const { return { fVec }; }
Mike Klein511ea172016-10-15 00:13:29 -040081
Mike Klein7c78f3a2016-10-19 09:21:11 -040082 AI static SkNx Load(const void* ptr) { return _mm_loadu_ps((const float*)ptr); }
83 AI void store(void* ptr) const { _mm_storeu_ps((float*)ptr, fVec); }
mtklein9db43ac2015-12-01 07:10:21 -080084
Mike Klein7c78f3a2016-10-19 09:21:11 -040085 AI static void Load4(const void* ptr, SkNx* r, SkNx* g, SkNx* b, SkNx* a) {
Mike Klein33cbfd72016-10-06 11:09:27 -040086 __m128 v0 = _mm_loadu_ps(((float*)ptr) + 0),
87 v1 = _mm_loadu_ps(((float*)ptr) + 4),
88 v2 = _mm_loadu_ps(((float*)ptr) + 8),
89 v3 = _mm_loadu_ps(((float*)ptr) + 12);
90 _MM_TRANSPOSE4_PS(v0, v1, v2, v3);
91 *r = v0;
92 *g = v1;
93 *b = v2;
94 *a = v3;
95 }
Mike Klein7c78f3a2016-10-19 09:21:11 -040096 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 -040097 __m128 v0 = r.fVec,
98 v1 = g.fVec,
99 v2 = b.fVec,
100 v3 = a.fVec;
101 _MM_TRANSPOSE4_PS(v0, v1, v2, v3);
102 _mm_storeu_ps(((float*) dst) + 0, v0);
103 _mm_storeu_ps(((float*) dst) + 4, v1);
104 _mm_storeu_ps(((float*) dst) + 8, v2);
105 _mm_storeu_ps(((float*) dst) + 12, v3);
106 }
107
Mike Klein7c78f3a2016-10-19 09:21:11 -0400108 AI SkNx operator + (const SkNx& o) const { return _mm_add_ps(fVec, o.fVec); }
109 AI SkNx operator - (const SkNx& o) const { return _mm_sub_ps(fVec, o.fVec); }
110 AI SkNx operator * (const SkNx& o) const { return _mm_mul_ps(fVec, o.fVec); }
111 AI SkNx operator / (const SkNx& o) const { return _mm_div_ps(fVec, o.fVec); }
mtkleinc9adb052015-03-30 10:50:27 -0700112
Mike Klein7c78f3a2016-10-19 09:21:11 -0400113 AI SkNx operator == (const SkNx& o) const { return _mm_cmpeq_ps (fVec, o.fVec); }
114 AI SkNx operator != (const SkNx& o) const { return _mm_cmpneq_ps(fVec, o.fVec); }
115 AI SkNx operator < (const SkNx& o) const { return _mm_cmplt_ps (fVec, o.fVec); }
116 AI SkNx operator > (const SkNx& o) const { return _mm_cmpgt_ps (fVec, o.fVec); }
117 AI SkNx operator <= (const SkNx& o) const { return _mm_cmple_ps (fVec, o.fVec); }
118 AI SkNx operator >= (const SkNx& o) const { return _mm_cmpge_ps (fVec, o.fVec); }
mtkleinc9adb052015-03-30 10:50:27 -0700119
Mike Klein7c78f3a2016-10-19 09:21:11 -0400120 AI static SkNx Min(const SkNx& l, const SkNx& r) { return _mm_min_ps(l.fVec, r.fVec); }
121 AI static SkNx Max(const SkNx& l, const SkNx& r) { return _mm_max_ps(l.fVec, r.fVec); }
mtkleinc9adb052015-03-30 10:50:27 -0700122
Mike Klein7c78f3a2016-10-19 09:21:11 -0400123 AI SkNx abs() const { return _mm_andnot_ps(_mm_set1_ps(-0.0f), fVec); }
124 AI SkNx floor() const {
mtklein5608e2e2016-07-11 09:59:21 -0700125 #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE41
126 return _mm_floor_ps(fVec);
127 #else
mtklein244a6532016-04-19 14:21:30 -0700128 // Emulate _mm_floor_ps() with SSE2:
129 // - roundtrip through integers via truncation
130 // - subtract 1 if that's too big (possible for negative values).
131 // This restricts the domain of our inputs to a maximum somehwere around 2^31.
132 // Seems plenty big.
133 __m128 roundtrip = _mm_cvtepi32_ps(_mm_cvttps_epi32(fVec));
134 __m128 too_big = _mm_cmpgt_ps(roundtrip, fVec);
135 return _mm_sub_ps(roundtrip, _mm_and_ps(too_big, _mm_set1_ps(1.0f)));
mtklein5608e2e2016-07-11 09:59:21 -0700136 #endif
mtklein244a6532016-04-19 14:21:30 -0700137 }
mtkleinc33065a2016-01-15 12:16:40 -0800138
Mike Klein7c78f3a2016-10-19 09:21:11 -0400139 AI SkNx sqrt() const { return _mm_sqrt_ps (fVec); }
140 AI SkNx rsqrt() const { return _mm_rsqrt_ps(fVec); }
141 AI SkNx invert() const { return _mm_rcp_ps(fVec); }
mtkleinc9adb052015-03-30 10:50:27 -0700142
Mike Klein7c78f3a2016-10-19 09:21:11 -0400143 AI float operator[](int k) const {
mtkleinc9adb052015-03-30 10:50:27 -0700144 SkASSERT(0 <= k && k < 4);
145 union { __m128 v; float fs[4]; } pun = {fVec};
mtkleina156a8f2015-04-03 06:16:13 -0700146 return pun.fs[k&3];
mtkleinc9adb052015-03-30 10:50:27 -0700147 }
148
Mike Klein7c78f3a2016-10-19 09:21:11 -0400149 AI bool allTrue() const { return 0xffff == _mm_movemask_epi8(_mm_castps_si128(fVec)); }
150 AI bool anyTrue() const { return 0x0000 != _mm_movemask_epi8(_mm_castps_si128(fVec)); }
mtkleinb5e86112015-06-24 15:18:39 -0700151
Mike Klein7c78f3a2016-10-19 09:21:11 -0400152 AI SkNx thenElse(const SkNx& t, const SkNx& e) const {
mtklein5608e2e2016-07-11 09:59:21 -0700153 #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE41
msarettd2809572016-06-20 06:07:45 -0700154 return _mm_blendv_ps(e.fVec, t.fVec, fVec);
mtklein5608e2e2016-07-11 09:59:21 -0700155 #else
mtklein2aab22a2015-06-26 10:46:31 -0700156 return _mm_or_ps(_mm_and_ps (fVec, t.fVec),
157 _mm_andnot_ps(fVec, e.fVec));
mtklein5608e2e2016-07-11 09:59:21 -0700158 #endif
mtklein2aab22a2015-06-26 10:46:31 -0700159 }
160
mtkleinc9adb052015-03-30 10:50:27 -0700161 __m128 fVec;
162};
163
mtklein115acee2015-04-14 14:02:52 -0700164template <>
mtkleind05a8752016-07-29 10:10:15 -0700165class SkNx<4, int32_t> {
mtklein8273ca42016-02-09 11:32:51 -0800166public:
Mike Klein7c78f3a2016-10-19 09:21:11 -0400167 AI SkNx(const __m128i& vec) : fVec(vec) {}
mtklein8273ca42016-02-09 11:32:51 -0800168
Mike Klein7c78f3a2016-10-19 09:21:11 -0400169 AI SkNx() {}
170 AI SkNx(int32_t val) : fVec(_mm_set1_epi32(val)) {}
171 AI static SkNx Load(const void* ptr) { return _mm_loadu_si128((const __m128i*)ptr); }
172 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 -0800173
Mike Klein7c78f3a2016-10-19 09:21:11 -0400174 AI void store(void* ptr) const { _mm_storeu_si128((__m128i*)ptr, fVec); }
mtklein8273ca42016-02-09 11:32:51 -0800175
Mike Klein7c78f3a2016-10-19 09:21:11 -0400176 AI SkNx operator + (const SkNx& o) const { return _mm_add_epi32(fVec, o.fVec); }
177 AI SkNx operator - (const SkNx& o) const { return _mm_sub_epi32(fVec, o.fVec); }
178 AI SkNx operator * (const SkNx& o) const {
mtklein8273ca42016-02-09 11:32:51 -0800179 __m128i mul20 = _mm_mul_epu32(fVec, o.fVec),
180 mul31 = _mm_mul_epu32(_mm_srli_si128(fVec, 4), _mm_srli_si128(o.fVec, 4));
181 return _mm_unpacklo_epi32(_mm_shuffle_epi32(mul20, _MM_SHUFFLE(0,0,2,0)),
182 _mm_shuffle_epi32(mul31, _MM_SHUFFLE(0,0,2,0)));
183 }
184
Mike Klein7c78f3a2016-10-19 09:21:11 -0400185 AI SkNx operator & (const SkNx& o) const { return _mm_and_si128(fVec, o.fVec); }
186 AI SkNx operator | (const SkNx& o) const { return _mm_or_si128(fVec, o.fVec); }
187 AI SkNx operator ^ (const SkNx& o) const { return _mm_xor_si128(fVec, o.fVec); }
mtklein64f061a2016-06-17 12:09:16 -0700188
Mike Klein7c78f3a2016-10-19 09:21:11 -0400189 AI SkNx operator << (int bits) const { return _mm_slli_epi32(fVec, bits); }
190 AI SkNx operator >> (int bits) const { return _mm_srai_epi32(fVec, bits); }
mtklein8273ca42016-02-09 11:32:51 -0800191
Mike Klein7c78f3a2016-10-19 09:21:11 -0400192 AI SkNx operator == (const SkNx& o) const { return _mm_cmpeq_epi32 (fVec, o.fVec); }
193 AI SkNx operator < (const SkNx& o) const { return _mm_cmplt_epi32 (fVec, o.fVec); }
194 AI SkNx operator > (const SkNx& o) const { return _mm_cmpgt_epi32 (fVec, o.fVec); }
mtklein58e389b2016-07-15 07:00:11 -0700195
Mike Klein7c78f3a2016-10-19 09:21:11 -0400196 AI int32_t operator[](int k) const {
mtklein8273ca42016-02-09 11:32:51 -0800197 SkASSERT(0 <= k && k < 4);
mtkleind05a8752016-07-29 10:10:15 -0700198 union { __m128i v; int32_t is[4]; } pun = {fVec};
mtklein8273ca42016-02-09 11:32:51 -0800199 return pun.is[k&3];
200 }
mtklein8273ca42016-02-09 11:32:51 -0800201
Mike Klein7c78f3a2016-10-19 09:21:11 -0400202 AI SkNx thenElse(const SkNx& t, const SkNx& e) const {
mtklein58e389b2016-07-15 07:00:11 -0700203 #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE41
204 return _mm_blendv_epi8(e.fVec, t.fVec, fVec);
205 #else
206 return _mm_or_si128(_mm_and_si128 (fVec, t.fVec),
207 _mm_andnot_si128(fVec, e.fVec));
208 #endif
209 }
210
mtklein8273ca42016-02-09 11:32:51 -0800211 __m128i fVec;
212};
213
214template <>
mtkleind05a8752016-07-29 10:10:15 -0700215class SkNx<4, uint32_t> {
216public:
Mike Klein7c78f3a2016-10-19 09:21:11 -0400217 AI SkNx(const __m128i& vec) : fVec(vec) {}
mtkleind05a8752016-07-29 10:10:15 -0700218
Mike Klein7c78f3a2016-10-19 09:21:11 -0400219 AI SkNx() {}
220 AI SkNx(uint32_t val) : fVec(_mm_set1_epi32(val)) {}
221 AI static SkNx Load(const void* ptr) { return _mm_loadu_si128((const __m128i*)ptr); }
222 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 -0700223
Mike Klein7c78f3a2016-10-19 09:21:11 -0400224 AI void store(void* ptr) const { _mm_storeu_si128((__m128i*)ptr, fVec); }
mtkleind05a8752016-07-29 10:10:15 -0700225
Mike Klein7c78f3a2016-10-19 09:21:11 -0400226 AI SkNx operator + (const SkNx& o) const { return _mm_add_epi32(fVec, o.fVec); }
227 AI SkNx operator - (const SkNx& o) const { return _mm_sub_epi32(fVec, o.fVec); }
mtkleind05a8752016-07-29 10:10:15 -0700228 // Not quite sure how to best do operator * in SSE2. We probably don't use it.
229
Mike Klein7c78f3a2016-10-19 09:21:11 -0400230 AI SkNx operator & (const SkNx& o) const { return _mm_and_si128(fVec, o.fVec); }
231 AI SkNx operator | (const SkNx& o) const { return _mm_or_si128(fVec, o.fVec); }
232 AI SkNx operator ^ (const SkNx& o) const { return _mm_xor_si128(fVec, o.fVec); }
mtkleind05a8752016-07-29 10:10:15 -0700233
Mike Klein7c78f3a2016-10-19 09:21:11 -0400234 AI SkNx operator << (int bits) const { return _mm_slli_epi32(fVec, bits); }
235 AI SkNx operator >> (int bits) const { return _mm_srli_epi32(fVec, bits); }
mtkleind05a8752016-07-29 10:10:15 -0700236
Mike Klein7c78f3a2016-10-19 09:21:11 -0400237 AI SkNx operator == (const SkNx& o) const { return _mm_cmpeq_epi32 (fVec, o.fVec); }
mtkleind05a8752016-07-29 10:10:15 -0700238 // operator < and > take a little extra fiddling to make work for unsigned ints.
239
Mike Klein7c78f3a2016-10-19 09:21:11 -0400240 AI uint32_t operator[](int k) const {
mtkleind05a8752016-07-29 10:10:15 -0700241 SkASSERT(0 <= k && k < 4);
242 union { __m128i v; uint32_t us[4]; } pun = {fVec};
243 return pun.us[k&3];
244 }
245
Mike Klein7c78f3a2016-10-19 09:21:11 -0400246 AI SkNx thenElse(const SkNx& t, const SkNx& e) const {
mtkleind05a8752016-07-29 10:10:15 -0700247 #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE41
248 return _mm_blendv_epi8(e.fVec, t.fVec, fVec);
249 #else
250 return _mm_or_si128(_mm_and_si128 (fVec, t.fVec),
251 _mm_andnot_si128(fVec, e.fVec));
252 #endif
253 }
254
255 __m128i fVec;
256};
257
258
259template <>
mtklein6c221b42015-11-20 13:53:19 -0800260class SkNx<4, uint16_t> {
mtklein115acee2015-04-14 14:02:52 -0700261public:
Mike Klein7c78f3a2016-10-19 09:21:11 -0400262 AI SkNx(const __m128i& vec) : fVec(vec) {}
mtklein115acee2015-04-14 14:02:52 -0700263
Mike Klein7c78f3a2016-10-19 09:21:11 -0400264 AI SkNx() {}
265 AI SkNx(uint16_t val) : fVec(_mm_set1_epi16(val)) {}
266 AI SkNx(uint16_t a, uint16_t b, uint16_t c, uint16_t d)
267 : fVec(_mm_setr_epi16(a,b,c,d,0,0,0,0)) {}
mtklein115acee2015-04-14 14:02:52 -0700268
Mike Klein7c78f3a2016-10-19 09:21:11 -0400269 AI static SkNx Load(const void* ptr) { return _mm_loadl_epi64((const __m128i*)ptr); }
270 AI void store(void* ptr) const { _mm_storel_epi64((__m128i*)ptr, fVec); }
mtklein115acee2015-04-14 14:02:52 -0700271
Mike Klein7c78f3a2016-10-19 09:21:11 -0400272 AI static void Load4(const void* ptr, SkNx* r, SkNx* g, SkNx* b, SkNx* a) {
Mike Klein33cbfd72016-10-06 11:09:27 -0400273 __m128i lo = _mm_loadu_si128(((__m128i*)ptr) + 0),
274 hi = _mm_loadu_si128(((__m128i*)ptr) + 1);
275 __m128i even = _mm_unpacklo_epi16(lo, hi), // r0 r2 g0 g2 b0 b2 a0 a2
276 odd = _mm_unpackhi_epi16(lo, hi); // r1 r3 ...
277 __m128i rg = _mm_unpacklo_epi16(even, odd), // r0 r1 r2 r3 g0 g1 g2 g3
278 ba = _mm_unpackhi_epi16(even, odd); // b0 b1 ... a0 a1 ...
279 *r = rg;
280 *g = _mm_srli_si128(rg, 8);
281 *b = ba;
282 *a = _mm_srli_si128(ba, 8);
283 }
Mike Klein7c78f3a2016-10-19 09:21:11 -0400284 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 -0400285 __m128i rg = _mm_unpacklo_epi16(r.fVec, g.fVec);
286 __m128i ba = _mm_unpacklo_epi16(b.fVec, a.fVec);
287 __m128i lo = _mm_unpacklo_epi32(rg, ba);
288 __m128i hi = _mm_unpackhi_epi32(rg, ba);
289 _mm_storeu_si128(((__m128i*) dst) + 0, lo);
290 _mm_storeu_si128(((__m128i*) dst) + 1, hi);
291 }
292
Mike Klein7c78f3a2016-10-19 09:21:11 -0400293 AI SkNx operator + (const SkNx& o) const { return _mm_add_epi16(fVec, o.fVec); }
294 AI SkNx operator - (const SkNx& o) const { return _mm_sub_epi16(fVec, o.fVec); }
295 AI SkNx operator * (const SkNx& o) const { return _mm_mullo_epi16(fVec, o.fVec); }
mtklein115acee2015-04-14 14:02:52 -0700296
Mike Klein7c78f3a2016-10-19 09:21:11 -0400297 AI SkNx operator << (int bits) const { return _mm_slli_epi16(fVec, bits); }
298 AI SkNx operator >> (int bits) const { return _mm_srli_epi16(fVec, bits); }
mtklein115acee2015-04-14 14:02:52 -0700299
Mike Klein7c78f3a2016-10-19 09:21:11 -0400300 AI uint16_t operator[](int k) const {
mtklein115acee2015-04-14 14:02:52 -0700301 SkASSERT(0 <= k && k < 4);
mtkleine4c0bee2016-02-09 10:35:27 -0800302 union { __m128i v; uint16_t us[8]; } pun = {fVec};
303 return pun.us[k&3];
mtklein115acee2015-04-14 14:02:52 -0700304 }
mtkleind2ffd362015-05-12 06:11:21 -0700305
mtklein115acee2015-04-14 14:02:52 -0700306 __m128i fVec;
307};
308
309template <>
mtklein6c221b42015-11-20 13:53:19 -0800310class SkNx<8, uint16_t> {
mtklein115acee2015-04-14 14:02:52 -0700311public:
Mike Klein7c78f3a2016-10-19 09:21:11 -0400312 AI SkNx(const __m128i& vec) : fVec(vec) {}
mtklein115acee2015-04-14 14:02:52 -0700313
Mike Klein7c78f3a2016-10-19 09:21:11 -0400314 AI SkNx() {}
315 AI SkNx(uint16_t val) : fVec(_mm_set1_epi16(val)) {}
316 AI SkNx(uint16_t a, uint16_t b, uint16_t c, uint16_t d,
317 uint16_t e, uint16_t f, uint16_t g, uint16_t h)
318 : fVec(_mm_setr_epi16(a,b,c,d,e,f,g,h)) {}
mtklein115acee2015-04-14 14:02:52 -0700319
Mike Klein7c78f3a2016-10-19 09:21:11 -0400320 AI static SkNx Load(const void* ptr) { return _mm_loadu_si128((const __m128i*)ptr); }
321 AI void store(void* ptr) const { _mm_storeu_si128((__m128i*)ptr, fVec); }
mtklein115acee2015-04-14 14:02:52 -0700322
Mike Klein7c78f3a2016-10-19 09:21:11 -0400323 AI static void Load4(const void* ptr, SkNx* r, SkNx* g, SkNx* b, SkNx* a) {
Mike Klein04adfda2016-10-12 09:52:55 -0400324 // TODO: AVX2 version
325 __m128i _01 = _mm_loadu_si128(((__m128i*)ptr) + 0),
326 _23 = _mm_loadu_si128(((__m128i*)ptr) + 1),
327 _45 = _mm_loadu_si128(((__m128i*)ptr) + 2),
328 _67 = _mm_loadu_si128(((__m128i*)ptr) + 3);
329
330 __m128i _02 = _mm_unpacklo_epi16(_01, _23), // r0 r2 g0 g2 b0 b2 a0 a2
331 _13 = _mm_unpackhi_epi16(_01, _23), // r1 r3 g1 g3 b1 b3 a1 a3
332 _46 = _mm_unpacklo_epi16(_45, _67),
333 _57 = _mm_unpackhi_epi16(_45, _67);
334
335 __m128i rg0123 = _mm_unpacklo_epi16(_02, _13), // r0 r1 r2 r3 g0 g1 g2 g3
336 ba0123 = _mm_unpackhi_epi16(_02, _13), // b0 b1 b2 b3 a0 a1 a2 a3
337 rg4567 = _mm_unpacklo_epi16(_46, _57),
338 ba4567 = _mm_unpackhi_epi16(_46, _57);
339
340 *r = _mm_unpacklo_epi64(rg0123, rg4567);
341 *g = _mm_unpackhi_epi64(rg0123, rg4567);
342 *b = _mm_unpacklo_epi64(ba0123, ba4567);
343 *a = _mm_unpackhi_epi64(ba0123, ba4567);
344 }
Mike Klein7c78f3a2016-10-19 09:21:11 -0400345 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 -0400346 // TODO: AVX2 version
347 __m128i rg0123 = _mm_unpacklo_epi16(r.fVec, g.fVec), // r0 g0 r1 g1 r2 g2 r3 g3
348 rg4567 = _mm_unpackhi_epi16(r.fVec, g.fVec), // r4 g4 r5 g5 r6 g6 r7 g7
349 ba0123 = _mm_unpacklo_epi16(b.fVec, a.fVec),
350 ba4567 = _mm_unpackhi_epi16(b.fVec, a.fVec);
351
352 _mm_storeu_si128((__m128i*)ptr + 0, _mm_unpacklo_epi32(rg0123, ba0123));
353 _mm_storeu_si128((__m128i*)ptr + 1, _mm_unpackhi_epi32(rg0123, ba0123));
354 _mm_storeu_si128((__m128i*)ptr + 2, _mm_unpacklo_epi32(rg4567, ba4567));
355 _mm_storeu_si128((__m128i*)ptr + 3, _mm_unpackhi_epi32(rg4567, ba4567));
356 }
357
Mike Klein7c78f3a2016-10-19 09:21:11 -0400358 AI SkNx operator + (const SkNx& o) const { return _mm_add_epi16(fVec, o.fVec); }
359 AI SkNx operator - (const SkNx& o) const { return _mm_sub_epi16(fVec, o.fVec); }
360 AI SkNx operator * (const SkNx& o) const { return _mm_mullo_epi16(fVec, o.fVec); }
mtklein115acee2015-04-14 14:02:52 -0700361
Mike Klein7c78f3a2016-10-19 09:21:11 -0400362 AI SkNx operator << (int bits) const { return _mm_slli_epi16(fVec, bits); }
363 AI SkNx operator >> (int bits) const { return _mm_srli_epi16(fVec, bits); }
mtklein115acee2015-04-14 14:02:52 -0700364
Mike Klein7c78f3a2016-10-19 09:21:11 -0400365 AI static SkNx Min(const SkNx& a, const SkNx& b) {
mtklein27e517a2015-05-14 17:53:04 -0700366 // No unsigned _mm_min_epu16, so we'll shift into a space where we can use the
367 // signed version, _mm_min_epi16, then shift back.
368 const uint16_t top = 0x8000; // Keep this separate from _mm_set1_epi16 or MSVC will whine.
369 const __m128i top_8x = _mm_set1_epi16(top);
370 return _mm_add_epi8(top_8x, _mm_min_epi16(_mm_sub_epi8(a.fVec, top_8x),
371 _mm_sub_epi8(b.fVec, top_8x)));
372 }
373
Mike Klein7c78f3a2016-10-19 09:21:11 -0400374 AI SkNx thenElse(const SkNx& t, const SkNx& e) const {
mtklein4be181e2015-07-14 10:54:19 -0700375 return _mm_or_si128(_mm_and_si128 (fVec, t.fVec),
376 _mm_andnot_si128(fVec, e.fVec));
377 }
378
Mike Klein7c78f3a2016-10-19 09:21:11 -0400379 AI uint16_t operator[](int k) const {
mtklein115acee2015-04-14 14:02:52 -0700380 SkASSERT(0 <= k && k < 8);
mtkleine4c0bee2016-02-09 10:35:27 -0800381 union { __m128i v; uint16_t us[8]; } pun = {fVec};
382 return pun.us[k&7];
mtklein115acee2015-04-14 14:02:52 -0700383 }
mtkleind2ffd362015-05-12 06:11:21 -0700384
385 __m128i fVec;
386};
387
388template <>
mtklein6f37b4a2015-12-14 11:25:18 -0800389class SkNx<4, uint8_t> {
390public:
Mike Klein7c78f3a2016-10-19 09:21:11 -0400391 AI SkNx() {}
392 AI SkNx(const __m128i& vec) : fVec(vec) {}
393 AI SkNx(uint8_t a, uint8_t b, uint8_t c, uint8_t d)
herbfd5a2602016-03-01 07:01:23 -0800394 : fVec(_mm_setr_epi8(a,b,c,d, 0,0,0,0, 0,0,0,0, 0,0,0,0)) {}
395
396
Mike Klein7c78f3a2016-10-19 09:21:11 -0400397 AI static SkNx Load(const void* ptr) { return _mm_cvtsi32_si128(*(const int*)ptr); }
398 AI void store(void* ptr) const { *(int*)ptr = _mm_cvtsi128_si32(fVec); }
mtklein6f37b4a2015-12-14 11:25:18 -0800399
Mike Klein7c78f3a2016-10-19 09:21:11 -0400400 AI uint8_t operator[](int k) const {
herbfd5a2602016-03-01 07:01:23 -0800401 SkASSERT(0 <= k && k < 4);
402 union { __m128i v; uint8_t us[16]; } pun = {fVec};
403 return pun.us[k&3];
404 }
405
mtklein6f37b4a2015-12-14 11:25:18 -0800406 // TODO as needed
407
408 __m128i fVec;
409};
410
411template <>
mtklein6c221b42015-11-20 13:53:19 -0800412class SkNx<16, uint8_t> {
mtkleind2ffd362015-05-12 06:11:21 -0700413public:
Mike Klein7c78f3a2016-10-19 09:21:11 -0400414 AI SkNx(const __m128i& vec) : fVec(vec) {}
mtkleind2ffd362015-05-12 06:11:21 -0700415
Mike Klein7c78f3a2016-10-19 09:21:11 -0400416 AI SkNx() {}
417 AI SkNx(uint8_t val) : fVec(_mm_set1_epi8(val)) {}
418 AI static SkNx Load(const void* ptr) { return _mm_loadu_si128((const __m128i*)ptr); }
419 AI SkNx(uint8_t a, uint8_t b, uint8_t c, uint8_t d,
420 uint8_t e, uint8_t f, uint8_t g, uint8_t h,
421 uint8_t i, uint8_t j, uint8_t k, uint8_t l,
422 uint8_t m, uint8_t n, uint8_t o, uint8_t p)
mtkleind2ffd362015-05-12 06:11:21 -0700423 : fVec(_mm_setr_epi8(a,b,c,d, e,f,g,h, i,j,k,l, m,n,o,p)) {}
424
Mike Klein7c78f3a2016-10-19 09:21:11 -0400425 AI void store(void* ptr) const { _mm_storeu_si128((__m128i*)ptr, fVec); }
mtkleind2ffd362015-05-12 06:11:21 -0700426
Mike Klein7c78f3a2016-10-19 09:21:11 -0400427 AI SkNx saturatedAdd(const SkNx& o) const { return _mm_adds_epu8(fVec, o.fVec); }
mtklein6cbf18c2015-05-12 15:48:09 -0700428
Mike Klein7c78f3a2016-10-19 09:21:11 -0400429 AI SkNx operator + (const SkNx& o) const { return _mm_add_epi8(fVec, o.fVec); }
430 AI SkNx operator - (const SkNx& o) const { return _mm_sub_epi8(fVec, o.fVec); }
mtkleind2ffd362015-05-12 06:11:21 -0700431
Mike Klein7c78f3a2016-10-19 09:21:11 -0400432 AI static SkNx Min(const SkNx& a, const SkNx& b) { return _mm_min_epu8(a.fVec, b.fVec); }
433 AI SkNx operator < (const SkNx& o) const {
mtkleinb5e86112015-06-24 15:18:39 -0700434 // There's no unsigned _mm_cmplt_epu8, so we flip the sign bits then use a signed compare.
435 auto flip = _mm_set1_epi8(char(0x80));
436 return _mm_cmplt_epi8(_mm_xor_si128(flip, fVec), _mm_xor_si128(flip, o.fVec));
437 }
mtklein27e517a2015-05-14 17:53:04 -0700438
Mike Klein7c78f3a2016-10-19 09:21:11 -0400439 AI uint8_t operator[](int k) const {
mtkleind2ffd362015-05-12 06:11:21 -0700440 SkASSERT(0 <= k && k < 16);
mtkleine4c0bee2016-02-09 10:35:27 -0800441 union { __m128i v; uint8_t us[16]; } pun = {fVec};
442 return pun.us[k&15];
mtkleind2ffd362015-05-12 06:11:21 -0700443 }
444
Mike Klein7c78f3a2016-10-19 09:21:11 -0400445 AI SkNx thenElse(const SkNx& t, const SkNx& e) const {
mtkleinb5e86112015-06-24 15:18:39 -0700446 return _mm_or_si128(_mm_and_si128 (fVec, t.fVec),
447 _mm_andnot_si128(fVec, e.fVec));
448 }
449
mtklein115acee2015-04-14 14:02:52 -0700450 __m128i fVec;
451};
mtkleinc9adb052015-03-30 10:50:27 -0700452
Mike Klein04adfda2016-10-12 09:52:55 -0400453#if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_AVX2
454
Mike Klein04adfda2016-10-12 09:52:55 -0400455 template <>
456 class SkNx<8, uint8_t> {
457 public:
Mike Klein7c78f3a2016-10-19 09:21:11 -0400458 AI SkNx(const __m128i& vec) : fVec(vec) {}
Mike Klein04adfda2016-10-12 09:52:55 -0400459
Mike Klein7c78f3a2016-10-19 09:21:11 -0400460 AI SkNx() {}
461 AI SkNx(uint8_t v) : fVec(_mm_set1_epi8(v)) {}
462 AI SkNx(uint8_t a, uint8_t b, uint8_t c, uint8_t d,
463 uint8_t e, uint8_t f, uint8_t g, uint8_t h)
Mike Klein04adfda2016-10-12 09:52:55 -0400464 : fVec(_mm_setr_epi8(a,b,c,d, e,f,g,h, 0,0,0,0, 0,0,0,0)) {}
465
466
Mike Klein7c78f3a2016-10-19 09:21:11 -0400467 AI static SkNx Load(const void* ptr) { return _mm_loadl_epi64((const __m128i*)ptr); }
468 AI void store(void* ptr) const { _mm_storel_epi64((__m128i*)ptr, fVec); }
Mike Klein04adfda2016-10-12 09:52:55 -0400469
Mike Klein7c78f3a2016-10-19 09:21:11 -0400470 AI uint8_t operator[](int k) const {
Mike Klein04adfda2016-10-12 09:52:55 -0400471 SkASSERT(0 <= k && k < 8);
472 union { __m128i v; uint8_t us[16]; } pun = {fVec};
473 return pun.us[k&7];
474 }
475
476 __m128i fVec;
477 };
478
479 template <>
480 class SkNx<8, int32_t> {
481 public:
Mike Klein7c78f3a2016-10-19 09:21:11 -0400482 AI SkNx(const __m256i& vec) : fVec(vec) {}
Mike Klein04adfda2016-10-12 09:52:55 -0400483
Mike Klein7c78f3a2016-10-19 09:21:11 -0400484 AI SkNx() {}
485 AI SkNx(int32_t v) : fVec(_mm256_set1_epi32(v)) {}
486 AI SkNx(int32_t a, int32_t b, int32_t c, int32_t d,
487 int32_t e, int32_t f, int32_t g, int32_t h)
Mike Klein04adfda2016-10-12 09:52:55 -0400488 : fVec(_mm256_setr_epi32(a,b,c,d, e,f,g,h)) {}
489
Mike Klein7c78f3a2016-10-19 09:21:11 -0400490 AI static SkNx Load(const void* ptr) { return _mm256_loadu_si256((const __m256i*)ptr); }
491 AI void store(void* ptr) const { _mm256_storeu_si256((__m256i*)ptr, fVec); }
Mike Klein04adfda2016-10-12 09:52:55 -0400492
Mike Klein7c78f3a2016-10-19 09:21:11 -0400493 AI SkNx operator + (const SkNx& o) const { return _mm256_add_epi32(fVec, o.fVec); }
494 AI SkNx operator - (const SkNx& o) const { return _mm256_sub_epi32(fVec, o.fVec); }
495 AI SkNx operator * (const SkNx& o) const { return _mm256_mullo_epi32(fVec, o.fVec); }
Mike Klein04adfda2016-10-12 09:52:55 -0400496
Mike Klein7c78f3a2016-10-19 09:21:11 -0400497 AI SkNx operator & (const SkNx& o) const { return _mm256_and_si256(fVec, o.fVec); }
498 AI SkNx operator | (const SkNx& o) const { return _mm256_or_si256(fVec, o.fVec); }
499 AI SkNx operator ^ (const SkNx& o) const { return _mm256_xor_si256(fVec, o.fVec); }
Mike Klein04adfda2016-10-12 09:52:55 -0400500
Mike Klein7c78f3a2016-10-19 09:21:11 -0400501 AI SkNx operator << (int bits) const { return _mm256_slli_epi32(fVec, bits); }
502 AI SkNx operator >> (int bits) const { return _mm256_srai_epi32(fVec, bits); }
Mike Klein04adfda2016-10-12 09:52:55 -0400503
Mike Klein7c78f3a2016-10-19 09:21:11 -0400504 AI int32_t operator[](int k) const {
Mike Klein04adfda2016-10-12 09:52:55 -0400505 SkASSERT(0 <= k && k < 8);
506 union { __m256i v; int32_t is[8]; } pun = {fVec};
507 return pun.is[k&7];
508 }
509
510 __m256i fVec;
511 };
512
513 template <>
514 class SkNx<8, uint32_t> {
515 public:
Mike Klein7c78f3a2016-10-19 09:21:11 -0400516 AI SkNx(const __m256i& vec) : fVec(vec) {}
Mike Klein04adfda2016-10-12 09:52:55 -0400517
Mike Klein7c78f3a2016-10-19 09:21:11 -0400518 AI SkNx() {}
519 AI SkNx(uint32_t v) : fVec(_mm256_set1_epi32(v)) {}
520 AI SkNx(uint32_t a, uint32_t b, uint32_t c, uint32_t d,
521 uint32_t e, uint32_t f, uint32_t g, uint32_t h)
Mike Klein04adfda2016-10-12 09:52:55 -0400522 : fVec(_mm256_setr_epi32(a,b,c,d, e,f,g,h)) {}
523
Mike Klein7c78f3a2016-10-19 09:21:11 -0400524 AI static SkNx Load(const void* ptr) { return _mm256_loadu_si256((const __m256i*)ptr); }
525 AI void store(void* ptr) const { _mm256_storeu_si256((__m256i*)ptr, fVec); }
Mike Klein04adfda2016-10-12 09:52:55 -0400526
Mike Klein7c78f3a2016-10-19 09:21:11 -0400527 AI SkNx operator + (const SkNx& o) const { return _mm256_add_epi32(fVec, o.fVec); }
528 AI SkNx operator - (const SkNx& o) const { return _mm256_sub_epi32(fVec, o.fVec); }
529 AI SkNx operator * (const SkNx& o) const { return _mm256_mullo_epi32(fVec, o.fVec); }
Mike Klein04adfda2016-10-12 09:52:55 -0400530
Mike Klein7c78f3a2016-10-19 09:21:11 -0400531 AI SkNx operator & (const SkNx& o) const { return _mm256_and_si256(fVec, o.fVec); }
532 AI SkNx operator | (const SkNx& o) const { return _mm256_or_si256(fVec, o.fVec); }
533 AI SkNx operator ^ (const SkNx& o) const { return _mm256_xor_si256(fVec, o.fVec); }
Mike Klein04adfda2016-10-12 09:52:55 -0400534
Mike Klein7c78f3a2016-10-19 09:21:11 -0400535 AI SkNx operator << (int bits) const { return _mm256_slli_epi32(fVec, bits); }
536 AI SkNx operator >> (int bits) const { return _mm256_srli_epi32(fVec, bits); }
Mike Klein04adfda2016-10-12 09:52:55 -0400537
Mike Klein7c78f3a2016-10-19 09:21:11 -0400538 AI uint32_t operator[](int k) const {
Mike Klein04adfda2016-10-12 09:52:55 -0400539 SkASSERT(0 <= k && k < 8);
540 union { __m256i v; uint32_t us[8]; } pun = {fVec};
541 return pun.us[k&7];
542 }
543
544 __m256i fVec;
545 };
546
547 template <>
548 class SkNx<8, float> {
549 public:
Mike Klein7c78f3a2016-10-19 09:21:11 -0400550 AI SkNx(const __m256& vec) : fVec(vec) {}
Mike Klein04adfda2016-10-12 09:52:55 -0400551
Mike Klein7c78f3a2016-10-19 09:21:11 -0400552 AI SkNx() {}
553 AI SkNx(float val) : fVec(_mm256_set1_ps(val)) {}
554 AI SkNx(float a, float b, float c, float d,
555 float e, float f, float g, float h) : fVec(_mm256_setr_ps(a,b,c,d,e,f,g,h)) {}
Mike Klein04adfda2016-10-12 09:52:55 -0400556
Mike Klein7c78f3a2016-10-19 09:21:11 -0400557 AI SkNx(const SkNx_abi<8,float>& a) : fVec(a.vec) {}
558 AI operator SkNx_abi<8,float>() const { return { fVec }; }
Mike Klein5ec55902016-10-15 01:18:45 -0400559
Mike Klein7c78f3a2016-10-19 09:21:11 -0400560 AI static SkNx Load(const void* ptr) { return _mm256_loadu_ps((const float*)ptr); }
561 AI void store(void* ptr) const { _mm256_storeu_ps((float*)ptr, fVec); }
Mike Klein04adfda2016-10-12 09:52:55 -0400562
Mike Klein7c78f3a2016-10-19 09:21:11 -0400563 AI SkNx operator+(const SkNx& o) const { return _mm256_add_ps(fVec, o.fVec); }
564 AI SkNx operator-(const SkNx& o) const { return _mm256_sub_ps(fVec, o.fVec); }
565 AI SkNx operator*(const SkNx& o) const { return _mm256_mul_ps(fVec, o.fVec); }
566 AI SkNx operator/(const SkNx& o) const { return _mm256_div_ps(fVec, o.fVec); }
Mike Klein04adfda2016-10-12 09:52:55 -0400567
Mike Klein7c78f3a2016-10-19 09:21:11 -0400568 AI SkNx operator==(const SkNx& o) const { return _mm256_cmp_ps(fVec, o.fVec, _CMP_EQ_OQ); }
569 AI SkNx operator!=(const SkNx& o) const { return _mm256_cmp_ps(fVec, o.fVec, _CMP_NEQ_OQ); }
570 AI SkNx operator <(const SkNx& o) const { return _mm256_cmp_ps(fVec, o.fVec, _CMP_LT_OQ); }
571 AI SkNx operator >(const SkNx& o) const { return _mm256_cmp_ps(fVec, o.fVec, _CMP_GT_OQ); }
572 AI SkNx operator<=(const SkNx& o) const { return _mm256_cmp_ps(fVec, o.fVec, _CMP_LE_OQ); }
573 AI SkNx operator>=(const SkNx& o) const { return _mm256_cmp_ps(fVec, o.fVec, _CMP_GE_OQ); }
Mike Klein04adfda2016-10-12 09:52:55 -0400574
Mike Klein7c78f3a2016-10-19 09:21:11 -0400575 AI static SkNx Min(const SkNx& l, const SkNx& r) { return _mm256_min_ps(l.fVec, r.fVec); }
576 AI static SkNx Max(const SkNx& l, const SkNx& r) { return _mm256_max_ps(l.fVec, r.fVec); }
Mike Klein04adfda2016-10-12 09:52:55 -0400577
Mike Klein7c78f3a2016-10-19 09:21:11 -0400578 AI SkNx sqrt() const { return _mm256_sqrt_ps (fVec); }
579 AI SkNx rsqrt() const { return _mm256_rsqrt_ps(fVec); }
580 AI SkNx invert() const { return _mm256_rcp_ps (fVec); }
Mike Klein04adfda2016-10-12 09:52:55 -0400581
Mike Klein7c78f3a2016-10-19 09:21:11 -0400582 AI float operator[](int k) const {
Mike Klein04adfda2016-10-12 09:52:55 -0400583 SkASSERT(0 <= k && k < 8);
584 union { __m256 v; float fs[8]; } pun = {fVec};
585 return pun.fs[k&7];
586 }
587
Mike Klein7c78f3a2016-10-19 09:21:11 -0400588 AI SkNx thenElse(const SkNx& t, const SkNx& e) const {
Mike Klein04adfda2016-10-12 09:52:55 -0400589 return _mm256_blendv_ps(e.fVec, t.fVec, fVec);
590 }
591
592 __m256 fVec;
593 };
594
Mike Klein7c78f3a2016-10-19 09:21:11 -0400595 AI static void SkNx_split(const Sk8f& v, Sk4f* lo, Sk4f* hi) {
Mike Klein04adfda2016-10-12 09:52:55 -0400596 *lo = _mm256_extractf128_ps(v.fVec, 0);
597 *hi = _mm256_extractf128_ps(v.fVec, 1);
598 }
599
Mike Klein7c78f3a2016-10-19 09:21:11 -0400600 AI static Sk8f SkNx_join(const Sk4f& lo, const Sk4f& hi) {
Mike Klein04adfda2016-10-12 09:52:55 -0400601 return _mm256_insertf128_ps(_mm256_castps128_ps256(lo.fVec), hi.fVec, 1);
602 }
603
Mike Klein7c78f3a2016-10-19 09:21:11 -0400604 AI static Sk8f SkNx_fma(const Sk8f& a, const Sk8f& b, const Sk8f& c) {
Mike Klein04adfda2016-10-12 09:52:55 -0400605 return _mm256_fmadd_ps(a.fVec, b.fVec, c.fVec);
606 }
607
Mike Klein7c78f3a2016-10-19 09:21:11 -0400608 template<> AI /*static*/ Sk8f SkNx_cast<float>(const Sk8b& src) {
Mike Klein04adfda2016-10-12 09:52:55 -0400609 return _mm256_cvtepi32_ps(_mm256_cvtepu8_epi32(src.fVec));
610 }
611
Mike Klein7c78f3a2016-10-19 09:21:11 -0400612 template<> AI /*static*/ Sk8f SkNx_cast<float>(const Sk8i& src) {
Mike Klein04adfda2016-10-12 09:52:55 -0400613 return _mm256_cvtepi32_ps(src.fVec);
614 }
615
Mike Klein7c78f3a2016-10-19 09:21:11 -0400616 template<> AI /*static*/ Sk8i SkNx_cast<int>(const Sk8f& src) {
Mike Klein04adfda2016-10-12 09:52:55 -0400617 return _mm256_cvttps_epi32(src.fVec);
618 }
619
Mike Klein7c78f3a2016-10-19 09:21:11 -0400620 template<> AI /*static*/ Sk8i SkNx_cast<int>(const Sk8h& src) {
Mike Klein04adfda2016-10-12 09:52:55 -0400621 return _mm256_cvtepu16_epi32(src.fVec);
622 }
Mike Klein7c78f3a2016-10-19 09:21:11 -0400623 template<> AI /*static*/ Sk8h SkNx_cast<uint16_t>(const Sk8i& src) {
Mike Klein04adfda2016-10-12 09:52:55 -0400624 __m128i lo = _mm256_extractf128_si256(src.fVec, 0),
625 hi = _mm256_extractf128_si256(src.fVec, 1);
626 return _mm_packus_epi32(lo, hi);
627 }
628
Mike Klein04adfda2016-10-12 09:52:55 -0400629#endif
630
Mike Klein7c78f3a2016-10-19 09:21:11 -0400631template<> AI /*static*/ Sk4f SkNx_cast<float, int32_t>(const Sk4i& src) {
mtklein0cf795f2016-02-17 07:23:36 -0800632 return _mm_cvtepi32_ps(src.fVec);
633}
Mike Klein7c78f3a2016-10-19 09:21:11 -0400634template<> AI /*static*/ Sk4f SkNx_cast<float, uint32_t>(const Sk4u& src) {
mtkleind05a8752016-07-29 10:10:15 -0700635 return SkNx_cast<float>(Sk4i::Load(&src));
636}
mtklein0cf795f2016-02-17 07:23:36 -0800637
Mike Klein7c78f3a2016-10-19 09:21:11 -0400638template <> AI /*static*/ Sk4i SkNx_cast<int32_t, float>(const Sk4f& src) {
mtklein0cf795f2016-02-17 07:23:36 -0800639 return _mm_cvttps_epi32(src.fVec);
640}
mtklein6c221b42015-11-20 13:53:19 -0800641
Mike Klein7c78f3a2016-10-19 09:21:11 -0400642template<> AI /*static*/ Sk4h SkNx_cast<uint16_t, int32_t>(const Sk4i& src) {
mtklein036e1832016-07-15 07:45:53 -0700643#if 0 && SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE41
644 // TODO: This seems to be causing code generation problems. Investigate?
645 return _mm_packus_epi32(src.fVec);
646#elif SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSSE3
mtklein629f25a2016-02-08 05:54:38 -0800647 // With SSSE3, we can just shuffle the low 2 bytes from each lane right into place.
648 const int _ = ~0;
mtklein036e1832016-07-15 07:45:53 -0700649 return _mm_shuffle_epi8(src.fVec, _mm_setr_epi8(0,1, 4,5, 8,9, 12,13, _,_,_,_,_,_,_,_));
mtklein629f25a2016-02-08 05:54:38 -0800650#else
mtklein036e1832016-07-15 07:45:53 -0700651 // With SSE2, we have to sign extend our input, making _mm_packs_epi32 do the pack we want.
652 __m128i x = _mm_srai_epi32(_mm_slli_epi32(src.fVec, 16), 16);
653 return _mm_packs_epi32(x,x);
mtklein629f25a2016-02-08 05:54:38 -0800654#endif
mtklein2d340f22016-02-06 19:38:39 -0800655}
656
Mike Klein7c78f3a2016-10-19 09:21:11 -0400657template<> AI /*static*/ Sk4h SkNx_cast<uint16_t, float>(const Sk4f& src) {
mtkleind05a8752016-07-29 10:10:15 -0700658 return SkNx_cast<uint16_t>(SkNx_cast<int32_t>(src));
mtklein036e1832016-07-15 07:45:53 -0700659}
660
Mike Klein7c78f3a2016-10-19 09:21:11 -0400661template<> AI /*static*/ Sk4b SkNx_cast<uint8_t, float>(const Sk4f& src) {
mtklein97120a72016-02-12 14:19:06 -0800662 auto _32 = _mm_cvttps_epi32(src.fVec);
663#if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSSE3
664 const int _ = ~0;
665 return _mm_shuffle_epi8(_32, _mm_setr_epi8(0,4,8,12, _,_,_,_, _,_,_,_, _,_,_,_));
666#else
667 auto _16 = _mm_packus_epi16(_32, _32);
668 return _mm_packus_epi16(_16, _16);
669#endif
mtkleinc1eb3112016-02-11 08:10:22 -0800670}
671
Mike Klein7c78f3a2016-10-19 09:21:11 -0400672template<> AI /*static*/ Sk4f SkNx_cast<float, uint8_t>(const Sk4b& src) {
mtklein97120a72016-02-12 14:19:06 -0800673#if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSSE3
674 const int _ = ~0;
675 auto _32 = _mm_shuffle_epi8(src.fVec, _mm_setr_epi8(0,_,_,_, 1,_,_,_, 2,_,_,_, 3,_,_,_));
676#else
677 auto _16 = _mm_unpacklo_epi8(src.fVec, _mm_setzero_si128()),
678 _32 = _mm_unpacklo_epi16(_16, _mm_setzero_si128());
679#endif
680 return _mm_cvtepi32_ps(_32);
mtkleinc1eb3112016-02-11 08:10:22 -0800681}
mtklein97120a72016-02-12 14:19:06 -0800682
Mike Klein7c78f3a2016-10-19 09:21:11 -0400683template<> AI /*static*/ Sk4f SkNx_cast<float, uint16_t>(const Sk4h& src) {
mtklein97120a72016-02-12 14:19:06 -0800684 auto _32 = _mm_unpacklo_epi16(src.fVec, _mm_setzero_si128());
685 return _mm_cvtepi32_ps(_32);
mtklein2d340f22016-02-06 19:38:39 -0800686}
687
Mike Klein7c78f3a2016-10-19 09:21:11 -0400688template<> AI /*static*/ Sk16b SkNx_cast<uint8_t, float>(const Sk16f& src) {
mtkleinf8f90e42016-03-21 10:04:46 -0700689 Sk8f ab, cd;
690 SkNx_split(src, &ab, &cd);
691
692 Sk4f a,b,c,d;
693 SkNx_split(ab, &a, &b);
694 SkNx_split(cd, &c, &d);
695
696 return _mm_packus_epi16(_mm_packus_epi16(_mm_cvttps_epi32(a.fVec),
697 _mm_cvttps_epi32(b.fVec)),
698 _mm_packus_epi16(_mm_cvttps_epi32(c.fVec),
699 _mm_cvttps_epi32(d.fVec)));
mtklein97120a72016-02-12 14:19:06 -0800700}
701
Mike Klein7c78f3a2016-10-19 09:21:11 -0400702template<> AI /*static*/ Sk4h SkNx_cast<uint16_t, uint8_t>(const Sk4b& src) {
mtklein97120a72016-02-12 14:19:06 -0800703 return _mm_unpacklo_epi8(src.fVec, _mm_setzero_si128());
704}
705
Mike Klein7c78f3a2016-10-19 09:21:11 -0400706template<> AI /*static*/ Sk4b SkNx_cast<uint8_t, uint16_t>(const Sk4h& src) {
mtklein97120a72016-02-12 14:19:06 -0800707 return _mm_packus_epi16(src.fVec, src.fVec);
mtklein550e9b02016-01-20 11:55:51 -0800708}
709
Mike Klein7c78f3a2016-10-19 09:21:11 -0400710template<> AI /*static*/ Sk4i SkNx_cast<int32_t, uint16_t>(const Sk4h& src) {
mtklein58e389b2016-07-15 07:00:11 -0700711 return _mm_unpacklo_epi16(src.fVec, _mm_setzero_si128());
712}
713
Mike Klein7c78f3a2016-10-19 09:21:11 -0400714template<> AI /*static*/ Sk4b SkNx_cast<uint8_t, int32_t>(const Sk4i& src) {
msarett7d3ff712016-07-12 14:55:45 -0700715 return _mm_packus_epi16(_mm_packus_epi16(src.fVec, src.fVec), src.fVec);
716}
717
Mike Klein7c78f3a2016-10-19 09:21:11 -0400718template<> AI /*static*/ Sk4i SkNx_cast<int32_t, uint32_t>(const Sk4u& src) {
msarett15ee3de2016-08-02 11:30:30 -0700719 return src.fVec;
720}
721
Mike Klein7c78f3a2016-10-19 09:21:11 -0400722AI static Sk4i Sk4f_round(const Sk4f& x) {
msarett7d3ff712016-07-12 14:55:45 -0700723 return _mm_cvtps_epi32(x.fVec);
724}
725
Mike Klein1e764642016-10-14 17:09:03 -0400726} // namespace
727
mtkleinc9adb052015-03-30 10:50:27 -0700728#endif//SkNx_sse_DEFINED