Exemplo n.º 1
0
        /** Return this raised to an integer power.
         * Implemented by repeated squaring and multiplication.
         * If y < 0, returns div_inv of the result. */
        public virtual Numeric power(IntNum y)
        {
            if (y.isNegative())
            {
                return(power(IntNum.neg(y)).div_inv());
            }
            Numeric pow2 = this;
            Numeric r    = null;

            for (;;)  // for (i = 0;  ; i++)
            {
                // pow2 == x**(2**i)
                // prod = x**(sum(j=0..i-1, (y>>j)&1))
                if (y.isOdd())
                {
                    r = r == null ? pow2 : r.mul(pow2);       // r *= pow2
                }
                y = IntNum.shift(y, -1);
                if (y.isZero())
                {
                    break;
                }
                // pow2 *= pow2;
                pow2 = pow2.mul(pow2);
            }
            return(r == null?mul_ident() : r);
        }
Exemplo n.º 2
0
        /** Return the logical (bit-wise) "and" of two IntNums. */
        public static IntNum and(IntNum x, IntNum y)
        {
            if (y.words == null)
            {
                return(and(x, y.ival));
            }
            else if (x.words == null)
            {
                return(and(y, x.ival));
            }
            if (x.ival < y.ival)
            {
                IntNum temp = x;  x = y;  y = temp;
            }
            int i;
            int len = y.isNegative() ? x.ival : y.ival;

            int[] words = new int[len];
            for (i = 0; i < y.ival; i++)
            {
                words[i] = x.words[i] & y.words[i];
            }
            for ( ; i < len; i++)
            {
                words[i] = x.words[i];
            }
            return(IntNum.make(words, len));
        }
Exemplo n.º 3
0
        public static RatNum make(IntNum num, IntNum den)
        {
            IntNum g = IntNum.gcd(num, den);

            if (den.isNegative())
            {
                g = IntNum.neg(g);
            }
            if (!g.isOne())
            {
                num = IntNum.quotient(num, g);
                den = IntNum.quotient(den, g);
            }
            return(den.isOne() ? (RatNum)num : (RatNum)(new IntFraction(num, den)));
        }
Exemplo n.º 4
0
Arquivo: BitOps.cs Projeto: fronx/ioke
 /** Count one bits in an IntNum.
  * If argument is negative, count zero bits instead. */
 public static int bitCount(IntNum x)
 {
     int i, x_len;
     int[] x_words = x.words;
     if (x_words == null)
         {
             x_len = 1;
             i = bitCount (x.ival);
         }
     else
         {
             x_len = x.ival;
             i = bitCount (x_words, x_len);
         }
     return x.isNegative () ? x_len * 32 - i : i;
 }
Exemplo n.º 5
0
        /** Count one bits in an IntNum.
         * If argument is negative, count zero bits instead. */
        public static int bitCount(IntNum x)
        {
            int i, x_len;

            int[] x_words = x.words;
            if (x_words == null)
            {
                x_len = 1;
                i     = bitCount(x.ival);
            }
            else
            {
                x_len = x.ival;
                i     = bitCount(x_words, x_len);
            }
            return(x.isNegative() ? x_len * 32 - i : i);
        }
Exemplo n.º 6
0
Arquivo: BitOps.cs Projeto: fronx/ioke
 /** Return the logical (bit-wise) "and" of two IntNums. */
 public static IntNum and(IntNum x, IntNum y)
 {
     if (y.words == null)
         return and (x, y.ival);
     else if (x.words == null)
         return and (y, x.ival);
     if (x.ival < y.ival)
         {
             IntNum temp = x;  x = y;  y = temp;
         }
     int i;
     int len = y.isNegative () ? x.ival : y.ival;
     int[] words = new int[len];
     for (i = 0;  i < y.ival;  i++)
         words[i] = x.words[i] & y.words[i];
     for ( ; i < len;  i++)
         words[i] = x.words[i];
     return IntNum.make (words, len);
 }
Exemplo n.º 7
0
 /** Return true iff two IntNums have any true bits in common. */
 public static bool test(IntNum x, IntNum y)
 {
     if (y.words == null)
     {
         return(test(x, y.ival));
     }
     else if (x.words == null)
     {
         return(test(y, x.ival));
     }
     if (x.ival < y.ival)
     {
         IntNum temp = x;  x = y;  y = temp;
     }
     for (int i = 0; i < y.ival; i++)
     {
         if ((x.words[i] & y.words[i]) != 0)
         {
             return(true);
         }
     }
     return(y.isNegative());
 }
