예제 #1
0
        static void bignum_divide_unsigned_small_denominator(Bignum numerator, BignumDigit denominator, out Bignum quotient, out Bignum remainder, bool qsign, bool rsign)
        {
            Bignum      q = numerator.NewSign(qsign);
            BignumDigit r = q.DestructiveScaleDown(denominator);

            quotient  = q.Trim();
            remainder = new Bignum(rsign, r);
        }
예제 #2
0
        static Bignum MultiplyUnsigned(Bignum x, Bignum y, bool negative)
        {
            if (y.Length > x.Length)
            {
                Bignum z = x;
                x = y;
                y = z;
            }
            BignumDigit carry;
            BignumDigit y_digit;
            long        y_digit_low;
            long        y_digit_high;
            BignumDigit x_digit;
            long        x_digit_low;
            long        x_digit_high;
            BignumDigit product_low;
            BignumDigit product_high;
            int         scan_r;
            int         scan_y;
            int         x_length = x.Length;
            int         y_length = y.Length;
            Bignum      r        = new Bignum(negative, x_length + y_length);
            int         scan_x   = 0;
            int         end_x    = x_length;
            int         start_y  = 0;
            int         end_y    = y_length;
            int         start_r  = 0;

            while (scan_x < end_x)
            {
                x_digit      = x [scan_x++];
                x_digit_low  = x_digit.Low;
                x_digit_high = x_digit.High;
                carry        = (BignumDigit)0L;
                scan_y       = start_y;
                scan_r       = start_r++;
                while (scan_y < end_y)
                {
                    y_digit      = y [scan_y++];
                    y_digit_low  = y_digit.Low;
                    y_digit_high = y_digit.High;
                    product_low  = (BignumDigit)(r [scan_r].ToLong() +
                                                 (x_digit_low * y_digit_low) +
                                                 carry.Low);
                    product_high = (BignumDigit)((x_digit_high * y_digit_low) +
                                                 (x_digit_low * y_digit_high) +
                                                 product_low.High +
                                                 carry.High);
                    r [scan_r++] = new BignumDigit(product_high.Low, product_low.Low);
                    carry        = (BignumDigit)((x_digit_high * y_digit_high) +
                                                 product_high.High);
                }
                r [scan_r] = (BignumDigit)(r [scan_r].ToLong() + carry.ToLong());
            }
            return(r.Trim());
        }
예제 #3
0
        static Bignum MultiplyUnsignedSmallFactor(Bignum old, long digit, bool sign)
        {
            int    length_x = old.Length;
            Bignum p        = new Bignum(sign, length_x + 1);

            old.DestructiveCopy(p);
            p [length_x] = (BignumDigit)0L;
            bignum_destructive_scale_up(p, digit);
            return(p.Trim());
        }
예제 #4
0
        static void bignum_divide_unsigned_large_denominator(Bignum numerator, Bignum denominator, out Bignum quotient, out Bignum remainder, bool qsign, bool rsign)
        {
            int    length_n = numerator.Length + 1;
            int    length_d = denominator.Length;
            Bignum q        = new Bignum(qsign, length_n - length_d);
            Bignum u        = new Bignum(rsign, length_n);

            int shift = 0;

            if (!(length_d > 1))
            {
                throw new NotImplementedException();
            }

            {
                BignumDigit v1 = denominator[length_d - 1];
                while (v1 < (BIGNUM_RADIX / 2))
                {
                    v1   <<= 1;
                    shift += 1;
                }
            }
            if (shift == 0)
            {
                numerator.DestructiveCopy(u);      // bignum_destructive_copy (numerator, u);
                u [length_n - 1] = (BignumDigit)0; // (BIGNUM_REF (u, (length_n - 1))) = 0;
                throw new NotImplementedException();
                // bignum_divide_unsigned_normalized (u, denominator, q);
            }
            else
            {
                Bignum v = new Bignum(false, length_d);         // (bignum_allocate (length_d, 0));
                numerator.DestructiveNormalization(u, shift);   //bignum_destructive_normalization (numerator, u, shift);
                denominator.DestructiveNormalization(v, shift); //bignum_destructive_normalization (denominator, v, shift);
                bignum_divide_unsigned_normalized(u, v, q);
                //bignum_divide_unsigned_normalized (u, v, q);
                //BIGNUM_DEALLOCATE (v);
                //if (remainder != ((bignum_type*) 0))
                //    bignum_destructive_unnormalization (u, shift);
            }
            quotient  = q.Trim();    // (bignum_trim (q));
            remainder = u.Trim();    //(bignum_trim (u));
            //else
            //    BIGNUM_DEALLOCATE (u);
            return;
        }
