Coverage Report

Created: 2026-06-14 06:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qtbase/src/gui/painting/qdrawhelper_sse2.cpp
Line
Count
Source
1
// Copyright (C) 2016 The Qt Company Ltd.
2
// Copyright (C) 2016 Intel Corporation.
3
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4
// Qt-Security score:significant reason:default
5
6
#include <private/qdrawhelper_x86_p.h>
7
8
#ifdef QT_COMPILER_SUPPORTS_SSE2
9
10
#include <private/qdrawingprimitive_sse2_p.h>
11
#include <private/qpaintengine_raster_p.h>
12
13
QT_BEGIN_NAMESPACE
14
15
#ifndef QDRAWHELPER_AVX
16
// in AVX mode, we'll use the SSSE3 code
17
void qt_blend_argb32_on_argb32_sse2(uchar *destPixels, int dbpl,
18
                                    const uchar *srcPixels, int sbpl,
19
                                    int w, int h,
20
                                    int const_alpha)
21
0
{
22
0
    const quint32 *src = (const quint32 *) srcPixels;
23
0
    quint32 *dst = (quint32 *) destPixels;
24
0
    if (const_alpha == 256) {
25
0
        const __m128i alphaMask = _mm_set1_epi32(0xff000000);
26
0
        const __m128i nullVector = _mm_set1_epi32(0);
27
0
        const __m128i half = _mm_set1_epi16(0x80);
28
0
        const __m128i one = _mm_set1_epi16(0xff);
29
0
        const __m128i colorMask = _mm_set1_epi32(0x00ff00ff);
30
0
        for (int y = 0; y < h; ++y) {
31
0
            BLEND_SOURCE_OVER_ARGB32_SSE2(dst, src, w, nullVector, half, one, colorMask, alphaMask);
32
0
            dst = (quint32 *)(((uchar *) dst) + dbpl);
33
0
            src = (const quint32 *)(((const uchar *) src) + sbpl);
34
0
        }
35
0
    } else if (const_alpha != 0) {
36
        // dest = (s + d * sia) * ca + d * cia
37
        //      = s * ca + d * (sia * ca + cia)
38
        //      = s * ca + d * (1 - sa*ca)
39
0
        const_alpha = (const_alpha * 255) >> 8;
40
0
        const __m128i nullVector = _mm_set1_epi32(0);
41
0
        const __m128i half = _mm_set1_epi16(0x80);
42
0
        const __m128i one = _mm_set1_epi16(0xff);
43
0
        const __m128i colorMask = _mm_set1_epi32(0x00ff00ff);
44
0
        const __m128i constAlphaVector = _mm_set1_epi16(const_alpha);
45
0
        for (int y = 0; y < h; ++y) {
46
0
            BLEND_SOURCE_OVER_ARGB32_WITH_CONST_ALPHA_SSE2(dst, src, w, nullVector, half, one, colorMask, constAlphaVector)
47
0
            dst = (quint32 *)(((uchar *) dst) + dbpl);
48
0
            src = (const quint32 *)(((const uchar *) src) + sbpl);
49
0
        }
50
0
    }
51
0
}
52
#endif
53
54
// qblendfunctions.cpp
55
void qt_blend_rgb32_on_rgb32(uchar *destPixels, int dbpl,
56
                             const uchar *srcPixels, int sbpl,
57
                             int w, int h,
58
                             int const_alpha);
59
60
void qt_blend_rgb32_on_rgb32_sse2(uchar *destPixels, int dbpl,
61
                                 const uchar *srcPixels, int sbpl,
62
                                 int w, int h,
63
                                 int const_alpha)
