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