Clean up warnings for round() function declaration.
[iverilog.git] / verinum.cc
blob7b26a33226087755923bd53b429f462408688135
1 /*
2 * Copyright (c) 1998-2000 Stephen Williams (steve@icarus.com)
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.
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.
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
19 #ifdef HAVE_CVS_IDENT
20 #ident "$Id: verinum.cc,v 1.52 2007/02/25 23:08:24 steve Exp $"
21 #endif
23 # include "config.h"
25 # include "verinum.h"
26 # include <iostream>
27 # include <cassert>
29 verinum::verinum()
30 : bits_(0), nbits_(0), has_len_(false), has_sign_(false), string_flag_(false)
34 verinum::verinum(const V*bits, unsigned nbits, bool has_len)
35 : has_len_(has_len), has_sign_(false), string_flag_(false)
37 nbits_ = nbits;
38 bits_ = new V [nbits];
39 for (unsigned idx = 0 ; idx < nbits ; idx += 1) {
40 bits_[idx] = bits[idx];
44 static string process_verilog_string_quotes(const string&str)
46 string res;
48 int idx = 0;
49 int str_len = str.length();
51 while (idx < str_len) {
52 if (str[idx] == '\\') {
53 idx += 1;
54 assert(idx < str_len);
55 switch (str[idx]) {
56 case 'n':
57 res = res + '\n';
58 idx += 1;
59 break;
60 case 't':
61 res = res + '\t';
62 idx += 1;
63 break;
64 case '0':
65 case '1':
66 case '2':
67 case '3':
68 case '4':
69 case '5':
70 case '6':
71 case '7': {
72 char byte_val = 0;
73 int odx = 0;
74 while (odx < 3 && idx+odx < str_len
75 && str[idx+odx] >= '0'
76 && str[idx+odx] <= '7') {
77 byte_val = 8*byte_val + str[idx+odx]-'0';
78 odx += 1;
80 idx += odx;
81 res = res + byte_val;
82 break;
84 default:
85 res = res + str[idx];
86 idx += 1;
87 break;
89 } else {
90 res = res + str[idx];
91 idx += 1;
94 return res;
97 verinum::verinum(const string&s)
98 : has_len_(true), has_sign_(false), string_flag_(true)
100 string str = process_verilog_string_quotes(s);
101 nbits_ = str.length() * 8;
103 // Special case: The string "" is 8 bits of 0.
104 if (nbits_ == 0) {
105 nbits_ = 8;
106 bits_ = new V [nbits_];
107 bits_[0] = V0;
108 bits_[1] = V0;
109 bits_[2] = V0;
110 bits_[3] = V0;
111 bits_[4] = V0;
112 bits_[5] = V0;
113 bits_[6] = V0;
114 bits_[7] = V0;
115 return;
118 bits_ = new V [nbits_];
120 unsigned idx, cp;
121 V*bp = bits_+nbits_;
122 for (idx = nbits_, cp = 0 ; idx > 0 ; idx -= 8, cp += 1) {
123 char ch = str[cp];
124 *(--bp) = (ch&0x80) ? V1 : V0;
125 *(--bp) = (ch&0x40) ? V1 : V0;
126 *(--bp) = (ch&0x20) ? V1 : V0;
127 *(--bp) = (ch&0x10) ? V1 : V0;
128 *(--bp) = (ch&0x08) ? V1 : V0;
129 *(--bp) = (ch&0x04) ? V1 : V0;
130 *(--bp) = (ch&0x02) ? V1 : V0;
131 *(--bp) = (ch&0x01) ? V1 : V0;
135 verinum::verinum(verinum::V val, unsigned n, bool h)
136 : has_len_(h), has_sign_(false), string_flag_(false)
138 nbits_ = n;
139 bits_ = new V[nbits_];
140 for (unsigned idx = 0 ; idx < nbits_ ; idx += 1)
141 bits_[idx] = val;
144 verinum::verinum(uint64_t val, unsigned n)
145 : has_len_(true), has_sign_(false), string_flag_(false)
147 nbits_ = n;
148 bits_ = new V[nbits_];
149 for (unsigned idx = 0 ; idx < nbits_ ; idx += 1) {
150 bits_[idx] = (val&1) ? V1 : V0;
151 val >>= (uint64_t)1;
155 verinum::verinum(const verinum&that)
157 string_flag_ = that.string_flag_;
158 nbits_ = that.nbits_;
159 bits_ = new V[nbits_];
160 has_len_ = that.has_len_;
161 has_sign_ = that.has_sign_;
162 for (unsigned idx = 0 ; idx < nbits_ ; idx += 1)
163 bits_[idx] = that.bits_[idx];
166 verinum::verinum(const verinum&that, unsigned nbits)
168 string_flag_ = false;
169 nbits_ = nbits;
170 bits_ = new V[nbits_];
171 has_len_ = true;
172 has_sign_ = that.has_sign_;
174 unsigned copy = nbits;
175 if (copy > that.nbits_)
176 copy = that.nbits_;
177 for (unsigned idx = 0 ; idx < copy ; idx += 1)
178 bits_[idx] = that.bits_[idx];
180 if (copy < nbits_) {
181 if (has_sign_) {
182 for (unsigned idx = copy ; idx < nbits_ ; idx += 1)
183 bits_[idx] = bits_[idx-1];
184 } else {
185 for (unsigned idx = copy ; idx < nbits_ ; idx += 1)
186 bits_[idx] = verinum::V0;
191 verinum::verinum(int64_t that)
192 : has_len_(false), has_sign_(true), string_flag_(false)
194 int64_t tmp;
196 tmp = that/2;
197 nbits_ = 1;
198 while ((tmp != 0) && (tmp != -1)) {
199 nbits_ += 1;
200 tmp /= 2;
203 nbits_ += 1;
205 bits_ = new V[nbits_];
206 for (unsigned idx = 0 ; idx < nbits_ ; idx += 1) {
207 bits_[idx] = (that & 1)? V1 : V0;
208 that /= 2;
212 verinum::~verinum()
214 delete[]bits_;
217 verinum& verinum::operator= (const verinum&that)
219 if (this == &that) return *this;
220 delete[]bits_;
221 nbits_ = that.nbits_;
222 bits_ = new V[that.nbits_];
223 for (unsigned idx = 0 ; idx < nbits_ ; idx += 1)
224 bits_[idx] = that.bits_[idx];
226 has_len_ = that.has_len_;
227 has_sign_ = that.has_sign_;
228 string_flag_ = that.string_flag_;
229 return *this;
232 verinum::V verinum::get(unsigned idx) const
234 assert(idx < nbits_);
235 return bits_[idx];
238 verinum::V verinum::set(unsigned idx, verinum::V val)
240 assert(idx < nbits_);
241 return bits_[idx] = val;
244 unsigned long verinum::as_ulong() const
246 if (nbits_ == 0)
247 return 0;
249 if (!is_defined())
250 return 0;
252 unsigned top = nbits_;
253 if (top >= (8 * sizeof(unsigned long)))
254 top = 8 * sizeof(unsigned long);
256 unsigned long val = 0;
257 unsigned long mask = 1;
258 for (unsigned idx = 0 ; idx < top ; idx += 1, mask <<= 1)
259 if (bits_[idx] == V1)
260 val |= mask;
262 return val;
265 uint64_t verinum::as_ulong64() const
267 if (nbits_ == 0)
268 return 0;
270 if (!is_defined())
271 return 0;
273 unsigned top = nbits_;
274 if (top >= (8 * sizeof(uint64_t)))
275 top = 8 * sizeof(uint64_t);
277 uint64_t val = 0;
278 uint64_t mask = 1;
279 for (unsigned idx = 0 ; idx < top ; idx += 1, mask <<= 1)
280 if (bits_[idx] == V1)
281 val |= mask;
283 return val;
287 * This function returns the native long integer that represents the
288 * value of this object. It accounts for sign extension if the value
289 * is signed.
291 * If the value is undefined, return 0.
293 * This function presumes that the native format is 2s compliment
294 * (pretty safe these days) and masks/sets bits accordingly. If the
295 * value is too large for the native form, it truncates the high bits.
297 signed long verinum::as_long() const
299 if (nbits_ == 0)
300 return 0;
302 if (!is_defined())
303 return 0;
305 signed long val = 0;
307 if (has_sign_ && (bits_[nbits_-1] == V1)) {
308 unsigned top = nbits_;
309 if (top > (8 * sizeof(long) - 1))
310 top = 8 * sizeof(long) - 1;
312 val = -1;
313 signed long mask = ~1L;
314 for (unsigned idx = 0 ; idx < top ; idx += 1) {
315 if (bits_[idx] == V0)
316 val &= mask;
318 mask = (mask << 1) | 1L;
321 } else {
322 unsigned top = nbits_;
323 if (top > (8 * sizeof(long) - 1))
324 top = 8 * sizeof(long) - 1;
326 signed long mask = 1;
327 for (unsigned idx = 0 ; idx < top ; idx += 1, mask <<= 1)
328 if (bits_[idx] == V1)
329 val |= mask;
332 return val;
335 string verinum::as_string() const
337 assert( nbits_%8 == 0 );
338 if (nbits_ == 0)
339 return "";
341 string res;
342 bool leading_nuls = true;
343 for (unsigned idx = nbits_ ; idx > 0 ; idx -= 8) {
344 char char_val = 0;
345 V*bp = bits_+idx;
347 if (*(--bp) == V1) char_val |= 0x80;
348 if (*(--bp) == V1) char_val |= 0x40;
349 if (*(--bp) == V1) char_val |= 0x20;
350 if (*(--bp) == V1) char_val |= 0x10;
351 if (*(--bp) == V1) char_val |= 0x08;
352 if (*(--bp) == V1) char_val |= 0x04;
353 if (*(--bp) == V1) char_val |= 0x02;
354 if (*(--bp) == V1) char_val |= 0x01;
355 if (char_val == 0 && leading_nuls)
356 continue;
358 if (char_val == '"' || char_val == '\\') {
359 char tmp[5];
360 snprintf(tmp, sizeof tmp, "\\\%03o", char_val);
361 res = res + tmp;
362 } else if (char_val == ' ' || isgraph(char_val)) {
363 res = res + char_val;
365 } else {
366 char tmp[5];
367 snprintf(tmp, sizeof tmp, "\\\%03o", char_val);
368 res = res + tmp;
371 return res;
374 bool verinum::is_before(const verinum&that) const
376 if (that.nbits_ > nbits_) return true;
377 if (that.nbits_ < nbits_) return false;
379 for (unsigned idx = nbits_ ; idx > 0 ; idx -= 1) {
380 if (bits_[idx-1] < that.bits_[idx-1]) return true;
381 if (bits_[idx-1] > that.bits_[idx-1]) return false;
383 return false;
386 bool verinum::is_defined() const
388 for (unsigned idx = 0 ; idx < nbits_ ; idx += 1) {
389 if (bits_[idx] == Vx) return false;
390 if (bits_[idx] == Vz) return false;
392 return true;
395 bool verinum::is_zero() const
397 for (unsigned idx = 0 ; idx < nbits_ ; idx += 1)
398 if (bits_[idx] != V0) return false;
400 return true;
403 verinum pad_to_width(const verinum&that, unsigned width)
405 if (that.len() >= width)
406 return that;
408 if (that.len() == 0) {
409 verinum val (verinum::V0, width, that.has_len());
410 val.has_sign(that.has_sign());
411 return val;
414 verinum::V pad = that[that.len()-1];
415 if (pad==verinum::V1 && !that.has_sign())
416 pad = verinum::V0;
417 if (that.has_len()) {
418 if (pad==verinum::Vx)
419 pad = verinum::V0;
420 if (pad==verinum::Vz)
421 pad = verinum::V0;
424 verinum val(pad, width, that.has_len());
426 for (unsigned idx = 0 ; idx < that.len() ; idx += 1)
427 val.set(idx, that[idx]);
429 val.has_sign(that.has_sign());
430 return val;
434 * This function returns a version of the verinum that has only as
435 * many bits as are needed to accurately represent the value. It takes
436 * into account the signedness of the value.
438 * If the input value has a definite length, then that value is just
439 * returned as is.
441 verinum trim_vnum(const verinum&that)
443 unsigned tlen;
445 if (that.has_len())
446 return that;
448 if (that.len() < 2)
449 return that;
451 if (that.has_sign()) {
452 unsigned top = that.len()-1;
453 verinum::V sign = that.get(top);
455 while ((top > 0) && (that.get(top) == sign))
456 top -= 1;
458 /* top points to the first digit that is not the
459 sign. Set the length to include this and one proper
460 sign bit. */
462 if (that.get(top) != sign)
463 top += 1;
465 tlen = top+1;
467 } else {
469 /* If the result is unsigned and has an indefinite
470 length, then trim off leading zeros. */
471 tlen = 1;
472 for (unsigned idx = tlen ; idx < that.len() ; idx += 1)
473 if (that.get(idx) != verinum::V0)
474 tlen = idx+1;
478 verinum tmp (verinum::V0, tlen, false);
479 tmp.has_sign(that.has_sign());
480 for (unsigned idx = 0 ; idx < tmp.len() ; idx += 1)
481 tmp.set(idx, that.get(idx));
483 return tmp;
486 ostream& operator<< (ostream&o, verinum::V v)
488 switch (v) {
489 case verinum::V0:
490 o << "0";
491 break;
492 case verinum::V1:
493 o << "1";
494 break;
495 case verinum::Vx:
496 o << "x";
497 break;
498 case verinum::Vz:
499 o << "z";
500 break;
502 return o;
506 * This operator is used by various dumpers to write the verilog
507 * number in a Verilog format.
509 ostream& operator<< (ostream&o, const verinum&v)
511 if (v.is_string()) {
512 o << "\"" << v.as_string() << "\"";
513 return o;
516 /* If the verinum number has a fixed length, dump all the bits
517 literally. This is how we express the fixed length in the
518 output. */
519 if (v.has_len()) {
520 o << v.len();
521 if (v.has_sign())
522 o << "'sb";
523 else
524 o << "'b";
526 if (v.len() == 0) {
527 o << "0";
528 return o;
531 for (unsigned idx = v.len() ; idx > 0 ; idx -= 1)
532 o << v[idx-1];
534 return o;
537 /* If the number is fully defined (no x or z) then print it
538 out as a decimal number. */
539 if (v.is_defined() && v.len() < sizeof(long)) {
540 if (v.has_sign())
541 o << "'sd" << v.as_long();
542 else
543 o << "'d" << v.as_ulong();
544 return o;
547 /* Oh, well. Print the minimum to get the value properly
548 displayed. */
549 if (v.has_sign())
550 o << "'sb";
551 else
552 o << "'b";
554 if (v.len() == 0) {
555 o << "0";
556 return o;
559 verinum::V trim_left = v.get(v.len()-1);
560 unsigned idx;
562 for (idx = v.len()-1; idx > 0; idx -= 1)
563 if (trim_left != v.get(idx-1))
564 break;
566 o << trim_left;
568 while (idx > 0) {
569 o << v.get(idx-1);
570 idx -= 1;
573 return o;
576 verinum::V operator == (const verinum&left, const verinum&right)
578 if (left.len() != right.len())
579 return verinum::V0;
581 for (unsigned idx = 0 ; idx < left.len() ; idx += 1)
582 if (left[idx] != right[idx])
583 return verinum::V0;
585 return verinum::V1;
588 verinum::V operator <= (const verinum&left, const verinum&right)
590 unsigned idx;
591 for (idx = left.len() ; idx > right.len() ; idx -= 1) {
592 if (left[idx-1] != verinum::V0) return verinum::V0;
595 for (idx = right.len() ; idx > left.len() ; idx -= 1) {
596 if (right[idx-1] != verinum::V0) return verinum::V1;
599 idx = right.len();
600 if (left.len() < idx) idx = left.len();
602 while (idx > 0) {
603 if (left[idx-1] == verinum::Vx) return verinum::Vx;
604 if (left[idx-1] == verinum::Vz) return verinum::Vx;
605 if (right[idx-1] == verinum::Vx) return verinum::Vx;
606 if (right[idx-1] == verinum::Vz) return verinum::Vx;
607 if (left[idx-1] > right[idx-1]) return verinum::V0;
608 if (left[idx-1] < right[idx-1]) return verinum::V1;
609 idx -= 1;
612 return verinum::V1;
615 verinum::V operator < (const verinum&left, const verinum&right)
617 unsigned idx;
618 for (idx = left.len() ; idx > right.len() ; idx -= 1) {
619 if (left[idx-1] != verinum::V0) return verinum::V0;
622 for (idx = right.len() ; idx > left.len() ; idx -= 1) {
623 if (right[idx-1] != verinum::V0) return verinum::V1;
626 while (idx > 0) {
627 if (left[idx-1] == verinum::Vx) return verinum::Vx;
628 if (left[idx-1] == verinum::Vz) return verinum::Vx;
629 if (right[idx-1] == verinum::Vx) return verinum::Vx;
630 if (right[idx-1] == verinum::Vz) return verinum::Vx;
631 if (left[idx-1] > right[idx-1]) return verinum::V0;
632 idx -= 1;
635 return verinum::V0;
638 static verinum::V add_with_carry(verinum::V l, verinum::V r, verinum::V&c)
640 unsigned sum = 0;
641 switch (c) {
642 case verinum::Vx:
643 case verinum::Vz:
644 c = verinum::Vx;
645 return verinum::Vx;
646 case verinum::V0:
647 break;
648 case verinum::V1:
649 sum += 1;
652 switch (l) {
653 case verinum::Vx:
654 case verinum::Vz:
655 c = verinum::Vx;
656 return verinum::Vx;
657 case verinum::V0:
658 break;
659 case verinum::V1:
660 sum += 1;
661 break;
664 switch (r) {
665 case verinum::Vx:
666 case verinum::Vz:
667 c = verinum::Vx;
668 return verinum::Vx;
669 case verinum::V0:
670 break;
671 case verinum::V1:
672 sum += 1;
673 break;
676 if (sum & 2)
677 c = verinum::V1;
678 else
679 c = verinum::V0;
680 if (sum & 1)
681 return verinum::V1;
682 else
683 return verinum::V0;
686 verinum v_not(const verinum&left)
688 verinum val = left;
689 for (unsigned idx = 0 ; idx < val.len() ; idx += 1)
690 switch (val[idx]) {
691 case verinum::V0:
692 val.set(idx, verinum::V1);
693 break;
694 case verinum::V1:
695 val.set(idx, verinum::V0);
696 break;
697 default:
698 val.set(idx, verinum::Vx);
699 break;
702 return val;
706 * Addition works a bit at a time, from the least significant up to
707 * the most significant. The result is signed only if both of the
708 * operands are signed. The result is also expanded as needed to
709 * prevent overflow. It is up to the caller to shrink the result back
710 * down if that is the desire.
712 verinum operator + (const verinum&left, const verinum&right)
714 unsigned min = left.len();
715 if (right.len() < min) min = right.len();
717 unsigned max = left.len();
718 if (right.len() > max) max = right.len();
720 bool signed_flag = left.has_sign() && right.has_sign();
721 verinum::V*val_bits = new verinum::V[max+1];
723 verinum::V carry = verinum::V0;
724 for (unsigned idx = 0 ; idx < min ; idx += 1)
725 val_bits[idx] = add_with_carry(left[idx], right[idx], carry);
727 verinum::V rpad = signed_flag? right[right.len()-1] : verinum::V0;
728 verinum::V lpad = signed_flag? left[left.len()-1] : verinum::V0;
730 if (left.len() > right.len()) {
732 for (unsigned idx = min ; idx < left.len() ; idx += 1)
733 val_bits[idx] = add_with_carry(left[idx], rpad, carry);
735 } else {
737 for (unsigned idx = min ; idx < right.len() ; idx += 1)
738 val_bits[idx] = add_with_carry(lpad, right[idx], carry);
741 val_bits[max] = add_with_carry(lpad, rpad, carry);
742 #if 0
743 if (signed_flag) {
744 if (val_bits[max] != val_bits[max-1])
745 max += 1;
747 #endif
748 verinum val (val_bits, max+1, false);
749 val.has_sign(signed_flag);
751 delete[]val_bits;
753 return val;
756 verinum operator - (const verinum&left, const verinum&right)
758 unsigned min = left.len();
759 if (right.len() < min) min = right.len();
761 unsigned max = left.len();
762 if (right.len() > max) max = right.len();
764 bool signed_flag = left.has_sign() && right.has_sign();
765 verinum::V*val_bits = new verinum::V[max+1];
767 verinum::V carry = verinum::V1;
768 for (unsigned idx = 0 ; idx < min ; idx += 1)
769 val_bits[idx] = add_with_carry(left[idx], ~right[idx], carry);
771 verinum::V rpad = signed_flag? ~right[right.len()-1] : verinum::V1;
772 verinum::V lpad = signed_flag? left[left.len()-1] : verinum::V0;
774 if (left.len() > right.len()) {
776 for (unsigned idx = min ; idx < left.len() ; idx += 1)
777 val_bits[idx] = add_with_carry(left[idx], rpad, carry);
779 } else {
781 for (unsigned idx = min ; idx < right.len() ; idx += 1)
782 val_bits[idx] = add_with_carry(lpad, ~right[idx], carry);
785 if (signed_flag) {
786 val_bits[max] = add_with_carry(lpad, rpad, carry);
787 if (val_bits[max] != val_bits[max-1])
788 max += 1;
791 verinum val (val_bits, max, false);
792 val.has_sign(signed_flag);
794 delete[]val_bits;
796 return val;
800 * This multiplies two verinum numbers together into a verinum
801 * result. The resulting number is as large as the sum of the sizes of
802 * the operand.
804 * The algorithm used is successive shift and add operations,
805 * implemented as the nested loops.
807 * If either value is not completely defined, then the result is not
808 * defined either.
810 verinum operator * (const verinum&left, const verinum&right)
812 const bool has_len_flag = left.has_len() && right.has_len();
814 /* If either operand is not fully defined, then the entire
815 result is undefined. Create a result that is the right size
816 and is filled with 'bx bits. */
817 if (! (left.is_defined() && right.is_defined())) {
818 verinum result (verinum::Vx, left.len()+right.len(), has_len_flag);
819 result.has_sign(left.has_sign() || right.has_sign());
820 return result;
823 verinum result(verinum::V0, left.len() + right.len(), has_len_flag);
824 result.has_sign(left.has_sign() || right.has_sign());
826 for (unsigned rdx = 0 ; rdx < right.len() ; rdx += 1) {
828 if (right.get(rdx) == verinum::V0)
829 continue;
831 verinum::V carry = verinum::V0;
832 for (unsigned ldx = 0 ; ldx < left.len() ; ldx += 1) {
833 result.set(ldx+rdx, add_with_carry(left[ldx],
834 result[rdx+ldx],
835 carry));
839 return trim_vnum(result);
842 verinum pow(const verinum&left, const verinum&right)
844 verinum result = left;
845 unsigned pow_count = right.as_ulong();
847 for (unsigned idx = 1 ; idx < pow_count ; idx += 1)
848 result = result * left;
850 return result;
853 verinum operator << (const verinum&that, unsigned shift)
855 verinum result(verinum::V0, that.len() + shift, that.has_len());
857 for (unsigned idx = 0 ; idx < that.len() ; idx += 1)
858 result.set(idx+shift, that.get(idx));
860 return result;
863 verinum operator >> (const verinum&that, unsigned shift)
865 if (shift >= that.len()) {
866 if (that.has_sign()) {
867 verinum result (that.get(that.len()-1), 1);
868 result.has_sign(true);
869 return result;
870 } else {
871 verinum result(verinum::V0, 1);
872 return result;
876 verinum result(that.has_sign()? that.get(that.len()-1) : verinum::V0,
877 that.len() - shift, that.has_len());
879 for (unsigned idx = shift ; idx < that.len() ; idx += 1)
880 result.set(idx-shift, that.get(idx));
882 return result;
885 static verinum unsigned_divide(verinum num, verinum den)
887 unsigned nwid = num.len();
888 while (nwid > 0 && (num.get(nwid-1) == verinum::V0))
889 nwid -= 1;
891 unsigned dwid = den.len();
892 while (dwid > 0 && (den.get(dwid-1) == verinum::V0))
893 dwid -= 1;
895 if (dwid > nwid)
896 return verinum(verinum::V0, 1);
898 den = den << (nwid-dwid);
900 unsigned idx = nwid - dwid + 1;
901 verinum result (verinum::V0, idx);
902 while (idx > 0) {
903 if (den <= num) {
904 verinum dif = num - den;
905 num = dif;
906 result.set(idx-1, verinum::V1);
908 den = den >> 1;
909 idx -= 1;
912 return result;
915 static verinum unsigned_modulus(verinum num, verinum den)
917 unsigned nwid = num.len();
918 while (nwid > 0 && (num.get(nwid-1) == verinum::V0))
919 nwid -= 1;
921 unsigned dwid = den.len();
922 while (dwid > 0 && (den.get(dwid-1) == verinum::V0))
923 dwid -= 1;
925 if (dwid > nwid)
926 return num;
928 den = den << (nwid-dwid);
930 unsigned idx = nwid - dwid + 1;
931 verinum result (verinum::V0, idx);
932 while (idx > 0) {
933 if (den <= num) {
934 verinum dif = num - den;
935 num = dif;
936 result.set(idx-1, verinum::V1);
938 den = den >> 1;
939 idx -= 1;
942 return num;
946 * This operator divides the left number by the right number. If
947 * either value is signed, the result is signed. If both values have a
948 * defined length, then the result has a defined length.
950 verinum operator / (const verinum&left, const verinum&right)
952 const bool has_len_flag = left.has_len() && right.has_len();
954 unsigned use_len = left.len();
956 /* If either operand is not fully defined, then the entire
957 result is undefined. Create a result that is the right size
958 and is filled with 'bx bits. */
959 if (! (left.is_defined() && right.is_defined())) {
960 verinum result (verinum::Vx, use_len, has_len_flag);
961 result.has_sign(left.has_sign() || right.has_sign());
962 return result;
965 /* If the right expression is a zero value, then the result is
966 filled with 'bx bits. */
967 if (right.is_zero()) {
968 verinum result (verinum::Vx, use_len, has_len_flag);
969 result.has_sign(left.has_sign() || right.has_sign());
970 return result;
973 verinum result(verinum::Vz, use_len, has_len_flag);
974 result.has_sign(left.has_sign() || right.has_sign());
976 /* do the operation differently, depending on whether the
977 result is signed or not. */
978 if (result.has_sign()) {
980 if (use_len <= (8*sizeof(long) - 1)) {
981 long l = left.as_long();
982 long r = right.as_long();
983 long v = l / r;
984 for (unsigned idx = 0 ; idx < use_len ; idx += 1) {
985 result.set(idx, (v & 1)? verinum::V1 : verinum::V0);
986 v >>= 1;
989 } else {
990 verinum use_left, use_right;
991 verinum zero(verinum::V0, 1, false);
992 bool negative = false;
993 if (left < zero) {
994 use_left = zero - left;
995 negative ^= negative;
996 } else {
997 use_left = left;
999 if (right < zero) {
1000 use_right = zero - right;
1001 negative ^= negative;
1002 } else {
1003 use_right = right;
1005 result = unsigned_divide(use_left, use_right);
1006 if (negative) result = zero - result;
1009 } else {
1011 if (use_len <= 8 * sizeof(unsigned long)) {
1012 /* Use native unsigned division to do the work. */
1014 unsigned long l = left.as_ulong();
1015 unsigned long r = right.as_ulong();
1016 unsigned long v = l / r;
1017 for (unsigned idx = 0 ; idx < use_len ; idx += 1) {
1018 result.set(idx, (v & 1)? verinum::V1 : verinum::V0);
1019 v >>= 1;
1022 } else {
1023 result = unsigned_divide(left, right);
1027 return trim_vnum(result);
1030 verinum operator % (const verinum&left, const verinum&right)
1032 const bool has_len_flag = left.has_len() && right.has_len();
1034 unsigned use_len = left.len();
1036 /* If either operand is not fully defined, then the entire
1037 result is undefined. Create a result that is the right size
1038 and is filled with 'bx bits. */
1039 if (! (left.is_defined() && right.is_defined())) {
1040 verinum result (verinum::Vx, use_len, has_len_flag);
1041 result.has_sign(left.has_sign() || right.has_sign());
1042 return result;
1045 /* If the right expression is a zero value, then the result is
1046 filled with 'bx bits. */
1047 if (right.as_ulong() == 0) {
1048 verinum result (verinum::Vx, use_len, has_len_flag);
1049 result.has_sign(left.has_sign() || right.has_sign());
1050 return result;
1053 verinum result(verinum::Vz, use_len, has_len_flag);
1054 result.has_sign(left.has_sign() || right.has_sign());
1056 if (result.has_sign()) {
1058 /* XXXX FIXME XXXX Use native unsigned division to do
1059 the work. This does not work if the result is too
1060 large for the native integer. */
1061 assert(use_len <= 8*sizeof(long));
1062 long l = left.as_long();
1063 long r = right.as_long();
1064 long v = l % r;
1065 for (unsigned idx = 0 ; idx < use_len ; idx += 1) {
1066 result.set(idx, (v & 1)? verinum::V1 : verinum::V0);
1067 v >>= 1;
1070 } else {
1072 /* Use native unsigned division, if possible, to do
1073 the work. This does not work if the result is too
1074 large for the native integer, so resort to a modulus
1075 function in that case. */
1076 if (use_len <= 8*sizeof(unsigned long)) {
1077 assert(use_len <= 8*sizeof(unsigned long));
1078 unsigned long l = left.as_ulong();
1079 unsigned long r = right.as_ulong();
1080 unsigned long v = l % r;
1081 for (unsigned idx = 0 ; idx < use_len ; idx += 1) {
1082 result.set(idx, (v & 1)? verinum::V1 : verinum::V0);
1083 v >>= 1;
1085 } else {
1086 result = unsigned_modulus(left, right);
1090 return trim_vnum(result);
1093 verinum concat(const verinum&left, const verinum&right)
1095 if (left.is_string() && right.is_string()) {
1096 std::string tmp = left.as_string() + right.as_string();
1097 verinum res (tmp);
1098 return res;
1101 verinum res (verinum::V0, left.len() + right.len());
1102 for (unsigned idx = 0 ; idx < right.len() ; idx += 1)
1103 res.set(idx, right.get(idx));
1105 for (unsigned idx = 0 ; idx < left.len() ; idx += 1)
1106 res.set(idx+right.len(), left.get(idx));
1108 return res;
1111 verinum::V operator ~ (verinum::V l)
1113 switch (l) {
1114 case verinum::V0:
1115 return verinum::V1;
1116 case verinum::V1:
1117 return verinum::V0;
1118 default:
1119 return verinum::Vx;
1123 verinum::V operator | (verinum::V l, verinum::V r)
1125 if (l == verinum::V1)
1126 return verinum::V1;
1127 if (r == verinum::V1)
1128 return verinum::V1;
1129 if (l != verinum::V0)
1130 return verinum::Vx;
1131 if (r != verinum::V0)
1132 return verinum::Vx;
1133 return verinum::V0;
1136 verinum::V operator & (verinum::V l, verinum::V r)
1138 if (l == verinum::V0)
1139 return verinum::V0;
1140 if (r == verinum::V0)
1141 return verinum::V0;
1142 if (l != verinum::V1)
1143 return verinum::Vx;
1144 if (r != verinum::V1)
1145 return verinum::Vx;
1146 return verinum::V1;
1149 verinum::V operator ^ (verinum::V l, verinum::V r)
1151 if (l == verinum::V0)
1152 return r;
1153 if (r == verinum::V0)
1154 return l;
1155 if ((l == verinum::V1) && (r == verinum::V1))
1156 return verinum::V0;
1158 return verinum::Vx;
1162 * $Log: verinum.cc,v $
1163 * Revision 1.52 2007/02/25 23:08:24 steve
1164 * Process Verilog escape sequences much earlier.
1166 * Revision 1.51 2007/01/27 05:36:11 steve
1167 * Fix padding of x when literal is sized and unsigned.
1169 * Revision 1.50 2007/01/19 05:42:04 steve
1170 * Fix calculation of verinum pow operation.
1172 * Revision 1.49 2006/12/08 19:56:09 steve
1173 * Handle very wide signed divide.
1175 * Revision 1.48 2006/08/08 05:11:37 steve
1176 * Handle 64bit delay constants.
1178 * Revision 1.47 2006/07/31 03:50:17 steve
1179 * Add support for power in constant expressions.
1181 * Revision 1.46 2006/06/02 04:48:50 steve
1182 * Make elaborate_expr methods aware of the width that the context
1183 * requires of it. In the process, fix sizing of the width of unary
1184 * minus is context determined sizes.
1186 * Revision 1.45 2006/06/01 03:54:51 steve
1187 * Fix broken subtraction of small constants.
1189 * Revision 1.44 2005/12/07 04:04:24 steve
1190 * Allow constant concat expressions.
1192 * Revision 1.43 2004/05/18 18:43:15 steve
1193 * Handle null string as a single nul character.
1195 * Revision 1.42 2004/02/17 06:52:55 steve
1196 * Support unsigned divide of huge numbers.
1198 * Revision 1.41 2003/10/26 04:54:56 steve
1199 * Support constant evaluation of binary ^ operator.
1201 * Revision 1.40 2003/05/25 03:01:19 steve
1202 * Get length of trimed unsigned value right.
1204 * Revision 1.39 2003/04/14 03:40:21 steve
1205 * Make some effort to preserve bits while
1206 * operating on constant values.
1208 * Revision 1.38 2003/04/03 04:30:00 steve
1209 * Prevent overrun comparing verinums to zero.
1211 * Revision 1.37 2003/02/02 00:43:16 steve
1212 * Fix conversion of signed numbes to long
1214 * Revision 1.36 2003/01/30 16:23:08 steve
1215 * Spelling fixes.
1217 * Revision 1.35 2002/08/19 02:39:17 steve
1218 * Support parameters with defined ranges.
1220 * Revision 1.34 2002/08/12 01:35:01 steve
1221 * conditional ident string using autoconfig.
1223 * Revision 1.33 2002/04/27 23:26:24 steve
1224 * Trim leading nulls from string forms.
1226 * Revision 1.32 2002/04/27 04:48:43 steve
1227 * Display string verinums as strings.
1229 * Revision 1.31 2002/02/01 05:09:14 steve
1230 * Propagate sign in unary minus.
1232 * Revision 1.30 2001/12/31 00:02:33 steve
1233 * Include s indicator in dump of signed numbers.
1235 * Revision 1.29 2001/11/19 02:54:12 steve
1236 * Handle division and modulus by zero while
1237 * evaluating run-time constants.
1239 * Revision 1.28 2001/11/06 06:11:55 steve
1240 * Support more real arithmetic in delay constants.
1242 * Revision 1.27 2001/07/25 03:10:50 steve
1243 * Create a config.h.in file to hold all the config
1244 * junk, and support gcc 3.0. (Stephan Boettcher)
1246 * Revision 1.26 2001/02/09 05:44:23 steve
1247 * support evaluation of constant < in expressions.
1249 * Revision 1.25 2001/02/08 05:38:18 steve
1250 * trim the length of unsized numbers.
1252 * Revision 1.24 2001/02/07 21:47:13 steve
1253 * Fix expression widths for rvalues and parameters (PR#131,132)