📄 sc_nbcommon.inc
字号:
/***************************************************************************** The following code is derived, directly or indirectly, from the SystemC source code Copyright (c) 1996-2005 by all Contributors. All Rights reserved. The contents of this file are subject to the restrictions and limitations set forth in the SystemC Open Source License Version 2.3 (the "License"); You may not use this file except in compliance with such restrictions and limitations. You may obtain instructions on how to receive a copy of the License at http://www.systemc.org/. Software distributed by Contributors under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License. *****************************************************************************//***************************************************************************** sc_nbcommon.cpp -- Functions common to both sc_signed and sc_unsigned. This file is included in sc_signed.cpp and sc_unsigned.cpp after the macros are defined accordingly. For example, sc_signed.cpp will first define CLASS_TYPE as sc_signed before including this file. This file like sc_nbfriends.cpp and sc_nbexterns.cpp is created in order to ensure only one version of each function, regardless of the class that they interface to. Original Author: Ali Dasdan, Synopsys, Inc. *****************************************************************************//***************************************************************************** MODIFICATION LOG - modifiers, enter your name, affiliation, date and changes you are making here. Name, Affiliation, Date: Description of Modification: *****************************************************************************/// ----------------------------------------------------------------------------// SECTION : Public members// ----------------------------------------------------------------------------// Create a CLASS_TYPE number with nb bits.CLASS_TYPE::CLASS_TYPE( int nb ){ sgn = default_sign(); if( nb > 0 ) { nbits = num_bits( nb ); } else { char msg[BUFSIZ]; std::sprintf( msg, "%s::%s( int nb ) : nb = %d is not valid", CLASS_TYPE_STR, CLASS_TYPE_STR, nb ); SC_REPORT_ERROR( sc_core::SC_ID_INIT_FAILED_, msg ); } ndigits = DIV_CEIL(nbits);#ifdef SC_MAX_NBITS test_bound(nb);#else digit = new sc_digit[ndigits];#endif makezero();}// Create a copy of v with sgn s. v is of the same type.CLASS_TYPE::CLASS_TYPE(const CLASS_TYPE& v){ sgn = v.sgn; nbits = v.nbits; ndigits = v.ndigits;#ifndef SC_MAX_NBITS digit = new sc_digit[ndigits];#endif vec_copy(ndigits, digit, v.digit);}// Create a copy of v where v is of the different type.CLASS_TYPE::CLASS_TYPE(const OTHER_CLASS_TYPE& v){ sgn = v.sgn; nbits = num_bits(v.nbits);#if (IF_SC_SIGNED == 1) ndigits = v.ndigits;#else ndigits = DIV_CEIL(nbits);#endif#ifndef SC_MAX_NBITS digit = new sc_digit[ndigits];#endif copy_digits(v.nbits, v.ndigits, v.digit);}// Create a copy of v where v is an sign-less instance.CLASS_TYPE::CLASS_TYPE(const sc_bv_base& v) { int nb = v.length(); sgn = default_sign(); if( nb > 0 ) { nbits = num_bits( nb ); } else { char msg[BUFSIZ]; std::sprintf( msg, "%s::%s( sc_bv_base ) : nb = %d is not valid", CLASS_TYPE_STR, CLASS_TYPE_STR, nb ); SC_REPORT_ERROR( sc_core::SC_ID_INIT_FAILED_, msg ); } ndigits = DIV_CEIL(nbits);# ifdef SC_MAX_NBITS test_bound(nb);# else digit = new sc_digit[ndigits];# endif makezero(); *this = v;}CLASS_TYPE::CLASS_TYPE(const sc_lv_base& v) { int nb = v.length(); sgn = default_sign(); if( nb > 0 ) { nbits = num_bits( nb ); } else { char msg[BUFSIZ]; std::sprintf( msg, "%s::%s( sc_lv_base ) : nb = %d is not valid", CLASS_TYPE_STR, CLASS_TYPE_STR, nb ); SC_REPORT_ERROR( sc_core::SC_ID_INIT_FAILED_, msg ); } ndigits = DIV_CEIL(nbits);# ifdef SC_MAX_NBITS test_bound(nb);# else digit = new sc_digit[ndigits];# endif makezero(); *this = v;}CLASS_TYPE::CLASS_TYPE(const sc_int_subref_r& v) { int nb = v.length(); sgn = default_sign(); if( nb > 0 ) { nbits = num_bits( nb ); } else { char msg[BUFSIZ]; std::sprintf( msg, "%s::%s( sc_int_subref ) : nb = %d is not valid", CLASS_TYPE_STR, CLASS_TYPE_STR, nb ); SC_REPORT_ERROR( sc_core::SC_ID_INIT_FAILED_, msg ); } ndigits = DIV_CEIL(nbits);# ifdef SC_MAX_NBITS test_bound(nb);# else digit = new sc_digit[ndigits];# endif makezero(); *this = v.to_uint64();}CLASS_TYPE::CLASS_TYPE(const sc_uint_subref_r& v) { int nb = v.length(); sgn = default_sign(); if( nb > 0 ) { nbits = num_bits( nb ); } else { char msg[BUFSIZ]; std::sprintf( msg, "%s::%s( sc_uint_subref ) : nb = %d is not valid", CLASS_TYPE_STR, CLASS_TYPE_STR, nb ); SC_REPORT_ERROR( sc_core::SC_ID_INIT_FAILED_, msg ); } ndigits = DIV_CEIL(nbits);# ifdef SC_MAX_NBITS test_bound(nb);# else digit = new sc_digit[ndigits];# endif makezero(); *this = v.to_uint64();}CLASS_TYPE::CLASS_TYPE(const sc_signed_subref_r& v) { int nb = v.length(); sgn = default_sign(); if( nb > 0 ) { nbits = num_bits( nb ); } else { char msg[BUFSIZ]; std::sprintf( msg, "%s::%s( sc_signed_subref ) : nb = %d is not valid", CLASS_TYPE_STR, CLASS_TYPE_STR, nb ); SC_REPORT_ERROR( sc_core::SC_ID_INIT_FAILED_, msg ); } ndigits = DIV_CEIL(nbits);# ifdef SC_MAX_NBITS test_bound(nb);# else digit = new sc_digit[ndigits];# endif makezero(); *this = sc_unsigned(v.m_obj_p, v.m_left, v.m_right);}CLASS_TYPE::CLASS_TYPE(const sc_unsigned_subref_r& v) { int nb = v.length(); sgn = default_sign(); if( nb > 0 ) { nbits = num_bits( nb ); } else { char msg[BUFSIZ]; std::sprintf( msg, "%s::%s( sc_unsigned_subref ) : nb = %d is not valid", CLASS_TYPE_STR, CLASS_TYPE_STR, nb ); SC_REPORT_ERROR( sc_core::SC_ID_INIT_FAILED_, msg ); } ndigits = DIV_CEIL(nbits);# ifdef SC_MAX_NBITS test_bound(nb);# else digit = new sc_digit[ndigits];# endif makezero(); *this = sc_unsigned(v.m_obj_p, v.m_left, v.m_right);}// ----------------------------------------------------------------------------// SECTION: Public members - Concatenation support.// ----------------------------------------------------------------------------// ----------------------------------------------------------------------------// SECTION: Public members - Assignment operators.// ----------------------------------------------------------------------------// Assignment from v of the same type.const CLASS_TYPE&CLASS_TYPE::operator=(const CLASS_TYPE& v){ if (this != &v) { sgn = v.sgn; if (sgn == SC_ZERO) vec_zero(ndigits, digit); else copy_digits(v.nbits, v.ndigits, v.digit); } return *this;}// Assignment from v of the different type.const CLASS_TYPE&CLASS_TYPE::operator=(const OTHER_CLASS_TYPE& v){ sgn = v.sgn; if (sgn == SC_ZERO) vec_zero(ndigits, digit); else copy_digits(v.nbits, v.ndigits, v.digit); return *this;}// Assignment from an sc_unsigned_subref_rconst CLASS_TYPE& CLASS_TYPE::operator=(const sc_unsigned_subref_r& v){ return operator=(sc_unsigned(v));}// Assignment from an sc_signed_subref_rconst CLASS_TYPE& CLASS_TYPE::operator=(const sc_signed_subref_r& v){ return operator=(sc_unsigned(v));}// ----------------------------------------------------------------------------// SECTION: Input and output operators// ----------------------------------------------------------------------------voidCLASS_TYPE::scan( ::std::istream& is ){ std::string s; is >> s; *this = s.c_str();}// ----------------------------------------------------------------------------// 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)const CLASS_TYPE&CLASS_TYPE::operator+=(const CLASS_TYPE& v){ // u = *this if (sgn == SC_ZERO) // case 1 return (*this = v); if (v.sgn == SC_ZERO) // case 2 return *this; // cases 3 and 4 add_on_help(sgn, nbits, ndigits, digit, v.sgn, v.nbits, v.ndigits, v.digit); convert_SM_to_2C_to_SM(); return *this; }const CLASS_TYPE&CLASS_TYPE::operator+=(const OTHER_CLASS_TYPE& v){ // u = *this if (sgn == SC_ZERO) // case 1 return (*this = v); if (v.sgn == SC_ZERO) // case 2 return *this; // cases 3 and 4 add_on_help(sgn, nbits, ndigits, digit, v.sgn, v.nbits, v.ndigits, v.digit); convert_SM_to_2C_to_SM(); return *this; }CLASS_TYPE&CLASS_TYPE::operator++() // prefix{ // u = *this if (sgn == SC_ZERO) { // case 1 digit[0] = 1; sgn = SC_POS; } else if (sgn == SC_POS) // case 3 vec_add_small_on(ndigits, digit, 1); else // case 4 vec_sub_small_on(ndigits, digit, 1); sgn = convert_signed_SM_to_2C_to_SM(sgn, nbits, ndigits, digit); return *this; }const CLASS_TYPECLASS_TYPE::operator++(int) // postfix{ // Copy digit into d.#ifdef SC_MAX_NBITS sc_digit d[MAX_NDIGITS];#else sc_digit *d = new sc_digit[ndigits];#endif small_type s = sgn; vec_copy(ndigits, d, digit); ++(*this); return CLASS_TYPE(s, nbits, ndigits, d);}const CLASS_TYPE&CLASS_TYPE::operator+=(int64 v){ // u = *this if (sgn == SC_ZERO) // case 1 return (*this = v); if (v == 0) // case 2 return *this; CONVERT_INT64(v); // cases 3 and 4 add_on_help(sgn, nbits, ndigits, digit, vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd); convert_SM_to_2C_to_SM(); return *this;}const CLASS_TYPE&CLASS_TYPE::operator+=(uint64 v){ // u = *this if (sgn == SC_ZERO) // case 1 return (*this = v); if (v == 0) // case 2 return *this; CONVERT_INT64(v); // cases 3 and 4 add_on_help(sgn, nbits, ndigits, digit, vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd); convert_SM_to_2C_to_SM(); return *this;}const CLASS_TYPE&CLASS_TYPE::operator+=(long v){ // u = *this if (sgn == SC_ZERO) // case 1 return (*this = v); if (v == 0) // case 2 return *this; CONVERT_LONG(v); // cases 3 and 4 add_on_help(sgn, nbits, ndigits, digit, vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd); convert_SM_to_2C_to_SM(); return *this;}const CLASS_TYPE&CLASS_TYPE::operator+=(unsigned long v){ // u = *this if (sgn == SC_ZERO) // case 1 return (*this = v); if (v == 0) // case 2 return *this; CONVERT_LONG(v); // cases 3 and 4 add_on_help(sgn, nbits, ndigits, digit, vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd); convert_SM_to_2C_to_SM(); return *this;}// ----------------------------------------------------------------------------// 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)const CLASS_TYPE&CLASS_TYPE::operator-=(const CLASS_TYPE& v){ // u = *this if (v.sgn == SC_ZERO) // case 1 return *this; if (sgn == SC_ZERO) { // case 2 sgn = -v.sgn; copy_digits(v.nbits, v.ndigits, v.digit); } else { // cases 3 and 4 add_on_help(sgn, nbits, ndigits, digit, -v.sgn, v.nbits, v.ndigits, v.digit); convert_SM_to_2C_to_SM(); } return *this;}const CLASS_TYPE&CLASS_TYPE::operator-=(const OTHER_CLASS_TYPE& v){ // u = *this if (v.sgn == SC_ZERO) // case 1 return *this; if (sgn == SC_ZERO) { // case 2 sgn = -v.sgn; copy_digits(v.nbits, v.ndigits, v.digit); } else { // cases 3 and 4 add_on_help(sgn, nbits, ndigits, digit, -v.sgn, v.nbits, v.ndigits, v.digit); convert_SM_to_2C_to_SM(); } return *this;}CLASS_TYPE&CLASS_TYPE::operator--() // prefix{ if (sgn == SC_ZERO) { // case 1 digit[0] = 1; sgn = SC_NEG; } else if (sgn == SC_POS) // case 3 vec_sub_small_on(ndigits, digit, 1); else // case 4 vec_add_small_on(ndigits, digit, 1); sgn = convert_signed_SM_to_2C_to_SM(sgn, nbits, ndigits, digit); return *this; }const CLASS_TYPECLASS_TYPE::operator--(int) // postfix{ // Copy digit into d.#ifdef SC_MAX_NBITS sc_digit d[MAX_NDIGITS];#else sc_digit *d = new sc_digit[ndigits];#endif small_type s = sgn; vec_copy(ndigits, d, digit); --(*this); return CLASS_TYPE(s, nbits, ndigits, d);}const CLASS_TYPE&CLASS_TYPE::operator-=(int64 v){ // u = *this if (v == 0) // case 1 return *this; if (sgn == SC_ZERO) // case 2 return (*this = -v); CONVERT_INT64(v); // cases 3 and 4 add_on_help(sgn, nbits, ndigits, digit, -vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd); convert_SM_to_2C_to_SM(); return *this;}const CLASS_TYPE&CLASS_TYPE::operator-=(uint64 v){ // u = *this if (v == 0) // case 1 return *this; int64 v2 = (int64) v; if (sgn == SC_ZERO) // case 2 return (*this = -v2); CONVERT_INT64(v); // cases 3 and 4 add_on_help(sgn, nbits, ndigits, digit, -vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd); convert_SM_to_2C_to_SM(); return *this;}const CLASS_TYPE&CLASS_TYPE::operator-=(long v){ // u = *this if (v == 0) // case 1 return *this; if (sgn == SC_ZERO) // case 2 return (*this = -v); CONVERT_LONG(v); // cases 3 and 4 add_on_help(sgn, nbits, ndigits, digit, -vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd); convert_SM_to_2C_to_SM(); return *this;}const CLASS_TYPE&CLASS_TYPE::operator-=(unsigned long v){ // u = *this if (v == 0) // case 1 return *this; long v2 = (long) v; if (sgn == SC_ZERO) // case 2 return (*this = -v2); CONVERT_LONG(v); // cases 3 and 4 add_on_help(sgn, nbits, ndigits, digit, -vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd); convert_SM_to_2C_to_SM(); return *this;}// ----------------------------------------------------------------------------// 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 * vconst CLASS_TYPE&CLASS_TYPE::operator*=(const CLASS_TYPE& v){ // u = *this sgn = mul_signs(sgn, v.sgn); if (sgn == SC_ZERO) // case 1 vec_zero(ndigits, digit); else // cases 2-4 MUL_ON_HELPER(sgn, nbits, ndigits, digit, v.nbits, v.ndigits, v.digit); return *this;}const CLASS_TYPE&CLASS_TYPE::operator*=(const OTHER_CLASS_TYPE& v){ // u = *this sgn = mul_signs(sgn, v.sgn);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -