blob: 9a0a013916d57ad66b31b9f60d56a51936c1c091 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
senorblanco@chromium.orgdc7de742009-11-30 20:00:29 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2009 The Android Open Source Project
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
senorblanco@chromium.orgdc7de742009-11-30 20:00:29 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
senorblanco@chromium.orgdc7de742009-11-30 20:00:29 +000010#include <emmintrin.h>
11#include "SkBitmapProcState_opts_SSE2.h"
12#include "SkUtils.h"
13
14void S32_opaque_D32_filter_DX_SSE2(const SkBitmapProcState& s,
15 const uint32_t* xy,
16 int count, uint32_t* colors) {
17 SkASSERT(count > 0 && colors != NULL);
18 SkASSERT(s.fDoFilter);
senorblanco@chromium.orgaa4f0c62009-12-01 13:36:19 +000019 SkASSERT(s.fBitmap->config() == SkBitmap::kARGB_8888_Config);
20 SkASSERT(s.fAlphaScale == 256);
senorblanco@chromium.orgdc7de742009-11-30 20:00:29 +000021
22 const char* srcAddr = static_cast<const char*>(s.fBitmap->getPixels());
23 unsigned rb = s.fBitmap->rowBytes();
24 uint32_t XY = *xy++;
25 unsigned y0 = XY >> 14;
26 const uint32_t* row0 = reinterpret_cast<const uint32_t*>(srcAddr + (y0 >> 4) * rb);
27 const uint32_t* row1 = reinterpret_cast<const uint32_t*>(srcAddr + (XY & 0x3FFF) * rb);
28 unsigned subY = y0 & 0xF;
29
30 // ( 0, 0, 0, 0, 0, 0, 0, 16)
31 __m128i sixteen = _mm_cvtsi32_si128(16);
32
33 // ( 0, 0, 0, 0, 16, 16, 16, 16)
34 sixteen = _mm_shufflelo_epi16(sixteen, 0);
35
36 // ( 0, 0, 0, 0, 0, 0, 0, y)
37 __m128i allY = _mm_cvtsi32_si128(subY);
38
39 // ( 0, 0, 0, 0, y, y, y, y)
40 allY = _mm_shufflelo_epi16(allY, 0);
41
42 // ( 0, 0, 0, 0, 16-y, 16-y, 16-y, 16-y)
43 __m128i negY = _mm_sub_epi16(sixteen, allY);
44
45 // (16-y, 16-y, 16-y, 16-y, y, y, y, y)
46 allY = _mm_unpacklo_epi64(allY, negY);
47
48 // (16, 16, 16, 16, 16, 16, 16, 16 )
49 sixteen = _mm_shuffle_epi32(sixteen, 0);
50
51 // ( 0, 0, 0, 0, 0, 0, 0, 0)
52 __m128i zero = _mm_setzero_si128();
53 do {
54 uint32_t XX = *xy++; // x0:14 | 4 | x1:14
55 unsigned x0 = XX >> 18;
56 unsigned x1 = XX & 0x3FFF;
57
58 // (0, 0, 0, 0, 0, 0, 0, x)
59 __m128i allX = _mm_cvtsi32_si128((XX >> 14) & 0x0F);
60
61 // (0, 0, 0, 0, x, x, x, x)
62 allX = _mm_shufflelo_epi16(allX, 0);
63
64 // (x, x, x, x, x, x, x, x)
65 allX = _mm_shuffle_epi32(allX, 0);
66
67 // (16-x, 16-x, 16-x, 16-x, 16-x, 16-x, 16-x)
68 __m128i negX = _mm_sub_epi16(sixteen, allX);
69
70 // Load 4 samples (pixels).
71 __m128i a00 = _mm_cvtsi32_si128(row0[x0]);
72 __m128i a01 = _mm_cvtsi32_si128(row0[x1]);
73 __m128i a10 = _mm_cvtsi32_si128(row1[x0]);
74 __m128i a11 = _mm_cvtsi32_si128(row1[x1]);
75
76 // (0, 0, a00, a10)
77 __m128i a00a10 = _mm_unpacklo_epi32(a10, a00);
78
79 // Expand to 16 bits per component.
80 a00a10 = _mm_unpacklo_epi8(a00a10, zero);
81
82 // ((a00 * (16-y)), (a10 * y)).
83 a00a10 = _mm_mullo_epi16(a00a10, allY);
84
85 // (a00 * (16-y) * (16-x), a10 * y * (16-x)).
86 a00a10 = _mm_mullo_epi16(a00a10, negX);
87
88 // (0, 0, a01, a10)
89 __m128i a01a11 = _mm_unpacklo_epi32(a11, a01);
90
91 // Expand to 16 bits per component.
92 a01a11 = _mm_unpacklo_epi8(a01a11, zero);
93
94 // (a01 * (16-y)), (a11 * y)
95 a01a11 = _mm_mullo_epi16(a01a11, allY);
96
97 // (a01 * (16-y) * x), (a11 * y * x)
98 a01a11 = _mm_mullo_epi16(a01a11, allX);
99
100 // (a00*w00 + a01*w01, a10*w10 + a11*w11)
101 __m128i sum = _mm_add_epi16(a00a10, a01a11);
102
103 // (DC, a00*w00 + a01*w01)
104 __m128i shifted = _mm_shuffle_epi32(sum, 0xEE);
105
106 // (DC, a00*w00 + a01*w01 + a10*w10 + a11*w11)
107 sum = _mm_add_epi16(sum, shifted);
108
109 // Divide each 16 bit component by 256.
110 sum = _mm_srli_epi16(sum, 8);
111
112 // Pack lower 4 16 bit values of sum into lower 4 bytes.
113 sum = _mm_packus_epi16(sum, zero);
114
115 // Extract low int and store.
116 *colors++ = _mm_cvtsi128_si32(sum);
117 } while (--count > 0);
118}
senorblanco@chromium.orgf3f0bd72009-12-10 22:46:31 +0000119
120void S32_alpha_D32_filter_DX_SSE2(const SkBitmapProcState& s,
121 const uint32_t* xy,
122 int count, uint32_t* colors) {
123 SkASSERT(count > 0 && colors != NULL);
124 SkASSERT(s.fDoFilter);
125 SkASSERT(s.fBitmap->config() == SkBitmap::kARGB_8888_Config);
126 SkASSERT(s.fAlphaScale < 256);
127
128 const char* srcAddr = static_cast<const char*>(s.fBitmap->getPixels());
129 unsigned rb = s.fBitmap->rowBytes();
130 uint32_t XY = *xy++;
131 unsigned y0 = XY >> 14;
132 const uint32_t* row0 = reinterpret_cast<const uint32_t*>(srcAddr + (y0 >> 4) * rb);
133 const uint32_t* row1 = reinterpret_cast<const uint32_t*>(srcAddr + (XY & 0x3FFF) * rb);
134 unsigned subY = y0 & 0xF;
135
136 // ( 0, 0, 0, 0, 0, 0, 0, 16)
137 __m128i sixteen = _mm_cvtsi32_si128(16);
138
139 // ( 0, 0, 0, 0, 16, 16, 16, 16)
140 sixteen = _mm_shufflelo_epi16(sixteen, 0);
141
142 // ( 0, 0, 0, 0, 0, 0, 0, y)
143 __m128i allY = _mm_cvtsi32_si128(subY);
144
145 // ( 0, 0, 0, 0, y, y, y, y)
146 allY = _mm_shufflelo_epi16(allY, 0);
147
148 // ( 0, 0, 0, 0, 16-y, 16-y, 16-y, 16-y)
149 __m128i negY = _mm_sub_epi16(sixteen, allY);
150
151 // (16-y, 16-y, 16-y, 16-y, y, y, y, y)
152 allY = _mm_unpacklo_epi64(allY, negY);
153
154 // (16, 16, 16, 16, 16, 16, 16, 16 )
155 sixteen = _mm_shuffle_epi32(sixteen, 0);
156
157 // ( 0, 0, 0, 0, 0, 0, 0, 0)
158 __m128i zero = _mm_setzero_si128();
159
160 // ( alpha, alpha, alpha, alpha, alpha, alpha, alpha, alpha )
161 __m128i alpha = _mm_set1_epi16(s.fAlphaScale);
162
163 do {
164 uint32_t XX = *xy++; // x0:14 | 4 | x1:14
165 unsigned x0 = XX >> 18;
166 unsigned x1 = XX & 0x3FFF;
167
168 // (0, 0, 0, 0, 0, 0, 0, x)
169 __m128i allX = _mm_cvtsi32_si128((XX >> 14) & 0x0F);
170
171 // (0, 0, 0, 0, x, x, x, x)
172 allX = _mm_shufflelo_epi16(allX, 0);
173
174 // (x, x, x, x, x, x, x, x)
175 allX = _mm_shuffle_epi32(allX, 0);
176
177 // (16-x, 16-x, 16-x, 16-x, 16-x, 16-x, 16-x)
178 __m128i negX = _mm_sub_epi16(sixteen, allX);
179
180 // Load 4 samples (pixels).
181 __m128i a00 = _mm_cvtsi32_si128(row0[x0]);
182 __m128i a01 = _mm_cvtsi32_si128(row0[x1]);
183 __m128i a10 = _mm_cvtsi32_si128(row1[x0]);
184 __m128i a11 = _mm_cvtsi32_si128(row1[x1]);
185
186 // (0, 0, a00, a10)
187 __m128i a00a10 = _mm_unpacklo_epi32(a10, a00);
188
189 // Expand to 16 bits per component.
190 a00a10 = _mm_unpacklo_epi8(a00a10, zero);
191
192 // ((a00 * (16-y)), (a10 * y)).
193 a00a10 = _mm_mullo_epi16(a00a10, allY);
194
195 // (a00 * (16-y) * (16-x), a10 * y * (16-x)).
196 a00a10 = _mm_mullo_epi16(a00a10, negX);
197
198 // (0, 0, a01, a10)
199 __m128i a01a11 = _mm_unpacklo_epi32(a11, a01);
200
201 // Expand to 16 bits per component.
202 a01a11 = _mm_unpacklo_epi8(a01a11, zero);
203
204 // (a01 * (16-y)), (a11 * y)
205 a01a11 = _mm_mullo_epi16(a01a11, allY);
206
207 // (a01 * (16-y) * x), (a11 * y * x)
208 a01a11 = _mm_mullo_epi16(a01a11, allX);
209
210 // (a00*w00 + a01*w01, a10*w10 + a11*w11)
211 __m128i sum = _mm_add_epi16(a00a10, a01a11);
212
213 // (DC, a00*w00 + a01*w01)
214 __m128i shifted = _mm_shuffle_epi32(sum, 0xEE);
215
216 // (DC, a00*w00 + a01*w01 + a10*w10 + a11*w11)
217 sum = _mm_add_epi16(sum, shifted);
218
219 // Divide each 16 bit component by 256.
220 sum = _mm_srli_epi16(sum, 8);
221
222 // Multiply by alpha.
223 sum = _mm_mullo_epi16(sum, alpha);
224
225 // Divide each 16 bit component by 256.
226 sum = _mm_srli_epi16(sum, 8);
227
228 // Pack lower 4 16 bit values of sum into lower 4 bytes.
229 sum = _mm_packus_epi16(sum, zero);
230
231 // Extract low int and store.
232 *colors++ = _mm_cvtsi128_si32(sum);
233 } while (--count > 0);
234}