/
sse-matvecmult.c
289 lines (238 loc) · 6.27 KB
/
sse-matvecmult.c
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
/*
Multiplication of matrix 4x4 by vector 4x1 --- FPU, SSE3 and SSE4 procedures
$Revision: 1.2 $
Author: Wojciech Mu³a
e-mail: wojciech_mula@poczta.onet.pl
www: http://0x80.pl/
License: BSD
initial release 24-06-2008, last update $Date: 2008-06-27 22:18:08 $
----------------------------------------------------------------------
Measure speed of different procedures: using FPU instruction, SSE3 (HADDPS)
and SSE4.1 (DPPS). Because DPPS has long latency (11 cycles) single
multiplication is slower then SSE3 procedure.
Compilation:
gcc -O3 -Wall -pedantic -std=c99 sse-matvecmul.c
you will need sse-aux.c file:
http://0x80.pl/snippets/asm/sse-aux.c
Usage:
run program without argument to read help
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <strings.h>
#include "sse-aux.c"
//========================================================================
float in_matrix[4][4] SIMD_ALIGN;
float in_vector[4] SIMD_ALIGN;
float out_vector1[4] SIMD_ALIGN;
float out_vector2[4] SIMD_ALIGN;
float out_vector3[4] SIMD_ALIGN;
//========================================================================
void matvecmul(float in_matrix[4][4], float in_vector[4], float out_vector[4]) {
int i, j;
for (i=0; i < 4; i++) {
out_vector[i] = 0.0;
for (j=0; j < 4; j++)
out_vector[i] += (in_matrix[i][j] * in_vector[j]);
}
}
//========================================================================
#ifdef DATA_ALIGNED
void sse3_matvecmul(float in_matrix[4][4], float in_vector[4], float out_vector[4]) {
__asm__ volatile (
"movaps (%0), %%xmm0 \n"
"movaps %%xmm0, %%xmm1 \n"
"movaps %%xmm0, %%xmm2 \n"
"movaps %%xmm0, %%xmm3 \n"
: /* no output */
: "r" (in_vector)
);
__asm__ volatile (
"mulps 0x00(%0), %%xmm0 \n"
"mulps 0x10(%0), %%xmm1 \n"
"mulps 0x20(%0), %%xmm2 \n"
"mulps 0x30(%0), %%xmm3 \n"
" \n"
"haddps %%xmm1, %%xmm0 \n"
"haddps %%xmm3, %%xmm2 \n"
"haddps %%xmm2, %%xmm0 \n"
" \n"
: /* no output */
: "r" (in_matrix)
);
__asm__ volatile (
"movaps %%xmm0, (%0)"
: /* no output */
: "r" (out_vector)
);
}
#else
void sse3_matvecmul(float in_matrix[4][4], float in_vector[4], float out_vector[4]) {
__asm__ volatile (
"movups (%0), %%xmm7"
: /* no output */
: "r" (in_vector)
);
__asm__ volatile (
"movups 0x00(%0), %%xmm0 \n"
"movups 0x10(%0), %%xmm1 \n"
"movups 0x20(%0), %%xmm2 \n"
"movups 0x30(%0), %%xmm3 \n"
" \n"
"mulps %%xmm7, %%xmm0 \n"
"mulps %%xmm7, %%xmm1 \n"
"mulps %%xmm7, %%xmm2 \n"
"mulps %%xmm7, %%xmm3 \n"
" \n"
"haddps %%xmm1, %%xmm0 \n"
"haddps %%xmm3, %%xmm2 \n"
"haddps %%xmm2, %%xmm0 \n"
" \n"
: /* no output */
: "r" (in_matrix)
);
__asm__ volatile (
"movups %%xmm0, (%0)"
: /* no output */
: "r" (out_vector)
);
}
#endif
//========================================================================
#ifdef DATA_ALIGNED
void sse4_matvecmul(float in_matrix[4][4], float in_vector[4], float out_vector[4]) {
__asm__ volatile (
"movaps (%0), %%xmm0 \n"
"movaps %%xmm0, %%xmm1 \n"
"movaps %%xmm0, %%xmm2 \n"
"movaps %%xmm0, %%xmm3 \n"
: /* no output */
: "r" (in_vector)
);
__asm__ volatile (
"dpps $0xf1, 0x00(%0), %%xmm0 \n"
"dpps $0xf2, 0x10(%0), %%xmm1 \n"
"dpps $0xf4, 0x20(%0), %%xmm2 \n"
"dpps $0xf8, 0x30(%0), %%xmm3 \n"
"orps %%xmm1, %%xmm0 \n"
"orps %%xmm3, %%xmm2 \n"
"orps %%xmm2, %%xmm0 \n"
: /* no output */
: "r" (in_matrix)
);
__asm__ volatile (
"movaps %%xmm0, (%0)"
:
: "r" (out_vector)
);
}
#else
void sse4_matvecmul(float in_matrix[4][4], float in_vector[4], float out_vector[4]) {
__asm__ volatile (
"movups (%0), %%xmm7"
:
: "r" (in_vector)
);
__asm__ volatile (
"movups 0x00(%0), %%xmm0 \n"
"movups 0x10(%0), %%xmm1 \n"
"movups 0x20(%0), %%xmm2 \n"
"movups 0x30(%0), %%xmm3 \n"
" \n"
"dpps $0xf1, %%xmm7, %%xmm0 \n"
"dpps $0xf2, %%xmm7, %%xmm1 \n"
"dpps $0xf4, %%xmm7, %%xmm2 \n"
"dpps $0xf8, %%xmm7, %%xmm3 \n"
" \n"
"orps %%xmm1, %%xmm0 \n"
"orps %%xmm3, %%xmm2 \n"
"orps %%xmm2, %%xmm0 \n"
" \n"
: /* no output */
: "r" (in_matrix)
);
__asm__ volatile (
"movups %%xmm0, (%0)"
:
: "r" (out_vector)
);
}
#endif
//========================================================================
void help(char* progname) {
printf("%s compare|fpu|sse3|sse4 iter-count\n", progname);
exit(EXIT_FAILURE);
}
int main(int argc, char* argv[]) {
int i, j;
enum {None, Compare, TestFPU, TestSSE3, TestSSE4} function;
int iterations = 0;
float range = 10000.0;
function = None;
if (argc > 1) {
if (strcasecmp(argv[1], "compare") == 0 || strcasecmp(argv[1], "cmp") == 0)
function = Compare;
else
if (strcasecmp(argv[1], "fpu") == 0)
function = TestFPU;
else
if (strcasecmp(argv[1], "sse3") == 0)
function = TestSSE3;
else
if (strcasecmp(argv[1], "sse4") == 0)
function = TestSSE4;
}
if (function == None)
help(argv[0]);
if (argc > 2)
iterations = atoi(argv[2]);
if (iterations <= 0)
iterations = 1;
initfrand();
for (i=0; i < 4; i++)
for (j=0; j < 4; j++)
in_matrix[i][j] = frand(-range, +range);
for (i=0; i < 4; i++)
in_vector[i] = frand(-range, range);
if (function != Compare && function != None)
printf("iterations = %d\n", iterations);
switch (function) {
case None:
break;
case Compare:
puts("Input matrix");
for (i=0; i < 4; i++)
print_vec_float(in_matrix[i]);
puts("");
puts("Input vector");
print_vec_float(in_vector);
puts("");
puts("mat*vec result:");
matvecmul(in_matrix, in_vector, out_vector1);
print_vec_float(out_vector1);
puts("");
puts("SSE3 mat*vec result:");
sse3_matvecmul(in_matrix, in_vector, out_vector2);
print_vec_float(out_vector2);
puts("");
puts("SSE4 mat*vec result:");
sse4_matvecmul(in_matrix, in_vector, out_vector3);
print_vec_float(out_vector3);
break;
case TestFPU:
while (iterations--)
matvecmul(in_matrix, in_vector, out_vector1);
break;
case TestSSE3:
while (iterations--)
sse3_matvecmul(in_matrix, in_vector, out_vector2);
break;
case TestSSE4:
while (iterations--)
sse4_matvecmul(in_matrix, in_vector, out_vector3);
break;
}
return 0;
}
// eof