-
Notifications
You must be signed in to change notification settings - Fork 0
/
v4_avx.h
313 lines (269 loc) · 7.91 KB
/
v4_avx.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#ifndef _V4_AVX_H_
#define _V4_AVX_H_
/* minimalistic mat4d/vec4d library with AVX. mat4d is m4, vec4d is v4.
* #define is your friend if you want to port code using any other name.
* add -mavx to your CFLAGS. you might also want to use -O3 -Winline for the inlines.
* v4_avx.c in the repository has extern inline decls which can be used to make a v4_avx.o
* @author Fahrican Koşar, duck2 at protonmail.com, MIT license. */
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <immintrin.h>
#define clamp(x) (fmin(1, fmax(x, 0)))
/* v4p holds the packed 128 bit vector. */
struct v4 {
union {
__m256d v4p;
struct {
union {double x; double r;};
union {double y; double g;};
union {double z; double b;};
union {double t; double a;};
};
};
};
typedef struct v4 v4;
struct m4 {
union {
v4 row[4];
double d[16];
double dd[4][4];
};
};
typedef struct m4 m4;
inline v4
mkv4(double x, double y, double z, double t){
v4 out;
out.v4p= _mm256_set_pd(t, z, y, x);
return out;
}
inline v4
addv4(v4 p, v4 q){
v4 out;
out.v4p = _mm256_add_pd(p.v4p, q.v4p);
return out;
}
inline v4
subv4(v4 p, v4 q){
v4 out;
out.v4p = _mm256_sub_pd(p.v4p, q.v4p);
return out;
}
/* elementwise product */
inline v4
elpv4(v4 p, v4 q){
v4 out;
out.v4p = _mm256_mul_pd(p.v4p, q.v4p);
return out;
}
inline v4
scv4(double s, v4 p){
v4 out; __m256d sc = _mm256_set1_pd(s);
out.v4p = _mm256_mul_pd(p.v4p, sc);
return out;
}
/* XXX: I believe there is a more quick way to do this */
inline double
dotv4(v4 p, v4 q){
v4 x;
x.v4p = _mm256_mul_pd(p.v4p, q.v4p);
return x.x + x.y + x.z + x.t;
}
/* there is no cross product in 4d. we assume 3d */
#define MASK _MM_SHUFFLE(3, 0, 2, 1)
inline v4
crv4(v4 p, v4 q){
v4 out;
__m256d pt = _mm256_permute4x64_pd(p.v4p, MASK);
__m256d qt = _mm256_permute4x64_pd(q.v4p, MASK);
__m256d pqt = _mm256_sub_pd(_mm256_mul_pd(p.v4p, qt), _mm256_mul_pd(q.v4p, pt));
out.v4p = _mm256_permute4x64_pd(pqt, MASK);
return out;
}
#undef MASK
inline v4
norv4(v4 p){
v4 out; double a = 1/sqrt(dotv4(p, p));
__m256d dn = _mm256_set1_pd(a);
out.v4p = _mm256_mul_pd(p.v4p, dn);
return out;
}
inline double
lenv4(v4 p){
return sqrt(dotv4(p, p));
}
/* m4 :D:D we carelessly pass 64 byte matrices by value, relying on the compiler to inline */
inline m4
mkm4(v4 x, v4 y, v4 z, v4 t){
m4 out;
out.row[0] = x; out.row[1] = y; out.row[2] = z; out.row[3] = t;
return out;
}
/* if you feel so AVX you can load from out.row[0] to __m256's and op them with _mm256256_op_pd. */
inline m4
addm4(m4 p, m4 q){
m4 out;
out.row[0] = addv4(p.row[0], q.row[0]); out.row[1] = addv4(p.row[1], q.row[1]);
out.row[2] = addv4(p.row[2], q.row[2]); out.row[3] = addv4(p.row[3], q.row[3]);
return out;
}
inline m4
subm4(m4 p, m4 q){
m4 out;
out.row[0] = subv4(p.row[0], q.row[0]); out.row[1] = subv4(p.row[1], q.row[1]);
out.row[2] = subv4(p.row[2], q.row[2]); out.row[3] = subv4(p.row[3], q.row[3]);
return out;
}
inline m4
elpm4(m4 p, m4 q){
m4 out;
out.row[0] = elpv4(p.row[0], q.row[0]); out.row[1] = elpv4(p.row[1], q.row[1]);
out.row[2] = elpv4(p.row[2], q.row[2]); out.row[3] = elpv4(p.row[3], q.row[3]);
return out;
}
inline m4
scm4(double s, m4 p){
m4 out;
out.row[0] = scv4(s, p.row[0]); out.row[1] = scv4(s, p.row[1]);
out.row[2] = scv4(s, p.row[2]); out.row[3] = scv4(s, p.row[3]);
return out;
}
/* see http://stackoverflow.com/questions/18499971/ */
inline m4
mulm4(m4 p, m4 q){
m4 out; __m256d xs, ys, zs, ts; int i;
for(i=0; i<4; i++){
xs = _mm256_set1_pd(p.row[i].x); ys = _mm256_set1_pd(p.row[i].y);
zs = _mm256_set1_pd(p.row[i].z); ts = _mm256_set1_pd(p.row[i].t);
out.row[i].v4p = _mm256_add_pd(_mm256_add_pd(_mm256_mul_pd(xs, q.row[0].v4p), _mm256_mul_pd(ys, q.row[1].v4p)),
_mm256_add_pd(_mm256_mul_pd(zs, q.row[2].v4p), _mm256_mul_pd(ts, q.row[3].v4p)));
}
return out;
}
/* XXX: there is certainly a quicker way */
inline v4
vecm4(m4 p, v4 q){
v4 out;
out.v4p = _mm256_set_pd(dotv4(p.row[3], q), dotv4(p.row[2], q), dotv4(p.row[1], q), dotv4(p.row[0], q));
return out;
}
/* XXX: there is certainly a quicker way */
inline m4
trpm4(m4 p){
m4 q = mkm4(mkv4(p.row[0].x, p.row[1].x, p.row[2].x, p.row[3].x),
mkv4(p.row[0].y, p.row[1].y, p.row[2].y, p.row[3].y),
mkv4(p.row[0].z, p.row[1].z, p.row[2].z, p.row[3].z),
mkv4(p.row[0].t, p.row[1].t, p.row[2].t, p.row[3].t));
return q;
}
/* see http://stackoverflow.com/questions/1148309/inverting-a-4x4-matrix
* slightly modified but there might be room for improvement. */
inline m4
invm4(m4 p){
m4 inv; double det;
inv.d[0] = p.d[5] * p.d[10] * p.d[15] -
p.d[5] * p.d[11] * p.d[14] -
p.d[9] * p.d[6] * p.d[15] +
p.d[9] * p.d[7] * p.d[14] +
p.d[13] * p.d[6] * p.d[11] -
p.d[13] * p.d[7] * p.d[10];
inv.d[4] = -p.d[4] * p.d[10] * p.d[15] +
p.d[4] * p.d[11] * p.d[14] +
p.d[8] * p.d[6] * p.d[15] -
p.d[8] * p.d[7] * p.d[14] -
p.d[12] * p.d[6] * p.d[11] +
p.d[12] * p.d[7] * p.d[10];
inv.d[8] = p.d[4] * p.d[9] * p.d[15] -
p.d[4] * p.d[11] * p.d[13] -
p.d[8] * p.d[5] * p.d[15] +
p.d[8] * p.d[7] * p.d[13] +
p.d[12] * p.d[5] * p.d[11] -
p.d[12] * p.d[7] * p.d[9];
inv.d[12] = -p.d[4] * p.d[9] * p.d[14] +
p.d[4] * p.d[10] * p.d[13] +
p.d[8] * p.d[5] * p.d[14] -
p.d[8] * p.d[6] * p.d[13] -
p.d[12] * p.d[5] * p.d[10] +
p.d[12] * p.d[6] * p.d[9];
inv.d[1] = -p.d[1] * p.d[10] * p.d[15] +
p.d[1] * p.d[11] * p.d[14] +
p.d[9] * p.d[2] * p.d[15] -
p.d[9] * p.d[3] * p.d[14] -
p.d[13] * p.d[2] * p.d[11] +
p.d[13] * p.d[3] * p.d[10];
inv.d[5] = p.d[0] * p.d[10] * p.d[15] -
p.d[0] * p.d[11] * p.d[14] -
p.d[8] * p.d[2] * p.d[15] +
p.d[8] * p.d[3] * p.d[14] +
p.d[12] * p.d[2] * p.d[11] -
p.d[12] * p.d[3] * p.d[10];
inv.d[9] = -p.d[0] * p.d[9] * p.d[15] +
p.d[0] * p.d[11] * p.d[13] +
p.d[8] * p.d[1] * p.d[15] -
p.d[8] * p.d[3] * p.d[13] -
p.d[12] * p.d[1] * p.d[11] +
p.d[12] * p.d[3] * p.d[9];
inv.d[13] = p.d[0] * p.d[9] * p.d[14] -
p.d[0] * p.d[10] * p.d[13] -
p.d[8] * p.d[1] * p.d[14] +
p.d[8] * p.d[2] * p.d[13] +
p.d[12] * p.d[1] * p.d[10] -
p.d[12] * p.d[2] * p.d[9];
inv.d[2] = p.d[1] * p.d[6] * p.d[15] -
p.d[1] * p.d[7] * p.d[14] -
p.d[5] * p.d[2] * p.d[15] +
p.d[5] * p.d[3] * p.d[14] +
p.d[13] * p.d[2] * p.d[7] -
p.d[13] * p.d[3] * p.d[6];
inv.d[6] = -p.d[0] * p.d[6] * p.d[15] +
p.d[0] * p.d[7] * p.d[14] +
p.d[4] * p.d[2] * p.d[15] -
p.d[4] * p.d[3] * p.d[14] -
p.d[12] * p.d[2] * p.d[7] +
p.d[12] * p.d[3] * p.d[6];
inv.d[10] = p.d[0] * p.d[5] * p.d[15] -
p.d[0] * p.d[7] * p.d[13] -
p.d[4] * p.d[1] * p.d[15] +
p.d[4] * p.d[3] * p.d[13] +
p.d[12] * p.d[1] * p.d[7] -
p.d[12] * p.d[3] * p.d[5];
inv.d[14] = -p.d[0] * p.d[5] * p.d[14] +
p.d[0] * p.d[6] * p.d[13] +
p.d[4] * p.d[1] * p.d[14] -
p.d[4] * p.d[2] * p.d[13] -
p.d[12] * p.d[1] * p.d[6] +
p.d[12] * p.d[2] * p.d[5];
inv.d[3] = -p.d[1] * p.d[6] * p.d[11] +
p.d[1] * p.d[7] * p.d[10] +
p.d[5] * p.d[2] * p.d[11] -
p.d[5] * p.d[3] * p.d[10] -
p.d[9] * p.d[2] * p.d[7] +
p.d[9] * p.d[3] * p.d[6];
inv.d[7] = p.d[0] * p.d[6] * p.d[11] -
p.d[0] * p.d[7] * p.d[10] -
p.d[4] * p.d[2] * p.d[11] +
p.d[4] * p.d[3] * p.d[10] +
p.d[8] * p.d[2] * p.d[7] -
p.d[8] * p.d[3] * p.d[6];
inv.d[11] = -p.d[0] * p.d[5] * p.d[11] +
p.d[0] * p.d[7] * p.d[9] +
p.d[4] * p.d[1] * p.d[11] -
p.d[4] * p.d[3] * p.d[9] -
p.d[8] * p.d[1] * p.d[7] +
p.d[8] * p.d[3] * p.d[5];
inv.d[15] = p.d[0] * p.d[5] * p.d[10] -
p.d[0] * p.d[6] * p.d[9] -
p.d[4] * p.d[1] * p.d[10] +
p.d[4] * p.d[2] * p.d[9] +
p.d[8] * p.d[1] * p.d[6] -
p.d[8] * p.d[2] * p.d[5];
det = p.d[0] * inv.d[0] + p.d[1] * inv.d[4] + p.d[2] * inv.d[8] + p.d[3] * inv.d[12];
det = 1.0 / det;
__m256d detv = _mm256_set1_pd(det);
inv.row[0].v4p = _mm256_mul_pd(inv.row[0].v4p, detv);
inv.row[1].v4p = _mm256_mul_pd(inv.row[1].v4p, detv);
inv.row[2].v4p = _mm256_mul_pd(inv.row[2].v4p, detv);
inv.row[3].v4p = _mm256_mul_pd(inv.row[3].v4p, detv);
return inv;
}
#endif