Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qdrawhelper_lsx.cpp
Go to the documentation of this file.
1// Copyright (C) 2024 Loongson Technology Corporation Limited.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#include <private/qdrawhelper_loongarch64_p.h>
6
7#ifdef QT_COMPILER_SUPPORTS_LSX
8
9#include <private/qblendfunctions_p.h>
10#include <private/qdrawingprimitive_lsx_p.h>
11#include <private/qpaintengine_raster_p.h>
12
13QT_BEGIN_NAMESPACE
14
15void qt_blend_argb32_on_argb32_lsx(uchar *destPixels, int dbpl,
16 const uchar *srcPixels, int sbpl,
17 int w, int h,
18 int const_alpha)
19{
20 const quint32 *src = (const quint32 *) srcPixels;
21 quint32 *dst = (quint32 *) destPixels;
22 if (const_alpha == 256) {
23 for (int y = 0; y < h; ++y) {
24 BLEND_SOURCE_OVER_ARGB32_LSX(dst, src, w);
25 dst = (quint32 *)(((uchar *) dst) + dbpl);
26 src = (const quint32 *)(((const uchar *) src) + sbpl);
27 }
28 } else if (const_alpha != 0) {
29 // dest = (s + d * sia) * ca + d * cia
30 // = s * ca + d * (sia * ca + cia)
31 // = s * ca + d * (1 - sa*ca)
32 const_alpha = (const_alpha * 255) >> 8;
33
34 for (int y = 0; y < h; ++y) {
35 BLEND_SOURCE_OVER_ARGB32_WITH_CONST_ALPHA_LSX(dst, src, w, const_alpha);
36 dst = (quint32 *)(((uchar *) dst) + dbpl);
37 src = (const quint32 *)(((const uchar *) src) + sbpl);
38 }
39 }
40}
41
42// qblendfunctions.cpp
43void qt_blend_rgb32_on_rgb32(uchar *destPixels, int dbpl,
44 const uchar *srcPixels, int sbpl,
45 int w, int h,
46 int const_alpha);
47
48void qt_blend_rgb32_on_rgb32_lsx(uchar *destPixels, int dbpl,
49 const uchar *srcPixels, int sbpl,
50 int w, int h,
51 int const_alpha)
52{
53 const quint32 *src = (const quint32 *) srcPixels;
54 quint32 *dst = (quint32 *) destPixels;
55 if (const_alpha != 256) {
56 if (const_alpha != 0) {
57 const __m128i half = __lsx_vreplgr2vr_h(0x80);
58 const __m128i colorMask = __lsx_vreplgr2vr_w(0x00ff00ff);
59
60 const_alpha = (const_alpha * 255) >> 8;
61 int one_minus_const_alpha = 255 - const_alpha;
62 const __m128i constAlphaVector = __lsx_vreplgr2vr_h(const_alpha);
63 const __m128i oneMinusConstAlpha = __lsx_vreplgr2vr_h(one_minus_const_alpha);
64 for (int y = 0; y < h; ++y) {
65 int x = 0;
66
67 // First, align dest to 16 bytes:
68 ALIGNMENT_PROLOGUE_16BYTES(dst, x, w) {
69 dst[x] = INTERPOLATE_PIXEL_255(src[x], const_alpha,
70 dst[x], one_minus_const_alpha);
71 }
72
73 for (; x < w-3; x += 4) {
74 __m128i srcVector = __lsx_vld(&src[x], 0);
75 __m128i dstVector = __lsx_vld(&dst[x], 0);
76 INTERPOLATE_PIXEL_255_LSX(srcVector, dstVector, constAlphaVector,
77 oneMinusConstAlpha, colorMask, half);
78 __lsx_vst(dstVector, &dst[x], 0);
79 }
80 SIMD_EPILOGUE(x, w, 3)
81 dst[x] = INTERPOLATE_PIXEL_255(src[x], const_alpha,
82 dst[x], one_minus_const_alpha);
83 dst = (quint32 *)(((uchar *) dst) + dbpl);
84 src = (const quint32 *)(((const uchar *) src) + sbpl);
85 }
86 }
87 } else {
88 qt_blend_rgb32_on_rgb32(destPixels, dbpl, srcPixels, sbpl, w, h, const_alpha);
89 }
90}
91
92void QT_FASTCALL comp_func_SourceOver_lsx(uint *destPixels, const uint *srcPixels,
93 int length, uint const_alpha)
94{
95 Q_ASSERT(const_alpha < 256);
96
97 const quint32 *src = (const quint32 *) srcPixels;
98 quint32 *dst = (quint32 *) destPixels;
99
100 if (const_alpha == 255) {
101 BLEND_SOURCE_OVER_ARGB32_LSX(dst, src, length);
102 } else {
103 BLEND_SOURCE_OVER_ARGB32_WITH_CONST_ALPHA_LSX(dst, src, length, const_alpha);
104 }
105}
106
107void QT_FASTCALL comp_func_Plus_lsx(uint *dst, const uint *src, int length, uint const_alpha)
108{
109 int x = 0;
110
111 if (const_alpha == 255) {
112 // 1) Prologue: align destination on 16 bytes
113 ALIGNMENT_PROLOGUE_16BYTES(dst, x, length)
114 dst[x] = comp_func_Plus_one_pixel(dst[x], src[x]);
115
116 // 2) composition with LSX
117 for (; x < length - 3; x += 4) {
118 const __m128i srcVector = __lsx_vld(&src[x], 0);
119 const __m128i dstVector = __lsx_vld(&dst[x], 0);
120
121 const __m128i result = __lsx_vsadd_bu(srcVector, dstVector);
122 __lsx_vst(result, &dst[x], 0);
123 }
124
125 // 3) Epilogue:
126 SIMD_EPILOGUE(x, length, 3)
127 dst[x] = comp_func_Plus_one_pixel(dst[x], src[x]);
128 } else {
129 const int one_minus_const_alpha = 255 - const_alpha;
130 const __m128i constAlphaVector = __lsx_vreplgr2vr_h(const_alpha);
131 const __m128i oneMinusConstAlpha = __lsx_vreplgr2vr_h(one_minus_const_alpha);
132
133 // 1) Prologue: align destination on 16 bytes
134 ALIGNMENT_PROLOGUE_16BYTES(dst, x, length)
135 dst[x] = comp_func_Plus_one_pixel_const_alpha(dst[x], src[x],
136 const_alpha,
137 one_minus_const_alpha);
138
139 const __m128i half = __lsx_vreplgr2vr_h(0x80);
140 const __m128i colorMask = __lsx_vreplgr2vr_w(0x00ff00ff);
141 // 2) composition with LSX
142 for (; x < length - 3; x += 4) {
143 const __m128i srcVector = __lsx_vld(&src[x], 0);
144 __m128i dstVector = __lsx_vld(&dst[x], 0);
145 __m128i result = __lsx_vsadd_bu(srcVector, dstVector);
146 INTERPOLATE_PIXEL_255_LSX(result, dstVector, constAlphaVector,
147 oneMinusConstAlpha, colorMask, half);
148 __lsx_vst(dstVector, &dst[x], 0);
149 }
150
151 // 3) Epilogue:
152 SIMD_EPILOGUE(x, length, 3)
153 dst[x] = comp_func_Plus_one_pixel_const_alpha(dst[x], src[x],
154 const_alpha, one_minus_const_alpha);
155 }
156}
157
158void QT_FASTCALL comp_func_Source_lsx(uint *dst, const uint *src, int length, uint const_alpha)
159{
160 if (const_alpha == 255) {
161 ::memcpy(dst, src, length * sizeof(uint));
162 } else {
163 const int ialpha = 255 - const_alpha;
164
165 int x = 0;
166
167 // 1) prologue, align on 16 bytes
168 ALIGNMENT_PROLOGUE_16BYTES(dst, x, length)
169 dst[x] = INTERPOLATE_PIXEL_255(src[x], const_alpha, dst[x], ialpha);
170
171 // 2) interpolate pixels with LSX
172 const __m128i half = __lsx_vreplgr2vr_h(0x80);
173 const __m128i colorMask = __lsx_vreplgr2vr_w(0x00ff00ff);
174
175 const __m128i constAlphaVector = __lsx_vreplgr2vr_h(const_alpha);
176 const __m128i oneMinusConstAlpha = __lsx_vreplgr2vr_h(ialpha);
177 for (; x < length - 3; x += 4) {
178 const __m128i srcVector = __lsx_vld(&src[x], 0);
179 __m128i dstVector = __lsx_vld(&dst[x], 0);
180 INTERPOLATE_PIXEL_255_LSX(srcVector, dstVector, constAlphaVector,
181 oneMinusConstAlpha, colorMask, half);
182 __lsx_vst(dstVector, &dst[x], 0);
183 }
184
185 // 3) Epilogue
186 SIMD_EPILOGUE(x, length, 3)
187 dst[x] = INTERPOLATE_PIXEL_255(src[x], const_alpha, dst[x], ialpha);
188 }
189}
190
191static Q_NEVER_INLINE
192void Q_DECL_VECTORCALL qt_memfillXX_aligned(void *dest, __m128i value128, quintptr bytecount)
193{
194 __m128i *dst128 = reinterpret_cast<__m128i *>(dest);
195 __m128i *end128 = reinterpret_cast<__m128i *>(static_cast<uchar *>(dest) + bytecount);
196
197 while (dst128 + 4 <= end128) {
198 __lsx_vst(value128, dst128 + 0, 0);
199 __lsx_vst(value128, dst128 + 1, 0);
200 __lsx_vst(value128, dst128 + 2, 0);
201 __lsx_vst(value128, dst128 + 3, 0);
202 dst128 += 4;
203 }
204
205 bytecount %= 4 * sizeof(__m128i);
206 switch (bytecount / sizeof(__m128i)) {
207 case 3: __lsx_vst(value128, dst128++, 0); Q_FALLTHROUGH();
208 case 2: __lsx_vst(value128, dst128++, 0); Q_FALLTHROUGH();
209 case 1: __lsx_vst(value128, dst128++, 0);
210 }
211}
212
213void qt_memfill64_lsx(quint64 *dest, quint64 value, qsizetype count)
214{
215 quintptr misaligned = quintptr(dest) % sizeof(__m128i);
216 if (misaligned && count) {
217 *dest++ = value;
218 --count;
219 }
220
221 if (count % 2) {
222 dest[count - 1] = value;
223 --count;
224 }
225
226 qt_memfillXX_aligned(dest, __lsx_vreplgr2vr_d(value), count * sizeof(quint64));
227}
228
229void qt_memfill32_lsx(quint32 *dest, quint32 value, qsizetype count)
230{
231 if (count < 4) {
232 // this simplifies the code below: the first switch can fall through
233 // without checking the value of count
234 switch (count) {
235 case 3: *dest++ = value; Q_FALLTHROUGH();
236 case 2: *dest++ = value; Q_FALLTHROUGH();
237 case 1: *dest = value;
238 }
239 return;
240 }
241
242 const int align = (quintptr)(dest) & 0xf;
243 switch (align) {
244 case 4: *dest++ = value; --count; Q_FALLTHROUGH();
245 case 8: *dest++ = value; --count; Q_FALLTHROUGH();
246 case 12: *dest++ = value; --count;
247 }
248
249 const int rest = count & 0x3;
250 if (rest) {
251 switch (rest) {
252 case 3: dest[count - 3] = value; Q_FALLTHROUGH();
253 case 2: dest[count - 2] = value; Q_FALLTHROUGH();
254 case 1: dest[count - 1] = value;
255 }
256 }
257
258 qt_memfillXX_aligned(dest, __lsx_vreplgr2vr_w(value), count * sizeof(quint32));
259}
260
261void QT_FASTCALL comp_func_solid_Source_lsx(uint *destPixels, int length,
262 uint color, uint const_alpha)
263{
264 if (const_alpha == 255) {
265 qt_memfill32(destPixels, color, length);
266 } else {
267 const quint32 ialpha = 255 - const_alpha;
268 color = BYTE_MUL(color, const_alpha);
269 int x = 0;
270
271 quint32 *dst = (quint32 *) destPixels;
272 const __m128i colorVector = __lsx_vreplgr2vr_w(color);
273 const __m128i colorMask = __lsx_vreplgr2vr_w(0x00ff00ff);
274 const __m128i half = __lsx_vreplgr2vr_h(0x80);
275 const __m128i iAlphaVector = __lsx_vreplgr2vr_h(ialpha);
276
277 ALIGNMENT_PROLOGUE_16BYTES(dst, x, length)
278 destPixels[x] = color + BYTE_MUL(destPixels[x], ialpha);
279
280 for (; x < length-3; x += 4) {
281 __m128i dstVector = __lsx_vld(&dst[x], 0);
282 BYTE_MUL_LSX(dstVector, iAlphaVector, colorMask, half);
283 dstVector = __lsx_vadd_b(colorVector, dstVector);
284 __lsx_vst(dstVector, &dst[x], 0);
285 }
286 SIMD_EPILOGUE(x, length, 3)
287 destPixels[x] = color + BYTE_MUL(destPixels[x], ialpha);
288 }
289}
290
291void QT_FASTCALL comp_func_solid_SourceOver_lsx(uint *destPixels, int length,
292 uint color, uint const_alpha)
293{
294 if ((const_alpha & qAlpha(color)) == 255) {
295 qt_memfill32(destPixels, color, length);
296 } else {
297 if (const_alpha != 255)
298 color = BYTE_MUL(color, const_alpha);
299
300 const quint32 minusAlphaOfColor = qAlpha(~color);
301 int x = 0;
302
303 quint32 *dst = (quint32 *) destPixels;
304 const __m128i colorVector = __lsx_vreplgr2vr_w(color);
305 const __m128i colorMask = __lsx_vreplgr2vr_w(0x00ff00ff);
306 const __m128i half = __lsx_vreplgr2vr_h(0x80);
307 const __m128i minusAlphaOfColorVector = __lsx_vreplgr2vr_h(minusAlphaOfColor);
308
309 ALIGNMENT_PROLOGUE_16BYTES(dst, x, length)
310 destPixels[x] = color + BYTE_MUL(destPixels[x], minusAlphaOfColor);
311
312 for (; x < length-3; x += 4) {
313 __m128i dstVector = __lsx_vld(&dst[x], 0);
314 BYTE_MUL_LSX(dstVector, minusAlphaOfColorVector, colorMask, half);
315 dstVector = __lsx_vadd_b(colorVector, dstVector);
316 __lsx_vst(dstVector, &dst[x], 0);
317 }
318 SIMD_EPILOGUE(x, length, 3)
319 destPixels[x] = color + BYTE_MUL(destPixels[x], minusAlphaOfColor);
320 }
321}
322
323void qt_bitmapblit32_lsx_base(QRasterBuffer *rasterBuffer, int x, int y,
324 quint32 color,
325 const uchar *src, int width, int height, int stride)
326{
327 quint32 *dest = reinterpret_cast<quint32*>(rasterBuffer->scanLine(y)) + x;
328 const int destStride = rasterBuffer->stride<quint32>();
329
330 const __m128i c128 = __lsx_vreplgr2vr_w(color);
331 const __m128i maskmask1 = (__m128i)(v4u32){0x80808080, 0x40404040,
332 0x20202020, 0x10101010};
333 const __m128i maskadd1 = (__m128i)(v4i32){0x00000000, 0x40404040,
334 0x60606060, 0x70707070};
335
336 if (width > 4) {
337 const __m128i maskmask2 = (__m128i)(v4i32){0x08080808, 0x04040404,
338 0x02020202, 0x01010101};
339 const __m128i maskadd2 = (__m128i)(v4i32){0x78787878, 0x7c7c7c7c,
340 0x7e7e7e7e, 0x7f7f7f7f};
341 while (height--) {
342 for (int x = 0; x < width; x += 8) {
343 const quint8 s = src[x >> 3];
344 if (!s)
345 continue;
346 __m128i mask1 = __lsx_vreplgr2vr_b(s);
347 __m128i mask2 = mask1;
348
349 mask1 = __lsx_vand_v(mask1, maskmask1);
350 mask1 = __lsx_vadd_b(mask1, maskadd1);
351
352 __m128i destSrc1 = __lsx_vld((char*)(dest + x), 0);
353
354 mask1 = __lsx_vslti_b(mask1,0);
355 destSrc1 = __lsx_vbitsel_v(destSrc1, c128, mask1);
356 __lsx_vst(destSrc1, (char*)(dest + x), 0);
357
358 __m128i destSrc2 = __lsx_vld((char*)(dest + x + 4), 0);
359
360 mask2 = __lsx_vand_v(mask2, maskmask2);
361 mask2 = __lsx_vadd_b(mask2, maskadd2);
362
363 mask2 = __lsx_vslti_b(mask2,0);
364 destSrc2 = __lsx_vbitsel_v(destSrc2, c128, mask2);
365 __lsx_vst(destSrc2, (char*)(dest + x + 4), 0);
366 }
367 dest += destStride;
368 src += stride;
369 }
370 } else {
371 while (height--) {
372 const quint8 s = *src;
373 if (s) {
374 __m128i mask1 = __lsx_vreplgr2vr_b(s);
375
376 __m128i destSrc1 = __lsx_vld((char*)(dest), 0);
377 mask1 = __lsx_vand_v(mask1, maskmask1);
378 mask1 = __lsx_vadd_b(mask1, maskadd1);
379
380 mask1 = __lsx_vslti_b(mask1, 0);
381 destSrc1 = __lsx_vbitsel_v(destSrc1, c128, mask1);
382 __lsx_vst(destSrc1, (char*)(dest), 0);
383 }
384 dest += destStride;
385 src += stride;
386 }
387 }
388}
389
390void qt_bitmapblit32_lsx(QRasterBuffer *rasterBuffer, int x, int y,
391 const QRgba64 &color,
392 const uchar *src, int width, int height, int stride)
393{
394 qt_bitmapblit32_lsx_base(rasterBuffer, x, y, color.toArgb32(), src, width, height, stride);
395}
396
397void qt_bitmapblit8888_lsx(QRasterBuffer *rasterBuffer, int x, int y,
398 const QRgba64 &color,
399 const uchar *src, int width, int height, int stride)
400{
401 qt_bitmapblit32_lsx_base(rasterBuffer, x, y, ARGB2RGBA(color.toArgb32()), src, width, height, stride);
402}
403
404void qt_bitmapblit16_lsx(QRasterBuffer *rasterBuffer, int x, int y,
405 const QRgba64 &color,
406 const uchar *src, int width, int height, int stride)
407{
408 const quint16 c = qConvertRgb32To16(color.toArgb32());
409 quint16 *dest = reinterpret_cast<quint16*>(rasterBuffer->scanLine(y)) + x;
410 const int destStride = rasterBuffer->stride<quint32>();
411
412 const __m128i c128 = __lsx_vreplgr2vr_h(c);
413 const __m128i maskmask = (__m128i)(v8u16){0x8080, 0x4040, 0x2020, 0x1010,
414 0x0808, 0x0404, 0x0202, 0x0101};
415
416 const __m128i maskadd = (__m128i)(v8i16){0x0000, 0x4040, 0x6060, 0x7070,
417 0x7878, 0x7c7c, 0x7e7e, 0x7f7f};
418 while (--height >= 0) {
419 for (int x = 0; x < width; x += 8) {
420 const quint8 s = src[x >> 3];
421 if (!s)
422 continue;
423 __m128i mask = __lsx_vreplgr2vr_b(s);
424 __m128i destSrc = __lsx_vld((char*)(dest + x), 0);
425 mask = __lsx_vand_v(mask, maskmask);
426 mask = __lsx_vadd_b(mask, maskadd);
427 mask = __lsx_vslti_b(mask, 0);
428 destSrc = __lsx_vbitsel_v(destSrc, c128, mask);
429 __lsx_vst(destSrc, (char*)(dest + x), 0);
430 }
431 dest += destStride;
432 src += stride;
433 }
434}
435
436class QSimdLsx
437{
438public:
439 typedef __m128i Int32x4;
440 typedef __m128 Float32x4;
441
442 union Vect_buffer_i { Int32x4 v; int i[4]; };
443 union Vect_buffer_f { Float32x4 v; float f[4]; };
444
445 static inline Float32x4 Q_DECL_VECTORCALL v_dup(float x) { return __lsx_vreplfr2vr_s(x); }
446 static inline Float32x4 Q_DECL_VECTORCALL v_dup(double x) { return __lsx_vreplfr2vr_s(x); }
447 static inline Int32x4 Q_DECL_VECTORCALL v_dup(int x) { return __lsx_vreplgr2vr_w(x); }
448 static inline Int32x4 Q_DECL_VECTORCALL v_dup(uint x) { return __lsx_vreplgr2vr_w(x); }
449
450 static inline Float32x4 Q_DECL_VECTORCALL v_add(Float32x4 a, Float32x4 b) { return __lsx_vfadd_s(a, b); }
451 static inline Int32x4 Q_DECL_VECTORCALL v_add(Int32x4 a, Int32x4 b) { return __lsx_vadd_w(a, b); }
452
453 static inline Float32x4 Q_DECL_VECTORCALL v_max(Float32x4 a, Float32x4 b) { return __lsx_vfmax_s(a, b); }
454 static inline Float32x4 Q_DECL_VECTORCALL v_min(Float32x4 a, Float32x4 b) { return __lsx_vfmin_s(a, b); }
455 static inline Int32x4 Q_DECL_VECTORCALL v_min_16(Int32x4 a, Int32x4 b) { return __lsx_vmin_h(a, b); }
456
457 static inline Int32x4 Q_DECL_VECTORCALL v_and(Int32x4 a, Int32x4 b) { return __lsx_vand_v(a, b); }
458
459 static inline Float32x4 Q_DECL_VECTORCALL v_sub(Float32x4 a, Float32x4 b) { return __lsx_vfsub_s(a, b); }
460 static inline Int32x4 Q_DECL_VECTORCALL v_sub(Int32x4 a, Int32x4 b) { return __lsx_vsub_w(a, b); }
461
462 static inline Float32x4 Q_DECL_VECTORCALL v_mul(Float32x4 a, Float32x4 b) { return __lsx_vfmul_s(a, b); }
463
464 static inline Float32x4 Q_DECL_VECTORCALL v_sqrt(Float32x4 x) { return __lsx_vfsqrt_s(x); }
465
466 static inline Int32x4 Q_DECL_VECTORCALL v_toInt(Float32x4 x) { return __lsx_vftintrz_w_s(x); }
467
468 static inline Int32x4 Q_DECL_VECTORCALL v_greaterOrEqual(Float32x4 a, Float32x4 b) { return __lsx_vfcmp_clt_s(b, a); }
469};
470
471const uint * QT_FASTCALL qt_fetch_radial_gradient_lsx(uint *buffer, const Operator *op,
472 const QSpanData *data,
473 int y, int x, int length)
474{
475 return qt_fetch_radial_gradient_template<QRadialFetchSimd<QSimdLsx>,uint>(buffer, op, data, y, x, length);
476}
477
478void qt_scale_image_argb32_on_argb32_lsx(uchar *destPixels, int dbpl,
479 const uchar *srcPixels, int sbpl, int srch,
480 const QRectF &targetRect,
481 const QRectF &sourceRect,
482 const QRect &clip,
483 int const_alpha)
484{
485 // The SIMD code below assumes x-coordinates small enough to be in 16.16 fixed point format, and opaque const_alpha.
486 if (const_alpha != 256 || !q20::in_range<int16_t>((int)sourceRect.right()) || !q20::in_range<int16_t>((int)sourceRect.left())) {
487 // from qblendfunctions.cpp
488 extern void qt_scale_image_argb32_on_argb32(uchar *destPixels, int dbpl,
489 const uchar *srcPixels, int sbpl, int srch,
490 const QRectF &targetRect,
491 const QRectF &sourceRect,
492 const QRect &clip,
493 int const_alpha);
494 return qt_scale_image_argb32_on_argb32(destPixels, dbpl, srcPixels, sbpl, srch,
495 targetRect, sourceRect, clip, const_alpha);
496 }
497
498 if (targetRect.width() == 0 || targetRect.height() == 0)
499 return;
500
501 const qreal sx = sourceRect.width() / (qreal)targetRect.width();
502 const qreal sy = sourceRect.height() / (qreal)targetRect.height();
503
504 const int ix = 0x00010000 * sx;
505 const int64_t iy = 0x00010000 * sy;
506
507 QRect tr = targetRect.normalized().toRect();
508 tr = tr.intersected(clip);
509 if (tr.isEmpty())
510 return;
511 const int tx1 = tr.left();
512 const int ty1 = tr.top();
513 int h = tr.height();
514 int w = tr.width();
515
516 quint32 basex;
517 quint64 srcy;
518
519 if (sx < 0) {
520 int dstx = qFloor((tx1 + qreal(0.5) - targetRect.right()) * sx * 65536) + 1;
521 basex = quint32(sourceRect.right() * 65536) + dstx;
522 } else {
523 int dstx = qCeil((tx1 + qreal(0.5) - targetRect.left()) * sx * 65536) - 1;
524 basex = quint32(sourceRect.left() * 65536) + dstx;
525 }
526 if (sy < 0) {
527 int64_t dsty = qFloor64((ty1 + qreal(0.5) - targetRect.bottom()) * sy * 65536) + 1;
528 srcy = quint64(sourceRect.bottom() * 65536) + dsty;
529 } else {
530 int64_t dsty = qCeil64((ty1 + qreal(0.5) - targetRect.top()) * sy * 65536) - 1;
531 srcy = quint64(sourceRect.top() * 65536) + dsty;
532 }
533
534 quint32 *dst = ((quint32 *) (destPixels + qsizetype(ty1) * dbpl)) + tx1;
535
536 const __m128i nullVector = __lsx_vreplgr2vr_w(0);
537 const __m128i half = __lsx_vreplgr2vr_h(0x80);
538 const __m128i one = __lsx_vreplgr2vr_h(0xff);
539 const __m128i colorMask = __lsx_vreplgr2vr_w(0x00ff00ff);
540 const __m128i alphaMask = __lsx_vreplgr2vr_w(0xff000000);
541 const __m128i ixVector = __lsx_vreplgr2vr_w(4*static_cast<unsigned>(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 const int ystart = static_cast<int>(srcy >> 16);
546 if (ystart >= srch && iy < 0) {
547 srcy += iy;
548 --h;
549 }
550 const int xstart = basex >> 16;
551 if (xstart >= (int)(sbpl/sizeof(quint32)) && ix < 0) {
552 basex += ix;
553 --w;
554 }
555 int yend = static_cast<int>((srcy + iy * (h - 1)) >> 16);
556 if (yend < 0 || yend >= srch)
557 --h;
558 int xend = (basex + ix * (w - 1)) >> 16;
559 if (xend < 0 || xend >= (int)(sbpl/sizeof(quint32)))
560 --w;
561
562 while (--h >= 0) {
563 const uint *src = (const quint32 *) (srcPixels + (srcy >> 16) * sbpl);
564 int srcx = basex;
565 int x = 0;
566
567 ALIGNMENT_PROLOGUE_16BYTES(dst, x, w) {
568 uint s = src[srcx >> 16];
569 dst[x] = s + BYTE_MUL(dst[x], qAlpha(~s));
570 srcx += ix;
571 }
572
573 __m128i srcxVector = (__m128i)(v4i32){srcx + ix + ix + ix, srcx + ix + ix, srcx + ix, srcx};
574
575 for (; x < (w - 3); x += 4) {
576 const int idx0 = __lsx_vpickve2gr_h(srcxVector, 1);
577 const int idx1 = __lsx_vpickve2gr_h(srcxVector, 3);
578 const int idx2 = __lsx_vpickve2gr_h(srcxVector, 5);
579 const int idx3 = __lsx_vpickve2gr_h(srcxVector, 7);
580 srcxVector = __lsx_vadd_w(srcxVector, ixVector);
581
582 const __m128i srcVector = (__m128i)((v4u32){src[idx3], src[idx2], src[idx1], src[idx0]});
583
584 BLEND_SOURCE_OVER_ARGB32_LSX_helper(dst, x, srcVector, nullVector, half, one, colorMask, alphaMask);
585 }
586
587 SIMD_EPILOGUE(x, w, 3) {
588 uint s = src[(basex + x*ix) >> 16];
589 dst[x] = s + BYTE_MUL(dst[x], qAlpha(~s));
590 }
591 dst = (quint32 *)(((uchar *) dst) + dbpl);
592 srcy += iy;
593 }
594}
595
596const uint *QT_FASTCALL fetchPixelsBPP24_lsx(uint *buffer, const uchar *src, int index, int count)
597{
598 const quint24 *s = reinterpret_cast<const quint24 *>(src);
599 for (int i = 0; i < count; ++i)
600 buffer[i] = s[index + i];
601 return buffer;
602}
603
604const uint * QT_FASTCALL qt_fetchUntransformed_888_lsx(uint *buffer, const Operator *,
605 const QSpanData *data,
606 int y, int x, int length)
607{
608 const uchar *line = data->texture.scanLine(y) + x * 3;
609 // from image/qimage_lsx.cpp
610 extern void QT_FASTCALL qt_convert_rgb888_to_rgb32_lsx(quint32 *dst, const uchar *src, int len);
611 qt_convert_rgb888_to_rgb32_lsx(buffer, line, length);
612 return buffer;
613}
614
615void qt_memfill24_lsx(quint24 *dest, quint24 color, qsizetype count)
616{
617 // LCM of 12 and 16 bytes is 48 bytes (16 px)
618 quint32 v = color;
619 __m128i m = __lsx_vinsgr2vr_w(__lsx_vldi(0), v, 0);
620 quint24 *end = dest + count;
621
622 constexpr uchar x = 2, y = 1, z = 0;
623 alignas(__m128i) static const uchar
624 shuffleMask[16 + 1] = { x, y, z, x, y, z, x, y, z, x, y, z, x, y, z, x, y };
625 __m128i indexMask = (__m128i)(v16i8){2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17};
626
627 __m128i mval1 = __lsx_vshuf_b(m, m, __lsx_vld(reinterpret_cast<const __m128i *>(shuffleMask), 0));
628 __m128i mval2 = __lsx_vshuf_b(m, m, __lsx_vld(reinterpret_cast<const __m128i *>(shuffleMask + 1), 0));
629 __m128i mval3 = __lsx_vshuf_b(mval2, mval1, indexMask);
630
631 for ( ; dest + 16 <= end; dest += 16) {
632 __lsx_vst(mval1, reinterpret_cast<__m128i *>(dest) + 0, 0);
633 __lsx_vst(mval2, reinterpret_cast<__m128i *>(dest) + 1, 0);
634 __lsx_vst(mval3, reinterpret_cast<__m128i *>(dest) + 2, 0);
635 }
636
637 if (count < 3) {
638 if (count > 1)
639 end[-2] = v;
640 if (count)
641 end[-1] = v;
642 return;
643 }
644
645 // less than 16px/48B left
646 uchar *ptr = reinterpret_cast<uchar *>(dest);
647 uchar *ptr_end = reinterpret_cast<uchar *>(end);
648 qptrdiff left = ptr_end - ptr;
649 if (left >= 24) {
650 // 8px/24B or more left
651 __lsx_vst(mval1, reinterpret_cast<__m128i *>(ptr) + 0, 0);
652 __lsx_vstelm_d(mval2, reinterpret_cast<__m128i *>(ptr) + 1, 0, 0);
653 ptr += 24;
654 left -= 24;
655 }
656
657 // less than 8px/24B left
658
659 if (left >= 16) {
660 // but more than 5px/15B left
661 __lsx_vst(mval1, reinterpret_cast<__m128i *>(ptr) , 0);
662 } else if (left >= 8) {
663 // but more than 2px/6B left
664 __lsx_vstelm_d(mval1, reinterpret_cast<__m128i *>(ptr), 0, 0);
665 }
666
667 if (left) {
668 // 1 or 2px left
669 // store 8 bytes ending with the right values (will overwrite a bit)
670 __lsx_vstelm_d(mval2, reinterpret_cast<__m128i *>(ptr_end - 8), 0, 0);
671 }
672}
673
674void QT_FASTCALL rbSwap_888_lsx(uchar *dst, const uchar *src, int count)
675{
676 int i = 0;
677 const static __m128i shuffleMask1 = (__m128i)(v16i8){2, 1, 0, 5, 4, 3, 8, 7, 6, 11, 10, 9, 14, 13, 12, 15};
678 const static __m128i shuffleMask2 = (__m128i)(v16i8){0, 1, 4, 3, 2, 7, 6, 5, 10, 9, 8, 13, 12, 11, 14, 15};
679 const static __m128i shuffleMask3 = (__m128i)(v16i8){0, 3, 2, 1, 6, 5, 4, 9, 8, 7, 12, 11, 10, 15, 14, 13};
680
681 for (; i + 15 < count; i += 16) {
682 __m128i s1 = __lsx_vld(src, 0);
683 __m128i s2 = __lsx_vld((src + 16), 0);
684 __m128i s3 = __lsx_vld((src + 32), 0);
685 s1 = __lsx_vshuf_b(s1, s1, shuffleMask1);
686 s2 = __lsx_vshuf_b(s2, s2, shuffleMask2);
687 s3 = __lsx_vshuf_b(s3, s3, shuffleMask3);
688 __lsx_vst(s1, dst, 0);
689 __lsx_vst(s2, (dst + 16), 0);
690 __lsx_vst(s3, (dst + 32), 0);
691
692 // Now fix the last four misplaced values
693 std::swap(dst[15], dst[17]);
694 std::swap(dst[30], dst[32]);
695
696 src += 48;
697 dst += 48;
698 }
699
700 if (src != dst) {
701 SIMD_EPILOGUE(i, count, 15) {
702 dst[0] = src[2];
703 dst[1] = src[1];
704 dst[2] = src[0];
705 dst += 3;
706 src += 3;
707 }
708 } else {
709 SIMD_EPILOGUE(i, count, 15) {
710 std::swap(dst[0], dst[2]);
711 dst += 3;
712 }
713 }
714}
715
716template<bool RGBA>
717static void convertARGBToARGB32PM_lsx(uint *buffer, const uint *src, int count)
718{
719 int i = 0;
720 const __m128i alphaMask = __lsx_vreplgr2vr_w(0xff000000);
721 const __m128i rgbaMask = (__m128i)(v16i8){2, 1, 0, 3, 6, 5, 4, 7, 10, 9, 8, 11, 14, 13, 12, 15};
722 const __m128i shuffleMask = (__m128i)(v16i8){6, 7, 6, 7, 6, 7, 6, 7, 14, 15, 14, 15, 14, 15, 14, 15};
723 const __m128i half = __lsx_vreplgr2vr_h(0x0080);
724 const __m128i zero = __lsx_vldi(0);
725
726 for (; i < count - 3; i += 4) {
727 __m128i srcVector = __lsx_vld(&src[i], 0);
728 const v4i32 testz = (v4i32)__lsx_vmsknz_b(__lsx_vand_v(srcVector, alphaMask));
729 if (testz[0]!=0) {
730 const v4i32 testc = (v4i32)__lsx_vmsknz_b(__lsx_vandn_v(srcVector, alphaMask));
731 if (testc[0]!=0) {
732 if (RGBA)
733 srcVector = __lsx_vshuf_b(zero, srcVector, rgbaMask);
734 __m128i src1 = __lsx_vilvl_b(zero, srcVector);
735 __m128i src2 = __lsx_vilvh_b(zero, srcVector);
736 __m128i alpha1 = __lsx_vshuf_b(zero, src1, shuffleMask);
737 __m128i alpha2 = __lsx_vshuf_b(zero, src2, shuffleMask);
738 src1 = __lsx_vmul_h(src1, alpha1);
739 src2 = __lsx_vmul_h(src2, alpha2);
740 src1 = __lsx_vadd_h(src1, __lsx_vsrli_h(src1, 8));
741 src2 = __lsx_vadd_h(src2, __lsx_vsrli_h(src2, 8));
742 src1 = __lsx_vadd_h(src1, half);
743 src2 = __lsx_vadd_h(src2, half);
744 src1 = __lsx_vsrli_h(src1, 8);
745 src2 = __lsx_vsrli_h(src2, 8);
746 __m128i blendMask = (__m128i)(v8i16){0, 1, 2, 11, 4, 5, 6, 15};
747 src1 = __lsx_vshuf_h(blendMask, alpha1, src1);
748 src2 = __lsx_vshuf_h(blendMask, alpha2, src2);
749 src1 = __lsx_vmaxi_h(src1, 0);
750 src2 = __lsx_vmaxi_h(src2, 0);
751 srcVector = __lsx_vpickev_b(__lsx_vsat_hu(src2, 7), __lsx_vsat_hu(src1, 7));
752 __lsx_vst(srcVector, &buffer[i], 0);
753 } else {
754 if (RGBA)
755 __lsx_vst(__lsx_vshuf_b(zero, srcVector, rgbaMask), &buffer[i], 0);
756 else if (buffer != src)
757 __lsx_vst(srcVector, &buffer[i], 0);
758 }
759 } else {
760 __lsx_vst(zero, &buffer[i], 0);
761 }
762 }
763
764 SIMD_EPILOGUE(i, count, 3) {
765 uint v = qPremultiply(src[i]);
766 buffer[i] = RGBA ? RGBA2ARGB(v) : v;
767 }
768}
769
770template<bool RGBA>
771static void convertARGBToRGBA64PM_lsx(QRgba64 *buffer, const uint *src, int count)
772{
773 int i = 0;
774 const __m128i alphaMask = __lsx_vreplgr2vr_w(0xff000000);
775 const __m128i rgbaMask = (__m128i)(v16i8){2, 1, 0, 3, 6, 5, 4, 7, 10, 9, 8, 11, 14, 13, 12, 15};
776 const __m128i shuffleMask = (__m128i)(v16i8){6, 7, 6, 7, 6, 7, 6, 7, 14, 15, 14, 15, 14, 15, 14, 15};
777 const __m128i zero = __lsx_vldi(0);
778
779 for (; i < count - 3; i += 4) {
780 __m128i srcVector = __lsx_vld(&src[i], 0);
781 const v4i32 testz = (v4i32)__lsx_vmsknz_b(__lsx_vand_v(srcVector, alphaMask));
782 if (testz[0]!=0) {
783 const v4i32 testc = (v4i32)__lsx_vmsknz_b(__lsx_vandn_v(srcVector, alphaMask));
784 if (!RGBA)
785 srcVector = __lsx_vshuf_b(zero, srcVector, rgbaMask);
786 const __m128i src1 = __lsx_vilvl_b(srcVector, srcVector);
787 const __m128i src2 = __lsx_vilvh_b(srcVector, srcVector);
788 if (testc[0]!=0) {
789 __m128i alpha1 = __lsx_vshuf_b(zero, src1, shuffleMask);
790 __m128i alpha2 = __lsx_vshuf_b(zero, src2, shuffleMask);
791 __m128i dst1 = __lsx_vmuh_hu(src1, alpha1);
792 __m128i dst2 = __lsx_vmuh_hu(src2, alpha2);
793 // Map 0->0xfffe to 0->0xffff
794 dst1 = __lsx_vadd_h(dst1, __lsx_vsrli_h(dst1, 15));
795 dst2 = __lsx_vadd_h(dst2, __lsx_vsrli_h(dst2, 15));
796 // correct alpha value:
797 const __m128i blendMask = (__m128i)(v8i16){0, 1, 2, 11, 4, 5, 6, 15};
798 dst1 = __lsx_vshuf_h(blendMask, src1, dst1);
799 dst2 = __lsx_vshuf_h(blendMask, src2, dst2);
800 __lsx_vst(dst1, &buffer[i], 0);
801 __lsx_vst(dst2, &buffer[i + 2], 0);
802 } else {
803 __lsx_vst(src1, &buffer[i], 0);
804 __lsx_vst(src2, &buffer[i + 2], 0);
805 }
806 } else {
807 __lsx_vst(zero, &buffer[i], 0);
808 __lsx_vst(zero, &buffer[i + 2], 0);
809 }
810 }
811
812 SIMD_EPILOGUE(i, count, 3) {
813 const uint s = RGBA ? RGBA2ARGB(src[i]) : src[i];
814 buffer[i] = QRgba64::fromArgb32(s).premultiplied();
815 }
816}
817
818template<bool RGBA, bool RGBx>
819static inline void convertARGBFromARGB32PM_lsx(uint *buffer, const uint *src, int count)
820{
821 int i = 0;
822 const __m128i alphaMask = __lsx_vreplgr2vr_w(0xff000000);
823 const __m128i rgbaMask = (__m128i)(v16i8){2, 1, 0, 3, 6, 5, 4, 7, 10, 9, 8, 11, 14, 13, 12, 15};
824 const __m128i zero = __lsx_vldi(0);
825
826 for (; i < count - 3; i += 4) {
827 __m128i srcVector = __lsx_vld(&src[i], 0);
828 const v4i32 testz = (v4i32)__lsx_vmsknz_b(__lsx_vand_v(srcVector, alphaMask));
829 if (testz[0]!=0) {
830 const v4i32 testc = (v4i32)__lsx_vmsknz_b(__lsx_vandn_v(srcVector, alphaMask));
831 if (testc[0]!=0) {
832 __m128i srcVectorAlpha = __lsx_vsrli_w(srcVector, 24);
833 if (RGBA)
834 srcVector = __lsx_vshuf_b(zero, srcVector, rgbaMask);
835 const __m128 a = __lsx_vffint_s_w(srcVectorAlpha);
836 const __m128 ia = reciprocal_mul_ps(a, 255.0f);
837 __m128i src1 = __lsx_vilvl_b(zero, srcVector);
838 __m128i src3 = __lsx_vilvh_b(zero, srcVector);
839 __m128i src2 = __lsx_vilvh_h(zero, src1);
840 __m128i src4 = __lsx_vilvh_h(zero, src3);
841 src1 = __lsx_vilvl_h(zero, src1);
842 src3 = __lsx_vilvl_h(zero, src3);
843 __m128 ia1 = (__m128)__lsx_vreplvei_w(ia, 0);
844 __m128 ia2 = (__m128)__lsx_vreplvei_w(ia, 1);
845 __m128 ia3 = (__m128)__lsx_vreplvei_w(ia, 2);
846 __m128 ia4 = (__m128)__lsx_vreplvei_w(ia, 3);
847 src1 = __lsx_vftintrne_w_s(__lsx_vfmul_s(__lsx_vffint_s_w(src1), ia1));
848 src2 = __lsx_vftintrne_w_s(__lsx_vfmul_s(__lsx_vffint_s_w(src2), ia2));
849 src3 = __lsx_vftintrne_w_s(__lsx_vfmul_s(__lsx_vffint_s_w(src3), ia3));
850 src4 = __lsx_vftintrne_w_s(__lsx_vfmul_s(__lsx_vffint_s_w(src4), ia4));
851 src1 = __lsx_vpickev_h(__lsx_vsat_wu(src2, 15), __lsx_vsat_wu(src1, 15));
852 src3 = __lsx_vpickev_h(__lsx_vsat_wu(src4, 15), __lsx_vsat_wu(src3, 15));
853 src1 = __lsx_vmaxi_h(src1, 0);
854 src3 = __lsx_vmaxi_h(src3, 0);
855 src1 = __lsx_vpickev_b(__lsx_vsat_hu(src3, 7), __lsx_vsat_hu(src1, 7));
856 // Handle potential alpha == 0 values:
857 __m128i srcVectorAlphaMask = __lsx_vseq_w(srcVectorAlpha, zero);
858 src1 = __lsx_vandn_v(srcVectorAlphaMask, src1);
859 // Fixup alpha values:
860 if (RGBx)
861 srcVector = __lsx_vor_v(src1, alphaMask);
862 else
863 srcVector = __lsx_vbitsel_v(src1, srcVector, __lsx_vslti_b(alphaMask, 0));
864 __lsx_vst(srcVector, &buffer[i], 0);
865 } else {
866 if (RGBA)
867 __lsx_vst(__lsx_vshuf_b(zero, srcVector, rgbaMask), &buffer[i], 0);
868 else if (buffer != src)
869 __lsx_vst(srcVector, &buffer[i], 0);
870 }
871 } else {
872 if (RGBx)
873 __lsx_vst(alphaMask, &buffer[i], 0);
874 else
875 __lsx_vst(zero, &buffer[i], 0);
876 }
877 }
878
879 SIMD_EPILOGUE(i, count, 3) {
880 uint v = qUnpremultiply_lsx(src[i]);
881 if (RGBx)
882 v = 0xff000000 | v;
883 if (RGBA)
884 v = ARGB2RGBA(v);
885 buffer[i] = v;
886 }
887}
888
889template<bool RGBA>
890static inline void convertARGBFromRGBA64PM_lsx(uint *buffer, const QRgba64 *src, int count)
891{
892 int i = 0;
893 const __m128i alphaMask = __lsx_vreplgr2vr_d(qint64(Q_UINT64_C(0xffff) << 48));
894 const __m128i alphaMask32 = __lsx_vreplgr2vr_w(0xff000000);
895 const __m128i rgbaMask = (__m128i)(v16i8){2, 1, 0, 3, 6, 5, 4, 7, 10, 9, 8, 11, 14, 13, 12, 15};
896 const __m128i zero = __lsx_vldi(0);
897
898 for (; i < count - 3; i += 4) {
899 __m128i srcVector1 = __lsx_vld(&src[i], 0);
900 __m128i srcVector2 = __lsx_vld(&src[i + 2], 0);
901 const v4i32 testz1 = (v4i32)__lsx_vmsknz_b(__lsx_vand_v(srcVector1, alphaMask));
902 bool transparent1 = testz1[0]==0;
903 const v4i32 testc1 = (v4i32)__lsx_vmsknz_b(__lsx_vandn_v(srcVector1, alphaMask));
904 bool opaque1 = testc1[0]==0;
905 const v4i32 testz2 = (v4i32)__lsx_vmsknz_b(__lsx_vand_v(srcVector2, alphaMask));
906 bool transparent2 = testz2[0]==0;
907 const v4i32 testc2 = (v4i32)__lsx_vmsknz_b(__lsx_vandn_v(srcVector2, alphaMask));
908 bool opaque2 = testc2[0]==0;
909
910 if (!(transparent1 && transparent2)) {
911 if (!(opaque1 && opaque2)) {
912 __m128i srcVector1Alpha = __lsx_vsrli_d(srcVector1, 48);
913 __m128i srcVector2Alpha = __lsx_vsrli_d(srcVector2, 48);
914 __m128i srcVectorAlpha = __lsx_vpickev_h(__lsx_vsat_wu(srcVector2Alpha, 15),
915 __lsx_vsat_wu(srcVector1Alpha, 15));
916 const __m128 a = __lsx_vffint_s_w(srcVectorAlpha);
917 // Convert srcVectorAlpha to final 8-bit alpha channel
918 srcVectorAlpha = __lsx_vadd_w(srcVectorAlpha, __lsx_vreplgr2vr_w(128));
919 srcVectorAlpha = __lsx_vsub_w(srcVectorAlpha, __lsx_vsrli_w(srcVectorAlpha, 8));
920 srcVectorAlpha = __lsx_vsrli_w(srcVectorAlpha, 8);
921 srcVectorAlpha = __lsx_vslli_w(srcVectorAlpha, 24);
922 const __m128 ia = reciprocal_mul_ps(a, 255.0f);
923 __m128i src1 = __lsx_vilvl_h(zero, srcVector1);
924 __m128i src2 = __lsx_vilvh_h(zero, srcVector1);
925 __m128i src3 = __lsx_vilvl_h(zero, srcVector2);
926 __m128i src4 = __lsx_vilvh_h(zero, srcVector2);
927 __m128 ia1 = (__m128)__lsx_vreplvei_w(ia, 0);
928 __m128 ia2 = (__m128)__lsx_vreplvei_w(ia, 1);
929 __m128 ia3 = (__m128)__lsx_vreplvei_w(ia, 2);
930 __m128 ia4 = (__m128)__lsx_vreplvei_w(ia, 3);
931 src1 = __lsx_vftintrne_w_s(__lsx_vfmul_s(__lsx_vffint_s_w(src1), ia1));
932 src2 = __lsx_vftintrne_w_s(__lsx_vfmul_s(__lsx_vffint_s_w(src2), ia2));
933 src3 = __lsx_vftintrne_w_s(__lsx_vfmul_s(__lsx_vffint_s_w(src3), ia3));
934 src4 = __lsx_vftintrne_w_s(__lsx_vfmul_s(__lsx_vffint_s_w(src4), ia4));
935 src1 = __lsx_vpickev_h(__lsx_vsat_wu(src2, 15), __lsx_vsat_wu(src1, 15));
936 src3 = __lsx_vpickev_h(__lsx_vsat_wu(src4, 15), __lsx_vsat_wu(src3, 15));
937 // Handle potential alpha == 0 values:
938 __m128i srcVector1AlphaMask = __lsx_vseq_d(srcVector1Alpha, zero);
939 __m128i srcVector2AlphaMask = __lsx_vseq_d(srcVector2Alpha, zero);
940 src1 = __lsx_vandn_v(srcVector1AlphaMask, src1);
941 src3 = __lsx_vandn_v(srcVector2AlphaMask, src3);
942 src1 = __lsx_vmaxi_h(src1, 0);
943 src3 = __lsx_vmaxi_h(src3, 0);
944 src1 = __lsx_vpickev_b(__lsx_vsat_hu(src3, 7), __lsx_vsat_hu(src1, 7));
945 // Fixup alpha values:
946 src1 = __lsx_vbitsel_v(src1, srcVectorAlpha, __lsx_vslti_b(alphaMask32, 0));
947 // Fix RGB order
948 if (!RGBA){
949 src1 = __lsx_vshuf_b(zero, src1, rgbaMask);}
950 __lsx_vst(src1, (__m128i *)&buffer[i], 0);
951 } else {
952 __m128i src1 = __lsx_vilvl_h(zero, srcVector1);
953 __m128i src2 = __lsx_vilvh_h(zero, srcVector1);
954 __m128i src3 = __lsx_vilvl_h(zero, srcVector2);
955 __m128i src4 = __lsx_vilvh_h(zero, srcVector2);
956 src1 = __lsx_vadd_w(src1, __lsx_vreplgr2vr_w(128));
957 src2 = __lsx_vadd_w(src2, __lsx_vreplgr2vr_w(128));
958 src3 = __lsx_vadd_w(src3, __lsx_vreplgr2vr_w(128));
959 src4 = __lsx_vadd_w(src4, __lsx_vreplgr2vr_w(128));
960 src1 = __lsx_vsub_w(src1, __lsx_vsrli_w(src1, 8));
961 src2 = __lsx_vsub_w(src2, __lsx_vsrli_w(src2, 8));
962 src3 = __lsx_vsub_w(src3, __lsx_vsrli_w(src3, 8));
963 src4 = __lsx_vsub_w(src4, __lsx_vsrli_w(src4, 8));
964 src1 = __lsx_vsrli_w(src1, 8);
965 src2 = __lsx_vsrli_w(src2, 8);
966 src3 = __lsx_vsrli_w(src3, 8);
967 src4 = __lsx_vsrli_w(src4, 8);
968 src1 = __lsx_vpickev_h(__lsx_vsat_wu(src2, 15), __lsx_vsat_wu(src1, 15));
969 src3 = __lsx_vpickev_h(__lsx_vsat_wu(src4, 15), __lsx_vsat_wu(src3, 15));
970 src1 = __lsx_vmaxi_h(src1, 0);
971 src3 = __lsx_vmaxi_h(src3, 0);
972 src1 = __lsx_vpickev_b(__lsx_vsat_hu(src3, 7), __lsx_vsat_hu(src1, 15));
973 if (!RGBA){
974 src1 = __lsx_vshuf_b(zero, src1, rgbaMask);}
975 __lsx_vst(src1, &buffer[i], 0);
976 }
977 } else {
978 __lsx_vst(zero, &buffer[i], 0);
979 }
980 }
981
982 SIMD_EPILOGUE(i, count, 3) {
983 buffer[i] = qConvertRgba64ToRgb32_lsx<RGBA ? PixelOrderRGB : PixelOrderBGR>(src[i]);
984 }
985}
986
987template<bool mask>
988static inline void convertRGBA64FromRGBA64PM_lsx(QRgba64 *buffer, const QRgba64 *src, int count)
989{
990 int i = 0;
991 const __m128i alphaMask = __lsx_vreplgr2vr_d(qint64(Q_UINT64_C(0xffff) << 48));
992 const __m128i zero = __lsx_vldi(0);
993
994 for (; i < count - 3; i += 4) {
995 __m128i srcVector1 = __lsx_vld(&src[i + 0], 0);
996 __m128i srcVector2 = __lsx_vld(&src[i + 2], 0);
997 const v4i32 testz1 = (v4i32)__lsx_vmsknz_b(__lsx_vand_v(srcVector1, alphaMask));
998 bool transparent1 = testz1[0]==0;
999 const v4i32 testc1 = (v4i32)__lsx_vmsknz_b(__lsx_vandn_v(srcVector1, alphaMask));
1000 bool opaque1 = testc1[0]==0;
1001 const v4i32 testz2 = (v4i32)__lsx_vmsknz_b(__lsx_vand_v(srcVector2, alphaMask));
1002 bool transparent2 = testz2[0]==0;
1003 const v4i32 testc2 = (v4i32)__lsx_vmsknz_b(__lsx_vandn_v(srcVector2, alphaMask));
1004 bool opaque2 = testc2[0]==0;
1005
1006 if (!(transparent1 && transparent2)) {
1007 if (!(opaque1 && opaque2)) {
1008 __m128i srcVector1Alpha = __lsx_vsrli_d(srcVector1, 48);
1009 __m128i srcVector2Alpha = __lsx_vsrli_d(srcVector2, 48);
1010 __m128i srcVectorAlpha = __lsx_vpickev_h(__lsx_vsat_wu(srcVector2Alpha, 15),
1011 __lsx_vsat_wu(srcVector1Alpha, 15));
1012 const __m128 a = __lsx_vffint_s_w(srcVectorAlpha);
1013 const __m128 ia = reciprocal_mul_ps(a, 65535.0f);
1014 __m128i src1 = __lsx_vilvl_h(zero, srcVector1);
1015 __m128i src2 = __lsx_vilvh_h(zero, srcVector1);
1016 __m128i src3 = __lsx_vilvl_h(zero, srcVector2);
1017 __m128i src4 = __lsx_vilvh_h(zero, srcVector2);
1018 __m128 ia1 = (__m128)__lsx_vreplvei_w(ia, 0);
1019 __m128 ia2 = (__m128)__lsx_vreplvei_w(ia, 1);
1020 __m128 ia3 = (__m128)__lsx_vreplvei_w(ia, 2);
1021 __m128 ia4 = (__m128)__lsx_vreplvei_w(ia, 3);
1022 src1 = __lsx_vftintrne_w_s(__lsx_vfmul_s(__lsx_vffint_s_w(src1), ia1));
1023 src2 = __lsx_vftintrne_w_s(__lsx_vfmul_s(__lsx_vffint_s_w(src2), ia2));
1024 src3 = __lsx_vftintrne_w_s(__lsx_vfmul_s(__lsx_vffint_s_w(src3), ia3));
1025 src4 = __lsx_vftintrne_w_s(__lsx_vfmul_s(__lsx_vffint_s_w(src4), ia4));
1026 src1 = __lsx_vpickev_h(__lsx_vsat_wu(src2, 15), __lsx_vsat_wu(src1, 15));
1027 src3 = __lsx_vpickev_h(__lsx_vsat_wu(src4, 15), __lsx_vsat_wu(src3, 15));
1028 // Handle potential alpha == 0 values:
1029 __m128i srcVector1AlphaMask = __lsx_vseq_d(srcVector1Alpha, zero);
1030 __m128i srcVector2AlphaMask = __lsx_vseq_d(srcVector2Alpha, zero);
1031 src1 = __lsx_vandn_v(srcVector1AlphaMask, src1);
1032 src3 = __lsx_vandn_v(srcVector2AlphaMask, src3);
1033 // Fixup alpha values:
1034 if (mask) {
1035 src1 = __lsx_vor_v(src1, alphaMask);
1036 src3 = __lsx_vor_v(src3, alphaMask);
1037 } else {
1038 src1 = __lsx_vbitsel_v(src1, srcVector1, __lsx_vslti_b(alphaMask, 0));
1039 src3 = __lsx_vbitsel_v(src3, srcVector2, __lsx_vslti_b(alphaMask, 0));
1040 }
1041 __lsx_vst(src1, &buffer[i + 0], 0);
1042 __lsx_vst(src3, &buffer[i + 2], 0);
1043 } else {
1044 if (mask) {
1045 srcVector1 = __lsx_vor_v(srcVector1, alphaMask);
1046 srcVector2 = __lsx_vor_v(srcVector2, alphaMask);
1047 }
1048 if (mask || src != buffer) {
1049 __lsx_vst(srcVector1, &buffer[i + 0], 0);
1050 __lsx_vst(srcVector2, &buffer[i + 2], 0);
1051 }
1052 }
1053 } else {
1054 __lsx_vst(zero, &buffer[i + 0], 0);
1055 __lsx_vst(zero, &buffer[i + 2], 0);
1056 }
1057 }
1058
1059 SIMD_EPILOGUE(i, count, 3) {
1060 QRgba64 v = src[i].unpremultiplied();
1061 if (mask)
1062 v.setAlpha(65535);
1063 buffer[i] = v;
1064 }
1065}
1066
1067void QT_FASTCALL convertARGB32ToARGB32PM_lsx(uint *buffer, int count, const QList<QRgb> *)
1068{
1069 convertARGBToARGB32PM_lsx<false>(buffer, buffer, count);
1070}
1071
1072void QT_FASTCALL convertRGBA8888ToARGB32PM_lsx(uint *buffer, int count, const QList<QRgb> *)
1073{
1074 convertARGBToARGB32PM_lsx<true>(buffer, buffer, count);
1075}
1076
1077const QRgba64 * QT_FASTCALL convertARGB32ToRGBA64PM_lsx(QRgba64 *buffer, const uint *src, int count,
1078 const QList<QRgb> *, QDitherInfo *)
1079{
1080 convertARGBToRGBA64PM_lsx<false>(buffer, src, count);
1081 return buffer;
1082}
1083
1084const QRgba64 * QT_FASTCALL convertRGBA8888ToRGBA64PM_lsx(QRgba64 *buffer, const uint *src, int count,
1085 const QList<QRgb> *, QDitherInfo *)
1086{
1087 convertARGBToRGBA64PM_lsx<true>(buffer, src, count);
1088 return buffer;
1089}
1090
1091const uint *QT_FASTCALL fetchARGB32ToARGB32PM_lsx(uint *buffer, const uchar *src, int index, int count,
1092 const QList<QRgb> *, QDitherInfo *)
1093{
1094 convertARGBToARGB32PM_lsx<false>(buffer, reinterpret_cast<const uint *>(src) + index, count);
1095 return buffer;
1096}
1097
1098const uint *QT_FASTCALL fetchRGBA8888ToARGB32PM_lsx(uint *buffer, const uchar *src, int index, int count,
1099 const QList<QRgb> *, QDitherInfo *)
1100{
1101 convertARGBToARGB32PM_lsx<true>(buffer, reinterpret_cast<const uint *>(src) + index, count);
1102 return buffer;
1103}
1104
1105const QRgba64 *QT_FASTCALL fetchARGB32ToRGBA64PM_lsx(QRgba64 *buffer, const uchar *src, int index, int count,
1106 const QList<QRgb> *, QDitherInfo *)
1107{
1108 convertARGBToRGBA64PM_lsx<false>(buffer, reinterpret_cast<const uint *>(src) + index, count);
1109 return buffer;
1110}
1111
1112const QRgba64 *QT_FASTCALL fetchRGBA8888ToRGBA64PM_lsx(QRgba64 *buffer, const uchar *src, int index, int count,
1113 const QList<QRgb> *, QDitherInfo *)
1114{
1115 convertARGBToRGBA64PM_lsx<true>(buffer, reinterpret_cast<const uint *>(src) + index, count);
1116 return buffer;
1117}
1118
1119void QT_FASTCALL storeRGB32FromARGB32PM_lsx(uchar *dest, const uint *src, int index, int count,
1120 const QList<QRgb> *, QDitherInfo *)
1121{
1122 uint *d = reinterpret_cast<uint *>(dest) + index;
1123 convertARGBFromARGB32PM_lsx<false,true>(d, src, count);
1124}
1125
1126void QT_FASTCALL storeARGB32FromARGB32PM_lsx(uchar *dest, const uint *src, int index, int count,
1127 const QList<QRgb> *, QDitherInfo *)
1128{
1129 uint *d = reinterpret_cast<uint *>(dest) + index;
1130 convertARGBFromARGB32PM_lsx<false,false>(d, src, count);
1131}
1132
1133void QT_FASTCALL storeRGBA8888FromARGB32PM_lsx(uchar *dest, const uint *src, int index, int count,
1134 const QList<QRgb> *, QDitherInfo *)
1135{
1136 uint *d = reinterpret_cast<uint *>(dest) + index;
1137 convertARGBFromARGB32PM_lsx<true,false>(d, src, count);
1138}
1139
1140void QT_FASTCALL storeRGBXFromARGB32PM_lsx(uchar *dest, const uint *src, int index, int count,
1141 const QList<QRgb> *, QDitherInfo *)
1142{
1143 uint *d = reinterpret_cast<uint *>(dest) + index;
1144 convertARGBFromARGB32PM_lsx<true,true>(d, src, count);
1145}
1146
1147template<QtPixelOrder PixelOrder>
1148void QT_FASTCALL storeA2RGB30PMFromARGB32PM_lsx(uchar *dest, const uint *src, int index, int count,
1149 const QList<QRgb> *, QDitherInfo *)
1150{
1151 uint *d = reinterpret_cast<uint *>(dest) + index;
1152 for (int i = 0; i < count; ++i)
1153 d[i] = qConvertArgb32ToA2rgb30_lsx<PixelOrder>(src[i]);
1154}
1155
1156#if QT_CONFIG(raster_64bit)
1157void QT_FASTCALL destStore64ARGB32_lsx(QRasterBuffer *rasterBuffer, int x,
1158 int y, const QRgba64 *buffer, int length)
1159{
1160 uint *dest = (uint*)rasterBuffer->scanLine(y) + x;
1161 convertARGBFromRGBA64PM_lsx<false>(dest, buffer, length);
1162}
1163
1164void QT_FASTCALL destStore64RGBA8888_lsx(QRasterBuffer *rasterBuffer, int x,
1165 int y, const QRgba64 *buffer, int length)
1166{
1167 uint *dest = (uint*)rasterBuffer->scanLine(y) + x;
1168 convertARGBFromRGBA64PM_lsx<true>(dest, buffer, length);
1169}
1170#endif
1171
1172void QT_FASTCALL storeARGB32FromRGBA64PM_lsx(uchar *dest, const QRgba64 *src, int index, int count,
1173 const QList<QRgb> *, QDitherInfo *)
1174{
1175 uint *d = (uint*)dest + index;
1176 convertARGBFromRGBA64PM_lsx<false>(d, src, count);
1177}
1178
1179void QT_FASTCALL storeRGBA8888FromRGBA64PM_lsx(uchar *dest, const QRgba64 *src, int index, int count,
1180 const QList<QRgb> *, QDitherInfo *)
1181{
1182 uint *d = (uint*)dest + index;
1183 convertARGBFromRGBA64PM_lsx<true>(d, src, count);
1184}
1185
1186template
1187void QT_FASTCALL storeA2RGB30PMFromARGB32PM_lsx<PixelOrderBGR>(uchar *dest, const uint *src, int index, int count,
1188 const QList<QRgb> *, QDitherInfo *);
1189template
1190void QT_FASTCALL storeA2RGB30PMFromARGB32PM_lsx<PixelOrderRGB>(uchar *dest, const uint *src, int index, int count,
1191 const QList<QRgb> *, QDitherInfo *);
1192
1193void QT_FASTCALL storeRGBA64FromRGBA64PM_lsx(uchar *dest, const QRgba64 *src, int index, int count,
1194 const QList<QRgb> *, QDitherInfo *)
1195{
1196 QRgba64 *d = (QRgba64 *)dest + index;
1197 convertRGBA64FromRGBA64PM_lsx<false>(d, src, count);
1198}
1199
1200void QT_FASTCALL storeRGBx64FromRGBA64PM_lsx(uchar *dest, const QRgba64 *src, int index, int count,
1201 const QList<QRgb> *, QDitherInfo *)
1202{
1203 QRgba64 *d = (QRgba64 *)dest + index;
1204 convertRGBA64FromRGBA64PM_lsx<true>(d, src, count);
1205}
1206
1207#if QT_CONFIG(raster_fp)
1208const QRgbaFloat32 *QT_FASTCALL fetchRGBA32FToRGBA32F_lsx(QRgbaFloat32 *buffer, const uchar *src,
1209 int index, int count,
1210 const QList<QRgb> *, QDitherInfo *)
1211{
1212 const QRgbaFloat32 *s = reinterpret_cast<const QRgbaFloat32 *>(src) + index;
1213 for (int i = 0; i < count; ++i) {
1214 __m128 vsf = (__m128)__lsx_vld(reinterpret_cast<const float *>(s + i), 0);
1215 __m128 vsa = (__m128)__lsx_vreplvei_w(vsf, 3);
1216 vsf = __lsx_vfmul_s(vsf, vsa);
1217 vsf = (__m128)__lsx_vextrins_w(vsf, vsa, 0x30);
1218 __lsx_vst(vsf, reinterpret_cast<float *>(buffer + i), 0);
1219 }
1220 return buffer;
1221}
1222
1223void QT_FASTCALL storeRGBX32FFromRGBA32F_lsx(uchar *dest, const QRgbaFloat32 *src,
1224 int index, int count,
1225 const QList<QRgb> *, QDitherInfo *)
1226{
1227 QRgbaFloat32 *d = reinterpret_cast<QRgbaFloat32 *>(dest) + index;
1228 const __m128 zero = (__m128)(v4f32){0.0f, 0.0f, 0.0f, 1.0f};
1229 for (int i = 0; i < count; ++i) {
1230 __m128 vsf = (__m128)__lsx_vld(reinterpret_cast<const float *>(src + i), 0);
1231 const __m128 vsa = (__m128)__lsx_vreplvei_w(vsf, 3);
1232 FloatInt a;
1233 a.i = __lsx_vpickve2gr_w(vsa, 0);
1234 if (a.f == 1.0f)
1235 { }
1236 else if (a.f == 0.0f)
1237 vsf = zero;
1238 else {
1239 __m128 vsr = __lsx_vfrecip_s(vsa);
1240 vsr = __lsx_vfsub_s(__lsx_vfadd_s(vsr, vsr),
1241 __lsx_vfmul_s(vsr, __lsx_vfmul_s(vsr, vsa)));
1242 vsf = __lsx_vfmul_s(vsf, vsr);
1243 FloatInt b = {.f = 1.0f};
1244 vsf = (__m128)__lsx_vinsgr2vr_w(vsf, b.i, 3);
1245 }
1246 __lsx_vst(vsf, reinterpret_cast<float *>(d + i), 0);
1247 }
1248}
1249
1250void QT_FASTCALL storeRGBA32FFromRGBA32F_lsx(uchar *dest, const QRgbaFloat32 *src,
1251 int index, int count,
1252 const QList<QRgb> *, QDitherInfo *)
1253{
1254 QRgbaFloat32 *d = reinterpret_cast<QRgbaFloat32 *>(dest) + index;
1255 const __m128 zero = (__m128)__lsx_vldi(0);
1256 for (int i = 0; i < count; ++i) {
1257 __m128 vsf = (__m128)__lsx_vld(reinterpret_cast<const float *>(src + i), 0);
1258 const __m128 vsa = (__m128)__lsx_vreplvei_w(vsf, 3);
1259 FloatInt a;
1260 a.i = __lsx_vpickve2gr_w(vsa, 0);
1261 if (a.f == 1.0f)
1262 { }
1263 else if (a.f == 0.0f)
1264 vsf = zero;
1265 else {
1266 __m128 vsr = __lsx_vfrecip_s(vsa);
1267 vsr = __lsx_vfsub_s(__lsx_vfadd_s(vsr, vsr),
1268 __lsx_vfmul_s(vsr, __lsx_vfmul_s(vsr, vsa)));
1269 FloatInt b = {.f = 1.0f};
1270 vsr = (__m128)__lsx_vinsgr2vr_w(vsr, b.i, 3);
1271 vsf = __lsx_vfmul_s(vsf, vsr);
1272 }
1273 __lsx_vst(vsf, reinterpret_cast<float *>(d + i), 0);
1274 }
1275}
1276#endif
1277
1278QT_END_NAMESPACE
1279
1280#endif // QT_COMPILER_SUPPORTS_LSX