[가수 변환식] (가수 범위= +1.0 > x > -1.0 )
아래 Code에서 xbits는 부호비트를 포함한 값이다.
-----------------------------------------------------------------------------
#include <windows.h>
#include <math.h>
#include <stdio.h>
#include <tchar.h>
DWORD ConvertMantissaToXBits (double value, DWORD xbits=24)
{
if (xbits>31 || xbits<12)
return 0;
static const DWORD MASK[33] =
{
/* 00 */ 0x00000000,
/* 01 */ 0x00000001,/* 02 */ 0x00000003,/* 03 */ 0x00000007,/* 04 */ 0x0000000f,
/* 05 */ 0x0000001f,/* 06 */ 0x0000003f,/* 07 */ 0x0000007f,/* 08 */ 0x000000ff,
/* 09 */ 0x000001ff,/* 10 */ 0x000003ff,/* 11 */ 0x000007ff,/* 12 */ 0x00000fff,
/* 13 */ 0x00001fff,/* 14 */ 0x00003fff,/* 15 */ 0x00007fff,/* 16 */ 0x0000ffff,
/* 17 */ 0x0001ffff,/* 18 */ 0x0003ffff,/* 19 */ 0x0007ffff,/* 20 */ 0x000fffff,
/* 21 */ 0x001fffff,/* 22 */ 0x003fffff,/* 23 */ 0x007fffff,/* 24 */ 0x00ffffff,
/* 25 */ 0x01ffffff,/* 26 */ 0x03ffffff,/* 27 */ 0x07ffffff,/* 28 */ 0x0fffffff,
/* 29 */ 0x1fffffff,/* 30 */ 0x3fffffff,/* 31 */ 0x7fffffff,/* 32 */ 0xffffffff
};
DWORD result = 0;
DWORD h = 0;
double m = 0.0;
if (value>=1.0 || value<-1.0)
return MASK[xbits] & (~(1<<(xbits-1)));
//m = pow (2.0, double(xbits-1)) * value;
m = (1<<(xbits-1)) * value;
h = DWORD (m);
result = h&MASK[xbits];
return result;
}
double ConvertXBitsToMantissa (DWORD value, DWORD xbits=24)
{
if (xbits>31 || xbits<12)
return 0.0;
double result = 0.0;
signed int n = 0;
n = value<<(32-xbits);
n = n >>(32-xbits); // no moved signal flag
//result = n/pow (2.0, double(xbits-1));
result = double(n)/(1<<(xbits-1));
return result;
}
int _tmain(int argc, _TCHAR* argv[])
{
DWORD bits = 24;
double v;
DWORD h;
double f;
int i;
double s;
v = -1;
s = 0.1;
for (i=0; i<22; i++)
{
h = ConvertMantissaToXBits(v,bits);
f = ConvertXBitsToMantissa(h,bits);
printf ("Mant->xBits(%+.3f)= 0x%08x : ", v, h);
printf ("xBits->Mant(0x%08x)= %+.3f\r\n", h, f);
v = v+s;
}
return 0;
}
-----------------------------------------------------------------------------
Mant->xBits(-1.000)= 0x00800000 : xBits->Mant(0x00800000)= -1.000
Mant->xBits(-0.900)= 0x008ccccd : xBits->Mant(0x008ccccd)= -0.900
Mant->xBits(-0.800)= 0x0099999a : xBits->Mant(0x0099999a)= -0.800
Mant->xBits(-0.700)= 0x00a66667 : xBits->Mant(0x00a66667)= -0.700
Mant->xBits(-0.600)= 0x00b33334 : xBits->Mant(0x00b33334)= -0.600
Mant->xBits(-0.500)= 0x00c00000 : xBits->Mant(0x00c00000)= -0.500
Mant->xBits(-0.400)= 0x00cccccd : xBits->Mant(0x00cccccd)= -0.400
Mant->xBits(-0.300)= 0x00d9999a : xBits->Mant(0x00d9999a)= -0.300
Mant->xBits(-0.200)= 0x00e66667 : xBits->Mant(0x00e66667)= -0.200
Mant->xBits(-0.100)= 0x00f33334 : xBits->Mant(0x00f33334)= -0.100
Mant->xBits(-0.000)= 0x00000000 : xBits->Mant(0x00000000)= +0.000
Mant->xBits(+0.100)= 0x000ccccc : xBits->Mant(0x000ccccc)= +0.100
Mant->xBits(+0.200)= 0x00199999 : xBits->Mant(0x00199999)= +0.200
Mant->xBits(+0.300)= 0x00266666 : xBits->Mant(0x00266666)= +0.300
Mant->xBits(+0.400)= 0x00333333 : xBits->Mant(0x00333333)= +0.400
Mant->xBits(+0.500)= 0x003fffff : xBits->Mant(0x003fffff)= +0.500
Mant->xBits(+0.600)= 0x004ccccc : xBits->Mant(0x004ccccc)= +0.600
Mant->xBits(+0.700)= 0x00599999 : xBits->Mant(0x00599999)= +0.700
Mant->xBits(+0.800)= 0x00666666 : xBits->Mant(0x00666666)= +0.800
Mant->xBits(+0.900)= 0x00733333 : xBits->Mant(0x00733333)= +0.900
Mant->xBits(+1.000)= 0x007fffff : xBits->Mant(0x007fffff)= +1.000
Mant->xBits(+1.100)= 0x007fffff : xBits->Mant(0x007fffff)= +1.000