📄 sc_fxval.cpp
字号:
/***************************************************************************** The following code is derived, directly or indirectly, from the SystemC source code Copyright (c) 1996-2006 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.4 (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_fxval.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: *****************************************************************************/// $Log: sc_fxval.cpp,v $// Revision 1.1.1.1 2006/12/15 20:31:36 acg// SystemC 2.2//// Revision 1.3 2006/01/13 18:53:58 acg// Andy Goodrich: added $Log command so that CVS comments are reproduced in// the source.//#include <ctype.h>#include <math.h>#include <float.h>#include "sysc/datatypes/fx/sc_fxval.h"namespace sc_dt{// ----------------------------------------------------------------------------// CLASS : sc_fxval//// Fixed-point value type; arbitrary precision.// ----------------------------------------------------------------------------// explicit conversion to character stringconst std::stringsc_fxval::to_string() const{ return std::string( m_rep->to_string( SC_DEC, -1, SC_E ) );}const std::stringsc_fxval::to_string( sc_numrep numrep ) const{ return std::string( m_rep->to_string( numrep, -1, SC_E ) );}const std::stringsc_fxval::to_string( sc_numrep numrep, bool w_prefix ) const{ return std::string( m_rep->to_string( numrep, (w_prefix ? 1 : 0), SC_E ) );}const std::stringsc_fxval::to_string( sc_fmt fmt ) const{ return std::string( m_rep->to_string( SC_DEC, -1, fmt ) );}const std::stringsc_fxval::to_string( sc_numrep numrep, sc_fmt fmt ) const{ return std::string( m_rep->to_string( numrep, -1, fmt ) );}const std::stringsc_fxval::to_string( sc_numrep numrep, bool w_prefix, sc_fmt fmt ) const{ return std::string( m_rep->to_string( numrep, (w_prefix ? 1 : 0), fmt ) );}const std::stringsc_fxval::to_dec() const{ return std::string( m_rep->to_string( SC_DEC, -1, SC_E ) );}const std::stringsc_fxval::to_bin() const{ return std::string( m_rep->to_string( SC_BIN, -1, SC_E ) );}const std::stringsc_fxval::to_oct() const{ return std::string( m_rep->to_string( SC_OCT, -1, SC_E ) );}const std::stringsc_fxval::to_hex() const{ return std::string( m_rep->to_string( SC_HEX, -1, SC_E ) );}// print or dump contentvoidsc_fxval::print( ::std::ostream& os ) const{ m_rep->print( os );}voidsc_fxval::scan( ::std::istream& is ){ std::string s; is >> s; *this = s.c_str();}voidsc_fxval::dump( ::std::ostream& os ) const{ os << "sc_fxval" << ::std::endl; os << "(" << ::std::endl; os << "rep = "; m_rep->dump( os ); // TO BE COMPLETED // os << "r_flag = " << m_r_flag << ::std::endl; // os << "observer = "; // if( m_observer != 0 ) // m_observer->dump( os ); // else // os << "0" << ::std::endl; os << ")" << ::std::endl;}// protected methods and friend functionssc_fxval_observer*sc_fxval::lock_observer() const{ SC_ASSERT_( m_observer != 0, "lock observer failed" ); sc_fxval_observer* tmp = m_observer; m_observer = 0; return tmp;}voidsc_fxval::unlock_observer( sc_fxval_observer* observer_ ) const{ SC_ASSERT_( observer_ != 0, "unlock observer failed" ); m_observer = observer_;}// ----------------------------------------------------------------------------// CLASS : sc_fxval_fast//// Fixed-point value types; limited precision.// ----------------------------------------------------------------------------staticvoidprint_dec( scfx_string& s, scfx_ieee_double id, int w_prefix, sc_fmt fmt ){ if( id.negative() != 0 ) { id.negative( 0 ); s += '-'; } if( w_prefix == 1 ) { scfx_print_prefix( s, SC_DEC ); } if( id.is_zero() ) { s += '0'; return; } // split 'id' into its integer and fractional part double int_part; double frac_part = modf( static_cast<double>( id ), &int_part ); int i; // print integer part int int_digits = 0; int int_zeros = 0; if( int_part != 0.0 ) { int_digits = (int) ceil( log10( int_part + 1.0 ) ); int len = s.length(); s.append( int_digits ); bool zero_digits = ( frac_part == 0.0 && fmt != SC_F ); for( i = int_digits + len - 1; i >= len; i-- ) { unsigned int remainder = (unsigned int) fmod( int_part, 10.0 ); s[i] = static_cast<char>( '0' + remainder ); if( zero_digits ) { if( remainder == 0 ) int_zeros ++; else zero_digits = false; } int_part /= 10.0; } // discard trailing zeros from int_part s.discard( int_zeros ); if( s[len] == '0' ) { // int_digits was overestimated by one s.remove( len ); -- int_digits; } } // print fractional part int frac_digits = 0; int frac_zeros = 0; if( frac_part != 0.0 ) { s += '.'; bool zero_digits = ( int_digits == 0 && fmt != SC_F ); frac_zeros = (int) floor( - log10( frac_part + DBL_EPSILON ) ); frac_part *= pow( 10.0, frac_zeros ); frac_digits = frac_zeros; if( ! zero_digits ) { for( i = 0; i < frac_zeros; i ++ ) s += '0'; frac_zeros = 0; } while( frac_part != 0.0 ) { frac_part *= 10.0; int n = static_cast<int>( frac_part ); if( zero_digits ) { if( n == 0 ) frac_zeros ++; else zero_digits = false; } if( ! zero_digits ) s += static_cast<char>( '0' + n ); frac_part -= n; frac_digits ++; } } // print exponent if( fmt != SC_F ) { if( frac_digits == 0 ) scfx_print_exp( s, int_zeros ); else if( int_digits == 0 ) scfx_print_exp( s, - frac_zeros ); }}staticvoidprint_other( scfx_string& s, const scfx_ieee_double& id, sc_numrep numrep, int w_prefix, sc_fmt fmt, const scfx_params* params ){ scfx_ieee_double id2 = id; sc_numrep numrep2 = numrep; bool numrep_is_sm = ( numrep == SC_BIN_SM || numrep == SC_OCT_SM || numrep == SC_HEX_SM ); if( numrep_is_sm ) { if( id2.negative() != 0 ) { s += '-'; id2.negative( 0 ); } switch( numrep ) { case SC_BIN_SM: numrep2 = SC_BIN_US; break; case SC_OCT_SM: numrep2 = SC_OCT_US; break; case SC_HEX_SM: numrep2 = SC_HEX_US; break; default: ; } } if( w_prefix != 0 ) { scfx_print_prefix( s, numrep ); } numrep = numrep2; sc_fxval_fast a( id2 ); int msb, lsb; if( params != 0 ) { msb = params->iwl() - 1; lsb = params->iwl() - params->wl(); if( params->enc() == SC_TC_ && ( numrep == SC_BIN_US || numrep == SC_OCT_US || numrep == SC_HEX_US ) && ! numrep_is_sm && params->wl() > 1 ) -- msb; else if( params->enc() == SC_US_ && ( numrep == SC_BIN || numrep == SC_OCT || numrep == SC_HEX || numrep == SC_CSD ) ) ++ msb; } else { if( a.is_zero() ) { msb = 0; lsb = 0; } else { msb = id2.exponent() + 1; while( a.get_bit( msb ) == a.get_bit( msb - 1 ) ) -- msb; if( numrep == SC_BIN_US || numrep == SC_OCT_US || numrep == SC_HEX_US ) -- msb; lsb = id2.exponent() - 52; while( ! a.get_bit( lsb ) ) ++ lsb; } } int step; switch( numrep ) { case SC_BIN: case SC_BIN_US: case SC_CSD: step = 1; break; case SC_OCT: case SC_OCT_US: step = 3; break; case SC_HEX: case SC_HEX_US: step = 4; break; default: step = 0; } msb = (int) ceil( double( msb + 1 ) / step ) * step - 1; lsb = (int) floor( double( lsb ) / step ) * step; if( msb < 0 ) { s += '.'; if( fmt == SC_F ) { int sign = ( id2.negative() != 0 ) ? ( 1 << step ) - 1 : 0; for( int i = ( msb + 1 ) / step; i < 0; i ++ ) { if( sign < 10 ) s += static_cast<char>( sign + '0' ); else s += static_cast<char>( sign + 'a' - 10 ); } } } int i = msb; while( i >= lsb ) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -