2 * Copyright (c) 1998-2008 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
25 # include <math.h> // Needed to get pow for as_double().
27 static verinum::V
add_with_carry(verinum::V l
, verinum::V r
, verinum::V
&c
);
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 complement
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 double verinum::as_double() const
337 if (nbits_
== 0) return 0.0;
339 /* Do we have a signed value? */
340 bool signed_flag
= false;
341 if (bits_
[nbits_
-1] == V1
) {
348 for (unsigned idx
= 0; idx
< nbits_
; idx
+= 1) {
349 V sum
= add_with_carry(~bits_
[idx
], V0
, carry
);
351 val
+= pow(2.0, (double)idx
);
354 // val = (double) as_long();
356 for (unsigned idx
= 0; idx
< nbits_
; idx
+= 1) {
357 if (bits_
[idx
] == V1
)
358 val
+= pow(2.0, (double)idx
);
364 string
verinum::as_string() const
366 assert( nbits_
%8 == 0 );
371 bool leading_nuls
= true;
372 for (unsigned idx
= nbits_
; idx
> 0 ; idx
-= 8) {
376 if (*(--bp
) == V1
) char_val
|= 0x80;
377 if (*(--bp
) == V1
) char_val
|= 0x40;
378 if (*(--bp
) == V1
) char_val
|= 0x20;
379 if (*(--bp
) == V1
) char_val
|= 0x10;
380 if (*(--bp
) == V1
) char_val
|= 0x08;
381 if (*(--bp
) == V1
) char_val
|= 0x04;
382 if (*(--bp
) == V1
) char_val
|= 0x02;
383 if (*(--bp
) == V1
) char_val
|= 0x01;
384 if (char_val
== 0 && leading_nuls
)
387 if (char_val
== '"' || char_val
== '\\') {
389 snprintf(tmp
, sizeof tmp
, "\\\%03o", char_val
);
391 } else if (char_val
== ' ' || isgraph(char_val
)) {
392 res
= res
+ char_val
;
396 snprintf(tmp
, sizeof tmp
, "\\\%03o", char_val
);
403 bool verinum::is_before(const verinum
&that
) const
405 if (that
.nbits_
> nbits_
) return true;
406 if (that
.nbits_
< nbits_
) return false;
408 for (unsigned idx
= nbits_
; idx
> 0 ; idx
-= 1) {
409 if (bits_
[idx
-1] < that
.bits_
[idx
-1]) return true;
410 if (bits_
[idx
-1] > that
.bits_
[idx
-1]) return false;
415 bool verinum::is_defined() const
417 for (unsigned idx
= 0 ; idx
< nbits_
; idx
+= 1) {
418 if (bits_
[idx
] == Vx
) return false;
419 if (bits_
[idx
] == Vz
) return false;
424 bool verinum::is_zero() const
426 for (unsigned idx
= 0 ; idx
< nbits_
; idx
+= 1)
427 if (bits_
[idx
] != V0
) return false;
432 bool verinum::is_negative() const
434 return (bits_
[nbits_
-1] == V1
) && has_sign();
437 verinum
pad_to_width(const verinum
&that
, unsigned width
)
439 if (that
.len() >= width
)
442 if (that
.len() == 0) {
443 verinum
val (verinum::V0
, width
, that
.has_len());
444 val
.has_sign(that
.has_sign());
448 verinum::V pad
= that
[that
.len()-1];
449 if (pad
==verinum::V1
&& !that
.has_sign())
451 if (that
.has_len() && !that
.has_sign()) {
452 if (pad
==verinum::Vx
)
454 if (pad
==verinum::Vz
)
458 verinum
val(pad
, width
, that
.has_len());
460 for (unsigned idx
= 0 ; idx
< that
.len() ; idx
+= 1)
461 val
.set(idx
, that
[idx
]);
463 val
.has_sign(that
.has_sign());
468 * This function returns a version of the verinum that has only as
469 * many bits as are needed to accurately represent the value. It takes
470 * into account the signedness of the value.
472 * If the input value has a definite length, then that value is just
475 verinum
trim_vnum(const verinum
&that
)
485 if (that
.has_sign()) {
486 unsigned top
= that
.len()-1;
487 verinum::V sign
= that
.get(top
);
489 while ((top
> 0) && (that
.get(top
) == sign
))
492 /* top points to the first digit that is not the
493 sign. Set the length to include this and one proper
496 if (that
.get(top
) != sign
)
503 /* If the result is unsigned and has an indefinite
504 length, then trim off all but one leading zero. */
505 unsigned top
= that
.len()-1;
506 while ((top
> 0) && (that
.get(top
) == verinum::V0
))
509 /* Now top is the index of the highest non-zero bit. If
510 that turns out to the highest bit in the vector, then
511 tere is no trimming possible. */
512 if (top
+1 == that
.len())
515 /* Make tlen wide enough to include the highest non-zero
516 bit, plus one extra 0 bit. */
519 /* This can only happen when the verinum is all zeros,
520 so make it a single bit wide. */
521 if (that
.get(top
) == verinum::V0
) tlen
-= 1;
524 verinum
tmp (verinum::V0
, tlen
, false);
525 tmp
.has_sign(that
.has_sign());
526 for (unsigned idx
= 0 ; idx
< tmp
.len() ; idx
+= 1)
527 tmp
.set(idx
, that
.get(idx
));
532 ostream
& operator<< (ostream
&o
, verinum::V v
)
552 * This operator is used by various dumpers to write the Verilog
553 * number in a Verilog format.
555 ostream
& operator<< (ostream
&o
, const verinum
&v
)
558 o
<< "\"" << v
.as_string() << "\"";
562 /* If the verinum number has a fixed length, dump all the bits
563 literally. This is how we express the fixed length in the
569 /* If the number is fully defined (no x or z) then print it
570 out as a decimal number. */
571 if (v
.is_defined() && v
.len() < 8*sizeof(long)) {
573 o
<< "'sd" << v
.as_long();
575 o
<< "'d" << v
.as_ulong();
579 /* Oh, well. Print the minimum to get the value properly
591 verinum::V trim_left
= v
.get(v
.len()-1);
594 for (idx
= v
.len()-1; idx
> 0; idx
-= 1)
595 if (trim_left
!= v
.get(idx
-1))
608 verinum::V
operator == (const verinum
&left
, const verinum
&right
)
610 if (left
.len() != right
.len())
613 for (unsigned idx
= 0 ; idx
< left
.len() ; idx
+= 1)
614 if (left
[idx
] != right
[idx
])
620 verinum::V
operator <= (const verinum
&left
, const verinum
&right
)
622 verinum::V left_pad
= verinum::V0
;
623 verinum::V right_pad
= verinum::V0
;
624 if (left
.has_sign() && right
.has_sign()) {
625 left_pad
= left
.get(left
.len()-1);
626 right_pad
= right
.get(right
.len()-1);
628 if (left_pad
== verinum::V1
&& right_pad
== verinum::V0
)
630 if (left_pad
== verinum::V0
&& right_pad
== verinum::V1
)
635 for (idx
= left
.len() ; idx
> right
.len() ; idx
-= 1) {
636 if (left
[idx
-1] != right_pad
) return verinum::V0
;
639 for (idx
= right
.len() ; idx
> left
.len() ; idx
-= 1) {
640 if (right
[idx
-1] != left_pad
) return verinum::V1
;
644 if (left
.len() < idx
) idx
= left
.len();
647 if (left
[idx
-1] == verinum::Vx
) return verinum::Vx
;
648 if (left
[idx
-1] == verinum::Vz
) return verinum::Vx
;
649 if (right
[idx
-1] == verinum::Vx
) return verinum::Vx
;
650 if (right
[idx
-1] == verinum::Vz
) return verinum::Vx
;
651 if (left
[idx
-1] > right
[idx
-1]) return verinum::V0
;
652 if (left
[idx
-1] < right
[idx
-1]) return verinum::V1
;
659 verinum::V
operator < (const verinum
&left
, const verinum
&right
)
661 verinum::V left_pad
= verinum::V0
;
662 verinum::V right_pad
= verinum::V0
;
663 if (left
.has_sign() && right
.has_sign()) {
664 left_pad
= left
.get(left
.len()-1);
665 right_pad
= right
.get(right
.len()-1);
667 if (left_pad
== verinum::V1
&& right_pad
== verinum::V0
)
669 if (left_pad
== verinum::V0
&& right_pad
== verinum::V1
)
674 for (idx
= left
.len() ; idx
> right
.len() ; idx
-= 1) {
675 if (left
[idx
-1] != right_pad
) return verinum::V0
;
678 for (idx
= right
.len() ; idx
> left
.len() ; idx
-= 1) {
679 if (right
[idx
-1] != left_pad
) return verinum::V1
;
683 if (left
[idx
-1] == verinum::Vx
) return verinum::Vx
;
684 if (left
[idx
-1] == verinum::Vz
) return verinum::Vx
;
685 if (right
[idx
-1] == verinum::Vx
) return verinum::Vx
;
686 if (right
[idx
-1] == verinum::Vz
) return verinum::Vx
;
687 if (left
[idx
-1] > right
[idx
-1]) return verinum::V0
;
688 if (left
[idx
-1] < right
[idx
-1]) return verinum::V1
;
695 static verinum::V
add_with_carry(verinum::V l
, verinum::V r
, verinum::V
&c
)
743 verinum
v_not(const verinum
&left
)
746 for (unsigned idx
= 0 ; idx
< val
.len() ; idx
+= 1)
749 val
.set(idx
, verinum::V1
);
752 val
.set(idx
, verinum::V0
);
755 val
.set(idx
, verinum::Vx
);
763 * Addition works a bit at a time, from the least significant up to
764 * the most significant. The result is signed only if both of the
765 * operands are signed. The result is also expanded as needed to
766 * prevent overflow. It is up to the caller to shrink the result back
767 * down if that is the desire.
769 verinum
operator + (const verinum
&left
, const verinum
&right
)
771 unsigned min
= left
.len();
772 if (right
.len() < min
) min
= right
.len();
774 unsigned max
= left
.len();
775 if (right
.len() > max
) max
= right
.len();
777 bool signed_flag
= left
.has_sign() && right
.has_sign();
778 verinum::V
*val_bits
= new verinum::V
[max
+1];
780 verinum::V carry
= verinum::V0
;
781 for (unsigned idx
= 0 ; idx
< min
; idx
+= 1)
782 val_bits
[idx
] = add_with_carry(left
[idx
], right
[idx
], carry
);
784 verinum::V rpad
= signed_flag
? right
[right
.len()-1] : verinum::V0
;
785 verinum::V lpad
= signed_flag
? left
[left
.len()-1] : verinum::V0
;
787 if (left
.len() > right
.len()) {
789 for (unsigned idx
= min
; idx
< left
.len() ; idx
+= 1)
790 val_bits
[idx
] = add_with_carry(left
[idx
], rpad
, carry
);
794 for (unsigned idx
= min
; idx
< right
.len() ; idx
+= 1)
795 val_bits
[idx
] = add_with_carry(lpad
, right
[idx
], carry
);
798 val_bits
[max
] = add_with_carry(lpad
, rpad
, carry
);
801 if (val_bits
[max
] != val_bits
[max
-1])
805 verinum
val (val_bits
, max
+1, false);
806 val
.has_sign(signed_flag
);
813 verinum
operator - (const verinum
&left
, const verinum
&right
)
815 unsigned min
= left
.len();
816 if (right
.len() < min
) min
= right
.len();
818 unsigned max
= left
.len();
819 if (right
.len() > max
) max
= right
.len();
821 bool signed_flag
= left
.has_sign() && right
.has_sign();
822 verinum::V
*val_bits
= new verinum::V
[max
+1];
824 verinum::V carry
= verinum::V1
;
825 for (unsigned idx
= 0 ; idx
< min
; idx
+= 1)
826 val_bits
[idx
] = add_with_carry(left
[idx
], ~right
[idx
], carry
);
828 verinum::V rpad
= signed_flag
? ~right
[right
.len()-1] : verinum::V1
;
829 verinum::V lpad
= signed_flag
? left
[left
.len()-1] : verinum::V0
;
831 if (left
.len() > right
.len()) {
833 for (unsigned idx
= min
; idx
< left
.len() ; idx
+= 1)
834 val_bits
[idx
] = add_with_carry(left
[idx
], rpad
, carry
);
838 for (unsigned idx
= min
; idx
< right
.len() ; idx
+= 1)
839 val_bits
[idx
] = add_with_carry(lpad
, ~right
[idx
], carry
);
843 val_bits
[max
] = add_with_carry(lpad
, rpad
, carry
);
844 if (val_bits
[max
] != val_bits
[max
-1])
848 verinum
val (val_bits
, max
, false);
849 val
.has_sign(signed_flag
);
857 * This multiplies two verinum numbers together into a verinum
858 * result. The resulting number is as large as the sum of the sizes of
861 * The algorithm used is successive shift and add operations,
862 * implemented as the nested loops.
864 * If either value is not completely defined, then the result is not
867 verinum
operator * (const verinum
&left
, const verinum
&right
)
869 const bool has_len_flag
= left
.has_len() && right
.has_len();
871 /* If either operand is not fully defined, then the entire
872 result is undefined. Create a result that is the right size
873 and is filled with 'bx bits. */
874 if (! (left
.is_defined() && right
.is_defined())) {
875 verinum
result (verinum::Vx
, left
.len()+right
.len(), has_len_flag
);
876 result
.has_sign(left
.has_sign() || right
.has_sign());
880 verinum
result(verinum::V0
, left
.len() + right
.len(), has_len_flag
);
881 result
.has_sign(left
.has_sign() || right
.has_sign());
883 verinum::V r_sign
= sign_bit(right
);
884 for (unsigned rdx
= 0 ; rdx
< result
.len() ; rdx
+= 1) {
886 verinum::V r_bit
= rdx
< right
.len()? right
.get(rdx
) : r_sign
;
887 if (r_bit
== verinum::V0
)
890 verinum::V l_sign
= sign_bit(left
);
891 verinum::V carry
= verinum::V0
;
892 for (unsigned ldx
= 0 ; ldx
< result
.len()-rdx
; ldx
+= 1) {
893 verinum::V l_bit
= ldx
< left
.len()? left
[ldx
] : l_sign
;
894 result
.set(ldx
+rdx
, add_with_carry(l_bit
,
900 return trim_vnum(result
);
903 verinum
pow(const verinum
&left
, const verinum
&right
)
905 verinum result
= left
;
906 unsigned pow_count
= right
.as_ulong();
908 for (unsigned idx
= 1 ; idx
< pow_count
; idx
+= 1)
909 result
= result
* left
;
914 verinum
operator << (const verinum
&that
, unsigned shift
)
916 verinum
result(verinum::V0
, that
.len() + shift
, that
.has_len());
918 for (unsigned idx
= 0 ; idx
< that
.len() ; idx
+= 1)
919 result
.set(idx
+shift
, that
.get(idx
));
924 verinum
operator >> (const verinum
&that
, unsigned shift
)
926 if (shift
>= that
.len()) {
927 if (that
.has_sign()) {
928 verinum
result (that
.get(that
.len()-1), 1);
929 result
.has_sign(true);
932 verinum
result(verinum::V0
, 1);
937 verinum
result(that
.has_sign()? that
.get(that
.len()-1) : verinum::V0
,
938 that
.len() - shift
, that
.has_len());
940 for (unsigned idx
= shift
; idx
< that
.len() ; idx
+= 1)
941 result
.set(idx
-shift
, that
.get(idx
));
946 static verinum
unsigned_divide(verinum num
, verinum den
, bool signed_result
)
948 unsigned nwid
= num
.len();
949 while (nwid
> 0 && (num
.get(nwid
-1) == verinum::V0
))
952 unsigned dwid
= den
.len();
953 while (dwid
> 0 && (den
.get(dwid
-1) == verinum::V0
))
957 return verinum(verinum::V0
, 1);
959 den
= den
<< (nwid
-dwid
);
961 unsigned idx
= nwid
- dwid
+ 1;
962 verinum
result (verinum::V0
, signed_result
? idx
+ 1 : idx
);
964 result
.set(idx
, verinum::V0
);
965 result
.has_sign(true);
969 verinum dif
= num
- den
;
971 result
.set(idx
-1, verinum::V1
);
980 static verinum
unsigned_modulus(verinum num
, verinum den
)
982 unsigned nwid
= num
.len();
983 while (nwid
> 0 && (num
.get(nwid
-1) == verinum::V0
))
986 unsigned dwid
= den
.len();
987 while (dwid
> 0 && (den
.get(dwid
-1) == verinum::V0
))
993 den
= den
<< (nwid
-dwid
);
995 unsigned idx
= nwid
- dwid
+ 1;
998 verinum dif
= num
- den
;
1009 * This operator divides the left number by the right number. If
1010 * either value is signed, the result is signed. If both values have a
1011 * defined length, then the result has a defined length.
1013 verinum
operator / (const verinum
&left
, const verinum
&right
)
1015 const bool has_len_flag
= left
.has_len() && right
.has_len();
1017 unsigned use_len
= left
.len();
1019 /* If either operand is not fully defined, then the entire
1020 result is undefined. Create a result that is the right size
1021 and is filled with 'bx bits. */
1022 if (! (left
.is_defined() && right
.is_defined())) {
1023 verinum
result (verinum::Vx
, use_len
, has_len_flag
);
1024 result
.has_sign(left
.has_sign() || right
.has_sign());
1028 /* If the right expression is a zero value, then the result is
1029 filled with 'bx bits. */
1030 if (right
.is_zero()) {
1031 verinum
result (verinum::Vx
, use_len
, has_len_flag
);
1032 result
.has_sign(left
.has_sign() || right
.has_sign());
1036 verinum
result(verinum::Vz
, use_len
, has_len_flag
);
1037 result
.has_sign(left
.has_sign() || right
.has_sign());
1039 /* do the operation differently, depending on whether the
1040 result is signed or not. */
1041 if (result
.has_sign()) {
1043 if (use_len
<= (8*sizeof(long) - 1)) {
1044 long l
= left
.as_long();
1045 long r
= right
.as_long();
1047 for (unsigned idx
= 0 ; idx
< use_len
; idx
+= 1) {
1048 result
.set(idx
, (v
& 1)? verinum::V1
: verinum::V0
);
1053 verinum use_left
, use_right
;
1054 verinum
zero(verinum::V0
, 1, false);
1055 zero
.has_sign(true);
1056 bool negative
= false;
1058 use_left
= zero
- left
;
1059 negative
= !negative
;
1064 use_right
= zero
- right
;
1065 negative
= !negative
;
1069 result
= unsigned_divide(use_left
, use_right
, true);
1070 if (negative
) result
= zero
- result
;
1075 if (use_len
<= 8 * sizeof(unsigned long)) {
1076 /* Use native unsigned division to do the work. */
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
);
1087 result
= unsigned_divide(left
, right
, false);
1091 return trim_vnum(result
);
1094 verinum
operator % (const verinum
&left
, const verinum
&right
)
1096 const bool has_len_flag
= left
.has_len() && right
.has_len();
1098 unsigned use_len
= left
.len();
1100 /* If either operand is not fully defined, then the entire
1101 result is undefined. Create a result that is the right size
1102 and is filled with 'bx bits. */
1103 if (! (left
.is_defined() && right
.is_defined())) {
1104 verinum
result (verinum::Vx
, use_len
, has_len_flag
);
1105 result
.has_sign(left
.has_sign() || right
.has_sign());
1109 /* If the right expression is a zero value, then the result is
1110 filled with 'bx bits. */
1111 if (right
.as_ulong() == 0) {
1112 verinum
result (verinum::Vx
, use_len
, has_len_flag
);
1113 result
.has_sign(left
.has_sign() || right
.has_sign());
1117 verinum
result(verinum::Vz
, use_len
, has_len_flag
);
1118 result
.has_sign(left
.has_sign() || right
.has_sign());
1120 if (result
.has_sign()) {
1121 if (use_len
<= 8*sizeof(long)) {
1122 /* Use native signed modulus to do the work. */
1123 long l
= left
.as_long();
1124 long r
= right
.as_long();
1126 for (unsigned idx
= 0 ; idx
< use_len
; idx
+= 1) {
1127 result
.set(idx
, (v
& 1)? verinum::V1
: verinum::V0
);
1131 verinum use_left
, use_right
;
1132 verinum
zero(verinum::V0
, 1, false);
1133 zero
.has_sign(true);
1134 bool negative
= false;
1136 use_left
= zero
- left
;
1142 use_right
= zero
- right
;
1146 result
= unsigned_modulus(use_left
, use_right
);
1147 result
.has_sign(true);
1148 if (negative
) result
= zero
- result
;
1151 if (use_len
<= 8*sizeof(unsigned long)) {
1152 /* Use native unsigned modulus to do the work. */
1153 unsigned long l
= left
.as_ulong();
1154 unsigned long r
= right
.as_ulong();
1155 unsigned long v
= l
% r
;
1156 for (unsigned idx
= 0 ; idx
< use_len
; idx
+= 1) {
1157 result
.set(idx
, (v
& 1)? verinum::V1
: verinum::V0
);
1161 result
= unsigned_modulus(left
, right
);
1165 return trim_vnum(result
);
1168 verinum
concat(const verinum
&left
, const verinum
&right
)
1170 if (left
.is_string() && right
.is_string()) {
1171 std::string tmp
= left
.as_string() + right
.as_string();
1176 verinum
res (verinum::V0
, left
.len() + right
.len());
1177 for (unsigned idx
= 0 ; idx
< right
.len() ; idx
+= 1)
1178 res
.set(idx
, right
.get(idx
));
1180 for (unsigned idx
= 0 ; idx
< left
.len() ; idx
+= 1)
1181 res
.set(idx
+right
.len(), left
.get(idx
));
1186 verinum::V
operator ~ (verinum::V l
)
1198 verinum::V
operator | (verinum::V l
, verinum::V r
)
1200 if (l
== verinum::V1
)
1202 if (r
== verinum::V1
)
1204 if (l
!= verinum::V0
)
1206 if (r
!= verinum::V0
)
1211 verinum::V
operator & (verinum::V l
, verinum::V r
)
1213 if (l
== verinum::V0
)
1215 if (r
== verinum::V0
)
1217 if (l
!= verinum::V1
)
1219 if (r
!= verinum::V1
)
1224 verinum::V
operator ^ (verinum::V l
, verinum::V r
)
1226 if (l
== verinum::V0
)
1228 if (r
== verinum::V0
)
1230 if ((l
== verinum::V1
) && (r
== verinum::V1
))