64
0
{
65
0
    const quint32 *src = (const quint32 *) srcPixels;
66
0
    quint32 *dst = (quint32 *) destPixels;
67
0
    if (const_alpha != 256) {
68
0
        if (const_alpha != 0) {
69
0
            const __m128i half = _mm_set1_epi16(0x80);
70
0
            const __m128i colorMask = _mm_set1_epi32(0x00ff00ff);
71
72
0
            const_alpha = (const_alpha * 255) >> 8;
73
0
            int one_minus_const_alpha = 255 - const_alpha;
74
0
            const __m128i constAlphaVector = _mm_set1_epi16(const_alpha);
75
0
            const __m128i oneMinusConstAlpha =  _mm_set1_epi16(one_minus_const_alpha);
76
0
            for (int y = 0; y < h; ++y) {
77
0
                int x = 0;
78
79
                // First, align dest to 16 bytes:
80
0
                ALIGNMENT_PROLOGUE_16BYTES(dst, x, w) {
81
0
                    dst[x] = INTERPOLATE_PIXEL_255(src[x], const_alpha, dst[x], one_minus_const_alpha);
82
0
                }
83
84
0
                for (; x < w-3; x += 4) {
85
0
                    __m128i srcVector = _mm_loadu_si128((const __m128i *)&src[x]);
86
0
                    const __m128i dstVector = _mm_load_si128((__m128i *)&dst[x]);
87
0
                    __m128i result;
88
0
                    INTERPOLATE_PIXEL_255_SSE2(result, srcVector, dstVector, constAlphaVector, oneMinusConstAlpha, colorMask, half);
89
0
                    _mm_store_si128((__m128i *)&dst[x], result);
90
0
                }
91
0
                SIMD_EPILOGUE(x, w, 3)
92
0
                    dst[x] = INTERPOLATE_PIXEL_255(src[x], const_alpha, dst[x], one_minus_const_alpha);
93
0
                dst = (quint32 *)(((uchar *) dst) + dbpl);
94
0
                src = (const quint32 *)(((const uchar *) src) + sbpl);
95
0
            }
96
0
        }
97
0
    } else {
98
0
        qt_blend_rgb32_on_rgb32(destPixels, dbpl, srcPixels, sbpl, w, h, const_alpha);
99
0
    }
100
0
}
101
102
void QT_FASTCALL comp_func_SourceOver_sse2(uint *destPixels, const uint *srcPixels, int length, uint const_alpha)
103
0
{
104
0
    Q_ASSERT(const_alpha < 256);
105
106
0
    const quint32 *src = (const quint32 *) srcPixels;
107
0
    quint32 *dst = (quint32 *) destPixels;
108
109
0
    const __m128i nullVector = _mm_set1_epi32(0);
110
0
    const __m128i half = _mm_set1_epi16(0x80);
111
0
    const __m128i one = _mm_set1_epi16(0xff);
112
0
    const __m128i colorMask = _mm_set1_epi32(0x00ff00ff);
113
0
    if (const_alpha == 255) {
114
0
        const __m128i alphaMask = _mm_set1_epi32(0xff000000);
115
0
        BLEND_SOURCE_OVER_ARGB32_SSE2(dst, src, length, nullVector, half, one, colorMask, alphaMask);
116
0
    } else {
117
0
        const __m128i constAlphaVector = _mm_set1_epi16(const_alpha);
118
0
        BLEND_SOURCE_OVER_ARGB32_WITH_CONST_ALPHA_SSE2(dst, src, length, nullVector, half, one, colorMask, constAlphaVector);
119
0
    }
120
0
}
121
122
void QT_FASTCALL comp_func_Plus_sse2(uint *dst, const uint *src, int length, uint const_alpha)
123
91.9k
{
124
91.9k
    int x = 0;
125
126
91.9k
    if (const_alpha == 255) {
127
        // 1) Prologue: align destination on 16 bytes
128
86.4k
        ALIGNMENT_PROLOGUE_16BYTES(dst, x, length)
129
0
            dst[x] = comp_func_Plus_one_pixel(dst[x], src[x]);
130
131
        // 2) composition with SSE2
132
1.43M
        for (; x < length - 3; x += 4) {
133
1.34M
            const __m128i srcVector = _mm_loadu_si128((const __m128i *)&src[x]);
134
1.34M
            const __m128i dstVector = _mm_load_si128((__m128i *)&dst[x]);
135
136
1.34M
            const __m128i result = _mm_adds_epu8(srcVector, dstVector);
137
1.34M
            _mm_store_si128((__m128i *)&dst[x], result);
138
1.34M
        }
139
140
        // 3) Epilogue:
141
86.4k
        SIMD_EPILOGUE(x, length, 3)
142
5.60k
            dst[x] = comp_func_Plus_one_pixel(dst[x], src[x]);
143
86.4k
    } else {
144
5.50k
        const int one_minus_const_alpha = 255 - const_alpha;
145
5.50k
        const __m128i constAlphaVector = _mm_set1_epi16(const_alpha);
146
5.50k
        const __m128i oneMinusConstAlpha =  _mm_set1_epi16(one_minus_const_alpha);
147
148
        // 1) Prologue: align destination on 16 bytes
149
5.50k
        ALIGNMENT_PROLOGUE_16BYTES(dst, x, length)
150
0
            dst[x] = comp_func_Plus_one_pixel_const_alpha(dst[x], src[x], const_alpha, one_minus_const_alpha);
151
152
5.50k
        const __m128i half = _mm_set1_epi16(0x80);
153
5.50k
        const __m128i colorMask = _mm_set1_epi32(0x00ff00ff);
154
        // 2) composition with SSE2
155
78.5k
        for (; x < length - 3; x += 4) {
156
73.0k
            const __m128i srcVector = _mm_loadu_si128((const __m128i *)&src[x]);
157
73.0k
            const __m128i dstVector = _mm_load_si128((__m128i *)&dst[x]);
158
159
73.0k
            __m128i result = _mm_adds_epu8(srcVector, dstVector);
160
73.0k
            INTERPOLATE_PIXEL_255_SSE2(result, result, dstVector, constAlphaVector, oneMinusConstAlpha, colorMask, half)
161
73.0k
            _mm_store_si128((__m128i *)&dst[x], result);
162
73.0k
        }
163
164
        // 3) Epilogue:
165
5.50k
        SIMD_EPILOGUE(x, length, 3)
166
3.01k
            dst[x] = comp_func_Plus_one_pixel_const_alpha(dst[x], src[x], const_alpha, one_minus_const_alpha);
167
5.50k
    }
168
91.9k
}
169
170
void QT_FASTCALL comp_func_Source_sse2(uint *dst, const uint *src, int length, uint const_alpha)
171
0
{
172
0
    if (const_alpha == 255) {
173
0
        ::memcpy(dst, src, length * sizeof(uint));
174
0
    } else {
175
0
        const int ialpha = 255 - const_alpha;
176
177
0
        int x = 0;
178
179
        // 1) prologue, align on 16 bytes
180
0
        ALIGNMENT_PROLOGUE_16BYTES(dst, x, length)
181
0
            dst[x] = INTERPOLATE_PIXEL_255(src[x], const_alpha, dst[x], ialpha);
182
183
        // 2) interpolate pixels with SSE2
184
0
        const __m128i half = _mm_set1_epi16(0x80);
185
0
        const __m128i colorMask = _mm_set1_epi32(0x00ff00ff);
186
0
        const __m128i constAlphaVector = _mm_set1_epi16(const_alpha);
187
0
        const __m128i oneMinusConstAlpha =  _mm_set1_epi16(ialpha);
188
0
        for (; x < length - 3; x += 4) {
189
0
            const __m128i srcVector = _mm_loadu_si128((const __m128i *)&src[x]);
190
0
            __m128i dstVector = _mm_load_si128((__m128i *)&dst[x]);
191
0
            INTERPOLATE_PIXEL_255_SSE2(dstVector, srcVector, dstVector, constAlphaVector, oneMinusConstAlpha, colorMask, half)
192
0
            _mm_store_si128((__m128i *)&dst[x], dstVector);
193
0
        }
194
195
        // 3) Epilogue
196
0
        SIMD_EPILOGUE(x, length, 3)
197
0
            dst[x] = INTERPOLATE_PIXEL_255(src[x], const_alpha, dst[x], ialpha);
198
0
    }
199
0
}
200
201
#ifndef __haswell__
202
Q_NEVER_INLINE static
203
void Q_DECL_VECTORCALL qt_memfillXX_aligned(void *dest, __m128i value128, quintptr bytecount)
204
0
{
205
0
    __m128i *dst128 = reinterpret_cast<__m128i *>(dest);
206
0
    __m128i *end128 = reinterpret_cast<__m128i *>(static_cast<uchar *>(dest) + bytecount);
207
208
0
    while (dst128 + 4 <= end128) {
209
0
        _mm_store_si128(dst128 + 0, value128);
210
0
        _mm_store_si128(dst128 + 1, value128);
211
0
        _mm_store_si128(dst128 + 2, value128);
212
0
        _mm_store_si128(dst128 + 3, value128);
213
0
        dst128 += 4;
214
0
    }
215
216
0
    bytecount %= 4 * sizeof(__m128i);
217
0
    switch (bytecount / sizeof(__m128i)) {
218
0
    case 3: _mm_store_si128(dst128++, value128); Q_FALLTHROUGH();
219
0
    case 2: _mm_store_si128(dst128++, value128); Q_FALLTHROUGH();
220
0
    case 1: _mm_store_si128(dst128++, value128);
221
0
    }
222
0
}
223
224
void qt_memfill64_sse2(quint64 *dest, quint64 value, qsizetype count)
225
0
{
226
0
    quintptr misaligned = quintptr(dest) % sizeof(__m128i);
227
0
    if (misaligned && count) {
228
#if defined(Q_PROCESSOR_X86_32)
229
        // Before SSE came out, the alignment of the stack used to be only 4
230
        // bytes and some OS/ABIs (notably, code generated by MSVC) still only
231
        // align to that. In any case, we cannot count on the alignment of
232
        // quint64 to be 8 -- see QtPrivate::AlignOf_WorkaroundForI386Abi in
233
        // qglobal.h.
234
        //
235
        // If the pointer is not aligned to at least 8 bytes, then we'll never
236
        // in turn hit a multiple of 16 for the qt_memfillXX_aligned call
237
        // below.
238
        if (Q_UNLIKELY(misaligned % sizeof(quint64)))
239
            return qt_memfill_template(dest, value, count);
240
#endif
241
242
0
        *dest++ = value;
243
0
        --count;
244
0
    }
245
246
0
    if (count % 2) {
247
0
        dest[count - 1] = value;
248
0
        --count;
249
0
    }
250
251
0
    qt_memfillXX_aligned(dest, _mm_set1_epi64x(value), count * sizeof(quint64));
252
0
}
253
254
void qt_memfill32_sse2(quint32 *dest, quint32 value, qsizetype count)
255
0
{
256
0
    if (count < 4) {
257
        // this simplifies the code below: the first switch can fall through
258
        // without checking the value of count
259
0
        switch (count) {
260
0
        case 3: *dest++ = value; Q_FALLTHROUGH();
261
0
        case 2: *dest++ = value; Q_FALLTHROUGH();
262
0
        case 1: *dest   = value;
263
0
        }
264
0
        return;
265
0
    }
266
267
0
    const int align = (quintptr)(dest) & 0xf;
268
0
    switch (align) {
269
0
    case 4:  *dest++ = value; --count; Q_FALLTHROUGH();
270
0
    case 8:  *dest++ = value; --count; Q_FALLTHROUGH();
271
0
    case 12: *dest++ = value; --count;
272
0
    }
273
274
0
    const int rest = count & 0x3;
275
0
    if (rest) {
276
0
        switch (rest) {
277
0
        case 3: dest[count - 3] = value; Q_FALLTHROUGH();
278
0
        case 2: dest[count - 2] = value; Q_FALLTHROUGH();
279
0
        case 1: dest[count - 1] = value;
280
0
        }
281
0
    }
282
283
0
    qt_memfillXX_aligned(dest, _mm_set1_epi32(value), count * sizeof(quint32));
284
0
}
285
#endif // !__haswell__
286
287
void QT_FASTCALL comp_func_solid_Source_sse2(uint *destPixels, int length, uint color, uint const_alpha)
288
0
{
289
0
    if (const_alpha == 255) {
290
0
        qt_memfill32(destPixels, color, length);
291
0
    } else {
292
0
        const quint32 ialpha = 255 - const_alpha;
293
0
        color = BYTE_MUL(color, const_alpha);
294
0
        int x = 0;
295
296
0
        quint32 *dst = (quint32 *) destPixels;
297
0
        const __m128i colorVector = _mm_set1_epi32(color);
298
0
        const __m128i colorMask = _mm_set1_epi32(0x00ff00ff);
299
0
        const __m128i half = _mm_set1_epi16(0x80);
300
0
        const __m128i iAlphaVector = _mm_set1_epi16(ialpha);
301
302
0
        ALIGNMENT_PROLOGUE_16BYTES(dst, x, length)
303
0
            destPixels[x] = color + BYTE_MUL(destPixels[x], ialpha);
304
305
0
        for (; x < length-3; x += 4) {
306
0
            __m128i dstVector = _mm_load_si128((__m128i *)&dst[x]);
307
0
            BYTE_MUL_SSE2(dstVector, dstVector, iAlphaVector, colorMask, half);
308
0
            dstVector = _mm_add_epi8(colorVector, dstVector);
309
0
            _mm_store_si128((__m128i *)&dst[x], dstVector);
310
0
        }
311
0
        SIMD_EPILOGUE(x, length, 3)
312
0
            destPixels[x] = color + BYTE_MUL(destPixels[x], ialpha);
313
0
    }
314
0
}
315
316
void QT_FASTCALL comp_func_solid_SourceOver_sse2(uint *destPixels, int length, uint color, uint const_alpha)
317
0
{
318
0
    if ((const_alpha & qAlpha(color)) == 255) {
319
0
        qt_memfill32(destPixels, color, length);
320
0
    } else {
321
0
        if (const_alpha != 255)
322
0
            color = BYTE_MUL(color, const_alpha);
323
324
0
        const quint32 minusAlphaOfColor = qAlpha(~color);
325
0
        int x = 0;
326
327
0
        quint32 *dst = (quint32 *) destPixels;
328
0
        const __m128i colorVector = _mm_set1_epi32(color);
329
0
        const __m128i colorMask = _mm_set1_epi32(0x00ff00ff);
330
0
        const __m128i half = _mm_set1_epi16(0x80);
331
0
        const __m128i minusAlphaOfColorVector = _mm_set1_epi16(minusAlphaOfColor);
332
333
0
        ALIGNMENT_PROLOGUE_16BYTES(dst, x, length)
334
0
            destPixels[x] = color + BYTE_MUL(destPixels[x], minusAlphaOfColor);
335
336
0
        for (; x < length-3; x += 4) {
337
0
            __m128i dstVector = _mm_load_si128((__m128i *)&dst[x]);
338
0
            BYTE_MUL_SSE2(dstVector, dstVector, minusAlphaOfColorVector, colorMask, half);
339
0
            dstVector = _mm_add_epi8(colorVector, dstVector);
340
0
            _mm_store_si128((__m128i *)&dst[x], dstVector);
341
0
        }
342
0
        SIMD_EPILOGUE(x, length, 3)
343
0
            destPixels[x] = color + BYTE_MUL(destPixels[x], minusAlphaOfColor);
344
0
    }
345
0
}
346
347
void qt_bitmapblit32_sse2_base(QRasterBuffer *rasterBuffer, int x, int y,
348
                          quint32 color,
349
                          const uchar *src, int width, int height, int stride)
