[2c47b73] | 1 | /** |
---|
| 2 | * @file ByteToolkit.c |
---|
| 3 | * @author Sheng Di |
---|
| 4 | * @date April, 2016 |
---|
| 5 | * @brief Byte Toolkit |
---|
| 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 "sz.h" |
---|
| 12 | #include "zlib.h" |
---|
| 13 | |
---|
| 14 | inline unsigned short bytesToUInt16_bigEndian(unsigned char* bytes) |
---|
| 15 | { |
---|
| 16 | int temp = 0; |
---|
| 17 | unsigned short res = 0; |
---|
| 18 | |
---|
| 19 | temp = bytes[0] & 0xff; |
---|
| 20 | res |= temp; |
---|
| 21 | |
---|
| 22 | res <<= 8; |
---|
| 23 | temp = bytes[1] & 0xff; |
---|
| 24 | res |= temp; |
---|
| 25 | |
---|
| 26 | return res; |
---|
| 27 | } |
---|
| 28 | |
---|
| 29 | inline unsigned int bytesToUInt32_bigEndian(unsigned char* bytes) |
---|
| 30 | { |
---|
| 31 | unsigned int temp = 0; |
---|
| 32 | unsigned int res = 0; |
---|
| 33 | |
---|
| 34 | res <<= 8; |
---|
| 35 | temp = bytes[0] & 0xff; |
---|
| 36 | res |= temp; |
---|
| 37 | |
---|
| 38 | res <<= 8; |
---|
| 39 | temp = bytes[1] & 0xff; |
---|
| 40 | res |= temp; |
---|
| 41 | |
---|
| 42 | res <<= 8; |
---|
| 43 | temp = bytes[2] & 0xff; |
---|
| 44 | res |= temp; |
---|
| 45 | |
---|
| 46 | res <<= 8; |
---|
| 47 | temp = bytes[3] & 0xff; |
---|
| 48 | res |= temp; |
---|
| 49 | |
---|
| 50 | return res; |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | inline unsigned long bytesToUInt64_bigEndian(unsigned char* b) { |
---|
| 54 | unsigned long temp = 0; |
---|
| 55 | unsigned long res = 0; |
---|
| 56 | |
---|
| 57 | res <<= 8; |
---|
| 58 | temp = b[0] & 0xff; |
---|
| 59 | res |= temp; |
---|
| 60 | |
---|
| 61 | res <<= 8; |
---|
| 62 | temp = b[1] & 0xff; |
---|
| 63 | res |= temp; |
---|
| 64 | |
---|
| 65 | res <<= 8; |
---|
| 66 | temp = b[2] & 0xff; |
---|
| 67 | res |= temp; |
---|
| 68 | |
---|
| 69 | res <<= 8; |
---|
| 70 | temp = b[3] & 0xff; |
---|
| 71 | res |= temp; |
---|
| 72 | |
---|
| 73 | res <<= 8; |
---|
| 74 | temp = b[4] & 0xff; |
---|
| 75 | res |= temp; |
---|
| 76 | |
---|
| 77 | res <<= 8; |
---|
| 78 | temp = b[5] & 0xff; |
---|
| 79 | res |= temp; |
---|
| 80 | |
---|
| 81 | res <<= 8; |
---|
| 82 | temp = b[6] & 0xff; |
---|
| 83 | res |= temp; |
---|
| 84 | |
---|
| 85 | res <<= 8; |
---|
| 86 | temp = b[7] & 0xff; |
---|
| 87 | res |= temp; |
---|
| 88 | |
---|
| 89 | return res; |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | inline short bytesToInt16_bigEndian(unsigned char* bytes) |
---|
| 93 | { |
---|
| 94 | int temp = 0; |
---|
| 95 | short res = 0; |
---|
| 96 | |
---|
| 97 | temp = bytes[0] & 0xff; |
---|
| 98 | res |= temp; |
---|
| 99 | |
---|
| 100 | res <<= 8; |
---|
| 101 | temp = bytes[1] & 0xff; |
---|
| 102 | res |= temp; |
---|
| 103 | |
---|
| 104 | return res; |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | inline int bytesToInt32_bigEndian(unsigned char* bytes) |
---|
| 108 | { |
---|
| 109 | int temp = 0; |
---|
| 110 | int res = 0; |
---|
| 111 | |
---|
| 112 | res <<= 8; |
---|
| 113 | temp = bytes[0] & 0xff; |
---|
| 114 | res |= temp; |
---|
| 115 | |
---|
| 116 | res <<= 8; |
---|
| 117 | temp = bytes[1] & 0xff; |
---|
| 118 | res |= temp; |
---|
| 119 | |
---|
| 120 | res <<= 8; |
---|
| 121 | temp = bytes[2] & 0xff; |
---|
| 122 | res |= temp; |
---|
| 123 | |
---|
| 124 | res <<= 8; |
---|
| 125 | temp = bytes[3] & 0xff; |
---|
| 126 | res |= temp; |
---|
| 127 | |
---|
| 128 | return res; |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | inline long bytesToInt64_bigEndian(unsigned char* b) { |
---|
| 132 | long temp = 0; |
---|
| 133 | long res = 0; |
---|
| 134 | |
---|
| 135 | res <<= 8; |
---|
| 136 | temp = b[0] & 0xff; |
---|
| 137 | res |= temp; |
---|
| 138 | |
---|
| 139 | res <<= 8; |
---|
| 140 | temp = b[1] & 0xff; |
---|
| 141 | res |= temp; |
---|
| 142 | |
---|
| 143 | res <<= 8; |
---|
| 144 | temp = b[2] & 0xff; |
---|
| 145 | res |= temp; |
---|
| 146 | |
---|
| 147 | res <<= 8; |
---|
| 148 | temp = b[3] & 0xff; |
---|
| 149 | res |= temp; |
---|
| 150 | |
---|
| 151 | res <<= 8; |
---|
| 152 | temp = b[4] & 0xff; |
---|
| 153 | res |= temp; |
---|
| 154 | |
---|
| 155 | res <<= 8; |
---|
| 156 | temp = b[5] & 0xff; |
---|
| 157 | res |= temp; |
---|
| 158 | |
---|
| 159 | res <<= 8; |
---|
| 160 | temp = b[6] & 0xff; |
---|
| 161 | res |= temp; |
---|
| 162 | |
---|
| 163 | res <<= 8; |
---|
| 164 | temp = b[7] & 0xff; |
---|
| 165 | res |= temp; |
---|
| 166 | |
---|
| 167 | return res; |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | inline int bytesToInt_bigEndian(unsigned char* bytes) |
---|
| 171 | { |
---|
| 172 | int temp = 0; |
---|
| 173 | int res = 0; |
---|
| 174 | |
---|
| 175 | res <<= 8; |
---|
| 176 | temp = bytes[0] & 0xff; |
---|
| 177 | res |= temp; |
---|
| 178 | |
---|
| 179 | res <<= 8; |
---|
| 180 | temp = bytes[1] & 0xff; |
---|
| 181 | res |= temp; |
---|
| 182 | |
---|
| 183 | res <<= 8; |
---|
| 184 | temp = bytes[2] & 0xff; |
---|
| 185 | res |= temp; |
---|
| 186 | |
---|
| 187 | res <<= 8; |
---|
| 188 | temp = bytes[3] & 0xff; |
---|
| 189 | res |= temp; |
---|
| 190 | |
---|
| 191 | return res; |
---|
| 192 | } |
---|
| 193 | |
---|
| 194 | /** |
---|
| 195 | * @unsigned char *b the variable to store the converted bytes (length=4) |
---|
| 196 | * @unsigned int num |
---|
| 197 | * */ |
---|
| 198 | inline void intToBytes_bigEndian(unsigned char *b, unsigned int num) |
---|
| 199 | { |
---|
| 200 | b[0] = (unsigned char)(num >> 24); |
---|
| 201 | b[1] = (unsigned char)(num >> 16); |
---|
| 202 | b[2] = (unsigned char)(num >> 8); |
---|
| 203 | b[3] = (unsigned char)(num); |
---|
| 204 | |
---|
| 205 | //note: num >> xxx already considered endian_type... |
---|
| 206 | //if(dataEndianType==LITTLE_ENDIAN_DATA) |
---|
| 207 | // symTransform_4bytes(*b); //change to BIG_ENDIAN_DATA |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | inline void int64ToBytes_bigEndian(unsigned char *b, uint64_t num) |
---|
| 211 | { |
---|
| 212 | b[0] = (unsigned char)(num>>56); |
---|
| 213 | b[1] = (unsigned char)(num>>48); |
---|
| 214 | b[2] = (unsigned char)(num>>40); |
---|
| 215 | b[3] = (unsigned char)(num>>32); |
---|
| 216 | b[4] = (unsigned char)(num>>24); |
---|
| 217 | b[5] = (unsigned char)(num>>16); |
---|
| 218 | b[6] = (unsigned char)(num>>8); |
---|
| 219 | b[7] = (unsigned char)(num); |
---|
| 220 | } |
---|
| 221 | |
---|
| 222 | inline void int32ToBytes_bigEndian(unsigned char *b, uint32_t num) |
---|
| 223 | { |
---|
| 224 | b[0] = (unsigned char)(num >> 24); |
---|
| 225 | b[1] = (unsigned char)(num >> 16); |
---|
| 226 | b[2] = (unsigned char)(num >> 8); |
---|
| 227 | b[3] = (unsigned char)(num); |
---|
| 228 | } |
---|
| 229 | |
---|
| 230 | inline void int16ToBytes_bigEndian(unsigned char *b, uint16_t num) |
---|
| 231 | { |
---|
| 232 | b[0] = (unsigned char)(num >> 8); |
---|
| 233 | b[1] = (unsigned char)(num); |
---|
| 234 | } |
---|
| 235 | |
---|
| 236 | /** |
---|
| 237 | * @endianType: refers to the endian_type of unsigned char* b. |
---|
| 238 | * */ |
---|
| 239 | inline long bytesToLong_bigEndian(unsigned char* b) { |
---|
| 240 | long temp = 0; |
---|
| 241 | long res = 0; |
---|
| 242 | |
---|
| 243 | res <<= 8; |
---|
| 244 | temp = b[0] & 0xff; |
---|
| 245 | res |= temp; |
---|
| 246 | |
---|
| 247 | res <<= 8; |
---|
| 248 | temp = b[1] & 0xff; |
---|
| 249 | res |= temp; |
---|
| 250 | |
---|
| 251 | res <<= 8; |
---|
| 252 | temp = b[2] & 0xff; |
---|
| 253 | res |= temp; |
---|
| 254 | |
---|
| 255 | res <<= 8; |
---|
| 256 | temp = b[3] & 0xff; |
---|
| 257 | res |= temp; |
---|
| 258 | |
---|
| 259 | res <<= 8; |
---|
| 260 | temp = b[4] & 0xff; |
---|
| 261 | res |= temp; |
---|
| 262 | |
---|
| 263 | res <<= 8; |
---|
| 264 | temp = b[5] & 0xff; |
---|
| 265 | res |= temp; |
---|
| 266 | |
---|
| 267 | res <<= 8; |
---|
| 268 | temp = b[6] & 0xff; |
---|
| 269 | res |= temp; |
---|
| 270 | |
---|
| 271 | res <<= 8; |
---|
| 272 | temp = b[7] & 0xff; |
---|
| 273 | res |= temp; |
---|
| 274 | |
---|
| 275 | return res; |
---|
| 276 | } |
---|
| 277 | |
---|
| 278 | inline void longToBytes_bigEndian(unsigned char *b, unsigned long num) |
---|
| 279 | { |
---|
| 280 | b[0] = (unsigned char)(num>>56); |
---|
| 281 | b[1] = (unsigned char)(num>>48); |
---|
| 282 | b[2] = (unsigned char)(num>>40); |
---|
| 283 | b[3] = (unsigned char)(num>>32); |
---|
| 284 | b[4] = (unsigned char)(num>>24); |
---|
| 285 | b[5] = (unsigned char)(num>>16); |
---|
| 286 | b[6] = (unsigned char)(num>>8); |
---|
| 287 | b[7] = (unsigned char)(num); |
---|
| 288 | // if(dataEndianType==LITTLE_ENDIAN_DATA) |
---|
| 289 | // symTransform_8bytes(*b); |
---|
| 290 | } |
---|
| 291 | |
---|
| 292 | |
---|
| 293 | inline long doubleToOSEndianLong(double value) |
---|
| 294 | { |
---|
| 295 | ldouble buf; |
---|
| 296 | buf.value = value; |
---|
| 297 | return buf.lvalue; |
---|
| 298 | } |
---|
| 299 | |
---|
| 300 | inline int floatToOSEndianInt(float value) |
---|
| 301 | { |
---|
| 302 | lfloat buf; |
---|
| 303 | buf.value = value; |
---|
| 304 | return buf.ivalue; |
---|
| 305 | } |
---|
| 306 | |
---|
| 307 | //TODO: debug: lfBuf.lvalue could be actually little_endian.... |
---|
| 308 | inline short getExponent_float(float value) |
---|
| 309 | { |
---|
| 310 | //int ivalue = floatToBigEndianInt(value); |
---|
| 311 | |
---|
| 312 | lfloat lbuf; |
---|
| 313 | lbuf.value = value; |
---|
| 314 | int ivalue = lbuf.ivalue; |
---|
| 315 | |
---|
| 316 | int expValue = (ivalue & 0x7F800000) >> 23; |
---|
| 317 | expValue -= 127; |
---|
| 318 | return (short)expValue; |
---|
| 319 | } |
---|
| 320 | |
---|
| 321 | inline short getPrecisionReqLength_float(float precision) |
---|
| 322 | { |
---|
| 323 | lfloat lbuf; |
---|
| 324 | lbuf.value = precision; |
---|
| 325 | int ivalue = lbuf.ivalue; |
---|
| 326 | |
---|
| 327 | int expValue = (ivalue & 0x7F800000) >> 23; |
---|
| 328 | expValue -= 127; |
---|
| 329 | // unsigned char the1stManBit = (unsigned char)((ivalue & 0x00400000) >> 22); |
---|
| 330 | // if(the1stManBit==1) |
---|
| 331 | // expValue--; |
---|
| 332 | return (short)expValue; |
---|
| 333 | } |
---|
| 334 | |
---|
| 335 | inline short getExponent_double(double value) |
---|
| 336 | { |
---|
| 337 | //long lvalue = doubleToBigEndianLong(value); |
---|
| 338 | |
---|
| 339 | ldouble lbuf; |
---|
| 340 | lbuf.value = value; |
---|
| 341 | long lvalue = lbuf.lvalue; |
---|
| 342 | |
---|
| 343 | int expValue = (int)((lvalue & 0x7FF0000000000000) >> 52); |
---|
| 344 | expValue -= 1023; |
---|
| 345 | return (short)expValue; |
---|
| 346 | } |
---|
| 347 | |
---|
| 348 | short getPrecisionReqLength_double(double precision) |
---|
| 349 | { |
---|
| 350 | ldouble lbuf; |
---|
| 351 | lbuf.value = precision; |
---|
| 352 | long lvalue = lbuf.lvalue; |
---|
| 353 | |
---|
| 354 | int expValue = (int)((lvalue & 0x7FF0000000000000) >> 52); |
---|
| 355 | expValue -= 1023; |
---|
| 356 | // unsigned char the1stManBit = (unsigned char)((lvalue & 0x0008000000000000) >> 51); |
---|
| 357 | // if(the1stManBit==1) |
---|
| 358 | // expValue--; |
---|
| 359 | return (short)expValue; |
---|
| 360 | } |
---|
| 361 | |
---|
| 362 | unsigned char numberOfLeadingZeros_Int(int i) { |
---|
| 363 | if (i == 0) |
---|
| 364 | return 32; |
---|
| 365 | unsigned char n = 1; |
---|
| 366 | if (((unsigned int)i) >> 16 == 0) { n += 16; i <<= 16; } |
---|
| 367 | if (((unsigned int)i) >> 24 == 0) { n += 8; i <<= 8; } |
---|
| 368 | if (((unsigned int)i) >> 28 == 0) { n += 4; i <<= 4; } |
---|
| 369 | if (((unsigned int)i) >> 30 == 0) { n += 2; i <<= 2; } |
---|
| 370 | n -= ((unsigned int)i) >> 31; |
---|
| 371 | return n; |
---|
| 372 | } |
---|
| 373 | |
---|
| 374 | unsigned char numberOfLeadingZeros_Long(long i) { |
---|
| 375 | if (i == 0) |
---|
| 376 | return 64; |
---|
| 377 | unsigned char n = 1; |
---|
| 378 | int x = (int)(((unsigned long)i) >> 32); |
---|
| 379 | if (x == 0) { n += 32; x = (int)i; } |
---|
| 380 | if (((unsigned int)x) >> 16 == 0) { n += 16; x <<= 16; } |
---|
| 381 | if (((unsigned int)x) >> 24 == 0) { n += 8; x <<= 8; } |
---|
| 382 | if (((unsigned int)x) >> 28 == 0) { n += 4; x <<= 4; } |
---|
| 383 | if (((unsigned int)x) >> 30 == 0) { n += 2; x <<= 2; } |
---|
| 384 | n -= ((unsigned int)x) >> 31; |
---|
| 385 | return n; |
---|
| 386 | } |
---|
| 387 | |
---|
| 388 | unsigned char getLeadingNumbers_Int(int v1, int v2) |
---|
| 389 | { |
---|
| 390 | int v = v1 ^ v2; |
---|
| 391 | return (unsigned char)numberOfLeadingZeros_Int(v); |
---|
| 392 | } |
---|
| 393 | |
---|
| 394 | unsigned char getLeadingNumbers_Long(long v1, long v2) |
---|
| 395 | { |
---|
| 396 | long v = v1 ^ v2; |
---|
| 397 | return (unsigned char)numberOfLeadingZeros_Long(v); |
---|
| 398 | } |
---|
| 399 | |
---|
| 400 | /** |
---|
| 401 | * By default, the endian type is OS endian type. |
---|
| 402 | * */ |
---|
| 403 | short bytesToShort(unsigned char* bytes) |
---|
| 404 | { |
---|
| 405 | lint16 buf; |
---|
| 406 | memcpy(buf.byte, bytes, 2); |
---|
| 407 | |
---|
| 408 | return buf.svalue; |
---|
| 409 | } |
---|
| 410 | |
---|
| 411 | void shortToBytes(unsigned char* b, short value) |
---|
| 412 | { |
---|
| 413 | lint16 buf; |
---|
| 414 | buf.svalue = value; |
---|
| 415 | memcpy(b, buf.byte, 2); |
---|
| 416 | } |
---|
| 417 | |
---|
| 418 | int bytesToInt(unsigned char* bytes) |
---|
| 419 | { |
---|
| 420 | lfloat buf; |
---|
| 421 | memcpy(buf.byte, bytes, 4); |
---|
| 422 | return buf.ivalue; |
---|
| 423 | } |
---|
| 424 | |
---|
| 425 | long bytesToLong(unsigned char* bytes) |
---|
| 426 | { |
---|
| 427 | ldouble buf; |
---|
| 428 | memcpy(buf.byte, bytes, 8); |
---|
| 429 | return buf.lvalue; |
---|
| 430 | } |
---|
| 431 | |
---|
| 432 | //the byte to input is in the big-endian format |
---|
| 433 | float bytesToFloat(unsigned char* bytes) |
---|
| 434 | { |
---|
| 435 | lfloat buf; |
---|
| 436 | memcpy(buf.byte, bytes, 4); |
---|
| 437 | if(sysEndianType==LITTLE_ENDIAN_SYSTEM) |
---|
| 438 | symTransform_4bytes(buf.byte); |
---|
| 439 | return buf.value; |
---|
| 440 | } |
---|
| 441 | |
---|
| 442 | void floatToBytes(unsigned char *b, float num) |
---|
| 443 | { |
---|
| 444 | lfloat buf; |
---|
| 445 | buf.value = num; |
---|
| 446 | memcpy(b, buf.byte, 4); |
---|
| 447 | if(sysEndianType==LITTLE_ENDIAN_SYSTEM) |
---|
| 448 | symTransform_4bytes(b); |
---|
| 449 | } |
---|
| 450 | |
---|
| 451 | //the byte to input is in the big-endian format |
---|
| 452 | double bytesToDouble(unsigned char* bytes) |
---|
| 453 | { |
---|
| 454 | ldouble buf; |
---|
| 455 | memcpy(buf.byte, bytes, 8); |
---|
| 456 | if(sysEndianType==LITTLE_ENDIAN_SYSTEM) |
---|
| 457 | symTransform_8bytes(buf.byte); |
---|
| 458 | return buf.value; |
---|
| 459 | } |
---|
| 460 | |
---|
| 461 | void doubleToBytes(unsigned char *b, double num) |
---|
| 462 | { |
---|
| 463 | ldouble buf; |
---|
| 464 | buf.value = num; |
---|
| 465 | memcpy(b, buf.byte, 8); |
---|
| 466 | if(sysEndianType==LITTLE_ENDIAN_SYSTEM) |
---|
| 467 | symTransform_8bytes(b); |
---|
| 468 | } |
---|
| 469 | |
---|
| 470 | int extractBytes(unsigned char* byteArray, size_t k, int validLength) |
---|
| 471 | { |
---|
| 472 | size_t outIndex = k/8; |
---|
| 473 | int innerIndex = k%8; |
---|
| 474 | unsigned char intBytes[4]; |
---|
| 475 | int length = innerIndex + validLength; |
---|
| 476 | int byteNum = 0; |
---|
| 477 | if(length%8==0) |
---|
| 478 | byteNum = length/8; |
---|
| 479 | else |
---|
| 480 | byteNum = length/8+1; |
---|
| 481 | |
---|
| 482 | int i; |
---|
| 483 | for(i = 0;i<byteNum;i++) |
---|
| 484 | intBytes[exe_params->SZ_SIZE_TYPE-byteNum+i] = byteArray[outIndex+i]; |
---|
| 485 | int result = bytesToInt_bigEndian(intBytes); |
---|
| 486 | int rightMovSteps = innerIndex +(8 - (innerIndex+validLength)%8)%8; |
---|
| 487 | result = result << innerIndex; |
---|
| 488 | switch(byteNum) |
---|
| 489 | { |
---|
| 490 | case 1: |
---|
| 491 | result = result & 0xff; |
---|
| 492 | break; |
---|
| 493 | case 2: |
---|
| 494 | result = result & 0xffff; |
---|
| 495 | break; |
---|
| 496 | case 3: |
---|
| 497 | result = result & 0xffffff; |
---|
| 498 | break; |
---|
| 499 | case 4: |
---|
| 500 | break; |
---|
| 501 | default: |
---|
| 502 | printf("Error: other cases are impossible...\n"); |
---|
| 503 | exit(0); |
---|
| 504 | } |
---|
| 505 | result = result >> rightMovSteps; |
---|
| 506 | |
---|
| 507 | return result; |
---|
| 508 | } |
---|
| 509 | |
---|
| 510 | int getMaskRightCode(int m) { |
---|
| 511 | switch (m) { |
---|
| 512 | case 1: |
---|
| 513 | return 0x01; |
---|
| 514 | case 2: |
---|
| 515 | return 0x03; |
---|
| 516 | case 3: |
---|
| 517 | return 0x07; |
---|
| 518 | case 4: |
---|
| 519 | return 0x0F; |
---|
| 520 | case 5: |
---|
| 521 | return 0x1F; |
---|
| 522 | case 6: |
---|
| 523 | return 0x3F; |
---|
| 524 | case 7: |
---|
| 525 | return 0X7F; |
---|
| 526 | case 8: |
---|
| 527 | return 0XFF; |
---|
| 528 | default: |
---|
| 529 | return 0; |
---|
| 530 | } |
---|
| 531 | } |
---|
| 532 | |
---|
| 533 | int getLeftMovingCode(int kMod8) |
---|
| 534 | { |
---|
| 535 | return getMaskRightCode(8 - kMod8); |
---|
| 536 | } |
---|
| 537 | |
---|
| 538 | int getRightMovingSteps(int kMod8, int resiBitLength) { |
---|
| 539 | return 8 - kMod8 - resiBitLength; |
---|
| 540 | } |
---|
| 541 | |
---|
| 542 | int getRightMovingCode(int kMod8, int resiBitLength) |
---|
| 543 | { |
---|
| 544 | int rightMovingSteps = 8 - kMod8 - resiBitLength; |
---|
| 545 | if(rightMovingSteps < 0) |
---|
| 546 | { |
---|
| 547 | switch(-rightMovingSteps) |
---|
| 548 | { |
---|
| 549 | case 1: |
---|
| 550 | return 0x80; |
---|
| 551 | case 2: |
---|
| 552 | return 0xC0; |
---|
| 553 | case 3: |
---|
| 554 | return 0xE0; |
---|
| 555 | case 4: |
---|
| 556 | return 0xF0; |
---|
| 557 | case 5: |
---|
| 558 | return 0xF8; |
---|
| 559 | case 6: |
---|
| 560 | return 0xFC; |
---|
| 561 | case 7: |
---|
| 562 | return 0XFE; |
---|
| 563 | default: |
---|
| 564 | return 0; |
---|
| 565 | } |
---|
| 566 | } |
---|
| 567 | else //if(rightMovingSteps >= 0) |
---|
| 568 | { |
---|
| 569 | int a = getMaskRightCode(8 - kMod8); |
---|
| 570 | int b = getMaskRightCode(8 - kMod8 - resiBitLength); |
---|
| 571 | int c = a - b; |
---|
| 572 | return c; |
---|
| 573 | } |
---|
| 574 | } |
---|
| 575 | |
---|
| 576 | short* convertByteDataToShortArray(unsigned char* bytes, size_t byteLength) |
---|
| 577 | { |
---|
| 578 | lint16 ls; |
---|
| 579 | size_t i, stateLength = byteLength/2; |
---|
| 580 | short* states = (short*)malloc(stateLength*sizeof(short)); |
---|
| 581 | if(sysEndianType==dataEndianType) |
---|
| 582 | { |
---|
| 583 | for(i=0;i<stateLength;i++) |
---|
| 584 | { |
---|
| 585 | ls.byte[0] = bytes[i*2]; |
---|
| 586 | ls.byte[1] = bytes[i*2+1]; |
---|
| 587 | states[i] = ls.svalue; |
---|
| 588 | } |
---|
| 589 | } |
---|
| 590 | else |
---|
| 591 | { |
---|
| 592 | for(i=0;i<stateLength;i++) |
---|
| 593 | { |
---|
| 594 | ls.byte[0] = bytes[i*2+1]; |
---|
| 595 | ls.byte[1] = bytes[i*2]; |
---|
| 596 | states[i] = ls.svalue; |
---|
| 597 | } |
---|
| 598 | } |
---|
| 599 | return states; |
---|
| 600 | } |
---|
| 601 | |
---|
| 602 | unsigned short* convertByteDataToUShortArray(unsigned char* bytes, size_t byteLength) |
---|
| 603 | { |
---|
| 604 | lint16 ls; |
---|
| 605 | size_t i, stateLength = byteLength/2; |
---|
| 606 | unsigned short* states = (unsigned short*)malloc(stateLength*sizeof(unsigned short)); |
---|
| 607 | if(sysEndianType==dataEndianType) |
---|
| 608 | { |
---|
| 609 | for(i=0;i<stateLength;i++) |
---|
| 610 | { |
---|
| 611 | ls.byte[0] = bytes[i*2]; |
---|
| 612 | ls.byte[1] = bytes[i*2+1]; |
---|
| 613 | states[i] = ls.usvalue; |
---|
| 614 | } |
---|
| 615 | } |
---|
| 616 | else |
---|
| 617 | { |
---|
| 618 | for(i=0;i<stateLength;i++) |
---|
| 619 | { |
---|
| 620 | ls.byte[0] = bytes[i*2+1]; |
---|
| 621 | ls.byte[1] = bytes[i*2]; |
---|
| 622 | states[i] = ls.usvalue; |
---|
| 623 | } |
---|
| 624 | } |
---|
| 625 | return states; |
---|
| 626 | } |
---|
| 627 | |
---|
| 628 | void convertShortArrayToBytes(short* states, size_t stateLength, unsigned char* bytes) |
---|
| 629 | { |
---|
| 630 | lint16 ls; |
---|
| 631 | size_t i; |
---|
| 632 | if(sysEndianType==dataEndianType) |
---|
| 633 | { |
---|
| 634 | for(i=0;i<stateLength;i++) |
---|
| 635 | { |
---|
| 636 | ls.svalue = states[i]; |
---|
| 637 | bytes[i*2] = ls.byte[0]; |
---|
| 638 | bytes[i*2+1] = ls.byte[1]; |
---|
| 639 | } |
---|
| 640 | } |
---|
| 641 | else |
---|
| 642 | { |
---|
| 643 | for(i=0;i<stateLength;i++) |
---|
| 644 | { |
---|
| 645 | ls.svalue = states[i]; |
---|
| 646 | bytes[i*2] = ls.byte[1]; |
---|
| 647 | bytes[i*2+1] = ls.byte[0]; |
---|
| 648 | } |
---|
| 649 | } |
---|
| 650 | } |
---|
| 651 | |
---|
| 652 | void convertUShortArrayToBytes(unsigned short* states, size_t stateLength, unsigned char* bytes) |
---|
| 653 | { |
---|
| 654 | lint16 ls; |
---|
| 655 | size_t i; |
---|
| 656 | if(sysEndianType==dataEndianType) |
---|
| 657 | { |
---|
| 658 | for(i=0;i<stateLength;i++) |
---|
| 659 | { |
---|
| 660 | ls.usvalue = states[i]; |
---|
| 661 | bytes[i*2] = ls.byte[0]; |
---|
| 662 | bytes[i*2+1] = ls.byte[1]; |
---|
| 663 | } |
---|
| 664 | } |
---|
| 665 | else |
---|
| 666 | { |
---|
| 667 | for(i=0;i<stateLength;i++) |
---|
| 668 | { |
---|
| 669 | ls.usvalue = states[i]; |
---|
| 670 | bytes[i*2] = ls.byte[1]; |
---|
| 671 | bytes[i*2+1] = ls.byte[0]; |
---|
| 672 | } |
---|
| 673 | } |
---|
| 674 | } |
---|
| 675 | |
---|
| 676 | void convertIntArrayToBytes(int* states, size_t stateLength, unsigned char* bytes) |
---|
| 677 | { |
---|
| 678 | lint32 ls; |
---|
| 679 | size_t index = 0; |
---|
| 680 | size_t i; |
---|
| 681 | if(sysEndianType==dataEndianType) |
---|
| 682 | { |
---|
| 683 | for(i=0;i<stateLength;i++) |
---|
| 684 | { |
---|
| 685 | index = i << 2; //==i*4 |
---|
| 686 | ls.ivalue = states[i]; |
---|
| 687 | bytes[index] = ls.byte[0]; |
---|
| 688 | bytes[index+1] = ls.byte[1]; |
---|
| 689 | bytes[index+2] = ls.byte[2]; |
---|
| 690 | bytes[index+3] = ls.byte[3]; |
---|
| 691 | } |
---|
| 692 | } |
---|
| 693 | else |
---|
| 694 | { |
---|
| 695 | for(i=0;i<stateLength;i++) |
---|
| 696 | { |
---|
| 697 | index = i << 2; //==i*4 |
---|
| 698 | ls.ivalue = states[i]; |
---|
| 699 | bytes[index] = ls.byte[3]; |
---|
| 700 | bytes[index+1] = ls.byte[2]; |
---|
| 701 | bytes[index+2] = ls.byte[1]; |
---|
| 702 | bytes[index+3] = ls.byte[0]; |
---|
| 703 | } |
---|
| 704 | } |
---|
| 705 | } |
---|
| 706 | |
---|
| 707 | void convertUIntArrayToBytes(unsigned int* states, size_t stateLength, unsigned char* bytes) |
---|
| 708 | { |
---|
| 709 | lint32 ls; |
---|
| 710 | size_t index = 0; |
---|
| 711 | size_t i; |
---|
| 712 | if(sysEndianType==dataEndianType) |
---|
| 713 | { |
---|
| 714 | for(i=0;i<stateLength;i++) |
---|
| 715 | { |
---|
| 716 | index = i << 2; //==i*4 |
---|
| 717 | ls.uivalue = states[i]; |
---|
| 718 | bytes[index] = ls.byte[0]; |
---|
| 719 | bytes[index+1] = ls.byte[1]; |
---|
| 720 | bytes[index+2] = ls.byte[2]; |
---|
| 721 | bytes[index+3] = ls.byte[3]; |
---|
| 722 | } |
---|
| 723 | } |
---|
| 724 | else |
---|
| 725 | { |
---|
| 726 | for(i=0;i<stateLength;i++) |
---|
| 727 | { |
---|
| 728 | index = i << 2; //==i*4 |
---|
| 729 | ls.uivalue = states[i]; |
---|
| 730 | bytes[index] = ls.byte[3]; |
---|
| 731 | bytes[index+1] = ls.byte[2]; |
---|
| 732 | bytes[index+2] = ls.byte[1]; |
---|
| 733 | bytes[index+3] = ls.byte[0]; |
---|
| 734 | } |
---|
| 735 | } |
---|
| 736 | } |
---|
| 737 | |
---|
| 738 | void convertLongArrayToBytes(int64_t* states, size_t stateLength, unsigned char* bytes) |
---|
| 739 | { |
---|
| 740 | lint64 ls; |
---|
| 741 | size_t index = 0; |
---|
| 742 | size_t i; |
---|
| 743 | if(sysEndianType==dataEndianType) |
---|
| 744 | { |
---|
| 745 | for(i=0;i<stateLength;i++) |
---|
| 746 | { |
---|
| 747 | index = i << 3; //==i*8 |
---|
| 748 | ls.lvalue = states[i]; |
---|
| 749 | bytes[index] = ls.byte[0]; |
---|
| 750 | bytes[index+1] = ls.byte[1]; |
---|
| 751 | bytes[index+2] = ls.byte[2]; |
---|
| 752 | bytes[index+3] = ls.byte[3]; |
---|
| 753 | bytes[index+4] = ls.byte[4]; |
---|
| 754 | bytes[index+5] = ls.byte[5]; |
---|
| 755 | bytes[index+6] = ls.byte[6]; |
---|
| 756 | bytes[index+7] = ls.byte[7]; |
---|
| 757 | } |
---|
| 758 | } |
---|
| 759 | else |
---|
| 760 | { |
---|
| 761 | for(i=0;i<stateLength;i++) |
---|
| 762 | { |
---|
| 763 | index = i << 3; //==i*8 |
---|
| 764 | ls.lvalue = states[i]; |
---|
| 765 | bytes[index] = ls.byte[7]; |
---|
| 766 | bytes[index+1] = ls.byte[6]; |
---|
| 767 | bytes[index+2] = ls.byte[5]; |
---|
| 768 | bytes[index+3] = ls.byte[4]; |
---|
| 769 | bytes[index+4] = ls.byte[3]; |
---|
| 770 | bytes[index+5] = ls.byte[2]; |
---|
| 771 | bytes[index+6] = ls.byte[1]; |
---|
| 772 | bytes[index+7] = ls.byte[0]; |
---|
| 773 | } |
---|
| 774 | } |
---|
| 775 | } |
---|
| 776 | |
---|
| 777 | void convertULongArrayToBytes(uint64_t* states, size_t stateLength, unsigned char* bytes) |
---|
| 778 | { |
---|
| 779 | lint64 ls; |
---|
| 780 | size_t index = 0; |
---|
| 781 | size_t i; |
---|
| 782 | if(sysEndianType==dataEndianType) |
---|
| 783 | { |
---|
| 784 | for(i=0;i<stateLength;i++) |
---|
| 785 | { |
---|
| 786 | index = i << 3; //==i*8 |
---|
| 787 | ls.ulvalue = states[i]; |
---|
| 788 | bytes[index] = ls.byte[0]; |
---|
| 789 | bytes[index+1] = ls.byte[1]; |
---|
| 790 | bytes[index+2] = ls.byte[2]; |
---|
| 791 | bytes[index+3] = ls.byte[3]; |
---|
| 792 | bytes[index+4] = ls.byte[4]; |
---|
| 793 | bytes[index+5] = ls.byte[5]; |
---|
| 794 | bytes[index+6] = ls.byte[6]; |
---|
| 795 | bytes[index+7] = ls.byte[7]; |
---|
| 796 | } |
---|
| 797 | } |
---|
| 798 | else |
---|
| 799 | { |
---|
| 800 | for(i=0;i<stateLength;i++) |
---|
| 801 | { |
---|
| 802 | index = i << 3; //==i*8 |
---|
| 803 | ls.ulvalue = states[i]; |
---|
| 804 | bytes[index] = ls.byte[7]; |
---|
| 805 | bytes[index+1] = ls.byte[6]; |
---|
| 806 | bytes[index+2] = ls.byte[5]; |
---|
| 807 | bytes[index+3] = ls.byte[4]; |
---|
| 808 | bytes[index+4] = ls.byte[3]; |
---|
| 809 | bytes[index+5] = ls.byte[2]; |
---|
| 810 | bytes[index+6] = ls.byte[1]; |
---|
| 811 | bytes[index+7] = ls.byte[0]; |
---|
| 812 | } |
---|
| 813 | } |
---|
| 814 | } |
---|
| 815 | |
---|
| 816 | |
---|
| 817 | size_t bytesToSize(unsigned char* bytes) |
---|
| 818 | { |
---|
| 819 | size_t result = 0; |
---|
| 820 | if(exe_params->SZ_SIZE_TYPE==4) |
---|
| 821 | result = bytesToInt_bigEndian(bytes);//4 |
---|
| 822 | else |
---|
| 823 | result = bytesToLong_bigEndian(bytes);//8 |
---|
| 824 | return result; |
---|
| 825 | } |
---|
| 826 | |
---|
| 827 | void sizeToBytes(unsigned char* outBytes, size_t size) |
---|
| 828 | { |
---|
| 829 | if(exe_params->SZ_SIZE_TYPE==4) |
---|
| 830 | intToBytes_bigEndian(outBytes, size);//4 |
---|
| 831 | else |
---|
| 832 | longToBytes_bigEndian(outBytes, size);//8 |
---|
| 833 | } |
---|
| 834 | |
---|
| 835 | void convertSZParamsToBytes(sz_params* params, unsigned char* result) |
---|
| 836 | { |
---|
| 837 | //unsigned char* result = (unsigned char*)malloc(16); |
---|
| 838 | unsigned char buf; |
---|
| 839 | //flag1: exe_params->optQuantMode(1bit), dataEndianType(1bit), sysEndianType(1bit), conf_params->szMode (1bit), conf_params->gzipMode (2bits), pwrType (2bits) |
---|
| 840 | buf = exe_params->optQuantMode; |
---|
| 841 | buf = (buf << 1) | dataEndianType; |
---|
| 842 | buf = (buf << 1) | sysEndianType; |
---|
| 843 | buf = (buf << 1) | params->szMode; |
---|
| 844 | |
---|
| 845 | int tmp = 0; |
---|
| 846 | switch(params->gzipMode) |
---|
| 847 | { |
---|
| 848 | case Z_BEST_SPEED: |
---|
| 849 | tmp = 0; |
---|
| 850 | break; |
---|
| 851 | case Z_DEFAULT_STRATEGY: |
---|
| 852 | tmp = 1; |
---|
| 853 | break; |
---|
| 854 | case Z_BEST_COMPRESSION: |
---|
| 855 | tmp = 2; |
---|
| 856 | break; |
---|
| 857 | } |
---|
| 858 | buf = (buf << 2) | tmp; |
---|
| 859 | buf = (buf << 2) | params->pwr_type; |
---|
| 860 | result[0] = buf; |
---|
| 861 | |
---|
| 862 | //sampleDistance; //2 bytes |
---|
| 863 | int16ToBytes_bigEndian(&result[1], params->sampleDistance); |
---|
| 864 | |
---|
| 865 | //conf_params->predThreshold; // 2 bytes |
---|
| 866 | short tmp2 = params->predThreshold * 10000; |
---|
| 867 | int16ToBytes_bigEndian(&result[3], tmp2); |
---|
| 868 | |
---|
| 869 | //errorBoundMode; //4bits(0.5 byte) |
---|
| 870 | result[5] = params->errorBoundMode; |
---|
| 871 | |
---|
| 872 | //data type (float, double, int8, int16, ....) //10 choices, so 4 bits |
---|
| 873 | result[5] = (result[5] << 4) | (params->dataType & 0x17); |
---|
| 874 | |
---|
| 875 | //result[5]: abs_err_bound or psnr //4 bytes |
---|
| 876 | //result[9]: rel_bound_ratio or pwr_err_bound//4 bytes |
---|
| 877 | switch(params->errorBoundMode) |
---|
| 878 | { |
---|
| 879 | case ABS: |
---|
| 880 | floatToBytes(&result[6], (float)(params->absErrBound)); //big_endian |
---|
| 881 | memset(&result[10], 0, 4); |
---|
| 882 | break; |
---|
| 883 | case REL: |
---|
| 884 | memset(&result[6], 0, 4); |
---|
| 885 | floatToBytes(&result[10], (float)(params->relBoundRatio)); //big_endian |
---|
| 886 | break; |
---|
| 887 | case ABS_AND_REL: |
---|
| 888 | case ABS_OR_REL: |
---|
| 889 | floatToBytes(&result[6], (float)(params->absErrBound)); |
---|
| 890 | floatToBytes(&result[10], (float)(params->relBoundRatio)); //big_endian |
---|
| 891 | break; |
---|
| 892 | case PSNR: |
---|
| 893 | floatToBytes(&result[6], (float)(params->psnr)); |
---|
| 894 | memset(&result[9], 0, 4); |
---|
| 895 | break; |
---|
| 896 | case ABS_AND_PW_REL: |
---|
| 897 | case ABS_OR_PW_REL: |
---|
| 898 | floatToBytes(&result[6], (float)(params->absErrBound)); |
---|
| 899 | floatToBytes(&result[10], (float)(params->pw_relBoundRatio)); //big_endian |
---|
| 900 | break; |
---|
| 901 | case REL_AND_PW_REL: |
---|
| 902 | case REL_OR_PW_REL: |
---|
| 903 | floatToBytes(&result[6], (float)(params->relBoundRatio)); |
---|
| 904 | floatToBytes(&result[10], (float)(params->pw_relBoundRatio)); //big_endian |
---|
| 905 | break; |
---|
| 906 | case PW_REL: |
---|
| 907 | memset(&result[6], 0, 4); |
---|
| 908 | floatToBytes(&result[10], (float)(params->pw_relBoundRatio)); //big_endian |
---|
| 909 | break; |
---|
| 910 | } |
---|
| 911 | |
---|
| 912 | //segment_size // 2 bytes |
---|
| 913 | int16ToBytes_bigEndian(&result[14], (short)(params->segment_size)); |
---|
| 914 | |
---|
| 915 | if(exe_params->optQuantMode==1) |
---|
| 916 | int32ToBytes_bigEndian(&result[16], params->max_quant_intervals); |
---|
| 917 | else |
---|
| 918 | int32ToBytes_bigEndian(&result[16], params->quantization_intervals); |
---|
| 919 | } |
---|
| 920 | |
---|
| 921 | sz_params* convertBytesToSZParams(unsigned char* bytes) |
---|
| 922 | { |
---|
| 923 | sz_params* params = (sz_params*)malloc(sizeof(struct sz_params)); |
---|
| 924 | unsigned char flag1 = bytes[0]; |
---|
| 925 | exe_params->optQuantMode = flag1 >> 7; |
---|
| 926 | dataEndianType = (flag1 & 0x7f) >> 7; |
---|
| 927 | sysEndianType = (flag1 & 0x3f) >> 7; |
---|
| 928 | |
---|
| 929 | params->szMode = (flag1 & 0x1f) >> 7; |
---|
| 930 | |
---|
| 931 | int tmp = (flag1 & 0x0f) >> 6; |
---|
| 932 | switch(tmp) |
---|
| 933 | { |
---|
| 934 | case 0: |
---|
| 935 | params->gzipMode = Z_BEST_SPEED; |
---|
| 936 | break; |
---|
| 937 | case 1: |
---|
| 938 | params->gzipMode = Z_DEFAULT_STRATEGY; |
---|
| 939 | break; |
---|
| 940 | case 2: |
---|
| 941 | params->gzipMode = Z_BEST_COMPRESSION; |
---|
| 942 | break; |
---|
| 943 | } |
---|
| 944 | |
---|
| 945 | params->pwr_type = (flag1 & 0x03) >> 6; |
---|
| 946 | |
---|
| 947 | params->sampleDistance = bytesToInt16_bigEndian(&bytes[1]); |
---|
| 948 | |
---|
| 949 | params->predThreshold = 1.0*bytesToInt16_bigEndian(&bytes[3])/10000.0; |
---|
| 950 | |
---|
| 951 | params->dataType = bytes[5] & 0x07; |
---|
| 952 | |
---|
| 953 | params->errorBoundMode = (bytes[5] & 0xf0) >> 4; |
---|
| 954 | |
---|
| 955 | switch(params->errorBoundMode) |
---|
| 956 | { |
---|
| 957 | case ABS: |
---|
| 958 | params->absErrBound = bytesToFloat(&bytes[6]); |
---|
| 959 | break; |
---|
| 960 | case REL: |
---|
| 961 | params->relBoundRatio = bytesToFloat(&bytes[10]); |
---|
| 962 | break; |
---|
| 963 | case ABS_AND_REL: |
---|
| 964 | case ABS_OR_REL: |
---|
| 965 | params->absErrBound = bytesToFloat(&bytes[6]); |
---|
| 966 | params->relBoundRatio = bytesToFloat(&bytes[10]); |
---|
| 967 | break; |
---|
| 968 | case PSNR: |
---|
| 969 | params->psnr = bytesToFloat(&bytes[6]); |
---|
| 970 | break; |
---|
| 971 | case ABS_AND_PW_REL: |
---|
| 972 | case ABS_OR_PW_REL: |
---|
| 973 | params->absErrBound = bytesToFloat(&bytes[6]); |
---|
| 974 | params->pw_relBoundRatio = bytesToFloat(&bytes[10]); |
---|
| 975 | break; |
---|
| 976 | case REL_AND_PW_REL: |
---|
| 977 | case REL_OR_PW_REL: |
---|
| 978 | params->relBoundRatio = bytesToFloat(&bytes[6]); |
---|
| 979 | params->pw_relBoundRatio = bytesToFloat(&bytes[10]); |
---|
| 980 | break; |
---|
| 981 | case PW_REL: |
---|
| 982 | params->pw_relBoundRatio = bytesToFloat(&bytes[10]); |
---|
| 983 | } |
---|
| 984 | |
---|
| 985 | //segment_size // 2 bytes |
---|
| 986 | params->segment_size = bytesToInt16_bigEndian(&bytes[14]); |
---|
| 987 | |
---|
| 988 | if(exe_params->optQuantMode==1) |
---|
| 989 | { |
---|
| 990 | params->max_quant_intervals = bytesToInt32_bigEndian(&bytes[16]); |
---|
| 991 | params->quantization_intervals = 0; |
---|
| 992 | } |
---|
| 993 | else |
---|
| 994 | { |
---|
| 995 | params->max_quant_intervals = 0; |
---|
| 996 | params->quantization_intervals = bytesToInt32_bigEndian(&bytes[16]); |
---|
| 997 | } |
---|
| 998 | return params; |
---|
| 999 | } |
---|