sc_fxnum.cpp

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

CPP
942
字号
/*****************************************************************************  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_fxnum.cpp -   Original Author: Martin Janssen, Synopsys, Inc. *****************************************************************************//*****************************************************************************  MODIFICATION LOG - modifiers, enter your name, affiliation, date and  changes you are making here.      Name, Affiliation, Date:  Description of Modification: *****************************************************************************/#include <math.h>#include "systemc/datatypes/fx/sc_fxnum.h"namespace sc_dt{// ----------------------------------------------------------------------------//  CLASS : sc_fxnum_bitref////  Proxy class for bit-selection in class sc_fxnum, behaves like sc_bit.// ----------------------------------------------------------------------------boolsc_fxnum_bitref::get() const{    return m_num.get_bit( m_idx );}voidsc_fxnum_bitref::set( bool high ){    m_num.set_bit( m_idx, high );}// print or dump contentvoidsc_fxnum_bitref::print( ostream& os ) const{    os << get();}voidsc_fxnum_bitref::scan( istream& is ){    bool b;    is >> b;    *this = b;}voidsc_fxnum_bitref::dump( ostream& os ) const{    os << "sc_fxnum_bitref" << endl;    os << "(" << endl;    os << "num = ";    m_num.dump( os );    os << "idx = " << m_idx << endl;    os << ")" << endl;}// ----------------------------------------------------------------------------//  CLASS : sc_fxnum_fast_bitref////  Proxy class for bit-selection in class sc_fxnum_fast, behaves like sc_bit.// ----------------------------------------------------------------------------boolsc_fxnum_fast_bitref::get() const{    return m_num.get_bit( m_idx );}voidsc_fxnum_fast_bitref::set( bool high ){    m_num.set_bit( m_idx, high );}// print or dump contentvoidsc_fxnum_fast_bitref::print( ostream& os ) const{    os << get();}voidsc_fxnum_fast_bitref::scan( istream& is ){    bool b;    is >> b;    *this = b;}voidsc_fxnum_fast_bitref::dump( ostream& os ) const{    os << "sc_fxnum_fast_bitref" << endl;    os << "(" << endl;    os << "num = ";    m_num.dump( os );    os << "idx = " << m_idx << endl;    os << ")" << endl;}// ----------------------------------------------------------------------------//  CLASS : sc_fxnum_subref////  Proxy class for part-selection in class sc_fxnum,//  behaves like sc_bv_base.// ----------------------------------------------------------------------------boolsc_fxnum_subref::get() const{    return m_num.get_slice( m_from, m_to, m_bv );}boolsc_fxnum_subref::set(){    return m_num.set_slice( m_from, m_to, m_bv );}// print or dump contentvoidsc_fxnum_subref::print( ostream& os ) const{    get();    m_bv.print( os );}voidsc_fxnum_subref::scan( istream& is ){    m_bv.scan( is );    set();}voidsc_fxnum_subref::dump( ostream& os ) const{    os << "sc_fxnum_subref" << endl;    os << "(" << endl;    os << "num  = ";    m_num.dump( os );    os << "from = " << m_from << endl;    os << "to   = " << m_to << endl;    os << ")" << endl;}// ----------------------------------------------------------------------------//  CLASS : sc_fxnum_fast_subref////  Proxy class for part-selection in class sc_fxnum_fast,//  behaves like sc_bv_base.// ----------------------------------------------------------------------------boolsc_fxnum_fast_subref::get() const{    return m_num.get_slice( m_from, m_to, m_bv );}boolsc_fxnum_fast_subref::set(){    return m_num.set_slice( m_from, m_to, m_bv );}// print or dump contentvoidsc_fxnum_fast_subref::print( ostream& os ) const{    get();    m_bv.print( os );}voidsc_fxnum_fast_subref::scan( istream& is ){    m_bv.scan( is );    set();}voidsc_fxnum_fast_subref::dump( ostream& os ) const{    os << "sc_fxnum_fast_subref" << endl;    os << "(" << endl;    os << "num  = ";    m_num.dump( os );    os << "from = " << m_from << endl;    os << "to   = " << m_to << endl;    os << ")" << endl;}// ----------------------------------------------------------------------------//  CLASS : sc_fxnum////  Base class for the fixed-point types; arbitrary precision.// ----------------------------------------------------------------------------// explicit conversion to character stringconst sc_stringsc_fxnum::to_string() const{    return sc_string( m_rep->to_string( SC_DEC, -1, SC_F, &m_params ) );}const sc_stringsc_fxnum::to_string( sc_numrep numrep ) const{    return sc_string( m_rep->to_string( numrep, -1, SC_F, &m_params ) );}const sc_stringsc_fxnum::to_string( sc_numrep numrep, bool w_prefix ) const{    return sc_string( m_rep->to_string( numrep, (w_prefix ? 1 : 0),					SC_F, &m_params ) );}const sc_stringsc_fxnum::to_string( sc_fmt fmt ) const{    return sc_string( m_rep->to_string( SC_DEC, -1, fmt, &m_params ) );}const sc_stringsc_fxnum::to_string( sc_numrep numrep, sc_fmt fmt ) const{    return sc_string( m_rep->to_string( numrep, -1, fmt, &m_params ) );}const sc_stringsc_fxnum::to_string( sc_numrep numrep, bool w_prefix, sc_fmt fmt ) const{    return sc_string( m_rep->to_string( numrep, (w_prefix ? 1 : 0),					fmt, &m_params ) );}const sc_stringsc_fxnum::to_dec() const{    return sc_string( m_rep->to_string( SC_DEC, -1, SC_F, &m_params ) );}const sc_stringsc_fxnum::to_bin() const{    return sc_string( m_rep->to_string( SC_BIN, -1, SC_F, &m_params ) );}const sc_stringsc_fxnum::to_oct() const{    return sc_string( m_rep->to_string( SC_OCT, -1, SC_F, &m_params ) );}const sc_stringsc_fxnum::to_hex() const{    return sc_string( m_rep->to_string( SC_HEX, -1, SC_F, &m_params ) );}// print or dump contentvoidsc_fxnum::print( ostream& os ) const{    os << m_rep->to_string( SC_DEC, -1, SC_F, &m_params );}voidsc_fxnum::scan( istream& is ){    sc_string s;    is >> s;    *this = s.c_str();}voidsc_fxnum::dump( ostream& os ) const{    os << "sc_fxnum" << endl;    os << "(" << endl;    os << "rep      = ";    m_rep->dump( os );    os << "params   = ";    m_params.dump( os );    os << "q_flag   = " << m_q_flag << endl;    os << "o_flag   = " << m_o_flag << endl;    // TO BE COMPLETED    // os << "observer = ";    // if( m_observer != 0 )    //     m_observer->dump( os );    // else    //     os << "0" << endl;    os << ")" << endl;}sc_fxnum_observer*sc_fxnum::lock_observer() const{    SC_ASSERT_( m_observer != 0, "lock observer failed" );    sc_fxnum_observer* tmp = m_observer;    m_observer = 0;    return tmp;}voidsc_fxnum::unlock_observer( sc_fxnum_observer* observer_ ) const{    SC_ASSERT_( observer_ != 0, "unlock observer failed" );    m_observer = observer_;}// ----------------------------------------------------------------------------//  CLASS : sc_fxnum_fast////  Base class for the fixed-point types; limited precision.// ----------------------------------------------------------------------------staticvoidquantization( double& c, const scfx_params& params, bool& q_flag ){    int fwl = params.wl() - params.iwl();    double scale = scfx_pow2( fwl );    double val = scale * c;    double int_part;    double frac_part = modf( val, &int_part );    q_flag = ( frac_part != 0.0 );    if( q_flag )    {        val = int_part;	switch( params.q_mode() )	{            case SC_TRN:			// truncation	    {	        if( c < 0.0 )		    val -= 1.0;		break;	    }            case SC_RND:			// rounding to plus infinity	    {		if( frac_part >= 0.5 )		    val += 1.0;		else if( frac_part < -0.5 )		    val -= 1.0;		break;	    }            case SC_TRN_ZERO:			// truncation to zero	    {	        break;	    }            case SC_RND_INF:			// rounding to infinity	    {		if( frac_part >= 0.5 )		    val += 1.0;		else if( frac_part <= -0.5 )		    val -= 1.0;		break;	    }            case SC_RND_CONV:			// convergent rounding	    {		if( frac_part > 0.5 ||		    frac_part == 0.5 && fmod( int_part, 2.0 ) != 0.0 )		    val += 1.0;		else if( frac_part < -0.5 ||			 frac_part == -0.5 && fmod( int_part, 2.0 ) != 0.0 )		    val -= 1.0;		break;	    }            case SC_RND_ZERO:			// rounding to zero	    {		if( frac_part > 0.5 )		    val += 1.0;		else if( frac_part < -0.5 )		    val -= 1.0;		break;	    }            case SC_RND_MIN_INF:		// rounding to minus infinity	    {		if( frac_part > 0.5 )		    val += 1.0;		else if( frac_part <= -0.5 )		    val -= 1.0;		break;	    }            default:	        ;	}    }    val /= scale;    c = val;}staticvoidoverflow( double& c, const scfx_params& params, bool& o_flag ){    int iwl = params.iwl();    int fwl = params.wl() - iwl;    double full_circle = scfx_pow2( iwl );    double resolution = scfx_pow2( -fwl );    double low, high;    if( params.enc() == SC_TC_ )    {	high = full_circle / 2.0 - resolution;	if( params.o_mode() == SC_SAT_SYM )	    low = - high;	else	    low = - full_circle / 2.0;    }    else    {	low = 0.0;	high = full_circle - resolution;    }    double val = c;    sc_fxval_fast c2 = c;

⌨️ 快捷键说明

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