30 #include "dgSimd_Instrutions.h" 37 const dgMatrix &dgGetIdentityMatrix();
40 DG_MSC_VECTOR_ALIGMENT
43 DG_CLASS_ALLOCATOR(allocator)
56 const dgVector &operator[](dgInt32 i)
const;
58 dgMatrix Inverse()
const;
59 dgMatrix Inverse4x4()
const;
60 dgMatrix Transpose()
const;
61 dgMatrix Transpose4X4()
const;
62 dgMatrix Symetric3by3Inverse()
const;
74 void TransformTriplex(dgFloat32 *
const dst, dgInt32 dstStrideInBytes,
75 const dgFloat32 *
const src, dgInt32 srcStrideInBytes, dgInt32 count)
const;
77 #ifndef __USE_DOUBLE_PRECISION__ 78 void TransformTriplex(dgFloat64 *
const dst, dgInt32 dstStrideInBytes,
79 const dgFloat64 *
const src, dgInt32 srcStrideInBytes, dgInt32 count)
const;
80 void TransformTriplex(dgFloat64 *
const dst, dgInt32 dstStrideInBytes,
81 const dgFloat32 *
const src, dgInt32 srcStrideInBytes, dgInt32 count)
const;
84 dgMatrix operator* (
const dgMatrix &B)
const;
90 void EigenVectors(
dgVector &eigenValues,
const dgMatrix &initialGuess = dgGetIdentityMatrix());
91 void EigenVectors(
const dgMatrix &initialGuess = dgGetIdentityMatrix());
95 dgMatrix InverseSimd()
const;
96 dgMatrix MultiplySimd(
const dgMatrix &B)
const;
100 void TransformVectorsSimd(
dgVector *
const dst,
const dgVector *
const src, dgInt32 count)
const;
106 } DG_GCC_VECTOR_ALIGMENT;
112 DG_INLINE dgMatrix::dgMatrix() {
116 : m_front(front), m_up(up), m_right(right), m_posit(posit) {
119 DG_INLINE dgMatrix::dgMatrix(
const dgVector &front) {
121 if (dgAbsf(front.m_z) > dgFloat32(0.577f)) {
122 m_right = front *
dgVector(-front.m_y, front.m_z, dgFloat32(0.0f), dgFloat32(0.0f));
124 m_right = front *
dgVector(-front.m_y, front.m_x, dgFloat32(0.0f), dgFloat32(0.0f));
126 m_right = m_right.Scale(dgRsqrt(m_right % m_right));
127 m_up = m_right * m_front;
129 m_front.m_w = dgFloat32(0.0f);
130 m_up.m_w = dgFloat32(0.0f);
131 m_right.m_w = dgFloat32(0.0f);
132 m_posit =
dgVector(dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(1.0f));
134 NEWTON_ASSERT((dgAbsf(m_front % m_front) - dgFloat32(1.0f)) < dgFloat32(1.0e-5f));
135 NEWTON_ASSERT((dgAbsf(m_up % m_up) - dgFloat32(1.0f)) < dgFloat32(1.0e-5f));
136 NEWTON_ASSERT((dgAbsf(m_right % m_right) - dgFloat32(1.0f)) < dgFloat32(1.0e-5f));
137 NEWTON_ASSERT((dgAbsf(m_right % (m_front * m_up)) - dgFloat32(1.0f)) < dgFloat32(1.0e-5f));
141 DG_INLINE
dgVector &dgMatrix::operator[](dgInt32 i) {
142 NEWTON_ASSERT(i < 4);
143 NEWTON_ASSERT(i >= 0);
144 return (&m_front)[i];
147 DG_INLINE
const dgVector &dgMatrix::operator[](dgInt32 i)
const {
148 NEWTON_ASSERT(i < 4);
149 NEWTON_ASSERT(i >= 0);
150 return (&m_front)[i];
154 DG_INLINE
dgMatrix dgMatrix::Transpose()
const {
155 return dgMatrix(
dgVector(m_front.m_x, m_up.m_x, m_right.m_x, dgFloat32(0.0f)),
156 dgVector(m_front.m_y, m_up.m_y, m_right.m_y, dgFloat32(0.0f)),
157 dgVector(m_front.m_z, m_up.m_z, m_right.m_z, dgFloat32(0.0f)),
158 dgVector(dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(1.0f)));
161 DG_INLINE
dgMatrix dgMatrix::Transpose4X4()
const {
163 dgVector(m_front.m_y, m_up.m_y, m_right.m_y, m_posit.m_y),
164 dgVector(m_front.m_z, m_up.m_z, m_right.m_z, m_posit.m_z),
165 dgVector(m_front.m_w, m_up.m_w, m_right.m_w, m_posit.m_w));
170 return dgVector(v.m_x * m_front.m_x + v.m_y * m_up.m_x + v.m_z * m_right.m_x,
171 v.m_x * m_front.m_y + v.m_y * m_up.m_y + v.m_z * m_right.m_y,
172 v.m_x * m_front.m_z + v.m_y * m_up.m_z + v.m_z * m_right.m_z, v.m_w);
177 return dgVector(v % m_front, v % m_up, v % m_right, v.m_w);
183 return dgVector(v.m_x * m_front.m_x + v.m_y * m_up.m_x + v.m_z * m_right.m_x + m_posit.m_x,
184 v.m_x * m_front.m_y + v.m_y * m_up.m_y + v.m_z * m_right.m_y + m_posit.m_y,
185 v.m_x * m_front.m_z + v.m_y * m_up.m_z + v.m_z * m_right.m_z + m_posit.m_z, v.m_w);
190 return UnrotateVector(v - m_posit);
193 DG_INLINE
dgPlane dgMatrix::TransformPlane(
const dgPlane &localPlane)
const {
194 return dgPlane(RotateVector(localPlane), localPlane.m_w - (localPlane % UnrotateVector(m_posit)));
197 DG_INLINE
dgPlane dgMatrix::UntransformPlane(
const dgPlane &globalPlane)
const {
198 return dgPlane(UnrotateVector(globalPlane), globalPlane.Evalue(m_posit));
201 DG_INLINE
void dgMatrix::EigenVectors(
const dgMatrix &initialGuess) {
203 EigenVectors(eigenValues, initialGuess);
207 DG_INLINE
dgMatrix dgPitchMatrix(dgFloat32 ang) {
212 return dgMatrix(
dgVector(dgFloat32(1.0f), dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f)),
213 dgVector(dgFloat32(0.0f), cosAng, sinAng, dgFloat32(0.0f)),
214 dgVector(dgFloat32(0.0f), -sinAng, cosAng, dgFloat32(0.0f)),
215 dgVector(dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(1.0f)));
219 DG_INLINE
dgMatrix dgYawMatrix(dgFloat32 ang) {
224 return dgMatrix(
dgVector(cosAng, dgFloat32(0.0f), -sinAng, dgFloat32(0.0f)),
225 dgVector(dgFloat32(0.0f), dgFloat32(1.0f), dgFloat32(0.0f), dgFloat32(0.0f)),
226 dgVector(sinAng, dgFloat32(0.0f), cosAng, dgFloat32(0.0f)),
227 dgVector(dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(1.0f)));
230 DG_INLINE
dgMatrix dgRollMatrix(dgFloat32 ang) {
236 dgVector(-sinAng, cosAng, dgFloat32(0.0f), dgFloat32(0.0f)),
237 dgVector(dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(1.0f), dgFloat32(0.0f)),
238 dgVector(dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(1.0f)));
242 DG_INLINE
dgMatrix dgMatrix::Inverse()
const {
243 return dgMatrix(
dgVector(m_front.m_x, m_up.m_x, m_right.m_x, dgFloat32(0.0f)),
244 dgVector(m_front.m_y, m_up.m_y, m_right.m_y, dgFloat32(0.0f)),
245 dgVector(m_front.m_z, m_up.m_z, m_right.m_z, dgFloat32(0.0f)),
246 dgVector(- (m_posit % m_front), - (m_posit % m_up), - (m_posit % m_right), dgFloat32(1.0f)));
250 #ifdef DG_BUILD_SIMD_CODE 254 simd_mul_add_v((simd_type &) source[3], (simd_type &) source[0], simd_permut_v((simd_type &) v, (simd_type &) v, PURMUT_MASK(0, 0, 0, 0))),
255 (simd_type &) source[1], simd_permut_v((simd_type &) v, (simd_type &) v, PURMUT_MASK(1, 1, 1, 1))),
256 (simd_type &) source[2], simd_permut_v((simd_type &) v, (simd_type &) v, PURMUT_MASK(2, 2, 2, 2))));
258 return dgVector(dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f));
262 DG_INLINE
void dgMatrix::TransformVectorsSimd(
dgVector *
const dst,
const dgVector *
const src, dgInt32 count)
const {
263 #ifdef DG_BUILD_SIMD_CODE 265 for (dgInt32 i = 0; i < count; i ++) {
266 (simd_type &)dst[i] = simd_mul_add_v(
268 simd_mul_add_v((simd_type &) source[3],
269 (simd_type &) source[0], simd_permut_v((simd_type &) src[i], (simd_type &) src[i], PURMUT_MASK(0, 0, 0, 0))),
270 (simd_type &) source[1], simd_permut_v((simd_type &) src[i], (simd_type &) src[i], PURMUT_MASK(1, 1, 1, 1))),
271 (simd_type &) source[2], simd_permut_v((simd_type &) src[i], (simd_type &) src[i], PURMUT_MASK(2, 2, 2, 2)));
278 #ifdef DG_BUILD_SIMD_CODE 282 simd_mul_v((simd_type &) source[0], simd_permut_v((simd_type &) v, (simd_type &) v, PURMUT_MASK(0, 0, 0, 0))),
283 (simd_type &) source[1], simd_permut_v((simd_type &) v, (simd_type &) v, PURMUT_MASK(1, 1, 1, 1))),
284 (simd_type &) source[2], simd_permut_v((simd_type &) v, (simd_type &) v, PURMUT_MASK(2, 2, 2, 2))));
287 return dgVector(dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f));
292 #ifdef DG_BUILD_SIMD_CODE 293 return dgVector(v.DotProductSimd(m_front), v.DotProductSimd(m_up), v.DotProductSimd(m_right), v.m_w);
295 return dgVector(dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f));
299 DG_INLINE
dgMatrix dgMatrix::InverseSimd()
const {
300 #ifdef DG_BUILD_SIMD_CODE 307 NEWTON_ASSERT((dgUnsigned64(
this) & 0x0f) == 0);
309 r2 = simd_set1(dgFloat32(0.0f));
310 r0 = simd_pack_lo_v((simd_type &) source[0], (simd_type &) source[1]);
311 r1 = simd_pack_lo_v((simd_type &) source[2], r2);
312 (simd_type &) matrix[0] = simd_move_lh_v(r0, r1);
313 (simd_type &) matrix[1] = simd_move_hl_v(r1, r0);
314 r0 = simd_pack_hi_v((simd_type &) source[0], (simd_type &) source[1]);
315 r1 = simd_pack_hi_v((simd_type &) source[2], r2);
316 (simd_type &) matrix[2] = simd_move_lh_v(r0, r1);
318 (simd_type &) matrix[3] = simd_sub_v(r2,
320 simd_mul_add_v(simd_mul_v((simd_type &) matrix[0], simd_permut_v((simd_type &) source[3], (simd_type &) source[3], PURMUT_MASK(3, 0, 0, 0))),
321 (simd_type &) matrix[1], simd_permut_v((simd_type &) source[3], (simd_type &) source[3], PURMUT_MASK(3, 1, 1, 1))),
322 (simd_type &) matrix[2], simd_permut_v((simd_type &) source[3], (simd_type &) source[3], PURMUT_MASK(3, 2, 2, 2))));
323 matrix[3][3] = dgFloat32(1.0f);
328 return dgGetIdentityMatrix();
333 #ifdef DG_BUILD_SIMD_CODE 337 simd_mul_add_v(simd_mul_v((simd_type &) B[0], simd_permut_v((simd_type &) A[0], (simd_type &) A[0], PURMUT_MASK(0, 0, 0, 0))),
338 (simd_type &) B[1], simd_permut_v((simd_type &) A[0], (simd_type &) A[0], PURMUT_MASK(1, 1, 1, 1))),
339 (simd_type &) B[2], simd_permut_v((simd_type &) A[0], (simd_type &) A[0], PURMUT_MASK(2, 2, 2, 2))),
340 (simd_type &) B[3], simd_permut_v((simd_type &) A[0], (simd_type &) A[0], PURMUT_MASK(3, 3, 3, 3)))),
344 simd_mul_add_v(simd_mul_v((simd_type &) B[0], simd_permut_v((simd_type &) A[1], (simd_type &) A[1], PURMUT_MASK(0, 0, 0, 0))),
345 (simd_type &) B[1], simd_permut_v((simd_type &) A[1], (simd_type &) A[1], PURMUT_MASK(1, 1, 1, 1))),
346 (simd_type &) B[2], simd_permut_v((simd_type &) A[1], (simd_type &) A[1], PURMUT_MASK(2, 2, 2, 2))),
347 (simd_type &) B[3], simd_permut_v((simd_type &) A[1], (simd_type &) A[1], PURMUT_MASK(3, 3, 3, 3)))),
351 simd_mul_add_v(simd_mul_v((simd_type &) B[0], simd_permut_v((simd_type &) A[2], (simd_type &) A[2], PURMUT_MASK(0, 0, 0, 0))),
352 (simd_type &) B[1], simd_permut_v((simd_type &) A[2], (simd_type &) A[2], PURMUT_MASK(1, 1, 1, 1))),
353 (simd_type &) B[2], simd_permut_v((simd_type &) A[2], (simd_type &) A[2], PURMUT_MASK(2, 2, 2, 2))),
354 (simd_type &) B[3], simd_permut_v((simd_type &) A[2], (simd_type &) A[2], PURMUT_MASK(3, 3, 3, 3)))),
359 simd_mul_add_v(simd_mul_v((simd_type &) B[0], simd_permut_v((simd_type &) A[3], (simd_type &) A[3], PURMUT_MASK(0, 0, 0, 0))),
360 (simd_type &) B[1], simd_permut_v((simd_type &) A[3], (simd_type &) A[3], PURMUT_MASK(1, 1, 1, 1))),
361 (simd_type &) B[2], simd_permut_v((simd_type &) A[3], (simd_type &) A[3], PURMUT_MASK(2, 2, 2, 2))),
362 (simd_type &) B[3], simd_permut_v((simd_type &) A[3], (simd_type &) A[3], PURMUT_MASK(3, 3, 3, 3)))));
364 return dgGetIdentityMatrix();
Definition: dgVector.h:86
Definition: dgQuaternion.h:31
Definition: dgMatrix.h:41