On the special case of float2int

0

When I try to complete float2int with the following function

int float2int(unsigned uf) {
    int s_    = uf>>31;
    int exp_  = ((uf&0x7f800000)>>23)-127;
    int frac_ = (uf&0x007fffff)|0x00800000;
    if(!(uf&0x7fffffff)) return 0;

    if(exp_ > 31) return 0x80000000;
    if(exp_ < 0) return 0;

    if(exp_ > 23) frac_ <<= (exp_-23);
    else frac_ >>= (23-exp_);

    if(!((frac_>>31)^s_)) return frac_;
    else if(frac_>>31) return 0x80000000;
    else return ~frac_+1;
}

There is a case where out of range (including Nan or infinity) should return 0x80000000u. exp_>31 is the case,so why exp_ can not greater than 31?

floating-point
integer
asked on Stack Overflow Jul 17, 2020 by user13945431

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0