void multiply(Bignum const & rhs, Bignum & result) const { product_t carry = 0; size_t digit = 0; for (;; ++digit) { //result.a[digit] = elem_t(carry % base); //carry /= base; product_t oldcarry = carry; carry /= base; result.a[digit] = elem_t(oldcarry - carry * base); bool found = false; for (size_t i = std::max<size_t>(digit < rhs.n ? 0 : digit - rhs.n + 1, this->n_max); i < n && i <= digit; ++i ) { product_t pval = result.a[digit] + a[i] * product_t(rhs.a[digit - i]); //carry += pval / base; //pval %= base; product_t quot = pval / base; carry += quot; pval -= quot * base; result.a[digit] = elem_t(pval); found = true; } if (!found) { break; } } for (; carry > 0; ++digit) { result.a[digit] = elem_t(carry % base); carry /= base; } //result.remove_leading_zeros(); }
inline int basic_dbgstreambuf<elem_t, traits_t>::sync() { if (ppos() == 0) //avoid unnecessary carriage return in dbgview return 0; *pptr() = elem_t(); OutputDebugStringX(pbase()); ppos(0); return 0; }
inline typename basic_dbgstreambuf<elem_t, traits_t>::int_type basic_dbgstreambuf<elem_t, traits_t>::overflow(int_type c_) { if (traits_t::eq_int_type(traits_t::eof(), c_)) return traits_t::eof(); const elem_t c = traits_t::to_char_type(c_); //sync(); //NOTE: No, we can't do this here, since dbgview will //break the text across lines when the auto-scroll //option is enabled. const ptrdiff_t oldlen = epptr() - pbase(); const ptrdiff_t newlen = oldlen ? 2 * oldlen : 1; if (elem_t* const newbuf = new (std::nothrow) elem_t[newlen + 1]) { const std::streamsize pos = ppos(); #if _MSC_VER >= 1400 const size_t size_in_bytes = newlen * sizeof(elem_t); traits_t::_Copy_s(newbuf, size_in_bytes, pbase(), pos); #else traits_t::copy(newbuf, pbase(), pos); #endif setp(newbuf, newbuf + pos, newbuf + newlen); delete[] m_buf; m_buf = newbuf; *pptr() = c; pbump(1); return traits_t::not_eof(c_); } if (oldlen) { sync(); *pbase() = c; pbump(1); } else { const elem_t str[2] = { c, elem_t() }; OutputDebugStringX(str); } return traits_t::not_eof(c_); }
inline std::streamsize basic_dbgstreambuf<elem_t, traits_t>::xsputn( const elem_t* str, std::streamsize n) { if (n <= plen()) { #if _MSC_VER >= 1400 const size_t size_in_bytes = plen() * sizeof(elem_t); traits_t::_Copy_s(pptr(), size_in_bytes, str, n); #else traits_t::copy(pptr(), str, n); #endif const int off = static_cast<int>(n); pbump(off); return n; } const std::streamsize pos = ppos(); const std::streamsize newlen = pos + n; if (elem_t* const newbuf = new (std::nothrow) elem_t[newlen + 1]) { #if _MSC_VER >= 1400 size_t size_in_bytes = newlen * sizeof(elem_t); traits_t::_Copy_s(newbuf, size_in_bytes, pbase(), pos); #else traits_t::copy(newbuf, pbase(), pos); #endif setp(newbuf, newbuf + pos, newbuf + newlen); delete[] m_buf; m_buf = newbuf; #if _MSC_VER >= 1400 size_in_bytes = plen() * sizeof(elem_t); traits_t::_Copy_s(pptr(), size_in_bytes, str, n); #else traits_t::copy(pptr(), str, n); #endif const int off = static_cast<int>(n); pbump(off); return n; } const ptrdiff_t oldlen_ = epptr() - pbase(); if (oldlen_ == 0) { elem_t buf[2]; buf[1] = elem_t(); for (std::streamsize i = 0; i < n; ++i) { buf[0] = *str++; OutputDebugStringX(buf); } return n; } std::streamsize nn = n; if (std::streamsize len = plen()) { #if _MSC_VER >= 1400 const size_t size_in_bytes = len * sizeof(elem_t); traits_t::_Copy_s(pptr(), size_in_bytes, str, len); #else traits_t::copy(pptr(), str, len); #endif const int off = static_cast<int>(len); pbump(off); str += len; nn -= len; } const std::streamsize oldlen = static_cast<std::streamsize>(oldlen_); #if _MSC_VER >= 1400 const size_t size_in_bytes = oldlen * sizeof(elem_t); #endif for (;;) { sync(); if (nn <= oldlen) { #if _MSC_VER >= 1400 traits_t::_Copy_s(pbase(), size_in_bytes, str, nn); #else traits_t::copy(pbase(), str, nn); #endif const int off = static_cast<int>(nn); pbump(off); return n; } #if _MSC_VER >= 1400 traits_t::_Copy_s(pbase(), size_in_bytes, str, oldlen); #else traits_t::copy(pbase(), str, oldlen); #endif const int off = static_cast<int>(oldlen); pbump(off); str += oldlen; nn -= oldlen; } }