OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 namespace { // See Sk4px.h | 8 namespace { // See Sk4px.h |
9 | 9 |
10 inline Sk4px Sk4px::DupPMColor(SkPMColor px) { return Sk16b(_mm_set1_epi32(px));
} | 10 inline Sk4px Sk4px::DupPMColor(SkPMColor px) { return Sk16b(_mm_set1_epi32(px));
} |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 | 86 |
87 inline Sk4px Sk4px::zeroColors() const { | 87 inline Sk4px Sk4px::zeroColors() const { |
88 return Sk16b(_mm_and_si128(_mm_set1_epi32(0xFF << SK_A32_SHIFT), this->fVec)
); | 88 return Sk16b(_mm_and_si128(_mm_set1_epi32(0xFF << SK_A32_SHIFT), this->fVec)
); |
89 } | 89 } |
90 | 90 |
91 inline Sk4px Sk4px::zeroAlphas() const { | 91 inline Sk4px Sk4px::zeroAlphas() const { |
92 // andnot(a,b) == ~a & b | 92 // andnot(a,b) == ~a & b |
93 return Sk16b(_mm_andnot_si128(_mm_set1_epi32(0xFF << SK_A32_SHIFT), this->fV
ec)); | 93 return Sk16b(_mm_andnot_si128(_mm_set1_epi32(0xFF << SK_A32_SHIFT), this->fV
ec)); |
94 } | 94 } |
95 | 95 |
| 96 static inline __m128i widen_low_half_to_8888(__m128i v) { |
| 97 // RGB565 format: |R....|G.....|B....| |
| 98 // Bit: 16 11 5 0 |
| 99 |
| 100 // First get each pixel into its own 32-bit lane. |
| 101 // v == ____ ____ ____ ____ rgb3 rgb2 rgb1 rgb0 |
| 102 // spread == 0000 rgb3 0000 rgb2 0000 rgb1 0000 rgb0 |
| 103 auto spread = _mm_unpacklo_epi16(v, _mm_setzero_si128()); |
| 104 |
| 105 // Get each color independently, still in 565 precison but down at bit 0. |
| 106 auto r5 = _mm_srli_epi32(spread, 11), |
| 107 g6 = _mm_and_si128(_mm_set1_epi32(63), _mm_srli_epi32(spread, 5)), |
| 108 b5 = _mm_and_si128(_mm_set1_epi32(31), spread); |
| 109 |
| 110 // Scale 565 precision up to 8-bit each, filling low 323 bits with high bits
of each component. |
| 111 auto r8 = _mm_or_si128(_mm_slli_epi32(r5, 3), _mm_srli_epi32(r5, 2)), |
| 112 g8 = _mm_or_si128(_mm_slli_epi32(g6, 2), _mm_srli_epi32(g6, 4)), |
| 113 b8 = _mm_or_si128(_mm_slli_epi32(b5, 3), _mm_srli_epi32(b5, 2)); |
| 114 |
| 115 // Now put all the 8-bit components into SkPMColor order. |
| 116 return _mm_or_si128(_mm_slli_epi32(r8, SK_R32_SHIFT), // TODO: one of thes
e shifts is zero... |
| 117 _mm_or_si128(_mm_slli_epi32(g8, SK_G32_SHIFT), |
| 118 _mm_or_si128(_mm_slli_epi32(b8, SK_B32_SHIFT), |
| 119 _mm_set1_epi32(0xFF << SK_A32_SHIFT)))); |
| 120 } |
| 121 |
| 122 static inline __m128i narrow_to_565(__m128i w) { |
| 123 // Extract out top RGB 565 bits of each pixel, with no rounding. |
| 124 auto r5 = _mm_and_si128(_mm_set1_epi32(31), _mm_srli_epi32(w, SK_R32_SHIFT +
3)), |
| 125 g6 = _mm_and_si128(_mm_set1_epi32(63), _mm_srli_epi32(w, SK_G32_SHIFT +
2)), |
| 126 b5 = _mm_and_si128(_mm_set1_epi32(31), _mm_srli_epi32(w, SK_B32_SHIFT +
3)); |
| 127 |
| 128 // Now put the bits in place in the low 16-bits of each 32-bit lane. |
| 129 auto spread = _mm_or_si128(_mm_slli_epi32(r5, 11), |
| 130 _mm_or_si128(_mm_slli_epi32(g6, 5), |
| 131 b5)); |
| 132 |
| 133 // We want to pack the bottom 16-bits of spread down into the low half of th
e register, v. |
| 134 // spread == 0000 rgb3 0000 rgb2 0000 rgb1 0000 rgb0 |
| 135 // v == ____ ____ ____ ____ rgb3 rgb2 rgb1 rgb0 |
| 136 |
| 137 // Ideally now we'd use _mm_packus_epi32(spread, <anything>) to pack v. But
that's from SSE4. |
| 138 // With only SSE2, we need to use _mm_packs_epi32. That does signed saturat
ion, and |
| 139 // we need to preserve all 16 bits. So we pretend our data is signed by sig
n-extending first. |
| 140 // TODO: is it faster to just _mm_shuffle_epi8 this when we have SSSE3? |
| 141 auto signExtended = _mm_srai_epi32(_mm_slli_epi32(spread, 16), 16); |
| 142 auto v = _mm_packs_epi32(signExtended, signExtended); |
| 143 return v; |
| 144 } |
| 145 |
| 146 inline Sk4px Sk4px::Load4(const SkPMColor16 src[4]) { |
| 147 return Sk16b(widen_low_half_to_8888(_mm_loadl_epi64((const __m128i*)src))); |
| 148 } |
| 149 inline Sk4px Sk4px::Load2(const SkPMColor16 src[2]) { |
| 150 auto src2 = ((uint32_t)src[0] ) |
| 151 | ((uint32_t)src[1] << 16); |
| 152 return Sk16b(widen_low_half_to_8888(_mm_cvtsi32_si128(src2))); |
| 153 } |
| 154 inline Sk4px Sk4px::Load1(const SkPMColor16 src[1]) { |
| 155 return Sk16b(widen_low_half_to_8888(_mm_insert_epi16(_mm_setzero_si128(), sr
c[0], 0))); |
| 156 } |
| 157 |
| 158 inline void Sk4px::store4(SkPMColor16 dst[4]) const { |
| 159 _mm_storel_epi64((__m128i*)dst, narrow_to_565(this->fVec)); |
| 160 } |
| 161 inline void Sk4px::store2(SkPMColor16 dst[2]) const { |
| 162 uint32_t dst2 = _mm_cvtsi128_si32(narrow_to_565(this->fVec)); |
| 163 dst[0] = dst2; |
| 164 dst[1] = dst2 >> 16; |
| 165 } |
| 166 inline void Sk4px::store1(SkPMColor16 dst[1]) const { |
| 167 uint32_t dst2 = _mm_cvtsi128_si32(narrow_to_565(this->fVec)); |
| 168 dst[0] = dst2; |
| 169 } |
| 170 |
96 } // namespace | 171 } // namespace |
OLD | NEW |