All pastes #247229 Raw Edit

Rick

public text v1 · immutable
#247229 ·published 2006-11-13 04:27 UTC
rendered paste body
void convertDoubleToExtended(double value, unsigned char *newBytes)
{
	//extract fields
	unsigned char *oldBytes;

	oldBytes = (unsigned char *)(&value);
	
	unsigned char s = (unsigned char)(oldBytes[7] & 0x80);
	short e = (short)(((oldBytes[7] & 0x7F) << 4) | ((oldBytes[6] & 0xF0) >> 4));
	unsigned char j = 0x80;
	__int64 f = oldBytes[6] & 0xF;
	
	for (char i = 5; i >= 0; i--)
	{
		f <<= 8;
		f |= oldBytes[i];
	}
	
	//translate f
	f <<= 11;
	
	if (e == 0x7FF) //+infinity, -infinity or nan
	{
		e = 0x7FFF;
	}
	else if (e == 0 && f == 0) //zero
	{
		j = 0;
	}
	else //normal or subnormal
	{
		if (e == 0) //subnormal
		{
			f <<= 1;

			while (f > 0)
			{
				e--;
				f <<= 1;
			}
			
			f &= 0x7FFFFF;
		}
		
		e += (0x3FFF - 0x3FF); //translate e
	}
	
	memset(newBytes, 0, 10);
	memcpy(newBytes, &f, 8);
	newBytes[9] = (unsigned char)(s | (e >> 8));
	newBytes[8] = (unsigned char)(e & 0xFF);
	newBytes[7] = (unsigned char)(j | newBytes[7]);
}