1 /*****************************************************************************
2
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
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
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.
17
18 *****************************************************************************/
19
20 /*****************************************************************************
21
22 sc_unsigned.cpp -- Arbitrary precision signed arithmetic.
23
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.
30
31 Original Author: Ali Dasdan, Synopsys, Inc.
32
33 *****************************************************************************/
34
35 /*****************************************************************************
36
37 MODIFICATION LOG - modifiers, enter your name, affiliation, date and
38 changes you are making here.
39
40 Name, Affiliation, Date:
41 Description of Modification:
42
43 *****************************************************************************/
44
45
46 // $Log: sc_unsigned.cpp,v $
47 // Revision 1.7 2011/02/18 20:19:15 acg
48 // Andy Goodrich: updating Copyright notice.
49 //
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.
53 //
54 // Revision 1.5 2008/06/19 17:47:57 acg
55 // Andy Goodrich: fixes for bugs. See 2.2.1 RELEASENOTES.
56 //
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().
60 //
61 // Revision 1.3 2007/11/04 21:27:00 acg
62 // Andy Goodrich: changes to make sure the proper value is returned from
63 // concat_get_data().
64 //
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.
67 //
68 // Revision 1.1.1.1 2006/12/15 20:20:05 acg
69 // SystemC 2.3
70 //
71 // Revision 1.4 2006/08/29 23:36:54 acg
72 // Andy Goodrich: fixed and_reduce and optimized or_reduce.
73 //
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
76 // source.
77 //
78
79 #include <cctype>
80 #include <cmath>
81
82 #include "sysc/kernel/sc_cmnhdr.h"
83 #include "sysc/kernel/sc_macros.h"
84 #include "sysc/datatypes/int/sc_unsigned.h"
85 #include "sysc/datatypes/int/sc_signed.h"
86 #include "sysc/datatypes/int/sc_int_base.h"
87 #include "sysc/datatypes/int/sc_uint_base.h"
88 #include "sysc/datatypes/int/sc_int_ids.h"
89 #include "sysc/datatypes/bit/sc_bv_base.h"
90 #include "sysc/datatypes/bit/sc_lv_base.h"
91 #include "sysc/datatypes/misc/sc_concatref.h"
92 #include "sysc/datatypes/fx/sc_ufix.h"
93 #include "sysc/datatypes/fx/scfx_other_defs.h"
94
95 #include <sstream>
96
97 // explicit template instantiations
98 namespace sc_core {
99 template class SC_API sc_vpool<sc_dt::sc_unsigned_bitref>;
100 template class SC_API sc_vpool<sc_dt::sc_unsigned_subref>;
101 template class SC_API sc_vpool<sc_dt::sc_unsigned>;
102 } // namespace sc_core
103
104 namespace sc_dt
105 {
106
107 // Pool of temporary instances:
108 // The sc_unsigned pool is used by the concatenation support.
109 // The bit and part reference pools allow references to be returned.
110
111 sc_core::sc_vpool<sc_unsigned> sc_unsigned::m_pool(8);
112 sc_core::sc_vpool<sc_unsigned_bitref> sc_unsigned_bitref::m_pool(9);
113 sc_core::sc_vpool<sc_unsigned_subref> sc_unsigned_subref::m_pool(9);
114
115
invalid_init(const char * type_name,int nb) const116 void sc_unsigned::invalid_init( const char* type_name, int nb ) const
117 {
118 std::stringstream msg;
119 msg << "sc_unsigned( "<< type_name << " ) : nb = " << nb << " is not valid";
120 SC_REPORT_ERROR( sc_core::SC_ID_INIT_FAILED_, msg.str().c_str() );
121 }
122
123
124 // -----------------------------------------------------------------------------
125 // SECTION: Public members - Invalid selections.
126 // -----------------------------------------------------------------------------
127
128 void
invalid_index(int i) const129 sc_unsigned::invalid_index( int i ) const
130 {
131 std::stringstream msg;
132 msg << "sc_biguint bit selection: index = " << i << " violates "
133 "0 <= index <= " << (nbits-2);
134 SC_REPORT_ERROR( sc_core::SC_ID_OUT_OF_BOUNDS_, msg.str().c_str() );
135 sc_core::sc_abort(); // can't recover from here
136 }
137
138 void
invalid_range(int l,int r) const139 sc_unsigned::invalid_range( int l, int r ) const
140 {
141 std::stringstream msg;
142 msg << "sc_biguint part selection: left = " << l << ", right = " << r << "\n"
143 " violates either (" << (nbits-2) << " >= left >= 0) or "
144 "(" << (nbits-2) << " >= right >= 0)";
145 SC_REPORT_ERROR( sc_core::SC_ID_OUT_OF_BOUNDS_, msg.str().c_str() );
146 sc_core::sc_abort(); // can't recover from here
147 }
148
149 // ----------------------------------------------------------------------------
150 // SECTION: Public members - Concatenation support.
151 // ----------------------------------------------------------------------------
152
153 // Most public members are included from sc_nbcommon.inc. However, some
154 // concatenation support appears here to optimize between the signed and
155 // unsigned cases.
156
157
158
159 // Insert this object's value at the specified place in a vector of big style
160 // values.
161
concat_get_ctrl(sc_digit * dst_p,int low_i) const162 bool sc_unsigned::concat_get_ctrl( sc_digit* dst_p, int low_i ) const
163 {
164 int dst_i; // Index to next word to set in dst_p.
165 int end_i; // Index of high order word to set.
166 int left_shift; // Amount to shift value left.
167 sc_digit mask; // Mask for partial word sets.
168
169
170 // CALCULATE METRICS FOR DATA MOVEMENT:
171
172 dst_i = low_i / BITS_PER_DIGIT;
173 end_i = (low_i + nbits - 2) / BITS_PER_DIGIT;
174 left_shift = low_i % BITS_PER_DIGIT;
175
176
177 // MOVE FIRST WORD (IT MAY BE PARTIAL) AND THEN ANY OTHERS:
178 //
179 // We may "clobber" upper bits, but they will be written at some point
180 // anyway.
181
182 mask = ~(~0U << left_shift);
183 dst_p[dst_i] = ( dst_p[dst_i] & ~mask );
184 dst_i++;
185
186 for ( ; dst_i <= end_i; dst_i++ ) dst_p[dst_i] = 0;
187
188 return false;
189 }
190
concat_get_data(sc_digit * dst_p,int low_i) const191 bool sc_unsigned::concat_get_data( sc_digit* dst_p, int low_i ) const
192 {
193 sc_digit carry; // Carry for negating value.
194 int dst_i; // Index to next word to set in dst_p.
195 int end_i; // Index of high order word to set.
196 int high_i; // Index w/in word of high order bit.
197 int left_shift; // Amount to shift value left.
198 sc_digit left_word; // High word component for set.
199 sc_digit mask; // Mask for partial word sets.
200 bool result; // True if inserting non-zero data.
201 int right_shift; // Amount to shift value right.
202 sc_digit right_word; // Low word component for set.
203 int real_bits; // nbits - 1.
204 int src_i; // Index to next word to get from digit.
205
206
207 // CALCULATE METRICS FOR DATA MOVEMENT:
208
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;
214
215
216 switch ( sgn )
217 {
218
219 // POSITIVE SOURCE VALUE:
220
221 case SC_POS:
222 result = true;
223
224 // ALL DATA TO BE MOVED IS IN A SINGLE WORD:
225
226 if ( dst_i == end_i )
227 {
228 mask = ~(~0U << left_shift);
229 dst_p[dst_i] = ( ( dst_p[dst_i] & mask ) |
230 (digit[0] << left_shift) ) & DIGIT_MASK;
231 }
232
233
234 // DATA IS IN MORE THAN ONE WORD, BUT IS WORD ALIGNED:
235
236 else if ( left_shift == 0 )
237 {
238 for ( src_i = 0; dst_i < end_i; dst_i++, src_i++ )
239 {
240 dst_p[dst_i] = digit[src_i];
241 }
242 high_i = high_i % BITS_PER_DIGIT;
243 mask = ~(~1U << high_i) & DIGIT_MASK;
244 dst_p[dst_i] = digit[src_i] & mask;
245 }
246
247
248 // DATA IS IN MORE THAN ONE WORD, AND NOT WORD ALIGNED:
249
250 else
251 {
252 high_i = high_i % BITS_PER_DIGIT;
253 right_shift = BITS_PER_DIGIT - left_shift;
254 mask = ~(~0U << left_shift);
255 right_word = digit[0];
256 dst_p[dst_i] = (dst_p[dst_i] & mask) |
257 ((right_word << left_shift) & DIGIT_MASK);
258 for ( src_i = 1, dst_i++; dst_i < end_i; dst_i++, src_i++ )
259 {
260 left_word = digit[src_i];
261 dst_p[dst_i] = ((left_word << left_shift)&DIGIT_MASK) |
262 (right_word >> right_shift);
263 right_word = left_word;
264 }
265 left_word = (src_i < ndigits) ? digit[src_i] : 0;
266 mask = ~(~1U << high_i) & DIGIT_MASK;
267 dst_p[dst_i] = ((left_word << left_shift) |
268 (right_word >> right_shift)) & mask;
269 }
270 break;
271
272 // SOURCE VALUE IS NEGATIVE:
273
274 case SC_NEG:
275
276 // ALL DATA TO BE MOVED IS IN A SINGLE WORD:
277
278 result = true;
279 if ( dst_i == end_i )
280 {
281 mask = ~(~0U << nbits);
282 right_word = ((digit[0] ^ DIGIT_MASK) + 1) & mask;
283 mask = ~(~0U << left_shift);
284 dst_p[dst_i] = ( ( dst_p[dst_i] & mask ) |
285 (right_word << left_shift) ) & DIGIT_MASK;
286 }
287
288
289 // DATA IS IN MORE THAN ONE WORD, BUT IS WORD ALIGNED:
290
291 else if ( left_shift == 0 )
292 {
293 carry = 1;
294 for ( src_i = 0; dst_i < end_i; dst_i++, src_i++ )
295 {
296 right_word = (digit[src_i] ^ DIGIT_MASK) + carry;
297 dst_p[dst_i] = right_word & DIGIT_MASK;
298 carry = right_word >> BITS_PER_DIGIT;
299 }
300 high_i = high_i % BITS_PER_DIGIT;
301 mask = (~(~1U << high_i)) & DIGIT_MASK;
302 right_word = (src_i < ndigits) ?
303 (digit[src_i] ^ DIGIT_MASK) + carry : DIGIT_MASK + carry;
304 dst_p[dst_i] = right_word & mask;
305 }
306
307
308 // DATA IS IN MORE THAN ONE WORD, AND NOT WORD ALIGNED:
309
310 else
311 {
312 high_i = high_i % BITS_PER_DIGIT;
313 right_shift = BITS_PER_DIGIT - left_shift;
314 mask = ~(~0U << left_shift);
315 carry = 1;
316 right_word = (digit[0] ^ DIGIT_MASK) + carry;
317 dst_p[dst_i] = (dst_p[dst_i] & mask) |
318 ((right_word << left_shift) & DIGIT_MASK);
319 carry = right_word >> BITS_PER_DIGIT;
320 right_word &= DIGIT_MASK;
321 for ( src_i = 1, dst_i++; dst_i < end_i; dst_i++, src_i++ )
322 {
323 left_word = (digit[src_i] ^ DIGIT_MASK) + carry;
324 dst_p[dst_i] = ((left_word << left_shift)&DIGIT_MASK) |
325 (right_word >> right_shift);
326 carry = left_word >> BITS_PER_DIGIT;
327 right_word = left_word & DIGIT_MASK;
328 }
329 left_word = (src_i < ndigits) ?
330 (digit[src_i] ^ DIGIT_MASK) + carry : carry;
331 mask = ~(~1U << high_i) & DIGIT_MASK;
332 dst_p[dst_i] = ((left_word << left_shift) |
333 (right_word >> right_shift)) & mask;
334 }
335 break;
336
337
338 // VALUE IS ZERO:
339
340 default:
341 result = false;
342
343 // ALL DATA TO BE MOVED IS IN A SINGLE WORD:
344
345 if ( dst_i == end_i )
346 {
347 mask = ~(~0U << real_bits) << left_shift;
348 dst_p[dst_i] = dst_p[dst_i] & ~mask;
349 }
350
351
352 // DATA IS IN MORE THAN ONE WORD, BUT IS WORD ALIGNED:
353
354 else if ( left_shift == 0 )
355 {
356 for ( src_i = 0; dst_i < end_i; dst_i++, src_i++ )
357 {
358 dst_p[dst_i] = 0;
359 }
360 dst_p[dst_i] = 0;
361 }
362
363
364 // DATA IS IN MORE THAN ONE WORD, AND NOT WORD ALIGNED:
365
366 else
367 {
368 mask = ~(~0U << left_shift);
369 dst_p[dst_i] = (dst_p[dst_i] & mask);
370 for ( dst_i++; dst_i <= end_i; dst_i++ )
371 {
372 dst_p[dst_i] = 0;
373 }
374 }
375 break;
376 }
377 return result;
378 }
379
380 // Return this object instance's bits as a uint64 without sign extension.
381
concat_get_uint64() const382 uint64 sc_unsigned::concat_get_uint64() const
383 {
384 uint64 result;
385
386 switch ( sgn )
387 {
388 case SC_POS:
389 result = 0;
390 if ( ndigits > 2 )
391 result = digit[2];
392 if ( ndigits > 1 )
393 result = (result << BITS_PER_DIGIT) | digit[1];
394 result = (result << BITS_PER_DIGIT) | digit[0];
395 break;
396 default:
397 result = 0;
398 break;
399 }
400 return result;
401 }
402
403 // #### OPTIMIZE
concat_set(int64 src,int low_i)404 void sc_unsigned::concat_set(int64 src, int low_i)
405 {
406 *this = (low_i < 64) ? src >> low_i : src >> 63;
407 }
408
concat_set(const sc_signed & src,int low_i)409 void sc_unsigned::concat_set(const sc_signed& src, int low_i)
410 {
411 if ( low_i < src.length() )
412 *this = src >> low_i;
413 else
414 *this = (src<0) ? (int_type)-1 : 0;
415 }
416
concat_set(const sc_unsigned & src,int low_i)417 void sc_unsigned::concat_set(const sc_unsigned& src, int low_i)
418 {
419 if ( low_i < src.length() )
420 *this = src >> low_i;
421 else
422 *this = 0;
423 }
424
concat_set(uint64 src,int low_i)425 void sc_unsigned::concat_set(uint64 src, int low_i)
426 {
427 *this = (low_i < 64) ? src >> low_i : 0;
428 }
429
430
431 // ----------------------------------------------------------------------------
432 // SECTION: Public members - Reduction methods.
433 // ----------------------------------------------------------------------------
434
and_reduce() const435 bool sc_unsigned::and_reduce() const
436 {
437 int i; // Digit examining.
438
439 if ( sgn == SC_ZERO ) return false;
440 for ( i = 0; i < ndigits-1; i++ )
441 if ( (digit[i] & DIGIT_MASK) != DIGIT_MASK ) return false;
442 if ( (digit[i] & ~(~0U << ((nbits-1) % BITS_PER_DIGIT))) ==
443 static_cast<sc_digit>(~(~0U << ((nbits-1) % BITS_PER_DIGIT))) )
444 return true;
445 return false;
446 }
447
or_reduce() const448 bool sc_unsigned::or_reduce() const
449 {
450 return ( sgn == SC_ZERO ) ? false : true;
451 }
452
xor_reduce() const453 bool sc_unsigned::xor_reduce() const
454 {
455 int i; // Digit examining.
456 int odd; // Flag for odd number of digits.
457
458 odd = 0;
459 for ( i = 0; i < nbits-1; i++ )
460 if ( test(i) ) odd = ~odd;
461 return odd ? true : false;
462 }
463
464
465 // ----------------------------------------------------------------------------
466 // SECTION: Public members - Assignment operators.
467 // ----------------------------------------------------------------------------
468
469 // assignment operators
470
471 const sc_unsigned&
operator =(const char * a)472 sc_unsigned::operator = ( const char* a )
473 {
474 if( a == 0 ) {
475 SC_REPORT_ERROR( sc_core::SC_ID_CONVERSION_FAILED_,
476 "character string is zero" );
477 }
478 else if( *a == 0 ) {
479 SC_REPORT_ERROR( sc_core::SC_ID_CONVERSION_FAILED_,
480 "character string is empty" );
481 }
482 else try {
483 int len = length();
484 sc_ufix aa( a, len, len, SC_TRN, SC_WRAP, 0, SC_ON );
485 return this->operator = ( aa );
486 } catch( const sc_core::sc_report & ) {
487 std::stringstream msg;
488 msg << "character string '" << a << "' is not valid";
489 SC_REPORT_ERROR( sc_core::SC_ID_CONVERSION_FAILED_, msg.str().c_str() );
490 }
491 return *this;
492 }
493
494 const sc_unsigned&
operator =(int64 v)495 sc_unsigned::operator=(int64 v)
496 {
497 sgn = get_sign(v);
498 if ( sgn == SC_ZERO ) {
499 vec_zero(ndigits, digit);
500 }
501 else {
502 from_uint(ndigits, digit, (uint64) v);
503 convert_SM_to_2C_to_SM();
504 }
505 return *this;
506 }
507
508 const sc_unsigned&
operator =(uint64 v)509 sc_unsigned::operator=(uint64 v)
510 {
511 if (v == 0) {
512 sgn = SC_ZERO;
513 vec_zero(ndigits, digit);
514 }
515 else {
516 sgn = SC_POS;
517 from_uint(ndigits, digit, v);
518 convert_SM_to_2C_to_SM();
519 }
520 return *this;
521 }
522
523 const sc_unsigned&
operator =(long v)524 sc_unsigned::operator=(long v)
525 {
526 sgn = get_sign(v);
527 if ( sgn == SC_ZERO ) {
528 vec_zero(ndigits, digit);
529 }
530 else {
531 from_uint(ndigits, digit, (unsigned long) v);
532 convert_SM_to_2C_to_SM();
533 }
534 return *this;
535 }
536
537 const sc_unsigned&
operator =(unsigned long v)538 sc_unsigned::operator=(unsigned long v)
539 {
540 if (v == 0) {
541 sgn = SC_ZERO;
542 vec_zero(ndigits, digit);
543 }
544 else {
545 sgn = SC_POS;
546 from_uint(ndigits, digit, v);
547 convert_SM_to_2C_to_SM();
548 }
549 return *this;
550 }
551
552 const sc_unsigned&
operator =(double v)553 sc_unsigned::operator=(double v)
554 {
555 is_bad_double(v);
556 sgn = SC_POS;
557 int i = 0;
558 while (std::floor(v) && (i < ndigits)) {
559 #ifndef _WIN32
560 digit[i++] = ((sc_digit)std::floor(remainder(v, DIGIT_RADIX))) & DIGIT_MASK;
561 #else
562 digit[i++] = ((sc_digit)std::floor(std::fmod(v, DIGIT_RADIX))) & DIGIT_MASK;
563 #endif
564 v /= DIGIT_RADIX;
565 }
566 vec_zero(i, ndigits, digit);
567 convert_SM_to_2C_to_SM();
568 return *this;
569 }
570
571
572 // ----------------------------------------------------------------------------
573
574 const sc_unsigned&
operator =(const sc_bv_base & v)575 sc_unsigned::operator = ( const sc_bv_base& v )
576 {
577 int minlen = sc_min( nbits, v.length() );
578 int i = 0;
579 for( ; i < minlen; ++ i ) {
580 safe_set( i, v.get_bit( i ), digit );
581 }
582 for( ; i < nbits; ++ i ) {
583 safe_set( i, 0, digit ); // zero-extend
584 }
585 convert_2C_to_SM();
586 return *this;
587 }
588
589 const sc_unsigned&
operator =(const sc_lv_base & v)590 sc_unsigned::operator = ( const sc_lv_base& v )
591 {
592 int minlen = sc_min( nbits, v.length() );
593 int i = 0;
594 for( ; i < minlen; ++ i ) {
595 safe_set( i, sc_logic( v.get_bit( i ) ).to_bool(), digit );
596 }
597 for( ; i < nbits; ++ i ) {
598 safe_set( i, 0, digit ); // zero-extend
599 }
600 convert_2C_to_SM();
601 return *this;
602 }
603
604
605 // explicit conversion to character string
606
607 const std::string
to_string(sc_numrep numrep) const608 sc_unsigned::to_string( sc_numrep numrep ) const
609 {
610 int len = length();
611 sc_ufix aa( *this, len, len, SC_TRN, SC_WRAP, 0, SC_ON );
612 return aa.to_string( numrep );
613 }
614
615 const std::string
to_string(sc_numrep numrep,bool w_prefix) const616 sc_unsigned::to_string( sc_numrep numrep, bool w_prefix ) const
617 {
618 int len = length();
619 sc_ufix aa( *this, len, len, SC_TRN, SC_WRAP, 0, SC_ON );
620 return aa.to_string( numrep, w_prefix );
621 }
622
623
624 // ----------------------------------------------------------------------------
625 // SECTION: Interfacing with sc_int_base
626 // ----------------------------------------------------------------------------
627
628 const sc_unsigned&
operator =(const sc_int_base & v)629 sc_unsigned::operator= (const sc_int_base& v)
630 { return operator=((int64) v); }
631
632 const sc_unsigned&
operator +=(const sc_int_base & v)633 sc_unsigned::operator+=(const sc_int_base& v)
634 { return operator+=((int64) v); }
635
636 const sc_unsigned&
operator -=(const sc_int_base & v)637 sc_unsigned::operator-=(const sc_int_base& v)
638 { return operator-=((int64) v); }
639
640 const sc_unsigned&
operator *=(const sc_int_base & v)641 sc_unsigned::operator*=(const sc_int_base& v)
642 { return operator*=((int64) v); }
643
644 const sc_unsigned&
operator /=(const sc_int_base & v)645 sc_unsigned::operator/=(const sc_int_base& v)
646 { return operator/=((int64) v); }
647
648 const sc_unsigned&
operator %=(const sc_int_base & v)649 sc_unsigned::operator%=(const sc_int_base& v)
650 { return operator%=((int64) v); }
651
652 const sc_unsigned&
operator &=(const sc_int_base & v)653 sc_unsigned::operator&=(const sc_int_base& v)
654 { return operator&=((int64) v); }
655
656 const sc_unsigned&
operator |=(const sc_int_base & v)657 sc_unsigned::operator|=(const sc_int_base& v)
658 { return operator|=((int64) v); }
659
660 const sc_unsigned&
operator ^=(const sc_int_base & v)661 sc_unsigned::operator^=(const sc_int_base& v)
662 { return operator^=((int64) v); }
663
664 sc_unsigned
operator <<(const sc_unsigned & u,const sc_int_base & v)665 operator<<(const sc_unsigned& u, const sc_int_base& v)
666 { return operator<<(u, (int64) v); }
667 const sc_unsigned&
operator <<=(const sc_int_base & v)668 sc_unsigned::operator<<=(const sc_int_base& v)
669 { return operator<<=((int64) v); }
670
671 sc_unsigned
operator >>(const sc_unsigned & u,const sc_int_base & v)672 operator>>(const sc_unsigned& u, const sc_int_base& v)
673 { return operator>>(u, (int64) v); }
674 const sc_unsigned&
operator >>=(const sc_int_base & v)675 sc_unsigned::operator>>=(const sc_int_base& v)
676 { return operator>>=((int64) v); }
677
678 bool
operator ==(const sc_unsigned & u,const sc_int_base & v)679 operator==(const sc_unsigned& u, const sc_int_base& v)
680 { return operator==(u, (int64) v); }
681 bool
operator ==(const sc_int_base & u,const sc_unsigned & v)682 operator==(const sc_int_base& u, const sc_unsigned& v)
683 { return operator==((int64) u, v); }
684
685 bool
operator !=(const sc_unsigned & u,const sc_int_base & v)686 operator!=(const sc_unsigned& u, const sc_int_base& v)
687 { return operator!=(u, (int64) v); }
688 bool
operator !=(const sc_int_base & u,const sc_unsigned & v)689 operator!=(const sc_int_base& u, const sc_unsigned& v)
690 { return operator!=((int64) u, v); }
691
692 bool
operator <(const sc_unsigned & u,const sc_int_base & v)693 operator<(const sc_unsigned& u, const sc_int_base& v)
694 { return operator<(u, (int64) v); }
695 bool
operator <(const sc_int_base & u,const sc_unsigned & v)696 operator<(const sc_int_base& u, const sc_unsigned& v)
697 { return operator<((int64) u, v); }
698
699 bool
operator <=(const sc_unsigned & u,const sc_int_base & v)700 operator<=(const sc_unsigned& u, const sc_int_base& v)
701 { return operator<=(u, (int64) v); }
702 bool
operator <=(const sc_int_base & u,const sc_unsigned & v)703 operator<=(const sc_int_base& u, const sc_unsigned& v)
704 { return operator<=((int64) u, v); }
705
706 bool
operator >(const sc_unsigned & u,const sc_int_base & v)707 operator>(const sc_unsigned& u, const sc_int_base& v)
708 { return operator>(u, (int64) v); }
709 bool
operator >(const sc_int_base & u,const sc_unsigned & v)710 operator>(const sc_int_base& u, const sc_unsigned& v)
711 { return operator>((int64) u, v); }
712
713 bool
operator >=(const sc_unsigned & u,const sc_int_base & v)714 operator>=(const sc_unsigned& u, const sc_int_base& v)
715 { return operator>=(u, (int64) v); }
716 bool
operator >=(const sc_int_base & u,const sc_unsigned & v)717 operator>=(const sc_int_base& u, const sc_unsigned& v)
718 { return operator>=((int64) u, v); }
719
720
721 // ----------------------------------------------------------------------------
722 // SECTION: Interfacing with sc_uint_base
723 // ----------------------------------------------------------------------------
724
725 const sc_unsigned&
operator =(const sc_uint_base & v)726 sc_unsigned::operator= (const sc_uint_base& v)
727 { return operator=((uint64) v); }
728
729 sc_unsigned
operator +(const sc_unsigned & u,const sc_uint_base & v)730 operator+(const sc_unsigned& u, const sc_uint_base& v)
731 { return operator+(u, (uint64) v); }
732 sc_unsigned
operator +(const sc_uint_base & u,const sc_unsigned & v)733 operator+(const sc_uint_base& u, const sc_unsigned& v)
734 { return operator+((uint64) u, v); }
735 const sc_unsigned&
operator +=(const sc_uint_base & v)736 sc_unsigned::operator+=(const sc_uint_base& v)
737 { return operator+=((uint64) v); }
738
739 const sc_unsigned&
operator -=(const sc_uint_base & v)740 sc_unsigned::operator-=(const sc_uint_base& v)
741 { return operator-=((uint64) v); }
742
743 sc_unsigned
operator *(const sc_unsigned & u,const sc_uint_base & v)744 operator*(const sc_unsigned& u, const sc_uint_base& v)
745 { return operator*(u, (uint64) v); }
746 sc_unsigned
operator *(const sc_uint_base & u,const sc_unsigned & v)747 operator*(const sc_uint_base& u, const sc_unsigned& v)
748 { return operator*((uint64) u, v); }
749 const sc_unsigned&
operator *=(const sc_uint_base & v)750 sc_unsigned::operator*=(const sc_uint_base& v)
751 { return operator*=((uint64) v); }
752
753 sc_unsigned
operator /(const sc_unsigned & u,const sc_uint_base & v)754 operator/(const sc_unsigned& u, const sc_uint_base& v)
755 { return operator/(u, (uint64) v); }
756 sc_unsigned
operator /(const sc_uint_base & u,const sc_unsigned & v)757 operator/(const sc_uint_base& u, const sc_unsigned& v)
758 { return operator/((uint64) u, v); }
759 const sc_unsigned&
operator /=(const sc_uint_base & v)760 sc_unsigned::operator/=(const sc_uint_base& v)
761 { return operator/=((uint64) v); }
762
763 sc_unsigned
operator %(const sc_unsigned & u,const sc_uint_base & v)764 operator%(const sc_unsigned& u, const sc_uint_base& v)
765 { return operator%(u, (uint64) v); }
766 sc_unsigned
operator %(const sc_uint_base & u,const sc_unsigned & v)767 operator%(const sc_uint_base& u, const sc_unsigned& v)
768 { return operator%((uint64) u, v); }
769 const sc_unsigned&
operator %=(const sc_uint_base & v)770 sc_unsigned::operator%=(const sc_uint_base& v)
771 { return operator%=((uint64) v); }
772
773 sc_unsigned
operator &(const sc_unsigned & u,const sc_uint_base & v)774 operator&(const sc_unsigned& u, const sc_uint_base& v)
775 { return operator&(u, (uint64) v); }
776 sc_unsigned
operator &(const sc_uint_base & u,const sc_unsigned & v)777 operator&(const sc_uint_base& u, const sc_unsigned& v)
778 { return operator&((uint64) u, v); }
779 const sc_unsigned&
operator &=(const sc_uint_base & v)780 sc_unsigned::operator&=(const sc_uint_base& v)
781 { return operator&=((uint64) v); }
782
783 sc_unsigned
operator |(const sc_unsigned & u,const sc_uint_base & v)784 operator|(const sc_unsigned& u, const sc_uint_base& v)
785 { return operator|(u, (uint64) v); }
786 sc_unsigned
operator |(const sc_uint_base & u,const sc_unsigned & v)787 operator|(const sc_uint_base& u, const sc_unsigned& v)
788 { return operator|((uint64) u, v); }
789 const sc_unsigned&
operator |=(const sc_uint_base & v)790 sc_unsigned::operator|=(const sc_uint_base& v)
791 { return operator|=((uint64) v); }
792
793 sc_unsigned
operator ^(const sc_unsigned & u,const sc_uint_base & v)794 operator^(const sc_unsigned& u, const sc_uint_base& v)
795 { return operator^(u, (uint64) v); }
796 sc_unsigned
operator ^(const sc_uint_base & u,const sc_unsigned & v)797 operator^(const sc_uint_base& u, const sc_unsigned& v)
798 { return operator^((uint64) u, v); }
799 const sc_unsigned&
operator ^=(const sc_uint_base & v)800 sc_unsigned::operator^=(const sc_uint_base& v)
801 { return operator^=((uint64) v); }
802
803 sc_unsigned
operator <<(const sc_unsigned & u,const sc_uint_base & v)804 operator<<(const sc_unsigned& u, const sc_uint_base& v)
805 { return operator<<(u, (uint64) v); }
806 const sc_unsigned&
operator <<=(const sc_uint_base & v)807 sc_unsigned::operator<<=(const sc_uint_base& v)
808 { return operator<<=((uint64) v); }
809
810 sc_unsigned
operator >>(const sc_unsigned & u,const sc_uint_base & v)811 operator>>(const sc_unsigned& u, const sc_uint_base& v)
812 { return operator>>(u, (uint64) v); }
813 const sc_unsigned&
operator >>=(const sc_uint_base & v)814 sc_unsigned::operator>>=(const sc_uint_base& v)
815 { return operator>>=((uint64) v); }
816
817 bool
operator ==(const sc_unsigned & u,const sc_uint_base & v)818 operator==(const sc_unsigned& u, const sc_uint_base& v)
819 { return operator==(u, (uint64) v); }
820 bool
operator ==(const sc_uint_base & u,const sc_unsigned & v)821 operator==(const sc_uint_base& u, const sc_unsigned& v)
822 { return operator==((uint64) u, v); }
823
824 bool
operator !=(const sc_unsigned & u,const sc_uint_base & v)825 operator!=(const sc_unsigned& u, const sc_uint_base& v)
826 { return operator!=(u, (uint64) v); }
827 bool
operator !=(const sc_uint_base & u,const sc_unsigned & v)828 operator!=(const sc_uint_base& u, const sc_unsigned& v)
829 { return operator!=((uint64) u, v); }
830
831 bool
operator <(const sc_unsigned & u,const sc_uint_base & v)832 operator<(const sc_unsigned& u, const sc_uint_base& v)
833 { return operator<(u, (uint64) v); }
834 bool
operator <(const sc_uint_base & u,const sc_unsigned & v)835 operator<(const sc_uint_base& u, const sc_unsigned& v)
836 { return operator<((uint64) u, v); }
837
838 bool
operator <=(const sc_unsigned & u,const sc_uint_base & v)839 operator<=(const sc_unsigned& u, const sc_uint_base& v)
840 { return operator<=(u, (uint64) v); }
841 bool
operator <=(const sc_uint_base & u,const sc_unsigned & v)842 operator<=(const sc_uint_base& u, const sc_unsigned& v)
843 { return operator<=((uint64) u, v); }
844
845 bool
operator >(const sc_unsigned & u,const sc_uint_base & v)846 operator>(const sc_unsigned& u, const sc_uint_base& v)
847 { return operator>(u, (uint64) v); }
848 bool
operator >(const sc_uint_base & u,const sc_unsigned & v)849 operator>(const sc_uint_base& u, const sc_unsigned& v)
850 { return operator>((uint64) u, v); }
851
852 bool
operator >=(const sc_unsigned & u,const sc_uint_base & v)853 operator>=(const sc_unsigned& u, const sc_uint_base& v)
854 { return operator>=(u, (uint64) v); }
855 bool
operator >=(const sc_uint_base & u,const sc_unsigned & v)856 operator>=(const sc_uint_base& u, const sc_unsigned& v)
857 { return operator>=((uint64) u, v); }
858
859
860 // ----------------------------------------------------------------------------
861 // SECTION: Input and output operators
862 // ----------------------------------------------------------------------------
863
864 // The operators in this section are included from sc_nbcommon.cpp.
865
866
867 // ----------------------------------------------------------------------------
868 // SECTION: Operator macros.
869 // ----------------------------------------------------------------------------
870
871 #define CONVERT_LONG(u) \
872 small_type u ## s = get_sign(u); \
873 sc_digit u ## d[DIGITS_PER_ULONG]; \
874 from_uint(DIGITS_PER_ULONG, u ## d, (unsigned long) u);
875
876 #define CONVERT_LONG_2(u) \
877 sc_digit u ## d[DIGITS_PER_ULONG]; \
878 from_uint(DIGITS_PER_ULONG, u ## d, (unsigned long) u);
879
880 #define CONVERT_INT(u) \
881 small_type u ## s = get_sign(u); \
882 sc_digit u ## d[DIGITS_PER_UINT]; \
883 from_uint(DIGITS_PER_UINT, u ## d, (unsigned int) u);
884
885 #define CONVERT_INT_2(u) \
886 sc_digit u ## d[DIGITS_PER_UINT]; \
887 from_uint(DIGITS_PER_UINT, u ## d, (unsigned int) u);
888
889 #define CONVERT_INT64(u) \
890 small_type u ## s = get_sign(u); \
891 sc_digit u ## d[DIGITS_PER_UINT64]; \
892 from_uint(DIGITS_PER_UINT64, u ## d, (uint64) u);
893
894 #define CONVERT_INT64_2(u) \
895 sc_digit u ## d[DIGITS_PER_UINT64]; \
896 from_uint(DIGITS_PER_UINT64, u ## d, (uint64) u);
897
898
899 // ----------------------------------------------------------------------------
900 // SECTION: PLUS operators: +, +=, ++
901 // ----------------------------------------------------------------------------
902
903 // Cases to consider when computing u + v:
904 // 1. 0 + v = v
905 // 2. u + 0 = u
906 // 3. if sgn(u) == sgn(v)
907 // 3.1 u + v = +(u + v) = sgn(u) * (u + v)
908 // 3.2 (-u) + (-v) = -(u + v) = sgn(u) * (u + v)
909 // 4. if sgn(u) != sgn(v)
910 // 4.1 u + (-v) = u - v = sgn(u) * (u - v)
911 // 4.2 (-u) + v = -(u - v) ==> sgn(u) * (u - v)
912 //
913 // Specialization of above cases for computing ++u or u++:
914 // 1. 0 + 1 = 1
915 // 3. u + 1 = u + 1 = sgn(u) * (u + 1)
916 // 4. (-u) + 1 = -(u - 1) = sgn(u) * (u - 1)
917
918 sc_unsigned
operator +(const sc_unsigned & u,const sc_unsigned & v)919 operator+(const sc_unsigned& u, const sc_unsigned& v)
920 {
921
922 if (u.sgn == SC_ZERO) // case 1
923 return sc_unsigned(v);
924
925 if (v.sgn == SC_ZERO) // case 2
926 return sc_unsigned(u);
927
928 // cases 3 and 4
929 return add_unsigned_friend(u.sgn, u.nbits, u.ndigits, u.digit,
930 v.sgn, v.nbits, v.ndigits, v.digit);
931
932 }
933
934
935 sc_unsigned
operator +(const sc_unsigned & u,uint64 v)936 operator+(const sc_unsigned &u, uint64 v)
937 {
938
939 if (v == 0) // case 2
940 return sc_unsigned(u);
941
942 CONVERT_INT64(v);
943
944 if (u.sgn == SC_ZERO) // case 1
945 return sc_unsigned(vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd, false);
946
947 // cases 3 and 4
948 return add_unsigned_friend(u.sgn, u.nbits, u.ndigits, u.digit,
949 vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd);
950
951 }
952
953
954 sc_unsigned
operator +(uint64 u,const sc_unsigned & v)955 operator+(uint64 u, const sc_unsigned &v)
956 {
957
958 if (u == 0) // case 1
959 return sc_unsigned(v);
960
961 CONVERT_INT64(u);
962
963 if (v.sgn == SC_ZERO) // case 2
964 return sc_unsigned(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud, false);
965
966 // cases 3 and 4
967
968 return add_unsigned_friend(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud,
969 v.sgn, v.nbits, v.ndigits, v.digit);
970
971 }
972
973
974 sc_unsigned
operator +(const sc_unsigned & u,unsigned long v)975 operator+(const sc_unsigned &u, unsigned long v)
976 {
977
978 if (v == 0) // case 2
979 return sc_unsigned(u);
980
981 CONVERT_LONG(v);
982
983 if (u.sgn == SC_ZERO) // case 1
984 return sc_unsigned(vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd, false);
985
986 // cases 3 and 4
987 return add_unsigned_friend(u.sgn, u.nbits, u.ndigits, u.digit,
988 vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd);
989
990 }
991
992
993 sc_unsigned
operator +(unsigned long u,const sc_unsigned & v)994 operator+(unsigned long u, const sc_unsigned &v)
995 {
996
997 if (u == 0) // case 1
998 return sc_unsigned(v);
999
1000 CONVERT_LONG(u);
1001
1002 if (v.sgn == SC_ZERO) // case 2
1003 return sc_unsigned(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud, false);
1004
1005 // cases 3 and 4
1006 return add_unsigned_friend(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud,
1007 v.sgn, v.nbits, v.ndigits, v.digit);
1008
1009 }
1010
1011 // The rest of the operators in this section are included from
1012 // sc_nbcommon.cpp.
1013
1014
1015 // ----------------------------------------------------------------------------
1016 // SECTION: MINUS operators: -, -=, --
1017 // ----------------------------------------------------------------------------
1018
1019 // Cases to consider when computing u + v:
1020 // 1. u - 0 = u
1021 // 2. 0 - v = -v
1022 // 3. if sgn(u) != sgn(v)
1023 // 3.1 u - (-v) = u + v = sgn(u) * (u + v)
1024 // 3.2 (-u) - v = -(u + v) ==> sgn(u) * (u + v)
1025 // 4. if sgn(u) == sgn(v)
1026 // 4.1 u - v = +(u - v) = sgn(u) * (u - v)
1027 // 4.2 (-u) - (-v) = -(u - v) = sgn(u) * (u - v)
1028 //
1029 // Specialization of above cases for computing --u or u--:
1030 // 1. 0 - 1 = -1
1031 // 3. (-u) - 1 = -(u + 1) = sgn(u) * (u + 1)
1032 // 4. u - 1 = u - 1 = sgn(u) * (u - 1)
1033
1034 // The operators in this section are included from sc_nbcommon.cpp.
1035
1036
1037 // ----------------------------------------------------------------------------
1038 // SECTION: MULTIPLICATION operators: *, *=
1039 // ----------------------------------------------------------------------------
1040
1041 // Cases to consider when computing u * v:
1042 // 1. u * 0 = 0 * v = 0
1043 // 2. 1 * v = v and -1 * v = -v
1044 // 3. u * 1 = u and u * -1 = -u
1045 // 4. u * v = u * v
1046
1047 sc_unsigned
operator *(const sc_unsigned & u,const sc_unsigned & v)1048 operator*(const sc_unsigned& u, const sc_unsigned& v)
1049 {
1050
1051 small_type s = mul_signs(u.sgn, v.sgn);
1052
1053 if (s == SC_ZERO) // case 1
1054 return sc_unsigned();
1055
1056 // cases 2-4
1057 return mul_unsigned_friend(s, u.nbits, u.ndigits, u.digit,
1058 v.nbits, v.ndigits, v.digit);
1059
1060 }
1061
1062
1063 sc_unsigned
operator *(const sc_unsigned & u,uint64 v)1064 operator*(const sc_unsigned& u, uint64 v)
1065 {
1066
1067 small_type s = mul_signs(u.sgn, get_sign(v));
1068
1069 if (s == SC_ZERO) // case 1
1070 return sc_unsigned();
1071
1072 CONVERT_INT64_2(v);
1073
1074 // cases 2-4
1075 return mul_unsigned_friend(s, u.nbits, u.ndigits, u.digit,
1076 BITS_PER_UINT64, DIGITS_PER_UINT64, vd);
1077
1078 }
1079
1080
1081 sc_unsigned
operator *(uint64 u,const sc_unsigned & v)1082 operator*(uint64 u, const sc_unsigned& v)
1083 {
1084
1085 small_type s = mul_signs(v.sgn, get_sign(u));
1086
1087 if (s == SC_ZERO) // case 1
1088 return sc_unsigned();
1089
1090 CONVERT_INT64_2(u);
1091
1092 // cases 2-4
1093 return mul_unsigned_friend(s, BITS_PER_UINT64, DIGITS_PER_UINT64, ud,
1094 v.nbits, v.ndigits, v.digit);
1095
1096 }
1097
1098
1099 sc_unsigned
operator *(const sc_unsigned & u,unsigned long v)1100 operator*(const sc_unsigned& u, unsigned long v)
1101 {
1102
1103 small_type s = mul_signs(u.sgn, get_sign(v));
1104
1105 if (s == SC_ZERO) // case 1
1106 return sc_unsigned();
1107
1108 CONVERT_LONG_2(v);
1109
1110 // else cases 2-4
1111 return mul_unsigned_friend(s, u.nbits, u.ndigits, u.digit,
1112 BITS_PER_ULONG, DIGITS_PER_ULONG, vd);
1113
1114 }
1115
1116 sc_unsigned
operator *(unsigned long u,const sc_unsigned & v)1117 operator*(unsigned long u, const sc_unsigned& v)
1118 {
1119
1120 small_type s = mul_signs(v.sgn, get_sign(u));
1121
1122 if (s == SC_ZERO) // case 1
1123 return sc_unsigned();
1124
1125 CONVERT_LONG_2(u);
1126
1127 // cases 2-4
1128 return mul_unsigned_friend(s, BITS_PER_ULONG, DIGITS_PER_ULONG, ud,
1129 v.nbits, v.ndigits, v.digit);
1130
1131 }
1132
1133 // The rest of the operators in this section are included from
1134 // sc_nbcommon.cpp.
1135
1136
1137 // ----------------------------------------------------------------------------
1138 // SECTION: DIVISION operators: /, /=
1139 // ----------------------------------------------------------------------------
1140
1141 // Cases to consider when finding the quotient q = floor(u/v):
1142 // Note that u = q * v + r for r < q.
1143 // 1. 0 / 0 or u / 0 => error
1144 // 2. 0 / v => 0 = 0 * v + 0
1145 // 3. u / v && u = v => u = 1 * u + 0 - u or v can be 1 or -1
1146 // 4. u / v && u < v => u = 0 * v + u - u can be 1 or -1
1147 // 5. u / v && u > v => u = q * v + r - v can be 1 or -1
1148
1149 sc_unsigned
operator /(const sc_unsigned & u,const sc_unsigned & v)1150 operator/(const sc_unsigned& u, const sc_unsigned& v)
1151 {
1152
1153 small_type s = mul_signs(u.sgn, v.sgn);
1154
1155 if (s == SC_ZERO) {
1156 div_by_zero(v.sgn); // case 1
1157 return sc_unsigned(); // case 2
1158 }
1159
1160 // other cases
1161 return div_unsigned_friend(s, u.nbits, u.ndigits, u.digit,
1162 v.nbits, v.ndigits, v.digit);
1163
1164 }
1165
1166
1167 sc_unsigned
operator /(const sc_unsigned & u,uint64 v)1168 operator/(const sc_unsigned& u, uint64 v)
1169 {
1170
1171 small_type s = mul_signs(u.sgn, get_sign(v));
1172
1173 if (s == SC_ZERO) {
1174 div_by_zero(v); // case 1
1175 return sc_unsigned(); // case 2
1176 }
1177
1178 CONVERT_INT64_2(v);
1179
1180 // other cases
1181 return div_unsigned_friend(s, u.nbits, u.ndigits, u.digit,
1182 BITS_PER_UINT64, DIGITS_PER_UINT64, vd);
1183
1184 }
1185
1186
1187 sc_unsigned
operator /(uint64 u,const sc_unsigned & v)1188 operator/(uint64 u, const sc_unsigned& v)
1189 {
1190
1191 small_type s = mul_signs(v.sgn, get_sign(u));
1192
1193 if (s == SC_ZERO) {
1194 div_by_zero(v.sgn); // case 1
1195 return sc_unsigned(); // case 2
1196
1197 }
1198
1199 CONVERT_INT64_2(u);
1200
1201 // other cases
1202 return div_unsigned_friend(s, BITS_PER_UINT64, DIGITS_PER_UINT64, ud,
1203 v.nbits, v.ndigits, v.digit);
1204
1205 }
1206
1207
1208 sc_unsigned
operator /(const sc_unsigned & u,unsigned long v)1209 operator/(const sc_unsigned& u, unsigned long v)
1210 {
1211
1212 small_type s = mul_signs(u.sgn, get_sign(v));
1213
1214 if (s == SC_ZERO) {
1215 div_by_zero(v); // case 1
1216 return sc_unsigned(); // case 2
1217 }
1218
1219 CONVERT_LONG_2(v);
1220
1221 // other cases
1222 return div_unsigned_friend(s, u.nbits, u.ndigits, u.digit,
1223 BITS_PER_ULONG, DIGITS_PER_ULONG, vd);
1224
1225 }
1226
1227
1228 sc_unsigned
operator /(unsigned long u,const sc_unsigned & v)1229 operator/(unsigned long u, const sc_unsigned& v)
1230 {
1231
1232 small_type s = mul_signs(v.sgn, get_sign(u));
1233
1234 if (s == SC_ZERO) {
1235 div_by_zero(v.sgn); // case 1
1236 return sc_unsigned(); // case 2
1237
1238 }
1239
1240 CONVERT_LONG_2(u);
1241
1242 // other cases
1243 return div_unsigned_friend(s, BITS_PER_ULONG, DIGITS_PER_ULONG, ud,
1244 v.nbits, v.ndigits, v.digit);
1245
1246 }
1247
1248 // The rest of the operators in this section are included from
1249 // sc_nbcommon.cpp.
1250
1251
1252 // ----------------------------------------------------------------------------
1253 // SECTION: MOD operators: %, %=.
1254 // ----------------------------------------------------------------------------
1255
1256 // Cases to consider when finding the remainder r = u % v:
1257 // Note that u = q * v + r for r < q.
1258 // 1. 0 % 0 or u % 0 => error
1259 // 2. 0 % v => 0 = 0 * v + 0
1260 // 3. u % v && u = v => u = 1 * u + 0 - u or v can be 1 or -1
1261 // 4. u % v && u < v => u = 0 * v + u - u can be 1 or -1
1262 // 5. u % v && u > v => u = q * v + r - v can be 1 or -1
1263
1264 sc_unsigned
operator %(const sc_unsigned & u,const sc_unsigned & v)1265 operator%(const sc_unsigned& u, const sc_unsigned& v)
1266 {
1267
1268 if ((u.sgn == SC_ZERO) || (v.sgn == SC_ZERO)) {
1269 div_by_zero(v.sgn); // case 1
1270 return sc_unsigned(); // case 2
1271 }
1272
1273 // other cases
1274 return mod_unsigned_friend(u.sgn, u.nbits, u.ndigits, u.digit,
1275 v.nbits, v.ndigits, v.digit);
1276 }
1277
1278
1279 sc_unsigned
operator %(const sc_unsigned & u,uint64 v)1280 operator%(const sc_unsigned& u, uint64 v)
1281 {
1282
1283 if ((u.sgn == SC_ZERO) || (v == 0)) {
1284 div_by_zero(v); // case 1
1285 return sc_unsigned(); // case 2
1286 }
1287
1288 CONVERT_INT64_2(v);
1289
1290 // other cases
1291 return mod_unsigned_friend(u.sgn, u.nbits, u.ndigits, u.digit,
1292 BITS_PER_UINT64, DIGITS_PER_UINT64, vd);
1293
1294 }
1295
1296
1297 sc_unsigned
operator %(uint64 u,const sc_unsigned & v)1298 operator%(uint64 u, const sc_unsigned& v)
1299 {
1300
1301 if ((u == 0) || (v.sgn == SC_ZERO)) {
1302 div_by_zero(v.sgn); // case 1
1303 return sc_unsigned(); // case 2
1304 }
1305
1306 CONVERT_INT64(u);
1307
1308 // other cases
1309 return mod_unsigned_friend(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud,
1310 v.nbits, v.ndigits, v.digit);
1311
1312 }
1313
1314
1315 sc_unsigned
operator %(const sc_unsigned & u,unsigned long v)1316 operator%(const sc_unsigned& u, unsigned long v)
1317 {
1318
1319 if ((u.sgn == SC_ZERO) || (v == 0)) {
1320 div_by_zero(v); // case 1
1321 return sc_unsigned(); // case 2
1322 }
1323
1324 CONVERT_LONG_2(v);
1325
1326 // other cases
1327 return mod_unsigned_friend(u.sgn, u.nbits, u.ndigits, u.digit,
1328 BITS_PER_ULONG, DIGITS_PER_ULONG, vd);
1329
1330 }
1331
1332
1333 sc_unsigned
operator %(unsigned long u,const sc_unsigned & v)1334 operator%(unsigned long u, const sc_unsigned& v)
1335 {
1336
1337 if ((u == 0) || (v.sgn == SC_ZERO)) {
1338 div_by_zero(v.sgn); // case 1
1339 return sc_unsigned(); // case 2
1340 }
1341
1342 CONVERT_LONG(u);
1343
1344 // other cases
1345 return mod_unsigned_friend(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud,
1346 v.nbits, v.ndigits, v.digit);
1347
1348 }
1349
1350 // The rest of the operators in this section are included from
1351 // sc_nbcommon.cpp.
1352
1353
1354 // ----------------------------------------------------------------------------
1355 // SECTION: Bitwise AND operators: &, &=
1356 // ----------------------------------------------------------------------------
1357
1358 // Cases to consider when computing u & v:
1359 // 1. u & 0 = 0 & v = 0
1360 // 2. u & v => sgn = +
1361 // 3. (-u) & (-v) => sgn = -
1362 // 4. u & (-v) => sgn = +
1363 // 5. (-u) & v => sgn = +
1364
1365 sc_unsigned
operator &(const sc_unsigned & u,const sc_unsigned & v)1366 operator&(const sc_unsigned& u, const sc_unsigned& v)
1367 {
1368
1369 if ((u.sgn == SC_ZERO) || (v.sgn == SC_ZERO)) // case 1
1370 return sc_unsigned();
1371
1372 // other cases
1373 return and_unsigned_friend(u.sgn, u.nbits, u.ndigits, u.digit,
1374 v.sgn, v.nbits, v.ndigits, v.digit);
1375
1376 }
1377
1378
1379 sc_unsigned
operator &(const sc_unsigned & u,uint64 v)1380 operator&(const sc_unsigned& u, uint64 v)
1381 {
1382
1383 if ((u.sgn == SC_ZERO) || (v == 0)) // case 1
1384 return sc_unsigned();
1385
1386 CONVERT_INT64(v);
1387
1388 // other cases
1389 return and_unsigned_friend(u.sgn, u.nbits, u.ndigits, u.digit,
1390 vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd);
1391
1392 }
1393
1394
1395 sc_unsigned
operator &(uint64 u,const sc_unsigned & v)1396 operator&(uint64 u, const sc_unsigned& v)
1397 {
1398
1399 if ((u == 0) || (v.sgn == SC_ZERO)) // case 1
1400 return sc_unsigned();
1401
1402 CONVERT_INT64(u);
1403
1404 // other cases
1405 return and_unsigned_friend(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud,
1406 v.sgn, v.nbits, v.ndigits, v.digit);
1407
1408 }
1409
1410
1411 sc_unsigned
operator &(const sc_unsigned & u,unsigned long v)1412 operator&(const sc_unsigned& u, unsigned long v)
1413 {
1414
1415 if ((u.sgn == SC_ZERO) || (v == 0)) // case 1
1416 return sc_unsigned();
1417
1418 CONVERT_LONG(v);
1419
1420 // other cases
1421 return and_unsigned_friend(u.sgn, u.nbits, u.ndigits, u.digit,
1422 vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd);
1423
1424 }
1425
1426
1427 sc_unsigned
operator &(unsigned long u,const sc_unsigned & v)1428 operator&(unsigned long u, const sc_unsigned& v)
1429 {
1430
1431 if ((u == 0) || (v.sgn == SC_ZERO)) // case 1
1432 return sc_unsigned();
1433
1434 CONVERT_LONG(u);
1435
1436 // other cases
1437 return and_unsigned_friend(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud,
1438 v.sgn, v.nbits, v.ndigits, v.digit);
1439
1440 }
1441
1442 // The rest of the operators in this section are included from
1443 // sc_nbcommon.cpp.
1444
1445
1446 // ----------------------------------------------------------------------------
1447 // SECTION: Bitwise OR operators: |, |=
1448 // ----------------------------------------------------------------------------
1449
1450 // Cases to consider when computing u | v:
1451 // 1. u | 0 = u
1452 // 2. 0 | v = v
1453 // 3. u | v => sgn = +
1454 // 4. (-u) | (-v) => sgn = -
1455 // 5. u | (-v) => sgn = -
1456 // 6. (-u) | v => sgn = -
1457
1458 sc_unsigned
operator |(const sc_unsigned & u,const sc_unsigned & v)1459 operator|(const sc_unsigned& u, const sc_unsigned& v)
1460 {
1461
1462 if (v.sgn == SC_ZERO) // case 1
1463 return sc_unsigned(u);
1464
1465 if (u.sgn == SC_ZERO) // case 2
1466 return sc_unsigned(v);
1467
1468 // other cases
1469 return or_unsigned_friend(u.sgn, u.nbits, u.ndigits, u.digit,
1470 v.sgn, v.nbits, v.ndigits, v.digit);
1471
1472 }
1473
1474
1475 sc_unsigned
operator |(const sc_unsigned & u,uint64 v)1476 operator|(const sc_unsigned& u, uint64 v)
1477 {
1478
1479 if (v == 0) // case 1
1480 return sc_unsigned(u);
1481
1482 CONVERT_INT64(v);
1483
1484 if (u.sgn == SC_ZERO) // case 2
1485 return sc_unsigned(vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd, false);
1486
1487 // other cases
1488 return or_unsigned_friend(u.sgn, u.nbits, u.ndigits, u.digit,
1489 vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd);
1490
1491 }
1492
1493
1494 sc_unsigned
operator |(uint64 u,const sc_unsigned & v)1495 operator|(uint64 u, const sc_unsigned& v)
1496 {
1497
1498 if (u == 0)
1499 return sc_unsigned(v);
1500
1501 CONVERT_INT64(u);
1502
1503 if (v.sgn == SC_ZERO)
1504 return sc_unsigned(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud, false);
1505
1506 // other cases
1507 return or_unsigned_friend(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud,
1508 v.sgn, v.nbits, v.ndigits, v.digit);
1509
1510 }
1511
1512
1513 sc_unsigned
operator |(const sc_unsigned & u,unsigned long v)1514 operator|(const sc_unsigned& u, unsigned long v)
1515 {
1516
1517 if (v == 0) // case 1
1518 return sc_unsigned(u);
1519
1520 CONVERT_LONG(v);
1521
1522 if (u.sgn == SC_ZERO) // case 2
1523 return sc_unsigned(vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd, false);
1524
1525 // other cases
1526 return or_unsigned_friend(u.sgn, u.nbits, u.ndigits, u.digit,
1527 vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd);
1528
1529 }
1530
1531
1532 sc_unsigned
operator |(unsigned long u,const sc_unsigned & v)1533 operator|(unsigned long u, const sc_unsigned& v)
1534 {
1535
1536 if (u == 0)
1537 return sc_unsigned(v);
1538
1539 CONVERT_LONG(u);
1540
1541 if (v.sgn == SC_ZERO)
1542 return sc_unsigned(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud, false);
1543
1544 // other cases
1545 return or_unsigned_friend(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud,
1546 v.sgn, v.nbits, v.ndigits, v.digit);
1547
1548 }
1549
1550 // The rest of the operators in this section are included from
1551 // sc_nbcommon.cpp.
1552
1553
1554 // ----------------------------------------------------------------------------
1555 // SECTION: Bitwise XOR operators: ^, ^=
1556 // ----------------------------------------------------------------------------
1557
1558 // Cases to consider when computing u ^ v:
1559 // Note that u ^ v = (~u & v) | (u & ~v).
1560 // 1. u ^ 0 = u
1561 // 2. 0 ^ v = v
1562 // 3. u ^ v => sgn = +
1563 // 4. (-u) ^ (-v) => sgn = -
1564 // 5. u ^ (-v) => sgn = -
1565 // 6. (-u) ^ v => sgn = +
1566
1567 sc_unsigned
operator ^(const sc_unsigned & u,const sc_unsigned & v)1568 operator^(const sc_unsigned& u, const sc_unsigned& v)
1569 {
1570
1571 if (v.sgn == SC_ZERO) // case 1
1572 return sc_unsigned(u);
1573
1574 if (u.sgn == SC_ZERO) // case 2
1575 return sc_unsigned(v);
1576
1577 // other cases
1578 return xor_unsigned_friend(u.sgn, u.nbits, u.ndigits, u.digit,
1579 v.sgn, v.nbits, v.ndigits, v.digit);
1580
1581 }
1582
1583
1584 sc_unsigned
operator ^(const sc_unsigned & u,uint64 v)1585 operator^(const sc_unsigned& u, uint64 v)
1586 {
1587
1588 if (v == 0) // case 1
1589 return sc_unsigned(u);
1590
1591 CONVERT_INT64(v);
1592
1593 if (u.sgn == SC_ZERO) // case 2
1594 return sc_unsigned(vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd, false);
1595
1596 // other cases
1597 return xor_unsigned_friend(u.sgn, u.nbits, u.ndigits, u.digit,
1598 vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd);
1599
1600 }
1601
1602 sc_unsigned
operator ^(uint64 u,const sc_unsigned & v)1603 operator^(uint64 u, const sc_unsigned& v)
1604 {
1605 if (u == 0)
1606 return sc_unsigned(v);
1607
1608 CONVERT_INT64(u);
1609
1610 if (v.sgn == SC_ZERO)
1611 return sc_unsigned(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud, false);
1612
1613 // other cases
1614 return xor_unsigned_friend(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud,
1615 v.sgn, v.nbits, v.ndigits, v.digit);
1616
1617 }
1618
1619
1620 sc_unsigned
operator ^(const sc_unsigned & u,unsigned long v)1621 operator^(const sc_unsigned& u, unsigned long v)
1622 {
1623
1624 if (v == 0) // case 1
1625 return sc_unsigned(u);
1626
1627 CONVERT_LONG(v);
1628
1629 if (u.sgn == SC_ZERO) // case 2
1630 return sc_unsigned(vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd, false);
1631
1632 // other cases
1633 return xor_unsigned_friend(u.sgn, u.nbits, u.ndigits, u.digit,
1634 vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd);
1635
1636 }
1637
1638 sc_unsigned
operator ^(unsigned long u,const sc_unsigned & v)1639 operator^(unsigned long u, const sc_unsigned& v)
1640 {
1641 if (u == 0)
1642 return sc_unsigned(v);
1643
1644 CONVERT_LONG(u);
1645
1646 if (v.sgn == SC_ZERO)
1647 return sc_unsigned(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud, false);
1648
1649 // other cases
1650 return xor_unsigned_friend(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud,
1651 v.sgn, v.nbits, v.ndigits, v.digit);
1652
1653 }
1654
1655 // The rest of the operators in this section are included from
1656 // sc_nbcommon.cpp.
1657
1658
1659 // ----------------------------------------------------------------------------
1660 // SECTION: Bitwise NOT operator: ~
1661 // ----------------------------------------------------------------------------
1662
1663 // Operators in this section are included from sc_nbcommon.cpp.
1664
1665
1666 // ----------------------------------------------------------------------------
1667 // SECTION: LEFT SHIFT operators: <<, <<=
1668 // ----------------------------------------------------------------------------
1669
1670 sc_unsigned
operator <<(const sc_unsigned & u,const sc_signed & v)1671 operator<<(const sc_unsigned& u, const sc_signed& v)
1672 {
1673 if ((v.sgn == SC_ZERO) || (v.sgn == SC_NEG))
1674 return sc_unsigned(u);
1675
1676 return operator<<(u, v.to_ulong());
1677 }
1678
1679 // The rest of the operators in this section are included from
1680 // sc_nbcommon.cpp.
1681
1682
1683 // ----------------------------------------------------------------------------
1684 // SECTION: RIGHT SHIFT operators: >>, >>=
1685 // ----------------------------------------------------------------------------
1686
1687 sc_unsigned
operator >>(const sc_unsigned & u,const sc_signed & v)1688 operator>>(const sc_unsigned& u, const sc_signed& v)
1689 {
1690
1691 if ((v.sgn == SC_ZERO) || (v.sgn == SC_NEG))
1692 return sc_unsigned(u);
1693
1694 return operator>>(u, v.to_long());
1695
1696 }
1697
1698 // The rest of the operators in this section are included from
1699 // sc_nbcommon.cpp.
1700
1701
1702 // ----------------------------------------------------------------------------
1703 // SECTION: Unary arithmetic operators.
1704 // ----------------------------------------------------------------------------
1705
1706 sc_unsigned
operator +(const sc_unsigned & u)1707 operator+(const sc_unsigned& u)
1708 {
1709 return sc_unsigned(u);
1710 }
1711
1712
1713 // ----------------------------------------------------------------------------
1714 // SECTION: EQUAL operator: ==
1715 // ----------------------------------------------------------------------------
1716
1717 bool
operator ==(const sc_unsigned & u,const sc_unsigned & v)1718 operator==(const sc_unsigned& u, const sc_unsigned& v)
1719 {
1720 if (&u == &v)
1721 return true;
1722 if (compare_unsigned(u.sgn, u.nbits, u.ndigits, u.digit,
1723 v.sgn, v.nbits, v.ndigits, v.digit) != 0)
1724 return false;
1725 return true;
1726 }
1727
1728
1729 bool
operator ==(const sc_unsigned & u,const sc_signed & v)1730 operator==(const sc_unsigned& u, const sc_signed& v)
1731 {
1732 if (v.sgn == SC_NEG)
1733 return false;
1734 if (compare_unsigned(u.sgn, u.nbits, u.ndigits, u.digit,
1735 v.sgn, v.nbits, v.ndigits, v.digit, 0, 1) != 0)
1736 return false;
1737 return true;
1738 }
1739
1740
1741 bool
operator ==(const sc_signed & u,const sc_unsigned & v)1742 operator==(const sc_signed& u, const sc_unsigned& v)
1743 {
1744 if (u.sgn == SC_NEG)
1745 return false;
1746 if (compare_unsigned(u.sgn, u.nbits, u.ndigits, u.digit,
1747 v.sgn, v.nbits, v.ndigits, v.digit, 1, 0) != 0)
1748 return false;
1749 return true;
1750 }
1751
1752
1753 bool
operator ==(const sc_unsigned & u,int64 v)1754 operator==(const sc_unsigned& u, int64 v)
1755 {
1756 if (v < 0)
1757 return false;
1758 CONVERT_INT64(v);
1759 if (compare_unsigned(u.sgn, u.nbits, u.ndigits, u.digit,
1760 vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd) != 0)
1761 return false;
1762 return true;
1763 }
1764
1765
1766 bool
operator ==(int64 u,const sc_unsigned & v)1767 operator==(int64 u, const sc_unsigned& v)
1768 {
1769 if (u < 0)
1770 return false;
1771 CONVERT_INT64(u);
1772 if (compare_unsigned(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud,
1773 v.sgn, v.nbits, v.ndigits, v.digit) != 0)
1774 return false;
1775 return true;
1776 }
1777
1778
1779 bool
operator ==(const sc_unsigned & u,uint64 v)1780 operator==(const sc_unsigned& u, uint64 v)
1781 {
1782 CONVERT_INT64(v);
1783 if (compare_unsigned(u.sgn, u.nbits, u.ndigits, u.digit,
1784 vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd) != 0)
1785 return false;
1786 return true;
1787 }
1788
1789
1790 bool
operator ==(uint64 u,const sc_unsigned & v)1791 operator==(uint64 u, const sc_unsigned& v)
1792 {
1793 CONVERT_INT64(u);
1794 if (compare_unsigned(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud,
1795 v.sgn, v.nbits, v.ndigits, v.digit) != 0)
1796 return false;
1797 return true;
1798 }
1799
1800
1801 bool
operator ==(const sc_unsigned & u,long v)1802 operator==(const sc_unsigned& u, long v)
1803 {
1804 if (v < 0)
1805 return false;
1806 CONVERT_LONG(v);
1807 if (compare_unsigned(u.sgn, u.nbits, u.ndigits, u.digit,
1808 vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd) != 0)
1809 return false;
1810 return true;
1811 }
1812
1813
1814 bool
operator ==(long u,const sc_unsigned & v)1815 operator==(long u, const sc_unsigned& v)
1816 {
1817 if (u < 0)
1818 return false;
1819 CONVERT_LONG(u);
1820 if (compare_unsigned(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud,
1821 v.sgn, v.nbits, v.ndigits, v.digit) != 0)
1822 return false;
1823 return true;
1824 }
1825
1826
1827 bool
operator ==(const sc_unsigned & u,unsigned long v)1828 operator==(const sc_unsigned& u, unsigned long v)
1829 {
1830 CONVERT_LONG(v);
1831 if (compare_unsigned(u.sgn, u.nbits, u.ndigits, u.digit,
1832 vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd) != 0)
1833 return false;
1834 return true;
1835 }
1836
1837
1838 bool
operator ==(unsigned long u,const sc_unsigned & v)1839 operator==(unsigned long u, const sc_unsigned& v)
1840 {
1841 CONVERT_LONG(u);
1842 if (compare_unsigned(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud,
1843 v.sgn, v.nbits, v.ndigits, v.digit) != 0)
1844 return false;
1845 return true;
1846 }
1847
1848
1849 // ----------------------------------------------------------------------------
1850 // SECTION: NOT_EQUAL operator: !=
1851 // ----------------------------------------------------------------------------
1852
1853 bool
operator !=(const sc_unsigned & u,const sc_signed & v)1854 operator!=(const sc_unsigned& u, const sc_signed& v)
1855 {
1856 return (! operator==(u, v));
1857 }
1858
1859
1860 bool
operator !=(const sc_signed & u,const sc_unsigned & v)1861 operator!=(const sc_signed& u, const sc_unsigned& v)
1862 {
1863 return (! operator==(u, v));
1864 }
1865
1866 // The rest of the operators in this section are included from sc_nbcommon.cpp.
1867
1868
1869 // ----------------------------------------------------------------------------
1870 // SECTION: LESS THAN operator: <
1871 // ----------------------------------------------------------------------------
1872
1873 bool
operator <(const sc_unsigned & u,const sc_unsigned & v)1874 operator<(const sc_unsigned& u, const sc_unsigned& v)
1875 {
1876 if (&u == &v)
1877 return false;
1878 if (compare_unsigned(u.sgn, u.nbits, u.ndigits, u.digit,
1879 v.sgn, v.nbits, v.ndigits, v.digit) < 0)
1880 return true;
1881 return false;
1882 }
1883
1884
1885 bool
operator <(const sc_unsigned & u,const sc_signed & v)1886 operator<(const sc_unsigned& u, const sc_signed& v)
1887 {
1888 if (v.sgn == SC_NEG)
1889 return false;
1890 if (compare_unsigned(u.sgn, u.nbits, u.ndigits, u.digit,
1891 v.sgn, v.nbits, v.ndigits, v.digit, 0, 1) < 0)
1892 return true;
1893 return false;
1894 }
1895
1896
1897 bool
operator <(const sc_signed & u,const sc_unsigned & v)1898 operator<(const sc_signed& u, const sc_unsigned& v)
1899 {
1900 if (u.sgn == SC_NEG)
1901 return true;
1902 if (compare_unsigned(u.sgn, u.nbits, u.ndigits, u.digit,
1903 v.sgn, v.nbits, v.ndigits, v.digit, 1, 0) < 0)
1904 return true;
1905 return false;
1906 }
1907
1908
1909 bool
operator <(const sc_unsigned & u,int64 v)1910 operator<(const sc_unsigned& u, int64 v)
1911 {
1912 if (v < 0)
1913 return false;
1914 CONVERT_INT64(v);
1915 if (compare_unsigned(u.sgn, u.nbits, u.ndigits, u.digit,
1916 vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd) < 0)
1917 return true;
1918 return false;
1919 }
1920
1921
1922 bool
operator <(int64 u,const sc_unsigned & v)1923 operator<(int64 u, const sc_unsigned& v)
1924 {
1925 if (u < 0)
1926 return true;
1927 CONVERT_INT64(u);
1928 if (compare_unsigned(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud,
1929 v.sgn, v.nbits, v.ndigits, v.digit) < 0)
1930 return true;
1931 return false;
1932 }
1933
1934
1935 bool
operator <(const sc_unsigned & u,uint64 v)1936 operator<(const sc_unsigned& u, uint64 v)
1937 {
1938 CONVERT_INT64(v);
1939 if (compare_unsigned(u.sgn, u.nbits, u.ndigits, u.digit,
1940 vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd) < 0)
1941 return true;
1942 return false;
1943 }
1944
1945
1946 bool
operator <(uint64 u,const sc_unsigned & v)1947 operator<(uint64 u, const sc_unsigned& v)
1948 {
1949 CONVERT_INT64(u);
1950 if (compare_unsigned(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud,
1951 v.sgn, v.nbits, v.ndigits, v.digit) < 0)
1952 return true;
1953 return false;
1954 }
1955
1956
1957 bool
operator <(const sc_unsigned & u,long v)1958 operator<(const sc_unsigned& u, long v)
1959 {
1960 if (v < 0)
1961 return false;
1962 CONVERT_LONG(v);
1963 if (compare_unsigned(u.sgn, u.nbits, u.ndigits, u.digit,
1964 vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd) < 0)
1965 return true;
1966 return false;
1967 }
1968
1969
1970 bool
operator <(long u,const sc_unsigned & v)1971 operator<(long u, const sc_unsigned& v)
1972 {
1973 if (u < 0)
1974 return true;
1975 CONVERT_LONG(u);
1976 if (compare_unsigned(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud,
1977 v.sgn, v.nbits, v.ndigits, v.digit) < 0)
1978 return true;
1979 return false;
1980 }
1981
1982
1983 bool
operator <(const sc_unsigned & u,unsigned long v)1984 operator<(const sc_unsigned& u, unsigned long v)
1985 {
1986 CONVERT_LONG(v);
1987 if (compare_unsigned(u.sgn, u.nbits, u.ndigits, u.digit,
1988 vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd) < 0)
1989 return true;
1990 return false;
1991 }
1992
1993
1994 bool
operator <(unsigned long u,const sc_unsigned & v)1995 operator<(unsigned long u, const sc_unsigned& v)
1996 {
1997 CONVERT_LONG(u);
1998 if (compare_unsigned(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud,
1999 v.sgn, v.nbits, v.ndigits, v.digit) < 0)
2000 return true;
2001 return false;
2002 }
2003
2004
2005 // ----------------------------------------------------------------------------
2006 // SECTION: LESS THAN or EQUAL operator: <=
2007 // ----------------------------------------------------------------------------
2008
2009 bool
operator <=(const sc_unsigned & u,const sc_signed & v)2010 operator<=(const sc_unsigned& u, const sc_signed& v)
2011 {
2012 return (operator<(u, v) || operator==(u, v));
2013 }
2014
2015
2016 bool
operator <=(const sc_signed & u,const sc_unsigned & v)2017 operator<=(const sc_signed& u, const sc_unsigned& v)
2018 {
2019 return (operator<(u, v) || operator==(u, v));
2020 }
2021
2022 // The rest of the operators in this section are included from sc_nbcommon.cpp.
2023
2024
2025 // ----------------------------------------------------------------------------
2026 // SECTION: GREATER THAN operator: >
2027 // ----------------------------------------------------------------------------
2028
2029 bool
operator >(const sc_unsigned & u,const sc_signed & v)2030 operator>(const sc_unsigned& u, const sc_signed& v)
2031 {
2032 return (! (operator<=(u, v)));
2033 }
2034
2035
2036 bool
operator >(const sc_signed & u,const sc_unsigned & v)2037 operator>(const sc_signed& u, const sc_unsigned& v)
2038 {
2039 return (! (operator<=(u, v)));
2040 }
2041
2042 // The rest of the operators in this section are included from sc_nbcommon.cpp.
2043
2044
2045 // ----------------------------------------------------------------------------
2046 // SECTION: GREATER THAN or EQUAL operator: >=
2047 // ----------------------------------------------------------------------------
2048
2049 bool
operator >=(const sc_unsigned & u,const sc_signed & v)2050 operator>=(const sc_unsigned& u, const sc_signed& v)
2051 {
2052 return (! (operator<(u, v)));
2053 }
2054
2055
2056 bool
operator >=(const sc_signed & u,const sc_unsigned & v)2057 operator>=(const sc_signed& u, const sc_unsigned& v)
2058 {
2059 return (! (operator<(u, v)));
2060 }
2061
2062 // The rest of the operators in this section are included from sc_nbcommon.cpp.
2063
2064
2065 // ----------------------------------------------------------------------------
2066 // SECTION: Friends
2067 // ----------------------------------------------------------------------------
2068
2069 // Compare u and v as unsigned and return r
2070 // r = 0 if u == v
2071 // r < 0 if u < v
2072 // r > 0 if u > v
2073
2074 int
compare_unsigned(small_type us,int unb,int und,const sc_digit * ud,small_type vs,int vnb,int vnd,const sc_digit * vd,small_type if_u_signed,small_type if_v_signed)2075 compare_unsigned(small_type us,
2076 int unb, int und, const sc_digit *ud,
2077 small_type vs,
2078 int vnb, int vnd, const sc_digit *vd,
2079 small_type if_u_signed,
2080 small_type if_v_signed)
2081 {
2082
2083 if (us == vs) {
2084
2085 if (us == SC_ZERO)
2086 return 0;
2087
2088 else {
2089
2090 int cmp_res = vec_skip_and_cmp(und, ud, vnd, vd);
2091
2092 if (us == SC_POS)
2093 return cmp_res;
2094 else
2095 return -cmp_res;
2096
2097 }
2098 }
2099 else {
2100
2101 if (us == SC_ZERO)
2102 return -vs;
2103
2104 if (vs == SC_ZERO)
2105 return us;
2106
2107 int cmp_res;
2108
2109 int nd = (us == SC_NEG ? und : vnd);
2110
2111 #ifdef SC_MAX_NBITS
2112 sc_digit d[MAX_NDIGITS];
2113 #else
2114 sc_digit *d = new sc_digit[nd];
2115 #endif
2116
2117 if (us == SC_NEG) {
2118
2119 vec_copy(nd, d, ud);
2120 vec_complement(nd, d);
2121 trim(if_u_signed, unb, nd, d);
2122 cmp_res = vec_skip_and_cmp(nd, d, vnd, vd);
2123
2124 }
2125 else {
2126
2127 vec_copy(nd, d, vd);
2128 vec_complement(nd, d);
2129 trim(if_v_signed, vnb, nd, d);
2130 cmp_res = vec_skip_and_cmp(und, ud, nd, d);
2131
2132 }
2133
2134 #ifndef SC_MAX_NBITS
2135 delete [] d;
2136 #endif
2137
2138 return cmp_res;
2139
2140 }
2141 }
2142
2143
2144 // ----------------------------------------------------------------------------
2145 // SECTION: Public members - Other utils.
2146 // ----------------------------------------------------------------------------
2147
2148 bool
iszero() const2149 sc_unsigned::iszero() const
2150 {
2151 if (sgn == SC_ZERO)
2152 return true;
2153
2154 else if (sgn == SC_NEG) {
2155
2156 // A negative unsigned number can be zero, e.g., -16 in 4 bits, so
2157 // check that.
2158
2159 #ifdef SC_MAX_NBITS
2160 sc_digit d[MAX_NDIGITS];
2161 #else
2162 sc_digit *d = new sc_digit[ndigits];
2163 #endif
2164
2165 vec_copy(ndigits, d, digit);
2166 vec_complement(ndigits, d);
2167 trim_unsigned(nbits, ndigits, d);
2168
2169 bool res = check_for_zero(ndigits, d);
2170
2171 #ifndef SC_MAX_NBITS
2172 delete [] d;
2173 #endif
2174
2175 return res;
2176
2177 }
2178 else
2179 return false;
2180 }
2181
2182 // The rest of the utils in this section are included from sc_nbcommon.cpp.
2183
2184
2185 // ----------------------------------------------------------------------------
2186 // SECTION: Private members.
2187 // ----------------------------------------------------------------------------
2188
2189 // The private members in this section are included from
2190 // sc_nbcommon.cpp.
2191
2192 #define CLASS_TYPE sc_unsigned
2193 #define CLASS_TYPE_STR "sc_unsigned"
2194
2195 #define ADD_HELPER add_unsigned_friend
2196 #define SUB_HELPER sub_unsigned_friend
2197 #define MUL_HELPER mul_unsigned_friend
2198 #define DIV_HELPER div_unsigned_friend
2199 #define MOD_HELPER mod_unsigned_friend
2200 #define AND_HELPER and_unsigned_friend
2201 #define OR_HELPER or_unsigned_friend
2202 #define XOR_HELPER xor_unsigned_friend
2203
2204 #include "sc_nbfriends.inc"
2205
2206 #undef SC_SIGNED
2207 #define SC_UNSIGNED
2208 #define IF_SC_SIGNED 0 // 0 = sc_unsigned
2209 #define CLASS_TYPE_SUBREF sc_unsigned_subref_r
2210 #define OTHER_CLASS_TYPE sc_signed
2211 #define OTHER_CLASS_TYPE_SUBREF sc_signed_subref_r
2212
2213 #define MUL_ON_HELPER mul_on_help_unsigned
2214 #define DIV_ON_HELPER div_on_help_unsigned
2215 #define MOD_ON_HELPER mod_on_help_unsigned
2216
2217 #include "sc_nbcommon.inc"
2218
2219 #undef MOD_ON_HELPER
2220 #undef DIV_ON_HELPER
2221 #undef MUL_ON_HELPER
2222
2223 #undef OTHER_CLASS_TYPE_SUBREF
2224 #undef OTHER_CLASS_TYPE
2225 #undef CLASS_TYPE_SUBREF
2226 #undef IF_SC_SIGNED
2227 #undef SC_UNSIGNED
2228
2229 #undef XOR_HELPER
2230 #undef OR_HELPER
2231 #undef AND_HELPER
2232 #undef MOD_HELPER
2233 #undef DIV_HELPER
2234 #undef MUL_HELPER
2235 #undef SUB_HELPER
2236 #undef ADD_HELPER
2237
2238 #undef CLASS_TYPE
2239 #undef CLASS_TYPE_STR
2240
2241 #include "sc_unsigned_bitref.inc"
2242 #include "sc_unsigned_subref.inc"
2243
2244 #undef CONVERT_LONG
2245 #undef CONVERT_LONG_2
2246 #undef CONVERT_INT64
2247 #undef CONVERT_INT64_2
2248
2249 } // namespace sc_dt
2250
2251
2252 // End of file.
2253