sc_unsigned.cpp
来自「基于4个mips核的noc设计」· C++ 代码 · 共 1,853 行 · 第 1/3 页
CPP
1,853 行
/***************************************************************************** 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_unsigned.cpp -- Arbitrary precision signed arithmetic. This file includes the definitions of sc_unsigned_bitref, sc_unsigned_subref, and sc_unsigned classes. The first two classes are proxy classes to reference one bit and a range of bits of a sc_unsigned number, respectively. This file also includes sc_nbcommon.cpp and sc_nbfriends.cpp, which contain the definitions shared by sc_unsigned. 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: *****************************************************************************/#include <ctype.h>#include <math.h>#include "systemc/kernel/sc_cmnhdr.h"#include "systemc/kernel/sc_macros.h"#include "systemc/datatypes/int/sc_unsigned.h"#include "systemc/datatypes/int/sc_signed.h"#include "systemc/datatypes/int/sc_int_base.h"#include "systemc/datatypes/int/sc_uint_base.h"#include "systemc/datatypes/int/sc_int_ids.h"#include "systemc/datatypes/bit/sc_bv_base.h"#include "systemc/datatypes/bit/sc_lv_base.h"#include "systemc/datatypes/fx/sc_ufix.h"#include "systemc/datatypes/fx/scfx_other_defs.h"#include "systemc/utils/sc_exception.h"namespace sc_dt{// ----------------------------------------------------------------------------// SECTION: Public members.// ----------------------------------------------------------------------------// The public members are included from sc_nbcommon.cpp.// ----------------------------------------------------------------------------// SECTION: Public members - Assignment operators.// ----------------------------------------------------------------------------// assignment operatorssc_unsigned&sc_unsigned::operator = ( const char* a ){ if( a == 0 ) { SC_REPORT_ERROR( SC_ID_CONVERSION_FAILED_, "character string is zero" ); } if( *a == 0 ) { SC_REPORT_ERROR( SC_ID_CONVERSION_FAILED_, "character string is empty" ); } try { int len = length(); sc_ufix aa( a, len, len, SC_TRN, SC_WRAP, 0, SC_ON ); return this->operator = ( aa ); } catch( sc_exception ) { char msg[BUFSIZ]; sprintf( msg, "character string '%s' is not valid", a ); SC_REPORT_ERROR( SC_ID_CONVERSION_FAILED_, msg ); // never reached return *this; }}sc_unsigned&sc_unsigned::operator=(int64 v){ if (v == 0) { sgn = SC_ZERO; vec_zero(ndigits, digit); } else { sgn = SC_POS; from_uint(ndigits, digit, (uint64) v); convert_SM_to_2C_to_SM(); } return *this;}sc_unsigned&sc_unsigned::operator=(uint64 v){ if (v == 0) { sgn = SC_ZERO; vec_zero(ndigits, digit); } else { sgn = SC_POS; from_uint(ndigits, digit, v); convert_SM_to_2C_to_SM(); } return *this;}sc_unsigned&sc_unsigned::operator=(long v){ if (v == 0) { sgn = SC_ZERO; vec_zero(ndigits, digit); } else { sgn = SC_POS; from_uint(ndigits, digit, (unsigned long) v); convert_SM_to_2C_to_SM(); } return *this;}sc_unsigned&sc_unsigned::operator=(unsigned long v){ if (v == 0) { sgn = SC_ZERO; vec_zero(ndigits, digit); } else { sgn = SC_POS; from_uint(ndigits, digit, v); convert_SM_to_2C_to_SM(); } return *this;}sc_unsigned&sc_unsigned::operator=(double v){ is_bad_double(v); sgn = SC_POS; register int i = 0; while (floor(v) && (i < ndigits)) {#ifndef WIN32 digit[i++] = (unsigned long) floor(remainder(v, DIGIT_RADIX));#else digit[i++] = (unsigned long) floor(fmod(v, DIGIT_RADIX));#endif v /= DIGIT_RADIX; } vec_zero(i, ndigits, digit); convert_SM_to_2C_to_SM(); return *this; }// ----------------------------------------------------------------------------sc_unsigned&sc_unsigned::operator = ( const sc_bv_base& v ){ int minlen = sc_min( nbits, v.length() ); int i = 0; for( ; i < minlen; ++ i ) { safe_set( i, v.get_bit( i ), digit ); } for( ; i < nbits; ++ i ) { safe_set( i, 0, digit ); // zero-extend } convert_2C_to_SM(); return *this;}sc_unsigned&sc_unsigned::operator = ( const sc_lv_base& v ){ int minlen = sc_min( nbits, v.length() ); int i = 0; for( ; i < minlen; ++ i ) { safe_set( i, sc_logic( v.get_bit( i ) ).to_bool(), digit ); } for( ; i < nbits; ++ i ) { safe_set( i, 0, digit ); // zero-extend } convert_2C_to_SM(); return *this;}// explicit conversion to character stringconst sc_stringsc_unsigned::to_string( sc_numrep numrep ) const{ int len = length(); sc_ufix aa( *this, len, len, SC_TRN, SC_WRAP, 0, SC_ON ); return aa.to_string( numrep );}const sc_stringsc_unsigned::to_string( sc_numrep numrep, bool w_prefix ) const{ int len = length(); sc_ufix aa( *this, len, len, SC_TRN, SC_WRAP, 0, SC_ON ); return aa.to_string( numrep, w_prefix );}// ----------------------------------------------------------------------------// SECTION: Interfacing with sc_int_base// ----------------------------------------------------------------------------sc_unsigned& sc_unsigned::operator= (const sc_int_base& v){ return operator=((int64) v); }sc_unsigned& sc_unsigned::operator+=(const sc_int_base& v){ return operator+=((int64) v); }sc_unsigned& sc_unsigned::operator-=(const sc_int_base& v){ return operator-=((int64) v); }sc_unsigned& sc_unsigned::operator*=(const sc_int_base& v){ return operator*=((int64) v); }sc_unsigned& sc_unsigned::operator/=(const sc_int_base& v){ return operator/=((int64) v); }sc_unsigned& sc_unsigned::operator%=(const sc_int_base& v){ return operator%=((int64) v); }sc_unsigned& sc_unsigned::operator&=(const sc_int_base& v){ return operator&=((int64) v); }sc_unsigned& sc_unsigned::operator|=(const sc_int_base& v){ return operator|=((int64) v); }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); }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); }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// ----------------------------------------------------------------------------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); }sc_unsigned& sc_unsigned::operator+=(const sc_uint_base& v){ return operator+=((uint64) v); }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); }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); }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); }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); }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); }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); }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& 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& 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); \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_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: -, -=, --// ----------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?