ByteToolkit.c 6.4 KB
Newer Older
T
tickduan 已提交
1 2 3 4 5 6 7 8 9 10
/**
 *  @file ByteToolkit.c
 *  @author Sheng Di
 *  @date April, 2016
 *  @brief Byte Toolkit
 *  (C) 2016 by Mathematics and Computer Science (MCS), Argonne National Laboratory.
 *      See COPYRIGHT in top-level directory.
 */
 
#include <stdlib.h>
T
tickduan 已提交
11
#include <string.h>
T
tickduan 已提交
12 13 14
#include "sz.h" 	
#include "zlib.h"

T
tickduan 已提交
15
INLINE int bytesToInt_bigEndian(unsigned char* bytes)
T
tickduan 已提交
16
{
T
tickduan 已提交
17 18 19 20 21 22
	int res;
	unsigned char* des = (unsigned char*)&res;
	des[0] = bytes[3];
	des[1] = bytes[2];
	des[2] = bytes[1];
	des[3] = bytes[0];
T
tickduan 已提交
23 24 25 26 27 28 29
	return res;
}

/**
 * @unsigned char *b the variable to store the converted bytes (length=4)
 * @unsigned int num
 * */
T
tickduan 已提交
30
INLINE void intToBytes_bigEndian(unsigned char *b, unsigned int num)
T
tickduan 已提交
31
{
T
tickduan 已提交
32 33 34 35 36
	unsigned char* sou =(unsigned char*)&num;
	b[0] = sou[3];
	b[1] = sou[2];
	b[2] = sou[1];
	b[3] = sou[0];
T
tickduan 已提交
37 38 39 40 41
}

/**
 * @endianType: refers to the endian_type of unsigned char* b.
 * */
T
tickduan 已提交
42
INLINE long bytesToLong_bigEndian(unsigned char* b) {
T
tickduan 已提交
43

T
tickduan 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
	long temp = 0;
	long res = 0;

	res <<= 8;
	temp = b[0] & 0xff;
	res |= temp;

	res <<= 8;
	temp = b[1] & 0xff;
	res |= temp;
	
	res <<= 8;
	temp = b[2] & 0xff;
	res |= temp;
	
	res <<= 8;
	temp = b[3] & 0xff;
	res |= temp;
	
	res <<= 8;
	temp = b[4] & 0xff;
	res |= temp;
	
	res <<= 8;
	temp = b[5] & 0xff;
	res |= temp;
	
	res <<= 8;
	temp = b[6] & 0xff;
	res |= temp;
	
	res <<= 8;
	temp = b[7] & 0xff;
	res |= temp;						
	
	return res;
T
tickduan 已提交
80
	
T
tickduan 已提交
81 82
}

T
tickduan 已提交
83
INLINE void longToBytes_bigEndian(unsigned char *b, long num) 
T
tickduan 已提交
84
{
T
tickduan 已提交
85
	unsigned char* sou = (unsigned char*)&num;
T
tickduan 已提交
86
#if defined(_TD_LINUX_64) || defined(_TD_ARM_64)  || defined(_TD_DARWIN_64)
T
tickduan 已提交
87 88 89 90 91 92 93 94 95
    // 8 bytes
	b[7] = sou[0];
	b[6] = sou[1];
	b[5] = sou[2];
	b[4] = sou[3];
	b[3] = sou[4];
	b[2] = sou[5];
	b[1] = sou[6];
	b[0] = sou[7];
T
tickduan 已提交
96 97
#else
	memset(b, 0, 4);
T
tickduan 已提交
98 99 100 101
	b[7] = sou[0];
	b[6] = sou[1];
	b[5] = sou[2];
	b[4] = sou[3];
102
#endif
T
tickduan 已提交
103 104 105
}

