📄 sc_unsigned.cpp
字号:
const sc_unsigned& sc_unsigned::operator*=(const sc_int_base& v){ return operator*=((int64) v); }const sc_unsigned& sc_unsigned::operator/=(const sc_int_base& v){ return operator/=((int64) v); }const sc_unsigned& sc_unsigned::operator%=(const sc_int_base& v){ return operator%=((int64) v); }const sc_unsigned& sc_unsigned::operator&=(const sc_int_base& v){ return operator&=((int64) v); }const sc_unsigned& sc_unsigned::operator|=(const sc_int_base& v){ return operator|=((int64) v); }const sc_unsigned& sc_unsigned::operator^=(const sc_int_base& v){ return operator^=((int64) v); }sc_unsigned operator<<(const sc_unsigned& u, const sc_int_base& v){ return operator<<(u, (int64) v); }const sc_unsigned& sc_unsigned::operator<<=(const sc_int_base& v){ return operator<<=((int64) v); }sc_unsigned operator>>(const sc_unsigned& u, const sc_int_base& v){ return operator>>(u, (int64) v); }const sc_unsigned& sc_unsigned::operator>>=(const sc_int_base& v){ return operator>>=((int64) v); }bool operator==(const sc_unsigned& u, const sc_int_base& v){ return operator==(u, (int64) v); }bool operator==(const sc_int_base& u, const sc_unsigned& v) { return operator==((int64) u, v); }bool operator!=(const sc_unsigned& u, const sc_int_base& v){ return operator!=(u, (int64) v); }bool operator!=(const sc_int_base& u, const sc_unsigned& v) { return operator!=((int64) u, v); }bool operator<(const sc_unsigned& u, const sc_int_base& v){ return operator<(u, (int64) v); }bool operator<(const sc_int_base& u, const sc_unsigned& v) { return operator<((int64) u, v); }bool operator<=(const sc_unsigned& u, const sc_int_base& v){ return operator<=(u, (int64) v); }bool operator<=(const sc_int_base& u, const sc_unsigned& v) { return operator<=((int64) u, v); }bool operator>(const sc_unsigned& u, const sc_int_base& v){ return operator>(u, (int64) v); }bool operator>(const sc_int_base& u, const sc_unsigned& v) { return operator>((int64) u, v); }bool operator>=(const sc_unsigned& u, const sc_int_base& v){ return operator>=(u, (int64) v); }bool operator>=(const sc_int_base& u, const sc_unsigned& v) { return operator>=((int64) u, v); }// ----------------------------------------------------------------------------// SECTION: Interfacing with sc_uint_base// ----------------------------------------------------------------------------const sc_unsigned& sc_unsigned::operator= (const sc_uint_base& v){ return operator=((uint64) v); }sc_unsigned operator+(const sc_unsigned& u, const sc_uint_base& v) { return operator+(u, (uint64) v); }sc_unsigned operator+(const sc_uint_base& u, const sc_unsigned& v) { return operator+((uint64) u, v); }const sc_unsigned& sc_unsigned::operator+=(const sc_uint_base& v){ return operator+=((uint64) v); }const sc_unsigned& sc_unsigned::operator-=(const sc_uint_base& v){ return operator-=((uint64) v); }sc_unsigned operator*(const sc_unsigned& u, const sc_uint_base& v) { return operator*(u, (uint64) v); }sc_unsigned operator*(const sc_uint_base& u, const sc_unsigned& v) { return operator*((uint64) u, v); }const sc_unsigned& sc_unsigned::operator*=(const sc_uint_base& v){ return operator*=((uint64) v); }sc_unsigned operator/(const sc_unsigned& u, const sc_uint_base& v) { return operator/(u, (uint64) v); }sc_unsigned operator/(const sc_uint_base& u, const sc_unsigned& v) { return operator/((uint64) u, v); }const sc_unsigned& sc_unsigned::operator/=(const sc_uint_base& v){ return operator/=((uint64) v); }sc_unsigned operator%(const sc_unsigned& u, const sc_uint_base& v) { return operator%(u, (uint64) v); }sc_unsigned operator%(const sc_uint_base& u, const sc_unsigned& v) { return operator%((uint64) u, v); }const sc_unsigned& sc_unsigned::operator%=(const sc_uint_base& v){ return operator%=((uint64) v); }sc_unsigned operator&(const sc_unsigned& u, const sc_uint_base& v) { return operator&(u, (uint64) v); }sc_unsigned operator&(const sc_uint_base& u, const sc_unsigned& v) { return operator&((uint64) u, v); }const sc_unsigned& sc_unsigned::operator&=(const sc_uint_base& v){ return operator&=((uint64) v); }sc_unsigned operator|(const sc_unsigned& u, const sc_uint_base& v) { return operator|(u, (uint64) v); }sc_unsigned operator|(const sc_uint_base& u, const sc_unsigned& v) { return operator|((uint64) u, v); }const sc_unsigned& sc_unsigned::operator|=(const sc_uint_base& v){ return operator|=((uint64) v); }sc_unsigned operator^(const sc_unsigned& u, const sc_uint_base& v) { return operator^(u, (uint64) v); }sc_unsigned operator^(const sc_uint_base& u, const sc_unsigned& v) { return operator^((uint64) u, v); }const sc_unsigned& sc_unsigned::operator^=(const sc_uint_base& v){ return operator^=((uint64) v); }sc_unsigned operator<<(const sc_unsigned& u, const sc_uint_base& v){ return operator<<(u, (uint64) v); }const sc_unsigned& sc_unsigned::operator<<=(const sc_uint_base& v){ return operator<<=((uint64) v); }sc_unsigned operator>>(const sc_unsigned& u, const sc_uint_base& v){ return operator>>(u, (uint64) v); }const sc_unsigned& sc_unsigned::operator>>=(const sc_uint_base& v){ return operator>>=((uint64) v); }bool operator==(const sc_unsigned& u, const sc_uint_base& v){ return operator==(u, (uint64) v); }bool operator==(const sc_uint_base& u, const sc_unsigned& v) { return operator==((uint64) u, v); }bool operator!=(const sc_unsigned& u, const sc_uint_base& v){ return operator!=(u, (uint64) v); }bool operator!=(const sc_uint_base& u, const sc_unsigned& v) { return operator!=((uint64) u, v); }bool operator<(const sc_unsigned& u, const sc_uint_base& v){ return operator<(u, (uint64) v); }bool operator<(const sc_uint_base& u, const sc_unsigned& v) { return operator<((uint64) u, v); }bool operator<=(const sc_unsigned& u, const sc_uint_base& v){ return operator<=(u, (uint64) v); }bool operator<=(const sc_uint_base& u, const sc_unsigned& v) { return operator<=((uint64) u, v); }bool operator>(const sc_unsigned& u, const sc_uint_base& v){ return operator>(u, (uint64) v); }bool operator>(const sc_uint_base& u, const sc_unsigned& v) { return operator>((uint64) u, v); }bool operator>=(const sc_unsigned& u, const sc_uint_base& v){ return operator>=(u, (uint64) v); }bool operator>=(const sc_uint_base& u, const sc_unsigned& v) { return operator>=((uint64) u, v); }// ----------------------------------------------------------------------------// SECTION: Input and output operators// ----------------------------------------------------------------------------// The operators in this section are included from sc_nbcommon.cpp.// ----------------------------------------------------------------------------// SECTION: Operator macros.// ----------------------------------------------------------------------------#define CONVERT_LONG(u) \small_type u ## s = get_sign(u); \sc_digit u ## d[DIGITS_PER_ULONG]; \from_uint(DIGITS_PER_ULONG, u ## d, (unsigned long) u); #define CONVERT_LONG_2(u) \sc_digit u ## d[DIGITS_PER_ULONG]; \from_uint(DIGITS_PER_ULONG, u ## d, (unsigned long) u); #define CONVERT_INT(u) \small_type u ## s = get_sign(u); \sc_digit u ## d[DIGITS_PER_UINT]; \from_uint(DIGITS_PER_UINT, u ## d, (unsigned int) u); #define CONVERT_INT_2(u) \sc_digit u ## d[DIGITS_PER_UINT]; \from_uint(DIGITS_PER_UINT, u ## d, (unsigned int) u); #define CONVERT_INT64(u) \small_type u ## s = get_sign(u); \sc_digit u ## d[DIGITS_PER_UINT64]; \from_uint(DIGITS_PER_UINT64, u ## d, (uint64) u); #define CONVERT_INT64_2(u) \sc_digit u ## d[DIGITS_PER_UINT64]; \from_uint(DIGITS_PER_UINT64, u ## d, (uint64) u); // ----------------------------------------------------------------------------// SECTION: PLUS operators: +, +=, ++// ----------------------------------------------------------------------------// Cases to consider when computing u + v:// 1. 0 + v = v// 2. u + 0 = u// 3. if sgn(u) == sgn(v)// 3.1 u + v = +(u + v) = sgn(u) * (u + v) // 3.2 (-u) + (-v) = -(u + v) = sgn(u) * (u + v)// 4. if sgn(u) != sgn(v)// 4.1 u + (-v) = u - v = sgn(u) * (u - v)// 4.2 (-u) + v = -(u - v) ==> sgn(u) * (u - v)//// Specialization of above cases for computing ++u or u++: // 1. 0 + 1 = 1// 3. u + 1 = u + 1 = sgn(u) * (u + 1)// 4. (-u) + 1 = -(u - 1) = sgn(u) * (u - 1)sc_unsignedoperator+(const sc_unsigned& u, const sc_unsigned& v){ if (u.sgn == SC_ZERO) // case 1 return sc_unsigned(v); if (v.sgn == SC_ZERO) // case 2 return sc_unsigned(u); // cases 3 and 4 return add_unsigned_friend(u.sgn, u.nbits, u.ndigits, u.digit, v.sgn, v.nbits, v.ndigits, v.digit); }sc_unsignedoperator+(const sc_unsigned &u, uint64 v){ if (v == 0) // case 2 return sc_unsigned(u); CONVERT_INT64(v); if (u.sgn == SC_ZERO) // case 1 return sc_unsigned(vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd, false); // cases 3 and 4 return add_unsigned_friend(u.sgn, u.nbits, u.ndigits, u.digit, vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd);}sc_unsignedoperator+(uint64 u, const sc_unsigned &v){ if (u == 0) // case 1 return sc_unsigned(v); CONVERT_INT64(u); if (v.sgn == SC_ZERO) // case 2 return sc_unsigned(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud, false); // cases 3 and 4 return add_unsigned_friend(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud, v.sgn, v.nbits, v.ndigits, v.digit);}sc_unsignedoperator+(const sc_unsigned &u, unsigned long v){ if (v == 0) // case 2 return sc_unsigned(u); CONVERT_LONG(v); if (u.sgn == SC_ZERO) // case 1 return sc_unsigned(vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd, false); // cases 3 and 4 return add_unsigned_friend(u.sgn, u.nbits, u.ndigits, u.digit, vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd);}sc_unsignedoperator+(unsigned long u, const sc_unsigned &v){ if (u == 0) // case 1 return sc_unsigned(v); CONVERT_LONG(u); if (v.sgn == SC_ZERO) // case 2 return sc_unsigned(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud, false); // cases 3 and 4 return add_unsigned_friend(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud, v.sgn, v.nbits, v.ndigits, v.digit);}// The rest of the operators in this section are included from// sc_nbcommon.cpp.// ----------------------------------------------------------------------------// SECTION: MINUS operators: -, -=, --// ----------------------------------------------------------------------------// Cases to consider when computing u + v:// 1. u - 0 = u // 2. 0 - v = -v// 3. if sgn(u) != sgn(v)// 3.1 u - (-v) = u + v = sgn(u) * (u + v)// 3.2 (-u) - v = -(u + v) ==> sgn(u) * (u + v)// 4. if sgn(u) == sgn(v)// 4.1 u - v = +(u - v) = sgn(u) * (u - v) // 4.2 (-u) - (-v) = -(u - v) = sgn(u) * (u - v)//// Specialization of above cases for computing --u or u--: // 1. 0 - 1 = -1// 3. (-u) - 1 = -(u + 1) = sgn(u) * (u + 1)// 4. u - 1 = u - 1 = sgn(u) * (u - 1)// The operators in this section are included from sc_nbcommon.cpp.// ----------------------------------------------------------------------------// SECTION: MULTIPLICATION operators: *, *=// ----------------------------------------------------------------------------// Cases to consider when computing u * v:// 1. u * 0 = 0 * v = 0// 2. 1 * v = v and -1 * v = -v// 3. u * 1 = u and u * -1 = -u// 4. u * v = u * vsc_unsignedoperator*(const sc_unsigned& u, const sc_unsigned& v){ small_type s = mul_signs(u.sgn, v.sgn); if (s == SC_ZERO) // case 1 return sc_unsigned(); // cases 2-4 return mul_unsigned_friend(s, u.nbits, u.ndigits, u.digit, v.nbits, v.ndigits, v.digit);}sc_unsignedoperator*(const sc_unsigned& u, uint64 v){ small_type s = mul_signs(u.sgn, get_sign(v)); if (s == SC_ZERO) // case 1 return sc_unsigned(); CONVERT_INT64_2(v); // cases 2-4 return mul_unsigned_friend(s, u.nbits, u.ndigits, u.digit, BITS_PER_UINT64, DIGITS_PER_UINT64, vd); }sc_unsignedoperator*(uint64 u, const sc_unsigned& v){ small_type s = mul_signs(v.sgn, get_sign(u)); if (s == SC_ZERO) // case 1 return sc_unsigned(); CONVERT_INT64_2(u); // cases 2-4 return mul_unsigned_friend(s, BITS_PER_UINT64, DIGITS_PER_UINT64, ud, v.nbits, v.ndigits, v.digit); }sc_unsignedoperator*(const sc_unsigned& u, unsigned long v){ small_type s = mul_signs(u.sgn, get_sign(v)); if (s == SC_ZERO) // case 1 return sc_unsigned(); CONVERT_LONG_2(v); // else cases 2-4 return mul_unsigned_friend(s, u.nbits, u.ndigits, u.digit, BITS_PER_ULONG, DIGITS_PER_ULONG, vd); }sc_unsignedoperator*(unsigned long u, const sc_unsigned& v){ small_type s = mul_signs(v.sgn, get_sign(u)); if (s == SC_ZERO) // case 1 return sc_unsigned(); CONVERT_LONG_2(u); // cases 2-4 return mul_unsigned_friend(s, BITS_PER_ULONG, DIGITS_PER_ULONG, ud, v.nbits, v.ndigits, v.digit); }// The rest of the operators in this section are included from// sc_nbcommon.cpp.// ----------------------------------------------------------------------------// SECTION: DIVISION operators: /, /=// ----------------------------------------------------------------------------// Cases to consider when finding the quotient q = floor(u/v):// Note that u = q * v + r for r < q.// 1. 0 / 0 or u / 0 => error// 2. 0 / v => 0 = 0 * v + 0// 3. u / v && u = v => u = 1 * u + 0 - u or v can be 1 or -1// 4. u / v && u < v => u = 0 * v + u - u can be 1 or -1// 5. u / v && u > v => u = q * v + r - v can be 1 or -1sc_unsignedoperator/(const sc_unsigned& u, const sc_unsigned& v){ small_type s = mul_signs(u.sgn, v.sgn); if (s == SC_ZERO) { div_by_zero(v.sgn); // case 1 return sc_unsigned(); // case 2 } // other cases return div_unsigned_friend(s, u.nbits, u.ndigits, u.digit, v.nbits, v.ndigits, v.digit);}sc_unsignedoperator/(const sc_unsigned& u, uint64 v){ small_type s = mul_signs(u.sgn, get_sign(v)); if (s == SC_ZERO) { div_by_zero(v); // case 1 return sc_unsigned(); // case 2 } CONVERT_INT64_2(v);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -