Merge branch 'master' into verilog-ams
[sverilog.git] / verireal.cc
blobb07c013a047328fc1f4add1634247461daa1e602
1 /*
2 * Copyright (c) 1999-2004 Stephen Williams (steve@icarus.com)
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.
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.
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
19 #ifdef HAVE_CVS_IDENT
20 #ident "$Id: verireal.cc,v 1.19 2007/04/07 04:46:18 steve Exp $"
21 #endif
23 # include "config.h"
25 # include "verireal.h"
26 # include "verinum.h"
27 # include <stdlib.h>
28 # include <ctype.h>
29 # include <iostream>
30 # include <math.h>
31 # include <assert.h>
32 # include <cstring>
34 verireal::verireal()
36 value_ = 0.0;
39 verireal::verireal(const char*txt)
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;
47 *cp++ = txt[idx];
49 cp[0] = 0;
51 value_ = strtod(tmp, 0);
52 delete[]tmp;
55 verireal::verireal(long val)
57 value_ = (double)val;
60 verireal::verireal(double val)
62 value_ = val;
65 verireal::~verireal()
69 long verireal::as_long(int shift) const
71 double out = value_ * pow(10.0,shift);
72 double outf;
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;
83 return (long) outf;
86 int64_t verireal::as_long64(int shift) const
88 double out = value_ * pow(10.0,shift);
89 double outf;
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;
100 return (int64_t) outf;
103 double verireal::as_double() const
105 return value_;
108 verireal operator+ (const verireal&l, const verireal&r)
110 verireal res;
111 res.value_ = l.value_ + r.value_;
112 return res;
115 verireal operator- (const verireal&l, const verireal&r)
117 verireal res;
118 res.value_ = l.value_ - r.value_;
119 return res;
122 verireal operator* (const verireal&l, const verireal&r)
124 verireal res;
125 res.value_ = l.value_ * r.value_;
126 return res;
129 verireal operator/ (const verireal&l, const verireal&r)
131 verireal res;
132 res.value_ = l.value_ / r.value_;
133 return res;
136 verireal operator/ (const verireal&l, const verinum&r)
138 verireal res;
139 res.value_ = l.value_ / (double)r.as_long();
140 return res;
143 verireal operator% (const verireal&l, const verireal&r)
145 verireal res;
146 assert(0);
147 return res;
150 verireal operator% (const verireal&l, const verinum&r)
152 verireal res;
153 assert(0);
154 return res;
157 verireal pow (const verireal&l, const verireal&r)
159 verireal res;
160 res.value_ = pow(l.value_, r.value_);
161 return res;
164 verireal operator- (const verireal&l)
166 verireal res;
167 res.value_ = - l.value_;
168 return res;
171 ostream& operator<< (ostream&out, const verireal&v)
173 out << v.value_;
174 return out;
178 * $Log: verireal.cc,v $
179 * Revision 1.19 2007/04/07 04:46:18 steve
180 * Handle evaluate of addition of real valued constants.
182 * Revision 1.18 2006/10/03 05:06:00 steve
183 * Support real valued specify delays, properly scaled.
185 * Revision 1.17 2006/08/08 05:11:37 steve
186 * Handle 64bit delay constants.
188 * Revision 1.16 2006/07/31 03:50:18 steve
189 * Add support for power in constant expressions.
191 * Revision 1.15 2004/06/04 23:33:51 steve
192 * Add unary minus as operator supported by verireal.
194 * Revision 1.14 2003/03/07 06:10:13 steve
195 * Use strtod to convert text to doubles.
197 * Revision 1.13 2003/03/05 03:45:01 steve
198 * Restore verireal constructor to match vvp processing of reals.
200 * Revision 1.11 2003/02/07 06:13:44 steve
201 * Store real values as native double.
203 * Revision 1.10 2003/02/07 02:48:43 steve
204 * NetEBDiv handles real value constant expressions.
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.
210 * Revision 1.8 2002/08/12 01:35:01 steve
211 * conditional ident string using autoconfig.
213 * Revision 1.7 2002/06/15 02:35:49 steve
214 * Rounding error.
216 * Revision 1.6 2001/11/06 06:11:55 steve
217 * Support more real arithmetic in delay constants.
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)
223 * Revision 1.4 2001/07/07 20:20:10 steve
224 * Pass parameters to system functions.
226 * Revision 1.3 2000/12/10 22:01:36 steve
227 * Support decimal constants in behavioral delays.
229 * Revision 1.2 2000/02/23 02:56:56 steve
230 * Macintosh compilers do not support ident.
232 * Revision 1.1 1999/06/15 02:50:02 steve
233 * Add lexical support for real numbers.