fp& fp::normalize(int e, long fr)
{ // set this object to normalized value of
// exponent e and fraction fr
if (fr == 0)
{ // handle zero specially
exp = 0;
frac = 0;
return *this;
}
is_neg = fr < 0;
fr = abs(fr);
if ((ONE << P) <= fr)
{ // handle fraction overflow
fr >>= 1;
++e;
}
else
while (fr < (MIN_NORM << P))
{ // scale left
fr <<= 1;
--e;
}
bool inx = round(fr);
if ((ONE << P) <= fr)
{ // handle fraction overflow
fr >>= 1;
++e;
}
if (e < MIN_EXP)
*this = inx ? exception(exu, fp(e - BIAS + 192,
fr >> P, is_neg))
: fp(0 - BIAS, fr >> (P - e), is_neg);
else if (MAX_EXP <= e)
*this = exception(exo, fp(e - BIAS - 192, fr >> P, is_neg));
else if (inx)
*this = exception(ine, fp(e - BIAS, fr >> P, is_neg));
else
*this = fp(e - BIAS, fr >> P, is_neg);
return *this;
}