1 /*****************************************************************************
3 Licensed to Accellera Systems Initiative Inc. (Accellera) under one or
4 more contributor license agreements. See the NOTICE file distributed
5 with this work for additional information regarding copyright ownership.
6 Accellera licenses this file to you under the Apache License, Version 2.0
7 (the "License"); you may not use this file except in compliance with the
8 License. You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
15 implied. See the License for the specific language governing
16 permissions and limitations under the License.
18 *****************************************************************************/
20 /*****************************************************************************
22 sc_unsigned.cpp -- Arbitrary precision signed arithmetic.
24 This file includes the definitions of sc_unsigned_bitref,
25 sc_unsigned_subref, and sc_unsigned classes. The first two classes
26 are proxy classes to reference one bit and a range of bits of a
27 sc_unsigned number, respectively. This file also includes
28 sc_nbcommon.cpp and sc_nbfriends.cpp, which contain the
29 definitions shared by sc_unsigned.
31 Original Author: Ali Dasdan, Synopsys, Inc.
33 *****************************************************************************/
35 /*****************************************************************************
37 MODIFICATION LOG - modifiers, enter your name, affiliation, date and
38 changes you are making here.
40 Name, Affiliation, Date:
41 Description of Modification:
43 *****************************************************************************/
46 // $Log: sc_unsigned.cpp,v $
47 // Revision 1.7 2011/02/18 20:19:15 acg
48 // Andy Goodrich: updating Copyright notice.
50 // Revision 1.6 2008/12/10 20:38:45 acg
51 // Andy Goodrich: fixed conversion of double values to the digits vector.
52 // The bits above the radix were not being masked off.
54 // Revision 1.5 2008/06/19 17:47:57 acg
55 // Andy Goodrich: fixes for bugs. See 2.2.1 RELEASENOTES.
57 // Revision 1.4 2008/06/19 16:57:57 acg
58 // Andy Goodrich: added case for negative unsigned values to the support in
59 // concate_get_data().
61 // Revision 1.3 2007/11/04 21:27:00 acg
62 // Andy Goodrich: changes to make sure the proper value is returned from
65 // Revision 1.2 2007/02/22 21:35:05 acg
66 // Andy Goodrich: cleaned up comments in concat_get_ctrl and concat_get_data.
68 // Revision 1.1.1.1 2006/12/15 20:20:05 acg
71 // Revision 1.4 2006/08/29 23:36:54 acg
72 // Andy Goodrich: fixed and_reduce and optimized or_reduce.
74 // Revision 1.3 2006/01/13 18:49:32 acg
75 // Added $Log command so that CVS check in comments are reproduced in the
83 #include "systemc/ext/dt/bit/sc_bv_base.hh"
84 #include "systemc/ext/dt/bit/sc_lv_base.hh"
85 #include "systemc/ext/dt/fx/sc_ufix.hh"
86 #include "systemc/ext/dt/fx/scfx_other_defs.hh"
87 #include "systemc/ext/dt/int/sc_int_base.hh"
88 #include "systemc/ext/dt/int/sc_signed.hh"
89 #include "systemc/ext/dt/int/sc_uint_base.hh"
90 #include "systemc/ext/dt/int/sc_unsigned.hh"
91 #include "systemc/ext/dt/misc/sc_concatref.hh"
93 // explicit template instantiations
97 template class sc_vpool
<sc_dt::sc_unsigned_bitref
>;
98 template class sc_vpool
<sc_dt::sc_unsigned_subref
>;
99 template class sc_vpool
<sc_dt::sc_unsigned
>;
101 } // namespace sc_core
106 // Pool of temporary instances:
107 // The sc_unsigned pool is used by the concatenation support.
108 // The bit and part reference pools allow references to be returned.
110 sc_core::sc_vpool
<sc_unsigned
> sc_unsigned::m_pool(8);
111 sc_core::sc_vpool
<sc_unsigned_bitref
> sc_unsigned_bitref::m_pool(9);
112 sc_core::sc_vpool
<sc_unsigned_subref
> sc_unsigned_subref::m_pool(9);
116 sc_unsigned::invalid_init(const char *type_name
, int nb
) const
118 std::stringstream msg
;
119 msg
<< "sc_unsigned("<< type_name
<< ") : nb = " << nb
<< " is not valid";
120 SC_REPORT_ERROR("initialization failed", msg
.str().c_str());
124 // ----------------------------------------------------------------------------
125 // SECTION: Public members - Invalid selections.
126 // ----------------------------------------------------------------------------
129 sc_unsigned::invalid_index(int i
) const
131 std::stringstream msg
;
132 msg
<< "sc_biguint bit selection: index = " << i
<< " violates "
133 "0 <= index <= " << (nbits
-2);
134 SC_REPORT_ERROR("out of bounds", msg
.str().c_str());
135 sc_core::sc_abort(); // can't recover from here
139 sc_unsigned::invalid_range(int l
, int r
) const
141 std::stringstream msg
;
142 msg
<< "sc_biguint part selection: left = " <<
143 l
<< ", right = " << r
<< "\n"
144 " violates either (" << (nbits
- 2) << " >= left >= 0) or "
145 "(" << (nbits
-2) << " >= right >= 0)";
146 SC_REPORT_ERROR("out of bounds", msg
.str().c_str());
147 sc_core::sc_abort(); // can't recover from here
150 // ----------------------------------------------------------------------------
151 // SECTION: Public members - Concatenation support.
152 // ----------------------------------------------------------------------------
154 // Most public members are included from sc_nbcommon.inc. However, some
155 // concatenation support appears here to optimize between the signed and
160 // Insert this object's value at the specified place in a vector of big style
164 sc_unsigned::concat_get_ctrl(sc_digit
*dst_p
, int low_i
) const
166 int dst_i
; // Index to next word to set in dst_p.
167 int end_i
; // Index of high order word to set.
168 int left_shift
; // Amount to shift value left.
169 sc_digit mask
; // Mask for partial word sets.
172 // CALCULATE METRICS FOR DATA MOVEMENT:
173 dst_i
= low_i
/ BITS_PER_DIGIT
;
174 end_i
= (low_i
+ nbits
- 2) / BITS_PER_DIGIT
;
175 left_shift
= low_i
% BITS_PER_DIGIT
;
177 // MOVE FIRST WORD (IT MAY BE PARTIAL) AND THEN ANY OTHERS:
179 // We may "clobber" upper bits, but they will be written at some point
182 mask
= ~(~0U << left_shift
);
183 dst_p
[dst_i
] = (dst_p
[dst_i
] & ~mask
);
186 for (; dst_i
<= end_i
; dst_i
++)
193 sc_unsigned::concat_get_data(sc_digit
*dst_p
, int low_i
) const
195 sc_digit carry
; // Carry for negating value.
196 int dst_i
; // Index to next word to set in dst_p.
197 int end_i
; // Index of high order word to set.
198 int high_i
; // Index w/in word of high order bit.
199 int left_shift
; // Amount to shift value left.
200 sc_digit left_word
; // High word component for set.
201 sc_digit mask
; // Mask for partial word sets.
202 bool result
; // True if inserting non-zero data.
203 int right_shift
; // Amount to shift value right.
204 sc_digit right_word
; // Low word component for set.
205 int real_bits
; // nbits - 1.
206 int src_i
; // Index to next word to get from digit.
208 // CALCULATE METRICS FOR DATA MOVEMENT:
209 real_bits
= nbits
- 1; // Remove that extra sign bit.
210 dst_i
= low_i
/ BITS_PER_DIGIT
;
211 high_i
= low_i
+ real_bits
- 1;
212 end_i
= high_i
/ BITS_PER_DIGIT
;
213 left_shift
= low_i
% BITS_PER_DIGIT
;
216 // POSITIVE SOURCE VALUE:
220 // ALL DATA TO BE MOVED IS IN A SINGLE WORD:
221 if (dst_i
== end_i
) {
222 mask
= ~(~0U << left_shift
);
223 dst_p
[dst_i
] = ((dst_p
[dst_i
] & mask
) |
224 (digit
[0] << left_shift
)) & DIGIT_MASK
;
226 // DATA IS IN MORE THAN ONE WORD, BUT IS WORD ALIGNED:
227 } else if (left_shift
== 0) {
228 for (src_i
= 0; dst_i
< end_i
; dst_i
++, src_i
++) {
229 dst_p
[dst_i
] = digit
[src_i
];
231 high_i
= high_i
% BITS_PER_DIGIT
;
232 mask
= ~(~1U << high_i
) & DIGIT_MASK
;
233 dst_p
[dst_i
] = digit
[src_i
] & mask
;
235 // DATA IS IN MORE THAN ONE WORD, AND NOT WORD ALIGNED:
237 high_i
= high_i
% BITS_PER_DIGIT
;
238 right_shift
= BITS_PER_DIGIT
- left_shift
;
239 mask
= ~(~0U << left_shift
);
240 right_word
= digit
[0];
241 dst_p
[dst_i
] = (dst_p
[dst_i
] & mask
) |
242 ((right_word
<< left_shift
) & DIGIT_MASK
);
243 for (src_i
= 1, dst_i
++; dst_i
< end_i
; dst_i
++, src_i
++) {
244 left_word
= digit
[src_i
];
245 dst_p
[dst_i
] = ((left_word
<< left_shift
) & DIGIT_MASK
) |
246 (right_word
>> right_shift
);
247 right_word
= left_word
;
249 left_word
= (src_i
< ndigits
) ? digit
[src_i
] : 0;
250 mask
= ~(~1U << high_i
) & DIGIT_MASK
;
251 dst_p
[dst_i
] = ((left_word
<< left_shift
) |
252 (right_word
>> right_shift
)) & mask
;
256 // SOURCE VALUE IS NEGATIVE:
258 // ALL DATA TO BE MOVED IS IN A SINGLE WORD:
260 if (dst_i
== end_i
) {
261 mask
= ~(~0U << nbits
);
262 right_word
= ((digit
[0] ^ DIGIT_MASK
) + 1) & mask
;
263 mask
= ~(~0U << left_shift
);
264 dst_p
[dst_i
] = ((dst_p
[dst_i
] & mask
) |
265 (right_word
<< left_shift
)) & DIGIT_MASK
;
267 // DATA IS IN MORE THAN ONE WORD, BUT IS WORD ALIGNED:
269 } else if (left_shift
== 0) {
271 for (src_i
= 0; dst_i
< end_i
; dst_i
++, src_i
++) {
272 right_word
= (digit
[src_i
] ^ DIGIT_MASK
) + carry
;
273 dst_p
[dst_i
] = right_word
& DIGIT_MASK
;
274 carry
= right_word
>> BITS_PER_DIGIT
;
276 high_i
= high_i
% BITS_PER_DIGIT
;
277 mask
= (~(~1U << high_i
)) & DIGIT_MASK
;
278 right_word
= (src_i
< ndigits
) ?
279 (digit
[src_i
] ^ DIGIT_MASK
) + carry
: DIGIT_MASK
+ carry
;
280 dst_p
[dst_i
] = right_word
& mask
;
282 // DATA IS IN MORE THAN ONE WORD, AND NOT WORD ALIGNED:
284 high_i
= high_i
% BITS_PER_DIGIT
;
285 right_shift
= BITS_PER_DIGIT
- left_shift
;
286 mask
= ~(~0U << left_shift
);
288 right_word
= (digit
[0] ^ DIGIT_MASK
) + carry
;
289 dst_p
[dst_i
] = (dst_p
[dst_i
] & mask
) |
290 ((right_word
<< left_shift
) & DIGIT_MASK
);
291 carry
= right_word
>> BITS_PER_DIGIT
;
292 right_word
&= DIGIT_MASK
;
293 for (src_i
= 1, dst_i
++; dst_i
< end_i
; dst_i
++, src_i
++) {
294 left_word
= (digit
[src_i
] ^ DIGIT_MASK
) + carry
;
295 dst_p
[dst_i
] = ((left_word
<< left_shift
)&DIGIT_MASK
) |
296 (right_word
>> right_shift
);
297 carry
= left_word
>> BITS_PER_DIGIT
;
298 right_word
= left_word
& DIGIT_MASK
;
300 left_word
= (src_i
< ndigits
) ?
301 (digit
[src_i
] ^ DIGIT_MASK
) + carry
: carry
;
302 mask
= ~(~1U << high_i
) & DIGIT_MASK
;
303 dst_p
[dst_i
] = ((left_word
<< left_shift
) |
304 (right_word
>> right_shift
)) & mask
;
310 // ALL DATA TO BE MOVED IS IN A SINGLE WORD:
311 if (dst_i
== end_i
) {
312 mask
= ~(~0U << real_bits
) << left_shift
;
313 dst_p
[dst_i
] = dst_p
[dst_i
] & ~mask
;
315 // DATA IS IN MORE THAN ONE WORD, BUT IS WORD ALIGNED:
317 } else if (left_shift
== 0) {
318 for (src_i
= 0; dst_i
< end_i
; dst_i
++, src_i
++) {
323 // DATA IS IN MORE THAN ONE WORD, AND NOT WORD ALIGNED:
325 mask
= ~(~0U << left_shift
);
326 dst_p
[dst_i
] = (dst_p
[dst_i
] & mask
);
327 for (dst_i
++; dst_i
<= end_i
; dst_i
++) {
336 // Return this object instance's bits as a uint64 without sign extension.
338 sc_unsigned::concat_get_uint64() const
348 result
= (result
<< BITS_PER_DIGIT
) | digit
[1];
349 result
= (result
<< BITS_PER_DIGIT
) | digit
[0];
360 sc_unsigned::concat_set(int64 src
, int low_i
)
362 *this = (low_i
< 64) ? src
>> low_i
: src
>> 63;
366 sc_unsigned::concat_set(const sc_signed
&src
, int low_i
)
368 if (low_i
< src
.length())
369 *this = src
>> low_i
;
371 *this = (src
< 0) ? (int_type
)-1 : 0;
375 sc_unsigned::concat_set(const sc_unsigned
&src
, int low_i
)
377 if (low_i
< src
.length())
378 *this = src
>> low_i
;
384 sc_unsigned::concat_set(uint64 src
, int low_i
)
386 *this = (low_i
< 64) ? src
>> low_i
: 0;
390 // ----------------------------------------------------------------------------
391 // SECTION: Public members - Reduction methods.
392 // ----------------------------------------------------------------------------
395 sc_unsigned::and_reduce() const
397 int i
; // Digit examining.
401 for (i
= 0; i
< ndigits
- 1; i
++)
402 if ((digit
[i
] & DIGIT_MASK
) != DIGIT_MASK
)
404 if ((digit
[i
] & ~(~0U << ((nbits
- 1) % BITS_PER_DIGIT
))) ==
405 static_cast<sc_digit
>(~(~0U << ((nbits
- 1) % BITS_PER_DIGIT
)))) {
412 sc_unsigned::or_reduce() const
414 return (sgn
== SC_ZERO
) ? false : true;
418 sc_unsigned::xor_reduce() const
420 int i
; // Digit examining.
421 int odd
; // Flag for odd number of digits.
424 for (i
= 0; i
< nbits
- 1; i
++)
427 return odd
? true : false;
431 // ----------------------------------------------------------------------------
432 // SECTION: Public members - Assignment operators.
433 // ----------------------------------------------------------------------------
435 // assignment operators
437 sc_unsigned::operator = (const char *a
)
440 SC_REPORT_ERROR("conversion failed",
441 "character string is zero");
442 } else if (*a
== 0) {
443 SC_REPORT_ERROR("conversion failed",
444 "character string is empty");
447 sc_ufix
aa(a
, len
, len
, SC_TRN
, SC_WRAP
, 0, SC_ON
);
448 return this->operator = (aa
);
449 } catch(const sc_core::sc_report
&) {
450 std::stringstream msg
;
451 msg
<< "character string '" << a
<< "' is not valid";
452 SC_REPORT_ERROR("conversion failed", msg
.str().c_str());
458 sc_unsigned::operator = (int64 v
)
461 if (sgn
== SC_ZERO
) {
462 vec_zero(ndigits
, digit
);
464 from_uint(ndigits
, digit
, (uint64
) v
);
465 convert_SM_to_2C_to_SM();
471 sc_unsigned::operator = (uint64 v
)
475 vec_zero(ndigits
, digit
);
478 from_uint(ndigits
, digit
, v
);
479 convert_SM_to_2C_to_SM();
485 sc_unsigned::operator = (long v
)
488 if (sgn
== SC_ZERO
) {
489 vec_zero(ndigits
, digit
);
491 from_uint(ndigits
, digit
, (unsigned long)v
);
492 convert_SM_to_2C_to_SM();
498 sc_unsigned::operator = (unsigned long v
)
502 vec_zero(ndigits
, digit
);
505 from_uint(ndigits
, digit
, v
);
506 convert_SM_to_2C_to_SM();
512 sc_unsigned::operator = (double v
)
517 while (std::floor(v
) && (i
< ndigits
)) {
518 digit
[i
++] = ((sc_digit
)std::floor(remainder(v
, DIGIT_RADIX
))) &
522 vec_zero(i
, ndigits
, digit
);
523 convert_SM_to_2C_to_SM();
528 // ----------------------------------------------------------------------------
531 sc_unsigned::operator = (const sc_bv_base
&v
)
533 int minlen
= sc_min(nbits
, v
.length());
535 for (; i
< minlen
; ++i
) {
536 safe_set(i
, v
.get_bit(i
), digit
);
538 for (; i
< nbits
; ++i
) {
539 safe_set(i
, 0, digit
); // zero-extend
546 sc_unsigned::operator = (const sc_lv_base
&v
)
548 int minlen
= sc_min(nbits
, v
.length());
550 for (; i
< minlen
; ++i
) {
551 safe_set(i
, sc_logic(v
.get_bit(i
)).to_bool(), digit
);
553 for (; i
< nbits
; ++i
) {
554 safe_set(i
, 0, digit
); // zero-extend
561 // explicit conversion to character string
563 sc_unsigned::to_string(sc_numrep numrep
) const
566 sc_ufix
aa(*this, len
, len
, SC_TRN
, SC_WRAP
, 0, SC_ON
);
567 return aa
.to_string(numrep
);
571 sc_unsigned::to_string(sc_numrep numrep
, bool w_prefix
) const
574 sc_ufix
aa(*this, len
, len
, SC_TRN
, SC_WRAP
, 0, SC_ON
);
575 return aa
.to_string(numrep
, w_prefix
);
579 // ----------------------------------------------------------------------------
580 // SECTION: Interfacing with sc_int_base
581 // ----------------------------------------------------------------------------
584 sc_unsigned::operator = (const sc_int_base
&v
)
586 return operator = ((int64
)v
);
590 sc_unsigned::operator += (const sc_int_base
&v
)
592 return operator += ((int64
)v
);
596 sc_unsigned::operator -= (const sc_int_base
&v
)
598 return operator -= ((int64
)v
);
602 sc_unsigned::operator *= (const sc_int_base
&v
)
604 return operator *= ((int64
)v
);
608 sc_unsigned::operator /= (const sc_int_base
&v
)
610 return operator /= ((int64
)v
);
614 sc_unsigned::operator %= (const sc_int_base
&v
)
616 return operator %= ((int64
)v
);
620 sc_unsigned::operator &= (const sc_int_base
&v
)
622 return operator &= ((int64
)v
);
626 sc_unsigned::operator |= (const sc_int_base
&v
)
628 return operator |= ((int64
)v
);
632 sc_unsigned::operator ^= (const sc_int_base
&v
)
634 return operator ^= ((int64
)v
);
638 operator << (const sc_unsigned
&u
, const sc_int_base
&v
)
640 return operator << (u
, (int64
)v
);
643 sc_unsigned::operator <<= (const sc_int_base
&v
)
645 return operator <<= ((int64
)v
);
649 operator >> (const sc_unsigned
& u
, const sc_int_base
& v
)
651 return operator >> (u
, (int64
)v
);
654 sc_unsigned::operator >>= (const sc_int_base
& v
)
656 return operator >>= ((int64
)v
);
660 operator == (const sc_unsigned
&u
, const sc_int_base
&v
)
662 return operator == (u
, (int64
)v
);
665 operator == (const sc_int_base
&u
, const sc_unsigned
&v
)
667 return operator == ((int64
)u
, v
);
671 operator != (const sc_unsigned
&u
, const sc_int_base
&v
)
673 return operator != (u
, (int64
)v
);
676 operator != (const sc_int_base
&u
, const sc_unsigned
&v
)
678 return operator != ((int64
)u
, v
);
682 operator < (const sc_unsigned
&u
, const sc_int_base
&v
)
684 return operator < (u
, (int64
)v
);
687 operator < (const sc_int_base
&u
, const sc_unsigned
&v
)
689 return operator < ((int64
)u
, v
);
693 operator <= (const sc_unsigned
&u
, const sc_int_base
&v
)
695 return operator <= (u
, (int64
)v
);
698 operator <= (const sc_int_base
&u
, const sc_unsigned
&v
)
700 return operator <= ((int64
)u
, v
);
704 operator > (const sc_unsigned
&u
, const sc_int_base
&v
)
706 return operator > (u
, (int64
)v
);
709 operator > (const sc_int_base
&u
, const sc_unsigned
&v
)
711 return operator > ((int64
)u
, v
);
715 operator >= (const sc_unsigned
&u
, const sc_int_base
&v
)
717 return operator >= (u
, (int64
)v
);
720 operator >= (const sc_int_base
&u
, const sc_unsigned
&v
)
722 return operator >= ((int64
)u
, v
);
726 // ----------------------------------------------------------------------------
727 // SECTION: Interfacing with sc_uint_base
728 // ----------------------------------------------------------------------------
731 sc_unsigned::operator = (const sc_uint_base
&v
)
733 return operator = ((uint64
)v
);
737 operator + (const sc_unsigned
&u
, const sc_uint_base
&v
)
739 return operator + (u
, (uint64
)v
);
742 operator + (const sc_uint_base
&u
, const sc_unsigned
&v
)
744 return operator + ((uint64
)u
, v
);
747 sc_unsigned::operator += (const sc_uint_base
&v
)
749 return operator += ((uint64
)v
);
753 sc_unsigned::operator -= (const sc_uint_base
&v
)
755 return operator -= ((uint64
)v
);
759 operator * (const sc_unsigned
&u
, const sc_uint_base
&v
)
761 return operator * (u
, (uint64
)v
);
764 operator * (const sc_uint_base
&u
, const sc_unsigned
&v
)
766 return operator * ((uint64
)u
, v
);
769 sc_unsigned::operator *= (const sc_uint_base
&v
)
771 return operator *= ((uint64
)v
);
775 operator / (const sc_unsigned
&u
, const sc_uint_base
&v
)
777 return operator / (u
, (uint64
)v
);
780 operator / (const sc_uint_base
&u
, const sc_unsigned
&v
)
782 return operator / ((uint64
)u
, v
);
785 sc_unsigned::operator /= (const sc_uint_base
&v
)
787 return operator /= ((uint64
)v
);
791 operator % (const sc_unsigned
&u
, const sc_uint_base
&v
)
793 return operator % (u
, (uint64
)v
);
796 operator % (const sc_uint_base
&u
, const sc_unsigned
&v
)
798 return operator % ((uint64
)u
, v
);
801 sc_unsigned::operator %= (const sc_uint_base
&v
)
803 return operator %= ((uint64
)v
);
807 operator & (const sc_unsigned
&u
, const sc_uint_base
&v
)
809 return operator & (u
, (uint64
)v
);
812 operator & (const sc_uint_base
&u
, const sc_unsigned
&v
)
814 return operator & ((uint64
)u
, v
);
817 sc_unsigned::operator &= (const sc_uint_base
&v
)
819 return operator &= ((uint64
)v
);
823 operator | (const sc_unsigned
&u
, const sc_uint_base
&v
)
825 return operator | (u
, (uint64
)v
);
828 operator | (const sc_uint_base
&u
, const sc_unsigned
&v
)
830 return operator | ((uint64
)u
, v
);
833 sc_unsigned::operator |= (const sc_uint_base
&v
)
835 return operator |= ((uint64
)v
);
839 operator ^ (const sc_unsigned
&u
, const sc_uint_base
&v
)
841 return operator ^ (u
, (uint64
)v
);
844 operator ^ (const sc_uint_base
&u
, const sc_unsigned
&v
)
846 return operator ^ ((uint64
)u
, v
);
849 sc_unsigned::operator ^= (const sc_uint_base
&v
)
851 return operator ^= ((uint64
)v
);
855 operator << (const sc_unsigned
&u
, const sc_uint_base
&v
)
857 return operator << (u
, (uint64
)v
);
860 sc_unsigned::operator <<= (const sc_uint_base
&v
)
862 return operator <<= ((uint64
)v
);
866 operator >> (const sc_unsigned
&u
, const sc_uint_base
&v
)
868 return operator >> (u
, (uint64
)v
);
871 sc_unsigned::operator >>= (const sc_uint_base
&v
)
873 return operator >>= ((uint64
)v
);
877 operator == (const sc_unsigned
&u
, const sc_uint_base
&v
)
879 return operator == (u
, (uint64
)v
);
882 operator == (const sc_uint_base
&u
, const sc_unsigned
&v
)
884 return operator == ((uint64
)u
, v
);
888 operator != (const sc_unsigned
&u
, const sc_uint_base
&v
)
890 return operator != (u
, (uint64
)v
);
893 operator != (const sc_uint_base
&u
, const sc_unsigned
&v
)
895 return operator != ((uint64
)u
, v
);
899 operator < (const sc_unsigned
&u
, const sc_uint_base
&v
)
901 return operator < (u
, (uint64
)v
);
904 operator < (const sc_uint_base
&u
, const sc_unsigned
&v
)
906 return operator < ((uint64
)u
, v
);
910 operator <= (const sc_unsigned
&u
, const sc_uint_base
&v
)
912 return operator <= (u
, (uint64
)v
);
915 operator <= (const sc_uint_base
&u
, const sc_unsigned
&v
)
917 return operator <= ((uint64
)u
, v
);
921 operator > (const sc_unsigned
&u
, const sc_uint_base
&v
)
923 return operator > (u
, (uint64
)v
);
926 operator > (const sc_uint_base
&u
, const sc_unsigned
&v
)
928 return operator > ((uint64
)u
, v
);
932 operator >= (const sc_unsigned
&u
, const sc_uint_base
&v
)
934 return operator >= (u
, (uint64
)v
);
937 operator >= (const sc_uint_base
&u
, const sc_unsigned
&v
)
939 return operator >= ((uint64
)u
, v
);
943 // ----------------------------------------------------------------------------
944 // SECTION: Input and output operators
945 // ----------------------------------------------------------------------------
947 // The operators in this section are included from sc_nbcommon.cpp.
950 // ----------------------------------------------------------------------------
951 // SECTION: Operator macros.
952 // ----------------------------------------------------------------------------
954 #define CONVERT_LONG(u) \
955 small_type u ## s = get_sign(u); \
956 sc_digit u ## d[DIGITS_PER_ULONG]; \
957 from_uint(DIGITS_PER_ULONG, u ## d, (unsigned long) u);
959 #define CONVERT_LONG_2(u) \
960 sc_digit u ## d[DIGITS_PER_ULONG]; \
961 from_uint(DIGITS_PER_ULONG, u ## d, (unsigned long) u);
963 #define CONVERT_INT(u) \
964 small_type u ## s = get_sign(u); \
965 sc_digit u ## d[DIGITS_PER_UINT]; \
966 from_uint(DIGITS_PER_UINT, u ## d, (unsigned int) u);
968 #define CONVERT_INT_2(u) \
969 sc_digit u ## d[DIGITS_PER_UINT]; \
970 from_uint(DIGITS_PER_UINT, u ## d, (unsigned int) u);
972 #define CONVERT_INT64(u) \
973 small_type u ## s = get_sign(u); \
974 sc_digit u ## d[DIGITS_PER_UINT64]; \
975 from_uint(DIGITS_PER_UINT64, u ## d, (uint64) u);
977 #define CONVERT_INT64_2(u) \
978 sc_digit u ## d[DIGITS_PER_UINT64]; \
979 from_uint(DIGITS_PER_UINT64, u ## d, (uint64) u);
982 // ----------------------------------------------------------------------------
983 // SECTION: PLUS operators: +, +=, ++
984 // ----------------------------------------------------------------------------
986 // Cases to consider when computing u + v:
989 // 3. if sgn(u) == sgn(v)
990 // 3.1 u + v = +(u + v) = sgn(u) * (u + v)
991 // 3.2 (-u) + (-v) = -(u + v) = sgn(u) * (u + v)
992 // 4. if sgn(u) != sgn(v)
993 // 4.1 u + (-v) = u - v = sgn(u) * (u - v)
994 // 4.2 (-u) + v = -(u - v) ==> sgn(u) * (u - v)
996 // Specialization of above cases for computing ++u or u++:
998 // 3. u + 1 = u + 1 = sgn(u) * (u + 1)
999 // 4. (-u) + 1 = -(u - 1) = sgn(u) * (u - 1)
1002 operator + (const sc_unsigned
&u
, const sc_unsigned
&v
)
1004 if (u
.sgn
== SC_ZERO
) // case 1
1005 return sc_unsigned(v
);
1007 if (v
.sgn
== SC_ZERO
) // case 2
1008 return sc_unsigned(u
);
1011 return add_unsigned_friend(u
.sgn
, u
.nbits
, u
.ndigits
, u
.digit
,
1012 v
.sgn
, v
.nbits
, v
.ndigits
, v
.digit
);
1017 operator + (const sc_unsigned
&u
, uint64 v
)
1019 if (v
== 0) // case 2
1020 return sc_unsigned(u
);
1024 if (u
.sgn
== SC_ZERO
) // case 1
1025 return sc_unsigned(vs
, BITS_PER_UINT64
, DIGITS_PER_UINT64
, vd
, false);
1028 return add_unsigned_friend(u
.sgn
, u
.nbits
, u
.ndigits
, u
.digit
,
1029 vs
, BITS_PER_UINT64
, DIGITS_PER_UINT64
, vd
);
1034 operator + (uint64 u
, const sc_unsigned
&v
)
1036 if (u
== 0) // case 1
1037 return sc_unsigned(v
);
1041 if (v
.sgn
== SC_ZERO
) // case 2
1042 return sc_unsigned(us
, BITS_PER_UINT64
, DIGITS_PER_UINT64
, ud
, false);
1045 return add_unsigned_friend(us
, BITS_PER_UINT64
, DIGITS_PER_UINT64
, ud
,
1046 v
.sgn
, v
.nbits
, v
.ndigits
, v
.digit
);
1051 operator + (const sc_unsigned
&u
, unsigned long v
)
1053 if (v
== 0) // case 2
1054 return sc_unsigned(u
);
1058 if (u
.sgn
== SC_ZERO
) // case 1
1059 return sc_unsigned(vs
, BITS_PER_ULONG
, DIGITS_PER_ULONG
, vd
, false);
1062 return add_unsigned_friend(u
.sgn
, u
.nbits
, u
.ndigits
, u
.digit
,
1063 vs
, BITS_PER_ULONG
, DIGITS_PER_ULONG
, vd
);
1068 operator + (unsigned long u
, const sc_unsigned
&v
)
1070 if (u
== 0) // case 1
1071 return sc_unsigned(v
);
1075 if (v
.sgn
== SC_ZERO
) // case 2
1076 return sc_unsigned(us
, BITS_PER_ULONG
, DIGITS_PER_ULONG
, ud
, false);
1079 return add_unsigned_friend(us
, BITS_PER_ULONG
, DIGITS_PER_ULONG
, ud
,
1080 v
.sgn
, v
.nbits
, v
.ndigits
, v
.digit
);
1083 // The rest of the operators in this section are included from
1087 // ----------------------------------------------------------------------------
1088 // SECTION: MINUS operators: -, -=, --
1089 // ----------------------------------------------------------------------------
1091 // Cases to consider when computing u + v:
1094 // 3. if sgn(u) != sgn(v)
1095 // 3.1 u - (-v) = u + v = sgn(u) * (u + v)
1096 // 3.2 (-u) - v = -(u + v) ==> sgn(u) * (u + v)
1097 // 4. if sgn(u) == sgn(v)
1098 // 4.1 u - v = +(u - v) = sgn(u) * (u - v)
1099 // 4.2 (-u) - (-v) = -(u - v) = sgn(u) * (u - v)
1101 // Specialization of above cases for computing --u or u--:
1103 // 3. (-u) - 1 = -(u + 1) = sgn(u) * (u + 1)
1104 // 4. u - 1 = u - 1 = sgn(u) * (u - 1)
1106 // The operators in this section are included from sc_nbcommon.cpp.
1109 // ----------------------------------------------------------------------------
1110 // SECTION: MULTIPLICATION operators: *, *=
1111 // ----------------------------------------------------------------------------
1113 // Cases to consider when computing u * v:
1114 // 1. u * 0 = 0 * v = 0
1115 // 2. 1 * v = v and -1 * v = -v
1116 // 3. u * 1 = u and u * -1 = -u
1120 operator * (const sc_unsigned
&u
, const sc_unsigned
&v
)
1122 small_type s
= mul_signs(u
.sgn
, v
.sgn
);
1124 if (s
== SC_ZERO
) // case 1
1125 return sc_unsigned();
1128 return mul_unsigned_friend(s
, u
.nbits
, u
.ndigits
, u
.digit
,
1129 v
.nbits
, v
.ndigits
, v
.digit
);
1134 operator * (const sc_unsigned
&u
, uint64 v
)
1136 small_type s
= mul_signs(u
.sgn
, get_sign(v
));
1138 if (s
== SC_ZERO
) // case 1
1139 return sc_unsigned();
1144 return mul_unsigned_friend(s
, u
.nbits
, u
.ndigits
, u
.digit
,
1145 BITS_PER_UINT64
, DIGITS_PER_UINT64
, vd
);
1150 operator * (uint64 u
, const sc_unsigned
&v
)
1152 small_type s
= mul_signs(v
.sgn
, get_sign(u
));
1154 if (s
== SC_ZERO
) // case 1
1155 return sc_unsigned();
1160 return mul_unsigned_friend(s
, BITS_PER_UINT64
, DIGITS_PER_UINT64
, ud
,
1161 v
.nbits
, v
.ndigits
, v
.digit
);
1166 operator * (const sc_unsigned
&u
, unsigned long v
)
1168 small_type s
= mul_signs(u
.sgn
, get_sign(v
));
1170 if (s
== SC_ZERO
) // case 1
1171 return sc_unsigned();
1176 return mul_unsigned_friend(s
, u
.nbits
, u
.ndigits
, u
.digit
,
1177 BITS_PER_ULONG
, DIGITS_PER_ULONG
, vd
);
1181 operator * (unsigned long u
, const sc_unsigned
&v
)
1183 small_type s
= mul_signs(v
.sgn
, get_sign(u
));
1185 if (s
== SC_ZERO
) // case 1
1186 return sc_unsigned();
1191 return mul_unsigned_friend(s
, BITS_PER_ULONG
, DIGITS_PER_ULONG
, ud
,
1192 v
.nbits
, v
.ndigits
, v
.digit
);
1195 // The rest of the operators in this section are included from
1199 // ----------------------------------------------------------------------------
1200 // SECTION: DIVISION operators: /, /=
1201 // ----------------------------------------------------------------------------
1203 // Cases to consider when finding the quotient q = floor(u/v):
1204 // Note that u = q * v + r for r < q.
1205 // 1. 0 / 0 or u / 0 => error
1206 // 2. 0 / v => 0 = 0 * v + 0
1207 // 3. u / v & &u = v => u = 1 * u + 0 - u or v can be 1 or -1
1208 // 4. u / v & &u < v => u = 0 * v + u - u can be 1 or -1
1209 // 5. u / v & &u > v => u = q * v + r - v can be 1 or -1
1212 operator / (const sc_unsigned
&u
, const sc_unsigned
&v
)
1214 small_type s
= mul_signs(u
.sgn
, v
.sgn
);
1217 div_by_zero(v
.sgn
); // case 1
1218 return sc_unsigned(); // case 2
1222 return div_unsigned_friend(s
, u
.nbits
, u
.ndigits
, u
.digit
,
1223 v
.nbits
, v
.ndigits
, v
.digit
);
1228 operator / (const sc_unsigned
&u
, uint64 v
)
1230 small_type s
= mul_signs(u
.sgn
, get_sign(v
));
1233 div_by_zero(v
); // case 1
1234 return sc_unsigned(); // case 2
1240 return div_unsigned_friend(s
, u
.nbits
, u
.ndigits
, u
.digit
,
1241 BITS_PER_UINT64
, DIGITS_PER_UINT64
, vd
);
1246 operator / (uint64 u
, const sc_unsigned
&v
)
1248 small_type s
= mul_signs(v
.sgn
, get_sign(u
));
1251 div_by_zero(v
.sgn
); // case 1
1252 return sc_unsigned(); // case 2
1259 return div_unsigned_friend(s
, BITS_PER_UINT64
, DIGITS_PER_UINT64
, ud
,
1260 v
.nbits
, v
.ndigits
, v
.digit
);
1265 operator / (const sc_unsigned
&u
, unsigned long v
)
1267 small_type s
= mul_signs(u
.sgn
, get_sign(v
));
1270 div_by_zero(v
); // case 1
1271 return sc_unsigned(); // case 2
1277 return div_unsigned_friend(s
, u
.nbits
, u
.ndigits
, u
.digit
,
1278 BITS_PER_ULONG
, DIGITS_PER_ULONG
, vd
);
1283 operator / (unsigned long u
, const sc_unsigned
&v
)
1285 small_type s
= mul_signs(v
.sgn
, get_sign(u
));
1288 div_by_zero(v
.sgn
); // case 1
1289 return sc_unsigned(); // case 2
1296 return div_unsigned_friend(s
, BITS_PER_ULONG
, DIGITS_PER_ULONG
, ud
,
1297 v
.nbits
, v
.ndigits
, v
.digit
);
1300 // The rest of the operators in this section are included from
1304 // ----------------------------------------------------------------------------
1305 // SECTION: MOD operators: %, %=.
1306 // ----------------------------------------------------------------------------
1308 // Cases to consider when finding the remainder r = u % v:
1309 // Note that u = q * v + r for r < q.
1310 // 1. 0 % 0 or u % 0 => error
1311 // 2. 0 % v => 0 = 0 * v + 0
1312 // 3. u % v & &u = v => u = 1 * u + 0 - u or v can be 1 or -1
1313 // 4. u % v & &u < v => u = 0 * v + u - u can be 1 or -1
1314 // 5. u % v & &u > v => u = q * v + r - v can be 1 or -1
1317 operator % (const sc_unsigned
&u
, const sc_unsigned
&v
)
1319 if ((u
.sgn
== SC_ZERO
) || (v
.sgn
== SC_ZERO
)) {
1320 div_by_zero(v
.sgn
); // case 1
1321 return sc_unsigned(); // case 2
1325 return mod_unsigned_friend(u
.sgn
, u
.nbits
, u
.ndigits
, u
.digit
,
1326 v
.nbits
, v
.ndigits
, v
.digit
);
1331 operator % (const sc_unsigned
&u
, uint64 v
)
1333 if ((u
.sgn
== SC_ZERO
) || (v
== 0)) {
1334 div_by_zero(v
); // case 1
1335 return sc_unsigned(); // case 2
1341 return mod_unsigned_friend(u
.sgn
, u
.nbits
, u
.ndigits
, u
.digit
,
1342 BITS_PER_UINT64
, DIGITS_PER_UINT64
, vd
);
1348 operator % (uint64 u
, const sc_unsigned
&v
)
1350 if ((u
== 0) || (v
.sgn
== SC_ZERO
)) {
1351 div_by_zero(v
.sgn
); // case 1
1352 return sc_unsigned(); // case 2
1358 return mod_unsigned_friend(us
, BITS_PER_UINT64
, DIGITS_PER_UINT64
, ud
,
1359 v
.nbits
, v
.ndigits
, v
.digit
);
1364 operator % (const sc_unsigned
&u
, unsigned long v
)
1366 if ((u
.sgn
== SC_ZERO
) || (v
== 0)) {
1367 div_by_zero(v
); // case 1
1368 return sc_unsigned(); // case 2
1374 return mod_unsigned_friend(u
.sgn
, u
.nbits
, u
.ndigits
, u
.digit
,
1375 BITS_PER_ULONG
, DIGITS_PER_ULONG
, vd
);
1380 operator % (unsigned long u
, const sc_unsigned
&v
)
1382 if ((u
== 0) || (v
.sgn
== SC_ZERO
)) {
1383 div_by_zero(v
.sgn
); // case 1
1384 return sc_unsigned(); // case 2
1390 return mod_unsigned_friend(us
, BITS_PER_ULONG
, DIGITS_PER_ULONG
, ud
,
1391 v
.nbits
, v
.ndigits
, v
.digit
);
1394 // The rest of the operators in this section are included from
1398 // ----------------------------------------------------------------------------
1399 // SECTION: Bitwise AND operators: &, &=
1400 // ----------------------------------------------------------------------------
1402 // Cases to consider when computing u &v:
1403 // 1. u & 0 = 0 &v = 0
1404 // 2. u &v => sgn = +
1405 // 3. (-u) & (-v) => sgn = -
1406 // 4. u & (-v) => sgn = +
1407 // 5. (-u) &v => sgn = +
1410 operator & (const sc_unsigned
&u
, const sc_unsigned
&v
)
1412 if ((u
.sgn
== SC_ZERO
) || (v
.sgn
== SC_ZERO
)) // case 1
1413 return sc_unsigned();
1416 return and_unsigned_friend(u
.sgn
, u
.nbits
, u
.ndigits
, u
.digit
,
1417 v
.sgn
, v
.nbits
, v
.ndigits
, v
.digit
);
1422 operator & (const sc_unsigned
&u
, uint64 v
)
1424 if ((u
.sgn
== SC_ZERO
) || (v
== 0)) // case 1
1425 return sc_unsigned();
1430 return and_unsigned_friend(u
.sgn
, u
.nbits
, u
.ndigits
, u
.digit
,
1431 vs
, BITS_PER_UINT64
, DIGITS_PER_UINT64
, vd
);
1436 operator & (uint64 u
, const sc_unsigned
&v
)
1438 if ((u
== 0) || (v
.sgn
== SC_ZERO
)) // case 1
1439 return sc_unsigned();
1444 return and_unsigned_friend(us
, BITS_PER_UINT64
, DIGITS_PER_UINT64
, ud
,
1445 v
.sgn
, v
.nbits
, v
.ndigits
, v
.digit
);
1450 operator & (const sc_unsigned
&u
, unsigned long v
)
1452 if ((u
.sgn
== SC_ZERO
) || (v
== 0)) // case 1
1453 return sc_unsigned();
1458 return and_unsigned_friend(u
.sgn
, u
.nbits
, u
.ndigits
, u
.digit
,
1459 vs
, BITS_PER_ULONG
, DIGITS_PER_ULONG
, vd
);
1464 operator & (unsigned long u
, const sc_unsigned
&v
)
1466 if ((u
== 0) || (v
.sgn
== SC_ZERO
)) // case 1
1467 return sc_unsigned();
1472 return and_unsigned_friend(us
, BITS_PER_ULONG
, DIGITS_PER_ULONG
, ud
,
1473 v
.sgn
, v
.nbits
, v
.ndigits
, v
.digit
);
1476 // The rest of the operators in this section are included from
1480 // ----------------------------------------------------------------------------
1481 // SECTION: Bitwise OR operators: |, |=
1482 // ----------------------------------------------------------------------------
1484 // Cases to consider when computing u | v:
1487 // 3. u | v => sgn = +
1488 // 4. (-u) | (-v) => sgn = -
1489 // 5. u | (-v) => sgn = -
1490 // 6. (-u) | v => sgn = -
1493 operator | (const sc_unsigned
&u
, const sc_unsigned
&v
)
1495 if (v
.sgn
== SC_ZERO
) // case 1
1496 return sc_unsigned(u
);
1498 if (u
.sgn
== SC_ZERO
) // case 2
1499 return sc_unsigned(v
);
1502 return or_unsigned_friend(u
.sgn
, u
.nbits
, u
.ndigits
, u
.digit
,
1503 v
.sgn
, v
.nbits
, v
.ndigits
, v
.digit
);
1508 operator | (const sc_unsigned
&u
, uint64 v
)
1510 if (v
== 0) // case 1
1511 return sc_unsigned(u
);
1515 if (u
.sgn
== SC_ZERO
) // case 2
1516 return sc_unsigned(vs
, BITS_PER_UINT64
, DIGITS_PER_UINT64
, vd
, false);
1519 return or_unsigned_friend(u
.sgn
, u
.nbits
, u
.ndigits
, u
.digit
,
1520 vs
, BITS_PER_UINT64
, DIGITS_PER_UINT64
, vd
);
1525 operator | (uint64 u
, const sc_unsigned
&v
)
1528 return sc_unsigned(v
);
1532 if (v
.sgn
== SC_ZERO
)
1533 return sc_unsigned(us
, BITS_PER_UINT64
, DIGITS_PER_UINT64
, ud
, false);
1536 return or_unsigned_friend(us
, BITS_PER_UINT64
, DIGITS_PER_UINT64
, ud
,
1537 v
.sgn
, v
.nbits
, v
.ndigits
, v
.digit
);
1542 operator | (const sc_unsigned
&u
, unsigned long v
)
1544 if (v
== 0) // case 1
1545 return sc_unsigned(u
);
1549 if (u
.sgn
== SC_ZERO
) // case 2
1550 return sc_unsigned(vs
, BITS_PER_ULONG
, DIGITS_PER_ULONG
, vd
, false);
1553 return or_unsigned_friend(u
.sgn
, u
.nbits
, u
.ndigits
, u
.digit
,
1554 vs
, BITS_PER_ULONG
, DIGITS_PER_ULONG
, vd
);
1559 operator | (unsigned long u
, const sc_unsigned
&v
)
1562 return sc_unsigned(v
);
1566 if (v
.sgn
== SC_ZERO
)
1567 return sc_unsigned(us
, BITS_PER_ULONG
, DIGITS_PER_ULONG
, ud
, false);
1570 return or_unsigned_friend(us
, BITS_PER_ULONG
, DIGITS_PER_ULONG
, ud
,
1571 v
.sgn
, v
.nbits
, v
.ndigits
, v
.digit
);
1574 // The rest of the operators in this section are included from
1578 // ----------------------------------------------------------------------------
1579 // SECTION: Bitwise XOR operators: ^, ^=
1580 // ----------------------------------------------------------------------------
1582 // Cases to consider when computing u ^ v:
1583 // Note that u ^ v = (~u &v) | (u & ~v).
1586 // 3. u ^ v => sgn = +
1587 // 4. (-u) ^ (-v) => sgn = -
1588 // 5. u ^ (-v) => sgn = -
1589 // 6. (-u) ^ v => sgn = +
1592 operator ^ (const sc_unsigned
&u
, const sc_unsigned
&v
)
1594 if (v
.sgn
== SC_ZERO
) // case 1
1595 return sc_unsigned(u
);
1597 if (u
.sgn
== SC_ZERO
) // case 2
1598 return sc_unsigned(v
);
1601 return xor_unsigned_friend(u
.sgn
, u
.nbits
, u
.ndigits
, u
.digit
,
1602 v
.sgn
, v
.nbits
, v
.ndigits
, v
.digit
);
1607 operator ^ (const sc_unsigned
&u
, uint64 v
)
1609 if (v
== 0) // case 1
1610 return sc_unsigned(u
);
1614 if (u
.sgn
== SC_ZERO
) // case 2
1615 return sc_unsigned(vs
, BITS_PER_UINT64
, DIGITS_PER_UINT64
, vd
, false);
1618 return xor_unsigned_friend(u
.sgn
, u
.nbits
, u
.ndigits
, u
.digit
,
1619 vs
, BITS_PER_UINT64
, DIGITS_PER_UINT64
, vd
);
1623 operator ^ (uint64 u
, const sc_unsigned
&v
)
1626 return sc_unsigned(v
);
1630 if (v
.sgn
== SC_ZERO
)
1631 return sc_unsigned(us
, BITS_PER_UINT64
, DIGITS_PER_UINT64
, ud
, false);
1634 return xor_unsigned_friend(us
, BITS_PER_UINT64
, DIGITS_PER_UINT64
, ud
,
1635 v
.sgn
, v
.nbits
, v
.ndigits
, v
.digit
);
1640 operator ^ (const sc_unsigned
&u
, unsigned long v
)
1642 if (v
== 0) // case 1
1643 return sc_unsigned(u
);
1647 if (u
.sgn
== SC_ZERO
) // case 2
1648 return sc_unsigned(vs
, BITS_PER_ULONG
, DIGITS_PER_ULONG
, vd
, false);
1651 return xor_unsigned_friend(u
.sgn
, u
.nbits
, u
.ndigits
, u
.digit
,
1652 vs
, BITS_PER_ULONG
, DIGITS_PER_ULONG
, vd
);
1656 operator ^ (unsigned long u
, const sc_unsigned
&v
)
1659 return sc_unsigned(v
);
1663 if (v
.sgn
== SC_ZERO
)
1664 return sc_unsigned(us
, BITS_PER_ULONG
, DIGITS_PER_ULONG
, ud
, false);
1667 return xor_unsigned_friend(us
, BITS_PER_ULONG
, DIGITS_PER_ULONG
, ud
,
1668 v
.sgn
, v
.nbits
, v
.ndigits
, v
.digit
);
1671 // The rest of the operators in this section are included from
1675 // ----------------------------------------------------------------------------
1676 // SECTION: Bitwise NOT operator: ~
1677 // ----------------------------------------------------------------------------
1679 // Operators in this section are included from sc_nbcommon.cpp.
1682 // ----------------------------------------------------------------------------
1683 // SECTION: LEFT SHIFT operators: <<, <<=
1684 // ----------------------------------------------------------------------------
1687 operator << (const sc_unsigned
&u
, const sc_signed
&v
)
1689 if ((v
.sgn
== SC_ZERO
) || (v
.sgn
== SC_NEG
))
1690 return sc_unsigned(u
);
1692 return operator << (u
, v
.to_ulong());
1695 // The rest of the operators in this section are included from
1699 // ----------------------------------------------------------------------------
1700 // SECTION: RIGHT SHIFT operators: >>, >>=
1701 // ----------------------------------------------------------------------------
1704 operator >> (const sc_unsigned
&u
, const sc_signed
&v
)
1707 if ((v
.sgn
== SC_ZERO
) || (v
.sgn
== SC_NEG
))
1708 return sc_unsigned(u
);
1710 return operator >> (u
, v
.to_long());
1714 // The rest of the operators in this section are included from
1718 // ----------------------------------------------------------------------------
1719 // SECTION: Unary arithmetic operators.
1720 // ----------------------------------------------------------------------------
1723 operator + (const sc_unsigned
&u
)
1725 return sc_unsigned(u
);
1729 // ----------------------------------------------------------------------------
1730 // SECTION: EQUAL operator: ==
1731 // ----------------------------------------------------------------------------
1734 operator == (const sc_unsigned
&u
, const sc_unsigned
&v
)
1738 if (compare_unsigned(u
.sgn
, u
.nbits
, u
.ndigits
, u
.digit
,
1739 v
.sgn
, v
.nbits
, v
.ndigits
, v
.digit
) != 0) {
1747 operator == (const sc_unsigned
&u
, const sc_signed
&v
)
1749 if (v
.sgn
== SC_NEG
)
1751 if (compare_unsigned(u
.sgn
, u
.nbits
, u
.ndigits
, u
.digit
,
1752 v
.sgn
, v
.nbits
, v
.ndigits
, v
.digit
, 0, 1) != 0) {
1760 operator == (const sc_signed
&u
, const sc_unsigned
&v
)
1762 if (u
.sgn
== SC_NEG
)
1764 if (compare_unsigned(u
.sgn
, u
.nbits
, u
.ndigits
, u
.digit
,
1765 v
.sgn
, v
.nbits
, v
.ndigits
, v
.digit
, 1, 0) != 0) {
1773 operator == (const sc_unsigned
&u
, int64 v
)
1778 if (compare_unsigned(u
.sgn
, u
.nbits
, u
.ndigits
, u
.digit
,
1779 vs
, BITS_PER_UINT64
, DIGITS_PER_UINT64
, vd
) != 0) {
1787 operator == (int64 u
, const sc_unsigned
&v
)
1792 if (compare_unsigned(us
, BITS_PER_UINT64
, DIGITS_PER_UINT64
, ud
,
1793 v
.sgn
, v
.nbits
, v
.ndigits
, v
.digit
) != 0) {
1801 operator == (const sc_unsigned
&u
, uint64 v
)
1804 if (compare_unsigned(u
.sgn
, u
.nbits
, u
.ndigits
, u
.digit
,
1805 vs
, BITS_PER_UINT64
, DIGITS_PER_UINT64
, vd
) != 0)
1812 operator == (uint64 u
, const sc_unsigned
&v
)
1815 if (compare_unsigned(us
, BITS_PER_UINT64
, DIGITS_PER_UINT64
, ud
,
1816 v
.sgn
, v
.nbits
, v
.ndigits
, v
.digit
) != 0)
1823 operator == (const sc_unsigned
&u
, long v
)
1828 if (compare_unsigned(u
.sgn
, u
.nbits
, u
.ndigits
, u
.digit
,
1829 vs
, BITS_PER_ULONG
, DIGITS_PER_ULONG
, vd
) != 0)
1836 operator == (long u
, const sc_unsigned
&v
)
1841 if (compare_unsigned(us
, BITS_PER_ULONG
, DIGITS_PER_ULONG
, ud
,
1842 v
.sgn
, v
.nbits
, v
.ndigits
, v
.digit
) != 0)
1849 operator == (const sc_unsigned
&u
, unsigned long v
)
1852 if (compare_unsigned(u
.sgn
, u
.nbits
, u
.ndigits
, u
.digit
,
1853 vs
, BITS_PER_ULONG
, DIGITS_PER_ULONG
, vd
) != 0)
1860 operator == (unsigned long u
, const sc_unsigned
&v
)
1863 if (compare_unsigned(us
, BITS_PER_ULONG
, DIGITS_PER_ULONG
, ud
,
1864 v
.sgn
, v
.nbits
, v
.ndigits
, v
.digit
) != 0)
1870 // ----------------------------------------------------------------------------
1871 // SECTION: NOT_EQUAL operator: !=
1872 // ----------------------------------------------------------------------------
1875 operator != (const sc_unsigned
&u
, const sc_signed
&v
)
1877 return (!operator == (u
, v
));
1882 operator != (const sc_signed
&u
, const sc_unsigned
&v
)
1884 return (!operator == (u
, v
));
1887 // The rest of the operators in this section are included from sc_nbcommon.cpp.
1890 // ----------------------------------------------------------------------------
1891 // SECTION: LESS THAN operator: <
1892 // ----------------------------------------------------------------------------
1895 operator < (const sc_unsigned
&u
, const sc_unsigned
&v
)
1899 if (compare_unsigned(u
.sgn
, u
.nbits
, u
.ndigits
, u
.digit
,
1900 v
.sgn
, v
.nbits
, v
.ndigits
, v
.digit
) < 0) {
1908 operator < (const sc_unsigned
&u
, const sc_signed
&v
)
1910 if (v
.sgn
== SC_NEG
)
1912 if (compare_unsigned(u
.sgn
, u
.nbits
, u
.ndigits
, u
.digit
,
1913 v
.sgn
, v
.nbits
, v
.ndigits
, v
.digit
, 0, 1) < 0) {
1921 operator < (const sc_signed
&u
, const sc_unsigned
&v
)
1923 if (u
.sgn
== SC_NEG
)
1925 if (compare_unsigned(u
.sgn
, u
.nbits
, u
.ndigits
, u
.digit
,
1926 v
.sgn
, v
.nbits
, v
.ndigits
, v
.digit
, 1, 0) < 0) {
1934 operator < (const sc_unsigned
&u
, int64 v
)
1939 if (compare_unsigned(u
.sgn
, u
.nbits
, u
.ndigits
, u
.digit
,
1940 vs
, BITS_PER_UINT64
, DIGITS_PER_UINT64
, vd
) < 0) {
1948 operator < (int64 u
, const sc_unsigned
&v
)
1953 if (compare_unsigned(us
, BITS_PER_UINT64
, DIGITS_PER_UINT64
, ud
,
1954 v
.sgn
, v
.nbits
, v
.ndigits
, v
.digit
) < 0) {
1962 operator < (const sc_unsigned
&u
, uint64 v
)
1965 if (compare_unsigned(u
.sgn
, u
.nbits
, u
.ndigits
, u
.digit
,
1966 vs
, BITS_PER_UINT64
, DIGITS_PER_UINT64
, vd
) < 0) {
1974 operator < (uint64 u
, const sc_unsigned
&v
)
1977 if (compare_unsigned(us
, BITS_PER_UINT64
, DIGITS_PER_UINT64
, ud
,
1978 v
.sgn
, v
.nbits
, v
.ndigits
, v
.digit
) < 0){
1986 operator < (const sc_unsigned
&u
, long v
)
1991 if (compare_unsigned(u
.sgn
, u
.nbits
, u
.ndigits
, u
.digit
,
1992 vs
, BITS_PER_ULONG
, DIGITS_PER_ULONG
, vd
) < 0) {
2000 operator < (long u
, const sc_unsigned
&v
)
2005 if (compare_unsigned(us
, BITS_PER_ULONG
, DIGITS_PER_ULONG
, ud
,
2006 v
.sgn
, v
.nbits
, v
.ndigits
, v
.digit
) < 0) {
2014 operator < (const sc_unsigned
&u
, unsigned long v
)
2017 if (compare_unsigned(u
.sgn
, u
.nbits
, u
.ndigits
, u
.digit
,
2018 vs
, BITS_PER_ULONG
, DIGITS_PER_ULONG
, vd
) < 0) {
2026 operator < (unsigned long u
, const sc_unsigned
&v
)
2029 if (compare_unsigned(us
, BITS_PER_ULONG
, DIGITS_PER_ULONG
, ud
,
2030 v
.sgn
, v
.nbits
, v
.ndigits
, v
.digit
) < 0) {
2037 // ----------------------------------------------------------------------------
2038 // SECTION: LESS THAN or EQUAL operator: <=
2039 // ----------------------------------------------------------------------------
2042 operator <= (const sc_unsigned
&u
, const sc_signed
&v
)
2044 return (operator < (u
, v
) || operator == (u
, v
));
2049 operator <= (const sc_signed
&u
, const sc_unsigned
&v
)
2051 return (operator < (u
, v
) || operator == (u
, v
));
2054 // The rest of the operators in this section are included from sc_nbcommon.cpp.
2057 // ----------------------------------------------------------------------------
2058 // SECTION: GREATER THAN operator: >
2059 // ----------------------------------------------------------------------------
2062 operator > (const sc_unsigned
&u
, const sc_signed
&v
)
2064 return (!(operator <= (u
, v
)));
2069 operator > (const sc_signed
&u
, const sc_unsigned
&v
)
2071 return (!(operator <= (u
, v
)));
2074 // The rest of the operators in this section are included from sc_nbcommon.cpp.
2077 // ----------------------------------------------------------------------------
2078 // SECTION: GREATER THAN or EQUAL operator: >=
2079 // ----------------------------------------------------------------------------
2082 operator >= (const sc_unsigned
&u
, const sc_signed
&v
)
2084 return (!(operator < (u
, v
)));
2089 operator >= (const sc_signed
&u
, const sc_unsigned
&v
)
2091 return (!(operator < (u
, v
)));
2094 // The rest of the operators in this section are included from sc_nbcommon.cpp.
2097 // ----------------------------------------------------------------------------
2099 // ----------------------------------------------------------------------------
2101 // Compare u and v as unsigned and return r
2107 compare_unsigned(small_type us
, int unb
, int und
, const sc_digit
*ud
,
2108 small_type vs
, int vnb
, int vnd
, const sc_digit
*vd
,
2109 small_type if_u_signed
, small_type if_v_signed
)
2112 if (us
== SC_ZERO
) {
2115 int cmp_res
= vec_skip_and_cmp(und
, ud
, vnd
, vd
);
2128 int nd
= (us
== SC_NEG
? und
: vnd
);
2131 sc_digit d
[MAX_NDIGITS
];
2133 sc_digit
*d
= new sc_digit
[nd
];
2137 vec_copy(nd
, d
, ud
);
2138 vec_complement(nd
, d
);
2139 trim(if_u_signed
, unb
, nd
, d
);
2140 cmp_res
= vec_skip_and_cmp(nd
, d
, vnd
, vd
);
2142 vec_copy(nd
, d
, vd
);
2143 vec_complement(nd
, d
);
2144 trim(if_v_signed
, vnb
, nd
, d
);
2145 cmp_res
= vec_skip_and_cmp(und
, ud
, nd
, d
);
2148 #ifndef SC_MAX_NBITS
2157 // ----------------------------------------------------------------------------
2158 // SECTION: Public members - Other utils.
2159 // ----------------------------------------------------------------------------
2162 sc_unsigned::iszero() const
2164 if (sgn
== SC_ZERO
) {
2166 } else if (sgn
== SC_NEG
) {
2167 // A negative unsigned number can be zero, e.g., -16 in 4 bits, so
2171 sc_digit d
[MAX_NDIGITS
];
2173 sc_digit
*d
= new sc_digit
[ndigits
];
2176 vec_copy(ndigits
, d
, digit
);
2177 vec_complement(ndigits
, d
);
2178 trim_unsigned(nbits
, ndigits
, d
);
2180 bool res
= check_for_zero(ndigits
, d
);
2182 #ifndef SC_MAX_NBITS
2192 // The rest of the utils in this section are included from sc_nbcommon.cpp.
2195 // ----------------------------------------------------------------------------
2196 // SECTION: Private members.
2197 // ----------------------------------------------------------------------------
2199 // The private members in this section are included from
2202 #define CLASS_TYPE sc_unsigned
2203 #define CLASS_TYPE_STR "sc_unsigned"
2205 #define ADD_HELPER add_unsigned_friend
2206 #define SUB_HELPER sub_unsigned_friend
2207 #define MUL_HELPER mul_unsigned_friend
2208 #define DIV_HELPER div_unsigned_friend
2209 #define MOD_HELPER mod_unsigned_friend
2210 #define AND_HELPER and_unsigned_friend
2211 #define OR_HELPER or_unsigned_friend
2212 #define XOR_HELPER xor_unsigned_friend
2214 #include "sc_nbfriends.inc"
2218 #define IF_SC_SIGNED 0 // 0 = sc_unsigned
2219 #define CLASS_TYPE_SUBREF sc_unsigned_subref_r
2220 #define OTHER_CLASS_TYPE sc_signed
2221 #define OTHER_CLASS_TYPE_SUBREF sc_signed_subref_r
2223 #define MUL_ON_HELPER mul_on_help_unsigned
2224 #define DIV_ON_HELPER div_on_help_unsigned
2225 #define MOD_ON_HELPER mod_on_help_unsigned
2227 #include "sc_nbcommon.inc"
2229 #undef MOD_ON_HELPER
2230 #undef DIV_ON_HELPER
2231 #undef MUL_ON_HELPER
2233 #undef OTHER_CLASS_TYPE_SUBREF
2234 #undef OTHER_CLASS_TYPE
2235 #undef CLASS_TYPE_SUBREF
2249 #undef CLASS_TYPE_STR
2251 #include "sc_unsigned_bitref.inc"
2252 #include "sc_unsigned_subref.inc"
2255 #undef CONVERT_LONG_2
2256 #undef CONVERT_INT64
2257 #undef CONVERT_INT64_2
2259 } // namespace sc_dt