-
Notifications
You must be signed in to change notification settings - Fork 12
/
example1.c
247 lines (204 loc) · 6.5 KB
/
example1.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
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <xmmintrin.h>
uint64_t
trace_cpu_time_now(void)
{
uint32_t a, d;
asm volatile ("rdtsc":"=a" (a), "=d" (d));
return (uint64_t)a + ((uint64_t)d << (uint64_t)32);
}
#define XSIZE 640
#define YSIZE 480
#define IMGSIZE XSIZE * YSIZE
#ifdef ALIGN_DATA_TO_16_BYTE
#define ALIGNED_DATA __attribute__ ((aligned(16)))
#else
#define ALIGNED_DATA
#endif
struct RGB {
unsigned char R;
unsigned char G;
unsigned char B;
} ALIGNED_DATA;
struct RGB input_IMG[IMGSIZE];
unsigned char output_IMG[IMGSIZE];
struct optimize_RGB {
unsigned char R[IMGSIZE];
unsigned char G[IMGSIZE];
unsigned char B[IMGSIZE];
} ALIGNED_DATA;
struct optimize_RGB opt_input_IMG;
typedef int v4si __attribute__ ((vector_size(16)));
struct simd_RGB {
v4si R[IMGSIZE / 4];
v4si G[IMGSIZE / 4];
v4si B[IMGSIZE / 4];
} ALIGNED_DATA;
struct simd_RGB simd_input_IMG;
v4si simd_output_IMG[IMGSIZE / 4];
#ifdef ORIGINAL_METHOD
void convert_RGB_2_black_and_white(struct RGB *img_input, unsigned char *img_output) {
int i;
double op = 0;
for(i = 0; i < IMGSIZE; i++) {
op = (0.299 * img_input[i].R) + (0.587 * img_input[i].G) + (0.144 * img_input[i].B);
img_output[i] = (unsigned char)op;
}
}
#endif
#ifdef FIXED_POINT_METHOD
void convert_RGB_2_black_and_white(struct RGB *img_input, unsigned char *img_output) {
int i;
unsigned int op = 0;
for(i = 0; i < IMGSIZE; i++) {
op = (1224 * img_input[i].R) + (2404 * img_input[i].G) + (467 * img_input[i].B);
img_output[i] = (unsigned char)(op >> 12);
}
}
#endif
#ifdef LOOP_UNROLLING_METHOD
void convert_RGB_2_black_and_white(struct RGB *img_input, unsigned char *img_output) {
int i;
unsigned int op1 = 0;
unsigned int op2 = 0;
unsigned int op3 = 0;
unsigned int op4 = 0;
for(i = 0; i < IMGSIZE; i += 4) {
op1 = (1224 * img_input[i].R) + (2404 * img_input[i].G) + (467 * img_input[i].B);
op2 = (1224 * img_input[i + 1].R) + (2404 * img_input[i + 1].G) + (467 * img_input[i + 1].B);
op3 = (1224 * img_input[i + 2].R) + (2404 * img_input[i + 2].G) + (467 * img_input[i + 2].B);
op4 = (1224 * img_input[i + 3].R) + (2404 * img_input[i + 3].G) + (467 * img_input[i + 3].B);
img_output[i] = (unsigned char)(op1 >> 12);
img_output[i + 1] = (unsigned char)(op2 >> 12);
img_output[i + 2] = (unsigned char)(op3 >> 12);
img_output[i + 3] = (unsigned char)(op4 >> 12);
}
}
#endif
#ifdef OPTIMIZED_STRUCT_METHOD
void convert_RGB_2_black_and_white(struct optimize_RGB *img_input, unsigned char *img_output) {
int i;
unsigned int op1 = 0;
unsigned int op2 = 0;
unsigned int op3 = 0;
unsigned int op4 = 0;
unsigned int R1 = 0;
unsigned int R2 = 0;
unsigned int R3 = 0;
unsigned int R4 = 0;
unsigned int G1 = 0;
unsigned int G2 = 0;
unsigned int G3 = 0;
unsigned int G4 = 0;
unsigned int B1 = 0;
unsigned int B2 = 0;
unsigned int B3 = 0;
unsigned int B4 = 0;
#ifdef NO_INDEPENDENT_LOOP
for(i = 0; i < IMGSIZE; i += 4) {
R1 = 1224 * img_input->R[i];
R2 = 1224 * img_input->R[i + 1];
R3 = 1224 * img_input->R[i + 2];
R4 = 1224 * img_input->R[i + 3];
G1 = 2404 * img_input->G[i];
G2 = 2404 * img_input->G[i + 1];
G3 = 2404 * img_input->G[i + 2];
G4 = 2404 * img_input->G[i + 3];
B1 = 467 * img_input->B[i];
B2 = 467 * img_input->B[i + 1];
B3 = 467 * img_input->B[i + 2];
B4 = 467 * img_input->B[i + 3];
op1 = R1 + G1 + B1;
op2 = R2 + G2 + B2;
op3 = R3 + G3 + B3;
op4 = R4 + G4 + B4;
img_output[i] = (unsigned char)(op1 >> 12);
img_output[i + 1] = (unsigned char)(op2 >> 12);
img_output[i + 2] = (unsigned char)(op3 >> 12);
img_output[i + 3] = (unsigned char)(op4 >> 12);
}
#else
for (i = 0; i < IMGSIZE; i += 4) {
R1 = 1224 * img_input->R[i];
R2 = 1224 * img_input->R[i + 1];
R3 = 1224 * img_input->R[i + 2];
R4 = 1224 * img_input->R[i + 3];
}
for (i = 0; i < IMGSIZE; i += 4) {
G1 = 2404 * img_input->G[i];
G2 = 2404 * img_input->G[i + 1];
G3 = 2404 * img_input->G[i + 2];
G4 = 2404 * img_input->G[i + 3];
}
for (i = 0; i < IMGSIZE; i += 4) {
B1 = 467 * img_input->B[i];
B2 = 467 * img_input->B[i + 1];
B3 = 467 * img_input->B[i + 2];
B4 = 467 * img_input->B[i + 3];
}
for (i = 0; i < IMGSIZE; i += 4) {
op1 = R1 + G1 + B1;
op2 = R2 + G2 + B2;
op3 = R3 + G3 + B3;
op4 = R4 + G4 + B4;
img_output[i] = (unsigned char)(op1 >> 12);
img_output[i + 1] = (unsigned char)(op2 >> 12);
img_output[i + 2] = (unsigned char)(op3 >> 12);
img_output[i + 3] = (unsigned char)(op4 >> 12);
}
#endif
}
#endif
#ifdef SIMD_METHOD
void convert_RGB_2_black_and_white(struct simd_RGB *img_input, v4si *img_output) {
int i = 0;
v4si or1, og1, ob1, op1;
v4si or2, og2, ob2, op2;
v4si or3, og3, ob3, op3;
v4si or4, og4, ob4, op4;
for (; i < (IMGSIZE / 4); i += 4) {
or1 = 1224 * img_input->R[i];
or2 = 1224 * img_input->R[i + 1];
or3 = 1224 * img_input->R[i + 2];
or4 = 1224 * img_input->R[i + 3];
og1 = 2404 * img_input->G[i];
og2 = 2404 * img_input->G[i + 1];
og3 = 2404 * img_input->G[i + 2];
og4 = 2404 * img_input->G[i + 3];
ob1 = 467 * img_input->B[i];
ob2 = 467 * img_input->B[i + 1];
ob3 = 467 * img_input->B[i + 2];
ob4 = 467 * img_input->B[i + 3];
op1 = or1 + og1 + ob1;
op2 = or2 + og2 + ob2;
op3 = or3 + og3 + ob3;
op4 = or4 + og4 + ob4;
img_output[i] = (op1 >> 12);
img_output[i + 1] = (op2 >> 12);
img_output[i + 2] = (op3 >> 12);
img_output[i + 3] = (op4 >> 12);
}
}
#endif
int
main(void)
{
uint32_t ite_count = 0;
asm volatile("" ::: "memory");
uint64_t start = trace_cpu_time_now();
for (; ite_count < 100; ite_count++) {
#ifdef OPTIMIZED_STRUCT_METHOD
convert_RGB_2_black_and_white(&opt_input_IMG, output_IMG);
#elif defined(SIMD_METHOD)
convert_RGB_2_black_and_white(&simd_input_IMG, simd_output_IMG);
#else
convert_RGB_2_black_and_white(input_IMG, output_IMG);
#endif
}
asm volatile("" ::: "memory");
uint64_t end = trace_cpu_time_now();
printf("CPU cycles: %ld\n", end - start);
return 0;
}