1/*****************************************************************************
2
3  Licensed to Accellera Systems Initiative Inc. (Accellera) under one or
4  more contributor license agreements.  See the NOTICE file distributed
5  with this work for additional information regarding copyright ownership.
6  Accellera licenses this file to you under the Apache License, Version 2.0
7  (the "License"); you may not use this file except in compliance with the
8  License.  You may obtain a copy of the License at
9
10    http://www.apache.org/licenses/LICENSE-2.0
11
12  Unless required by applicable law or agreed to in writing, software
13  distributed under the License is distributed on an "AS IS" BASIS,
14  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
15  implied.  See the License for the specific language governing
16  permissions and limitations under the License.
17
18 *****************************************************************************/
19
20/*****************************************************************************
21
22  scfx_utils.cpp -
23
24  Original Author: Martin Janssen, Synopsys, Inc.
25
26 *****************************************************************************/
27
28/*****************************************************************************
29
30  MODIFICATION LOG - modifiers, enter your name, affiliation, date and
31  changes you are making here.
32
33      Name, Affiliation, Date:
34  Description of Modification:
35
36 *****************************************************************************/
37
38
39// $Log: scfx_utils.cpp,v $
40// Revision 1.1.1.1  2006/12/15 20:20:04  acg
41// SystemC 2.3
42//
43// Revision 1.3  2006/01/13 18:53:58  acg
44// Andy Goodrich: added $Log command so that CVS comments are reproduced in
45// the source.
46//
47
48#include "sysc/datatypes/fx/scfx_utils.h"
49
50
51namespace sc_dt
52{
53
54void
55scfx_tc2csd( scfx_string& s, int w_prefix )
56{
57    if( w_prefix != 0 ) {
58	SC_ASSERT_( s[0] == '0' && s[1] == 'c' &&
59		    s[2] == 's' && s[3] == 'd', "invalid prefix" );
60    }
61
62    scfx_string csd;
63
64    // copy bits from 's' into 'csd'; skip prefix, point, and exponent
65    int i = 0;
66    int j = (w_prefix != 0 ? 4 : 0);
67    while( s[j] )
68    {
69	if( s[j] == '0' || s[j] == '1' )
70	    csd[i ++] = s[j];
71	else if( s[j] != '.' )
72	    break;
73	++ j;
74    }
75    csd[i] = '\0';
76
77    // convert 'csd' from two's complement to csd
78    -- i;
79    while( i >= 0 )
80    {
81	if( csd[i] == '0' )
82	    -- i;
83	else
84	{
85	    if( i > 0 && csd[i - 1] == '0' )
86		-- i;
87	    else if( i == 0 )
88		csd[i --] = '-';
89	    else
90	    {   // i > 0 && csd[i - 1] == '1'
91		csd[i --] = '-';
92		while( i >= 0 && csd[i] == '1' )
93		    csd[i --] = '0';
94		if( i > 0 )
95		    csd[i] = '1';
96		else if( i == 0 )
97		    csd[i --] = '1';
98	    }
99	}
100    }
101
102    // copy bits from 'csd' back into 's'
103    i = 0;
104    j = (w_prefix != 0 ? 4 : 0);
105    while( csd[i] )
106    {
107	if( s[j] == '.' )
108	    ++ j;
109	s[j ++] = csd[i ++];
110    }
111}
112
113
114void
115scfx_csd2tc( scfx_string& csd )
116{
117    SC_ASSERT_( csd[0] == '0' && csd[1] == 'c' &&
118		csd[2] == 's' && csd[3] == 'd', "invalid prefix" );
119
120    scfx_string s;
121
122    // copy bits from 'csd' into 's'; skip prefix, point, and exponent
123    int i = 0;
124    s[i ++] = '0';
125    int j = 4;
126    while( csd[j] )
127    {
128	if( csd[j] == '-' || csd[j] == '0' || csd[j] == '1' )
129	    s[i ++] = csd[j];
130	else if( csd[j] != '.' )
131	    break;
132	++ j;
133    }
134    s[i] = '\0';
135
136    // convert 's' from csd to two's complement
137    int len = i;
138    i = 1;
139    while( i < len )
140    {
141        while( i < len && s[i] != '-' )
142	    i ++;
143	if( i < len )
144	{
145	    j = i ++;
146	    s[j --] = '1';
147	    while( j >= 0 && s[j] == '0' )
148	        s[j --] = '1';
149	    if( j >= 0 )
150	        s[j] = '0';
151	}
152    }
153
154    // copy bits from 's' back into 'csd'
155    j = csd.length();
156    csd[j + 1] = '\0';
157    while( j > 4 )
158    {
159	csd[j] = csd[j - 1];
160	-- j;
161    }
162
163    i = 0;
164    j = 4;
165    while( s[i] )
166    {
167	if( csd[j] == '.' )
168	    ++ j;
169	csd[j ++] = s[i ++];
170    }
171}
172
173} // namespace sc_dt
174
175
176// Taf!
177