1 | /** |
---|
2 | * @file TypeManager.c |
---|
3 | * @author Sheng Di |
---|
4 | * @date May, 2016 |
---|
5 | * @brief TypeManager is used to manage the type array: parsing of the bytes and other types in between. |
---|
6 | * (C) 2016 by Mathematics and Computer Science (MCS), Argonne National Laboratory. |
---|
7 | * See COPYRIGHT in top-level directory. |
---|
8 | */ |
---|
9 | |
---|
10 | #include <stdio.h> |
---|
11 | #include <stdlib.h> |
---|
12 | #include "DynamicByteArray.h" |
---|
13 | #include "sz.h" |
---|
14 | |
---|
15 | //int convertIntArray2ByteArray_fast_8b() |
---|
16 | |
---|
17 | size_t convertIntArray2ByteArray_fast_1b(unsigned char* intArray, size_t intArrayLength, unsigned char **result) |
---|
18 | { |
---|
19 | size_t byteLength = 0; |
---|
20 | size_t i, j; |
---|
21 | if(intArrayLength%8==0) |
---|
22 | byteLength = intArrayLength/8; |
---|
23 | else |
---|
24 | byteLength = intArrayLength/8+1; |
---|
25 | |
---|
26 | if(byteLength>0) |
---|
27 | *result = (unsigned char*)malloc(byteLength*sizeof(unsigned char)); |
---|
28 | else |
---|
29 | *result = NULL; |
---|
30 | size_t n = 0; |
---|
31 | int tmp, type; |
---|
32 | for(i = 0;i<byteLength;i++) |
---|
33 | { |
---|
34 | tmp = 0; |
---|
35 | for(j = 0;j<8&&n<intArrayLength;j++) |
---|
36 | { |
---|
37 | type = intArray[n]; |
---|
38 | if(type == 1) |
---|
39 | tmp = (tmp | (1 << (7-j))); |
---|
40 | n++; |
---|
41 | } |
---|
42 | (*result)[i] = (unsigned char)tmp; |
---|
43 | } |
---|
44 | return byteLength; |
---|
45 | } |
---|
46 | |
---|
47 | size_t convertIntArray2ByteArray_fast_1b_to_result(unsigned char* intArray, size_t intArrayLength, unsigned char *result) |
---|
48 | { |
---|
49 | size_t byteLength = 0; |
---|
50 | size_t i, j; |
---|
51 | if(intArrayLength%8==0) |
---|
52 | byteLength = intArrayLength/8; |
---|
53 | else |
---|
54 | byteLength = intArrayLength/8+1; |
---|
55 | |
---|
56 | size_t n = 0; |
---|
57 | int tmp, type; |
---|
58 | for(i = 0;i<byteLength;i++) |
---|
59 | { |
---|
60 | tmp = 0; |
---|
61 | for(j = 0;j<8&&n<intArrayLength;j++) |
---|
62 | { |
---|
63 | type = intArray[n]; |
---|
64 | if(type == 1) |
---|
65 | tmp = (tmp | (1 << (7-j))); |
---|
66 | n++; |
---|
67 | } |
---|
68 | result[i] = (unsigned char)tmp; |
---|
69 | } |
---|
70 | return byteLength; |
---|
71 | } |
---|
72 | |
---|
73 | void convertByteArray2IntArray_fast_1b(size_t intArrayLength, unsigned char* byteArray, size_t byteArrayLength, unsigned char **intArray) |
---|
74 | { |
---|
75 | if(intArrayLength > byteArrayLength*8) |
---|
76 | { |
---|
77 | printf("Error: intArrayLength > byteArrayLength*8\n"); |
---|
78 | printf("intArrayLength=%zu, byteArrayLength = %zu", intArrayLength, byteArrayLength); |
---|
79 | exit(0); |
---|
80 | } |
---|
81 | if(intArrayLength>0) |
---|
82 | *intArray = (unsigned char*)malloc(intArrayLength*sizeof(unsigned char)); |
---|
83 | else |
---|
84 | *intArray = NULL; |
---|
85 | |
---|
86 | size_t n = 0, i; |
---|
87 | int tmp; |
---|
88 | for (i = 0; i < byteArrayLength-1; i++) |
---|
89 | { |
---|
90 | tmp = byteArray[i]; |
---|
91 | (*intArray)[n++] = (tmp & 0x80) >> 7; |
---|
92 | (*intArray)[n++] = (tmp & 0x40) >> 6; |
---|
93 | (*intArray)[n++] = (tmp & 0x20) >> 5; |
---|
94 | (*intArray)[n++] = (tmp & 0x10) >> 4; |
---|
95 | (*intArray)[n++] = (tmp & 0x08) >> 3; |
---|
96 | (*intArray)[n++] = (tmp & 0x04) >> 2; |
---|
97 | (*intArray)[n++] = (tmp & 0x02) >> 1; |
---|
98 | (*intArray)[n++] = (tmp & 0x01) >> 0; |
---|
99 | } |
---|
100 | |
---|
101 | tmp = byteArray[i]; |
---|
102 | if(n == intArrayLength) |
---|
103 | return; |
---|
104 | (*intArray)[n++] = (tmp & 0x80) >> 7; |
---|
105 | if(n == intArrayLength) |
---|
106 | return; |
---|
107 | (*intArray)[n++] = (tmp & 0x40) >> 6; |
---|
108 | if(n == intArrayLength) |
---|
109 | return; |
---|
110 | (*intArray)[n++] = (tmp & 0x20) >> 5; |
---|
111 | if(n == intArrayLength) |
---|
112 | return; |
---|
113 | (*intArray)[n++] = (tmp & 0x10) >> 4; |
---|
114 | if(n == intArrayLength) |
---|
115 | return; |
---|
116 | (*intArray)[n++] = (tmp & 0x08) >> 3; |
---|
117 | if(n == intArrayLength) |
---|
118 | return; |
---|
119 | (*intArray)[n++] = (tmp & 0x04) >> 2; |
---|
120 | if(n == intArrayLength) |
---|
121 | return; |
---|
122 | (*intArray)[n++] = (tmp & 0x02) >> 1; |
---|
123 | if(n == intArrayLength) |
---|
124 | return; |
---|
125 | (*intArray)[n++] = (tmp & 0x01) >> 0; |
---|
126 | } |
---|
127 | |
---|
128 | /** |
---|
129 | * little endian |
---|
130 | * [01|10|11|00|....]-->[01|10|11|00][....] |
---|
131 | * @param timeStepType |
---|
132 | * @return |
---|
133 | */ |
---|
134 | size_t convertIntArray2ByteArray_fast_2b(unsigned char* timeStepType, size_t timeStepTypeLength, unsigned char **result) |
---|
135 | { |
---|
136 | size_t i, j, byteLength = 0; |
---|
137 | if(timeStepTypeLength%4==0) |
---|
138 | byteLength = timeStepTypeLength*2/8; |
---|
139 | else |
---|
140 | byteLength = timeStepTypeLength*2/8+1; |
---|
141 | if(byteLength>0) |
---|
142 | *result = (unsigned char*)malloc(byteLength*sizeof(unsigned char)); |
---|
143 | else |
---|
144 | *result = NULL; |
---|
145 | size_t n = 0; |
---|
146 | for(i = 0;i<byteLength;i++) |
---|
147 | { |
---|
148 | int tmp = 0; |
---|
149 | for(j = 0;j<4&&n<timeStepTypeLength;j++) |
---|
150 | { |
---|
151 | int type = timeStepType[n]; |
---|
152 | switch(type) |
---|
153 | { |
---|
154 | case 0: |
---|
155 | |
---|
156 | break; |
---|
157 | case 1: |
---|
158 | tmp = (tmp | (1 << (6-j*2))); |
---|
159 | break; |
---|
160 | case 2: |
---|
161 | tmp = (tmp | (2 << (6-j*2))); |
---|
162 | break; |
---|
163 | case 3: |
---|
164 | tmp = (tmp | (3 << (6-j*2))); |
---|
165 | break; |
---|
166 | default: |
---|
167 | printf("Error: wrong timestep type...: type[%zu]=%d\n", n, type); |
---|
168 | exit(0); |
---|
169 | } |
---|
170 | n++; |
---|
171 | } |
---|
172 | (*result)[i] = (unsigned char)tmp; |
---|
173 | } |
---|
174 | return byteLength; |
---|
175 | } |
---|
176 | |
---|
177 | size_t convertIntArray2ByteArray_fast_2b_inplace(unsigned char* timeStepType, size_t timeStepTypeLength, unsigned char *result) |
---|
178 | { |
---|
179 | size_t i, j, byteLength = 0; |
---|
180 | if(timeStepTypeLength%4==0) |
---|
181 | byteLength = timeStepTypeLength*2/8; |
---|
182 | else |
---|
183 | byteLength = timeStepTypeLength*2/8+1; |
---|
184 | |
---|
185 | size_t n = 0; |
---|
186 | for(i = 0;i<byteLength;i++) |
---|
187 | { |
---|
188 | int tmp = 0; |
---|
189 | for(j = 0;j<4&&n<timeStepTypeLength;j++) |
---|
190 | { |
---|
191 | int type = timeStepType[n]; |
---|
192 | switch(type) |
---|
193 | { |
---|
194 | case 0: |
---|
195 | |
---|
196 | break; |
---|
197 | case 1: |
---|
198 | tmp = (tmp | (1 << (6-j*2))); |
---|
199 | break; |
---|
200 | case 2: |
---|
201 | tmp = (tmp | (2 << (6-j*2))); |
---|
202 | break; |
---|
203 | case 3: |
---|
204 | tmp = (tmp | (3 << (6-j*2))); |
---|
205 | break; |
---|
206 | default: |
---|
207 | printf("Error: wrong timestep type...: type[%zu]=%d\n", n, type); |
---|
208 | exit(0); |
---|
209 | } |
---|
210 | n++; |
---|
211 | } |
---|
212 | result[i] = (unsigned char)tmp; |
---|
213 | } |
---|
214 | return byteLength; |
---|
215 | } |
---|
216 | |
---|
217 | void convertByteArray2IntArray_fast_2b(size_t stepLength, unsigned char* byteArray, size_t byteArrayLength, unsigned char **intArray) |
---|
218 | { |
---|
219 | if(stepLength > byteArrayLength*4) |
---|
220 | { |
---|
221 | printf("Error: stepLength > byteArray.length*4\n"); |
---|
222 | printf("stepLength=%zu, byteArray.length=%zu\n", stepLength, byteArrayLength); |
---|
223 | exit(0); |
---|
224 | } |
---|
225 | if(stepLength>0) |
---|
226 | *intArray = (unsigned char*)malloc(stepLength*sizeof(unsigned char)); |
---|
227 | else |
---|
228 | *intArray = NULL; |
---|
229 | size_t i, n = 0; |
---|
230 | |
---|
231 | for (i = 0; i < byteArrayLength; i++) { |
---|
232 | unsigned char tmp = byteArray[i]; |
---|
233 | (*intArray)[n++] = (tmp & 0xC0) >> 6; |
---|
234 | if(n==stepLength) |
---|
235 | break; |
---|
236 | (*intArray)[n++] = (tmp & 0x30) >> 4; |
---|
237 | if(n==stepLength) |
---|
238 | break; |
---|
239 | (*intArray)[n++] = (tmp & 0x0C) >> 2; |
---|
240 | if(n==stepLength) |
---|
241 | break; |
---|
242 | (*intArray)[n++] = tmp & 0x03; |
---|
243 | if(n==stepLength) |
---|
244 | break; |
---|
245 | } |
---|
246 | } |
---|
247 | |
---|
248 | size_t convertIntArray2ByteArray_fast_3b(unsigned char* timeStepType, size_t timeStepTypeLength, unsigned char **result) |
---|
249 | { |
---|
250 | size_t i = 0, k = 0, byteLength = 0, n = 0; |
---|
251 | if(timeStepTypeLength%8==0) |
---|
252 | byteLength = timeStepTypeLength*3/8; |
---|
253 | else |
---|
254 | byteLength = timeStepTypeLength*3/8+1; |
---|
255 | |
---|
256 | if(byteLength>0) |
---|
257 | *result = (unsigned char*)malloc(byteLength*sizeof(unsigned char)); |
---|
258 | else |
---|
259 | *result = NULL; |
---|
260 | int tmp = 0; |
---|
261 | for(n = 0;n<timeStepTypeLength;n++) |
---|
262 | { |
---|
263 | k = n%8; |
---|
264 | switch(k) |
---|
265 | { |
---|
266 | case 0: |
---|
267 | tmp = tmp | (timeStepType[n] << 5); |
---|
268 | break; |
---|
269 | case 1: |
---|
270 | tmp = tmp | (timeStepType[n] << 2); |
---|
271 | break; |
---|
272 | case 2: |
---|
273 | tmp = tmp | (timeStepType[n] >> 1); |
---|
274 | (*result)[i++] = (unsigned char)tmp; |
---|
275 | tmp = 0 | (timeStepType[n] << 7); |
---|
276 | break; |
---|
277 | case 3: |
---|
278 | tmp = tmp | (timeStepType[n] << 4); |
---|
279 | break; |
---|
280 | case 4: |
---|
281 | tmp = tmp | (timeStepType[n] << 1); |
---|
282 | break; |
---|
283 | case 5: |
---|
284 | tmp = tmp | (timeStepType[n] >> 2); |
---|
285 | (*result)[i++] = (unsigned char)tmp; |
---|
286 | tmp = 0 | (timeStepType[n] << 6); |
---|
287 | break; |
---|
288 | case 6: |
---|
289 | tmp = tmp | (timeStepType[n] << 3); |
---|
290 | break; |
---|
291 | case 7: |
---|
292 | tmp = tmp | (timeStepType[n] << 0); |
---|
293 | (*result)[i++] = (unsigned char)tmp; |
---|
294 | tmp = 0; |
---|
295 | break; |
---|
296 | } |
---|
297 | } |
---|
298 | if(k!=7) //load the last one |
---|
299 | (*result)[i] = (unsigned char)tmp; |
---|
300 | |
---|
301 | return byteLength; |
---|
302 | } |
---|
303 | |
---|
304 | void convertByteArray2IntArray_fast_3b(size_t stepLength, unsigned char* byteArray, size_t byteArrayLength, unsigned char **intArray) |
---|
305 | { |
---|
306 | if(stepLength > byteArrayLength*8/3) |
---|
307 | { |
---|
308 | printf("Error: stepLength > byteArray.length*8/3, impossible case unless bugs elsewhere.\n"); |
---|
309 | printf("stepLength=%zu, byteArray.length=%zu\n", stepLength, byteArrayLength); |
---|
310 | exit(0); |
---|
311 | } |
---|
312 | if(stepLength>0) |
---|
313 | *intArray = (unsigned char*)malloc(stepLength*sizeof(unsigned char)); |
---|
314 | else |
---|
315 | *intArray = NULL; |
---|
316 | size_t i = 0, ii = 0, n = 0; |
---|
317 | unsigned char tmp = byteArray[i]; |
---|
318 | for(n=0;n<stepLength;) |
---|
319 | { |
---|
320 | switch(n%8) |
---|
321 | { |
---|
322 | case 0: |
---|
323 | (*intArray)[n++] = (tmp & 0xE0) >> 5; |
---|
324 | break; |
---|
325 | case 1: |
---|
326 | (*intArray)[n++] = (tmp & 0x1C) >> 2; |
---|
327 | break; |
---|
328 | case 2: |
---|
329 | ii = (tmp & 0x03) << 1; |
---|
330 | i++; |
---|
331 | tmp = byteArray[i]; |
---|
332 | ii |= (tmp & 0x80) >> 7; |
---|
333 | (*intArray)[n++] = ii; |
---|
334 | break; |
---|
335 | case 3: |
---|
336 | (*intArray)[n++] = (tmp & 0x70) >> 4; |
---|
337 | break; |
---|
338 | case 4: |
---|
339 | (*intArray)[n++] = (tmp & 0x0E) >> 1; |
---|
340 | break; |
---|
341 | case 5: |
---|
342 | ii = (tmp & 0x01) << 2; |
---|
343 | i++; |
---|
344 | tmp = byteArray[i]; |
---|
345 | ii |= (tmp & 0xC0) >> 6; |
---|
346 | (*intArray)[n++] = ii; |
---|
347 | break; |
---|
348 | case 6: |
---|
349 | (*intArray)[n++] = (tmp & 0x38) >> 3; |
---|
350 | break; |
---|
351 | case 7: |
---|
352 | (*intArray)[n++] = (tmp & 0x07); |
---|
353 | i++; |
---|
354 | tmp = byteArray[i]; |
---|
355 | break; |
---|
356 | } |
---|
357 | } |
---|
358 | } |
---|
359 | |
---|
360 | inline int getLeftMovingSteps(size_t k, unsigned char resiBitLength) |
---|
361 | { |
---|
362 | return 8 - k%8 - resiBitLength; |
---|
363 | } |
---|
364 | |
---|
365 | /** |
---|
366 | * |
---|
367 | * @param timeStepType is the resiMidBits |
---|
368 | * @param resiBitLength is the length of resiMidBits for each element, (the number of resiBitLength == the # of unpredictable elements |
---|
369 | * @return |
---|
370 | */ |
---|
371 | size_t convertIntArray2ByteArray_fast_dynamic(unsigned char* timeStepType, unsigned char resiBitLength, size_t nbEle, unsigned char **bytes) |
---|
372 | { |
---|
373 | size_t i = 0, j = 0, k = 0; |
---|
374 | int value; |
---|
375 | DynamicByteArray* dba; |
---|
376 | new_DBA(&dba, 1024); |
---|
377 | int tmp = 0, leftMovSteps = 0; |
---|
378 | for(j = 0;j<nbEle;j++) |
---|
379 | { |
---|
380 | if(resiBitLength==0) |
---|
381 | continue; |
---|
382 | value = timeStepType[i]; |
---|
383 | leftMovSteps = getLeftMovingSteps(k, resiBitLength); |
---|
384 | if(leftMovSteps < 0) |
---|
385 | { |
---|
386 | tmp = tmp | (value >> (-leftMovSteps)); |
---|
387 | addDBA_Data(dba, (unsigned char)tmp); |
---|
388 | tmp = 0 | (value << (8+leftMovSteps)); |
---|
389 | } |
---|
390 | else if(leftMovSteps > 0) |
---|
391 | { |
---|
392 | tmp = tmp | (value << leftMovSteps); |
---|
393 | } |
---|
394 | else //==0 |
---|
395 | { |
---|
396 | tmp = tmp | value; |
---|
397 | addDBA_Data(dba, (unsigned char)tmp); |
---|
398 | tmp = 0; |
---|
399 | } |
---|
400 | i++; |
---|
401 | k += resiBitLength; |
---|
402 | } |
---|
403 | if(leftMovSteps != 0) |
---|
404 | addDBA_Data(dba, (unsigned char)tmp); |
---|
405 | convertDBAtoBytes(dba, bytes); |
---|
406 | size_t size = dba->size; |
---|
407 | free_DBA(dba); |
---|
408 | return size; |
---|
409 | } |
---|
410 | |
---|
411 | /** |
---|
412 | * |
---|
413 | * @param timeStepType is the resiMidBits |
---|
414 | * @param resiBitLength is the length of resiMidBits for each element, (the number of resiBitLength == the # of unpredictable elements |
---|
415 | * @return |
---|
416 | */ |
---|
417 | size_t convertIntArray2ByteArray_fast_dynamic2(unsigned char* timeStepType, unsigned char* resiBitLength, size_t resiBitLengthLength, unsigned char **bytes) |
---|
418 | { |
---|
419 | size_t i = 0, j = 0, k = 0; |
---|
420 | int value; |
---|
421 | DynamicByteArray* dba; |
---|
422 | new_DBA(&dba, 1024); |
---|
423 | int tmp = 0, leftMovSteps = 0; |
---|
424 | for(j = 0;j<resiBitLengthLength;j++) |
---|
425 | { |
---|
426 | unsigned char rbl = resiBitLength[j]; |
---|
427 | if(rbl==0) |
---|
428 | continue; |
---|
429 | value = timeStepType[i]; |
---|
430 | leftMovSteps = getLeftMovingSteps(k, rbl); |
---|
431 | if(leftMovSteps < 0) |
---|
432 | { |
---|
433 | tmp = tmp | (value >> (-leftMovSteps)); |
---|
434 | addDBA_Data(dba, (unsigned char)tmp); |
---|
435 | tmp = 0 | (value << (8+leftMovSteps)); |
---|
436 | } |
---|
437 | else if(leftMovSteps > 0) |
---|
438 | { |
---|
439 | tmp = tmp | (value << leftMovSteps); |
---|
440 | } |
---|
441 | else //==0 |
---|
442 | { |
---|
443 | tmp = tmp | value; |
---|
444 | addDBA_Data(dba, (unsigned char)tmp); |
---|
445 | tmp = 0; |
---|
446 | } |
---|
447 | i++; |
---|
448 | k += rbl; |
---|
449 | } |
---|
450 | if(leftMovSteps != 0) |
---|
451 | addDBA_Data(dba, (unsigned char)tmp); |
---|
452 | convertDBAtoBytes(dba, bytes); |
---|
453 | size_t size = dba->size; |
---|
454 | free_DBA(dba); |
---|
455 | return size; |
---|
456 | } |
---|
457 | |
---|
458 | int computeBitNumRequired(size_t dataLength) |
---|
459 | { |
---|
460 | if(exe_params->SZ_SIZE_TYPE==4) |
---|
461 | return 32 - numberOfLeadingZeros_Int(dataLength); |
---|
462 | else |
---|
463 | return 64 - numberOfLeadingZeros_Long(dataLength); |
---|
464 | |
---|
465 | } |
---|
466 | |
---|
467 | void decompressBitArraybySimpleLZ77(int** result, unsigned char* bytes, size_t bytesLength, size_t totalLength, int validLength) |
---|
468 | { |
---|
469 | size_t pairLength = (bytesLength*8)/(validLength+1); |
---|
470 | size_t tmpLength = pairLength*2; |
---|
471 | int tmpResult[tmpLength]; |
---|
472 | size_t i, j, k = 0; |
---|
473 | for(i = 0;i<tmpLength;i+=2) |
---|
474 | { |
---|
475 | size_t outIndex = k/8; |
---|
476 | int innerIndex = k%8; |
---|
477 | |
---|
478 | unsigned char curByte = bytes[outIndex]; |
---|
479 | tmpResult[i] = (curByte >> (8-1-innerIndex)) & 0x01; |
---|
480 | k++; |
---|
481 | |
---|
482 | int numResult = extractBytes(bytes, k, validLength); |
---|
483 | |
---|
484 | tmpResult[i+1] = numResult; |
---|
485 | k = k + validLength; |
---|
486 | } |
---|
487 | |
---|
488 | *result = (int*)malloc(sizeof(int)*totalLength); |
---|
489 | k = 0; |
---|
490 | for(i = 0;i<tmpLength;i=i+2) |
---|
491 | { |
---|
492 | int state = tmpResult[i]; |
---|
493 | int num = tmpResult[i+1]; |
---|
494 | for(j = 0;j<num;j++) |
---|
495 | (*result)[k++] = state; |
---|
496 | } |
---|
497 | } |
---|