-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbit_convertMain.c
308 lines (244 loc) · 6.32 KB
/
bit_convertMain.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
* bit_convertMain.c
*
* Created on: Nov 7, 2013
* Author: hxin
*/
#include "bit_convert.h"
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define OPT_COUNT 4
char* functions[OPT_COUNT] = { "verify", "serial", "sse3_11", "sse3_1" };
#define _MAX_LENGTH_ 320
#define _MAX_LENGTH_11_ 128
char read_t[_MAX_LENGTH_] __aligned__;
uint8_t read_bit_t[_MAX_LENGTH_ / 4] __aligned__;
uint8_t ref_bit_t[_MAX_LENGTH_ / 4] __aligned__;
uint8_t read_bit0_t[_MAX_LENGTH_ / 8] __aligned__;
uint8_t read_bit1_t[_MAX_LENGTH_ / 8] __aligned__;
uint8_t ref_bit0_t[_MAX_LENGTH_11_ / 8] __aligned__;
uint8_t ref_bit1_t[_MAX_LENGTH_11_ / 8] __aligned__;
void help(const char* progname) {
int i;
printf("%s ", progname);
printf("%s", functions[0]);
for (i = 1; i < OPT_COUNT; i++)
printf("|%s", functions[i]);
printf(" length-count repeat-count\n");
exit(1);
}
char verify(int index, void (*func)(char*, int, uint8_t*), char *str,
int length, uint8_t* bits, uint8_t* bits_ref) {
char failed = 0;
(*func)(str, length, bits);
int i;
printf("%10s -> ", functions[index]);
for (i = 0; i <= (length - 1) * 2 / (sizeof(bits[0]) * 8); i++) {
if (bits[i] != bits_ref[i])
failed = 1;
int j;
for (j = sizeof(bits[0]) * 8 - 1; j >= 0; j--) {
if (bits[i] & 1ULL << j)
printf("1");
else
printf("0");
}
}
printf("\n");
if (failed)
printf("***FAILED!!!***\n");
return failed;
}
char verify(int index, void (*func)(char*, uint8_t*, uint8_t*), char *str,
int length, uint8_t* bits0, uint8_t* bits1, uint8_t* bits_ref) {
char failed = 0;
(*func)(str, bits0, bits1);
int i;
uint8_t bit;
printf("%10s -> ", functions[index]);
for (i = 0; i <= (length - 1) * 2 / (sizeof(bits_ref[0]) * 8); i++) {
bit = 0;
if (i % 2 == 0) {
for (int j = 0; j < 4; j++) {
bit |= (bits0[i / 2] & (1 << j)) << j;
bit |= (bits1[i / 2] & (1 << j)) << (j + 1);
}
} else {
for (int j = 0; j < 4; j++) {
bit |= (bits0[i / 2] & (1 << (j + 4))) >> 4 << j;
bit |= (bits1[i / 2] & (1 << (j + 4))) >> 4 << (j + 1);
}
}
// printf("bit: %x, bits_ref[%d]: %x\n", bit, i, bits_ref[i]);
if (bit != bits_ref[i])
failed = 1;
}
// printf("\nbit0: ");
//
// for (i = 0; i < length / (sizeof(bits0[0]) * 8); i++) {
// int j;
// for (j = sizeof(bits0[0]) * 8 - 1; j >= 0; j--) {
// if (bits0[i] & 1ULL << j)
// printf("1");
// else
// printf("0");
// }
// }
//
// printf("\nbit1: ");
//
// for (i = 0; i < length / (sizeof(bits1[0]) * 8); i++) {
// int j;
// for (j = sizeof(bits1[0]) * 8 - 1; j >= 0; j--) {
// if (bits1[i] & 1ULL << j)
// printf("1");
// else
// printf("0");
// }
// }
// printf("\nmerged together: ");
for (i = 0; i < (length - 1) / (sizeof(bits1[0]) * 8) + 1; i++) {
int j;
for (j = 3; j >= 0; j--) {
if (bits1[i] & 1ULL << j)
printf("1");
else
printf("0");
if (bits0[i] & 1ULL << j)
printf("1");
else
printf("0");
}
for (j = 7; j >= 4; j--) {
if (bits1[i] & 1ULL << j)
printf("1");
else
printf("0");
if (bits0[i] & 1ULL << j)
printf("1");
else
printf("0");
}
}
printf("\n");
if (failed)
printf("***FAILED!!!***\n");
return failed;
}
int main(int argc, char* argv[]) {
// prog parametrs
int function;
int length_count;
int repeat_count;
int default_length_count = 100;
int default_repeat_count = 100000;
double time_beg = 0;
double time_end = 0;
int i;
// parse arguments
if (argc == 1)
help(argv[0]);
// - function
for (function = 0; function < OPT_COUNT; function++)
if (strcasecmp(argv[1], functions[function]) == 0)
break;
if (function == OPT_COUNT)
help(argv[0]);
// - 16-byte chunks
if (argc >= 3) {
length_count = atoi(argv[2]);
if (length_count <= 0 || length_count > _MAX_LENGTH_)
help(argv[0]);
} else
length_count = default_length_count;
// - repeat count
if (argc >= 4) {
repeat_count = atoi(argv[3]);
if (repeat_count <= 0)
help(argv[0]);
} else
repeat_count = default_repeat_count;
// fill buffer with random data
// srand(time(NULL ));
// for (i = 0; i < sizeof(read_t); i++) {
// read_t[i] = rand() % 4;
// switch (read_t[i]) {
// case 0:
// read_t[i] = 'A';
// break;
// case 1:
// read_t[i] = 'C';
// break;
// case 2:
// read_t[i] = 'G';
// break;
// case 3:
// read_t[i] = 'T';
// break;
// }
// }
strcpy(read_t,
"ACGCTAGTAGCCGGAATAACAGGTAGGCCTACATTTTCTATACGGCGCCGGCAACCTTGAGGGGCCGCGCCCCGTTACACTTTATACGTTTCCCTTGCAAGCCTTCGTGTCGGAGCATATGTATATGG");
printf("Data: ");
for (i = 0; i < length_count; i++)
printf("%c", read_t[i]);
printf("\n");
// run
printf("action=%s, length=%d, repeat count=%d\n", functions[function],
length_count, repeat_count);
char failed = 0;
time_beg = clock();
switch (function) {
case 0:
c_convert2bit(read_t, length_count, ref_bit_t);
// serial is reference reference
printf("%10s -> ", functions[1]);
for (i = 0; i <= (length_count - 1) * 2 / (sizeof(ref_bit_t[0]) * 8);
i++) {
int j;
for (j = sizeof(ref_bit_t[0]) * 8 - 1; j >= 0; j--) {
if (ref_bit_t[i] & 1ULL << j)
printf("1");
else
printf("0");
}
}
printf("\n");
// for (i = 0; i <= (length_count - 1) * 2 / (sizeof(ref_bit_t[0]) * 8);
// i++)
// printf("%2x", ref_bit_t[i]);
// printf("\n");
// printf("Before swapping: %s\n", read_t);
strcpy(read_t,
"ACGCTAGTAGCCGGAATAACAGGTAGGCCTACATTTTCTATACGGCGCCGGCAACCTTGAGGGGCCGCGCCCCGTTACACTTTATACGTTTCCCTTGCAAGCCTTCGTGTCGGAGCATATGTATATGG");
failed |= verify(2, &sse3_convert2bit11, read_t, length_count,
read_bit_t, ref_bit_t);
// printf("After swapping: %s\n", read_t);
strcpy(read_t,
"ACGCTAGTAGCCGGAATAACAGGTAGGCCTACATTTTCTATACGGCGCCGGCAACCTTGAGGGGCCGCGCCCCGTTACACTTTATACGTTTCCCTTGCAAGCCTTCGTGTCGGAGCATATGTATATGG");
failed |= verify(3, &sse3_convert2bit1, read_t, length_count,
read_bit0_t, read_bit1_t, ref_bit_t);
// printf("After swapping: %s\n", read_t);
if (failed)
return EXIT_FAILURE;
break;
case 1:
while (repeat_count--)
c_convert2bit(read_t, length_count, read_bit_t);
break;
case 2:
while (repeat_count--)
sse3_convert2bit11(read_t, length_count, read_bit_t);
break;
case 3:
while (repeat_count--)
sse3_convert2bit1(read_t, read_bit0_t, read_bit1_t);
break;
}
time_end = clock();
printf("Total time: %f\n", (time_end - time_beg) / CLOCKS_PER_SEC);
return EXIT_SUCCESS;
}
// eof