aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/opts/SkBitmapProcState_opts_SSE2.cpp
blob: 9a0a013916d57ad66b31b9f60d56a51936c1c091 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234

/*
 * Copyright 2009 The Android Open Source Project
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */


#include <emmintrin.h>
#include "SkBitmapProcState_opts_SSE2.h"
#include "SkUtils.h"

void S32_opaque_D32_filter_DX_SSE2(const SkBitmapProcState& s,
                                   const uint32_t* xy,
                                   int count, uint32_t* colors) {
    SkASSERT(count > 0 && colors != NULL);
    SkASSERT(s.fDoFilter);
    SkASSERT(s.fBitmap->config() == SkBitmap::kARGB_8888_Config);
    SkASSERT(s.fAlphaScale == 256);

    const char* srcAddr = static_cast<const char*>(s.fBitmap->getPixels());
    unsigned rb = s.fBitmap->rowBytes();
    uint32_t XY = *xy++;
    unsigned y0 = XY >> 14;
    const uint32_t* row0 = reinterpret_cast<const uint32_t*>(srcAddr + (y0 >> 4) * rb);
    const uint32_t* row1 = reinterpret_cast<const uint32_t*>(srcAddr + (XY & 0x3FFF) * rb);
    unsigned subY = y0 & 0xF;

    // ( 0,  0,  0,  0,  0,  0,  0, 16)
    __m128i sixteen = _mm_cvtsi32_si128(16);

    // ( 0,  0,  0,  0, 16, 16, 16, 16)
    sixteen = _mm_shufflelo_epi16(sixteen, 0);

    // ( 0,  0,  0,  0,  0,  0,  0,  y)
    __m128i allY = _mm_cvtsi32_si128(subY);

    // ( 0,  0,  0,  0,  y,  y,  y,  y)
    allY = _mm_shufflelo_epi16(allY, 0);

    // ( 0,  0,  0,  0, 16-y, 16-y, 16-y, 16-y)
    __m128i negY = _mm_sub_epi16(sixteen, allY);

    // (16-y, 16-y, 16-y, 16-y, y, y, y, y)
    allY = _mm_unpacklo_epi64(allY, negY);

    // (16, 16, 16, 16, 16, 16, 16, 16 )
    sixteen = _mm_shuffle_epi32(sixteen, 0);

    // ( 0,  0,  0,  0,  0,  0,  0,  0)
    __m128i zero = _mm_setzero_si128();
    do {
        uint32_t XX = *xy++;    // x0:14 | 4 | x1:14
        unsigned x0 = XX >> 18;
        unsigned x1 = XX & 0x3FFF;

        // (0, 0, 0, 0, 0, 0, 0, x)
        __m128i allX = _mm_cvtsi32_si128((XX >> 14) & 0x0F);
        
        // (0, 0, 0, 0, x, x, x, x)
        allX = _mm_shufflelo_epi16(allX, 0);

        // (x, x, x, x, x, x, x, x)
        allX = _mm_shuffle_epi32(allX, 0);

        // (16-x, 16-x, 16-x, 16-x, 16-x, 16-x, 16-x)
        __m128i negX = _mm_sub_epi16(sixteen, allX);

        // Load 4 samples (pixels).
        __m128i a00 = _mm_cvtsi32_si128(row0[x0]);
        __m128i a01 = _mm_cvtsi32_si128(row0[x1]);
        __m128i a10 = _mm_cvtsi32_si128(row1[x0]);
        __m128i a11 = _mm_cvtsi32_si128(row1[x1]);

        // (0, 0, a00, a10)
        __m128i a00a10 = _mm_unpacklo_epi32(a10, a00);

        // Expand to 16 bits per component.
        a00a10 = _mm_unpacklo_epi8(a00a10, zero);

        // ((a00 * (16-y)), (a10 * y)).
        a00a10 = _mm_mullo_epi16(a00a10, allY);

        // (a00 * (16-y) * (16-x), a10 * y * (16-x)).
        a00a10 = _mm_mullo_epi16(a00a10, negX);

        // (0, 0, a01, a10)
        __m128i a01a11 = _mm_unpacklo_epi32(a11, a01);

        // Expand to 16 bits per component.
        a01a11 = _mm_unpacklo_epi8(a01a11, zero);

        // (a01 * (16-y)), (a11 * y)
        a01a11 = _mm_mullo_epi16(a01a11, allY);

        // (a01 * (16-y) * x), (a11 * y * x)
        a01a11 = _mm_mullo_epi16(a01a11, allX);

        // (a00*w00 + a01*w01, a10*w10 + a11*w11)
        __m128i sum = _mm_add_epi16(a00a10, a01a11);

        // (DC, a00*w00 + a01*w01)
        __m128i shifted = _mm_shuffle_epi32(sum, 0xEE);

        // (DC, a00*w00 + a01*w01 + a10*w10 + a11*w11)
        sum = _mm_add_epi16(sum, shifted);

        // Divide each 16 bit component by 256.
        sum = _mm_srli_epi16(sum, 8);

        // Pack lower 4 16 bit values of sum into lower 4 bytes.
        sum = _mm_packus_epi16(sum, zero);

        // Extract low int and store.
        *colors++ = _mm_cvtsi128_si32(sum);
    } while (--count > 0);
}

