示例#1
0
        /// <summary>
        /// Converts a half precision floating point number to a single precision floating point number
        /// </summary>
        /// <param name="value">16bit Int representation of the float</param>
        /// <returns>Resulting Float</returns>
        public static float ToFloat(ushort value)
        {
            // Define bit values
            FloatBits v, s = new FloatBits();

            // Set the initial values (if we don't do this, .NET considers them unassigned)
            v.SignedInteger   = 0;
            v.UnsignedInteger = 0;
            v.Float           = 0;
            s.SignedInteger   = 0;
            s.UnsignedInteger = 0;
            s.Float           = 0;
            // Assign the initial value given
            v.UnsignedInteger = value;
            // Calculate sign
            int sign = v.SignedInteger & SignC;

            v.SignedInteger ^= sign;
            sign           <<= ShiftSign;
            v.SignedInteger ^= ((v.SignedInteger + MinD) ^ v.SignedInteger) & -(v.SignedInteger > SubC ? 1 : 0);
            v.SignedInteger ^= ((v.SignedInteger + MaxD) ^ v.SignedInteger) & -(v.SignedInteger > MaxC ? 1 : 0);
            // Inverse Subnormals
            s.SignedInteger = MulC;
            s.Float        *= v.SignedInteger;
            int mask = -(NorC > v.SignedInteger ? 1 : 0);

            v.SignedInteger <<= Shift;
            v.SignedInteger  ^= (s.SignedInteger ^ v.SignedInteger) & mask;
            v.SignedInteger  |= sign;
            // Return the expanded result
            return(v.Float);
        }
示例#2
0
        public static float Sqrt(float f)
        {
            FloatBits fb = FloatBitsConverter;

            fb.Value = f;
            fb.Bits  = ApproxSqrtMagicBits - (fb.Bits >> 1);
            return(f * fb.Value * (1.5f - 0.5f * f * fb.Value * fb.Value));
        }
示例#3
0
        public static float SqrtRough(float f)
        {
            if (f <= 0.0f)
            {
                return(0.0f);
            }
            FloatBits fb = FloatBitsConverter;

            fb.Value = f;
            fb.Bits  = ((fb.Bits - ApproxSqrtBit1) >> 1) + ApproxSqrtBit2;
            return(fb.Value);
        }
示例#4
0
        internal static float ClipValue(float value, ref bool clipped)
        {
            FloatBits floatBits = default(FloatBits);

            floatBits.Bits  = 0u;
            floatBits.Float = value;
            if ((floatBits.Bits & int.MaxValue) > 1065353215)
            {
                clipped        = true;
                floatBits.Bits = (uint)(0x3F7FFFFF | ((int)floatBits.Bits & int.MinValue));
            }
            return(floatBits.Float);
        }