//TODO: debug: lfBuf.lvalue could be actually little_endian....
T
tickduan 已提交
106
INLINE short getExponent_float(float value)
T
tickduan 已提交
107 108 109 110 111 112 113 114 115 116 117 118
{
	//int ivalue = floatToBigEndianInt(value);

	lfloat lbuf;
	lbuf.value = value;
	int ivalue = lbuf.ivalue;
	
	int expValue = (ivalue & 0x7F800000) >> 23;
	expValue -= 127;
	return (short)expValue;
}

T
tickduan 已提交
119
INLINE short getPrecisionReqLength_float(float precision)
T
tickduan 已提交
120 121 122 123 124 125 126 127 128 129
{
	lfloat lbuf;
	lbuf.value = precision;
	int ivalue = lbuf.ivalue;
	
	int expValue = (ivalue & 0x7F800000) >> 23;
	expValue -= 127;
	return (short)expValue;
}

T
tickduan 已提交
130
INLINE short getExponent_double(double value)
T
tickduan 已提交
131 132 133 134 135 136 137 138 139 140 141 142
{
	//long lvalue = doubleToBigEndianLong(value);
	
	ldouble lbuf;
	lbuf.value = value;
	long lvalue = lbuf.lvalue;
	
	int expValue = (int)((lvalue & 0x7FF0000000000000) >> 52);
	expValue -= 1023;
	return (short)expValue;
}

T
tickduan 已提交
143
INLINE short getPrecisionReqLength_double(double precision)
T
tickduan 已提交
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
{
	ldouble lbuf;
	lbuf.value = precision;
	long lvalue = lbuf.lvalue;
	
	int expValue = (int)((lvalue & 0x7FF0000000000000) >> 52);
	expValue -= 1023;
//	unsigned char the1stManBit = (unsigned char)((lvalue & 0x0008000000000000) >> 51);
//	if(the1stManBit==1)
//		expValue--;
	return (short)expValue;
}


//the byte to input is in the big-endian format
T
tickduan 已提交
159
INLINE float bytesToFloat(unsigned char* bytes)
T
tickduan 已提交
160 161 162 163 164 165 166 167
{
	lfloat buf;
	memcpy(buf.byte, bytes, 4);
	if(sysEndianType==LITTLE_ENDIAN_SYSTEM)
		symTransform_4bytes(buf.byte);	
	return buf.value;
}

T
tickduan 已提交
168
INLINE void floatToBytes(unsigned char *b, float num)
T
tickduan 已提交
169 170 171 172 173 174 175 176 177
{
	lfloat buf;
	buf.value = num;
	memcpy(b, buf.byte, 4);
	if(sysEndianType==LITTLE_ENDIAN_SYSTEM)
		symTransform_4bytes(b);		
}

//the byte to input is in the big-endian format
T
tickduan 已提交
178
INLINE double bytesToDouble(unsigned char* bytes)
T
tickduan 已提交
179 180 181 182 183 184 185 186
{
	ldouble buf;
	memcpy(buf.byte, bytes, 8);
	if(sysEndianType==LITTLE_ENDIAN_SYSTEM)
		symTransform_8bytes(buf.byte);
	return buf.value;
}

T
tickduan 已提交
187
INLINE void doubleToBytes(unsigned char *b, double num)
T
tickduan 已提交
188 189 190 191 192 193 194 195 196
{
	ldouble buf;
	buf.value = num;
	memcpy(b, buf.byte, 8);
	if(sysEndianType==LITTLE_ENDIAN_SYSTEM)
		symTransform_8bytes(b);
}


T
tickduan 已提交
197
INLINE int getMaskRightCode(int m) {
T
tickduan 已提交
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
	switch (m) {
	case 1:
		return 0x01;
	case 2:
		return 0x03;
	case 3:
		return 0x07;
	case 4:
		return 0x0F;
	case 5:
		return 0x1F;
	case 6:
		return 0x3F;
	case 7:
		return 0X7F;
	case 8:
		return 0XFF;
	default:
		return 0;
	}
}

T
tickduan 已提交
220
INLINE int getLeftMovingCode(int kMod8)
T
tickduan 已提交
221 222 223 224
{
	return getMaskRightCode(8 - kMod8);
}

