-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSIMDMatrix.cpp
More file actions
226 lines (188 loc) · 7.55 KB
/
SIMDMatrix.cpp
File metadata and controls
226 lines (188 loc) · 7.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include "SIMDMatrix.hpp"
#include "Matrix.hpp"
using namespace cliqCity::graphicsMath;
inline SIMDMatrix::SIMDMatrix(const SIMDMatrix& other)
{
this->m[0] = other.m[0];
this->m[1] = other.m[1];
this->m[2] = other.m[2];
this->m[3] = other.m[3];
}
inline SIMDMatrix::SIMDMatrix(const float128_t& m0, const float128_t& m1, const float128_t& m2, const float128_t& m3)
{
this->m[0] = m0;
this->m[1] = m1;
this->m[2] = m2;
this->m[3] = m3;
}
// SIMDMatrix operations
inline SIMDMatrix SIMDMatrix::transpose() const
{
return
{
simd::Set(u.x, v.x, w.x, t.x),
simd::Set(u.y, v.y, w.y, t.y),
simd::Set(u.z, v.z, w.z, t.z),
simd::Set(u.w, v.w, w.w, t.w)
};
}
SIMDMatrix SIMDMatrix::inverse() const
{
/*(((u.y * v.z) - (u.z * v.y)) * w.x) +
(((u.z * v.x) - (u.x * v.z)) * w.y) +
(((u.x * v.y) - (u.y * v.x)) * w.z);*/
float c11 = +Matrix3(v.y, v.z, v.w, w.y, w.z, w.w, t.y, t.z, t.w).determinant();
float c12 = -Matrix3(v.x, v.z, v.w, w.x, w.z, w.w, t.x, t.z, t.w).determinant();
float c13 = +Matrix3(v.x, v.y, v.w, w.x, w.y, w.w, t.x, t.y, t.w).determinant();
float c14 = -Matrix3(v.x, v.y, v.z, w.x, w.y, w.z, t.x, t.y, t.z).determinant();
float c21 = -Matrix3(u.y, u.z, u.w, w.y, w.z, w.w, t.y, t.z, t.w).determinant();
float c22 = +Matrix3(u.x, u.z, u.w, w.x, w.z, w.w, t.x, t.z, t.w).determinant();
float c23 = -Matrix3(u.x, u.y, u.w, w.x, w.y, w.w, t.x, t.y, t.w).determinant();
float c24 = +Matrix3(u.x, u.y, u.z, w.x, w.y, w.z, t.x, t.y, t.z).determinant();
float c31 = +Matrix3(u.y, u.z, u.w, v.y, v.z, v.w, t.y, t.z, t.w).determinant();
float c32 = -Matrix3(u.x, u.z, u.w, v.x, v.z, v.w, t.x, t.z, t.w).determinant();
float c33 = +Matrix3(u.x, u.y, u.w, v.x, v.y, v.w, t.x, t.y, t.w).determinant();
float c34 = -Matrix3(u.x, u.y, u.z, v.x, v.y, v.z, t.x, t.y, t.z).determinant();
float c41 = -Matrix3(u.y, u.z, u.w, v.y, v.z, v.w, w.y, w.z, w.w).determinant();
float c42 = +Matrix3(u.x, u.z, u.w, v.x, v.z, v.w, w.x, w.z, w.w).determinant();
float c43 = -Matrix3(u.x, u.y, u.w, v.x, v.y, v.w, w.x, w.y, w.w).determinant();
float c44 = +Matrix3(u.x, u.y, u.z, v.x, v.y, v.z, w.x, w.y, w.z).determinant();
float128_t invDeterminant = simd::Set(1.0f / ((u.x * c11) + (u.y * c12) + (u.z * c13) + (u.w * c14)));
return
{
simd::Multiply(simd::Set(c11, c21, c31, c41), invDeterminant),
simd::Multiply(simd::Set(c12, c22, c32, c42), invDeterminant),
simd::Multiply(simd::Set(c13, c23, c33, c43), invDeterminant),
simd::Multiply(simd::Set(c14, c24, c34, c44), invDeterminant),
};
}
float SIMDMatrix::determinant() const
{
float c11 = +Matrix3(v.y, v.z, v.w, w.y, w.z, w.w, t.y, t.z, t.w).determinant();
float c12 = -Matrix3(v.x, v.z, v.w, w.x, w.z, w.w, t.x, t.z, t.w).determinant();
float c13 = +Matrix3(v.x, v.y, v.w, w.x, w.y, w.w, t.x, t.y, t.w).determinant();
float c14 = -Matrix3(v.x, v.y, v.z, w.x, w.y, w.z, t.x, t.y, t.z).determinant();
return (u.x * c11) + (u.y * c12) + (u.z * c13) + (u.w * c14);
}
// Compound Assignment
inline SIMDMatrix SIMDMatrix::operator+=(const SIMDMatrix& rhs)
{
this->m[0] = simd::Add(this->m[0], rhs.m[0]);
this->m[0] = simd::Add(this->m[1], rhs.m[1]);
this->m[0] = simd::Add(this->m[2], rhs.m[2]);
this->m[0] = simd::Add(this->m[3], rhs.m[3]);
return *this;
}
inline SIMDMatrix SIMDMatrix::operator-=(const SIMDMatrix& rhs)
{
this->m[0] = simd::Subtract(this->m[0], rhs.m[0]);
this->m[0] = simd::Subtract(this->m[1], rhs.m[1]);
this->m[0] = simd::Subtract(this->m[2], rhs.m[2]);
this->m[0] = simd::Subtract(this->m[3], rhs.m[3]);
return *this;
}
inline SIMDMatrix SIMDMatrix::operator*=(const float& rhs)
{
float128_t rhs_ = simd::Set(rhs);
this->m[0] = simd::Multiply(this->m[0], rhs_);
this->m[1] = simd::Multiply(this->m[1], rhs_);
this->m[2] = simd::Multiply(this->m[2], rhs_);
this->m[3] = simd::Multiply(this->m[3], rhs_);
return *this;
}
// Unary
inline SIMDMatrix SIMDMatrix::operator=(const SIMDMatrix& rhs)
{
this->m[0] = rhs.m[0];
this->m[1] = rhs.m[1];
this->m[2] = rhs.m[2];
this->m[3] = rhs.m[3];
return *this;
}
inline SIMDMatrix SIMDMatrix::operator-()
{
this->m[0] = simd::Negate(this->m[0]);
this->m[1] = simd::Negate(this->m[1]);
this->m[2] = simd::Negate(this->m[2]);
this->m[3] = simd::Negate(this->m[3]);
return *this;
}
inline SIMDVector& SIMDMatrix::operator[](const unsigned int& index)
{
return pRows[index];
}
inline float SIMDMatrix::operator()(const unsigned int& row, const unsigned int& column)
{
return simd::ExtractFloat(m[row], column);
}
#pragma region Operators
inline SIMDMatrix cliqCity::graphicsMath::operator+(const SIMDMatrix& lhs, const SIMDMatrix& rhs)
{
return
{
simd::Add(lhs.m[0], rhs.m[0]),
simd::Add(lhs.m[1], rhs.m[1]),
simd::Add(lhs.m[2], rhs.m[2]),
simd::Add(lhs.m[3], rhs.m[3]),
};
}
inline SIMDMatrix cliqCity::graphicsMath::operator-(const SIMDMatrix& lhs, const SIMDMatrix& rhs)
{
return
{
simd::Subtract(lhs.m[0], rhs.m[0]),
simd::Subtract(lhs.m[1], rhs.m[1]),
simd::Subtract(lhs.m[2], rhs.m[2]),
simd::Subtract(lhs.m[3], rhs.m[3]),
};
}
inline SIMDMatrix cliqCity::graphicsMath::operator*(const SIMDMatrix& lhs, const SIMDMatrix& rhs)
{
//Matrix4 m = rhs.transpose();
//return
//{
// dot(lhs.pRows[0], m.pRows[0]), dot(lhs.pRows[0], m.pRows[1]), dot(lhs.pRows[0], m.pRows[2]), dot(lhs.pRows[0], m.pRows[3]),
// dot(lhs.pRows[1], m.pRows[0]), dot(lhs.pRows[1], m.pRows[1]), dot(lhs.pRows[1], m.pRows[2]), dot(lhs.pRows[1], m.pRows[3]),
// dot(lhs.pRows[2], m.pRows[0]), dot(lhs.pRows[2], m.pRows[1]), dot(lhs.pRows[2], m.pRows[2]), dot(lhs.pRows[2], m.pRows[3]),
// dot(lhs.pRows[3], m.pRows[0]), dot(lhs.pRows[3], m.pRows[1]), dot(lhs.pRows[3], m.pRows[2]), dot(lhs.pRows[3], m.pRows[3])
//};
SIMDMatrix rhs_ = rhs.transpose();
return
{
simd::Set(simd::Dot(lhs.m[0], rhs_.m[0]), simd::Dot(lhs.m[0], rhs_.m[1]), simd::Dot(lhs.m[0], rhs_.m[2]), simd::Dot(lhs.m[0], rhs_.m[3])),
simd::Set(simd::Dot(lhs.m[1], rhs_.m[0]), simd::Dot(lhs.m[1], rhs_.m[1]), simd::Dot(lhs.m[1], rhs_.m[2]), simd::Dot(lhs.m[1], rhs_.m[3])),
simd::Set(simd::Dot(lhs.m[2], rhs_.m[0]), simd::Dot(lhs.m[2], rhs_.m[1]), simd::Dot(lhs.m[2], rhs_.m[2]), simd::Dot(lhs.m[2], rhs_.m[3])),
simd::Set(simd::Dot(lhs.m[3], rhs_.m[0]), simd::Dot(lhs.m[3], rhs_.m[1]), simd::Dot(lhs.m[3], rhs_.m[2]), simd::Dot(lhs.m[3], rhs_.m[3]))
};
}
inline SIMDVector cliqCity::graphicsMath::operator*(const SIMDVector& lhs, const SIMDMatrix& rhs)
{
//return
//{
// (lhs.pCols[0] * rhs.pRows[0].pCols[0]) + (lhs.pCols[1] * rhs.pRows[1].pCols[0]) + (lhs.pCols[2] * rhs.pRows[2].pCols[0]) + (lhs.pCols[3] * rhs.pRows[3].pCols[0]),
// (lhs.pCols[0] * rhs.pRows[0].pCols[1]) + (lhs.pCols[1] * rhs.pRows[1].pCols[1]) + (lhs.pCols[2] * rhs.pRows[2].pCols[1]) + (lhs.pCols[3] * rhs.pRows[3].pCols[1]),
// (lhs.pCols[0] * rhs.pRows[0].pCols[2]) + (lhs.pCols[1] * rhs.pRows[1].pCols[2]) + (lhs.pCols[2] * rhs.pRows[2].pCols[2]) + (lhs.pCols[3] * rhs.pRows[3].pCols[2]),
// (lhs.pCols[0] * rhs.pRows[0].pCols[3]) + (lhs.pCols[1] * rhs.pRows[1].pCols[3]) + (lhs.pCols[2] * rhs.pRows[2].pCols[3]) + (lhs.pCols[3] * rhs.pRows[3].pCols[3])
//};
SIMDMatrix rhs_ = rhs.transpose();
return
{
simd::Set(simd::Dot(lhs.m, rhs_.m[0]), simd::Dot(lhs.m, rhs_.m[1]), simd::Dot(lhs.m, rhs_.m[2]), simd::Dot(lhs.m, rhs_.m[3]))
};
}
inline SIMDMatrix cliqCity::graphicsMath::operator*(const SIMDMatrix& lhs, const float& rhs)
{
float128_t rhs_ = simd::Set(rhs);
return
{
simd::Multiply(lhs.m[0], rhs_),
simd::Multiply(lhs.m[1], rhs_),
simd::Multiply(lhs.m[2], rhs_),
simd::Multiply(lhs.m[3], rhs_),
};
}
inline SIMDMatrix cliqCity::graphicsMath::operator*(const float& lhs, const SIMDMatrix& rhs)
{
return rhs * lhs;
}
#pragma endregion