📄 sc_fxval.cpp
字号:
int value = 0; for( int j = step - 1; j >= 0; -- j ) { value += static_cast<int>( a.get_bit( i ) ) << j; -- i; } if( value < 10 ) s += static_cast<char>( value + '0' ); else s += static_cast<char>( value + 'a' - 10 ); if( i == -1 ) s += '.'; } if( lsb > 0 && fmt == SC_F ) { for( int i = lsb / step; i > 0; i -- ) s += '0'; } if( s[s.length() - 1] == '.' ) s.discard( 1 ); if( fmt != SC_F ) { if( msb < 0 ) scfx_print_exp( s, ( msb + 1 ) / step ); else if( lsb > 0 ) scfx_print_exp( s, lsb / step ); } if( numrep == SC_CSD ) scfx_tc2csd( s, w_prefix );}const char*to_string( const scfx_ieee_double& id, sc_numrep numrep, int w_prefix, sc_fmt fmt, const scfx_params* params = 0 ){ static scfx_string s; s.clear(); if( id.is_nan() ) scfx_print_nan( s ); else if( id.is_inf() ) scfx_print_inf( s, static_cast<bool>( id.negative() ) ); else if( id.negative() && ! id.is_zero() && ( numrep == SC_BIN_US || numrep == SC_OCT_US || numrep == SC_HEX_US ) ) s += "negative"; else if( numrep == SC_DEC ) sc_dt::print_dec( s, id, w_prefix, fmt ); else sc_dt::print_other( s, id, numrep, w_prefix, fmt, params ); return s;}// explicit conversion to character stringconst std::stringsc_fxval_fast::to_string() const{ return std::string( sc_dt::to_string( m_val, SC_DEC, -1, SC_E ) );}const std::stringsc_fxval_fast::to_string( sc_numrep numrep ) const{ return std::string( sc_dt::to_string( m_val, numrep, -1, SC_E ) );}const std::stringsc_fxval_fast::to_string( sc_numrep numrep, bool w_prefix ) const{ return std::string( sc_dt::to_string( m_val, numrep, (w_prefix ? 1 : 0), SC_E ) );}const std::stringsc_fxval_fast::to_string( sc_fmt fmt ) const{ return std::string( sc_dt::to_string( m_val, SC_DEC, -1, fmt ) );}const std::stringsc_fxval_fast::to_string( sc_numrep numrep, sc_fmt fmt ) const{ return std::string( sc_dt::to_string( m_val, numrep, -1, fmt ) );}const std::stringsc_fxval_fast::to_string( sc_numrep numrep, bool w_prefix, sc_fmt fmt ) const{ return std::string( sc_dt::to_string( m_val, numrep, (w_prefix ? 1 : 0), fmt ) );}const std::stringsc_fxval_fast::to_dec() const{ return std::string( sc_dt::to_string( m_val, SC_DEC, -1, SC_E ) );}const std::stringsc_fxval_fast::to_bin() const{ return std::string( sc_dt::to_string( m_val, SC_BIN, -1, SC_E ) );}const std::stringsc_fxval_fast::to_oct() const{ return std::string( sc_dt::to_string( m_val, SC_OCT, -1, SC_E ) );}const std::stringsc_fxval_fast::to_hex() const{ return std::string( sc_dt::to_string( m_val, SC_HEX, -1, SC_E ) );}// print or dump contentvoidsc_fxval_fast::print( ::std::ostream& os ) const{ os << sc_dt::to_string( m_val, SC_DEC, -1, SC_E );}voidsc_fxval_fast::scan( ::std::istream& is ){ std::string s; is >> s; *this = s.c_str();}voidsc_fxval_fast::dump( ::std::ostream& os ) const{ os << "sc_fxval_fast" << ::std::endl; os << "(" << ::std::endl; os << "val = " << m_val << ::std::endl; // 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;}// internal use only;boolsc_fxval_fast::get_bit( int i ) const{ scfx_ieee_double id( m_val ); if( id.is_zero() || id.is_nan() || id.is_inf() ) return false; // convert to two's complement unsigned int m0 = id.mantissa0(); unsigned int m1 = id.mantissa1(); if( id.is_normal() ) m0 += 1U << 20; if( id.negative() != 0 ) { m0 = ~ m0; m1 = ~ m1; unsigned int tmp = m1; m1 += 1U; if( m1 <= tmp ) m0 += 1U; } // get the right bit int j = i - id.exponent(); if( ( j += 20 ) >= 32 ) return ( ( m0 & 1U << 31 ) != 0 ); else if( j >= 0 ) return ( ( m0 & 1U << j ) != 0 ); else if( ( j += 32 ) >= 0 ) return ( ( m1 & 1U << j ) != 0 ); else return false;}// protected methods and friend functionssc_fxval_fast_observer*sc_fxval_fast::lock_observer() const{ SC_ASSERT_( m_observer != 0, "lock observer failed" ); sc_fxval_fast_observer* tmp = m_observer; m_observer = 0; return tmp;}voidsc_fxval_fast::unlock_observer( sc_fxval_fast_observer* observer_ ) const{ SC_ASSERT_( observer_ != 0, "unlock observer failed" ); m_observer = observer_;}#define SCFX_FAIL_IF_(cnd) \{ \ if( ( cnd ) ) \ return static_cast<double>( scfx_ieee_double::nan() ); \}doublesc_fxval_fast::from_string( const char* s ){ SCFX_FAIL_IF_( s == 0 || *s == 0 ); scfx_string s2; s2 += s; s2 += '\0'; bool sign_char; int sign = scfx_parse_sign( s, sign_char ); sc_numrep numrep = scfx_parse_prefix( s ); int base = 0; switch( numrep ) { case SC_DEC: { base = 10; if( scfx_is_nan( s ) ) // special case: NaN return static_cast<double>( scfx_ieee_double::nan() ); if( scfx_is_inf( s ) ) // special case: Infinity return static_cast<double>( scfx_ieee_double::inf( sign ) ); break; } case SC_BIN: case SC_BIN_US: { SCFX_FAIL_IF_( sign_char ); base = 2; break; } case SC_BIN_SM: { base = 2; break; } case SC_OCT: case SC_OCT_US: { SCFX_FAIL_IF_( sign_char ); base = 8; break; } case SC_OCT_SM: { base = 8; break; } case SC_HEX: case SC_HEX_US: { SCFX_FAIL_IF_( sign_char ); base = 16; break; } case SC_HEX_SM: { base = 16; break; } case SC_CSD: { SCFX_FAIL_IF_( sign_char ); base = 2; scfx_csd2tc( s2 ); s = (const char*) s2 + 4; numrep = SC_BIN; break; } default:;// Martin, what is default??? } // // find end of mantissa and count the digits and points // const char *end = s; bool based_point = false; int int_digits = 0; int frac_digits = 0; while( *end ) { if( scfx_exp_start( end ) ) break; if( *end == '.' ) { SCFX_FAIL_IF_( based_point ); based_point = true; } else { SCFX_FAIL_IF_( ! scfx_is_digit( *end, numrep ) ); if( based_point ) frac_digits ++; else int_digits ++; } end ++; } SCFX_FAIL_IF_( int_digits == 0 && frac_digits == 0 ); // [ exponent ] int exponent = 0; if( *end ) { for( const char *e = end + 2; *e; e ++ ) SCFX_FAIL_IF_( ! scfx_is_digit( *e, SC_DEC ) ); exponent = atoi( end + 1 ); } // // convert the mantissa // double integer = 0.0; if( int_digits != 0 ) { bool first_digit = true; for( ; s < end; s ++ ) { if( *s == '.' ) break; if( first_digit ) { integer = scfx_to_digit( *s, numrep ); switch( numrep ) { case SC_BIN: case SC_OCT: case SC_HEX: { if( integer >= ( base >> 1 ) ) integer -= base; // two's complement break; } default: ; } first_digit = false; } else { integer *= base; integer += scfx_to_digit( *s, numrep ); } } } // [ . fraction ] double fraction = 0.0; if( frac_digits != 0 ) { s ++; // skip '.' bool first_digit = ( int_digits == 0 ); double scale = 1.0; for( ; s < end; s ++ ) { scale /= base; if( first_digit ) { fraction = scfx_to_digit( *s, numrep ); switch( numrep ) { case SC_BIN: case SC_OCT: case SC_HEX: { if( fraction >= ( base >> 1 ) ) fraction -= base; // two's complement break; } default: ; } fraction *= scale; first_digit = false; } else fraction += scfx_to_digit( *s, numrep ) * scale; } } double exp = ( exponent != 0 ) ? pow( (double) base, (double) exponent ) : 1; return ( sign * ( integer + fraction ) * exp );}#undef SCFX_FAIL_IF_} // namespace sc_dt// Taf!
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -