[PATCH] Implement System Verilog $urandom and $urandom_range functions.
[iverilog.git] / verireal.cc
blobfd0292ea6cec1040042e7cb63c492685c02aea0f
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>
33 verireal::verireal()
35 value_ = 0.0;
38 verireal::verireal(const char*txt)
40 char*tmp = new char[strlen(txt)+1];
41 char*cp = tmp;
42 for (unsigned idx = 0 ; txt[idx] ; idx += 1) {
43 if (txt[idx] == '_')
44 continue;
46 *cp++ = txt[idx];
48 cp[0] = 0;
50 value_ = strtod(tmp, 0);
51 delete[]tmp;
54 verireal::verireal(long val)
56 value_ = (double)val;
59 verireal::verireal(double val)
61 value_ = val;
64 verireal::~verireal()
68 long verireal::as_long(int shift) const
70 double out = value_ * pow(10.0,shift);
71 double outf;
73 if (out >= 0.0) {
74 outf = floor(out);
75 if (out >= (outf + 0.5))
76 outf += 1.0;
77 } else {
78 outf = ceil(out);
79 if (out <= (outf - 0.5))
80 outf -= 1.0;
82 return (long) outf;
85 int64_t verireal::as_long64(int shift) const
87 double out = value_ * pow(10.0,shift);
88 double outf;
90 if (out >= 0.0) {
91 outf = floor(out);
92 if (out >= (outf + 0.5))
93 outf += 1.0;
94 } else {
95 outf = ceil(out);
96 if (out <= (outf - 0.5))
97 outf -= 1.0;
99 return (int64_t) outf;
102 double verireal::as_double() const
104 return value_;
107 verireal operator+ (const verireal&l, const verireal&r)
109 verireal res;
110 res.value_ = l.value_ + r.value_;
111 return res;
114 verireal operator- (const verireal&l, const verireal&r)
116 verireal res;
117 res.value_ = l.value_ - r.value_;
118 return res;
121 verireal operator* (const verireal&l, const verireal&r)
123 verireal res;
124 res.value_ = l.value_ * r.value_;
125 return res;
128 verireal operator/ (const verireal&l, const verireal&r)
130 verireal res;
131 res.value_ = l.value_ / r.value_;
132 return res;
135 verireal operator/ (const verireal&l, const verinum&r)
137 verireal res;
138 res.value_ = l.value_ / (double)r.as_long();
139 return res;
142 verireal operator% (const verireal&l, const verireal&r)
144 verireal res;
145 assert(0);
146 return res;
149 verireal operator% (const verireal&l, const verinum&r)
151 verireal res;
152 assert(0);
153 return res;
156 verireal pow (const verireal&l, const verireal&r)
158 verireal res;
159 res.value_ = pow(l.value_, r.value_);
160 return res;
163 verireal operator- (const verireal&l)
165 verireal res;
166 res.value_ = - l.value_;
167 return res;
170 ostream& operator<< (ostream&out, const verireal&v)
172 out << v.value_;
173 return out;
177 * $Log: verireal.cc,v $
178 * Revision 1.19 2007/04/07 04:46:18 steve
179 * Handle evaluate of addition of real valued constants.
181 * Revision 1.18 2006/10/03 05:06:00 steve
182 * Support real valued specify delays, properly scaled.
184 * Revision 1.17 2006/08/08 05:11:37 steve
185 * Handle 64bit delay constants.
187 * Revision 1.16 2006/07/31 03:50:18 steve
188 * Add support for power in constant expressions.
190 * Revision 1.15 2004/06/04 23:33:51 steve
191 * Add unary minus as operator supported by verireal.
193 * Revision 1.14 2003/03/07 06:10:13 steve
194 * Use strtod to convert text to doubles.
196 * Revision 1.13 2003/03/05 03:45:01 steve
197 * Restore verireal constructor to match vvp processing of reals.
199 * Revision 1.11 2003/02/07 06:13:44 steve
200 * Store real values as native double.
202 * Revision 1.10 2003/02/07 02:48:43 steve
203 * NetEBDiv handles real value constant expressions.
205 * Revision 1.9 2003/01/26 21:15:59 steve
206 * Rework expression parsing and elaboration to
207 * accommodate real/realtime values and expressions.
209 * Revision 1.8 2002/08/12 01:35:01 steve
210 * conditional ident string using autoconfig.
212 * Revision 1.7 2002/06/15 02:35:49 steve
213 * Rounding error.
215 * Revision 1.6 2001/11/06 06:11:55 steve
216 * Support more real arithmetic in delay constants.
218 * Revision 1.5 2001/07/25 03:10:50 steve
219 * Create a config.h.in file to hold all the config
220 * junk, and support gcc 3.0. (Stephan Boettcher)
222 * Revision 1.4 2001/07/07 20:20:10 steve
223 * Pass parameters to system functions.
225 * Revision 1.3 2000/12/10 22:01:36 steve
226 * Support decimal constants in behavioral delays.
228 * Revision 1.2 2000/02/23 02:56:56 steve
229 * Macintosh compilers do not support ident.
231 * Revision 1.1 1999/06/15 02:50:02 steve
232 * Add lexical support for real numbers.