scfx_utils.cpp

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-2006 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.4 (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   scfx_utils.cpp - 
00021 
00022   Original Author: Martin Janssen, 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 // $Log: scfx_utils.cpp,v $
00038 // Revision 1.1.1.1  2006/12/15 20:31:36  acg
00039 // SystemC 2.2
00040 //
00041 // Revision 1.3  2006/01/13 18:53:58  acg
00042 // Andy Goodrich: added $Log command so that CVS comments are reproduced in
00043 // the source.
00044 //
00045 
00046 #include "sysc/datatypes/fx/scfx_utils.h"
00047 
00048 
00049 namespace sc_dt
00050 {
00051 
00052 void
00053 scfx_tc2csd( scfx_string& s, int w_prefix )
00054 {
00055     if( w_prefix != 0 ) {
00056     SC_ASSERT_( s[0] == '0' && s[1] == 'c' &&
00057             s[2] == 's' && s[3] == 'd', "invalid prefix" );
00058     }
00059 
00060     scfx_string csd;
00061 
00062     // copy bits from 's' into 'csd'; skip prefix, point, and exponent
00063     int i = 0;
00064     int j = (w_prefix != 0 ? 4 : 0);
00065     while( s[j] )
00066     {
00067     if( s[j] == '0' || s[j] == '1' )
00068         csd[i ++] = s[j];
00069     else if( s[j] != '.' )
00070         break;
00071     ++ j;
00072     }
00073     csd[i] = '\0';
00074 
00075     // convert 'csd' from two's complement to csd
00076     -- i;
00077     while( i >= 0 )
00078     {
00079     if( csd[i] == '0' )
00080         -- i;
00081     else
00082     {
00083         if( i > 0 && csd[i - 1] == '0' )
00084         -- i;
00085         else if( i == 0 )
00086         csd[i --] = '-';
00087         else
00088         {   // i > 0 && csd[i - 1] == '1'
00089         csd[i --] = '-';
00090         while( i >= 0 && csd[i] == '1' )
00091             csd[i --] = '0';
00092         if( i > 0 )
00093             csd[i] = '1';
00094         else if( i == 0 )
00095             csd[i --] = '1';
00096         }
00097     }
00098     }
00099 
00100     // copy bits from 'csd' back into 's'
00101     i = 0;
00102     j = (w_prefix != 0 ? 4 : 0);
00103     while( csd[i] )
00104     {
00105     if( s[j] == '.' )
00106         ++ j;
00107     s[j ++] = csd[i ++];
00108     }
00109 }
00110 
00111 
00112 void
00113 scfx_csd2tc( scfx_string& csd )
00114 {
00115     SC_ASSERT_( csd[0] == '0' && csd[1] == 'c' &&
00116         csd[2] == 's' && csd[3] == 'd', "invalid prefix" );
00117 
00118     scfx_string s;
00119 
00120     // copy bits from 'csd' into 's'; skip prefix, point, and exponent
00121     int i = 0;
00122     s[i ++] = '0';
00123     int j = 4;
00124     while( csd[j] )
00125     {
00126     if( csd[j] == '-' || csd[j] == '0' || csd[j] == '1' )
00127         s[i ++] = csd[j];
00128     else if( csd[j] != '.' )
00129         break;
00130     ++ j;
00131     }
00132     s[i] = '\0';
00133 
00134     // convert 's' from csd to two's complement
00135     int len = i;
00136     i = 1;
00137     while( i < len )
00138     {
00139         while( i < len && s[i] != '-' )
00140         i ++;
00141     if( i < len )
00142     {
00143         j = i ++;
00144         s[j --] = '1';
00145         while( j >= 0 && s[j] == '0' )
00146             s[j --] = '1';
00147         if( j >= 0 )
00148             s[j] = '0';
00149     }
00150     }
00151 
00152     // copy bits from 's' back into 'csd'
00153     j = csd.length();
00154     csd[j + 1] = '\0';
00155     while( j > 4 )
00156     {
00157     csd[j] = csd[j - 1];
00158     -- j;
00159     }
00160         
00161     i = 0;
00162     j = 4;
00163     while( s[i] )
00164     {
00165     if( csd[j] == '.' )
00166         ++ j;
00167     csd[j ++] = s[i ++];
00168     }
00169 }
00170 
00171 } // namespace sc_dt
00172 
00173 
00174 // Taf!

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