sc_signed.cpp

来自「基于4个mips核的noc设计」· C++ 代码 · 共 3,016 行 · 第 1/5 页

CPP
3,016
字号
sc_signed operator << (const sc_signed& u, const sc_uint_base& v){ return operator<<(u, (uint64) v); }sc_signed& sc_signed::operator <<= (const sc_uint_base& v){ return operator<<=((uint64) v); }sc_signedoperator >> (const sc_signed&    u, const sc_uint_base&  v){ return operator>>(u, (uint64) v); }sc_signed& sc_signed::operator >>= (const sc_uint_base&  v){ return operator>>=((uint64) v); }bool operator == (const sc_signed& u, const sc_uint_base& v){ return operator==(u, (uint64) v); }bool operator == (const sc_uint_base& u, const sc_signed& v) { return operator==((uint64) u, v); }bool operator != (const sc_signed& u, const sc_uint_base& v){ return operator!=(u, (uint64) v); }bool operator != (const sc_uint_base& u, const sc_signed& v) { return operator!=((uint64) u, v); }bool operator < (const sc_signed& u, const sc_uint_base& v){ return operator<(u, (uint64) v); }bool operator < (const sc_uint_base& u, const sc_signed& v) { return operator<((uint64) u, v); }bool operator <= (const sc_signed& u, const sc_uint_base& v){ return operator<=(u, (uint64) v); }bool operator <= (const sc_uint_base& u, const sc_signed& v) { return operator<=((uint64) u, v); }bool operator > (const sc_signed& u, const sc_uint_base& v){ return operator>(u, (uint64) v); }bool operator > (const sc_uint_base& u, const sc_signed& v) { return operator>((uint64) u, v); }bool operator >= (const sc_signed& u, const sc_uint_base& v){ return operator>=(u, (uint64) v); }bool operator >= (const sc_uint_base& u, const sc_signed& v) { return operator>=((uint64) u, v); }// ----------------------------------------------------------------------------//  SECTION: Input and output operators// ----------------------------------------------------------------------------// Operators in this section are included from sc_nbcommon.cpp.// ----------------------------------------------------------------------------//  SECTION: Operator macros.// ----------------------------------------------------------------------------#define CONVERT_LONG(u) \small_type u ## s = get_sign(u);                        \unsigned long u ## d[DIGITS_PER_ULONG];                    \from_uint(DIGITS_PER_ULONG, u ## d, (unsigned long) u); #define CONVERT_LONG_2(u) \unsigned long u ## d[DIGITS_PER_ULONG];                     \from_uint(DIGITS_PER_ULONG, u ## d, (unsigned long) u); #define CONVERT_INT64(u) \small_type u ## s = get_sign(u);                   \unsigned long u ## d[DIGITS_PER_UINT64];              \from_uint(DIGITS_PER_UINT64, u ## d, (uint64) u); #define CONVERT_INT64_2(u) \unsigned long 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_signedoperator+(const sc_unsigned& u, const sc_signed& v){  if (u.sgn == SC_ZERO) // case 1    return sc_signed(v);  if (v.sgn == SC_ZERO) // case 2    return sc_signed(u);  // cases 3 and 4  return add_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit,                           v.sgn, v.nbits, v.ndigits, v.digit);}sc_signedoperator+(const sc_signed& u, const sc_unsigned& v){  if (u.sgn == SC_ZERO) // case 1    return sc_signed(v);  if (v.sgn == SC_ZERO) // case 2    return sc_signed(u);  // cases 3 and 4  return add_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit,                           v.sgn, v.nbits, v.ndigits, v.digit);}sc_signedoperator+(const sc_signed& u, const sc_signed& v){  if (u.sgn == SC_ZERO) // case 1    return sc_signed(v);  if (v.sgn == SC_ZERO) // case 2    return sc_signed(u);  // cases 3 and 4  return add_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit,                           v.sgn, v.nbits, v.ndigits, v.digit);  }sc_signedoperator+(const sc_signed &u, int64 v){  if (v == 0)  // case 2    return sc_signed(u);  CONVERT_INT64(v);  if (u.sgn == SC_ZERO)  // case 1    return sc_signed(vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd, false);  // cases 3 and 4  return add_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit,                           vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd);}sc_signedoperator+(int64 u, const sc_signed &v){  if (u == 0) // case 1    return sc_signed(v);  CONVERT_INT64(u);  if (v.sgn == SC_ZERO)  // case 2    return sc_signed(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud, false);  // cases 3 and 4  return add_signed_friend(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud,                           v.sgn, v.nbits, v.ndigits, v.digit);}sc_signedoperator+(const sc_unsigned &u, int64 v){  if (v == 0)  // case 2    return sc_signed(u);  CONVERT_INT64(v);  if (u.sgn == SC_ZERO)  // case 1    return sc_signed(vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd, false);  // cases 3 and 4  return add_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit,                           vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd);}sc_signedoperator+(int64 u, const sc_unsigned &v){  if (u == 0) // case 1    return sc_signed(v);  CONVERT_INT64(u);  if (v.sgn == SC_ZERO)  // case 2    return sc_signed(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud, false);  // cases 3 and 4  return add_signed_friend(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud,                           v.sgn, v.nbits, v.ndigits, v.digit);}sc_signedoperator+(const sc_signed &u, uint64 v){  if (v == 0)  // case 2    return sc_signed(u);  CONVERT_INT64(v);  if (u.sgn == SC_ZERO)  // case 1    return sc_signed(vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd, false);  // cases 3 and 4  return add_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit,                           vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd);}sc_signedoperator+(uint64 u, const sc_signed &v){  if (u == 0) // case 1    return sc_signed(v);  CONVERT_INT64(u);  if (v.sgn == SC_ZERO)  // case 2    return sc_signed(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud, false);  // cases 3 and 4  return add_signed_friend(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud,                           v.sgn, v.nbits, v.ndigits, v.digit);}sc_signedoperator+(const sc_signed &u, long v){  if (v == 0)  // case 2    return sc_signed(u);  CONVERT_LONG(v);  if (u.sgn == SC_ZERO)  // case 1    return sc_signed(vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd, false);  // cases 3 and 4  return add_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit,                           vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd);}sc_signedoperator+(long u, const sc_signed &v){  if (u == 0) // case 1    return sc_signed(v);  CONVERT_LONG(u);  if (v.sgn == SC_ZERO)  // case 2    return sc_signed(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud, false);  // cases 3 and 4  return add_signed_friend(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud,                           v.sgn, v.nbits, v.ndigits, v.digit);}sc_signedoperator+(const sc_unsigned &u, long v){  if (v == 0)  // case 2    return sc_signed(u);  CONVERT_LONG(v);  if (u.sgn == SC_ZERO)  // case 1    return sc_signed(vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd, false);  // cases 3 and 4  return add_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit,                           vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd);}sc_signedoperator+(long u, const sc_unsigned &v){  if (u == 0) // case 1    return sc_signed(v);  CONVERT_LONG(u);  if (v.sgn == SC_ZERO)  // case 2    return sc_signed(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud, false);  // cases 3 and 4  return add_signed_friend(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud,                           v.sgn, v.nbits, v.ndigits, v.digit);}sc_signedoperator+(const sc_signed &u, unsigned long v){  if (v == 0) // case 2    return sc_signed(u);  CONVERT_LONG(v);  if (u.sgn == SC_ZERO)  // case 1    return sc_signed(vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd, false);  // cases 3 and 4  return add_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit,                           vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd);}sc_signedoperator+(unsigned long u, const sc_signed &v){  if (u == 0) // case 1    return sc_signed(v);  CONVERT_LONG(u);  if (v.sgn == SC_ZERO)  // case 2    return sc_signed(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud, false);  // cases 3 and 4  return add_signed_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)sc_signedoperator-(const sc_unsigned& u, const sc_unsigned& v){  if (v.sgn == SC_ZERO)  // case 1    return sc_signed(u);  if (u.sgn == SC_ZERO) // case 2    return sc_signed(v, -v.sgn);  // cases 3 and 4  return add_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit,                           -v.sgn, v.nbits, v.ndigits, v.digit);}sc_signedoperator-(const sc_unsigned& u, const sc_signed& v){  if (v.sgn == SC_ZERO)  // case 1    return sc_signed(u);  if (u.sgn == SC_ZERO) // case 2    return sc_signed(v, -v.sgn);  // cases 3 and 4  return add_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit,                           -v.sgn, v.nbits, v.ndigits, v.digit);}sc_signedoperator-(const sc_signed& u, const sc_unsigned& v){  if (v.sgn == SC_ZERO)  // case 1    return sc_signed(u);  if (u.sgn == SC_ZERO) // case 2    return sc_signed(v, -v.sgn);  // cases 3 and 4  return add_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit,                           -v.sgn, v.nbits, v.ndigits, v.digit);}sc_signedoperator-(const sc_signed& u, const sc_signed& v){  if (v.sgn == SC_ZERO)  // case 1    return sc_signed(u);  if (u.sgn == SC_ZERO) // case 2    return sc_signed(v, -v.sgn);  // cases 3 and 4  return add_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit,                           -v.sgn, v.nbits, v.ndigits, v.digit);}sc_signedoperator-(const sc_signed &u, int64 v){  if (v == 0) // case 1    return sc_signed(u);  CONVERT_INT64(v);  if (u.sgn == SC_ZERO) // case 2    return sc_signed(-vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd, false);  // cases 3 and 4  return add_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit,                           -vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd);}sc_signedoperator-(int64 u, const sc_signed& v){  if (u == 0) // case 1    return sc_signed(v, -v.sgn);  CONVERT_INT64(u);  if (v.sgn == SC_ZERO) // case 2    return sc_signed(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud, false);  // cases 3 and 4  return add_signed_friend(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud,                           -v.sgn, v.nbits, v.ndigits, v.digit);}sc_signedoperator-(const sc_unsigned &u, int64 v){  if (v == 0) // case 1    return sc_signed(u);  CONVERT_INT64(v);  if (u.sgn == SC_ZERO) // case 2    return sc_signed(-vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd, false);  // cases 3 and 4  return add_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit,                           -vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd);}sc_signedoperator-(int64 u, const sc_unsigned& v){  if (u == 0) // case 1    return sc_signed(v, -v.sgn);  CONVERT_INT64(u);  if (v.sgn == SC_ZERO) // case 2    return sc_signed(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud, false);  // cases 3 and 4  return add_signed_friend(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud,                           -v.sgn, v.nbits, v.ndigits, v.digit);}sc_signedoperator-(const sc_signed &u, uint64 v){  if (v == 0) // case 1    return sc_signed(u);  CONVERT_INT64(v);  if (u.sgn == SC_ZERO) // case 2    return sc_signed(-vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd, false);  // cases 3 and 4  return add_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit,                           -vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd);}sc_signedoperator-(uint64 u, const sc_signed& v){  if (u == 0) // case 1    return sc_signed(v, -v.sgn);  CONVERT_INT64(u);  if (v.sgn == SC_ZERO) // case 2    return sc_signed(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud, false);  // cases 3 and 4

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?