Debian Codegraph @

src/core/transforms/matrix4x4_avx2_p.h

raw symbols SCIP
1 // Copyright (C) 2016 Paul Lemire <[email protected]>
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 QT3DCORE_MATRIX4X4_AVX2_P_H
5 #define QT3DCORE_MATRIX4X4_AVX2_P_H
6
7 //
8 // W A R N I N G
9 // -------------
10 //
11 // This file is not part of the Qt3D 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 <Qt3DCore/private/vector4d_p.h>
19 #include <Qt3DCore/private/vector3d_p.h>
20 #include <private/qsimd_p.h>
21 #include <QMatrix4x4>
22
23 #ifdef __AVX2__
24
25 // Some GCC versions don't have _mm256_set_m128 available
26 // Work around that
27 #ifndef _mm256_set_m128
28 #define _mm256_set_m128(va, vb) \
29 _mm256_insertf128_ps(_mm256_castps128_ps256(vb), va, 1)
30 #endif
31
32 QT_BEGIN_NAMESPACE
33
34 namespace Qt3DCore {
35
36 class Matrix4x4_SSE
37 {
38 public:
39
40 Q_ALWAYS_INLINE Matrix4x4_SSE() { setToIdentity(); }
41 explicit Q_ALWAYS_INLINE Matrix4x4_SSE(Qt::Initialization) {}
42
43 // Assumes data is 32 bytes aligned (and in column major order)
44 explicit Q_ALWAYS_INLINE Matrix4x4_SSE(float *data)
45 {
46 m_col12 = _mm256_load_ps(data);
47 m_col34 = _mm256_load_ps(data + 8);
48 }
49
50 // QMatrix4x4::constData returns in column major order
51 explicit Q_ALWAYS_INLINE Matrix4x4_SSE(const QMatrix4x4 &mat)
52 {
53 // data may not be properly aligned, using unaligned loads
54 const float *data = mat.constData();
55 m_col12 = _mm256_loadu_ps(data);
56 m_col34 = _mm256_loadu_ps(data + 8);
57 }
58
59 // In (row major) but we store in column major order
60 explicit Q_ALWAYS_INLINE Matrix4x4_SSE(float m11, float m12, float m13, float m14,
61 float m21, float m22, float m23, float m24,
62 float m31, float m32, float m33, float m34,
63 float m41, float m42, float m43, float m44)
64 {
65 m_col12 = _mm256_set_ps(m42, m32, m22, m12, m41, m31, m21, m11);
66 m_col34 = _mm256_set_ps(m44, m34, m24, m14, m43, m33, m23, m13);
67 }
68
69 Q_ALWAYS_INLINE void setToIdentity()
70 {
71 // 23 instructions
72 m_col12 = _mm256_set_ps(0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f);
73 m_col34 = _mm256_set_ps(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f);
74
75 // 23 instructions
76 // 1, 0, 0, 0
77 // __m128 vec = _mm_set_ss(1.0f);
78 // // 0, 1, 0, 0
79 // // 0b01010001 == 0x51
80 // __m128 tmp = _mm_permute_ps(vec, 0x51);
81
82 // // 1, 0, 0, 0, 0, 1, 0, 0
83 // m_col12 = _mm256_set_m128(tmp, vec);
84
85 // // 0, 0, 1, 0
86 // // 0b01000101 == 0x45
87 // tmp = _mm_permute_ps(vec, 0x45);
88
89 // // 0, 0, 0, 1
90 // // 0b00010101 == 0x15
91 // vec = _mm_permute_ps(vec, 0x15);
92
93 // // 0, 0, 1, 0, 0, 0, 0, 1
94 // m_col34 = _mm256_set_m128(vec, tmp);
95
96 // Using a static identity matrix and assigning it is 27 instructions
97 }
98
99 Q_ALWAYS_INLINE Matrix4x4_SSE operator*(const Matrix4x4_SSE &other) const
100 {
101 // Shuffling: (Latency 1)
102 // (8 bits -> first two pairs used to select from the first vector, second pairs from second vector)
103
104 // 00 01 10 11, 00 01 10 11
105 // v1 = m11 m12 m13 m14 m21 m22 m23 m24
106 // v2 = m11 m12 m13 m14 m21 m22 m23 m24
107
108 // shuffled with 00 00 00 00
109 // v1[0] v1[0] v2[0] v2[0] v1[0] v1[0] v2[0] v2[0]
110 // -> m11 m11 m11 m11 m21 m21 m21 m21
111
112 // Broadcasting: (Latency 1)
113 // -> n11 n12 n13 n14 broadcasted
114 // n11 n12 n13 n14 n11 n12 n13 n14
115
116 // Multiplying (Latency 5):
117 // m11 m11 m11 m11 m21 m21 m21 m21 * n11 n12 n13 n14 n11 n12 n13 n14
118
119 // -> m11 n11, m11 n12, m11 n13, m11 n14
120 // m21 n11, m21 n12, m21 n13, m21 n14
121
122 // 00 01 10 11, 00 01 10 11
123 // v1 = m11 m12 m13 m14 m21 m22 m23 m24
124 // v2 = m11 m12 m13 m14 m21 m22 m23 m24
125 const __m256 otherCol12 = other.m_col12;
126 const __m256 otherCol34 = other.m_col34;
127 const __m128 col1 = _mm256_extractf128_ps(m_col12, 0);
128 const __m128 col2 = _mm256_extractf128_ps(m_col12, 1);
129 const __m128 col3 = _mm256_extractf128_ps(m_col34, 0);
130 const __m128 col4 = _mm256_extractf128_ps(m_col34, 1);
131
132 // const __m256 col12 = _mm256_load_ps(m);
133 // const __m256 col34 = _mm256_load_ps(m + 8);
134 // const __m128 otherCol1 = _mm_load_ps(other.m);
135 // const __m128 otherCol2 = _mm_load_ps(other.m + 4);
136 // const __m128 otherCol3 = _mm_load_ps(other.m + 8);
137 // const __m128 otherCol4 = _mm_load_ps(other.m + 12);
138
139 __m256 tmp = _mm256_mul_ps(_mm256_shuffle_ps(otherCol12, otherCol12, 0x00), _mm256_broadcast_ps(&col1));
140
141 // shuffled with 01 01 01 01
142 // v1[1] v1[1] v2[1] v2[1] v1[1] v1[1] v2[1] v2[1]
143 // -> m12 m12 m12 m12 m22 m22 m22 m22
144
145
146 // 00 01 10 11, 00 01 10 11
147 // m11 m12 m13 m14, m21 m22 m23 m24 shuffled with 01 01 01 01
148 // -> m12 m12 m12 m12 m22 m22 m22 m22 x n21 n22 n23 n24 n21 n22 n23 n24
149
150 // -> m12 n21, m12 n22, m12 n23, m12 n24,
151 // -> m22 n21, m22 n22, m22 n23, m22 n24
152 tmp = _mm256_add_ps(_mm256_mul_ps(_mm256_shuffle_ps(otherCol12, otherCol12, 0x55), _mm256_broadcast_ps(&col2)), tmp);
153
154 // m11 m12 m13 m14 m11 m12 m13 m14 shuffled with 10 10 10 10
155 // m13 m13 m13 m13, m23 m23 m23 m23
156
157 // Multiplying with other.col3
158 // -> m13 n31, m13 n32, m13 n33, m13 n34
159 // -> m23 n31, m23 n32, m23 n33, m23 n34
160 tmp = _mm256_add_ps(_mm256_mul_ps(_mm256_shuffle_ps(otherCol12, otherCol12, 0xaa), _mm256_broadcast_ps(&col3)), tmp);
161
162 // m11 m12 m13 m14 m11 m12 m13 m14 shuffled with 11 11 11 11
163 // m14 m14 m14 m14 m24 m24 m24 m24
164
165 // -> m14 n41, m14 n42, m14 n43, m14 n44
166 // -> m24 n41, m24 n42, m24 n43, m24 n44
167 tmp = _mm256_add_ps(_mm256_mul_ps(_mm256_shuffle_ps(otherCol12, otherCol12, 0xff), _mm256_broadcast_ps(&col4)), tmp);
168
169 // Which finally gives
170 // c11 -> m11 n11 + m12 n21 + m13 n31 + m14 n41,
171 // c12 -> m11 n12 + m12 n22 + m13 n32 + m14 n42
172 // c13 -> m11 n13 + m12 n23 + m13 n33 + m14 n43
173 // c14 -> m11 n14 + m12 n24 + m13 n34 + m14 n44
174
175 // c21 -> m21 n11 + m22 n21 + m23 n31 + m24 n41,
176 // c12 -> m21 n12 + m22 n22 + m23 n32 + m24 n42
177 // c13 -> m21 n13 + m22 n23 + m23 n33 + m24 n43
178 // c14 -> m21 n14 + m22 n24 + m23 n34 + m24 n44
179
180 __m256 tmp2 = _mm256_mul_ps(_mm256_shuffle_ps(otherCol34, otherCol34, 0x00), _mm256_broadcast_ps(&col1));
181 tmp2 = _mm256_add_ps(_mm256_mul_ps(_mm256_shuffle_ps(otherCol34, otherCol34, 0x55), _mm256_broadcast_ps(&col2)), tmp2);
182 tmp2 = _mm256_add_ps(_mm256_mul_ps(_mm256_shuffle_ps(otherCol34, otherCol34, 0xaa), _mm256_broadcast_ps(&col3)), tmp2);
183 tmp2 = _mm256_add_ps(_mm256_mul_ps(_mm256_shuffle_ps(otherCol34, otherCol34, 0xff), _mm256_broadcast_ps(&col4)), tmp2);
184
185 Matrix4x4_SSE c(Qt::Uninitialized);
186 c.m_col12 = tmp;
187 c.m_col34 = tmp2;
188 return c;
189 }
190
191 Q_ALWAYS_INLINE Matrix4x4_SSE operator-(const Matrix4x4_SSE &other) const
192 {
193 Matrix4x4_SSE c(Qt::Uninitialized);
194
195 c.m_col12 = _mm256_sub_ps(m_col12, other.m_col12);
196 c.m_col34 = _mm256_sub_ps(m_col34, other.m_col34);
197 return c;
198 }
199
200 Q_ALWAYS_INLINE Matrix4x4_SSE operator+(const Matrix4x4_SSE &other) const
201 {
202 Matrix4x4_SSE c(Qt::Uninitialized);
203
204 c.m_col12 = _mm256_add_ps(m_col12, other.m_col12);
205 c.m_col34 = _mm256_add_ps(m_col34, other.m_col34);
206 return c;
207 }
208
209 Q_ALWAYS_INLINE Matrix4x4_SSE &operator*=(const Matrix4x4_SSE &other)
210 {
211 *this = *this * other;
212 return *this;
213 }
214
215 Q_ALWAYS_INLINE Matrix4x4_SSE &operator-=(const Matrix4x4_SSE &other)
216 {
217 *this = *this - other;
218 return *this;
219 }
220
221 Q_ALWAYS_INLINE Matrix4x4_SSE &operator+=(const Matrix4x4_SSE &other)
222 {
223 *this = *this + other;
224 return *this;
225 }
226
227 Q_ALWAYS_INLINE Matrix4x4_SSE transposed() const
228 {
229 Matrix4x4_SSE c(Qt::Uninitialized);
230 const __m128 col1 = _mm256_extractf128_ps(m_col12, 0);
231 const __m128 col2 = _mm256_extractf128_ps(m_col12, 1);
232 const __m128 col3 = _mm256_extractf128_ps(m_col34, 0);
233 const __m128 col4 = _mm256_extractf128_ps(m_col34, 1);
234
235 // ~117 instructions
236 // Matrix4x4_SSE c = *this;
237 // _MM_TRANSPOSE4_PS(c.m_col1, c.m_col2, c.m_col3, c.m_col4);
238
239 // ~131 instructions - AVX2
240 // const __m256i indexes = _mm256_set_epi32(7, 3, 6, 2, 5, 1, 4, 0);
241 // c.m_col12 = _mm256_permutevar8x32_ps(_mm256_unpacklo_ps(m_col12, m_col34), indexes);
242 // c.m_col34 = _mm256_permutevar8x32_ps(_mm256_unpackhi_ps(m_col12, m_col34), indexes);
243
244 // ~193 instructions
245 // c.m_col12 = _mm256_setr_ps(m_m11, m_m21, m_m31, m_m41, m_m12, m_m22, m_m32, m_m42);
246 // c.m_col34 = _mm256_setr_ps(m_m13, m_m23, m_m33, m_m43, m_m14, m_m24, m_m34, m_m44);
247
248 // ~113 instructions
249 // union {
250 // struct
251 // {
252 // __m256 twin;
253 // };
254 // struct
255 // {
256 // __m128 col1;
257 // __m128 col2;
258 // };
259 // } u;
260
261 // u.twin = _mm256_shuffle_ps(m_col12, m_col34, 0b01000100);
262 // c.m_col1 = _mm_permute_ps(_mm_shuffle_ps(u.col1, u.col2, 0b10001000), 0b11011000);
263 // c.m_col2 = _mm_permute_ps(_mm_shuffle_ps(u.col1, u.col2, 0b11011101), 0b11011000);
264
265 // u.twin = _mm256_shuffle_ps(m_col12, m_col34, 0b11101110);
266 // c.m_col3 = _mm_permute_ps(_mm_shuffle_ps(u.col1, u.col2, 0b10001000), 0b11011000);
267 // c.m_col4 = _mm_permute_ps(_mm_shuffle_ps(u.col1, u.col2, 0b11011101), 0b11011000);
268
269 // ~113 instructions
270 // 0b11011101 == 0xdd
271 // 0b10001000 == 0x88
272 const __m128 tmp1 = _mm_shuffle_ps(col1, col2, 0xdd);
273 const __m128 tmp2 = _mm_shuffle_ps(col1, col2, 0x88);
274 const __m128 tmp3 = _mm_shuffle_ps(col3, col4, 0xdd);
275 const __m128 tmp4 = _mm_shuffle_ps(col3, col4, 0x88);
276 c.m_col12 = _mm256_set_m128(_mm_shuffle_ps(tmp1, tmp3, 0x88), _mm_shuffle_ps(tmp2, tmp4, 0x88));
277 c.m_col34 = _mm256_set_m128(_mm_shuffle_ps(tmp1, tmp3, 0xdd), _mm_shuffle_ps(tmp2, tmp4, 0xdd));
278
279 return c;
280 }
281
282 Q_ALWAYS_INLINE Matrix4x4_SSE inverted() const
283 {
284 // TO DO: Optimize
285 const QMatrix4x4 mat = toQMatrix4x4();
286 return Matrix4x4_SSE(mat.inverted());
287 }
288
289 Q_ALWAYS_INLINE bool operator==(const Matrix4x4_SSE &other) const
290 {
291 // cmp returns (-1, -1, -1, -1, -1, -1, -1, -1) if the two m256 are equals
292 // movemask takes the most significant bits (8x 1 in this case) which equals 0xff
293 return (_mm256_movemask_ps(_mm256_cmp_ps(m_col12, other.m_col12, _CMP_EQ_OQ)) == 0xff &&
294 _mm256_movemask_ps(_mm256_cmp_ps(m_col34, other.m_col34, _CMP_EQ_OQ)) == 0xff);
295
296 }
297
298 Q_ALWAYS_INLINE bool operator!=(const Matrix4x4_SSE &other) const
299 {
300 return !(*this == other);
301 }
302
303 // For some reason _mm256_cvtss_f32 doesn't seem to be defined
304 Q_ALWAYS_INLINE float m11() const { return _mm_cvtss_f32(_mm256_extractf128_ps(m_col12, 0)); }
305 Q_ALWAYS_INLINE float m12() const { return _mm_cvtss_f32(_mm256_extractf128_ps(m_col12, 1)); }
306 Q_ALWAYS_INLINE float m13() const { return _mm_cvtss_f32(_mm256_extractf128_ps(m_col34, 0)); }
307 Q_ALWAYS_INLINE float m14() const { return _mm_cvtss_f32(_mm256_extractf128_ps(m_col34, 1)); }
308
309 Q_ALWAYS_INLINE float m21() const
310 {
311 // 0b01010101 = 0x55
312 const __m128 v = _mm256_extractf128_ps(m_col12, 0);
313 return _mm_cvtss_f32(_mm_shuffle_ps(v, v, 0x55));
314 }
315 Q_ALWAYS_INLINE float m22() const
316 {
317 // 0b01010101 = 0x55
318 const __m128 v = _mm256_extractf128_ps(m_col12, 1);
319 return _mm_cvtss_f32(_mm_shuffle_ps(v, v, 0x55));
320 }
321 Q_ALWAYS_INLINE float m23() const
322 {
323 // 0b01010101 = 0x55
324 const __m128 v = _mm256_extractf128_ps(m_col34, 0);
325 return _mm_cvtss_f32(_mm_shuffle_ps(v, v, 0x55));
326 }
327 Q_ALWAYS_INLINE float m24() const
328 {
329 // 0b01010101 = 0x55
330 const __m128 v = _mm256_extractf128_ps(m_col34, 1);
331 return _mm_cvtss_f32(_mm_shuffle_ps(v, v, 0x55));
332 }
333
334 Q_ALWAYS_INLINE float m31() const
335 {
336 // 0b10101010 = 0xaa
337 const __m128 v = _mm256_extractf128_ps(m_col12, 0);
338 return _mm_cvtss_f32(_mm_shuffle_ps(v, v, 0xaa));
339 }
340 Q_ALWAYS_INLINE float m32() const
341 {
342 // 0b10101010 = 0xaa
343 const __m128 v = _mm256_extractf128_ps(m_col12, 1);
344 return _mm_cvtss_f32(_mm_shuffle_ps(v, v, 0xaa));
345 }
346 Q_ALWAYS_INLINE float m33() const
347 {
348 // 0b10101010 = 0xaa
349 const __m128 v = _mm256_extractf128_ps(m_col34, 0);
350 return _mm_cvtss_f32(_mm_shuffle_ps(v, v, 0xaa));
351 }
352 Q_ALWAYS_INLINE float m34() const
353 {
354 // 0b10101010 = 0xaa
355 const __m128 v = _mm256_extractf128_ps(m_col34, 1);
356 return _mm_cvtss_f32(_mm_shuffle_ps(v, v, 0xaa));
357 }
358
359 Q_ALWAYS_INLINE float m41() const
360 {
361 // 0b11111111 = 0xff
362 const __m128 v = _mm256_extractf128_ps(m_col12, 0);
363 return _mm_cvtss_f32(_mm_shuffle_ps(v, v, 0xff));
364 }
365 Q_ALWAYS_INLINE float m42() const
366 {
367 // 0b11111111 = 0xff
368 const __m128 v = _mm256_extractf128_ps(m_col12, 1);
369 return _mm_cvtss_f32(_mm_shuffle_ps(v, v, 0xff));
370 }
371 Q_ALWAYS_INLINE float m43() const
372 {
373 // 0b11111111 = 0xff
374 const __m128 v = _mm256_extractf128_ps(m_col34, 0);
375 return _mm_cvtss_f32(_mm_shuffle_ps(v, v, 0xff));
376 }
377 Q_ALWAYS_INLINE float m44() const
378 {
379 // 0b11111111 = 0xff
380 const __m128 v = _mm256_extractf128_ps(m_col34, 1);
381 return _mm_cvtss_f32(_mm_shuffle_ps(v, v, 0xff));
382 }
383 Q_ALWAYS_INLINE QMatrix4x4 toQMatrix4x4() const { return QMatrix4x4(m11(), m12(), m13(), m14(),
384 m21(), m22(), m23(), m24(),
385 m31(), m32(), m33(), m34(),
386 m41(), m42(), m43(), m44()); }
387
388 Q_ALWAYS_INLINE Vector4D row(int index) const
389 {
390 switch (index) {
391 case 0:
392 return Vector4D(m11(), m12(), m13(), m14());
393 case 1:
394 return Vector4D(m21(), m22(), m23(), m24());
395 case 2:
396 return Vector4D(m31(), m32(), m33(), m34());
397 case 3:
398 return Vector4D(m41(), m42(), m43(), m44());
399 default:
400 Q_UNREACHABLE();
401 return Vector4D();
402 }
403 }
404
405 Q_ALWAYS_INLINE Vector4D column(int index) const
406 {
407 Vector4D c(Qt::Uninitialized);
408 switch (index) {
409 case 0:
410 c.m_xyzw = _mm256_extractf128_ps(m_col12, 0);
411 break;
412 case 1:
413 c.m_xyzw = _mm256_extractf128_ps(m_col12, 1);
414 break;
415 case 2:
416 c.m_xyzw = _mm256_extractf128_ps(m_col34, 0);
417 break;
418 case 3:
419 c.m_xyzw = _mm256_extractf128_ps(m_col34, 1);
420 break;
421 default:
422 Q_UNREACHABLE();
423 return Vector4D();
424 }
425 return c;
426 }
427
428 Q_ALWAYS_INLINE float operator()(int row, int column) const {
429 return this->row(row)[column];
430 }
431
432 Q_ALWAYS_INLINE Vector3D_SSE map(const Vector3D_SSE &point) const
433 {
434 return *this * point;
435 }
436
437 Q_ALWAYS_INLINE Vector4D_SSE map(const Vector4D_SSE &point) const
438 {
439 return *this * point;
440 }
441
442 Vector3D_SSE mapVector(const Vector3D_SSE &vector) const
443 {
444 const Vector3D_SSE row1(m11(), m12(), m13());
445 const Vector3D_SSE row2(m21(), m22(), m23());
446 const Vector3D_SSE row3(m31(), m32(), m33());
447
448 return Vector3D(Vector3D_SSE::dotProduct(row1, vector),
449 Vector3D_SSE::dotProduct(row2, vector),
450 Vector3D_SSE::dotProduct(row3, vector));
451 }
452
453 friend Vector4D operator*(const Vector4D &vector, const Matrix4x4_SSE &matrix);
454 friend Vector4D operator*(const Matrix4x4_SSE &matrix, const Vector4D &vector);
455
456 friend Vector3D operator*(const Vector3D &vector, const Matrix4x4_SSE &matrix);
457 friend Vector3D operator*(const Matrix4x4_SSE &matrix, const Vector3D &vector);
458
459 friend Q_3DCORE_PRIVATE_EXPORT QDebug operator<<(QDebug dbg, const Matrix4x4_SSE &m);
460
461 private:
462 // column major order
463 // aligned on 32 bytes boundaries for AVX, compatible with 16 bytes boundary for SSE
464 // union Q_DECL_ALIGN(32)
465 // {
466 // float m[16];
467 // struct
468 // {
469 // float m_m11, m_m21, m_m31, m_m41;
470 // float m_m12, m_m22, m_m32, m_m42;
471 // float m_m13, m_m23, m_m33, m_m43;
472 // float m_m14, m_m24, m_m34, m_m44;
473 // };
474 // };
475 __m256 m_col12;
476 __m256 m_col34;
477 };
478
479 Q_ALWAYS_INLINE Vector4D operator*(const Vector4D &vector, const Matrix4x4_SSE &matrix)
480 {
481 const __m256 vecMultiplier = _mm256_broadcast_ps(&vector.m_xyzw);
482 // a1 a2 a3 a4 b1 b2 b3 b4, c1 c2 c3 c4 d1 d2 d3 d4
483 // a1 + a2, a3 + a4, c1 + c2, c3 + c4
484 // b1 + b2, b3 + b3, d1 + d2, d3 + d4
485
486 const __m256 partialSum = _mm256_hadd_ps(_mm256_mul_ps(matrix.m_col12, vecMultiplier),
487 _mm256_mul_ps(matrix.m_col34, vecMultiplier));
488
489 Vector4D v(Qt::Uninitialized);
490 // a12 + a34, b12 + b34, c12 + c34, d12 + d34
491 // _mm256_permute4x64_pd is AVX2
492 // 0b11011000 == 0xd8
493 const __m256 shuffledSum = _mm256_castpd_ps(_mm256_permute4x64_pd(_mm256_castps_pd(partialSum), 0xd8));
494 v.m_xyzw = _mm_hadd_ps(_mm256_extractf128_ps(shuffledSum, 0), _mm256_extractf128_ps(shuffledSum, 1));
495 return v;
496 }
497
498 Q_ALWAYS_INLINE Vector4D operator*(const Matrix4x4_SSE &matrix, const Vector4D &vector)
499 {
500 const Matrix4x4_SSE transposed = matrix.transposed();
501 return vector * transposed;
502 }
503
504 Q_ALWAYS_INLINE Vector3D operator*(const Vector3D &vector, const Matrix4x4_SSE &matrix)
505 {
506 const __m128 vec4 = _mm_set_ps(1.0f, vector.z(), vector.y(), vector.x());
507 const __m256 vecMultiplier = _mm256_broadcast_ps(&vec4);
508 // a1 a2 a3 a4 b1 b2 b3 b4, c1 c2 c3 c4 d1 d2 d3 d4
509 // a1 + a2, a3 + a4, c1 + c2, c3 + c4
510 // b1 + b2, b3 + b3, d1 + d2, d3 + d4
511 const __m256 partialSum = _mm256_hadd_ps(_mm256_mul_ps(matrix.m_col12, vecMultiplier),
512 _mm256_mul_ps(matrix.m_col34, vecMultiplier));
513
514 // _mm256_permute4x64_pd is AVX2
515 // 0b11011000 == 0xd8
516 const __m256 shuffledSum = _mm256_castpd_ps(_mm256_permute4x64_pd(_mm256_castps_pd(partialSum), 0xd8));
517 // a12 + a34, b12 + b34, c12 + c34, d12 + d34
518 const __m128 result = _mm_hadd_ps(_mm256_extractf128_ps(shuffledSum, 0), _mm256_extractf128_ps(shuffledSum, 1));
519 // 0b11111111 = 0xff
520 const __m128 divisor = _mm_shuffle_ps(result, result, 0xff);
521
522 Vector3D v(Qt::Uninitialized);
523 v.m_xyzw = _mm_div_ps(result, divisor);;
524 return v;
525 }
526
527 Q_ALWAYS_INLINE Vector3D operator*(const Matrix4x4_SSE &matrix, const Vector3D &vector)
528 {
529 const Matrix4x4_SSE transposed = matrix.transposed();
530 return vector * transposed;
531 }
532
533 } // Qt3DCore
534
535 Q_DECLARE_TYPEINFO(Qt3DCore::Matrix4x4_SSE, Q_PRIMITIVE_TYPE);
536
537 QT_END_NAMESPACE
538
539 Q_DECLARE_METATYPE(Qt3DCore::Matrix4x4_SSE)
540
541 #endif // __AVX2__
542
543 #endif // QT3DCORE_MATRIX4X4_AVX2_P_H