350
0
{
351
0
    quint32 *dest = reinterpret_cast<quint32*>(rasterBuffer->scanLine(y)) + x;
352
0
    const int destStride = rasterBuffer->stride<quint32>();
353
354
0
    const __m128i c128 = _mm_set1_epi32(color);
355
0
    const __m128i maskmask1 = _mm_set_epi32(0x10101010, 0x20202020,
356
0
                                            0x40404040, 0x80808080);
357
0
    const __m128i maskadd1 = _mm_set_epi32(0x70707070, 0x60606060,
358
0
                                           0x40404040, 0x00000000);
359
360
0
    if (width > 4) {
361
0
        const __m128i maskmask2 = _mm_set_epi32(0x01010101, 0x02020202,
362
0
                                                0x04040404, 0x08080808);
363
0
        const __m128i maskadd2 = _mm_set_epi32(0x7f7f7f7f, 0x7e7e7e7e,
364
0
                                               0x7c7c7c7c, 0x78787878);
365
0
        while (--height >= 0) {
366
0
            for (int x = 0; x < width; x += 8) {
367
0
                const quint8 s = src[x >> 3];
368
0
                if (!s)
369
0
                    continue;
370
0
                __m128i mask1 = _mm_set1_epi8(s);
371
0
                __m128i mask2 = mask1;
372
373
0
                mask1 = _mm_and_si128(mask1, maskmask1);
374
0
                mask1 = _mm_add_epi8(mask1, maskadd1);
375
0
                _mm_maskmoveu_si128(c128, mask1, (char*)(dest + x));
376
0
                mask2 = _mm_and_si128(mask2, maskmask2);
377
0
                mask2 = _mm_add_epi8(mask2, maskadd2);
378
0
                _mm_maskmoveu_si128(c128, mask2, (char*)(dest + x + 4));
379
0
            }
380
0
            dest += destStride;
381
0
            src += stride;
382
0
        }
383
0
    } else {
384
0
        while (--height >= 0) {
385
0
            const quint8 s = *src;
386
0
            if (s) {
387
0
                __m128i mask1 = _mm_set1_epi8(s);
388
0
                mask1 = _mm_and_si128(mask1, maskmask1);
389
0
                mask1 = _mm_add_epi8(mask1, maskadd1);
390
0
                _mm_maskmoveu_si128(c128, mask1, (char*)(dest));
391
0
            }
392
0
            dest += destStride;
393
0
            src += stride;
394
0
        }
395
0
    }
396
0
}
397
398
void qt_bitmapblit32_sse2(QRasterBuffer *rasterBuffer, int x, int y,
399
                          const QRgba64 &color,
400
                          const uchar *src, int width, int height, int stride)
