-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmode1.cpp
More file actions
73 lines (52 loc) · 1.3 KB
/
mode1.cpp
File metadata and controls
73 lines (52 loc) · 1.3 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
// mode1
#include <immintrin.h> // portable to all x86 compilers
#include <stdio.h>
#include <time.h>
#define DATA float
const int SIZE = 128;
// size 128 / 256 / 512
DATA __attribute__((aligned(16))) A[SIZE] ;
DATA __attribute__((aligned(16))) B[SIZE] ;
double seconds() {
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
return now.tv_sec + now.tv_nsec / 1000000000.0;
}
void initialize_array(DATA a[], int size) {
for (int i = 0 ; i < size ; i++) a[i] = rand()%2;
}
int vec(DATA *s1, DATA *s2,int size) {
int i;
DATA prod = 0;
for(i=0; i<size; i++) prod += s1[i] * s2[i];
return prod;
}
int vec_sse(DATA *m1, DATA *m2,int size) {
DATA prod = 0;
int i;
__m128 X, Y, Z;
Z[0] = Z[1] = Z[2] = Z[3] = 0;
for(i=0; i<size; i+=4) {
X = _mm_load_ps(&m1[i]);
Y = _mm_load_ps(&m2[i]);
X = _mm_mul_ps(X, Y);
Z = _mm_add_ps(X, Z);
}
for(i=0; i<4; i++) prod += Z[i];
return prod;
}
int main() {
DATA r;
double before,after;
initialize_array(A,SIZE);
initialize_array(B,SIZE);
before = seconds();
r = vec(A,B,SIZE);
after = seconds();
printf("Result:%f Time:%f\n",r,after-before);
before = seconds();
r = vec_sse(A,B,SIZE);
after = seconds();
printf("Result:%f Time:%f\n",r,after-before);
return 0;
}