Debian Codegraph @

test/Bullet2/vectormath/sse/mat_aos.h

raw symbols SCIP
1 /*
2 Copyright (C) 2006, 2007 Sony Computer Entertainment Inc.
3 All rights reserved.
4
5 Redistribution and use in source and binary forms,
6 with or without modification, are permitted provided that the
7 following conditions are met:
8 * Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13 * Neither the name of the Sony Computer Entertainment Inc nor the names
14 of its contributors may be used to endorse or promote products derived
15 from this software without specific prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #ifndef _VECTORMATH_MAT_AOS_CPP_H
31 #define _VECTORMATH_MAT_AOS_CPP_H
32
33 namespace Vectormath
34 {
35 namespace Aos
36 {
37 //-----------------------------------------------------------------------------
38 // Constants
39 // for shuffles, words are labeled [x,y,z,w] [a,b,c,d]
40
41 #define _VECTORMATH_PERM_ZBWX ((vec_uchar16)(vec_uint4){_VECTORMATH_PERM_Z, _VECTORMATH_PERM_B, _VECTORMATH_PERM_W, _VECTORMATH_PERM_X})
42 #define _VECTORMATH_PERM_XCYX ((vec_uchar16)(vec_uint4){_VECTORMATH_PERM_X, _VECTORMATH_PERM_C, _VECTORMATH_PERM_Y, _VECTORMATH_PERM_X})
43 #define _VECTORMATH_PERM_XYAB ((vec_uchar16)(vec_uint4){_VECTORMATH_PERM_X, _VECTORMATH_PERM_Y, _VECTORMATH_PERM_A, _VECTORMATH_PERM_B})
44 #define _VECTORMATH_PERM_ZWCD ((vec_uchar16)(vec_uint4){_VECTORMATH_PERM_Z, _VECTORMATH_PERM_W, _VECTORMATH_PERM_C, _VECTORMATH_PERM_D})
45 #define _VECTORMATH_PERM_XZBX ((vec_uchar16)(vec_uint4){_VECTORMATH_PERM_X, _VECTORMATH_PERM_Z, _VECTORMATH_PERM_B, _VECTORMATH_PERM_X})
46 #define _VECTORMATH_PERM_CXXX ((vec_uchar16)(vec_uint4){_VECTORMATH_PERM_C, _VECTORMATH_PERM_X, _VECTORMATH_PERM_X, _VECTORMATH_PERM_X})
47 #define _VECTORMATH_PERM_YAXX ((vec_uchar16)(vec_uint4){_VECTORMATH_PERM_Y, _VECTORMATH_PERM_A, _VECTORMATH_PERM_X, _VECTORMATH_PERM_X})
48 #define _VECTORMATH_PERM_XAZC ((vec_uchar16)(vec_uint4){_VECTORMATH_PERM_X, _VECTORMATH_PERM_A, _VECTORMATH_PERM_Z, _VECTORMATH_PERM_C})
49 #define _VECTORMATH_PERM_YXWZ ((vec_uchar16)(vec_uint4){_VECTORMATH_PERM_Y, _VECTORMATH_PERM_X, _VECTORMATH_PERM_W, _VECTORMATH_PERM_Z})
50 #define _VECTORMATH_PERM_YBWD ((vec_uchar16)(vec_uint4){_VECTORMATH_PERM_Y, _VECTORMATH_PERM_B, _VECTORMATH_PERM_W, _VECTORMATH_PERM_D})
51 #define _VECTORMATH_PERM_XYCX ((vec_uchar16)(vec_uint4){_VECTORMATH_PERM_X, _VECTORMATH_PERM_Y, _VECTORMATH_PERM_C, _VECTORMATH_PERM_X})
52 #define _VECTORMATH_PERM_YCXY ((vec_uchar16)(vec_uint4){_VECTORMATH_PERM_Y, _VECTORMATH_PERM_C, _VECTORMATH_PERM_X, _VECTORMATH_PERM_Y})
53 #define _VECTORMATH_PERM_CXYC ((vec_uchar16)(vec_uint4){_VECTORMATH_PERM_C, _VECTORMATH_PERM_X, _VECTORMATH_PERM_Y, _VECTORMATH_PERM_C})
54 #define _VECTORMATH_PERM_ZAYX ((vec_uchar16)(vec_uint4){_VECTORMATH_PERM_Z, _VECTORMATH_PERM_A, _VECTORMATH_PERM_Y, _VECTORMATH_PERM_X})
55 #define _VECTORMATH_PERM_BZXX ((vec_uchar16)(vec_uint4){_VECTORMATH_PERM_B, _VECTORMATH_PERM_Z, _VECTORMATH_PERM_X, _VECTORMATH_PERM_X})
56 #define _VECTORMATH_PERM_XZYA ((vec_uchar16)(vec_uint4){_VECTORMATH_PERM_X, _VECTORMATH_PERM_Z, _VECTORMATH_PERM_Y, _VECTORMATH_PERM_A})
57 #define _VECTORMATH_PERM_ZXXB ((vec_uchar16)(vec_uint4){_VECTORMATH_PERM_Z, _VECTORMATH_PERM_X, _VECTORMATH_PERM_X, _VECTORMATH_PERM_B})
58 #define _VECTORMATH_PERM_YXXC ((vec_uchar16)(vec_uint4){_VECTORMATH_PERM_Y, _VECTORMATH_PERM_X, _VECTORMATH_PERM_X, _VECTORMATH_PERM_C})
59 #define _VECTORMATH_PERM_BBYX ((vec_uchar16)(vec_uint4){_VECTORMATH_PERM_B, _VECTORMATH_PERM_B, _VECTORMATH_PERM_Y, _VECTORMATH_PERM_X})
60 #define _VECTORMATH_PI_OVER_2 1.570796327f
61
62 //-----------------------------------------------------------------------------
63 // Definitions
64
65 VECTORMATH_FORCE_INLINE Matrix3::Matrix3(const Matrix3 &mat)
66 {
67 mCol0 = mat.mCol0;
68 mCol1 = mat.mCol1;
69 mCol2 = mat.mCol2;
70 }
71
72 VECTORMATH_FORCE_INLINE Matrix3::Matrix3(float scalar)
73 {
74 mCol0 = Vector3(scalar);
75 mCol1 = Vector3(scalar);
76 mCol2 = Vector3(scalar);
77 }
78
79 VECTORMATH_FORCE_INLINE Matrix3::Matrix3(const floatInVec &scalar)
80 {
81 mCol0 = Vector3(scalar);
82 mCol1 = Vector3(scalar);
83 mCol2 = Vector3(scalar);
84 }
85
86 VECTORMATH_FORCE_INLINE Matrix3::Matrix3(const Quat &unitQuat)
87 {
88 __m128 xyzw_2, wwww, yzxw, zxyw, yzxw_2, zxyw_2;
89 __m128 tmp0, tmp1, tmp2, tmp3, tmp4, tmp5;
90 VM_ATTRIBUTE_ALIGN16 unsigned int sx[4] = {0xffffffff, 0, 0, 0};
91 VM_ATTRIBUTE_ALIGN16 unsigned int sz[4] = {0, 0, 0xffffffff, 0};
92 __m128 select_x = _mm_load_ps((float *)sx);
93 __m128 select_z = _mm_load_ps((float *)sz);
94
95 xyzw_2 = _mm_add_ps(unitQuat.get128(), unitQuat.get128());
96 wwww = _mm_shuffle_ps(unitQuat.get128(), unitQuat.get128(), _MM_SHUFFLE(3, 3, 3, 3));
97 yzxw = _mm_shuffle_ps(unitQuat.get128(), unitQuat.get128(), _MM_SHUFFLE(3, 0, 2, 1));
98 zxyw = _mm_shuffle_ps(unitQuat.get128(), unitQuat.get128(), _MM_SHUFFLE(3, 1, 0, 2));
99 yzxw_2 = _mm_shuffle_ps(xyzw_2, xyzw_2, _MM_SHUFFLE(3, 0, 2, 1));
100 zxyw_2 = _mm_shuffle_ps(xyzw_2, xyzw_2, _MM_SHUFFLE(3, 1, 0, 2));
101
102 tmp0 = _mm_mul_ps(yzxw_2, wwww); // tmp0 = 2yw, 2zw, 2xw, 2w2
103 tmp1 = _mm_sub_ps(_mm_set1_ps(1.0f), _mm_mul_ps(yzxw, yzxw_2)); // tmp1 = 1 - 2y2, 1 - 2z2, 1 - 2x2, 1 - 2w2
104 tmp2 = _mm_mul_ps(yzxw, xyzw_2); // tmp2 = 2xy, 2yz, 2xz, 2w2
105 tmp0 = _mm_add_ps(_mm_mul_ps(zxyw, xyzw_2), tmp0); // tmp0 = 2yw + 2zx, 2zw + 2xy, 2xw + 2yz, 2w2 + 2w2
106 tmp1 = _mm_sub_ps(tmp1, _mm_mul_ps(zxyw, zxyw_2)); // tmp1 = 1 - 2y2 - 2z2, 1 - 2z2 - 2x2, 1 - 2x2 - 2y2, 1 - 2w2 - 2w2
107 tmp2 = _mm_sub_ps(tmp2, _mm_mul_ps(zxyw_2, wwww)); // tmp2 = 2xy - 2zw, 2yz - 2xw, 2xz - 2yw, 2w2 -2w2
108
109 tmp3 = vec_sel(tmp0, tmp1, select_x);
110 tmp4 = vec_sel(tmp1, tmp2, select_x);
111 tmp5 = vec_sel(tmp2, tmp0, select_x);
112 mCol0 = Vector3(vec_sel(tmp3, tmp2, select_z));
113 mCol1 = Vector3(vec_sel(tmp4, tmp0, select_z));
114 mCol2 = Vector3(vec_sel(tmp5, tmp1, select_z));
115 }
116
117 VECTORMATH_FORCE_INLINE Matrix3::Matrix3(const Vector3 &_col0, const Vector3 &_col1, const Vector3 &_col2)
118 {
119 mCol0 = _col0;
120 mCol1 = _col1;
121 mCol2 = _col2;
122 }
123
124 VECTORMATH_FORCE_INLINE Matrix3 &Matrix3::setCol0(const Vector3 &_col0)
125 {
126 mCol0 = _col0;
127 return *this;
128 }
129
130 VECTORMATH_FORCE_INLINE Matrix3 &Matrix3::setCol1(const Vector3 &_col1)
131 {
132 mCol1 = _col1;
133 return *this;
134 }
135
136 VECTORMATH_FORCE_INLINE Matrix3 &Matrix3::setCol2(const Vector3 &_col2)
137 {
138 mCol2 = _col2;
139 return *this;
140 }
141
142 VECTORMATH_FORCE_INLINE Matrix3 &Matrix3::setCol(int col, const Vector3 &vec)
143 {
144 *(&mCol0 + col) = vec;
145 return *this;
146 }
147
148 VECTORMATH_FORCE_INLINE Matrix3 &Matrix3::setRow(int row, const Vector3 &vec)
149 {
150 mCol0.setElem(row, vec.getElem(0));
151 mCol1.setElem(row, vec.getElem(1));
152 mCol2.setElem(row, vec.getElem(2));
153 return *this;
154 }
155
156 VECTORMATH_FORCE_INLINE Matrix3 &Matrix3::setElem(int col, int row, float val)
157 {
158 (*this)[col].setElem(row, val);
159 return *this;
160 }
161
162 VECTORMATH_FORCE_INLINE Matrix3 &Matrix3::setElem(int col, int row, const floatInVec &val)
163 {
164 Vector3 tmpV3_0;
165 tmpV3_0 = this->getCol(col);
166 tmpV3_0.setElem(row, val);
167 this->setCol(col, tmpV3_0);
168 return *this;
169 }
170
171 VECTORMATH_FORCE_INLINE const floatInVec Matrix3::getElem(int col, int row) const
172 {
173 return this->getCol(col).getElem(row);
174 }
175
176 VECTORMATH_FORCE_INLINE const Vector3 Matrix3::getCol0() const
177 {
178 return mCol0;
179 }
180
181 VECTORMATH_FORCE_INLINE const Vector3 Matrix3::getCol1() const
182 {
183 return mCol1;
184 }
185
186 VECTORMATH_FORCE_INLINE const Vector3 Matrix3::getCol2() const
187 {
188 return mCol2;
189 }
190
191 VECTORMATH_FORCE_INLINE const Vector3 Matrix3::getCol(int col) const
192 {
193 return *(&mCol0 + col);
194 }
195
196 VECTORMATH_FORCE_INLINE const Vector3 Matrix3::getRow(int row) const
197 {
198 return Vector3(mCol0.getElem(row), mCol1.getElem(row), mCol2.getElem(row));
199 }
200
201 VECTORMATH_FORCE_INLINE Vector3 &Matrix3::operator[](int col)
202 {
203 return *(&mCol0 + col);
204 }
205
206 VECTORMATH_FORCE_INLINE const Vector3 Matrix3::operator[](int col) const
207 {
208 return *(&mCol0 + col);
209 }
210
211 VECTORMATH_FORCE_INLINE Matrix3 &Matrix3::operator=(const Matrix3 &mat)
212 {
213 mCol0 = mat.mCol0;
214 mCol1 = mat.mCol1;
215 mCol2 = mat.mCol2;
216 return *this;
217 }
218
219 VECTORMATH_FORCE_INLINE const Matrix3 transpose(const Matrix3 &mat)
220 {
221 __m128 tmp0, tmp1, res0, res1, res2;
222 tmp0 = vec_mergeh(mat.getCol0().get128(), mat.getCol2().get128());
223 tmp1 = vec_mergel(mat.getCol0().get128(), mat.getCol2().get128());
224 res0 = vec_mergeh(tmp0, mat.getCol1().get128());
225 //res1 = vec_perm( tmp0, mat.getCol1().get128(), _VECTORMATH_PERM_ZBWX );
226 VM_ATTRIBUTE_ALIGN16 unsigned int select_y[4] = {0, 0xffffffff, 0, 0};
227 res1 = _mm_shuffle_ps(tmp0, tmp0, _MM_SHUFFLE(0, 3, 2, 2));
228 res1 = vec_sel(res1, mat.getCol1().get128(), select_y);
229 //res2 = vec_perm( tmp1, mat.getCol1().get128(), _VECTORMATH_PERM_XCYX );
230 res2 = _mm_shuffle_ps(tmp1, tmp1, _MM_SHUFFLE(0, 1, 1, 0));
231 res2 = vec_sel(res2, vec_splat(mat.getCol1().get128(), 2), select_y);
232 return Matrix3(
233 Vector3(res0),
234 Vector3(res1),
235 Vector3(res2));
236 }
237
238 VECTORMATH_FORCE_INLINE const Matrix3 inverse(const Matrix3 &mat)
239 {
240 __m128 tmp0, tmp1, tmp2, tmp3, tmp4, dot, invdet, inv0, inv1, inv2;
241 tmp2 = _vmathVfCross(mat.getCol0().get128(), mat.getCol1().get128());
242 tmp0 = _vmathVfCross(mat.getCol1().get128(), mat.getCol2().get128());
243 tmp1 = _vmathVfCross(mat.getCol2().get128(), mat.getCol0().get128());
244 dot = _vmathVfDot3(tmp2, mat.getCol2().get128());
245 dot = vec_splat(dot, 0);
246 invdet = recipf4(dot);
247 tmp3 = vec_mergeh(tmp0, tmp2);
248 tmp4 = vec_mergel(tmp0, tmp2);
249 inv0 = vec_mergeh(tmp3, tmp1);
250 //inv1 = vec_perm( tmp3, tmp1, _VECTORMATH_PERM_ZBWX );
251 VM_ATTRIBUTE_ALIGN16 unsigned int select_y[4] = {0, 0xffffffff, 0, 0};
252 inv1 = _mm_shuffle_ps(tmp3, tmp3, _MM_SHUFFLE(0, 3, 2, 2));
253 inv1 = vec_sel(inv1, tmp1, select_y);
254 //inv2 = vec_perm( tmp4, tmp1, _VECTORMATH_PERM_XCYX );
255 inv2 = _mm_shuffle_ps(tmp4, tmp4, _MM_SHUFFLE(0, 1, 1, 0));
256 inv2 = vec_sel(inv2, vec_splat(tmp1, 2), select_y);
257 inv0 = vec_mul(inv0, invdet);
258 inv1 = vec_mul(inv1, invdet);
259 inv2 = vec_mul(inv2, invdet);
260 return Matrix3(
261 Vector3(inv0),
262 Vector3(inv1),
263 Vector3(inv2));
264 }
265
266 VECTORMATH_FORCE_INLINE const floatInVec determinant(const Matrix3 &mat)
267 {
268 return dot(mat.getCol2(), cross(mat.getCol0(), mat.getCol1()));
269 }
270
271 VECTORMATH_FORCE_INLINE const Matrix3 Matrix3::operator+(const Matrix3 &mat) const
272 {
273 return Matrix3(
274 (mCol0 + mat.mCol0),
275 (mCol1 + mat.mCol1),
276 (mCol2 + mat.mCol2));
277 }
278
279 VECTORMATH_FORCE_INLINE const Matrix3 Matrix3::operator-(const Matrix3 &mat) const
280 {
281 return Matrix3(
282 (mCol0 - mat.mCol0),
283 (mCol1 - mat.mCol1),
284 (mCol2 - mat.mCol2));
285 }
286
287 VECTORMATH_FORCE_INLINE Matrix3 &Matrix3::operator+=(const Matrix3 &mat)
288 {
289 *this = *this + mat;
290 return *this;
291 }
292
293 VECTORMATH_FORCE_INLINE Matrix3 &Matrix3::operator-=(const Matrix3 &mat)
294 {
295 *this = *this - mat;
296 return *this;
297 }
298
299 VECTORMATH_FORCE_INLINE const Matrix3 Matrix3::operator-() const
300 {
301 return Matrix3(
302 (-mCol0),
303 (-mCol1),
304 (-mCol2));
305 }
306
307 VECTORMATH_FORCE_INLINE const Matrix3 absPerElem(const Matrix3 &mat)
308 {
309 return Matrix3(
310 absPerElem(mat.getCol0()),
311 absPerElem(mat.getCol1()),
312 absPerElem(mat.getCol2()));
313 }
314
315 VECTORMATH_FORCE_INLINE const Matrix3 Matrix3::operator*(float scalar) const
316 {
317 return *this * floatInVec(scalar);
318 }
319
320 VECTORMATH_FORCE_INLINE const Matrix3 Matrix3::operator*(const floatInVec &scalar) const
321 {
322 return Matrix3(
323 (mCol0 * scalar),
324 (mCol1 * scalar),
325 (mCol2 * scalar));
326 }
327
328 VECTORMATH_FORCE_INLINE Matrix3 &Matrix3::operator*=(float scalar)
329 {
330 return *this *= floatInVec(scalar);
331 }
332
333 VECTORMATH_FORCE_INLINE Matrix3 &Matrix3::operator*=(const floatInVec &scalar)
334 {
335 *this = *this * scalar;
336 return *this;
337 }
338
339 VECTORMATH_FORCE_INLINE const Matrix3 operator*(float scalar, const Matrix3 &mat)
340 {
341 return floatInVec(scalar) * mat;
342 }
343
344 VECTORMATH_FORCE_INLINE const Matrix3 operator*(const floatInVec &scalar, const Matrix3 &mat)
345 {
346 return mat * scalar;
347 }
348
349 VECTORMATH_FORCE_INLINE const Vector3 Matrix3::operator*(const Vector3 &vec) const
350 {
351 __m128 res;
352 __m128 xxxx, yyyy, zzzz;
353 xxxx = vec_splat(vec.get128(), 0);
354 yyyy = vec_splat(vec.get128(), 1);
355 zzzz = vec_splat(vec.get128(), 2);
356 res = vec_mul(mCol0.get128(), xxxx);
357 res = vec_madd(mCol1.get128(), yyyy, res);
358 res = vec_madd(mCol2.get128(), zzzz, res);
359 return Vector3(res);
360 }
361
362 VECTORMATH_FORCE_INLINE const Matrix3 Matrix3::operator*(const Matrix3 &mat) const
363 {
364 return Matrix3(
365 (*this * mat.mCol0),
366 (*this * mat.mCol1),
367 (*this * mat.mCol2));
368 }
369
370 VECTORMATH_FORCE_INLINE Matrix3 &Matrix3::operator*=(const Matrix3 &mat)
371 {
372 *this = *this * mat;
373 return *this;
374 }
375
376 VECTORMATH_FORCE_INLINE const Matrix3 mulPerElem(const Matrix3 &mat0, const Matrix3 &mat1)
377 {
378 return Matrix3(
379 mulPerElem(mat0.getCol0(), mat1.getCol0()),
380 mulPerElem(mat0.getCol1(), mat1.getCol1()),
381 mulPerElem(mat0.getCol2(), mat1.getCol2()));
382 }
383
384 VECTORMATH_FORCE_INLINE const Matrix3 Matrix3::identity()
385 {
386 return Matrix3(
387 Vector3::xAxis(),
388 Vector3::yAxis(),
389 Vector3::zAxis());
390 }
391
392 VECTORMATH_FORCE_INLINE const Matrix3 Matrix3::rotationX(float radians)
393 {
394 return rotationX(floatInVec(radians));
395 }
396
397 VECTORMATH_FORCE_INLINE const Matrix3 Matrix3::rotationX(const floatInVec &radians)
398 {
399 __m128 s, c, res1, res2;
400 __m128 zero;
401 VM_ATTRIBUTE_ALIGN16 unsigned int select_y[4] = {0, 0xffffffff, 0, 0};
402 VM_ATTRIBUTE_ALIGN16 unsigned int select_z[4] = {0, 0, 0xffffffff, 0};
403 zero = _mm_setzero_ps();
404 sincosf4(radians.get128(), &s, &c);
405 res1 = vec_sel(zero, c, select_y);
406 res1 = vec_sel(res1, s, select_z);
407 res2 = vec_sel(zero, negatef4(s), select_y);
408 res2 = vec_sel(res2, c, select_z);
409 return Matrix3(
410 Vector3::xAxis(),
411 Vector3(res1),
412 Vector3(res2));
413 }
414
415 VECTORMATH_FORCE_INLINE const Matrix3 Matrix3::rotationY(float radians)
416 {
417 return rotationY(floatInVec(radians));
418 }
419
420 VECTORMATH_FORCE_INLINE const Matrix3 Matrix3::rotationY(const floatInVec &radians)
421 {
422 __m128 s, c, res0, res2;
423 __m128 zero;
424 VM_ATTRIBUTE_ALIGN16 unsigned int select_x[4] = {0xffffffff, 0, 0, 0};
425 VM_ATTRIBUTE_ALIGN16 unsigned int select_z[4] = {0, 0, 0xffffffff, 0};
426 zero = _mm_setzero_ps();
427 sincosf4(radians.get128(), &s, &c);
428 res0 = vec_sel(zero, c, select_x);
429 res0 = vec_sel(res0, negatef4(s), select_z);
430 res2 = vec_sel(zero, s, select_x);
431 res2 = vec_sel(res2, c, select_z);
432 return Matrix3(
433 Vector3(res0),
434 Vector3::yAxis(),
435 Vector3(res2));
436 }
437
438 VECTORMATH_FORCE_INLINE const Matrix3 Matrix3::rotationZ(float radians)
439 {
440 return rotationZ(floatInVec(radians));
441 }
442
443 VECTORMATH_FORCE_INLINE const Matrix3 Matrix3::rotationZ(const floatInVec &radians)
444 {
445 __m128 s, c, res0, res1;
446 __m128 zero;
447 VM_ATTRIBUTE_ALIGN16 unsigned int select_x[4] = {0xffffffff, 0, 0, 0};
448 VM_ATTRIBUTE_ALIGN16 unsigned int select_y[4] = {0, 0xffffffff, 0, 0};
449 zero = _mm_setzero_ps();
450 sincosf4(radians.get128(), &s, &c);
451 res0 = vec_sel(zero, c, select_x);
452 res0 = vec_sel(res0, s, select_y);
453 res1 = vec_sel(zero, negatef4(s), select_x);
454 res1 = vec_sel(res1, c, select_y);
455 return Matrix3(
456 Vector3(res0),
457 Vector3(res1),
458 Vector3::zAxis());
459 }
460
461 VECTORMATH_FORCE_INLINE const Matrix3 Matrix3::rotationZYX(const Vector3 &radiansXYZ)
462 {
463 __m128 angles, s, negS, c, X0, X1, Y0, Y1, Z0, Z1, tmp;
464 angles = Vector4(radiansXYZ, 0.0f).get128();
465 sincosf4(angles, &s, &c);
466 negS = negatef4(s);
467 Z0 = vec_mergel(c, s);
468 Z1 = vec_mergel(negS, c);
469 VM_ATTRIBUTE_ALIGN16 unsigned int select_xyz[4] = {0xffffffff, 0xffffffff, 0xffffffff, 0};
470 Z1 = vec_and(Z1, _mm_load_ps((float *)select_xyz));
471 Y0 = _mm_shuffle_ps(c, negS, _MM_SHUFFLE(0, 1, 1, 1));
472 Y1 = _mm_shuffle_ps(s, c, _MM_SHUFFLE(0, 1, 1, 1));
473 X0 = vec_splat(s, 0);
474 X1 = vec_splat(c, 0);
475 tmp = vec_mul(Z0, Y1);
476 return Matrix3(
477 Vector3(vec_mul(Z0, Y0)),
478 Vector3(vec_madd(Z1, X1, vec_mul(tmp, X0))),
479 Vector3(vec_nmsub(Z1, X0, vec_mul(tmp, X1))));
480 }
481
482 VECTORMATH_FORCE_INLINE const Matrix3 Matrix3::rotation(float radians, const Vector3 &unitVec)
483 {
484 return rotation(floatInVec(radians), unitVec);
485 }
486
487 VECTORMATH_FORCE_INLINE const Matrix3 Matrix3::rotation(const floatInVec &radians, const Vector3 &unitVec)
488 {
489 __m128 axis, s, c, oneMinusC, axisS, negAxisS, xxxx, yyyy, zzzz, tmp0, tmp1, tmp2;
490 axis = unitVec.get128();
491 sincosf4(radians.get128(), &s, &c);
492 xxxx = vec_splat(axis, 0);
493 yyyy = vec_splat(axis, 1);
494 zzzz = vec_splat(axis, 2);
495 oneMinusC = vec_sub(_mm_set1_ps(1.0f), c);
496 axisS = vec_mul(axis, s);
497 negAxisS = negatef4(axisS);
498 VM_ATTRIBUTE_ALIGN16 unsigned int select_x[4] = {0xffffffff, 0, 0, 0};
499 VM_ATTRIBUTE_ALIGN16 unsigned int select_y[4] = {0, 0xffffffff, 0, 0};
500 VM_ATTRIBUTE_ALIGN16 unsigned int select_z[4] = {0, 0, 0xffffffff, 0};
501 //tmp0 = vec_perm( axisS, negAxisS, _VECTORMATH_PERM_XZBX );
502 tmp0 = _mm_shuffle_ps(axisS, axisS, _MM_SHUFFLE(0, 0, 2, 0));
503 tmp0 = vec_sel(tmp0, vec_splat(negAxisS, 1), select_z);
504 //tmp1 = vec_perm( axisS, negAxisS, _VECTORMATH_PERM_CXXX );
505 tmp1 = vec_sel(vec_splat(axisS, 0), vec_splat(negAxisS, 2), select_x);
506 //tmp2 = vec_perm( axisS, negAxisS, _VECTORMATH_PERM_YAXX );
507 tmp2 = _mm_shuffle_ps(axisS, axisS, _MM_SHUFFLE(0, 0, 0, 1));
508 tmp2 = vec_sel(tmp2, vec_splat(negAxisS, 0), select_y);
509 tmp0 = vec_sel(tmp0, c, select_x);
510 tmp1 = vec_sel(tmp1, c, select_y);
511 tmp2 = vec_sel(tmp2, c, select_z);
512 return Matrix3(
513 Vector3(vec_madd(vec_mul(axis, xxxx), oneMinusC, tmp0)),
514 Vector3(vec_madd(vec_mul(axis, yyyy), oneMinusC, tmp1)),
515 Vector3(vec_madd(vec_mul(axis, zzzz), oneMinusC, tmp2)));
516 }
517
518 VECTORMATH_FORCE_INLINE const Matrix3 Matrix3::rotation(const Quat &unitQuat)
519 {
520 return Matrix3(unitQuat);
521 }
522
523 VECTORMATH_FORCE_INLINE const Matrix3 Matrix3::scale(const Vector3 &scaleVec)
524 {
525 __m128 zero = _mm_setzero_ps();
526 VM_ATTRIBUTE_ALIGN16 unsigned int select_x[4] = {0xffffffff, 0, 0, 0};
527 VM_ATTRIBUTE_ALIGN16 unsigned int select_y[4] = {0, 0xffffffff, 0, 0};
528 VM_ATTRIBUTE_ALIGN16 unsigned int select_z[4] = {0, 0, 0xffffffff, 0};
529 return Matrix3(
530 Vector3(vec_sel(zero, scaleVec.get128(), select_x)),
531 Vector3(vec_sel(zero, scaleVec.get128(), select_y)),
532 Vector3(vec_sel(zero, scaleVec.get128(), select_z)));
533 }
534
535 VECTORMATH_FORCE_INLINE const Matrix3 appendScale(const Matrix3 &mat, const Vector3 &scaleVec)
536 {
537 return Matrix3(
538 (mat.getCol0() * scaleVec.getX()),
539 (mat.getCol1() * scaleVec.getY()),
540 (mat.getCol2() * scaleVec.getZ()));
541 }
542
543 VECTORMATH_FORCE_INLINE const Matrix3 prependScale(const Vector3 &scaleVec, const Matrix3 &mat)
544 {
545 return Matrix3(
546 mulPerElem(mat.getCol0(), scaleVec),
547 mulPerElem(mat.getCol1(), scaleVec),
548 mulPerElem(mat.getCol2(), scaleVec));
549 }
550
551 VECTORMATH_FORCE_INLINE const Matrix3 select(const Matrix3 &mat0, const Matrix3 &mat1, bool select1)
552 {
553 return Matrix3(
554 select(mat0.getCol0(), mat1.getCol0(), select1),
555 select(mat0.getCol1(), mat1.getCol1(), select1),
556 select(mat0.getCol2(), mat1.getCol2(), select1));
557 }
558
559 VECTORMATH_FORCE_INLINE const Matrix3 select(const Matrix3 &mat0, const Matrix3 &mat1, const boolInVec &select1)
560 {
561 return Matrix3(
562 select(mat0.getCol0(), mat1.getCol0(), select1),
563 select(mat0.getCol1(), mat1.getCol1(), select1),
564 select(mat0.getCol2(), mat1.getCol2(), select1));
565 }
566
567 #ifdef _VECTORMATH_DEBUG
568
569 VECTORMATH_FORCE_INLINE void print(const Matrix3 &mat)
570 {
571 print(mat.getRow(0));
572 print(mat.getRow(1));
573 print(mat.getRow(2));
574 }
575
576 VECTORMATH_FORCE_INLINE void print(const Matrix3 &mat, const char *name)
577 {
578 printf("%s:\n", name);
579 print(mat);
580 }
581
582 #endif
583
584 VECTORMATH_FORCE_INLINE Matrix4::Matrix4(const Matrix4 &mat)
585 {
586 mCol0 = mat.mCol0;
587 mCol1 = mat.mCol1;
588 mCol2 = mat.mCol2;
589 mCol3 = mat.mCol3;
590 }
591
592 VECTORMATH_FORCE_INLINE Matrix4::Matrix4(float scalar)
593 {
594 mCol0 = Vector4(scalar);
595 mCol1 = Vector4(scalar);
596 mCol2 = Vector4(scalar);
597 mCol3 = Vector4(scalar);
598 }
599
600 VECTORMATH_FORCE_INLINE Matrix4::Matrix4(const floatInVec &scalar)
601 {
602 mCol0 = Vector4(scalar);
603 mCol1 = Vector4(scalar);
604 mCol2 = Vector4(scalar);
605 mCol3 = Vector4(scalar);
606 }
607
608 VECTORMATH_FORCE_INLINE Matrix4::Matrix4(const Transform3 &mat)
609 {
610 mCol0 = Vector4(mat.getCol0(), 0.0f);
611 mCol1 = Vector4(mat.getCol1(), 0.0f);
612 mCol2 = Vector4(mat.getCol2(), 0.0f);
613 mCol3 = Vector4(mat.getCol3(), 1.0f);
614 }
615
616 VECTORMATH_FORCE_INLINE Matrix4::Matrix4(const Vector4 &_col0, const Vector4 &_col1, const Vector4 &_col2, const Vector4 &_col3)
617 {
618 mCol0 = _col0;
619 mCol1 = _col1;
620 mCol2 = _col2;
621 mCol3 = _col3;
622 }
623
624 VECTORMATH_FORCE_INLINE Matrix4::Matrix4(const Matrix3 &mat, const Vector3 &translateVec)
625 {
626 mCol0 = Vector4(mat.getCol0(), 0.0f);
627 mCol1 = Vector4(mat.getCol1(), 0.0f);
628 mCol2 = Vector4(mat.getCol2(), 0.0f);
629 mCol3 = Vector4(translateVec, 1.0f);
630 }
631
632 VECTORMATH_FORCE_INLINE Matrix4::Matrix4(const Quat &unitQuat, const Vector3 &translateVec)
633 {
634 Matrix3 mat;
635 mat = Matrix3(unitQuat);
636 mCol0 = Vector4(mat.getCol0(), 0.0f);
637 mCol1 = Vector4(mat.getCol1(), 0.0f);
638 mCol2 = Vector4(mat.getCol2(), 0.0f);
639 mCol3 = Vector4(translateVec, 1.0f);
640 }
641
642 VECTORMATH_FORCE_INLINE Matrix4 &Matrix4::setCol0(const Vector4 &_col0)
643 {
644 mCol0 = _col0;
645 return *this;
646 }
647
648 VECTORMATH_FORCE_INLINE Matrix4 &Matrix4::setCol1(const Vector4 &_col1)
649 {
650 mCol1 = _col1;
651 return *this;
652 }
653
654 VECTORMATH_FORCE_INLINE Matrix4 &Matrix4::setCol2(const Vector4 &_col2)
655 {
656 mCol2 = _col2;
657 return *this;
658 }
659
660 VECTORMATH_FORCE_INLINE Matrix4 &Matrix4::setCol3(const Vector4 &_col3)
661 {
662 mCol3 = _col3;
663 return *this;
664 }
665
666 VECTORMATH_FORCE_INLINE Matrix4 &Matrix4::setCol(int col, const Vector4 &vec)
667 {
668 *(&mCol0 + col) = vec;
669 return *this;
670 }
671
672 VECTORMATH_FORCE_INLINE Matrix4 &Matrix4::setRow(int row, const Vector4 &vec)
673 {
674 mCol0.setElem(row, vec.getElem(0));
675 mCol1.setElem(row, vec.getElem(1));
676 mCol2.setElem(row, vec.getElem(2));
677 mCol3.setElem(row, vec.getElem(3));
678 return *this;
679 }
680
681 VECTORMATH_FORCE_INLINE Matrix4 &Matrix4::setElem(int col, int row, float val)
682 {
683 (*this)[col].setElem(row, val);
684 return *this;
685 }
686
687 VECTORMATH_FORCE_INLINE Matrix4 &Matrix4::setElem(int col, int row, const floatInVec &val)
688 {
689 Vector4 tmpV3_0;
690 tmpV3_0 = this->getCol(col);
691 tmpV3_0.setElem(row, val);
692 this->setCol(col, tmpV3_0);
693 return *this;
694 }
695
696 VECTORMATH_FORCE_INLINE const floatInVec Matrix4::getElem(int col, int row) const
697 {
698 return this->getCol(col).getElem(row);
699 }
700
701 VECTORMATH_FORCE_INLINE const Vector4 Matrix4::getCol0() const
702 {
703 return mCol0;
704 }
705
706 VECTORMATH_FORCE_INLINE const Vector4 Matrix4::getCol1() const
707 {
708 return mCol1;
709 }
710
711 VECTORMATH_FORCE_INLINE const Vector4 Matrix4::getCol2() const
712 {
713 return mCol2;
714 }
715
716 VECTORMATH_FORCE_INLINE const Vector4 Matrix4::getCol3() const
717 {
718 return mCol3;
719 }
720
721 VECTORMATH_FORCE_INLINE const Vector4 Matrix4::getCol(int col) const
722 {
723 return *(&mCol0 + col);
724 }
725
726 VECTORMATH_FORCE_INLINE const Vector4 Matrix4::getRow(int row) const
727 {
728 return Vector4(mCol0.getElem(row), mCol1.getElem(row), mCol2.getElem(row), mCol3.getElem(row));
729 }
730
731 VECTORMATH_FORCE_INLINE Vector4 &Matrix4::operator[](int col)
732 {
733 return *(&mCol0 + col);
734 }
735
736 VECTORMATH_FORCE_INLINE const Vector4 Matrix4::operator[](int col) const
737 {
738 return *(&mCol0 + col);
739 }
740
741 VECTORMATH_FORCE_INLINE Matrix4 &Matrix4::operator=(const Matrix4 &mat)
742 {
743 mCol0 = mat.mCol0;
744 mCol1 = mat.mCol1;
745 mCol2 = mat.mCol2;
746 mCol3 = mat.mCol3;
747 return *this;
748 }
749
750 VECTORMATH_FORCE_INLINE const Matrix4 transpose(const Matrix4 &mat)
751 {
752 __m128 tmp0, tmp1, tmp2, tmp3, res0, res1, res2, res3;
753 tmp0 = vec_mergeh(mat.getCol0().get128(), mat.getCol2().get128());
754 tmp1 = vec_mergeh(mat.getCol1().get128(), mat.getCol3().get128());
755 tmp2 = vec_mergel(mat.getCol0().get128(), mat.getCol2().get128());
756 tmp3 = vec_mergel(mat.getCol1().get128(), mat.getCol3().get128());
757 res0 = vec_mergeh(tmp0, tmp1);
758 res1 = vec_mergel(tmp0, tmp1);
759 res2 = vec_mergeh(tmp2, tmp3);
760 res3 = vec_mergel(tmp2, tmp3);
761 return Matrix4(
762 Vector4(res0),
763 Vector4(res1),
764 Vector4(res2),
765 Vector4(res3));
766 }
767
768 // TODO: Tidy
769 static VM_ATTRIBUTE_ALIGN16 const unsigned int _vmathPNPN[4] = {0x00000000, 0x80000000, 0x00000000, 0x80000000};
770 static VM_ATTRIBUTE_ALIGN16 const unsigned int _vmathNPNP[4] = {0x80000000, 0x00000000, 0x80000000, 0x00000000};
771 static VM_ATTRIBUTE_ALIGN16 const float _vmathZERONE[4] = {1.0f, 0.0f, 0.0f, 1.0f};
772
773 VECTORMATH_FORCE_INLINE const Matrix4 inverse(const Matrix4 &mat)
774 {
775 __m128 Va, Vb, Vc;
776 __m128 r1, r2, r3, tt, tt2;
777 __m128 sum, Det, RDet;
778 __m128 trns0, trns1, trns2, trns3;
779
780 __m128 _L1 = mat.getCol0().get128();
781 __m128 _L2 = mat.getCol1().get128();
782 __m128 _L3 = mat.getCol2().get128();
783 __m128 _L4 = mat.getCol3().get128();
784 // Calculating the minterms for the first line.
785
786 // _mm_ror_ps is just a macro using _mm_shuffle_ps().
787 tt = _L4;
788 tt2 = _mm_ror_ps(_L3, 1);
789 Vc = _mm_mul_ps(tt2, _mm_ror_ps(tt, 0)); // V3'dot V4
790 Va = _mm_mul_ps(tt2, _mm_ror_ps(tt, 2)); // V3'dot V4"
791 Vb = _mm_mul_ps(tt2, _mm_ror_ps(tt, 3)); // V3' dot V4^
792
793 r1 = _mm_sub_ps(_mm_ror_ps(Va, 1), _mm_ror_ps(Vc, 2)); // V3" dot V4^ - V3^ dot V4"
794 r2 = _mm_sub_ps(_mm_ror_ps(Vb, 2), _mm_ror_ps(Vb, 0)); // V3^ dot V4' - V3' dot V4^
795 r3 = _mm_sub_ps(_mm_ror_ps(Va, 0), _mm_ror_ps(Vc, 1)); // V3' dot V4" - V3" dot V4'
796
797 tt = _L2;
798 Va = _mm_ror_ps(tt, 1);
799 sum = _mm_mul_ps(Va, r1);
800 Vb = _mm_ror_ps(tt, 2);
801 sum = _mm_add_ps(sum, _mm_mul_ps(Vb, r2));
802 Vc = _mm_ror_ps(tt, 3);
803 sum = _mm_add_ps(sum, _mm_mul_ps(Vc, r3));
804
805 // Calculating the determinant.
806 Det = _mm_mul_ps(sum, _L1);
807 Det = _mm_add_ps(Det, _mm_movehl_ps(Det, Det));
808
809 const __m128 Sign_PNPN = _mm_load_ps((float *)_vmathPNPN);
810 const __m128 Sign_NPNP = _mm_load_ps((float *)_vmathNPNP);
811
812 __m128 mtL1 = _mm_xor_ps(sum, Sign_PNPN);
813
814 // Calculating the minterms of the second line (using previous results).
815 tt = _mm_ror_ps(_L1, 1);
816 sum = _mm_mul_ps(tt, r1);
817 tt = _mm_ror_ps(tt, 1);
818 sum = _mm_add_ps(sum, _mm_mul_ps(tt, r2));
819 tt = _mm_ror_ps(tt, 1);
820 sum = _mm_add_ps(sum, _mm_mul_ps(tt, r3));
821 __m128 mtL2 = _mm_xor_ps(sum, Sign_NPNP);
822
823 // Testing the determinant.
824 Det = _mm_sub_ss(Det, _mm_shuffle_ps(Det, Det, 1));
825
826 // Calculating the minterms of the third line.
827 tt = _mm_ror_ps(_L1, 1);
828 Va = _mm_mul_ps(tt, Vb); // V1' dot V2"
829 Vb = _mm_mul_ps(tt, Vc); // V1' dot V2^
830 Vc = _mm_mul_ps(tt, _L2); // V1' dot V2
831
832 r1 = _mm_sub_ps(_mm_ror_ps(Va, 1), _mm_ror_ps(Vc, 2)); // V1" dot V2^ - V1^ dot V2"
833 r2 = _mm_sub_ps(_mm_ror_ps(Vb, 2), _mm_ror_ps(Vb, 0)); // V1^ dot V2' - V1' dot V2^
834 r3 = _mm_sub_ps(_mm_ror_ps(Va, 0), _mm_ror_ps(Vc, 1)); // V1' dot V2" - V1" dot V2'
835
836 tt = _mm_ror_ps(_L4, 1);
837 sum = _mm_mul_ps(tt, r1);
838 tt = _mm_ror_ps(tt, 1);
839 sum = _mm_add_ps(sum, _mm_mul_ps(tt, r2));
840 tt = _mm_ror_ps(tt, 1);
841 sum = _mm_add_ps(sum, _mm_mul_ps(tt, r3));
842 __m128 mtL3 = _mm_xor_ps(sum, Sign_PNPN);
843
844 // Dividing is FASTER than rcp_nr! (Because rcp_nr causes many register-memory RWs).
845 RDet = _mm_div_ss(_mm_load_ss((float *)&_vmathZERONE), Det); // TODO: just 1.0f?
846 RDet = _mm_shuffle_ps(RDet, RDet, 0x00);
847
848 // Devide the first 12 minterms with the determinant.
849 mtL1 = _mm_mul_ps(mtL1, RDet);
850 mtL2 = _mm_mul_ps(mtL2, RDet);
851 mtL3 = _mm_mul_ps(mtL3, RDet);
852
853 // Calculate the minterms of the forth line and devide by the determinant.
854 tt = _mm_ror_ps(_L3, 1);
855 sum = _mm_mul_ps(tt, r1);
856 tt = _mm_ror_ps(tt, 1);
857 sum = _mm_add_ps(sum, _mm_mul_ps(tt, r2));
858 tt = _mm_ror_ps(tt, 1);
859 sum = _mm_add_ps(sum, _mm_mul_ps(tt, r3));
860 __m128 mtL4 = _mm_xor_ps(sum, Sign_NPNP);
861 mtL4 = _mm_mul_ps(mtL4, RDet);
862
863 // Now we just have to transpose the minterms matrix.
864 trns0 = _mm_unpacklo_ps(mtL1, mtL2);
865 trns1 = _mm_unpacklo_ps(mtL3, mtL4);
866 trns2 = _mm_unpackhi_ps(mtL1, mtL2);
867 trns3 = _mm_unpackhi_ps(mtL3, mtL4);
868 _L1 = _mm_movelh_ps(trns0, trns1);
869 _L2 = _mm_movehl_ps(trns1, trns0);
870 _L3 = _mm_movelh_ps(trns2, trns3);
871 _L4 = _mm_movehl_ps(trns3, trns2);
872
873 return Matrix4(
874 Vector4(_L1),
875 Vector4(_L2),
876 Vector4(_L3),
877 Vector4(_L4));
878 }
879
880 VECTORMATH_FORCE_INLINE const Matrix4 affineInverse(const Matrix4 &mat)
881 {
882 Transform3 affineMat;
883 affineMat.setCol0(mat.getCol0().getXYZ());
884 affineMat.setCol1(mat.getCol1().getXYZ());
885 affineMat.setCol2(mat.getCol2().getXYZ());
886 affineMat.setCol3(mat.getCol3().getXYZ());
887 return Matrix4(inverse(affineMat));
888 }
889
890 VECTORMATH_FORCE_INLINE const Matrix4 orthoInverse(const Matrix4 &mat)
891 {
892 Transform3 affineMat;
893 affineMat.setCol0(mat.getCol0().getXYZ());
894 affineMat.setCol1(mat.getCol1().getXYZ());
895 affineMat.setCol2(mat.getCol2().getXYZ());
896 affineMat.setCol3(mat.getCol3().getXYZ());
897 return Matrix4(orthoInverse(affineMat));
898 }
899
900 VECTORMATH_FORCE_INLINE const floatInVec determinant(const Matrix4 &mat)
901 {
902 __m128 Va, Vb, Vc;
903 __m128 r1, r2, r3, tt, tt2;
904 __m128 sum, Det;
905
906 __m128 _L1 = mat.getCol0().get128();
907 __m128 _L2 = mat.getCol1().get128();
908 __m128 _L3 = mat.getCol2().get128();
909 __m128 _L4 = mat.getCol3().get128();
910 // Calculating the minterms for the first line.
911
912 // _mm_ror_ps is just a macro using _mm_shuffle_ps().
913 tt = _L4;
914 tt2 = _mm_ror_ps(_L3, 1);
915 Vc = _mm_mul_ps(tt2, _mm_ror_ps(tt, 0)); // V3' dot V4
916 Va = _mm_mul_ps(tt2, _mm_ror_ps(tt, 2)); // V3' dot V4"
917 Vb = _mm_mul_ps(tt2, _mm_ror_ps(tt, 3)); // V3' dot V4^
918
919 r1 = _mm_sub_ps(_mm_ror_ps(Va, 1), _mm_ror_ps(Vc, 2)); // V3" dot V4^ - V3^ dot V4"
920 r2 = _mm_sub_ps(_mm_ror_ps(Vb, 2), _mm_ror_ps(Vb, 0)); // V3^ dot V4' - V3' dot V4^
921 r3 = _mm_sub_ps(_mm_ror_ps(Va, 0), _mm_ror_ps(Vc, 1)); // V3' dot V4" - V3" dot V4'
922
923 tt = _L2;
924 Va = _mm_ror_ps(tt, 1);
925 sum = _mm_mul_ps(Va, r1);
926 Vb = _mm_ror_ps(tt, 2);
927 sum = _mm_add_ps(sum, _mm_mul_ps(Vb, r2));
928 Vc = _mm_ror_ps(tt, 3);
929 sum = _mm_add_ps(sum, _mm_mul_ps(Vc, r3));
930
931 // Calculating the determinant.
932 Det = _mm_mul_ps(sum, _L1);
933 Det = _mm_add_ps(Det, _mm_movehl_ps(Det, Det));
934
935 // Calculating the minterms of the second line (using previous results).
936 tt = _mm_ror_ps(_L1, 1);
937 sum = _mm_mul_ps(tt, r1);
938 tt = _mm_ror_ps(tt, 1);
939 sum = _mm_add_ps(sum, _mm_mul_ps(tt, r2));
940 tt = _mm_ror_ps(tt, 1);
941 sum = _mm_add_ps(sum, _mm_mul_ps(tt, r3));
942
943 // Testing the determinant.
944 Det = _mm_sub_ss(Det, _mm_shuffle_ps(Det, Det, 1));
945 return floatInVec(Det, 0);
946 }
947
948 VECTORMATH_FORCE_INLINE const Matrix4 Matrix4::operator+(const Matrix4 &mat) const
949 {
950 return Matrix4(
951 (mCol0 + mat.mCol0),
952 (mCol1 + mat.mCol1),
953 (mCol2 + mat.mCol2),
954 (mCol3 + mat.mCol3));
955 }
956
957 VECTORMATH_FORCE_INLINE const Matrix4 Matrix4::operator-(const Matrix4 &mat) const
958 {
959 return Matrix4(
960 (mCol0 - mat.mCol0),
961 (mCol1 - mat.mCol1),
962 (mCol2 - mat.mCol2),
963 (mCol3 - mat.mCol3));
964 }
965
966 VECTORMATH_FORCE_INLINE Matrix4 &Matrix4::operator+=(const Matrix4 &mat)
967 {
968 *this = *this + mat;
969 return *this;
970 }
971
972 VECTORMATH_FORCE_INLINE Matrix4 &Matrix4::operator-=(const Matrix4 &mat)
973 {
974 *this = *this - mat;
975 return *this;
976 }
977
978 VECTORMATH_FORCE_INLINE const Matrix4 Matrix4::operator-() const
979 {
980 return Matrix4(
981 (-mCol0),
982 (-mCol1),
983 (-mCol2),
984 (-mCol3));
985 }
986
987 VECTORMATH_FORCE_INLINE const Matrix4 absPerElem(const Matrix4 &mat)
988 {
989 return Matrix4(
990 absPerElem(mat.getCol0()),
991 absPerElem(mat.getCol1()),
992 absPerElem(mat.getCol2()),
993 absPerElem(mat.getCol3()));
994 }
995
996 VECTORMATH_FORCE_INLINE const Matrix4 Matrix4::operator*(float scalar) const
997 {
998 return *this * floatInVec(scalar);
999 }
1000
1001 VECTORMATH_FORCE_INLINE const Matrix4 Matrix4::operator*(const floatInVec &scalar) const
1002 {
1003 return Matrix4(
1004 (mCol0 * scalar),
1005 (mCol1 * scalar),
1006 (mCol2 * scalar),
1007 (mCol3 * scalar));
1008 }
1009
1010 VECTORMATH_FORCE_INLINE Matrix4 &Matrix4::operator*=(float scalar)
1011 {
1012 return *this *= floatInVec(scalar);
1013 }
1014
1015 VECTORMATH_FORCE_INLINE Matrix4 &Matrix4::operator*=(const floatInVec &scalar)
1016 {
1017 *this = *this * scalar;
1018 return *this;
1019 }
1020
1021 VECTORMATH_FORCE_INLINE const Matrix4 operator*(float scalar, const Matrix4 &mat)
1022 {
1023 return floatInVec(scalar) * mat;
1024 }
1025
1026 VECTORMATH_FORCE_INLINE const Matrix4 operator*(const floatInVec &scalar, const Matrix4 &mat)
1027 {
1028 return mat * scalar;
1029 }
1030
1031 VECTORMATH_FORCE_INLINE const Vector4 Matrix4::operator*(const Vector4 &vec) const
1032 {
1033 return Vector4(
1034 _mm_add_ps(
1035 _mm_add_ps(_mm_mul_ps(mCol0.get128(), _mm_shuffle_ps(vec.get128(), vec.get128(), _MM_SHUFFLE(0, 0, 0, 0))), _mm_mul_ps(mCol1.get128(), _mm_shuffle_ps(vec.get128(), vec.get128(), _MM_SHUFFLE(1, 1, 1, 1)))),
1036 _mm_add_ps(_mm_mul_ps(mCol2.get128(), _mm_shuffle_ps(vec.get128(), vec.get128(), _MM_SHUFFLE(2, 2, 2, 2))), _mm_mul_ps(mCol3.get128(), _mm_shuffle_ps(vec.get128(), vec.get128(), _MM_SHUFFLE(3, 3, 3, 3))))));
1037 }
1038
1039 VECTORMATH_FORCE_INLINE const Vector4 Matrix4::operator*(const Vector3 &vec) const
1040 {
1041 return Vector4(
1042 _mm_add_ps(
1043 _mm_add_ps(_mm_mul_ps(mCol0.get128(), _mm_shuffle_ps(vec.get128(), vec.get128(), _MM_SHUFFLE(0, 0, 0, 0))), _mm_mul_ps(mCol1.get128(), _mm_shuffle_ps(vec.get128(), vec.get128(), _MM_SHUFFLE(1, 1, 1, 1)))),
1044 _mm_mul_ps(mCol2.get128(), _mm_shuffle_ps(vec.get128(), vec.get128(), _MM_SHUFFLE(2, 2, 2, 2)))));
1045 }
1046
1047 VECTORMATH_FORCE_INLINE const Vector4 Matrix4::operator*(const Point3 &pnt) const
1048 {
1049 return Vector4(
1050 _mm_add_ps(
1051 _mm_add_ps(_mm_mul_ps(mCol0.get128(), _mm_shuffle_ps(pnt.get128(), pnt.get128(), _MM_SHUFFLE(0, 0, 0, 0))), _mm_mul_ps(mCol1.get128(), _mm_shuffle_ps(pnt.get128(), pnt.get128(), _MM_SHUFFLE(1, 1, 1, 1)))),
1052 _mm_add_ps(_mm_mul_ps(mCol2.get128(), _mm_shuffle_ps(pnt.get128(), pnt.get128(), _MM_SHUFFLE(2, 2, 2, 2))), mCol3.get128())));
1053 }
1054
1055 VECTORMATH_FORCE_INLINE const Matrix4 Matrix4::operator*(const Matrix4 &mat) const
1056 {
1057 return Matrix4(
1058 (*this * mat.mCol0),
1059 (*this * mat.mCol1),
1060 (*this * mat.mCol2),
1061 (*this * mat.mCol3));
1062 }
1063
1064 VECTORMATH_FORCE_INLINE Matrix4 &Matrix4::operator*=(const Matrix4 &mat)
1065 {
1066 *this = *this * mat;
1067 return *this;
1068 }
1069
1070 VECTORMATH_FORCE_INLINE const Matrix4 Matrix4::operator*(const Transform3 &tfrm) const
1071 {
1072 return Matrix4(
1073 (*this * tfrm.getCol0()),
1074 (*this * tfrm.getCol1()),
1075 (*this * tfrm.getCol2()),
1076 (*this * Point3(tfrm.getCol3())));
1077 }
1078
1079 VECTORMATH_FORCE_INLINE Matrix4 &Matrix4::operator*=(const Transform3 &tfrm)
1080 {
1081 *this = *this * tfrm;
1082 return *this;
1083 }
1084
1085 VECTORMATH_FORCE_INLINE const Matrix4 mulPerElem(const Matrix4 &mat0, const Matrix4 &mat1)
1086 {
1087 return Matrix4(
1088 mulPerElem(mat0.getCol0(), mat1.getCol0()),
1089 mulPerElem(mat0.getCol1(), mat1.getCol1()),
1090 mulPerElem(mat0.getCol2(), mat1.getCol2()),
1091 mulPerElem(mat0.getCol3(), mat1.getCol3()));
1092 }
1093
1094 VECTORMATH_FORCE_INLINE const Matrix4 Matrix4::identity()
1095 {
1096 return Matrix4(
1097 Vector4::xAxis(),
1098 Vector4::yAxis(),
1099 Vector4::zAxis(),
1100 Vector4::wAxis());
1101 }
1102
1103 VECTORMATH_FORCE_INLINE Matrix4 &Matrix4::setUpper3x3(const Matrix3 &mat3)
1104 {
1105 mCol0.setXYZ(mat3.getCol0());
1106 mCol1.setXYZ(mat3.getCol1());
1107 mCol2.setXYZ(mat3.getCol2());
1108 return *this;
1109 }
1110
1111 VECTORMATH_FORCE_INLINE const Matrix3 Matrix4::getUpper3x3() const
1112 {
1113 return Matrix3(
1114 mCol0.getXYZ(),
1115 mCol1.getXYZ(),
1116 mCol2.getXYZ());
1117 }
1118
1119 VECTORMATH_FORCE_INLINE Matrix4 &Matrix4::setTranslation(const Vector3 &translateVec)
1120 {
1121 mCol3.setXYZ(translateVec);
1122 return *this;
1123 }
1124
1125 VECTORMATH_FORCE_INLINE const Vector3 Matrix4::getTranslation() const
1126 {
1127 return mCol3.getXYZ();
1128 }
1129
1130 VECTORMATH_FORCE_INLINE const Matrix4 Matrix4::rotationX(float radians)
1131 {
1132 return rotationX(floatInVec(radians));
1133 }
1134
1135 VECTORMATH_FORCE_INLINE const Matrix4 Matrix4::rotationX(const floatInVec &radians)
1136 {
1137 __m128 s, c, res1, res2;
1138 __m128 zero;
1139 VM_ATTRIBUTE_ALIGN16 unsigned int select_y[4] = {0, 0xffffffff, 0, 0};
1140 VM_ATTRIBUTE_ALIGN16 unsigned int select_z[4] = {0, 0, 0xffffffff, 0};
1141 zero = _mm_setzero_ps();
1142 sincosf4(radians.get128(), &s, &c);
1143 res1 = vec_sel(zero, c, select_y);
1144 res1 = vec_sel(res1, s, select_z);
1145 res2 = vec_sel(zero, negatef4(s), select_y);
1146 res2 = vec_sel(res2, c, select_z);
1147 return Matrix4(
1148 Vector4::xAxis(),
1149 Vector4(res1),
1150 Vector4(res2),
1151 Vector4::wAxis());
1152 }
1153
1154 VECTORMATH_FORCE_INLINE const Matrix4 Matrix4::rotationY(float radians)
1155 {
1156 return rotationY(floatInVec(radians));
1157 }
1158
1159 VECTORMATH_FORCE_INLINE const Matrix4 Matrix4::rotationY(const floatInVec &radians)
1160 {
1161 __m128 s, c, res0, res2;
1162 __m128 zero;
1163 VM_ATTRIBUTE_ALIGN16 unsigned int select_x[4] = {0xffffffff, 0, 0, 0};
1164 VM_ATTRIBUTE_ALIGN16 unsigned int select_z[4] = {0, 0, 0xffffffff, 0};
1165 zero = _mm_setzero_ps();
1166 sincosf4(radians.get128(), &s, &c);
1167 res0 = vec_sel(zero, c, select_x);
1168 res0 = vec_sel(res0, negatef4(s), select_z);
1169 res2 = vec_sel(zero, s, select_x);
1170 res2 = vec_sel(res2, c, select_z);
1171 return Matrix4(
1172 Vector4(res0),
1173 Vector4::yAxis(),
1174 Vector4(res2),
1175 Vector4::wAxis());
1176 }
1177
1178 VECTORMATH_FORCE_INLINE const Matrix4 Matrix4::rotationZ(float radians)
1179 {
1180 return rotationZ(floatInVec(radians));
1181 }
1182
1183 VECTORMATH_FORCE_INLINE const Matrix4 Matrix4::rotationZ(const floatInVec &radians)
1184 {
1185 __m128 s, c, res0, res1;
1186 __m128 zero;
1187 VM_ATTRIBUTE_ALIGN16 unsigned int select_x[4] = {0xffffffff, 0, 0, 0};
1188 VM_ATTRIBUTE_ALIGN16 unsigned int select_y[4] = {0, 0xffffffff, 0, 0};
1189 zero = _mm_setzero_ps();
1190 sincosf4(radians.get128(), &s, &c);
1191 res0 = vec_sel(zero, c, select_x);
1192 res0 = vec_sel(res0, s, select_y);
1193 res1 = vec_sel(zero, negatef4(s), select_x);
1194 res1 = vec_sel(res1, c, select_y);
1195 return Matrix4(
1196 Vector4(res0),
1197 Vector4(res1),
1198 Vector4::zAxis(),
1199 Vector4::wAxis());
1200 }
1201
1202 VECTORMATH_FORCE_INLINE const Matrix4 Matrix4::rotationZYX(const Vector3 &radiansXYZ)
1203 {
1204 __m128 angles, s, negS, c, X0, X1, Y0, Y1, Z0, Z1, tmp;
1205 angles = Vector4(radiansXYZ, 0.0f).get128();
1206 sincosf4(angles, &s, &c);
1207 negS = negatef4(s);
1208 Z0 = vec_mergel(c, s);
1209 Z1 = vec_mergel(negS, c);
1210 VM_ATTRIBUTE_ALIGN16 unsigned int select_xyz[4] = {0xffffffff, 0xffffffff, 0xffffffff, 0};
1211 Z1 = vec_and(Z1, _mm_load_ps((float *)select_xyz));
1212 Y0 = _mm_shuffle_ps(c, negS, _MM_SHUFFLE(0, 1, 1, 1));
1213 Y1 = _mm_shuffle_ps(s, c, _MM_SHUFFLE(0, 1, 1, 1));
1214 X0 = vec_splat(s, 0);
1215 X1 = vec_splat(c, 0);
1216 tmp = vec_mul(Z0, Y1);
1217 return Matrix4(
1218 Vector4(vec_mul(Z0, Y0)),
1219 Vector4(vec_madd(Z1, X1, vec_mul(tmp, X0))),
1220 Vector4(vec_nmsub(Z1, X0, vec_mul(tmp, X1))),
1221 Vector4::wAxis());
1222 }
1223
1224 VECTORMATH_FORCE_INLINE const Matrix4 Matrix4::rotation(float radians, const Vector3 &unitVec)
1225 {
1226 return rotation(floatInVec(radians), unitVec);
1227 }
1228
1229 VECTORMATH_FORCE_INLINE const Matrix4 Matrix4::rotation(const floatInVec &radians, const Vector3 &unitVec)
1230 {
1231 __m128 axis, s, c, oneMinusC, axisS, negAxisS, xxxx, yyyy, zzzz, tmp0, tmp1, tmp2;
1232 axis = unitVec.get128();
1233 sincosf4(radians.get128(), &s, &c);
1234 xxxx = vec_splat(axis, 0);
1235 yyyy = vec_splat(axis, 1);
1236 zzzz = vec_splat(axis, 2);
1237 oneMinusC = vec_sub(_mm_set1_ps(1.0f), c);
1238 axisS = vec_mul(axis, s);
1239 negAxisS = negatef4(axisS);
1240 VM_ATTRIBUTE_ALIGN16 unsigned int select_x[4] = {0xffffffff, 0, 0, 0};
1241 VM_ATTRIBUTE_ALIGN16 unsigned int select_y[4] = {0, 0xffffffff, 0, 0};
1242 VM_ATTRIBUTE_ALIGN16 unsigned int select_z[4] = {0, 0, 0xffffffff, 0};
1243 //tmp0 = vec_perm( axisS, negAxisS, _VECTORMATH_PERM_XZBX );
1244 tmp0 = _mm_shuffle_ps(axisS, axisS, _MM_SHUFFLE(0, 0, 2, 0));
1245 tmp0 = vec_sel(tmp0, vec_splat(negAxisS, 1), select_z);
1246 //tmp1 = vec_perm( axisS, negAxisS, _VECTORMATH_PERM_CXXX );
1247 tmp1 = vec_sel(vec_splat(axisS, 0), vec_splat(negAxisS, 2), select_x);
1248 //tmp2 = vec_perm( axisS, negAxisS, _VECTORMATH_PERM_YAXX );
1249 tmp2 = _mm_shuffle_ps(axisS, axisS, _MM_SHUFFLE(0, 0, 0, 1));
1250 tmp2 = vec_sel(tmp2, vec_splat(negAxisS, 0), select_y);
1251 tmp0 = vec_sel(tmp0, c, select_x);
1252 tmp1 = vec_sel(tmp1, c, select_y);
1253 tmp2 = vec_sel(tmp2, c, select_z);
1254 VM_ATTRIBUTE_ALIGN16 unsigned int select_xyz[4] = {0xffffffff, 0xffffffff, 0xffffffff, 0};
1255 axis = vec_and(axis, _mm_load_ps((float *)select_xyz));
1256 tmp0 = vec_and(tmp0, _mm_load_ps((float *)select_xyz));
1257 tmp1 = vec_and(tmp1, _mm_load_ps((float *)select_xyz));
1258 tmp2 = vec_and(tmp2, _mm_load_ps((float *)select_xyz));
1259 return Matrix4(
1260 Vector4(vec_madd(vec_mul(axis, xxxx), oneMinusC, tmp0)),
1261 Vector4(vec_madd(vec_mul(axis, yyyy), oneMinusC, tmp1)),
1262 Vector4(vec_madd(vec_mul(axis, zzzz), oneMinusC, tmp2)),
1263 Vector4::wAxis());
1264 }
1265
1266 VECTORMATH_FORCE_INLINE const Matrix4 Matrix4::rotation(const Quat &unitQuat)
1267 {
1268 return Matrix4(Transform3::rotation(unitQuat));
1269 }
1270
1271 VECTORMATH_FORCE_INLINE const Matrix4 Matrix4::scale(const Vector3 &scaleVec)
1272 {
1273 __m128 zero = _mm_setzero_ps();
1274 VM_ATTRIBUTE_ALIGN16 unsigned int select_x[4] = {0xffffffff, 0, 0, 0};
1275 VM_ATTRIBUTE_ALIGN16 unsigned int select_y[4] = {0, 0xffffffff, 0, 0};
1276 VM_ATTRIBUTE_ALIGN16 unsigned int select_z[4] = {0, 0, 0xffffffff, 0};
1277 return Matrix4(
1278 Vector4(vec_sel(zero, scaleVec.get128(), select_x)),
1279 Vector4(vec_sel(zero, scaleVec.get128(), select_y)),
1280 Vector4(vec_sel(zero, scaleVec.get128(), select_z)),
1281 Vector4::wAxis());
1282 }
1283
1284 VECTORMATH_FORCE_INLINE const Matrix4 appendScale(const Matrix4 &mat, const Vector3 &scaleVec)
1285 {
1286 return Matrix4(
1287 (mat.getCol0() * scaleVec.getX()),
1288 (mat.getCol1() * scaleVec.getY()),
1289 (mat.getCol2() * scaleVec.getZ()),
1290 mat.getCol3());
1291 }
1292
1293 VECTORMATH_FORCE_INLINE const Matrix4 prependScale(const Vector3 &scaleVec, const Matrix4 &mat)
1294 {
1295 Vector4 scale4;
1296 scale4 = Vector4(scaleVec, 1.0f);
1297 return Matrix4(
1298 mulPerElem(mat.getCol0(), scale4),
1299 mulPerElem(mat.getCol1(), scale4),
1300 mulPerElem(mat.getCol2(), scale4),
1301 mulPerElem(mat.getCol3(), scale4));
1302 }
1303
1304 VECTORMATH_FORCE_INLINE const Matrix4 Matrix4::translation(const Vector3 &translateVec)
1305 {
1306 return Matrix4(
1307 Vector4::xAxis(),
1308 Vector4::yAxis(),
1309 Vector4::zAxis(),
1310 Vector4(translateVec, 1.0f));
1311 }
1312
1313 VECTORMATH_FORCE_INLINE const Matrix4 Matrix4::lookAt(const Point3 &eyePos, const Point3 &lookAtPos, const Vector3 &upVec)
1314 {
1315 Matrix4 m4EyeFrame;
1316 Vector3 v3X, v3Y, v3Z;
1317 v3Y = normalize(upVec);
1318 v3Z = normalize((eyePos - lookAtPos));
1319 v3X = normalize(cross(v3Y, v3Z));
1320 v3Y = cross(v3Z, v3X);
1321 m4EyeFrame = Matrix4(Vector4(v3X), Vector4(v3Y), Vector4(v3Z), Vector4(eyePos));
1322 return orthoInverse(m4EyeFrame);
1323 }
1324
1325 VECTORMATH_FORCE_INLINE const Matrix4 Matrix4::perspective(float fovyRadians, float aspect, float zNear, float zFar)
1326 {
1327 float f, rangeInv;
1328 __m128 zero, col0, col1, col2, col3;
1329 union {
1330 __m128 v;
1331 float s[4];
1332 } tmp;
1333 f = tanf(_VECTORMATH_PI_OVER_2 - fovyRadians * 0.5f);
1334 rangeInv = 1.0f / (zNear - zFar);
1335 zero = _mm_setzero_ps();
1336 tmp.v = zero;
1337 tmp.s[0] = f / aspect;
1338 col0 = tmp.v;
1339 tmp.v = zero;
1340 tmp.s[1] = f;
1341 col1 = tmp.v;
1342 tmp.v = zero;
1343 tmp.s[2] = (zNear + zFar) * rangeInv;
1344 tmp.s[3] = -1.0f;
1345 col2 = tmp.v;
1346 tmp.v = zero;
1347 tmp.s[2] = zNear * zFar * rangeInv * 2.0f;
1348 col3 = tmp.v;
1349 return Matrix4(
1350 Vector4(col0),
1351 Vector4(col1),
1352 Vector4(col2),
1353 Vector4(col3));
1354 }
1355
1356 VECTORMATH_FORCE_INLINE const Matrix4 Matrix4::frustum(float left, float right, float bottom, float top, float zNear, float zFar)
1357 {
1358 /* function implementation based on code from STIDC SDK: */
1359 /* -------------------------------------------------------------- */
1360 /* PLEASE DO NOT MODIFY THIS SECTION */
1361 /* This prolog section is automatically generated. */
1362 /* */
1363 /* (C)Copyright */
1364 /* Sony Computer Entertainment, Inc., */
1365 /* Toshiba Corporation, */
1366 /* International Business Machines Corporation, */
1367 /* 2001,2002. */
1368 /* S/T/I Confidential Information */
1369 /* -------------------------------------------------------------- */
1370 __m128 lbf, rtn;
1371 __m128 diff, sum, inv_diff;
1372 __m128 diagonal, column, near2;
1373 __m128 zero = _mm_setzero_ps();
1374 union {
1375 __m128 v;
1376 float s[4];
1377 } l, f, r, n, b, t; // TODO: Union?
1378 l.s[0] = left;
1379 f.s[0] = zFar;
1380 r.s[0] = right;
1381 n.s[0] = zNear;
1382 b.s[0] = bottom;
1383 t.s[0] = top;
1384 lbf = vec_mergeh(l.v, f.v);
1385 rtn = vec_mergeh(r.v, n.v);
1386 lbf = vec_mergeh(lbf, b.v);
1387 rtn = vec_mergeh(rtn, t.v);
1388 diff = vec_sub(rtn, lbf);
1389 sum = vec_add(rtn, lbf);
1390 inv_diff = recipf4(diff);
1391 near2 = vec_splat(n.v, 0);
1392 near2 = vec_add(near2, near2);
1393 diagonal = vec_mul(near2, inv_diff);
1394 column = vec_mul(sum, inv_diff);
1395 VM_ATTRIBUTE_ALIGN16 unsigned int select_x[4] = {0xffffffff, 0, 0, 0};
1396 VM_ATTRIBUTE_ALIGN16 unsigned int select_y[4] = {0, 0xffffffff, 0, 0};
1397 VM_ATTRIBUTE_ALIGN16 unsigned int select_z[4] = {0, 0, 0xffffffff, 0};
1398 VM_ATTRIBUTE_ALIGN16 unsigned int select_w[4] = {0, 0, 0, 0xffffffff};
1399 return Matrix4(
1400 Vector4(vec_sel(zero, diagonal, select_x)),
1401 Vector4(vec_sel(zero, diagonal, select_y)),
1402 Vector4(vec_sel(column, _mm_set1_ps(-1.0f), select_w)),
1403 Vector4(vec_sel(zero, vec_mul(diagonal, vec_splat(f.v, 0)), select_z)));
1404 }
1405
1406 VECTORMATH_FORCE_INLINE const Matrix4 Matrix4::orthographic(float left, float right, float bottom, float top, float zNear, float zFar)
1407 {
1408 /* function implementation based on code from STIDC SDK: */
1409 /* -------------------------------------------------------------- */
1410 /* PLEASE DO NOT MODIFY THIS SECTION */
1411 /* This prolog section is automatically generated. */
1412 /* */
1413 /* (C)Copyright */
1414 /* Sony Computer Entertainment, Inc., */
1415 /* Toshiba Corporation, */
1416 /* International Business Machines Corporation, */
1417 /* 2001,2002. */
1418 /* S/T/I Confidential Information */
1419 /* -------------------------------------------------------------- */
1420 __m128 lbf, rtn;
1421 __m128 diff, sum, inv_diff, neg_inv_diff;
1422 __m128 diagonal, column;
1423 __m128 zero = _mm_setzero_ps();
1424 union {
1425 __m128 v;
1426 float s[4];
1427 } l, f, r, n, b, t;
1428 l.s[0] = left;
1429 f.s[0] = zFar;
1430 r.s[0] = right;
1431 n.s[0] = zNear;
1432 b.s[0] = bottom;
1433 t.s[0] = top;
1434 lbf = vec_mergeh(l.v, f.v);
1435 rtn = vec_mergeh(r.v, n.v);
1436 lbf = vec_mergeh(lbf, b.v);
1437 rtn = vec_mergeh(rtn, t.v);
1438 diff = vec_sub(rtn, lbf);
1439 sum = vec_add(rtn, lbf);
1440 inv_diff = recipf4(diff);
1441 neg_inv_diff = negatef4(inv_diff);
1442 diagonal = vec_add(inv_diff, inv_diff);
1443 VM_ATTRIBUTE_ALIGN16 unsigned int select_x[4] = {0xffffffff, 0, 0, 0};
1444 VM_ATTRIBUTE_ALIGN16 unsigned int select_y[4] = {0, 0xffffffff, 0, 0};
1445 VM_ATTRIBUTE_ALIGN16 unsigned int select_z[4] = {0, 0, 0xffffffff, 0};
1446 VM_ATTRIBUTE_ALIGN16 unsigned int select_w[4] = {0, 0, 0, 0xffffffff};
1447 column = vec_mul(sum, vec_sel(neg_inv_diff, inv_diff, select_z)); // TODO: no madds with zero
1448 return Matrix4(
1449 Vector4(vec_sel(zero, diagonal, select_x)),
1450 Vector4(vec_sel(zero, diagonal, select_y)),
1451 Vector4(vec_sel(zero, diagonal, select_z)),
1452 Vector4(vec_sel(column, _mm_set1_ps(1.0f), select_w)));
1453 }
1454
1455 VECTORMATH_FORCE_INLINE const Matrix4 select(const Matrix4 &mat0, const Matrix4 &mat1, bool select1)
1456 {
1457 return Matrix4(
1458 select(mat0.getCol0(), mat1.getCol0(), select1),
1459 select(mat0.getCol1(), mat1.getCol1(), select1),
1460 select(mat0.getCol2(), mat1.getCol2(), select1),
1461 select(mat0.getCol3(), mat1.getCol3(), select1));
1462 }
1463
1464 VECTORMATH_FORCE_INLINE const Matrix4 select(const Matrix4 &mat0, const Matrix4 &mat1, const boolInVec &select1)
1465 {
1466 return Matrix4(
1467 select(mat0.getCol0(), mat1.getCol0(), select1),
1468 select(mat0.getCol1(), mat1.getCol1(), select1),
1469 select(mat0.getCol2(), mat1.getCol2(), select1),
1470 select(mat0.getCol3(), mat1.getCol3(), select1));
1471 }
1472
1473 #ifdef _VECTORMATH_DEBUG
1474
1475 VECTORMATH_FORCE_INLINE void print(const Matrix4 &mat)
1476 {
1477 print(mat.getRow(0));
1478 print(mat.getRow(1));
1479 print(mat.getRow(2));
1480 print(mat.getRow(3));
1481 }
1482
1483 VECTORMATH_FORCE_INLINE void print(const Matrix4 &mat, const char *name)
1484 {
1485 printf("%s:\n", name);
1486 print(mat);
1487 }
1488
1489 #endif
1490
1491 VECTORMATH_FORCE_INLINE Transform3::Transform3(const Transform3 &tfrm)
1492 {
1493 mCol0 = tfrm.mCol0;
1494 mCol1 = tfrm.mCol1;
1495 mCol2 = tfrm.mCol2;
1496 mCol3 = tfrm.mCol3;
1497 }
1498
1499 VECTORMATH_FORCE_INLINE Transform3::Transform3(float scalar)
1500 {
1501 mCol0 = Vector3(scalar);
1502 mCol1 = Vector3(scalar);
1503 mCol2 = Vector3(scalar);
1504 mCol3 = Vector3(scalar);
1505 }
1506
1507 VECTORMATH_FORCE_INLINE Transform3::Transform3(const floatInVec &scalar)
1508 {
1509 mCol0 = Vector3(scalar);
1510 mCol1 = Vector3(scalar);
1511 mCol2 = Vector3(scalar);
1512 mCol3 = Vector3(scalar);
1513 }
1514
1515 VECTORMATH_FORCE_INLINE Transform3::Transform3(const Vector3 &_col0, const Vector3 &_col1, const Vector3 &_col2, const Vector3 &_col3)
1516 {
1517 mCol0 = _col0;
1518 mCol1 = _col1;
1519 mCol2 = _col2;
1520 mCol3 = _col3;
1521 }
1522
1523 VECTORMATH_FORCE_INLINE Transform3::Transform3(const Matrix3 &tfrm, const Vector3 &translateVec)
1524 {
1525 this->setUpper3x3(tfrm);
1526 this->setTranslation(translateVec);
1527 }
1528
1529 VECTORMATH_FORCE_INLINE Transform3::Transform3(const Quat &unitQuat, const Vector3 &translateVec)
1530 {
1531 this->setUpper3x3(Matrix3(unitQuat));
1532 this->setTranslation(translateVec);
1533 }
1534
1535 VECTORMATH_FORCE_INLINE Transform3 &Transform3::setCol0(const Vector3 &_col0)
1536 {
1537 mCol0 = _col0;
1538 return *this;
1539 }
1540
1541 VECTORMATH_FORCE_INLINE Transform3 &Transform3::setCol1(const Vector3 &_col1)
1542 {
1543 mCol1 = _col1;
1544 return *this;
1545 }
1546
1547 VECTORMATH_FORCE_INLINE Transform3 &Transform3::setCol2(const Vector3 &_col2)
1548 {
1549 mCol2 = _col2;
1550 return *this;
1551 }
1552
1553 VECTORMATH_FORCE_INLINE Transform3 &Transform3::setCol3(const Vector3 &_col3)
1554 {
1555 mCol3 = _col3;
1556 return *this;
1557 }
1558
1559 VECTORMATH_FORCE_INLINE Transform3 &Transform3::setCol(int col, const Vector3 &vec)
1560 {
1561 *(&mCol0 + col) = vec;
1562 return *this;
1563 }
1564
1565 VECTORMATH_FORCE_INLINE Transform3 &Transform3::setRow(int row, const Vector4 &vec)
1566 {
1567 mCol0.setElem(row, vec.getElem(0));
1568 mCol1.setElem(row, vec.getElem(1));
1569 mCol2.setElem(row, vec.getElem(2));
1570 mCol3.setElem(row, vec.getElem(3));
1571 return *this;
1572 }
1573
1574 VECTORMATH_FORCE_INLINE Transform3 &Transform3::setElem(int col, int row, float val)
1575 {
1576 (*this)[col].setElem(row, val);
1577 return *this;
1578 }
1579
1580 VECTORMATH_FORCE_INLINE Transform3 &Transform3::setElem(int col, int row, const floatInVec &val)
1581 {
1582 Vector3 tmpV3_0;
1583 tmpV3_0 = this->getCol(col);
1584 tmpV3_0.setElem(row, val);
1585 this->setCol(col, tmpV3_0);
1586 return *this;
1587 }
1588
1589 VECTORMATH_FORCE_INLINE const floatInVec Transform3::getElem(int col, int row) const
1590 {
1591 return this->getCol(col).getElem(row);
1592 }
1593
1594 VECTORMATH_FORCE_INLINE const Vector3 Transform3::getCol0() const
1595 {
1596 return mCol0;
1597 }
1598
1599 VECTORMATH_FORCE_INLINE const Vector3 Transform3::getCol1() const
1600 {
1601 return mCol1;
1602 }
1603
1604 VECTORMATH_FORCE_INLINE const Vector3 Transform3::getCol2() const
1605 {
1606 return mCol2;
1607 }
1608
1609 VECTORMATH_FORCE_INLINE const Vector3 Transform3::getCol3() const
1610 {
1611 return mCol3;
1612 }
1613
1614 VECTORMATH_FORCE_INLINE const Vector3 Transform3::getCol(int col) const
1615 {
1616 return *(&mCol0 + col);
1617 }
1618
1619 VECTORMATH_FORCE_INLINE const Vector4 Transform3::getRow(int row) const
1620 {
1621 return Vector4(mCol0.getElem(row), mCol1.getElem(row), mCol2.getElem(row), mCol3.getElem(row));
1622 }
1623
1624 VECTORMATH_FORCE_INLINE Vector3 &Transform3::operator[](int col)
1625 {
1626 return *(&mCol0 + col);
1627 }
1628
1629 VECTORMATH_FORCE_INLINE const Vector3 Transform3::operator[](int col) const
1630 {
1631 return *(&mCol0 + col);
1632 }
1633
1634 VECTORMATH_FORCE_INLINE Transform3 &Transform3::operator=(const Transform3 &tfrm)
1635 {
1636 mCol0 = tfrm.mCol0;
1637 mCol1 = tfrm.mCol1;
1638 mCol2 = tfrm.mCol2;
1639 mCol3 = tfrm.mCol3;
1640 return *this;
1641 }
1642
1643 VECTORMATH_FORCE_INLINE const Transform3 inverse(const Transform3 &tfrm)
1644 {
1645 __m128 inv0, inv1, inv2, inv3;
1646 __m128 tmp0, tmp1, tmp2, tmp3, tmp4, dot, invdet;
1647 __m128 xxxx, yyyy, zzzz;
1648 tmp2 = _vmathVfCross(tfrm.getCol0().get128(), tfrm.getCol1().get128());
1649 tmp0 = _vmathVfCross(tfrm.getCol1().get128(), tfrm.getCol2().get128());
1650 tmp1 = _vmathVfCross(tfrm.getCol2().get128(), tfrm.getCol0().get128());
1651 inv3 = negatef4(tfrm.getCol3().get128());
1652 dot = _vmathVfDot3(tmp2, tfrm.getCol2().get128());
1653 dot = vec_splat(dot, 0);
1654 invdet = recipf4(dot);
1655 tmp3 = vec_mergeh(tmp0, tmp2);
1656 tmp4 = vec_mergel(tmp0, tmp2);
1657 inv0 = vec_mergeh(tmp3, tmp1);
1658 xxxx = vec_splat(inv3, 0);
1659 //inv1 = vec_perm( tmp3, tmp1, _VECTORMATH_PERM_ZBWX );
1660 VM_ATTRIBUTE_ALIGN16 unsigned int select_y[4] = {0, 0xffffffff, 0, 0};
1661 inv1 = _mm_shuffle_ps(tmp3, tmp3, _MM_SHUFFLE(0, 3, 2, 2));
1662 inv1 = vec_sel(inv1, tmp1, select_y);
1663 //inv2 = vec_perm( tmp4, tmp1, _VECTORMATH_PERM_XCYX );
1664 inv2 = _mm_shuffle_ps(tmp4, tmp4, _MM_SHUFFLE(0, 1, 1, 0));
1665 inv2 = vec_sel(inv2, vec_splat(tmp1, 2), select_y);
1666 yyyy = vec_splat(inv3, 1);
1667 zzzz = vec_splat(inv3, 2);
1668 inv3 = vec_mul(inv0, xxxx);
1669 inv3 = vec_madd(inv1, yyyy, inv3);
1670 inv3 = vec_madd(inv2, zzzz, inv3);
1671 inv0 = vec_mul(inv0, invdet);
1672 inv1 = vec_mul(inv1, invdet);
1673 inv2 = vec_mul(inv2, invdet);
1674 inv3 = vec_mul(inv3, invdet);
1675 return Transform3(
1676 Vector3(inv0),
1677 Vector3(inv1),
1678 Vector3(inv2),
1679 Vector3(inv3));
1680 }
1681
1682 VECTORMATH_FORCE_INLINE const Transform3 orthoInverse(const Transform3 &tfrm)
1683 {
1684 __m128 inv0, inv1, inv2, inv3;
1685 __m128 tmp0, tmp1;
1686 __m128 xxxx, yyyy, zzzz;
1687 tmp0 = vec_mergeh(tfrm.getCol0().get128(), tfrm.getCol2().get128());
1688 tmp1 = vec_mergel(tfrm.getCol0().get128(), tfrm.getCol2().get128());
1689 inv3 = negatef4(tfrm.getCol3().get128());
1690 inv0 = vec_mergeh(tmp0, tfrm.getCol1().get128());
1691 xxxx = vec_splat(inv3, 0);
1692 //inv1 = vec_perm( tmp0, tfrm.getCol1().get128(), _VECTORMATH_PERM_ZBWX );
1693 VM_ATTRIBUTE_ALIGN16 unsigned int select_y[4] = {0, 0xffffffff, 0, 0};
1694 inv1 = _mm_shuffle_ps(tmp0, tmp0, _MM_SHUFFLE(0, 3, 2, 2));
1695 inv1 = vec_sel(inv1, tfrm.getCol1().get128(), select_y);
1696 //inv2 = vec_perm( tmp1, tfrm.getCol1().get128(), _VECTORMATH_PERM_XCYX );
1697 inv2 = _mm_shuffle_ps(tmp1, tmp1, _MM_SHUFFLE(0, 1, 1, 0));
1698 inv2 = vec_sel(inv2, vec_splat(tfrm.getCol1().get128(), 2), select_y);
1699 yyyy = vec_splat(inv3, 1);
1700 zzzz = vec_splat(inv3, 2);
1701 inv3 = vec_mul(inv0, xxxx);
1702 inv3 = vec_madd(inv1, yyyy, inv3);
1703 inv3 = vec_madd(inv2, zzzz, inv3);
1704 return Transform3(
1705 Vector3(inv0),
1706 Vector3(inv1),
1707 Vector3(inv2),
1708 Vector3(inv3));
1709 }
1710
1711 VECTORMATH_FORCE_INLINE const Transform3 absPerElem(const Transform3 &tfrm)
1712 {
1713 return Transform3(
1714 absPerElem(tfrm.getCol0()),
1715 absPerElem(tfrm.getCol1()),
1716 absPerElem(tfrm.getCol2()),
1717 absPerElem(tfrm.getCol3()));
1718 }
1719
1720 VECTORMATH_FORCE_INLINE const Vector3 Transform3::operator*(const Vector3 &vec) const
1721 {
1722 __m128 res;
1723 __m128 xxxx, yyyy, zzzz;
1724 xxxx = vec_splat(vec.get128(), 0);
1725 yyyy = vec_splat(vec.get128(), 1);
1726 zzzz = vec_splat(vec.get128(), 2);
1727 res = vec_mul(mCol0.get128(), xxxx);
1728 res = vec_madd(mCol1.get128(), yyyy, res);
1729 res = vec_madd(mCol2.get128(), zzzz, res);
1730 return Vector3(res);
1731 }
1732
1733 VECTORMATH_FORCE_INLINE const Point3 Transform3::operator*(const Point3 &pnt) const
1734 {
1735 __m128 tmp0, tmp1, res;
1736 __m128 xxxx, yyyy, zzzz;
1737 xxxx = vec_splat(pnt.get128(), 0);
1738 yyyy = vec_splat(pnt.get128(), 1);
1739 zzzz = vec_splat(pnt.get128(), 2);
1740 tmp0 = vec_mul(mCol0.get128(), xxxx);
1741 tmp1 = vec_mul(mCol1.get128(), yyyy);
1742 tmp0 = vec_madd(mCol2.get128(), zzzz, tmp0);
1743 tmp1 = vec_add(mCol3.get128(), tmp1);
1744 res = vec_add(tmp0, tmp1);
1745 return Point3(res);
1746 }
1747
1748 VECTORMATH_FORCE_INLINE const Transform3 Transform3::operator*(const Transform3 &tfrm) const
1749 {
1750 return Transform3(
1751 (*this * tfrm.mCol0),
1752 (*this * tfrm.mCol1),
1753 (*this * tfrm.mCol2),
1754 Vector3((*this * Point3(tfrm.mCol3))));
1755 }
1756
1757 VECTORMATH_FORCE_INLINE Transform3 &Transform3::operator*=(const Transform3 &tfrm)
1758 {
1759 *this = *this * tfrm;
1760 return *this;
1761 }
1762
1763 VECTORMATH_FORCE_INLINE const Transform3 mulPerElem(const Transform3 &tfrm0, const Transform3 &tfrm1)
1764 {
1765 return Transform3(
1766 mulPerElem(tfrm0.getCol0(), tfrm1.getCol0()),
1767 mulPerElem(tfrm0.getCol1(), tfrm1.getCol1()),
1768 mulPerElem(tfrm0.getCol2(), tfrm1.getCol2()),
1769 mulPerElem(tfrm0.getCol3(), tfrm1.getCol3()));
1770 }
1771
1772 VECTORMATH_FORCE_INLINE const Transform3 Transform3::identity()
1773 {
1774 return Transform3(
1775 Vector3::xAxis(),
1776 Vector3::yAxis(),
1777 Vector3::zAxis(),
1778 Vector3(0.0f));
1779 }
1780
1781 VECTORMATH_FORCE_INLINE Transform3 &Transform3::setUpper3x3(const Matrix3 &tfrm)
1782 {
1783 mCol0 = tfrm.getCol0();
1784 mCol1 = tfrm.getCol1();
1785 mCol2 = tfrm.getCol2();
1786 return *this;
1787 }
1788
1789 VECTORMATH_FORCE_INLINE const Matrix3 Transform3::getUpper3x3() const
1790 {
1791 return Matrix3(mCol0, mCol1, mCol2);
1792 }
1793
1794 VECTORMATH_FORCE_INLINE Transform3 &Transform3::setTranslation(const Vector3 &translateVec)
1795 {
1796 mCol3 = translateVec;
1797 return *this;
1798 }
1799
1800 VECTORMATH_FORCE_INLINE const Vector3 Transform3::getTranslation() const
1801 {
1802 return mCol3;
1803 }
1804
1805 VECTORMATH_FORCE_INLINE const Transform3 Transform3::rotationX(float radians)
1806 {
1807 return rotationX(floatInVec(radians));
1808 }
1809
1810 VECTORMATH_FORCE_INLINE const Transform3 Transform3::rotationX(const floatInVec &radians)
1811 {
1812 __m128 s, c, res1, res2;
1813 __m128 zero;
1814 VM_ATTRIBUTE_ALIGN16 unsigned int select_y[4] = {0, 0xffffffff, 0, 0};
1815 VM_ATTRIBUTE_ALIGN16 unsigned int select_z[4] = {0, 0, 0xffffffff, 0};
1816 zero = _mm_setzero_ps();
1817 sincosf4(radians.get128(), &s, &c);
1818 res1 = vec_sel(zero, c, select_y);
1819 res1 = vec_sel(res1, s, select_z);
1820 res2 = vec_sel(zero, negatef4(s), select_y);
1821 res2 = vec_sel(res2, c, select_z);
1822 return Transform3(
1823 Vector3::xAxis(),
1824 Vector3(res1),
1825 Vector3(res2),
1826 Vector3(_mm_setzero_ps()));
1827 }
1828
1829 VECTORMATH_FORCE_INLINE const Transform3 Transform3::rotationY(float radians)
1830 {
1831 return rotationY(floatInVec(radians));
1832 }
1833
1834 VECTORMATH_FORCE_INLINE const Transform3 Transform3::rotationY(const floatInVec &radians)
1835 {
1836 __m128 s, c, res0, res2;
1837 __m128 zero;
1838 VM_ATTRIBUTE_ALIGN16 unsigned int select_x[4] = {0xffffffff, 0, 0, 0};
1839 VM_ATTRIBUTE_ALIGN16 unsigned int select_z[4] = {0, 0, 0xffffffff, 0};
1840 zero = _mm_setzero_ps();
1841 sincosf4(radians.get128(), &s, &c);
1842 res0 = vec_sel(zero, c, select_x);
1843 res0 = vec_sel(res0, negatef4(s), select_z);
1844 res2 = vec_sel(zero, s, select_x);
1845 res2 = vec_sel(res2, c, select_z);
1846 return Transform3(
1847 Vector3(res0),
1848 Vector3::yAxis(),
1849 Vector3(res2),
1850 Vector3(0.0f));
1851 }
1852
1853 VECTORMATH_FORCE_INLINE const Transform3 Transform3::rotationZ(float radians)
1854 {
1855 return rotationZ(floatInVec(radians));
1856 }
1857
1858 VECTORMATH_FORCE_INLINE const Transform3 Transform3::rotationZ(const floatInVec &radians)
1859 {
1860 __m128 s, c, res0, res1;
1861 VM_ATTRIBUTE_ALIGN16 unsigned int select_x[4] = {0xffffffff, 0, 0, 0};
1862 VM_ATTRIBUTE_ALIGN16 unsigned int select_y[4] = {0, 0xffffffff, 0, 0};
1863 __m128 zero = _mm_setzero_ps();
1864 sincosf4(radians.get128(), &s, &c);
1865 res0 = vec_sel(zero, c, select_x);
1866 res0 = vec_sel(res0, s, select_y);
1867 res1 = vec_sel(zero, negatef4(s), select_x);
1868 res1 = vec_sel(res1, c, select_y);
1869 return Transform3(
1870 Vector3(res0),
1871 Vector3(res1),
1872 Vector3::zAxis(),
1873 Vector3(0.0f));
1874 }
1875
1876 VECTORMATH_FORCE_INLINE const Transform3 Transform3::rotationZYX(const Vector3 &radiansXYZ)
1877 {
1878 __m128 angles, s, negS, c, X0, X1, Y0, Y1, Z0, Z1, tmp;
1879 angles = Vector4(radiansXYZ, 0.0f).get128();
1880 sincosf4(angles, &s, &c);
1881 negS = negatef4(s);
1882 Z0 = vec_mergel(c, s);
1883 Z1 = vec_mergel(negS, c);
1884 VM_ATTRIBUTE_ALIGN16 unsigned int select_xyz[4] = {0xffffffff, 0xffffffff, 0xffffffff, 0};
1885 Z1 = vec_and(Z1, _mm_load_ps((float *)select_xyz));
1886 Y0 = _mm_shuffle_ps(c, negS, _MM_SHUFFLE(0, 1, 1, 1));
1887 Y1 = _mm_shuffle_ps(s, c, _MM_SHUFFLE(0, 1, 1, 1));
1888 X0 = vec_splat(s, 0);
1889 X1 = vec_splat(c, 0);
1890 tmp = vec_mul(Z0, Y1);
1891 return Transform3(
1892 Vector3(vec_mul(Z0, Y0)),
1893 Vector3(vec_madd(Z1, X1, vec_mul(tmp, X0))),
1894 Vector3(vec_nmsub(Z1, X0, vec_mul(tmp, X1))),
1895 Vector3(0.0f));
1896 }
1897
1898 VECTORMATH_FORCE_INLINE const Transform3 Transform3::rotation(float radians, const Vector3 &unitVec)
1899 {
1900 return rotation(floatInVec(radians), unitVec);
1901 }
1902
1903 VECTORMATH_FORCE_INLINE const Transform3 Transform3::rotation(const floatInVec &radians, const Vector3 &unitVec)
1904 {
1905 return Transform3(Matrix3::rotation(radians, unitVec), Vector3(0.0f));
1906 }
1907
1908 VECTORMATH_FORCE_INLINE const Transform3 Transform3::rotation(const Quat &unitQuat)
1909 {
1910 return Transform3(Matrix3(unitQuat), Vector3(0.0f));
1911 }
1912
1913 VECTORMATH_FORCE_INLINE const Transform3 Transform3::scale(const Vector3 &scaleVec)
1914 {
1915 __m128 zero = _mm_setzero_ps();
1916 VM_ATTRIBUTE_ALIGN16 unsigned int select_x[4] = {0xffffffff, 0, 0, 0};
1917 VM_ATTRIBUTE_ALIGN16 unsigned int select_y[4] = {0, 0xffffffff, 0, 0};
1918 VM_ATTRIBUTE_ALIGN16 unsigned int select_z[4] = {0, 0, 0xffffffff, 0};
1919 return Transform3(
1920 Vector3(vec_sel(zero, scaleVec.get128(), select_x)),
1921 Vector3(vec_sel(zero, scaleVec.get128(), select_y)),
1922 Vector3(vec_sel(zero, scaleVec.get128(), select_z)),
1923 Vector3(0.0f));
1924 }
1925
1926 VECTORMATH_FORCE_INLINE const Transform3 appendScale(const Transform3 &tfrm, const Vector3 &scaleVec)
1927 {
1928 return Transform3(
1929 (tfrm.getCol0() * scaleVec.getX()),
1930 (tfrm.getCol1() * scaleVec.getY()),
1931 (tfrm.getCol2() * scaleVec.getZ()),
1932 tfrm.getCol3());
1933 }
1934
1935 VECTORMATH_FORCE_INLINE const Transform3 prependScale(const Vector3 &scaleVec, const Transform3 &tfrm)
1936 {
1937 return Transform3(
1938 mulPerElem(tfrm.getCol0(), scaleVec),
1939 mulPerElem(tfrm.getCol1(), scaleVec),
1940 mulPerElem(tfrm.getCol2(), scaleVec),
1941 mulPerElem(tfrm.getCol3(), scaleVec));
1942 }
1943
1944 VECTORMATH_FORCE_INLINE const Transform3 Transform3::translation(const Vector3 &translateVec)
1945 {
1946 return Transform3(
1947 Vector3::xAxis(),
1948 Vector3::yAxis(),
1949 Vector3::zAxis(),
1950 translateVec);
1951 }
1952
1953 VECTORMATH_FORCE_INLINE const Transform3 select(const Transform3 &tfrm0, const Transform3 &tfrm1, bool select1)
1954 {
1955 return Transform3(
1956 select(tfrm0.getCol0(), tfrm1.getCol0(), select1),
1957 select(tfrm0.getCol1(), tfrm1.getCol1(), select1),
1958 select(tfrm0.getCol2(), tfrm1.getCol2(), select1),
1959 select(tfrm0.getCol3(), tfrm1.getCol3(), select1));
1960 }
1961
1962 VECTORMATH_FORCE_INLINE const Transform3 select(const Transform3 &tfrm0, const Transform3 &tfrm1, const boolInVec &select1)
1963 {
1964 return Transform3(
1965 select(tfrm0.getCol0(), tfrm1.getCol0(), select1),
1966 select(tfrm0.getCol1(), tfrm1.getCol1(), select1),
1967 select(tfrm0.getCol2(), tfrm1.getCol2(), select1),
1968 select(tfrm0.getCol3(), tfrm1.getCol3(), select1));
1969 }
1970
1971 #ifdef _VECTORMATH_DEBUG
1972
1973 VECTORMATH_FORCE_INLINE void print(const Transform3 &tfrm)
1974 {
1975 print(tfrm.getRow(0));
1976 print(tfrm.getRow(1));
1977 print(tfrm.getRow(2));
1978 }
1979
1980 VECTORMATH_FORCE_INLINE void print(const Transform3 &tfrm, const char *name)
1981 {
1982 printf("%s:\n", name);
1983 print(tfrm);
1984 }
1985
1986 #endif
1987
1988 VECTORMATH_FORCE_INLINE Quat::Quat(const Matrix3 &tfrm)
1989 {
1990 __m128 res;
1991 __m128 col0, col1, col2;
1992 __m128 xx_yy, xx_yy_zz_xx, yy_zz_xx_yy, zz_xx_yy_zz, diagSum, diagDiff;
1993 __m128 zy_xz_yx, yz_zx_xy, sum, diff;
1994 __m128 radicand, invSqrt, scale;
1995 __m128 res0, res1, res2, res3;
1996 __m128 xx, yy, zz;
1997 VM_ATTRIBUTE_ALIGN16 unsigned int select_x[4] = {0xffffffff, 0, 0, 0};
1998 VM_ATTRIBUTE_ALIGN16 unsigned int select_y[4] = {0, 0xffffffff, 0, 0};
1999 VM_ATTRIBUTE_ALIGN16 unsigned int select_z[4] = {0, 0, 0xffffffff, 0};
2000 VM_ATTRIBUTE_ALIGN16 unsigned int select_w[4] = {0, 0, 0, 0xffffffff};
2001
2002 col0 = tfrm.getCol0().get128();
2003 col1 = tfrm.getCol1().get128();
2004 col2 = tfrm.getCol2().get128();
2005
2006 /* four cases: */
2007 /* trace > 0 */
2008 /* else */
2009 /* xx largest diagonal element */
2010 /* yy largest diagonal element */
2011 /* zz largest diagonal element */
2012
2013 /* compute quaternion for each case */
2014
2015 xx_yy = vec_sel(col0, col1, select_y);
2016 //xx_yy_zz_xx = vec_perm( xx_yy, col2, _VECTORMATH_PERM_XYCX );
2017 //yy_zz_xx_yy = vec_perm( xx_yy, col2, _VECTORMATH_PERM_YCXY );
2018 //zz_xx_yy_zz = vec_perm( xx_yy, col2, _VECTORMATH_PERM_CXYC );
2019 xx_yy_zz_xx = _mm_shuffle_ps(xx_yy, xx_yy, _MM_SHUFFLE(0, 0, 1, 0));
2020 xx_yy_zz_xx = vec_sel(xx_yy_zz_xx, col2, select_z); // TODO: Ck
2021 yy_zz_xx_yy = _mm_shuffle_ps(xx_yy_zz_xx, xx_yy_zz_xx, _MM_SHUFFLE(1, 0, 2, 1));
2022 zz_xx_yy_zz = _mm_shuffle_ps(xx_yy_zz_xx, xx_yy_zz_xx, _MM_SHUFFLE(2, 1, 0, 2));
2023
2024 diagSum = vec_add(vec_add(xx_yy_zz_xx, yy_zz_xx_yy), zz_xx_yy_zz);
2025 diagDiff = vec_sub(vec_sub(xx_yy_zz_xx, yy_zz_xx_yy), zz_xx_yy_zz);
2026 radicand = vec_add(vec_sel(diagDiff, diagSum, select_w), _mm_set1_ps(1.0f));
2027 // invSqrt = rsqrtf4( radicand );
2028 invSqrt = newtonrapson_rsqrt4(radicand);
2029
2030 zy_xz_yx = vec_sel(col0, col1, select_z); // zy_xz_yx = 00 01 12 03
2031 //zy_xz_yx = vec_perm( zy_xz_yx, col2, _VECTORMATH_PERM_ZAYX );
2032 zy_xz_yx = _mm_shuffle_ps(zy_xz_yx, zy_xz_yx, _MM_SHUFFLE(0, 1, 2, 2)); // zy_xz_yx = 12 12 01 00
2033 zy_xz_yx = vec_sel(zy_xz_yx, vec_splat(col2, 0), select_y); // zy_xz_yx = 12 20 01 00
2034 yz_zx_xy = vec_sel(col0, col1, select_x); // yz_zx_xy = 10 01 02 03
2035 //yz_zx_xy = vec_perm( yz_zx_xy, col2, _VECTORMATH_PERM_BZXX );
2036 yz_zx_xy = _mm_shuffle_ps(yz_zx_xy, yz_zx_xy, _MM_SHUFFLE(0, 0, 2, 0)); // yz_zx_xy = 10 02 10 10
2037 yz_zx_xy = vec_sel(yz_zx_xy, vec_splat(col2, 1), select_x); // yz_zx_xy = 21 02 10 10
2038
2039 sum = vec_add(zy_xz_yx, yz_zx_xy);
2040 diff = vec_sub(zy_xz_yx, yz_zx_xy);
2041
2042 scale = vec_mul(invSqrt, _mm_set1_ps(0.5f));
2043
2044 //res0 = vec_perm( sum, diff, _VECTORMATH_PERM_XZYA );
2045 res0 = _mm_shuffle_ps(sum, sum, _MM_SHUFFLE(0, 1, 2, 0));
2046 res0 = vec_sel(res0, vec_splat(diff, 0), select_w); // TODO: Ck
2047 //res1 = vec_perm( sum, diff, _VECTORMATH_PERM_ZXXB );
2048 res1 = _mm_shuffle_ps(sum, sum, _MM_SHUFFLE(0, 0, 0, 2));
2049 res1 = vec_sel(res1, vec_splat(diff, 1), select_w); // TODO: Ck
2050 //res2 = vec_perm( sum, diff, _VECTORMATH_PERM_YXXC );
2051 res2 = _mm_shuffle_ps(sum, sum, _MM_SHUFFLE(0, 0, 0, 1));
2052 res2 = vec_sel(res2, vec_splat(diff, 2), select_w); // TODO: Ck
2053 res3 = diff;
2054 res0 = vec_sel(res0, radicand, select_x);
2055 res1 = vec_sel(res1, radicand, select_y);
2056 res2 = vec_sel(res2, radicand, select_z);
2057 res3 = vec_sel(res3, radicand, select_w);
2058 res0 = vec_mul(res0, vec_splat(scale, 0));
2059 res1 = vec_mul(res1, vec_splat(scale, 1));
2060 res2 = vec_mul(res2, vec_splat(scale, 2));
2061 res3 = vec_mul(res3, vec_splat(scale, 3));
2062
2063 /* determine case and select answer */
2064
2065 xx = vec_splat(col0, 0);
2066 yy = vec_splat(col1, 1);
2067 zz = vec_splat(col2, 2);
2068 res = vec_sel(res0, res1, vec_cmpgt(yy, xx));
2069 res = vec_sel(res, res2, vec_and(vec_cmpgt(zz, xx), vec_cmpgt(zz, yy)));
2070 res = vec_sel(res, res3, vec_cmpgt(vec_splat(diagSum, 0), _mm_setzero_ps()));
2071 mVec128 = res;
2072 }
2073
2074 VECTORMATH_FORCE_INLINE const Matrix3 outer(const Vector3 &tfrm0, const Vector3 &tfrm1)
2075 {
2076 return Matrix3(
2077 (tfrm0 * tfrm1.getX()),
2078 (tfrm0 * tfrm1.getY()),
2079 (tfrm0 * tfrm1.getZ()));
2080 }
2081
2082 VECTORMATH_FORCE_INLINE const Matrix4 outer(const Vector4 &tfrm0, const Vector4 &tfrm1)
2083 {
2084 return Matrix4(
2085 (tfrm0 * tfrm1.getX()),
2086 (tfrm0 * tfrm1.getY()),
2087 (tfrm0 * tfrm1.getZ()),
2088 (tfrm0 * tfrm1.getW()));
2089 }
2090
2091 VECTORMATH_FORCE_INLINE const Vector3 rowMul(const Vector3 &vec, const Matrix3 &mat)
2092 {
2093 __m128 tmp0, tmp1, mcol0, mcol1, mcol2, res;
2094 __m128 xxxx, yyyy, zzzz;
2095 tmp0 = vec_mergeh(mat.getCol0().get128(), mat.getCol2().get128());
2096 tmp1 = vec_mergel(mat.getCol0().get128(), mat.getCol2().get128());
2097 xxxx = vec_splat(vec.get128(), 0);
2098 mcol0 = vec_mergeh(tmp0, mat.getCol1().get128());
2099 //mcol1 = vec_perm( tmp0, mat.getCol1().get128(), _VECTORMATH_PERM_ZBWX );
2100 VM_ATTRIBUTE_ALIGN16 unsigned int select_y[4] = {0, 0xffffffff, 0, 0};
2101 mcol1 = _mm_shuffle_ps(tmp0, tmp0, _MM_SHUFFLE(0, 3, 2, 2));
2102 mcol1 = vec_sel(mcol1, mat.getCol1().get128(), select_y);
2103 //mcol2 = vec_perm( tmp1, mat.getCol1().get128(), _VECTORMATH_PERM_XCYX );
2104 mcol2 = _mm_shuffle_ps(tmp1, tmp1, _MM_SHUFFLE(0, 1, 1, 0));
2105 mcol2 = vec_sel(mcol2, vec_splat(mat.getCol1().get128(), 2), select_y);
2106 yyyy = vec_splat(vec.get128(), 1);
2107 res = vec_mul(mcol0, xxxx);
2108 zzzz = vec_splat(vec.get128(), 2);
2109 res = vec_madd(mcol1, yyyy, res);
2110 res = vec_madd(mcol2, zzzz, res);
2111 return Vector3(res);
2112 }
2113
2114 VECTORMATH_FORCE_INLINE const Matrix3 crossMatrix(const Vector3 &vec)
2115 {
2116 __m128 neg, res0, res1, res2;
2117 neg = negatef4(vec.get128());
2118 VM_ATTRIBUTE_ALIGN16 unsigned int select_x[4] = {0xffffffff, 0, 0, 0};
2119 VM_ATTRIBUTE_ALIGN16 unsigned int select_y[4] = {0, 0xffffffff, 0, 0};
2120 VM_ATTRIBUTE_ALIGN16 unsigned int select_z[4] = {0, 0, 0xffffffff, 0};
2121 //res0 = vec_perm( vec.get128(), neg, _VECTORMATH_PERM_XZBX );
2122 res0 = _mm_shuffle_ps(vec.get128(), vec.get128(), _MM_SHUFFLE(0, 2, 2, 0));
2123 res0 = vec_sel(res0, vec_splat(neg, 1), select_z);
2124 //res1 = vec_perm( vec.get128(), neg, _VECTORMATH_PERM_CXXX );
2125 res1 = vec_sel(vec_splat(vec.get128(), 0), vec_splat(neg, 2), select_x);
2126 //res2 = vec_perm( vec.get128(), neg, _VECTORMATH_PERM_YAXX );
2127 res2 = _mm_shuffle_ps(vec.get128(), vec.get128(), _MM_SHUFFLE(0, 0, 1, 1));
2128 res2 = vec_sel(res2, vec_splat(neg, 0), select_y);
2129 VM_ATTRIBUTE_ALIGN16 unsigned int filter_x[4] = {0, 0xffffffff, 0xffffffff, 0xffffffff};
2130 VM_ATTRIBUTE_ALIGN16 unsigned int filter_y[4] = {0xffffffff, 0, 0xffffffff, 0xffffffff};
2131 VM_ATTRIBUTE_ALIGN16 unsigned int filter_z[4] = {0xffffffff, 0xffffffff, 0, 0xffffffff};
2132 res0 = vec_and(res0, _mm_load_ps((float *)filter_x));
2133 res1 = vec_and(res1, _mm_load_ps((float *)filter_y));
2134 res2 = vec_and(res2, _mm_load_ps((float *)filter_z)); // TODO: Use selects?
2135 return Matrix3(
2136 Vector3(res0),
2137 Vector3(res1),
2138 Vector3(res2));
2139 }
2140
2141 VECTORMATH_FORCE_INLINE const Matrix3 crossMatrixMul(const Vector3 &vec, const Matrix3 &mat)
2142 {
2143 return Matrix3(cross(vec, mat.getCol0()), cross(vec, mat.getCol1()), cross(vec, mat.getCol2()));
2144 }
2145
2146 } // namespace Aos
2147 } // namespace Vectormath
2148
2149 #endif