-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbit_convert.c
371 lines (301 loc) · 9.61 KB
/
bit_convert.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*
* bit_convert.c
*
* Created on: Nov 7, 2013
* Author: hxin
*/
#include "print.h"
#include "bit_convert.h"
#include <stdio.h>
#include <xmmintrin.h>
#include <tmmintrin.h>
#include <emmintrin.h>
uint8_t BASE_SHIFT11[16] __aligned__ = { 0x0, 0x4, 0x8, 0xc, 0x2, 0x6, 0xa, 0xe,
0x1, 0x5, 0x9, 0xd, 0x3, 0x7, 0xb, 0xf };
char MASKA_16[16] __aligned__ = { 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
'A', 'A', 'A', 'A', 'A', 'A', 'A' };
char MASKC_16[16] __aligned__ = { 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C',
'C', 'C', 'C', 'C', 'C', 'C', 'C' };
char MASKG_16[16] __aligned__ = { 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G',
'G', 'G', 'G', 'G', 'G', 'G', 'G' };
char MASKT_16[16] __aligned__ = { 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'T',
'T', 'T', 'T', 'T', 'T', 'T', 'T' };
uint8_t BIT_A_16[16] __aligned__ = { 0x00, 0x00, 0x00, 0x00, //A
0x00, 0x00, 0x00, 0x00, //A
0x00, 0x00, 0x00, 0x00, //A
0x00, 0x00, 0x00, 0x00 //A
};
uint8_t BIT_C_16[16] __aligned__ = { 0x55, 0x55, 0x55, 0x55, //C
0x55, 0x55, 0x55, 0x55, //C
0x55, 0x55, 0x55, 0x55, //C
0x55, 0x55, 0x55, 0x55 //C
};
uint8_t BIT_G_16[16] __aligned__ = { 0xaa, 0xaa, 0xaa, 0xaa, //G
0xaa, 0xaa, 0xaa, 0xaa, //G
0xaa, 0xaa, 0xaa, 0xaa, //G
0xaa, 0xaa, 0xaa, 0xaa //G
};
uint8_t BIT_T_16[16] __aligned__ = { 0xff, 0xff, 0xff, 0xff, //T
0xff, 0xff, 0xff, 0xff, //T
0xff, 0xff, 0xff, 0xff, //T
0xff, 0xff, 0xff, 0xff //T
};
//Have to consider Intel's endians
uint8_t LOC_MASK11[64] = { 0x03, 0x03, 0x03, 0x03, //1
0x03, 0x03, 0x03, 0x03, //1
0x03, 0x03, 0x03, 0x03, //1
0x03, 0x03, 0x03, 0x03, //1
0x30, 0x30, 0x30, 0x30, //3
0x30, 0x30, 0x30, 0x30, //3
0x30, 0x30, 0x30, 0x30, //3
0x30, 0x30, 0x30, 0x30, //3
0x0c, 0x0c, 0x0c, 0x0c, //2
0x0c, 0x0c, 0x0c, 0x0c, //2
0x0c, 0x0c, 0x0c, 0x0c, //2
0x0c, 0x0c, 0x0c, 0x0c, //2
0xc0, 0xc0, 0xc0, 0xc0, //4
0xc0, 0xc0, 0xc0, 0xc0, //4
0xc0, 0xc0, 0xc0, 0xc0, //4
0xc0, 0xc0, 0xc0, 0xc0 //4
};
void c_convert2bit(char *str, int length, uint8_t *bits) {
int i;
int j;
int k;
for (j = 0; j < length * 2 / (8 * sizeof(bits[0])) + 1; j++)
bits[j] = 0;
for (i = 0; i < length; i++) {
j = i * 2 / (sizeof(bits[0]) * 8);
k = i * 2 % (sizeof(bits[0]) * 8);
switch (str[i]) {
case 'C':
bits[j] += 1ULL << k;
break;
case 'G':
bits[j] += 2ULL << k;
break;
case 'T':
bits[j] += 3ULL << k;
break;
default:
break;
}
/*
int m;
for (m = 63; m >= 0; m--) {
// cout << "m:" << m << " ";
if (temp & (1ULL << m) )
printf("1");
else
printf("0");
}
*/
}
}
void sse3_convert2bit11(char *str, int length, uint8_t *bits) {
__m128i *shift_hint = (__m128i *) BASE_SHIFT11;
__m128i *cast_str;
// __m128i *output_str;
__m128i temp;
__m128i result;
//loading comparison values
__m128i *maskA = (__m128i *) MASKA_16;
__m128i *maskC = (__m128i *) MASKC_16;
__m128i *maskG = (__m128i *) MASKG_16;
__m128i *maskT = (__m128i *) MASKT_16;
__m128i *mask;
int i, j;
for (i = 0; i < length; i += 64) {
for (j = 0; j < 64; j += 16) {
cast_str = (__m128i *) (str + i + j);
*cast_str = _mm_shuffle_epi8(*cast_str, *shift_hint);
}
//printf("After shifting 0: %s\n", A_filled_t);
for (j = 8; j < 64; j += 32) {
cast_str = (__m128i *) (str + i + j);
temp = _mm_loadu_si128(cast_str);
temp = _mm_shuffle_epi32(temp, 0x4e);
_mm_storeu_si128(cast_str, temp);
}
//printf("After shifting 1: %s\n", A_filled_t);
temp = *((__m128i *) (str + i + 16));
*((__m128i *) (str + i + 16)) = *((__m128i *) (str + i + 32));
*((__m128i *) (str + i + 32)) = temp;
//printf("After shifting 2: %s\n", A_filled_t);
for (j = 0; j < 64; j += 16) {
cast_str = (__m128i *) (str + i + j);
temp = _mm_load_si128(cast_str);
temp = _mm_shuffle_epi32(temp, 0xd8);
_mm_store_si128(cast_str, temp);
}
//printf("After shifting 3: %s\n", A_filled_t);
for (j = 8; j < 64; j += 32) {
cast_str = (__m128i *) (str + i + j);
temp = _mm_loadu_si128(cast_str);
temp = _mm_shuffle_epi32(temp, 0x4e);
_mm_storeu_si128(cast_str, temp);
}
result = _mm_set1_epi32(0);
__m128i* bit_idx = (__m128i*) (bits + (i * 2) / (8 * sizeof(bits[0]) ) );
*bit_idx = _mm_set1_epi32(0);
for (j = 0; j < 64; j += 16) {
cast_str = (__m128i *) (str + i + j);
temp = _mm_cmpeq_epi8(*maskC, *cast_str);
result = _mm_and_si128(temp, *((__m128i *) BIT_C_16));
//print128_hex(result);
temp = _mm_cmpeq_epi8(*maskG, *cast_str);
temp = _mm_and_si128(temp, *((__m128i *) BIT_G_16));
result = _mm_or_si128(result, temp);
//print128_hex(result);
temp = _mm_cmpeq_epi8(*maskT, *cast_str);
temp = _mm_and_si128(temp, *((__m128i *) BIT_T_16));
result = _mm_or_si128(result, temp);
//print128_hex(result);
mask = (__m128i *) (LOC_MASK11 + j);
result = _mm_and_si128(*mask, result);
//print128_hex(result);
*bit_idx = _mm_or_si128(*bit_idx, result);
//print128_bit(result);
//print128_bit(*bit_idx);
}
}
}
uint8_t BIT_00[16] __aligned__ = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
uint8_t BIT_FF[16] __aligned__ = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
uint8_t BASE_SHIFT1[16] __aligned__ = { 0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13,
3, 11, 7, 15 };
uint8_t BASE_SHIFT2[16] __aligned__ = { 0, 1, 8, 9, 4, 5, 12, 13, 2, 3, 10, 11,
6, 7, 14, 15 };
//Have to consider Intel's endians
uint8_t LOC_MASK1[128] = { 0x01, 0x01, 0x01, 0x01, //1
0x01, 0x01, 0x01, 0x01, //1
0x01, 0x01, 0x01, 0x01, //1
0x01, 0x01, 0x01, 0x01, //1
0x04, 0x04, 0x04, 0x04, //3
0x04, 0x04, 0x04, 0x04, //3
0x04, 0x04, 0x04, 0x04, //3
0x04, 0x04, 0x04, 0x04, //3
0x02, 0x02, 0x02, 0x02, //2
0x02, 0x02, 0x02, 0x02, //2
0x02, 0x02, 0x02, 0x02, //2
0x02, 0x02, 0x02, 0x02, //2
0x08, 0x08, 0x08, 0x08, //4
0x08, 0x08, 0x08, 0x08, //4
0x08, 0x08, 0x08, 0x08, //4
0x08, 0x08, 0x08, 0x08, //4
0x10, 0x10, 0x10, 0x10, //5
0x10, 0x10, 0x10, 0x10, //5
0x10, 0x10, 0x10, 0x10, //5
0x10, 0x10, 0x10, 0x10, //5
0x40, 0x40, 0x40, 0x40, //7
0x40, 0x40, 0x40, 0x40, //7
0x40, 0x40, 0x40, 0x40, //7
0x40, 0x40, 0x40, 0x40, //7
0x20, 0x20, 0x20, 0x20, //6
0x20, 0x20, 0x20, 0x20, //6
0x20, 0x20, 0x20, 0x20, //6
0x20, 0x20, 0x20, 0x20, //6
0x80, 0x80, 0x80, 0x80, //8
0x80, 0x80, 0x80, 0x80, //8
0x80, 0x80, 0x80, 0x80, //8
0x80, 0x80, 0x80, 0x80 //8
};
//AATG_GCGA_CGAG_CCTA
//AATG_GCGA_CGAG_CCTA
//AAAA AA
void sse3_convert2bit1(char *str, uint8_t *bits0, uint8_t *bits1) {
__m128i *shift_hint = (__m128i *) BASE_SHIFT1;
__m128i *cast_str;
__m128i temp;
__m128i result0, result1;
//Mask for bit0 and bit1
__m128i *maskA = (__m128i *) MASKA_16;
__m128i *maskC = (__m128i *) MASKC_16;
__m128i *maskG = (__m128i *) MASKG_16;
__m128i *maskT = (__m128i *) MASKT_16;
__m128i *mask;
int i;
for (i = 0; i < 128; i += 16) {
cast_str = (__m128i *) (str + i);
*cast_str = _mm_shuffle_epi8(*cast_str, *shift_hint);
}
// printf("After shifting 0: %s\n", str);
for (i = 8; i < 128; i += 32) {
cast_str = (__m128i *) (str + i);
temp = _mm_loadu_si128(cast_str);
temp = _mm_shuffle_epi32(temp, 0x4e);
_mm_storeu_si128(cast_str, temp);
}
// printf("After shifting 1: %s\n", str);
shift_hint = (__m128i *) BASE_SHIFT2;
for (i = 0; i < 128; i += 16) {
cast_str = (__m128i *) (str + i);
*cast_str = _mm_shuffle_epi8(*cast_str, *shift_hint);
}
// printf("After shifting 2: %s\n", str);
for (i = 16; i < 128; i += 64) {
temp = *((__m128i *) (str + i));
*((__m128i *) (str + i)) = *((__m128i *) (str + i + 16));
*((__m128i *) (str + i + 16)) = temp;
}
// printf("After shifting 3: %s\n", str);
for (i = 8; i < 128; i += 32) {
cast_str = (__m128i *) (str + i);
temp = _mm_loadu_si128(cast_str);
temp = _mm_shuffle_epi32(temp, 0x4e);
_mm_storeu_si128(cast_str, temp);
}
// printf("After shifting 4: %s\n", str);
for (i = 0; i < 128; i += 16) {
cast_str = (__m128i *) (str + i);
temp = _mm_load_si128(cast_str);
temp = _mm_shuffle_epi32(temp, 0xd8);
_mm_store_si128(cast_str, temp);
}
// printf("After shifting 5: %s\n", str);
for (i = 16; i < 64; i += 32) {
temp = *((__m128i *) (str + i));
*((__m128i *) (str + i)) = *((__m128i *) (str + i + 48));
*((__m128i *) (str + i + 48)) = temp;
}
// printf("After shifting 6: %s\n", str);
for (i = 8; i < 128; i += 32) {
cast_str = (__m128i *) (str + i);
temp = _mm_loadu_si128(cast_str);
temp = _mm_shuffle_epi32(temp, 0x4e);
_mm_storeu_si128(cast_str, temp);
}
// printf("After shifting 7: %s\n", str);
result0 = _mm_set1_epi32(0);
result1 = _mm_set1_epi32(0);
__m128i* bit0_reg = (__m128i*) bits0;
__m128i* bit1_reg = (__m128i*) bits1;
*bit0_reg = _mm_set1_epi32(0);
*bit1_reg = _mm_set1_epi32(0);
for (i = 0; i < 128; i += 16) {
cast_str = (__m128i *) (str + i);
temp = _mm_cmpeq_epi8(*maskC, *cast_str);
result0 = _mm_and_si128(temp, *((__m128i *) BIT_FF)); //C=01
//print128_hex(result);
temp = _mm_cmpeq_epi8(*maskG, *cast_str);
temp = _mm_and_si128(temp, *((__m128i *) BIT_FF));
result1 = _mm_or_si128(result1, temp); //G=10
//print128_hex(result);
temp = _mm_cmpeq_epi8(*maskT, *cast_str);
temp = _mm_and_si128(temp, *((__m128i *) BIT_FF)); //T=11
result0 = _mm_or_si128(result0, temp); //T=11
result1 = _mm_or_si128(result1, temp); //T=11
//print128_hex(result);
mask = (__m128i *) (LOC_MASK1 + i);
result0 = _mm_and_si128(*mask, result0);
result1 = _mm_and_si128(*mask, result1);
//print128_hex(result);
*bit0_reg = _mm_or_si128(*bit0_reg, result0);
*bit1_reg = _mm_or_si128(*bit1_reg, result1);
//print128_bit(result);
// print128_bit(*bit0_reg);
// print128_bit(*bit1_reg);
}
}