sc_signed.cpp revision 12027
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_signed.cpp -- Arbitrary precision signed arithmetic. 23 24 This file includes the definitions of sc_signed_bitref, 25 sc_signed_subref, and sc_signed classes. The first two 26 classes are proxy classes to reference one bit and a range 27 of bits of a sc_signed number, respectively. This file also 28 includes sc_nbcommon.cpp and sc_nbfriends.cpp, which 29 contain the definitions shared by sc_unsigned. 30 31 Original Author: Ali Dasdan, Synopsys, Inc. 32 33 *****************************************************************************/ 34 35/***************************************************************************** 36 37 MODIFICATION LOG - modifiers, enter your name, affiliation, date and 38 changes you are making here. 39 40 Name, Affiliation, Date: 41 Description of Modification: 42 43 *****************************************************************************/ 44 45 46// $Log: sc_signed.cpp,v $ 47// Revision 1.6 2011/02/18 20:19:15 acg 48// Andy Goodrich: updating Copyright notice. 49// 50// Revision 1.5 2008/12/10 20:38:45 acg 51// Andy Goodrich: fixed conversion of double values to the digits vector. 52// The bits above the radix were not being masked off. 53// 54// Revision 1.4 2008/06/19 17:47:56 acg 55// Andy Goodrich: fixes for bugs. See 2.2.1 RELEASENOTES. 56// 57// Revision 1.3 2008/04/29 21:20:41 acg 58// Andy Goodrich: added mask to first word transferred when processing 59// a negative sc_signed value in sc_signed::concat_get_data(). 60// 61// Revision 1.2 2007/11/04 21:27:00 acg 62// Andy Goodrich: changes to make sure the proper value is returned from 63// concat_get_data(). 64// 65// Revision 1.1.1.1 2006/12/15 20:20:05 acg 66// SystemC 2.3 67// 68// Revision 1.5 2006/10/23 19:32:47 acg 69// Andy Goodrich: further fix for incorrect value being returned from 70// concat_get_data. This one is in the non-aligned value code. 71// 72// Revision 1.3 2006/01/13 18:49:32 acg 73// Added $Log command so that CVS check in comments are reproduced in the 74// source. 75// 76 77#include <ctype.h> 78#include <math.h> 79 80#include "sysc/kernel/sc_cmnhdr.h" 81#include "sysc/kernel/sc_macros.h" 82#include "sysc/datatypes/int/sc_signed.h" 83#include "sysc/datatypes/int/sc_unsigned.h" 84#include "sysc/datatypes/int/sc_int_base.h" 85#include "sysc/datatypes/int/sc_uint_base.h" 86#include "sysc/datatypes/int/sc_int_ids.h" 87#include "sysc/datatypes/bit/sc_bv_base.h" 88#include "sysc/datatypes/bit/sc_lv_base.h" 89#include "sysc/datatypes/misc/sc_concatref.h" 90#include "sysc/datatypes/fx/sc_fix.h" 91#include "sysc/datatypes/fx/scfx_other_defs.h" 92 93 94namespace sc_dt 95{ 96 97// Pool of temporary instances: 98 99sc_core::sc_vpool<sc_signed_bitref> sc_signed_bitref::m_pool(9); 100sc_core::sc_vpool<sc_signed_subref> sc_signed_subref::m_pool(9); 101 102// ----------------------------------------------------------------------------- 103// SECTION: Public members - Invalid selections. 104// ----------------------------------------------------------------------------- 105 106void 107sc_signed::invalid_index( int i ) const 108{ 109 char msg[BUFSIZ]; 110 std::sprintf( msg, 111 "sc_bigint bit selection: index = %d violates " 112 "0 <= index <= %d", i, nbits - 1 ); 113 SC_REPORT_ERROR( sc_core::SC_ID_OUT_OF_BOUNDS_, msg ); 114} 115 116void 117sc_signed::invalid_range( int l, int r ) const 118{ 119 char msg[BUFSIZ]; 120 std::sprintf( msg, 121 "sc_bigint part selection: left = %d, right = %d \n" 122 " violates either (%d >= left >= 0) or (%d >= right >= 0)", 123 l, r, nbits-1, nbits-1 ); 124 SC_REPORT_ERROR( sc_core::SC_ID_OUT_OF_BOUNDS_, msg ); 125} 126 127 128// ---------------------------------------------------------------------------- 129 130// ---------------------------------------------------------------------------- 131// SECTION: Public members. 132// ---------------------------------------------------------------------------- 133 134// Most public members are included from sc_nbcommon.inc. However, some 135// concatenation support appears here to optimize between the signed and 136// unsigned cases. 137 138// Insert this object's value at the specified place in a vector of biguint 139// style values. 140 141bool sc_signed::concat_get_ctrl( sc_digit* dst_p, int low_i ) const 142{ 143 int dst_i; // Index to next word to set in dst_p. 144 int end_i; // Index of high order word to set. 145 int left_shift; // Amount to shift value left. 146 sc_digit mask; // Mask for partial word sets. 147 148 149 // CALCULATE METRICS FOR DATA MOVEMENT: 150 151 dst_i = low_i / BITS_PER_DIGIT; 152 end_i = (low_i + nbits - 1) / BITS_PER_DIGIT; 153 left_shift = low_i % BITS_PER_DIGIT; 154 155 156 // ALL DATA TO BE MOVED IS IN A SINGLE WORD: 157 158 mask = ~(-1 << left_shift); 159 dst_p[dst_i] = ( dst_p[dst_i] & ~mask ); 160 dst_i++; 161 162 for ( ; dst_i <= end_i; dst_i++ ) dst_p[dst_i] = 0; 163 164 return false; 165} 166 167bool sc_signed::concat_get_data( sc_digit* dst_p, int low_i ) const 168{ 169 sc_digit carry; // Carry bit for complements. 170 int dst_i; // Index to next word to set in dst_p. 171 int end_i; // Index of high order word to set. 172 int high_i; // Index w/in word of high order bit. 173 int left_shift; // Amount to shift value left. 174 sc_digit left_word; // High word component for set. 175 sc_digit mask; // Mask for partial word sets. 176 bool result; // True if inserted non-zero data. 177 int right_shift; // Amount to shift value right. 178 sc_digit right_word; // Low word component for set. 179 int src_i; // Index to next word to get from digit. 180 181 182 183 // CALCULATE METRICS FOR DATA MOVEMENT: 184 185 dst_i = low_i / BITS_PER_DIGIT; 186 high_i = low_i + nbits - 1; 187 end_i = high_i / BITS_PER_DIGIT; 188 left_shift = low_i % BITS_PER_DIGIT; 189 190 switch ( sgn ) 191 { 192 // POSITIVE SOURCE VALUE: 193 194 case SC_POS: 195 196 result = true; 197 198 // ALL DATA TO BE MOVED IS IN A SINGLE WORD: 199 200 if ( dst_i == end_i ) 201 { 202 mask = ~(-1 << left_shift); 203 dst_p[dst_i] = ( ( dst_p[dst_i] & mask ) | 204 (digit[0] << left_shift) ) & DIGIT_MASK; 205 } 206 207 208 // DATA IS IN MORE THAN ONE WORD, BUT IS WORD ALIGNED: 209 210 else if ( left_shift == 0 ) 211 { 212 for ( src_i = 0; dst_i < end_i; dst_i++, src_i++ ) 213 { 214 dst_p[dst_i] = digit[src_i]; 215 } 216 high_i = high_i % BITS_PER_DIGIT; 217 mask = ~(-2 << high_i) & DIGIT_MASK; 218 dst_p[dst_i] = digit[src_i] & mask; 219 } 220 221 222 // DATA IS IN MORE THAN ONE WORD, AND NOT WORD ALIGNED: 223 224 else 225 { 226 high_i = high_i % BITS_PER_DIGIT; 227 right_shift = BITS_PER_DIGIT - left_shift; 228 mask = ~(-1 << left_shift); 229 right_word = digit[0]; 230 dst_p[dst_i] = (dst_p[dst_i] & mask) | 231 ((right_word << left_shift) & DIGIT_MASK); 232 for ( src_i = 1, dst_i++; dst_i < end_i; dst_i++, src_i++ ) 233 { 234 left_word = digit[src_i]; 235 dst_p[dst_i] = ((left_word << left_shift)&DIGIT_MASK) | 236 (right_word >> right_shift); 237 right_word = left_word; 238 } 239 left_word = (src_i < ndigits) ? digit[src_i] : 0; 240 mask = ~(-2 << high_i) & DIGIT_MASK; 241 dst_p[dst_i] = ((left_word << left_shift) | 242 (right_word >> right_shift)) & mask; 243 } 244 break; 245 246 247 // SOURCE VALUE IS NEGATIVE: 248 249 case SC_NEG: 250 251 // ALL DATA TO BE MOVED IS IN A SINGLE WORD: 252 253 result = true; 254 if ( dst_i == end_i ) 255 { 256 mask = ~(-1 << nbits); 257 right_word = ((digit[0] ^ DIGIT_MASK) + 1) & mask; 258 mask = ~(-1 << left_shift); 259 dst_p[dst_i] = ( ( dst_p[dst_i] & mask ) | 260 (right_word << left_shift) ) & DIGIT_MASK; 261 } 262 263 264 // DATA IS IN MORE THAN ONE WORD, BUT IS WORD ALIGNED: 265 266 else if ( left_shift == 0 ) 267 { 268 carry = 1; 269 for ( src_i = 0; dst_i < end_i; dst_i++, src_i++ ) 270 { 271 right_word = (digit[src_i] ^ DIGIT_MASK) + carry; 272 dst_p[dst_i] = right_word & DIGIT_MASK; 273 carry = right_word >> BITS_PER_DIGIT; 274 } 275 high_i = high_i % BITS_PER_DIGIT; 276 mask = (~(-2 << high_i)) & DIGIT_MASK; 277 right_word = (src_i < ndigits) ? 278 (digit[src_i] ^ DIGIT_MASK) + carry : DIGIT_MASK + carry; 279 dst_p[dst_i] = right_word & mask; 280 } 281 282 283 // DATA IS IN MORE THAN ONE WORD, AND NOT WORD ALIGNED: 284 285 else 286 { 287 high_i = high_i % BITS_PER_DIGIT; 288 right_shift = BITS_PER_DIGIT - left_shift; 289 mask = ~(-1 << left_shift); 290 carry = 1; 291 right_word = (digit[0] ^ DIGIT_MASK) + carry; 292 dst_p[dst_i] = (dst_p[dst_i] & mask) | 293 ((right_word << left_shift) & DIGIT_MASK); 294 carry = right_word >> BITS_PER_DIGIT; 295 right_word &= DIGIT_MASK; 296 for ( src_i = 1, dst_i++; dst_i < end_i; dst_i++, src_i++ ) 297 { 298 left_word = (digit[src_i] ^ DIGIT_MASK) + carry; 299 dst_p[dst_i] = ((left_word << left_shift)&DIGIT_MASK) | 300 (right_word >> right_shift); 301 carry = left_word >> BITS_PER_DIGIT; 302 right_word = left_word & DIGIT_MASK; 303 } 304 left_word = (src_i < ndigits) ? 305 (digit[src_i] ^ DIGIT_MASK) + carry : carry; 306 mask = ~(-2 << high_i) & DIGIT_MASK; 307 dst_p[dst_i] = ((left_word << left_shift) | 308 (right_word >> right_shift)) & mask; 309 } 310 break; 311 312 313 // VALUE IS ZERO: 314 315 default: 316 result = false; 317 318 319 // ALL DATA TO BE MOVED IS IN A SINGLE WORD: 320 321 if ( dst_i == end_i ) 322 { 323 mask = ~(-1 << nbits) << left_shift; 324 dst_p[dst_i] = dst_p[dst_i] & ~mask; 325 } 326 327 328 // DATA IS IN MORE THAN ONE WORD, BUT IS WORD ALIGNED: 329 330 else if ( left_shift == 0 ) 331 { 332 carry = 1; 333 for ( src_i = 0; dst_i < end_i; dst_i++, src_i++ ) 334 { 335 dst_p[dst_i] = 0; 336 } 337 high_i = high_i % BITS_PER_DIGIT; 338 mask = ~(-2 << high_i) & DIGIT_MASK; 339 dst_p[dst_i] = 0; // #### digit[src_i] & mask; 340 } 341 342 343 // DATA IS IN MORE THAN ONE WORD, AND NOT WORD ALIGNED: 344 345 else 346 { 347 high_i = high_i % BITS_PER_DIGIT; 348 right_shift = BITS_PER_DIGIT - left_shift; 349 mask = ~(-1 << left_shift); 350 dst_p[dst_i] = (dst_p[dst_i] & mask); 351 for ( dst_i++; dst_i <= end_i; dst_i++ ) 352 { 353 dst_p[dst_i] = 0; 354 } 355 } 356 break; 357 } 358 return result; 359} 360 361// Return this object instance's bits as a uint64 without sign extension. 362 363uint64 sc_signed::concat_get_uint64() const 364{ 365 uint64 result; 366 367 switch ( sgn ) 368 { 369 case SC_POS: 370 result = 0; 371 if ( ndigits > 2 ) 372 result = digit[2]; 373 if ( ndigits > 1 ) 374 result = (result << BITS_PER_DIGIT) | digit[1]; 375 result = (result << BITS_PER_DIGIT) | digit[0]; 376 break; 377 case SC_NEG: 378 result = 0; 379 if ( ndigits > 2 ) 380 result = digit[2]; 381 if ( ndigits > 1 ) 382 result = (result << BITS_PER_DIGIT) | digit[1]; 383 result = (result << BITS_PER_DIGIT) | digit[0]; 384 result = -result; 385 if ( nbits < 64 ) 386 { 387 uint64 mask = ~0; 388 result = result & ~(mask << nbits); 389 } 390 break; 391 default: 392 result = 0; 393 break; 394 } 395 return result; 396} 397 398// #### OPTIMIZE 399void sc_signed::concat_set(int64 src, int low_i) 400{ 401 *this = (low_i < 64) ? src >> low_i : src >> 63; 402} 403 404void sc_signed::concat_set(const sc_signed& src, int low_i) 405{ 406 if ( low_i < src.length() ) 407 *this = src >> low_i; 408 else 409 *this = (src<0) ? (int_type)-1 : 0; 410} 411 412void sc_signed::concat_set(const sc_unsigned& src, int low_i) 413{ 414 if ( low_i < src.length() ) 415 *this = src >> low_i; 416 else 417 *this = 0; 418} 419 420void sc_signed::concat_set(uint64 src, int low_i) 421{ 422 *this = (low_i < 64) ? src >> low_i : 0; 423} 424 425// ---------------------------------------------------------------------------- 426// SECTION: Public members - Reduction methods. 427// ---------------------------------------------------------------------------- 428 429bool sc_signed::and_reduce() const 430{ 431 sc_digit current; // Current digit examining. 432 int i; // Index of digit examining. 433 434 if ( sgn == SC_NEG ) 435 { 436 current = (1 << BITS_PER_DIGIT); 437 for ( i = 0; i < ndigits-1; i++ ) 438 { 439 current = (current >> BITS_PER_DIGIT) + (digit[i]^DIGIT_MASK); 440 if ( (current & DIGIT_MASK) != DIGIT_MASK ) return false; 441 } 442 current = (current >> BITS_PER_DIGIT) + (digit[i]^DIGIT_MASK); 443 if ( (current & ~(-1 << (nbits % BITS_PER_DIGIT))) == 444 (sc_digit) ~(-1 << (nbits % BITS_PER_DIGIT)) ) 445 return true; 446 } 447 return false; 448} 449 450bool sc_signed::or_reduce() const 451{ 452 return sgn == SC_ZERO ? false : true; 453} 454 455bool sc_signed::xor_reduce() const 456{ 457 int i; // Digit examining. 458 int odd; // Flag for odd number of digits. 459 460 odd = 0; 461 for ( i = 0; i < nbits; i++ ) 462 if ( test(i) ) odd = ~odd; 463 return odd ? true : false; 464} 465 466 467 468// ---------------------------------------------------------------------------- 469// SECTION: Public members - Assignment operators. 470// ---------------------------------------------------------------------------- 471 472// assignment operators 473 474const sc_signed& 475sc_signed::operator = ( const char* a ) 476{ 477 if( a == 0 ) { 478 SC_REPORT_ERROR( sc_core::SC_ID_CONVERSION_FAILED_, 479 "character string is zero" ); 480 } 481 if( *a == 0 ) { 482 SC_REPORT_ERROR( sc_core::SC_ID_CONVERSION_FAILED_, 483 "character string is empty" ); 484 } 485 try { 486 int len = length(); 487 sc_fix aa( a, len, len, SC_TRN, SC_WRAP, 0, SC_ON ); 488 return this->operator = ( aa ); 489 } catch( sc_core::sc_report ) { 490 char msg[BUFSIZ]; 491 std::sprintf( msg, "character string '%s' is not valid", a ); 492 SC_REPORT_ERROR( sc_core::SC_ID_CONVERSION_FAILED_, msg ); 493 // never reached 494 } 495 return *this; 496} 497 498const sc_signed& 499sc_signed::operator=(int64 v) 500{ 501 sgn = get_sign(v); 502 // v >= 0 now. 503 if (sgn == SC_ZERO) 504 vec_zero(ndigits, digit); 505 else { 506 from_uint(ndigits, digit, (uint64) v); 507 if (nbits <= (int)BITS_PER_INT64) 508 convert_SM_to_2C_to_SM(); 509 } 510 return *this; 511} 512 513const sc_signed& 514sc_signed::operator=(uint64 v) 515{ 516 sgn = get_sign(v); 517 if (sgn == SC_ZERO) 518 vec_zero(ndigits, digit); 519 else { 520 from_uint(ndigits, digit, v); 521 if (nbits <= (int)BITS_PER_INT64) 522 convert_SM_to_2C_to_SM(); 523 } 524 return *this; 525} 526 527const sc_signed& 528sc_signed::operator=(long v) 529{ 530 sgn = get_sign(v); 531 // v >= 0 now. 532 if (sgn == SC_ZERO) 533 vec_zero(ndigits, digit); 534 else { 535 from_uint(ndigits, digit, (unsigned long) v); 536 if (nbits <= (int)BITS_PER_LONG) 537 convert_SM_to_2C_to_SM(); 538 } 539 return *this; 540} 541 542const sc_signed& 543sc_signed::operator=(unsigned long v) 544{ 545 sgn = get_sign(v); 546 if (sgn == SC_ZERO) 547 vec_zero(ndigits, digit); 548 else { 549 from_uint(ndigits, digit, v); 550 if (nbits <= (int)BITS_PER_LONG) 551 convert_SM_to_2C_to_SM(); 552 } 553 return *this; 554} 555 556const sc_signed& 557sc_signed::operator=(double v) 558{ 559 is_bad_double(v); 560 if (v < 0) { 561 v = -v; 562 sgn = SC_NEG; 563 } 564 else 565 sgn = SC_POS; 566 int i = 0; 567 while (floor(v) && (i < ndigits)) { 568#ifndef _WIN32 569 digit[i++] = ((sc_digit)floor(remainder(v, DIGIT_RADIX))) & DIGIT_MASK; 570#else 571 digit[i++] = ((sc_digit)floor(fmod(v, DIGIT_RADIX))) & DIGIT_MASK; 572#endif 573 v /= DIGIT_RADIX; 574 } 575 vec_zero(i, ndigits, digit); 576 convert_SM_to_2C_to_SM(); 577 return *this; 578} 579 580 581// ---------------------------------------------------------------------------- 582 583const sc_signed& 584sc_signed::operator = ( const sc_bv_base& v ) 585{ 586 int minlen = sc_min( nbits, v.length() ); 587 int i = 0; 588 for( ; i < minlen; ++ i ) { 589 safe_set( i, v.get_bit( i ), digit ); 590 } 591 for( ; i < nbits; ++ i ) { 592 safe_set( i, 0, digit ); // zero-extend 593 } 594 convert_2C_to_SM(); 595 return *this; 596} 597 598const sc_signed& 599sc_signed::operator = ( const sc_lv_base& v ) 600{ 601 int minlen = sc_min( nbits, v.length() ); 602 int i = 0; 603 for( ; i < minlen; ++ i ) { 604 safe_set( i, sc_logic( v.get_bit( i ) ).to_bool(), digit ); 605 } 606 for( ; i < nbits; ++ i ) { 607 safe_set( i, 0, digit ); // zero-extend 608 } 609 convert_2C_to_SM(); 610 return *this; 611} 612 613 614// explicit conversion to character string 615 616const std::string 617sc_signed::to_string( sc_numrep numrep ) const 618{ 619 int len = length(); 620 sc_fix aa( *this, len, len, SC_TRN, SC_WRAP, 0, SC_ON ); 621 return aa.to_string( numrep ); 622} 623 624const std::string 625sc_signed::to_string( sc_numrep numrep, bool w_prefix ) const 626{ 627 int len = length(); 628 sc_fix aa( *this, len, len, SC_TRN, SC_WRAP, 0, SC_ON ); 629 return aa.to_string( numrep, w_prefix ); 630} 631 632 633// ---------------------------------------------------------------------------- 634// SECTION: Interfacing with sc_int_base 635// ---------------------------------------------------------------------------- 636 637const sc_signed& 638sc_signed::operator = (const sc_int_base& v) 639{ return operator=((int64) v); } 640 641 642sc_signed 643operator + ( const sc_unsigned& u, const sc_int_base& v ) 644{ return operator + ( u, SCAST<int64>( v ) ); } 645 646sc_signed 647operator + ( const sc_int_base& u, const sc_unsigned& v ) 648{ return operator + ( SCAST<int64>( u ), v ); } 649 650sc_signed 651operator + (const sc_signed& u, const sc_int_base& v) 652{ return operator+(u, (int64) v); } 653 654sc_signed 655operator + (const sc_int_base& u, const sc_signed& v) 656{ return operator+((int64) u, v); } 657 658const sc_signed& 659sc_signed::operator += (const sc_int_base& v) 660{ return operator+=((int64) v); } 661 662 663sc_signed 664operator - (const sc_unsigned& u, const sc_int_base& v) 665{ return operator-(u, (int64) v); } 666 667sc_signed 668operator - (const sc_int_base& u, const sc_unsigned& v) 669{ return operator-((int64) u, v); } 670 671sc_signed 672operator-(const sc_signed& u, const sc_int_base& v) 673{ return operator-(u, (int64) v); } 674 675sc_signed 676operator - (const sc_int_base& u, const sc_signed& v) 677{ return operator-((int64) u, v); } 678 679const sc_signed& 680sc_signed::operator -= (const sc_int_base& v) 681{ return operator-=((int64) v); } 682 683 684sc_signed 685operator * ( const sc_unsigned& u, const sc_int_base& v ) 686{ return operator * ( u, SCAST<int64>( v ) ); } 687 688sc_signed 689operator * ( const sc_int_base& u, const sc_unsigned& v ) 690{ return operator * ( SCAST<int64>( u ), v ); } 691 692sc_signed 693operator * (const sc_signed& u, const sc_int_base& v) 694{ return operator*(u, (int64) v); } 695 696sc_signed 697operator * (const sc_int_base& u, const sc_signed& v) 698{ return operator*((int64) u, v); } 699 700const sc_signed& 701sc_signed::operator *= (const sc_int_base& v) 702{ return operator*=((int64) v); } 703 704 705sc_signed 706operator / ( const sc_unsigned& u, const sc_int_base& v ) 707{ return operator / ( u, SCAST<int64>( v ) ); } 708 709sc_signed 710operator / ( const sc_int_base& u, const sc_unsigned& v ) 711{ return operator / ( SCAST<int64>( u ), v ); } 712 713sc_signed 714operator / (const sc_signed& u, const sc_int_base& v) 715{ return operator/(u, (int64) v); } 716 717sc_signed 718operator / (const sc_int_base& u, const sc_signed& v) 719{ return operator/((int64) u, v); } 720 721const sc_signed& 722sc_signed::operator /= (const sc_int_base& v) 723{ return operator/=((int64) v); } 724 725 726sc_signed 727operator % ( const sc_unsigned& u, const sc_int_base& v ) 728{ return operator % ( u, SCAST<int64>( v ) ); } 729 730sc_signed 731operator % ( const sc_int_base& u, const sc_unsigned& v ) 732{ return operator % ( SCAST<int64>( u ), v ); } 733 734sc_signed 735operator % (const sc_signed& u, const sc_int_base& v) 736{ return operator%(u, (int64) v); } 737 738sc_signed 739operator % (const sc_int_base& u, const sc_signed& v) 740{ return operator%((int64) u, v); } 741 742const sc_signed& 743sc_signed::operator %= (const sc_int_base& v) 744{ return operator%=((int64) v); } 745 746 747sc_signed 748operator & ( const sc_unsigned& u, const sc_int_base& v ) 749{ return operator & ( u, SCAST<int64>( v ) ); } 750 751sc_signed 752operator & ( const sc_int_base& u, const sc_unsigned& v ) 753{ return operator & ( SCAST<int64>( u ), v ); } 754 755sc_signed 756operator & (const sc_signed& u, const sc_int_base& v) 757{ return operator&(u, (int64) v); } 758 759sc_signed 760operator & (const sc_int_base& u, const sc_signed& v) 761{ return operator&((int64) u, v); } 762 763const sc_signed& 764sc_signed::operator &= (const sc_int_base& v) 765{ return operator&=((int64) v); } 766 767 768sc_signed 769operator | ( const sc_unsigned& u, const sc_int_base& v ) 770{ return operator | ( u, SCAST<int64>( v ) ); } 771 772sc_signed 773operator | ( const sc_int_base& u, const sc_unsigned& v ) 774{ return operator | ( SCAST<int64>( u ), v ); } 775 776sc_signed 777operator | (const sc_signed& u, const sc_int_base& v) 778{ return operator|(u, (int64) v); } 779 780sc_signed 781operator | (const sc_int_base& u, const sc_signed& v) 782{ return operator|((int64) u, v); } 783 784const sc_signed& 785sc_signed::operator |= (const sc_int_base& v) 786{ return operator|=((int64) v); } 787 788 789sc_signed 790operator ^ ( const sc_unsigned& u, const sc_int_base& v ) 791{ return operator ^ ( u, SCAST<int64>( v ) ); } 792 793sc_signed 794operator ^ ( const sc_int_base& u, const sc_unsigned& v ) 795{ return operator ^ ( SCAST<int64>( u ), v ); } 796 797sc_signed 798operator ^ (const sc_signed& u, const sc_int_base& v) 799{ return operator^(u, (int64) v); } 800 801sc_signed 802operator ^ (const sc_int_base& u, const sc_signed& v) 803{ return operator^((int64) u, v); } 804 805const sc_signed& 806sc_signed::operator ^= (const sc_int_base& v) 807{ return operator^=((int64) v); } 808 809 810sc_signed 811operator << (const sc_signed& u, const sc_int_base& v) 812{ return operator<<(u, (int64) v); } 813 814const sc_signed& 815sc_signed::operator <<= (const sc_int_base& v) 816{ return operator<<=((int64) v); } 817 818 819sc_signed 820operator >> (const sc_signed& u, const sc_int_base& v) 821{ return operator>>(u, (int64) v); } 822 823const sc_signed& 824sc_signed::operator >>= (const sc_int_base& v) 825{ return operator>>=((int64) v); } 826 827 828bool 829operator == (const sc_signed& u, const sc_int_base& v) 830{ return operator==(u, (int64) v); } 831 832bool 833operator == (const sc_int_base& u, const sc_signed& v) 834{ return operator==((int64) u, v); } 835 836 837bool 838operator != (const sc_signed& u, const sc_int_base& v) 839{ return operator!=(u, (int64) v); } 840 841bool 842operator != (const sc_int_base& u, const sc_signed& v) 843{ return operator!=((int64) u, v); } 844 845 846bool 847operator < (const sc_signed& u, const sc_int_base& v) 848{ return operator<(u, (int64) v); } 849 850bool 851operator < (const sc_int_base& u, const sc_signed& v) 852{ return operator<((int64) u, v); } 853 854 855bool 856operator <= (const sc_signed& u, const sc_int_base& v) 857{ return operator<=(u, (int64) v); } 858 859bool 860operator <= (const sc_int_base& u, const sc_signed& v) 861{ return operator<=((int64) u, v); } 862 863 864bool 865operator > (const sc_signed& u, const sc_int_base& v) 866{ return operator>(u, (int64) v); } 867 868bool 869operator > (const sc_int_base& u, const sc_signed& v) 870{ return operator>((int64) u, v); } 871 872 873bool 874operator >= (const sc_signed& u, const sc_int_base& v) 875{ return operator>=(u, (int64) v); } 876 877bool 878operator >= (const sc_int_base& u, const sc_signed& v) 879{ return operator>=((int64) u, v); } 880 881 882// ---------------------------------------------------------------------------- 883// SECTION: Interfacing with sc_uint_base 884// ---------------------------------------------------------------------------- 885 886const sc_signed& 887sc_signed::operator = (const sc_uint_base& v) 888{ return operator=((uint64) v); } 889 890 891sc_signed 892operator + (const sc_signed& u, const sc_uint_base& v) 893{ return operator+(u, (uint64) v); } 894 895sc_signed 896operator + (const sc_uint_base& u, const sc_signed& v) 897{ return operator+((uint64) u, v); } 898 899const sc_signed& 900sc_signed::operator += (const sc_uint_base& v) 901{ return operator+=((uint64) v); } 902 903 904sc_signed 905operator - (const sc_unsigned& u, const sc_uint_base& v) 906{ return operator-(u, (uint64) v); } 907 908sc_signed 909operator - (const sc_uint_base& u, const sc_unsigned& v) 910{ return operator-((uint64) u, v); } 911 912sc_signed 913operator - (const sc_signed& u, const sc_uint_base& v) 914{ return operator-(u, (uint64) v); } 915 916sc_signed 917operator - (const sc_uint_base& u, const sc_signed& v) 918{ return operator-((uint64) u, v); } 919 920const sc_signed& 921sc_signed::operator -= (const sc_uint_base& v) 922{ return operator-=((uint64) v); } 923 924 925sc_signed 926operator * (const sc_signed& u, const sc_uint_base& v) 927{ return operator*(u, (uint64) v); } 928 929sc_signed 930operator * (const sc_uint_base& u, const sc_signed& v) 931{ return operator*((uint64) u, v); } 932 933const sc_signed& 934sc_signed::operator *= (const sc_uint_base& v) 935{ return operator*=((uint64) v); } 936 937 938sc_signed 939operator / (const sc_signed& u, const sc_uint_base& v) 940{ return operator/(u, (uint64) v); } 941 942sc_signed 943operator / (const sc_uint_base& u, const sc_signed& v) 944{ return operator/((uint64) u, v); } 945 946const sc_signed& 947sc_signed::operator /= (const sc_uint_base& v) 948{ return operator/=((uint64) v); } 949 950 951sc_signed 952operator % (const sc_signed& u, const sc_uint_base& v) 953{ return operator%(u, (uint64) v); } 954 955sc_signed 956operator % (const sc_uint_base& u, const sc_signed& v) 957{ return operator%((uint64) u, v); } 958 959const sc_signed& 960sc_signed::operator %= (const sc_uint_base& v) 961{ return operator%=((uint64) v); } 962 963 964sc_signed 965operator & (const sc_signed& u, const sc_uint_base& v) 966{ return operator&(u, (uint64) v); } 967 968sc_signed 969operator & (const sc_uint_base& u, const sc_signed& v) 970{ return operator&((uint64) u, v); } 971 972const sc_signed& 973sc_signed::operator &= (const sc_uint_base& v) 974{ return operator&=((uint64) v); } 975 976 977sc_signed 978operator | (const sc_signed& u, const sc_uint_base& v) 979{ return operator|(u, (uint64) v); } 980 981sc_signed 982operator | (const sc_uint_base& u, const sc_signed& v) 983{ return operator|((uint64) u, v); } 984 985const sc_signed& 986sc_signed::operator |= (const sc_uint_base& v) 987{ return operator|=((uint64) v); } 988 989 990sc_signed 991operator ^ (const sc_signed& u, const sc_uint_base& v) 992{ return operator^(u, (uint64) v); } 993 994sc_signed 995operator ^ (const sc_uint_base& u, const sc_signed& v) 996{ return operator^((uint64) u, v); } 997 998const sc_signed& 999sc_signed::operator ^= (const sc_uint_base& v) 1000{ return operator^=((uint64) v); } 1001 1002 1003sc_signed 1004operator << (const sc_signed& u, const sc_uint_base& v) 1005{ return operator<<(u, (uint64) v); } 1006 1007const sc_signed& 1008sc_signed::operator <<= (const sc_uint_base& v) 1009{ return operator<<=((uint64) v); } 1010 1011 1012sc_signed 1013operator >> (const sc_signed& u, const sc_uint_base& v) 1014{ return operator>>(u, (uint64) v); } 1015 1016const sc_signed& 1017sc_signed::operator >>= (const sc_uint_base& v) 1018{ return operator>>=((uint64) v); } 1019 1020 1021bool 1022operator == (const sc_signed& u, const sc_uint_base& v) 1023{ return operator==(u, (uint64) v); } 1024 1025bool 1026operator == (const sc_uint_base& u, const sc_signed& v) 1027{ return operator==((uint64) u, v); } 1028 1029 1030bool 1031operator != (const sc_signed& u, const sc_uint_base& v) 1032{ return operator!=(u, (uint64) v); } 1033 1034bool 1035operator != (const sc_uint_base& u, const sc_signed& v) 1036{ return operator!=((uint64) u, v); } 1037 1038 1039bool 1040operator < (const sc_signed& u, const sc_uint_base& v) 1041{ return operator<(u, (uint64) v); } 1042 1043bool 1044operator < (const sc_uint_base& u, const sc_signed& v) 1045{ return operator<((uint64) u, v); } 1046 1047 1048bool 1049operator <= (const sc_signed& u, const sc_uint_base& v) 1050{ return operator<=(u, (uint64) v); } 1051 1052bool 1053operator <= (const sc_uint_base& u, const sc_signed& v) 1054{ return operator<=((uint64) u, v); } 1055 1056 1057bool 1058operator > (const sc_signed& u, const sc_uint_base& v) 1059{ return operator>(u, (uint64) v); } 1060 1061bool 1062operator > (const sc_uint_base& u, const sc_signed& v) 1063{ return operator>((uint64) u, v); } 1064 1065 1066bool 1067operator >= (const sc_signed& u, const sc_uint_base& v) 1068{ return operator>=(u, (uint64) v); } 1069 1070bool 1071operator >= (const sc_uint_base& u, const sc_signed& v) 1072{ return operator>=((uint64) u, v); } 1073 1074 1075// ---------------------------------------------------------------------------- 1076// SECTION: Input and output operators 1077// ---------------------------------------------------------------------------- 1078 1079// Operators in this section are included from sc_nbcommon.cpp. 1080 1081 1082// ---------------------------------------------------------------------------- 1083// SECTION: Operator macros. 1084// ---------------------------------------------------------------------------- 1085 1086#define CONVERT_LONG(u) \ 1087small_type u ## s = get_sign(u); \ 1088sc_digit u ## d[DIGITS_PER_ULONG]; \ 1089from_uint(DIGITS_PER_ULONG, u ## d, (unsigned long) u); 1090 1091#define CONVERT_LONG_2(u) \ 1092sc_digit u ## d[DIGITS_PER_ULONG]; \ 1093from_uint(DIGITS_PER_ULONG, u ## d, (unsigned long) u); 1094 1095#define CONVERT_INT(u) \ 1096small_type u ## s = get_sign(u); \ 1097sc_digit u ## d[DIGITS_PER_UINT]; \ 1098from_uint(DIGITS_PER_UINT, u ## d, (unsigned int) u); 1099 1100#define CONVERT_INT_2(u) \ 1101sc_digit u ## d[DIGITS_PER_UINT]; \ 1102from_uint(DIGITS_PER_UINT, u ## d, (unsigned int) u); 1103 1104#define CONVERT_INT64(u) \ 1105small_type u ## s = get_sign(u); \ 1106sc_digit u ## d[DIGITS_PER_UINT64]; \ 1107from_uint(DIGITS_PER_UINT64, u ## d, (uint64) u); 1108 1109#define CONVERT_INT64_2(u) \ 1110sc_digit u ## d[DIGITS_PER_UINT64]; \ 1111from_uint(DIGITS_PER_UINT64, u ## d, (uint64) u); 1112 1113 1114// ---------------------------------------------------------------------------- 1115// SECTION: PLUS operators: +, +=, ++ 1116// ---------------------------------------------------------------------------- 1117 1118// Cases to consider when computing u + v: 1119// 1. 0 + v = v 1120// 2. u + 0 = u 1121// 3. if sgn(u) == sgn(v) 1122// 3.1 u + v = +(u + v) = sgn(u) * (u + v) 1123// 3.2 (-u) + (-v) = -(u + v) = sgn(u) * (u + v) 1124// 4. if sgn(u) != sgn(v) 1125// 4.1 u + (-v) = u - v = sgn(u) * (u - v) 1126// 4.2 (-u) + v = -(u - v) ==> sgn(u) * (u - v) 1127// 1128// Specialization of above cases for computing ++u or u++: 1129// 1. 0 + 1 = 1 1130// 3. u + 1 = u + 1 = sgn(u) * (u + 1) 1131// 4. (-u) + 1 = -(u - 1) = sgn(u) * (u - 1) 1132 1133sc_signed 1134operator+(const sc_unsigned& u, const sc_signed& v) 1135{ 1136 1137 if (u.sgn == SC_ZERO) // case 1 1138 return sc_signed(v); 1139 1140 if (v.sgn == SC_ZERO) // case 2 1141 return sc_signed(u); 1142 1143 // cases 3 and 4 1144 return add_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit, 1145 v.sgn, v.nbits, v.ndigits, v.digit); 1146 1147} 1148 1149 1150sc_signed 1151operator+(const sc_signed& u, const sc_unsigned& v) 1152{ 1153 1154 if (u.sgn == SC_ZERO) // case 1 1155 return sc_signed(v); 1156 1157 if (v.sgn == SC_ZERO) // case 2 1158 return sc_signed(u); 1159 1160 // cases 3 and 4 1161 return add_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit, 1162 v.sgn, v.nbits, v.ndigits, v.digit); 1163 1164} 1165 1166 1167sc_signed 1168operator+(const sc_signed& u, const sc_signed& v) 1169{ 1170 1171 if (u.sgn == SC_ZERO) // case 1 1172 return sc_signed(v); 1173 1174 if (v.sgn == SC_ZERO) // case 2 1175 return sc_signed(u); 1176 1177 // cases 3 and 4 1178 return add_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit, 1179 v.sgn, v.nbits, v.ndigits, v.digit); 1180 1181} 1182 1183 1184sc_signed 1185operator+(const sc_signed &u, int64 v) 1186{ 1187 1188 if (v == 0) // case 2 1189 return sc_signed(u); 1190 1191 CONVERT_INT64(v); 1192 1193 if (u.sgn == SC_ZERO) // case 1 1194 return sc_signed(vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd, false); 1195 1196 // cases 3 and 4 1197 return add_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit, 1198 vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd); 1199 1200} 1201 1202 1203sc_signed 1204operator+(int64 u, const sc_signed &v) 1205{ 1206 1207 if (u == 0) // case 1 1208 return sc_signed(v); 1209 1210 CONVERT_INT64(u); 1211 1212 if (v.sgn == SC_ZERO) // case 2 1213 return sc_signed(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud, false); 1214 1215 // cases 3 and 4 1216 1217 return add_signed_friend(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud, 1218 v.sgn, v.nbits, v.ndigits, v.digit); 1219 1220} 1221 1222 1223sc_signed 1224operator+(const sc_unsigned &u, int64 v) 1225{ 1226 1227 if (v == 0) // case 2 1228 return sc_signed(u); 1229 1230 CONVERT_INT64(v); 1231 1232 if (u.sgn == SC_ZERO) // case 1 1233 return sc_signed(vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd, false); 1234 1235 // cases 3 and 4 1236 return add_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit, 1237 vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd); 1238 1239} 1240 1241 1242sc_signed 1243operator+(int64 u, const sc_unsigned &v) 1244{ 1245 1246 if (u == 0) // case 1 1247 return sc_signed(v); 1248 1249 CONVERT_INT64(u); 1250 1251 if (v.sgn == SC_ZERO) // case 2 1252 return sc_signed(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud, false); 1253 1254 // cases 3 and 4 1255 1256 return add_signed_friend(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud, 1257 v.sgn, v.nbits, v.ndigits, v.digit); 1258 1259} 1260 1261 1262sc_signed 1263operator+(const sc_signed &u, uint64 v) 1264{ 1265 1266 if (v == 0) // case 2 1267 return sc_signed(u); 1268 1269 CONVERT_INT64(v); 1270 1271 if (u.sgn == SC_ZERO) // case 1 1272 return sc_signed(vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd, false); 1273 1274 // cases 3 and 4 1275 return add_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit, 1276 vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd); 1277 1278} 1279 1280 1281sc_signed 1282operator+(uint64 u, const sc_signed &v) 1283{ 1284 1285 if (u == 0) // case 1 1286 return sc_signed(v); 1287 1288 CONVERT_INT64(u); 1289 1290 if (v.sgn == SC_ZERO) // case 2 1291 return sc_signed(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud, false); 1292 1293 // cases 3 and 4 1294 1295 return add_signed_friend(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud, 1296 v.sgn, v.nbits, v.ndigits, v.digit); 1297 1298} 1299 1300 1301sc_signed 1302operator+(const sc_signed &u, long v) 1303{ 1304 1305 if (v == 0) // case 2 1306 return sc_signed(u); 1307 1308 CONVERT_LONG(v); 1309 1310 if (u.sgn == SC_ZERO) // case 1 1311 return sc_signed(vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd, false); 1312 1313 // cases 3 and 4 1314 return add_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit, 1315 vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd); 1316 1317} 1318 1319 1320sc_signed 1321operator+(long u, const sc_signed &v) 1322{ 1323 1324 if (u == 0) // case 1 1325 return sc_signed(v); 1326 1327 CONVERT_LONG(u); 1328 1329 if (v.sgn == SC_ZERO) // case 2 1330 return sc_signed(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud, false); 1331 1332 // cases 3 and 4 1333 return add_signed_friend(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud, 1334 v.sgn, v.nbits, v.ndigits, v.digit); 1335 1336} 1337 1338 1339sc_signed 1340operator+(const sc_unsigned &u, long v) 1341{ 1342 1343 if (v == 0) // case 2 1344 return sc_signed(u); 1345 1346 CONVERT_LONG(v); 1347 1348 if (u.sgn == SC_ZERO) // case 1 1349 return sc_signed(vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd, false); 1350 1351 // cases 3 and 4 1352 return add_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit, 1353 vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd); 1354 1355} 1356 1357 1358sc_signed 1359operator+(long u, const sc_unsigned &v) 1360{ 1361 1362 if (u == 0) // case 1 1363 return sc_signed(v); 1364 1365 CONVERT_LONG(u); 1366 1367 if (v.sgn == SC_ZERO) // case 2 1368 return sc_signed(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud, false); 1369 1370 // cases 3 and 4 1371 return add_signed_friend(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud, 1372 v.sgn, v.nbits, v.ndigits, v.digit); 1373 1374} 1375 1376 1377sc_signed 1378operator+(const sc_signed &u, unsigned long v) 1379{ 1380 1381 if (v == 0) // case 2 1382 return sc_signed(u); 1383 1384 CONVERT_LONG(v); 1385 1386 if (u.sgn == SC_ZERO) // case 1 1387 return sc_signed(vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd, false); 1388 1389 // cases 3 and 4 1390 return add_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit, 1391 vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd); 1392 1393} 1394 1395 1396sc_signed 1397operator+(unsigned long u, const sc_signed &v) 1398{ 1399 1400 if (u == 0) // case 1 1401 return sc_signed(v); 1402 1403 CONVERT_LONG(u); 1404 1405 if (v.sgn == SC_ZERO) // case 2 1406 return sc_signed(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud, false); 1407 1408 // cases 3 and 4 1409 return add_signed_friend(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud, 1410 v.sgn, v.nbits, v.ndigits, v.digit); 1411 1412} 1413 1414// The rest of the operators in this section are included from 1415// sc_nbcommon.cpp. 1416 1417// ---------------------------------------------------------------------------- 1418// SECTION: MINUS operators: -, -=, -- 1419// ---------------------------------------------------------------------------- 1420 1421// Cases to consider when computing u + v: 1422// 1. u - 0 = u 1423// 2. 0 - v = -v 1424// 3. if sgn(u) != sgn(v) 1425// 3.1 u - (-v) = u + v = sgn(u) * (u + v) 1426// 3.2 (-u) - v = -(u + v) ==> sgn(u) * (u + v) 1427// 4. if sgn(u) == sgn(v) 1428// 4.1 u - v = +(u - v) = sgn(u) * (u - v) 1429// 4.2 (-u) - (-v) = -(u - v) = sgn(u) * (u - v) 1430// 1431// Specialization of above cases for computing --u or u--: 1432// 1. 0 - 1 = -1 1433// 3. (-u) - 1 = -(u + 1) = sgn(u) * (u + 1) 1434// 4. u - 1 = u - 1 = sgn(u) * (u - 1) 1435 1436sc_signed 1437operator-(const sc_unsigned& u, const sc_unsigned& v) 1438{ 1439 1440 if (v.sgn == SC_ZERO) // case 1 1441 return sc_signed(u); 1442 1443 if (u.sgn == SC_ZERO) // case 2 1444 return sc_signed(v, -v.sgn); 1445 1446 // cases 3 and 4 1447 return add_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit, 1448 -v.sgn, v.nbits, v.ndigits, v.digit); 1449 1450} 1451 1452 1453sc_signed 1454operator-(const sc_unsigned& u, const sc_signed& v) 1455{ 1456 1457 if (v.sgn == SC_ZERO) // case 1 1458 return sc_signed(u); 1459 1460 if (u.sgn == SC_ZERO) // case 2 1461 return sc_signed(v, -v.sgn); 1462 1463 // cases 3 and 4 1464 return add_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit, 1465 -v.sgn, v.nbits, v.ndigits, v.digit); 1466 1467} 1468 1469 1470sc_signed 1471operator-(const sc_signed& u, const sc_unsigned& v) 1472{ 1473 1474 if (v.sgn == SC_ZERO) // case 1 1475 return sc_signed(u); 1476 1477 if (u.sgn == SC_ZERO) // case 2 1478 return sc_signed(v, -v.sgn); 1479 1480 // cases 3 and 4 1481 return add_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit, 1482 -v.sgn, v.nbits, v.ndigits, v.digit); 1483 1484} 1485 1486 1487sc_signed 1488operator-(const sc_signed& u, const sc_signed& v) 1489{ 1490 1491 if (v.sgn == SC_ZERO) // case 1 1492 return sc_signed(u); 1493 1494 if (u.sgn == SC_ZERO) // case 2 1495 return sc_signed(v, -v.sgn); 1496 1497 // cases 3 and 4 1498 return add_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit, 1499 -v.sgn, v.nbits, v.ndigits, v.digit); 1500 1501} 1502 1503 1504sc_signed 1505operator-(const sc_signed &u, int64 v) 1506{ 1507 1508 if (v == 0) // case 1 1509 return sc_signed(u); 1510 1511 CONVERT_INT64(v); 1512 1513 if (u.sgn == SC_ZERO) // case 2 1514 return sc_signed(-vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd, false); 1515 1516 // cases 3 and 4 1517 return add_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit, 1518 -vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd); 1519 1520} 1521 1522 1523sc_signed 1524operator-(int64 u, const sc_signed& v) 1525{ 1526 1527 if (u == 0) // case 1 1528 return sc_signed(v, -v.sgn); 1529 1530 CONVERT_INT64(u); 1531 1532 if (v.sgn == SC_ZERO) // case 2 1533 return sc_signed(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud, false); 1534 1535 // cases 3 and 4 1536 1537 return add_signed_friend(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud, 1538 -v.sgn, v.nbits, v.ndigits, v.digit); 1539 1540} 1541 1542 1543sc_signed 1544operator-(const sc_unsigned &u, int64 v) 1545{ 1546 1547 if (v == 0) // case 1 1548 return sc_signed(u); 1549 1550 CONVERT_INT64(v); 1551 1552 if (u.sgn == SC_ZERO) // case 2 1553 return sc_signed(-vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd, false); 1554 1555 // cases 3 and 4 1556 return add_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit, 1557 -vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd); 1558 1559} 1560 1561 1562sc_signed 1563operator-(int64 u, const sc_unsigned& v) 1564{ 1565 1566 if (u == 0) // case 1 1567 return sc_signed(v, -v.sgn); 1568 1569 CONVERT_INT64(u); 1570 1571 if (v.sgn == SC_ZERO) // case 2 1572 return sc_signed(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud, false); 1573 1574 // cases 3 and 4 1575 1576 return add_signed_friend(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud, 1577 -v.sgn, v.nbits, v.ndigits, v.digit); 1578 1579} 1580 1581 1582sc_signed 1583operator-(const sc_signed &u, uint64 v) 1584{ 1585 1586 if (v == 0) // case 1 1587 return sc_signed(u); 1588 1589 CONVERT_INT64(v); 1590 1591 if (u.sgn == SC_ZERO) // case 2 1592 return sc_signed(-vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd, false); 1593 1594 // cases 3 and 4 1595 1596 return add_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit, 1597 -vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd); 1598 1599} 1600 1601 1602sc_signed 1603operator-(uint64 u, const sc_signed& v) 1604{ 1605 1606 if (u == 0) // case 1 1607 return sc_signed(v, -v.sgn); 1608 1609 CONVERT_INT64(u); 1610 1611 if (v.sgn == SC_ZERO) // case 2 1612 return sc_signed(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud, false); 1613 1614 // cases 3 and 4 1615 return add_signed_friend(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud, 1616 -v.sgn, v.nbits, v.ndigits, v.digit); 1617 1618} 1619 1620 1621sc_signed 1622operator-(const sc_unsigned &u, uint64 v) 1623{ 1624 1625 if (v == 0) // case 1 1626 return sc_signed(u); 1627 1628 CONVERT_INT64(v); 1629 1630 if (u.sgn == SC_ZERO) // case 2 1631 return sc_signed(-vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd, false); 1632 1633 // cases 3 and 4 1634 1635 return add_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit, 1636 -vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd); 1637 1638} 1639 1640 1641sc_signed 1642operator-(uint64 u, const sc_unsigned& v) 1643{ 1644 1645 if (u == 0) // case 1 1646 return sc_signed(v, -v.sgn); 1647 1648 CONVERT_INT64(u); 1649 1650 if (v.sgn == SC_ZERO) // case 2 1651 return sc_signed(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud, false); 1652 1653 // cases 3 and 4 1654 return add_signed_friend(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud, 1655 -v.sgn, v.nbits, v.ndigits, v.digit); 1656 1657} 1658 1659 1660sc_signed 1661operator-(const sc_signed &u, long v) 1662{ 1663 1664 if (v == 0) // case 1 1665 return sc_signed(u); 1666 1667 CONVERT_LONG(v); 1668 1669 if (u.sgn == SC_ZERO) // case 2 1670 return sc_signed(-vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd, false); 1671 1672 // cases 3 and 4 1673 return add_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit, 1674 -vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd); 1675 1676} 1677 1678 1679sc_signed 1680operator-(long u, const sc_signed& v) 1681{ 1682 1683 if (u == 0) // case 1 1684 return sc_signed(v, -v.sgn); 1685 1686 CONVERT_LONG(u); 1687 1688 if (v.sgn == SC_ZERO) // case 2 1689 return sc_signed(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud, false); 1690 1691 // cases 3 and 4 1692 return add_signed_friend(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud, 1693 -v.sgn, v.nbits, v.ndigits, v.digit); 1694 1695} 1696 1697 1698sc_signed 1699operator-(const sc_unsigned &u, long v) 1700{ 1701 1702 if (v == 0) // case 1 1703 return sc_signed(u); 1704 1705 CONVERT_LONG(v); 1706 1707 if (u.sgn == SC_ZERO) // case 2 1708 return sc_signed(-vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd, false); 1709 1710 // cases 3 and 4 1711 return add_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit, 1712 -vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd); 1713 1714} 1715 1716 1717sc_signed 1718operator-(long u, const sc_unsigned& v) 1719{ 1720 1721 if (u == 0) // case 1 1722 return sc_signed(v, -v.sgn); 1723 1724 CONVERT_LONG(u); 1725 1726 if (v.sgn == SC_ZERO) // case 2 1727 return sc_signed(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud, false); 1728 1729 // cases 3 and 4 1730 return add_signed_friend(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud, 1731 -v.sgn, v.nbits, v.ndigits, v.digit); 1732 1733} 1734 1735 1736sc_signed 1737operator-(const sc_signed &u, unsigned long v) 1738{ 1739 1740 if (v == 0) // case 1 1741 return sc_signed(u); 1742 1743 CONVERT_LONG(v); 1744 1745 if (u.sgn == SC_ZERO) // case 2 1746 return sc_signed(-vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd, false); 1747 1748 // cases 3 and 4 1749 return add_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit, 1750 -vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd); 1751 1752} 1753 1754 1755sc_signed 1756operator-(unsigned long u, const sc_signed& v) 1757{ 1758 if (u == 0) // case 1 1759 return sc_signed(v, -v.sgn); 1760 1761 CONVERT_LONG(u); 1762 1763 if (v.sgn == SC_ZERO) // case 2 1764 return sc_signed(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud, false); 1765 1766 // cases 3 and 4 1767 return add_signed_friend(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud, 1768 -v.sgn, v.nbits, v.ndigits, v.digit); 1769 1770} 1771 1772 1773sc_signed 1774operator-(const sc_unsigned &u, unsigned long v) 1775{ 1776 1777 if (v == 0) // case 1 1778 return sc_signed(u); 1779 1780 CONVERT_LONG(v); 1781 1782 if (u.sgn == SC_ZERO) // case 2 1783 return sc_signed(-vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd, false); 1784 1785 // cases 3 and 4 1786 return add_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit, 1787 -vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd); 1788 1789} 1790 1791 1792sc_signed 1793operator-(unsigned long u, const sc_unsigned& v) 1794{ 1795 if (u == 0) // case 1 1796 return sc_signed(v, -v.sgn); 1797 1798 CONVERT_LONG(u); 1799 1800 if (v.sgn == SC_ZERO) // case 2 1801 return sc_signed(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud, false); 1802 1803 // cases 3 and 4 1804 return add_signed_friend(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud, 1805 -v.sgn, v.nbits, v.ndigits, v.digit); 1806 1807} 1808 1809// The rest of the operators in this section are included from 1810// sc_nbcommon.cpp. 1811 1812 1813// ---------------------------------------------------------------------------- 1814// SECTION: MULTIPLICATION operators: *, *= 1815// ---------------------------------------------------------------------------- 1816 1817// Cases to consider when computing u * v: 1818// 1. u * 0 = 0 * v = 0 1819// 2. 1 * v = v and -1 * v = -v 1820// 3. u * 1 = u and u * -1 = -u 1821// 4. u * v = u * v 1822 1823sc_signed 1824operator*(const sc_unsigned& u, const sc_signed& v) 1825{ 1826 1827 small_type s = mul_signs(u.sgn, v.sgn); 1828 1829 if (s == SC_ZERO) // case 1 1830 return sc_signed(); 1831 1832 // cases 2-4 1833 return mul_signed_friend(s, u.nbits, u.ndigits, u.digit, 1834 v.nbits, v.ndigits, v.digit); 1835 1836} 1837 1838 1839sc_signed 1840operator*(const sc_signed& u, const sc_unsigned& v) 1841{ 1842 1843 small_type s = mul_signs(u.sgn, v.sgn); 1844 1845 if (s == SC_ZERO) // case 1 1846 return sc_signed(); 1847 1848 // cases 2-4 1849 return mul_signed_friend(s, u.nbits, u.ndigits, u.digit, 1850 v.nbits, v.ndigits, v.digit); 1851 1852} 1853 1854 1855sc_signed 1856operator*(const sc_signed& u, const sc_signed& v) 1857{ 1858 1859 small_type s = mul_signs(u.sgn, v.sgn); 1860 1861 if (s == SC_ZERO) // case 1 1862 return sc_signed(); 1863 1864 // cases 2-4 1865 return mul_signed_friend(s, u.nbits, u.ndigits, u.digit, 1866 v.nbits, v.ndigits, v.digit); 1867 1868} 1869 1870 1871sc_signed 1872operator*(const sc_signed& u, int64 v) 1873{ 1874 1875 small_type s = mul_signs(u.sgn, get_sign(v)); 1876 1877 if (s == SC_ZERO) // case 1 1878 return sc_signed(); 1879 1880 CONVERT_INT64_2(v); 1881 1882 // cases 2-4 1883 return mul_signed_friend(s, u.nbits, u.ndigits, u.digit, 1884 BITS_PER_UINT64, DIGITS_PER_UINT64, vd); 1885 1886} 1887 1888 1889sc_signed 1890operator*(int64 u, const sc_signed& v) 1891{ 1892 1893 small_type s = mul_signs(v.sgn, get_sign(u)); 1894 1895 if (s == SC_ZERO) // case 1 1896 return sc_signed(); 1897 1898 CONVERT_INT64_2(u); 1899 1900 // cases 2-4 1901 return mul_signed_friend(s, BITS_PER_UINT64, DIGITS_PER_UINT64, ud, 1902 v.nbits, v.ndigits, v.digit); 1903 1904} 1905 1906 1907sc_signed 1908operator*(const sc_unsigned& u, int64 v) 1909{ 1910 1911 small_type s = mul_signs(u.sgn, get_sign(v)); 1912 1913 if (s == SC_ZERO) // case 1 1914 return sc_signed(); 1915 1916 CONVERT_INT64_2(v); 1917 1918 // cases 2-4 1919 return mul_signed_friend(s, u.nbits, u.ndigits, u.digit, 1920 BITS_PER_UINT64, DIGITS_PER_UINT64, vd); 1921 1922} 1923 1924 1925sc_signed 1926operator*(int64 u, const sc_unsigned& v) 1927{ 1928 1929 small_type s = mul_signs(v.sgn, get_sign(u)); 1930 1931 if (s == SC_ZERO) // case 1 1932 return sc_signed(); 1933 1934 CONVERT_INT64_2(u); 1935 1936 // cases 2-4 1937 return mul_signed_friend(s, BITS_PER_UINT64, DIGITS_PER_UINT64, ud, 1938 v.nbits, v.ndigits, v.digit); 1939 1940} 1941 1942 1943sc_signed 1944operator*(const sc_signed& u, uint64 v) 1945{ 1946 1947 small_type s = mul_signs(u.sgn, get_sign(v)); 1948 1949 if (s == SC_ZERO) // case 1 1950 return sc_signed(); 1951 1952 CONVERT_INT64_2(v); 1953 1954 // cases 2-4 1955 return mul_signed_friend(s, u.nbits, u.ndigits, u.digit, 1956 BITS_PER_UINT64, DIGITS_PER_UINT64, vd); 1957 1958} 1959 1960 1961sc_signed 1962operator*(uint64 u, const sc_signed& v) 1963{ 1964 1965 small_type s = mul_signs(v.sgn, get_sign(u)); 1966 1967 if (s == SC_ZERO) // case 1 1968 return sc_signed(); 1969 1970 CONVERT_INT64_2(u); 1971 1972 // cases 2-4 1973 return mul_signed_friend(s, BITS_PER_UINT64, DIGITS_PER_UINT64, ud, 1974 v.nbits, v.ndigits, v.digit); 1975 1976} 1977 1978 1979sc_signed 1980operator*(const sc_signed& u, long v) 1981{ 1982 1983 small_type s = mul_signs(u.sgn, get_sign(v)); 1984 1985 if (s == SC_ZERO) // case 1 1986 return sc_signed(); 1987 1988 CONVERT_LONG_2(v); 1989 1990 // cases 2-4 1991 return mul_signed_friend(s, u.nbits, u.ndigits, u.digit, 1992 BITS_PER_ULONG, DIGITS_PER_ULONG, vd); 1993 1994} 1995 1996 1997sc_signed 1998operator*(long u, const sc_signed& v) 1999{ 2000 2001 small_type s = mul_signs(v.sgn, get_sign(u)); 2002 2003 if (s == SC_ZERO) // case 1 2004 return sc_signed(); 2005 2006 CONVERT_LONG_2(u); 2007 2008 // cases 2-4 2009 return mul_signed_friend(s, BITS_PER_ULONG, DIGITS_PER_ULONG, ud, 2010 v.nbits, v.ndigits, v.digit); 2011 2012} 2013 2014 2015sc_signed 2016operator*(const sc_unsigned& u, long v) 2017{ 2018 2019 small_type s = mul_signs(u.sgn, get_sign(v)); 2020 2021 if (s == SC_ZERO) // case 1 2022 return sc_signed(); 2023 2024 CONVERT_LONG_2(v); 2025 2026 // cases 2-4 2027 return mul_signed_friend(s, u.nbits, u.ndigits, u.digit, 2028 BITS_PER_ULONG, DIGITS_PER_ULONG, vd); 2029 2030} 2031 2032 2033sc_signed 2034operator*(long u, const sc_unsigned& v) 2035{ 2036 2037 small_type s = mul_signs(v.sgn, get_sign(u)); 2038 2039 if (s == SC_ZERO) // case 1 2040 return sc_signed(); 2041 2042 CONVERT_LONG_2(u); 2043 2044 // cases 2-4 2045 return mul_signed_friend(s, BITS_PER_ULONG, DIGITS_PER_ULONG, ud, 2046 v.nbits, v.ndigits, v.digit); 2047 2048} 2049 2050 2051sc_signed 2052operator*(const sc_signed& u, unsigned long v) 2053{ 2054 2055 small_type s = mul_signs(u.sgn, get_sign(v)); 2056 2057 if (s == SC_ZERO) // case 1 2058 return sc_signed(); 2059 2060 CONVERT_LONG_2(v); 2061 2062 // else cases 2-4 2063 return mul_signed_friend(s, u.nbits, u.ndigits, u.digit, 2064 BITS_PER_ULONG, DIGITS_PER_ULONG, vd); 2065 2066} 2067 2068sc_signed 2069operator*(unsigned long u, const sc_signed& v) 2070{ 2071 2072 small_type s = mul_signs(v.sgn, get_sign(u)); 2073 2074 if (s == SC_ZERO) // case 1 2075 return sc_signed(); 2076 2077 CONVERT_LONG_2(u); 2078 2079 // cases 2-4 2080 return mul_signed_friend(s, BITS_PER_ULONG, DIGITS_PER_ULONG, ud, 2081 v.nbits, v.ndigits, v.digit); 2082 2083} 2084 2085// The rest of the operators in this section are included from 2086// sc_nbcommon.cpp. 2087 2088 2089// ---------------------------------------------------------------------------- 2090// SECTION: DIVISION operators: /, /= 2091// ---------------------------------------------------------------------------- 2092 2093// Cases to consider when finding the quotient q = floor(u/v): 2094// Note that u = q * v + r for r < q. 2095// 1. 0 / 0 or u / 0 => error 2096// 2. 0 / v => 0 = 0 * v + 0 2097// 3. u / v && u = v => u = 1 * u + 0 - u or v can be 1 or -1 2098// 4. u / v && u < v => u = 0 * v + u - u can be 1 or -1 2099// 5. u / v && u > v => u = q * v + r - v can be 1 or -1 2100 2101sc_signed 2102operator/(const sc_unsigned& u, const sc_signed& v) 2103{ 2104 2105 small_type s = mul_signs(u.sgn, v.sgn); 2106 2107 if (s == SC_ZERO) { 2108 div_by_zero(v.sgn); // case 1 2109 return sc_signed(); // case 2 2110 } 2111 2112 // other cases 2113 return div_signed_friend(s, u.nbits, u.ndigits, u.digit, 2114 v.nbits, v.ndigits, v.digit); 2115 2116} 2117 2118 2119sc_signed 2120operator/(const sc_signed& u, const sc_unsigned& v) 2121{ 2122 2123 small_type s = mul_signs(u.sgn, v.sgn); 2124 2125 if (s == SC_ZERO) { 2126 div_by_zero(v.sgn); // case 1 2127 return sc_signed(); // case 2 2128 } 2129 2130 // other cases 2131 return div_signed_friend(s, u.nbits, u.ndigits, u.digit, 2132 v.nbits, v.ndigits, v.digit); 2133 2134} 2135 2136 2137sc_signed 2138operator/(const sc_signed& u, const sc_signed& v) 2139{ 2140 2141 small_type s = mul_signs(u.sgn, v.sgn); 2142 2143 if (s == SC_ZERO) { 2144 div_by_zero(v.sgn); // case 1 2145 return sc_signed(); // case 2 2146 } 2147 2148 // other cases 2149 return div_signed_friend(s, u.nbits, u.ndigits, u.digit, 2150 v.nbits, v.ndigits, v.digit); 2151 2152} 2153 2154 2155sc_signed 2156operator/(const sc_signed& u, int64 v) 2157{ 2158 2159 small_type s = mul_signs(u.sgn, get_sign(v)); 2160 2161 if (s == SC_ZERO) { 2162 div_by_zero(v); // case 1 2163 return sc_signed(); // case 2 2164 } 2165 2166 CONVERT_INT64_2(v); 2167 2168 // other cases 2169 return div_signed_friend(s, u.nbits, u.ndigits, u.digit, 2170 BITS_PER_UINT64, DIGITS_PER_UINT64, vd); 2171 2172} 2173 2174 2175sc_signed 2176operator/(int64 u, const sc_signed& v) 2177{ 2178 2179 small_type s = mul_signs(v.sgn, get_sign(u)); 2180 2181 if (s == SC_ZERO) { 2182 div_by_zero(v.sgn); // case 1 2183 return sc_signed(); // case 2 2184 } 2185 2186 CONVERT_INT64_2(u); 2187 2188 // other cases 2189 return div_signed_friend(s, BITS_PER_UINT64, DIGITS_PER_UINT64, ud, 2190 v.nbits, v.ndigits, v.digit); 2191 2192} 2193 2194 2195sc_signed 2196operator/(const sc_unsigned& u, int64 v) 2197{ 2198 2199 small_type s = mul_signs(u.sgn, get_sign(v)); 2200 2201 if (s == SC_ZERO) { 2202 div_by_zero(v); // case 1 2203 return sc_signed(); // case 2 2204 } 2205 2206 CONVERT_INT64_2(v); 2207 2208 // other cases 2209 return div_signed_friend(s, u.nbits, u.ndigits, u.digit, 2210 BITS_PER_UINT64, DIGITS_PER_UINT64, vd); 2211 2212} 2213 2214 2215sc_signed 2216operator/(int64 u, const sc_unsigned& v) 2217{ 2218 2219 small_type s = mul_signs(v.sgn, get_sign(u)); 2220 2221 if (s == SC_ZERO) { 2222 div_by_zero(v.sgn); // case 1 2223 return sc_signed(); // case 2 2224 } 2225 2226 CONVERT_INT64_2(u); 2227 2228 // other cases 2229 return div_signed_friend(s, BITS_PER_UINT64, DIGITS_PER_UINT64, ud, 2230 v.nbits, v.ndigits, v.digit); 2231 2232} 2233 2234 2235sc_signed 2236operator/(const sc_signed& u, uint64 v) 2237{ 2238 2239 small_type s = mul_signs(u.sgn, get_sign(v)); 2240 2241 if (s == SC_ZERO) { 2242 div_by_zero(v); // case 1 2243 return sc_signed(); // case 2 2244 } 2245 2246 CONVERT_INT64_2(v); 2247 2248 // other cases 2249 return div_signed_friend(s, u.nbits, u.ndigits, u.digit, 2250 BITS_PER_UINT64, DIGITS_PER_UINT64, vd); 2251 2252} 2253 2254 2255sc_signed 2256operator/(uint64 u, const sc_signed& v) 2257{ 2258 2259 small_type s = mul_signs(v.sgn, get_sign(u)); 2260 2261 if (s == SC_ZERO) { 2262 div_by_zero(v.sgn); // case 1 2263 return sc_signed(); // case 2 2264 2265 } 2266 2267 CONVERT_INT64_2(u); 2268 2269 // other cases 2270 return div_signed_friend(s, BITS_PER_UINT64, DIGITS_PER_UINT64, ud, 2271 v.nbits, v.ndigits, v.digit); 2272 2273} 2274 2275 2276sc_signed 2277operator/(const sc_signed& u, long v) 2278{ 2279 2280 small_type s = mul_signs(u.sgn, get_sign(v)); 2281 2282 if (s == SC_ZERO) { 2283 div_by_zero(v); // case 1 2284 return sc_signed(); // case 2 2285 } 2286 2287 CONVERT_LONG_2(v); 2288 2289 // other cases 2290 return div_signed_friend(s, u.nbits, u.ndigits, u.digit, 2291 BITS_PER_ULONG, DIGITS_PER_ULONG, vd); 2292 2293} 2294 2295 2296sc_signed 2297operator/(long u, const sc_signed& v) 2298{ 2299 2300 small_type s = mul_signs(v.sgn, get_sign(u)); 2301 2302 if (s == SC_ZERO) { 2303 div_by_zero(v.sgn); // case 1 2304 return sc_signed(); // case 2 2305 } 2306 2307 CONVERT_LONG_2(u); 2308 2309 // other cases 2310 return div_signed_friend(s, BITS_PER_ULONG, DIGITS_PER_ULONG, ud, 2311 v.nbits, v.ndigits, v.digit); 2312 2313} 2314 2315 2316sc_signed 2317operator/(const sc_unsigned& u, long v) 2318{ 2319 2320 small_type s = mul_signs(u.sgn, get_sign(v)); 2321 2322 if (s == SC_ZERO) { 2323 div_by_zero(v); // case 1 2324 return sc_signed(); // case 2 2325 } 2326 2327 CONVERT_LONG_2(v); 2328 2329 // other cases 2330 return div_signed_friend(s, u.nbits, u.ndigits, u.digit, 2331 BITS_PER_ULONG, DIGITS_PER_ULONG, vd); 2332 2333} 2334 2335 2336sc_signed 2337operator/(long u, const sc_unsigned& v) 2338{ 2339 2340 small_type s = mul_signs(v.sgn, get_sign(u)); 2341 2342 if (s == SC_ZERO) { 2343 div_by_zero(v.sgn); // case 1 2344 return sc_signed(); // case 2 2345 } 2346 2347 CONVERT_LONG_2(u); 2348 2349 // other cases 2350 return div_signed_friend(s, BITS_PER_ULONG, DIGITS_PER_ULONG, ud, 2351 v.nbits, v.ndigits, v.digit); 2352 2353} 2354 2355 2356sc_signed 2357operator/(const sc_signed& u, unsigned long v) 2358{ 2359 2360 small_type s = mul_signs(u.sgn, get_sign(v)); 2361 2362 if (s == SC_ZERO) { 2363 div_by_zero(v); // case 1 2364 return sc_signed(); // case 2 2365 } 2366 2367 CONVERT_LONG_2(v); 2368 2369 // other cases 2370 return div_signed_friend(s, u.nbits, u.ndigits, u.digit, 2371 BITS_PER_ULONG, DIGITS_PER_ULONG, vd); 2372 2373} 2374 2375 2376sc_signed 2377operator/(unsigned long u, const sc_signed& v) 2378{ 2379 2380 small_type s = mul_signs(v.sgn, get_sign(u)); 2381 2382 if (s == SC_ZERO) { 2383 div_by_zero(v.sgn); // case 1 2384 return sc_signed(); // case 2 2385 2386 } 2387 2388 CONVERT_LONG_2(u); 2389 2390 // other cases 2391 return div_signed_friend(s, BITS_PER_ULONG, DIGITS_PER_ULONG, ud, 2392 v.nbits, v.ndigits, v.digit); 2393 2394} 2395 2396// The rest of the operators in this section are included from 2397// sc_nbcommon.cpp. 2398 2399 2400// ---------------------------------------------------------------------------- 2401// SECTION: MOD operators: %, %=. 2402// ---------------------------------------------------------------------------- 2403 2404// Cases to consider when finding the remainder r = u % v: 2405// Note that u = q * v + r for r < q. 2406// 1. 0 % 0 or u % 0 => error 2407// 2. 0 % v => 0 = 0 * v + 0 2408// 3. u % v && u = v => u = 1 * u + 0 - u or v can be 1 or -1 2409// 4. u % v && u < v => u = 0 * v + u - u can be 1 or -1 2410// 5. u % v && u > v => u = q * v + r - v can be 1 or -1 2411 2412sc_signed 2413operator%(const sc_unsigned& u, const sc_signed& v) 2414{ 2415 2416 if ((u.sgn == SC_ZERO) || (v.sgn == SC_ZERO)) { 2417 div_by_zero(v.sgn); // case 1 2418 return sc_signed(); // case 2 2419 } 2420 2421 // other cases 2422 return mod_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit, 2423 v.nbits, v.ndigits, v.digit); 2424} 2425 2426 2427sc_signed 2428operator%(const sc_signed& u, const sc_unsigned& v) 2429{ 2430 2431 if ((u.sgn == SC_ZERO) || (v.sgn == SC_ZERO)) { 2432 div_by_zero(v.sgn); // case 1 2433 return sc_signed(); // case 2 2434 } 2435 2436 // other cases 2437 return mod_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit, 2438 v.nbits, v.ndigits, v.digit); 2439} 2440 2441 2442sc_signed 2443operator%(const sc_signed& u, const sc_signed& v) 2444{ 2445 2446 if ((u.sgn == SC_ZERO) || (v.sgn == SC_ZERO)) { 2447 div_by_zero(v.sgn); // case 1 2448 return sc_signed(); // case 2 2449 } 2450 2451 // other cases 2452 return mod_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit, 2453 v.nbits, v.ndigits, v.digit); 2454} 2455 2456 2457sc_signed 2458operator%(const sc_signed& u, int64 v) 2459{ 2460 2461 small_type vs = get_sign(v); 2462 2463 if ((u.sgn == SC_ZERO) || (vs == SC_ZERO)) { 2464 div_by_zero(v); // case 1 2465 return sc_signed(); // case 2 2466 } 2467 2468 CONVERT_INT64_2(v); 2469 2470 // other cases 2471 return mod_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit, 2472 BITS_PER_UINT64, DIGITS_PER_UINT64, vd); 2473 2474} 2475 2476 2477sc_signed 2478operator%(int64 u, const sc_signed& v) 2479{ 2480 2481 small_type us = get_sign(u); 2482 2483 if ((us == SC_ZERO) || (v.sgn == SC_ZERO)) { 2484 div_by_zero(v.sgn); // case 1 2485 return sc_signed(); // case 2 2486 } 2487 2488 CONVERT_INT64_2(u); 2489 2490 // other cases 2491 return mod_signed_friend(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud, 2492 v.nbits, v.ndigits, v.digit); 2493 2494} 2495 2496 2497sc_signed 2498operator%(const sc_unsigned& u, int64 v) 2499{ 2500 2501 small_type vs = get_sign(v); 2502 2503 if ((u.sgn == SC_ZERO) || (vs == SC_ZERO)) { 2504 div_by_zero(v); // case 1 2505 return sc_signed(); // case 2 2506 } 2507 2508 CONVERT_INT64_2(v); 2509 2510 // other cases 2511 return mod_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit, 2512 BITS_PER_UINT64, DIGITS_PER_UINT64, vd); 2513 2514} 2515 2516 2517sc_signed 2518operator%(int64 u, const sc_unsigned& v) 2519{ 2520 2521 small_type us = get_sign(u); 2522 2523 if ((us == SC_ZERO) || (v.sgn == SC_ZERO)) { 2524 div_by_zero(v.sgn); // case 1 2525 return sc_signed(); // case 2 2526 } 2527 2528 CONVERT_INT64_2(u); 2529 2530 // other cases 2531 return mod_signed_friend(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud, 2532 v.nbits, v.ndigits, v.digit); 2533 2534} 2535 2536 2537sc_signed 2538operator%(const sc_signed& u, uint64 v) 2539{ 2540 2541 if ((u.sgn == SC_ZERO) || (v == 0)) { 2542 div_by_zero(v); // case 1 2543 return sc_signed(); // case 2 2544 } 2545 2546 CONVERT_INT64_2(v); 2547 2548 // other cases 2549 return mod_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit, 2550 BITS_PER_UINT64, DIGITS_PER_UINT64, vd); 2551 2552} 2553 2554 2555sc_signed 2556operator%(uint64 u, const sc_signed& v) 2557{ 2558 2559 if ((u == 0) || (v.sgn == SC_ZERO)) { 2560 div_by_zero(v.sgn); // case 1 2561 return sc_signed(); // case 2 2562 } 2563 2564 CONVERT_INT64(u); 2565 2566 // other cases 2567 return mod_signed_friend(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud, 2568 v.nbits, v.ndigits, v.digit); 2569 2570} 2571 2572 2573sc_signed 2574operator%(const sc_signed& u, long v) 2575{ 2576 2577 small_type vs = get_sign(v); 2578 2579 if ((u.sgn == SC_ZERO) || (vs == SC_ZERO)) { 2580 div_by_zero(v); // case 1 2581 return sc_signed(); // case 2 2582 } 2583 2584 CONVERT_LONG_2(v); 2585 2586 // other cases 2587 return mod_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit, 2588 BITS_PER_ULONG, DIGITS_PER_ULONG, vd); 2589} 2590 2591 2592sc_signed 2593operator%(long u, const sc_signed& v) 2594{ 2595 2596 small_type us = get_sign(u); 2597 2598 if ((us == SC_ZERO) || (v.sgn == SC_ZERO)) { 2599 div_by_zero(v.sgn); // case 1 2600 return sc_signed(); // case 2 2601 } 2602 2603 CONVERT_LONG_2(u); 2604 2605 // other cases 2606 return mod_signed_friend(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud, 2607 v.nbits, v.ndigits, v.digit); 2608 2609} 2610 2611 2612sc_signed 2613operator%(const sc_unsigned& u, long v) 2614{ 2615 2616 small_type vs = get_sign(v); 2617 2618 if ((u.sgn == SC_ZERO) || (vs == SC_ZERO)) { 2619 div_by_zero(v); // case 1 2620 return sc_signed(); // case 2 2621 } 2622 2623 CONVERT_LONG_2(v); 2624 2625 // other cases 2626 return mod_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit, 2627 BITS_PER_ULONG, DIGITS_PER_ULONG, vd); 2628} 2629 2630 2631sc_signed 2632operator%(long u, const sc_unsigned& v) 2633{ 2634 2635 small_type us = get_sign(u); 2636 2637 if ((us == SC_ZERO) || (v.sgn == SC_ZERO)) { 2638 div_by_zero(v.sgn); // case 1 2639 return sc_signed(); // case 2 2640 } 2641 2642 CONVERT_LONG_2(u); 2643 2644 // other cases 2645 return mod_signed_friend(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud, 2646 v.nbits, v.ndigits, v.digit); 2647 2648} 2649 2650 2651sc_signed 2652operator%(const sc_signed& u, unsigned long v) 2653{ 2654 2655 if ((u.sgn == SC_ZERO) || (v == 0)) { 2656 div_by_zero(v); // case 1 2657 return sc_signed(); // case 2 2658 } 2659 2660 CONVERT_LONG_2(v); 2661 2662 // other cases 2663 return mod_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit, 2664 BITS_PER_ULONG, DIGITS_PER_ULONG, vd); 2665 2666} 2667 2668 2669sc_signed 2670operator%(unsigned long u, const sc_signed& v) 2671{ 2672 2673 if ((u == 0) || (v.sgn == SC_ZERO)) { 2674 div_by_zero(v.sgn); // case 1 2675 return sc_signed(); // case 2 2676 } 2677 2678 CONVERT_LONG(u); 2679 2680 // other cases 2681 return mod_signed_friend(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud, 2682 v.nbits, v.ndigits, v.digit); 2683 2684} 2685 2686// The rest of the operators in this section are included from 2687// sc_nbcommon.cpp. 2688 2689 2690// ---------------------------------------------------------------------------- 2691// SECTION: Bitwise AND operators: &, &= 2692// ---------------------------------------------------------------------------- 2693 2694// Cases to consider when computing u & v: 2695// 1. u & 0 = 0 & v = 0 2696// 2. u & v => sgn = + 2697// 3. (-u) & (-v) => sgn = - 2698// 4. u & (-v) => sgn = + 2699// 5. (-u) & v => sgn = + 2700 2701sc_signed 2702operator&(const sc_unsigned& u, const sc_signed& v) 2703{ 2704 2705 if ((u.sgn == SC_ZERO) || (v.sgn == SC_ZERO)) // case 1 2706 return sc_signed(); 2707 2708 // other cases 2709 return and_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit, 2710 v.sgn, v.nbits, v.ndigits, v.digit); 2711 2712} 2713 2714 2715sc_signed 2716operator&(const sc_signed& u, const sc_unsigned& v) 2717{ 2718 2719 if ((u.sgn == SC_ZERO) || (v.sgn == SC_ZERO)) // case 1 2720 return sc_signed(); 2721 2722 // other cases 2723 return and_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit, 2724 v.sgn, v.nbits, v.ndigits, v.digit); 2725 2726} 2727 2728 2729sc_signed 2730operator&(const sc_signed& u, const sc_signed& v) 2731{ 2732 2733 if ((u.sgn == SC_ZERO) || (v.sgn == SC_ZERO)) // case 1 2734 return sc_signed(); 2735 2736 // other cases 2737 return and_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit, 2738 v.sgn, v.nbits, v.ndigits, v.digit); 2739 2740} 2741 2742 2743sc_signed 2744operator&(const sc_signed& u, int64 v) 2745{ 2746 2747 if ((u.sgn == SC_ZERO) || (v == 0)) // case 1 2748 return sc_signed(); 2749 2750 CONVERT_INT64(v); 2751 2752 // other cases 2753 return and_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit, 2754 vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd); 2755 2756} 2757 2758 2759sc_signed 2760operator&(int64 u, const sc_signed& v) 2761{ 2762 2763 if ((u == 0) || (v.sgn == SC_ZERO)) // case 1 2764 return sc_signed(); 2765 2766 CONVERT_INT64(u); 2767 2768 // other cases 2769 return and_signed_friend(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud, 2770 v.sgn, v.nbits, v.ndigits, v.digit); 2771 2772} 2773 2774 2775sc_signed 2776operator&(const sc_unsigned& u, int64 v) 2777{ 2778 2779 if ((u.sgn == SC_ZERO) || (v == 0)) // case 1 2780 return sc_signed(); 2781 2782 CONVERT_INT64(v); 2783 2784 // other cases 2785 return and_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit, 2786 vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd); 2787 2788} 2789 2790 2791sc_signed 2792operator&(int64 u, const sc_unsigned& v) 2793{ 2794 2795 if ((u == 0) || (v.sgn == SC_ZERO)) // case 1 2796 return sc_signed(); 2797 2798 CONVERT_INT64(u); 2799 2800 // other cases 2801 return and_signed_friend(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud, 2802 v.sgn, v.nbits, v.ndigits, v.digit); 2803 2804} 2805 2806 2807sc_signed 2808operator&(const sc_signed& u, uint64 v) 2809{ 2810 2811 if ((u.sgn == SC_ZERO) || (v == 0)) // case 1 2812 return sc_signed(); 2813 2814 CONVERT_INT64(v); 2815 2816 // other cases 2817 return and_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit, 2818 vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd); 2819 2820} 2821 2822 2823sc_signed 2824operator&(uint64 u, const sc_signed& v) 2825{ 2826 2827 if ((u == 0) || (v.sgn == SC_ZERO)) // case 1 2828 return sc_signed(); 2829 2830 CONVERT_INT64(u); 2831 2832 // other cases 2833 return and_signed_friend(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud, 2834 v.sgn, v.nbits, v.ndigits, v.digit); 2835 2836} 2837 2838 2839sc_signed 2840operator&(const sc_signed& u, long v) 2841{ 2842 2843 if ((u.sgn == SC_ZERO) || (v == 0)) // case 1 2844 return sc_signed(); 2845 2846 CONVERT_LONG(v); 2847 2848 // other cases 2849 return and_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit, 2850 vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd); 2851 2852} 2853 2854 2855sc_signed 2856operator&(long u, const sc_signed& v) 2857{ 2858 2859 if ((u == 0) || (v.sgn == SC_ZERO)) // case 1 2860 return sc_signed(); 2861 2862 CONVERT_LONG(u); 2863 2864 // other cases 2865 return and_signed_friend(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud, 2866 v.sgn, v.nbits, v.ndigits, v.digit); 2867 2868} 2869 2870 2871sc_signed 2872operator&(const sc_unsigned& u, long v) 2873{ 2874 2875 if ((u.sgn == SC_ZERO) || (v == 0)) // case 1 2876 return sc_signed(); 2877 2878 CONVERT_LONG(v); 2879 2880 // other cases 2881 return and_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit, 2882 vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd); 2883 2884} 2885 2886 2887sc_signed 2888operator&(long u, const sc_unsigned& v) 2889{ 2890 2891 if ((u == 0) || (v.sgn == SC_ZERO)) // case 1 2892 return sc_signed(); 2893 2894 CONVERT_LONG(u); 2895 2896 // other cases 2897 return and_signed_friend(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud, 2898 v.sgn, v.nbits, v.ndigits, v.digit); 2899 2900} 2901 2902 2903sc_signed 2904operator&(const sc_signed& u, unsigned long v) 2905{ 2906 2907 if ((u.sgn == SC_ZERO) || (v == 0)) // case 1 2908 return sc_signed(); 2909 2910 CONVERT_LONG(v); 2911 2912 // other cases 2913 return and_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit, 2914 vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd); 2915 2916} 2917 2918 2919sc_signed 2920operator&(unsigned long u, const sc_signed& v) 2921{ 2922 2923 if ((u == 0) || (v.sgn == SC_ZERO)) // case 1 2924 return sc_signed(); 2925 2926 CONVERT_LONG(u); 2927 2928 // other cases 2929 return and_signed_friend(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud, 2930 v.sgn, v.nbits, v.ndigits, v.digit); 2931 2932} 2933 2934// The rest of the operators in this section are included from 2935// sc_nbcommon.cpp. 2936 2937 2938// ---------------------------------------------------------------------------- 2939// SECTION: Bitwise OR operators: |, |= 2940// ---------------------------------------------------------------------------- 2941 2942// Cases to consider when computing u | v: 2943// 1. u | 0 = u 2944// 2. 0 | v = v 2945// 3. u | v => sgn = + 2946// 4. (-u) | (-v) => sgn = - 2947// 5. u | (-v) => sgn = - 2948// 6. (-u) | v => sgn = - 2949 2950sc_signed 2951operator|(const sc_unsigned& u, const sc_signed& v) 2952{ 2953 2954 if (v.sgn == SC_ZERO) // case 1 2955 return sc_signed(u); 2956 2957 if (u.sgn == SC_ZERO) // case 2 2958 return sc_signed(v); 2959 2960 // other cases 2961 return or_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit, 2962 v.sgn, v.nbits, v.ndigits, v.digit); 2963 2964} 2965 2966 2967sc_signed 2968operator|(const sc_signed& u, const sc_unsigned& v) 2969{ 2970 2971 if (v.sgn == SC_ZERO) // case 1 2972 return sc_signed(u); 2973 2974 if (u.sgn == SC_ZERO) // case 2 2975 return sc_signed(v); 2976 2977 // other cases 2978 return or_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit, 2979 v.sgn, v.nbits, v.ndigits, v.digit); 2980 2981} 2982 2983 2984sc_signed 2985operator|(const sc_signed& u, const sc_signed& v) 2986{ 2987 2988 if (v.sgn == SC_ZERO) // case 1 2989 return sc_signed(u); 2990 2991 if (u.sgn == SC_ZERO) // case 2 2992 return sc_signed(v); 2993 2994 // other cases 2995 return or_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit, 2996 v.sgn, v.nbits, v.ndigits, v.digit); 2997 2998} 2999 3000 3001sc_signed 3002operator|(const sc_signed& u, int64 v) 3003{ 3004 3005 if (v == 0) // case 1 3006 return sc_signed(u); 3007 3008 CONVERT_INT64(v); 3009 3010 if (u.sgn == SC_ZERO) // case 2 3011 return sc_signed(vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd, false); 3012 3013 // other cases 3014 return or_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit, 3015 vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd); 3016 3017} 3018 3019 3020sc_signed 3021operator|(int64 u, const sc_signed& v) 3022{ 3023 3024 if (u == 0) 3025 return sc_signed(v); 3026 3027 CONVERT_INT64(u); 3028 3029 if (v.sgn == SC_ZERO) 3030 return sc_signed(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud, false); 3031 3032 // other cases 3033 return or_signed_friend(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud, 3034 v.sgn, v.nbits, v.ndigits, v.digit); 3035 3036} 3037 3038 3039sc_signed 3040operator|(const sc_unsigned& u, int64 v) 3041{ 3042 3043 if (v == 0) // case 1 3044 return sc_signed(u); 3045 3046 CONVERT_INT64(v); 3047 3048 if (u.sgn == SC_ZERO) // case 2 3049 return sc_signed(vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd, false); 3050 3051 // other cases 3052 return or_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit, 3053 vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd); 3054 3055} 3056 3057 3058sc_signed 3059operator|(int64 u, const sc_unsigned& v) 3060{ 3061 3062 if (u == 0) 3063 return sc_signed(v); 3064 3065 CONVERT_INT64(u); 3066 3067 if (v.sgn == SC_ZERO) 3068 return sc_signed(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud, false); 3069 3070 // other cases 3071 return or_signed_friend(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud, 3072 v.sgn, v.nbits, v.ndigits, v.digit); 3073 3074} 3075 3076 3077sc_signed 3078operator|(const sc_signed& u, uint64 v) 3079{ 3080 3081 if (v == 0) // case 1 3082 return sc_signed(u); 3083 3084 CONVERT_INT64(v); 3085 3086 if (u.sgn == SC_ZERO) // case 2 3087 return sc_signed(vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd, false); 3088 3089 // other cases 3090 return or_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit, 3091 vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd); 3092 3093} 3094 3095 3096sc_signed 3097operator|(uint64 u, const sc_signed& v) 3098{ 3099 3100 if (u == 0) 3101 return sc_signed(v); 3102 3103 CONVERT_INT64(u); 3104 3105 if (v.sgn == SC_ZERO) 3106 return sc_signed(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud, false); 3107 3108 // other cases 3109 return or_signed_friend(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud, 3110 v.sgn, v.nbits, v.ndigits, v.digit); 3111 3112} 3113 3114 3115sc_signed 3116operator|(const sc_signed& u, long v) 3117{ 3118 3119 if (v == 0) // case 1 3120 return sc_signed(u); 3121 3122 CONVERT_LONG(v); 3123 3124 if (u.sgn == SC_ZERO) // case 2 3125 return sc_signed(vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd, false); 3126 3127 // other cases 3128 return or_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit, 3129 vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd); 3130 3131} 3132 3133 3134sc_signed 3135operator|(long u, const sc_signed& v) 3136{ 3137 3138 if (u == 0) 3139 return sc_signed(v); 3140 3141 CONVERT_LONG(u); 3142 3143 if (v.sgn == SC_ZERO) 3144 return sc_signed(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud, false); 3145 3146 // other cases 3147 return or_signed_friend(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud, 3148 v.sgn, v.nbits, v.ndigits, v.digit); 3149 3150} 3151 3152 3153sc_signed 3154operator|(const sc_unsigned& u, long v) 3155{ 3156 3157 if (v == 0) // case 1 3158 return sc_signed(u); 3159 3160 CONVERT_LONG(v); 3161 3162 if (u.sgn == SC_ZERO) // case 2 3163 return sc_signed(vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd, false); 3164 3165 // other cases 3166 return or_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit, 3167 vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd); 3168 3169} 3170 3171 3172sc_signed 3173operator|(long u, const sc_unsigned& v) 3174{ 3175 3176 if (u == 0) 3177 return sc_signed(v); 3178 3179 CONVERT_LONG(u); 3180 3181 if (v.sgn == SC_ZERO) 3182 return sc_signed(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud, false); 3183 3184 // other cases 3185 return or_signed_friend(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud, 3186 v.sgn, v.nbits, v.ndigits, v.digit); 3187 3188} 3189 3190 3191sc_signed 3192operator|(const sc_signed& u, unsigned long v) 3193{ 3194 3195 if (v == 0) // case 1 3196 return sc_signed(u); 3197 3198 CONVERT_LONG(v); 3199 3200 if (u.sgn == SC_ZERO) // case 2 3201 return sc_signed(vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd, false); 3202 3203 // other cases 3204 return or_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit, 3205 vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd); 3206 3207} 3208 3209 3210sc_signed 3211operator|(unsigned long u, const sc_signed& v) 3212{ 3213 3214 if (u == 0) 3215 return sc_signed(v); 3216 3217 CONVERT_LONG(u); 3218 3219 if (v.sgn == SC_ZERO) 3220 return sc_signed(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud, false); 3221 3222 // other cases 3223 return or_signed_friend(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud, 3224 v.sgn, v.nbits, v.ndigits, v.digit); 3225 3226} 3227 3228// The rest of the operators in this section are included from 3229// sc_nbcommon.cpp. 3230 3231 3232// ---------------------------------------------------------------------------- 3233// SECTION: Bitwise XOR operators: ^, ^= 3234// ---------------------------------------------------------------------------- 3235 3236// Cases to consider when computing u ^ v: 3237// Note that u ^ v = (~u & v) | (u & ~v). 3238// 1. u ^ 0 = u 3239// 2. 0 ^ v = v 3240// 3. u ^ v => sgn = + 3241// 4. (-u) ^ (-v) => sgn = - 3242// 5. u ^ (-v) => sgn = - 3243// 6. (-u) ^ v => sgn = + 3244 3245sc_signed 3246operator^(const sc_unsigned& u, const sc_signed& v) 3247{ 3248 3249 if (v.sgn == SC_ZERO) // case 1 3250 return sc_signed(u); 3251 3252 if (u.sgn == SC_ZERO) // case 2 3253 return sc_signed(v); 3254 3255 // other cases 3256 return xor_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit, 3257 v.sgn, v.nbits, v.ndigits, v.digit); 3258 3259} 3260 3261 3262sc_signed 3263operator^(const sc_signed& u, const sc_unsigned& v) 3264{ 3265 3266 if (v.sgn == SC_ZERO) // case 1 3267 return sc_signed(u); 3268 3269 if (u.sgn == SC_ZERO) // case 2 3270 return sc_signed(v); 3271 3272 // other cases 3273 return xor_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit, 3274 v.sgn, v.nbits, v.ndigits, v.digit); 3275 3276} 3277 3278 3279sc_signed 3280operator^(const sc_signed& u, const sc_signed& v) 3281{ 3282 3283 if (v.sgn == SC_ZERO) // case 1 3284 return sc_signed(u); 3285 3286 if (u.sgn == SC_ZERO) // case 2 3287 return sc_signed(v); 3288 3289 // other cases 3290 return xor_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit, 3291 v.sgn, v.nbits, v.ndigits, v.digit); 3292 3293} 3294 3295 3296sc_signed 3297operator^(const sc_signed& u, int64 v) 3298{ 3299 3300 if (v == 0) // case 1 3301 return sc_signed(u); 3302 3303 CONVERT_INT64(v); 3304 3305 if (u.sgn == SC_ZERO) // case 2 3306 return sc_signed(vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd, false); 3307 3308 // other cases 3309 return xor_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit, 3310 vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd); 3311 3312} 3313 3314 3315sc_signed 3316operator^(int64 u, const sc_signed& v) 3317{ 3318 3319 if (u == 0) 3320 return sc_signed(v); 3321 3322 CONVERT_INT64(u); 3323 3324 if (v.sgn == SC_ZERO) 3325 return sc_signed(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud, false); 3326 3327 // other cases 3328 return xor_signed_friend(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud, 3329 v.sgn, v.nbits, v.ndigits, v.digit); 3330 3331} 3332 3333 3334sc_signed 3335operator^(const sc_unsigned& u, int64 v) 3336{ 3337 3338 if (v == 0) // case 1 3339 return sc_signed(u); 3340 3341 CONVERT_INT64(v); 3342 3343 if (u.sgn == SC_ZERO) // case 2 3344 return sc_signed(vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd, false); 3345 3346 // other cases 3347 return xor_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit, 3348 vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd); 3349 3350} 3351 3352 3353sc_signed 3354operator^(int64 u, const sc_unsigned& v) 3355{ 3356 3357 if (u == 0) 3358 return sc_signed(v); 3359 3360 CONVERT_INT64(u); 3361 3362 if (v.sgn == SC_ZERO) 3363 return sc_signed(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud, false); 3364 3365 // other cases 3366 return xor_signed_friend(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud, 3367 v.sgn, v.nbits, v.ndigits, v.digit); 3368 3369} 3370 3371 3372sc_signed 3373operator^(const sc_signed& u, uint64 v) 3374{ 3375 3376 if (v == 0) // case 1 3377 return sc_signed(u); 3378 3379 CONVERT_INT64(v); 3380 3381 if (u.sgn == SC_ZERO) // case 2 3382 return sc_signed(vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd, false); 3383 3384 // other cases 3385 return xor_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit, 3386 vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd); 3387 3388} 3389 3390sc_signed 3391operator^(uint64 u, const sc_signed& v) 3392{ 3393 if (u == 0) 3394 return sc_signed(v); 3395 3396 CONVERT_INT64(u); 3397 3398 if (v.sgn == SC_ZERO) 3399 return sc_signed(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud, false); 3400 3401 // other cases 3402 return xor_signed_friend(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud, 3403 v.sgn, v.nbits, v.ndigits, v.digit); 3404 3405} 3406 3407 3408sc_signed 3409operator^(const sc_signed& u, long v) 3410{ 3411 3412 if (v == 0) // case 1 3413 return sc_signed(u); 3414 3415 CONVERT_LONG(v); 3416 3417 if (u.sgn == SC_ZERO) // case 2 3418 return sc_signed(vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd, false); 3419 3420 // other cases 3421 return xor_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit, 3422 vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd); 3423 3424} 3425 3426 3427sc_signed 3428operator^(long u, const sc_signed& v) 3429{ 3430 3431 if (u == 0) 3432 return sc_signed(v); 3433 3434 CONVERT_LONG(u); 3435 3436 if (v.sgn == SC_ZERO) 3437 return sc_signed(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud, false); 3438 3439 // other cases 3440 return xor_signed_friend(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud, 3441 v.sgn, v.nbits, v.ndigits, v.digit); 3442 3443} 3444 3445 3446sc_signed 3447operator^(const sc_unsigned& u, long v) 3448{ 3449 3450 if (v == 0) // case 1 3451 return sc_signed(u); 3452 3453 CONVERT_LONG(v); 3454 3455 if (u.sgn == SC_ZERO) // case 2 3456 return sc_signed(vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd, false); 3457 3458 // other cases 3459 return xor_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit, 3460 vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd); 3461 3462} 3463 3464 3465sc_signed 3466operator^(long u, const sc_unsigned& v) 3467{ 3468 3469 if (u == 0) 3470 return sc_signed(v); 3471 3472 CONVERT_LONG(u); 3473 3474 if (v.sgn == SC_ZERO) 3475 return sc_signed(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud, false); 3476 3477 // other cases 3478 return xor_signed_friend(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud, 3479 v.sgn, v.nbits, v.ndigits, v.digit); 3480 3481} 3482 3483 3484sc_signed 3485operator^(const sc_signed& u, unsigned long v) 3486{ 3487 3488 if (v == 0) // case 1 3489 return sc_signed(u); 3490 3491 CONVERT_LONG(v); 3492 3493 if (u.sgn == SC_ZERO) // case 2 3494 return sc_signed(vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd, false); 3495 3496 // other cases 3497 return xor_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit, 3498 vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd); 3499 3500} 3501 3502sc_signed 3503operator^(unsigned long u, const sc_signed& v) 3504{ 3505 if (u == 0) 3506 return sc_signed(v); 3507 3508 CONVERT_LONG(u); 3509 3510 if (v.sgn == SC_ZERO) 3511 return sc_signed(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud, false); 3512 3513 // other cases 3514 return xor_signed_friend(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud, 3515 v.sgn, v.nbits, v.ndigits, v.digit); 3516 3517} 3518 3519// The rest of the operators in this section are included from 3520// sc_nbcommon.cpp. 3521 3522 3523// ---------------------------------------------------------------------------- 3524// SECTION: Bitwise NOT operator: ~ 3525// ---------------------------------------------------------------------------- 3526 3527// Operators in this section are included from sc_nbcommon.cpp. 3528 3529 3530// ---------------------------------------------------------------------------- 3531// SECTION: LEFT SHIFT operators: <<, <<= 3532// ---------------------------------------------------------------------------- 3533 3534sc_signed 3535operator<<(const sc_signed& u, const sc_unsigned& v) 3536{ 3537 if (v.sgn == SC_ZERO) 3538 return sc_signed(u); 3539 3540 return operator<<(u, v.to_ulong()); 3541} 3542 3543// The rest of the operators in this section are included from 3544// sc_nbcommon.cpp. 3545 3546 3547// ---------------------------------------------------------------------------- 3548// SECTION: RIGHT SHIFT operators: >>, >>= 3549// ---------------------------------------------------------------------------- 3550 3551sc_signed 3552operator>>(const sc_signed& u, const sc_unsigned& v) 3553{ 3554 3555 if (v.sgn == SC_ZERO) 3556 return sc_signed(u); 3557 3558 return operator>>(u, v.to_ulong()); 3559 3560} 3561 3562// The rest of the operators in this section are included from 3563// sc_nbcommon.cpp. 3564 3565 3566// ---------------------------------------------------------------------------- 3567// SECTION: Unary arithmetic operators. 3568// ---------------------------------------------------------------------------- 3569 3570sc_signed 3571operator+(const sc_signed& u) 3572{ 3573 return sc_signed(u); 3574} 3575 3576sc_signed 3577operator-(const sc_signed& u) 3578{ 3579 return sc_signed(u, -u.sgn); 3580} 3581 3582sc_signed 3583operator-(const sc_unsigned& u) 3584{ 3585 return sc_signed(u, -u.sgn); 3586} 3587 3588 3589// ---------------------------------------------------------------------------- 3590// SECTION: EQUAL operator: == 3591// ---------------------------------------------------------------------------- 3592 3593bool 3594operator==(const sc_signed& u, const sc_signed& v) 3595{ 3596 3597 if (u.sgn != v.sgn) 3598 return false; 3599 3600 if (&u == &v) 3601 return true; 3602 3603 if (vec_skip_and_cmp(u.ndigits, u.digit, v.ndigits, v.digit) != 0) 3604 return false; 3605 3606 return true; 3607 3608} 3609 3610 3611bool 3612operator==(const sc_signed& u, int64 v) 3613{ 3614 3615 CONVERT_INT64(v); 3616 3617 if (u.sgn != vs) 3618 return false; 3619 3620 if (vec_skip_and_cmp(u.ndigits, u.digit, DIGITS_PER_INT64, vd) != 0) 3621 return false; 3622 3623 return true; 3624 3625} 3626 3627 3628bool 3629operator==(int64 u, const sc_signed& v) 3630{ 3631 3632 CONVERT_INT64(u); 3633 3634 if (us != v.sgn) 3635 return false; 3636 3637 if (vec_skip_and_cmp(DIGITS_PER_INT64, ud, v.ndigits, v.digit) != 0) 3638 return false; 3639 3640 return true; 3641 3642} 3643 3644 3645bool 3646operator==(const sc_signed& u, uint64 v) 3647{ 3648 3649 CONVERT_INT64(v); 3650 3651 if (u.sgn != vs) 3652 return false; 3653 3654 if (vec_skip_and_cmp(u.ndigits, u.digit, DIGITS_PER_INT64, vd) != 0) 3655 return false; 3656 3657 return true; 3658 3659} 3660 3661 3662bool 3663operator==(uint64 u, const sc_signed& v) 3664{ 3665 3666 CONVERT_INT64(u); 3667 3668 if (us != v.sgn) 3669 return false; 3670 3671 if (vec_skip_and_cmp(DIGITS_PER_INT64, ud, v.ndigits, v.digit) != 0) 3672 return false; 3673 3674 return true; 3675 3676} 3677 3678 3679bool 3680operator==(const sc_signed& u, long v) 3681{ 3682 3683 CONVERT_LONG(v); 3684 3685 if (u.sgn != vs) 3686 return false; 3687 3688 if (vec_skip_and_cmp(u.ndigits, u.digit, DIGITS_PER_LONG, vd) != 0) 3689 return false; 3690 3691 return true; 3692 3693} 3694 3695 3696bool 3697operator==(long u, const sc_signed& v) 3698{ 3699 3700 CONVERT_LONG(u); 3701 3702 if (us != v.sgn) 3703 return false; 3704 3705 if (vec_skip_and_cmp(DIGITS_PER_LONG, ud, v.ndigits, v.digit) != 0) 3706 return false; 3707 3708 return true; 3709 3710} 3711 3712 3713bool 3714operator==(const sc_signed& u, unsigned long v) 3715{ 3716 3717 CONVERT_LONG(v); 3718 3719 if (u.sgn != vs) 3720 return false; 3721 3722 if (vec_skip_and_cmp(u.ndigits, u.digit, DIGITS_PER_LONG, vd) != 0) 3723 return false; 3724 3725 return true; 3726 3727} 3728 3729 3730bool 3731operator==(unsigned long u, const sc_signed& v) 3732{ 3733 3734 CONVERT_LONG(u); 3735 3736 if (us != v.sgn) 3737 return false; 3738 3739 if (vec_skip_and_cmp(DIGITS_PER_LONG, ud, v.ndigits, v.digit) != 0) 3740 return false; 3741 3742 return true; 3743 3744} 3745 3746 3747// ---------------------------------------------------------------------------- 3748// SECTION: NOT_EQUAL operator: != 3749// ---------------------------------------------------------------------------- 3750 3751// Operators in this section are included from sc_nbcommon.cpp. 3752 3753 3754// ---------------------------------------------------------------------------- 3755// SECTION: LESS THAN operator: < 3756// ---------------------------------------------------------------------------- 3757 3758bool 3759operator<(const sc_signed& u, const sc_signed& v) 3760{ 3761 3762 if (u.sgn < v.sgn) 3763 return true; 3764 3765 if (u.sgn > v.sgn) 3766 return false; 3767 3768 // u.sgn == v.sgn 3769 3770 if (&u == &v) 3771 return false; 3772 3773 if (u.sgn == SC_POS) { 3774 3775 if (vec_skip_and_cmp(u.ndigits, u.digit, v.ndigits, v.digit) < 0) 3776 return true; 3777 3778 } 3779 else if (u.sgn == SC_NEG) { 3780 3781 if (vec_skip_and_cmp(u.ndigits, u.digit, v.ndigits, v.digit) > 0) 3782 return true; 3783 3784 } 3785 3786 return false; 3787 3788} 3789 3790 3791bool 3792operator<(const sc_signed& u, int64 v) 3793{ 3794 3795 CONVERT_INT64(v); 3796 3797 if (u.sgn < vs) 3798 return true; 3799 3800 if (u.sgn > vs) 3801 return false; 3802 3803 // u.sgn == vs 3804 3805 if (vs == SC_POS) { 3806 3807 if (vec_skip_and_cmp(u.ndigits, u.digit, DIGITS_PER_INT64, vd) < 0) 3808 return true; 3809 3810 } 3811 else if (vs == SC_NEG) { 3812 3813 if (vec_skip_and_cmp(u.ndigits, u.digit, DIGITS_PER_INT64, vd) > 0) 3814 return true; 3815 3816 } 3817 3818 return false; 3819 3820} 3821 3822 3823bool 3824operator<(int64 u, const sc_signed& v) 3825{ 3826 3827 CONVERT_INT64(u); 3828 3829 if (us < v.sgn) 3830 return true; 3831 3832 if (us > v.sgn) 3833 return false; 3834 3835 // us == v.sgn 3836 3837 if (us == SC_POS) { 3838 3839 if (vec_skip_and_cmp(DIGITS_PER_INT64, ud, v.ndigits, v.digit) < 0) 3840 return true; 3841 3842 } 3843 else if (us == SC_NEG) { 3844 3845 if (vec_skip_and_cmp(DIGITS_PER_INT64, ud, v.ndigits, v.digit) > 0) 3846 return true; 3847 3848 } 3849 3850 return false; 3851 3852} 3853 3854 3855bool 3856operator<(const sc_signed& u, uint64 v) 3857{ 3858 3859 CONVERT_INT64(v); 3860 3861 if (u.sgn < vs) 3862 return true; 3863 3864 if (u.sgn > vs) 3865 return false; 3866 3867 // u.sgn == vs 3868 3869 if (vs == SC_POS) { 3870 3871 if (vec_skip_and_cmp(u.ndigits, u.digit, DIGITS_PER_INT64, vd) < 0) 3872 return true; 3873 3874 } 3875 3876 return false; 3877 3878} 3879 3880 3881bool 3882operator<(uint64 u, const sc_signed& v) 3883{ 3884 3885 CONVERT_INT64(u); 3886 3887 if (us < v.sgn) 3888 return true; 3889 3890 if (us > v.sgn) 3891 return false; 3892 3893 // us == v.sgn 3894 3895 if (us == SC_POS) { 3896 3897 if (vec_skip_and_cmp(DIGITS_PER_INT64, ud, v.ndigits, v.digit) < 0) 3898 return true; 3899 3900 } 3901 3902 return false; 3903 3904} 3905 3906 3907bool 3908operator<(const sc_signed& u, long v) 3909{ 3910 3911 CONVERT_LONG(v); 3912 3913 if (u.sgn < vs) 3914 return true; 3915 3916 if (u.sgn > vs) 3917 return false; 3918 3919 // u.sgn == vs 3920 3921 if (vs == SC_POS) { 3922 3923 if (vec_skip_and_cmp(u.ndigits, u.digit, DIGITS_PER_LONG, vd) < 0) 3924 return true; 3925 3926 } 3927 else if (vs == SC_NEG) { 3928 3929 if (vec_skip_and_cmp(u.ndigits, u.digit, DIGITS_PER_LONG, vd) > 0) 3930 return true; 3931 3932 } 3933 3934 return false; 3935 3936} 3937 3938 3939bool 3940operator<(long u, const sc_signed& v) 3941{ 3942 CONVERT_LONG(u); 3943 3944 if (us < v.sgn) 3945 return true; 3946 3947 if (us > v.sgn) 3948 return false; 3949 3950 // us == v.sgn 3951 3952 if (us == SC_POS) { 3953 3954 if (vec_skip_and_cmp(DIGITS_PER_LONG, ud, v.ndigits, v.digit) < 0) 3955 return true; 3956 3957 } 3958 else if (us == SC_NEG) { 3959 3960 if (vec_skip_and_cmp(DIGITS_PER_LONG, ud, v.ndigits, v.digit) > 0) 3961 return true; 3962 3963 } 3964 3965 return false; 3966} 3967 3968 3969bool 3970operator<(const sc_signed& u, unsigned long v) 3971{ 3972 CONVERT_LONG(v); 3973 3974 if (u.sgn < vs) 3975 return true; 3976 3977 if (u.sgn > vs) 3978 return false; 3979 3980 // u.sgn == vs 3981 3982 if (vs == SC_POS) { 3983 3984 if (vec_skip_and_cmp(u.ndigits, u.digit, DIGITS_PER_LONG, vd) < 0) 3985 return true; 3986 3987 } 3988 3989 return false; 3990} 3991 3992 3993bool 3994operator<(unsigned long u, const sc_signed& v) 3995{ 3996 CONVERT_LONG(u); 3997 3998 if (us < v.sgn) 3999 return true; 4000 4001 if (us > v.sgn) 4002 return false; 4003 4004 // us == v.sgn 4005 4006 if (us == SC_POS) { 4007 4008 if (vec_skip_and_cmp(DIGITS_PER_LONG, ud, v.ndigits, v.digit) < 0) 4009 return true; 4010 4011 } 4012 4013 return false; 4014} 4015 4016 4017// --------------------------------------------------------------------------- 4018// SECTION: LESS THAN or EQUAL operator: <= 4019// --------------------------------------------------------------------------- 4020 4021// Operators in this section are included from sc_nbcommon.cpp. 4022 4023 4024// --------------------------------------------------------------------------- 4025// SECTION: GREATER THAN operator: > 4026// --------------------------------------------------------------------------- 4027 4028// Operators in this section are included from sc_nbcommon.cpp. 4029 4030 4031// --------------------------------------------------------------------------- 4032// SECTION: GREATER THAN or EQUAL operator: >= 4033// --------------------------------------------------------------------------- 4034 4035// Operators in this section are included from sc_nbcommon.cpp. 4036 4037 4038// --------------------------------------------------------------------------- 4039// SECTION: Public members - Other utils. 4040// --------------------------------------------------------------------------- 4041 4042bool 4043sc_signed::iszero() const 4044{ 4045 if (sgn == SC_ZERO) 4046 return true; 4047 else if (sgn != SC_NOSIGN) 4048 return false; 4049 else 4050 return check_for_zero(ndigits, digit); 4051} 4052 4053 4054bool 4055sc_signed::sign() const 4056{ 4057 if (sgn == SC_NEG) 4058 return 1; 4059 else if (sgn != SC_NOSIGN) 4060 return 0; 4061 else 4062 return ((digit[ndigits - 1] & one_and_zeros(bit_ord(nbits - 1))) != 0); 4063} 4064 4065// The rest of the utils in this section are included from sc_nbcommon.cpp. 4066 4067 4068// ---------------------------------------------------------------------------- 4069// SECTION: Private members. 4070// ---------------------------------------------------------------------------- 4071 4072// The private members in this section are included from sc_nbcommon.cpp. 4073 4074#define CLASS_TYPE sc_signed 4075#define CLASS_TYPE_STR "sc_signed" 4076 4077#define ADD_HELPER add_signed_friend 4078#define SUB_HELPER sub_signed_friend 4079#define MUL_HELPER mul_signed_friend 4080#define DIV_HELPER div_signed_friend 4081#define MOD_HELPER mod_signed_friend 4082#define AND_HELPER and_signed_friend 4083#define OR_HELPER or_signed_friend 4084#define XOR_HELPER xor_signed_friend 4085 4086#include "sc_nbfriends.inc" 4087 4088#undef SC_UNSIGNED 4089#define SC_SIGNED 4090#define IF_SC_SIGNED 1 // 1 = sc_signed 4091#define CLASS_TYPE_SUBREF sc_signed_subref_r 4092#define OTHER_CLASS_TYPE sc_unsigned 4093#define OTHER_CLASS_TYPE_SUBREF sc_unsigned_subref_r 4094 4095#define MUL_ON_HELPER mul_on_help_signed 4096#define DIV_ON_HELPER div_on_help_signed 4097#define MOD_ON_HELPER mod_on_help_signed 4098 4099#include "sc_nbcommon.inc" 4100 4101#undef MOD_ON_HELPER 4102#undef DIV_ON_HELPER 4103#undef MUL_ON_HELPER 4104 4105#undef OTHER_CLASS_TYPE_SUBREF 4106#undef OTHER_CLASS_TYPE 4107#undef CLASS_TYPE_SUBREF 4108#undef IF_SC_SIGNED 4109#undef SC_SIGNED 4110 4111#undef XOR_HELPER 4112#undef OR_HELPER 4113#undef AND_HELPER 4114#undef MOD_HELPER 4115#undef DIV_HELPER 4116#undef MUL_HELPER 4117#undef SUB_HELPER 4118#undef ADD_HELPER 4119 4120#undef CLASS_TYPE 4121#undef CLASS_TYPE_STR 4122 4123#include "sc_signed_bitref.inc" 4124#include "sc_signed_subref.inc" 4125 4126#undef CONVERT_LONG 4127#undef CONVERT_LONG_2 4128#undef CONVERT_INT64 4129#undef CONVERT_INT64_2 4130 4131} // namespace sc_dt 4132 4133 4134// End of file. 4135