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