Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 180 lines (155 sloc) 3.898 kB
430d7b2 Add lexical support for real numbers.
steve authored
1 /*
1993bf6 @caryr Remove malloc.h support and for C++ files use <c...> include files.
caryr authored
2 * Copyright (c) 1999-2010 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
f5aafc3 @arunpersaud updated FSF-address
arunpersaud authored
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
430d7b2 Add lexical support for real numbers.
steve authored
18 */
19
b825f8d Create a config.h.in file to hold all the config
steve authored
20 # include "config.h"
5cf7e26 @caryr Finish real modulus in verireal.
caryr authored
21 # include "compiler.h"
b825f8d Create a config.h.in file to hold all the config
steve authored
22
430d7b2 Add lexical support for real numbers.
steve authored
23 # include "verireal.h"
f8d1e15 NetEBDiv handles real value constant expressions.
steve authored
24 # include "verinum.h"
1993bf6 @caryr Remove malloc.h support and for C++ files use <c...> include files.
caryr authored
25 # include <cstdlib>
26 # include <cctype>
084a464 Support decimal constants in behavioral delays.
steve authored
27 # include <iostream>
1993bf6 @caryr Remove malloc.h support and for C++ files use <c...> include files.
caryr authored
28 # include <cmath>
29 # include <cassert>
8ea3b6b @ldoolitt header includes for gcc-4.3 compatibility
ldoolitt authored
30 # include <cstring>
430d7b2 Add lexical support for real numbers.
steve authored
31
32 verireal::verireal()
33 {
79b2beb Store real values as native double.
steve authored
34 value_ = 0.0;
430d7b2 Add lexical support for real numbers.
steve authored
35 }
36
37 verireal::verireal(const char*txt)
38 {
12d432d Use strtod to convert text to doubles.
steve authored
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
44
12d432d Use strtod to convert text to doubles.
steve authored
45 *cp++ = txt[idx];
e6e0396 Restore verireal constructor to match vvp processing of reals.
steve authored
46 }
12d432d Use strtod to convert text to doubles.
steve authored
47 cp[0] = 0;
e6e0396 Restore verireal constructor to match vvp processing of reals.
steve authored
48
12d432d Use strtod to convert text to doubles.
steve authored
49 value_ = strtod(tmp, 0);
50 delete[]tmp;
430d7b2 Add lexical support for real numbers.
steve authored
51 }
52
9572ddd Support more real arithmetic in delay constants.
steve authored
53 verireal::verireal(long val)
54 {
79b2beb Store real values as native double.
steve authored
55 value_ = (double)val;
9572ddd Support more real arithmetic in delay constants.
steve authored
56 }
430d7b2 Add lexical support for real numbers.
steve authored
57
69cd007 Support real valued specify delays, properly scaled.
steve authored
58 verireal::verireal(double val)
59 {
60 value_ = val;
61 }
62
430d7b2 Add lexical support for real numbers.
steve authored
63 verireal::~verireal()
64 {
65 }
66
084a464 Support decimal constants in behavioral delays.
steve authored
67 long verireal::as_long(int shift) const
68 {
79b2beb Store real values as native double.
steve authored
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
80 }
79b2beb Store real values as native double.
steve authored
81 return (long) outf;
084a464 Support decimal constants in behavioral delays.
steve authored
82 }
83
fc0695b Handle 64bit delay constants.
steve authored
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
101 double verireal::as_double() const
102 {
79b2beb Store real values as native double.
steve authored
103 return value_;
46253ed Rework expression parsing and elaboration to
steve authored
104 }
105
33e5f22 Handle evaluate of addition of real valued constants.
steve authored
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
120 verireal operator* (const verireal&l, const verireal&r)
121 {
122 verireal res;
79b2beb Store real values as native double.
steve authored
123 res.value_ = l.value_ * r.value_;
9572ddd Support more real arithmetic in delay constants.
steve authored
124 return res;
125 }
126
f8d1e15 NetEBDiv handles real value constant expressions.
steve authored
127 verireal operator/ (const verireal&l, const verireal&r)
128 {
129 verireal res;
79b2beb Store real values as native double.
steve authored
130 res.value_ = l.value_ / r.value_;
f8d1e15 NetEBDiv handles real value constant expressions.
steve authored
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
137 res.value_ = l.value_ / (double)r.as_long();
f8d1e15 NetEBDiv handles real value constant expressions.
steve authored
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
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
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
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
158 return res;
159 }
160
49b65e8 Add support for power in constant expressions.
steve authored
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
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
175 ostream& operator<< (ostream&out, const verireal&v)
176 {
74ea0ec @caryr Show verireal constants with a decimal point.
caryr authored
177 out << showpoint << v.value_;
8cc89a3 Pass parameters to system functions.
steve authored
178 return out;
084a464 Support decimal constants in behavioral delays.
steve authored
179 }
Something went wrong with that request. Please try again.