Skip to content
Newer
Older
100644 130 lines (111 sloc) 2.68 KB
430d7b2 Add lexical support for real numbers.
steve authored Jun 15, 1999
1 /*
ae14a58 @caryr V0.8: Remove deprecated #ident and fix some other compile warnings
caryr authored Dec 13, 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"
21
430d7b2 Add lexical support for real numbers.
steve authored Jun 15, 1999
22 # include "verireal.h"
f8d1e15 NetEBDiv handles real value constant expressions.
steve authored Feb 7, 2003
23 # include "verinum.h"
d49e445 Fix reported build errors with gcc 4.3
Stephen Williams authored Jan 21, 2008
24 # include <cstdlib>
25 # include <cstring>
430d7b2 Add lexical support for real numbers.
steve authored Jun 15, 1999
26 # include <ctype.h>
084a464 Support decimal constants in behavioral delays.
steve authored Dec 10, 2000
27 # include <iostream>
46253ed Rework expression parsing and elaboration to
steve authored Jan 26, 2003
28 # include <math.h>
430d7b2 Add lexical support for real numbers.
steve authored Jun 15, 1999
29 # include <assert.h>
30
31 verireal::verireal()
32 {
79b2beb Store real values as native double.
steve authored Feb 7, 2003
33 value_ = 0.0;
430d7b2 Add lexical support for real numbers.
steve authored Jun 15, 1999
34 }
35
36 verireal::verireal(const char*txt)
37 {
12d432d Use strtod to convert text to doubles.
steve authored Mar 7, 2003
38 char*tmp = new char[strlen(txt)+1];
39 char*cp = tmp;
40 for (unsigned idx = 0 ; txt[idx] ; idx += 1) {
41 if (txt[idx] == '_')
42 continue;
430d7b2 Add lexical support for real numbers.
steve authored Jun 15, 1999
43
12d432d Use strtod to convert text to doubles.
steve authored Mar 7, 2003
44 *cp++ = txt[idx];
e6e0396 Restore verireal constructor to match vvp processing of reals.
steve authored Mar 5, 2003
45 }
12d432d Use strtod to convert text to doubles.
steve authored Mar 7, 2003
46 cp[0] = 0;
e6e0396 Restore verireal constructor to match vvp processing of reals.
steve authored Mar 5, 2003
47
12d432d Use strtod to convert text to doubles.
steve authored Mar 7, 2003
48 value_ = strtod(tmp, 0);
49 delete[]tmp;
430d7b2 Add lexical support for real numbers.
steve authored Jun 15, 1999
50 }
51
9572ddd Support more real arithmetic in delay constants.
steve authored Nov 6, 2001
52 verireal::verireal(long val)
53 {
79b2beb Store real values as native double.
steve authored Feb 7, 2003
54 value_ = (double)val;
9572ddd Support more real arithmetic in delay constants.
steve authored Nov 6, 2001
55 }
430d7b2 Add lexical support for real numbers.
steve authored Jun 15, 1999
56
57 verireal::~verireal()
58 {
59 }
60
084a464 Support decimal constants in behavioral delays.
steve authored Dec 10, 2000
61 long verireal::as_long(int shift) const
62 {
79b2beb Store real values as native double.
steve authored Feb 7, 2003
63 double out = value_ * pow(10.0,shift);
64 double outf;
65
66 if (out >= 0.0) {
67 outf = floor(out);
68 if (out >= (outf + 0.5))
69 outf += 1.0;
70 } else {
71 outf = ceil(out);
72 if (out <= (outf - 0.5))
73 outf -= 1.0;
084a464 Support decimal constants in behavioral delays.
steve authored Dec 10, 2000
74 }
79b2beb Store real values as native double.
steve authored Feb 7, 2003
75 return (long) outf;
084a464 Support decimal constants in behavioral delays.
steve authored Dec 10, 2000
76 }
77
46253ed Rework expression parsing and elaboration to
steve authored Jan 26, 2003
78 double verireal::as_double() const
79 {
79b2beb Store real values as native double.
steve authored Feb 7, 2003
80 return value_;
46253ed Rework expression parsing and elaboration to
steve authored Jan 26, 2003
81 }
82
9572ddd Support more real arithmetic in delay constants.
steve authored Nov 6, 2001
83 verireal operator* (const verireal&l, const verireal&r)
84 {
85 verireal res;
79b2beb Store real values as native double.
steve authored Feb 7, 2003
86 res.value_ = l.value_ * r.value_;
9572ddd Support more real arithmetic in delay constants.
steve authored Nov 6, 2001
87 return res;
88 }
89
f8d1e15 NetEBDiv handles real value constant expressions.
steve authored Feb 7, 2003
90 verireal operator/ (const verireal&l, const verireal&r)
91 {
92 verireal res;
79b2beb Store real values as native double.
steve authored Feb 7, 2003
93 res.value_ = l.value_ / r.value_;
f8d1e15 NetEBDiv handles real value constant expressions.
steve authored Feb 7, 2003
94 return res;
95 }
96
97 verireal operator/ (const verireal&l, const verinum&r)
98 {
99 verireal res;
79b2beb Store real values as native double.
steve authored Feb 7, 2003
100 res.value_ = l.value_ / (double)r.as_long();
f8d1e15 NetEBDiv handles real value constant expressions.
steve authored Feb 7, 2003
101 return res;
102 }
103
104 verireal operator% (const verireal&l, const verireal&r)
105 {
106 verireal res;
107 assert(0);
108 return res;
109 }
110
111 verireal operator% (const verireal&l, const verinum&r)
112 {
113 verireal res;
114 assert(0);
115 return res;
116 }
117
fbfc796 Add unary minus as operator supported by verireal.
steve authored Jun 4, 2004
118 verireal operator- (const verireal&l)
119 {
120 verireal res;
121 res.value_ = - l.value_;
122 return res;
123 }
124
8c73fa7 Fix compilation warnings/errors with newer compilers.
steve authored Aug 13, 2005
125 std::ostream& operator<< (std::ostream&out, const verireal&v)
084a464 Support decimal constants in behavioral delays.
steve authored Dec 10, 2000
126 {
79b2beb Store real values as native double.
steve authored Feb 7, 2003
127 out << v.value_;
8cc89a3 Pass parameters to system functions.
steve authored Jul 7, 2001
128 return out;
084a464 Support decimal constants in behavioral delays.
steve authored Dec 10, 2000
129 }
Something went wrong with that request. Please try again.