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