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