sc_nbcommon.inc
来自「基于4个mips核的noc设计」· INC 代码 · 共 2,729 行 · 第 1/4 页
INC
2,729 行
/***************************************************************************** The following code is derived, directly or indirectly, from the SystemC source code Copyright (c) 1996-2002 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]; sprintf( msg, "%s::%s( int nb ) : nb = %d is not valid", CLASS_TYPE_STR, CLASS_TYPE_STR, nb ); SC_REPORT_ERROR( SC_ID_INIT_FAILED_, msg ); } ndigits = DIV_CEIL(nbits);#ifdef SC_MAX_NBITS test_bound(nb);#else digit = new unsigned long[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 unsigned long[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 unsigned long[ndigits];#endif copy_digits(v.nbits, v.ndigits, v.digit);}// ----------------------------------------------------------------------------// SECTION: Public members - Assignment operators.// ----------------------------------------------------------------------------// Assignment from v of the same type.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.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 a subref v of the same type.CLASS_TYPE& CLASS_TYPE::operator=(const CLASS_TYPE_SUBREF& v){ return operator=(CLASS_TYPE(v));}// Assignment from a subref v of the different type.CLASS_TYPE& CLASS_TYPE::operator=(const OTHER_CLASS_TYPE_SUBREF& v){ return operator=(OTHER_CLASS_TYPE(v));}// ----------------------------------------------------------------------------// SECTION: Input and output operators// ----------------------------------------------------------------------------voidCLASS_TYPE::scan( istream& is ){ sc_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)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; }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 unsigned long d[MAX_NDIGITS];#else unsigned long *d = new unsigned long[ndigits];#endif small_type s = sgn; vec_copy(ndigits, d, digit); ++(*this); return CLASS_TYPE(s, nbits, ndigits, d);}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;}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;}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;}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)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;}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 unsigned long d[MAX_NDIGITS];#else unsigned long *d = new unsigned long[ndigits];#endif small_type s = sgn; vec_copy(ndigits, d, digit); --(*this); return CLASS_TYPE(s, nbits, ndigits, d);}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;}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;}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;}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 * vCLASS_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;}CLASS_TYPE&CLASS_TYPE::operator*=(const OTHER_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;}CLASS_TYPE&CLASS_TYPE::operator*=(int64 v){ // u = *this sgn = mul_signs(sgn, get_sign(v)); if (sgn == SC_ZERO) // case 1 vec_zero(ndigits, digit); else { // cases 2-4 CONVERT_INT64_2(v); MUL_ON_HELPER(sgn, nbits, ndigits, digit, BITS_PER_UINT64, DIGITS_PER_UINT64, vd); } return *this;}CLASS_TYPE&CLASS_TYPE::operator*=(uint64 v){ // u = *this sgn = mul_signs(sgn, get_sign(v)); if (sgn == SC_ZERO) // case 1 vec_zero(ndigits, digit); else { // cases 2-4 CONVERT_INT64_2(v); MUL_ON_HELPER(sgn, nbits, ndigits, digit, BITS_PER_UINT64, DIGITS_PER_UINT64, vd); } return *this;}CLASS_TYPE&CLASS_TYPE::operator*=(long v){ // u = *this sgn = mul_signs(sgn, get_sign(v)); if (sgn == SC_ZERO) // case 1
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?