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 | |
mtklein | e18fa44 | 2016-06-09 13:40:56 -0700 | [diff] [blame] | 11 | #include <immintrin.h> |
mtklein | 244a653 | 2016-04-19 14:21:30 -0700 | [diff] [blame] | 12 | |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 13 | // 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] | 14 | // 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] | 15 | |
fmalita | c2e0ac4 | 2015-12-03 09:15:25 -0800 | [diff] [blame] | 16 | #define SKNX_IS_FAST |
| 17 | |
Mike Klein | 511ea17 | 2016-10-15 00:13:29 -0400 | [diff] [blame] | 18 | template <> struct SkNx_abi<4,float> { __m128 vec; }; |
Mike Klein | 5ec5590 | 2016-10-15 01:18:45 -0400 | [diff] [blame] | 19 | #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_AVX2 |
| 20 | template <> struct SkNx_abi<8,float> { __m256 vec; }; |
| 21 | #endif |
Mike Klein | 511ea17 | 2016-10-15 00:13:29 -0400 | [diff] [blame] | 22 | |
Mike Klein | 1e76464 | 2016-10-14 17:09:03 -0400 | [diff] [blame] | 23 | namespace { |
| 24 | |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 25 | template <> |
mtklein | 6c221b4 | 2015-11-20 13:53:19 -0800 | [diff] [blame] | 26 | class SkNx<2, float> { |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 27 | public: |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 28 | AI SkNx(const __m128& vec) : fVec(vec) {} |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 29 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 30 | AI SkNx() {} |
| 31 | AI SkNx(float val) : fVec(_mm_set1_ps(val)) {} |
| 32 | AI static SkNx Load(const void* ptr) { |
mtklein | 507ef6d | 2016-01-31 08:02:47 -0800 | [diff] [blame] | 33 | return _mm_castsi128_ps(_mm_loadl_epi64((const __m128i*)ptr)); |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 34 | } |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 35 | 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] | 36 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 37 | AI void store(void* ptr) const { _mm_storel_pi((__m64*)ptr, fVec); } |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 38 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 39 | AI SkNx operator + (const SkNx& o) const { return _mm_add_ps(fVec, o.fVec); } |
| 40 | AI SkNx operator - (const SkNx& o) const { return _mm_sub_ps(fVec, o.fVec); } |
| 41 | AI SkNx operator * (const SkNx& o) const { return _mm_mul_ps(fVec, o.fVec); } |
| 42 | AI SkNx operator / (const SkNx& o) const { return _mm_div_ps(fVec, o.fVec); } |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 43 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 44 | AI SkNx operator == (const SkNx& o) const { return _mm_cmpeq_ps (fVec, o.fVec); } |
| 45 | AI SkNx operator != (const SkNx& o) const { return _mm_cmpneq_ps(fVec, o.fVec); } |
| 46 | AI SkNx operator < (const SkNx& o) const { return _mm_cmplt_ps (fVec, o.fVec); } |
| 47 | AI SkNx operator > (const SkNx& o) const { return _mm_cmpgt_ps (fVec, o.fVec); } |
| 48 | AI SkNx operator <= (const SkNx& o) const { return _mm_cmple_ps (fVec, o.fVec); } |
| 49 | AI SkNx operator >= (const SkNx& o) const { return _mm_cmpge_ps (fVec, o.fVec); } |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 50 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 51 | AI static SkNx Min(const SkNx& l, const SkNx& r) { return _mm_min_ps(l.fVec, r.fVec); } |
| 52 | AI static SkNx Max(const SkNx& l, const SkNx& r) { return _mm_max_ps(l.fVec, r.fVec); } |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 53 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 54 | AI SkNx sqrt() const { return _mm_sqrt_ps (fVec); } |
| 55 | AI SkNx rsqrt() const { return _mm_rsqrt_ps(fVec); } |
| 56 | AI SkNx invert() const { return _mm_rcp_ps(fVec); } |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 57 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 58 | AI float operator[](int k) const { |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 59 | SkASSERT(0 <= k && k < 2); |
| 60 | union { __m128 v; float fs[4]; } pun = {fVec}; |
mtklein | a156a8f | 2015-04-03 06:16:13 -0700 | [diff] [blame] | 61 | return pun.fs[k&1]; |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 64 | AI bool allTrue() const { return 0xff == (_mm_movemask_epi8(_mm_castps_si128(fVec)) & 0xff); } |
| 65 | AI bool anyTrue() const { return 0x00 != (_mm_movemask_epi8(_mm_castps_si128(fVec)) & 0xff); } |
mtklein | b5e8611 | 2015-06-24 15:18:39 -0700 | [diff] [blame] | 66 | |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 67 | __m128 fVec; |
| 68 | }; |
| 69 | |
| 70 | template <> |
mtklein | 6c221b4 | 2015-11-20 13:53:19 -0800 | [diff] [blame] | 71 | class SkNx<4, float> { |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 72 | public: |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 73 | AI SkNx(const __m128& vec) : fVec(vec) {} |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 74 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 75 | AI SkNx() {} |
| 76 | AI SkNx(float val) : fVec( _mm_set1_ps(val) ) {} |
| 77 | AI SkNx(float a, float b, float c, float d) : fVec(_mm_setr_ps(a,b,c,d)) {} |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 78 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 79 | AI SkNx(const SkNx_abi<4,float>& a) : fVec(a.vec) {} |
| 80 | AI operator SkNx_abi<4,float>() const { return { fVec }; } |
Mike Klein | 511ea17 | 2016-10-15 00:13:29 -0400 | [diff] [blame] | 81 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 82 | AI static SkNx Load(const void* ptr) { return _mm_loadu_ps((const float*)ptr); } |
| 83 | AI void store(void* ptr) const { _mm_storeu_ps((float*)ptr, fVec); } |
mtklein | 9db43ac | 2015-12-01 07:10:21 -0800 | [diff] [blame] | 84 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 85 | 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] | 86 | __m128 v0 = _mm_loadu_ps(((float*)ptr) + 0), |
| 87 | v1 = _mm_loadu_ps(((float*)ptr) + 4), |
| 88 | v2 = _mm_loadu_ps(((float*)ptr) + 8), |
| 89 | v3 = _mm_loadu_ps(((float*)ptr) + 12); |
| 90 | _MM_TRANSPOSE4_PS(v0, v1, v2, v3); |
| 91 | *r = v0; |
| 92 | *g = v1; |
| 93 | *b = v2; |
| 94 | *a = v3; |
| 95 | } |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 96 | 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] | 97 | __m128 v0 = r.fVec, |
| 98 | v1 = g.fVec, |
| 99 | v2 = b.fVec, |
| 100 | v3 = a.fVec; |
| 101 | _MM_TRANSPOSE4_PS(v0, v1, v2, v3); |
| 102 | _mm_storeu_ps(((float*) dst) + 0, v0); |
| 103 | _mm_storeu_ps(((float*) dst) + 4, v1); |
| 104 | _mm_storeu_ps(((float*) dst) + 8, v2); |
| 105 | _mm_storeu_ps(((float*) dst) + 12, v3); |
| 106 | } |
| 107 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 108 | AI SkNx operator + (const SkNx& o) const { return _mm_add_ps(fVec, o.fVec); } |
| 109 | AI SkNx operator - (const SkNx& o) const { return _mm_sub_ps(fVec, o.fVec); } |
| 110 | AI SkNx operator * (const SkNx& o) const { return _mm_mul_ps(fVec, o.fVec); } |
| 111 | AI SkNx operator / (const SkNx& o) const { return _mm_div_ps(fVec, o.fVec); } |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 112 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 113 | AI SkNx operator == (const SkNx& o) const { return _mm_cmpeq_ps (fVec, o.fVec); } |
| 114 | AI SkNx operator != (const SkNx& o) const { return _mm_cmpneq_ps(fVec, o.fVec); } |
| 115 | AI SkNx operator < (const SkNx& o) const { return _mm_cmplt_ps (fVec, o.fVec); } |
| 116 | AI SkNx operator > (const SkNx& o) const { return _mm_cmpgt_ps (fVec, o.fVec); } |
| 117 | AI SkNx operator <= (const SkNx& o) const { return _mm_cmple_ps (fVec, o.fVec); } |
| 118 | AI SkNx operator >= (const SkNx& o) const { return _mm_cmpge_ps (fVec, o.fVec); } |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 119 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 120 | AI static SkNx Min(const SkNx& l, const SkNx& r) { return _mm_min_ps(l.fVec, r.fVec); } |
| 121 | AI static SkNx Max(const SkNx& l, const SkNx& r) { return _mm_max_ps(l.fVec, r.fVec); } |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 122 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 123 | AI SkNx abs() const { return _mm_andnot_ps(_mm_set1_ps(-0.0f), fVec); } |
| 124 | AI SkNx floor() const { |
mtklein | 5608e2e | 2016-07-11 09:59:21 -0700 | [diff] [blame] | 125 | #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE41 |
| 126 | return _mm_floor_ps(fVec); |
| 127 | #else |
mtklein | 244a653 | 2016-04-19 14:21:30 -0700 | [diff] [blame] | 128 | // Emulate _mm_floor_ps() with SSE2: |
| 129 | // - roundtrip through integers via truncation |
| 130 | // - subtract 1 if that's too big (possible for negative values). |
| 131 | // This restricts the domain of our inputs to a maximum somehwere around 2^31. |
| 132 | // Seems plenty big. |
| 133 | __m128 roundtrip = _mm_cvtepi32_ps(_mm_cvttps_epi32(fVec)); |
| 134 | __m128 too_big = _mm_cmpgt_ps(roundtrip, fVec); |
| 135 | return _mm_sub_ps(roundtrip, _mm_and_ps(too_big, _mm_set1_ps(1.0f))); |
mtklein | 5608e2e | 2016-07-11 09:59:21 -0700 | [diff] [blame] | 136 | #endif |
mtklein | 244a653 | 2016-04-19 14:21:30 -0700 | [diff] [blame] | 137 | } |
mtklein | c33065a | 2016-01-15 12:16:40 -0800 | [diff] [blame] | 138 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 139 | AI SkNx sqrt() const { return _mm_sqrt_ps (fVec); } |
| 140 | AI SkNx rsqrt() const { return _mm_rsqrt_ps(fVec); } |
| 141 | AI SkNx invert() const { return _mm_rcp_ps(fVec); } |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 142 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 143 | AI float operator[](int k) const { |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 144 | SkASSERT(0 <= k && k < 4); |
| 145 | union { __m128 v; float fs[4]; } pun = {fVec}; |
mtklein | a156a8f | 2015-04-03 06:16:13 -0700 | [diff] [blame] | 146 | return pun.fs[k&3]; |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 149 | AI bool allTrue() const { return 0xffff == _mm_movemask_epi8(_mm_castps_si128(fVec)); } |
| 150 | AI bool anyTrue() const { return 0x0000 != _mm_movemask_epi8(_mm_castps_si128(fVec)); } |
mtklein | b5e8611 | 2015-06-24 15:18:39 -0700 | [diff] [blame] | 151 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 152 | AI SkNx thenElse(const SkNx& t, const SkNx& e) const { |
mtklein | 5608e2e | 2016-07-11 09:59:21 -0700 | [diff] [blame] | 153 | #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE41 |
msarett | d280957 | 2016-06-20 06:07:45 -0700 | [diff] [blame] | 154 | return _mm_blendv_ps(e.fVec, t.fVec, fVec); |
mtklein | 5608e2e | 2016-07-11 09:59:21 -0700 | [diff] [blame] | 155 | #else |
mtklein | 2aab22a | 2015-06-26 10:46:31 -0700 | [diff] [blame] | 156 | return _mm_or_ps(_mm_and_ps (fVec, t.fVec), |
| 157 | _mm_andnot_ps(fVec, e.fVec)); |
mtklein | 5608e2e | 2016-07-11 09:59:21 -0700 | [diff] [blame] | 158 | #endif |
mtklein | 2aab22a | 2015-06-26 10:46:31 -0700 | [diff] [blame] | 159 | } |
| 160 | |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 161 | __m128 fVec; |
| 162 | }; |
| 163 | |
mtklein | 115acee | 2015-04-14 14:02:52 -0700 | [diff] [blame] | 164 | template <> |
mtklein | d05a875 | 2016-07-29 10:10:15 -0700 | [diff] [blame] | 165 | class SkNx<4, int32_t> { |
mtklein | 8273ca4 | 2016-02-09 11:32:51 -0800 | [diff] [blame] | 166 | public: |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 167 | AI SkNx(const __m128i& vec) : fVec(vec) {} |
mtklein | 8273ca4 | 2016-02-09 11:32:51 -0800 | [diff] [blame] | 168 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 169 | AI SkNx() {} |
| 170 | AI SkNx(int32_t val) : fVec(_mm_set1_epi32(val)) {} |
| 171 | AI static SkNx Load(const void* ptr) { return _mm_loadu_si128((const __m128i*)ptr); } |
| 172 | AI SkNx(int32_t a, int32_t b, int32_t c, int32_t d) : fVec(_mm_setr_epi32(a,b,c,d)) {} |
mtklein | 8273ca4 | 2016-02-09 11:32:51 -0800 | [diff] [blame] | 173 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 174 | AI void store(void* ptr) const { _mm_storeu_si128((__m128i*)ptr, fVec); } |
mtklein | 8273ca4 | 2016-02-09 11:32:51 -0800 | [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_add_epi32(fVec, o.fVec); } |
| 177 | AI SkNx operator - (const SkNx& o) const { return _mm_sub_epi32(fVec, o.fVec); } |
| 178 | AI SkNx operator * (const SkNx& o) const { |
mtklein | 8273ca4 | 2016-02-09 11:32:51 -0800 | [diff] [blame] | 179 | __m128i mul20 = _mm_mul_epu32(fVec, o.fVec), |
| 180 | mul31 = _mm_mul_epu32(_mm_srli_si128(fVec, 4), _mm_srli_si128(o.fVec, 4)); |
| 181 | return _mm_unpacklo_epi32(_mm_shuffle_epi32(mul20, _MM_SHUFFLE(0,0,2,0)), |
| 182 | _mm_shuffle_epi32(mul31, _MM_SHUFFLE(0,0,2,0))); |
| 183 | } |
| 184 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 185 | AI SkNx operator & (const SkNx& o) const { return _mm_and_si128(fVec, o.fVec); } |
| 186 | AI SkNx operator | (const SkNx& o) const { return _mm_or_si128(fVec, o.fVec); } |
| 187 | AI SkNx operator ^ (const SkNx& o) const { return _mm_xor_si128(fVec, o.fVec); } |
mtklein | 64f061a | 2016-06-17 12:09:16 -0700 | [diff] [blame] | 188 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 189 | AI SkNx operator << (int bits) const { return _mm_slli_epi32(fVec, bits); } |
| 190 | AI SkNx operator >> (int bits) const { return _mm_srai_epi32(fVec, bits); } |
mtklein | 8273ca4 | 2016-02-09 11:32:51 -0800 | [diff] [blame] | 191 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 192 | AI SkNx operator == (const SkNx& o) const { return _mm_cmpeq_epi32 (fVec, o.fVec); } |
| 193 | AI SkNx operator < (const SkNx& o) const { return _mm_cmplt_epi32 (fVec, o.fVec); } |
| 194 | AI SkNx operator > (const SkNx& o) const { return _mm_cmpgt_epi32 (fVec, o.fVec); } |
mtklein | 58e389b | 2016-07-15 07:00:11 -0700 | [diff] [blame] | 195 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 196 | AI int32_t operator[](int k) const { |
mtklein | 8273ca4 | 2016-02-09 11:32:51 -0800 | [diff] [blame] | 197 | SkASSERT(0 <= k && k < 4); |
mtklein | d05a875 | 2016-07-29 10:10:15 -0700 | [diff] [blame] | 198 | union { __m128i v; int32_t is[4]; } pun = {fVec}; |
mtklein | 8273ca4 | 2016-02-09 11:32:51 -0800 | [diff] [blame] | 199 | return pun.is[k&3]; |
| 200 | } |
mtklein | 8273ca4 | 2016-02-09 11:32:51 -0800 | [diff] [blame] | 201 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 202 | AI SkNx thenElse(const SkNx& t, const SkNx& e) const { |
mtklein | 58e389b | 2016-07-15 07:00:11 -0700 | [diff] [blame] | 203 | #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE41 |
| 204 | return _mm_blendv_epi8(e.fVec, t.fVec, fVec); |
| 205 | #else |
| 206 | return _mm_or_si128(_mm_and_si128 (fVec, t.fVec), |
| 207 | _mm_andnot_si128(fVec, e.fVec)); |
| 208 | #endif |
| 209 | } |
| 210 | |
mtklein | 8273ca4 | 2016-02-09 11:32:51 -0800 | [diff] [blame] | 211 | __m128i fVec; |
| 212 | }; |
| 213 | |
| 214 | template <> |
mtklein | d05a875 | 2016-07-29 10:10:15 -0700 | [diff] [blame] | 215 | class SkNx<4, uint32_t> { |
| 216 | public: |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 217 | AI SkNx(const __m128i& vec) : fVec(vec) {} |
mtklein | d05a875 | 2016-07-29 10:10:15 -0700 | [diff] [blame] | 218 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 219 | AI SkNx() {} |
| 220 | AI SkNx(uint32_t val) : fVec(_mm_set1_epi32(val)) {} |
| 221 | AI static SkNx Load(const void* ptr) { return _mm_loadu_si128((const __m128i*)ptr); } |
| 222 | AI SkNx(uint32_t a, uint32_t b, uint32_t c, uint32_t d) : fVec(_mm_setr_epi32(a,b,c,d)) {} |
mtklein | d05a875 | 2016-07-29 10:10:15 -0700 | [diff] [blame] | 223 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 224 | AI void store(void* ptr) const { _mm_storeu_si128((__m128i*)ptr, fVec); } |
mtklein | d05a875 | 2016-07-29 10:10:15 -0700 | [diff] [blame] | 225 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 226 | AI SkNx operator + (const SkNx& o) const { return _mm_add_epi32(fVec, o.fVec); } |
| 227 | AI SkNx operator - (const SkNx& o) const { return _mm_sub_epi32(fVec, o.fVec); } |
mtklein | d05a875 | 2016-07-29 10:10:15 -0700 | [diff] [blame] | 228 | // Not quite sure how to best do operator * in SSE2. We probably don't use it. |
| 229 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 230 | AI SkNx operator & (const SkNx& o) const { return _mm_and_si128(fVec, o.fVec); } |
| 231 | AI SkNx operator | (const SkNx& o) const { return _mm_or_si128(fVec, o.fVec); } |
| 232 | AI SkNx operator ^ (const SkNx& o) const { return _mm_xor_si128(fVec, o.fVec); } |
mtklein | d05a875 | 2016-07-29 10:10:15 -0700 | [diff] [blame] | 233 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 234 | AI SkNx operator << (int bits) const { return _mm_slli_epi32(fVec, bits); } |
| 235 | AI SkNx operator >> (int bits) const { return _mm_srli_epi32(fVec, bits); } |
mtklein | d05a875 | 2016-07-29 10:10:15 -0700 | [diff] [blame] | 236 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 237 | AI SkNx operator == (const SkNx& o) const { return _mm_cmpeq_epi32 (fVec, o.fVec); } |
mtklein | d05a875 | 2016-07-29 10:10:15 -0700 | [diff] [blame] | 238 | // operator < and > take a little extra fiddling to make work for unsigned ints. |
| 239 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 240 | AI uint32_t operator[](int k) const { |
mtklein | d05a875 | 2016-07-29 10:10:15 -0700 | [diff] [blame] | 241 | SkASSERT(0 <= k && k < 4); |
| 242 | union { __m128i v; uint32_t us[4]; } pun = {fVec}; |
| 243 | return pun.us[k&3]; |
| 244 | } |
| 245 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 246 | AI SkNx thenElse(const SkNx& t, const SkNx& e) const { |
mtklein | d05a875 | 2016-07-29 10:10:15 -0700 | [diff] [blame] | 247 | #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE41 |
| 248 | return _mm_blendv_epi8(e.fVec, t.fVec, fVec); |
| 249 | #else |
| 250 | return _mm_or_si128(_mm_and_si128 (fVec, t.fVec), |
| 251 | _mm_andnot_si128(fVec, e.fVec)); |
| 252 | #endif |
| 253 | } |
| 254 | |
| 255 | __m128i fVec; |
| 256 | }; |
| 257 | |
| 258 | |
| 259 | template <> |
mtklein | 6c221b4 | 2015-11-20 13:53:19 -0800 | [diff] [blame] | 260 | class SkNx<4, uint16_t> { |
mtklein | 115acee | 2015-04-14 14:02:52 -0700 | [diff] [blame] | 261 | public: |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 262 | AI SkNx(const __m128i& vec) : fVec(vec) {} |
mtklein | 115acee | 2015-04-14 14:02:52 -0700 | [diff] [blame] | 263 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 264 | AI SkNx() {} |
| 265 | AI SkNx(uint16_t val) : fVec(_mm_set1_epi16(val)) {} |
| 266 | AI SkNx(uint16_t a, uint16_t b, uint16_t c, uint16_t d) |
| 267 | : fVec(_mm_setr_epi16(a,b,c,d,0,0,0,0)) {} |
mtklein | 115acee | 2015-04-14 14:02:52 -0700 | [diff] [blame] | 268 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 269 | AI static SkNx Load(const void* ptr) { return _mm_loadl_epi64((const __m128i*)ptr); } |
| 270 | AI void store(void* ptr) const { _mm_storel_epi64((__m128i*)ptr, fVec); } |
mtklein | 115acee | 2015-04-14 14:02:52 -0700 | [diff] [blame] | 271 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 272 | 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] | 273 | __m128i lo = _mm_loadu_si128(((__m128i*)ptr) + 0), |
| 274 | hi = _mm_loadu_si128(((__m128i*)ptr) + 1); |
| 275 | __m128i even = _mm_unpacklo_epi16(lo, hi), // r0 r2 g0 g2 b0 b2 a0 a2 |
| 276 | odd = _mm_unpackhi_epi16(lo, hi); // r1 r3 ... |
| 277 | __m128i rg = _mm_unpacklo_epi16(even, odd), // r0 r1 r2 r3 g0 g1 g2 g3 |
| 278 | ba = _mm_unpackhi_epi16(even, odd); // b0 b1 ... a0 a1 ... |
| 279 | *r = rg; |
| 280 | *g = _mm_srli_si128(rg, 8); |
| 281 | *b = ba; |
| 282 | *a = _mm_srli_si128(ba, 8); |
| 283 | } |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 284 | 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] | 285 | __m128i rg = _mm_unpacklo_epi16(r.fVec, g.fVec); |
| 286 | __m128i ba = _mm_unpacklo_epi16(b.fVec, a.fVec); |
| 287 | __m128i lo = _mm_unpacklo_epi32(rg, ba); |
| 288 | __m128i hi = _mm_unpackhi_epi32(rg, ba); |
| 289 | _mm_storeu_si128(((__m128i*) dst) + 0, lo); |
| 290 | _mm_storeu_si128(((__m128i*) dst) + 1, hi); |
| 291 | } |
| 292 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 293 | AI SkNx operator + (const SkNx& o) const { return _mm_add_epi16(fVec, o.fVec); } |
| 294 | AI SkNx operator - (const SkNx& o) const { return _mm_sub_epi16(fVec, o.fVec); } |
| 295 | AI SkNx operator * (const SkNx& o) const { return _mm_mullo_epi16(fVec, o.fVec); } |
mtklein | 115acee | 2015-04-14 14:02:52 -0700 | [diff] [blame] | 296 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 297 | AI SkNx operator << (int bits) const { return _mm_slli_epi16(fVec, bits); } |
| 298 | AI SkNx operator >> (int bits) const { return _mm_srli_epi16(fVec, bits); } |
mtklein | 115acee | 2015-04-14 14:02:52 -0700 | [diff] [blame] | 299 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 300 | AI uint16_t operator[](int k) const { |
mtklein | 115acee | 2015-04-14 14:02:52 -0700 | [diff] [blame] | 301 | SkASSERT(0 <= k && k < 4); |
mtklein | e4c0bee | 2016-02-09 10:35:27 -0800 | [diff] [blame] | 302 | union { __m128i v; uint16_t us[8]; } pun = {fVec}; |
| 303 | return pun.us[k&3]; |
mtklein | 115acee | 2015-04-14 14:02:52 -0700 | [diff] [blame] | 304 | } |
mtklein | d2ffd36 | 2015-05-12 06:11:21 -0700 | [diff] [blame] | 305 | |
mtklein | 115acee | 2015-04-14 14:02:52 -0700 | [diff] [blame] | 306 | __m128i fVec; |
| 307 | }; |
| 308 | |
| 309 | template <> |
mtklein | 6c221b4 | 2015-11-20 13:53:19 -0800 | [diff] [blame] | 310 | class SkNx<8, uint16_t> { |
mtklein | 115acee | 2015-04-14 14:02:52 -0700 | [diff] [blame] | 311 | public: |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 312 | AI SkNx(const __m128i& vec) : fVec(vec) {} |
mtklein | 115acee | 2015-04-14 14:02:52 -0700 | [diff] [blame] | 313 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 314 | AI SkNx() {} |
| 315 | AI SkNx(uint16_t val) : fVec(_mm_set1_epi16(val)) {} |
| 316 | AI SkNx(uint16_t a, uint16_t b, uint16_t c, uint16_t d, |
| 317 | uint16_t e, uint16_t f, uint16_t g, uint16_t h) |
| 318 | : fVec(_mm_setr_epi16(a,b,c,d,e,f,g,h)) {} |
mtklein | 115acee | 2015-04-14 14:02:52 -0700 | [diff] [blame] | 319 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 320 | AI static SkNx Load(const void* ptr) { return _mm_loadu_si128((const __m128i*)ptr); } |
| 321 | AI void store(void* ptr) const { _mm_storeu_si128((__m128i*)ptr, fVec); } |
mtklein | 115acee | 2015-04-14 14:02:52 -0700 | [diff] [blame] | 322 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 323 | 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] | 324 | // TODO: AVX2 version |
| 325 | __m128i _01 = _mm_loadu_si128(((__m128i*)ptr) + 0), |
| 326 | _23 = _mm_loadu_si128(((__m128i*)ptr) + 1), |
| 327 | _45 = _mm_loadu_si128(((__m128i*)ptr) + 2), |
| 328 | _67 = _mm_loadu_si128(((__m128i*)ptr) + 3); |
| 329 | |
| 330 | __m128i _02 = _mm_unpacklo_epi16(_01, _23), // r0 r2 g0 g2 b0 b2 a0 a2 |
| 331 | _13 = _mm_unpackhi_epi16(_01, _23), // r1 r3 g1 g3 b1 b3 a1 a3 |
| 332 | _46 = _mm_unpacklo_epi16(_45, _67), |
| 333 | _57 = _mm_unpackhi_epi16(_45, _67); |
| 334 | |
| 335 | __m128i rg0123 = _mm_unpacklo_epi16(_02, _13), // r0 r1 r2 r3 g0 g1 g2 g3 |
| 336 | ba0123 = _mm_unpackhi_epi16(_02, _13), // b0 b1 b2 b3 a0 a1 a2 a3 |
| 337 | rg4567 = _mm_unpacklo_epi16(_46, _57), |
| 338 | ba4567 = _mm_unpackhi_epi16(_46, _57); |
| 339 | |
| 340 | *r = _mm_unpacklo_epi64(rg0123, rg4567); |
| 341 | *g = _mm_unpackhi_epi64(rg0123, rg4567); |
| 342 | *b = _mm_unpacklo_epi64(ba0123, ba4567); |
| 343 | *a = _mm_unpackhi_epi64(ba0123, ba4567); |
| 344 | } |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 345 | 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] | 346 | // TODO: AVX2 version |
| 347 | __m128i rg0123 = _mm_unpacklo_epi16(r.fVec, g.fVec), // r0 g0 r1 g1 r2 g2 r3 g3 |
| 348 | rg4567 = _mm_unpackhi_epi16(r.fVec, g.fVec), // r4 g4 r5 g5 r6 g6 r7 g7 |
| 349 | ba0123 = _mm_unpacklo_epi16(b.fVec, a.fVec), |
| 350 | ba4567 = _mm_unpackhi_epi16(b.fVec, a.fVec); |
| 351 | |
| 352 | _mm_storeu_si128((__m128i*)ptr + 0, _mm_unpacklo_epi32(rg0123, ba0123)); |
| 353 | _mm_storeu_si128((__m128i*)ptr + 1, _mm_unpackhi_epi32(rg0123, ba0123)); |
| 354 | _mm_storeu_si128((__m128i*)ptr + 2, _mm_unpacklo_epi32(rg4567, ba4567)); |
| 355 | _mm_storeu_si128((__m128i*)ptr + 3, _mm_unpackhi_epi32(rg4567, ba4567)); |
| 356 | } |
| 357 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 358 | AI SkNx operator + (const SkNx& o) const { return _mm_add_epi16(fVec, o.fVec); } |
| 359 | AI SkNx operator - (const SkNx& o) const { return _mm_sub_epi16(fVec, o.fVec); } |
| 360 | AI SkNx operator * (const SkNx& o) const { return _mm_mullo_epi16(fVec, o.fVec); } |
mtklein | 115acee | 2015-04-14 14:02:52 -0700 | [diff] [blame] | 361 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 362 | AI SkNx operator << (int bits) const { return _mm_slli_epi16(fVec, bits); } |
| 363 | AI SkNx operator >> (int bits) const { return _mm_srli_epi16(fVec, bits); } |
mtklein | 115acee | 2015-04-14 14:02:52 -0700 | [diff] [blame] | 364 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 365 | AI static SkNx Min(const SkNx& a, const SkNx& b) { |
mtklein | 27e517a | 2015-05-14 17:53:04 -0700 | [diff] [blame] | 366 | // No unsigned _mm_min_epu16, so we'll shift into a space where we can use the |
| 367 | // signed version, _mm_min_epi16, then shift back. |
| 368 | const uint16_t top = 0x8000; // Keep this separate from _mm_set1_epi16 or MSVC will whine. |
| 369 | const __m128i top_8x = _mm_set1_epi16(top); |
| 370 | return _mm_add_epi8(top_8x, _mm_min_epi16(_mm_sub_epi8(a.fVec, top_8x), |
| 371 | _mm_sub_epi8(b.fVec, top_8x))); |
| 372 | } |
| 373 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 374 | AI SkNx thenElse(const SkNx& t, const SkNx& e) const { |
mtklein | 4be181e | 2015-07-14 10:54:19 -0700 | [diff] [blame] | 375 | return _mm_or_si128(_mm_and_si128 (fVec, t.fVec), |
| 376 | _mm_andnot_si128(fVec, e.fVec)); |
| 377 | } |
| 378 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 379 | AI uint16_t operator[](int k) const { |
mtklein | 115acee | 2015-04-14 14:02:52 -0700 | [diff] [blame] | 380 | SkASSERT(0 <= k && k < 8); |
mtklein | e4c0bee | 2016-02-09 10:35:27 -0800 | [diff] [blame] | 381 | union { __m128i v; uint16_t us[8]; } pun = {fVec}; |
| 382 | return pun.us[k&7]; |
mtklein | 115acee | 2015-04-14 14:02:52 -0700 | [diff] [blame] | 383 | } |
mtklein | d2ffd36 | 2015-05-12 06:11:21 -0700 | [diff] [blame] | 384 | |
| 385 | __m128i fVec; |
| 386 | }; |
| 387 | |
| 388 | template <> |
mtklein | 6f37b4a | 2015-12-14 11:25:18 -0800 | [diff] [blame] | 389 | class SkNx<4, uint8_t> { |
| 390 | public: |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 391 | AI SkNx() {} |
| 392 | AI SkNx(const __m128i& vec) : fVec(vec) {} |
| 393 | AI SkNx(uint8_t a, uint8_t b, uint8_t c, uint8_t d) |
herb | fd5a260 | 2016-03-01 07:01:23 -0800 | [diff] [blame] | 394 | : fVec(_mm_setr_epi8(a,b,c,d, 0,0,0,0, 0,0,0,0, 0,0,0,0)) {} |
| 395 | |
| 396 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 397 | AI static SkNx Load(const void* ptr) { return _mm_cvtsi32_si128(*(const int*)ptr); } |
| 398 | AI void store(void* ptr) const { *(int*)ptr = _mm_cvtsi128_si32(fVec); } |
mtklein | 6f37b4a | 2015-12-14 11:25:18 -0800 | [diff] [blame] | 399 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 400 | AI uint8_t operator[](int k) const { |
herb | fd5a260 | 2016-03-01 07:01:23 -0800 | [diff] [blame] | 401 | SkASSERT(0 <= k && k < 4); |
| 402 | union { __m128i v; uint8_t us[16]; } pun = {fVec}; |
| 403 | return pun.us[k&3]; |
| 404 | } |
| 405 | |
mtklein | 6f37b4a | 2015-12-14 11:25:18 -0800 | [diff] [blame] | 406 | // TODO as needed |
| 407 | |
| 408 | __m128i fVec; |
| 409 | }; |
| 410 | |
| 411 | template <> |
mtklein | 6c221b4 | 2015-11-20 13:53:19 -0800 | [diff] [blame] | 412 | class SkNx<16, uint8_t> { |
mtklein | d2ffd36 | 2015-05-12 06:11:21 -0700 | [diff] [blame] | 413 | public: |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 414 | AI SkNx(const __m128i& vec) : fVec(vec) {} |
mtklein | d2ffd36 | 2015-05-12 06:11:21 -0700 | [diff] [blame] | 415 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 416 | AI SkNx() {} |
| 417 | AI SkNx(uint8_t val) : fVec(_mm_set1_epi8(val)) {} |
| 418 | AI static SkNx Load(const void* ptr) { return _mm_loadu_si128((const __m128i*)ptr); } |
| 419 | AI SkNx(uint8_t a, uint8_t b, uint8_t c, uint8_t d, |
| 420 | uint8_t e, uint8_t f, uint8_t g, uint8_t h, |
| 421 | uint8_t i, uint8_t j, uint8_t k, uint8_t l, |
| 422 | uint8_t m, uint8_t n, uint8_t o, uint8_t p) |
mtklein | d2ffd36 | 2015-05-12 06:11:21 -0700 | [diff] [blame] | 423 | : fVec(_mm_setr_epi8(a,b,c,d, e,f,g,h, i,j,k,l, m,n,o,p)) {} |
| 424 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 425 | AI void store(void* ptr) const { _mm_storeu_si128((__m128i*)ptr, fVec); } |
mtklein | d2ffd36 | 2015-05-12 06:11:21 -0700 | [diff] [blame] | 426 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 427 | 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] | 428 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 429 | AI SkNx operator + (const SkNx& o) const { return _mm_add_epi8(fVec, o.fVec); } |
| 430 | AI SkNx operator - (const SkNx& o) const { return _mm_sub_epi8(fVec, o.fVec); } |
mtklein | d2ffd36 | 2015-05-12 06:11:21 -0700 | [diff] [blame] | 431 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 432 | AI static SkNx Min(const SkNx& a, const SkNx& b) { return _mm_min_epu8(a.fVec, b.fVec); } |
| 433 | AI SkNx operator < (const SkNx& o) const { |
mtklein | b5e8611 | 2015-06-24 15:18:39 -0700 | [diff] [blame] | 434 | // There's no unsigned _mm_cmplt_epu8, so we flip the sign bits then use a signed compare. |
| 435 | auto flip = _mm_set1_epi8(char(0x80)); |
| 436 | return _mm_cmplt_epi8(_mm_xor_si128(flip, fVec), _mm_xor_si128(flip, o.fVec)); |
| 437 | } |
mtklein | 27e517a | 2015-05-14 17:53:04 -0700 | [diff] [blame] | 438 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 439 | AI uint8_t operator[](int k) const { |
mtklein | d2ffd36 | 2015-05-12 06:11:21 -0700 | [diff] [blame] | 440 | SkASSERT(0 <= k && k < 16); |
mtklein | e4c0bee | 2016-02-09 10:35:27 -0800 | [diff] [blame] | 441 | union { __m128i v; uint8_t us[16]; } pun = {fVec}; |
| 442 | return pun.us[k&15]; |
mtklein | d2ffd36 | 2015-05-12 06:11:21 -0700 | [diff] [blame] | 443 | } |
| 444 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 445 | AI SkNx thenElse(const SkNx& t, const SkNx& e) const { |
mtklein | b5e8611 | 2015-06-24 15:18:39 -0700 | [diff] [blame] | 446 | return _mm_or_si128(_mm_and_si128 (fVec, t.fVec), |
| 447 | _mm_andnot_si128(fVec, e.fVec)); |
| 448 | } |
| 449 | |
mtklein | 115acee | 2015-04-14 14:02:52 -0700 | [diff] [blame] | 450 | __m128i fVec; |
| 451 | }; |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 452 | |
Mike Klein | 04adfda | 2016-10-12 09:52:55 -0400 | [diff] [blame] | 453 | #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_AVX2 |
| 454 | |
Mike Klein | 04adfda | 2016-10-12 09:52:55 -0400 | [diff] [blame] | 455 | template <> |
| 456 | class SkNx<8, uint8_t> { |
| 457 | public: |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 458 | AI SkNx(const __m128i& vec) : fVec(vec) {} |
Mike Klein | 04adfda | 2016-10-12 09:52:55 -0400 | [diff] [blame] | 459 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 460 | AI SkNx() {} |
| 461 | AI SkNx(uint8_t v) : fVec(_mm_set1_epi8(v)) {} |
| 462 | AI SkNx(uint8_t a, uint8_t b, uint8_t c, uint8_t d, |
| 463 | uint8_t e, uint8_t f, uint8_t g, uint8_t h) |
Mike Klein | 04adfda | 2016-10-12 09:52:55 -0400 | [diff] [blame] | 464 | : fVec(_mm_setr_epi8(a,b,c,d, e,f,g,h, 0,0,0,0, 0,0,0,0)) {} |
| 465 | |
| 466 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 467 | AI static SkNx Load(const void* ptr) { return _mm_loadl_epi64((const __m128i*)ptr); } |
| 468 | AI void store(void* ptr) const { _mm_storel_epi64((__m128i*)ptr, fVec); } |
Mike Klein | 04adfda | 2016-10-12 09:52:55 -0400 | [diff] [blame] | 469 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 470 | AI uint8_t operator[](int k) const { |
Mike Klein | 04adfda | 2016-10-12 09:52:55 -0400 | [diff] [blame] | 471 | SkASSERT(0 <= k && k < 8); |
| 472 | union { __m128i v; uint8_t us[16]; } pun = {fVec}; |
| 473 | return pun.us[k&7]; |
| 474 | } |
| 475 | |
| 476 | __m128i fVec; |
| 477 | }; |
| 478 | |
| 479 | template <> |
| 480 | class SkNx<8, int32_t> { |
| 481 | public: |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 482 | AI SkNx(const __m256i& vec) : fVec(vec) {} |
Mike Klein | 04adfda | 2016-10-12 09:52:55 -0400 | [diff] [blame] | 483 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 484 | AI SkNx() {} |
| 485 | AI SkNx(int32_t v) : fVec(_mm256_set1_epi32(v)) {} |
| 486 | AI SkNx(int32_t a, int32_t b, int32_t c, int32_t d, |
| 487 | int32_t e, int32_t f, int32_t g, int32_t h) |
Mike Klein | 04adfda | 2016-10-12 09:52:55 -0400 | [diff] [blame] | 488 | : fVec(_mm256_setr_epi32(a,b,c,d, e,f,g,h)) {} |
| 489 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 490 | AI static SkNx Load(const void* ptr) { return _mm256_loadu_si256((const __m256i*)ptr); } |
| 491 | AI void store(void* ptr) const { _mm256_storeu_si256((__m256i*)ptr, fVec); } |
Mike Klein | 04adfda | 2016-10-12 09:52:55 -0400 | [diff] [blame] | 492 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 493 | AI SkNx operator + (const SkNx& o) const { return _mm256_add_epi32(fVec, o.fVec); } |
| 494 | AI SkNx operator - (const SkNx& o) const { return _mm256_sub_epi32(fVec, o.fVec); } |
| 495 | AI SkNx operator * (const SkNx& o) const { return _mm256_mullo_epi32(fVec, o.fVec); } |
Mike Klein | 04adfda | 2016-10-12 09:52:55 -0400 | [diff] [blame] | 496 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 497 | AI SkNx operator & (const SkNx& o) const { return _mm256_and_si256(fVec, o.fVec); } |
| 498 | AI SkNx operator | (const SkNx& o) const { return _mm256_or_si256(fVec, o.fVec); } |
| 499 | AI SkNx operator ^ (const SkNx& o) const { return _mm256_xor_si256(fVec, o.fVec); } |
Mike Klein | 04adfda | 2016-10-12 09:52:55 -0400 | [diff] [blame] | 500 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 501 | AI SkNx operator << (int bits) const { return _mm256_slli_epi32(fVec, bits); } |
| 502 | AI SkNx operator >> (int bits) const { return _mm256_srai_epi32(fVec, bits); } |
Mike Klein | 04adfda | 2016-10-12 09:52:55 -0400 | [diff] [blame] | 503 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 504 | AI int32_t operator[](int k) const { |
Mike Klein | 04adfda | 2016-10-12 09:52:55 -0400 | [diff] [blame] | 505 | SkASSERT(0 <= k && k < 8); |
| 506 | union { __m256i v; int32_t is[8]; } pun = {fVec}; |
| 507 | return pun.is[k&7]; |
| 508 | } |
| 509 | |
| 510 | __m256i fVec; |
| 511 | }; |
| 512 | |
| 513 | template <> |
| 514 | class SkNx<8, uint32_t> { |
| 515 | public: |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 516 | AI SkNx(const __m256i& vec) : fVec(vec) {} |
Mike Klein | 04adfda | 2016-10-12 09:52:55 -0400 | [diff] [blame] | 517 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 518 | AI SkNx() {} |
| 519 | AI SkNx(uint32_t v) : fVec(_mm256_set1_epi32(v)) {} |
| 520 | AI SkNx(uint32_t a, uint32_t b, uint32_t c, uint32_t d, |
| 521 | uint32_t e, uint32_t f, uint32_t g, uint32_t h) |
Mike Klein | 04adfda | 2016-10-12 09:52:55 -0400 | [diff] [blame] | 522 | : fVec(_mm256_setr_epi32(a,b,c,d, e,f,g,h)) {} |
| 523 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 524 | AI static SkNx Load(const void* ptr) { return _mm256_loadu_si256((const __m256i*)ptr); } |
| 525 | AI void store(void* ptr) const { _mm256_storeu_si256((__m256i*)ptr, fVec); } |
Mike Klein | 04adfda | 2016-10-12 09:52:55 -0400 | [diff] [blame] | 526 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 527 | AI SkNx operator + (const SkNx& o) const { return _mm256_add_epi32(fVec, o.fVec); } |
| 528 | AI SkNx operator - (const SkNx& o) const { return _mm256_sub_epi32(fVec, o.fVec); } |
| 529 | AI SkNx operator * (const SkNx& o) const { return _mm256_mullo_epi32(fVec, o.fVec); } |
Mike Klein | 04adfda | 2016-10-12 09:52:55 -0400 | [diff] [blame] | 530 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 531 | AI SkNx operator & (const SkNx& o) const { return _mm256_and_si256(fVec, o.fVec); } |
| 532 | AI SkNx operator | (const SkNx& o) const { return _mm256_or_si256(fVec, o.fVec); } |
| 533 | AI SkNx operator ^ (const SkNx& o) const { return _mm256_xor_si256(fVec, o.fVec); } |
Mike Klein | 04adfda | 2016-10-12 09:52:55 -0400 | [diff] [blame] | 534 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 535 | AI SkNx operator << (int bits) const { return _mm256_slli_epi32(fVec, bits); } |
| 536 | AI SkNx operator >> (int bits) const { return _mm256_srli_epi32(fVec, bits); } |
Mike Klein | 04adfda | 2016-10-12 09:52:55 -0400 | [diff] [blame] | 537 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 538 | AI uint32_t operator[](int k) const { |
Mike Klein | 04adfda | 2016-10-12 09:52:55 -0400 | [diff] [blame] | 539 | SkASSERT(0 <= k && k < 8); |
| 540 | union { __m256i v; uint32_t us[8]; } pun = {fVec}; |
| 541 | return pun.us[k&7]; |
| 542 | } |
| 543 | |
| 544 | __m256i fVec; |
| 545 | }; |
| 546 | |
| 547 | template <> |
| 548 | class SkNx<8, float> { |
| 549 | public: |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 550 | AI SkNx(const __m256& vec) : fVec(vec) {} |
Mike Klein | 04adfda | 2016-10-12 09:52:55 -0400 | [diff] [blame] | 551 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 552 | AI SkNx() {} |
| 553 | AI SkNx(float val) : fVec(_mm256_set1_ps(val)) {} |
| 554 | AI SkNx(float a, float b, float c, float d, |
| 555 | float e, float f, float g, float h) : fVec(_mm256_setr_ps(a,b,c,d,e,f,g,h)) {} |
Mike Klein | 04adfda | 2016-10-12 09:52:55 -0400 | [diff] [blame] | 556 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 557 | AI SkNx(const SkNx_abi<8,float>& a) : fVec(a.vec) {} |
| 558 | AI operator SkNx_abi<8,float>() const { return { fVec }; } |
Mike Klein | 5ec5590 | 2016-10-15 01:18:45 -0400 | [diff] [blame] | 559 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 560 | AI static SkNx Load(const void* ptr) { return _mm256_loadu_ps((const float*)ptr); } |
| 561 | AI void store(void* ptr) const { _mm256_storeu_ps((float*)ptr, fVec); } |
Mike Klein | 04adfda | 2016-10-12 09:52:55 -0400 | [diff] [blame] | 562 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 563 | AI SkNx operator+(const SkNx& o) const { return _mm256_add_ps(fVec, o.fVec); } |
| 564 | AI SkNx operator-(const SkNx& o) const { return _mm256_sub_ps(fVec, o.fVec); } |
| 565 | AI SkNx operator*(const SkNx& o) const { return _mm256_mul_ps(fVec, o.fVec); } |
| 566 | AI SkNx operator/(const SkNx& o) const { return _mm256_div_ps(fVec, o.fVec); } |
Mike Klein | 04adfda | 2016-10-12 09:52:55 -0400 | [diff] [blame] | 567 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 568 | AI SkNx operator==(const SkNx& o) const { return _mm256_cmp_ps(fVec, o.fVec, _CMP_EQ_OQ); } |
| 569 | AI SkNx operator!=(const SkNx& o) const { return _mm256_cmp_ps(fVec, o.fVec, _CMP_NEQ_OQ); } |
| 570 | AI SkNx operator <(const SkNx& o) const { return _mm256_cmp_ps(fVec, o.fVec, _CMP_LT_OQ); } |
| 571 | AI SkNx operator >(const SkNx& o) const { return _mm256_cmp_ps(fVec, o.fVec, _CMP_GT_OQ); } |
| 572 | AI SkNx operator<=(const SkNx& o) const { return _mm256_cmp_ps(fVec, o.fVec, _CMP_LE_OQ); } |
| 573 | AI SkNx operator>=(const SkNx& o) const { return _mm256_cmp_ps(fVec, o.fVec, _CMP_GE_OQ); } |
Mike Klein | 04adfda | 2016-10-12 09:52:55 -0400 | [diff] [blame] | 574 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 575 | AI static SkNx Min(const SkNx& l, const SkNx& r) { return _mm256_min_ps(l.fVec, r.fVec); } |
| 576 | AI static SkNx Max(const SkNx& l, const SkNx& r) { return _mm256_max_ps(l.fVec, r.fVec); } |
Mike Klein | 04adfda | 2016-10-12 09:52:55 -0400 | [diff] [blame] | 577 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 578 | AI SkNx sqrt() const { return _mm256_sqrt_ps (fVec); } |
| 579 | AI SkNx rsqrt() const { return _mm256_rsqrt_ps(fVec); } |
| 580 | AI SkNx invert() const { return _mm256_rcp_ps (fVec); } |
Mike Klein | 04adfda | 2016-10-12 09:52:55 -0400 | [diff] [blame] | 581 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 582 | AI float operator[](int k) const { |
Mike Klein | 04adfda | 2016-10-12 09:52:55 -0400 | [diff] [blame] | 583 | SkASSERT(0 <= k && k < 8); |
| 584 | union { __m256 v; float fs[8]; } pun = {fVec}; |
| 585 | return pun.fs[k&7]; |
| 586 | } |
| 587 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 588 | AI SkNx thenElse(const SkNx& t, const SkNx& e) const { |
Mike Klein | 04adfda | 2016-10-12 09:52:55 -0400 | [diff] [blame] | 589 | return _mm256_blendv_ps(e.fVec, t.fVec, fVec); |
| 590 | } |
| 591 | |
| 592 | __m256 fVec; |
| 593 | }; |
| 594 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 595 | AI static void SkNx_split(const Sk8f& v, Sk4f* lo, Sk4f* hi) { |
Mike Klein | 04adfda | 2016-10-12 09:52:55 -0400 | [diff] [blame] | 596 | *lo = _mm256_extractf128_ps(v.fVec, 0); |
| 597 | *hi = _mm256_extractf128_ps(v.fVec, 1); |
| 598 | } |
| 599 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 600 | AI static Sk8f SkNx_join(const Sk4f& lo, const Sk4f& hi) { |
Mike Klein | 04adfda | 2016-10-12 09:52:55 -0400 | [diff] [blame] | 601 | return _mm256_insertf128_ps(_mm256_castps128_ps256(lo.fVec), hi.fVec, 1); |
| 602 | } |
| 603 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 604 | AI static Sk8f SkNx_fma(const Sk8f& a, const Sk8f& b, const Sk8f& c) { |
Mike Klein | 04adfda | 2016-10-12 09:52:55 -0400 | [diff] [blame] | 605 | return _mm256_fmadd_ps(a.fVec, b.fVec, c.fVec); |
| 606 | } |
| 607 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 608 | template<> AI /*static*/ Sk8f SkNx_cast<float>(const Sk8b& src) { |
Mike Klein | 04adfda | 2016-10-12 09:52:55 -0400 | [diff] [blame] | 609 | return _mm256_cvtepi32_ps(_mm256_cvtepu8_epi32(src.fVec)); |
| 610 | } |
| 611 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 612 | template<> AI /*static*/ Sk8f SkNx_cast<float>(const Sk8i& src) { |
Mike Klein | 04adfda | 2016-10-12 09:52:55 -0400 | [diff] [blame] | 613 | return _mm256_cvtepi32_ps(src.fVec); |
| 614 | } |
| 615 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 616 | template<> AI /*static*/ Sk8i SkNx_cast<int>(const Sk8f& src) { |
Mike Klein | 04adfda | 2016-10-12 09:52:55 -0400 | [diff] [blame] | 617 | return _mm256_cvttps_epi32(src.fVec); |
| 618 | } |
| 619 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 620 | template<> AI /*static*/ Sk8i SkNx_cast<int>(const Sk8h& src) { |
Mike Klein | 04adfda | 2016-10-12 09:52:55 -0400 | [diff] [blame] | 621 | return _mm256_cvtepu16_epi32(src.fVec); |
| 622 | } |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 623 | template<> AI /*static*/ Sk8h SkNx_cast<uint16_t>(const Sk8i& src) { |
Mike Klein | 04adfda | 2016-10-12 09:52:55 -0400 | [diff] [blame] | 624 | __m128i lo = _mm256_extractf128_si256(src.fVec, 0), |
| 625 | hi = _mm256_extractf128_si256(src.fVec, 1); |
| 626 | return _mm_packus_epi32(lo, hi); |
| 627 | } |
| 628 | |
Mike Klein | 04adfda | 2016-10-12 09:52:55 -0400 | [diff] [blame] | 629 | #endif |
| 630 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 631 | template<> AI /*static*/ Sk4f SkNx_cast<float, int32_t>(const Sk4i& src) { |
mtklein | 0cf795f | 2016-02-17 07:23:36 -0800 | [diff] [blame] | 632 | return _mm_cvtepi32_ps(src.fVec); |
| 633 | } |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 634 | template<> AI /*static*/ Sk4f SkNx_cast<float, uint32_t>(const Sk4u& src) { |
mtklein | d05a875 | 2016-07-29 10:10:15 -0700 | [diff] [blame] | 635 | return SkNx_cast<float>(Sk4i::Load(&src)); |
| 636 | } |
mtklein | 0cf795f | 2016-02-17 07:23:36 -0800 | [diff] [blame] | 637 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 638 | template <> AI /*static*/ Sk4i SkNx_cast<int32_t, float>(const Sk4f& src) { |
mtklein | 0cf795f | 2016-02-17 07:23:36 -0800 | [diff] [blame] | 639 | return _mm_cvttps_epi32(src.fVec); |
| 640 | } |
mtklein | 6c221b4 | 2015-11-20 13:53:19 -0800 | [diff] [blame] | 641 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 642 | template<> AI /*static*/ Sk4h SkNx_cast<uint16_t, int32_t>(const Sk4i& src) { |
mtklein | 036e183 | 2016-07-15 07:45:53 -0700 | [diff] [blame] | 643 | #if 0 && SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE41 |
| 644 | // TODO: This seems to be causing code generation problems. Investigate? |
| 645 | return _mm_packus_epi32(src.fVec); |
| 646 | #elif SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSSE3 |
mtklein | 629f25a | 2016-02-08 05:54:38 -0800 | [diff] [blame] | 647 | // With SSSE3, we can just shuffle the low 2 bytes from each lane right into place. |
| 648 | const int _ = ~0; |
mtklein | 036e183 | 2016-07-15 07:45:53 -0700 | [diff] [blame] | 649 | 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] | 650 | #else |
mtklein | 036e183 | 2016-07-15 07:45:53 -0700 | [diff] [blame] | 651 | // With SSE2, we have to sign extend our input, making _mm_packs_epi32 do the pack we want. |
| 652 | __m128i x = _mm_srai_epi32(_mm_slli_epi32(src.fVec, 16), 16); |
| 653 | return _mm_packs_epi32(x,x); |
mtklein | 629f25a | 2016-02-08 05:54:38 -0800 | [diff] [blame] | 654 | #endif |
mtklein | 2d340f2 | 2016-02-06 19:38:39 -0800 | [diff] [blame] | 655 | } |
| 656 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 657 | template<> AI /*static*/ Sk4h SkNx_cast<uint16_t, float>(const Sk4f& src) { |
mtklein | d05a875 | 2016-07-29 10:10:15 -0700 | [diff] [blame] | 658 | return SkNx_cast<uint16_t>(SkNx_cast<int32_t>(src)); |
mtklein | 036e183 | 2016-07-15 07:45:53 -0700 | [diff] [blame] | 659 | } |
| 660 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 661 | template<> AI /*static*/ Sk4b SkNx_cast<uint8_t, float>(const Sk4f& src) { |
mtklein | 97120a7 | 2016-02-12 14:19:06 -0800 | [diff] [blame] | 662 | auto _32 = _mm_cvttps_epi32(src.fVec); |
| 663 | #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSSE3 |
| 664 | const int _ = ~0; |
| 665 | return _mm_shuffle_epi8(_32, _mm_setr_epi8(0,4,8,12, _,_,_,_, _,_,_,_, _,_,_,_)); |
| 666 | #else |
| 667 | auto _16 = _mm_packus_epi16(_32, _32); |
| 668 | return _mm_packus_epi16(_16, _16); |
| 669 | #endif |
mtklein | c1eb311 | 2016-02-11 08:10:22 -0800 | [diff] [blame] | 670 | } |
| 671 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 672 | template<> AI /*static*/ Sk4f SkNx_cast<float, uint8_t>(const Sk4b& src) { |
mtklein | 97120a7 | 2016-02-12 14:19:06 -0800 | [diff] [blame] | 673 | #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSSE3 |
| 674 | const int _ = ~0; |
| 675 | auto _32 = _mm_shuffle_epi8(src.fVec, _mm_setr_epi8(0,_,_,_, 1,_,_,_, 2,_,_,_, 3,_,_,_)); |
| 676 | #else |
| 677 | auto _16 = _mm_unpacklo_epi8(src.fVec, _mm_setzero_si128()), |
| 678 | _32 = _mm_unpacklo_epi16(_16, _mm_setzero_si128()); |
| 679 | #endif |
| 680 | return _mm_cvtepi32_ps(_32); |
mtklein | c1eb311 | 2016-02-11 08:10:22 -0800 | [diff] [blame] | 681 | } |
mtklein | 97120a7 | 2016-02-12 14:19:06 -0800 | [diff] [blame] | 682 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 683 | template<> AI /*static*/ Sk4f SkNx_cast<float, uint16_t>(const Sk4h& src) { |
mtklein | 97120a7 | 2016-02-12 14:19:06 -0800 | [diff] [blame] | 684 | auto _32 = _mm_unpacklo_epi16(src.fVec, _mm_setzero_si128()); |
| 685 | return _mm_cvtepi32_ps(_32); |
mtklein | 2d340f2 | 2016-02-06 19:38:39 -0800 | [diff] [blame] | 686 | } |
| 687 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 688 | template<> AI /*static*/ Sk16b SkNx_cast<uint8_t, float>(const Sk16f& src) { |
mtklein | f8f90e4 | 2016-03-21 10:04:46 -0700 | [diff] [blame] | 689 | Sk8f ab, cd; |
| 690 | SkNx_split(src, &ab, &cd); |
| 691 | |
| 692 | Sk4f a,b,c,d; |
| 693 | SkNx_split(ab, &a, &b); |
| 694 | SkNx_split(cd, &c, &d); |
| 695 | |
| 696 | return _mm_packus_epi16(_mm_packus_epi16(_mm_cvttps_epi32(a.fVec), |
| 697 | _mm_cvttps_epi32(b.fVec)), |
| 698 | _mm_packus_epi16(_mm_cvttps_epi32(c.fVec), |
| 699 | _mm_cvttps_epi32(d.fVec))); |
mtklein | 97120a7 | 2016-02-12 14:19:06 -0800 | [diff] [blame] | 700 | } |
| 701 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 702 | template<> AI /*static*/ Sk4h SkNx_cast<uint16_t, uint8_t>(const Sk4b& src) { |
mtklein | 97120a7 | 2016-02-12 14:19:06 -0800 | [diff] [blame] | 703 | return _mm_unpacklo_epi8(src.fVec, _mm_setzero_si128()); |
| 704 | } |
| 705 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 706 | template<> AI /*static*/ Sk4b SkNx_cast<uint8_t, uint16_t>(const Sk4h& src) { |
mtklein | 97120a7 | 2016-02-12 14:19:06 -0800 | [diff] [blame] | 707 | return _mm_packus_epi16(src.fVec, src.fVec); |
mtklein | 550e9b0 | 2016-01-20 11:55:51 -0800 | [diff] [blame] | 708 | } |
| 709 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 710 | template<> AI /*static*/ Sk4i SkNx_cast<int32_t, uint16_t>(const Sk4h& src) { |
mtklein | 58e389b | 2016-07-15 07:00:11 -0700 | [diff] [blame] | 711 | return _mm_unpacklo_epi16(src.fVec, _mm_setzero_si128()); |
| 712 | } |
| 713 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 714 | template<> AI /*static*/ Sk4b SkNx_cast<uint8_t, int32_t>(const Sk4i& src) { |
msarett | 7d3ff71 | 2016-07-12 14:55:45 -0700 | [diff] [blame] | 715 | return _mm_packus_epi16(_mm_packus_epi16(src.fVec, src.fVec), src.fVec); |
| 716 | } |
| 717 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 718 | template<> AI /*static*/ Sk4i SkNx_cast<int32_t, uint32_t>(const Sk4u& src) { |
msarett | 15ee3de | 2016-08-02 11:30:30 -0700 | [diff] [blame] | 719 | return src.fVec; |
| 720 | } |
| 721 | |
Mike Klein | 7c78f3a | 2016-10-19 09:21:11 -0400 | [diff] [blame] | 722 | AI static Sk4i Sk4f_round(const Sk4f& x) { |
msarett | 7d3ff71 | 2016-07-12 14:55:45 -0700 | [diff] [blame] | 723 | return _mm_cvtps_epi32(x.fVec); |
| 724 | } |
| 725 | |
Mike Klein | 1e76464 | 2016-10-14 17:09:03 -0400 | [diff] [blame] | 726 | } // namespace |
| 727 | |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 728 | #endif//SkNx_sse_DEFINED |