Fix for pr1833776.
[iverilog.git] / verinum.h
blob047f110cd22752e0d7af2e3d1a72505b885fcf9a
1 #ifndef __verinum_H
2 #define __verinum_H
3 /*
4 * Copyright (c) 1998 Stephen Williams (steve@icarus.com)
6 * This source code is free software; you can redistribute it
7 * and/or modify it in source code form under the terms of the GNU
8 * General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
10 * any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
21 #ifdef HAVE_CVS_IDENT
22 #ident "$Id: verinum.h,v 1.33 2007/02/02 04:33:01 steve Exp $"
23 #endif
25 # include <string>
27 # include "config.h"
28 #ifdef HAVE_IOSFWD
29 # include <iosfwd>
30 #else
31 class ostream;
32 #endif
34 using namespace std;
37 * Numbers in Verilog are multibit strings, where each bit has 4
38 * possible values: 0, 1, x or z. The verinum number is store in
39 * little-endian format. This means that if the long value is 2b'10,
40 * get(0) is 0 and get(1) is 1.
42 class verinum {
44 public:
45 enum V { V0 = 0, V1, Vx, Vz };
47 verinum();
48 verinum(const string&str);
49 verinum(const V*v, unsigned nbits, bool has_len =true);
50 verinum(V, unsigned nbits =1, bool has_len =true);
51 verinum(uint64_t val, unsigned bits);
52 verinum(const verinum&);
54 // Create a signed number, with an unspecified number of bits.
55 explicit verinum(int64_t val);
57 // Copy only the specified number of bits from the
58 // source. Also mark this number as has_len.
59 verinum(const verinum&, unsigned bits);
61 ~verinum();
62 verinum& operator= (const verinum&);
64 // Number of significant bits in this number.
65 unsigned len() const { return nbits_; }
67 // A number "has a length" if the length was specified fixed
68 // in some way.
69 bool has_len() const { return has_len_; }
71 bool has_sign(bool flag) { has_sign_ = flag; return has_sign_; }
72 bool has_sign() const { return has_sign_; }
74 // A number is "defined" if there are no x or z bits in its value.
75 bool is_defined() const;
76 bool is_zero() const;
78 // A number is "a string" if its value came directly from
79 // an ASCII description instead of a number value.
80 bool is_string() const { return string_flag_; }
82 // Comparison for use in sorting algorithms.
83 bool is_before(const verinum&that) const;
85 // Individual bits can be accessed with the get and set
86 // methods.
87 V get(unsigned idx) const;
88 V set(unsigned idx, V val);
90 V operator[] (unsigned idx) const { return get(idx); }
93 uint64_t as_ulong64() const;
94 unsigned long as_ulong() const;
95 signed long as_long() const;
96 string as_string() const;
98 private:
99 V* bits_;
100 unsigned nbits_;
101 bool has_len_;
102 bool has_sign_;
104 // These are some convenience flags that help us do a better
105 // job of pretty-printing values.
106 bool string_flag_;
109 /* Return a verinum that has the same value as the input, but is at
110 least as wide as the requested width. This may involve sign
111 extension, if the value is signed. */
112 extern verinum pad_to_width(const verinum&, unsigned width);
114 /* Return a verinum that is minimal. That is, it has only the length
115 needed to accurately represent the contained value, signed or not. */
116 extern verinum trim_vnum(const verinum&);
118 extern ostream& operator<< (ostream&, const verinum&);
119 extern ostream& operator<< (ostream&, verinum::V);
121 extern verinum::V operator ~ (verinum::V l);
122 extern verinum::V operator | (verinum::V l, verinum::V r);
123 extern verinum::V operator & (verinum::V l, verinum::V r);
124 extern verinum::V operator ^ (verinum::V l, verinum::V r);
126 extern verinum::V operator == (const verinum&left, const verinum&right);
127 extern verinum::V operator <= (const verinum&left, const verinum&right);
128 extern verinum::V operator < (const verinum&left, const verinum&right);
130 inline verinum::V operator > (const verinum&left, const verinum&right)
131 { return right < left; }
133 inline verinum::V operator >= (const verinum&left, const verinum&right)
134 { return right <= left; }
136 inline verinum::V operator != (const verinum&left, const verinum&right)
137 { return (left == right)? verinum::V0 : verinum::V1; }
140 /* These are arithmetic operators. These generally work to produce
141 results that do not overflow. That means the result may expand or
142 contract to hold the bits needed to hold the operation results
143 accurately. It is up to the caller to truncate or pad if a specific
144 width is expected. */
145 extern verinum operator + (const verinum&left, const verinum&right);
146 extern verinum operator - (const verinum&left, const verinum&right);
147 extern verinum operator * (const verinum&left, const verinum&right);
148 extern verinum operator / (const verinum&left, const verinum&right);
149 extern verinum operator % (const verinum&left, const verinum&right);
151 extern verinum pow(const verinum&left, const verinum&right);
153 extern verinum operator<< (const verinum&left, unsigned shift);
154 extern verinum operator>> (const verinum&left, unsigned shift);
156 extern verinum concat(const verinum&left, const verinum&right);
158 /* Bitwise not returns the ones complement. */
159 extern verinum v_not(const verinum&left);
162 * $Log: verinum.h,v $
163 * Revision 1.33 2007/02/02 04:33:01 steve
164 * Use inttypes.h instead of stdint.h for portability.
166 * Revision 1.32 2006/08/08 05:11:37 steve
167 * Handle 64bit delay constants.
169 * Revision 1.31 2006/07/31 03:50:17 steve
170 * Add support for power in constant expressions.
172 * Revision 1.30 2006/06/02 04:48:50 steve
173 * Make elaborate_expr methods aware of the width that the context
174 * requires of it. In the process, fix sizing of the width of unary
175 * minus is context determined sizes.
177 * Revision 1.29 2006/06/01 03:54:51 steve
178 * Fix broken subtraction of small constants.
180 * Revision 1.28 2005/12/07 04:04:24 steve
181 * Allow constant concat expressions.
183 * Revision 1.27 2005/06/14 19:13:43 steve
184 * gcc3/4 compile errors.
186 * Revision 1.26 2004/02/17 06:52:55 steve
187 * Support unsigned divide of huge numbers.
189 * Revision 1.25 2003/10/26 04:54:56 steve
190 * Support constant evaluation of binary ^ operator.
192 * Revision 1.24 2003/04/14 03:40:21 steve
193 * Make some effort to preserve bits while
194 * operating on constant values.
196 * Revision 1.23 2003/04/03 04:30:00 steve
197 * Prevent overrun comparing verinums to zero.
199 * Revision 1.22 2003/01/30 16:23:08 steve
200 * Spelling fixes.
202 * Revision 1.21 2003/01/30 04:23:25 steve
203 * include config.h to get iosfwd flags.
205 * Revision 1.20 2002/08/12 01:35:01 steve
206 * conditional ident string using autoconfig.
208 * Revision 1.19 2002/06/03 04:04:24 steve
209 * Add verinum != operator.
211 * Revision 1.18 2001/02/10 20:29:39 steve
212 * In the context of range declarations, use elab_and_eval instead
213 * of the less robust eval_const methods.
215 * Revision 1.17 2001/02/09 05:44:23 steve
216 * support evaluation of constant < in expressions.
218 * Revision 1.16 2001/02/07 02:46:31 steve
219 * Support constant evaluation of / and % (PR#124)
221 * Revision 1.15 2001/01/16 02:44:18 steve
222 * Use the iosfwd header if available.
224 * Revision 1.14 2001/01/02 03:23:40 steve
225 * Evaluate constant &, | and unary ~.
227 * Revision 1.13 2000/12/10 22:01:36 steve
228 * Support decimal constants in behavioral delays.
230 * Revision 1.12 2000/09/27 18:28:37 steve
231 * multiply in parameter expressions.
233 * Revision 1.11 2000/02/23 04:43:43 steve
234 * Some compilers do not accept the not symbol.
236 * Revision 1.10 2000/02/23 02:56:56 steve
237 * Macintosh compilers do not support ident.
239 * Revision 1.9 2000/01/07 03:45:49 steve
240 * Initial support for signed constants.
242 * Revision 1.8 1999/11/06 16:00:17 steve
243 * Put number constants into a static table.
245 * Revision 1.7 1999/10/22 23:57:53 steve
246 * do the <= in bits, not numbers.
248 * Revision 1.6 1999/10/10 23:29:37 steve
249 * Support evaluating + operator at compile time.
251 * Revision 1.5 1999/05/13 04:02:09 steve
252 * More precise handling of verinum bit lengths.
254 * Revision 1.4 1998/12/20 02:05:41 steve
255 * Function to calculate wire initial value.
257 * Revision 1.3 1998/11/11 00:01:51 steve
258 * Check net ranges in declarations.
260 * Revision 1.2 1998/11/09 18:55:35 steve
261 * Add procedural while loops,
262 * Parse procedural for loops,
263 * Add procedural wait statements,
264 * Add constant nodes,
265 * Add XNOR logic gate,
266 * Make vvm output look a bit prettier.
268 * Revision 1.1 1998/11/03 23:29:08 steve
269 * Introduce verilog to CVS.
272 #endif