401
0
{
402
0
    qt_bitmapblit32_sse2_base(rasterBuffer, x, y, color.toArgb32(), src, width, height, stride);
403
0
}
404
405
void qt_bitmapblit8888_sse2(QRasterBuffer *rasterBuffer, int x, int y,
406
                            const QRgba64 &color,
407
                            const uchar *src, int width, int height, int stride)
408
0
{
409
0
    qt_bitmapblit32_sse2_base(rasterBuffer, x, y, ARGB2RGBA(color.toArgb32()), src, width, height, stride);
410
0
}
411
412
void qt_bitmapblit16_sse2(QRasterBuffer *rasterBuffer, int x, int y,
413
                          const QRgba64 &color,
414
                          const uchar *src, int width, int height, int stride)
415
0
{
416
0
    const quint16 c = qConvertRgb32To16(color.toArgb32());
417
0
    quint16 *dest = reinterpret_cast<quint16*>(rasterBuffer->scanLine(y)) + x;
418
0
    const int destStride = rasterBuffer->stride<quint32>();
419
420
0
    const __m128i c128 = _mm_set1_epi16(c);
421
0
QT_WARNING_DISABLE_MSVC(4309) // truncation of constant value
422
0
    const __m128i maskmask = _mm_set_epi16(0x0101, 0x0202, 0x0404, 0x0808,
423
0
                                           0x1010, 0x2020, 0x4040, 0x8080);
424
0
    const __m128i maskadd = _mm_set_epi16(0x7f7f, 0x7e7e, 0x7c7c, 0x7878,
425
0
                                          0x7070, 0x6060, 0x4040, 0x0000);
426
427
0
    while (--height >= 0) {
428
0
        for (int x = 0; x < width; x += 8) {
429
0
            const quint8 s = src[x >> 3];
430
0
            if (!s)
431
0
                continue;
432
0
            __m128i mask = _mm_set1_epi8(s);
433
0
            mask = _mm_and_si128(mask, maskmask);
434
0
            mask = _mm_add_epi8(mask, maskadd);
435
0
            _mm_maskmoveu_si128(c128, mask, (char*)(dest + x));
436
0
        }
437
0
        dest += destStride;
438
0
        src += stride;
439
0
    }
440
0
}
441
442
class QSimdSse2
443
{
444
public:
445
    typedef __m128i Int32x4;
446
    typedef __m128 Float32x4;
447
448
    union Vect_buffer_i { Int32x4 v; int i[4]; };
449
    union Vect_buffer_f { Float32x4 v; float f[4]; };
450
451
0
    static inline Float32x4 Q_DECL_VECTORCALL v_dup(float x) { return _mm_set1_ps(x); }
452
0
    static inline Float32x4 Q_DECL_VECTORCALL v_dup(double x) { return _mm_set1_ps(x); }
453
0
    static inline Int32x4 Q_DECL_VECTORCALL v_dup(int x) { return _mm_set1_epi32(x); }
454
0
    static inline Int32x4 Q_DECL_VECTORCALL v_dup(uint x) { return _mm_set1_epi32(x); }
455
456
0
    static inline Float32x4 Q_DECL_VECTORCALL v_add(Float32x4 a, Float32x4 b) { return _mm_add_ps(a, b); }
457
0
    static inline Int32x4 Q_DECL_VECTORCALL v_add(Int32x4 a, Int32x4 b) { return _mm_add_epi32(a, b); }
458
459
0
    static inline Float32x4 Q_DECL_VECTORCALL v_max(Float32x4 a, Float32x4 b) { return _mm_max_ps(a, b); }
460
0
    static inline Float32x4 Q_DECL_VECTORCALL v_min(Float32x4 a, Float32x4 b) { return _mm_min_ps(a, b); }
461
0
    static inline Int32x4 Q_DECL_VECTORCALL v_min_16(Int32x4 a, Int32x4 b) { return _mm_min_epi16(a, b); }
462
463
0
    static inline Int32x4 Q_DECL_VECTORCALL v_and(Int32x4 a, Int32x4 b) { return _mm_and_si128(a, b); }
464
465
0
    static inline Float32x4 Q_DECL_VECTORCALL v_sub(Float32x4 a, Float32x4 b) { return _mm_sub_ps(a, b); }
466
0
    static inline Int32x4 Q_DECL_VECTORCALL v_sub(Int32x4 a, Int32x4 b) { return _mm_sub_epi32(a, b); }
467
468
0
    static inline Float32x4 Q_DECL_VECTORCALL v_mul(Float32x4 a, Float32x4 b) { return _mm_mul_ps(a, b); }
469
470
0
    static inline Float32x4 Q_DECL_VECTORCALL v_sqrt(Float32x4 x) { return _mm_sqrt_ps(x); }
471
472
0
    static inline Int32x4 Q_DECL_VECTORCALL v_toInt(Float32x4 x) { return _mm_cvttps_epi32(x); }
473
474
0
    static inline Int32x4 Q_DECL_VECTORCALL v_greaterOrEqual(Float32x4 a, Float32x4 b) { return _mm_castps_si128(_mm_cmpgt_ps(a, b)); }
475
};
476
477
const uint * QT_FASTCALL qt_fetch_radial_gradient_sse2(uint *buffer, const Operator *op, const QSpanData *data,
478
                                                       int y, int x, int length)
