1 /* 2 * Copyright 2014 The Android Open Source Project 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 SkColor_opts_SSE2_DEFINED 9 #define SkColor_opts_SSE2_DEFINED 10 11 #include <emmintrin.h> 12 13 // Because no _mm_mul_epi32() in SSE2, we emulate it here. 14 // Multiplies 4 32-bit integers from a by 4 32-bit intergers from b. 15 // The 4 multiplication results should be represented within 32-bit 16 // integers, otherwise they would be overflow. 17 static inline __m128i Multiply32_SSE2(const __m128i& a, const __m128i& b) { 18 // Calculate results of a0 * b0 and a2 * b2. 19 __m128i r1 = _mm_mul_epu32(a, b); 20 // Calculate results of a1 * b1 and a3 * b3. 21 __m128i r2 = _mm_mul_epu32(_mm_srli_si128(a, 4), _mm_srli_si128(b, 4)); 22 // Shuffle results to [63..0] and interleave the results. 23 __m128i r = _mm_unpacklo_epi32(_mm_shuffle_epi32(r1, _MM_SHUFFLE(0,0,2,0)), 24 _mm_shuffle_epi32(r2, _MM_SHUFFLE(0,0,2,0))); 25 return r; 26 } 27 28 static inline __m128i SkAlpha255To256_SSE2(const __m128i& alpha) { 29 return _mm_add_epi32(alpha, _mm_set1_epi32(1)); 30 } 31 32 // See #define SkAlphaMulAlpha(a, b) SkMulDiv255Round(a, b) in SkXfermode.cpp. 33 static inline __m128i SkAlphaMulAlpha_SSE2(const __m128i& a, 34 const __m128i& b) { 35 __m128i prod = _mm_mullo_epi16(a, b); 36 prod = _mm_add_epi32(prod, _mm_set1_epi32(128)); 37 prod = _mm_add_epi32(prod, _mm_srli_epi32(prod, 8)); 38 prod = _mm_srli_epi32(prod, 8); 39 40 return prod; 41 } 42 43 // Portable version SkAlphaMulQ is in SkColorPriv.h. 44 static inline __m128i SkAlphaMulQ_SSE2(const __m128i& c, const __m128i& scale) { 45 __m128i mask = _mm_set1_epi32(0xFF00FF); 46 __m128i s = _mm_or_si128(_mm_slli_epi32(scale, 16), scale); 47 48 // uint32_t rb = ((c & mask) * scale) >> 8 49 __m128i rb = _mm_and_si128(mask, c); 50 rb = _mm_mullo_epi16(rb, s); 51 rb = _mm_srli_epi16(rb, 8); 52 53 // uint32_t ag = ((c >> 8) & mask) * scale 54 __m128i ag = _mm_srli_epi16(c, 8); 55 ag = _mm_and_si128(ag, mask); 56 ag = _mm_mullo_epi16(ag, s); 57 58 // (rb & mask) | (ag & ~mask) 59 rb = _mm_and_si128(mask, rb); 60 ag = _mm_andnot_si128(mask, ag); 61 return _mm_or_si128(rb, ag); 62 } 63 64 static inline __m128i SkGetPackedA32_SSE2(const __m128i& src) { 65 __m128i a = _mm_slli_epi32(src, (24 - SK_A32_SHIFT)); 66 return _mm_srli_epi32(a, 24); 67 } 68 69 static inline __m128i SkGetPackedR32_SSE2(const __m128i& src) { 70 __m128i r = _mm_slli_epi32(src, (24 - SK_R32_SHIFT)); 71 return _mm_srli_epi32(r, 24); 72 } 73 74 static inline __m128i SkGetPackedG32_SSE2(const __m128i& src) { 75 __m128i g = _mm_slli_epi32(src, (24 - SK_G32_SHIFT)); 76 return _mm_srli_epi32(g, 24); 77 } 78 79 static inline __m128i SkGetPackedB32_SSE2(const __m128i& src) { 80 __m128i b = _mm_slli_epi32(src, (24 - SK_B32_SHIFT)); 81 return _mm_srli_epi32(b, 24); 82 } 83 84 static inline __m128i SkMul16ShiftRound_SSE2(const __m128i& a, 85 const __m128i& b, int shift) { 86 __m128i prod = _mm_mullo_epi16(a, b); 87 prod = _mm_add_epi16(prod, _mm_set1_epi16(1 << (shift - 1))); 88 prod = _mm_add_epi16(prod, _mm_srli_epi16(prod, shift)); 89 prod = _mm_srli_epi16(prod, shift); 90 91 return prod; 92 } 93 94 static inline __m128i SkPackRGB16_SSE2(const __m128i& r, 95 const __m128i& g, const __m128i& b) { 96 __m128i dr = _mm_slli_epi16(r, SK_R16_SHIFT); 97 __m128i dg = _mm_slli_epi16(g, SK_G16_SHIFT); 98 __m128i db = _mm_slli_epi16(b, SK_B16_SHIFT); 99 100 __m128i c = _mm_or_si128(dr, dg); 101 return _mm_or_si128(c, db); 102 } 103 104 static inline __m128i SkPackARGB32_SSE2(const __m128i& a, const __m128i& r, 105 const __m128i& g, const __m128i& b) { 106 __m128i da = _mm_slli_epi32(a, SK_A32_SHIFT); 107 __m128i dr = _mm_slli_epi32(r, SK_R32_SHIFT); 108 __m128i dg = _mm_slli_epi32(g, SK_G32_SHIFT); 109 __m128i db = _mm_slli_epi32(b, SK_B32_SHIFT); 110 111 __m128i c = _mm_or_si128(da, dr); 112 c = _mm_or_si128(c, dg); 113 return _mm_or_si128(c, db); 114 } 115 116 static inline __m128i SkPacked16ToR32_SSE2(const __m128i& src) { 117 __m128i r = _mm_srli_epi32(src, SK_R16_SHIFT); 118 r = _mm_and_si128(r, _mm_set1_epi32(SK_R16_MASK)); 119 r = _mm_or_si128(_mm_slli_epi32(r, (8 - SK_R16_BITS)), 120 _mm_srli_epi32(r, (2 * SK_R16_BITS - 8))); 121 122 return r; 123 } 124 125 static inline __m128i SkPacked16ToG32_SSE2(const __m128i& src) { 126 __m128i g = _mm_srli_epi32(src, SK_G16_SHIFT); 127 g = _mm_and_si128(g, _mm_set1_epi32(SK_G16_MASK)); 128 g = _mm_or_si128(_mm_slli_epi32(g, (8 - SK_G16_BITS)), 129 _mm_srli_epi32(g, (2 * SK_G16_BITS - 8))); 130 131 return g; 132 } 133 134 static inline __m128i SkPacked16ToB32_SSE2(const __m128i& src) { 135 __m128i b = _mm_srli_epi32(src, SK_B16_SHIFT); 136 b = _mm_and_si128(b, _mm_set1_epi32(SK_B16_MASK)); 137 b = _mm_or_si128(_mm_slli_epi32(b, (8 - SK_B16_BITS)), 138 _mm_srli_epi32(b, (2 * SK_B16_BITS - 8))); 139 140 return b; 141 } 142 143 static inline __m128i SkPixel16ToPixel32_SSE2(const __m128i& src) { 144 __m128i r = SkPacked16ToR32_SSE2(src); 145 __m128i g = SkPacked16ToG32_SSE2(src); 146 __m128i b = SkPacked16ToB32_SSE2(src); 147 148 return SkPackARGB32_SSE2(_mm_set1_epi32(0xFF), r, g, b); 149 } 150 151 static inline __m128i SkPixel32ToPixel16_ToU16_SSE2(const __m128i& src_pixel1, 152 const __m128i& src_pixel2) { 153 // Calculate result r. 154 __m128i r1 = _mm_srli_epi32(src_pixel1, 155 SK_R32_SHIFT + (8 - SK_R16_BITS)); 156 r1 = _mm_and_si128(r1, _mm_set1_epi32(SK_R16_MASK)); 157 __m128i r2 = _mm_srli_epi32(src_pixel2, 158 SK_R32_SHIFT + (8 - SK_R16_BITS)); 159 r2 = _mm_and_si128(r2, _mm_set1_epi32(SK_R16_MASK)); 160 __m128i r = _mm_packs_epi32(r1, r2); 161 162 // Calculate result g. 163 __m128i g1 = _mm_srli_epi32(src_pixel1, 164 SK_G32_SHIFT + (8 - SK_G16_BITS)); 165 g1 = _mm_and_si128(g1, _mm_set1_epi32(SK_G16_MASK)); 166 __m128i g2 = _mm_srli_epi32(src_pixel2, 167 SK_G32_SHIFT + (8 - SK_G16_BITS)); 168 g2 = _mm_and_si128(g2, _mm_set1_epi32(SK_G16_MASK)); 169 __m128i g = _mm_packs_epi32(g1, g2); 170 171 // Calculate result b. 172 __m128i b1 = _mm_srli_epi32(src_pixel1, 173 SK_B32_SHIFT + (8 - SK_B16_BITS)); 174 b1 = _mm_and_si128(b1, _mm_set1_epi32(SK_B16_MASK)); 175 __m128i b2 = _mm_srli_epi32(src_pixel2, 176 SK_B32_SHIFT + (8 - SK_B16_BITS)); 177 b2 = _mm_and_si128(b2, _mm_set1_epi32(SK_B16_MASK)); 178 __m128i b = _mm_packs_epi32(b1, b2); 179 180 // Store 8 16-bit colors in dst. 181 __m128i d_pixel = SkPackRGB16_SSE2(r, g, b); 182 183 return d_pixel; 184 } 185 186 #endif // SkColor_opts_SSE2_DEFINED 187