mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #ifndef SkNx_sse_DEFINED |
| 9 | #define SkNx_sse_DEFINED |
| 10 | |
Mike Klein | 8ea971b | 2018-06-12 12:11:28 -0400 | [diff] [blame] | 11 | #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 |
mtklein | 244a653 | 2016-04-19 14:21:30 -0700 | [diff] [blame] | 20 | |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 21 | // This file may assume <= SSE2, but must check SK_CPU_SSE_LEVEL for anything more recent. |
mtklein | e4c0bee | 2016-02-09 10:35:27 -0800 | [diff] [blame] | 22 | // If you do, make sure this is in a static inline function... anywhere else risks violating ODR. |
mtklein | aa999cb | 2015-05-22 17:18:21 -0700 | [diff] [blame] | 23 | |
Mike Klein | c33d614 | 2018-12-12 08:47:54 -0500 | [diff] [blame] | 24 | namespace { // NOLINT(google-build-namespaces) |
Mike Klein | 1e76464 | 2016-10-14 17:09:03 -0400 | [diff] [blame] | 25 | |
Chris Dalton | 89c5e88 | 2018-06-08 11:46:42 -0600 | [diff] [blame] | 26 | // Emulate _mm_floor_ps() with SSE2: |
| 27 | // - roundtrip through integers via truncation |
| 28 | // - subtract 1 if that's too big (possible for negative values). |
| 29 | // This restricts the domain of our inputs to a maximum somehwere around 2^31. |
| 30 | // Seems plenty big. |
| 31 | AI static __m128 emulate_mm_floor_ps(__m128 v) { |
| 32 | __m128 roundtrip = _mm_cvtepi32_ps(_mm_cvttps_epi32(v)); |
| 33 | __m128 too_big = _mm_cmpgt_ps(roundtrip, v); |
| 34 | return _mm_sub_ps(roundtrip, _mm_and_ps(too_big, _mm_set1_ps(1.0f))); |
| 35 | } |
| 36 | |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 37 | template <> |
mtklein | 6c221b4 | 2015-11-20 13:53:19 -0800 | [diff] [blame] | 38 | class SkNx<2, float> { |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 39 | public: |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 40 | AI SkNx(const __m128& vec) : fVec(vec) {} |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 41 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 42 | AI SkNx() {} |
| 43 | AI SkNx(float val) : fVec(_mm_set1_ps(val)) {} |
| 44 | AI static SkNx Load(const void* ptr) { |
mtklein | 507ef6d | 2016-01-31 08:02:47 -0800 | [diff] [blame] | 45 | return _mm_castsi128_ps(_mm_loadl_epi64((const __m128i*)ptr)); |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 46 | } |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 47 | AI SkNx(float a, float b) : fVec(_mm_setr_ps(a,b,0,0)) {} |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 48 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 49 | AI void store(void* ptr) const { _mm_storel_pi((__m64*)ptr, fVec); } |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 50 | |
Chris Dalton | 21f6437 | 2018-04-11 14:01:04 -0600 | [diff] [blame] | 51 | AI static void Load2(const void* ptr, SkNx* x, SkNx* y) { |
| 52 | const float* m = (const float*)ptr; |
| 53 | *x = SkNx{m[0], m[2]}; |
| 54 | *y = SkNx{m[1], m[3]}; |
| 55 | } |
| 56 | |
Chris Dalton | 42f02aa | 2018-04-08 23:58:43 -0600 | [diff] [blame] | 57 | AI static void Store2(void* dst, const SkNx& a, const SkNx& b) { |
| 58 | auto vals = _mm_unpacklo_ps(a.fVec, b.fVec); |
| 59 | _mm_storeu_ps((float*)dst, vals); |
| 60 | } |
| 61 | |
Chris Dalton | 0cb7587 | 2017-12-01 13:23:05 -0700 | [diff] [blame] | 62 | AI static void Store3(void* dst, const SkNx& a, const SkNx& b, const SkNx& c) { |
| 63 | auto lo = _mm_setr_ps(a[0], b[0], c[0], a[1]), |
| 64 | hi = _mm_setr_ps(b[1], c[1], 0, 0); |
| 65 | _mm_storeu_ps((float*)dst, lo); |
| 66 | _mm_storel_pi(((__m64*)dst) + 2, hi); |
| 67 | } |
| 68 | |
Chris Dalton | 6f8fa4e | 2018-02-06 17:55:30 -0700 | [diff] [blame] | 69 | AI static void Store4(void* dst, const SkNx& a, const SkNx& b, const SkNx& c, const SkNx& d) { |
| 70 | auto lo = _mm_setr_ps(a[0], b[0], c[0], d[0]), |
| 71 | hi = _mm_setr_ps(a[1], b[1], c[1], d[1]); |
| 72 | _mm_storeu_ps((float*)dst, lo); |
| 73 | _mm_storeu_ps(((float*)dst) + 4, hi); |
| 74 | } |
| 75 | |
Chris Dalton | 7732f4f | 2017-08-28 14:45:40 -0600 | [diff] [blame] | 76 | AI SkNx operator - () const { return _mm_xor_ps(_mm_set1_ps(-0.0f), fVec); } |
| 77 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 78 | AI SkNx operator + (const SkNx& o) const { return _mm_add_ps(fVec, o.fVec); } |
| 79 | AI SkNx operator - (const SkNx& o) const { return _mm_sub_ps(fVec, o.fVec); } |
| 80 | AI SkNx operator * (const SkNx& o) const { return _mm_mul_ps(fVec, o.fVec); } |
| 81 | AI SkNx operator / (const SkNx& o) const { return _mm_div_ps(fVec, o.fVec); } |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 82 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 83 | AI SkNx operator == (const SkNx& o) const { return _mm_cmpeq_ps (fVec, o.fVec); } |
| 84 | AI SkNx operator != (const SkNx& o) const { return _mm_cmpneq_ps(fVec, o.fVec); } |
| 85 | AI SkNx operator < (const SkNx& o) const { return _mm_cmplt_ps (fVec, o.fVec); } |
| 86 | AI SkNx operator > (const SkNx& o) const { return _mm_cmpgt_ps (fVec, o.fVec); } |
| 87 | AI SkNx operator <= (const SkNx& o) const { return _mm_cmple_ps (fVec, o.fVec); } |
| 88 | AI SkNx operator >= (const SkNx& o) const { return _mm_cmpge_ps (fVec, o.fVec); } |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 89 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 90 | 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); } |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 92 | |
Chris Dalton | 89c5e88 | 2018-06-08 11:46:42 -0600 | [diff] [blame] | 93 | 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 Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 102 | 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); } |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 105 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 106 | AI float operator[](int k) const { |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 107 | SkASSERT(0 <= k && k < 2); |
| 108 | union { __m128 v; float fs[4]; } pun = {fVec}; |
mtklein | a156a8f | 2015-04-03 06:16:13 -0700 | [diff] [blame] | 109 | return pun.fs[k&1]; |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 112 | 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); } |
mtklein | b5e8611 | 2015-06-24 15:18:39 -0700 | [diff] [blame] | 114 | |
Chris Dalton | 7732f4f | 2017-08-28 14:45:40 -0600 | [diff] [blame] | 115 | 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 | |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 124 | __m128 fVec; |
| 125 | }; |
| 126 | |
| 127 | template <> |
mtklein | 6c221b4 | 2015-11-20 13:53:19 -0800 | [diff] [blame] | 128 | class SkNx<4, float> { |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 129 | public: |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 130 | AI SkNx(const __m128& vec) : fVec(vec) {} |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 131 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 132 | 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)) {} |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 135 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 136 | 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); } |
mtklein | 9db43ac | 2015-12-01 07:10:21 -0800 | [diff] [blame] | 138 | |
Mike Klein | 213d821 | 2017-11-30 12:07:20 -0500 | [diff] [blame] | 139 | 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 Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 146 | AI static void Load4(const void* ptr, SkNx* r, SkNx* g, SkNx* b, SkNx* a) { |
Mike Klein | 33cbfd7 | 2016-10-06 11:09:27 -0400 | [diff] [blame] | 147 | __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 Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 157 | AI static void Store4(void* dst, const SkNx& r, const SkNx& g, const SkNx& b, const SkNx& a) { |
Mike Klein | 33cbfd7 | 2016-10-06 11:09:27 -0400 | [diff] [blame] | 158 | __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 Dalton | 7732f4f | 2017-08-28 14:45:40 -0600 | [diff] [blame] | 169 | AI SkNx operator - () const { return _mm_xor_ps(_mm_set1_ps(-0.0f), fVec); } |
| 170 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 171 | 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); } |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 175 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 176 | 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); } |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 182 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 183 | 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); } |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 185 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 186 | AI SkNx abs() const { return _mm_andnot_ps(_mm_set1_ps(-0.0f), fVec); } |
| 187 | AI SkNx floor() const { |
mtklein | 5608e2e | 2016-07-11 09:59:21 -0700 | [diff] [blame] | 188 | #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE41 |
| 189 | return _mm_floor_ps(fVec); |
| 190 | #else |
Chris Dalton | 89c5e88 | 2018-06-08 11:46:42 -0600 | [diff] [blame] | 191 | return emulate_mm_floor_ps(fVec); |
mtklein | 5608e2e | 2016-07-11 09:59:21 -0700 | [diff] [blame] | 192 | #endif |
mtklein | 244a653 | 2016-04-19 14:21:30 -0700 | [diff] [blame] | 193 | } |
mtklein | c33065a | 2016-01-15 12:16:40 -0800 | [diff] [blame] | 194 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 195 | 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); } |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 198 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 199 | AI float operator[](int k) const { |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 200 | SkASSERT(0 <= k && k < 4); |
| 201 | union { __m128 v; float fs[4]; } pun = {fVec}; |
mtklein | a156a8f | 2015-04-03 06:16:13 -0700 | [diff] [blame] | 202 | return pun.fs[k&3]; |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Chris Dalton | e3fda93 | 2018-04-11 13:18:09 -0600 | [diff] [blame] | 205 | 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 Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 217 | 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)); } |
mtklein | b5e8611 | 2015-06-24 15:18:39 -0700 | [diff] [blame] | 219 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 220 | AI SkNx thenElse(const SkNx& t, const SkNx& e) const { |
mtklein | 5608e2e | 2016-07-11 09:59:21 -0700 | [diff] [blame] | 221 | #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE41 |
msarett | d280957 | 2016-06-20 06:07:45 -0700 | [diff] [blame] | 222 | return _mm_blendv_ps(e.fVec, t.fVec, fVec); |
mtklein | 5608e2e | 2016-07-11 09:59:21 -0700 | [diff] [blame] | 223 | #else |
mtklein | 2aab22a | 2015-06-26 10:46:31 -0700 | [diff] [blame] | 224 | return _mm_or_ps(_mm_and_ps (fVec, t.fVec), |
| 225 | _mm_andnot_ps(fVec, e.fVec)); |
mtklein | 5608e2e | 2016-07-11 09:59:21 -0700 | [diff] [blame] | 226 | #endif |
mtklein | 2aab22a | 2015-06-26 10:46:31 -0700 | [diff] [blame] | 227 | } |
| 228 | |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 229 | __m128 fVec; |
| 230 | }; |
| 231 | |
Herb Derby | 0f96bb3 | 2017-09-13 16:46:05 -0400 | [diff] [blame] | 232 | AI 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 | |
mtklein | 115acee | 2015-04-14 14:02:52 -0700 | [diff] [blame] | 243 | template <> |
mtklein | d05a875 | 2016-07-29 10:10:15 -0700 | [diff] [blame] | 244 | class SkNx<4, int32_t> { |
mtklein | 8273ca4 | 2016-02-09 11:32:51 -0800 | [diff] [blame] | 245 | public: |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 246 | AI SkNx(const __m128i& vec) : fVec(vec) {} |
mtklein | 8273ca4 | 2016-02-09 11:32:51 -0800 | [diff] [blame] | 247 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 248 | 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)) {} |
mtklein | 8273ca4 | 2016-02-09 11:32:51 -0800 | [diff] [blame] | 252 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 253 | AI void store(void* ptr) const { _mm_storeu_si128((__m128i*)ptr, fVec); } |
mtklein | 8273ca4 | 2016-02-09 11:32:51 -0800 | [diff] [blame] | 254 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 255 | 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 Derby | 0f96bb3 | 2017-09-13 16:46:05 -0400 | [diff] [blame] | 257 | AI SkNx operator * (const SkNx& o) const { return mullo32(fVec, o.fVec); } |
mtklein | 8273ca4 | 2016-02-09 11:32:51 -0800 | [diff] [blame] | 258 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 259 | AI SkNx operator & (const SkNx& o) const { return _mm_and_si128(fVec, o.fVec); } |
Herb Derby | 0f96bb3 | 2017-09-13 16:46:05 -0400 | [diff] [blame] | 260 | AI SkNx operator | (const SkNx& o) const { return _mm_or_si128(fVec, o.fVec); } |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 261 | AI SkNx operator ^ (const SkNx& o) const { return _mm_xor_si128(fVec, o.fVec); } |
mtklein | 64f061a | 2016-06-17 12:09:16 -0700 | [diff] [blame] | 262 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 263 | 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); } |
mtklein | 8273ca4 | 2016-02-09 11:32:51 -0800 | [diff] [blame] | 265 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 266 | 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); } |
mtklein | 58e389b | 2016-07-15 07:00:11 -0700 | [diff] [blame] | 269 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 270 | AI int32_t operator[](int k) const { |
mtklein | 8273ca4 | 2016-02-09 11:32:51 -0800 | [diff] [blame] | 271 | SkASSERT(0 <= k && k < 4); |
mtklein | d05a875 | 2016-07-29 10:10:15 -0700 | [diff] [blame] | 272 | union { __m128i v; int32_t is[4]; } pun = {fVec}; |
mtklein | 8273ca4 | 2016-02-09 11:32:51 -0800 | [diff] [blame] | 273 | return pun.is[k&3]; |
| 274 | } |
mtklein | 8273ca4 | 2016-02-09 11:32:51 -0800 | [diff] [blame] | 275 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 276 | AI SkNx thenElse(const SkNx& t, const SkNx& e) const { |
mtklein | 58e389b | 2016-07-15 07:00:11 -0700 | [diff] [blame] | 277 | #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 Li | 7da6ba2 | 2017-07-12 13:36:05 -0400 | [diff] [blame] | 285 | 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 Li | e94865e | 2017-07-14 10:17:04 -0400 | [diff] [blame] | 298 | return (x < y).thenElse(x, y); |
Yuqian Li | 7da6ba2 | 2017-07-12 13:36:05 -0400 | [diff] [blame] | 299 | #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 Li | e94865e | 2017-07-14 10:17:04 -0400 | [diff] [blame] | 306 | return (x > y).thenElse(x, y); |
Yuqian Li | 7da6ba2 | 2017-07-12 13:36:05 -0400 | [diff] [blame] | 307 | #endif |
| 308 | } |
| 309 | |
mtklein | 8273ca4 | 2016-02-09 11:32:51 -0800 | [diff] [blame] | 310 | __m128i fVec; |
| 311 | }; |
| 312 | |
| 313 | template <> |
Mike Reed | d92a739 | 2018-03-20 12:42:38 -0400 | [diff] [blame] | 314 | class SkNx<2, uint32_t> { |
| 315 | public: |
| 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 | |
| 360 | template <> |
mtklein | d05a875 | 2016-07-29 10:10:15 -0700 | [diff] [blame] | 361 | class SkNx<4, uint32_t> { |
| 362 | public: |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 363 | AI SkNx(const __m128i& vec) : fVec(vec) {} |
mtklein | d05a875 | 2016-07-29 10:10:15 -0700 | [diff] [blame] | 364 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 365 | 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)) {} |
mtklein | d05a875 | 2016-07-29 10:10:15 -0700 | [diff] [blame] | 369 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 370 | AI void store(void* ptr) const { _mm_storeu_si128((__m128i*)ptr, fVec); } |
mtklein | d05a875 | 2016-07-29 10:10:15 -0700 | [diff] [blame] | 371 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 372 | 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 Derby | 0f96bb3 | 2017-09-13 16:46:05 -0400 | [diff] [blame] | 374 | AI SkNx operator * (const SkNx& o) const { return mullo32(fVec, o.fVec); } |
mtklein | d05a875 | 2016-07-29 10:10:15 -0700 | [diff] [blame] | 375 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 376 | AI SkNx operator & (const SkNx& o) const { return _mm_and_si128(fVec, o.fVec); } |
Herb Derby | 0f96bb3 | 2017-09-13 16:46:05 -0400 | [diff] [blame] | 377 | AI SkNx operator | (const SkNx& o) const { return _mm_or_si128(fVec, o.fVec); } |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 378 | AI SkNx operator ^ (const SkNx& o) const { return _mm_xor_si128(fVec, o.fVec); } |
mtklein | d05a875 | 2016-07-29 10:10:15 -0700 | [diff] [blame] | 379 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 380 | 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); } |
mtklein | d05a875 | 2016-07-29 10:10:15 -0700 | [diff] [blame] | 382 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 383 | AI SkNx operator == (const SkNx& o) const { return _mm_cmpeq_epi32 (fVec, o.fVec); } |
Mike Reed | d92a739 | 2018-03-20 12:42:38 -0400 | [diff] [blame] | 384 | AI SkNx operator != (const SkNx& o) const { return (*this == o) ^ 0xffffffff; } |
| 385 | |
mtklein | d05a875 | 2016-07-29 10:10:15 -0700 | [diff] [blame] | 386 | // operator < and > take a little extra fiddling to make work for unsigned ints. |
| 387 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 388 | AI uint32_t operator[](int k) const { |
mtklein | d05a875 | 2016-07-29 10:10:15 -0700 | [diff] [blame] | 389 | SkASSERT(0 <= k && k < 4); |
| 390 | union { __m128i v; uint32_t us[4]; } pun = {fVec}; |
| 391 | return pun.us[k&3]; |
| 392 | } |
| 393 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 394 | AI SkNx thenElse(const SkNx& t, const SkNx& e) const { |
mtklein | d05a875 | 2016-07-29 10:10:15 -0700 | [diff] [blame] | 395 | #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 Derby | 5eb1528 | 2017-10-10 17:14:18 -0400 | [diff] [blame] | 403 | 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 | |
mtklein | d05a875 | 2016-07-29 10:10:15 -0700 | [diff] [blame] | 410 | __m128i fVec; |
| 411 | }; |
| 412 | |
mtklein | d05a875 | 2016-07-29 10:10:15 -0700 | [diff] [blame] | 413 | template <> |
mtklein | 6c221b4 | 2015-11-20 13:53:19 -0800 | [diff] [blame] | 414 | class SkNx<4, uint16_t> { |
mtklein | 115acee | 2015-04-14 14:02:52 -0700 | [diff] [blame] | 415 | public: |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 416 | AI SkNx(const __m128i& vec) : fVec(vec) {} |
mtklein | 115acee | 2015-04-14 14:02:52 -0700 | [diff] [blame] | 417 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 418 | 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)) {} |
mtklein | 115acee | 2015-04-14 14:02:52 -0700 | [diff] [blame] | 422 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 423 | 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); } |
mtklein | 115acee | 2015-04-14 14:02:52 -0700 | [diff] [blame] | 425 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 426 | AI static void Load4(const void* ptr, SkNx* r, SkNx* g, SkNx* b, SkNx* a) { |
Mike Klein | 33cbfd7 | 2016-10-06 11:09:27 -0400 | [diff] [blame] | 427 | __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 Sarett | 5bee0b6 | 2017-01-19 12:04:32 -0500 | [diff] [blame] | 438 | 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 Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 454 | AI static void Store4(void* dst, const SkNx& r, const SkNx& g, const SkNx& b, const SkNx& a) { |
Mike Klein | 33cbfd7 | 2016-10-06 11:09:27 -0400 | [diff] [blame] | 455 | __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 Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 463 | 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 Sarett | 379938e | 2017-01-12 18:34:29 -0500 | [diff] [blame] | 466 | 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); } |
mtklein | 115acee | 2015-04-14 14:02:52 -0700 | [diff] [blame] | 468 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 469 | 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); } |
mtklein | 115acee | 2015-04-14 14:02:52 -0700 | [diff] [blame] | 471 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 472 | AI uint16_t operator[](int k) const { |
mtklein | 115acee | 2015-04-14 14:02:52 -0700 | [diff] [blame] | 473 | SkASSERT(0 <= k && k < 4); |
mtklein | e4c0bee | 2016-02-09 10:35:27 -0800 | [diff] [blame] | 474 | union { __m128i v; uint16_t us[8]; } pun = {fVec}; |
| 475 | return pun.us[k&3]; |
mtklein | 115acee | 2015-04-14 14:02:52 -0700 | [diff] [blame] | 476 | } |
mtklein | d2ffd36 | 2015-05-12 06:11:21 -0700 | [diff] [blame] | 477 | |
mtklein | 115acee | 2015-04-14 14:02:52 -0700 | [diff] [blame] | 478 | __m128i fVec; |
| 479 | }; |
| 480 | |
| 481 | template <> |
mtklein | 6c221b4 | 2015-11-20 13:53:19 -0800 | [diff] [blame] | 482 | class SkNx<8, uint16_t> { |
mtklein | 115acee | 2015-04-14 14:02:52 -0700 | [diff] [blame] | 483 | public: |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 484 | AI SkNx(const __m128i& vec) : fVec(vec) {} |
mtklein | 115acee | 2015-04-14 14:02:52 -0700 | [diff] [blame] | 485 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 486 | 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)) {} |
mtklein | 115acee | 2015-04-14 14:02:52 -0700 | [diff] [blame] | 491 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 492 | 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); } |
mtklein | 115acee | 2015-04-14 14:02:52 -0700 | [diff] [blame] | 494 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 495 | AI static void Load4(const void* ptr, SkNx* r, SkNx* g, SkNx* b, SkNx* a) { |
Mike Klein | 04adfda | 2016-10-12 09:52:55 -0400 | [diff] [blame] | 496 | __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 Sarett | 5bee0b6 | 2017-01-19 12:04:32 -0500 | [diff] [blame] | 516 | AI static void Load3(const void* ptr, SkNx* r, SkNx* g, SkNx* b) { |
Matt Sarett | 5bee0b6 | 2017-01-19 12:04:32 -0500 | [diff] [blame] | 517 | 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 Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 541 | AI static void Store4(void* ptr, const SkNx& r, const SkNx& g, const SkNx& b, const SkNx& a) { |
Mike Klein | 04adfda | 2016-10-12 09:52:55 -0400 | [diff] [blame] | 542 | __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 Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 553 | 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 Sarett | 379938e | 2017-01-12 18:34:29 -0500 | [diff] [blame] | 556 | 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); } |
mtklein | 115acee | 2015-04-14 14:02:52 -0700 | [diff] [blame] | 558 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 559 | 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); } |
mtklein | 115acee | 2015-04-14 14:02:52 -0700 | [diff] [blame] | 561 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 562 | AI static SkNx Min(const SkNx& a, const SkNx& b) { |
mtklein | 27e517a | 2015-05-14 17:53:04 -0700 | [diff] [blame] | 563 | // 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 Derby | d1b3c78 | 2017-11-02 13:18:38 -0400 | [diff] [blame] | 571 | AI SkNx mulHi(const SkNx& m) const { |
| 572 | return _mm_mulhi_epu16(fVec, m.fVec); |
| 573 | } |
| 574 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 575 | AI SkNx thenElse(const SkNx& t, const SkNx& e) const { |
mtklein | 4be181e | 2015-07-14 10:54:19 -0700 | [diff] [blame] | 576 | return _mm_or_si128(_mm_and_si128 (fVec, t.fVec), |
| 577 | _mm_andnot_si128(fVec, e.fVec)); |
| 578 | } |
| 579 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 580 | AI uint16_t operator[](int k) const { |
mtklein | 115acee | 2015-04-14 14:02:52 -0700 | [diff] [blame] | 581 | SkASSERT(0 <= k && k < 8); |
mtklein | e4c0bee | 2016-02-09 10:35:27 -0800 | [diff] [blame] | 582 | union { __m128i v; uint16_t us[8]; } pun = {fVec}; |
| 583 | return pun.us[k&7]; |
mtklein | 115acee | 2015-04-14 14:02:52 -0700 | [diff] [blame] | 584 | } |
mtklein | d2ffd36 | 2015-05-12 06:11:21 -0700 | [diff] [blame] | 585 | |
| 586 | __m128i fVec; |
| 587 | }; |
| 588 | |
| 589 | template <> |
mtklein | 6f37b4a | 2015-12-14 11:25:18 -0800 | [diff] [blame] | 590 | class SkNx<4, uint8_t> { |
| 591 | public: |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 592 | 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) |
herb | fd5a260 | 2016-03-01 07:01:23 -0800 | [diff] [blame] | 595 | : fVec(_mm_setr_epi8(a,b,c,d, 0,0,0,0, 0,0,0,0, 0,0,0,0)) {} |
| 596 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 597 | 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); } |
mtklein | 6f37b4a | 2015-12-14 11:25:18 -0800 | [diff] [blame] | 599 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 600 | AI uint8_t operator[](int k) const { |
herb | fd5a260 | 2016-03-01 07:01:23 -0800 | [diff] [blame] | 601 | SkASSERT(0 <= k && k < 4); |
| 602 | union { __m128i v; uint8_t us[16]; } pun = {fVec}; |
| 603 | return pun.us[k&3]; |
| 604 | } |
| 605 | |
mtklein | 6f37b4a | 2015-12-14 11:25:18 -0800 | [diff] [blame] | 606 | // TODO as needed |
| 607 | |
| 608 | __m128i fVec; |
| 609 | }; |
| 610 | |
| 611 | template <> |
Herb Derby | d1b3c78 | 2017-11-02 13:18:38 -0400 | [diff] [blame] | 612 | class SkNx<8, uint8_t> { |
| 613 | public: |
| 614 | AI SkNx(const __m128i& vec) : fVec(vec) {} |
| 615 | |
| 616 | AI SkNx() {} |
| 617 | AI SkNx(uint8_t val) : fVec(_mm_set1_epi8(val)) {} |
Herb Derby | b8b3086 | 2017-11-16 16:04:42 -0500 | [diff] [blame] | 618 | AI static SkNx Load(const void* ptr) { return _mm_loadl_epi64((const __m128i*)ptr); } |
Herb Derby | d1b3c78 | 2017-11-02 13:18:38 -0400 | [diff] [blame] | 619 | 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 | |
| 651 | template <> |
mtklein | 6c221b4 | 2015-11-20 13:53:19 -0800 | [diff] [blame] | 652 | class SkNx<16, uint8_t> { |
mtklein | d2ffd36 | 2015-05-12 06:11:21 -0700 | [diff] [blame] | 653 | public: |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 654 | AI SkNx(const __m128i& vec) : fVec(vec) {} |
mtklein | d2ffd36 | 2015-05-12 06:11:21 -0700 | [diff] [blame] | 655 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 656 | 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) |
mtklein | d2ffd36 | 2015-05-12 06:11:21 -0700 | [diff] [blame] | 663 | : fVec(_mm_setr_epi8(a,b,c,d, e,f,g,h, i,j,k,l, m,n,o,p)) {} |
| 664 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 665 | AI void store(void* ptr) const { _mm_storeu_si128((__m128i*)ptr, fVec); } |
mtklein | d2ffd36 | 2015-05-12 06:11:21 -0700 | [diff] [blame] | 666 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 667 | AI SkNx saturatedAdd(const SkNx& o) const { return _mm_adds_epu8(fVec, o.fVec); } |
mtklein | 6cbf18c | 2015-05-12 15:48:09 -0700 | [diff] [blame] | 668 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 669 | 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 Klein | 7dfe6d9 | 2018-12-18 14:53:37 -0500 | [diff] [blame] | 671 | AI SkNx operator & (const SkNx& o) const { return _mm_and_si128(fVec, o.fVec); } |
mtklein | d2ffd36 | 2015-05-12 06:11:21 -0700 | [diff] [blame] | 672 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 673 | 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 { |
mtklein | b5e8611 | 2015-06-24 15:18:39 -0700 | [diff] [blame] | 675 | // 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 | } |
mtklein | 27e517a | 2015-05-14 17:53:04 -0700 | [diff] [blame] | 679 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 680 | AI uint8_t operator[](int k) const { |
mtklein | d2ffd36 | 2015-05-12 06:11:21 -0700 | [diff] [blame] | 681 | SkASSERT(0 <= k && k < 16); |
mtklein | e4c0bee | 2016-02-09 10:35:27 -0800 | [diff] [blame] | 682 | union { __m128i v; uint8_t us[16]; } pun = {fVec}; |
| 683 | return pun.us[k&15]; |
mtklein | d2ffd36 | 2015-05-12 06:11:21 -0700 | [diff] [blame] | 684 | } |
| 685 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 686 | AI SkNx thenElse(const SkNx& t, const SkNx& e) const { |
mtklein | b5e8611 | 2015-06-24 15:18:39 -0700 | [diff] [blame] | 687 | return _mm_or_si128(_mm_and_si128 (fVec, t.fVec), |
| 688 | _mm_andnot_si128(fVec, e.fVec)); |
| 689 | } |
| 690 | |
mtklein | 115acee | 2015-04-14 14:02:52 -0700 | [diff] [blame] | 691 | __m128i fVec; |
| 692 | }; |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 693 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 694 | template<> AI /*static*/ Sk4f SkNx_cast<float, int32_t>(const Sk4i& src) { |
mtklein | 0cf795f | 2016-02-17 07:23:36 -0800 | [diff] [blame] | 695 | return _mm_cvtepi32_ps(src.fVec); |
| 696 | } |
Herb Derby | d1b3c78 | 2017-11-02 13:18:38 -0400 | [diff] [blame] | 697 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 698 | template<> AI /*static*/ Sk4f SkNx_cast<float, uint32_t>(const Sk4u& src) { |
mtklein | d05a875 | 2016-07-29 10:10:15 -0700 | [diff] [blame] | 699 | return SkNx_cast<float>(Sk4i::Load(&src)); |
| 700 | } |
mtklein | 0cf795f | 2016-02-17 07:23:36 -0800 | [diff] [blame] | 701 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 702 | template <> AI /*static*/ Sk4i SkNx_cast<int32_t, float>(const Sk4f& src) { |
mtklein | 0cf795f | 2016-02-17 07:23:36 -0800 | [diff] [blame] | 703 | return _mm_cvttps_epi32(src.fVec); |
| 704 | } |
mtklein | 6c221b4 | 2015-11-20 13:53:19 -0800 | [diff] [blame] | 705 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 706 | template<> AI /*static*/ Sk4h SkNx_cast<uint16_t, int32_t>(const Sk4i& src) { |
mtklein | 036e183 | 2016-07-15 07:45:53 -0700 | [diff] [blame] | 707 | #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 |
mtklein | 629f25a | 2016-02-08 05:54:38 -0800 | [diff] [blame] | 711 | // With SSSE3, we can just shuffle the low 2 bytes from each lane right into place. |
| 712 | const int _ = ~0; |
mtklein | 036e183 | 2016-07-15 07:45:53 -0700 | [diff] [blame] | 713 | return _mm_shuffle_epi8(src.fVec, _mm_setr_epi8(0,1, 4,5, 8,9, 12,13, _,_,_,_,_,_,_,_)); |
mtklein | 629f25a | 2016-02-08 05:54:38 -0800 | [diff] [blame] | 714 | #else |
mtklein | 036e183 | 2016-07-15 07:45:53 -0700 | [diff] [blame] | 715 | // 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); |
mtklein | 629f25a | 2016-02-08 05:54:38 -0800 | [diff] [blame] | 718 | #endif |
mtklein | 2d340f2 | 2016-02-06 19:38:39 -0800 | [diff] [blame] | 719 | } |
| 720 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 721 | template<> AI /*static*/ Sk4h SkNx_cast<uint16_t, float>(const Sk4f& src) { |
mtklein | d05a875 | 2016-07-29 10:10:15 -0700 | [diff] [blame] | 722 | return SkNx_cast<uint16_t>(SkNx_cast<int32_t>(src)); |
mtklein | 036e183 | 2016-07-15 07:45:53 -0700 | [diff] [blame] | 723 | } |
| 724 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 725 | template<> AI /*static*/ Sk4b SkNx_cast<uint8_t, float>(const Sk4f& src) { |
mtklein | 97120a7 | 2016-02-12 14:19:06 -0800 | [diff] [blame] | 726 | 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 |
mtklein | c1eb311 | 2016-02-11 08:10:22 -0800 | [diff] [blame] | 734 | } |
| 735 | |
Herb Derby | 5eb1528 | 2017-10-10 17:14:18 -0400 | [diff] [blame] | 736 | template<> AI /*static*/ Sk4u SkNx_cast<uint32_t, uint8_t>(const Sk4b& src) { |
mtklein | 97120a7 | 2016-02-12 14:19:06 -0800 | [diff] [blame] | 737 | #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSSE3 |
| 738 | const int _ = ~0; |
Mike Klein | 06a65e2 | 2016-11-17 12:39:09 -0500 | [diff] [blame] | 739 | return _mm_shuffle_epi8(src.fVec, _mm_setr_epi8(0,_,_,_, 1,_,_,_, 2,_,_,_, 3,_,_,_)); |
mtklein | 97120a7 | 2016-02-12 14:19:06 -0800 | [diff] [blame] | 740 | #else |
Mike Klein | 06a65e2 | 2016-11-17 12:39:09 -0500 | [diff] [blame] | 741 | auto _16 = _mm_unpacklo_epi8(src.fVec, _mm_setzero_si128()); |
| 742 | return _mm_unpacklo_epi16(_16, _mm_setzero_si128()); |
mtklein | 97120a7 | 2016-02-12 14:19:06 -0800 | [diff] [blame] | 743 | #endif |
Mike Klein | 06a65e2 | 2016-11-17 12:39:09 -0500 | [diff] [blame] | 744 | } |
| 745 | |
Herb Derby | 5eb1528 | 2017-10-10 17:14:18 -0400 | [diff] [blame] | 746 | template<> AI /*static*/ Sk4i SkNx_cast<int32_t, uint8_t>(const Sk4b& src) { |
| 747 | return SkNx_cast<uint32_t>(src).fVec; |
| 748 | } |
| 749 | |
Mike Klein | 06a65e2 | 2016-11-17 12:39:09 -0500 | [diff] [blame] | 750 | template<> AI /*static*/ Sk4f SkNx_cast<float, uint8_t>(const Sk4b& src) { |
| 751 | return _mm_cvtepi32_ps(SkNx_cast<int32_t>(src).fVec); |
mtklein | c1eb311 | 2016-02-11 08:10:22 -0800 | [diff] [blame] | 752 | } |
mtklein | 97120a7 | 2016-02-12 14:19:06 -0800 | [diff] [blame] | 753 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 754 | template<> AI /*static*/ Sk4f SkNx_cast<float, uint16_t>(const Sk4h& src) { |
mtklein | 97120a7 | 2016-02-12 14:19:06 -0800 | [diff] [blame] | 755 | auto _32 = _mm_unpacklo_epi16(src.fVec, _mm_setzero_si128()); |
| 756 | return _mm_cvtepi32_ps(_32); |
mtklein | 2d340f2 | 2016-02-06 19:38:39 -0800 | [diff] [blame] | 757 | } |
| 758 | |
Herb Derby | d1b3c78 | 2017-11-02 13:18:38 -0400 | [diff] [blame] | 759 | template<> 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 Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 767 | template<> AI /*static*/ Sk16b SkNx_cast<uint8_t, float>(const Sk16f& src) { |
mtklein | f8f90e4 | 2016-03-21 10:04:46 -0700 | [diff] [blame] | 768 | 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))); |
mtklein | 97120a7 | 2016-02-12 14:19:06 -0800 | [diff] [blame] | 779 | } |
| 780 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 781 | template<> AI /*static*/ Sk4h SkNx_cast<uint16_t, uint8_t>(const Sk4b& src) { |
mtklein | 97120a7 | 2016-02-12 14:19:06 -0800 | [diff] [blame] | 782 | return _mm_unpacklo_epi8(src.fVec, _mm_setzero_si128()); |
| 783 | } |
| 784 | |
Herb Derby | d1b3c78 | 2017-11-02 13:18:38 -0400 | [diff] [blame] | 785 | template<> 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 Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 789 | template<> AI /*static*/ Sk4b SkNx_cast<uint8_t, uint16_t>(const Sk4h& src) { |
mtklein | 97120a7 | 2016-02-12 14:19:06 -0800 | [diff] [blame] | 790 | return _mm_packus_epi16(src.fVec, src.fVec); |
mtklein | 550e9b0 | 2016-01-20 11:55:51 -0800 | [diff] [blame] | 791 | } |
| 792 | |
Herb Derby | d1b3c78 | 2017-11-02 13:18:38 -0400 | [diff] [blame] | 793 | template<> AI /*static*/ Sk8b SkNx_cast<uint8_t, uint16_t>(const Sk8h& src) { |
| 794 | return _mm_packus_epi16(src.fVec, src.fVec); |
| 795 | } |
| 796 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 797 | template<> AI /*static*/ Sk4i SkNx_cast<int32_t, uint16_t>(const Sk4h& src) { |
mtklein | 58e389b | 2016-07-15 07:00:11 -0700 | [diff] [blame] | 798 | return _mm_unpacklo_epi16(src.fVec, _mm_setzero_si128()); |
| 799 | } |
| 800 | |
Herb Derby | d1b3c78 | 2017-11-02 13:18:38 -0400 | [diff] [blame] | 801 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 802 | template<> AI /*static*/ Sk4b SkNx_cast<uint8_t, int32_t>(const Sk4i& src) { |
msarett | 7d3ff71 | 2016-07-12 14:55:45 -0700 | [diff] [blame] | 803 | return _mm_packus_epi16(_mm_packus_epi16(src.fVec, src.fVec), src.fVec); |
| 804 | } |
| 805 | |
Herb Derby | 0f96bb3 | 2017-09-13 16:46:05 -0400 | [diff] [blame] | 806 | template<> 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 Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 810 | template<> AI /*static*/ Sk4i SkNx_cast<int32_t, uint32_t>(const Sk4u& src) { |
msarett | 15ee3de | 2016-08-02 11:30:30 -0700 | [diff] [blame] | 811 | return src.fVec; |
| 812 | } |
| 813 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 814 | AI static Sk4i Sk4f_round(const Sk4f& x) { |
msarett | 7d3ff71 | 2016-07-12 14:55:45 -0700 | [diff] [blame] | 815 | return _mm_cvtps_epi32(x.fVec); |
| 816 | } |
| 817 | |
Mike Klein | 1e76464 | 2016-10-14 17:09:03 -0400 | [diff] [blame] | 818 | } // namespace |
| 819 | |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 820 | #endif//SkNx_sse_DEFINED |