⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 scfx_ieee.h

📁 system C源码 一种替代verilog的语言
💻 H
字号:
/*****************************************************************************  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. *****************************************************************************//*****************************************************************************  scfx_ieee.h -   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: scfx_ieee.h,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.//#ifndef SCFX_IEEE_H#define SCFX_IEEE_H#include "sysc/datatypes/fx/sc_fxdefs.h"namespace sc_dt{// classes defined in this moduleunion ieee_double;class scfx_ieee_double;union ieee_float;class scfx_ieee_float;// ----------------------------------------------------------------------------//  UNION : ieee_double////  IEEE 754 double-precision format.// ----------------------------------------------------------------------------    union ieee_double{    double d;    struct    {#if defined( SC_BIG_ENDIAN )        unsigned negative:1;        unsigned exponent:11;        unsigned mantissa0:20;        unsigned mantissa1:32;#elif defined( SC_LITTLE_ENDIAN )        unsigned mantissa1:32;        unsigned mantissa0:20;        unsigned exponent:11;        unsigned negative:1;#endif    } s;};const unsigned int SCFX_IEEE_DOUBLE_BIAS   =  1023U;const int          SCFX_IEEE_DOUBLE_E_MAX  =  1023;const int          SCFX_IEEE_DOUBLE_E_MIN  = -1022;const unsigned int SCFX_IEEE_DOUBLE_M_SIZE =    52;// ----------------------------------------------------------------------------//  CLASS : scfx_ieee_double////  Convenient interface to union ieee_double.// ----------------------------------------------------------------------------class scfx_ieee_double{    ieee_double m_id;public:        scfx_ieee_double();    scfx_ieee_double( double );    scfx_ieee_double( const scfx_ieee_double& );        scfx_ieee_double& operator = ( double );    scfx_ieee_double& operator = ( const scfx_ieee_double& );        operator double() const;    unsigned int negative() const;    void negative( unsigned int );    int exponent() const;    void exponent( int );    unsigned int mantissa0() const;    void mantissa0( unsigned int );    unsigned int mantissa1() const;    void mantissa1( unsigned int );    bool is_zero() const;    bool is_subnormal() const;    bool is_normal() const;    bool is_inf() const;    bool is_nan() const;    void set_inf();    void set_nan();    int msb() const;            // most significant non-zero bit    int lsb() const;            // least significant non-zero bit    static const scfx_ieee_double nan();    static const scfx_ieee_double inf( int );};// IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII    inlinescfx_ieee_double::scfx_ieee_double(){    m_id.d = 0.0;}inlinescfx_ieee_double::scfx_ieee_double( double d ){    m_id.d = d;}inlinescfx_ieee_double::scfx_ieee_double( const scfx_ieee_double& a ){    m_id.d = a.m_id.d;}    inlinescfx_ieee_double&scfx_ieee_double::operator = ( double d ){    m_id.d = d;    return *this;}inlinescfx_ieee_double&scfx_ieee_double::operator = ( const scfx_ieee_double& a ){    m_id.d = a.m_id.d;    return *this;}    inlinescfx_ieee_double::operator double() const{    return m_id.d;}inlineunsigned intscfx_ieee_double::negative() const{    return m_id.s.negative;}inlinevoidscfx_ieee_double::negative( unsigned int a ){    m_id.s.negative = a;}inlineintscfx_ieee_double::exponent() const{    return m_id.s.exponent - SCFX_IEEE_DOUBLE_BIAS;}inlinevoidscfx_ieee_double::exponent( int a ){    m_id.s.exponent = SCFX_IEEE_DOUBLE_BIAS + a;}inlineunsigned intscfx_ieee_double::mantissa0() const{    return m_id.s.mantissa0;}inlinevoidscfx_ieee_double::mantissa0( unsigned int a ){    m_id.s.mantissa0 = a;}inlineunsigned intscfx_ieee_double::mantissa1() const{    return m_id.s.mantissa1;}inlinevoidscfx_ieee_double::mantissa1( unsigned int a ){    m_id.s.mantissa1 = a;}inlineboolscfx_ieee_double::is_zero() const{    return ( exponent() == SCFX_IEEE_DOUBLE_E_MIN - 1 &&             mantissa0() == 0U && mantissa1() == 0U );}inlineboolscfx_ieee_double::is_subnormal() const{    return ( exponent() == SCFX_IEEE_DOUBLE_E_MIN - 1 &&             ( mantissa0() != 0U || mantissa1() != 0U ) );}inlineboolscfx_ieee_double::is_normal() const{    return ( exponent() >= SCFX_IEEE_DOUBLE_E_MIN &&             exponent() <= SCFX_IEEE_DOUBLE_E_MAX );}inlineboolscfx_ieee_double::is_inf() const{    return ( exponent() == SCFX_IEEE_DOUBLE_E_MAX + 1 &&             mantissa0() == 0U && mantissa1() == 0U );}inlineboolscfx_ieee_double::is_nan() const{    return ( exponent() == SCFX_IEEE_DOUBLE_E_MAX + 1 &&             ( mantissa0() != 0U || mantissa1() != 0U ) );}inlinevoidscfx_ieee_double::set_inf(){    exponent( SCFX_IEEE_DOUBLE_E_MAX + 1 );    mantissa0( 0U );    mantissa1( 0U );}inlinevoidscfx_ieee_double::set_nan(){    exponent( SCFX_IEEE_DOUBLE_E_MAX + 1 );    mantissa0( (unsigned int) -1 );    mantissa1( (unsigned int) -1 );}#define MSB_STATEMENT(x,n) if( x >> n ) { x >>= n; i += n; }inlineintscfx_ieee_double::msb() const{    unsigned int m0 = mantissa0();    unsigned int m1 = mantissa1();    if( m0 != 0 )    {        int i = 0;        MSB_STATEMENT(m0,16);        MSB_STATEMENT(m0,8);        MSB_STATEMENT(m0,4);        MSB_STATEMENT(m0,2);        MSB_STATEMENT(m0,1);        return ( i - 20 );    }    else if( m1 != 0 )    {        int i = 0;        MSB_STATEMENT(m1,16);        MSB_STATEMENT(m1,8);        MSB_STATEMENT(m1,4);        MSB_STATEMENT(m1,2);        MSB_STATEMENT(m1,1);        return ( i - 52 );    }    else    {        return 0;    }}#undef MSB_STATEMENT#define LSB_STATEMENT(x,n) if( x << n ) { x <<= n; i -= n; }inlineintscfx_ieee_double::lsb() const{    unsigned int m0 = mantissa0();    unsigned int m1 = mantissa1();    if( m1 != 0 )    {        int i = 31;        LSB_STATEMENT(m1,16);        LSB_STATEMENT(m1,8);        LSB_STATEMENT(m1,4);        LSB_STATEMENT(m1,2);        LSB_STATEMENT(m1,1);        return ( i - 52 );    }    else if( m0 != 0 )    {        int i = 31;        LSB_STATEMENT(m0,16);        LSB_STATEMENT(m0,8);        LSB_STATEMENT(m0,4);        LSB_STATEMENT(m0,2);        LSB_STATEMENT(m0,1);        return ( i - 20 );    }    else    {        return 0;    }}#undef LSB_STATEMENTinlineconst scfx_ieee_doublescfx_ieee_double::nan(){    scfx_ieee_double id;    id.set_nan();    return id;}inlineconst scfx_ieee_doublescfx_ieee_double::inf( int sign ){    scfx_ieee_double id( sign );    id.set_inf();    return id;}// ----------------------------------------------------------------------------//  UNION : ieee_float////  IEEE 754 single-precision format.// ----------------------------------------------------------------------------    union ieee_float{    float f;    struct    {#if defined( SC_BIG_ENDIAN )        unsigned negative:1;        unsigned exponent:8;        unsigned mantissa:23;#elif defined( SC_LITTLE_ENDIAN )        unsigned mantissa:23;        unsigned exponent:8;        unsigned negative:1;#endif    } s;};const unsigned int SCFX_IEEE_FLOAT_BIAS   =  127U;const int          SCFX_IEEE_FLOAT_E_MAX  =  127;const int          SCFX_IEEE_FLOAT_E_MIN  = -126;const unsigned int SCFX_IEEE_FLOAT_M_SIZE =   23;// ----------------------------------------------------------------------------//  CLASS : scfx_ieee_float//// Convenient wrapper to union ieee_float.// ----------------------------------------------------------------------------class scfx_ieee_float{    ieee_float m_if;public:    scfx_ieee_float();    scfx_ieee_float( float );    scfx_ieee_float( const scfx_ieee_float& );    scfx_ieee_float& operator = ( float );    scfx_ieee_float& operator = ( const scfx_ieee_float& );    operator float() const;    unsigned int negative() const;    void negative( unsigned int );    int exponent() const;    void exponent( int );    unsigned int mantissa() const;    void mantissa( unsigned int );    bool is_zero() const;    bool is_subnormal() const;    bool is_normal() const;    bool is_inf() const;    bool is_nan() const;    void set_inf();    void set_nan();};// IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII    inlinescfx_ieee_float::scfx_ieee_float(){    m_if.f = 0.0;}inlinescfx_ieee_float::scfx_ieee_float( float f ){    m_if.f = f;}inlinescfx_ieee_float::scfx_ieee_float( const scfx_ieee_float& a ){    m_if.f = a.m_if.f;}inlinescfx_ieee_float&scfx_ieee_float::operator = ( float f ){    m_if.f = f;    return *this;}inlinescfx_ieee_float&scfx_ieee_float::operator = ( const scfx_ieee_float& a ){    m_if.f = a.m_if.f;    return *this;}    inlinescfx_ieee_float::operator float() const{    return m_if.f;}inlineunsigned intscfx_ieee_float::negative() const{    return m_if.s.negative;}inlinevoidscfx_ieee_float::negative( unsigned int a ){    m_if.s.negative = a;}inlineintscfx_ieee_float::exponent() const{    return m_if.s.exponent - SCFX_IEEE_FLOAT_BIAS;}inlinevoidscfx_ieee_float::exponent( int a ){    m_if.s.exponent = SCFX_IEEE_FLOAT_BIAS + a;}inlineunsigned intscfx_ieee_float::mantissa() const{    return m_if.s.mantissa;}inlinevoidscfx_ieee_float::mantissa( unsigned int a ){    m_if.s.mantissa = a;}inlineboolscfx_ieee_float::is_zero() const{    return ( exponent() == SCFX_IEEE_FLOAT_E_MIN - 1 && mantissa() == 0U );}inlineboolscfx_ieee_float::is_subnormal() const{    return ( exponent() == SCFX_IEEE_FLOAT_E_MIN - 1 && mantissa() != 0U );}inlineboolscfx_ieee_float::is_normal() const{    return ( exponent() >= SCFX_IEEE_FLOAT_E_MIN &&             exponent() <= SCFX_IEEE_FLOAT_E_MAX );}inlineboolscfx_ieee_float::is_inf() const{    return ( exponent() == SCFX_IEEE_FLOAT_E_MAX + 1 && mantissa() == 0U );}inlineboolscfx_ieee_float::is_nan() const{    return ( exponent() == SCFX_IEEE_FLOAT_E_MAX + 1 && mantissa() != 0U );}inlinevoidscfx_ieee_float::set_inf(){    exponent( SCFX_IEEE_FLOAT_E_MAX + 1 );    mantissa( 0U );}inlinevoidscfx_ieee_float::set_nan(){    exponent( SCFX_IEEE_FLOAT_E_MAX + 1 );    mantissa( (unsigned int) -1 );}// ----------------------------------------------------------------------------//  FUNCTION : scfx_pow2////  Computes 2.0**exp in double-precision.// ----------------------------------------------------------------------------inlinedouble scfx_pow2( int exp ){    scfx_ieee_double r;    if( exp < SCFX_IEEE_DOUBLE_E_MIN )    {        r = 0.0;        // handle subnormal case        exp -= SCFX_IEEE_DOUBLE_E_MIN;        if( ( exp += 20 ) >= 0 )	{            r.mantissa0( 1U << exp );        }	else if( ( exp += 32 ) >= 0 )	{            r.mantissa1( 1U << exp );        }    }    else if( exp > SCFX_IEEE_DOUBLE_E_MAX )    {        r.set_inf();    }    else    {        r = 1.0;        r.exponent( exp );    }    return r;}// ----------------------------------------------------------------------------//  FUNCTION : uint64_to_double////  Platform independent conversion from double uint64 to double.//  Needed because VC++6 doesn't support this conversion.// ----------------------------------------------------------------------------inlinedoubleuint64_to_double( uint64 a ){#if defined( _MSC_VER )    // conversion from uint64 to double not implemented; use int64    double tmp = static_cast<double>( static_cast<int64>( a ) );    return ( tmp >= 0 ) ? tmp : tmp + sc_dt::scfx_pow2( 64 );#else    return static_cast<double>( a );#endif}} // namespace sc_dt#endif// Taf!

⌨️ 快捷键说明

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