示例#1
0
        void SubtractTimes(Bignum other, uint factor)
        {
#if DEBUG
            var a = new Bignum();
            var b = new Bignum();
            a.AssignBignum(this);
            b.AssignBignum(other);
            b.MultiplyByUInt32(factor);
            a.SubtractBignum(b);
#endif
            Debug.Assert(exponent_ <= other.exponent_);
            if (factor < 3)
            {
                for (int i = 0; i < factor; ++i)
                {
                    SubtractBignum(other);
                }

                return;
            }

            uint borrow        = 0;
            int  exponent_diff = other.exponent_ - exponent_;
            for (int i = 0; i < other.used_digits_; ++i)
            {
                ulong product    = factor * other.bigits_[i];
                ulong remove     = borrow + product;
                uint  difference = bigits_[i + exponent_diff] - (uint)(remove & kBigitMask);
                bigits_[i + exponent_diff] = difference & kBigitMask;
                borrow = (uint)((difference >> (kChunkSize - 1)) + (remove >> kBigitSize));
            }

            for (int i = other.used_digits_ + exponent_diff; i < used_digits_; ++i)
            {
                if (borrow == 0)
                {
                    return;
                }
                uint difference = bigits_[i] - borrow;
                bigits_[i] = difference & kBigitMask;
                borrow     = difference >> (kChunkSize - 1);
            }

            Clamp();

#if DEBUG
            Debug.Assert(Equal(a, this));
#endif
        }