1 | /** |
---|
2 | * @file TightPointDataStorageF.c |
---|
3 | * @author Sheng Di and Dingwen Tao |
---|
4 | * @date Aug, 2016 |
---|
5 | * @brief The functions used to construct the tightPointDataStorage element for storing compressed bytes. |
---|
6 | * (C) 2016 by Mathematics and Computer Science (MCS), Argonne National Laboratory. |
---|
7 | * See COPYRIGHT in top-level directory. |
---|
8 | */ |
---|
9 | |
---|
10 | #include <stdlib.h> |
---|
11 | #include <stdio.h> |
---|
12 | #include <string.h> |
---|
13 | #include "TightDataPointStorageF.h" |
---|
14 | #include "sz.h" |
---|
15 | #include "Huffman.h" |
---|
16 | //#include "rw.h" |
---|
17 | |
---|
18 | void new_TightDataPointStorageF_Empty(TightDataPointStorageF **this) |
---|
19 | { |
---|
20 | *this = (TightDataPointStorageF*)malloc(sizeof(TightDataPointStorageF)); |
---|
21 | (*this)->dataSeriesLength = 0; |
---|
22 | (*this)->allSameData = 0; |
---|
23 | (*this)->exactDataNum = 0; |
---|
24 | (*this)->reservedValue = 0; |
---|
25 | (*this)->reqLength = 0; |
---|
26 | (*this)->radExpo = 0; |
---|
27 | |
---|
28 | (*this)->rtypeArray = NULL; |
---|
29 | (*this)->rtypeArray_size = 0; |
---|
30 | |
---|
31 | (*this)->typeArray = NULL; //its size is dataSeriesLength/4 (or xxx/4+1) |
---|
32 | (*this)->typeArray_size = 0; |
---|
33 | |
---|
34 | (*this)->leadNumArray = NULL; //its size is exactDataNum/4 (or exactDataNum/4+1) |
---|
35 | (*this)->leadNumArray_size = 0; |
---|
36 | |
---|
37 | (*this)->exactMidBytes = NULL; |
---|
38 | (*this)->exactMidBytes_size = 0; |
---|
39 | |
---|
40 | (*this)->residualMidBits = NULL; |
---|
41 | (*this)->residualMidBits_size = 0; |
---|
42 | |
---|
43 | (*this)->intervals = 0; |
---|
44 | (*this)->isLossless = 0; |
---|
45 | |
---|
46 | (*this)->segment_size = 0; |
---|
47 | (*this)->pwrErrBoundBytes = NULL; |
---|
48 | (*this)->pwrErrBoundBytes_size = 0; |
---|
49 | } |
---|
50 | |
---|
51 | int new_TightDataPointStorageF_fromFlatBytes(TightDataPointStorageF **this, unsigned char* flatBytes, size_t flatBytesLength) |
---|
52 | { |
---|
53 | new_TightDataPointStorageF_Empty(this); |
---|
54 | size_t i, index = 0; |
---|
55 | size_t pwrErrBoundBytes_size = 0, segmentL = 0, radExpoL = 0, pwrErrBoundBytesL = 0; |
---|
56 | char version[3]; |
---|
57 | for (i = 0; i < 3; i++) |
---|
58 | version[i] = flatBytes[index++]; //3 |
---|
59 | unsigned char sameRByte = flatBytes[index++]; //1 |
---|
60 | if(checkVersion(version)!=1) |
---|
61 | { |
---|
62 | //wrong version |
---|
63 | printf("Wrong version: \nCompressed-data version (%d.%d.%d)\n",version[0], version[1], version[2]); |
---|
64 | printf("Current sz version: (%d.%d.%d)\n", versionNumber[0], versionNumber[1], versionNumber[2]); |
---|
65 | printf("Please double-check if the compressed data (or file) is correct.\n"); |
---|
66 | exit(0); |
---|
67 | } |
---|
68 | int same = sameRByte & 0x01; |
---|
69 | //confparams_dec->szMode = (sameRByte & 0x06)>>1; |
---|
70 | (*this)->isLossless = (sameRByte & 0x10)>>4; |
---|
71 | int isPW_REL = (sameRByte & 0x20)>>5; |
---|
72 | exe_params->SZ_SIZE_TYPE = ((sameRByte & 0x40)>>6)==1?8:4; |
---|
73 | int errorBoundMode = ABS; |
---|
74 | if(isPW_REL) |
---|
75 | { |
---|
76 | errorBoundMode = PW_REL; |
---|
77 | segmentL = exe_params->SZ_SIZE_TYPE; |
---|
78 | pwrErrBoundBytesL = 4; |
---|
79 | } |
---|
80 | |
---|
81 | sz_params* params = convertBytesToSZParams(&(flatBytes[index])); |
---|
82 | int mode = confparams_dec->szMode; |
---|
83 | int predictionMode = confparams_dec->predictionMode; |
---|
84 | if(confparams_dec!=NULL) |
---|
85 | free(confparams_dec); |
---|
86 | confparams_dec = params; |
---|
87 | confparams_dec->szMode = mode; |
---|
88 | if(mode==SZ_TEMPORAL_COMPRESSION) |
---|
89 | { |
---|
90 | confparams_dec->szMode = SZ_TEMPORAL_COMPRESSION; |
---|
91 | confparams_dec->predictionMode = predictionMode; |
---|
92 | } |
---|
93 | |
---|
94 | index += MetaDataByteLength; |
---|
95 | |
---|
96 | unsigned char dsLengthBytes[8]; |
---|
97 | for (i = 0; i < exe_params->SZ_SIZE_TYPE; i++) |
---|
98 | dsLengthBytes[i] = flatBytes[index++]; |
---|
99 | (*this)->dataSeriesLength = bytesToSize(dsLengthBytes);// 4 or 8 |
---|
100 | |
---|
101 | if((*this)->isLossless==1) |
---|
102 | { |
---|
103 | //(*this)->exactMidBytes = flatBytes+8; |
---|
104 | return errorBoundMode; |
---|
105 | } |
---|
106 | else if(same==1) |
---|
107 | { |
---|
108 | (*this)->allSameData = 1; |
---|
109 | size_t exactMidBytesLength = sizeof(float); //flatBytesLength - 3 - 1 - MetaDataByteLength - exe_params->SZ_SIZE_TYPE; |
---|
110 | if(exactMidBytesLength>0) |
---|
111 | (*this)->exactMidBytes = (unsigned char*)malloc(sizeof(unsigned char)*exactMidBytesLength); |
---|
112 | else |
---|
113 | (*this)->exactMidBytes = NULL; |
---|
114 | for(i = 0;i<exactMidBytesLength;i++) |
---|
115 | (*this)->exactMidBytes[i] = flatBytes[index++]; |
---|
116 | return errorBoundMode; |
---|
117 | } |
---|
118 | else |
---|
119 | (*this)->allSameData = 0; |
---|
120 | |
---|
121 | int rtype_ = sameRByte & 0x08; //=00001000 |
---|
122 | unsigned char byteBuf[8]; |
---|
123 | |
---|
124 | for (i = 0; i < 4; i++) |
---|
125 | byteBuf[i] = flatBytes[index++]; |
---|
126 | int max_quant_intervals = bytesToInt_bigEndian(byteBuf);// 4 |
---|
127 | |
---|
128 | confparams_dec->maxRangeRadius = max_quant_intervals/2; |
---|
129 | |
---|
130 | if(errorBoundMode>=PW_REL) |
---|
131 | { |
---|
132 | (*this)->radExpo = flatBytes[index++];//1 |
---|
133 | radExpoL = 1; |
---|
134 | for (i = 0; i < exe_params->SZ_SIZE_TYPE; i++) |
---|
135 | byteBuf[i] = flatBytes[index++]; |
---|
136 | params->segment_size = (*this)->segment_size = bytesToSize(byteBuf);// exe_params->SZ_SIZE_TYPE |
---|
137 | |
---|
138 | for (i = 0; i < 4; i++) |
---|
139 | byteBuf[i] = flatBytes[index++]; |
---|
140 | pwrErrBoundBytes_size = (*this)->pwrErrBoundBytes_size = bytesToInt_bigEndian(byteBuf);// 4 |
---|
141 | } |
---|
142 | else |
---|
143 | { |
---|
144 | pwrErrBoundBytes_size = 0; |
---|
145 | (*this)->pwrErrBoundBytes = NULL; |
---|
146 | } |
---|
147 | for (i = 0; i < 4; i++) |
---|
148 | byteBuf[i] = flatBytes[index++]; |
---|
149 | (*this)->intervals = bytesToInt_bigEndian(byteBuf);// 4 |
---|
150 | |
---|
151 | for (i = 0; i < 4; i++) |
---|
152 | byteBuf[i] = flatBytes[index++]; |
---|
153 | (*this)->medianValue = bytesToFloat(byteBuf); //4 |
---|
154 | |
---|
155 | (*this)->reqLength = flatBytes[index++]; //1 |
---|
156 | |
---|
157 | for (i = 0; i < 8; i++) |
---|
158 | byteBuf[i] = flatBytes[index++]; |
---|
159 | (*this)->realPrecision = bytesToDouble(byteBuf);//8 |
---|
160 | |
---|
161 | for (i = 0; i < exe_params->SZ_SIZE_TYPE; i++) |
---|
162 | byteBuf[i] = flatBytes[index++]; |
---|
163 | (*this)->typeArray_size = bytesToSize(byteBuf);// 4 |
---|
164 | if(rtype_!=0) |
---|
165 | { |
---|
166 | for(i = 0;i<exe_params->SZ_SIZE_TYPE;i++) |
---|
167 | byteBuf[i] = flatBytes[index++]; |
---|
168 | (*this)->rtypeArray_size = bytesToSize(byteBuf);//(ST) |
---|
169 | } |
---|
170 | else |
---|
171 | (*this)->rtypeArray_size = 0; |
---|
172 | |
---|
173 | for (i = 0; i < exe_params->SZ_SIZE_TYPE; i++) |
---|
174 | byteBuf[i] = flatBytes[index++]; |
---|
175 | (*this)->exactDataNum = bytesToSize(byteBuf);// ST |
---|
176 | |
---|
177 | for (i = 0; i < exe_params->SZ_SIZE_TYPE; i++) |
---|
178 | byteBuf[i] = flatBytes[index++]; |
---|
179 | (*this)->exactMidBytes_size = bytesToSize(byteBuf);// ST |
---|
180 | |
---|
181 | if (rtype_ != 0) { |
---|
182 | if((*this)->rtypeArray_size>0) |
---|
183 | (*this)->rtypeArray = (unsigned char*)malloc(sizeof(unsigned char)*(*this)->rtypeArray_size); |
---|
184 | else |
---|
185 | (*this)->rtypeArray = NULL; |
---|
186 | |
---|
187 | for (i = 0; i < 4; i++) |
---|
188 | byteBuf[i] = flatBytes[index++]; |
---|
189 | (*this)->reservedValue = bytesToFloat(byteBuf);//4 |
---|
190 | } |
---|
191 | |
---|
192 | size_t logicLeadNumBitsNum = (*this)->exactDataNum * 2; |
---|
193 | if (logicLeadNumBitsNum % 8 == 0) |
---|
194 | { |
---|
195 | (*this)->leadNumArray_size = logicLeadNumBitsNum >> 3; |
---|
196 | } |
---|
197 | else |
---|
198 | { |
---|
199 | (*this)->leadNumArray_size = (logicLeadNumBitsNum >> 3) + 1; |
---|
200 | } |
---|
201 | |
---|
202 | if ((*this)->rtypeArray != NULL) |
---|
203 | { |
---|
204 | (*this)->residualMidBits_size = flatBytesLength - 3 - 1 - MetaDataByteLength - exe_params->SZ_SIZE_TYPE - 4 - radExpoL - segmentL - pwrErrBoundBytesL - 4 - 4 - 1 - 8 |
---|
205 | - exe_params->SZ_SIZE_TYPE - exe_params->SZ_SIZE_TYPE - exe_params->SZ_SIZE_TYPE - exe_params->SZ_SIZE_TYPE - 4 - (*this)->rtypeArray_size |
---|
206 | - (*this)->typeArray_size - (*this)->leadNumArray_size |
---|
207 | - (*this)->exactMidBytes_size - pwrErrBoundBytes_size; |
---|
208 | for (i = 0; i < (*this)->rtypeArray_size; i++) |
---|
209 | (*this)->rtypeArray[i] = flatBytes[index++]; |
---|
210 | } |
---|
211 | else |
---|
212 | { |
---|
213 | (*this)->residualMidBits_size = flatBytesLength - 3 - 1 - MetaDataByteLength - exe_params->SZ_SIZE_TYPE - 4 - radExpoL - segmentL - pwrErrBoundBytesL - 4 - 4 - 1 - 8 |
---|
214 | - exe_params->SZ_SIZE_TYPE - exe_params->SZ_SIZE_TYPE - exe_params->SZ_SIZE_TYPE - (*this)->typeArray_size |
---|
215 | - (*this)->leadNumArray_size - (*this)->exactMidBytes_size - pwrErrBoundBytes_size; |
---|
216 | } |
---|
217 | |
---|
218 | (*this)->typeArray = &flatBytes[index]; |
---|
219 | //retrieve the number of states (i.e., stateNum) |
---|
220 | (*this)->allNodes = bytesToInt_bigEndian((*this)->typeArray); //the first 4 bytes store the stateNum |
---|
221 | (*this)->stateNum = ((*this)->allNodes+1)/2; |
---|
222 | |
---|
223 | index+=(*this)->typeArray_size; |
---|
224 | |
---|
225 | (*this)->pwrErrBoundBytes = &flatBytes[index]; |
---|
226 | |
---|
227 | index+=pwrErrBoundBytes_size; |
---|
228 | |
---|
229 | (*this)->leadNumArray = &flatBytes[index]; |
---|
230 | |
---|
231 | index+=(*this)->leadNumArray_size; |
---|
232 | |
---|
233 | (*this)->exactMidBytes = &flatBytes[index]; |
---|
234 | |
---|
235 | index+=(*this)->exactMidBytes_size; |
---|
236 | |
---|
237 | (*this)->residualMidBits = &flatBytes[index]; |
---|
238 | |
---|
239 | //index+=(*this)->residualMidBits_size; |
---|
240 | |
---|
241 | return errorBoundMode; |
---|
242 | } |
---|
243 | |
---|
244 | /** |
---|
245 | * |
---|
246 | * type's length == dataSeriesLength |
---|
247 | * exactMidBytes's length == exactMidBytes_size |
---|
248 | * leadNumIntArray's length == exactDataNum |
---|
249 | * escBytes's length == escBytes_size |
---|
250 | * resiBitLength's length == resiBitLengthSize |
---|
251 | * */ |
---|
252 | void new_TightDataPointStorageF(TightDataPointStorageF **this, |
---|
253 | size_t dataSeriesLength, size_t exactDataNum, |
---|
254 | int* type, unsigned char* exactMidBytes, size_t exactMidBytes_size, |
---|
255 | unsigned char* leadNumIntArray, //leadNumIntArray contains readable numbers.... |
---|
256 | unsigned char* resiMidBits, size_t resiMidBits_size, |
---|
257 | unsigned char resiBitLength, |
---|
258 | double realPrecision, float medianValue, char reqLength, unsigned int intervals, |
---|
259 | unsigned char* pwrErrBoundBytes, size_t pwrErrBoundBytes_size, unsigned char radExpo) { |
---|
260 | |
---|
261 | *this = (TightDataPointStorageF *)malloc(sizeof(TightDataPointStorageF)); |
---|
262 | (*this)->allSameData = 0; |
---|
263 | (*this)->realPrecision = realPrecision; |
---|
264 | (*this)->medianValue = medianValue; |
---|
265 | (*this)->reqLength = reqLength; |
---|
266 | |
---|
267 | (*this)->dataSeriesLength = dataSeriesLength; |
---|
268 | (*this)->exactDataNum = exactDataNum; |
---|
269 | |
---|
270 | (*this)->rtypeArray = NULL; |
---|
271 | (*this)->rtypeArray_size = 0; |
---|
272 | |
---|
273 | int stateNum = 2*intervals; |
---|
274 | HuffmanTree* huffmanTree = createHuffmanTree(stateNum); |
---|
275 | encode_withTree(huffmanTree, type, dataSeriesLength, &(*this)->typeArray, &(*this)->typeArray_size); |
---|
276 | SZ_ReleaseHuffman(huffmanTree); |
---|
277 | |
---|
278 | (*this)->exactMidBytes = exactMidBytes; |
---|
279 | (*this)->exactMidBytes_size = exactMidBytes_size; |
---|
280 | |
---|
281 | (*this)->leadNumArray_size = convertIntArray2ByteArray_fast_2b(leadNumIntArray, exactDataNum, &((*this)->leadNumArray)); |
---|
282 | |
---|
283 | (*this)->residualMidBits_size = convertIntArray2ByteArray_fast_dynamic(resiMidBits, resiBitLength, exactDataNum, &((*this)->residualMidBits)); |
---|
284 | |
---|
285 | (*this)->intervals = intervals; |
---|
286 | |
---|
287 | (*this)->isLossless = 0; |
---|
288 | |
---|
289 | if(confparams_cpr->errorBoundMode>=PW_REL) |
---|
290 | (*this)->pwrErrBoundBytes = pwrErrBoundBytes; |
---|
291 | else |
---|
292 | (*this)->pwrErrBoundBytes = NULL; |
---|
293 | |
---|
294 | (*this)->radExpo = radExpo; |
---|
295 | |
---|
296 | (*this)->pwrErrBoundBytes_size = pwrErrBoundBytes_size; |
---|
297 | } |
---|
298 | |
---|
299 | void new_TightDataPointStorageF2(TightDataPointStorageF **this, |
---|
300 | size_t dataSeriesLength, size_t exactDataNum, |
---|
301 | int* type, unsigned char* exactMidBytes, size_t exactMidBytes_size, |
---|
302 | unsigned char* leadNumIntArray, //leadNumIntArray contains readable numbers.... |
---|
303 | unsigned char* resiMidBits, size_t resiMidBits_size, |
---|
304 | unsigned char* resiBitLength, size_t resiBitLengthSize, |
---|
305 | double realPrecision, float medianValue, char reqLength, unsigned int intervals, |
---|
306 | unsigned char* pwrErrBoundBytes, size_t pwrErrBoundBytes_size, unsigned char radExpo) { |
---|
307 | //int i = 0; |
---|
308 | *this = (TightDataPointStorageF *)malloc(sizeof(TightDataPointStorageF)); |
---|
309 | (*this)->allSameData = 0; |
---|
310 | (*this)->realPrecision = realPrecision; |
---|
311 | (*this)->medianValue = medianValue; |
---|
312 | (*this)->reqLength = reqLength; |
---|
313 | |
---|
314 | (*this)->dataSeriesLength = dataSeriesLength; |
---|
315 | (*this)->exactDataNum = exactDataNum; |
---|
316 | |
---|
317 | (*this)->rtypeArray = NULL; |
---|
318 | (*this)->rtypeArray_size = 0; |
---|
319 | |
---|
320 | int stateNum = 2*intervals; |
---|
321 | HuffmanTree* huffmanTree = createHuffmanTree(stateNum); |
---|
322 | encode_withTree(huffmanTree, type, dataSeriesLength, &(*this)->typeArray, &(*this)->typeArray_size); |
---|
323 | SZ_ReleaseHuffman(huffmanTree); |
---|
324 | |
---|
325 | (*this)->exactMidBytes = exactMidBytes; |
---|
326 | (*this)->exactMidBytes_size = exactMidBytes_size; |
---|
327 | |
---|
328 | (*this)->leadNumArray_size = convertIntArray2ByteArray_fast_2b(leadNumIntArray, exactDataNum, &((*this)->leadNumArray)); |
---|
329 | |
---|
330 | //(*this)->residualMidBits = resiMidBits; |
---|
331 | //(*this)->residualMidBits_size = resiMidBits_size; |
---|
332 | |
---|
333 | (*this)->residualMidBits_size = convertIntArray2ByteArray_fast_dynamic2(resiMidBits, resiBitLength, resiBitLengthSize, &((*this)->residualMidBits)); |
---|
334 | |
---|
335 | (*this)->intervals = intervals; |
---|
336 | |
---|
337 | (*this)->isLossless = 0; |
---|
338 | |
---|
339 | if(confparams_cpr->errorBoundMode>=PW_REL) |
---|
340 | (*this)->pwrErrBoundBytes = pwrErrBoundBytes; |
---|
341 | else |
---|
342 | (*this)->pwrErrBoundBytes = NULL; |
---|
343 | |
---|
344 | (*this)->radExpo = radExpo; |
---|
345 | |
---|
346 | (*this)->pwrErrBoundBytes_size = pwrErrBoundBytes_size; |
---|
347 | } |
---|
348 | |
---|
349 | void convertTDPStoBytes_float(TightDataPointStorageF* tdps, unsigned char* bytes, unsigned char* dsLengthBytes, unsigned char sameByte) |
---|
350 | { |
---|
351 | size_t i, k = 0; |
---|
352 | unsigned char intervalsBytes[4]; |
---|
353 | unsigned char typeArrayLengthBytes[8]; |
---|
354 | unsigned char exactLengthBytes[8]; |
---|
355 | unsigned char exactMidBytesLength[8]; |
---|
356 | unsigned char realPrecisionBytes[8]; |
---|
357 | |
---|
358 | unsigned char medianValueBytes[4]; |
---|
359 | |
---|
360 | unsigned char segment_sizeBytes[8]; |
---|
361 | unsigned char pwrErrBoundBytes_sizeBytes[4]; |
---|
362 | unsigned char max_quant_intervals_Bytes[4]; |
---|
363 | |
---|
364 | |
---|
365 | for(i = 0;i<3;i++)//3 bytes |
---|
366 | bytes[k++] = versionNumber[i]; |
---|
367 | bytes[k++] = sameByte; //1 byte |
---|
368 | |
---|
369 | convertSZParamsToBytes(confparams_cpr, &(bytes[k])); |
---|
370 | k = k + MetaDataByteLength; |
---|
371 | |
---|
372 | for(i = 0;i<exe_params->SZ_SIZE_TYPE;i++)//ST: 4 or 8 bytes |
---|
373 | bytes[k++] = dsLengthBytes[i]; |
---|
374 | intToBytes_bigEndian(max_quant_intervals_Bytes, confparams_cpr->max_quant_intervals); |
---|
375 | for(i = 0;i<4;i++)//4 |
---|
376 | bytes[k++] = max_quant_intervals_Bytes[i]; |
---|
377 | |
---|
378 | if(confparams_cpr->errorBoundMode>=PW_REL) |
---|
379 | { |
---|
380 | bytes[k++] = tdps->radExpo; //1 byte |
---|
381 | |
---|
382 | sizeToBytes(segment_sizeBytes, confparams_cpr->segment_size); |
---|
383 | for(i = 0;i<exe_params->SZ_SIZE_TYPE;i++)//ST |
---|
384 | bytes[k++] = segment_sizeBytes[i]; |
---|
385 | |
---|
386 | intToBytes_bigEndian(pwrErrBoundBytes_sizeBytes, tdps->pwrErrBoundBytes_size); |
---|
387 | for(i = 0;i<4;i++)//4 |
---|
388 | bytes[k++] = pwrErrBoundBytes_sizeBytes[i]; |
---|
389 | } |
---|
390 | |
---|
391 | intToBytes_bigEndian(intervalsBytes, tdps->intervals); |
---|
392 | for(i = 0;i<4;i++)//4 |
---|
393 | bytes[k++] = intervalsBytes[i]; |
---|
394 | |
---|
395 | floatToBytes(medianValueBytes, tdps->medianValue); |
---|
396 | for (i = 0; i < 4; i++)// 4 |
---|
397 | bytes[k++] = medianValueBytes[i]; |
---|
398 | |
---|
399 | bytes[k++] = tdps->reqLength; //1 byte |
---|
400 | |
---|
401 | /* if(errorBoundMode>=PW_REL) |
---|
402 | doubleToBytes(realPrecisionBytes, pw_relBoundRatio); |
---|
403 | else*/ |
---|
404 | doubleToBytes(realPrecisionBytes, tdps->realPrecision); |
---|
405 | |
---|
406 | for (i = 0; i < 8; i++)// 8 |
---|
407 | bytes[k++] = realPrecisionBytes[i]; |
---|
408 | |
---|
409 | sizeToBytes(typeArrayLengthBytes, tdps->typeArray_size); |
---|
410 | for(i = 0;i<exe_params->SZ_SIZE_TYPE;i++)//ST |
---|
411 | bytes[k++] = typeArrayLengthBytes[i]; |
---|
412 | |
---|
413 | sizeToBytes(exactLengthBytes, tdps->exactDataNum); |
---|
414 | for(i = 0;i<exe_params->SZ_SIZE_TYPE;i++)//ST |
---|
415 | bytes[k++] = exactLengthBytes[i]; |
---|
416 | |
---|
417 | sizeToBytes(exactMidBytesLength, tdps->exactMidBytes_size); |
---|
418 | for(i = 0;i<exe_params->SZ_SIZE_TYPE;i++)//ST |
---|
419 | bytes[k++] = exactMidBytesLength[i]; |
---|
420 | |
---|
421 | memcpy(&(bytes[k]), tdps->typeArray, tdps->typeArray_size); |
---|
422 | k += tdps->typeArray_size; |
---|
423 | if(confparams_cpr->errorBoundMode>=PW_REL) |
---|
424 | { |
---|
425 | memcpy(&(bytes[k]), tdps->pwrErrBoundBytes, tdps->pwrErrBoundBytes_size); |
---|
426 | k += tdps->pwrErrBoundBytes_size; |
---|
427 | } |
---|
428 | |
---|
429 | memcpy(&(bytes[k]), tdps->leadNumArray, tdps->leadNumArray_size); |
---|
430 | k += tdps->leadNumArray_size; |
---|
431 | memcpy(&(bytes[k]), tdps->exactMidBytes, tdps->exactMidBytes_size); |
---|
432 | k += tdps->exactMidBytes_size; |
---|
433 | |
---|
434 | if(tdps->residualMidBits!=NULL) |
---|
435 | { |
---|
436 | memcpy(&(bytes[k]), tdps->residualMidBits, tdps->residualMidBits_size); |
---|
437 | k += tdps->residualMidBits_size; |
---|
438 | } |
---|
439 | } |
---|
440 | |
---|
441 | void convertTDPStoBytes_float_reserve(TightDataPointStorageF* tdps, unsigned char* bytes, unsigned char* dsLengthBytes, unsigned char sameByte) |
---|
442 | { |
---|
443 | size_t i, k = 0; |
---|
444 | unsigned char intervalsBytes[4]; |
---|
445 | unsigned char typeArrayLengthBytes[8]; |
---|
446 | unsigned char rTypeLengthBytes[8]; |
---|
447 | unsigned char exactLengthBytes[8]; |
---|
448 | unsigned char exactMidBytesLength[8]; |
---|
449 | unsigned char realPrecisionBytes[8]; |
---|
450 | unsigned char reservedValueBytes[4]; |
---|
451 | |
---|
452 | unsigned char medianValueBytes[4]; |
---|
453 | |
---|
454 | unsigned char segment_sizeBytes[8]; |
---|
455 | unsigned char pwrErrBoundBytes_sizeBytes[4]; |
---|
456 | unsigned char max_quant_intervals_Bytes[4]; |
---|
457 | |
---|
458 | for(i = 0;i<3;i++)//3 |
---|
459 | bytes[k++] = versionNumber[i]; |
---|
460 | bytes[k++] = sameByte; //1 |
---|
461 | |
---|
462 | convertSZParamsToBytes(confparams_cpr, &(bytes[k])); |
---|
463 | k = k + MetaDataByteLength; |
---|
464 | |
---|
465 | for(i = 0;i<exe_params->SZ_SIZE_TYPE;i++)//ST |
---|
466 | bytes[k++] = dsLengthBytes[i]; |
---|
467 | |
---|
468 | |
---|
469 | intToBytes_bigEndian(max_quant_intervals_Bytes, confparams_cpr->max_quant_intervals); |
---|
470 | for(i = 0;i<4;i++)//4 |
---|
471 | bytes[k++] = max_quant_intervals_Bytes[i]; |
---|
472 | |
---|
473 | if(confparams_cpr->errorBoundMode>=PW_REL) |
---|
474 | { |
---|
475 | bytes[k++] = tdps->radExpo; //1 byte |
---|
476 | |
---|
477 | sizeToBytes(segment_sizeBytes, confparams_cpr->segment_size); |
---|
478 | for(i = 0;i<exe_params->SZ_SIZE_TYPE;i++)//ST |
---|
479 | bytes[k++] = segment_sizeBytes[i]; |
---|
480 | |
---|
481 | intToBytes_bigEndian(pwrErrBoundBytes_sizeBytes, tdps->pwrErrBoundBytes_size); |
---|
482 | for(i = 0;i<4;i++)//4 |
---|
483 | bytes[k++] = pwrErrBoundBytes_sizeBytes[i]; |
---|
484 | } |
---|
485 | |
---|
486 | intToBytes_bigEndian(intervalsBytes, tdps->intervals); |
---|
487 | for(i = 0;i<4;i++)//4 |
---|
488 | bytes[k++] = intervalsBytes[i]; |
---|
489 | |
---|
490 | floatToBytes(medianValueBytes, tdps->medianValue); |
---|
491 | for (i = 0; i < 4; i++)// 4 |
---|
492 | bytes[k++] = medianValueBytes[i]; |
---|
493 | |
---|
494 | bytes[k++] = tdps->reqLength; //1 byte |
---|
495 | |
---|
496 | floatToBytes(realPrecisionBytes, tdps->realPrecision); |
---|
497 | for (i = 0; i < 8; i++)// 8 |
---|
498 | bytes[k++] = realPrecisionBytes[i]; |
---|
499 | |
---|
500 | sizeToBytes(typeArrayLengthBytes, tdps->typeArray_size); |
---|
501 | for(i = 0;i<exe_params->SZ_SIZE_TYPE;i++)//ST |
---|
502 | bytes[k++] = typeArrayLengthBytes[i]; |
---|
503 | |
---|
504 | sizeToBytes(rTypeLengthBytes, tdps->rtypeArray_size); |
---|
505 | for(i = 0;i<exe_params->SZ_SIZE_TYPE;i++)//ST |
---|
506 | bytes[k++] = rTypeLengthBytes[i]; |
---|
507 | |
---|
508 | sizeToBytes(exactLengthBytes, tdps->exactDataNum); |
---|
509 | for(i = 0;i<exe_params->SZ_SIZE_TYPE;i++)//ST |
---|
510 | bytes[k++] = exactLengthBytes[i]; |
---|
511 | |
---|
512 | sizeToBytes(exactMidBytesLength, tdps->exactMidBytes_size); |
---|
513 | for(i = 0;i<exe_params->SZ_SIZE_TYPE;i++)//ST |
---|
514 | bytes[k++] = exactMidBytesLength[i]; |
---|
515 | |
---|
516 | floatToBytes(reservedValueBytes, tdps->reservedValue); |
---|
517 | for (i = 0; i < 4; i++)// 4 |
---|
518 | bytes[k++] = reservedValueBytes[i]; |
---|
519 | |
---|
520 | memcpy(&(bytes[k]), tdps->rtypeArray, tdps->rtypeArray_size); |
---|
521 | k += tdps->rtypeArray_size; |
---|
522 | memcpy(&(bytes[k]), tdps->typeArray, tdps->typeArray_size); |
---|
523 | k += tdps->typeArray_size; |
---|
524 | if(confparams_cpr->errorBoundMode>=PW_REL) |
---|
525 | { |
---|
526 | memcpy(&(bytes[k]), tdps->pwrErrBoundBytes, tdps->pwrErrBoundBytes_size); |
---|
527 | k += tdps->pwrErrBoundBytes_size; |
---|
528 | } |
---|
529 | memcpy(&(bytes[k]), tdps->leadNumArray, tdps->leadNumArray_size); |
---|
530 | k += tdps->leadNumArray_size; |
---|
531 | memcpy(&(bytes[k]), tdps->exactMidBytes, tdps->exactMidBytes_size); |
---|
532 | k += tdps->exactMidBytes_size; |
---|
533 | if(tdps->residualMidBits!=NULL) |
---|
534 | { |
---|
535 | memcpy(&(bytes[k]), tdps->residualMidBits, tdps->residualMidBits_size); |
---|
536 | k += tdps->residualMidBits_size; |
---|
537 | } |
---|
538 | } |
---|
539 | |
---|
540 | //convert TightDataPointStorageD to bytes... |
---|
541 | void convertTDPStoFlatBytes_float(TightDataPointStorageF *tdps, unsigned char** bytes, size_t *size) |
---|
542 | { |
---|
543 | size_t i, k = 0; |
---|
544 | unsigned char dsLengthBytes[8]; |
---|
545 | |
---|
546 | if(exe_params->SZ_SIZE_TYPE==4) |
---|
547 | intToBytes_bigEndian(dsLengthBytes, tdps->dataSeriesLength);//4 |
---|
548 | else |
---|
549 | longToBytes_bigEndian(dsLengthBytes, tdps->dataSeriesLength);//8 |
---|
550 | |
---|
551 | unsigned char sameByte = tdps->allSameData==1?(unsigned char)1:(unsigned char)0; |
---|
552 | sameByte = sameByte | (confparams_cpr->szMode << 1); |
---|
553 | if(tdps->isLossless) |
---|
554 | sameByte = (unsigned char) (sameByte | 0x10); |
---|
555 | if(confparams_cpr->errorBoundMode>=PW_REL) |
---|
556 | sameByte = (unsigned char) (sameByte | 0x20); // 00100000, the 5th bit |
---|
557 | if(exe_params->SZ_SIZE_TYPE==8) |
---|
558 | sameByte = (unsigned char) (sameByte | 0x40); // 01000000, the 6th bit |
---|
559 | |
---|
560 | if(tdps->allSameData==1) |
---|
561 | { |
---|
562 | size_t totalByteLength = 3 + 1 + MetaDataByteLength + exe_params->SZ_SIZE_TYPE + tdps->exactMidBytes_size; |
---|
563 | *bytes = (unsigned char *)malloc(sizeof(unsigned char)*totalByteLength); |
---|
564 | |
---|
565 | for (i = 0; i < 3; i++)//3 |
---|
566 | (*bytes)[k++] = versionNumber[i]; |
---|
567 | (*bytes)[k++] = sameByte; |
---|
568 | |
---|
569 | convertSZParamsToBytes(confparams_cpr, &((*bytes)[k])); |
---|
570 | k = k + MetaDataByteLength; |
---|
571 | |
---|
572 | for (i = 0; i < exe_params->SZ_SIZE_TYPE; i++) |
---|
573 | (*bytes)[k++] = dsLengthBytes[i]; |
---|
574 | |
---|
575 | for (i = 0; i < tdps->exactMidBytes_size; i++) |
---|
576 | (*bytes)[k++] = tdps->exactMidBytes[i]; |
---|
577 | |
---|
578 | *size = totalByteLength; |
---|
579 | } |
---|
580 | else if (tdps->rtypeArray == NULL) |
---|
581 | { |
---|
582 | size_t residualMidBitsLength = tdps->residualMidBits == NULL ? 0 : tdps->residualMidBits_size; |
---|
583 | size_t segmentL = 0, radExpoL = 0, pwrBoundArrayL = 0; |
---|
584 | if(confparams_cpr->errorBoundMode>=PW_REL) |
---|
585 | { |
---|
586 | segmentL = exe_params->SZ_SIZE_TYPE; |
---|
587 | radExpoL = 1; |
---|
588 | pwrBoundArrayL = 4; |
---|
589 | } |
---|
590 | |
---|
591 | size_t totalByteLength = 3 + 1 + MetaDataByteLength + exe_params->SZ_SIZE_TYPE + 4 + radExpoL + segmentL + pwrBoundArrayL + 4 + 4 + 1 + 8 |
---|
592 | + exe_params->SZ_SIZE_TYPE + exe_params->SZ_SIZE_TYPE + exe_params->SZ_SIZE_TYPE |
---|
593 | + tdps->typeArray_size + tdps->leadNumArray_size |
---|
594 | + tdps->exactMidBytes_size + residualMidBitsLength + tdps->pwrErrBoundBytes_size; |
---|
595 | |
---|
596 | *bytes = (unsigned char *)malloc(sizeof(unsigned char)*totalByteLength); |
---|
597 | |
---|
598 | convertTDPStoBytes_float(tdps, *bytes, dsLengthBytes, sameByte); |
---|
599 | |
---|
600 | *size = totalByteLength; |
---|
601 | } |
---|
602 | else //the case with reserved value |
---|
603 | { |
---|
604 | size_t residualMidBitsLength = tdps->residualMidBits == NULL ? 0 : tdps->residualMidBits_size; |
---|
605 | size_t segmentL = 0, radExpoL = 0, pwrBoundArrayL = 0; |
---|
606 | if(confparams_cpr->errorBoundMode>=PW_REL) |
---|
607 | { |
---|
608 | segmentL = exe_params->SZ_SIZE_TYPE; |
---|
609 | radExpoL = 1; |
---|
610 | pwrBoundArrayL = 4; |
---|
611 | } |
---|
612 | |
---|
613 | size_t totalByteLength = 3 + 1 + MetaDataByteLength + exe_params->SZ_SIZE_TYPE + 4 + radExpoL + segmentL + pwrBoundArrayL + 4 + 4 + 1 + 8 |
---|
614 | + exe_params->SZ_SIZE_TYPE + exe_params->SZ_SIZE_TYPE + exe_params->SZ_SIZE_TYPE + exe_params->SZ_SIZE_TYPE + 4 + tdps->rtypeArray_size |
---|
615 | + tdps->typeArray_size + tdps->leadNumArray_size |
---|
616 | + tdps->exactMidBytes_size + residualMidBitsLength + tdps->pwrErrBoundBytes_size; |
---|
617 | |
---|
618 | sameByte = (unsigned char) (sameByte | 0x08); // 00001000, the 4th bit |
---|
619 | // denotes whether it is |
---|
620 | // with "reserved value" |
---|
621 | |
---|
622 | if(confparams_cpr->errorBoundMode>=PW_REL) |
---|
623 | sameByte = (unsigned char) (sameByte | 0x10); // 00001000, the 5th bit |
---|
624 | |
---|
625 | *bytes = (unsigned char*)malloc(sizeof(unsigned char)*totalByteLength); |
---|
626 | |
---|
627 | convertTDPStoBytes_float_reserve(tdps, *bytes, dsLengthBytes, sameByte); |
---|
628 | |
---|
629 | *size = totalByteLength; |
---|
630 | } |
---|
631 | } |
---|
632 | |
---|
633 | void convertTDPStoFlatBytes_float_args(TightDataPointStorageF *tdps, unsigned char* bytes, size_t *size) |
---|
634 | { |
---|
635 | size_t i, k = 0; |
---|
636 | unsigned char dsLengthBytes[8]; |
---|
637 | |
---|
638 | if(exe_params->SZ_SIZE_TYPE==4) |
---|
639 | intToBytes_bigEndian(dsLengthBytes, tdps->dataSeriesLength);//4 |
---|
640 | else |
---|
641 | longToBytes_bigEndian(dsLengthBytes, tdps->dataSeriesLength);//8 |
---|
642 | |
---|
643 | unsigned char sameByte = tdps->allSameData==1?(unsigned char)1:(unsigned char)0; |
---|
644 | sameByte = sameByte | (confparams_cpr->szMode << 1); |
---|
645 | if(tdps->isLossless) |
---|
646 | sameByte = (unsigned char) (sameByte | 0x10); |
---|
647 | if(confparams_cpr->errorBoundMode>=PW_REL) |
---|
648 | sameByte = (unsigned char) (sameByte | 0x20); // 00100000, the 5th bit |
---|
649 | if(exe_params->SZ_SIZE_TYPE==8) |
---|
650 | sameByte = (unsigned char) (sameByte | 0x40); // 01000000, the 6th bit |
---|
651 | |
---|
652 | if(tdps->allSameData==1) |
---|
653 | { |
---|
654 | size_t totalByteLength = 3 + 1 + MetaDataByteLength + exe_params->SZ_SIZE_TYPE + tdps->exactMidBytes_size; |
---|
655 | //*bytes = (unsigned char *)malloc(sizeof(unsigned char)*totalByteLength); |
---|
656 | |
---|
657 | for (i = 0; i < 3; i++)//3 |
---|
658 | bytes[k++] = versionNumber[i]; |
---|
659 | bytes[k++] = sameByte; |
---|
660 | |
---|
661 | convertSZParamsToBytes(confparams_cpr, &(bytes[k])); |
---|
662 | k = k + MetaDataByteLength; |
---|
663 | |
---|
664 | for (i = 0; i < exe_params->SZ_SIZE_TYPE; i++) |
---|
665 | bytes[k++] = dsLengthBytes[i]; |
---|
666 | for (i = 0; i < tdps->exactMidBytes_size; i++) |
---|
667 | bytes[k++] = tdps->exactMidBytes[i]; |
---|
668 | |
---|
669 | *size = totalByteLength; |
---|
670 | } |
---|
671 | else if (tdps->rtypeArray == NULL) |
---|
672 | { |
---|
673 | size_t residualMidBitsLength = tdps->residualMidBits == NULL ? 0 : tdps->residualMidBits_size; |
---|
674 | size_t segmentL = 0, radExpoL = 0, pwrBoundArrayL = 0; |
---|
675 | if(confparams_cpr->errorBoundMode>=PW_REL) |
---|
676 | { |
---|
677 | segmentL = exe_params->SZ_SIZE_TYPE; |
---|
678 | radExpoL = 1; |
---|
679 | pwrBoundArrayL = 4; |
---|
680 | } |
---|
681 | |
---|
682 | size_t totalByteLength = 3 + 1 + MetaDataByteLength + exe_params->SZ_SIZE_TYPE + 4 + radExpoL + segmentL + pwrBoundArrayL + 4 + 4 + 1 + 8 |
---|
683 | + exe_params->SZ_SIZE_TYPE + exe_params->SZ_SIZE_TYPE + exe_params->SZ_SIZE_TYPE |
---|
684 | + tdps->typeArray_size + tdps->leadNumArray_size |
---|
685 | + tdps->exactMidBytes_size + residualMidBitsLength + tdps->pwrErrBoundBytes_size; |
---|
686 | |
---|
687 | convertTDPStoBytes_float(tdps, bytes, dsLengthBytes, sameByte); |
---|
688 | |
---|
689 | *size = totalByteLength; |
---|
690 | } |
---|
691 | else //the case with reserved value |
---|
692 | { |
---|
693 | size_t residualMidBitsLength = tdps->residualMidBits == NULL ? 0 : tdps->residualMidBits_size; |
---|
694 | size_t segmentL = 0, radExpoL = 0, pwrBoundArrayL = 0; |
---|
695 | if(confparams_cpr->errorBoundMode>=PW_REL) |
---|
696 | { |
---|
697 | segmentL = exe_params->SZ_SIZE_TYPE; |
---|
698 | radExpoL = 1; |
---|
699 | pwrBoundArrayL = 4; |
---|
700 | } |
---|
701 | |
---|
702 | size_t totalByteLength = 3 + 1 + MetaDataByteLength + exe_params->SZ_SIZE_TYPE + 4 + radExpoL + segmentL + pwrBoundArrayL + 4 + 4 + 1 + 8 |
---|
703 | + exe_params->SZ_SIZE_TYPE + exe_params->SZ_SIZE_TYPE + exe_params->SZ_SIZE_TYPE + exe_params->SZ_SIZE_TYPE + 4 + tdps->rtypeArray_size |
---|
704 | + tdps->typeArray_size + tdps->leadNumArray_size |
---|
705 | + tdps->exactMidBytes_size + residualMidBitsLength + tdps->pwrErrBoundBytes_size; |
---|
706 | |
---|
707 | sameByte = (unsigned char) (sameByte | 0x08); // 00001000, the 4th bit |
---|
708 | // denotes whether it is |
---|
709 | // with "reserved value" |
---|
710 | |
---|
711 | if(confparams_cpr->errorBoundMode>=PW_REL) |
---|
712 | sameByte = (unsigned char) (sameByte | 0x10); // 00001000, the 5th bit |
---|
713 | |
---|
714 | convertTDPStoBytes_float_reserve(tdps, bytes, dsLengthBytes, sameByte); |
---|
715 | |
---|
716 | *size = totalByteLength; |
---|
717 | } |
---|
718 | } |
---|
719 | |
---|
720 | /** |
---|
721 | * to free the memory used in the compression |
---|
722 | * */ |
---|
723 | void free_TightDataPointStorageF(TightDataPointStorageF *tdps) |
---|
724 | { |
---|
725 | if(tdps->rtypeArray!=NULL) |
---|
726 | free(tdps->rtypeArray); |
---|
727 | if(tdps->typeArray!=NULL) |
---|
728 | free(tdps->typeArray); |
---|
729 | if(tdps->leadNumArray!=NULL) |
---|
730 | free(tdps->leadNumArray); |
---|
731 | if(tdps->exactMidBytes!=NULL) |
---|
732 | free(tdps->exactMidBytes); |
---|
733 | if(tdps->residualMidBits!=NULL) |
---|
734 | free(tdps->residualMidBits); |
---|
735 | if(tdps->pwrErrBoundBytes!=NULL) |
---|
736 | free(tdps->pwrErrBoundBytes); |
---|
737 | free(tdps); |
---|
738 | } |
---|
739 | |
---|
740 | /** |
---|
741 | * to free the memory used in the decompression |
---|
742 | * */ |
---|
743 | void free_TightDataPointStorageF2(TightDataPointStorageF *tdps) |
---|
744 | { |
---|
745 | free(tdps); |
---|
746 | } |
---|