Ejemplo n.º 1
0
        public static Posit maxPos(int size, int es)
        {
            Posit mp = new Posit(size, es);

            mp.regime = size - 2;
            return(mp);
        }
Ejemplo n.º 2
0
        public static Posit minPos(int size, int es)
        {
            Posit mp = new Posit(size, es);

            mp.regime = 2 - size;
            return(mp);
        }
Ejemplo n.º 3
0
        public static Posit Zero(int size, int es)
        {
            Posit zero = new Posit(size, es);

            zero.sign   = false;
            zero.regime = 1 - size;
            return(zero);
        }
Ejemplo n.º 4
0
        public static Posit Infinity(int size, int es)
        {
            Posit inf = new Posit(size, es);

            inf.sign   = true;
            inf.regime = 1 - size;
            return(inf);
        }
Ejemplo n.º 5
0
        public Posit BitStep(int step)
        {
            if (sign)
            {
                step = -step;
            }

            if (IsZero)
            {
                if (step > 0)
                {
                    return(Posit.minPos(Size, ES));
                }
                else if (step < 0)
                {
                    Posit nmp = minPos(Size, ES);
                    nmp.sign = true;
                    return(nmp);
                }
            }

            long newFraction = fraction + step;
            int  carry       = 0;

            long maxFraction = 1 << FractionSize;

            if (newFraction >= maxFraction)
            {
                newFraction -= maxFraction;
                carry        = 1;
            }
            else if (newFraction < 0)
            {
                newFraction += maxFraction;
                carry        = -1;
            }

            Posit r = new Posit(Size, ES);

            r.fraction = (uint)newFraction;
            r.sign     = sign;
            r.regime   = regime;

            /*
             * int newExponent = FullExponent + carry;
             * decomposeFullExponent(newExponent, ES, out r.exponent, out r.regime);
             */

            int newExponent = exponent + carry;
            int maxExponent = 1 << ExponentSize;

            if (newExponent >= maxExponent)
            {
                newExponent -= maxExponent;
                ++r.regime;
            }
            else if (newExponent < 0)
            {
                newExponent += maxExponent;
                --r.regime;
            }
            r.exponent = newExponent;

            if (r.regime > 0 && r.RegimeSize > r.Size - 1)
            {
                return(Posit.Infinity(r.size, r.es));
            }
            if (r.regime < 0 && r.RegimeSize > r.Size - 1)
            {
                return(Posit.Zero(r.size, r.es));
            }
            return(r);
        }
Ejemplo n.º 6
0
        private Posit add(Posit p)
        {
            int fullExp1 = FullExponent;
            int fullExp2 = p.FullExponent;

            uint fractionFirstBitMask1 = 1u << FractionSize;
            uint fractionFirstBitMask2 = 1u << p.FractionSize;

            int   maxFractionSize = Math.Max(FractionSize, p.FractionSize);
            ulong fraction1       = fractionFirstBitMask1 | fraction;   // (fraction >> 1);
            ulong fraction2       = fractionFirstBitMask2 | p.fraction; // (p.fraction >> 1);

            // normalizing fractions
            fraction1 <<= (maxFractionSize - FractionSize);
            fraction2 <<= (maxFractionSize - p.FractionSize);

            Posit r              = new Posit(Size, ES);
            int   resultFullExp  = 0;
            ulong resultFraction = 0;

            if (sign == p.sign)
            {
                if (fullExp1 > fullExp2)
                {
                    resultFullExp = fullExp1;
                    r.loss       += shiftRightLoss((uint)fraction2, fullExp1 - fullExp2);
                    r.inexact     = true;
                    fraction2   >>= fullExp1 - fullExp2;
                }
                else
                {
                    resultFullExp = fullExp2;
                    r.loss       += shiftRightLoss((uint)fraction1, fullExp2 - fullExp1);
                    r.inexact     = true;
                    fraction1   >>= fullExp2 - fullExp1;
                }

                resultFraction = (ulong)(fraction1 + fraction2);
                if (resultFraction >> (maxFractionSize + 1) > 0)
                {
                    r.loss          += shiftRightLoss((uint)resultFraction, 1);
                    r.inexact        = true;
                    resultFraction >>= 1;
                    ++resultFullExp;
                }

                resultFraction = ((~(1u << maxFractionSize)) & resultFraction);

                r.sign     = sign;
                r.fraction = (uint)resultFraction;
                decomposeFullExponent(resultFullExp, ES, out r.exponent, out r.regime);
                if (maxFractionSize > r.FractionSize)
                {
                    r.loss          += shiftRightLoss((uint)resultFraction, maxFractionSize - r.FractionSize);
                    r.inexact        = true;
                    resultFraction >>= (maxFractionSize - r.FractionSize);
                }
                else if (r.FractionSize > maxFractionSize)
                {
                    resultFraction <<= (r.FractionSize - maxFractionSize);
                }

                r.fraction = (uint)resultFraction;
            }
            else
            {
                if (fullExp1 > fullExp2 || ((fullExp1 == fullExp2 && fraction1 > fraction2)))
                {
                    resultFullExp  = fullExp1;
                    r.sign         = sign;
                    fraction2    >>= fullExp1 - fullExp2;
                    resultFraction = (ulong)(fraction1 - fraction2);
                }
                else
                {
                    r.sign         = !sign;
                    resultFullExp  = fullExp2;
                    fraction1    >>= fullExp2 - fullExp1;
                    resultFraction = (ulong)(fraction2 - fraction1);
                }

                if (resultFraction == 0)
                {
                    r.sign        = false;
                    resultFullExp = 0;
                }

                int shift = clz((uint)resultFraction, maxFractionSize + 1);
                //if (shift > 0)
                {
                    resultFullExp   -= shift;
                    resultFraction <<= shift;
                }
                resultFraction = ((~(1u << maxFractionSize)) & resultFraction);

                decomposeFullExponent(resultFullExp, ES, out r.exponent, out r.regime);
                if (maxFractionSize > r.FractionSize)
                {
                    resultFraction >>= (maxFractionSize - r.FractionSize);
                }
                else if (r.FractionSize > maxFractionSize)
                {
                    resultFraction <<= (r.FractionSize - maxFractionSize);
                }

                r.fraction = (uint)resultFraction;
            }

            return(r);
        }