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#else
190 return QRgba64::fromRgba64(qMin(a.red() + b.red(), 65535),
191 qMin(a.green() + b.green(), 65535),
192 qMin(a.blue() + b.blue(), 65535),
193 qMin(a.alpha() + b.alpha(), 65535));
194#endif
195}
196
197#if QT_COMPILER_SUPPORTS_HERE(SSE2)
198QT_FUNCTION_TARGET(SSE2)
199static inline uint Q_DECL_VECTORCALL toArgb32(__m128i v)
200{
201 v = _mm_unpacklo_epi16(a: v, b: _mm_setzero_si128());
202 v = _mm_add_epi32(a: v, b: _mm_set1_epi32(i: 128));
203 v = _mm_sub_epi32(a: v, b: _mm_srli_epi32(a: v, count: 8));
204 v = _mm_srli_epi32(a: v, count: 8);
205 v = _mm_packs_epi32(a: v, b: v);
206 v = _mm_packus_epi16(a: v, b: v);
207 return _mm_cvtsi128_si32(a: v);
208}
209#elif defined __ARM_NEON__
210static inline uint toArgb32(uint16x4_t v)
211{
212 v = vsub_u16(v, vrshr_n_u16(v, 8));
213 v = vrshr_n_u16(v, 8);
214 uint8x8_t v8 = vmovn_u16(vcombine_u16(v, v));
215 return vget_lane_u32(vreinterpret_u32_u8(v8), 0);
216}
217#endif
218
219static inline uint toArgb32(QRgba64 rgba64)
220{
221#if defined __SSE2__
222 __m128i v = _mm_loadl_epi64(p: reinterpret_cast<const __m128i *>(&rgba64));
223 v = _mm_shufflelo_epi16(v, _MM_SHUFFLE(3, 0, 1, 2));
224 return toArgb32(v);
225#elif defined __ARM_NEON__
226 uint16x4_t v = vreinterpret_u16_u64(vld1_u64(reinterpret_cast<const uint64_t *>(&rgba64)));
227#if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
228 const uint8x8_t shuffleMask = { 4, 5, 2, 3, 0, 1, 6, 7 };
229 v = vreinterpret_u16_u8(vtbl1_u8(vreinterpret_u8_u16(v), shuffleMask));
230#else
231 v = vext_u16(v, v, 3);
232#endif
233 return toArgb32(v);
234#else
235 return rgba64.toArgb32();
236#endif
237}
238
239static inline uint toRgba8888(QRgba64 rgba64)
240{
241#if defined __SSE2__
242 __m128i v = _mm_loadl_epi64(p: reinterpret_cast<const __m128i *>(&rgba64));
243 return toArgb32(v);
244#elif defined __ARM_NEON__
245 uint16x4_t v = vreinterpret_u16_u64(vld1_u64(reinterpret_cast<const uint64_t *>(&rgba64)));
246 return toArgb32(v);
247#else
248 return ARGB2RGBA(toArgb32(rgba64));
249#endif
250}
251
252static inline QRgba64 rgbBlend(QRgba64 d, QRgba64 s, uint rgbAlpha)
253{
254 QRgba64 blend;
255#if defined(__SSE2__)
256 __m128i vd = _mm_loadl_epi64(p: reinterpret_cast<const __m128i *>(&d));
257 __m128i vs = _mm_loadl_epi64(p: reinterpret_cast<const __m128i *>(&s));
258 __m128i va = _mm_cvtsi32_si128(a: rgbAlpha);
259 va = _mm_unpacklo_epi8(a: va, b: va);
260 va = _mm_shufflelo_epi16(va, _MM_SHUFFLE(3, 0, 1, 2));
261 __m128i vb = _mm_xor_si128(a: _mm_set1_epi16(w: -1), b: va);
262
263 vs = _mm_unpacklo_epi16(a: _mm_mullo_epi16(a: vs, b: va), b: _mm_mulhi_epu16(a: vs, b: va));
264 vd = _mm_unpacklo_epi16(a: _mm_mullo_epi16(a: vd, b: vb), b: _mm_mulhi_epu16(a: vd, b: vb));
265 vd = _mm_add_epi32(a: vd, b: vs);
266 vd = _mm_add_epi32(a: vd, b: _mm_srli_epi32(a: vd, count: 16));
267 vd = _mm_add_epi32(a: vd, b: _mm_set1_epi32(i: 0x8000));
268 vd = _mm_srai_epi32(a: vd, count: 16);
269 vd = _mm_packs_epi32(a: vd, b: vd);
270
271 _mm_storel_epi64(p: reinterpret_cast<__m128i *>(&blend), a: vd);
272#elif defined(__ARM_NEON__)
273 uint16x4_t vd = vreinterpret_u16_u64(vmov_n_u64(d));
274 uint16x4_t vs = vreinterpret_u16_u64(vmov_n_u64(s));
275 uint8x8_t va8 = vreinterpret_u8_u32(vmov_n_u32(ARGB2RGBA(rgbAlpha)));
276 uint16x4_t va = vreinterpret_u16_u8(vzip_u8(va8, va8).val[0]);
277 uint16x4_t vb = vdup_n_u16(0xffff);
278 vb = vsub_u16(vb, va);
279
280 uint32x4_t vs32 = vmull_u16(vs, va);
281 uint32x4_t vd32 = vmull_u16(vd, vb);
282 vd32 = vaddq_u32(vd32, vs32);
283 vd32 = vsraq_n_u32(vd32, vd32, 16);
284 vd = vrshrn_n_u32(vd32, 16);
285 vst1_u64(reinterpret_cast<uint64_t *>(&blend), vreinterpret_u64_u16(vd));
286#else
287 const int mr = qRed(rgbAlpha);
288 const int mg = qGreen(rgbAlpha);
289 const int mb = qBlue(rgbAlpha);
290 blend = qRgba64(qt_div_255(s.red() * mr + d.red() * (255 - mr)),
291 qt_div_255(s.green() * mg + d.green() * (255 - mg)),
292 qt_div_255(s.blue() * mb + d.blue() * (255 - mb)),
293 s.alpha());
294#endif
295 return blend;
296}
297
298static inline void blend_pixel(QRgba64 &dst, QRgba64 src)
299{
300 if (src.isOpaque())
301 dst = src;
302 else if (!src.isTransparent()) {
303#if defined(__SSE2__)
304 const __m128i vd = _mm_loadl_epi64(p: reinterpret_cast<const __m128i *>(&dst));
305 const __m128i vs = _mm_loadl_epi64(p: reinterpret_cast<const __m128i *>(&src));
306 const __m128i via = _mm_xor_si128(a: _mm_set1_epi16(w: -1), _mm_shufflelo_epi16(vs, _MM_SHUFFLE(3, 3, 3, 3)));
307 const __m128i vr = _mm_add_epi16(a: vs, b: multiplyAlpha65535(rgba64: vd, va: via));
308 _mm_storel_epi64(p: reinterpret_cast<__m128i *>(&dst), a: vr);
309#else
310 dst = src + multiplyAlpha65535(dst, 65535 - src.alpha());
311#endif
312 }
313}
314
315static inline void blend_pixel(QRgba64 &dst, QRgba64 src, const int const_alpha)
316{
317 if (const_alpha == 255)
318 return blend_pixel(dst, src);
319 if (!src.isTransparent()) {
320#if defined(__SSE2__)
321 const __m128i vd = _mm_loadl_epi64(p: reinterpret_cast<const __m128i *>(&dst));
322 __m128i vs = _mm_loadl_epi64(p: reinterpret_cast<const __m128i *>(&src));
323 vs = multiplyAlpha255(rgba64: vs, alpha255: const_alpha);
324 const __m128i via = _mm_xor_si128(a: _mm_set1_epi16(w: -1), _mm_shufflelo_epi16(vs, _MM_SHUFFLE(3, 3, 3, 3)));
325 const __m128i vr = _mm_add_epi16(a: vs, b: multiplyAlpha65535(rgba64: vd, va: via));
326 _mm_storel_epi64(p: reinterpret_cast<__m128i *>(&dst), a: vr);
327#else
328 src = multiplyAlpha255(src, const_alpha);
329 dst = src + multiplyAlpha65535(dst, 65535 - src.alpha());
330#endif
331 }
332}
333
334QT_END_NAMESPACE
335
336#endif // QRGBA64_P_H
337

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