-
Notifications
You must be signed in to change notification settings - Fork 63
/
ulaalgo.h
216 lines (197 loc) · 7.5 KB
/
ulaalgo.h
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
// This file is part of the uSTL library, an STL implementation.
//
// Copyright (c) 2005 by Mike Sharov <[email protected]>
// This file is free software, distributed under the MIT License.
#pragma once
#include "umatrix.h"
#include "simd.h"
namespace ustl {
/// \brief Creates an identity matrix in \p m
/// \ingroup NumericAlgorithms
template <size_t NX, size_t NY, typename T>
void load_identity (matrix<NX,NY,T>& m)
{
fill_n (m.begin(), NX * NY, 0);
for (typename matrix<NX,NY,T>::iterator i = m.begin(); i < m.end(); i += NX + 1)
*i = 1;
}
/// \brief Multiplies two matrices
/// \ingroup NumericAlgorithms
template <size_t NX, size_t NY, typename T>
matrix<NY,NY,T> operator* (const matrix<NX,NY,T>& m1, const matrix<NY,NX,T>& m2)
{
matrix<NY,NY,T> mr;
for (uoff_t ry = 0; ry < NY; ++ ry) {
for (uoff_t rx = 0; rx < NY; ++ rx) {
T dpv (0);
for (uoff_t x = 0; x < NX; ++ x)
dpv += m1[ry][x] * m2[x][rx];
mr[ry][rx] = dpv;
}
}
return (mr);
}
/// \brief Transforms vector \p t with matrix \p m
/// \ingroup NumericAlgorithms
template <size_t NX, size_t NY, typename T>
tuple<NX,T> operator* (const tuple<NY,T>& t, const matrix<NX,NY,T>& m)
{
tuple<NX,T> tr;
for (uoff_t x = 0; x < NX; ++ x) {
T dpv (0);
for (uoff_t y = 0; y < NY; ++ y)
dpv += t[y] * m[y][x];
tr[x] = dpv;
}
return (tr);
}
/// \brief Transposes (exchanges rows and columns) matrix \p m.
/// \ingroup NumericAlgorithms
template <size_t N, typename T>
void transpose (matrix<N,N,T>& m)
{
for (uoff_t x = 0; x < N; ++ x)
for (uoff_t y = x; y < N; ++ y)
swap (m[x][y], m[y][x]);
}
#if WANT_UNROLLED_COPY
#if CPU_HAS_SSE
#if linux // Non-linux gcc versions (BSD, Solaris) can't handle "x" constraint and provide no alternative.
template <>
inline void load_identity (matrix<4,4,float>& m)
{
asm (
"movaps %4, %%xmm1 \n\t" // 1 0 0 0
"movups %4, %0 \n\t" // 1 0 0 0
"shufps $0xB1,%%xmm1,%%xmm1 \n\t" // 0 1 0 0
"movups %%xmm1, %1 \n\t" // 0 1 0 0
"shufps $0x4F,%4,%%xmm1 \n\t" // 0 0 1 0
"shufps $0x1B,%4,%4 \n\t" // 0 0 0 1
"movups %%xmm1, %2 \n\t" // 0 0 1 0
"movups %4, %3" // 0 0 0 1
: "=m"(m[0][0]), "=m"(m[1][0]), "=m"(m[2][0]), "=m"(m[3][0])
: "x"(1.0f)
: "xmm1", "memory"
);
asm ("":::"memory");
}
#endif
inline void _sse_load_matrix (const float* m)
{
asm (
"movups %0, %%xmm4 \n\t" // xmm4 = m[1 2 3 4]
"movups %1, %%xmm5 \n\t" // xmm5 = m[1 2 3 4]
"movups %2, %%xmm6 \n\t" // xmm6 = m[1 2 3 4]
"movups %3, %%xmm7" // xmm7 = m[1 2 3 4]
: : "m"(m[0]), "m"(m[4]), "m"(m[8]), "m"(m[12])
: "xmm4", "xmm5", "xmm6", "xmm7", "memory"
);
}
inline void _sse_transform_to_vector (float* result)
{
asm (
"movaps %%xmm0, %%xmm1 \n\t" // xmm1 = t[0 1 2 3]
"movaps %%xmm0, %%xmm2 \n\t" // xmm1 = t[0 1 2 3]
"movaps %%xmm0, %%xmm3 \n\t" // xmm1 = t[0 1 2 3]
"shufps $0x00, %%xmm0, %%xmm0 \n\t" // xmm0 = t[0 0 0 0]
"shufps $0x66, %%xmm1, %%xmm1 \n\t" // xmm1 = t[1 1 1 1]
"shufps $0xAA, %%xmm2, %%xmm2 \n\t" // xmm2 = t[2 2 2 2]
"shufps $0xFF, %%xmm3, %%xmm3 \n\t" // xmm3 = t[3 3 3 3]
"mulps %%xmm4, %%xmm0 \n\t" // xmm0 = t[0 0 0 0] * m[0 1 2 3]
"mulps %%xmm5, %%xmm1 \n\t" // xmm1 = t[1 1 1 1] * m[0 1 2 3]
"addps %%xmm1, %%xmm0 \n\t" // xmm0 = xmm0 + xmm1
"mulps %%xmm6, %%xmm2 \n\t" // xmm2 = t[2 2 2 2] * m[0 1 2 3]
"mulps %%xmm7, %%xmm3 \n\t" // xmm3 = t[3 3 3 3] * m[0 1 2 3]
"addps %%xmm3, %%xmm2 \n\t" // xmm2 = xmm2 + xmm3
"addps %%xmm2, %%xmm0 \n\t" // xmm0 = result
"movups %%xmm0, %0"
: "=m"(result[0]), "=m"(result[1]), "=m"(result[2]), "=m"(result[3]) :
: "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6", "xmm7", "memory"
);
}
template <>
inline tuple<4,float> operator* (const tuple<4,float>& t, const matrix<4,4,float>& m)
{
tuple<4,float> result;
_sse_load_matrix (m.begin());
asm ("movups %0, %%xmm0" : : "m"(t[0]), "m"(t[1]), "m"(t[2]), "m"(t[3]) : "xmm0", "memory");
_sse_transform_to_vector (result.begin());
return (result);
}
template <>
inline matrix<4,4,float> operator* (const matrix<4,4,float>& m1, const matrix<4,4,float>& m2)
{
matrix<4,4,float> result;
_sse_load_matrix (m2.begin());
for (uoff_t r = 0; r < 4; ++ r) {
asm ("movups %0, %%xmm0" : : "m"(m1[r][0]), "m"(m1[r][1]), "m"(m1[r][2]), "m"(m1[r][3]) : "xmm0", "memory");
_sse_transform_to_vector (result[r]);
}
return (result);
}
#elif CPU_HAS_3DNOW
/// Specialization for 4-component vector transform, the slow part of 3D graphics.
template <>
static tuple<4,float> operator* (const tuple<4,float>& t, const matrix<4,4,float>& m)
{
tuple<4,float> result;
// This is taken from "AMD Athlon Code Optimization Guide" from AMD. 18 cycles!
// If you are writing a 3D engine, you may want to copy it instead of calling it
// because of the femms instruction at the end, which takes 2 cycles.
asm (
"movq %2, %%mm0 \n\t" // y | x
"movq %3, %%mm1 \n\t" // w | z
"movq %%mm0, %%mm2 \n\t" // y | x
"movq %4, %%mm3 \n\t" // m[0][1] | m[0][0]
"punpckldq %%mm0, %%mm0 \n\t" // x | x
"movq %6, %%mm4 \n\t" // m[1][1] | m[1][0]
"pfmul %%mm0, %%mm3 \n\t" // x*m[0][1] | x*m[0][0]
"punpckhdq %%mm2, %%mm2 \n\t" // y | y
"pfmul %%mm2, %%mm4 \n\t" // y*m[1][1] | y*m[1][0]
"movq %5, %%mm5 \n\t" // m[0][3] | m[0][2]
"movq %7, %%mm7 \n\t" // m[1][3] | m[1][2]
"movq %%mm1, %%mm6 \n\t" // w | z
"pfmul %%mm0, %%mm5 \n\t" // x*m[0][3] | v0>x*m[0][2]
"movq %8, %%mm0 \n\t" // m[2][1] | m[2][0]
"punpckldq %%mm1, %%mm1 \n\t" // z | z
"pfmul %%mm2, %%mm7 \n\t" // y*m[1][3] | y*m[1][2]
"movq %9, %%mm2 \n\t" // m[2][3] | m[2][2]
"pfmul %%mm1, %%mm0 \n\t" // z*m[2][1] | z*m[2][0]
"pfadd %%mm4, %%mm3 \n\t" // x*m[0][1]+y*m[1][1] | x*m[0][0]+y*m[1][0]
"movq %10, %%mm4 \n\t" // m[3][1] | m[3][0]
"pfmul %%mm1, %%mm2 \n\t" // z*m[2][3] | z*m[2][2]
"pfadd %%mm7, %%mm5 \n\t" // x*m[0][3]+y*m[1][3] | x*m[0][2]+y*m[1][2]
"movq %11, %%mm1 \n\t" // m[3][3] | m[3][2]
"punpckhdq %%mm6, %%mm6 \n\t" // w | w
"pfadd %%mm0, %%mm3 \n\t" // x*m[0][1]+y*m[1][1]+z*m[2][1] | x*m[0][0]+y*m[1][0]+z*m[2][0]
"pfmul %%mm6, %%mm4 \n\t" // w*m[3][1] | w*m[3][0]
"pfmul %%mm6, %%mm1 \n\t" // w*m[3][3] | w*m[3][2]
"pfadd %%mm2, %%mm5 \n\t" // x*m[0][3]+y*m[1][3]+z*m[2][3] | x*m[0][2]+y*m[1][2]+z*m[2][2]
"pfadd %%mm4, %%mm3 \n\t" // x*m[0][1]+y*m[1][1]+z*m[2][1]+w*m[3][1] | x*m[0][0]+y*m[1][0]+z*m[2][0]+w*m[3][0]
"movq %%mm3, %0 \n\t" // store result->y | result->x
"pfadd %%mm1, %%mm5 \n\t" // x*m[0][3]+y*m[1][3]+z*m[2][3]+w*m[3][3] | x*m[0][2]+y*m[1][2]+z*m[2][2]+w*m[3][2]
"movq %%mm5, %1" // store result->w | result->z
: "=m"(result[0]), "=m"(result[2])
: "m"(t[0]), "m"(t[2]),
"m"(m[0][0]), "m"(m[0][2]),
"m"(m[1][0]), "m"(m[1][2]),
"m"(m[2][0]), "m"(m[2][2]),
"m"(m[3][0]), "m"(m[3][2])
: ALL_MMX_REGS_CHANGELIST, "memory"
);
asm ("":::"memory");
simd::reset_mmx();
return (result);
}
#else // If no processor extensions, just unroll the multiplication
/// Specialization for 4-component vector transform, the slow part of 3D graphics.
template <> inline tuple<4,float> operator* (const tuple<4,float>& t, const matrix<4,4,float>& m)
{
tuple<4,float> tr;
for (uoff_t i = 0; i < 4; ++ i)
tr[i] = t[0] * m[0][i] + t[1] * m[1][i] + t[2] * m[2][i] + t[3] * m[3][i];
return (tr);
}
#endif // CPU_HAS_3DNOW
#endif // WANT_UNROLLED_COPY
} // namespace ustl