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