Example #1
0
        public static float ToFloat(int imm16)
        {
            var s = (imm16 >> 15) & 0x00000001; // Sign
            var e = (imm16 >> 10) & 0x0000001f; // Exponent
            var f = (imm16 >> 0) & 0x000003ff;  // Fraction

            // Need to handle 0x7C00 INF and 0xFC00 -INF?
            if (e == 0)
            {
                // Need to handle +-0 case f==0 or f=0x8000?
                if (f == 0)
                {
                    // Plus or minus zero
                    return(MathFloat.ReinterpretIntAsFloat(s << 31));
                }
                // Denormalized number -- renormalize it
                while ((f & 0x00000400) == 0)
                {
                    f <<= 1;
                    e  -= 1;
                }
                e += 1;
                f &= ~0x00000400;
            }
            else if (e == 31)
            {
                if (f == 0)
                {
                    // Inf
                    return(MathFloat.ReinterpretIntAsFloat((s << 31) | 0x7f800000));
                }
                // NaN
                return(MathFloat.ReinterpretIntAsFloat((s << 31) | 0x7f800000 | (f << 13)));
            }

            e = e + (127 - 15);
            f = f << 13;

            return(MathFloat.ReinterpretIntAsFloat((s << 31) | (e << 23) | f));
        }