00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056 #include <ctype.h>
00057 #include <math.h>
00058
00059 #include "sysc/kernel/sc_cmnhdr.h"
00060 #include "sysc/kernel/sc_macros.h"
00061 #include "sysc/datatypes/int/sc_unsigned.h"
00062 #include "sysc/datatypes/int/sc_signed.h"
00063 #include "sysc/datatypes/int/sc_int_base.h"
00064 #include "sysc/datatypes/int/sc_uint_base.h"
00065 #include "sysc/datatypes/int/sc_int_ids.h"
00066 #include "sysc/datatypes/bit/sc_bv_base.h"
00067 #include "sysc/datatypes/bit/sc_lv_base.h"
00068 #include "sysc/datatypes/misc/sc_concatref.h"
00069 #include "sysc/datatypes/fx/sc_ufix.h"
00070 #include "sysc/datatypes/fx/scfx_other_defs.h"
00071
00072 namespace sc_dt
00073 {
00074
00075
00076
00077
00078
00079 sc_core::sc_vpool<sc_unsigned> sc_unsigned::m_pool(8);
00080 sc_core::sc_vpool<sc_unsigned_bitref> sc_unsigned_bitref::m_pool(9);
00081 sc_core::sc_vpool<sc_unsigned_subref> sc_unsigned_subref::m_pool(9);
00082
00083
00084
00085
00086
00087 void
00088 sc_unsigned::invalid_index( int i ) const
00089 {
00090 char msg[BUFSIZ];
00091 std::sprintf( msg,
00092 "sc_biguint bit selection: index = %d violates "
00093 "0 <= index <= %d",
00094 i, nbits - 2 );
00095 SC_REPORT_ERROR( sc_core::SC_ID_OUT_OF_BOUNDS_, msg );
00096 }
00097
00098 void
00099 sc_unsigned::invalid_range( int l, int r ) const
00100 {
00101 char msg[BUFSIZ];
00102 std::sprintf( msg,
00103 "sc_biguint part selection: left = %d, right = %d \n"
00104 " violates either (0 <= left <= %d) or (0 <= right <= %d)",
00105 l, r, nbits-2, nbits-2 );
00106 SC_REPORT_ERROR( sc_core::SC_ID_OUT_OF_BOUNDS_, msg );
00107 }
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122 bool sc_unsigned::concat_get_ctrl( sc_digit* dst_p, int low_i ) const
00123 {
00124 int dst_i;
00125 int end_i;
00126 int left_shift;
00127 sc_digit mask;
00128
00129
00130
00131
00132 dst_i = low_i / BITS_PER_DIGIT;
00133 end_i = (low_i + nbits - 2) / BITS_PER_DIGIT;
00134 left_shift = low_i % BITS_PER_DIGIT;
00135
00136
00137
00138
00139
00140
00141
00142 mask = ~(-1 << left_shift);
00143 dst_p[dst_i] = ( dst_p[dst_i] & ~mask );
00144 dst_i++;
00145
00146 for ( ; dst_i <= end_i; dst_i++ ) dst_p[dst_i] = 0;
00147
00148 return false;
00149 }
00150
00151 bool sc_unsigned::concat_get_data( sc_digit* dst_p, int low_i ) const
00152 {
00153 int dst_i;
00154 int end_i;
00155 int high_i;
00156 int left_shift;
00157 sc_digit left_word;
00158 sc_digit mask;
00159 bool result;
00160 int right_shift;
00161 sc_digit right_word;
00162 int real_bits;
00163 int src_i;
00164
00165
00166
00167
00168 real_bits = nbits - 1;
00169 dst_i = low_i / BITS_PER_DIGIT;
00170 high_i = low_i + real_bits - 1;
00171 end_i = high_i / BITS_PER_DIGIT;
00172 left_shift = low_i % BITS_PER_DIGIT;
00173
00174
00175 switch ( sgn )
00176 {
00177
00178
00179
00180 case SC_POS:
00181 result = true;
00182
00183
00184
00185 if ( dst_i == end_i )
00186 {
00187 mask = ~(-1 << real_bits) << left_shift;
00188 dst_p[dst_i] = ( dst_p[dst_i] & ~mask ) |
00189 ((digit[0] << left_shift) & mask);
00190
00191 }
00192
00193
00194
00195
00196 else if ( left_shift == 0 )
00197 {
00198 for ( src_i = 0; dst_i < end_i; dst_i++, src_i++ )
00199 {
00200 dst_p[dst_i] = digit[src_i];
00201 }
00202 high_i = high_i % BITS_PER_DIGIT;
00203 mask = ~(-2 << high_i) & DIGIT_MASK;
00204 dst_p[dst_i] = digit[src_i] & mask;
00205 }
00206
00207
00208
00209
00210 else
00211 {
00212 high_i = high_i % BITS_PER_DIGIT;
00213 right_shift = BITS_PER_DIGIT - left_shift;
00214 mask = ~(-1 << left_shift);
00215 right_word = digit[0];
00216 dst_p[dst_i] = (dst_p[dst_i] & mask) |
00217 ((right_word << left_shift) & DIGIT_MASK);
00218 for ( src_i = 1, dst_i++; dst_i < end_i; dst_i++, src_i++ )
00219 {
00220 left_word = digit[src_i];
00221 dst_p[dst_i] = ((left_word << left_shift)&DIGIT_MASK) |
00222 (right_word >> right_shift);
00223 right_word = left_word;
00224 }
00225 left_word = digit[src_i];
00226 mask = ~(-2 << high_i) & DIGIT_MASK;
00227 dst_p[dst_i] = ((left_word << left_shift) |
00228 (right_word >> right_shift)) & mask;
00229 }
00230 break;
00231
00232
00233
00234
00235 default:
00236 result = false;
00237
00238
00239
00240 if ( dst_i == end_i )
00241 {
00242 mask = ~(-1 << nbits) << left_shift;
00243 dst_p[dst_i] = dst_p[dst_i] & ~mask;
00244 }
00245
00246
00247
00248
00249 else if ( left_shift == 0 )
00250 {
00251 for ( src_i = 0; dst_i < end_i; dst_i++, src_i++ )
00252 {
00253 dst_p[dst_i] = 0;
00254 }
00255 high_i = high_i % BITS_PER_DIGIT;
00256 mask = ~(-2 << high_i) & DIGIT_MASK;
00257 dst_p[dst_i] = 0;
00258 }
00259
00260
00261
00262
00263 else
00264 {
00265 high_i = high_i % BITS_PER_DIGIT;
00266 right_shift = BITS_PER_DIGIT - left_shift;
00267 mask = ~(-1 << left_shift);
00268 dst_p[dst_i] = (dst_p[dst_i] & mask);
00269 for ( dst_i++; dst_i <= end_i; dst_i++ )
00270 {
00271 dst_p[dst_i] = 0;
00272 }
00273 }
00274 break;
00275 }
00276 return result;
00277 }
00278
00279
00280
00281 uint64 sc_unsigned::concat_get_uint64() const
00282 {
00283 uint64 result;
00284
00285 switch ( sgn )
00286 {
00287 case SC_POS:
00288 result = 0;
00289 if ( ndigits > 2 )
00290 result = digit[2];
00291 if ( ndigits > 1 )
00292 result = (result << BITS_PER_DIGIT) | digit[1];
00293 result = (result << BITS_PER_DIGIT) | digit[0];
00294 break;
00295 default:
00296 result = 0;
00297 break;
00298 }
00299 return result;
00300 }
00301
00302
00303 void sc_unsigned::concat_set(int64 src, int low_i)
00304 {
00305 *this = (low_i < 64) ? src >> low_i : src >> 63;
00306 }
00307
00308 void sc_unsigned::concat_set(const sc_signed& src, int low_i)
00309 {
00310 if ( low_i < src.length() )
00311 *this = src >> low_i;
00312 else
00313 *this = (src<0) ? (int_type)-1 : 0;
00314 }
00315
00316 void sc_unsigned::concat_set(const sc_unsigned& src, int low_i)
00317 {
00318 if ( low_i < src.length() )
00319 *this = src >> low_i;
00320 else
00321 *this = 0;
00322 }
00323
00324 void sc_unsigned::concat_set(uint64 src, int low_i)
00325 {
00326 *this = (low_i < 64) ? src >> low_i : 0;
00327 }
00328
00329
00330
00331
00332
00333
00334 bool sc_unsigned::and_reduce() const
00335 {
00336 int i;
00337
00338 if ( sgn == SC_ZERO ) return false;
00339 for ( i = 0; i < ndigits-1; i++ )
00340 if ( (digit[i] & DIGIT_MASK) != DIGIT_MASK ) return false;
00341 if ( (digit[i] & ~(-1 << ((nbits-1) % BITS_PER_DIGIT))) ==
00342 (sc_digit)~(-1 << ((nbits-1) % BITS_PER_DIGIT)))
00343 return true;
00344 return false;
00345 }
00346
00347 bool sc_unsigned::or_reduce() const
00348 {
00349 return ( sgn == SC_ZERO ) ? false : true;
00350 }
00351
00352 bool sc_unsigned::xor_reduce() const
00353 {
00354 int i;
00355 int odd;
00356
00357 odd = 0;
00358 for ( i = 0; i < nbits-1; i++ )
00359 if ( test(i) ) odd = ~odd;
00360 return odd ? true : false;
00361 }
00362
00363
00364
00365
00366
00367
00368
00369
00370 const sc_unsigned&
00371 sc_unsigned::operator = ( const char* a )
00372 {
00373 if( a == 0 ) {
00374 SC_REPORT_ERROR( sc_core::SC_ID_CONVERSION_FAILED_,
00375 "character string is zero" );
00376 }
00377 if( *a == 0 ) {
00378 SC_REPORT_ERROR( sc_core::SC_ID_CONVERSION_FAILED_,
00379 "character string is empty" );
00380 }
00381 try {
00382 int len = length();
00383 sc_ufix aa( a, len, len, SC_TRN, SC_WRAP, 0, SC_ON );
00384 return this->operator = ( aa );
00385 } catch( sc_core::sc_report ) {
00386 char msg[BUFSIZ];
00387 std::sprintf( msg, "character string '%s' is not valid", a );
00388 SC_REPORT_ERROR( sc_core::SC_ID_CONVERSION_FAILED_, msg );
00389
00390 }
00391 return *this;
00392 }
00393
00394 const sc_unsigned&
00395 sc_unsigned::operator=(int64 v)
00396 {
00397 sgn = get_sign(v);
00398 if ( sgn == SC_ZERO ) {
00399 vec_zero(ndigits, digit);
00400 }
00401 else {
00402 from_uint(ndigits, digit, (uint64) v);
00403 convert_SM_to_2C_to_SM();
00404 }
00405 return *this;
00406 }
00407
00408 const sc_unsigned&
00409 sc_unsigned::operator=(uint64 v)
00410 {
00411 if (v == 0) {
00412 sgn = SC_ZERO;
00413 vec_zero(ndigits, digit);
00414 }
00415 else {
00416 sgn = SC_POS;
00417 from_uint(ndigits, digit, v);
00418 convert_SM_to_2C_to_SM();
00419 }
00420 return *this;
00421 }
00422
00423 const sc_unsigned&
00424 sc_unsigned::operator=(long v)
00425 {
00426 sgn = get_sign(v);
00427 if ( sgn == SC_ZERO ) {
00428 vec_zero(ndigits, digit);
00429 }
00430 else {
00431 from_uint(ndigits, digit, (unsigned long) v);
00432 convert_SM_to_2C_to_SM();
00433 }
00434 return *this;
00435 }
00436
00437 const sc_unsigned&
00438 sc_unsigned::operator=(unsigned long v)
00439 {
00440 if (v == 0) {
00441 sgn = SC_ZERO;
00442 vec_zero(ndigits, digit);
00443 }
00444 else {
00445 sgn = SC_POS;
00446 from_uint(ndigits, digit, v);
00447 convert_SM_to_2C_to_SM();
00448 }
00449 return *this;
00450 }
00451
00452 const sc_unsigned&
00453 sc_unsigned::operator=(double v)
00454 {
00455 is_bad_double(v);
00456 sgn = SC_POS;
00457 register int i = 0;
00458 while (floor(v) && (i < ndigits)) {
00459 #ifndef WIN32
00460 digit[i++] = (sc_digit) floor(remainder(v, DIGIT_RADIX));
00461 #else
00462 digit[i++] = (sc_digit) floor(fmod(v, DIGIT_RADIX));
00463 #endif
00464 v /= DIGIT_RADIX;
00465 }
00466 vec_zero(i, ndigits, digit);
00467 convert_SM_to_2C_to_SM();
00468 return *this;
00469 }
00470
00471
00472
00473
00474 const sc_unsigned&
00475 sc_unsigned::operator = ( const sc_bv_base& v )
00476 {
00477 int minlen = sc_min( nbits, v.length() );
00478 int i = 0;
00479 for( ; i < minlen; ++ i ) {
00480 safe_set( i, v.get_bit( i ), digit );
00481 }
00482 for( ; i < nbits; ++ i ) {
00483 safe_set( i, 0, digit );
00484 }
00485 convert_2C_to_SM();
00486 return *this;
00487 }
00488
00489 const sc_unsigned&
00490 sc_unsigned::operator = ( const sc_lv_base& v )
00491 {
00492 int minlen = sc_min( nbits, v.length() );
00493 int i = 0;
00494 for( ; i < minlen; ++ i ) {
00495 safe_set( i, sc_logic( v.get_bit( i ) ).to_bool(), digit );
00496 }
00497 for( ; i < nbits; ++ i ) {
00498 safe_set( i, 0, digit );
00499 }
00500 convert_2C_to_SM();
00501 return *this;
00502 }
00503
00504
00505
00506
00507 const std::string
00508 sc_unsigned::to_string( sc_numrep numrep ) const
00509 {
00510 int len = length();
00511 sc_ufix aa( *this, len, len, SC_TRN, SC_WRAP, 0, SC_ON );
00512 return aa.to_string( numrep );
00513 }
00514
00515 const std::string
00516 sc_unsigned::to_string( sc_numrep numrep, bool w_prefix ) const
00517 {
00518 int len = length();
00519 sc_ufix aa( *this, len, len, SC_TRN, SC_WRAP, 0, SC_ON );
00520 return aa.to_string( numrep, w_prefix );
00521 }
00522
00523
00524
00525
00526
00527
00528 const sc_unsigned&
00529 sc_unsigned::operator= (const sc_int_base& v)
00530 { return operator=((int64) v); }
00531
00532 const sc_unsigned&
00533 sc_unsigned::operator+=(const sc_int_base& v)
00534 { return operator+=((int64) v); }
00535
00536 const sc_unsigned&
00537 sc_unsigned::operator-=(const sc_int_base& v)
00538 { return operator-=((int64) v); }
00539
00540 const sc_unsigned&
00541 sc_unsigned::operator*=(const sc_int_base& v)
00542 { return operator*=((int64) v); }
00543
00544 const sc_unsigned&
00545 sc_unsigned::operator/=(const sc_int_base& v)
00546 { return operator/=((int64) v); }
00547
00548 const sc_unsigned&
00549 sc_unsigned::operator%=(const sc_int_base& v)
00550 { return operator%=((int64) v); }
00551
00552 const sc_unsigned&
00553 sc_unsigned::operator&=(const sc_int_base& v)
00554 { return operator&=((int64) v); }
00555
00556 const sc_unsigned&
00557 sc_unsigned::operator|=(const sc_int_base& v)
00558 { return operator|=((int64) v); }
00559
00560 const sc_unsigned&
00561 sc_unsigned::operator^=(const sc_int_base& v)
00562 { return operator^=((int64) v); }
00563
00564 sc_unsigned
00565 operator<<(const sc_unsigned& u, const sc_int_base& v)
00566 { return operator<<(u, (int64) v); }
00567 const sc_unsigned&
00568 sc_unsigned::operator<<=(const sc_int_base& v)
00569 { return operator<<=((int64) v); }
00570
00571 sc_unsigned
00572 operator>>(const sc_unsigned& u, const sc_int_base& v)
00573 { return operator>>(u, (int64) v); }
00574 const sc_unsigned&
00575 sc_unsigned::operator>>=(const sc_int_base& v)
00576 { return operator>>=((int64) v); }
00577
00578 bool
00579 operator==(const sc_unsigned& u, const sc_int_base& v)
00580 { return operator==(u, (int64) v); }
00581 bool
00582 operator==(const sc_int_base& u, const sc_unsigned& v)
00583 { return operator==((int64) u, v); }
00584
00585 bool
00586 operator!=(const sc_unsigned& u, const sc_int_base& v)
00587 { return operator!=(u, (int64) v); }
00588 bool
00589 operator!=(const sc_int_base& u, const sc_unsigned& v)
00590 { return operator!=((int64) u, v); }
00591
00592 bool
00593 operator<(const sc_unsigned& u, const sc_int_base& v)
00594 { return operator<(u, (int64) v); }
00595 bool
00596 operator<(const sc_int_base& u, const sc_unsigned& v)
00597 { return operator<((int64) u, v); }
00598
00599 bool
00600 operator<=(const sc_unsigned& u, const sc_int_base& v)
00601 { return operator<=(u, (int64) v); }
00602 bool
00603 operator<=(const sc_int_base& u, const sc_unsigned& v)
00604 { return operator<=((int64) u, v); }
00605
00606 bool
00607 operator>(const sc_unsigned& u, const sc_int_base& v)
00608 { return operator>(u, (int64) v); }
00609 bool
00610 operator>(const sc_int_base& u, const sc_unsigned& v)
00611 { return operator>((int64) u, v); }
00612
00613 bool
00614 operator>=(const sc_unsigned& u, const sc_int_base& v)
00615 { return operator>=(u, (int64) v); }
00616 bool
00617 operator>=(const sc_int_base& u, const sc_unsigned& v)
00618 { return operator>=((int64) u, v); }
00619
00620
00621
00622
00623
00624
00625 const sc_unsigned&
00626 sc_unsigned::operator= (const sc_uint_base& v)
00627 { return operator=((uint64) v); }
00628
00629 sc_unsigned
00630 operator+(const sc_unsigned& u, const sc_uint_base& v)
00631 { return operator+(u, (uint64) v); }
00632 sc_unsigned
00633 operator+(const sc_uint_base& u, const sc_unsigned& v)
00634 { return operator+((uint64) u, v); }
00635 const sc_unsigned&
00636 sc_unsigned::operator+=(const sc_uint_base& v)
00637 { return operator+=((uint64) v); }
00638
00639 const sc_unsigned&
00640 sc_unsigned::operator-=(const sc_uint_base& v)
00641 { return operator-=((uint64) v); }
00642
00643 sc_unsigned
00644 operator*(const sc_unsigned& u, const sc_uint_base& v)
00645 { return operator*(u, (uint64) v); }
00646 sc_unsigned
00647 operator*(const sc_uint_base& u, const sc_unsigned& v)
00648 { return operator*((uint64) u, v); }
00649 const sc_unsigned&
00650 sc_unsigned::operator*=(const sc_uint_base& v)
00651 { return operator*=((uint64) v); }
00652
00653 sc_unsigned
00654 operator/(const sc_unsigned& u, const sc_uint_base& v)
00655 { return operator/(u, (uint64) v); }
00656 sc_unsigned
00657 operator/(const sc_uint_base& u, const sc_unsigned& v)
00658 { return operator/((uint64) u, v); }
00659 const sc_unsigned&
00660 sc_unsigned::operator/=(const sc_uint_base& v)
00661 { return operator/=((uint64) v); }
00662
00663 sc_unsigned
00664 operator%(const sc_unsigned& u, const sc_uint_base& v)
00665 { return operator%(u, (uint64) v); }
00666 sc_unsigned
00667 operator%(const sc_uint_base& u, const sc_unsigned& v)
00668 { return operator%((uint64) u, v); }
00669 const sc_unsigned&
00670 sc_unsigned::operator%=(const sc_uint_base& v)
00671 { return operator%=((uint64) v); }
00672
00673 sc_unsigned
00674 operator&(const sc_unsigned& u, const sc_uint_base& v)
00675 { return operator&(u, (uint64) v); }
00676 sc_unsigned
00677 operator&(const sc_uint_base& u, const sc_unsigned& v)
00678 { return operator&((uint64) u, v); }
00679 const sc_unsigned&
00680 sc_unsigned::operator&=(const sc_uint_base& v)
00681 { return operator&=((uint64) v); }
00682
00683 sc_unsigned
00684 operator|(const sc_unsigned& u, const sc_uint_base& v)
00685 { return operator|(u, (uint64) v); }
00686 sc_unsigned
00687 operator|(const sc_uint_base& u, const sc_unsigned& v)
00688 { return operator|((uint64) u, v); }
00689 const sc_unsigned&
00690 sc_unsigned::operator|=(const sc_uint_base& v)
00691 { return operator|=((uint64) v); }
00692
00693 sc_unsigned
00694 operator^(const sc_unsigned& u, const sc_uint_base& v)
00695 { return operator^(u, (uint64) v); }
00696 sc_unsigned
00697 operator^(const sc_uint_base& u, const sc_unsigned& v)
00698 { return operator^((uint64) u, v); }
00699 const sc_unsigned&
00700 sc_unsigned::operator^=(const sc_uint_base& v)
00701 { return operator^=((uint64) v); }
00702
00703 sc_unsigned
00704 operator<<(const sc_unsigned& u, const sc_uint_base& v)
00705 { return operator<<(u, (uint64) v); }
00706 const sc_unsigned&
00707 sc_unsigned::operator<<=(const sc_uint_base& v)
00708 { return operator<<=((uint64) v); }
00709
00710 sc_unsigned
00711 operator>>(const sc_unsigned& u, const sc_uint_base& v)
00712 { return operator>>(u, (uint64) v); }
00713 const sc_unsigned&
00714 sc_unsigned::operator>>=(const sc_uint_base& v)
00715 { return operator>>=((uint64) v); }
00716
00717 bool
00718 operator==(const sc_unsigned& u, const sc_uint_base& v)
00719 { return operator==(u, (uint64) v); }
00720 bool
00721 operator==(const sc_uint_base& u, const sc_unsigned& v)
00722 { return operator==((uint64) u, v); }
00723
00724 bool
00725 operator!=(const sc_unsigned& u, const sc_uint_base& v)
00726 { return operator!=(u, (uint64) v); }
00727 bool
00728 operator!=(const sc_uint_base& u, const sc_unsigned& v)
00729 { return operator!=((uint64) u, v); }
00730
00731 bool
00732 operator<(const sc_unsigned& u, const sc_uint_base& v)
00733 { return operator<(u, (uint64) v); }
00734 bool
00735 operator<(const sc_uint_base& u, const sc_unsigned& v)
00736 { return operator<((uint64) u, v); }
00737
00738 bool
00739 operator<=(const sc_unsigned& u, const sc_uint_base& v)
00740 { return operator<=(u, (uint64) v); }
00741 bool
00742 operator<=(const sc_uint_base& u, const sc_unsigned& v)
00743 { return operator<=((uint64) u, v); }
00744
00745 bool
00746 operator>(const sc_unsigned& u, const sc_uint_base& v)
00747 { return operator>(u, (uint64) v); }
00748 bool
00749 operator>(const sc_uint_base& u, const sc_unsigned& v)
00750 { return operator>((uint64) u, v); }
00751
00752 bool
00753 operator>=(const sc_unsigned& u, const sc_uint_base& v)
00754 { return operator>=(u, (uint64) v); }
00755 bool
00756 operator>=(const sc_uint_base& u, const sc_unsigned& v)
00757 { return operator>=((uint64) u, v); }
00758
00759
00760
00761
00762
00763
00764
00765
00766
00767
00768
00769
00770
00771 #define CONVERT_LONG(u) \
00772 small_type u ## s = get_sign(u); \
00773 sc_digit u ## d[DIGITS_PER_ULONG]; \
00774 from_uint(DIGITS_PER_ULONG, u ## d, (unsigned long) u);
00775
00776 #define CONVERT_LONG_2(u) \
00777 sc_digit u ## d[DIGITS_PER_ULONG]; \
00778 from_uint(DIGITS_PER_ULONG, u ## d, (unsigned long) u);
00779
00780 #define CONVERT_INT(u) \
00781 small_type u ## s = get_sign(u); \
00782 sc_digit u ## d[DIGITS_PER_UINT]; \
00783 from_uint(DIGITS_PER_UINT, u ## d, (unsigned int) u);
00784
00785 #define CONVERT_INT_2(u) \
00786 sc_digit u ## d[DIGITS_PER_UINT]; \
00787 from_uint(DIGITS_PER_UINT, u ## d, (unsigned int) u);
00788
00789 #define CONVERT_INT64(u) \
00790 small_type u ## s = get_sign(u); \
00791 sc_digit u ## d[DIGITS_PER_UINT64]; \
00792 from_uint(DIGITS_PER_UINT64, u ## d, (uint64) u);
00793
00794 #define CONVERT_INT64_2(u) \
00795 sc_digit u ## d[DIGITS_PER_UINT64]; \
00796 from_uint(DIGITS_PER_UINT64, u ## d, (uint64) u);
00797
00798
00799
00800
00801
00802
00803
00804
00805
00806
00807
00808
00809
00810
00811
00812
00813
00814
00815
00816
00817
00818 sc_unsigned
00819 operator+(const sc_unsigned& u, const sc_unsigned& v)
00820 {
00821
00822 if (u.sgn == SC_ZERO)
00823 return sc_unsigned(v);
00824
00825 if (v.sgn == SC_ZERO)
00826 return sc_unsigned(u);
00827
00828
00829 return add_unsigned_friend(u.sgn, u.nbits, u.ndigits, u.digit,
00830 v.sgn, v.nbits, v.ndigits, v.digit);
00831
00832 }
00833
00834
00835 sc_unsigned
00836 operator+(const sc_unsigned &u, uint64 v)
00837 {
00838
00839 if (v == 0)
00840 return sc_unsigned(u);
00841
00842 CONVERT_INT64(v);
00843
00844 if (u.sgn == SC_ZERO)
00845 return sc_unsigned(vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd, false);
00846
00847
00848 return add_unsigned_friend(u.sgn, u.nbits, u.ndigits, u.digit,
00849 vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd);
00850
00851 }
00852
00853
00854 sc_unsigned
00855 operator+(uint64 u, const sc_unsigned &v)
00856 {
00857
00858 if (u == 0)
00859 return sc_unsigned(v);
00860
00861 CONVERT_INT64(u);
00862
00863 if (v.sgn == SC_ZERO)
00864 return sc_unsigned(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud, false);
00865
00866
00867
00868 return add_unsigned_friend(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud,
00869 v.sgn, v.nbits, v.ndigits, v.digit);
00870
00871 }
00872
00873
00874 sc_unsigned
00875 operator+(const sc_unsigned &u, unsigned long v)
00876 {
00877
00878 if (v == 0)
00879 return sc_unsigned(u);
00880
00881 CONVERT_LONG(v);
00882
00883 if (u.sgn == SC_ZERO)
00884 return sc_unsigned(vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd, false);
00885
00886
00887 return add_unsigned_friend(u.sgn, u.nbits, u.ndigits, u.digit,
00888 vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd);
00889
00890 }
00891
00892
00893 sc_unsigned
00894 operator+(unsigned long u, const sc_unsigned &v)
00895 {
00896
00897 if (u == 0)
00898 return sc_unsigned(v);
00899
00900 CONVERT_LONG(u);
00901
00902 if (v.sgn == SC_ZERO)
00903 return sc_unsigned(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud, false);
00904
00905
00906 return add_unsigned_friend(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud,
00907 v.sgn, v.nbits, v.ndigits, v.digit);
00908
00909 }
00910
00911
00912
00913
00914
00915
00916
00917
00918
00919
00920
00921
00922
00923
00924
00925
00926
00927
00928
00929
00930
00931
00932
00933
00934
00935
00936
00937
00938
00939
00940
00941
00942
00943
00944
00945
00946
00947 sc_unsigned
00948 operator*(const sc_unsigned& u, const sc_unsigned& v)
00949 {
00950
00951 small_type s = mul_signs(u.sgn, v.sgn);
00952
00953 if (s == SC_ZERO)
00954 return sc_unsigned();
00955
00956
00957 return mul_unsigned_friend(s, u.nbits, u.ndigits, u.digit,
00958 v.nbits, v.ndigits, v.digit);
00959
00960 }
00961
00962
00963 sc_unsigned
00964 operator*(const sc_unsigned& u, uint64 v)
00965 {
00966
00967 small_type s = mul_signs(u.sgn, get_sign(v));
00968
00969 if (s == SC_ZERO)
00970 return sc_unsigned();
00971
00972 CONVERT_INT64_2(v);
00973
00974
00975 return mul_unsigned_friend(s, u.nbits, u.ndigits, u.digit,
00976 BITS_PER_UINT64, DIGITS_PER_UINT64, vd);
00977
00978 }
00979
00980
00981 sc_unsigned
00982 operator*(uint64 u, const sc_unsigned& v)
00983 {
00984
00985 small_type s = mul_signs(v.sgn, get_sign(u));
00986
00987 if (s == SC_ZERO)
00988 return sc_unsigned();
00989
00990 CONVERT_INT64_2(u);
00991
00992
00993 return mul_unsigned_friend(s, BITS_PER_UINT64, DIGITS_PER_UINT64, ud,
00994 v.nbits, v.ndigits, v.digit);
00995
00996 }
00997
00998
00999 sc_unsigned
01000 operator*(const sc_unsigned& u, unsigned long v)
01001 {
01002
01003 small_type s = mul_signs(u.sgn, get_sign(v));
01004
01005 if (s == SC_ZERO)
01006 return sc_unsigned();
01007
01008 CONVERT_LONG_2(v);
01009
01010
01011 return mul_unsigned_friend(s, u.nbits, u.ndigits, u.digit,
01012 BITS_PER_ULONG, DIGITS_PER_ULONG, vd);
01013
01014 }
01015
01016 sc_unsigned
01017 operator*(unsigned long u, const sc_unsigned& v)
01018 {
01019
01020 small_type s = mul_signs(v.sgn, get_sign(u));
01021
01022 if (s == SC_ZERO)
01023 return sc_unsigned();
01024
01025 CONVERT_LONG_2(u);
01026
01027
01028 return mul_unsigned_friend(s, BITS_PER_ULONG, DIGITS_PER_ULONG, ud,
01029 v.nbits, v.ndigits, v.digit);
01030
01031 }
01032
01033
01034
01035
01036
01037
01038
01039
01040
01041
01042
01043
01044
01045
01046
01047
01048
01049 sc_unsigned
01050 operator/(const sc_unsigned& u, const sc_unsigned& v)
01051 {
01052
01053 small_type s = mul_signs(u.sgn, v.sgn);
01054
01055 if (s == SC_ZERO) {
01056 div_by_zero(v.sgn);
01057 return sc_unsigned();
01058 }
01059
01060
01061 return div_unsigned_friend(s, u.nbits, u.ndigits, u.digit,
01062 v.nbits, v.ndigits, v.digit);
01063
01064 }
01065
01066
01067 sc_unsigned
01068 operator/(const sc_unsigned& u, uint64 v)
01069 {
01070
01071 small_type s = mul_signs(u.sgn, get_sign(v));
01072
01073 if (s == SC_ZERO) {
01074 div_by_zero(v);
01075 return sc_unsigned();
01076 }
01077
01078 CONVERT_INT64_2(v);
01079
01080
01081 return div_unsigned_friend(s, u.nbits, u.ndigits, u.digit,
01082 BITS_PER_UINT64, DIGITS_PER_UINT64, vd);
01083
01084 }
01085
01086
01087 sc_unsigned
01088 operator/(uint64 u, const sc_unsigned& v)
01089 {
01090
01091 small_type s = mul_signs(v.sgn, get_sign(u));
01092
01093 if (s == SC_ZERO) {
01094 div_by_zero(v.sgn);
01095 return sc_unsigned();
01096
01097 }
01098
01099 CONVERT_INT64_2(u);
01100
01101
01102 return div_unsigned_friend(s, BITS_PER_UINT64, DIGITS_PER_UINT64, ud,
01103 v.nbits, v.ndigits, v.digit);
01104
01105 }
01106
01107
01108 sc_unsigned
01109 operator/(const sc_unsigned& u, unsigned long v)
01110 {
01111
01112 small_type s = mul_signs(u.sgn, get_sign(v));
01113
01114 if (s == SC_ZERO) {
01115 div_by_zero(v);
01116 return sc_unsigned();
01117 }
01118
01119 CONVERT_LONG_2(v);
01120
01121
01122 return div_unsigned_friend(s, u.nbits, u.ndigits, u.digit,
01123 BITS_PER_ULONG, DIGITS_PER_ULONG, vd);
01124
01125 }
01126
01127
01128 sc_unsigned
01129 operator/(unsigned long u, const sc_unsigned& v)
01130 {
01131
01132 small_type s = mul_signs(v.sgn, get_sign(u));
01133
01134 if (s == SC_ZERO) {
01135 div_by_zero(v.sgn);
01136 return sc_unsigned();
01137
01138 }
01139
01140 CONVERT_LONG_2(u);
01141
01142
01143 return div_unsigned_friend(s, BITS_PER_ULONG, DIGITS_PER_ULONG, ud,
01144 v.nbits, v.ndigits, v.digit);
01145
01146 }
01147
01148
01149
01150
01151
01152
01153
01154
01155
01156
01157
01158
01159
01160
01161
01162
01163
01164 sc_unsigned
01165 operator%(const sc_unsigned& u, const sc_unsigned& v)
01166 {
01167
01168 if ((u.sgn == SC_ZERO) || (v.sgn == SC_ZERO)) {
01169 div_by_zero(v.sgn);
01170 return sc_unsigned();
01171 }
01172
01173
01174 return mod_unsigned_friend(u.sgn, u.nbits, u.ndigits, u.digit,
01175 v.nbits, v.ndigits, v.digit);
01176 }
01177
01178
01179 sc_unsigned
01180 operator%(const sc_unsigned& u, uint64 v)
01181 {
01182
01183 if ((u.sgn == SC_ZERO) || (v == 0)) {
01184 div_by_zero(v);
01185 return sc_unsigned();
01186 }
01187
01188 CONVERT_INT64_2(v);
01189
01190
01191 return mod_unsigned_friend(u.sgn, u.nbits, u.ndigits, u.digit,
01192 BITS_PER_UINT64, DIGITS_PER_UINT64, vd);
01193
01194 }
01195
01196
01197 sc_unsigned
01198 operator%(uint64 u, const sc_unsigned& v)
01199 {
01200
01201 if ((u == 0) || (v.sgn == SC_ZERO)) {
01202 div_by_zero(v.sgn);
01203 return sc_unsigned();
01204 }
01205
01206 CONVERT_INT64(u);
01207
01208
01209 return mod_unsigned_friend(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud,
01210 v.nbits, v.ndigits, v.digit);
01211
01212 }
01213
01214
01215 sc_unsigned
01216 operator%(const sc_unsigned& u, unsigned long v)
01217 {
01218
01219 if ((u.sgn == SC_ZERO) || (v == 0)) {
01220 div_by_zero(v);
01221 return sc_unsigned();
01222 }
01223
01224 CONVERT_LONG_2(v);
01225
01226
01227 return mod_unsigned_friend(u.sgn, u.nbits, u.ndigits, u.digit,
01228 BITS_PER_ULONG, DIGITS_PER_ULONG, vd);
01229
01230 }
01231
01232
01233 sc_unsigned
01234 operator%(unsigned long u, const sc_unsigned& v)
01235 {
01236
01237 if ((u == 0) || (v.sgn == SC_ZERO)) {
01238 div_by_zero(v.sgn);
01239 return sc_unsigned();
01240 }
01241
01242 CONVERT_LONG(u);
01243
01244
01245 return mod_unsigned_friend(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud,
01246 v.nbits, v.ndigits, v.digit);
01247
01248 }
01249
01250
01251
01252
01253
01254
01255
01256
01257
01258
01259
01260
01261
01262
01263
01264
01265 sc_unsigned
01266 operator&(const sc_unsigned& u, const sc_unsigned& v)
01267 {
01268
01269 if ((u.sgn == SC_ZERO) || (v.sgn == SC_ZERO))
01270 return sc_unsigned();
01271
01272
01273 return and_unsigned_friend(u.sgn, u.nbits, u.ndigits, u.digit,
01274 v.sgn, v.nbits, v.ndigits, v.digit);
01275
01276 }
01277
01278
01279 sc_unsigned
01280 operator&(const sc_unsigned& u, uint64 v)
01281 {
01282
01283 if ((u.sgn == SC_ZERO) || (v == 0))
01284 return sc_unsigned();
01285
01286 CONVERT_INT64(v);
01287
01288
01289 return and_unsigned_friend(u.sgn, u.nbits, u.ndigits, u.digit,
01290 vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd);
01291
01292 }
01293
01294
01295 sc_unsigned
01296 operator&(uint64 u, const sc_unsigned& v)
01297 {
01298
01299 if ((u == 0) || (v.sgn == SC_ZERO))
01300 return sc_unsigned();
01301
01302 CONVERT_INT64(u);
01303
01304
01305 return and_unsigned_friend(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud,
01306 v.sgn, v.nbits, v.ndigits, v.digit);
01307
01308 }
01309
01310
01311 sc_unsigned
01312 operator&(const sc_unsigned& u, unsigned long v)
01313 {
01314
01315 if ((u.sgn == SC_ZERO) || (v == 0))
01316 return sc_unsigned();
01317
01318 CONVERT_LONG(v);
01319
01320
01321 return and_unsigned_friend(u.sgn, u.nbits, u.ndigits, u.digit,
01322 vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd);
01323
01324 }
01325
01326
01327 sc_unsigned
01328 operator&(unsigned long u, const sc_unsigned& v)
01329 {
01330
01331 if ((u == 0) || (v.sgn == SC_ZERO))
01332 return sc_unsigned();
01333
01334 CONVERT_LONG(u);
01335
01336
01337 return and_unsigned_friend(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud,
01338 v.sgn, v.nbits, v.ndigits, v.digit);
01339
01340 }
01341
01342
01343
01344
01345
01346
01347
01348
01349
01350
01351
01352
01353
01354
01355
01356
01357
01358 sc_unsigned
01359 operator|(const sc_unsigned& u, const sc_unsigned& v)
01360 {
01361
01362 if (v.sgn == SC_ZERO)
01363 return sc_unsigned(u);
01364
01365 if (u.sgn == SC_ZERO)
01366 return sc_unsigned(v);
01367
01368
01369 return or_unsigned_friend(u.sgn, u.nbits, u.ndigits, u.digit,
01370 v.sgn, v.nbits, v.ndigits, v.digit);
01371
01372 }
01373
01374
01375 sc_unsigned
01376 operator|(const sc_unsigned& u, uint64 v)
01377 {
01378
01379 if (v == 0)
01380 return sc_unsigned(u);
01381
01382 CONVERT_INT64(v);
01383
01384 if (u.sgn == SC_ZERO)
01385 return sc_unsigned(vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd, false);
01386
01387
01388 return or_unsigned_friend(u.sgn, u.nbits, u.ndigits, u.digit,
01389 vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd);
01390
01391 }
01392
01393
01394 sc_unsigned
01395 operator|(uint64 u, const sc_unsigned& v)
01396 {
01397
01398 if (u == 0)
01399 return sc_unsigned(v);
01400
01401 CONVERT_INT64(u);
01402
01403 if (v.sgn == SC_ZERO)
01404 return sc_unsigned(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud, false);
01405
01406
01407 return or_unsigned_friend(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud,
01408 v.sgn, v.nbits, v.ndigits, v.digit);
01409
01410 }
01411
01412
01413 sc_unsigned
01414 operator|(const sc_unsigned& u, unsigned long v)
01415 {
01416
01417 if (v == 0)
01418 return sc_unsigned(u);
01419
01420 CONVERT_LONG(v);
01421
01422 if (u.sgn == SC_ZERO)
01423 return sc_unsigned(vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd, false);
01424
01425
01426 return or_unsigned_friend(u.sgn, u.nbits, u.ndigits, u.digit,
01427 vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd);
01428
01429 }
01430
01431
01432 sc_unsigned
01433 operator|(unsigned long u, const sc_unsigned& v)
01434 {
01435
01436 if (u == 0)
01437 return sc_unsigned(v);
01438
01439 CONVERT_LONG(u);
01440
01441 if (v.sgn == SC_ZERO)
01442 return sc_unsigned(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud, false);
01443
01444
01445 return or_unsigned_friend(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud,
01446 v.sgn, v.nbits, v.ndigits, v.digit);
01447
01448 }
01449
01450
01451
01452
01453
01454
01455
01456
01457
01458
01459
01460
01461
01462
01463
01464
01465
01466
01467 sc_unsigned
01468 operator^(const sc_unsigned& u, const sc_unsigned& v)
01469 {
01470
01471 if (v.sgn == SC_ZERO)
01472 return sc_unsigned(u);
01473
01474 if (u.sgn == SC_ZERO)
01475 return sc_unsigned(v);
01476
01477
01478 return xor_unsigned_friend(u.sgn, u.nbits, u.ndigits, u.digit,
01479 v.sgn, v.nbits, v.ndigits, v.digit);
01480
01481 }
01482
01483
01484 sc_unsigned
01485 operator^(const sc_unsigned& u, uint64 v)
01486 {
01487
01488 if (v == 0)
01489 return sc_unsigned(u);
01490
01491 CONVERT_INT64(v);
01492
01493 if (u.sgn == SC_ZERO)
01494 return sc_unsigned(vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd, false);
01495
01496
01497 return xor_unsigned_friend(u.sgn, u.nbits, u.ndigits, u.digit,
01498 vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd);
01499
01500 }
01501
01502 sc_unsigned
01503 operator^(uint64 u, const sc_unsigned& v)
01504 {
01505 if (u == 0)
01506 return sc_unsigned(v);
01507
01508 CONVERT_INT64(u);
01509
01510 if (v.sgn == SC_ZERO)
01511 return sc_unsigned(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud, false);
01512
01513
01514 return xor_unsigned_friend(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud,
01515 v.sgn, v.nbits, v.ndigits, v.digit);
01516
01517 }
01518
01519
01520 sc_unsigned
01521 operator^(const sc_unsigned& u, unsigned long v)
01522 {
01523
01524 if (v == 0)
01525 return sc_unsigned(u);
01526
01527 CONVERT_LONG(v);
01528
01529 if (u.sgn == SC_ZERO)
01530 return sc_unsigned(vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd, false);
01531
01532
01533 return xor_unsigned_friend(u.sgn, u.nbits, u.ndigits, u.digit,
01534 vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd);
01535
01536 }
01537
01538 sc_unsigned
01539 operator^(unsigned long u, const sc_unsigned& v)
01540 {
01541 if (u == 0)
01542 return sc_unsigned(v);
01543
01544 CONVERT_LONG(u);
01545
01546 if (v.sgn == SC_ZERO)
01547 return sc_unsigned(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud, false);
01548
01549
01550 return xor_unsigned_friend(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud,
01551 v.sgn, v.nbits, v.ndigits, v.digit);
01552
01553 }
01554
01555
01556
01557
01558
01559
01560
01561
01562
01563
01564
01565
01566
01567
01568
01569
01570 sc_unsigned
01571 operator<<(const sc_unsigned& u, const sc_signed& v)
01572 {
01573 if ((v.sgn == SC_ZERO) || (v.sgn == SC_NEG))
01574 return sc_unsigned(u);
01575
01576 return operator<<(u, v.to_ulong());
01577 }
01578
01579
01580
01581
01582
01583
01584
01585
01586
01587 sc_unsigned
01588 operator>>(const sc_unsigned& u, const sc_signed& v)
01589 {
01590
01591 if ((v.sgn == SC_ZERO) || (v.sgn == SC_NEG))
01592 return sc_unsigned(u);
01593
01594 return operator>>(u, v.to_long());
01595
01596 }
01597
01598
01599
01600
01601
01602
01603
01604
01605
01606 sc_unsigned
01607 operator+(const sc_unsigned& u)
01608 {
01609 return sc_unsigned(u);
01610 }
01611
01612
01613
01614
01615
01616
01617 bool
01618 operator==(const sc_unsigned& u, const sc_unsigned& v)
01619 {
01620 if (&u == &v)
01621 return true;
01622 if (compare_unsigned(u.sgn, u.nbits, u.ndigits, u.digit,
01623 v.sgn, v.nbits, v.ndigits, v.digit) != 0)
01624 return false;
01625 return true;
01626 }
01627
01628
01629 bool
01630 operator==(const sc_unsigned& u, const sc_signed& v)
01631 {
01632 if (v.sgn == SC_NEG)
01633 return false;
01634 if (compare_unsigned(u.sgn, u.nbits, u.ndigits, u.digit,
01635 v.sgn, v.nbits, v.ndigits, v.digit, 0, 1) != 0)
01636 return false;
01637 return true;
01638 }
01639
01640
01641 bool
01642 operator==(const sc_signed& u, const sc_unsigned& v)
01643 {
01644 if (u.sgn == SC_NEG)
01645 return false;
01646 if (compare_unsigned(u.sgn, u.nbits, u.ndigits, u.digit,
01647 v.sgn, v.nbits, v.ndigits, v.digit, 1, 0) != 0)
01648 return false;
01649 return true;
01650 }
01651
01652
01653 bool
01654 operator==(const sc_unsigned& u, int64 v)
01655 {
01656 if (v < 0)
01657 return false;
01658 CONVERT_INT64(v);
01659 if (compare_unsigned(u.sgn, u.nbits, u.ndigits, u.digit,
01660 vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd) != 0)
01661 return false;
01662 return true;
01663 }
01664
01665
01666 bool
01667 operator==(int64 u, const sc_unsigned& v)
01668 {
01669 if (u < 0)
01670 return false;
01671 CONVERT_INT64(u);
01672 if (compare_unsigned(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud,
01673 v.sgn, v.nbits, v.ndigits, v.digit) != 0)
01674 return false;
01675 return true;
01676 }
01677
01678
01679 bool
01680 operator==(const sc_unsigned& u, uint64 v)
01681 {
01682 CONVERT_INT64(v);
01683 if (compare_unsigned(u.sgn, u.nbits, u.ndigits, u.digit,
01684 vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd) != 0)
01685 return false;
01686 return true;
01687 }
01688
01689
01690 bool
01691 operator==(uint64 u, const sc_unsigned& v)
01692 {
01693 CONVERT_INT64(u);
01694 if (compare_unsigned(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud,
01695 v.sgn, v.nbits, v.ndigits, v.digit) != 0)
01696 return false;
01697 return true;
01698 }
01699
01700
01701 bool
01702 operator==(const sc_unsigned& u, long v)
01703 {
01704 if (v < 0)
01705 return false;
01706 CONVERT_LONG(v);
01707 if (compare_unsigned(u.sgn, u.nbits, u.ndigits, u.digit,
01708 vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd) != 0)
01709 return false;
01710 return true;
01711 }
01712
01713
01714 bool
01715 operator==(long u, const sc_unsigned& v)
01716 {
01717 if (u < 0)
01718 return false;
01719 CONVERT_LONG(u);
01720 if (compare_unsigned(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud,
01721 v.sgn, v.nbits, v.ndigits, v.digit) != 0)
01722 return false;
01723 return true;
01724 }
01725
01726
01727 bool
01728 operator==(const sc_unsigned& u, unsigned long v)
01729 {
01730 CONVERT_LONG(v);
01731 if (compare_unsigned(u.sgn, u.nbits, u.ndigits, u.digit,
01732 vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd) != 0)
01733 return false;
01734 return true;
01735 }
01736
01737
01738 bool
01739 operator==(unsigned long u, const sc_unsigned& v)
01740 {
01741 CONVERT_LONG(u);
01742 if (compare_unsigned(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud,
01743 v.sgn, v.nbits, v.ndigits, v.digit) != 0)
01744 return false;
01745 return true;
01746 }
01747
01748
01749
01750
01751
01752
01753 bool
01754 operator!=(const sc_unsigned& u, const sc_signed& v)
01755 {
01756 return (! operator==(u, v));
01757 }
01758
01759
01760 bool
01761 operator!=(const sc_signed& u, const sc_unsigned& v)
01762 {
01763 return (! operator==(u, v));
01764 }
01765
01766
01767
01768
01769
01770
01771
01772
01773 bool
01774 operator<(const sc_unsigned& u, const sc_unsigned& v)
01775 {
01776 if (&u == &v)
01777 return false;
01778 if (compare_unsigned(u.sgn, u.nbits, u.ndigits, u.digit,
01779 v.sgn, v.nbits, v.ndigits, v.digit) < 0)
01780 return true;
01781 return false;
01782 }
01783
01784
01785 bool
01786 operator<(const sc_unsigned& u, const sc_signed& v)
01787 {
01788 if (v.sgn == SC_NEG)
01789 return false;
01790 if (compare_unsigned(u.sgn, u.nbits, u.ndigits, u.digit,
01791 v.sgn, v.nbits, v.ndigits, v.digit, 0, 1) < 0)
01792 return true;
01793 return false;
01794 }
01795
01796
01797 bool
01798 operator<(const sc_signed& u, const sc_unsigned& v)
01799 {
01800 if (u.sgn == SC_NEG)
01801 return true;
01802 if (compare_unsigned(u.sgn, u.nbits, u.ndigits, u.digit,
01803 v.sgn, v.nbits, v.ndigits, v.digit, 1, 0) < 0)
01804 return true;
01805 return false;
01806 }
01807
01808
01809 bool
01810 operator<(const sc_unsigned& u, int64 v)
01811 {
01812 if (v < 0)
01813 return false;
01814 CONVERT_INT64(v);
01815 if (compare_unsigned(u.sgn, u.nbits, u.ndigits, u.digit,
01816 vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd) < 0)
01817 return true;
01818 return false;
01819 }
01820
01821
01822 bool
01823 operator<(int64 u, const sc_unsigned& v)
01824 {
01825 if (u < 0)
01826 return true;
01827 CONVERT_INT64(u);
01828 if (compare_unsigned(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud,
01829 v.sgn, v.nbits, v.ndigits, v.digit) < 0)
01830 return true;
01831 return false;
01832 }
01833
01834
01835 bool
01836 operator<(const sc_unsigned& u, uint64 v)
01837 {
01838 CONVERT_INT64(v);
01839 if (compare_unsigned(u.sgn, u.nbits, u.ndigits, u.digit,
01840 vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd) < 0)
01841 return true;
01842 return false;
01843 }
01844
01845
01846 bool
01847 operator<(uint64 u, const sc_unsigned& v)
01848 {
01849 CONVERT_INT64(u);
01850 if (compare_unsigned(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud,
01851 v.sgn, v.nbits, v.ndigits, v.digit) < 0)
01852 return true;
01853 return false;
01854 }
01855
01856
01857 bool
01858 operator<(const sc_unsigned& u, long v)
01859 {
01860 if (v < 0)
01861 return false;
01862 CONVERT_LONG(v);
01863 if (compare_unsigned(u.sgn, u.nbits, u.ndigits, u.digit,
01864 vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd) < 0)
01865 return true;
01866 return false;
01867 }
01868
01869
01870 bool
01871 operator<(long u, const sc_unsigned& v)
01872 {
01873 if (u < 0)
01874 return true;
01875 CONVERT_LONG(u);
01876 if (compare_unsigned(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud,
01877 v.sgn, v.nbits, v.ndigits, v.digit) < 0)
01878 return true;
01879 return false;
01880 }
01881
01882
01883 bool
01884 operator<(const sc_unsigned& u, unsigned long v)
01885 {
01886 CONVERT_LONG(v);
01887 if (compare_unsigned(u.sgn, u.nbits, u.ndigits, u.digit,
01888 vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd) < 0)
01889 return true;
01890 return false;
01891 }
01892
01893
01894 bool
01895 operator<(unsigned long u, const sc_unsigned& v)
01896 {
01897 CONVERT_LONG(u);
01898 if (compare_unsigned(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud,
01899 v.sgn, v.nbits, v.ndigits, v.digit) < 0)
01900 return true;
01901 return false;
01902 }
01903
01904
01905
01906
01907
01908
01909 bool
01910 operator<=(const sc_unsigned& u, const sc_signed& v)
01911 {
01912 return (operator<(u, v) || operator==(u, v));
01913 }
01914
01915
01916 bool
01917 operator<=(const sc_signed& u, const sc_unsigned& v)
01918 {
01919 return (operator<(u, v) || operator==(u, v));
01920 }
01921
01922
01923
01924
01925
01926
01927
01928
01929 bool
01930 operator>(const sc_unsigned& u, const sc_signed& v)
01931 {
01932 return (! (operator<=(u, v)));
01933 }
01934
01935
01936 bool
01937 operator>(const sc_signed& u, const sc_unsigned& v)
01938 {
01939 return (! (operator<=(u, v)));
01940 }
01941
01942
01943
01944
01945
01946
01947
01948
01949 bool
01950 operator>=(const sc_unsigned& u, const sc_signed& v)
01951 {
01952 return (! (operator<(u, v)));
01953 }
01954
01955
01956 bool
01957 operator>=(const sc_signed& u, const sc_unsigned& v)
01958 {
01959 return (! (operator<(u, v)));
01960 }
01961
01962
01963
01964
01965
01966
01967
01968
01969
01970
01971
01972
01973
01974 int
01975 compare_unsigned(small_type us,
01976 int unb, int und, const sc_digit *ud,
01977 small_type vs,
01978 int vnb, int vnd, const sc_digit *vd,
01979 small_type if_u_signed,
01980 small_type if_v_signed)
01981 {
01982
01983 if (us == vs) {
01984
01985 if (us == SC_ZERO)
01986 return 0;
01987
01988 else {
01989
01990 int cmp_res = vec_skip_and_cmp(und, ud, vnd, vd);
01991
01992 if (us == SC_POS)
01993 return cmp_res;
01994 else
01995 return -cmp_res;
01996
01997 }
01998 }
01999 else {
02000
02001 if (us == SC_ZERO)
02002 return -vs;
02003
02004 if (vs == SC_ZERO)
02005 return us;
02006
02007 int cmp_res;
02008
02009 int nd = (us == SC_NEG ? und : vnd);
02010
02011 #ifdef SC_MAX_NBITS
02012 sc_digit d[MAX_NDIGITS];
02013 #else
02014 sc_digit *d = new sc_digit[nd];
02015 #endif
02016
02017 if (us == SC_NEG) {
02018
02019 vec_copy(nd, d, ud);
02020 vec_complement(nd, d);
02021 trim(if_u_signed, unb, nd, d);
02022 cmp_res = vec_skip_and_cmp(nd, d, vnd, vd);
02023
02024 }
02025 else {
02026
02027 vec_copy(nd, d, vd);
02028 vec_complement(nd, d);
02029 trim(if_v_signed, vnb, nd, d);
02030 cmp_res = vec_skip_and_cmp(und, ud, nd, d);
02031
02032 }
02033
02034 #ifndef SC_MAX_NBITS
02035 delete [] d;
02036 #endif
02037
02038 return cmp_res;
02039
02040 }
02041 }
02042
02043
02044
02045
02046
02047
02048 bool
02049 sc_unsigned::iszero() const
02050 {
02051 if (sgn == SC_ZERO)
02052 return true;
02053
02054 else if (sgn == SC_NEG) {
02055
02056
02057
02058
02059 #ifdef SC_MAX_NBITS
02060 sc_digit d[MAX_NDIGITS];
02061 #else
02062 sc_digit *d = new sc_digit[ndigits];
02063 #endif
02064
02065 vec_copy(ndigits, d, digit);
02066 vec_complement(ndigits, d);
02067 trim_unsigned(nbits, ndigits, d);
02068
02069 bool res = check_for_zero(ndigits, d);
02070
02071 #ifndef SC_MAX_NBITS
02072 delete [] d;
02073 #endif
02074
02075 return res;
02076
02077 }
02078 else
02079 return false;
02080 }
02081
02082
02083
02084
02085
02086
02087
02088
02089
02090
02091
02092 #define CLASS_TYPE sc_unsigned
02093 #define CLASS_TYPE_STR "sc_unsigned"
02094
02095 #define ADD_HELPER add_unsigned_friend
02096 #define SUB_HELPER sub_unsigned_friend
02097 #define MUL_HELPER mul_unsigned_friend
02098 #define DIV_HELPER div_unsigned_friend
02099 #define MOD_HELPER mod_unsigned_friend
02100 #define AND_HELPER and_unsigned_friend
02101 #define OR_HELPER or_unsigned_friend
02102 #define XOR_HELPER xor_unsigned_friend
02103
02104 #include "sc_nbfriends.inc"
02105
02106 #undef SC_SIGNED
02107 #define SC_UNSIGNED
02108 #define IF_SC_SIGNED 0 // 0 = sc_unsigned
02109 #define CLASS_TYPE_SUBREF sc_unsigned_subref_r
02110 #define OTHER_CLASS_TYPE sc_signed
02111 #define OTHER_CLASS_TYPE_SUBREF sc_signed_subref_r
02112
02113 #define MUL_ON_HELPER mul_on_help_unsigned
02114 #define DIV_ON_HELPER div_on_help_unsigned
02115 #define MOD_ON_HELPER mod_on_help_unsigned
02116
02117 #include "sc_nbcommon.inc"
02118
02119 #undef MOD_ON_HELPER
02120 #undef DIV_ON_HELPER
02121 #undef MUL_ON_HELPER
02122
02123 #undef OTHER_CLASS_TYPE_SUBREF
02124 #undef OTHER_CLASS_TYPE
02125 #undef CLASS_TYPE_SUBREF
02126 #undef IF_SC_SIGNED
02127 #undef SC_UNSIGNED
02128
02129 #undef XOR_HELPER
02130 #undef OR_HELPER
02131 #undef AND_HELPER
02132 #undef MOD_HELPER
02133 #undef DIV_HELPER
02134 #undef MUL_HELPER
02135 #undef SUB_HELPER
02136 #undef ADD_HELPER
02137
02138 #undef CLASS_TYPE
02139 #undef CLASS_TYPE_STR
02140
02141 #include "sc_unsigned_bitref.inc"
02142 #include "sc_unsigned_subref.inc"
02143
02144 #undef CONVERT_LONG
02145 #undef CONVERT_LONG_2
02146 #undef CONVERT_INT64
02147 #undef CONVERT_INT64_2
02148
02149 }
02150
02151
02152