void S32_alpha_D32_filter_DX_SSE2(const SkBitmapProcState& s,
                                  const uint32_t* xy,
                                  int count, uint32_t* colors) {
    SkASSERT(count > 0 && colors != NULL);
    SkASSERT(s.fDoFilter);
    SkASSERT(s.fBitmap->config() == SkBitmap::kARGB_8888_Config);
    SkASSERT(s.fAlphaScale < 256);

    const char* srcAddr = static_cast<const char*>(s.fBitmap->getPixels());
    unsigned rb = s.fBitmap->rowBytes();
    uint32_t XY = *xy++;
    unsigned y0 = XY >> 14;
    const uint32_t* row0 = reinterpret_cast<const uint32_t*>(srcAddr + (y0 >> 4) * rb);
    const uint32_t* row1 = reinterpret_cast<const uint32_t*>(srcAddr + (XY & 0x3FFF) * rb);
    unsigned subY = y0 & 0xF;

    // ( 0,  0,  0,  0,  0,  0,  0, 16)
    __m128i sixteen = _mm_cvtsi32_si128(16);

    // ( 0,  0,  0,  0, 16, 16, 16, 16)
    sixteen = _mm_shufflelo_epi16(sixteen, 0);

    // ( 0,  0,  0,  0,  0,  0,  0,  y)
    __m128i allY = _mm_cvtsi32_si128(subY);

    // ( 0,  0,  0,  0,  y,  y,  y,  y)
    allY = _mm_shufflelo_epi16(allY, 0);

    // ( 0,  0,  0,  0, 16-y, 16-y, 16-y, 16-y)
    __m128i negY = _mm_sub_epi16(sixteen, allY);

    // (16-y, 16-y, 16-y, 16-y, y, y, y, y)
    allY = _mm_unpacklo_epi64(allY, negY);

    // (16, 16, 16, 16, 16, 16, 16, 16 )
    sixteen = _mm_shuffle_epi32(sixteen, 0);

    // ( 0,  0,  0,  0,  0,  0,  0,  0)
    __m128i zero = _mm_setzero_si128();

    // ( alpha, alpha, alpha, alpha, alpha, alpha, alpha, alpha )
    __m128i alpha = _mm_set1_epi16(s.fAlphaScale);

    do {
        uint32_t XX = *xy++;    // x0:14 | 4 | x1:14
        unsigned x0 = XX >> 18;
        unsigned x1 = XX & 0x3FFF;

        // (0, 0, 0, 0, 0, 0, 0, x)
        __m128i allX = _mm_cvtsi32_si128((XX >> 14) & 0x0F);
        
        // (0, 0, 0, 0, x, x, x, x)
        allX = _mm_shufflelo_epi16(allX, 0);

        // (x, x, x, x, x, x, x, x)
        allX = _mm_shuffle_epi32(allX, 0);

        // (16-x, 16-x, 16-x, 16-x, 16-x, 16-x, 16-x)
        __m128i negX = _mm_sub_epi16(sixteen, allX);

        // Load 4 samples (pixels).
        __m128i a00 = _mm_cvtsi32_si128(row0[x0]);
        __m128i a01 = _mm_cvtsi32_si128(row0[x1]);
        __m128i a10 = _mm_cvtsi32_si128(row1[x0]);
        __m128i a11 = _mm_cvtsi32_si128(row1[x1]);

        // (0, 0, a00, a10)
        __m128i a00a10 = _mm_unpacklo_epi32(a10, a00);

        // Expand to 16 bits per component.
        a00a10 = _mm_unpacklo_epi8(a00a10, zero);

        // ((a00 * (16-y)), (a10 * y)).
        a00a10 = _mm_mullo_epi16(a00a10, allY);

        // (a00 * (16-y) * (16-x), a10 * y * (16-x)).
        a00a10 = _mm_mullo_epi16(a00a10, negX);

        // (0, 0, a01, a10)
        __m128i a01a11 = _mm_unpacklo_epi32(a11, a01);

        // Expand to 16 bits per component.
        a01a11 = _mm_unpacklo_epi8(a01a11, zero);

        // (a01 * (16-y)), (a11 * y)
        a01a11 = _mm_mullo_epi16(a01a11, allY);

        // (a01 * (16-y) * x), (a11 * y * x)
        a01a11 = _mm_mullo_epi16(a01a11, allX);

        // (a00*w00 + a01*w01, a10*w10 + a11*w11)
        __m128i sum = _mm_add_epi16(a00a10, a01a11);

        // (DC, a00*w00 + a01*w01)
        __m128i shifted = _mm_shuffle_epi32(sum, 0xEE);

        // (DC, a00*w00 + a01*w01 + a10*w10 + a11*w11)
        sum = _mm_add_epi16(sum, shifted);

        // Divide each 16 bit component by 256.
        sum = _mm_srli_epi16(sum, 8);

        // Multiply by alpha.
        sum = _mm_mullo_epi16(sum, alpha);

        // Divide each 16 bit component by 256.
        sum = _mm_srli_epi16(sum, 8);

        // Pack lower 4 16 bit values of sum into lower 4 bytes.
        sum = _mm_packus_epi16(sum, zero);

        // Extract low int and store.
        *colors++ = _mm_cvtsi128_si32(sum);
    } while (--count > 0);
}