Commit 387428c0 authored by Cerlane Leong's avatar Cerlane Leong 💬

Added support for bit mapping when converting p32 to double

parent 1605ccc9
/*============================================================================
This C source file is part of the SoftPosit Posit Arithmetic Package
......@@ -184,66 +183,49 @@ __float128 convertPX2ToQuad(posit_2_t a){
double convertP32ToDouble(posit32_t a){
union ui32_p32 uZ;
double d32;
uZ.p = a;
union ui32_p32 uA;
union ui64_double uZ;
uint_fast32_t uiA, tmp=0;
uint_fast64_t expA=0, uiZ, fracA=0;
bool signA=0, regSA;
int_fast32_t scale, kA=0;
if (uZ.ui==0){
return 0;
}
else if(uZ.ui==0x7FFFFFFF){ //maxpos
return 1.329227995784916e+36;
}
else if (uZ.ui==0x80000001){ //-maxpos
return -1.329227995784916e+36;
}
else if (uZ.ui == 0x80000000){
return INFINITY;
}
bool regS, sign;
uint_fast32_t reg, shift=2, frac, tmp;
int_fast32_t k=0;
int_fast8_t exp;
double fraction_max;
uA.p = pA;
uiA = uA.ui;
sign = signP32UI( uZ.ui );
if (sign)
uZ.ui = -uZ.ui & 0xFFFFFFFF;
regS = signregP32UI( uZ.ui );
if (uA.ui == 0)
return 0;
else if(uA.ui == 0x80000000)
return NAN;
tmp = (uZ.ui<<2)&0xFFFFFFFF;
if (regS){
while (tmp>>31){
k++;
shift++;
tmp= (tmp<<1) & 0xFFFFFFFF;
}
reg = k+1;
}
else{
k=-1;
while (!(tmp>>31)){
k--;
shift++;
tmp= (tmp<<1) & 0xFFFFFFFF;
signA = signP32UI( uiA );
if(signA) uiA = (-uiA & 0xFFFFFFFF);
regSA = signregP32UI(uiA);
tmp = (uiA<<2)&0xFFFFFFFF;
if (regSA){
while (tmp>>31){
kA++;
tmp= (tmp<<1) & 0xFFFFFFFF;
}
}
tmp&=0x7FFFFFFF;
reg =-k;
}
exp = tmp>>29;
frac = (tmp & 0x1FFFFFFF) >> shift;
(reg>28) ? (fraction_max=1) : (fraction_max = pow(2, 28-reg) ) ;
d32 = (double)( pow(16, k)* pow(2, exp) * (1+((double)frac/fraction_max)) );
if (sign)
d32 = -d32;
return d32;
else{
kA=-1;
while (!(tmp>>31)){
kA--;
tmp= (tmp<<1) & 0xFFFFFFFF;
}
tmp&=0x7FFFFFFF;
}
expA = tmp>>29; //to get 2 bits
fracA = (((uint64_t) tmp<<3) & 0xFFFFFFFF)<<20;
expA = (((kA<<2)+expA) + 1023) << 52;
uiZ = expA + fracA + (((uint64_t)signA&0x1)<<63);
uZ.ui = uiZ;
return uZ.d;
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment