Skip to content
Newer
Older
100644 223 lines (191 sloc) 4.85 KB
430d7b2 Add lexical support for real numbers.
steve authored
1 /*
2 * Copyright (c) 1999 Stephen Williams (steve@icarus.com)
3 *
4 * This source code is free software; you can redistribute it
5 * and/or modify it in source code form under the terms of the GNU
6 * General Public License as published by the Free Software
7 * Foundation; either version 2 of the License, or (at your option)
8 * any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
18 */
52bf4e6 conditional ident string using autoconfig.
steve authored
19 #ifdef HAVE_CVS_IDENT
f8d1e15 NetEBDiv handles real value constant expressions.
steve authored
20 #ident "$Id: verireal.cc,v 1.10 2003/02/07 02:48:43 steve Exp $"
430d7b2 Add lexical support for real numbers.
steve authored
21 #endif
22
b825f8d Create a config.h.in file to hold all the config
steve authored
23 # include "config.h"
24
430d7b2 Add lexical support for real numbers.
steve authored
25 # include "verireal.h"
f8d1e15 NetEBDiv handles real value constant expressions.
steve authored
26 # include "verinum.h"
430d7b2 Add lexical support for real numbers.
steve authored
27 # include <ctype.h>
084a464 Support decimal constants in behavioral delays.
steve authored
28 # include <iostream>
46253ed Rework expression parsing and elaboration to
steve authored
29 # include <math.h>
430d7b2 Add lexical support for real numbers.
steve authored
30 # include <assert.h>
31
32 verireal::verireal()
33 {
34 sign_ = false;
35 mant_ = 0;
36 exp10_ = 0;
37 }
38
39 verireal::verireal(const char*txt)
40 {
41 mant_ = 0;
42 sign_ = false;
43 exp10_ = 0;
44
45 const char*ptr = txt;
46 for ( ; *ptr ; ptr += 1) {
47 if (*ptr == '.') break;
48 if (*ptr == 'e') break;
49 if (*ptr == 'E') break;
50 if (*ptr == '_') continue;
51
52 assert(isdigit(*ptr));
53
54 mant_ *= 10;
55 mant_ += *ptr - '0';
56 }
57
58 if (*ptr == '.') {
59 ptr += 1;
60 for ( ; *ptr ; ptr += 1) {
61 if (*ptr == 'e') break;
62 if (*ptr == 'E') break;
63 if (*ptr == '_') continue;
64
65 assert(isdigit(*ptr));
66
67 mant_ *= 10;
68 mant_ += *ptr - '0';
69 exp10_ -= 1;
70 }
71 }
72
73 if ((*ptr == 'e') || (*ptr == 'E')) {
74 ptr += 1;
75 long tmp = 0;
76 bool sflag = false;
77 if (*ptr == '+') {ptr += 1; sflag = false;}
78 if (*ptr == '-') {ptr += 1; sflag = true;}
79
80 for ( ; *ptr ; ptr += 1) {
81 if (*ptr == '_') continue;
82 assert(isdigit(*ptr));
83 tmp *= 10;
84 tmp += *ptr - '0';
85 }
86
87 exp10_ += sflag? -tmp : +tmp;
88 }
89
90 assert(*ptr == 0);
91 }
92
9572ddd Support more real arithmetic in delay constants.
steve authored
93 verireal::verireal(long val)
94 {
95 sign_ = val < 0;
96 mant_ = sign_? -val : +val;
97 exp10_ = 0;
98 }
430d7b2 Add lexical support for real numbers.
steve authored
99
100 verireal::~verireal()
101 {
102 }
103
084a464 Support decimal constants in behavioral delays.
steve authored
104 long verireal::as_long(int shift) const
105 {
106 long val = mant_;
107 int ex = exp10_ + shift;
108
109 while (ex < 0) {
110 long mod = val % 10;
111 val /= 10;
067d097 Rounding error.
steve authored
112 if (ex == -1 && mod >= 5)
084a464 Support decimal constants in behavioral delays.
steve authored
113 val += 1;
114 ex += 1;
115 }
116
117 while (ex > 0) {
118 val *= 10;
119 ex -= 1;
120 }
121
122 if (sign_)
123 return -val;
124 else
125 return val;
126 }
127
46253ed Rework expression parsing and elaboration to
steve authored
128 double verireal::as_double() const
129 {
130 return mant_ * pow(10.0, exp10_) * (sign_? -1 : 1);
131 }
132
9572ddd Support more real arithmetic in delay constants.
steve authored
133 verireal operator* (const verireal&l, const verireal&r)
134 {
135 verireal res;
136 res.sign_ = l.sign_ != r.sign_;
137 res.mant_ = l.mant_ * r.mant_;
138 res.exp10_= l.exp10_ + r.exp10_;
139 return res;
140 }
141
f8d1e15 NetEBDiv handles real value constant expressions.
steve authored
142 verireal operator/ (const verireal&l, const verireal&r)
143 {
144 verireal res;
145 res.sign_ = l.sign_ != r.sign_;
146 res.mant_ = l.mant_ / r.mant_;
147 res.exp10_= l.exp10_ - r.exp10_;
148 return res;
149 }
150
151 verireal operator/ (const verireal&l, const verinum&r)
152 {
153 verireal res;
154 res.sign_ = l.sign_;
155
156 long rmant = r.as_long();
157 if (rmant < 0) {
158 rmant = -rmant;
159 res.sign_ = !res.sign_;
160 }
161
162 res.mant_ = l.mant_ / rmant;
163 res.exp10_= l.exp10_;
164 return res;
165 }
166
167 verireal operator% (const verireal&l, const verireal&r)
168 {
169 verireal res;
170 assert(0);
171 return res;
172 }
173
174 verireal operator% (const verireal&l, const verinum&r)
175 {
176 verireal res;
177 assert(0);
178 return res;
179 }
180
084a464 Support decimal constants in behavioral delays.
steve authored
181 ostream& operator<< (ostream&out, const verireal&v)
182 {
183 out << (v.sign_? "-" : "+") << v.mant_ << "e" << v.exp10_;
8cc89a3 Pass parameters to system functions.
steve authored
184 return out;
084a464 Support decimal constants in behavioral delays.
steve authored
185 }
186
430d7b2 Add lexical support for real numbers.
steve authored
187 /*
188 * $Log: verireal.cc,v $
f8d1e15 NetEBDiv handles real value constant expressions.
steve authored
189 * Revision 1.10 2003/02/07 02:48:43 steve
190 * NetEBDiv handles real value constant expressions.
191 *
46253ed Rework expression parsing and elaboration to
steve authored
192 * Revision 1.9 2003/01/26 21:15:59 steve
193 * Rework expression parsing and elaboration to
194 * accommodate real/realtime values and expressions.
195 *
52bf4e6 conditional ident string using autoconfig.
steve authored
196 * Revision 1.8 2002/08/12 01:35:01 steve
197 * conditional ident string using autoconfig.
198 *
067d097 Rounding error.
steve authored
199 * Revision 1.7 2002/06/15 02:35:49 steve
200 * Rounding error.
201 *
9572ddd Support more real arithmetic in delay constants.
steve authored
202 * Revision 1.6 2001/11/06 06:11:55 steve
203 * Support more real arithmetic in delay constants.
204 *
b825f8d Create a config.h.in file to hold all the config
steve authored
205 * Revision 1.5 2001/07/25 03:10:50 steve
206 * Create a config.h.in file to hold all the config
207 * junk, and support gcc 3.0. (Stephan Boettcher)
208 *
8cc89a3 Pass parameters to system functions.
steve authored
209 * Revision 1.4 2001/07/07 20:20:10 steve
210 * Pass parameters to system functions.
211 *
084a464 Support decimal constants in behavioral delays.
steve authored
212 * Revision 1.3 2000/12/10 22:01:36 steve
213 * Support decimal constants in behavioral delays.
214 *
b734ecf Macintosh compilers do not support ident.
steve authored
215 * Revision 1.2 2000/02/23 02:56:56 steve
216 * Macintosh compilers do not support ident.
217 *
430d7b2 Add lexical support for real numbers.
steve authored
218 * Revision 1.1 1999/06/15 02:50:02 steve
219 * Add lexical support for real numbers.
220 *
221 */
222
Something went wrong with that request. Please try again.