1// Copyright (C) 2020 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#ifndef QRGBA64_P_H
5#define QRGBA64_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include "qrgba64.h"
19#include "qdrawhelper_p.h"
20
21#include <QtCore/private/qsimd_p.h>
22#include <QtGui/private/qtguiglobal_p.h>
23
24QT_BEGIN_NAMESPACE
25
26inline QRgba64 combineAlpha256(QRgba64 rgba64, uint alpha256)
27{
28 return QRgba64::fromRgba64(red: rgba64.red(), green: rgba64.green(), blue: rgba64.blue(), alpha: (rgba64.alpha() * alpha256) >> 8);
29}
30
31#if defined(__SSE2__)
32static inline __m128i Q_DECL_VECTORCALL multiplyAlpha65535(__m128i rgba64, __m128i va)
33{
34 __m128i vs = rgba64;
35 vs = _mm_unpacklo_epi16(a: _mm_mullo_epi16(a: vs, b: va), b: _mm_mulhi_epu16(a: vs, b: va));
36 vs = _mm_add_epi32(a: vs, b: _mm_srli_epi32(a: vs, count: 16));
37 vs = _mm_add_epi32(a: vs, b: _mm_set1_epi32(i: 0x8000));
38 vs = _mm_srai_epi32(a: vs, count: 16);
39 vs = _mm_packs_epi32(a: vs, b: vs);
40 return vs;
41}
42static inline __m128i Q_DECL_VECTORCALL multiplyAlpha65535(__m128i rgba64, uint alpha65535)
43{
44 const __m128i va = _mm_shufflelo_epi16(_mm_cvtsi32_si128(alpha65535), _MM_SHUFFLE(0, 0, 0, 0));
45 return multiplyAlpha65535(rgba64, va);
46}
47#elif defined(__ARM_NEON__)
48static inline uint16x4_t multiplyAlpha65535(uint16x4_t rgba64, uint16x4_t alpha65535)
49{
50 uint32x4_t vs32 = vmull_u16(rgba64, alpha65535); // vs = vs * alpha
51 vs32 = vsraq_n_u32(vs32, vs32, 16); // vs = vs + (vs >> 16)
52 return vrshrn_n_u32(vs32, 16); // vs = (vs + 0x8000) >> 16
53}
54static inline uint16x4_t multiplyAlpha65535(uint16x4_t rgba64, uint alpha65535)
55{
56 uint32x4_t vs32 = vmull_n_u16(rgba64, alpha65535); // vs = vs * alpha
57 vs32 = vsraq_n_u32(vs32, vs32, 16); // vs = vs + (vs >> 16)
58 return vrshrn_n_u32(vs32, 16); // vs = (vs + 0x8000) >> 16
59}
60#endif
61
62static inline QRgba64 multiplyAlpha65535(QRgba64 rgba64, uint alpha65535)
63{
64#if defined(__SSE2__)
65 const __m128i v = _mm_loadl_epi64(p: reinterpret_cast<const __m128i *>(&rgba64));
66 const __m128i vr = multiplyAlpha65535(rgba64: v, alpha65535);
67 QRgba64 r;
68 _mm_storel_epi64(p: reinterpret_cast<__m128i *>(&r), a: vr);
69 return r;
70#elif defined(__ARM_NEON__)
71 const uint16x4_t v = vreinterpret_u16_u64(vld1_u64(reinterpret_cast<const uint64_t *>(&rgba64)));
72 const uint16x4_t vr = multiplyAlpha65535(v, alpha65535);
73 QRgba64 r;
74 vst1_u64(reinterpret_cast<uint64_t *>(&r), vreinterpret_u64_u16(vr));
75 return r;
76#else
77 return QRgba64::fromRgba64(qt_div_65535(rgba64.red() * alpha65535),
78 qt_div_65535(rgba64.green() * alpha65535),
79 qt_div_65535(rgba64.blue() * alpha65535),
80 qt_div_65535(rgba64.alpha() * alpha65535));
81#endif
82}
83
84#if defined(__SSE2__) || defined(__ARM_NEON__)
85template<typename T>
86static inline T Q_DECL_VECTORCALL multiplyAlpha255(T rgba64, uint alpha255)
87{
88 return multiplyAlpha65535(rgba64, alpha255 * 257);
89}
90#else
91template<typename T>
92static inline T multiplyAlpha255(T rgba64, uint alpha255)
93{
94 return QRgba64::fromRgba64(qt_div_255(rgba64.red() * alpha255),
95 qt_div_255(rgba64.green() * alpha255),
96 qt_div_255(rgba64.blue() * alpha255),
97 qt_div_255(rgba64.alpha() * alpha255));
98}
99#endif
100
101#if defined __SSE2__
102static inline __m128i Q_DECL_VECTORCALL interpolate255(__m128i x, uint alpha1, __m128i y, uint alpha2)
103{
104 return _mm_add_epi16(a: multiplyAlpha255(rgba64: x, alpha255: alpha1), b: multiplyAlpha255(rgba64: y, alpha255: alpha2));
105}
106#endif
107
108#if defined __ARM_NEON__
109inline uint16x4_t interpolate255(uint16x4_t x, uint alpha1, uint16x4_t y, uint alpha2)
110{
111 return vadd_u16(multiplyAlpha255(x, alpha1), multiplyAlpha255(y, alpha2));
112}
113#endif
114
115static inline QRgba64 interpolate255(QRgba64 x, uint alpha1, QRgba64 y, uint alpha2)
116{
117#if defined(__SSE2__)
118 const __m128i vx = _mm_loadl_epi64(p: reinterpret_cast<const __m128i *>(&x));
119 const __m128i vy = _mm_loadl_epi64(p: reinterpret_cast<const __m128i *>(&y));
120 const __m128i vr = interpolate255(x: vx, alpha1, y: vy, alpha2);
121 QRgba64 r;
122 _mm_storel_epi64(p: reinterpret_cast<__m128i *>(&r), a: vr);
123 return r;
124#elif defined(__ARM_NEON__)
125 const uint16x4_t vx = vreinterpret_u16_u64(vld1_u64(reinterpret_cast<const uint64_t *>(&x)));
126 const uint16x4_t vy = vreinterpret_u16_u64(vld1_u64(reinterpret_cast<const uint64_t *>(&y)));
127 const uint16x4_t vr = interpolate255(vx, alpha1, vy, alpha2);
128 QRgba64 r;
129 vst1_u64(reinterpret_cast<uint64_t *>(&r), vreinterpret_u64_u16(vr));
130 return r;
131#else
132 return QRgba64::fromRgba64(multiplyAlpha255(x, alpha1) + multiplyAlpha255(y, alpha2));
133#endif
134}
135
136#if defined __SSE2__
137static inline __m128i Q_DECL_VECTORCALL interpolate65535(__m128i x, uint alpha1, __m128i y, uint alpha2)
138{
139 return _mm_add_epi16(a: multiplyAlpha65535(rgba64: x, alpha65535: alpha1), b: multiplyAlpha65535(rgba64: y, alpha65535: alpha2));
140}
141
142static inline __m128i Q_DECL_VECTORCALL interpolate65535(__m128i x, __m128i alpha1, __m128i y, __m128i alpha2)
143{
144 return _mm_add_epi16(a: multiplyAlpha65535(rgba64: x, va: alpha1), b: multiplyAlpha65535(rgba64: y, va: alpha2));
145}
146#endif
147
148#if defined __ARM_NEON__
149inline uint16x4_t interpolate65535(uint16x4_t x, uint alpha1, uint16x4_t y, uint alpha2)
150{
151 return vadd_u16(multiplyAlpha65535(x, alpha1), multiplyAlpha65535(y, alpha2));
152}
153inline uint16x4_t interpolate65535(uint16x4_t x, uint16x4_t alpha1, uint16x4_t y, uint16x4_t alpha2)
154{
155 return vadd_u16(multiplyAlpha65535(x, alpha1), multiplyAlpha65535(y, alpha2));
156}
157#endif
158
159static inline QRgba64 interpolate65535(QRgba64 x, uint alpha1, QRgba64 y, uint alpha2)
160{
161#if defined(__SSE2__)
162 const __m128i vx = _mm_loadl_epi64(p: reinterpret_cast<const __m128i *>(&x));
163 const __m128i vy = _mm_loadl_epi64(p: reinterpret_cast<const __m128i *>(&y));
164 const __m128i vr = interpolate65535(x: vx, alpha1, y: vy, alpha2);
165 QRgba64 r;
166 _mm_storel_epi64(p: reinterpret_cast<__m128i *>(&r), a: vr);
167 return r;
168#elif defined(__ARM_NEON__)
169 const uint16x4_t vx = vreinterpret_u16_u64(vld1_u64(reinterpret_cast<const uint64_t *>(&x)));
170 const uint16x4_t vy = vreinterpret_u16_u64(vld1_u64(reinterpret_cast<const uint64_t *>(&y)));
171 const uint16x4_t vr = interpolate65535(vx, alpha1, vy, alpha2);
172 QRgba64 r;
173 vst1_u64(reinterpret_cast<uint64_t *>(&r), vreinterpret_u64_u16(vr));
174 return r;
175#else
176 return QRgba64::fromRgba64(multiplyAlpha65535(x, alpha1) + multiplyAlpha65535(y, alpha2));
177#endif
178}
179
180static inline QRgba64 addWithSaturation(QRgba64 a, QRgba64 b)
181{
182#if defined(__SSE2__)
183 const __m128i va = _mm_loadl_epi64(p: reinterpret_cast<const __m128i *>(&a));
184 const __m128i vb = _mm_loadl_epi64(p: reinterpret_cast<const __m128i *>(&b));
185 const __m128i vr = _mm_adds_epu16(a: va, b: vb);
186 QRgba64 r;
187 _mm_storel_epi64(p: reinterpret_cast<__m128i *>(&r), a: vr);
188 return r;
189#elif defined(__ARM_NEON__)
190 const uint16x4_t va = vreinterpret_u16_u64(vld1_u64(reinterpret_cast<const uint64_t *>(&a)));
191 const uint16x4_t vb = vreinterpret_u16_u64(vld1_u64(reinterpret_cast<const uint64_t *>(&b)));
192 QRgba64 r;
193 vst1_u64(reinterpret_cast<uint64_t *>(&r), vreinterpret_u64_u16(vqadd_u16(va, vb)));
194 return r;
195#else
196
197 return QRgba64::fromRgba64(qMin(a.red() + b.red(), 65535),
198 qMin(a.green() + b.green(), 65535),
199 qMin(a.blue() + b.blue(), 65535),
200 qMin(a.alpha() + b.alpha(), 65535));
201#endif
202}
203
204#if QT_COMPILER_SUPPORTS_HERE(SSE2)
205QT_FUNCTION_TARGET(SSE2)
206static inline uint Q_DECL_VECTORCALL toArgb32(__m128i v)
207{
208 v = _mm_unpacklo_epi16(a: v, b: _mm_setzero_si128());
209 v = _mm_add_epi32(a: v, b: _mm_set1_epi32(i: 128));
210 v = _mm_sub_epi32(a: v, b: _mm_srli_epi32(a: v, count: 8));
211 v = _mm_srli_epi32(a: v, count: 8);
212 v = _mm_packs_epi32(a: v, b: v);
213 v = _mm_packus_epi16(a: v, b: v);
214 return _mm_cvtsi128_si32(a: v);
215}
216#elif defined __ARM_NEON__
217static inline uint toArgb32(uint16x4_t v)
218{
219 v = vsub_u16(v, vrshr_n_u16(v, 8));
220 v = vrshr_n_u16(v, 8);
221 uint8x8_t v8 = vmovn_u16(vcombine_u16(v, v));
222 return vget_lane_u32(vreinterpret_u32_u8(v8), 0);
223}
224#endif
225
226static inline uint toArgb32(QRgba64 rgba64)
227{
228#if defined __SSE2__
229 __m128i v = _mm_loadl_epi64(p: reinterpret_cast<const __m128i *>(&rgba64));
230 v = _mm_shufflelo_epi16(v, _MM_SHUFFLE(3, 0, 1, 2));
231 return toArgb32(v);
232#elif defined __ARM_NEON__
233 uint16x4_t v = vreinterpret_u16_u64(vld1_u64(reinterpret_cast<const uint64_t *>(&rgba64)));
234#if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
235 const uint8x8_t shuffleMask = qvset_n_u8(4, 5, 2, 3, 0, 1, 6, 7);
236 v = vreinterpret_u16_u8(vtbl1_u8(vreinterpret_u8_u16(v), shuffleMask));
237#else
238 v = vext_u16(v, v, 3);
239#endif
240 return toArgb32(v);
241#else
242 return rgba64.toArgb32();
243#endif
244}
245
246static inline uint toRgba8888(QRgba64 rgba64)
247{
248#if defined __SSE2__
249 __m128i v = _mm_loadl_epi64(p: reinterpret_cast<const __m128i *>(&rgba64));
250 return toArgb32(v);
251#elif defined __ARM_NEON__
252 uint16x4_t v = vreinterpret_u16_u64(vld1_u64(reinterpret_cast<const uint64_t *>(&rgba64)));
253 return toArgb32(v);
254#else
255 return ARGB2RGBA(toArgb32(rgba64));
256#endif
257}
258
259static inline QRgba64 rgbBlend(QRgba64 d, QRgba64 s, uint rgbAlpha)
260{
261 QRgba64 blend;
262#if defined(__SSE2__)
263 __m128i vd = _mm_loadl_epi64(p: reinterpret_cast<const __m128i *>(&d));
264 __m128i vs = _mm_loadl_epi64(p: reinterpret_cast<const __m128i *>(&s));
265 __m128i va = _mm_cvtsi32_si128(a: rgbAlpha);
266 va = _mm_unpacklo_epi8(a: va, b: va);
267 va = _mm_shufflelo_epi16(va, _MM_SHUFFLE(3, 0, 1, 2));
268 __m128i vb = _mm_xor_si128(a: _mm_set1_epi16(w: -1), b: va);
269
270 vs = _mm_unpacklo_epi16(a: _mm_mullo_epi16(a: vs, b: va), b: _mm_mulhi_epu16(a: vs, b: va));
271 vd = _mm_unpacklo_epi16(a: _mm_mullo_epi16(a: vd, b: vb), b: _mm_mulhi_epu16(a: vd, b: vb));
272 vd = _mm_add_epi32(a: vd, b: vs);
273 vd = _mm_add_epi32(a: vd, b: _mm_srli_epi32(a: vd, count: 16));
274 vd = _mm_add_epi32(a: vd, b: _mm_set1_epi32(i: 0x8000));
275 vd = _mm_srai_epi32(a: vd, count: 16);
276 vd = _mm_packs_epi32(a: vd, b: vd);
277
278 _mm_storel_epi64(p: reinterpret_cast<__m128i *>(&blend), a: vd);
279#elif defined(__ARM_NEON__)
280 uint16x4_t vd = vreinterpret_u16_u64(vmov_n_u64(d));
281 uint16x4_t vs = vreinterpret_u16_u64(vmov_n_u64(s));
282 uint8x8_t va8 = vreinterpret_u8_u32(vmov_n_u32(ARGB2RGBA(rgbAlpha)));
283 uint16x4_t va = vreinterpret_u16_u8(vzip_u8(va8, va8).val[0]);
284 uint16x4_t vb = veor_u16(vdup_n_u16(0xffff), va);
285
286 uint32x4_t vs32 = vmull_u16(vs, va);
287 uint32x4_t vd32 = vmull_u16(vd, vb);
288 vd32 = vaddq_u32(vd32, vs32);
289 vd32 = vsraq_n_u32(vd32, vd32, 16);
290 vd = vrshrn_n_u32(vd32, 16);
291 vst1_u64(reinterpret_cast<uint64_t *>(&blend), vreinterpret_u64_u16(vd));
292#else
293 const int mr = qRed(rgbAlpha);
294 const int mg = qGreen(rgbAlpha);
295 const int mb = qBlue(rgbAlpha);
296 blend = qRgba64(qt_div_255(s.red() * mr + d.red() * (255 - mr)),
297 qt_div_255(s.green() * mg + d.green() * (255 - mg)),
298 qt_div_255(s.blue() * mb + d.blue() * (255 - mb)),
299 s.alpha());
300#endif
301 return blend;
302}
303
304static inline void blend_pixel(QRgba64 &dst, QRgba64 src)
305{
306 if (src.isOpaque())
307 dst = src;
308 else if (!src.isTransparent()) {
309#if defined(__SSE2__)
310 const __m128i vd = _mm_loadl_epi64(p: reinterpret_cast<const __m128i *>(&dst));
311 const __m128i vs = _mm_loadl_epi64(p: reinterpret_cast<const __m128i *>(&src));
312 const __m128i via = _mm_xor_si128(a: _mm_set1_epi16(w: -1), _mm_shufflelo_epi16(vs, _MM_SHUFFLE(3, 3, 3, 3)));
313 const __m128i vr = _mm_add_epi16(a: vs, b: multiplyAlpha65535(rgba64: vd, va: via));
314 _mm_storel_epi64(p: reinterpret_cast<__m128i *>(&dst), a: vr);
315#elif defined(__ARM_NEON__)
316 const uint16x4_t vd = vreinterpret_u16_u64(vld1_u64(reinterpret_cast<const uint64_t *>(&dst)));
317 const uint16x4_t vs = vreinterpret_u16_u64(vld1_u64(reinterpret_cast<const uint64_t *>(&src)));
318 const uint16x4_t via = veor_u16(vdup_n_u16(0xffff), vdup_lane_u16(vs, 3));
319 const uint16x4_t vr = vadd_u16(vs, multiplyAlpha65535(vd, via));
320 vst1_u64(reinterpret_cast<uint64_t *>(&dst), vreinterpret_u64_u16(vr));
321#else
322 dst = src + multiplyAlpha65535(dst, 65535 - src.alpha());
323#endif
324 }
325}
326
327static inline void blend_pixel(QRgba64 &dst, QRgba64 src, const int const_alpha)
328{
329 if (const_alpha == 255)
330 return blend_pixel(dst, src);
331 if (!src.isTransparent()) {
332#if defined(__SSE2__)
333 const __m128i vd = _mm_loadl_epi64(p: reinterpret_cast<const __m128i *>(&dst));
334 __m128i vs = _mm_loadl_epi64(p: reinterpret_cast<const __m128i *>(&src));
335 vs = multiplyAlpha255(rgba64: vs, alpha255: const_alpha);
336 const __m128i via = _mm_xor_si128(a: _mm_set1_epi16(w: -1), _mm_shufflelo_epi16(vs, _MM_SHUFFLE(3, 3, 3, 3)));
337 const __m128i vr = _mm_add_epi16(a: vs, b: multiplyAlpha65535(rgba64: vd, va: via));
338 _mm_storel_epi64(p: reinterpret_cast<__m128i *>(&dst), a: vr);
339#elif defined(__ARM_NEON__)
340 const uint16x4_t vd = vreinterpret_u16_u64(vld1_u64(reinterpret_cast<const uint64_t *>(&dst)));
341 uint16x4_t vs = vreinterpret_u16_u64(vld1_u64(reinterpret_cast<const uint64_t *>(&src)));
342 vs = multiplyAlpha255(vs, const_alpha);
343 const uint16x4_t via = veor_u16(vdup_n_u16(0xffff), vdup_lane_u16(vs, 3));
344 const uint16x4_t vr = vadd_u16(vs, multiplyAlpha65535(vd, via));
345 vst1_u64(reinterpret_cast<uint64_t *>(&dst), vreinterpret_u64_u16(vr));
346#else
347 src = multiplyAlpha255(src, const_alpha);
348 dst = src + multiplyAlpha65535(dst, 65535 - src.alpha());
349#endif
350 }
351}
352
353QT_END_NAMESPACE
354
355#endif // QRGBA64_P_H
356

Provided by KDAB

Privacy Policy
Start learning QML with our Intro Training
Find out more

source code of qtbase/src/gui/painting/qrgba64_p.h