예제 #5
0
        static void bignum_divide_unsigned_large_denominator (Bignum numerator, Bignum denominator, out Bignum quotient, out Bignum remainder, bool qsign, bool rsign)
        {
            int length_n = numerator.Length + 1;
            int length_d = denominator.Length;
            Bignum q = new Bignum (qsign, length_n - length_d);
            Bignum u = new Bignum (rsign, length_n);

            int shift = 0;
            if (!(length_d > 1)) throw new NotImplementedException ();

            {
                BignumDigit v1 = denominator[length_d - 1];
                while (v1 < (BIGNUM_RADIX / 2)) {
                    v1 <<= 1;
                    shift += 1;
                }
            }
            if (shift == 0) {
                numerator.DestructiveCopy (u); // bignum_destructive_copy (numerator, u);
                u [length_n - 1] = (BignumDigit) 0; // (BIGNUM_REF (u, (length_n - 1))) = 0;
                throw new NotImplementedException ();
                // bignum_divide_unsigned_normalized (u, denominator, q);
            }
            else {
                Bignum v = new Bignum (false, length_d); // (bignum_allocate (length_d, 0));
                numerator.DestructiveNormalization (u, shift);   //bignum_destructive_normalization (numerator, u, shift);
                denominator.DestructiveNormalization (v, shift); //bignum_destructive_normalization (denominator, v, shift);
                bignum_divide_unsigned_normalized (u, v, q);
                //bignum_divide_unsigned_normalized (u, v, q);
                //BIGNUM_DEALLOCATE (v);
                //if (remainder != ((bignum_type*) 0))
                //    bignum_destructive_unnormalization (u, shift);
            }
                quotient = q.Trim ();// (bignum_trim (q));
                remainder = u.Trim(); //(bignum_trim (u));
            //else
            //    BIGNUM_DEALLOCATE (u);
            return;
        }
예제 #6
0
 static Bignum MultiplyUnsigned (Bignum x, Bignum y, bool negative)
 {
     if (y.Length > x.Length)
     {
         Bignum z = x;
         x = y;
         y = z;
     }
     BignumDigit carry;
     BignumDigit y_digit;
     long y_digit_low;
     long y_digit_high;
     BignumDigit x_digit;
     long x_digit_low;
     long x_digit_high;
     BignumDigit product_low;
     BignumDigit product_high;
     int scan_r;
     int scan_y;
     int x_length = x.Length;
     int y_length = y.Length;
     Bignum r = new Bignum (negative, x_length + y_length);
     int scan_x = 0;
     int end_x = x_length;
     int start_y = 0;
     int end_y = y_length;
     int start_r = 0;
     while (scan_x < end_x)
     {
         x_digit = x [scan_x++];
         x_digit_low = x_digit.Low;
         x_digit_high = x_digit.High;
         carry = (BignumDigit) 0L;
         scan_y = start_y;
         scan_r = start_r++;
         while (scan_y < end_y)
         {
             y_digit = y [scan_y++];
             y_digit_low = y_digit.Low;
             y_digit_high = y_digit.High;
             product_low = (BignumDigit) (r [scan_r].ToLong () +
                           (x_digit_low * y_digit_low) +
                           carry.Low);
             product_high = (BignumDigit) ((x_digit_high * y_digit_low) +
                            (x_digit_low * y_digit_high) +
                            product_low.High +
                            carry.High);
             r [scan_r++] = new BignumDigit (product_high.Low, product_low.Low);
             carry = (BignumDigit) ((x_digit_high * y_digit_high) +
                     product_high.High);
         }
         r [scan_r] = (BignumDigit) (r [scan_r].ToLong () + carry.ToLong ());
     }
     return r.Trim ();
 }
예제 #7
0
 static Bignum MultiplyUnsignedSmallFactor (Bignum old, long digit, bool sign)
 {
     int length_x = old.Length;
     Bignum p = new Bignum (sign, length_x + 1);
     old.DestructiveCopy (p);
     p [length_x] = (BignumDigit) 0L;
     bignum_destructive_scale_up (p, digit);
     return p.Trim ();
 }