Exemplo n.º 8
0
        /** Extract a bit-field as an unsigned integer. */
        public static IntNum extract(IntNum x, int startBit, int endBit)
        {
            //System.err.print("extract([");  if (x.words!=null) MPN.dprint(x.words);
            //System.err.println (","+x.ival+"], start:"+startBit+", end:"+endBit);
            if (endBit < 32)
            {
                int word0 = x.words == null ? x.ival : x.words[0];
                return(IntNum.make((word0 & ~((-1) << endBit)) >> startBit));
            }
            int x_len;

            if (x.words == null)
            {
                if (x.ival >= 0)
                {
                    return(IntNum.make(startBit >= 31 ? 0 : (x.ival >> startBit)));
                }
                x_len = 1;
            }
            else
            {
                x_len = x.ival;
            }
            bool neg = x.isNegative();

            if (endBit > 32 * x_len)
            {
                endBit = 32 * x_len;
                if (!neg && startBit == 0)
                {
                    return(x);
                }
            }
            else
            {
                x_len = (endBit + 31) >> 5;
            }
            int length = endBit - startBit;

            if (length < 64)
            {
                long l;
                if (x.words == null)
                {
                    l = x.ival >> (startBit >= 32 ? 31 : startBit);
                }
                else
                {
                    l = MPN.rshift_long(x.words, x_len, startBit);
                }
                return(IntNum.make(l & ~((-1L) << length)));
            }
            int startWord = startBit >> 5;
            // Allocate a work buffer, which has to be large enough for the result
            // AND large enough for all words we use from x (including possible
            // partial words at both ends).
            int buf_len = (endBit >> 5) + 1 - startWord;

            int[] buf = new int[buf_len];
            if (x.words == null)  // x < 0.
            {
                buf[0] = startBit >= 32 ? -1 : (x.ival >> startBit);
            }
            else
            {
                x_len    -= startWord;
                startBit &= 31;
                MPN.rshift0(buf, x.words, startWord, x_len, startBit);
            }
            x_len       = length >> 5;
            buf[x_len] &= ~((-1) << length);
            return(IntNum.make(buf, x_len + 1));
        }
Exemplo n.º 9
0
 public override bool isNegative()
 {
     return(num.isNegative());
 }
Exemplo n.º 10
0
Arquivo: BitOps.cs Projeto: fronx/ioke
 /** Return true iff two IntNums have any true bits in common. */
 public static bool test(IntNum x, IntNum y)
 {
     if (y.words == null)
         return test (x, y.ival);
     else if (x.words == null)
         return test (y, x.ival);
     if (x.ival < y.ival)
         {
             IntNum temp = x;  x = y;  y = temp;
         }
     for (int i = 0;  i < y.ival;  i++)
         {
             if ((x.words[i] & y.words[i]) != 0)
                 return true;
         }
     return y.isNegative ();
 }
Exemplo n.º 11
0
Arquivo: BitOps.cs Projeto: fronx/ioke
 /** Extract a bit-field as an unsigned integer. */
 public static IntNum extract(IntNum x, int startBit, int endBit)
 {
     //System.err.print("extract([");  if (x.words!=null) MPN.dprint(x.words);
     //System.err.println (","+x.ival+"], start:"+startBit+", end:"+endBit);
     if (endBit < 32)
         {
             int word0 = x.words == null ? x.ival : x.words[0];
             return IntNum.make ((word0 & ~((-1) << endBit)) >> startBit);
         }
     int x_len;
     if (x.words == null)
         {
             if (x.ival >= 0)
                 return IntNum.make (startBit >= 31 ? 0 : (x.ival >> startBit));
             x_len = 1;
         }
     else
         x_len = x.ival;
     bool neg = x.isNegative ();
     if (endBit > 32 * x_len)
         {
             endBit = 32 * x_len;
             if (!neg && startBit == 0)
                 return x;
         }
     else
         x_len = (endBit + 31) >> 5;
     int length = endBit - startBit;
     if (length < 64)
         {
             long l;
             if (x.words == null)
                 l = x.ival >> (startBit >= 32 ? 31 : startBit);
             else
                 l = MPN.rshift_long (x.words, x_len, startBit);
             return IntNum.make (l & ~((-1L) << length));
         }
     int startWord = startBit >> 5;
     // Allocate a work buffer, which has to be large enough for the result
     // AND large enough for all words we use from x (including possible
     // partial words at both ends).
     int buf_len = (endBit >> 5) + 1 - startWord;
     int[] buf = new int[buf_len];
     if (x.words == null)  // x < 0.
         buf[0] = startBit >= 32 ? -1 : (x.ival >> startBit);
     else
         {
             x_len -= startWord;
             startBit &= 31;
             MPN.rshift0 (buf, x.words, startWord, x_len, startBit);
         }
     x_len = length >> 5;
     buf[x_len] &= ~((-1) << length);
     return IntNum.make (buf, x_len + 1);
 }