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