blob: 394115626ebed36d23db53c8683d6fcea47dda2c [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
Mike Klein1e764642016-10-14 17:09:03 -040016namespace {
17
mtkleinc9adb052015-03-30 10:50:27 -070018template <>
mtklein6c221b42015-11-20 13:53:19 -080019class SkNx<2, float> {
mtkleinc9adb052015-03-30 10:50:27 -070020public:
Mike Klein7c78f3a2016-10-19 09:21:11 -040021 AI SkNx(const __m128& vec) : fVec(vec) {}
mtkleinc9adb052015-03-30 10:50:27 -070022
Mike Klein7c78f3a2016-10-19 09:21:11 -040023 AI SkNx() {}
24 AI SkNx(float val) : fVec(_mm_set1_ps(val)) {}
25 AI static SkNx Load(const void* ptr) {
mtklein507ef6d2016-01-31 08:02:47 -080026 return _mm_castsi128_ps(_mm_loadl_epi64((const __m128i*)ptr));
mtkleinc9adb052015-03-30 10:50:27 -070027 }
Mike Klein7c78f3a2016-10-19 09:21:11 -040028 AI SkNx(float a, float b) : fVec(_mm_setr_ps(a,b,0,0)) {}
mtkleinc9adb052015-03-30 10:50:27 -070029
Mike Klein7c78f3a2016-10-19 09:21:11 -040030 AI void store(void* ptr) const { _mm_storel_pi((__m64*)ptr, fVec); }
mtkleinc9adb052015-03-30 10:50:27 -070031
Mike Klein7c78f3a2016-10-19 09:21:11 -040032 AI SkNx operator + (const SkNx& o) const { return _mm_add_ps(fVec, o.fVec); }
33 AI SkNx operator - (const SkNx& o) const { return _mm_sub_ps(fVec, o.fVec); }
34 AI SkNx operator * (const SkNx& o) const { return _mm_mul_ps(fVec, o.fVec); }
35 AI SkNx operator / (const SkNx& o) const { return _mm_div_ps(fVec, o.fVec); }
mtkleinc9adb052015-03-30 10:50:27 -070036
Mike Klein7c78f3a2016-10-19 09:21:11 -040037 AI SkNx operator == (const SkNx& o) const { return _mm_cmpeq_ps (fVec, o.fVec); }
38 AI SkNx operator != (const SkNx& o) const { return _mm_cmpneq_ps(fVec, o.fVec); }
39 AI SkNx operator < (const SkNx& o) const { return _mm_cmplt_ps (fVec, o.fVec); }
40 AI SkNx operator > (const SkNx& o) const { return _mm_cmpgt_ps (fVec, o.fVec); }
41 AI SkNx operator <= (const SkNx& o) const { return _mm_cmple_ps (fVec, o.fVec); }
42 AI SkNx operator >= (const SkNx& o) const { return _mm_cmpge_ps (fVec, o.fVec); }
mtkleinc9adb052015-03-30 10:50:27 -070043
Mike Klein7c78f3a2016-10-19 09:21:11 -040044 AI static SkNx Min(const SkNx& l, const SkNx& r) { return _mm_min_ps(l.fVec, r.fVec); }
45 AI static SkNx Max(const SkNx& l, const SkNx& r) { return _mm_max_ps(l.fVec, r.fVec); }
mtkleinc9adb052015-03-30 10:50:27 -070046
Mike Klein7c78f3a2016-10-19 09:21:11 -040047 AI SkNx sqrt() const { return _mm_sqrt_ps (fVec); }
48 AI SkNx rsqrt() const { return _mm_rsqrt_ps(fVec); }
49 AI SkNx invert() const { return _mm_rcp_ps(fVec); }
mtkleinc9adb052015-03-30 10:50:27 -070050
Mike Klein7c78f3a2016-10-19 09:21:11 -040051 AI float operator[](int k) const {
mtkleinc9adb052015-03-30 10:50:27 -070052 SkASSERT(0 <= k && k < 2);
53 union { __m128 v; float fs[4]; } pun = {fVec};
mtkleina156a8f2015-04-03 06:16:13 -070054 return pun.fs[k&1];
mtkleinc9adb052015-03-30 10:50:27 -070055 }
56
Mike Klein7c78f3a2016-10-19 09:21:11 -040057 AI bool allTrue() const { return 0xff == (_mm_movemask_epi8(_mm_castps_si128(fVec)) & 0xff); }
58 AI bool anyTrue() const { return 0x00 != (_mm_movemask_epi8(_mm_castps_si128(fVec)) & 0xff); }
mtkleinb5e86112015-06-24 15:18:39 -070059
mtkleinc9adb052015-03-30 10:50:27 -070060 __m128 fVec;
61};
62
63template <>
mtklein6c221b42015-11-20 13:53:19 -080064class SkNx<4, float> {
mtkleinc9adb052015-03-30 10:50:27 -070065public:
Mike Klein7c78f3a2016-10-19 09:21:11 -040066 AI SkNx(const __m128& vec) : fVec(vec) {}
mtkleinc9adb052015-03-30 10:50:27 -070067
Mike Klein7c78f3a2016-10-19 09:21:11 -040068 AI SkNx() {}
69 AI SkNx(float val) : fVec( _mm_set1_ps(val) ) {}
70 AI SkNx(float a, float b, float c, float d) : fVec(_mm_setr_ps(a,b,c,d)) {}
mtkleinc9adb052015-03-30 10:50:27 -070071
Mike Klein7c78f3a2016-10-19 09:21:11 -040072 AI static SkNx Load(const void* ptr) { return _mm_loadu_ps((const float*)ptr); }
73 AI void store(void* ptr) const { _mm_storeu_ps((float*)ptr, fVec); }
mtklein9db43ac2015-12-01 07:10:21 -080074
Mike Klein7c78f3a2016-10-19 09:21:11 -040075 AI static void Load4(const void* ptr, SkNx* r, SkNx* g, SkNx* b, SkNx* a) {
Mike Klein33cbfd72016-10-06 11:09:27 -040076 __m128 v0 = _mm_loadu_ps(((float*)ptr) + 0),
77 v1 = _mm_loadu_ps(((float*)ptr) + 4),
78 v2 = _mm_loadu_ps(((float*)ptr) + 8),
79 v3 = _mm_loadu_ps(((float*)ptr) + 12);
80 _MM_TRANSPOSE4_PS(v0, v1, v2, v3);
81 *r = v0;
82 *g = v1;
83 *b = v2;
84 *a = v3;
85 }
Mike Klein7c78f3a2016-10-19 09:21:11 -040086 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 -040087 __m128 v0 = r.fVec,
88 v1 = g.fVec,
89 v2 = b.fVec,
90 v3 = a.fVec;
91 _MM_TRANSPOSE4_PS(v0, v1, v2, v3);
92 _mm_storeu_ps(((float*) dst) + 0, v0);
93 _mm_storeu_ps(((float*) dst) + 4, v1);
94 _mm_storeu_ps(((float*) dst) + 8, v2);
95 _mm_storeu_ps(((float*) dst) + 12, v3);
96 }
97
Mike Klein7c78f3a2016-10-19 09:21:11 -040098 AI SkNx operator + (const SkNx& o) const { return _mm_add_ps(fVec, o.fVec); }
99 AI SkNx operator - (const SkNx& o) const { return _mm_sub_ps(fVec, o.fVec); }
100 AI SkNx operator * (const SkNx& o) const { return _mm_mul_ps(fVec, o.fVec); }
101 AI SkNx operator / (const SkNx& o) const { return _mm_div_ps(fVec, o.fVec); }
mtkleinc9adb052015-03-30 10:50:27 -0700102
Mike Klein7c78f3a2016-10-19 09:21:11 -0400103 AI SkNx operator == (const SkNx& o) const { return _mm_cmpeq_ps (fVec, o.fVec); }
104 AI SkNx operator != (const SkNx& o) const { return _mm_cmpneq_ps(fVec, o.fVec); }
105 AI SkNx operator < (const SkNx& o) const { return _mm_cmplt_ps (fVec, o.fVec); }
106 AI SkNx operator > (const SkNx& o) const { return _mm_cmpgt_ps (fVec, o.fVec); }
107 AI SkNx operator <= (const SkNx& o) const { return _mm_cmple_ps (fVec, o.fVec); }
108 AI SkNx operator >= (const SkNx& o) const { return _mm_cmpge_ps (fVec, o.fVec); }
mtkleinc9adb052015-03-30 10:50:27 -0700109
Mike Klein7c78f3a2016-10-19 09:21:11 -0400110 AI static SkNx Min(const SkNx& l, const SkNx& r) { return _mm_min_ps(l.fVec, r.fVec); }
111 AI static SkNx Max(const SkNx& l, const SkNx& r) { return _mm_max_ps(l.fVec, r.fVec); }
mtkleinc9adb052015-03-30 10:50:27 -0700112
Mike Klein7c78f3a2016-10-19 09:21:11 -0400113 AI SkNx abs() const { return _mm_andnot_ps(_mm_set1_ps(-0.0f), fVec); }
114 AI SkNx floor() const {
mtklein5608e2e2016-07-11 09:59:21 -0700115 #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE41
116 return _mm_floor_ps(fVec);
117 #else
mtklein244a6532016-04-19 14:21:30 -0700118 // Emulate _mm_floor_ps() with SSE2:
119 // - roundtrip through integers via truncation
120 // - subtract 1 if that's too big (possible for negative values).
121 // This restricts the domain of our inputs to a maximum somehwere around 2^31.
122 // Seems plenty big.
123 __m128 roundtrip = _mm_cvtepi32_ps(_mm_cvttps_epi32(fVec));
124 __m128 too_big = _mm_cmpgt_ps(roundtrip, fVec);
125 return _mm_sub_ps(roundtrip, _mm_and_ps(too_big, _mm_set1_ps(1.0f)));
mtklein5608e2e2016-07-11 09:59:21 -0700126 #endif
mtklein244a6532016-04-19 14:21:30 -0700127 }
mtkleinc33065a2016-01-15 12:16:40 -0800128
Mike Klein7c78f3a2016-10-19 09:21:11 -0400129 AI SkNx sqrt() const { return _mm_sqrt_ps (fVec); }
130 AI SkNx rsqrt() const { return _mm_rsqrt_ps(fVec); }
131 AI SkNx invert() const { return _mm_rcp_ps(fVec); }
mtkleinc9adb052015-03-30 10:50:27 -0700132
Mike Klein7c78f3a2016-10-19 09:21:11 -0400133 AI float operator[](int k) const {
mtkleinc9adb052015-03-30 10:50:27 -0700134 SkASSERT(0 <= k && k < 4);
135 union { __m128 v; float fs[4]; } pun = {fVec};
mtkleina156a8f2015-04-03 06:16:13 -0700136 return pun.fs[k&3];
mtkleinc9adb052015-03-30 10:50:27 -0700137 }
138
Mike Klein7c78f3a2016-10-19 09:21:11 -0400139 AI bool allTrue() const { return 0xffff == _mm_movemask_epi8(_mm_castps_si128(fVec)); }
140 AI bool anyTrue() const { return 0x0000 != _mm_movemask_epi8(_mm_castps_si128(fVec)); }
mtkleinb5e86112015-06-24 15:18:39 -0700141
Mike Klein7c78f3a2016-10-19 09:21:11 -0400142 AI SkNx thenElse(const SkNx& t, const SkNx& e) const {
mtklein5608e2e2016-07-11 09:59:21 -0700143 #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE41
msarettd2809572016-06-20 06:07:45 -0700144 return _mm_blendv_ps(e.fVec, t.fVec, fVec);
mtklein5608e2e2016-07-11 09:59:21 -0700145 #else
mtklein2aab22a2015-06-26 10:46:31 -0700146 return _mm_or_ps(_mm_and_ps (fVec, t.fVec),
147 _mm_andnot_ps(fVec, e.fVec));
mtklein5608e2e2016-07-11 09:59:21 -0700148 #endif
mtklein2aab22a2015-06-26 10:46:31 -0700149 }
150
mtkleinc9adb052015-03-30 10:50:27 -0700151 __m128 fVec;
152};
153
mtklein115acee2015-04-14 14:02:52 -0700154template <>
mtkleind05a8752016-07-29 10:10:15 -0700155class SkNx<4, int32_t> {
mtklein8273ca42016-02-09 11:32:51 -0800156public:
Mike Klein7c78f3a2016-10-19 09:21:11 -0400157 AI SkNx(const __m128i& vec) : fVec(vec) {}
mtklein8273ca42016-02-09 11:32:51 -0800158
Mike Klein7c78f3a2016-10-19 09:21:11 -0400159 AI SkNx() {}
160 AI SkNx(int32_t val) : fVec(_mm_set1_epi32(val)) {}
161 AI static SkNx Load(const void* ptr) { return _mm_loadu_si128((const __m128i*)ptr); }
162 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 -0800163
Mike Klein7c78f3a2016-10-19 09:21:11 -0400164 AI void store(void* ptr) const { _mm_storeu_si128((__m128i*)ptr, fVec); }
mtklein8273ca42016-02-09 11:32:51 -0800165
Mike Klein7c78f3a2016-10-19 09:21:11 -0400166 AI SkNx operator + (const SkNx& o) const { return _mm_add_epi32(fVec, o.fVec); }
167 AI SkNx operator - (const SkNx& o) const { return _mm_sub_epi32(fVec, o.fVec); }
168 AI SkNx operator * (const SkNx& o) const {
mtklein8273ca42016-02-09 11:32:51 -0800169 __m128i mul20 = _mm_mul_epu32(fVec, o.fVec),
170 mul31 = _mm_mul_epu32(_mm_srli_si128(fVec, 4), _mm_srli_si128(o.fVec, 4));
171 return _mm_unpacklo_epi32(_mm_shuffle_epi32(mul20, _MM_SHUFFLE(0,0,2,0)),
172 _mm_shuffle_epi32(mul31, _MM_SHUFFLE(0,0,2,0)));
173 }
174
Mike Klein7c78f3a2016-10-19 09:21:11 -0400175 AI SkNx operator & (const SkNx& o) const { return _mm_and_si128(fVec, o.fVec); }
176 AI SkNx operator | (const SkNx& o) const { return _mm_or_si128(fVec, o.fVec); }
177 AI SkNx operator ^ (const SkNx& o) const { return _mm_xor_si128(fVec, o.fVec); }
mtklein64f061a2016-06-17 12:09:16 -0700178
Mike Klein7c78f3a2016-10-19 09:21:11 -0400179 AI SkNx operator << (int bits) const { return _mm_slli_epi32(fVec, bits); }
180 AI SkNx operator >> (int bits) const { return _mm_srai_epi32(fVec, bits); }
mtklein8273ca42016-02-09 11:32:51 -0800181
Mike Klein7c78f3a2016-10-19 09:21:11 -0400182 AI SkNx operator == (const SkNx& o) const { return _mm_cmpeq_epi32 (fVec, o.fVec); }
183 AI SkNx operator < (const SkNx& o) const { return _mm_cmplt_epi32 (fVec, o.fVec); }
184 AI SkNx operator > (const SkNx& o) const { return _mm_cmpgt_epi32 (fVec, o.fVec); }
mtklein58e389b2016-07-15 07:00:11 -0700185
Mike Klein7c78f3a2016-10-19 09:21:11 -0400186 AI int32_t operator[](int k) const {
mtklein8273ca42016-02-09 11:32:51 -0800187 SkASSERT(0 <= k && k < 4);
mtkleind05a8752016-07-29 10:10:15 -0700188 union { __m128i v; int32_t is[4]; } pun = {fVec};
mtklein8273ca42016-02-09 11:32:51 -0800189 return pun.is[k&3];
190 }
mtklein8273ca42016-02-09 11:32:51 -0800191
Mike Klein7c78f3a2016-10-19 09:21:11 -0400192 AI SkNx thenElse(const SkNx& t, const SkNx& e) const {
mtklein58e389b2016-07-15 07:00:11 -0700193 #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE41
194 return _mm_blendv_epi8(e.fVec, t.fVec, fVec);
195 #else
196 return _mm_or_si128(_mm_and_si128 (fVec, t.fVec),
197 _mm_andnot_si128(fVec, e.fVec));
198 #endif
199 }
200
mtklein8273ca42016-02-09 11:32:51 -0800201 __m128i fVec;
202};
203
204template <>
mtkleind05a8752016-07-29 10:10:15 -0700205class SkNx<4, uint32_t> {
206public:
Mike Klein7c78f3a2016-10-19 09:21:11 -0400207 AI SkNx(const __m128i& vec) : fVec(vec) {}
mtkleind05a8752016-07-29 10:10:15 -0700208
Mike Klein7c78f3a2016-10-19 09:21:11 -0400209 AI SkNx() {}
210 AI SkNx(uint32_t val) : fVec(_mm_set1_epi32(val)) {}
211 AI static SkNx Load(const void* ptr) { return _mm_loadu_si128((const __m128i*)ptr); }
212 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 -0700213
Mike Klein7c78f3a2016-10-19 09:21:11 -0400214 AI void store(void* ptr) const { _mm_storeu_si128((__m128i*)ptr, fVec); }
mtkleind05a8752016-07-29 10:10:15 -0700215
Mike Klein7c78f3a2016-10-19 09:21:11 -0400216 AI SkNx operator + (const SkNx& o) const { return _mm_add_epi32(fVec, o.fVec); }
217 AI SkNx operator - (const SkNx& o) const { return _mm_sub_epi32(fVec, o.fVec); }
mtkleind05a8752016-07-29 10:10:15 -0700218 // Not quite sure how to best do operator * in SSE2. We probably don't use it.
219
Mike Klein7c78f3a2016-10-19 09:21:11 -0400220 AI SkNx operator & (const SkNx& o) const { return _mm_and_si128(fVec, o.fVec); }
221 AI SkNx operator | (const SkNx& o) const { return _mm_or_si128(fVec, o.fVec); }
222 AI SkNx operator ^ (const SkNx& o) const { return _mm_xor_si128(fVec, o.fVec); }
mtkleind05a8752016-07-29 10:10:15 -0700223
Mike Klein7c78f3a2016-10-19 09:21:11 -0400224 AI SkNx operator << (int bits) const { return _mm_slli_epi32(fVec, bits); }
225 AI SkNx operator >> (int bits) const { return _mm_srli_epi32(fVec, bits); }
mtkleind05a8752016-07-29 10:10:15 -0700226
Mike Klein7c78f3a2016-10-19 09:21:11 -0400227 AI SkNx operator == (const SkNx& o) const { return _mm_cmpeq_epi32 (fVec, o.fVec); }
mtkleind05a8752016-07-29 10:10:15 -0700228 // operator < and > take a little extra fiddling to make work for unsigned ints.
229
Mike Klein7c78f3a2016-10-19 09:21:11 -0400230 AI uint32_t operator[](int k) const {
mtkleind05a8752016-07-29 10:10:15 -0700231 SkASSERT(0 <= k && k < 4);
232 union { __m128i v; uint32_t us[4]; } pun = {fVec};
233 return pun.us[k&3];
234 }
235
Mike Klein7c78f3a2016-10-19 09:21:11 -0400236 AI SkNx thenElse(const SkNx& t, const SkNx& e) const {
mtkleind05a8752016-07-29 10:10:15 -0700237 #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE41
238 return _mm_blendv_epi8(e.fVec, t.fVec, fVec);
239 #else
240 return _mm_or_si128(_mm_and_si128 (fVec, t.fVec),
241 _mm_andnot_si128(fVec, e.fVec));
242 #endif
243 }
244
245 __m128i fVec;
246};
247
248
249template <>
mtklein6c221b42015-11-20 13:53:19 -0800250class SkNx<4, uint16_t> {
mtklein115acee2015-04-14 14:02:52 -0700251public:
Mike Klein7c78f3a2016-10-19 09:21:11 -0400252 AI SkNx(const __m128i& vec) : fVec(vec) {}
mtklein115acee2015-04-14 14:02:52 -0700253
Mike Klein7c78f3a2016-10-19 09:21:11 -0400254 AI SkNx() {}
255 AI SkNx(uint16_t val) : fVec(_mm_set1_epi16(val)) {}
256 AI SkNx(uint16_t a, uint16_t b, uint16_t c, uint16_t d)
257 : fVec(_mm_setr_epi16(a,b,c,d,0,0,0,0)) {}
mtklein115acee2015-04-14 14:02:52 -0700258
Mike Klein7c78f3a2016-10-19 09:21:11 -0400259 AI static SkNx Load(const void* ptr) { return _mm_loadl_epi64((const __m128i*)ptr); }
260 AI void store(void* ptr) const { _mm_storel_epi64((__m128i*)ptr, fVec); }
mtklein115acee2015-04-14 14:02:52 -0700261
Mike Klein7c78f3a2016-10-19 09:21:11 -0400262 AI static void Load4(const void* ptr, SkNx* r, SkNx* g, SkNx* b, SkNx* a) {
Mike Klein33cbfd72016-10-06 11:09:27 -0400263 __m128i lo = _mm_loadu_si128(((__m128i*)ptr) + 0),
264 hi = _mm_loadu_si128(((__m128i*)ptr) + 1);
265 __m128i even = _mm_unpacklo_epi16(lo, hi), // r0 r2 g0 g2 b0 b2 a0 a2
266 odd = _mm_unpackhi_epi16(lo, hi); // r1 r3 ...
267 __m128i rg = _mm_unpacklo_epi16(even, odd), // r0 r1 r2 r3 g0 g1 g2 g3
268 ba = _mm_unpackhi_epi16(even, odd); // b0 b1 ... a0 a1 ...
269 *r = rg;
270 *g = _mm_srli_si128(rg, 8);
271 *b = ba;
272 *a = _mm_srli_si128(ba, 8);
273 }
Mike Klein7c78f3a2016-10-19 09:21:11 -0400274 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 -0400275 __m128i rg = _mm_unpacklo_epi16(r.fVec, g.fVec);
276 __m128i ba = _mm_unpacklo_epi16(b.fVec, a.fVec);
277 __m128i lo = _mm_unpacklo_epi32(rg, ba);
278 __m128i hi = _mm_unpackhi_epi32(rg, ba);
279 _mm_storeu_si128(((__m128i*) dst) + 0, lo);
280 _mm_storeu_si128(((__m128i*) dst) + 1, hi);
281 }
282
Mike Klein7c78f3a2016-10-19 09:21:11 -0400283 AI SkNx operator + (const SkNx& o) const { return _mm_add_epi16(fVec, o.fVec); }
284 AI SkNx operator - (const SkNx& o) const { return _mm_sub_epi16(fVec, o.fVec); }
285 AI SkNx operator * (const SkNx& o) const { return _mm_mullo_epi16(fVec, o.fVec); }
mtklein115acee2015-04-14 14:02:52 -0700286
Mike Klein7c78f3a2016-10-19 09:21:11 -0400287 AI SkNx operator << (int bits) const { return _mm_slli_epi16(fVec, bits); }
288 AI SkNx operator >> (int bits) const { return _mm_srli_epi16(fVec, bits); }
mtklein115acee2015-04-14 14:02:52 -0700289
Mike Klein7c78f3a2016-10-19 09:21:11 -0400290 AI uint16_t operator[](int k) const {
mtklein115acee2015-04-14 14:02:52 -0700291 SkASSERT(0 <= k && k < 4);
mtkleine4c0bee2016-02-09 10:35:27 -0800292 union { __m128i v; uint16_t us[8]; } pun = {fVec};
293 return pun.us[k&3];
mtklein115acee2015-04-14 14:02:52 -0700294 }
mtkleind2ffd362015-05-12 06:11:21 -0700295
mtklein115acee2015-04-14 14:02:52 -0700296 __m128i fVec;
297};
298
299template <>
mtklein6c221b42015-11-20 13:53:19 -0800300class SkNx<8, uint16_t> {
mtklein115acee2015-04-14 14:02:52 -0700301public:
Mike Klein7c78f3a2016-10-19 09:21:11 -0400302 AI SkNx(const __m128i& vec) : fVec(vec) {}
mtklein115acee2015-04-14 14:02:52 -0700303
Mike Klein7c78f3a2016-10-19 09:21:11 -0400304 AI SkNx() {}
305 AI SkNx(uint16_t val) : fVec(_mm_set1_epi16(val)) {}
306 AI SkNx(uint16_t a, uint16_t b, uint16_t c, uint16_t d,
307 uint16_t e, uint16_t f, uint16_t g, uint16_t h)
308 : fVec(_mm_setr_epi16(a,b,c,d,e,f,g,h)) {}
mtklein115acee2015-04-14 14:02:52 -0700309
Mike Klein7c78f3a2016-10-19 09:21:11 -0400310 AI static SkNx Load(const void* ptr) { return _mm_loadu_si128((const __m128i*)ptr); }
311 AI void store(void* ptr) const { _mm_storeu_si128((__m128i*)ptr, fVec); }
mtklein115acee2015-04-14 14:02:52 -0700312
Mike Klein7c78f3a2016-10-19 09:21:11 -0400313 AI static void Load4(const void* ptr, SkNx* r, SkNx* g, SkNx* b, SkNx* a) {
Mike Klein04adfda2016-10-12 09:52:55 -0400314 // TODO: AVX2 version
315 __m128i _01 = _mm_loadu_si128(((__m128i*)ptr) + 0),
316 _23 = _mm_loadu_si128(((__m128i*)ptr) + 1),
317 _45 = _mm_loadu_si128(((__m128i*)ptr) + 2),
318 _67 = _mm_loadu_si128(((__m128i*)ptr) + 3);
319
320 __m128i _02 = _mm_unpacklo_epi16(_01, _23), // r0 r2 g0 g2 b0 b2 a0 a2
321 _13 = _mm_unpackhi_epi16(_01, _23), // r1 r3 g1 g3 b1 b3 a1 a3
322 _46 = _mm_unpacklo_epi16(_45, _67),
323 _57 = _mm_unpackhi_epi16(_45, _67);
324
325 __m128i rg0123 = _mm_unpacklo_epi16(_02, _13), // r0 r1 r2 r3 g0 g1 g2 g3
326 ba0123 = _mm_unpackhi_epi16(_02, _13), // b0 b1 b2 b3 a0 a1 a2 a3
327 rg4567 = _mm_unpacklo_epi16(_46, _57),
328 ba4567 = _mm_unpackhi_epi16(_46, _57);
329
330 *r = _mm_unpacklo_epi64(rg0123, rg4567);
331 *g = _mm_unpackhi_epi64(rg0123, rg4567);
332 *b = _mm_unpacklo_epi64(ba0123, ba4567);
333 *a = _mm_unpackhi_epi64(ba0123, ba4567);
334 }
Mike Klein7c78f3a2016-10-19 09:21:11 -0400335 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 -0400336 // TODO: AVX2 version
337 __m128i rg0123 = _mm_unpacklo_epi16(r.fVec, g.fVec), // r0 g0 r1 g1 r2 g2 r3 g3
338 rg4567 = _mm_unpackhi_epi16(r.fVec, g.fVec), // r4 g4 r5 g5 r6 g6 r7 g7
339 ba0123 = _mm_unpacklo_epi16(b.fVec, a.fVec),
340 ba4567 = _mm_unpackhi_epi16(b.fVec, a.fVec);
341
342 _mm_storeu_si128((__m128i*)ptr + 0, _mm_unpacklo_epi32(rg0123, ba0123));
343 _mm_storeu_si128((__m128i*)ptr + 1, _mm_unpackhi_epi32(rg0123, ba0123));
344 _mm_storeu_si128((__m128i*)ptr + 2, _mm_unpacklo_epi32(rg4567, ba4567));
345 _mm_storeu_si128((__m128i*)ptr + 3, _mm_unpackhi_epi32(rg4567, ba4567));
346 }
347
Mike Klein7c78f3a2016-10-19 09:21:11 -0400348 AI SkNx operator + (const SkNx& o) const { return _mm_add_epi16(fVec, o.fVec); }
349 AI SkNx operator - (const SkNx& o) const { return _mm_sub_epi16(fVec, o.fVec); }
350 AI SkNx operator * (const SkNx& o) const { return _mm_mullo_epi16(fVec, o.fVec); }
mtklein115acee2015-04-14 14:02:52 -0700351
Mike Klein7c78f3a2016-10-19 09:21:11 -0400352 AI SkNx operator << (int bits) const { return _mm_slli_epi16(fVec, bits); }
353 AI SkNx operator >> (int bits) const { return _mm_srli_epi16(fVec, bits); }
mtklein115acee2015-04-14 14:02:52 -0700354
Mike Klein7c78f3a2016-10-19 09:21:11 -0400355 AI static SkNx Min(const SkNx& a, const SkNx& b) {
mtklein27e517a2015-05-14 17:53:04 -0700356 // No unsigned _mm_min_epu16, so we'll shift into a space where we can use the
357 // signed version, _mm_min_epi16, then shift back.
358 const uint16_t top = 0x8000; // Keep this separate from _mm_set1_epi16 or MSVC will whine.
359 const __m128i top_8x = _mm_set1_epi16(top);
360 return _mm_add_epi8(top_8x, _mm_min_epi16(_mm_sub_epi8(a.fVec, top_8x),
361 _mm_sub_epi8(b.fVec, top_8x)));
362 }
363
Mike Klein7c78f3a2016-10-19 09:21:11 -0400364 AI SkNx thenElse(const SkNx& t, const SkNx& e) const {
mtklein4be181e2015-07-14 10:54:19 -0700365 return _mm_or_si128(_mm_and_si128 (fVec, t.fVec),
366 _mm_andnot_si128(fVec, e.fVec));
367 }
368
Mike Klein7c78f3a2016-10-19 09:21:11 -0400369 AI uint16_t operator[](int k) const {
mtklein115acee2015-04-14 14:02:52 -0700370 SkASSERT(0 <= k && k < 8);
mtkleine4c0bee2016-02-09 10:35:27 -0800371 union { __m128i v; uint16_t us[8]; } pun = {fVec};
372 return pun.us[k&7];
mtklein115acee2015-04-14 14:02:52 -0700373 }
mtkleind2ffd362015-05-12 06:11:21 -0700374
375 __m128i fVec;
376};
377
378template <>
mtklein6f37b4a2015-12-14 11:25:18 -0800379class SkNx<4, uint8_t> {
380public:
Mike Klein7c78f3a2016-10-19 09:21:11 -0400381 AI SkNx() {}
382 AI SkNx(const __m128i& vec) : fVec(vec) {}
383 AI SkNx(uint8_t a, uint8_t b, uint8_t c, uint8_t d)
herbfd5a2602016-03-01 07:01:23 -0800384 : fVec(_mm_setr_epi8(a,b,c,d, 0,0,0,0, 0,0,0,0, 0,0,0,0)) {}
385
386
Mike Klein7c78f3a2016-10-19 09:21:11 -0400387 AI static SkNx Load(const void* ptr) { return _mm_cvtsi32_si128(*(const int*)ptr); }
388 AI void store(void* ptr) const { *(int*)ptr = _mm_cvtsi128_si32(fVec); }
mtklein6f37b4a2015-12-14 11:25:18 -0800389
Mike Klein7c78f3a2016-10-19 09:21:11 -0400390 AI uint8_t operator[](int k) const {
herbfd5a2602016-03-01 07:01:23 -0800391 SkASSERT(0 <= k && k < 4);
392 union { __m128i v; uint8_t us[16]; } pun = {fVec};
393 return pun.us[k&3];
394 }
395
mtklein6f37b4a2015-12-14 11:25:18 -0800396 // TODO as needed
397
398 __m128i fVec;
399};
400
401template <>
mtklein6c221b42015-11-20 13:53:19 -0800402class SkNx<16, uint8_t> {
mtkleind2ffd362015-05-12 06:11:21 -0700403public:
Mike Klein7c78f3a2016-10-19 09:21:11 -0400404 AI SkNx(const __m128i& vec) : fVec(vec) {}
mtkleind2ffd362015-05-12 06:11:21 -0700405
Mike Klein7c78f3a2016-10-19 09:21:11 -0400406 AI SkNx() {}
407 AI SkNx(uint8_t val) : fVec(_mm_set1_epi8(val)) {}
408 AI static SkNx Load(const void* ptr) { return _mm_loadu_si128((const __m128i*)ptr); }
409 AI SkNx(uint8_t a, uint8_t b, uint8_t c, uint8_t d,
410 uint8_t e, uint8_t f, uint8_t g, uint8_t h,
411 uint8_t i, uint8_t j, uint8_t k, uint8_t l,
412 uint8_t m, uint8_t n, uint8_t o, uint8_t p)
mtkleind2ffd362015-05-12 06:11:21 -0700413 : fVec(_mm_setr_epi8(a,b,c,d, e,f,g,h, i,j,k,l, m,n,o,p)) {}
414
Mike Klein7c78f3a2016-10-19 09:21:11 -0400415 AI void store(void* ptr) const { _mm_storeu_si128((__m128i*)ptr, fVec); }
mtkleind2ffd362015-05-12 06:11:21 -0700416
Mike Klein7c78f3a2016-10-19 09:21:11 -0400417 AI SkNx saturatedAdd(const SkNx& o) const { return _mm_adds_epu8(fVec, o.fVec); }
mtklein6cbf18c2015-05-12 15:48:09 -0700418
Mike Klein7c78f3a2016-10-19 09:21:11 -0400419 AI SkNx operator + (const SkNx& o) const { return _mm_add_epi8(fVec, o.fVec); }
420 AI SkNx operator - (const SkNx& o) const { return _mm_sub_epi8(fVec, o.fVec); }
mtkleind2ffd362015-05-12 06:11:21 -0700421
Mike Klein7c78f3a2016-10-19 09:21:11 -0400422 AI static SkNx Min(const SkNx& a, const SkNx& b) { return _mm_min_epu8(a.fVec, b.fVec); }
423 AI SkNx operator < (const SkNx& o) const {
mtkleinb5e86112015-06-24 15:18:39 -0700424 // There's no unsigned _mm_cmplt_epu8, so we flip the sign bits then use a signed compare.
425 auto flip = _mm_set1_epi8(char(0x80));
426 return _mm_cmplt_epi8(_mm_xor_si128(flip, fVec), _mm_xor_si128(flip, o.fVec));
427 }
mtklein27e517a2015-05-14 17:53:04 -0700428
Mike Klein7c78f3a2016-10-19 09:21:11 -0400429 AI uint8_t operator[](int k) const {
mtkleind2ffd362015-05-12 06:11:21 -0700430 SkASSERT(0 <= k && k < 16);
mtkleine4c0bee2016-02-09 10:35:27 -0800431 union { __m128i v; uint8_t us[16]; } pun = {fVec};
432 return pun.us[k&15];
mtkleind2ffd362015-05-12 06:11:21 -0700433 }
434
Mike Klein7c78f3a2016-10-19 09:21:11 -0400435 AI SkNx thenElse(const SkNx& t, const SkNx& e) const {
mtkleinb5e86112015-06-24 15:18:39 -0700436 return _mm_or_si128(_mm_and_si128 (fVec, t.fVec),
437 _mm_andnot_si128(fVec, e.fVec));
438 }
439
mtklein115acee2015-04-14 14:02:52 -0700440 __m128i fVec;
441};
mtkleinc9adb052015-03-30 10:50:27 -0700442
Mike Klein04adfda2016-10-12 09:52:55 -0400443#if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_AVX2
444
Mike Klein04adfda2016-10-12 09:52:55 -0400445 template <>
446 class SkNx<8, uint8_t> {
447 public:
Mike Klein7c78f3a2016-10-19 09:21:11 -0400448 AI SkNx(const __m128i& vec) : fVec(vec) {}
Mike Klein04adfda2016-10-12 09:52:55 -0400449
Mike Klein7c78f3a2016-10-19 09:21:11 -0400450 AI SkNx() {}
451 AI SkNx(uint8_t v) : fVec(_mm_set1_epi8(v)) {}
452 AI SkNx(uint8_t a, uint8_t b, uint8_t c, uint8_t d,
453 uint8_t e, uint8_t f, uint8_t g, uint8_t h)
Mike Klein04adfda2016-10-12 09:52:55 -0400454 : fVec(_mm_setr_epi8(a,b,c,d, e,f,g,h, 0,0,0,0, 0,0,0,0)) {}
455
456
Mike Klein7c78f3a2016-10-19 09:21:11 -0400457 AI static SkNx Load(const void* ptr) { return _mm_loadl_epi64((const __m128i*)ptr); }
458 AI void store(void* ptr) const { _mm_storel_epi64((__m128i*)ptr, fVec); }
Mike Klein04adfda2016-10-12 09:52:55 -0400459
Mike Klein7c78f3a2016-10-19 09:21:11 -0400460 AI uint8_t operator[](int k) const {
Mike Klein04adfda2016-10-12 09:52:55 -0400461 SkASSERT(0 <= k && k < 8);
462 union { __m128i v; uint8_t us[16]; } pun = {fVec};
463 return pun.us[k&7];
464 }
465
466 __m128i fVec;
467 };
468
469 template <>
470 class SkNx<8, int32_t> {
471 public:
Mike Klein7c78f3a2016-10-19 09:21:11 -0400472 AI SkNx(const __m256i& vec) : fVec(vec) {}
Mike Klein04adfda2016-10-12 09:52:55 -0400473
Mike Klein7c78f3a2016-10-19 09:21:11 -0400474 AI SkNx() {}
475 AI SkNx(int32_t v) : fVec(_mm256_set1_epi32(v)) {}
476 AI SkNx(int32_t a, int32_t b, int32_t c, int32_t d,
477 int32_t e, int32_t f, int32_t g, int32_t h)
Mike Klein04adfda2016-10-12 09:52:55 -0400478 : fVec(_mm256_setr_epi32(a,b,c,d, e,f,g,h)) {}
479
Mike Klein7c78f3a2016-10-19 09:21:11 -0400480 AI static SkNx Load(const void* ptr) { return _mm256_loadu_si256((const __m256i*)ptr); }
481 AI void store(void* ptr) const { _mm256_storeu_si256((__m256i*)ptr, fVec); }
Mike Klein04adfda2016-10-12 09:52:55 -0400482
Mike Klein7c78f3a2016-10-19 09:21:11 -0400483 AI SkNx operator + (const SkNx& o) const { return _mm256_add_epi32(fVec, o.fVec); }
484 AI SkNx operator - (const SkNx& o) const { return _mm256_sub_epi32(fVec, o.fVec); }
485 AI SkNx operator * (const SkNx& o) const { return _mm256_mullo_epi32(fVec, o.fVec); }
Mike Klein04adfda2016-10-12 09:52:55 -0400486
Mike Klein7c78f3a2016-10-19 09:21:11 -0400487 AI SkNx operator & (const SkNx& o) const { return _mm256_and_si256(fVec, o.fVec); }
488 AI SkNx operator | (const SkNx& o) const { return _mm256_or_si256(fVec, o.fVec); }
489 AI SkNx operator ^ (const SkNx& o) const { return _mm256_xor_si256(fVec, o.fVec); }
Mike Klein04adfda2016-10-12 09:52:55 -0400490
Mike Klein7c78f3a2016-10-19 09:21:11 -0400491 AI SkNx operator << (int bits) const { return _mm256_slli_epi32(fVec, bits); }
492 AI SkNx operator >> (int bits) const { return _mm256_srai_epi32(fVec, bits); }
Mike Klein04adfda2016-10-12 09:52:55 -0400493
Mike Klein7c78f3a2016-10-19 09:21:11 -0400494 AI int32_t operator[](int k) const {
Mike Klein04adfda2016-10-12 09:52:55 -0400495 SkASSERT(0 <= k && k < 8);
496 union { __m256i v; int32_t is[8]; } pun = {fVec};
497 return pun.is[k&7];
498 }
499
500 __m256i fVec;
501 };
502
503 template <>
504 class SkNx<8, uint32_t> {
505 public:
Mike Klein7c78f3a2016-10-19 09:21:11 -0400506 AI SkNx(const __m256i& vec) : fVec(vec) {}
Mike Klein04adfda2016-10-12 09:52:55 -0400507
Mike Klein7c78f3a2016-10-19 09:21:11 -0400508 AI SkNx() {}
509 AI SkNx(uint32_t v) : fVec(_mm256_set1_epi32(v)) {}
510 AI SkNx(uint32_t a, uint32_t b, uint32_t c, uint32_t d,
511 uint32_t e, uint32_t f, uint32_t g, uint32_t h)
Mike Klein04adfda2016-10-12 09:52:55 -0400512 : fVec(_mm256_setr_epi32(a,b,c,d, e,f,g,h)) {}
513
Mike Klein7c78f3a2016-10-19 09:21:11 -0400514 AI static SkNx Load(const void* ptr) { return _mm256_loadu_si256((const __m256i*)ptr); }
515 AI void store(void* ptr) const { _mm256_storeu_si256((__m256i*)ptr, fVec); }
Mike Klein04adfda2016-10-12 09:52:55 -0400516
Mike Klein7c78f3a2016-10-19 09:21:11 -0400517 AI SkNx operator + (const SkNx& o) const { return _mm256_add_epi32(fVec, o.fVec); }
518 AI SkNx operator - (const SkNx& o) const { return _mm256_sub_epi32(fVec, o.fVec); }
519 AI SkNx operator * (const SkNx& o) const { return _mm256_mullo_epi32(fVec, o.fVec); }
Mike Klein04adfda2016-10-12 09:52:55 -0400520
Mike Klein7c78f3a2016-10-19 09:21:11 -0400521 AI SkNx operator & (const SkNx& o) const { return _mm256_and_si256(fVec, o.fVec); }
522 AI SkNx operator | (const SkNx& o) const { return _mm256_or_si256(fVec, o.fVec); }
523 AI SkNx operator ^ (const SkNx& o) const { return _mm256_xor_si256(fVec, o.fVec); }
Mike Klein04adfda2016-10-12 09:52:55 -0400524
Mike Klein7c78f3a2016-10-19 09:21:11 -0400525 AI SkNx operator << (int bits) const { return _mm256_slli_epi32(fVec, bits); }
526 AI SkNx operator >> (int bits) const { return _mm256_srli_epi32(fVec, bits); }
Mike Klein04adfda2016-10-12 09:52:55 -0400527
Mike Klein7c78f3a2016-10-19 09:21:11 -0400528 AI uint32_t operator[](int k) const {
Mike Klein04adfda2016-10-12 09:52:55 -0400529 SkASSERT(0 <= k && k < 8);
530 union { __m256i v; uint32_t us[8]; } pun = {fVec};
531 return pun.us[k&7];
532 }
533
534 __m256i fVec;
535 };
536
mtkleina4a44882016-11-04 13:20:07 -0700537 // _mm256_unpack{lo,hi}_pd() auto-casting to and from __m256d.
538 AI static __m256 unpacklo_pd(__m256 x, __m256 y) {
539 return _mm256_castpd_ps(_mm256_unpacklo_pd(_mm256_castps_pd(x), _mm256_castps_pd(y)));
540 }
541 AI static __m256 unpackhi_pd(__m256 x, __m256 y) {
542 return _mm256_castpd_ps(_mm256_unpackhi_pd(_mm256_castps_pd(x), _mm256_castps_pd(y)));
543 }
544
Mike Klein04adfda2016-10-12 09:52:55 -0400545 template <>
546 class SkNx<8, float> {
547 public:
Mike Klein7c78f3a2016-10-19 09:21:11 -0400548 AI SkNx(const __m256& vec) : fVec(vec) {}
Mike Klein04adfda2016-10-12 09:52:55 -0400549
Mike Klein7c78f3a2016-10-19 09:21:11 -0400550 AI SkNx() {}
551 AI SkNx(float val) : fVec(_mm256_set1_ps(val)) {}
552 AI SkNx(float a, float b, float c, float d,
553 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 -0400554
Mike Klein7c78f3a2016-10-19 09:21:11 -0400555 AI static SkNx Load(const void* ptr) { return _mm256_loadu_ps((const float*)ptr); }
556 AI void store(void* ptr) const { _mm256_storeu_ps((float*)ptr, fVec); }
Mike Klein04adfda2016-10-12 09:52:55 -0400557
mtkleina4a44882016-11-04 13:20:07 -0700558 AI static void Store4(void* ptr,
559 const SkNx& r, const SkNx& g, const SkNx& b, const SkNx& a) {
560 __m256 rg0145 = _mm256_unpacklo_ps(r.fVec, g.fVec), // r0 g0 r1 g1 | r4 g4 r5 g5
561 rg2367 = _mm256_unpackhi_ps(r.fVec, g.fVec), // r2 ... | r6 ...
562 ba0145 = _mm256_unpacklo_ps(b.fVec, a.fVec), // b0 a0 b1 a1 | b4 a4 b5 a5
563 ba2367 = _mm256_unpackhi_ps(b.fVec, a.fVec); // b2 ... | b6 ...
564
565 __m256 _04 = unpacklo_pd(rg0145, ba0145), // r0 g0 b0 a0 | r4 g4 b4 a4
566 _15 = unpackhi_pd(rg0145, ba0145), // r1 ... | r5 ...
567 _26 = unpacklo_pd(rg2367, ba2367), // r2 ... | r6 ...
568 _37 = unpackhi_pd(rg2367, ba2367); // r3 ... | r7 ...
569
570 __m256 _01 = _mm256_permute2f128_ps(_04, _15, 16), // 16 == 010 000 == lo, lo
571 _23 = _mm256_permute2f128_ps(_26, _37, 16),
572 _45 = _mm256_permute2f128_ps(_04, _15, 25), // 25 == 011 001 == hi, hi
573 _67 = _mm256_permute2f128_ps(_26, _37, 25);
574
575 _mm256_storeu_ps((float*)ptr + 0*8, _01);
576 _mm256_storeu_ps((float*)ptr + 1*8, _23);
577 _mm256_storeu_ps((float*)ptr + 2*8, _45);
578 _mm256_storeu_ps((float*)ptr + 3*8, _67);
579 }
580
Mike Klein7c78f3a2016-10-19 09:21:11 -0400581 AI SkNx operator+(const SkNx& o) const { return _mm256_add_ps(fVec, o.fVec); }
582 AI SkNx operator-(const SkNx& o) const { return _mm256_sub_ps(fVec, o.fVec); }
583 AI SkNx operator*(const SkNx& o) const { return _mm256_mul_ps(fVec, o.fVec); }
584 AI SkNx operator/(const SkNx& o) const { return _mm256_div_ps(fVec, o.fVec); }
Mike Klein04adfda2016-10-12 09:52:55 -0400585
Mike Klein7c78f3a2016-10-19 09:21:11 -0400586 AI SkNx operator==(const SkNx& o) const { return _mm256_cmp_ps(fVec, o.fVec, _CMP_EQ_OQ); }
587 AI SkNx operator!=(const SkNx& o) const { return _mm256_cmp_ps(fVec, o.fVec, _CMP_NEQ_OQ); }
588 AI SkNx operator <(const SkNx& o) const { return _mm256_cmp_ps(fVec, o.fVec, _CMP_LT_OQ); }
589 AI SkNx operator >(const SkNx& o) const { return _mm256_cmp_ps(fVec, o.fVec, _CMP_GT_OQ); }
590 AI SkNx operator<=(const SkNx& o) const { return _mm256_cmp_ps(fVec, o.fVec, _CMP_LE_OQ); }
591 AI SkNx operator>=(const SkNx& o) const { return _mm256_cmp_ps(fVec, o.fVec, _CMP_GE_OQ); }
Mike Klein04adfda2016-10-12 09:52:55 -0400592
Mike Klein7c78f3a2016-10-19 09:21:11 -0400593 AI static SkNx Min(const SkNx& l, const SkNx& r) { return _mm256_min_ps(l.fVec, r.fVec); }
594 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 -0400595
Mike Klein7c78f3a2016-10-19 09:21:11 -0400596 AI SkNx sqrt() const { return _mm256_sqrt_ps (fVec); }
597 AI SkNx rsqrt() const { return _mm256_rsqrt_ps(fVec); }
598 AI SkNx invert() const { return _mm256_rcp_ps (fVec); }
Mike Klein04adfda2016-10-12 09:52:55 -0400599
Mike Klein2e35e8a2016-11-18 15:47:22 -0500600 AI SkNx abs() const { return _mm256_andnot_ps(_mm256_set1_ps(-0.0f), fVec); }
Mike Kleinb273fc42016-11-17 15:42:22 -0500601 AI SkNx floor() const { return _mm256_floor_ps(fVec); }
602
Mike Klein7c78f3a2016-10-19 09:21:11 -0400603 AI float operator[](int k) const {
Mike Klein04adfda2016-10-12 09:52:55 -0400604 SkASSERT(0 <= k && k < 8);
605 union { __m256 v; float fs[8]; } pun = {fVec};
606 return pun.fs[k&7];
607 }
608
Mike Klein7c78f3a2016-10-19 09:21:11 -0400609 AI SkNx thenElse(const SkNx& t, const SkNx& e) const {
Mike Klein04adfda2016-10-12 09:52:55 -0400610 return _mm256_blendv_ps(e.fVec, t.fVec, fVec);
611 }
612
613 __m256 fVec;
614 };
615
Mike Klein7c78f3a2016-10-19 09:21:11 -0400616 AI static void SkNx_split(const Sk8f& v, Sk4f* lo, Sk4f* hi) {
Mike Klein04adfda2016-10-12 09:52:55 -0400617 *lo = _mm256_extractf128_ps(v.fVec, 0);
618 *hi = _mm256_extractf128_ps(v.fVec, 1);
619 }
620
Mike Klein7c78f3a2016-10-19 09:21:11 -0400621 AI static Sk8f SkNx_join(const Sk4f& lo, const Sk4f& hi) {
Mike Klein04adfda2016-10-12 09:52:55 -0400622 return _mm256_insertf128_ps(_mm256_castps128_ps256(lo.fVec), hi.fVec, 1);
623 }
624
Mike Klein7c78f3a2016-10-19 09:21:11 -0400625 AI static Sk8f SkNx_fma(const Sk8f& a, const Sk8f& b, const Sk8f& c) {
Mike Klein04adfda2016-10-12 09:52:55 -0400626 return _mm256_fmadd_ps(a.fVec, b.fVec, c.fVec);
627 }
628
Mike Klein06a65e22016-11-17 12:39:09 -0500629 template<> AI /*static*/ Sk8i SkNx_cast<int>(const Sk8b& src) {
630 return _mm256_cvtepu8_epi32(src.fVec);
631 }
632
Mike Klein7c78f3a2016-10-19 09:21:11 -0400633 template<> AI /*static*/ Sk8f SkNx_cast<float>(const Sk8b& src) {
Mike Klein06a65e22016-11-17 12:39:09 -0500634 return _mm256_cvtepi32_ps(SkNx_cast<int>(src).fVec);
Mike Klein04adfda2016-10-12 09:52:55 -0400635 }
636
Mike Klein7c78f3a2016-10-19 09:21:11 -0400637 template<> AI /*static*/ Sk8f SkNx_cast<float>(const Sk8i& src) {
Mike Klein04adfda2016-10-12 09:52:55 -0400638 return _mm256_cvtepi32_ps(src.fVec);
639 }
640
Mike Klein7c78f3a2016-10-19 09:21:11 -0400641 template<> AI /*static*/ Sk8i SkNx_cast<int>(const Sk8f& src) {
Mike Klein04adfda2016-10-12 09:52:55 -0400642 return _mm256_cvttps_epi32(src.fVec);
643 }
644
Mike Klein7c78f3a2016-10-19 09:21:11 -0400645 template<> AI /*static*/ Sk8i SkNx_cast<int>(const Sk8h& src) {
Mike Klein04adfda2016-10-12 09:52:55 -0400646 return _mm256_cvtepu16_epi32(src.fVec);
647 }
Mike Klein7c78f3a2016-10-19 09:21:11 -0400648 template<> AI /*static*/ Sk8h SkNx_cast<uint16_t>(const Sk8i& src) {
Mike Klein04adfda2016-10-12 09:52:55 -0400649 __m128i lo = _mm256_extractf128_si256(src.fVec, 0),
650 hi = _mm256_extractf128_si256(src.fVec, 1);
651 return _mm_packus_epi32(lo, hi);
652 }
653
Mike Klein04adfda2016-10-12 09:52:55 -0400654#endif
655
Mike Klein7c78f3a2016-10-19 09:21:11 -0400656template<> AI /*static*/ Sk4f SkNx_cast<float, int32_t>(const Sk4i& src) {
mtklein0cf795f2016-02-17 07:23:36 -0800657 return _mm_cvtepi32_ps(src.fVec);
658}
Mike Klein7c78f3a2016-10-19 09:21:11 -0400659template<> AI /*static*/ Sk4f SkNx_cast<float, uint32_t>(const Sk4u& src) {
mtkleind05a8752016-07-29 10:10:15 -0700660 return SkNx_cast<float>(Sk4i::Load(&src));
661}
mtklein0cf795f2016-02-17 07:23:36 -0800662
Mike Klein7c78f3a2016-10-19 09:21:11 -0400663template <> AI /*static*/ Sk4i SkNx_cast<int32_t, float>(const Sk4f& src) {
mtklein0cf795f2016-02-17 07:23:36 -0800664 return _mm_cvttps_epi32(src.fVec);
665}
mtklein6c221b42015-11-20 13:53:19 -0800666
Mike Klein7c78f3a2016-10-19 09:21:11 -0400667template<> AI /*static*/ Sk4h SkNx_cast<uint16_t, int32_t>(const Sk4i& src) {
mtklein036e1832016-07-15 07:45:53 -0700668#if 0 && SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE41
669 // TODO: This seems to be causing code generation problems. Investigate?
670 return _mm_packus_epi32(src.fVec);
671#elif SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSSE3
mtklein629f25a2016-02-08 05:54:38 -0800672 // With SSSE3, we can just shuffle the low 2 bytes from each lane right into place.
673 const int _ = ~0;
mtklein036e1832016-07-15 07:45:53 -0700674 return _mm_shuffle_epi8(src.fVec, _mm_setr_epi8(0,1, 4,5, 8,9, 12,13, _,_,_,_,_,_,_,_));
mtklein629f25a2016-02-08 05:54:38 -0800675#else
mtklein036e1832016-07-15 07:45:53 -0700676 // With SSE2, we have to sign extend our input, making _mm_packs_epi32 do the pack we want.
677 __m128i x = _mm_srai_epi32(_mm_slli_epi32(src.fVec, 16), 16);
678 return _mm_packs_epi32(x,x);
mtklein629f25a2016-02-08 05:54:38 -0800679#endif
mtklein2d340f22016-02-06 19:38:39 -0800680}
681
Mike Klein7c78f3a2016-10-19 09:21:11 -0400682template<> AI /*static*/ Sk4h SkNx_cast<uint16_t, float>(const Sk4f& src) {
mtkleind05a8752016-07-29 10:10:15 -0700683 return SkNx_cast<uint16_t>(SkNx_cast<int32_t>(src));
mtklein036e1832016-07-15 07:45:53 -0700684}
685
Mike Klein7c78f3a2016-10-19 09:21:11 -0400686template<> AI /*static*/ Sk4b SkNx_cast<uint8_t, float>(const Sk4f& src) {
mtklein97120a72016-02-12 14:19:06 -0800687 auto _32 = _mm_cvttps_epi32(src.fVec);
688#if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSSE3
689 const int _ = ~0;
690 return _mm_shuffle_epi8(_32, _mm_setr_epi8(0,4,8,12, _,_,_,_, _,_,_,_, _,_,_,_));
691#else
692 auto _16 = _mm_packus_epi16(_32, _32);
693 return _mm_packus_epi16(_16, _16);
694#endif
mtkleinc1eb3112016-02-11 08:10:22 -0800695}
696
Mike Klein06a65e22016-11-17 12:39:09 -0500697template<> AI /*static*/ Sk4i SkNx_cast<int32_t, uint8_t>(const Sk4b& src) {
mtklein97120a72016-02-12 14:19:06 -0800698#if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSSE3
699 const int _ = ~0;
Mike Klein06a65e22016-11-17 12:39:09 -0500700 return _mm_shuffle_epi8(src.fVec, _mm_setr_epi8(0,_,_,_, 1,_,_,_, 2,_,_,_, 3,_,_,_));
mtklein97120a72016-02-12 14:19:06 -0800701#else
Mike Klein06a65e22016-11-17 12:39:09 -0500702 auto _16 = _mm_unpacklo_epi8(src.fVec, _mm_setzero_si128());
703 return _mm_unpacklo_epi16(_16, _mm_setzero_si128());
mtklein97120a72016-02-12 14:19:06 -0800704#endif
Mike Klein06a65e22016-11-17 12:39:09 -0500705}
706
707template<> AI /*static*/ Sk4f SkNx_cast<float, uint8_t>(const Sk4b& src) {
708 return _mm_cvtepi32_ps(SkNx_cast<int32_t>(src).fVec);
mtkleinc1eb3112016-02-11 08:10:22 -0800709}
mtklein97120a72016-02-12 14:19:06 -0800710
Mike Klein7c78f3a2016-10-19 09:21:11 -0400711template<> AI /*static*/ Sk4f SkNx_cast<float, uint16_t>(const Sk4h& src) {
mtklein97120a72016-02-12 14:19:06 -0800712 auto _32 = _mm_unpacklo_epi16(src.fVec, _mm_setzero_si128());
713 return _mm_cvtepi32_ps(_32);
mtklein2d340f22016-02-06 19:38:39 -0800714}
715
Mike Klein7c78f3a2016-10-19 09:21:11 -0400716template<> AI /*static*/ Sk16b SkNx_cast<uint8_t, float>(const Sk16f& src) {
mtkleinf8f90e42016-03-21 10:04:46 -0700717 Sk8f ab, cd;
718 SkNx_split(src, &ab, &cd);
719
720 Sk4f a,b,c,d;
721 SkNx_split(ab, &a, &b);
722 SkNx_split(cd, &c, &d);
723
724 return _mm_packus_epi16(_mm_packus_epi16(_mm_cvttps_epi32(a.fVec),
725 _mm_cvttps_epi32(b.fVec)),
726 _mm_packus_epi16(_mm_cvttps_epi32(c.fVec),
727 _mm_cvttps_epi32(d.fVec)));
mtklein97120a72016-02-12 14:19:06 -0800728}
729
Mike Klein7c78f3a2016-10-19 09:21:11 -0400730template<> AI /*static*/ Sk4h SkNx_cast<uint16_t, uint8_t>(const Sk4b& src) {
mtklein97120a72016-02-12 14:19:06 -0800731 return _mm_unpacklo_epi8(src.fVec, _mm_setzero_si128());
732}
733
Mike Klein7c78f3a2016-10-19 09:21:11 -0400734template<> AI /*static*/ Sk4b SkNx_cast<uint8_t, uint16_t>(const Sk4h& src) {
mtklein97120a72016-02-12 14:19:06 -0800735 return _mm_packus_epi16(src.fVec, src.fVec);
mtklein550e9b02016-01-20 11:55:51 -0800736}
737
Mike Klein7c78f3a2016-10-19 09:21:11 -0400738template<> AI /*static*/ Sk4i SkNx_cast<int32_t, uint16_t>(const Sk4h& src) {
mtklein58e389b2016-07-15 07:00:11 -0700739 return _mm_unpacklo_epi16(src.fVec, _mm_setzero_si128());
740}
741
Mike Klein7c78f3a2016-10-19 09:21:11 -0400742template<> AI /*static*/ Sk4b SkNx_cast<uint8_t, int32_t>(const Sk4i& src) {
msarett7d3ff712016-07-12 14:55:45 -0700743 return _mm_packus_epi16(_mm_packus_epi16(src.fVec, src.fVec), src.fVec);
744}
745
Mike Klein7c78f3a2016-10-19 09:21:11 -0400746template<> AI /*static*/ Sk4i SkNx_cast<int32_t, uint32_t>(const Sk4u& src) {
msarett15ee3de2016-08-02 11:30:30 -0700747 return src.fVec;
748}
749
Mike Klein7c78f3a2016-10-19 09:21:11 -0400750AI static Sk4i Sk4f_round(const Sk4f& x) {
msarett7d3ff712016-07-12 14:55:45 -0700751 return _mm_cvtps_epi32(x.fVec);
752}
753
Mike Klein1e764642016-10-14 17:09:03 -0400754} // namespace
755
mtkleinc9adb052015-03-30 10:50:27 -0700756#endif//SkNx_sse_DEFINED