sc_signed.cpp
来自「基于4个mips核的noc设计」· C++ 代码 · 共 3,016 行 · 第 1/5 页
CPP
3,016 行
/***************************************************************************** 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_signed.cpp -- Arbitrary precision signed arithmetic. This file includes the definitions of sc_signed_bitref, sc_signed_subref, and sc_signed classes. The first two classes are proxy classes to reference one bit and a range of bits of a sc_signed 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_signed.h"#include "systemc/datatypes/int/sc_unsigned.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_fix.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_signed&sc_signed::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_fix 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_signed&sc_signed::operator=(int64 v){ sgn = get_sign(v); // v >= 0 now. if (sgn == SC_ZERO) vec_zero(ndigits, digit); else { from_uint(ndigits, digit, (uint64) v); if (nbits <= BITS_PER_INT64) convert_SM_to_2C_to_SM(); } return *this;}sc_signed&sc_signed::operator=(uint64 v){ sgn = get_sign(v); if (sgn == SC_ZERO) vec_zero(ndigits, digit); else { from_uint(ndigits, digit, v); if (nbits <= BITS_PER_INT64) convert_SM_to_2C_to_SM(); } return *this;}sc_signed&sc_signed::operator=(long v){ sgn = get_sign(v); // v >= 0 now. if (sgn == SC_ZERO) vec_zero(ndigits, digit); else { from_uint(ndigits, digit, (unsigned long) v); if (nbits <= BITS_PER_LONG) convert_SM_to_2C_to_SM(); } return *this;}sc_signed&sc_signed::operator=(unsigned long v){ sgn = get_sign(v); if (sgn == SC_ZERO) vec_zero(ndigits, digit); else { from_uint(ndigits, digit, v); if (nbits <= BITS_PER_LONG) convert_SM_to_2C_to_SM(); } return *this;}sc_signed&sc_signed::operator=(double v){ is_bad_double(v); if (v < 0) { v = -v; sgn = SC_NEG; } else 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_signed&sc_signed::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_signed&sc_signed::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_signed::to_string( sc_numrep numrep ) const{ int len = length(); sc_fix aa( *this, len, len, SC_TRN, SC_WRAP, 0, SC_ON ); return aa.to_string( numrep );}const sc_stringsc_signed::to_string( sc_numrep numrep, bool w_prefix ) const{ int len = length(); sc_fix 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_signed&sc_signed::operator = (const sc_int_base& v){ return operator=((int64) v); }sc_signedoperator + ( const sc_unsigned& u, const sc_int_base& v ){ return operator + ( u, SCAST<int64>( v ) ); }sc_signedoperator + ( const sc_int_base& u, const sc_unsigned& v ){ return operator + ( SCAST<int64>( u ), v ); }sc_signed operator + (const sc_signed& u, const sc_int_base& v) { return operator+(u, (int64) v); }sc_signed operator + (const sc_int_base& u, const sc_signed& v) { return operator+((int64) u, v); }sc_signed& sc_signed::operator += (const sc_int_base& v){ return operator+=((int64) v); }sc_signed operator - (const sc_unsigned& u, const sc_int_base& v) { return operator-(u, (int64) v); }sc_signed operator - (const sc_int_base& u, const sc_unsigned& v) { return operator-((int64) u, v); }sc_signed operator-(const sc_signed& u, const sc_int_base& v) { return operator-(u, (int64) v); }sc_signed operator - (const sc_int_base& u, const sc_signed& v) { return operator-((int64) u, v); }sc_signed& sc_signed::operator -= (const sc_int_base& v){ return operator-=((int64) v); }sc_signedoperator * ( const sc_unsigned& u, const sc_int_base& v ){ return operator * ( u, SCAST<int64>( v ) ); }sc_signedoperator * ( const sc_int_base& u, const sc_unsigned& v ){ return operator * ( SCAST<int64>( u ), v ); }sc_signed operator * (const sc_signed& u, const sc_int_base& v) { return operator*(u, (int64) v); }sc_signed operator * (const sc_int_base& u, const sc_signed& v) { return operator*((int64) u, v); }sc_signed& sc_signed::operator *= (const sc_int_base& v){ return operator*=((int64) v); }sc_signedoperator / ( const sc_unsigned& u, const sc_int_base& v ){ return operator / ( u, SCAST<int64>( v ) ); }sc_signedoperator / ( const sc_int_base& u, const sc_unsigned& v ){ return operator / ( SCAST<int64>( u ), v ); }sc_signed operator / (const sc_signed& u, const sc_int_base& v) { return operator/(u, (int64) v); }sc_signed operator / (const sc_int_base& u, const sc_signed& v) { return operator/((int64) u, v); }sc_signed& sc_signed::operator /= (const sc_int_base& v){ return operator/=((int64) v); }sc_signedoperator % ( const sc_unsigned& u, const sc_int_base& v ){ return operator % ( u, SCAST<int64>( v ) ); }sc_signedoperator % ( const sc_int_base& u, const sc_unsigned& v ){ return operator % ( SCAST<int64>( u ), v ); }sc_signed operator % (const sc_signed& u, const sc_int_base& v) { return operator%(u, (int64) v); }sc_signed operator % (const sc_int_base& u, const sc_signed& v) { return operator%((int64) u, v); }sc_signed& sc_signed::operator %= (const sc_int_base& v){ return operator%=((int64) v); }sc_signedoperator & ( const sc_unsigned& u, const sc_int_base& v ){ return operator & ( u, SCAST<int64>( v ) ); }sc_signedoperator & ( const sc_int_base& u, const sc_unsigned& v ){ return operator & ( SCAST<int64>( u ), v ); }sc_signed operator & (const sc_signed& u, const sc_int_base& v) { return operator&(u, (int64) v); }sc_signed operator & (const sc_int_base& u, const sc_signed& v) { return operator&((int64) u, v); }sc_signed& sc_signed::operator &= (const sc_int_base& v){ return operator&=((int64) v); }sc_signedoperator | ( const sc_unsigned& u, const sc_int_base& v ){ return operator | ( u, SCAST<int64>( v ) ); }sc_signedoperator | ( const sc_int_base& u, const sc_unsigned& v ){ return operator | ( SCAST<int64>( u ), v ); }sc_signed operator | (const sc_signed& u, const sc_int_base& v) { return operator|(u, (int64) v); }sc_signed operator | (const sc_int_base& u, const sc_signed& v) { return operator|((int64) u, v); }sc_signed& sc_signed::operator |= (const sc_int_base& v){ return operator|=((int64) v); }sc_signedoperator ^ ( const sc_unsigned& u, const sc_int_base& v ){ return operator ^ ( u, SCAST<int64>( v ) ); }sc_signedoperator ^ ( const sc_int_base& u, const sc_unsigned& v ){ return operator ^ ( SCAST<int64>( u ), v ); }sc_signed operator ^ (const sc_signed& u, const sc_int_base& v) { return operator^(u, (int64) v); }sc_signed operator ^ (const sc_int_base& u, const sc_signed& v) { return operator^((int64) u, v); }sc_signed& sc_signed::operator ^= (const sc_int_base& v){ return operator^=((int64) v); }sc_signed operator << (const sc_signed& u, const sc_int_base& v){ return operator<<(u, (int64) v); }sc_signed& sc_signed::operator <<= (const sc_int_base& v){ return operator<<=((int64) v); }sc_signed operator >> (const sc_signed& u, const sc_int_base& v){ return operator>>(u, (int64) v); }sc_signed& sc_signed::operator >>= (const sc_int_base& v){ return operator>>=((int64) v); }bool operator == (const sc_signed& u, const sc_int_base& v){ return operator==(u, (int64) v); }bool operator == (const sc_int_base& u, const sc_signed& v) { return operator==((int64) u, v); }bool operator != (const sc_signed& u, const sc_int_base& v){ return operator!=(u, (int64) v); }bool operator != (const sc_int_base& u, const sc_signed& v) { return operator!=((int64) u, v); }bool operator < (const sc_signed& u, const sc_int_base& v){ return operator<(u, (int64) v); }bool operator < (const sc_int_base& u, const sc_signed& v) { return operator<((int64) u, v); }bool operator <= (const sc_signed& u, const sc_int_base& v){ return operator<=(u, (int64) v); }booloperator <= (const sc_int_base& u, const sc_signed& v) { return operator<=((int64) u, v); }bool operator > (const sc_signed& u, const sc_int_base& v){ return operator>(u, (int64) v); }bool operator > (const sc_int_base& u, const sc_signed& v) { return operator>((int64) u, v); }bool operator >= (const sc_signed& u, const sc_int_base& v){ return operator>=(u, (int64) v); }bool operator >= (const sc_int_base& u, const sc_signed& v) { return operator>=((int64) u, v); }// ----------------------------------------------------------------------------// SECTION: Interfacing with sc_uint_base// ----------------------------------------------------------------------------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_signedoperator + (const sc_uint_base& u, const sc_signed& v) { return operator+((uint64) u, v); }sc_signed&sc_signed::operator += (const sc_uint_base& v){ return operator+=((uint64) v); }sc_signedoperator - (const sc_unsigned& u, const sc_uint_base& v) { return operator-(u, (uint64) v); }sc_signedoperator - (const sc_uint_base& u, const sc_unsigned& v) { return operator-((uint64) u, v); }sc_signedoperator - (const sc_signed& u, const sc_uint_base& v) { return operator-(u, (uint64) v); }sc_signedoperator - (const sc_uint_base& u, const sc_signed& v) { return operator-((uint64) u, v); }sc_signed&sc_signed::operator -= (const sc_uint_base& v){ return operator-=((uint64) v); }sc_signed operator * (const sc_signed& u, const sc_uint_base& v) { return operator*(u, (uint64) v); }sc_signed operator * (const sc_uint_base& u, const sc_signed& v) { return operator*((uint64) u, 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_signedoperator / (const sc_uint_base& u, const sc_signed& v) { return operator/((uint64) u, v); }sc_signed&sc_signed::operator /= (const sc_uint_base& v){ return operator/=((uint64) v); }sc_signed operator % (const sc_signed& u, const sc_uint_base& v) { return operator%(u, (uint64) v); }sc_signed operator % (const sc_uint_base& u, const sc_signed& v) { return operator%((uint64) u, v); }sc_signed& sc_signed::operator %= (const sc_uint_base& v){ return operator%=((uint64) v); }sc_signed operator & (const sc_signed& u, const sc_uint_base& v) { return operator&(u, (uint64) v); }sc_signed operator & (const sc_uint_base& u, const sc_signed& v) { return operator&((uint64) u, v); }sc_signed& sc_signed::operator &= (const sc_uint_base& v){ return operator&=((uint64) v); }sc_signed operator | (const sc_signed& u, const sc_uint_base& v) { return operator|(u, (uint64) v); }sc_signed operator | (const sc_uint_base& u, const sc_signed& v) { return operator|((uint64) u, v); }sc_signed& sc_signed::operator |= (const sc_uint_base& v){ return operator|=((uint64) v); }sc_signed operator ^ (const sc_signed& u, const sc_uint_base& v) { return operator^(u, (uint64) v); }sc_signed operator ^ (const sc_uint_base& u, const sc_signed& v) { return operator^((uint64) u, v); }sc_signed& sc_signed::operator ^= (const sc_uint_base& v){ return operator^=((uint64) v); }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?