1/***************************************************************************** 2 3 Licensed to Accellera Systems Initiative Inc. (Accellera) under one or 4 more contributor license agreements. See the NOTICE file distributed 5 with this work for additional information regarding copyright ownership. 6 Accellera licenses this file to you under the Apache License, Version 2.0 7 (the "License"); you may not use this file except in compliance with the 8 License. You may obtain a copy of the License at 9 10 http://www.apache.org/licenses/LICENSE-2.0 11 12 Unless required by applicable law or agreed to in writing, software 13 distributed under the License is distributed on an "AS IS" BASIS, 14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 15 implied. See the License for the specific language governing 16 permissions and limitations under the License. 17 18 *****************************************************************************/ 19 20/***************************************************************************** 21 22 sc_nbcommon.cpp -- Functions common to both sc_signed and sc_unsigned. 23 This file is included in sc_signed.cpp and 24 sc_unsigned.cpp after the macros are defined accordingly. 25 For example, sc_signed.cpp will first define CLASS_TYPE 26 as sc_signed before including this file. This file like 27 sc_nbfriends.cpp and sc_nbexterns.cpp is created in order 28 to ensure only one version of each function, regardless 29 of the class that they interface to. 30 31 Original Author: Ali Dasdan, Synopsys, Inc. 32 33 *****************************************************************************/ 34 35/***************************************************************************** 36 37 MODIFICATION LOG - modifiers, enter your name, affiliation, date and 38 changes you are making here. 39 40 Name, Affiliation, Date: 41 Description of Modification: 42 43 *****************************************************************************/ 44 45 46// ---------------------------------------------------------------------------- 47// SECTION : Public members 48// ---------------------------------------------------------------------------- 49 50// Create a CLASS_TYPE number with nb bits. 51CLASS_TYPE::CLASS_TYPE( int nb ) : 52 sc_value_base(), sgn(), nbits(), ndigits(), digit() 53{ 54 sgn = default_sign(); 55 if( nb > 0 ) { 56 nbits = num_bits( nb ); 57 } else { 58 invalid_init( "int nb", nb ); 59 sc_core::sc_abort(); // can't recover from here 60 } 61 ndigits = DIV_CEIL(nbits); 62#ifdef SC_MAX_NBITS 63 test_bound(nb); 64#else 65 digit = new sc_digit[ndigits]; 66#endif 67 makezero(); 68} 69 70 71// Create a copy of v with sgn s. v is of the same type. 72CLASS_TYPE::CLASS_TYPE(const CLASS_TYPE& v) : 73 sc_value_base(v), sgn(v.sgn), nbits(v.nbits), ndigits(v.ndigits), digit() 74{ 75#ifndef SC_MAX_NBITS 76 digit = new sc_digit[ndigits]; 77#endif 78 79 vec_copy(ndigits, digit, v.digit); 80} 81 82 83// Create a copy of v where v is of the different type. 84CLASS_TYPE::CLASS_TYPE(const OTHER_CLASS_TYPE& v) : 85 sc_value_base(v), sgn(v.sgn), nbits(num_bits(v.nbits)), ndigits(), digit() 86{ 87#if (IF_SC_SIGNED == 1) 88 ndigits = v.ndigits; 89#else 90 ndigits = DIV_CEIL(nbits); 91#endif 92 93#ifndef SC_MAX_NBITS 94 digit = new sc_digit[ndigits]; 95#endif 96 97 copy_digits(v.nbits, v.ndigits, v.digit); 98} 99 100// Create a copy of v where v is an sign-less instance. 101CLASS_TYPE::CLASS_TYPE(const sc_bv_base& v) : 102 sc_value_base(), sgn(), nbits(), ndigits(), digit() 103{ 104 int nb = v.length(); 105 sgn = default_sign(); 106 if( nb > 0 ) { 107 nbits = num_bits( nb ); 108 } else { 109 invalid_init( "sc_bv_base", nb ); 110 sc_core::sc_abort(); // can't recover from here 111 } 112 ndigits = DIV_CEIL(nbits); 113# ifdef SC_MAX_NBITS 114 test_bound(nb); 115# else 116 digit = new sc_digit[ndigits]; 117# endif 118 makezero(); 119 *this = v; 120} 121 122CLASS_TYPE::CLASS_TYPE(const sc_lv_base& v) : 123 sc_value_base(), sgn(), nbits(), ndigits(), digit() 124{ 125 int nb = v.length(); 126 sgn = default_sign(); 127 if( nb > 0 ) { 128 nbits = num_bits( nb ); 129 } else { 130 invalid_init( "sc_lv_base", nb ); 131 sc_core::sc_abort(); // can't recover from here 132 } 133 ndigits = DIV_CEIL(nbits); 134# ifdef SC_MAX_NBITS 135 test_bound(nb); 136# else 137 digit = new sc_digit[ndigits]; 138# endif 139 makezero(); 140 *this = v; 141} 142 143CLASS_TYPE::CLASS_TYPE(const sc_int_subref_r& v) : 144 sc_value_base(v), sgn(), nbits(), ndigits(), digit() 145{ 146 int nb = v.length(); 147 sgn = default_sign(); 148 if( nb > 0 ) { 149 nbits = num_bits( nb ); 150 } else { 151 invalid_init( "sc_int_subref", nb ); 152 sc_core::sc_abort(); // can't recover from here 153 } 154 ndigits = DIV_CEIL(nbits); 155# ifdef SC_MAX_NBITS 156 test_bound(nb); 157# else 158 digit = new sc_digit[ndigits]; 159# endif 160 makezero(); 161 *this = v.to_uint64(); 162} 163 164CLASS_TYPE::CLASS_TYPE(const sc_uint_subref_r& v) : 165 sc_value_base(v), sgn(), nbits(), ndigits(), digit() 166{ 167 int nb = v.length(); 168 sgn = default_sign(); 169 if( nb > 0 ) { 170 nbits = num_bits( nb ); 171 } else { 172 invalid_init( "sc_uint_subref", nb ); 173 sc_core::sc_abort(); // can't recover from here 174 } 175 ndigits = DIV_CEIL(nbits); 176# ifdef SC_MAX_NBITS 177 test_bound(nb); 178# else 179 digit = new sc_digit[ndigits]; 180# endif 181 makezero(); 182 *this = v.to_uint64(); 183} 184 185CLASS_TYPE::CLASS_TYPE(const sc_signed_subref_r& v) : 186 sc_value_base(v), sgn(), nbits(), ndigits(), digit() 187{ 188 int nb = v.length(); 189 sgn = default_sign(); 190 if( nb > 0 ) { 191 nbits = num_bits( nb ); 192 } else { 193 invalid_init( "sc_signed_subref", nb ); 194 sc_core::sc_abort(); // can't recover from here 195 } 196 ndigits = DIV_CEIL(nbits); 197# ifdef SC_MAX_NBITS 198 test_bound(nb); 199# else 200 digit = new sc_digit[ndigits]; 201# endif 202 makezero(); 203 *this = sc_unsigned(v.m_obj_p, v.m_left, v.m_right); 204} 205 206CLASS_TYPE::CLASS_TYPE(const sc_unsigned_subref_r& v) : 207 sc_value_base(v), sgn(), nbits(), ndigits(), digit() 208{ 209 int nb = v.length(); 210 sgn = default_sign(); 211 if( nb > 0 ) { 212 nbits = num_bits( nb ); 213 } else { 214 invalid_init( "sc_unsigned_subref", nb ); 215 sc_core::sc_abort(); // can't recover from here 216 } 217 ndigits = DIV_CEIL(nbits); 218# ifdef SC_MAX_NBITS 219 test_bound(nb); 220# else 221 digit = new sc_digit[ndigits]; 222# endif 223 makezero(); 224 *this = sc_unsigned(v.m_obj_p, v.m_left, v.m_right); 225} 226 227// ---------------------------------------------------------------------------- 228// SECTION: Public members - Concatenation support. 229// ---------------------------------------------------------------------------- 230 231 232// ---------------------------------------------------------------------------- 233// SECTION: Public members - Assignment operators. 234// ---------------------------------------------------------------------------- 235 236// Assignment from v of the same type. 237const CLASS_TYPE& 238CLASS_TYPE::operator=(const CLASS_TYPE& v) 239{ 240 if (this != &v) { 241 242 sgn = v.sgn; 243 244 if (sgn == SC_ZERO) 245 vec_zero(ndigits, digit); 246 247 else 248 copy_digits(v.nbits, v.ndigits, v.digit); 249 250 } 251 252 return *this; 253} 254 255 256// Assignment from v of the different type. 257const CLASS_TYPE& 258CLASS_TYPE::operator=(const OTHER_CLASS_TYPE& v) 259{ 260 sgn = v.sgn; 261 262 if (sgn == SC_ZERO) 263 vec_zero(ndigits, digit); 264 265 else 266 copy_digits(v.nbits, v.ndigits, v.digit); 267 268 return *this; 269} 270 271 272// Assignment from an sc_unsigned_subref_r 273const CLASS_TYPE& 274CLASS_TYPE::operator=(const sc_unsigned_subref_r& v) 275{ 276 return operator=(sc_unsigned(v)); 277} 278 279 280// Assignment from an sc_signed_subref_r 281const CLASS_TYPE& 282CLASS_TYPE::operator=(const sc_signed_subref_r& v) 283{ 284 return operator=(sc_unsigned(v)); 285} 286 287 288// ---------------------------------------------------------------------------- 289// SECTION: Input and output operators 290// ---------------------------------------------------------------------------- 291 292void 293CLASS_TYPE::scan( ::std::istream& is ) 294{ 295 std::string s; 296 is >> s; 297 *this = s.c_str(); 298} 299 300 301// ---------------------------------------------------------------------------- 302// SECTION: PLUS operators: +, +=, ++ 303// ---------------------------------------------------------------------------- 304 305// Cases to consider when computing u + v: 306// 1. 0 + v = v 307// 2. u + 0 = u 308// 3. if sgn(u) == sgn(v) 309// 3.1 u + v = +(u + v) = sgn(u) * (u + v) 310// 3.2 (-u) + (-v) = -(u + v) = sgn(u) * (u + v) 311// 4. if sgn(u) != sgn(v) 312// 4.1 u + (-v) = u - v = sgn(u) * (u - v) 313// 4.2 (-u) + v = -(u - v) ==> sgn(u) * (u - v) 314// 315// Specialization of above cases for computing ++u or u++: 316// 1. 0 + 1 = 1 317// 3. u + 1 = u + 1 = sgn(u) * (u + 1) 318// 4. (-u) + 1 = -(u - 1) = sgn(u) * (u - 1) 319 320const CLASS_TYPE& 321CLASS_TYPE::operator+=(const CLASS_TYPE& v) 322{ 323 // u = *this 324 325 if (sgn == SC_ZERO) // case 1 326 return (*this = v); 327 328 if (v.sgn == SC_ZERO) // case 2 329 return *this; 330 331 // cases 3 and 4 332 add_on_help(sgn, nbits, ndigits, digit, 333 v.sgn, v.nbits, v.ndigits, v.digit); 334 335 convert_SM_to_2C_to_SM(); 336 337 return *this; 338} 339 340 341const CLASS_TYPE& 342CLASS_TYPE::operator+=(const OTHER_CLASS_TYPE& v) 343{ 344 // u = *this 345 346 if (sgn == SC_ZERO) // case 1 347 return (*this = v); 348 349 if (v.sgn == SC_ZERO) // case 2 350 return *this; 351 352 // cases 3 and 4 353 add_on_help(sgn, nbits, ndigits, digit, 354 v.sgn, v.nbits, v.ndigits, v.digit); 355 356 convert_SM_to_2C_to_SM(); 357 358 return *this; 359} 360 361 362CLASS_TYPE& 363CLASS_TYPE::operator++() // prefix 364{ 365 *this = *this + 1; 366 return *this; 367} 368 369 370const CLASS_TYPE 371CLASS_TYPE::operator++(int) // postfix 372{ 373 // Copy digit into d. 374 375#ifdef SC_MAX_NBITS 376 sc_digit d[MAX_NDIGITS]; 377#else 378 sc_digit *d = new sc_digit[ndigits]; 379#endif 380 381 small_type s = sgn; 382 383 vec_copy(ndigits, d, digit); 384 385 *this = *this + 1; 386 387 return CLASS_TYPE(s, nbits, ndigits, d); 388} 389 390 391const CLASS_TYPE& 392CLASS_TYPE::operator+=(int64 v) 393{ 394 // u = *this 395 396 if (sgn == SC_ZERO) // case 1 397 return (*this = v); 398 399 if (v == 0) // case 2 400 return *this; 401 402 CONVERT_INT64(v); 403 404 // cases 3 and 4 405 add_on_help(sgn, nbits, ndigits, digit, 406 vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd); 407 408 convert_SM_to_2C_to_SM(); 409 410 return *this; 411} 412 413 414const CLASS_TYPE& 415CLASS_TYPE::operator+=(uint64 v) 416{ 417 // u = *this 418 419 if (sgn == SC_ZERO) // case 1 420 return (*this = v); 421 422 if (v == 0) // case 2 423 return *this; 424 425 CONVERT_INT64(v); 426 427 // cases 3 and 4 428 add_on_help(sgn, nbits, ndigits, digit, 429 vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd); 430 431 convert_SM_to_2C_to_SM(); 432 433 return *this; 434} 435 436 437const CLASS_TYPE& 438CLASS_TYPE::operator+=(long v) 439{ 440 // u = *this 441 442 if (sgn == SC_ZERO) // case 1 443 return (*this = v); 444 445 if (v == 0) // case 2 446 return *this; 447 448 CONVERT_LONG(v); 449 450 // cases 3 and 4 451 add_on_help(sgn, nbits, ndigits, digit, 452 vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd); 453 454 convert_SM_to_2C_to_SM(); 455 456 return *this; 457} 458 459 460const CLASS_TYPE& 461CLASS_TYPE::operator+=(unsigned long v) 462{ 463 // u = *this 464 465 if (sgn == SC_ZERO) // case 1 466 return (*this = v); 467 468 if (v == 0) // case 2 469 return *this; 470 471 CONVERT_LONG(v); 472 473 // cases 3 and 4 474 add_on_help(sgn, nbits, ndigits, digit, 475 vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd); 476 477 convert_SM_to_2C_to_SM(); 478 479 return *this; 480} 481 482 483// ---------------------------------------------------------------------------- 484// SECTION: MINUS operators: -, -=, -- 485// ---------------------------------------------------------------------------- 486 487// Cases to consider when computing u + v: 488// 1. u - 0 = u 489// 2. 0 - v = -v 490// 3. if sgn(u) != sgn(v) 491// 3.1 u - (-v) = u + v = sgn(u) * (u + v) 492// 3.2 (-u) - v = -(u + v) ==> sgn(u) * (u + v) 493// 4. if sgn(u) == sgn(v) 494// 4.1 u - v = +(u - v) = sgn(u) * (u - v) 495// 4.2 (-u) - (-v) = -(u - v) = sgn(u) * (u - v) 496// 497// Specialization of above cases for computing --u or u--: 498// 1. 0 - 1 = -1 499// 3. (-u) - 1 = -(u + 1) = sgn(u) * (u + 1) 500// 4. u - 1 = u - 1 = sgn(u) * (u - 1) 501 502const CLASS_TYPE& 503CLASS_TYPE::operator-=(const CLASS_TYPE& v) 504{ 505 // u = *this 506 507 if (v.sgn == SC_ZERO) // case 1 508 return *this; 509 510 if (sgn == SC_ZERO) { // case 2 511 512 sgn = -v.sgn; 513 copy_digits(v.nbits, v.ndigits, v.digit); 514 515 } 516 else { 517 518 // cases 3 and 4 519 add_on_help(sgn, nbits, ndigits, digit, 520 -v.sgn, v.nbits, v.ndigits, v.digit); 521 522 convert_SM_to_2C_to_SM(); 523 524 } 525 526 return *this; 527} 528 529 530const CLASS_TYPE& 531CLASS_TYPE::operator-=(const OTHER_CLASS_TYPE& v) 532{ 533 // u = *this 534 535 if (v.sgn == SC_ZERO) // case 1 536 return *this; 537 538 if (sgn == SC_ZERO) { // case 2 539 540 sgn = -v.sgn; 541 copy_digits(v.nbits, v.ndigits, v.digit); 542 543 } 544 else { 545 546 // cases 3 and 4 547 add_on_help(sgn, nbits, ndigits, digit, 548 -v.sgn, v.nbits, v.ndigits, v.digit); 549 550 convert_SM_to_2C_to_SM(); 551 552 } 553 554 return *this; 555} 556 557 558CLASS_TYPE& 559CLASS_TYPE::operator--() // prefix 560{ 561 *this = *this - 1; 562 return *this; 563} 564 565 566const CLASS_TYPE 567CLASS_TYPE::operator--(int) // postfix 568{ 569 // Copy digit into d. 570 571#ifdef SC_MAX_NBITS 572 sc_digit d[MAX_NDIGITS]; 573#else 574 sc_digit *d = new sc_digit[ndigits]; 575#endif 576 577 small_type s = sgn; 578 579 vec_copy(ndigits, d, digit); 580 581 *this = *this - 1; 582 583 return CLASS_TYPE(s, nbits, ndigits, d); 584} 585 586 587const CLASS_TYPE& 588CLASS_TYPE::operator-=(int64 v) 589{ 590 // u = *this 591 592 if (v == 0) // case 1 593 return *this; 594 595 if (sgn == SC_ZERO) // case 2 596 return (*this = -v); 597 598 CONVERT_INT64(v); 599 600 // cases 3 and 4 601 add_on_help(sgn, nbits, ndigits, digit, 602 -vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd); 603 604 convert_SM_to_2C_to_SM(); 605 606 return *this; 607} 608 609 610const CLASS_TYPE& 611CLASS_TYPE::operator-=(uint64 v) 612{ 613 // u = *this 614 615 if (v == 0) // case 1 616 return *this; 617 618 int64 v2 = (int64) v; 619 620 if (sgn == SC_ZERO) // case 2 621 return (*this = -v2); 622 623 CONVERT_INT64(v); 624 625 // cases 3 and 4 626 add_on_help(sgn, nbits, ndigits, digit, 627 -vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd); 628 629 convert_SM_to_2C_to_SM(); 630 631 return *this; 632} 633 634 635const CLASS_TYPE& 636CLASS_TYPE::operator-=(long v) 637{ 638 // u = *this 639 640 if (v == 0) // case 1 641 return *this; 642 643 if (sgn == SC_ZERO) // case 2 644 return (*this = -v); 645 646 CONVERT_LONG(v); 647 648 // cases 3 and 4 649 add_on_help(sgn, nbits, ndigits, digit, 650 -vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd); 651 652 convert_SM_to_2C_to_SM(); 653 654 return *this; 655} 656 657 658const CLASS_TYPE& 659CLASS_TYPE::operator-=(unsigned long v) 660{ 661 // u = *this 662 663 if (v == 0) // case 1 664 return *this; 665 666 long v2 = (long) v; 667 668 if (sgn == SC_ZERO) // case 2 669 return (*this = -v2); 670 671 CONVERT_LONG(v); 672 673 // cases 3 and 4 674 add_on_help(sgn, nbits, ndigits, digit, 675 -vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd); 676 677 convert_SM_to_2C_to_SM(); 678 679 return *this; 680} 681 682 683// ---------------------------------------------------------------------------- 684// SECTION: MULTIPLICATION operators: *, *= 685// ---------------------------------------------------------------------------- 686 687// Cases to consider when computing u * v: 688// 1. u * 0 = 0 * v = 0 689// 2. 1 * v = v and -1 * v = -v 690// 3. u * 1 = u and u * -1 = -u 691// 4. u * v = u * v 692 693const CLASS_TYPE& 694CLASS_TYPE::operator*=(const CLASS_TYPE& v) 695{ 696 // u = *this 697 698 sgn = mul_signs(sgn, v.sgn); 699 700 if (sgn == SC_ZERO) // case 1 701 vec_zero(ndigits, digit); 702 703 else 704 // cases 2-4 705 MUL_ON_HELPER(sgn, nbits, ndigits, digit, 706 v.nbits, v.ndigits, v.digit); 707 708 return *this; 709} 710 711 712const CLASS_TYPE& 713CLASS_TYPE::operator*=(const OTHER_CLASS_TYPE& v) 714{ 715 // u = *this 716 717 sgn = mul_signs(sgn, v.sgn); 718 719 if (sgn == SC_ZERO) // case 1 720 vec_zero(ndigits, digit); 721 722 else 723 // cases 2-4 724 MUL_ON_HELPER(sgn, nbits, ndigits, digit, 725 v.nbits, v.ndigits, v.digit); 726 727 return *this; 728} 729 730 731const CLASS_TYPE& 732CLASS_TYPE::operator*=(int64 v) 733{ 734 // u = *this 735 736 sgn = mul_signs(sgn, get_sign(v)); 737 738 if (sgn == SC_ZERO) // case 1 739 vec_zero(ndigits, digit); 740 741 else { // cases 2-4 742 743 CONVERT_INT64_2(v); 744 745 MUL_ON_HELPER(sgn, nbits, ndigits, digit, 746 BITS_PER_UINT64, DIGITS_PER_UINT64, vd); 747 748 } 749 750 return *this; 751} 752 753 754const CLASS_TYPE& 755CLASS_TYPE::operator*=(uint64 v) 756{ 757 // u = *this 758 759 sgn = mul_signs(sgn, get_sign(v)); 760 761 if (sgn == SC_ZERO) // case 1 762 vec_zero(ndigits, digit); 763 764 else { // cases 2-4 765 766 CONVERT_INT64_2(v); 767 768 MUL_ON_HELPER(sgn, nbits, ndigits, digit, 769 BITS_PER_UINT64, DIGITS_PER_UINT64, vd); 770 771 } 772 773 return *this; 774} 775 776 777const CLASS_TYPE& 778CLASS_TYPE::operator*=(long v) 779{ 780 // u = *this 781 782 sgn = mul_signs(sgn, get_sign(v)); 783 784 if (sgn == SC_ZERO) // case 1 785 vec_zero(ndigits, digit); 786 787 else { // cases 2-4 788 789 CONVERT_LONG_2(v); 790 791 MUL_ON_HELPER(sgn, nbits, ndigits, digit, 792 BITS_PER_ULONG, DIGITS_PER_ULONG, vd); 793 794 } 795 796 return *this; 797} 798 799 800const CLASS_TYPE& 801CLASS_TYPE::operator*=(unsigned long v) 802{ 803 // u = *this 804 805 sgn = mul_signs(sgn, get_sign(v)); 806 807 if (sgn == SC_ZERO) // case 1 808 vec_zero(ndigits, digit); 809 810 else { // cases 2-4 811 812 CONVERT_LONG_2(v); 813 814 MUL_ON_HELPER(sgn, nbits, ndigits, digit, 815 BITS_PER_ULONG, DIGITS_PER_ULONG, vd); 816 817 } 818 819 return *this; 820} 821 822 823// ---------------------------------------------------------------------------- 824// SECTION: DIVISION operators: /, /= 825// ---------------------------------------------------------------------------- 826 827// Cases to consider when finding the quotient q = floor(u/v): 828// Note that u = q * v + r for r < q. 829// 1. 0 / 0 or u / 0 => error 830// 2. 0 / v => 0 = 0 * v + 0 831// 3. u / v && u = v => u = 1 * u + 0 - u or v can be 1 or -1 832// 4. u / v && u < v => u = 0 * v + u - u can be 1 or -1 833// 5. u / v && u > v => u = q * v + r - v can be 1 or -1 834 835const CLASS_TYPE& 836CLASS_TYPE::operator/=(const CLASS_TYPE& v) 837{ 838 sgn = mul_signs(sgn, v.sgn); 839 840 if (sgn == SC_ZERO) { 841 842 div_by_zero(v.sgn); // case 1 843 vec_zero(ndigits, digit); // case 2 844 845 } 846 else // other cases 847 DIV_ON_HELPER(sgn, nbits, ndigits, digit, 848 v.nbits, v.ndigits, v.digit); 849 850 return *this; 851} 852 853 854const CLASS_TYPE& 855CLASS_TYPE::operator/=(const OTHER_CLASS_TYPE& v) 856{ 857 sgn = mul_signs(sgn, v.sgn); 858 859 if (sgn == SC_ZERO) { 860 861 div_by_zero(v.sgn); // case 1 862 vec_zero(ndigits, digit); // case 2 863 864 } 865 else // other cases 866 DIV_ON_HELPER(sgn, nbits, ndigits, digit, 867 v.nbits, v.ndigits, v.digit); 868 869 return *this; 870} 871 872 873const CLASS_TYPE& 874CLASS_TYPE::operator/=(int64 v) 875{ 876 // u = *this 877 878 sgn = mul_signs(sgn, get_sign(v)); 879 880 if (sgn == SC_ZERO) { 881 882 div_by_zero(v); // case 1 883 vec_zero(ndigits, digit); // case 2 884 885 } 886 else { 887 888 CONVERT_INT64_2(v); 889 890 // other cases 891 DIV_ON_HELPER(sgn, nbits, ndigits, digit, 892 BITS_PER_UINT64, DIGITS_PER_UINT64, vd); 893 894 } 895 896 return *this; 897} 898 899 900const CLASS_TYPE& 901CLASS_TYPE::operator/=(uint64 v) 902{ 903 // u = *this 904 905 sgn = mul_signs(sgn, get_sign(v)); 906 907 if (sgn == SC_ZERO) { 908 909 div_by_zero(v); // case 1 910 vec_zero(ndigits, digit); // case 2 911 912 } 913 else { 914 915 CONVERT_INT64_2(v); 916 917 // other cases 918 DIV_ON_HELPER(sgn, nbits, ndigits, digit, 919 BITS_PER_UINT64, DIGITS_PER_UINT64, vd); 920 921 } 922 923 return *this; 924} 925 926 927const CLASS_TYPE& 928CLASS_TYPE::operator/=(long v) 929{ 930 // u = *this 931 932 sgn = mul_signs(sgn, get_sign(v)); 933 934 if (sgn == SC_ZERO) { 935 936 div_by_zero(v); // case 1 937 vec_zero(ndigits, digit); // case 2 938 939 } 940 else { 941 942 CONVERT_LONG_2(v); 943 944 // other cases 945 DIV_ON_HELPER(sgn, nbits, ndigits, digit, 946 BITS_PER_ULONG, DIGITS_PER_ULONG, vd); 947 948 } 949 950 return *this; 951} 952 953 954const CLASS_TYPE& 955CLASS_TYPE::operator/=(unsigned long v) 956{ 957 // u = *this 958 959 sgn = mul_signs(sgn, get_sign(v)); 960 961 if (sgn == SC_ZERO) { 962 963 div_by_zero(v); // case 1 964 vec_zero(ndigits, digit); // case 2 965 966 } 967 else { 968 969 CONVERT_LONG_2(v); 970 971 // other cases 972 DIV_ON_HELPER(sgn, nbits, ndigits, digit, 973 BITS_PER_ULONG, DIGITS_PER_ULONG, vd); 974 975 } 976 977 return *this; 978} 979 980 981// ---------------------------------------------------------------------------- 982// SECTION: MOD operators: %, %=. 983// ---------------------------------------------------------------------------- 984 985// Cases to consider when finding the remainder r = u % v: 986// Note that u = q * v + r for r < q. 987// 1. 0 % 0 or u % 0 => error 988// 2. 0 % v => 0 = 0 * v + 0 989// 3. u % v && u = v => u = 1 * u + 0 - u or v can be 1 or -1 990// 4. u % v && u < v => u = 0 * v + u - u can be 1 or -1 991// 5. u % v && u > v => u = q * v + r - v can be 1 or -1 992 993const CLASS_TYPE& 994CLASS_TYPE::operator%=(const CLASS_TYPE& v) 995{ 996 if ((sgn == SC_ZERO) || (v.sgn == SC_ZERO)) { 997 998 div_by_zero(v.sgn); // case 1 999 vec_zero(ndigits, digit); // case 2 1000 1001 } 1002 else // other cases 1003 MOD_ON_HELPER(sgn, nbits, ndigits, digit, 1004 v.nbits, v.ndigits, v.digit); 1005 1006 return *this; 1007} 1008 1009 1010const CLASS_TYPE& 1011CLASS_TYPE::operator%=(const OTHER_CLASS_TYPE& v) 1012{ 1013 if ((sgn == SC_ZERO) || (v.sgn == SC_ZERO)) { 1014 1015 div_by_zero(v.sgn); // case 1 1016 vec_zero(ndigits, digit); // case 2 1017 1018 } 1019 else // other cases 1020 MOD_ON_HELPER(sgn, nbits, ndigits, digit, 1021 v.nbits, v.ndigits, v.digit); 1022 1023 return *this; 1024} 1025 1026 1027const CLASS_TYPE& 1028CLASS_TYPE::operator%=(int64 v) 1029{ 1030 small_type vs = get_sign(v); 1031 1032 if ((sgn == SC_ZERO) || (vs == SC_ZERO)) { 1033 1034 div_by_zero(v); // case 1 1035 vec_zero(ndigits, digit); // case 2 1036 1037 } 1038 else { 1039 1040 CONVERT_INT64_2(v); 1041 1042 // other cases 1043 MOD_ON_HELPER(sgn, nbits, ndigits, digit, 1044 BITS_PER_UINT64, DIGITS_PER_UINT64, vd); 1045 1046 } 1047 1048 return *this; 1049} 1050 1051 1052const CLASS_TYPE& 1053CLASS_TYPE::operator%=(uint64 v) 1054{ 1055 if ((sgn == SC_ZERO) || (v == 0)) { 1056 1057 div_by_zero(v); // case 1 1058 vec_zero(ndigits, digit); // case 2 1059 1060 } 1061 else { 1062 1063 CONVERT_INT64_2(v); 1064 1065 // other cases 1066 MOD_ON_HELPER(sgn, nbits, ndigits, digit, 1067 BITS_PER_UINT64, DIGITS_PER_UINT64, vd); 1068 1069 } 1070 1071 return *this; 1072} 1073 1074 1075const CLASS_TYPE& 1076CLASS_TYPE::operator%=(long v) 1077{ 1078 small_type vs = get_sign(v); 1079 1080 if ((sgn == SC_ZERO) || (vs == SC_ZERO)) { 1081 1082 div_by_zero(v); // case 1 1083 vec_zero(ndigits, digit); // case 2 1084 1085 } 1086 else { 1087 1088 CONVERT_LONG_2(v); 1089 1090 // other cases 1091 MOD_ON_HELPER(sgn, nbits, ndigits, digit, 1092 BITS_PER_ULONG, DIGITS_PER_ULONG, vd); 1093 1094 } 1095 1096 return *this; 1097} 1098 1099 1100const CLASS_TYPE& 1101CLASS_TYPE::operator%=(unsigned long v) 1102{ 1103 if ((sgn == SC_ZERO) || (v == 0)) { 1104 1105 div_by_zero(v); // case 1 1106 vec_zero(ndigits, digit); // case 2 1107 1108 } 1109 else { 1110 1111 CONVERT_LONG_2(v); 1112 1113 // other cases 1114 MOD_ON_HELPER(sgn, nbits, ndigits, digit, 1115 BITS_PER_ULONG, DIGITS_PER_ULONG, vd); 1116 1117 } 1118 1119 return *this; 1120} 1121 1122 1123// ---------------------------------------------------------------------------- 1124// SECTION: Bitwise AND operators: &, &= 1125// ---------------------------------------------------------------------------- 1126 1127// Cases to consider when computing u & v: 1128// 1. u & 0 = 0 & v = 0 1129// 2. u & v => sgn = + 1130// 3. (-u) & (-v) => sgn = - 1131// 4. u & (-v) => sgn = + 1132// 5. (-u) & v => sgn = + 1133 1134const CLASS_TYPE& 1135CLASS_TYPE::operator&=(const CLASS_TYPE& v) 1136{ 1137 // u = *this 1138 1139 if ((sgn == SC_ZERO) || (v.sgn == SC_ZERO)) // case 1 1140 makezero(); 1141 1142 else { // other cases 1143 1144 and_on_help(sgn, nbits, ndigits, digit, 1145 v.sgn, v.nbits, v.ndigits, v.digit); 1146 1147 convert_2C_to_SM(); 1148 1149 } 1150 1151 return *this; 1152} 1153 1154 1155const CLASS_TYPE& 1156CLASS_TYPE::operator&=(const OTHER_CLASS_TYPE& v) 1157{ 1158 // u = *this 1159 1160 if ((sgn == SC_ZERO) || (v.sgn == SC_ZERO)) // case 1 1161 makezero(); 1162 1163 else { // other cases 1164 1165 and_on_help(sgn, nbits, ndigits, digit, 1166 v.sgn, v.nbits, v.ndigits, v.digit); 1167 1168 convert_2C_to_SM(); 1169 1170 } 1171 1172 return *this; 1173} 1174 1175 1176const CLASS_TYPE& 1177CLASS_TYPE::operator&=(int64 v) 1178{ 1179 // u = *this 1180 1181 if ((sgn == SC_ZERO) || (v == 0)) // case 1 1182 makezero(); 1183 1184 else { // other cases 1185 1186 CONVERT_INT64(v); 1187 1188 and_on_help(sgn, nbits, ndigits, digit, 1189 vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd); 1190 1191 convert_2C_to_SM(); 1192 1193 } 1194 1195 return *this; 1196} 1197 1198 1199const CLASS_TYPE& 1200CLASS_TYPE::operator&=(uint64 v) 1201{ 1202 // u = *this 1203 1204 if ((sgn == SC_ZERO) || (v == 0)) // case 1 1205 makezero(); 1206 1207 else { // other cases 1208 1209 CONVERT_INT64(v); 1210 1211 and_on_help(sgn, nbits, ndigits, digit, 1212 vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd); 1213 1214 convert_2C_to_SM(); 1215 1216 } 1217 1218 return *this; 1219} 1220 1221 1222const CLASS_TYPE& 1223CLASS_TYPE::operator&=(long v) 1224{ 1225 // u = *this 1226 1227 if ((sgn == SC_ZERO) || (v == 0)) // case 1 1228 makezero(); 1229 1230 else { // other cases 1231 1232 CONVERT_LONG(v); 1233 1234 and_on_help(sgn, nbits, ndigits, digit, 1235 vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd); 1236 1237 convert_2C_to_SM(); 1238 1239 } 1240 1241 return *this; 1242} 1243 1244 1245const CLASS_TYPE& 1246CLASS_TYPE::operator&=(unsigned long v) 1247{ 1248 // u = *this 1249 1250 if ((sgn == SC_ZERO) || (v == 0)) // case 1 1251 makezero(); 1252 1253 else { // other cases 1254 1255 CONVERT_LONG(v); 1256 1257 and_on_help(sgn, nbits, ndigits, digit, 1258 vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd); 1259 1260 convert_2C_to_SM(); 1261 1262 } 1263 1264 return *this; 1265} 1266 1267 1268// ---------------------------------------------------------------------------- 1269// SECTION: Bitwise OR operators: |, |= 1270// ---------------------------------------------------------------------------- 1271 1272// Cases to consider when computing u | v: 1273// 1. u | 0 = u 1274// 2. 0 | v = v 1275// 3. u | v => sgn = + 1276// 4. (-u) | (-v) => sgn = - 1277// 5. u | (-v) => sgn = - 1278// 6. (-u) | v => sgn = - 1279 1280const CLASS_TYPE& 1281CLASS_TYPE::operator|=(const CLASS_TYPE& v) 1282{ 1283 if (v.sgn == SC_ZERO) // case 1 1284 return *this; 1285 1286 if (sgn == SC_ZERO) // case 2 1287 return (*this = v); 1288 1289 // other cases 1290 or_on_help(sgn, nbits, ndigits, digit, 1291 v.sgn, v.nbits, v.ndigits, v.digit); 1292 1293 convert_2C_to_SM(); 1294 1295 return *this; 1296} 1297 1298 1299const CLASS_TYPE& 1300CLASS_TYPE::operator|=(const OTHER_CLASS_TYPE& v) 1301{ 1302 if (v.sgn == SC_ZERO) // case 1 1303 return *this; 1304 1305 if (sgn == SC_ZERO) // case 2 1306 return (*this = v); 1307 1308 // other cases 1309 or_on_help(sgn, nbits, ndigits, digit, 1310 v.sgn, v.nbits, v.ndigits, v.digit); 1311 1312 convert_2C_to_SM(); 1313 1314 return *this; 1315} 1316 1317 1318const CLASS_TYPE& 1319CLASS_TYPE::operator|=(int64 v) 1320{ 1321 if (v == 0) // case 1 1322 return *this; 1323 1324 if (sgn == SC_ZERO) // case 2 1325 return (*this = v); 1326 1327 // other cases 1328 1329 CONVERT_INT64(v); 1330 1331 or_on_help(sgn, nbits, ndigits, digit, 1332 vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd); 1333 1334 convert_2C_to_SM(); 1335 1336 return *this; 1337} 1338 1339 1340const CLASS_TYPE& 1341CLASS_TYPE::operator|=(uint64 v) 1342{ 1343 if (v == 0) // case 1 1344 return *this; 1345 1346 if (sgn == SC_ZERO) // case 2 1347 return (*this = v); 1348 1349 // other cases 1350 1351 CONVERT_INT64(v); 1352 1353 or_on_help(sgn, nbits, ndigits, digit, 1354 vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd); 1355 1356 convert_2C_to_SM(); 1357 1358 return *this; 1359} 1360 1361 1362const CLASS_TYPE& 1363CLASS_TYPE::operator|=(long v) 1364{ 1365 if (v == 0) // case 1 1366 return *this; 1367 1368 if (sgn == SC_ZERO) // case 2 1369 return (*this = v); 1370 1371 // other cases 1372 1373 CONVERT_LONG(v); 1374 1375 or_on_help(sgn, nbits, ndigits, digit, 1376 vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd); 1377 1378 convert_2C_to_SM(); 1379 1380 return *this; 1381} 1382 1383 1384const CLASS_TYPE& 1385CLASS_TYPE::operator|=(unsigned long v) 1386{ 1387 if (v == 0) // case 1 1388 return *this; 1389 1390 if (sgn == SC_ZERO) // case 2 1391 return (*this = v); 1392 1393 // other cases 1394 1395 CONVERT_LONG(v); 1396 1397 or_on_help(sgn, nbits, ndigits, digit, 1398 vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd); 1399 1400 convert_2C_to_SM(); 1401 1402 return *this; 1403} 1404 1405 1406// ---------------------------------------------------------------------------- 1407// SECTION: Bitwise XOR operators: ^, ^= 1408// ---------------------------------------------------------------------------- 1409 1410// Cases to consider when computing u ^ v: 1411// Note that u ^ v = (~u & v) | (u & ~v). 1412// 1. u ^ 0 = u 1413// 2. 0 ^ v = v 1414// 3. u ^ v => sgn = + 1415// 4. (-u) ^ (-v) => sgn = - 1416// 5. u ^ (-v) => sgn = - 1417// 6. (-u) ^ v => sgn = + 1418 1419const CLASS_TYPE& 1420CLASS_TYPE::operator^=(const CLASS_TYPE& v) 1421{ 1422 // u = *this 1423 1424 if (v.sgn == SC_ZERO) // case 1 1425 return *this; 1426 1427 if (sgn == SC_ZERO) // case 2 1428 return (*this = v); 1429 1430 // other cases 1431 xor_on_help(sgn, nbits, ndigits, digit, 1432 v.sgn, v.nbits, v.ndigits, v.digit); 1433 1434 convert_2C_to_SM(); 1435 1436 return *this; 1437} 1438 1439 1440const CLASS_TYPE& 1441CLASS_TYPE::operator^=(const OTHER_CLASS_TYPE& v) 1442{ 1443 // u = *this 1444 1445 if (v.sgn == SC_ZERO) // case 1 1446 return *this; 1447 1448 if (sgn == SC_ZERO) // case 2 1449 return (*this = v); 1450 1451 // other cases 1452 xor_on_help(sgn, nbits, ndigits, digit, 1453 v.sgn, v.nbits, v.ndigits, v.digit); 1454 1455 convert_2C_to_SM(); 1456 1457 return *this; 1458} 1459 1460 1461const CLASS_TYPE& 1462CLASS_TYPE::operator^=(int64 v) 1463{ 1464 if (v == 0) // case 1 1465 return *this; 1466 1467 if (sgn == SC_ZERO) // case 2 1468 return (*this = v); 1469 1470 // other cases 1471 1472 CONVERT_INT64(v); 1473 1474 xor_on_help(sgn, nbits, ndigits, digit, 1475 vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd); 1476 1477 convert_2C_to_SM(); 1478 1479 return *this; 1480} 1481 1482 1483const CLASS_TYPE& 1484CLASS_TYPE::operator^=(uint64 v) 1485{ 1486 if (v == 0) // case 1 1487 return *this; 1488 1489 if (sgn == SC_ZERO) // case 2 1490 return (*this = v); 1491 1492 // other cases 1493 1494 CONVERT_INT64(v); 1495 1496 xor_on_help(sgn, nbits, ndigits, digit, 1497 vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd); 1498 1499 convert_2C_to_SM(); 1500 1501 return *this; 1502} 1503 1504 1505const CLASS_TYPE& 1506CLASS_TYPE::operator^=(long v) 1507{ 1508 if (v == 0) // case 1 1509 return *this; 1510 1511 if (sgn == SC_ZERO) // case 2 1512 return (*this = v); 1513 1514 // other cases 1515 1516 CONVERT_LONG(v); 1517 1518 xor_on_help(sgn, nbits, ndigits, digit, 1519 vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd); 1520 1521 convert_2C_to_SM(); 1522 1523 return *this; 1524} 1525 1526 1527const CLASS_TYPE& 1528CLASS_TYPE::operator^=(unsigned long v) 1529{ 1530 if (v == 0) // case 1 1531 return *this; 1532 1533 if (sgn == SC_ZERO) // case 2 1534 return (*this = v); 1535 1536 // other cases 1537 1538 CONVERT_LONG(v); 1539 1540 xor_on_help(sgn, nbits, ndigits, digit, 1541 vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd); 1542 1543 convert_2C_to_SM(); 1544 1545 return *this; 1546} 1547 1548 1549// ---------------------------------------------------------------------------- 1550// SECTION: Bitwise NOT operator: ~ 1551// ---------------------------------------------------------------------------- 1552 1553CLASS_TYPE 1554operator~(const CLASS_TYPE& u) 1555{ 1556 small_type s = u.sgn; 1557 1558 if (s == SC_ZERO) { 1559 1560 sc_digit d = 1; 1561 return CLASS_TYPE(SC_NEG, u.nbits, 1, &d, false); 1562 1563 } 1564 1565 int nd = u.ndigits; 1566 1567#ifdef SC_MAX_NBITS 1568 sc_digit d[MAX_NDIGITS]; 1569#else 1570 sc_digit *d = new sc_digit[nd]; 1571#endif 1572 1573 vec_copy(nd, d, u.digit); 1574 1575 if (s == SC_POS) { 1576 1577 s = SC_NEG; 1578 vec_add_small_on(nd, d, 1); 1579 1580 } 1581 else { 1582 1583 s = SC_POS; 1584 vec_sub_small_on(nd, d, 1); 1585 1586 if (check_for_zero(nd, d)) 1587 s = SC_ZERO; 1588 1589 } 1590 1591 return CLASS_TYPE(s, u.nbits, nd, d); 1592} 1593 1594 1595// ---------------------------------------------------------------------------- 1596// SECTION: LEFT SHIFT operators: <<, <<= 1597// ---------------------------------------------------------------------------- 1598 1599CLASS_TYPE 1600operator<<(const CLASS_TYPE& u, const CLASS_TYPE& v) 1601{ 1602 if (v.sgn == SC_ZERO) 1603 return CLASS_TYPE(u); 1604 1605#ifdef SC_SIGNED 1606 if (v.sgn == SC_NEG) 1607 return CLASS_TYPE(u); 1608#endif 1609 1610 return operator<<(u, v.to_ulong()); 1611} 1612 1613 1614const CLASS_TYPE& 1615CLASS_TYPE::operator<<=(const CLASS_TYPE& v) 1616{ 1617 if (v.sgn == SC_ZERO) 1618 return *this; 1619 1620#ifdef SC_SIGNED 1621 if (v.sgn == SC_NEG) 1622 return *this; 1623#endif 1624 1625 return operator<<=(v.to_ulong()); 1626} 1627 1628 1629const CLASS_TYPE& 1630CLASS_TYPE::operator<<=(const OTHER_CLASS_TYPE& v) 1631{ 1632 if (v.sgn == SC_ZERO) 1633 return *this; 1634 1635#ifdef SC_UNSIGNED 1636 if (v.sgn == SC_NEG) 1637 return *this; 1638#endif 1639 1640 return operator<<=(v.to_ulong()); 1641} 1642 1643 1644CLASS_TYPE 1645operator<<(const CLASS_TYPE& u, int64 v) 1646{ 1647 if (v <= 0) 1648 return CLASS_TYPE(u); 1649 1650 return operator<<(u, (unsigned long) v); 1651} 1652 1653 1654CLASS_TYPE 1655operator<<(const CLASS_TYPE& u, uint64 v) 1656{ 1657 if (v == 0) 1658 return CLASS_TYPE(u); 1659 1660 return operator<<(u, (unsigned long) v); 1661} 1662 1663 1664const CLASS_TYPE& 1665CLASS_TYPE::operator<<=(int64 v) 1666{ 1667 if (v <= 0) 1668 return *this; 1669 1670 return operator<<=((unsigned long) v); 1671} 1672 1673 1674const CLASS_TYPE& 1675CLASS_TYPE::operator<<=(uint64 v) 1676{ 1677 if (v == 0) 1678 return *this; 1679 1680 return operator<<=((unsigned long) v); 1681} 1682 1683 1684CLASS_TYPE 1685operator<<(const CLASS_TYPE& u, long v) 1686{ 1687 if (v <= 0) 1688 return CLASS_TYPE(u); 1689 1690 return operator<<(u, (unsigned long) v); 1691} 1692 1693CLASS_TYPE 1694operator<<(const CLASS_TYPE& u, unsigned long v) 1695{ 1696 if (v == 0) 1697 return CLASS_TYPE(u); 1698 1699 if (u.sgn == SC_ZERO) 1700 return CLASS_TYPE(u); 1701 1702 int nb = u.nbits + v; 1703 int nd = DIV_CEIL(nb); 1704 1705#ifdef SC_MAX_NBITS 1706 test_bound(nb); 1707 sc_digit d[MAX_NDIGITS]; 1708#else 1709 sc_digit *d = new sc_digit[nd]; 1710#endif 1711 1712 vec_copy_and_zero(nd, d, u.ndigits, u.digit); 1713 1714 convert_SM_to_2C(u.sgn, nd, d); 1715 1716 vec_shift_left(nd, d, v); 1717 1718 small_type s = convert_signed_2C_to_SM(nb, nd, d); 1719 1720 return CLASS_TYPE(s, nb, nd, d); 1721} 1722 1723 1724const CLASS_TYPE& 1725CLASS_TYPE::operator<<=(long v) 1726{ 1727 if (v <= 0) 1728 return *this; 1729 1730 return operator<<=((unsigned long) v); 1731} 1732 1733 1734const CLASS_TYPE& 1735CLASS_TYPE::operator<<=(unsigned long v) 1736{ 1737 if (v == 0) 1738 return *this; 1739 1740 if (sgn == SC_ZERO) 1741 return *this; 1742 1743 convert_SM_to_2C(); 1744 1745 vec_shift_left(ndigits, digit, v); 1746 1747 convert_2C_to_SM(); 1748 1749 return *this; 1750} 1751 1752 1753// ---------------------------------------------------------------------------- 1754// SECTION: RIGHT SHIFT operators: >>, >>= 1755// ---------------------------------------------------------------------------- 1756 1757CLASS_TYPE 1758operator>>(const CLASS_TYPE& u, const CLASS_TYPE& v) 1759{ 1760 if (v.sgn == SC_ZERO) 1761 return CLASS_TYPE(u); 1762 1763#ifdef SC_SIGNED 1764 if (v.sgn == SC_NEG) 1765 return CLASS_TYPE(u); 1766#endif 1767 1768 return operator>>(u, v.to_long()); 1769} 1770 1771 1772const CLASS_TYPE& 1773CLASS_TYPE::operator>>=(const CLASS_TYPE& v) 1774{ 1775 if (v.sgn == SC_ZERO) 1776 return *this; 1777 1778#ifdef SC_SIGNED 1779 if (v.sgn == SC_NEG) 1780 return *this; 1781#endif 1782 1783 return operator>>=(v.to_long()); 1784} 1785 1786 1787const CLASS_TYPE& 1788CLASS_TYPE::operator>>=(const OTHER_CLASS_TYPE& v) 1789{ 1790 if (v.sgn == SC_ZERO) 1791 return *this; 1792 1793#ifdef SC_UNSIGNED 1794 if (v.sgn == SC_NEG) 1795 return *this; 1796#endif 1797 1798 return operator>>=(v.to_ulong()); 1799} 1800 1801 1802CLASS_TYPE 1803operator>>(const CLASS_TYPE& u, int64 v) 1804{ 1805 if (v <= 0) 1806 return CLASS_TYPE(u); 1807 1808 return operator>>(u, (unsigned long) v); 1809} 1810 1811 1812CLASS_TYPE 1813operator>>(const CLASS_TYPE& u, uint64 v) 1814{ 1815 if (v == 0) 1816 return CLASS_TYPE(u); 1817 1818 return operator>>(u, (unsigned long) v); 1819} 1820 1821const CLASS_TYPE& 1822CLASS_TYPE::operator>>=(int64 v) 1823{ 1824 if (v <= 0) 1825 return *this; 1826 1827 return operator>>=((unsigned long) v); 1828} 1829 1830 1831const CLASS_TYPE& 1832CLASS_TYPE::operator>>=(uint64 v) 1833{ 1834 if (v == 0) 1835 return *this; 1836 1837 return operator>>=((unsigned long) v); 1838} 1839 1840 1841CLASS_TYPE 1842operator>>(const CLASS_TYPE& u, long v) 1843{ 1844 if (v <= 0) 1845 return CLASS_TYPE(u); 1846 1847 return operator>>(u, (unsigned long) v); 1848} 1849 1850 1851CLASS_TYPE 1852operator>>(const CLASS_TYPE& u, unsigned long v) 1853{ 1854 if (v == 0) 1855 return CLASS_TYPE(u); 1856 1857 if (u.sgn == SC_ZERO) 1858 return CLASS_TYPE(u); 1859 1860 int nb = u.nbits; 1861 int nd = u.ndigits; 1862 1863#ifdef SC_MAX_NBITS 1864 sc_digit d[MAX_NDIGITS]; 1865#else 1866 sc_digit *d = new sc_digit[nd]; 1867#endif 1868 1869 vec_copy(nd, d, u.digit); 1870 1871 convert_SM_to_2C(u.sgn, nd, d); 1872 1873 if (u.sgn == SC_NEG) 1874 vec_shift_right(nd, d, v, DIGIT_MASK); 1875 else 1876 vec_shift_right(nd, d, v, 0); 1877 1878 small_type s = convert_signed_2C_to_SM(nb, nd, d); 1879 1880 return CLASS_TYPE(s, nb, nd, d); 1881} 1882 1883 1884const CLASS_TYPE& 1885CLASS_TYPE::operator>>=(long v) 1886{ 1887 if (v <= 0) 1888 return *this; 1889 1890 return operator>>=((unsigned long) v); 1891} 1892 1893 1894const CLASS_TYPE& 1895CLASS_TYPE::operator>>=(unsigned long v) 1896{ 1897 if (v == 0) 1898 return *this; 1899 1900 if (sgn == SC_ZERO) 1901 return *this; 1902 1903 convert_SM_to_2C(); 1904 1905 if (sgn == SC_NEG) 1906 vec_shift_right(ndigits, digit, v, DIGIT_MASK); 1907 else 1908 vec_shift_right(ndigits, digit, v, 0); 1909 1910 convert_2C_to_SM(); 1911 1912 return *this; 1913} 1914 1915 1916// ---------------------------------------------------------------------------- 1917// SECTION: EQUAL TO operator: == 1918// ---------------------------------------------------------------------------- 1919 1920// Defined in the sc_signed.cpp and sc_unsigned.cpp. 1921 1922 1923// ---------------------------------------------------------------------------- 1924// SECTION: NOT_EQUAL operator: != 1925// ---------------------------------------------------------------------------- 1926 1927bool 1928operator!=(const CLASS_TYPE& u, const CLASS_TYPE& v) 1929{ 1930 return (! operator==(u, v)); 1931} 1932 1933 1934bool 1935operator!=(const CLASS_TYPE& u, int64 v) 1936{ 1937 return (! operator==(u, v)); 1938} 1939 1940 1941bool 1942operator!=(int64 u, const CLASS_TYPE& v) 1943{ 1944 return (! operator==(u, v)); 1945} 1946 1947 1948bool 1949operator!=(const CLASS_TYPE& u, uint64 v) 1950{ 1951 return (! operator==(u, v)); 1952} 1953 1954 1955bool 1956operator!=(uint64 u, const CLASS_TYPE& v) 1957{ 1958 return (! operator==(u, v)); 1959} 1960 1961 1962bool 1963operator!=(const CLASS_TYPE& u, long v) 1964{ 1965 return (! operator==(u, v)); 1966} 1967 1968 1969bool 1970operator!=(long u, const CLASS_TYPE& v) 1971{ 1972 return (! operator==(u, v)); 1973} 1974 1975 1976bool 1977operator!=(const CLASS_TYPE& u, unsigned long v) 1978{ 1979 return (! operator==(u, v)); 1980} 1981 1982 1983bool 1984operator!=(unsigned long u, const CLASS_TYPE& v) 1985{ 1986 return (! operator==(u, v)); 1987} 1988 1989 1990// ---------------------------------------------------------------------------- 1991// SECTION: LESS THAN operator: < 1992// ---------------------------------------------------------------------------- 1993 1994// Defined in the sc_signed.cpp and sc_unsigned.cpp. 1995 1996 1997// ---------------------------------------------------------------------------- 1998// SECTION: LESS THAN or EQUAL operator: <= 1999// ---------------------------------------------------------------------------- 2000 2001bool 2002operator<=(const CLASS_TYPE& u, const CLASS_TYPE& v) 2003{ 2004 return (operator<(u, v) || operator==(u, v)); 2005} 2006 2007 2008bool 2009operator<=(const CLASS_TYPE& u, int64 v) 2010{ 2011 return (operator<(u, v) || operator==(u, v)); 2012} 2013 2014 2015bool 2016operator<=(int64 u, const CLASS_TYPE& v) 2017{ 2018 return (operator<(u, v) || operator==(u, v)); 2019} 2020 2021 2022bool 2023operator<=(const CLASS_TYPE& u, uint64 v) 2024{ 2025 return (operator<(u, v) || operator==(u, v)); 2026} 2027 2028 2029bool 2030operator<=(uint64 u, const CLASS_TYPE& v) 2031{ 2032 return (operator<(u, v) || operator==(u, v)); 2033} 2034 2035 2036bool 2037operator<=(const CLASS_TYPE& u, long v) 2038{ 2039 return (operator<(u, v) || operator==(u, v)); 2040} 2041 2042 2043bool 2044operator<=(long u, const CLASS_TYPE& v) 2045{ 2046 return (operator<(u, v) || operator==(u, v)); 2047} 2048 2049 2050bool 2051operator<=(const CLASS_TYPE& u, unsigned long v) 2052{ 2053 return (operator<(u, v) || operator==(u, v)); 2054} 2055 2056 2057bool 2058operator<=(unsigned long u, const CLASS_TYPE& v) 2059{ 2060 return (operator<(u, v) || operator==(u, v)); 2061} 2062 2063 2064// ---------------------------------------------------------------------------- 2065// SECTION: GREATER THAN operator: > 2066// ---------------------------------------------------------------------------- 2067 2068bool 2069operator>(const CLASS_TYPE& u, const CLASS_TYPE& v) 2070{ 2071 return (! (operator<=(u, v))); 2072} 2073 2074 2075bool 2076operator>(const CLASS_TYPE& u, int64 v) 2077{ 2078 return (! (operator<=(u, v))); 2079} 2080 2081 2082bool 2083operator>(int64 u, const CLASS_TYPE& v) 2084{ 2085 return (! (operator<=(u, v))); 2086} 2087 2088 2089bool 2090operator>(const CLASS_TYPE& u, uint64 v) 2091{ 2092 return (! (operator<=(u, v))); 2093} 2094 2095 2096bool 2097operator>(uint64 u, const CLASS_TYPE& v) 2098{ 2099 return (! (operator<=(u, v))); 2100} 2101 2102 2103bool 2104operator>(const CLASS_TYPE& u, long v) 2105{ 2106 return (! (operator<=(u, v))); 2107} 2108 2109 2110bool 2111operator>(long u, const CLASS_TYPE& v) 2112{ 2113 return (! (operator<=(u, v))); 2114} 2115 2116 2117bool 2118operator>(const CLASS_TYPE& u, unsigned long v) 2119{ 2120 return (! (operator<=(u, v))); 2121} 2122 2123 2124bool 2125operator>(unsigned long u, const CLASS_TYPE& v) 2126{ 2127 return (! (operator<=(u, v))); 2128} 2129 2130 2131// ---------------------------------------------------------------------------- 2132// SECTION: GREATER THAN or EQUAL operator: >= 2133// ---------------------------------------------------------------------------- 2134 2135bool 2136operator>=(const CLASS_TYPE& u, const CLASS_TYPE& v) 2137{ 2138 return (! (operator<(u, v))); 2139} 2140 2141 2142bool 2143operator>=(const CLASS_TYPE& u, int64 v) 2144{ 2145 return (! (operator<(u, v))); 2146} 2147 2148 2149bool 2150operator>=(int64 u, const CLASS_TYPE& v) 2151{ 2152 return (! (operator<(u, v))); 2153} 2154 2155 2156bool 2157operator>=(const CLASS_TYPE& u, uint64 v) 2158{ 2159 return (! (operator<(u, v))); 2160} 2161 2162 2163bool 2164operator>=(uint64 u, const CLASS_TYPE& v) 2165{ 2166 return (! (operator<(u, v))); 2167} 2168 2169 2170bool 2171operator>=(const CLASS_TYPE& u, long v) 2172{ 2173 return (! (operator<(u, v))); 2174} 2175 2176 2177bool 2178operator>=(long u, const CLASS_TYPE& v) 2179{ 2180 return (! (operator<(u, v))); 2181} 2182 2183 2184bool 2185operator>=(const CLASS_TYPE& u, unsigned long v) 2186{ 2187 return (! (operator<(u, v))); 2188} 2189 2190 2191bool 2192operator>=(unsigned long u, const CLASS_TYPE& v) 2193{ 2194 return (! (operator<(u, v))); 2195} 2196 2197 2198// ---------------------------------------------------------------------------- 2199// SECTION: Public members - Other utils. 2200// ---------------------------------------------------------------------------- 2201 2202// Convert to int64, long, or int. 2203#define TO_INTX(RET_TYPE, UP_RET_TYPE) \ 2204 \ 2205if (sgn == SC_ZERO) \ 2206return 0; \ 2207 \ 2208int vnd = sc_min((int)DIGITS_PER_ ## UP_RET_TYPE, ndigits); \ 2209 \ 2210RET_TYPE v = 0; \ 2211while (--vnd >= 0) \ 2212v = (v << BITS_PER_DIGIT) + digit[vnd]; \ 2213 \ 2214if (sgn == SC_NEG) \ 2215return -v; \ 2216else \ 2217return v; 2218 2219 2220int64 2221CLASS_TYPE::to_int64() const 2222{ 2223 TO_INTX(int64, INT64); 2224} 2225 2226 2227long 2228CLASS_TYPE::to_long() const 2229{ 2230 TO_INTX(long, LONG); 2231} 2232 2233 2234int 2235CLASS_TYPE::to_int() const 2236{ 2237 TO_INTX(int, INT); 2238} 2239 2240 2241// Convert to unsigned int64, unsigned long or unsigned 2242// int. to_uint64, to_ulong, and to_uint have the same body except for 2243// the type of v defined inside. 2244uint64 2245CLASS_TYPE::to_uint64() const 2246{ 2247 if (sgn == SC_ZERO) 2248 return 0; 2249 2250 int vnd = sc_min((int)DIGITS_PER_INT64, ndigits); 2251 2252 uint64 v = 0; 2253 2254 if (sgn == SC_NEG) { 2255 2256#ifdef SC_MAX_NBITS 2257 sc_digit d[MAX_NDIGITS]; 2258#else 2259 sc_digit *d = new sc_digit[ndigits]; 2260#endif 2261 2262 vec_copy(ndigits, d, digit); 2263 2264 convert_SM_to_2C_trimmed(IF_SC_SIGNED, sgn, nbits, ndigits, d); 2265 2266 while (--vnd >= 0) 2267 v = (v << BITS_PER_DIGIT) + d[vnd]; 2268 2269#ifndef SC_MAX_NBITS 2270 delete [] d; 2271#endif 2272 2273 } 2274 else { 2275 2276 while (--vnd >= 0) 2277 v = (v << BITS_PER_DIGIT) + digit[vnd]; 2278 2279 } 2280 2281 return v; 2282} 2283 2284 2285unsigned long 2286CLASS_TYPE::to_ulong() const 2287{ 2288 if (sgn == SC_ZERO) 2289 return 0; 2290 2291 int vnd = sc_min((int)DIGITS_PER_LONG, ndigits); 2292 2293 unsigned long v = 0; 2294 2295 if (sgn == SC_NEG) { 2296 2297#ifdef SC_MAX_NBITS 2298 sc_digit d[MAX_NDIGITS]; 2299#else 2300 sc_digit *d = new sc_digit[ndigits]; 2301#endif 2302 2303 vec_copy(ndigits, d, digit); 2304 2305 convert_SM_to_2C_trimmed(IF_SC_SIGNED, sgn, nbits, ndigits, d); 2306 2307 while (--vnd >= 0) 2308 v = (v << BITS_PER_DIGIT) + d[vnd]; 2309 2310#ifndef SC_MAX_NBITS 2311 delete [] d; 2312#endif 2313 2314 } 2315 else { 2316 2317 while (--vnd >= 0) 2318 v = (v << BITS_PER_DIGIT) + digit[vnd]; 2319 2320 } 2321 2322 return v; 2323} 2324 2325 2326unsigned int 2327CLASS_TYPE::to_uint() const 2328{ 2329 if (sgn == SC_ZERO) 2330 return 0; 2331 2332 int vnd = sc_min((int)DIGITS_PER_INT, ndigits); 2333 2334 unsigned int v = 0; 2335 2336 if (sgn == SC_NEG) { 2337 2338#ifdef SC_MAX_NBITS 2339 sc_digit d[MAX_NDIGITS]; 2340#else 2341 sc_digit *d = new sc_digit[ndigits]; 2342#endif 2343 2344 vec_copy(ndigits, d, digit); 2345 2346 convert_SM_to_2C_trimmed(IF_SC_SIGNED, sgn, nbits, ndigits, d); 2347 2348 while (--vnd >= 0) 2349 v = (v << BITS_PER_DIGIT) + d[vnd]; 2350 2351#ifndef SC_MAX_NBITS 2352 delete [] d; 2353#endif 2354 2355 } 2356 else { 2357 2358 while (--vnd >= 0) 2359 v = (v << BITS_PER_DIGIT) + digit[vnd]; 2360 2361 } 2362 2363 return v; 2364} 2365 2366 2367// Convert to double. 2368double 2369CLASS_TYPE::to_double() const 2370{ 2371 if (sgn == SC_ZERO) 2372 return (double) 0.0; 2373 2374 int vnd = ndigits; 2375 2376 double v = 0.0; 2377 while (--vnd >= 0) 2378 v = v * DIGIT_RADIX + digit[vnd]; 2379 2380 if (sgn == SC_NEG) 2381 return -v; 2382 else 2383 return v; 2384} 2385 2386 2387// Return true if the bit i is 1, false otherwise. If i is outside the 2388// bounds, return 1/0 according to the sign of the number by assuming 2389// that the number has infinite length. 2390 2391bool 2392CLASS_TYPE::test(int i) const 2393{ 2394#ifdef SC_SIGNED 2395 if (check_if_outside(i)) { 2396 if (sgn == SC_NEG) 2397 return 1; 2398 else 2399 return 0; 2400 } 2401#else 2402 if (check_if_outside(i)) 2403 return 0; 2404#endif 2405 2406 int bit_num = bit_ord(i); 2407 int digit_num = digit_ord(i); 2408 2409 if (sgn == SC_NEG) { 2410 2411#ifdef SC_MAX_NBITS 2412 sc_digit d[MAX_NDIGITS]; 2413#else 2414 sc_digit *d = new sc_digit[ndigits]; 2415#endif 2416 2417 vec_copy(ndigits, d, digit); 2418 vec_complement(ndigits, d); 2419 bool val = ((d[digit_num] & one_and_zeros(bit_num)) != 0); 2420 2421#ifndef SC_MAX_NBITS 2422 delete [] d; 2423#endif 2424 2425 return val; 2426 2427 } 2428 else 2429 return ((digit[digit_num] & one_and_zeros(bit_num)) != 0); 2430} 2431 2432 2433// Set the ith bit with 1. 2434void 2435CLASS_TYPE::set(int i) 2436{ 2437 if (check_if_outside(i)) 2438 return; 2439 2440 int bit_num = bit_ord(i); 2441 int digit_num = digit_ord(i); 2442 2443 convert_SM_to_2C(); 2444 digit[digit_num] |= one_and_zeros(bit_num); 2445 digit[digit_num] &= DIGIT_MASK; // Needed to zero the overflow bits. 2446 convert_2C_to_SM(); 2447} 2448 2449 2450// Set the ith bit with 0, i.e., clear the ith bit. 2451void 2452CLASS_TYPE::clear(int i) 2453{ 2454 if (check_if_outside(i)) 2455 return; 2456 2457 int bit_num = bit_ord(i); 2458 int digit_num = digit_ord(i); 2459 2460 convert_SM_to_2C(); 2461 digit[digit_num] &= ~(one_and_zeros(bit_num)); 2462 digit[digit_num] &= DIGIT_MASK; // Needed to zero the overflow bits. 2463 convert_2C_to_SM(); 2464} 2465 2466 2467// Create a mirror image of the number. 2468void 2469CLASS_TYPE::reverse() 2470{ 2471 convert_SM_to_2C(); 2472 vec_reverse(length(), ndigits, digit, length() - 1); 2473 convert_2C_to_SM(); 2474} 2475 2476 2477// Get a packed bit representation of the number. 2478void 2479CLASS_TYPE::get_packed_rep(sc_digit *buf) const 2480{ 2481 int buf_ndigits = (length() - 1) / BITS_PER_DIGIT_TYPE + 1; 2482 2483 // Initialize buf to zero. 2484 vec_zero(buf_ndigits, buf); 2485 2486 if (sgn == SC_ZERO) 2487 return; 2488 2489 const sc_digit *digit_or_d; 2490#ifdef SC_MAX_NBITS 2491 sc_digit d[MAX_NDIGITS]; 2492#else 2493 sc_digit *d = new sc_digit[ndigits]; 2494#endif 2495 2496 if (sgn == SC_POS) 2497 digit_or_d = digit; 2498 2499 else 2500 { 2501 // If sgn is negative, we have to convert digit to its 2's 2502 // complement. Since this function is const, we can not do it on 2503 // digit. Since buf doesn't have overflow bits, we cannot also do 2504 // it on buf. Thus, we have to do the complementation on a copy of 2505 // digit, i.e., on d. 2506 2507 vec_copy(ndigits, d, digit); 2508 vec_complement(ndigits, d); 2509 2510 buf[buf_ndigits - 1] = ~((sc_digit) 0); 2511 2512 digit_or_d = d; 2513 2514 } 2515 2516 // Copy the bits from digit to buf. The division and mod operations 2517 // below can be converted to addition/subtraction and comparison 2518 // operations at the expense of complicating the code. We can do it 2519 // if we see any performance problems. 2520 2521 for (int i = length() - 1; i >= 0; --i) { 2522 2523 if ((digit_or_d[digit_ord(i)] & one_and_zeros(bit_ord(i))) != 0) // Test. 2524 2525 buf[i / BITS_PER_DIGIT_TYPE] |= 2526 one_and_zeros(i % BITS_PER_DIGIT_TYPE); // Set. 2527 2528 else 2529 2530 buf[i / BITS_PER_DIGIT_TYPE] &= 2531 ~(one_and_zeros(i % BITS_PER_DIGIT_TYPE)); // Clear. 2532 2533 } 2534 2535#ifndef SC_MAX_NBITS 2536 delete[] d; 2537#endif 2538} 2539 2540 2541// Set a packed bit representation of the number. 2542void 2543CLASS_TYPE::set_packed_rep(sc_digit *buf) 2544{ 2545 // Initialize digit to zero. 2546 vec_zero(ndigits, digit); 2547 2548 // Copy the bits from buf to digit. 2549 for (int i = length() - 1; i >= 0; --i) { 2550 2551 if ((buf[i / BITS_PER_DIGIT_TYPE] & 2552 one_and_zeros(i % BITS_PER_DIGIT_TYPE)) != 0) // Test. 2553 2554 digit[digit_ord(i)] |= one_and_zeros(bit_ord(i)); // Set. 2555 2556 else 2557 2558 digit[digit_ord(i)] &= ~(one_and_zeros(bit_ord(i))); // Clear 2559 2560 } 2561 2562 convert_2C_to_SM(); 2563} 2564 2565 2566// ---------------------------------------------------------------------------- 2567// SECTION: Private members. 2568// ---------------------------------------------------------------------------- 2569 2570// Create a copy of v with sgn s. 2571CLASS_TYPE::CLASS_TYPE(const CLASS_TYPE& v, small_type s) : 2572 sc_value_base(v), sgn(s), nbits(v.nbits), ndigits(v.ndigits), digit() 2573{ 2574#ifndef SC_MAX_NBITS 2575 digit = new sc_digit[ndigits]; 2576#endif 2577 2578 vec_copy(ndigits, digit, v.digit); 2579} 2580 2581 2582// Create a copy of v where v is of the different type. 2583CLASS_TYPE::CLASS_TYPE(const OTHER_CLASS_TYPE& v, small_type s) : 2584 sc_value_base(v), sgn(s), nbits(num_bits(v.nbits)), ndigits(), digit() 2585{ 2586#if (IF_SC_SIGNED == 1) 2587 ndigits = v.ndigits; 2588#else 2589 ndigits = DIV_CEIL(nbits); 2590#endif 2591 2592#ifndef SC_MAX_NBITS 2593 digit = new sc_digit[ndigits]; 2594#endif 2595 2596 copy_digits(v.nbits, v.ndigits, v.digit); 2597} 2598 2599 2600// Create a signed number with (s, nb, nd, d) as its attributes (as 2601// defined in class CLASS_TYPE). If alloc is set, delete d. 2602CLASS_TYPE::CLASS_TYPE(small_type s, int nb, 2603 int nd, sc_digit *d, 2604 bool alloc) : 2605 sc_value_base(), sgn(s), nbits(num_bits(nb)), ndigits(), digit() 2606{ 2607 ndigits = DIV_CEIL(nbits); 2608 2609#ifndef SC_MAX_NBITS 2610 digit = new sc_digit[ndigits]; 2611#endif 2612 2613 if (ndigits <= nd) 2614 vec_copy(ndigits, digit, d); 2615 else 2616 vec_copy_and_zero(ndigits, digit, nd, d); 2617 2618#ifndef SC_MAX_NBITS 2619 if (alloc) 2620 delete [] d; 2621#endif 2622} 2623 2624// This constructor is mainly used in finding a "range" of bits from a 2625// number of type CLASS_TYPE. The function range(l, r) can have 2626// arbitrary precedence between l and r. If l is smaller than r, then 2627// the output is the reverse of range(r, l). 2628CLASS_TYPE::CLASS_TYPE(const CLASS_TYPE* u, int l, int r) : 2629 sc_value_base(), sgn(), nbits(), ndigits(), digit() 2630{ 2631 bool reversed = false; 2632 2633 if( l < r ) { 2634 reversed = true; 2635 int tmp = l; 2636 l = r; 2637 r = tmp; 2638 } 2639 2640 // at this point, l >= r 2641 2642 // make sure that l and r point to the bits of u 2643 r = sc_max( r, 0 ); 2644 l = sc_min( l, u->nbits - 1 ); 2645 2646 nbits = num_bits( l - r + 1 ); 2647 2648 // nbits can still be <= 0 because l and r have just been updated 2649 // with the bounds of u. 2650 2651 // if u == 0 or the range is out of bounds, return 0 2652 if( u->sgn == SC_ZERO || nbits <= num_bits( 0 ) ) { 2653 sgn = SC_ZERO; 2654 if( nbits <= num_bits( 0 ) ) { 2655 nbits = 1; 2656 } 2657 ndigits = DIV_CEIL( nbits ); 2658#ifndef SC_MAX_NBITS 2659 digit = new sc_digit[ndigits]; 2660#endif 2661 vec_zero( ndigits, digit ); 2662 return; 2663 } 2664 2665 // The rest will be executed if u is not zero. 2666 2667 ndigits = DIV_CEIL(nbits); 2668 2669 // The number of bits up to and including l and r, respectively. 2670 int nl = l + 1; 2671 int nr = r + 1; 2672 2673 // The indices of the digits that have lth and rth bits, respectively. 2674 int left_digit = DIV_CEIL(nl) - 1; 2675 int right_digit = DIV_CEIL(nr) - 1; 2676 2677 int nd; 2678 2679 // The range is performed on the 2's complement representation, so 2680 // first get the indices for that. 2681 if (u->sgn == SC_NEG) 2682 nd = left_digit + 1; 2683 else 2684 nd = left_digit - right_digit + 1; 2685 2686 // Allocate memory for the range. 2687#ifdef SC_MAX_NBITS 2688 sc_digit d[MAX_NDIGITS]; 2689#else 2690 digit = new sc_digit[ndigits]; 2691 sc_digit *d = new sc_digit[nd]; 2692#endif 2693 2694 // Getting the range on the 2's complement representation. 2695 if (u->sgn == SC_NEG) { 2696 2697 vec_copy(nd, d, u->digit); 2698 vec_complement(nd, d); // d = -d; 2699 vec_shift_right(nd, d, r, DIGIT_MASK); 2700 2701 } 2702 else { 2703 2704 for (int i = right_digit; i <= left_digit; ++i) 2705 d[i - right_digit] = u->digit[i]; 2706 2707 vec_shift_right(nd, d, r - right_digit * BITS_PER_DIGIT, 0); 2708 2709 } 2710 2711 vec_zero(ndigits, digit); 2712 2713 if (! reversed) 2714 vec_copy(sc_min(nd, ndigits), digit, d); 2715 2716 else { 2717 2718 // If l < r, i.e., reversed is set, reverse the bits of digit. d 2719 // will be used as a temporary store. The following code tries to 2720 // minimize the use of bit_ord and digit_ord, which use mod and 2721 // div operators. Since these operators are function calls to 2722 // standard library routines, they are slow. The main idea in 2723 // reversing is "read bits out of d from left to right and push 2724 // them into digit using right shifting." 2725 2726 // Take care of the last digit. 2727 int nd_less_1 = nd - 1; 2728 2729 // Deletions will start from the left end and move one position 2730 // after each deletion. 2731 sc_digit del_mask = one_and_zeros(bit_ord(l - r)); 2732 2733 while (del_mask) { 2734 vec_shift_right(ndigits, digit, 1, ((d[nd_less_1] & del_mask) != 0)); 2735 del_mask >>= 1; 2736 } 2737 2738 // Take care of the other digits if any. 2739 2740 // Insertion to digit will always occur at the left end. 2741 sc_digit ins_mask = one_and_zeros(BITS_PER_DIGIT - 1); 2742 2743 for (int j = nd - 2; j >= 0; --j) { // j = nd - 2 2744 2745 // Deletions will start from the left end and move one position 2746 // after each deletion. 2747 del_mask = ins_mask; 2748 2749 while (del_mask) { 2750 vec_shift_right(ndigits, digit, 1, ((d[j] & del_mask) != 0)); 2751 del_mask >>= 1; 2752 } 2753 } 2754 2755 if (u->sgn == SC_NEG) 2756 vec_shift_right(ndigits, digit, 2757 ndigits * BITS_PER_DIGIT - length(), DIGIT_MASK); 2758 else 2759 vec_shift_right(ndigits, digit, 2760 ndigits * BITS_PER_DIGIT - length(), 0); 2761 2762 2763 } // if reversed. 2764 2765 convert_2C_to_SM(); 2766 2767#ifndef SC_MAX_NBITS 2768 delete [] d; 2769#endif 2770} 2771 2772// This constructor is mainly used in finding a "range" of bits from a 2773// number of type OTHER_CLASS_TYPE. The function range(l, r) can have 2774// arbitrary precedence between l and r. If l is smaller than r, then 2775// the output is the reverse of range(r, l). 2776CLASS_TYPE::CLASS_TYPE(const OTHER_CLASS_TYPE* u, int l, int r) : 2777 sc_value_base(), sgn(), nbits(), ndigits(), digit() 2778{ 2779 bool reversed = false; 2780 2781 if( l < r ) { 2782 reversed = true; 2783 int tmp = l; 2784 l = r; 2785 r = tmp; 2786 } 2787 2788 // at this point, l >= r 2789 2790 // make sure that l and r point to the bits of u 2791 r = sc_max( r, 0 ); 2792 l = sc_min( l, u->nbits - 1 ); 2793 2794 nbits = num_bits( l - r + 1 ); 2795 2796 // nbits can still be <= 0 because l and r have just been updated 2797 // with the bounds of u. 2798 2799 // if u == 0 or the range is out of bounds, return 0 2800 if( u->sgn == SC_ZERO || nbits <= num_bits( 0 ) ) { 2801 sgn = SC_ZERO; 2802 if( nbits <= num_bits( 0 ) ) { 2803 nbits = 1; 2804 } 2805 ndigits = DIV_CEIL( nbits ); 2806#ifndef SC_MAX_NBITS 2807 digit = new sc_digit[ndigits]; 2808#endif 2809 vec_zero( ndigits, digit ); 2810 return; 2811 } 2812 2813 // The rest will be executed if u is not zero. 2814 2815 ndigits = DIV_CEIL(nbits); 2816 2817 // The number of bits up to and including l and r, respectively. 2818 int nl = l + 1; 2819 int nr = r + 1; 2820 2821 // The indices of the digits that have lth and rth bits, respectively. 2822 int left_digit = DIV_CEIL(nl) - 1; 2823 int right_digit = DIV_CEIL(nr) - 1; 2824 2825 int nd; 2826 2827 // The range is performed on the 2's complement representation, so 2828 // first get the indices for that. 2829 if (u->sgn == SC_NEG) 2830 nd = left_digit + 1; 2831 else 2832 nd = left_digit - right_digit + 1; 2833 2834 // Allocate memory for the range. 2835#ifdef SC_MAX_NBITS 2836 sc_digit d[MAX_NDIGITS]; 2837#else 2838 digit = new sc_digit[ndigits]; 2839 sc_digit *d = new sc_digit[nd]; 2840#endif 2841 2842 // Getting the range on the 2's complement representation. 2843 if (u->sgn == SC_NEG) { 2844 2845 vec_copy(nd, d, u->digit); 2846 vec_complement(nd, d); // d = -d; 2847 vec_shift_right(nd, d, r, DIGIT_MASK); 2848 2849 } 2850 else { 2851 2852 for (int i = right_digit; i <= left_digit; ++i) 2853 d[i - right_digit] = u->digit[i]; 2854 2855 vec_shift_right(nd, d, r - right_digit * BITS_PER_DIGIT, 0); 2856 2857 } 2858 2859 vec_zero(ndigits, digit); 2860 2861 if (! reversed) 2862 vec_copy(sc_min(nd, ndigits), digit, d); 2863 2864 else { 2865 2866 // If l < r, i.e., reversed is set, reverse the bits of digit. d 2867 // will be used as a temporary store. The following code tries to 2868 // minimize the use of bit_ord and digit_ord, which use mod and 2869 // div operators. Since these operators are function calls to 2870 // standard library routines, they are slow. The main idea in 2871 // reversing is "read bits out of d from left to right and push 2872 // them into digit using right shifting." 2873 2874 // Take care of the last digit. 2875 int nd_less_1 = nd - 1; 2876 2877 // Deletions will start from the left end and move one position 2878 // after each deletion. 2879 sc_digit del_mask = one_and_zeros(bit_ord(l - r)); 2880 2881 while (del_mask) { 2882 vec_shift_right(ndigits, digit, 1, ((d[nd_less_1] & del_mask) != 0)); 2883 del_mask >>= 1; 2884 } 2885 2886 // Take care of the other digits if any. 2887 2888 // Insertion to digit will always occur at the left end. 2889 sc_digit ins_mask = one_and_zeros(BITS_PER_DIGIT - 1); 2890 2891 for (int j = nd - 2; j >= 0; --j) { // j = nd - 2 2892 2893 // Deletions will start from the left end and move one position 2894 // after each deletion. 2895 del_mask = ins_mask; 2896 2897 while (del_mask) { 2898 vec_shift_right(ndigits, digit, 1, ((d[j] & del_mask) != 0)); 2899 del_mask >>= 1; 2900 } 2901 } 2902 2903 if (u->sgn == SC_NEG) 2904 vec_shift_right(ndigits, digit, 2905 ndigits * BITS_PER_DIGIT - length(), DIGIT_MASK); 2906 else 2907 vec_shift_right(ndigits, digit, 2908 ndigits * BITS_PER_DIGIT - length(), 0); 2909 2910 2911 } // if reversed. 2912 2913 convert_2C_to_SM(); 2914 2915#ifndef SC_MAX_NBITS 2916 delete [] d; 2917#endif 2918} 2919 2920 2921// Print out all the physical attributes. 2922void 2923CLASS_TYPE::dump(::std::ostream& os) const 2924{ 2925 // Save the current setting, and set the base to decimal. 2926 ::std::ios::fmtflags old_flags = os.setf(::std::ios::dec, ::std::ios::basefield); 2927 2928 os << "width = " << length() << ::std::endl; 2929 os << "value = " << *this << ::std::endl; 2930 os << "bits = "; 2931 2932 int len = length(); 2933 2934 for (int i = len - 1; i >= 0; --i) { 2935 2936 os << "01"[test(i)]; 2937 if (--len % 4 == 0) 2938 os << " "; 2939 2940 } 2941 2942 os << ::std::endl; 2943 2944 // Restore old_flags. 2945 os.setf(old_flags, ::std::ios::basefield); 2946} 2947 2948 2949// Checks to see if bit_num is out of bounds. 2950bool 2951CLASS_TYPE::check_if_outside(int bit_num) const 2952{ 2953 if ((bit_num < 0) || (num_bits(bit_num) >= nbits)) { 2954 2955#ifdef DEBUG_SYSTEMC 2956 if( bit_num < 0 || bit_num >= nbits ) { 2957 std::stringstream msg; 2958 msg << CLASS_TYPE_STR "::check_if_outside( int bit_num ) : " 2959 "bit_num = " << bit_num << " is out of bounds"; 2960 SC_REPORT_WARNING( sc_core::SC_ID_OUT_OF_BOUNDS_, msg.str().c_str() ); 2961 } 2962#endif 2963 2964 return true; 2965 } 2966 2967 return false; 2968} 2969 2970// End of file. 2971