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 | void convertByteArray2IntArray_fast_1b(size_t intArrayLength, unsigned char* byteArray, size_t byteArrayLength, unsigned char **intArray) |
---|
48 | { |
---|
49 | if(intArrayLength > byteArrayLength*8) |
---|
50 | { |
---|
51 | printf("Error: intArrayLength > byteArrayLength*8\n"); |
---|
52 | printf("intArrayLength=%zu, byteArrayLength = %zu", intArrayLength, byteArrayLength); |
---|
53 | exit(0); |
---|
54 | } |
---|
55 | if(intArrayLength>0) |
---|
56 | *intArray = (unsigned char*)malloc(intArrayLength*sizeof(unsigned char)); |
---|
57 | else |
---|
58 | *intArray = NULL; |
---|
59 | |
---|
60 | size_t n = 0, i; |
---|
61 | int tmp; |
---|
62 | for (i = 0; i < byteArrayLength-1; i++) |
---|
63 | { |
---|
64 | tmp = byteArray[i]; |
---|
65 | (*intArray)[n++] = (tmp & 0x80) >> 7; |
---|
66 | (*intArray)[n++] = (tmp & 0x40) >> 6; |
---|
67 | (*intArray)[n++] = (tmp & 0x20) >> 5; |
---|
68 | (*intArray)[n++] = (tmp & 0x10) >> 4; |
---|
69 | (*intArray)[n++] = (tmp & 0x08) >> 3; |
---|
70 | (*intArray)[n++] = (tmp & 0x04) >> 2; |
---|
71 | (*intArray)[n++] = (tmp & 0x02) >> 1; |
---|
72 | (*intArray)[n++] = (tmp & 0x01) >> 0; |
---|
73 | } |
---|
74 | |
---|
75 | tmp = byteArray[i]; |
---|
76 | if(n == intArrayLength) |
---|
77 | return; |
---|
78 | (*intArray)[n++] = (tmp & 0x80) >> 7; |
---|
79 | if(n == intArrayLength) |
---|
80 | return; |
---|
81 | (*intArray)[n++] = (tmp & 0x40) >> 6; |
---|
82 | if(n == intArrayLength) |
---|
83 | return; |
---|
84 | (*intArray)[n++] = (tmp & 0x20) >> 5; |
---|
85 | if(n == intArrayLength) |
---|
86 | return; |
---|
87 | (*intArray)[n++] = (tmp & 0x10) >> 4; |
---|
88 | if(n == intArrayLength) |
---|
89 | return; |
---|
90 | (*intArray)[n++] = (tmp & 0x08) >> 3; |
---|
91 | if(n == intArrayLength) |
---|
92 | return; |
---|
93 | (*intArray)[n++] = (tmp & 0x04) >> 2; |
---|
94 | if(n == intArrayLength) |
---|
95 | return; |
---|
96 | (*intArray)[n++] = (tmp & 0x02) >> 1; |
---|
97 | if(n == intArrayLength) |
---|
98 | return; |
---|
99 | (*intArray)[n++] = (tmp & 0x01) >> 0; |
---|
100 | } |
---|
101 | |
---|
102 | /** |
---|
103 | * little endian |
---|
104 | * [01|10|11|00|....]-->[01|10|11|00][....] |
---|
105 | * @param timeStepType |
---|
106 | * @return |
---|
107 | */ |
---|
108 | size_t convertIntArray2ByteArray_fast_2b(unsigned char* timeStepType, size_t timeStepTypeLength, unsigned char **result) |
---|
109 | { |
---|
110 | size_t i, j, byteLength = 0; |
---|
111 | if(timeStepTypeLength%4==0) |
---|
112 | byteLength = timeStepTypeLength*2/8; |
---|
113 | else |
---|
114 | byteLength = timeStepTypeLength*2/8+1; |
---|
115 | if(byteLength>0) |
---|
116 | *result = (unsigned char*)malloc(byteLength*sizeof(unsigned char)); |
---|
117 | else |
---|
118 | *result = NULL; |
---|
119 | size_t n = 0; |
---|
120 | for(i = 0;i<byteLength;i++) |
---|
121 | { |
---|
122 | int tmp = 0; |
---|
123 | for(j = 0;j<4&&n<timeStepTypeLength;j++) |
---|
124 | { |
---|
125 | int type = timeStepType[n]; |
---|
126 | switch(type) |
---|
127 | { |
---|
128 | case 0: |
---|
129 | |
---|
130 | break; |
---|
131 | case 1: |
---|
132 | tmp = (tmp | (1 << (6-j*2))); |
---|
133 | break; |
---|
134 | case 2: |
---|
135 | tmp = (tmp | (2 << (6-j*2))); |
---|
136 | break; |
---|
137 | case 3: |
---|
138 | tmp = (tmp | (3 << (6-j*2))); |
---|
139 | break; |
---|
140 | default: |
---|
141 | printf("Error: wrong timestep type...: type[%zu]=%d\n", n, type); |
---|
142 | exit(0); |
---|
143 | } |
---|
144 | n++; |
---|
145 | } |
---|
146 | (*result)[i] = (unsigned char)tmp; |
---|
147 | } |
---|
148 | return byteLength; |
---|
149 | } |
---|
150 | |
---|
151 | void convertByteArray2IntArray_fast_2b(size_t stepLength, unsigned char* byteArray, size_t byteArrayLength, unsigned char **intArray) |
---|
152 | { |
---|
153 | if(stepLength > byteArrayLength*4) |
---|
154 | { |
---|
155 | printf("Error: stepLength > byteArray.length*4\n"); |
---|
156 | printf("stepLength=%zu, byteArray.length=%zu\n", stepLength, byteArrayLength); |
---|
157 | exit(0); |
---|
158 | } |
---|
159 | if(stepLength>0) |
---|
160 | *intArray = (unsigned char*)malloc(stepLength*sizeof(unsigned char)); |
---|
161 | else |
---|
162 | *intArray = NULL; |
---|
163 | size_t i, n = 0; |
---|
164 | |
---|
165 | for (i = 0; i < byteArrayLength; i++) { |
---|
166 | unsigned char tmp = byteArray[i]; |
---|
167 | (*intArray)[n++] = (tmp & 0xC0) >> 6; |
---|
168 | if(n==stepLength) |
---|
169 | break; |
---|
170 | (*intArray)[n++] = (tmp & 0x30) >> 4; |
---|
171 | if(n==stepLength) |
---|
172 | break; |
---|
173 | (*intArray)[n++] = (tmp & 0x0C) >> 2; |
---|
174 | if(n==stepLength) |
---|
175 | break; |
---|
176 | (*intArray)[n++] = tmp & 0x03; |
---|
177 | if(n==stepLength) |
---|
178 | break; |
---|
179 | } |
---|
180 | } |
---|
181 | |
---|
182 | size_t convertIntArray2ByteArray_fast_3b(unsigned char* timeStepType, size_t timeStepTypeLength, unsigned char **result) |
---|
183 | { |
---|
184 | size_t i = 0, k = 0, byteLength = 0, n = 0; |
---|
185 | if(timeStepTypeLength%8==0) |
---|
186 | byteLength = timeStepTypeLength*3/8; |
---|
187 | else |
---|
188 | byteLength = timeStepTypeLength*3/8+1; |
---|
189 | |
---|
190 | if(byteLength>0) |
---|
191 | *result = (unsigned char*)malloc(byteLength*sizeof(unsigned char)); |
---|
192 | else |
---|
193 | *result = NULL; |
---|
194 | int tmp = 0; |
---|
195 | for(n = 0;n<timeStepTypeLength;n++) |
---|
196 | { |
---|
197 | k = n%8; |
---|
198 | switch(k) |
---|
199 | { |
---|
200 | case 0: |
---|
201 | tmp = tmp | (timeStepType[n] << 5); |
---|
202 | break; |
---|
203 | case 1: |
---|
204 | tmp = tmp | (timeStepType[n] << 2); |
---|
205 | break; |
---|
206 | case 2: |
---|
207 | tmp = tmp | (timeStepType[n] >> 1); |
---|
208 | (*result)[i++] = (unsigned char)tmp; |
---|
209 | tmp = 0 | (timeStepType[n] << 7); |
---|
210 | break; |
---|
211 | case 3: |
---|
212 | tmp = tmp | (timeStepType[n] << 4); |
---|
213 | break; |
---|
214 | case 4: |
---|
215 | tmp = tmp | (timeStepType[n] << 1); |
---|
216 | break; |
---|
217 | case 5: |
---|
218 | tmp = tmp | (timeStepType[n] >> 2); |
---|
219 | (*result)[i++] = (unsigned char)tmp; |
---|
220 | tmp = 0 | (timeStepType[n] << 6); |
---|
221 | break; |
---|
222 | case 6: |
---|
223 | tmp = tmp | (timeStepType[n] << 3); |
---|
224 | break; |
---|
225 | case 7: |
---|
226 | tmp = tmp | (timeStepType[n] << 0); |
---|
227 | (*result)[i++] = (unsigned char)tmp; |
---|
228 | tmp = 0; |
---|
229 | break; |
---|
230 | } |
---|
231 | } |
---|
232 | if(k!=7) //load the last one |
---|
233 | (*result)[i] = (unsigned char)tmp; |
---|
234 | |
---|
235 | return byteLength; |
---|
236 | } |
---|
237 | |
---|
238 | void convertByteArray2IntArray_fast_3b(size_t stepLength, unsigned char* byteArray, size_t byteArrayLength, unsigned char **intArray) |
---|
239 | { |
---|
240 | if(stepLength > byteArrayLength*8/3) |
---|
241 | { |
---|
242 | printf("Error: stepLength > byteArray.length*8/3, impossible case unless bugs elsewhere.\n"); |
---|
243 | printf("stepLength=%zu, byteArray.length=%zu\n", stepLength, byteArrayLength); |
---|
244 | exit(0); |
---|
245 | } |
---|
246 | if(stepLength>0) |
---|
247 | *intArray = (unsigned char*)malloc(stepLength*sizeof(unsigned char)); |
---|
248 | else |
---|
249 | *intArray = NULL; |
---|
250 | size_t i = 0, ii = 0, n = 0; |
---|
251 | unsigned char tmp = byteArray[i]; |
---|
252 | for(n=0;n<stepLength;) |
---|
253 | { |
---|
254 | switch(n%8) |
---|
255 | { |
---|
256 | case 0: |
---|
257 | (*intArray)[n++] = (tmp & 0xE0) >> 5; |
---|
258 | break; |
---|
259 | case 1: |
---|
260 | (*intArray)[n++] = (tmp & 0x1C) >> 2; |
---|
261 | break; |
---|
262 | case 2: |
---|
263 | ii = (tmp & 0x03) << 1; |
---|
264 | i++; |
---|
265 | tmp = byteArray[i]; |
---|
266 | ii |= (tmp & 0x80) >> 7; |
---|
267 | (*intArray)[n++] = ii; |
---|
268 | break; |
---|
269 | case 3: |
---|
270 | (*intArray)[n++] = (tmp & 0x70) >> 4; |
---|
271 | break; |
---|
272 | case 4: |
---|
273 | (*intArray)[n++] = (tmp & 0x0E) >> 1; |
---|
274 | break; |
---|
275 | case 5: |
---|
276 | ii = (tmp & 0x01) << 2; |
---|
277 | i++; |
---|
278 | tmp = byteArray[i]; |
---|
279 | ii |= (tmp & 0xC0) >> 6; |
---|
280 | (*intArray)[n++] = ii; |
---|
281 | break; |
---|
282 | case 6: |
---|
283 | (*intArray)[n++] = (tmp & 0x38) >> 3; |
---|
284 | break; |
---|
285 | case 7: |
---|
286 | (*intArray)[n++] = (tmp & 0x07); |
---|
287 | i++; |
---|
288 | tmp = byteArray[i]; |
---|
289 | break; |
---|
290 | } |
---|
291 | } |
---|
292 | } |
---|
293 | |
---|
294 | int getLeftMovingSteps(size_t k, unsigned char resiBitLength) |
---|
295 | { |
---|
296 | return 8 - k%8 - resiBitLength; |
---|
297 | } |
---|
298 | |
---|
299 | /** |
---|
300 | * |
---|
301 | * @param timeStepType is the resiMidBits |
---|
302 | * @param resiBitLength is the length of resiMidBits for each element, (the number of resiBitLength == the # of unpredictable elements |
---|
303 | * @return |
---|
304 | */ |
---|
305 | size_t convertIntArray2ByteArray_fast_dynamic(unsigned char* timeStepType, unsigned char resiBitLength, size_t nbEle, unsigned char **bytes) |
---|
306 | { |
---|
307 | size_t i = 0, j = 0, k = 0; |
---|
308 | int value; |
---|
309 | DynamicByteArray* dba; |
---|
310 | new_DBA(&dba, 1024); |
---|
311 | int tmp = 0, leftMovSteps = 0; |
---|
312 | for(j = 0;j<nbEle;j++) |
---|
313 | { |
---|
314 | if(resiBitLength==0) |
---|
315 | continue; |
---|
316 | value = timeStepType[i]; |
---|
317 | leftMovSteps = getLeftMovingSteps(k, resiBitLength); |
---|
318 | if(leftMovSteps < 0) |
---|
319 | { |
---|
320 | tmp = tmp | (value >> (-leftMovSteps)); |
---|
321 | addDBA_Data(dba, (unsigned char)tmp); |
---|
322 | tmp = 0 | (value << (8+leftMovSteps)); |
---|
323 | } |
---|
324 | else if(leftMovSteps > 0) |
---|
325 | { |
---|
326 | tmp = tmp | (value << leftMovSteps); |
---|
327 | } |
---|
328 | else //==0 |
---|
329 | { |
---|
330 | tmp = tmp | value; |
---|
331 | addDBA_Data(dba, (unsigned char)tmp); |
---|
332 | tmp = 0; |
---|
333 | } |
---|
334 | i++; |
---|
335 | k += resiBitLength; |
---|
336 | } |
---|
337 | if(leftMovSteps != 0) |
---|
338 | addDBA_Data(dba, (unsigned char)tmp); |
---|
339 | convertDBAtoBytes(dba, bytes); |
---|
340 | size_t size = dba->size; |
---|
341 | free_DBA(dba); |
---|
342 | return size; |
---|
343 | } |
---|
344 | |
---|
345 | /** |
---|
346 | * |
---|
347 | * @param timeStepType is the resiMidBits |
---|
348 | * @param resiBitLength is the length of resiMidBits for each element, (the number of resiBitLength == the # of unpredictable elements |
---|
349 | * @return |
---|
350 | */ |
---|
351 | size_t convertIntArray2ByteArray_fast_dynamic2(unsigned char* timeStepType, unsigned char* resiBitLength, size_t resiBitLengthLength, unsigned char **bytes) |
---|
352 | { |
---|
353 | size_t i = 0, j = 0, k = 0; |
---|
354 | int value; |
---|
355 | DynamicByteArray* dba; |
---|
356 | new_DBA(&dba, 1024); |
---|
357 | int tmp = 0, leftMovSteps = 0; |
---|
358 | for(j = 0;j<resiBitLengthLength;j++) |
---|
359 | { |
---|
360 | unsigned char rbl = resiBitLength[j]; |
---|
361 | if(rbl==0) |
---|
362 | continue; |
---|
363 | value = timeStepType[i]; |
---|
364 | leftMovSteps = getLeftMovingSteps(k, rbl); |
---|
365 | if(leftMovSteps < 0) |
---|
366 | { |
---|
367 | tmp = tmp | (value >> (-leftMovSteps)); |
---|
368 | addDBA_Data(dba, (unsigned char)tmp); |
---|
369 | tmp = 0 | (value << (8+leftMovSteps)); |
---|
370 | } |
---|
371 | else if(leftMovSteps > 0) |
---|
372 | { |
---|
373 | tmp = tmp | (value << leftMovSteps); |
---|
374 | } |
---|
375 | else //==0 |
---|
376 | { |
---|
377 | tmp = tmp | value; |
---|
378 | addDBA_Data(dba, (unsigned char)tmp); |
---|
379 | tmp = 0; |
---|
380 | } |
---|
381 | i++; |
---|
382 | k += rbl; |
---|
383 | } |
---|
384 | if(leftMovSteps != 0) |
---|
385 | addDBA_Data(dba, (unsigned char)tmp); |
---|
386 | convertDBAtoBytes(dba, bytes); |
---|
387 | size_t size = dba->size; |
---|
388 | free_DBA(dba); |
---|
389 | return size; |
---|
390 | } |
---|
391 | |
---|
392 | int computeBitNumRequired(size_t dataLength) |
---|
393 | { |
---|
394 | if(exe_params->SZ_SIZE_TYPE==4) |
---|
395 | return 32 - numberOfLeadingZeros_Int(dataLength); |
---|
396 | else |
---|
397 | return 64 - numberOfLeadingZeros_Long(dataLength); |
---|
398 | |
---|
399 | } |
---|
400 | |
---|
401 | void decompressBitArraybySimpleLZ77(int** result, unsigned char* bytes, size_t bytesLength, size_t totalLength, int validLength) |
---|
402 | { |
---|
403 | size_t pairLength = (bytesLength*8)/(validLength+1); |
---|
404 | size_t tmpLength = pairLength*2; |
---|
405 | int tmpResult[tmpLength]; |
---|
406 | size_t i, j, k = 0; |
---|
407 | for(i = 0;i<tmpLength;i+=2) |
---|
408 | { |
---|
409 | size_t outIndex = k/8; |
---|
410 | int innerIndex = k%8; |
---|
411 | |
---|
412 | unsigned char curByte = bytes[outIndex]; |
---|
413 | tmpResult[i] = (curByte >> (8-1-innerIndex)) & 0x01; |
---|
414 | k++; |
---|
415 | |
---|
416 | int numResult = extractBytes(bytes, k, validLength); |
---|
417 | |
---|
418 | tmpResult[i+1] = numResult; |
---|
419 | k = k + validLength; |
---|
420 | } |
---|
421 | |
---|
422 | *result = (int*)malloc(sizeof(int)*totalLength); |
---|
423 | k = 0; |
---|
424 | for(i = 0;i<tmpLength;i=i+2) |
---|
425 | { |
---|
426 | int state = tmpResult[i]; |
---|
427 | int num = tmpResult[i+1]; |
---|
428 | for(j = 0;j<num;j++) |
---|
429 | (*result)[k++] = state; |
---|
430 | } |
---|
431 | } |
---|