Skip to content
Newer
Older
100644 180 lines (155 sloc) 3.8 KB
430d7b2 Add lexical support for real numbers.
steve authored Jun 15, 1999
1 /*
1993bf6 @caryr Remove malloc.h support and for C++ files use <c...> include files.
caryr authored May 31, 2010
2 * Copyright (c) 1999-2010 Stephen Williams (steve@icarus.com)
430d7b2 Add lexical support for real numbers.
steve authored Jun 15, 1999
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 */
19
b825f8d Create a config.h.in file to hold all the config
steve authored Jul 25, 2001
20 # include "config.h"
5cf7e26 @caryr Finish real modulus in verireal.
caryr authored Jan 9, 2009
21 # include "compiler.h"
b825f8d Create a config.h.in file to hold all the config
steve authored Jul 25, 2001
22
430d7b2 Add lexical support for real numbers.
steve authored Jun 15, 1999
23 # include "verireal.h"
f8d1e15 NetEBDiv handles real value constant expressions.
steve authored Feb 7, 2003
24 # include "verinum.h"
1993bf6 @caryr Remove malloc.h support and for C++ files use <c...> include files.
caryr authored May 31, 2010
25 # include <cstdlib>
26 # include <cctype>
084a464 Support decimal constants in behavioral delays.
steve authored Dec 10, 2000
27 # include <iostream>
1993bf6 @caryr Remove malloc.h support and for C++ files use <c...> include files.
caryr authored May 31, 2010
28 # include <cmath>
29 # include <cassert>
8ea3b6b @ldoolitt header includes for gcc-4.3 compatibility
ldoolitt authored Jan 4, 2008
30 # include <cstring>
430d7b2 Add lexical support for real numbers.
steve authored Jun 15, 1999
31
32 verireal::verireal()
33 {
79b2beb Store real values as native double.
steve authored Feb 7, 2003
34 value_ = 0.0;
430d7b2 Add lexical support for real numbers.
steve authored Jun 15, 1999
35 }
36
37 verireal::verireal(const char*txt)
38 {
12d432d Use strtod to convert text to doubles.
steve authored Mar 7, 2003
39 char*tmp = new char[strlen(txt)+1];
40 char*cp = tmp;
41 for (unsigned idx = 0 ; txt[idx] ; idx += 1) {
42 if (txt[idx] == '_')
43 continue;
430d7b2 Add lexical support for real numbers.
steve authored Jun 15, 1999
44
12d432d Use strtod to convert text to doubles.
steve authored Mar 7, 2003
45 *cp++ = txt[idx];
e6e0396 Restore verireal constructor to match vvp processing of reals.
steve authored Mar 5, 2003
46 }
12d432d Use strtod to convert text to doubles.
steve authored Mar 7, 2003
47 cp[0] = 0;
e6e0396 Restore verireal constructor to match vvp processing of reals.
steve authored Mar 5, 2003
48
12d432d Use strtod to convert text to doubles.
steve authored Mar 7, 2003
49 value_ = strtod(tmp, 0);
50 delete[]tmp;
430d7b2 Add lexical support for real numbers.
steve authored Jun 15, 1999
51 }
52
9572ddd Support more real arithmetic in delay constants.
steve authored Nov 6, 2001
53 verireal::verireal(long val)
54 {
79b2beb Store real values as native double.
steve authored Feb 7, 2003
55 value_ = (double)val;
9572ddd Support more real arithmetic in delay constants.
steve authored Nov 6, 2001
56 }
430d7b2 Add lexical support for real numbers.
steve authored Jun 15, 1999
57
69cd007 Support real valued specify delays, properly scaled.
steve authored Oct 3, 2006
58 verireal::verireal(double val)
59 {
60 value_ = val;
61 }
62
430d7b2 Add lexical support for real numbers.
steve authored Jun 15, 1999
63 verireal::~verireal()
64 {
65 }
66
084a464 Support decimal constants in behavioral delays.
steve authored Dec 10, 2000
67 long verireal::as_long(int shift) const
68 {
79b2beb Store real values as native double.
steve authored Feb 7, 2003
69 double out = value_ * pow(10.0,shift);
70 double outf;
71
72 if (out >= 0.0) {
73 outf = floor(out);
74 if (out >= (outf + 0.5))
75 outf += 1.0;
76 } else {
77 outf = ceil(out);
78 if (out <= (outf - 0.5))
79 outf -= 1.0;
084a464 Support decimal constants in behavioral delays.
steve authored Dec 10, 2000
80 }
79b2beb Store real values as native double.
steve authored Feb 7, 2003
81 return (long) outf;
084a464 Support decimal constants in behavioral delays.
steve authored Dec 10, 2000
82 }
83
fc0695b Handle 64bit delay constants.
steve authored Aug 8, 2006
84 int64_t verireal::as_long64(int shift) const
85 {
86 double out = value_ * pow(10.0,shift);
87 double outf;
88
89 if (out >= 0.0) {
90 outf = floor(out);
91 if (out >= (outf + 0.5))
92 outf += 1.0;
93 } else {
94 outf = ceil(out);
95 if (out <= (outf - 0.5))
96 outf -= 1.0;
97 }
98 return (int64_t) outf;
99 }
100
46253ed Rework expression parsing and elaboration to
steve authored Jan 26, 2003
101 double verireal::as_double() const
102 {
79b2beb Store real values as native double.
steve authored Feb 7, 2003
103 return value_;
46253ed Rework expression parsing and elaboration to
steve authored Jan 26, 2003
104 }
105
33e5f22 Handle evaluate of addition of real valued constants.
steve authored Apr 7, 2007
106 verireal operator+ (const verireal&l, const verireal&r)
107 {
108 verireal res;
109 res.value_ = l.value_ + r.value_;
110 return res;
111 }
112
113 verireal operator- (const verireal&l, const verireal&r)
114 {
115 verireal res;
116 res.value_ = l.value_ - r.value_;
117 return res;
118 }
119
9572ddd Support more real arithmetic in delay constants.
steve authored Nov 6, 2001
120 verireal operator* (const verireal&l, const verireal&r)
121 {
122 verireal res;
79b2beb Store real values as native double.
steve authored Feb 7, 2003
123 res.value_ = l.value_ * r.value_;
9572ddd Support more real arithmetic in delay constants.
steve authored Nov 6, 2001
124 return res;
125 }
126
f8d1e15 NetEBDiv handles real value constant expressions.
steve authored Feb 7, 2003
127 verireal operator/ (const verireal&l, const verireal&r)
128 {
129 verireal res;
79b2beb Store real values as native double.
steve authored Feb 7, 2003
130 res.value_ = l.value_ / r.value_;
f8d1e15 NetEBDiv handles real value constant expressions.
steve authored Feb 7, 2003
131 return res;
132 }
133
134 verireal operator/ (const verireal&l, const verinum&r)
135 {
136 verireal res;
79b2beb Store real values as native double.
steve authored Feb 7, 2003
137 res.value_ = l.value_ / (double)r.as_long();
f8d1e15 NetEBDiv handles real value constant expressions.
steve authored Feb 7, 2003
138 return res;
139 }
140
141 verireal operator% (const verireal&l, const verireal&r)
142 {
143 verireal res;
5cf7e26 @caryr Finish real modulus in verireal.
caryr authored Jan 9, 2009
144 // Modulus of a real value is not supported by the standard,
145 // but we support it as an extension. Assert that we are in
146 // the correct state before doing the operation.
147 assert(gn_icarus_misc_flag);
148 res.value_ = fmod(l.value_, r.value_);
f8d1e15 NetEBDiv handles real value constant expressions.
steve authored Feb 7, 2003
149 return res;
150 }
151
152 verireal operator% (const verireal&l, const verinum&r)
153 {
154 verireal res;
5cf7e26 @caryr Finish real modulus in verireal.
caryr authored Jan 9, 2009
155 // See above.
156 assert(gn_icarus_misc_flag);
157 res.value_ = fmod(l.value_, (double)r.as_long());
f8d1e15 NetEBDiv handles real value constant expressions.
steve authored Feb 7, 2003
158 return res;
159 }
160
49b65e8 Add support for power in constant expressions.
steve authored Jul 31, 2006
161 verireal pow (const verireal&l, const verireal&r)
162 {
163 verireal res;
164 res.value_ = pow(l.value_, r.value_);
165 return res;
166 }
167
fbfc796 Add unary minus as operator supported by verireal.
steve authored Jun 4, 2004
168 verireal operator- (const verireal&l)
169 {
170 verireal res;
171 res.value_ = - l.value_;
172 return res;
173 }
174
084a464 Support decimal constants in behavioral delays.
steve authored Dec 10, 2000
175 ostream& operator<< (ostream&out, const verireal&v)
176 {
74ea0ec @caryr Show verireal constants with a decimal point.
caryr authored Apr 2, 2009
177 out << showpoint << v.value_;
8cc89a3 Pass parameters to system functions.
steve authored Jul 7, 2001
178 return out;
084a464 Support decimal constants in behavioral delays.
steve authored Dec 10, 2000
179 }
Something went wrong with that request. Please try again.