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_neon.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include <private/qdrawhelper_neon_p.h>
5#include <private/qblendfunctions_p.h>
6#include <private/qmath_p.h>
7#include <private/qpixellayout_p.h>
8
9#ifdef __ARM_NEON__
10
11#include <private/qpaintengine_raster_p.h>
12
13QT_BEGIN_NAMESPACE
14
15void qt_memfill32(quint32 *dest, quint32 value, qsizetype count)
16{
17 const int epilogueSize = count % 16;
18#if defined(Q_CC_GHS) || defined(Q_CC_MSVC)
19 // inline assembler free version:
20 if (count >= 16) {
21 quint32 *const neonEnd = dest + count - epilogueSize;
22 const uint32x4_t valueVector1 = vdupq_n_u32(value);
23 const uint32x4x4_t valueVector4 = { valueVector1, valueVector1, valueVector1, valueVector1 };
24 do {
25 vst4q_u32(dest, valueVector4);
26 dest += 16;
27 } while (dest != neonEnd);
28 }
29#elif !defined(Q_PROCESSOR_ARM_64)
30 if (count >= 16) {
31 quint32 *const neonEnd = dest + count - epilogueSize;
32 register uint32x4_t valueVector1 asm ("q0") = vdupq_n_u32(value);
33 register uint32x4_t valueVector2 asm ("q1") = valueVector1;
34 while (dest != neonEnd) {
35 asm volatile (
36 "vst2.32 { d0, d1, d2, d3 }, [%[DST]] !\n\t"
37 "vst2.32 { d0, d1, d2, d3 }, [%[DST]] !\n\t"
38 : [DST]"+r" (dest)
39 : [VALUE1]"w"(valueVector1), [VALUE2]"w"(valueVector2)
40 : "memory"
41 );
42 }
43 }
44#else
45 if (count >= 16) {
46 quint32 *const neonEnd = dest + count - epilogueSize;
47 register uint32x4_t valueVector1 asm ("v0") = vdupq_n_u32(value);
48 register uint32x4_t valueVector2 asm ("v1") = valueVector1;
49 while (dest != neonEnd) {
50 asm volatile (
51 "st2 { v0.4s, v1.4s }, [%[DST]], #32 \n\t"
52 "st2 { v0.4s, v1.4s }, [%[DST]], #32 \n\t"
53 : [DST]"+r" (dest)
54 : [VALUE1]"w"(valueVector1), [VALUE2]"w"(valueVector2)
55 : "memory"
56 );
57 }
58 }
59#endif
60
61 switch (epilogueSize)
62 {
63 case 15: *dest++ = value; Q_FALLTHROUGH();
64 case 14: *dest++ = value; Q_FALLTHROUGH();
65 case 13: *dest++ = value; Q_FALLTHROUGH();
66 case 12: *dest++ = value; Q_FALLTHROUGH();
67 case 11: *dest++ = value; Q_FALLTHROUGH();
68 case 10: *dest++ = value; Q_FALLTHROUGH();
69 case 9: *dest++ = value; Q_FALLTHROUGH();
70 case 8: *dest++ = value; Q_FALLTHROUGH();
71 case 7: *dest++ = value; Q_FALLTHROUGH();
72 case 6: *dest++ = value; Q_FALLTHROUGH();
73 case 5: *dest++ = value; Q_FALLTHROUGH();
74 case 4: *dest++ = value; Q_FALLTHROUGH();
75 case 3: *dest++ = value; Q_FALLTHROUGH();
76 case 2: *dest++ = value; Q_FALLTHROUGH();
77 case 1: *dest++ = value;
78 }
79}
80
81static inline uint16x8_t qvdiv_255_u16(uint16x8_t x, uint16x8_t half)
82{
83 // result = (x + (x >> 8) + 0x80) >> 8
84
85 const uint16x8_t temp = vshrq_n_u16(x, 8); // x >> 8
86 const uint16x8_t sum_part = vaddq_u16(x, half); // x + 0x80
87 const uint16x8_t sum = vaddq_u16(temp, sum_part);
88
89 return vshrq_n_u16(sum, 8);
90}
91
92static inline uint16x8_t qvbyte_mul_u16(uint16x8_t x, uint16x8_t alpha, uint16x8_t half)
93{
94 // t = qRound(x * alpha / 255.0)
95
96 const uint16x8_t t = vmulq_u16(x, alpha); // t
97 return qvdiv_255_u16(t, half);
98}
99
100static inline uint16x8_t qvinterpolate_pixel_255(uint16x8_t x, uint16x8_t a, uint16x8_t y, uint16x8_t b, uint16x8_t half)
101{
102 // t = x * a + y * b
103
104 const uint16x8_t ta = vmulq_u16(x, a);
105 const uint16x8_t tb = vmulq_u16(y, b);
106
107 return qvdiv_255_u16(vaddq_u16(ta, tb), half);
108}
109
110static inline uint16x8_t qvsource_over_u16(uint16x8_t src16, uint16x8_t dst16, uint16x8_t half, uint16x8_t full)
111{
112 const uint16x4_t alpha16_high = vdup_lane_u16(vget_high_u16(src16), 3);
113 const uint16x4_t alpha16_low = vdup_lane_u16(vget_low_u16(src16), 3);
114
115 const uint16x8_t alpha16 = vsubq_u16(full, vcombine_u16(alpha16_low, alpha16_high));
116
117 return vaddq_u16(src16, qvbyte_mul_u16(dst16, alpha16, half));
118}
119
120#if defined(ENABLE_PIXMAN_DRAWHELPERS)
121extern "C" void
122pixman_composite_over_8888_0565_asm_neon (int32_t w,
123 int32_t h,
124 uint16_t *dst,
125 int32_t dst_stride,
126 uint32_t *src,
127 int32_t src_stride);
128
129extern "C" void
130pixman_composite_over_8888_8888_asm_neon (int32_t w,
131 int32_t h,
132 uint32_t *dst,
133 int32_t dst_stride,
134 uint32_t *src,
135 int32_t src_stride);
136
137extern "C" void
138pixman_composite_src_0565_8888_asm_neon (int32_t w,
139 int32_t h,
140 uint32_t *dst,
141 int32_t dst_stride,
142 uint16_t *src,
143 int32_t src_stride);
144
145extern "C" void
146pixman_composite_over_n_8_0565_asm_neon (int32_t w,
147 int32_t h,
148 uint16_t *dst,
149 int32_t dst_stride,
150 uint32_t src,
151 int32_t unused,
152 uint8_t *mask,
153 int32_t mask_stride);
154
155extern "C" void
156pixman_composite_scanline_over_asm_neon (int32_t w,
157 const uint32_t *dst,
158 const uint32_t *src);
159
160extern "C" void
161pixman_composite_src_0565_0565_asm_neon (int32_t w,
162 int32_t h,
163 uint16_t *dst,
164 int32_t dst_stride,
165 uint16_t *src,
166 int32_t src_stride);
167// qblendfunctions.cpp
168void qt_blend_argb32_on_rgb16_const_alpha(uchar *destPixels, int dbpl,
169 const uchar *srcPixels, int sbpl,
170 int w, int h,
171 int const_alpha);
172
173void qt_blend_rgb16_on_argb32_neon(uchar *destPixels, int dbpl,
174 const uchar *srcPixels, int sbpl,
175 int w, int h,
176 int const_alpha)
177{
178 dbpl /= 4;
179 sbpl /= 2;
180
181 quint32 *dst = (quint32 *) destPixels;
182 quint16 *src = (quint16 *) srcPixels;
183
184 if (const_alpha != 256) {
185 quint8 a = (255 * const_alpha) >> 8;
186 quint8 ia = 255 - a;
187
188 while (--h >= 0) {
189 for (int x=0; x<w; ++x)
190 dst[x] = INTERPOLATE_PIXEL_255(qConvertRgb16To32(src[x]), a, dst[x], ia);
191 dst += dbpl;
192 src += sbpl;
193 }
194 return;
195 }
196
197 pixman_composite_src_0565_8888_asm_neon(w, h, dst, dbpl, src, sbpl);
198}
199
200// qblendfunctions.cpp
201void qt_blend_rgb16_on_rgb16(uchar *dst, int dbpl,
202 const uchar *src, int sbpl,
203 int w, int h,
204 int const_alpha);
205
206
207template <int N>
208static inline void scanLineBlit16(quint16 *dst, quint16 *src, int dstride)
209{
210 if (N >= 2) {
211 ((quint32 *)dst)[0] = ((quint32 *)src)[0];
212 __builtin_prefetch(dst + dstride, 1, 0);
213 }
214 for (int i = 1; i < N/2; ++i)
215 ((quint32 *)dst)[i] = ((quint32 *)src)[i];
216 if (N & 1)
217 dst[N-1] = src[N-1];
218}
219
220template <int Width>
221static inline void blockBlit16(quint16 *dst, quint16 *src, int dstride, int sstride, int h)
222{
223 union {
224 quintptr address;
225 quint16 *pointer;
226 } u;
227
228 u.pointer = dst;
229
230 if (u.address & 2) {
231 while (--h >= 0) {
232 // align dst
233 dst[0] = src[0];
234 if (Width > 1)
235 scanLineBlit16<Width-1>(dst + 1, src + 1, dstride);
236 dst += dstride;
237 src += sstride;
238 }
239 } else {
240 while (--h >= 0) {
241 scanLineBlit16<Width>(dst, src, dstride);
242
243 dst += dstride;
244 src += sstride;
245 }
246 }
247}
248
249void qt_blend_rgb16_on_rgb16_neon(uchar *destPixels, int dbpl,
250 const uchar *srcPixels, int sbpl,
251 int w, int h,
252 int const_alpha)
253{
254 // testing show that the default memcpy is faster for widths 150 and up
255 if (const_alpha != 256 || w >= 150) {
256 qt_blend_rgb16_on_rgb16(destPixels, dbpl, srcPixels, sbpl, w, h, const_alpha);
257 return;
258 }
259
260 int dstride = dbpl / 2;
261 int sstride = sbpl / 2;
262
263 quint16 *dst = (quint16 *) destPixels;
264 quint16 *src = (quint16 *) srcPixels;
265
266 switch (w) {
267#define BLOCKBLIT(n) case n: blockBlit16<n>(dst, src, dstride, sstride, h); return;
268 BLOCKBLIT(1);
269 BLOCKBLIT(2);
270 BLOCKBLIT(3);
271 BLOCKBLIT(4);
272 BLOCKBLIT(5);
273 BLOCKBLIT(6);
274 BLOCKBLIT(7);
275 BLOCKBLIT(8);
276 BLOCKBLIT(9);
277 BLOCKBLIT(10);
278 BLOCKBLIT(11);
279 BLOCKBLIT(12);
280 BLOCKBLIT(13);
281 BLOCKBLIT(14);
282 BLOCKBLIT(15);
283#undef BLOCKBLIT
284 default:
285 break;
286 }
287
288 pixman_composite_src_0565_0565_asm_neon (w, h, dst, dstride, src, sstride);
289}
290
291extern "C" void blend_8_pixels_argb32_on_rgb16_neon(quint16 *dst, const quint32 *src, int const_alpha);
292
293void qt_blend_argb32_on_rgb16_neon(uchar *destPixels, int dbpl,
294 const uchar *srcPixels, int sbpl,
295 int w, int h,
296 int const_alpha)
297{
298 quint16 *dst = (quint16 *) destPixels;
299 quint32 *src = (quint32 *) srcPixels;
300
301 if (const_alpha != 256) {
302 for (int y=0; y<h; ++y) {
303 int i = 0;
304 for (; i < w-7; i += 8)
305 blend_8_pixels_argb32_on_rgb16_neon(&dst[i], &src[i], const_alpha);
306
307 if (i < w) {
308 int tail = w - i;
309
310 quint16 dstBuffer[8];
311 quint32 srcBuffer[8];
312
313 for (int j = 0; j < tail; ++j) {
314 dstBuffer[j] = dst[i + j];
315 srcBuffer[j] = src[i + j];
316 }
317
318 blend_8_pixels_argb32_on_rgb16_neon(dstBuffer, srcBuffer, const_alpha);
319
320 for (int j = 0; j < tail; ++j)
321 dst[i + j] = dstBuffer[j];
322 }
323
324 dst = (quint16 *)(((uchar *) dst) + dbpl);
325 src = (quint32 *)(((uchar *) src) + sbpl);
326 }
327 return;
328 }
329
330 pixman_composite_over_8888_0565_asm_neon(w, h, dst, dbpl / 2, src, sbpl / 4);
331}
332#endif
333
334void qt_blend_argb32_on_argb32_scanline_neon(uint *dest, const uint *src, int length, uint const_alpha)
335{
336 if (const_alpha == 255) {
337#if defined(ENABLE_PIXMAN_DRAWHELPERS)
338 pixman_composite_scanline_over_asm_neon(length, dest, src);
339#else
340 qt_blend_argb32_on_argb32_neon((uchar *)dest, 4 * length, (uchar *)src, 4 * length, length, 1, 256);
341#endif
342 } else {
343 qt_blend_argb32_on_argb32_neon((uchar *)dest, 4 * length, (uchar *)src, 4 * length, length, 1, (const_alpha * 256) / 255);
344 }
345}
346
347void qt_blend_argb32_on_argb32_neon(uchar *destPixels, int dbpl,
348 const uchar *srcPixels, int sbpl,
349 int w, int h,
350 int const_alpha)
351{
352 const uint *src = (const uint *) srcPixels;
353 uint *dst = (uint *) destPixels;
354 uint16x8_t half = vdupq_n_u16(0x80);
355 uint16x8_t full = vdupq_n_u16(0xff);
356 if (const_alpha == 256) {
357#if defined(ENABLE_PIXMAN_DRAWHELPERS)
358 pixman_composite_over_8888_8888_asm_neon(w, h, (uint32_t *)destPixels, dbpl / 4, (uint32_t *)srcPixels, sbpl / 4);
359#else
360 for (int y=0; y<h; ++y) {
361 int x = 0;
362 for (; x < w-3; x += 4) {
363 if (src[x] | src[x+1] | src[x+2] | src[x+3]) {
364 uint32x4_t src32 = vld1q_u32((uint32_t *)&src[x]);
365 uint32x4_t dst32 = vld1q_u32((uint32_t *)&dst[x]);
366
367 const uint8x16_t src8 = vreinterpretq_u8_u32(src32);
368 const uint8x16_t dst8 = vreinterpretq_u8_u32(dst32);
369
370 const uint8x8_t src8_low = vget_low_u8(src8);
371 const uint8x8_t dst8_low = vget_low_u8(dst8);
372
373 const uint8x8_t src8_high = vget_high_u8(src8);
374 const uint8x8_t dst8_high = vget_high_u8(dst8);
375
376 const uint16x8_t src16_low = vmovl_u8(src8_low);
377 const uint16x8_t dst16_low = vmovl_u8(dst8_low);
378
379 const uint16x8_t src16_high = vmovl_u8(src8_high);
380 const uint16x8_t dst16_high = vmovl_u8(dst8_high);
381
382 const uint16x8_t result16_low = qvsource_over_u16(src16_low, dst16_low, half, full);
383 const uint16x8_t result16_high = qvsource_over_u16(src16_high, dst16_high, half, full);
384
385 const uint32x2_t result32_low = vreinterpret_u32_u8(vmovn_u16(result16_low));
386 const uint32x2_t result32_high = vreinterpret_u32_u8(vmovn_u16(result16_high));
387
388 vst1q_u32((uint32_t *)&dst[x], vcombine_u32(result32_low, result32_high));
389 }
390 }
391 for (; x<w; ++x) {
392 uint s = src[x];
393 if (s >= 0xff000000)
394 dst[x] = s;
395 else if (s != 0)
396 dst[x] = s + BYTE_MUL(dst[x], qAlpha(~s));
397 }
398 dst = (quint32 *)(((uchar *) dst) + dbpl);
399 src = (const quint32 *)(((const uchar *) src) + sbpl);
400 }
401#endif
402 } else if (const_alpha != 0) {
403 const_alpha = (const_alpha * 255) >> 8;
404 uint16x8_t const_alpha16 = vdupq_n_u16(const_alpha);
405 for (int y = 0; y < h; ++y) {
406 int x = 0;
407 for (; x < w-3; x += 4) {
408 if (src[x] | src[x+1] | src[x+2] | src[x+3]) {
409 uint32x4_t src32 = vld1q_u32((uint32_t *)&src[x]);
410 uint32x4_t dst32 = vld1q_u32((uint32_t *)&dst[x]);
411
412 const uint8x16_t src8 = vreinterpretq_u8_u32(src32);
413 const uint8x16_t dst8 = vreinterpretq_u8_u32(dst32);
414
415 const uint8x8_t src8_low = vget_low_u8(src8);
416 const uint8x8_t dst8_low = vget_low_u8(dst8);
417
418 const uint8x8_t src8_high = vget_high_u8(src8);
419 const uint8x8_t dst8_high = vget_high_u8(dst8);
420
421 const uint16x8_t src16_low = vmovl_u8(src8_low);
422 const uint16x8_t dst16_low = vmovl_u8(dst8_low);
423
424 const uint16x8_t src16_high = vmovl_u8(src8_high);
425 const uint16x8_t dst16_high = vmovl_u8(dst8_high);
426
427 const uint16x8_t srcalpha16_low = qvbyte_mul_u16(src16_low, const_alpha16, half);
428 const uint16x8_t srcalpha16_high = qvbyte_mul_u16(src16_high, const_alpha16, half);
429
430 const uint16x8_t result16_low = qvsource_over_u16(srcalpha16_low, dst16_low, half, full);
431 const uint16x8_t result16_high = qvsource_over_u16(srcalpha16_high, dst16_high, half, full);
432
433 const uint32x2_t result32_low = vreinterpret_u32_u8(vmovn_u16(result16_low));
434 const uint32x2_t result32_high = vreinterpret_u32_u8(vmovn_u16(result16_high));
435
436 vst1q_u32((uint32_t *)&dst[x], vcombine_u32(result32_low, result32_high));
437 }
438 }
439 for (; x<w; ++x) {
440 uint s = src[x];
441 if (s != 0) {
442 s = BYTE_MUL(s, const_alpha);
443 dst[x] = s + BYTE_MUL(dst[x], qAlpha(~s));
444 }
445 }
446 dst = (quint32 *)(((uchar *) dst) + dbpl);
447 src = (const quint32 *)(((const uchar *) src) + sbpl);
448 }
449 }
450}
451
452// qblendfunctions.cpp
453void qt_blend_rgb32_on_rgb32(uchar *destPixels, int dbpl,
454 const uchar *srcPixels, int sbpl,
455 int w, int h,
456 int const_alpha);
457
458void qt_blend_rgb32_on_rgb32_neon(uchar *destPixels, int dbpl,
459 const uchar *srcPixels, int sbpl,
460 int w, int h,
461 int const_alpha)
462{
463 if (const_alpha != 256) {
464 if (const_alpha != 0) {
465 const uint *src = (const uint *) srcPixels;
466 uint *dst = (uint *) destPixels;
467 uint16x8_t half = vdupq_n_u16(0x80);
468 const_alpha = (const_alpha * 255) >> 8;
469 int one_minus_const_alpha = 255 - const_alpha;
470 uint16x8_t const_alpha16 = vdupq_n_u16(const_alpha);
471 uint16x8_t one_minus_const_alpha16 = vdupq_n_u16(255 - const_alpha);
472 for (int y = 0; y < h; ++y) {
473 int x = 0;
474 for (; x < w-3; x += 4) {
475 uint32x4_t src32 = vld1q_u32((uint32_t *)&src[x]);
476 uint32x4_t dst32 = vld1q_u32((uint32_t *)&dst[x]);
477
478 const uint8x16_t src8 = vreinterpretq_u8_u32(src32);
479 const uint8x16_t dst8 = vreinterpretq_u8_u32(dst32);
480
481 const uint8x8_t src8_low = vget_low_u8(src8);
482 const uint8x8_t dst8_low = vget_low_u8(dst8);
483
484 const uint8x8_t src8_high = vget_high_u8(src8);
485 const uint8x8_t dst8_high = vget_high_u8(dst8);
486
487 const uint16x8_t src16_low = vmovl_u8(src8_low);
488 const uint16x8_t dst16_low = vmovl_u8(dst8_low);
489
490 const uint16x8_t src16_high = vmovl_u8(src8_high);
491 const uint16x8_t dst16_high = vmovl_u8(dst8_high);
492
493 const uint16x8_t result16_low = qvinterpolate_pixel_255(src16_low, const_alpha16, dst16_low, one_minus_const_alpha16, half);
494 const uint16x8_t result16_high = qvinterpolate_pixel_255(src16_high, const_alpha16, dst16_high, one_minus_const_alpha16, half);
495
496 const uint32x2_t result32_low = vreinterpret_u32_u8(vmovn_u16(result16_low));
497 const uint32x2_t result32_high = vreinterpret_u32_u8(vmovn_u16(result16_high));
498
499 vst1q_u32((uint32_t *)&dst[x], vcombine_u32(result32_low, result32_high));
500 }
501 for (; x<w; ++x) {
502 dst[x] = INTERPOLATE_PIXEL_255(src[x], const_alpha, dst[x], one_minus_const_alpha);
503 }
504 dst = (quint32 *)(((uchar *) dst) + dbpl);
505 src = (const quint32 *)(((const uchar *) src) + sbpl);
506 }
507 }
508 } else {
509 qt_blend_rgb32_on_rgb32(destPixels, dbpl, srcPixels, sbpl, w, h, const_alpha);
510 }
511}
512
513#if defined(ENABLE_PIXMAN_DRAWHELPERS)
514extern void qt_alphamapblit_quint16(QRasterBuffer *rasterBuffer,
515 int x, int y, const QRgba64 &color,
516 const uchar *map,
517 int mapWidth, int mapHeight, int mapStride,
518 const QClipData *clip, bool useGammaCorrection);
519
520void qt_alphamapblit_quint16_neon(QRasterBuffer *rasterBuffer,
521 int x, int y, const QRgba64 &color,
522 const uchar *bitmap,
523 int mapWidth, int mapHeight, int mapStride,
524 const QClipData *clip, bool useGammaCorrection)
525{
526 if (clip || useGammaCorrection) {
527 qt_alphamapblit_quint16(rasterBuffer, x, y, color, bitmap, mapWidth, mapHeight, mapStride, clip, useGammaCorrection);
528 return;
529 }
530
531 quint16 *dest = reinterpret_cast<quint16*>(rasterBuffer->scanLine(y)) + x;
532 const int destStride = rasterBuffer->bytesPerLine() / sizeof(quint16);
533
534 uchar *mask = const_cast<uchar *>(bitmap);
535 const uint c = color.toArgb32();
536
537 pixman_composite_over_n_8_0565_asm_neon(mapWidth, mapHeight, dest, destStride, c, 0, mask, mapStride);
538}
539
540extern "C" void blend_8_pixels_rgb16_on_rgb16_neon(quint16 *dst, const quint16 *src, int const_alpha);
541
542template <typename SRC, typename BlendFunc>
543struct Blend_on_RGB16_SourceAndConstAlpha_Neon {
544 Blend_on_RGB16_SourceAndConstAlpha_Neon(BlendFunc blender, int const_alpha)
545 : m_index(0)
546 , m_blender(blender)
547 , m_const_alpha(const_alpha)
548 {
549 }
550
551 inline void write(quint16 *dst, quint32 src)
552 {
553 srcBuffer[m_index++] = src;
554
555 if (m_index == 8) {
556 m_blender(dst - 7, srcBuffer, m_const_alpha);
557 m_index = 0;
558 }
559 }
560
561 inline void flush(quint16 *dst)
562 {
563 if (m_index > 0) {
564 quint16 dstBuffer[8];
565 for (int i = 0; i < m_index; ++i)
566 dstBuffer[i] = dst[i - m_index];
567
568 m_blender(dstBuffer, srcBuffer, m_const_alpha);
569
570 for (int i = 0; i < m_index; ++i)
571 dst[i - m_index] = dstBuffer[i];
572
573 m_index = 0;
574 }
575 }
576
577 SRC srcBuffer[8];
578
579 int m_index;
580 BlendFunc m_blender;
581 int m_const_alpha;
582};
583
584template <typename SRC, typename BlendFunc>
585Blend_on_RGB16_SourceAndConstAlpha_Neon<SRC, BlendFunc>
586Blend_on_RGB16_SourceAndConstAlpha_Neon_create(BlendFunc blender, int const_alpha)
587{
588 return Blend_on_RGB16_SourceAndConstAlpha_Neon<SRC, BlendFunc>(blender, const_alpha);
589}
590
591void qt_scale_image_argb32_on_rgb16_neon(uchar *destPixels, int dbpl,
592 const uchar *srcPixels, int sbpl, int srch,
593 const QRectF &targetRect,
594 const QRectF &sourceRect,
595 const QRect &clip,
596 int const_alpha)
597{
598 if (const_alpha == 0)
599 return;
600
601 qt_scale_image_16bit<quint32>(destPixels, dbpl, srcPixels, sbpl, srch, targetRect, sourceRect, clip,
602 Blend_on_RGB16_SourceAndConstAlpha_Neon_create<quint32>(blend_8_pixels_argb32_on_rgb16_neon, const_alpha));
603}
604
605void qt_scale_image_rgb16_on_rgb16(uchar *destPixels, int dbpl,
606 const uchar *srcPixels, int sbpl, int srch,
607 const QRectF &targetRect,
608 const QRectF &sourceRect,
609 const QRect &clip,
610 int const_alpha);
611
612void qt_scale_image_rgb16_on_rgb16_neon(uchar *destPixels, int dbpl,
613 const uchar *srcPixels, int sbpl, int srch,
614 const QRectF &targetRect,
615 const QRectF &sourceRect,
616 const QRect &clip,
617 int const_alpha)
618{
619 if (const_alpha == 0)
620 return;
621
622 if (const_alpha == 256) {
623 qt_scale_image_rgb16_on_rgb16(destPixels, dbpl, srcPixels, sbpl, srch, targetRect, sourceRect, clip, const_alpha);
624 return;
625 }
626
627 qt_scale_image_16bit<quint16>(destPixels, dbpl, srcPixels, sbpl, srch, targetRect, sourceRect, clip,
628 Blend_on_RGB16_SourceAndConstAlpha_Neon_create<quint16>(blend_8_pixels_rgb16_on_rgb16_neon, const_alpha));
629}
630
631extern void qt_transform_image_rgb16_on_rgb16(uchar *destPixels, int dbpl,
632 const uchar *srcPixels, int sbpl,
633 const QRectF &targetRect,
634 const QRectF &sourceRect,
635 const QRect &clip,
636 const QTransform &targetRectTransform,
637 int const_alpha);
638
639void qt_transform_image_rgb16_on_rgb16_neon(uchar *destPixels, int dbpl,
640 const uchar *srcPixels, int sbpl,
641 const QRectF &targetRect,
642 const QRectF &sourceRect,
643 const QRect &clip,
644 const QTransform &targetRectTransform,
645 int const_alpha)
646{
647 if (const_alpha == 0)
648 return;
649
650 if (const_alpha == 256) {
651 qt_transform_image_rgb16_on_rgb16(destPixels, dbpl, srcPixels, sbpl, targetRect, sourceRect, clip, targetRectTransform, const_alpha);
652 return;
653 }
654
655 qt_transform_image(reinterpret_cast<quint16 *>(destPixels), dbpl,
656 reinterpret_cast<const quint16 *>(srcPixels), sbpl, targetRect, sourceRect, clip, targetRectTransform,
657 Blend_on_RGB16_SourceAndConstAlpha_Neon_create<quint16>(blend_8_pixels_rgb16_on_rgb16_neon, const_alpha));
658}
659
660void qt_transform_image_argb32_on_rgb16_neon(uchar *destPixels, int dbpl,
661 const uchar *srcPixels, int sbpl,
662 const QRectF &targetRect,
663 const QRectF &sourceRect,
664 const QRect &clip,
665 const QTransform &targetRectTransform,
666 int const_alpha)
667{
668 if (const_alpha == 0)
669 return;
670
671 qt_transform_image(reinterpret_cast<quint16 *>(destPixels), dbpl,
672 reinterpret_cast<const quint32 *>(srcPixels), sbpl, targetRect, sourceRect, clip, targetRectTransform,
673 Blend_on_RGB16_SourceAndConstAlpha_Neon_create<quint32>(blend_8_pixels_argb32_on_rgb16_neon, const_alpha));
674}
675
676static inline void convert_8_pixels_rgb16_to_argb32(quint32 *dst, const quint16 *src)
677{
678 asm volatile (
679 "vld1.16 { d0, d1 }, [%[SRC]]\n\t"
680
681 /* convert 8 r5g6b5 pixel data from {d0, d1} to planar 8-bit format
682 and put data into d4 - red, d3 - green, d2 - blue */
683 "vshrn.u16 d4, q0, #8\n\t"
684 "vshrn.u16 d3, q0, #3\n\t"
685 "vsli.u16 q0, q0, #5\n\t"
686 "vsri.u8 d4, d4, #5\n\t"
687 "vsri.u8 d3, d3, #6\n\t"
688 "vshrn.u16 d2, q0, #2\n\t"
689
690 /* fill d5 - alpha with 0xff */
691 "mov r2, #255\n\t"
692 "vdup.8 d5, r2\n\t"
693
694 "vst4.8 { d2, d3, d4, d5 }, [%[DST]]"
695 : : [DST]"r" (dst), [SRC]"r" (src)
696 : "memory", "r2", "d0", "d1", "d2", "d3", "d4", "d5"
697 );
698}
699
700uint * QT_FASTCALL qt_destFetchRGB16_neon(uint *buffer, QRasterBuffer *rasterBuffer, int x, int y, int length)
701{
702 const ushort *data = (const ushort *)rasterBuffer->scanLine(y) + x;
703
704 int i = 0;
705 for (; i < length - 7; i += 8)
706 convert_8_pixels_rgb16_to_argb32(&buffer[i], &data[i]);
707
708 if (i < length) {
709 quint16 srcBuffer[8];
710 quint32 dstBuffer[8];
711
712 int tail = length - i;
713 for (int j = 0; j < tail; ++j)
714 srcBuffer[j] = data[i + j];
715
716 convert_8_pixels_rgb16_to_argb32(dstBuffer, srcBuffer);
717
718 for (int j = 0; j < tail; ++j)
719 buffer[i + j] = dstBuffer[j];
720 }
721
722 return buffer;
723}
724
725static inline void convert_8_pixels_argb32_to_rgb16(quint16 *dst, const quint32 *src)
726{
727 asm volatile (
728 "vld4.8 { d0, d1, d2, d3 }, [%[SRC]]\n\t"
729
730 /* convert to r5g6b5 and store it into {d28, d29} */
731 "vshll.u8 q14, d2, #8\n\t"
732 "vshll.u8 q8, d1, #8\n\t"
733 "vshll.u8 q9, d0, #8\n\t"
734 "vsri.u16 q14, q8, #5\n\t"
735 "vsri.u16 q14, q9, #11\n\t"
736
737 "vst1.16 { d28, d29 }, [%[DST]]"
738 : : [DST]"r" (dst), [SRC]"r" (src)
739 : "memory", "d0", "d1", "d2", "d3", "d16", "d17", "d18", "d19", "d28", "d29"
740 );
741}
742
743void QT_FASTCALL qt_destStoreRGB16_neon(QRasterBuffer *rasterBuffer, int x, int y, const uint *buffer, int length)
744{
745 quint16 *data = (quint16*)rasterBuffer->scanLine(y) + x;
746
747 int i = 0;
748 for (; i < length - 7; i += 8)
749 convert_8_pixels_argb32_to_rgb16(&data[i], &buffer[i]);
750
751 if (i < length) {
752 quint32 srcBuffer[8];
753 quint16 dstBuffer[8];
754
755 int tail = length - i;
756 for (int j = 0; j < tail; ++j)
757 srcBuffer[j] = buffer[i + j];
758
759 convert_8_pixels_argb32_to_rgb16(dstBuffer, srcBuffer);
760
761 for (int j = 0; j < tail; ++j)
762 data[i + j] = dstBuffer[j];
763 }
764}
765#endif
766
767void QT_FASTCALL comp_func_solid_SourceOver_neon(uint *destPixels, int length, uint color, uint const_alpha)
768{
769 if ((const_alpha & qAlpha(color)) == 255) {
770 qt_memfill32(destPixels, color, length);
771 } else {
772 if (const_alpha != 255)
773 color = BYTE_MUL(color, const_alpha);
774
775 const quint32 minusAlphaOfColor = qAlpha(~color);
776 int x = 0;
777
778 uint32_t *dst = (uint32_t *) destPixels;
779 const uint32x4_t colorVector = vdupq_n_u32(color);
780 uint16x8_t half = vdupq_n_u16(0x80);
781 const uint16x8_t minusAlphaOfColorVector = vdupq_n_u16(minusAlphaOfColor);
782
783 for (; x < length-3; x += 4) {
784 uint32x4_t dstVector = vld1q_u32(&dst[x]);
785
786 const uint8x16_t dst8 = vreinterpretq_u8_u32(dstVector);
787
788 const uint8x8_t dst8_low = vget_low_u8(dst8);
789 const uint8x8_t dst8_high = vget_high_u8(dst8);
790
791 const uint16x8_t dst16_low = vmovl_u8(dst8_low);
792 const uint16x8_t dst16_high = vmovl_u8(dst8_high);
793
794 const uint16x8_t result16_low = qvbyte_mul_u16(dst16_low, minusAlphaOfColorVector, half);
795 const uint16x8_t result16_high = qvbyte_mul_u16(dst16_high, minusAlphaOfColorVector, half);
796
797 const uint32x2_t result32_low = vreinterpret_u32_u8(vmovn_u16(result16_low));
798 const uint32x2_t result32_high = vreinterpret_u32_u8(vmovn_u16(result16_high));
799
800 uint32x4_t blendedPixels = vcombine_u32(result32_low, result32_high);
801 uint32x4_t colorPlusBlendedPixels = vaddq_u32(colorVector, blendedPixels);
802 vst1q_u32(&dst[x], colorPlusBlendedPixels);
803 }
804
805 SIMD_EPILOGUE(x, length, 3)
806 destPixels[x] = color + BYTE_MUL(destPixels[x], minusAlphaOfColor);
807 }
808}
809
810void QT_FASTCALL comp_func_Plus_neon(uint *dst, const uint *src, int length, uint const_alpha)
811{
812 if (const_alpha == 255) {
813 uint *const end = dst + length;
814 uint *const neonEnd = end - 3;
815
816 while (dst < neonEnd) {
817 uint8x16_t vs = vld1q_u8((const uint8_t*)src);
818 const uint8x16_t vd = vld1q_u8((uint8_t*)dst);
819 vs = vqaddq_u8(vs, vd);
820 vst1q_u8((uint8_t*)dst, vs);
821 src += 4;
822 dst += 4;
823 };
824
825 while (dst != end) {
826 *dst = comp_func_Plus_one_pixel(*dst, *src);
827 ++dst;
828 ++src;
829 }
830 } else {
831 int x = 0;
832 const int one_minus_const_alpha = 255 - const_alpha;
833 const uint16x8_t constAlphaVector = vdupq_n_u16(const_alpha);
834 const uint16x8_t oneMinusconstAlphaVector = vdupq_n_u16(one_minus_const_alpha);
835
836 const uint16x8_t half = vdupq_n_u16(0x80);
837 for (; x < length - 3; x += 4) {
838 const uint32x4_t src32 = vld1q_u32((uint32_t *)&src[x]);
839 const uint8x16_t src8 = vreinterpretq_u8_u32(src32);
840 uint8x16_t dst8 = vld1q_u8((uint8_t *)&dst[x]);
841 uint8x16_t result = vqaddq_u8(dst8, src8);
842
843 uint16x8_t result_low = vmovl_u8(vget_low_u8(result));
844 uint16x8_t result_high = vmovl_u8(vget_high_u8(result));
845
846 uint16x8_t dst_low = vmovl_u8(vget_low_u8(dst8));
847 uint16x8_t dst_high = vmovl_u8(vget_high_u8(dst8));
848
849 result_low = qvinterpolate_pixel_255(result_low, constAlphaVector, dst_low, oneMinusconstAlphaVector, half);
850 result_high = qvinterpolate_pixel_255(result_high, constAlphaVector, dst_high, oneMinusconstAlphaVector, half);
851
852 const uint32x2_t result32_low = vreinterpret_u32_u8(vmovn_u16(result_low));
853 const uint32x2_t result32_high = vreinterpret_u32_u8(vmovn_u16(result_high));
854 vst1q_u32((uint32_t *)&dst[x], vcombine_u32(result32_low, result32_high));
855 }
856
857 SIMD_EPILOGUE(x, length, 3)
858 dst[x] = comp_func_Plus_one_pixel_const_alpha(dst[x], src[x], const_alpha, one_minus_const_alpha);
859 }
860}
861
862#if defined(ENABLE_PIXMAN_DRAWHELPERS)
863static const int tileSize = 32;
864
865extern "C" void qt_rotate90_16_neon(quint16 *dst, const quint16 *src, int sstride, int dstride, int count);
866
867void qt_memrotate90_16_neon(const uchar *srcPixels, int w, int h, int sstride, uchar *destPixels, int dstride)
868{
869 const ushort *src = (const ushort *)srcPixels;
870 ushort *dest = (ushort *)destPixels;
871
872 sstride /= sizeof(ushort);
873 dstride /= sizeof(ushort);
874
875 const int pack = sizeof(quint32) / sizeof(ushort);
876 const int unaligned =
877 qMin(uint((quintptr(dest) & (sizeof(quint32)-1)) / sizeof(ushort)), uint(h));
878 const int restX = w % tileSize;
879 const int restY = (h - unaligned) % tileSize;
880 const int unoptimizedY = restY % pack;
881 const int numTilesX = w / tileSize + (restX > 0);
882 const int numTilesY = (h - unaligned) / tileSize + (restY >= pack);
883
884 for (int tx = 0; tx < numTilesX; ++tx) {
885 const int startx = w - tx * tileSize - 1;
886 const int stopx = qMax(startx - tileSize, 0);
887
888 if (unaligned) {
889 for (int x = startx; x >= stopx; --x) {
890 ushort *d = dest + (w - x - 1) * dstride;
891 for (int y = 0; y < unaligned; ++y) {
892 *d++ = src[y * sstride + x];
893 }
894 }
895 }
896
897 for (int ty = 0; ty < numTilesY; ++ty) {
898 const int starty = ty * tileSize + unaligned;
899 const int stopy = qMin(starty + tileSize, h - unoptimizedY);
900
901 int x = startx;
902 // qt_rotate90_16_neon writes to eight rows, four pixels at a time
903 for (; x >= stopx + 7; x -= 8) {
904 ushort *d = dest + (w - x - 1) * dstride + starty;
905 const ushort *s = &src[starty * sstride + x - 7];
906 qt_rotate90_16_neon(d, s, sstride * 2, dstride * 2, stopy - starty);
907 }
908
909 for (; x >= stopx; --x) {
910 quint32 *d = reinterpret_cast<quint32*>(dest + (w - x - 1) * dstride + starty);
911 for (int y = starty; y < stopy; y += pack) {
912 quint32 c = src[y * sstride + x];
913 for (int i = 1; i < pack; ++i) {
914 const int shift = (sizeof(int) * 8 / pack * i);
915 const ushort color = src[(y + i) * sstride + x];
916 c |= color << shift;
917 }
918 *d++ = c;
919 }
920 }
921 }
922
923 if (unoptimizedY) {
924 const int starty = h - unoptimizedY;
925 for (int x = startx; x >= stopx; --x) {
926 ushort *d = dest + (w - x - 1) * dstride + starty;
927 for (int y = starty; y < h; ++y) {
928 *d++ = src[y * sstride + x];
929 }
930 }
931 }
932 }
933}
934
935extern "C" void qt_rotate270_16_neon(quint16 *dst, const quint16 *src, int sstride, int dstride, int count);
936
937void qt_memrotate270_16_neon(const uchar *srcPixels, int w, int h,
938 int sstride,
939 uchar *destPixels, int dstride)
940{
941 const ushort *src = (const ushort *)srcPixels;
942 ushort *dest = (ushort *)destPixels;
943
944 sstride /= sizeof(ushort);
945 dstride /= sizeof(ushort);
946
947 const int pack = sizeof(quint32) / sizeof(ushort);
948 const int unaligned =
949 qMin(uint((long(dest) & (sizeof(quint32)-1)) / sizeof(ushort)), uint(h));
950 const int restX = w % tileSize;
951 const int restY = (h - unaligned) % tileSize;
952 const int unoptimizedY = restY % pack;
953 const int numTilesX = w / tileSize + (restX > 0);
954 const int numTilesY = (h - unaligned) / tileSize + (restY >= pack);
955
956 for (int tx = 0; tx < numTilesX; ++tx) {
957 const int startx = tx * tileSize;
958 const int stopx = qMin(startx + tileSize, w);
959
960 if (unaligned) {
961 for (int x = startx; x < stopx; ++x) {
962 ushort *d = dest + x * dstride;
963 for (int y = h - 1; y >= h - unaligned; --y) {
964 *d++ = src[y * sstride + x];
965 }
966 }
967 }
968
969 for (int ty = 0; ty < numTilesY; ++ty) {
970 const int starty = h - 1 - unaligned - ty * tileSize;
971 const int stopy = qMax(starty - tileSize, unoptimizedY);
972
973 int x = startx;
974 // qt_rotate90_16_neon writes to eight rows, four pixels at a time
975 for (; x < stopx - 7; x += 8) {
976 ushort *d = dest + x * dstride + h - 1 - starty;
977 const ushort *s = &src[starty * sstride + x];
978 qt_rotate90_16_neon(d + 7 * dstride, s, -sstride * 2, -dstride * 2, starty - stopy);
979 }
980
981 for (; x < stopx; ++x) {
982 quint32 *d = reinterpret_cast<quint32*>(dest + x * dstride
983 + h - 1 - starty);
984 for (int y = starty; y > stopy; y -= pack) {
985 quint32 c = src[y * sstride + x];
986 for (int i = 1; i < pack; ++i) {
987 const int shift = (sizeof(int) * 8 / pack * i);
988 const ushort color = src[(y - i) * sstride + x];
989 c |= color << shift;
990 }
991 *d++ = c;
992 }
993 }
994 }
995 if (unoptimizedY) {
996 const int starty = unoptimizedY - 1;
997 for (int x = startx; x < stopx; ++x) {
998 ushort *d = dest + x * dstride + h - 1 - starty;
999 for (int y = starty; y >= 0; --y) {
1000 *d++ = src[y * sstride + x];
1001 }
1002 }
1003 }
1004 }
1005}
1006#endif
1007
1008class QSimdNeon
1009{
1010public:
1011 struct Int32x4 {
1012 Int32x4() = default;
1013 Int32x4(int32x4_t v) : v(v) {}
1014 int32x4_t v;
1015 operator int32x4_t() const { return v; }
1016 };
1017 struct Float32x4 {
1018 Float32x4() = default;
1019 Float32x4(float32x4_t v) : v(v) {};
1020 float32x4_t v;
1021 operator float32x4_t() const { return v; }
1022 };
1023
1024 union Vect_buffer_i { Int32x4 v; int i[4]; };
1025 union Vect_buffer_f { Float32x4 v; float f[4]; };
1026
1027 static inline Float32x4 v_dup(double x) { return vdupq_n_f32(float(x)); }
1028 static inline Float32x4 v_dup(float x) { return vdupq_n_f32(x); }
1029 static inline Int32x4 v_dup(int x) { return vdupq_n_s32(x); }
1030 static inline Int32x4 v_dup(uint x) { return vdupq_n_s32(x); }
1031
1032 static inline Float32x4 v_add(Float32x4 a, Float32x4 b) { return vaddq_f32(a, b); }
1033 static inline Int32x4 v_add(Int32x4 a, Int32x4 b) { return vaddq_s32(a, b); }
1034
1035 static inline Float32x4 v_max(Float32x4 a, Float32x4 b) { return vmaxq_f32(a, b); }
1036 static inline Float32x4 v_min(Float32x4 a, Float32x4 b) { return vminq_f32(a, b); }
1037 static inline Int32x4 v_min_16(Int32x4 a, Int32x4 b) { return vminq_s32(a, b); }
1038
1039 static inline Int32x4 v_and(Int32x4 a, Int32x4 b) { return vandq_s32(a, b); }
1040
1041 static inline Float32x4 v_sub(Float32x4 a, Float32x4 b) { return vsubq_f32(a, b); }
1042 static inline Int32x4 v_sub(Int32x4 a, Int32x4 b) { return vsubq_s32(a, b); }
1043
1044 static inline Float32x4 v_mul(Float32x4 a, Float32x4 b) { return vmulq_f32(a, b); }
1045
1046 static inline Float32x4 v_sqrt(Float32x4 x) { Float32x4 y = vrsqrteq_f32(x); y = vmulq_f32(y, vrsqrtsq_f32(x, vmulq_f32(y, y))); return vmulq_f32(x, y); }
1047
1048 static inline Int32x4 v_toInt(Float32x4 x) { return vcvtq_s32_f32(x); }
1049
1050 static inline Int32x4 v_greaterOrEqual(Float32x4 a, Float32x4 b) { return vreinterpretq_s32_u32(vcgeq_f32(a, b)); }
1051};
1052
1053const uint * QT_FASTCALL qt_fetch_radial_gradient_neon(uint *buffer, const Operator *op, const QSpanData *data,
1054 int y, int x, int length)
1055{
1056 return qt_fetch_radial_gradient_template<QRadialFetchSimd<QSimdNeon>,uint>(buffer, op, data, y, x, length);
1057}
1058
1059extern void QT_FASTCALL qt_convert_rgb888_to_rgb32_neon(quint32 *dst, const uchar *src, int len);
1060
1061const uint * QT_FASTCALL qt_fetchUntransformed_888_neon(uint *buffer, const Operator *, const QSpanData *data,
1062 int y, int x, int length)
1063{
1064 const uchar *line = data->texture.scanLine(y) + x * 3;
1065 qt_convert_rgb888_to_rgb32_neon(buffer, line, length);
1066 return buffer;
1067}
1068
1069#if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
1070static inline uint32x4_t vrgba2argb(uint32x4_t srcVector)
1071{
1072#if defined(Q_PROCESSOR_ARM_64)
1073 const uint8x16_t rgbaMask = qvsetq_n_u8(2, 1, 0, 3, 6, 5, 4, 7, 10, 9, 8, 11, 14, 13, 12, 15);
1074#else
1075 const uint8x8_t rgbaMask = qvset_n_u8(2, 1, 0, 3, 6, 5, 4, 7);
1076#endif
1077#if defined(Q_PROCESSOR_ARM_64)
1078 srcVector = vreinterpretq_u32_u8(vqtbl1q_u8(vreinterpretq_u8_u32(srcVector), rgbaMask));
1079#else
1080 // no vqtbl1q_u8, so use two vtbl1_u8
1081 const uint8x8_t low = vtbl1_u8(vreinterpret_u8_u32(vget_low_u32(srcVector)), rgbaMask);
1082 const uint8x8_t high = vtbl1_u8(vreinterpret_u8_u32(vget_high_u32(srcVector)), rgbaMask);
1083 srcVector = vcombine_u32(vreinterpret_u32_u8(low), vreinterpret_u32_u8(high));
1084#endif
1085 return srcVector;
1086}
1087
1088template<bool RGBA>
1089static inline void convertARGBToARGB32PM_neon(uint *buffer, const uint *src, int count)
1090{
1091 int i = 0;
1092 const uint8x8_t shuffleMask = qvset_n_u8(3, 3, 3, 3, 7, 7, 7, 7);
1093 const uint32x4_t blendMask = vdupq_n_u32(0xff000000);
1094
1095 for (; i < count - 3; i += 4) {
1096 uint32x4_t srcVector = vld1q_u32(src + i);
1097 uint32x4_t alphaVector = vshrq_n_u32(srcVector, 24);
1098#if defined(Q_PROCESSOR_ARM_64)
1099 uint32_t alphaSum = vaddvq_u32(alphaVector);
1100#else
1101 // no vaddvq_u32
1102 uint32x2_t tmp = vpadd_u32(vget_low_u32(alphaVector), vget_high_u32(alphaVector));
1103 uint32_t alphaSum = vget_lane_u32(vpadd_u32(tmp, tmp), 0);
1104#endif
1105 if (alphaSum) {
1106 if (alphaSum != 255 * 4) {
1107 if (RGBA)
1108 srcVector = vrgba2argb(srcVector);
1109 const uint8x8_t s1 = vreinterpret_u8_u32(vget_low_u32(srcVector));
1110 const uint8x8_t s2 = vreinterpret_u8_u32(vget_high_u32(srcVector));
1111 const uint8x8_t alpha1 = vtbl1_u8(s1, shuffleMask);
1112 const uint8x8_t alpha2 = vtbl1_u8(s2, shuffleMask);
1113 uint16x8_t src1 = vmull_u8(s1, alpha1);
1114 uint16x8_t src2 = vmull_u8(s2, alpha2);
1115 src1 = vsraq_n_u16(src1, src1, 8);
1116 src2 = vsraq_n_u16(src2, src2, 8);
1117 const uint8x8_t d1 = vrshrn_n_u16(src1, 8);
1118 const uint8x8_t d2 = vrshrn_n_u16(src2, 8);
1119 const uint32x4_t d = vbslq_u32(blendMask, srcVector, vreinterpretq_u32_u8(vcombine_u8(d1, d2)));
1120 vst1q_u32(buffer + i, d);
1121 } else {
1122 if (RGBA)
1123 vst1q_u32(buffer + i, vrgba2argb(srcVector));
1124 else if (buffer != src)
1125 vst1q_u32(buffer + i, srcVector);
1126 }
1127 } else {
1128 vst1q_u32(buffer + i, vdupq_n_u32(0));
1129 }
1130 }
1131
1132 SIMD_EPILOGUE(i, count, 3) {
1133 uint v = qPremultiply(src[i]);
1134 buffer[i] = RGBA ? RGBA2ARGB(v) : v;
1135 }
1136}
1137
1138template<bool RGBA>
1139static inline void convertARGB32ToRGBA64PM_neon(QRgba64 *buffer, const uint *src, int count)
1140{
1141 if (count <= 0)
1142 return;
1143
1144 const uint8x8_t shuffleMask = qvset_n_u8(3, 3, 3, 3, 7, 7, 7, 7);
1145 const uint64x2_t blendMask = vdupq_n_u64(Q_UINT64_C(0xffff000000000000));
1146
1147 int i = 0;
1148 for (; i < count-3; i += 4) {
1149 uint32x4_t vs32 = vld1q_u32(src + i);
1150 uint32x4_t alphaVector = vshrq_n_u32(vs32, 24);
1151#if defined(Q_PROCESSOR_ARM_64)
1152 uint32_t alphaSum = vaddvq_u32(alphaVector);
1153#else
1154 // no vaddvq_u32
1155 uint32x2_t tmp = vpadd_u32(vget_low_u32(alphaVector), vget_high_u32(alphaVector));
1156 uint32_t alphaSum = vget_lane_u32(vpadd_u32(tmp, tmp), 0);
1157#endif
1158 if (alphaSum) {
1159 if (!RGBA)
1160 vs32 = vrgba2argb(vs32);
1161 const uint8x16_t vs8 = vreinterpretq_u8_u32(vs32);
1162 const uint8x16x2_t v = vzipq_u8(vs8, vs8);
1163 if (alphaSum != 255 * 4) {
1164 const uint8x8_t s1 = vreinterpret_u8_u32(vget_low_u32(vs32));
1165 const uint8x8_t s2 = vreinterpret_u8_u32(vget_high_u32(vs32));
1166 const uint8x8_t alpha1 = vtbl1_u8(s1, shuffleMask);
1167 const uint8x8_t alpha2 = vtbl1_u8(s2, shuffleMask);
1168 uint16x8_t src1 = vmull_u8(s1, alpha1);
1169 uint16x8_t src2 = vmull_u8(s2, alpha2);
1170 // convert from 0->(255x255) to 0->(255x257)
1171 src1 = vsraq_n_u16(src1, src1, 7);
1172 src2 = vsraq_n_u16(src2, src2, 7);
1173
1174 // now restore alpha from the trivial conversion
1175 const uint64x2_t d1 = vbslq_u64(blendMask, vreinterpretq_u64_u8(v.val[0]), vreinterpretq_u64_u16(src1));
1176 const uint64x2_t d2 = vbslq_u64(blendMask, vreinterpretq_u64_u8(v.val[1]), vreinterpretq_u64_u16(src2));
1177
1178 vst1q_u16((uint16_t *)buffer, vreinterpretq_u16_u64(d1));
1179 buffer += 2;
1180 vst1q_u16((uint16_t *)buffer, vreinterpretq_u16_u64(d2));
1181 buffer += 2;
1182 } else {
1183 vst1q_u16((uint16_t *)buffer, vreinterpretq_u16_u8(v.val[0]));
1184 buffer += 2;
1185 vst1q_u16((uint16_t *)buffer, vreinterpretq_u16_u8(v.val[1]));
1186 buffer += 2;
1187 }
1188 } else {
1189 vst1q_u16((uint16_t *)buffer, vdupq_n_u16(0));
1190 buffer += 2;
1191 vst1q_u16((uint16_t *)buffer, vdupq_n_u16(0));
1192 buffer += 2;
1193 }
1194 }
1195
1196 SIMD_EPILOGUE(i, count, 3) {
1197 uint s = src[i];
1198 if (RGBA)
1199 s = RGBA2ARGB(s);
1200 *buffer++ = QRgba64::fromArgb32(s).premultiplied();
1201 }
1202}
1203
1204static inline float32x4_t reciprocal_mul_ps(float32x4_t a, float mul)
1205{
1206 float32x4_t ia = vrecpeq_f32(a); // estimate 1/a
1207 ia = vmulq_f32(vrecpsq_f32(a, ia), vmulq_n_f32(ia, mul)); // estimate improvement step * mul
1208 return ia;
1209}
1210
1211template<bool RGBA, bool RGBx>
1212static inline void convertARGBFromARGB32PM_neon(uint *buffer, const uint *src, int count)
1213{
1214 int i = 0;
1215 const uint32x4_t alphaMask = vdupq_n_u32(0xff000000);
1216
1217 for (; i < count - 3; i += 4) {
1218 uint32x4_t srcVector = vld1q_u32(src + i);
1219 uint32x4_t alphaVector = vshrq_n_u32(srcVector, 24);
1220#if defined(Q_PROCESSOR_ARM_64)
1221 uint32_t alphaSum = vaddvq_u32(alphaVector);
1222#else
1223 // no vaddvq_u32
1224 uint32x2_t tmp = vpadd_u32(vget_low_u32(alphaVector), vget_high_u32(alphaVector));
1225 uint32_t alphaSum = vget_lane_u32(vpadd_u32(tmp, tmp), 0);
1226#endif
1227 if (alphaSum) {
1228 if (alphaSum != 255 * 4) {
1229 if (RGBA)
1230 srcVector = vrgba2argb(srcVector);
1231 const float32x4_t a = vcvtq_f32_u32(alphaVector);
1232 const float32x4_t ia = reciprocal_mul_ps(a, 255.0f);
1233 // Convert 4x(4xU8) to 4x(4xF32)
1234 uint16x8_t tmp1 = vmovl_u8(vget_low_u8(vreinterpretq_u8_u32(srcVector)));
1235 uint16x8_t tmp3 = vmovl_u8(vget_high_u8(vreinterpretq_u8_u32(srcVector)));
1236 float32x4_t src1 = vcvtq_f32_u32(vmovl_u16(vget_low_u16(tmp1)));
1237 float32x4_t src2 = vcvtq_f32_u32(vmovl_u16(vget_high_u16(tmp1)));
1238 float32x4_t src3 = vcvtq_f32_u32(vmovl_u16(vget_low_u16(tmp3)));
1239 float32x4_t src4 = vcvtq_f32_u32(vmovl_u16(vget_high_u16(tmp3)));
1240 src1 = vmulq_lane_f32(src1, vget_low_f32(ia), 0);
1241 src2 = vmulq_lane_f32(src2, vget_low_f32(ia), 1);
1242 src3 = vmulq_lane_f32(src3, vget_high_f32(ia), 0);
1243 src4 = vmulq_lane_f32(src4, vget_high_f32(ia), 1);
1244 // Convert 4x(4xF32) back to 4x(4xU8) (over a 8.1 fixed point format to get rounding)
1245 tmp1 = vcombine_u16(vrshrn_n_u32(vcvtq_n_u32_f32(src1, 1), 1),
1246 vrshrn_n_u32(vcvtq_n_u32_f32(src2, 1), 1));
1247 tmp3 = vcombine_u16(vrshrn_n_u32(vcvtq_n_u32_f32(src3, 1), 1),
1248 vrshrn_n_u32(vcvtq_n_u32_f32(src4, 1), 1));
1249 uint32x4_t dstVector = vreinterpretq_u32_u8(vcombine_u8(vmovn_u16(tmp1), vmovn_u16(tmp3)));
1250 // Overwrite any undefined results from alpha==0 with zeros:
1251#if defined(Q_PROCESSOR_ARM_64)
1252 uint32x4_t srcVectorAlphaMask = vceqzq_u32(alphaVector);
1253#else
1254 uint32x4_t srcVectorAlphaMask = vceqq_u32(alphaVector, vdupq_n_u32(0));
1255#endif
1256 dstVector = vbicq_u32(dstVector, srcVectorAlphaMask);
1257 // Restore or mask alpha values:
1258 if (RGBx)
1259 dstVector = vorrq_u32(alphaMask, dstVector);
1260 else
1261 dstVector = vbslq_u32(alphaMask, srcVector, dstVector);
1262 vst1q_u32(&buffer[i], dstVector);
1263 } else {
1264 // 4xAlpha==255, no change except if we are doing RGBA->ARGB:
1265 if (RGBA)
1266 vst1q_u32(&buffer[i], vrgba2argb(srcVector));
1267 else if (buffer != src)
1268 vst1q_u32(&buffer[i], srcVector);
1269 }
1270 } else {
1271 // 4xAlpha==0, always zero, except if output is RGBx:
1272 if (RGBx)
1273 vst1q_u32(&buffer[i], alphaMask);
1274 else
1275 vst1q_u32(&buffer[i], vdupq_n_u32(0));
1276 }
1277 }
1278
1279 SIMD_EPILOGUE(i, count, 3) {
1280 uint v = qUnpremultiply(src[i]);
1281 if (RGBx)
1282 v = 0xff000000 | v;
1283 if (RGBA)
1284 v = ARGB2RGBA(v);
1285 buffer[i] = v;
1286 }
1287}
1288
1289void QT_FASTCALL convertARGB32ToARGB32PM_neon(uint *buffer, int count, const QList<QRgb> *)
1290{
1291 convertARGBToARGB32PM_neon<false>(buffer, buffer, count);
1292}
1293
1294void QT_FASTCALL convertRGBA8888ToARGB32PM_neon(uint *buffer, int count, const QList<QRgb> *)
1295{
1296 convertARGBToARGB32PM_neon<true>(buffer, buffer, count);
1297}
1298
1299const uint *QT_FASTCALL fetchARGB32ToARGB32PM_neon(uint *buffer, const uchar *src, int index, int count,
1300 const QList<QRgb> *, QDitherInfo *)
1301{
1302 convertARGBToARGB32PM_neon<false>(buffer, reinterpret_cast<const uint *>(src) + index, count);
1303 return buffer;
1304}
1305
1306const uint *QT_FASTCALL fetchRGBA8888ToARGB32PM_neon(uint *buffer, const uchar *src, int index, int count,
1307 const QList<QRgb> *, QDitherInfo *)
1308{
1309 convertARGBToARGB32PM_neon<true>(buffer, reinterpret_cast<const uint *>(src) + index, count);
1310 return buffer;
1311}
1312
1313const QRgba64 * QT_FASTCALL convertARGB32ToRGBA64PM_neon(QRgba64 *buffer, const uint *src, int count,
1314 const QList<QRgb> *, QDitherInfo *)
1315{
1316 convertARGB32ToRGBA64PM_neon<false>(buffer, src, count);
1317 return buffer;
1318}
1319
1320const QRgba64 * QT_FASTCALL convertRGBA8888ToRGBA64PM_neon(QRgba64 *buffer, const uint *src, int count,
1321 const QList<QRgb> *, QDitherInfo *)
1322{
1323 convertARGB32ToRGBA64PM_neon<true>(buffer, src, count);
1324 return buffer;
1325}
1326
1327const QRgba64 *QT_FASTCALL fetchARGB32ToRGBA64PM_neon(QRgba64 *buffer, const uchar *src, int index, int count,
1328 const QList<QRgb> *, QDitherInfo *)
1329{
1330 convertARGB32ToRGBA64PM_neon<false>(buffer, reinterpret_cast<const uint *>(src) + index, count);
1331 return buffer;
1332}
1333
1334const QRgba64 *QT_FASTCALL fetchRGBA8888ToRGBA64PM_neon(QRgba64 *buffer, const uchar *src, int index, int count,
1335 const QList<QRgb> *, QDitherInfo *)
1336{
1337 convertARGB32ToRGBA64PM_neon<true>(buffer, reinterpret_cast<const uint *>(src) + index, count);
1338 return buffer;
1339}
1340
1341void QT_FASTCALL storeRGB32FromARGB32PM_neon(uchar *dest, const uint *src, int index, int count,
1342 const QList<QRgb> *, QDitherInfo *)
1343{
1344 uint *d = reinterpret_cast<uint *>(dest) + index;
1345 convertARGBFromARGB32PM_neon<false,true>(d, src, count);
1346}
1347
1348void QT_FASTCALL storeARGB32FromARGB32PM_neon(uchar *dest, const uint *src, int index, int count,
1349 const QList<QRgb> *, QDitherInfo *)
1350{
1351 uint *d = reinterpret_cast<uint *>(dest) + index;
1352 convertARGBFromARGB32PM_neon<false,false>(d, src, count);
1353}
1354
1355void QT_FASTCALL storeRGBA8888FromARGB32PM_neon(uchar *dest, const uint *src, int index, int count,
1356 const QList<QRgb> *, QDitherInfo *)
1357{
1358 uint *d = reinterpret_cast<uint *>(dest) + index;
1359 convertARGBFromARGB32PM_neon<true,false>(d, src, count);
1360}
1361
1362void QT_FASTCALL storeRGBXFromARGB32PM_neon(uchar *dest, const uint *src, int index, int count,
1363 const QList<QRgb> *, QDitherInfo *)
1364{
1365 uint *d = reinterpret_cast<uint *>(dest) + index;
1366 convertARGBFromARGB32PM_neon<true,true>(d, src, count);
1367}
1368
1369#endif // Q_BYTE_ORDER == Q_LITTLE_ENDIAN
1370
1371QT_END_NAMESPACE
1372
1373#endif // __ARM_NEON__