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

📄 sc_signed.cpp

📁 system C源码 一种替代verilog的语言
💻 CPP
📖 第 1 页 / 共 5 页
字号:
/*****************************************************************************  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_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: *****************************************************************************/// $Log: sc_signed.cpp,v $// Revision 1.1.1.1  2006/12/15 20:31:36  acg// SystemC 2.2//// Revision 1.3  2006/01/13 18:49:32  acg// Added $Log command so that CVS check in comments are reproduced in the// source.//#include <ctype.h>#include <math.h>#include "sysc/kernel/sc_cmnhdr.h"#include "sysc/kernel/sc_macros.h"#include "sysc/datatypes/int/sc_signed.h"#include "sysc/datatypes/int/sc_unsigned.h"#include "sysc/datatypes/int/sc_int_base.h"#include "sysc/datatypes/int/sc_uint_base.h"#include "sysc/datatypes/int/sc_int_ids.h"#include "sysc/datatypes/bit/sc_bv_base.h"#include "sysc/datatypes/bit/sc_lv_base.h"#include "sysc/datatypes/misc/sc_concatref.h"#include "sysc/datatypes/fx/sc_fix.h"#include "sysc/datatypes/fx/scfx_other_defs.h"namespace sc_dt{// Pool of temporary instances:sc_core::sc_vpool<sc_signed_bitref> sc_signed_bitref::m_pool(9);sc_core::sc_vpool<sc_signed_subref> sc_signed_subref::m_pool(9);// -----------------------------------------------------------------------------// SECTION: Public members - Invalid selections.// -----------------------------------------------------------------------------voidsc_signed::invalid_index( int i ) const{    char msg[BUFSIZ];    std::sprintf( msg,         "sc_bigint bit selection: index = %d violates "         "0 <= index <= %d", i, nbits - 1 );    SC_REPORT_ERROR( sc_core::SC_ID_OUT_OF_BOUNDS_, msg );}voidsc_signed::invalid_range( int l, int r ) const{    char msg[BUFSIZ];    std::sprintf( msg,         "sc_bigint part selection: left = %d, right = %d \n"         "  violates either (0 <= left <= %d) or (0 <= right <= %d)",         l, r, nbits-1, nbits-1 );    SC_REPORT_ERROR( sc_core::SC_ID_OUT_OF_BOUNDS_, msg );}// ----------------------------------------------------------------------------// ----------------------------------------------------------------------------//  SECTION: Public members.// ----------------------------------------------------------------------------// Most public members are included from sc_nbcommon.inc. However, some // concatenation support appears here to optimize between the signed and// unsigned cases.// Insert this object's value at the specified place in a vector of biguint// style values.bool sc_signed::concat_get_ctrl( sc_digit* dst_p, int low_i ) const{    int      dst_i;        // Index to next word to set in dst_p.    int      end_i;        // Index of high order word to set.    int      left_shift;   // Amount to shift value left.    sc_digit mask;         // Mask for partial word sets.    // CALCULATE METRICS FOR DATA MOVEMENT:    dst_i = low_i / BITS_PER_DIGIT;    end_i = (low_i + nbits - 1) / BITS_PER_DIGIT;                   left_shift = low_i % BITS_PER_DIGIT;    // ALL DATA TO BE MOVED IS IN A SINGLE WORD:    mask = ~(-1 << left_shift);        dst_p[dst_i] = ( dst_p[dst_i] & ~mask );             dst_i++;    for ( ; dst_i <= end_i; dst_i++ ) dst_p[dst_i] = 0;        return false; }bool sc_signed::concat_get_data( sc_digit* dst_p, int low_i ) const{    sc_digit carry;        // Carry bit for complements.    int      dst_i;        // Index to next word to set in dst_p.    int      end_i;        // Index of high order word to set.    int      high_i;       // Index w/in word of high order bit.    int      left_shift;   // Amount to shift value left.    sc_digit left_word;    // High word component for set.    sc_digit mask;         // Mask for partial word sets.    bool     result;	 // True if inserted non-zero data.    int      right_shift;  // Amount to shift value right.    sc_digit right_word;   // Low word component for set.    int      src_i;        // Index to next word to get from digit.    // CALCULATE METRICS FOR DATA MOVEMENT:    dst_i = low_i / BITS_PER_DIGIT;    high_i = low_i + nbits - 1;    end_i = high_i / BITS_PER_DIGIT;    left_shift = low_i % BITS_PER_DIGIT;    switch ( sgn )    {      // POSITIVE SOURCE VALUE:      case SC_POS:	result = true;        // ALL DATA TO BE MOVED IS IN A SINGLE WORD:        if ( dst_i == end_i )        {            mask = ~(-1 << nbits) << left_shift;            dst_p[dst_i] = ( dst_p[dst_i] & ~mask ) |                 ((digit[0] << left_shift) & mask);        }        // DATA IS IN MORE THAN ONE WORD, BUT IS WORD ALIGNED:        else if ( left_shift == 0 )        {            carry = 1;            for ( src_i = 0; dst_i < end_i; dst_i++, src_i++ )            {                dst_p[dst_i] = digit[src_i];            }            high_i = high_i % BITS_PER_DIGIT;            mask = ~(-2 << high_i) & DIGIT_MASK;            dst_p[dst_i] = digit[src_i] & mask;        }        // DATA IS IN MORE THAN ONE WORD, AND NOT WORD ALIGNED:        else        {            high_i = high_i % BITS_PER_DIGIT;            right_shift = BITS_PER_DIGIT - left_shift;            mask = ~(-1 << left_shift);            right_word = digit[0];            dst_p[dst_i] = (dst_p[dst_i] & mask) |                 ((right_word << left_shift) & DIGIT_MASK);            for ( src_i = 1, dst_i++; dst_i < end_i; dst_i++, src_i++ )            {                left_word = digit[src_i];                dst_p[dst_i] = ((left_word << left_shift)&DIGIT_MASK) |                    (right_word >> right_shift);                right_word = left_word;            }            left_word = digit[src_i];            mask = ~(-2 << high_i) & DIGIT_MASK;            dst_p[dst_i] = ((left_word << left_shift) |                (right_word >> right_shift)) & mask;        }        break;      // SOURCE VALUE IS NEGATIVE:      case SC_NEG:        // ALL DATA TO BE MOVED IS IN A SINGLE WORD:	result = true;        if ( dst_i == end_i )        {            mask = ~(-1 << nbits) << left_shift;            right_word = (digit[0] ^ DIGIT_MASK) + 1;            dst_p[dst_i] = ( dst_p[dst_i] & ~mask ) |                 ((right_word << left_shift) & mask);        }        // DATA IS IN MORE THAN ONE WORD, BUT IS WORD ALIGNED:        else if ( left_shift == 0 )        {            carry = 1;            for ( src_i = 0; dst_i < end_i; dst_i++, src_i++ )            {                right_word = (digit[src_i] ^ DIGIT_MASK) + carry;                dst_p[dst_i] = right_word &  DIGIT_MASK;                carry = right_word >> BITS_PER_DIGIT;            }            high_i = high_i % BITS_PER_DIGIT;            mask = (~(-2 << high_i)) & DIGIT_MASK;            right_word = (digit[src_i] ^ DIGIT_MASK) + carry;            dst_p[dst_i] = right_word & mask;        }        // DATA IS IN MORE THAN ONE WORD, AND NOT WORD ALIGNED:        else        {            high_i = high_i % BITS_PER_DIGIT;            right_shift = BITS_PER_DIGIT - left_shift;            mask = ~(-1 << left_shift);            carry = 1;            right_word = (digit[0] ^ DIGIT_MASK) + carry;            dst_p[dst_i] = (dst_p[dst_i] & mask) |                 ((right_word << left_shift) & DIGIT_MASK);	    carry = right_word >> BITS_PER_DIGIT;            for ( src_i = 1, dst_i++; dst_i < end_i; dst_i++, src_i++ )            {                left_word = (digit[src_i] ^ DIGIT_MASK) + carry;                dst_p[dst_i] = ((left_word << left_shift)&DIGIT_MASK) |                    (right_word >> right_shift);                carry = left_word >> BITS_PER_DIGIT;                right_word = left_word & DIGIT_MASK;            }            left_word = (digit[src_i] ^ DIGIT_MASK) + carry;            mask = ~(-2 << high_i) & DIGIT_MASK;            dst_p[dst_i] = ((left_word << left_shift) |                (right_word >> right_shift)) & mask;        }	break;      // VALUE IS ZERO:      default:	result = false;        // ALL DATA TO BE MOVED IS IN A SINGLE WORD:        if ( dst_i == end_i )        {            mask = ~(-1 << nbits) << left_shift;            dst_p[dst_i] = dst_p[dst_i] & ~mask;        }        // DATA IS IN MORE THAN ONE WORD, BUT IS WORD ALIGNED:        else if ( left_shift == 0 )        {            carry = 1;            for ( src_i = 0; dst_i < end_i; dst_i++, src_i++ )            {                dst_p[dst_i] = 0;            }            high_i = high_i % BITS_PER_DIGIT;            mask = ~(-2 << high_i) & DIGIT_MASK;            dst_p[dst_i] = 0; // #### digit[src_i] & mask;        }        // DATA IS IN MORE THAN ONE WORD, AND NOT WORD ALIGNED:        else        {            high_i = high_i % BITS_PER_DIGIT;            right_shift = BITS_PER_DIGIT - left_shift;            mask = ~(-1 << left_shift);            dst_p[dst_i] = (dst_p[dst_i] & mask);            for ( dst_i++; dst_i <= end_i; dst_i++ )            {                dst_p[dst_i] = 0;            }        }	break;    }    return result;}// Return this object instance's bits as a uint64 without sign extension.uint64 sc_signed::concat_get_uint64() const{    uint64        result;    switch ( sgn )    {      case SC_POS:	result = 0;	if ( ndigits > 2 )	    result = digit[2];	if ( ndigits > 1 )	    result = (result << BITS_PER_DIGIT) | digit[1];	result = (result << BITS_PER_DIGIT) | digit[0];	break;      case SC_NEG:	result = 0;	if ( ndigits > 2 )	    result = digit[2];	if ( ndigits > 1 )	    result = (result << BITS_PER_DIGIT) | digit[1];	result = (result << BITS_PER_DIGIT) | digit[0];	result = -result;	if ( nbits < 64 ) 	{	    uint64 mask = ~0;	    result = result & ~(mask << nbits);	}	break;      default:	result = 0;	break;    }    return result;}// #### OPTIMIZEvoid sc_signed::concat_set(int64 src, int low_i)  {        *this = (low_i < 64) ? src >> low_i : src >> 63;}void sc_signed::concat_set(const sc_signed& src, int low_i){    if ( low_i < src.length() )        *this = src >> low_i;    else        *this = (src<0) ? (int_type)-1 : 0;}       void sc_signed::concat_set(const sc_unsigned& src, int low_i){    if ( low_i < src.length() )        *this = src >> low_i;    else        *this = 0;}void sc_signed::concat_set(uint64 src, int low_i){    *this = (low_i < 64) ? src >> low_i : 0;}// ----------------------------------------------------------------------------//  SECTION: Public members - Reduction methods.   // ----------------------------------------------------------------------------bool sc_signed::and_reduce() const {    sc_digit current; // Current digit examining.    int      i;       // Index of digit examining.    if ( sgn == SC_NEG )    {	current = (1 << BITS_PER_DIGIT);	for ( i = 0; i < ndigits-1; i++ )  	{	    current = (current >> BITS_PER_DIGIT) + (digit[i]^DIGIT_MASK);	    if ( (current & DIGIT_MASK) != DIGIT_MASK ) return false;	}	current = (current >> BITS_PER_DIGIT) + (digit[i]^DIGIT_MASK);	if ( (current & ~(-1 << (nbits % BITS_PER_DIGIT))) == 	    (sc_digit) ~(-1 << (nbits % BITS_PER_DIGIT)) ) 		return true;    }    return false;}bool sc_signed::or_reduce() const {    return sgn == SC_ZERO ? false : true;}bool sc_signed::xor_reduce() const {    int i;   // Digit examining.    int odd; // Flag for odd number of digits.    odd = 0;    for ( i = 0; i < nbits; i++ )            if ( test(i) ) odd = ~odd;    return odd ? true : false;}// ----------------------------------------------------------------------------//  SECTION: Public members - Assignment operators.// ----------------------------------------------------------------------------// assignment operatorsconst sc_signed&sc_signed::operator = ( const char* a ){    if( a == 0 ) {        SC_REPORT_ERROR( sc_core::SC_ID_CONVERSION_FAILED_,                         "character string is zero" );    }     if( *a == 0 ) {        SC_REPORT_ERROR( sc_core::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_core::sc_report ) {        char msg[BUFSIZ];        std::sprintf( msg, "character string '%s' is not valid", a );        SC_REPORT_ERROR( sc_core::SC_ID_CONVERSION_FAILED_, msg );        // never reached    }    return *this;}const 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 <= (int)BITS_PER_INT64)      convert_SM_to_2C_to_SM();  }  return *this;}const 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 <= (int)BITS_PER_INT64)      convert_SM_to_2C_to_SM();  }  return *this;}const 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 <= (int)BITS_PER_LONG)      convert_SM_to_2C_to_SM();  }  return *this;}const 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 <= (int)BITS_PER_LONG)      convert_SM_to_2C_to_SM();  }  return *this;}const 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++] = (sc_digit) floor(remainder(v, DIGIT_RADIX));#else    digit[i++] = (sc_digit) floor(fmod(v, DIGIT_RADIX));#endif    v /= DIGIT_RADIX;  }  vec_zero(i, ndigits, digit);  convert_SM_to_2C_to_SM();  return *this;  }// ----------------------------------------------------------------------------const 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;}const sc_signed&

⌨️ 快捷键说明

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