T
tickduan 已提交
225
INLINE int getRightMovingSteps(int kMod8, int resiBitLength) {
T
tickduan 已提交
226 227 228
	return 8 - kMod8 - resiBitLength;
}

T
tickduan 已提交
229
INLINE int getRightMovingCode(int kMod8, int resiBitLength)
T
tickduan 已提交
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
{
	int rightMovingSteps = 8 - kMod8 - resiBitLength;
	if(rightMovingSteps < 0)
	{
		switch(-rightMovingSteps)
		{
		case 1:
			return 0x80;
		case 2:
			return 0xC0;
		case 3:
			return 0xE0;
		case 4:
			return 0xF0;
		case 5:
			return 0xF8;
		case 6:
			return 0xFC;
		case 7:
			return 0XFE;
		default:
			return 0;
		}    		
	}
	else //if(rightMovingSteps >= 0)
	{
		int a = getMaskRightCode(8 - kMod8);
		int b = getMaskRightCode(8 - kMod8 - resiBitLength);
		int c = a - b;
		return c;
	}
}

T
tickduan 已提交
263
INLINE size_t bytesToSize(unsigned char* bytes)
T
tickduan 已提交
264 265 266 267 268 269 270 271 272
{
	size_t result = 0;
	if(exe_params->SZ_SIZE_TYPE==4)	
		result = bytesToInt_bigEndian(bytes);//4		
	else
		result = bytesToLong_bigEndian(bytes);//8	
	return result;
}

T
tickduan 已提交
273
INLINE void sizeToBytes(unsigned char* outBytes, size_t size)
T
tickduan 已提交
274 275
{
	if(exe_params->SZ_SIZE_TYPE==4)
T
tickduan 已提交
276
		intToBytes_bigEndian(outBytes, (unsigned int)size);//4
T
tickduan 已提交
277
	else
T
tickduan 已提交
278
		longToBytes_bigEndian(outBytes, (unsigned long)size);//8
T
tickduan 已提交
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
}

void convertSZParamsToBytes(sz_params* params, unsigned char* result)
{
	//unsigned char* result = (unsigned char*)malloc(16);
	unsigned char buf = 0;
	//flag1: exe_params->optQuantMode(1bit), dataEndianType(1bit), sysEndianType(1bit), conf_params->szMode (1bit), conf_params->gzipMode (2bits), pwrType (2bits)
	buf = exe_params->optQuantMode;
	buf = (buf << 1) | dataEndianType;
	buf = (buf << 1) | sysEndianType;
	buf = (buf << 2) | params->szMode;
	
	int tmp = 0;
	switch(params->gzipMode)
	{
	case Z_BEST_SPEED:
		tmp = 0;
		break;
	case Z_DEFAULT_STRATEGY:
		tmp = 1;
		break;
	case Z_BEST_COMPRESSION:
		tmp = 2;
		break;
	}
	buf = (buf << 2) | tmp;
	//buf = (buf << 2) |  params->pwr_type; //deprecated
	result[0] = buf;
}

T
tickduan 已提交
309
void convertBytesToSZParams(unsigned char* bytes, sz_params* params, sz_exedata* pde_exe)
T
tickduan 已提交
310 311
{
	unsigned char flag1 = bytes[0];
T
tickduan 已提交
312
	pde_exe->optQuantMode = (flag1 & 0x40) >> 6;
T
tickduan 已提交
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
	dataEndianType = (flag1 & 0x20) >> 5;
	//sysEndianType = (flag1 & 0x10) >> 4;
	
	params->szMode = (flag1 & 0x0c) >> 2;
	
	int tmp = (flag1 & 0x03);
	switch(tmp)
	{
	case 0:
		params->gzipMode = Z_BEST_SPEED;
		break;
	case 1:
		params->gzipMode = Z_DEFAULT_STRATEGY;
		break;
	case 2:
		params->gzipMode = Z_BEST_COMPRESSION;
		break;
	}
}