Ejemplo n.º 1
0
        public void Decode(BitLattice bitLattice)
        {
            sign = bitLattice.GetBool(SignField);
            if (bitLattice.GetBool(RegimeField))
            {
                regime = bitLattice.GetFieldLength(RegimeField) - 1;
            }
            else
            {
                regime = -bitLattice.GetFieldLength(RegimeField);
            }

            if (bitLattice.HasField(ExponentField))
            {
                exponent = (int)bitLattice.GetUint(ExponentField);
            }
            else
            {
                exponent = 0;
            }

            if (bitLattice.HasField(FractionField))
            {
                fraction = bitLattice.GetUint(FractionField);
            }
            else
            {
                fraction = 0;
            }
        }
Ejemplo n.º 2
0
        private void fromFloat(float number)
        {
            inexact = false;
            byte[]     data  = BitConverter.GetBytes(number);
            BitLattice fbits = new BitLattice(data);

            // IEEE 754
            int floatExpSize  = 8;
            int floatFracSize = 23;

            fbits.AddField(SignField, fbits.Size - 1, 1);
            fbits.AddField(ExponentField, fbits.Size - floatExpSize - 1, floatExpSize);
            fbits.AddField(FractionField, 0, floatFracSize);

            //int floatSign = fbits.GetBool(SignField) ? 1 : -1;
            int  floatExpBias  = 1 - (2 << (fbits.GetFieldLength(ExponentField) - 2)); // -127
            int  floatExp      = (int)fbits.GetUint(ExponentField) + floatExpBias;
            uint floatFraction = fbits.GetUint(FractionField);

            sign = fbits.GetBool(SignField);

            int twoPowES = 1 << es;

            if (floatExp > 0)
            {
                regime = floatExp / twoPowES;
            }
            else if (floatExp < 0)
            {
                regime = -(1 + (-floatExp - 1) / twoPowES);//-((-floatExp + 1) / twoPowES);
            }
            exponent = floatExp - regime * twoPowES;
            fraction = floatFraction;

            int fracResize = FractionSize - floatFracSize;

            if (fracResize < 0)
            {
                inexact    = true;
                fraction >>= (-fracResize);
            }
            else if (fracResize > 0)
            {
                fraction <<= fracResize;
            }

            /*
             * while (fraction > (1 << FractionSize))
             * {
             *  fraction >>= 1;
             *  inexact = true;
             * }
             */

            //BitLattice pbits = new BitLattice(fbits.Size);

            //pbits.AddField(SignField, fbits.Size - 1, 1);
            //pbits.AddField(RegimeField, fbits.Size - 2, )
        }
Ejemplo n.º 3
0
        public float ToFloat(out BitLattice fbits)
        {
            inexact = false;
            fbits   = new BitLattice(32);

            // IEEE 754
            int floatExpSize  = 8;
            int floatFracSize = 23;

            fbits.AddField(SignField, fbits.Size - 1, 1);
            fbits.AddField(ExponentField, fbits.Size - floatExpSize - 1, floatExpSize);
            fbits.AddField(FractionField, 0, floatFracSize);

            int floatExpBias = 1 - (2 << (fbits.GetFieldLength(ExponentField) - 2)); // -127
            int floatExp     = FullExponent - floatExpBias;

            if (floatExp > (1 << floatExpSize) - 1)
            {
                floatExp = (1 << floatExpSize) - 1;
                inexact  = true;
            }

            fbits.SetBool(SignField, sign);
            fbits.SetUint(ExponentField, (uint)floatExp);

            int  fracResize = floatFracSize - FractionSize;
            uint floatFrac  = fraction;

            if (fracResize < 0)
            {
                inexact     = true;
                floatFrac >>= (-fracResize);
            }
            else if (fracResize > 0)
            {
                floatFrac <<= fracResize;
            }

            fbits.SetUint(FractionField, (uint)floatFrac);

            return(BitConverter.ToSingle(fbits.ToBytes()));
        }