sc_unsigned_subref.inc

Go to the documentation of this file.
00001 /*****************************************************************************
00002 
00003   The following code is derived, directly or indirectly, from the SystemC
00004   source code Copyright (c) 1996-2005 by all Contributors.
00005   All Rights reserved.
00006 
00007   The contents of this file are subject to the restrictions and limitations
00008   set forth in the SystemC Open Source License Version 2.3 (the "License");
00009   You may not use this file except in compliance with such restrictions and
00010   limitations. You may obtain instructions on how to receive a copy of the
00011   License at http://www.systemc.org/. Software distributed by Contributors
00012   under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF
00013   ANY KIND, either express or implied. See the License for the specific
00014   language governing rights and limitations under the License.
00015 
00016  *****************************************************************************/
00017 
00018 /*****************************************************************************
00019 
00020   sc_unsigned_subref.h -- Proxy class that is declared in sc_unsigned.h.  
00021 
00022   Original Author: Ali Dasdan, Synopsys, Inc.
00023  
00024  *****************************************************************************/
00025 
00026 /*****************************************************************************
00027 
00028   MODIFICATION LOG - modifiers, enter your name, affiliation, date and
00029   changes you are making here.
00030 
00031       Name, Affiliation, Date:
00032   Description of Modification:
00033 
00034  *****************************************************************************/
00035 
00036 
00037 // ----------------------------------------------------------------------------
00038 //  CLASS : sc_unsigned_subref_r
00039 //
00040 //  Proxy class for sc_unsigned part selection (r-value only).
00041 // ----------------------------------------------------------------------------
00042 
00043 // concatenation support
00044 
00045 uint64 sc_unsigned_subref_r::concat_get_uint64() const // #### Speed up!
00046 {
00047     sc_unsigned a( m_obj_p, m_left, m_right );
00048     return a.to_uint64();
00049 }
00050 
00051 
00052 bool sc_unsigned_subref_r::concat_get_ctrl(sc_digit* dst_p, int low_i) const
00053     // #### Speed up!
00054 {
00055     sc_unsigned a( m_obj_p, m_left, m_right );  
00056     return a.concat_get_ctrl( dst_p, low_i );
00057 }
00058 
00059 bool sc_unsigned_subref_r::concat_get_data(sc_digit* dst_p, int low_i) const
00060     // #### Speed up!
00061 {
00062     sc_unsigned a( m_obj_p, m_left, m_right );  
00063     return a.concat_get_data( dst_p, low_i );
00064 }
00065 
00066 
00067 // implicit conversion to sc_unsigned
00068 
00069 sc_unsigned_subref_r::operator sc_unsigned () const
00070 {
00071     return sc_unsigned( m_obj_p, m_left, m_right );
00072 }
00073 
00074 
00075 // explicit conversions
00076 
00077 int
00078 sc_unsigned_subref_r::to_int() const
00079 {
00080     sc_unsigned a(  m_obj_p, m_left, m_right );
00081     return a.to_int();
00082 }
00083 
00084 unsigned int
00085 sc_unsigned_subref_r::to_uint() const
00086 {
00087     sc_unsigned a( m_obj_p, m_left, m_right );
00088     return a.to_uint();
00089 }
00090 
00091 long
00092 sc_unsigned_subref_r::to_long() const
00093 {
00094     sc_unsigned a( m_obj_p, m_left, m_right );
00095     return a.to_long();
00096 }
00097 
00098 unsigned long
00099 sc_unsigned_subref_r::to_ulong() const
00100 {
00101     sc_unsigned a( m_obj_p, m_left, m_right );
00102     return a.to_ulong();
00103 }
00104 
00105 int64
00106 sc_unsigned_subref_r::to_int64() const
00107 {
00108     sc_unsigned a( m_obj_p, m_left, m_right );
00109     return a.to_int64();
00110 }
00111 
00112 uint64
00113 sc_unsigned_subref_r::to_uint64() const
00114 {
00115     sc_unsigned a( m_obj_p, m_left, m_right );
00116     return a.to_uint64();
00117 }
00118 
00119 double
00120 sc_unsigned_subref_r::to_double() const
00121 {
00122     sc_unsigned a( m_obj_p, m_left, m_right );
00123     return a.to_double();
00124 }
00125 
00126 
00127 // explicit conversion to character string
00128 
00129 const std::string
00130 sc_unsigned_subref_r::to_string( sc_numrep numrep ) const
00131 {
00132     sc_unsigned a( length() );
00133     a = *this;
00134     return a.to_string( numrep );
00135 }
00136 
00137 const std::string
00138 sc_unsigned_subref_r::to_string( sc_numrep numrep, bool w_prefix ) const
00139 {
00140     sc_unsigned a( length() );
00141     a = *this;
00142     return a.to_string( numrep, w_prefix );
00143 }
00144 
00145 
00146 // ----------------------------------------------------------------------------
00147 //  CLASS : sc_unsigned_subref
00148 //
00149 //  Proxy class for sc_unsigned part selection (r-value and l-value).
00150 // ----------------------------------------------------------------------------
00151 
00152 // assignment operators
00153 
00154 const sc_unsigned_subref& 
00155 sc_unsigned_subref::operator = ( const sc_unsigned_subref_r& a )
00156 {
00157     return operator = ( (sc_unsigned)( a ) );
00158 }
00159 
00160 const sc_unsigned_subref& 
00161 sc_unsigned_subref::operator = ( const sc_unsigned_subref& a )
00162 {
00163     if( this == &a ) {
00164     return *this;
00165     }
00166     return operator = ( (sc_unsigned)( a ) );
00167 }
00168 
00169 const sc_unsigned_subref& 
00170 sc_unsigned_subref::operator = ( const sc_unsigned& v )
00171 {
00172     int l = sc_min( m_left, v.nbits - 1 + m_right );
00173     for( int i = m_right; i <= l; ++ i ) {
00174     m_obj_p->set( i, v.test( i - m_right ) );
00175     }
00176     return *this;
00177 }
00178 
00179 const sc_unsigned_subref& 
00180 sc_unsigned_subref::operator = ( const sc_signed_subref_r& v )
00181 {
00182     return operator = ( (sc_unsigned)( v ) );
00183 }
00184 
00185 const sc_unsigned_subref& 
00186 sc_unsigned_subref::operator = ( const sc_signed& v )
00187 {
00188     int l = sc_min( m_left, v.nbits - 1 + m_right );
00189     for( int i = m_right; i <= l; ++ i ) {
00190     m_obj_p->set( i, v.test( i - m_right ) );
00191     }
00192     return *this;
00193 }
00194 
00195 const sc_unsigned_subref& 
00196 sc_unsigned_subref::operator = ( unsigned long v )
00197 {
00198     for( int i = m_right; i <= m_left; ++ i ) {
00199     m_obj_p->set( i, static_cast<bool>( v & 1 ) );
00200     v >>= 1;
00201     }
00202     return *this;
00203 }
00204 
00205 const sc_unsigned_subref& 
00206 sc_unsigned_subref::operator = ( long v ) 
00207 {
00208     unsigned long v2 = (unsigned long) v;
00209     for( int i = m_right; i <= m_left; ++ i ) {
00210     m_obj_p->set( i, static_cast<bool>( v2 & 1 ) );
00211     v2 >>= 1;
00212     }
00213     return *this;
00214 }
00215 
00216 const sc_unsigned_subref& 
00217 sc_unsigned_subref::operator = ( uint64  v )
00218 {
00219     for( int i = m_right; i <= m_left; ++ i ) {
00220     m_obj_p->set( i, static_cast<bool>( v & 1 ) );
00221     v >>= 1;
00222     }
00223     return *this;
00224 }
00225 
00226 const sc_unsigned_subref& 
00227 sc_unsigned_subref::operator = ( int64 v ) 
00228 {
00229     uint64 v2 = (uint64) v;
00230     for( int i = m_right; i <= m_left; ++ i ) {
00231     m_obj_p->set( i, static_cast<bool>( v2 & 1 ) );
00232     v2 >>= 1;
00233     }
00234     return *this;
00235 }
00236 
00237 const sc_unsigned_subref& 
00238 sc_unsigned_subref::operator = ( double v )
00239 {
00240     is_bad_double(v);
00241 
00242     int nb = m_left - m_right + 1;
00243     int nd = DIV_CEIL(nb);
00244 
00245 #ifdef SC_MAX_NBITS
00246     sc_digit d[MAX_NDIGITS];
00247 #else
00248     sc_digit *d = new sc_digit[nd];
00249 #endif
00250 
00251     if (v < 0)
00252     v = -v;
00253 
00254     register int i = 0;
00255 
00256     while (floor(v) && (i < nd)) {
00257 #ifndef WIN32
00258     d[i++] = (sc_digit) floor(remainder(v, DIGIT_RADIX));
00259 #else
00260     d[i++] = (sc_digit) floor(fmod(v, DIGIT_RADIX));
00261 #endif
00262     v /= DIGIT_RADIX;
00263     }
00264 
00265     vec_zero(i, nd, d);
00266 
00267     sc_digit val = 1;  // Bit value.
00268     int j = 0;   // Current digit in d.
00269 
00270     i = 0;  // Current bit in d.
00271 
00272     while (i < nb) {
00273 
00274     m_obj_p->set(i + m_right, (bool) (d[j] & val));
00275 
00276     ++i;
00277 
00278     if (i % BITS_PER_DIGIT == 0) {
00279         val = 1;
00280         ++j;
00281     }
00282     else
00283         val <<= 1;
00284     }
00285 
00286 #ifndef SC_MAX_NBITS
00287     delete [] d;
00288 #endif
00289 
00290     return *this;
00291 }
00292 
00293 const sc_unsigned_subref&
00294 sc_unsigned_subref::operator = ( const sc_int_base& a )
00295 {
00296     return operator = ( (int64) a );
00297 }
00298 
00299 const sc_unsigned_subref&
00300 sc_unsigned_subref::operator = ( const sc_uint_base& a )
00301 {
00302     return operator = ( (uint64) a );
00303 }
00304 
00305 // concatenation methods
00306 
00307 void sc_unsigned_subref::concat_set( int64 src, int low_i )
00308 {
00309     int  i;
00310     int  l;
00311     bool sign = src < 0;
00312 
00313     if ( low_i < 64 )    
00314     {    
00315     src = src >> low_i;
00316     l = sc_min( m_left, (63-low_i) + m_right );    
00317     for( i = m_right; i <= l; ++ i ) {    
00318         m_obj_p->set( i, src & 1 );    
00319         src = src >> 1;    
00320     }    
00321     for ( ; i <= m_left; i++ ) m_obj_p->set(sign);     
00322     }    
00323     else    
00324     {    
00325     for( i = m_right; i <= m_left; ++ i ) m_obj_p->set(sign);     
00326     }    
00327 }
00328 
00329 void sc_unsigned_subref::concat_set( const sc_signed& src, int low_i )
00330 {
00331     int i;
00332     int l;
00333     int src_i;
00334     bool sign = src.test(src.nbits-1);
00335     l = src.nbits - (low_i+1);
00336     if ( l >= 0 )
00337     {
00338     src_i = low_i;
00339     l = sc_min( m_left, l + m_right );
00340     for( i = m_right; i <= l; ++ i, src_i++ ) {
00341         m_obj_p->set( i, src.test( src_i ) );
00342     }
00343     for ( ; i <= m_left; i++ ) m_obj_p->set(i, sign);
00344     }
00345     else
00346     {    
00347     for( i = m_right; i <= m_left; ++ i ) m_obj_p->set(i, sign);     
00348     }    
00349 }
00350 
00351 void sc_unsigned_subref::concat_set( const sc_unsigned& src, int low_i )
00352 {
00353     int i;
00354     int l;
00355     int src_i;
00356     l = src.nbits - (low_i+2);
00357     if ( l >= 0 )
00358     {
00359     src_i = low_i;
00360     l = sc_min( m_left, l + m_right );
00361     for( i = m_right; i <= l; ++ i, src_i++ ) {
00362         m_obj_p->set( i, src.test( src_i ) );
00363     }
00364     for ( ; i <= m_left; i++ ) m_obj_p->set(i, false);
00365     }
00366     else
00367     {    
00368     for( i = m_right; i <= m_left; ++ i ) m_obj_p->set(i, false);     
00369     }    
00370 }
00371 
00372 void sc_unsigned_subref::concat_set( uint64 src, int low_i )
00373 {
00374     int  i;
00375     int  l;
00376 
00377     if ( low_i < 64 )    
00378     {    
00379     src = src >> low_i;
00380     l = sc_min( m_left, (63-low_i) + m_right );    
00381     for( i = m_right; i <= l; ++ i ) {    
00382         m_obj_p->set( i, src & 1 );    
00383         src = src >> 1;    
00384     }    
00385     for ( ; i <= m_left; i++ ) m_obj_p->set(false);     
00386     }    
00387     else    
00388     {    
00389     for( i = m_right; i <= m_left; ++ i ) m_obj_p->set(false);     
00390     }    
00391 }
00392 // other methods
00393 
00394 void
00395 sc_unsigned_subref::scan( ::std::istream& is )
00396 {
00397     std::string s;
00398     is >> s;
00399     *this = s.c_str();
00400 }
00401 
00402 
00403 // End of file

Generated on Wed Jan 21 15:32:09 2009 for SystemC by  doxygen 1.5.5