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)
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
20 #ident "$Id: verinum.cc,v 1.52 2007/02/25 23:08:24 steve Exp $"
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)
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
)
49 int str_len
= str
.length();
51 while (idx
< str_len
) {
52 if (str
[idx
] == '\\') {
54 assert(idx
< str_len
);
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';
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.
106 bits_
= new V
[nbits_
];
118 bits_
= new V
[nbits_
];
122 for (idx
= nbits_
, cp
= 0 ; idx
> 0 ; idx
-= 8, cp
+= 1) {
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)
139 bits_
= new V
[nbits_
];
140 for (unsigned idx
= 0 ; idx
< nbits_
; idx
+= 1)
144 verinum::verinum(uint64_t val
, unsigned n
)
145 : has_len_(true), has_sign_(false), string_flag_(false)
148 bits_
= new V
[nbits_
];
149 for (unsigned idx
= 0 ; idx
< nbits_
; idx
+= 1) {
150 bits_
[idx
] = (val
&1) ? V1
: V0
;
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;
170 bits_
= new V
[nbits_
];
172 has_sign_
= that
.has_sign_
;
174 unsigned copy
= nbits
;
175 if (copy
> that
.nbits_
)
177 for (unsigned idx
= 0 ; idx
< copy
; idx
+= 1)
178 bits_
[idx
] = that
.bits_
[idx
];
182 for (unsigned idx
= copy
; idx
< nbits_
; idx
+= 1)
183 bits_
[idx
] = bits_
[idx
-1];
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)
198 while ((tmp
!= 0) && (tmp
!= -1)) {
205 bits_
= new V
[nbits_
];
206 for (unsigned idx
= 0 ; idx
< nbits_
; idx
+= 1) {
207 bits_
[idx
] = (that
& 1)? V1
: V0
;
217 verinum
& verinum::operator= (const verinum
&that
)
219 if (this == &that
) return *this;
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_
;
232 verinum::V
verinum::get(unsigned idx
) const
234 assert(idx
< nbits_
);
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
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
)
265 uint64_t verinum::as_ulong64() const
273 unsigned top
= nbits_
;
274 if (top
>= (8 * sizeof(uint64_t)))
275 top
= 8 * sizeof(uint64_t);
279 for (unsigned idx
= 0 ; idx
< top
; idx
+= 1, mask
<<= 1)
280 if (bits_
[idx
] == V1
)
287 * This function returns the native long integer that represents the
288 * value of this object. It accounts for sign extension if the value
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
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;
313 signed long mask
= ~1L;
314 for (unsigned idx
= 0 ; idx
< top
; idx
+= 1) {
315 if (bits_
[idx
] == V0
)
318 mask
= (mask
<< 1) | 1L;
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
)
335 string
verinum::as_string() const
337 assert( nbits_
%8 == 0 );
342 bool leading_nuls
= true;
343 for (unsigned idx
= nbits_
; idx
> 0 ; idx
-= 8) {
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
)
358 if (char_val
== '"' || char_val
== '\\') {
360 snprintf(tmp
, sizeof tmp
, "\\\%03o", char_val
);
362 } else if (char_val
== ' ' || isgraph(char_val
)) {
363 res
= res
+ char_val
;
367 snprintf(tmp
, sizeof tmp
, "\\\%03o", char_val
);
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;
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;
395 bool verinum::is_zero() const
397 for (unsigned idx
= 0 ; idx
< nbits_
; idx
+= 1)
398 if (bits_
[idx
] != V0
) return false;
403 verinum
pad_to_width(const verinum
&that
, unsigned width
)
405 if (that
.len() >= width
)
408 if (that
.len() == 0) {
409 verinum
val (verinum::V0
, width
, that
.has_len());
410 val
.has_sign(that
.has_sign());
414 verinum::V pad
= that
[that
.len()-1];
415 if (pad
==verinum::V1
&& !that
.has_sign())
417 if (that
.has_len()) {
418 if (pad
==verinum::Vx
)
420 if (pad
==verinum::Vz
)
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());
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
441 verinum
trim_vnum(const verinum
&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
))
458 /* top points to the first digit that is not the
459 sign. Set the length to include this and one proper
462 if (that
.get(top
) != sign
)
469 /* If the result is unsigned and has an indefinite
470 length, then trim off leading zeros. */
472 for (unsigned idx
= tlen
; idx
< that
.len() ; idx
+= 1)
473 if (that
.get(idx
) != verinum::V0
)
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
));
486 ostream
& operator<< (ostream
&o
, verinum::V v
)
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
)
512 o
<< "\"" << v
.as_string() << "\"";
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
531 for (unsigned idx
= v
.len() ; idx
> 0 ; idx
-= 1)
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)) {
541 o
<< "'sd" << v
.as_long();
543 o
<< "'d" << v
.as_ulong();
547 /* Oh, well. Print the minimum to get the value properly
559 verinum::V trim_left
= v
.get(v
.len()-1);
562 for (idx
= v
.len()-1; idx
> 0; idx
-= 1)
563 if (trim_left
!= v
.get(idx
-1))
576 verinum::V
operator == (const verinum
&left
, const verinum
&right
)
578 if (left
.len() != right
.len())
581 for (unsigned idx
= 0 ; idx
< left
.len() ; idx
+= 1)
582 if (left
[idx
] != right
[idx
])
588 verinum::V
operator <= (const verinum
&left
, const verinum
&right
)
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
;
600 if (left
.len() < idx
) idx
= left
.len();
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
;
615 verinum::V
operator < (const verinum
&left
, const verinum
&right
)
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
;
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
;
638 static verinum::V
add_with_carry(verinum::V l
, verinum::V r
, verinum::V
&c
)
686 verinum
v_not(const verinum
&left
)
689 for (unsigned idx
= 0 ; idx
< val
.len() ; idx
+= 1)
692 val
.set(idx
, verinum::V1
);
695 val
.set(idx
, verinum::V0
);
698 val
.set(idx
, verinum::Vx
);
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
);
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
);
744 if (val_bits
[max
] != val_bits
[max
-1])
748 verinum
val (val_bits
, max
+1, false);
749 val
.has_sign(signed_flag
);
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
);
781 for (unsigned idx
= min
; idx
< right
.len() ; idx
+= 1)
782 val_bits
[idx
] = add_with_carry(lpad
, ~right
[idx
], carry
);
786 val_bits
[max
] = add_with_carry(lpad
, rpad
, carry
);
787 if (val_bits
[max
] != val_bits
[max
-1])
791 verinum
val (val_bits
, max
, false);
792 val
.has_sign(signed_flag
);
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
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
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());
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
)
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
],
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
;
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
));
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);
871 verinum
result(verinum::V0
, 1);
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
));
885 static verinum
unsigned_divide(verinum num
, verinum den
)
887 unsigned nwid
= num
.len();
888 while (nwid
> 0 && (num
.get(nwid
-1) == verinum::V0
))
891 unsigned dwid
= den
.len();
892 while (dwid
> 0 && (den
.get(dwid
-1) == verinum::V0
))
896 return verinum(verinum::V0
, 1);
898 den
= den
<< (nwid
-dwid
);
900 unsigned idx
= nwid
- dwid
+ 1;
901 verinum
result (verinum::V0
, idx
);
904 verinum dif
= num
- den
;
906 result
.set(idx
-1, verinum::V1
);
915 static verinum
unsigned_modulus(verinum num
, verinum den
)
917 unsigned nwid
= num
.len();
918 while (nwid
> 0 && (num
.get(nwid
-1) == verinum::V0
))
921 unsigned dwid
= den
.len();
922 while (dwid
> 0 && (den
.get(dwid
-1) == verinum::V0
))
928 den
= den
<< (nwid
-dwid
);
930 unsigned idx
= nwid
- dwid
+ 1;
931 verinum
result (verinum::V0
, idx
);
934 verinum dif
= num
- den
;
936 result
.set(idx
-1, verinum::V1
);
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());
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());
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();
984 for (unsigned idx
= 0 ; idx
< use_len
; idx
+= 1) {
985 result
.set(idx
, (v
& 1)? verinum::V1
: verinum::V0
);
990 verinum use_left
, use_right
;
991 verinum
zero(verinum::V0
, 1, false);
992 bool negative
= false;
994 use_left
= zero
- left
;
995 negative
^= negative
;
1000 use_right
= zero
- right
;
1001 negative
^= negative
;
1005 result
= unsigned_divide(use_left
, use_right
);
1006 if (negative
) result
= zero
- result
;
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
);
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());
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());
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();
1065 for (unsigned idx
= 0 ; idx
< use_len
; idx
+= 1) {
1066 result
.set(idx
, (v
& 1)? verinum::V1
: verinum::V0
);
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
);
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();
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
));
1111 verinum::V
operator ~ (verinum::V l
)
1123 verinum::V
operator | (verinum::V l
, verinum::V r
)
1125 if (l
== verinum::V1
)
1127 if (r
== verinum::V1
)
1129 if (l
!= verinum::V0
)
1131 if (r
!= verinum::V0
)
1136 verinum::V
operator & (verinum::V l
, verinum::V r
)
1138 if (l
== verinum::V0
)
1140 if (r
== verinum::V0
)
1142 if (l
!= verinum::V1
)
1144 if (r
!= verinum::V1
)
1149 verinum::V
operator ^ (verinum::V l
, verinum::V r
)
1151 if (l
== verinum::V0
)
1153 if (r
== verinum::V0
)
1155 if ((l
== verinum::V1
) && (r
== verinum::V1
))
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
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)