479
0
{
480
0
    return qt_fetch_radial_gradient_template<QRadialFetchSimd<QSimdSse2>,uint>(buffer, op, data, y, x, length);
481
0
}
482
483
void qt_scale_image_argb32_on_argb32_sse2(uchar *destPixels, int dbpl,
484
                                          const uchar *srcPixels, int sbpl, int srch,
485
                                          const QRectF &targetRect,
486
                                          const QRectF &sourceRect,
487
                                          const QRect &clip,
488
                                          int const_alpha)
489
0
{
490
0
    if (const_alpha != 256) {
491
        // from qblendfunctions.cpp
492
0
        extern void qt_scale_image_argb32_on_argb32(uchar *destPixels, int dbpl,
493
0
                                               const uchar *srcPixels, int sbpl, int srch,
494
0
                                               const QRectF &targetRect,
495
0
                                               const QRectF &sourceRect,
496
0
                                               const QRect &clip,
497
0
                                               int const_alpha);
498
0
        return qt_scale_image_argb32_on_argb32(destPixels, dbpl, srcPixels, sbpl, srch, targetRect, sourceRect, clip, const_alpha);
499
0
    }
500
501
0
    qreal sx = sourceRect.width() / (qreal)targetRect.width();
502
0
    qreal sy = sourceRect.height() / (qreal)targetRect.height();
503
504
0
    const int ix = 0x00010000 * sx;
505
0
    const int iy = 0x00010000 * sy;
506
507
0
    QRect tr = targetRect.normalized().toRect();
508
0
    tr = tr.intersected(clip);
509
0
    if (tr.isEmpty())
510
0
        return;
511
0
    const int tx1 = tr.left();
512
0
    const int ty1 = tr.top();
513
0
    int h = tr.height();
514
0
    int w = tr.width();
515
516
0
    quint32 basex;
517
0
    quint32 srcy;
518
519
0
    if (sx < 0) {
520
0
        int dstx = qFloor((tx1 + qreal(0.5) - targetRect.right()) * sx * 65536) + 1;
521
0
        basex = quint32(sourceRect.right() * 65536) + dstx;
522
0
    } else {
523
0
        int dstx = qCeil((tx1 + qreal(0.5) - targetRect.left()) * sx * 65536) - 1;
524
0
        basex = quint32(sourceRect.left() * 65536) + dstx;
525
0
    }
526
0
    if (sy < 0) {
527
0
        int dsty = qFloor((ty1 + qreal(0.5) - targetRect.bottom()) * sy * 65536) + 1;
528
0
        srcy = quint32(sourceRect.bottom() * 65536) + dsty;
529
0
    } else {
530
0
        int dsty = qCeil((ty1 + qreal(0.5) - targetRect.top()) * sy * 65536) - 1;
531
0
        srcy = quint32(sourceRect.top() * 65536) + dsty;
532
0
    }
533
534
0
    quint32 *dst = ((quint32 *) (destPixels + ty1 * dbpl)) + tx1;
535
536
0
    const __m128i nullVector = _mm_setzero_si128();
537
0
    const __m128i half = _mm_set1_epi16(0x80);
538
0
    const __m128i one = _mm_set1_epi16(0xff);
539
0
    const __m128i colorMask = _mm_set1_epi32(0x00ff00ff);
540
0
    const __m128i alphaMask = _mm_set1_epi32(0xff000000);
541
0
    const __m128i ixVector = _mm_set1_epi32(4*ix);
542
543
    // this bounds check here is required as floating point rounding above might in some cases lead to
544
    // w/h values that are one pixel too large, falling outside of the valid image area.
545
0
    const int ystart = srcy >> 16;
546
0
    if (ystart >= srch && iy < 0) {
547
0
        srcy += iy;
548
0
        --h;
549
0
    }
550
0
    const int xstart = basex >> 16;
551
0
    if (xstart >=  (int)(sbpl/sizeof(quint32)) && ix < 0) {
552
0
        basex += ix;
553
0
        --w;
554
0
    }
555
0
    int yend = (srcy + iy * (h - 1)) >> 16;
556
0
    if (yend < 0 || yend >= srch)
557
0
        --h;
558
0
    int xend = (basex + ix * (w - 1)) >> 16;
559
0
    if (xend < 0 || xend >= (int)(sbpl/sizeof(quint32)))
560
0
        --w;
561
562
0
    while (--h >= 0) {
563
0
        const uint *src = (const quint32 *) (srcPixels + (srcy >> 16) * sbpl);
564
0
        int srcx = basex;
565
0
        int x = 0;
566
567
0
        ALIGNMENT_PROLOGUE_16BYTES(dst, x, w) {
568
0
            uint s = src[srcx >> 16];
569
0
            dst[x] = s + BYTE_MUL(dst[x], qAlpha(~s));
570
0
            srcx += ix;
571
0
        }
572
573
0
        __m128i srcxVector = _mm_set_epi32(srcx, srcx + ix, srcx + ix + ix, srcx + ix + ix + ix);
574
575
0
        for (; x < (w - 3); x += 4) {
576
0
            const int idx0 = _mm_extract_epi16(srcxVector, 1);
577
0
            const int idx1 = _mm_extract_epi16(srcxVector, 3);
578
0
            const int idx2 = _mm_extract_epi16(srcxVector, 5);
579
0
            const int idx3 = _mm_extract_epi16(srcxVector, 7);
580
0
            srcxVector = _mm_add_epi32(srcxVector, ixVector);
581
582
0
            const __m128i srcVector = _mm_set_epi32(src[idx0], src[idx1], src[idx2], src[idx3]);
583
0
            BLEND_SOURCE_OVER_ARGB32_SSE2_helper(dst, srcVector, nullVector, half, one, colorMask, alphaMask);
584
0
        }
585
586
0
        SIMD_EPILOGUE(x, w, 3) {
587
0
            uint s = src[(basex + x*ix) >> 16];
588
0
            dst[x] = s + BYTE_MUL(dst[x], qAlpha(~s));
589
0
        }
590
0
        dst = (quint32 *)(((uchar *) dst) + dbpl);
591
0
        srcy += iy;
592
0
    }
593
0
}
594
595
596
QT_END_NAMESPACE
597
598
#endif // QT_COMPILER_SUPPORTS_SSE2