Skip to content
Newer
Older
100644 237 lines (210 sloc) 5.36 KB
430d7b2 Add lexical support for real numbers.
steve authored
1 /*
fbfc796 Add unary minus as operator supported by verireal.
steve authored
2 * Copyright (c) 1999-2004 Stephen Williams (steve@icarus.com)
430d7b2 Add lexical support for real numbers.
steve authored
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
33e5f22 Handle evaluate of addition of real valued constants.
steve authored
20 #ident "$Id: verireal.cc,v 1.19 2007/04/07 04:46:18 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"
12d432d Use strtod to convert text to doubles.
steve authored
27 # include <stdlib.h>
430d7b2 Add lexical support for real numbers.
steve authored
28 # include <ctype.h>
084a464 Support decimal constants in behavioral delays.
steve authored
29 # include <iostream>
46253ed Rework expression parsing and elaboration to
steve authored
30 # include <math.h>
430d7b2 Add lexical support for real numbers.
steve authored
31 # include <assert.h>
8ea3b6b @ldoolitt header includes for gcc-4.3 compatibility
ldoolitt authored
32 # include <cstring>
430d7b2 Add lexical support for real numbers.
steve authored
33
34 verireal::verireal()
35 {
79b2beb Store real values as native double.
steve authored
36 value_ = 0.0;
430d7b2 Add lexical support for real numbers.
steve authored
37 }
38
39 verireal::verireal(const char*txt)
40 {
12d432d Use strtod to convert text to doubles.
steve authored
41 char*tmp = new char[strlen(txt)+1];
42 char*cp = tmp;
43 for (unsigned idx = 0 ; txt[idx] ; idx += 1) {
44 if (txt[idx] == '_')
45 continue;
430d7b2 Add lexical support for real numbers.
steve authored
46
12d432d Use strtod to convert text to doubles.
steve authored
47 *cp++ = txt[idx];
e6e0396 Restore verireal constructor to match vvp processing of reals.
steve authored
48 }
12d432d Use strtod to convert text to doubles.
steve authored
49 cp[0] = 0;
e6e0396 Restore verireal constructor to match vvp processing of reals.
steve authored
50
12d432d Use strtod to convert text to doubles.
steve authored
51 value_ = strtod(tmp, 0);
52 delete[]tmp;
430d7b2 Add lexical support for real numbers.
steve authored
53 }
54
9572ddd Support more real arithmetic in delay constants.
steve authored
55 verireal::verireal(long val)
56 {
79b2beb Store real values as native double.
steve authored
57 value_ = (double)val;
9572ddd Support more real arithmetic in delay constants.
steve authored
58 }
430d7b2 Add lexical support for real numbers.
steve authored
59
69cd007 Support real valued specify delays, properly scaled.
steve authored
60 verireal::verireal(double val)
61 {
62 value_ = val;
63 }
64
430d7b2 Add lexical support for real numbers.
steve authored
65 verireal::~verireal()
66 {
67 }
68
084a464 Support decimal constants in behavioral delays.
steve authored
69 long verireal::as_long(int shift) const
70 {
79b2beb Store real values as native double.
steve authored
71 double out = value_ * pow(10.0,shift);
72 double outf;
73
74 if (out >= 0.0) {
75 outf = floor(out);
76 if (out >= (outf + 0.5))
77 outf += 1.0;
78 } else {
79 outf = ceil(out);
80 if (out <= (outf - 0.5))
81 outf -= 1.0;
084a464 Support decimal constants in behavioral delays.
steve authored
82 }
79b2beb Store real values as native double.
steve authored
83 return (long) outf;
084a464 Support decimal constants in behavioral delays.
steve authored
84 }
85
fc0695b Handle 64bit delay constants.
steve authored
86 int64_t verireal::as_long64(int shift) const
87 {
88 double out = value_ * pow(10.0,shift);
89 double outf;
90
91 if (out >= 0.0) {
92 outf = floor(out);
93 if (out >= (outf + 0.5))
94 outf += 1.0;
95 } else {
96 outf = ceil(out);
97 if (out <= (outf - 0.5))
98 outf -= 1.0;
99 }
100 return (int64_t) outf;
101 }
102
46253ed Rework expression parsing and elaboration to
steve authored
103 double verireal::as_double() const
104 {
79b2beb Store real values as native double.
steve authored
105 return value_;
46253ed Rework expression parsing and elaboration to
steve authored
106 }
107
33e5f22 Handle evaluate of addition of real valued constants.
steve authored
108 verireal operator+ (const verireal&l, const verireal&r)
109 {
110 verireal res;
111 res.value_ = l.value_ + r.value_;
112 return res;
113 }
114
115 verireal operator- (const verireal&l, const verireal&r)
116 {
117 verireal res;
118 res.value_ = l.value_ - r.value_;
119 return res;
120 }
121
9572ddd Support more real arithmetic in delay constants.
steve authored
122 verireal operator* (const verireal&l, const verireal&r)
123 {
124 verireal res;
79b2beb Store real values as native double.
steve authored
125 res.value_ = l.value_ * r.value_;
9572ddd Support more real arithmetic in delay constants.
steve authored
126 return res;
127 }
128
f8d1e15 NetEBDiv handles real value constant expressions.
steve authored
129 verireal operator/ (const verireal&l, const verireal&r)
130 {
131 verireal res;
79b2beb Store real values as native double.
steve authored
132 res.value_ = l.value_ / r.value_;
f8d1e15 NetEBDiv handles real value constant expressions.
steve authored
133 return res;
134 }
135
136 verireal operator/ (const verireal&l, const verinum&r)
137 {
138 verireal res;
79b2beb Store real values as native double.
steve authored
139 res.value_ = l.value_ / (double)r.as_long();
f8d1e15 NetEBDiv handles real value constant expressions.
steve authored
140 return res;
141 }
142
143 verireal operator% (const verireal&l, const verireal&r)
144 {
145 verireal res;
146 assert(0);
147 return res;
148 }
149
150 verireal operator% (const verireal&l, const verinum&r)
151 {
152 verireal res;
153 assert(0);
154 return res;
155 }
156
49b65e8 Add support for power in constant expressions.
steve authored
157 verireal pow (const verireal&l, const verireal&r)
158 {
159 verireal res;
160 res.value_ = pow(l.value_, r.value_);
161 return res;
162 }
163
fbfc796 Add unary minus as operator supported by verireal.
steve authored
164 verireal operator- (const verireal&l)
165 {
166 verireal res;
167 res.value_ = - l.value_;
168 return res;
169 }
170
084a464 Support decimal constants in behavioral delays.
steve authored
171 ostream& operator<< (ostream&out, const verireal&v)
172 {
79b2beb Store real values as native double.
steve authored
173 out << v.value_;
8cc89a3 Pass parameters to system functions.
steve authored
174 return out;
084a464 Support decimal constants in behavioral delays.
steve authored
175 }
176
430d7b2 Add lexical support for real numbers.
steve authored
177 /*
178 * $Log: verireal.cc,v $
33e5f22 Handle evaluate of addition of real valued constants.
steve authored
179 * Revision 1.19 2007/04/07 04:46:18 steve
180 * Handle evaluate of addition of real valued constants.
181 *
69cd007 Support real valued specify delays, properly scaled.
steve authored
182 * Revision 1.18 2006/10/03 05:06:00 steve
183 * Support real valued specify delays, properly scaled.
184 *
fc0695b Handle 64bit delay constants.
steve authored
185 * Revision 1.17 2006/08/08 05:11:37 steve
186 * Handle 64bit delay constants.
187 *
49b65e8 Add support for power in constant expressions.
steve authored
188 * Revision 1.16 2006/07/31 03:50:18 steve
189 * Add support for power in constant expressions.
190 *
fbfc796 Add unary minus as operator supported by verireal.
steve authored
191 * Revision 1.15 2004/06/04 23:33:51 steve
192 * Add unary minus as operator supported by verireal.
193 *
12d432d Use strtod to convert text to doubles.
steve authored
194 * Revision 1.14 2003/03/07 06:10:13 steve
195 * Use strtod to convert text to doubles.
196 *
e6e0396 Restore verireal constructor to match vvp processing of reals.
steve authored
197 * Revision 1.13 2003/03/05 03:45:01 steve
198 * Restore verireal constructor to match vvp processing of reals.
1e73cf8 Use sscanf to make doubles from strings.
steve authored
199 *
79b2beb Store real values as native double.
steve authored
200 * Revision 1.11 2003/02/07 06:13:44 steve
201 * Store real values as native double.
202 *
f8d1e15 NetEBDiv handles real value constant expressions.
steve authored
203 * Revision 1.10 2003/02/07 02:48:43 steve
204 * NetEBDiv handles real value constant expressions.
205 *
46253ed Rework expression parsing and elaboration to
steve authored
206 * Revision 1.9 2003/01/26 21:15:59 steve
207 * Rework expression parsing and elaboration to
208 * accommodate real/realtime values and expressions.
209 *
52bf4e6 conditional ident string using autoconfig.
steve authored
210 * Revision 1.8 2002/08/12 01:35:01 steve
211 * conditional ident string using autoconfig.
212 *
067d097 Rounding error.
steve authored
213 * Revision 1.7 2002/06/15 02:35:49 steve
214 * Rounding error.
215 *
9572ddd Support more real arithmetic in delay constants.
steve authored
216 * Revision 1.6 2001/11/06 06:11:55 steve
217 * Support more real arithmetic in delay constants.
218 *
b825f8d Create a config.h.in file to hold all the config
steve authored
219 * Revision 1.5 2001/07/25 03:10:50 steve
220 * Create a config.h.in file to hold all the config
221 * junk, and support gcc 3.0. (Stephan Boettcher)
222 *
8cc89a3 Pass parameters to system functions.
steve authored
223 * Revision 1.4 2001/07/07 20:20:10 steve
224 * Pass parameters to system functions.
225 *
084a464 Support decimal constants in behavioral delays.
steve authored
226 * Revision 1.3 2000/12/10 22:01:36 steve
227 * Support decimal constants in behavioral delays.
228 *
b734ecf Macintosh compilers do not support ident.
steve authored
229 * Revision 1.2 2000/02/23 02:56:56 steve
230 * Macintosh compilers do not support ident.
231 *
430d7b2 Add lexical support for real numbers.
steve authored
232 * Revision 1.1 1999/06/15 02:50:02 steve
233 * Add lexical support for real numbers.
234 *
235 */
236
Something went wrong with that request. Please try again.