Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 180 lines (154 sloc) 4.097 kb
430d7b2 Add lexical support for real numbers.
steve authored
1 /*
2 * Copyright (c) 1999 Stephen Williams (steve@icarus.com)
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 */
52bf4e6 conditional ident string using autoconfig.
steve authored
19 #ifdef HAVE_CVS_IDENT
46253ed Rework expression parsing and elaboration to
steve authored
20 #ident "$Id: verireal.cc,v 1.9 2003/01/26 21:15:59 steve Exp $"
430d7b2 Add lexical support for real numbers.
steve authored
21 #endif
22
b825f8d Create a config.h.in file to hold all the config
steve authored
23 # include "config.h"
24
430d7b2 Add lexical support for real numbers.
steve authored
25 # include "verireal.h"
26 # include <ctype.h>
084a464 Support decimal constants in behavioral delays.
steve authored
27 # include <iostream>
46253ed Rework expression parsing and elaboration to
steve authored
28 # include <math.h>
430d7b2 Add lexical support for real numbers.
steve authored
29 # include <assert.h>
30
31 verireal::verireal()
32 {
33 sign_ = false;
34 mant_ = 0;
35 exp10_ = 0;
36 }
37
38 verireal::verireal(const char*txt)
39 {
40 mant_ = 0;
41 sign_ = false;
42 exp10_ = 0;
43
44 const char*ptr = txt;
45 for ( ; *ptr ; ptr += 1) {
46 if (*ptr == '.') break;
47 if (*ptr == 'e') break;
48 if (*ptr == 'E') break;
49 if (*ptr == '_') continue;
50
51 assert(isdigit(*ptr));
52
53 mant_ *= 10;
54 mant_ += *ptr - '0';
55 }
56
57 if (*ptr == '.') {
58 ptr += 1;
59 for ( ; *ptr ; ptr += 1) {
60 if (*ptr == 'e') break;
61 if (*ptr == 'E') break;
62 if (*ptr == '_') continue;
63
64 assert(isdigit(*ptr));
65
66 mant_ *= 10;
67 mant_ += *ptr - '0';
68 exp10_ -= 1;
69 }
70 }
71
72 if ((*ptr == 'e') || (*ptr == 'E')) {
73 ptr += 1;
74 long tmp = 0;
75 bool sflag = false;
76 if (*ptr == '+') {ptr += 1; sflag = false;}
77 if (*ptr == '-') {ptr += 1; sflag = true;}
78
79 for ( ; *ptr ; ptr += 1) {
80 if (*ptr == '_') continue;
81 assert(isdigit(*ptr));
82 tmp *= 10;
83 tmp += *ptr - '0';
84 }
85
86 exp10_ += sflag? -tmp : +tmp;
87 }
88
89 assert(*ptr == 0);
90 }
91
9572ddd Support more real arithmetic in delay constants.
steve authored
92 verireal::verireal(long val)
93 {
94 sign_ = val < 0;
95 mant_ = sign_? -val : +val;
96 exp10_ = 0;
97 }
430d7b2 Add lexical support for real numbers.
steve authored
98
99 verireal::~verireal()
100 {
101 }
102
084a464 Support decimal constants in behavioral delays.
steve authored
103 long verireal::as_long(int shift) const
104 {
105 long val = mant_;
106 int ex = exp10_ + shift;
107
108 while (ex < 0) {
109 long mod = val % 10;
110 val /= 10;
067d097 Rounding error.
steve authored
111 if (ex == -1 && mod >= 5)
084a464 Support decimal constants in behavioral delays.
steve authored
112 val += 1;
113 ex += 1;
114 }
115
116 while (ex > 0) {
117 val *= 10;
118 ex -= 1;
119 }
120
121 if (sign_)
122 return -val;
123 else
124 return val;
125 }
126
46253ed Rework expression parsing and elaboration to
steve authored
127 double verireal::as_double() const
128 {
129 return mant_ * pow(10.0, exp10_) * (sign_? -1 : 1);
130 }
131
9572ddd Support more real arithmetic in delay constants.
steve authored
132 verireal operator* (const verireal&l, const verireal&r)
133 {
134 verireal res;
135 res.sign_ = l.sign_ != r.sign_;
136 res.mant_ = l.mant_ * r.mant_;
137 res.exp10_= l.exp10_ + r.exp10_;
138 return res;
139 }
140
084a464 Support decimal constants in behavioral delays.
steve authored
141 ostream& operator<< (ostream&out, const verireal&v)
142 {
143 out << (v.sign_? "-" : "+") << v.mant_ << "e" << v.exp10_;
8cc89a3 Pass parameters to system functions.
steve authored
144 return out;
084a464 Support decimal constants in behavioral delays.
steve authored
145 }
146
430d7b2 Add lexical support for real numbers.
steve authored
147 /*
148 * $Log: verireal.cc,v $
46253ed Rework expression parsing and elaboration to
steve authored
149 * Revision 1.9 2003/01/26 21:15:59 steve
150 * Rework expression parsing and elaboration to
151 * accommodate real/realtime values and expressions.
152 *
52bf4e6 conditional ident string using autoconfig.
steve authored
153 * Revision 1.8 2002/08/12 01:35:01 steve
154 * conditional ident string using autoconfig.
155 *
067d097 Rounding error.
steve authored
156 * Revision 1.7 2002/06/15 02:35:49 steve
157 * Rounding error.
158 *
9572ddd Support more real arithmetic in delay constants.
steve authored
159 * Revision 1.6 2001/11/06 06:11:55 steve
160 * Support more real arithmetic in delay constants.
161 *
b825f8d Create a config.h.in file to hold all the config
steve authored
162 * Revision 1.5 2001/07/25 03:10:50 steve
163 * Create a config.h.in file to hold all the config
164 * junk, and support gcc 3.0. (Stephan Boettcher)
165 *
8cc89a3 Pass parameters to system functions.
steve authored
166 * Revision 1.4 2001/07/07 20:20:10 steve
167 * Pass parameters to system functions.
168 *
084a464 Support decimal constants in behavioral delays.
steve authored
169 * Revision 1.3 2000/12/10 22:01:36 steve
170 * Support decimal constants in behavioral delays.
171 *
b734ecf Macintosh compilers do not support ident.
steve authored
172 * Revision 1.2 2000/02/23 02:56:56 steve
173 * Macintosh compilers do not support ident.
174 *
430d7b2 Add lexical support for real numbers.
steve authored
175 * Revision 1.1 1999/06/15 02:50:02 steve
176 * Add lexical support for real numbers.
177 *
178 */
179
Something went wrong with that request. Please try again.