コード例 #1
0
ファイル: Posit.cs プロジェクト: surtweig/kpi-posit-fpga
        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;
            }
        }
コード例 #2
0
ファイル: Posit.cs プロジェクト: surtweig/kpi-posit-fpga
        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, )
        }