112854Sgabeblack@google.com/*****************************************************************************
212854Sgabeblack@google.com
312854Sgabeblack@google.com  Licensed to Accellera Systems Initiative Inc. (Accellera) under one or
412854Sgabeblack@google.com  more contributor license agreements.  See the NOTICE file distributed
512854Sgabeblack@google.com  with this work for additional information regarding copyright ownership.
612854Sgabeblack@google.com  Accellera licenses this file to you under the Apache License, Version 2.0
712854Sgabeblack@google.com  (the "License"); you may not use this file except in compliance with the
812854Sgabeblack@google.com  License.  You may obtain a copy of the License at
912854Sgabeblack@google.com
1012854Sgabeblack@google.com    http://www.apache.org/licenses/LICENSE-2.0
1112854Sgabeblack@google.com
1212854Sgabeblack@google.com  Unless required by applicable law or agreed to in writing, software
1312854Sgabeblack@google.com  distributed under the License is distributed on an "AS IS" BASIS,
1412854Sgabeblack@google.com  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
1512854Sgabeblack@google.com  implied.  See the License for the specific language governing
1612854Sgabeblack@google.com  permissions and limitations under the License.
1712854Sgabeblack@google.com
1812854Sgabeblack@google.com *****************************************************************************/
1912854Sgabeblack@google.com
2012854Sgabeblack@google.com/*****************************************************************************
2112854Sgabeblack@google.com
2212854Sgabeblack@google.com  sc_signed.cpp -- Arbitrary precision signed arithmetic.
2312854Sgabeblack@google.com
2412854Sgabeblack@google.com                   This file includes the definitions of sc_signed_bitref,
2512854Sgabeblack@google.com                   sc_signed_subref, and sc_signed classes. The first two
2612854Sgabeblack@google.com                   classes are proxy classes to reference one bit and a range
2712854Sgabeblack@google.com                   of bits of a sc_signed number, respectively. This file also
2812854Sgabeblack@google.com                   includes sc_nbcommon.cpp and sc_nbfriends.cpp, which
2912854Sgabeblack@google.com                   contain the definitions shared by sc_unsigned.
3012854Sgabeblack@google.com
3112854Sgabeblack@google.com  Original Author: Ali Dasdan, Synopsys, Inc.
3212854Sgabeblack@google.com
3312854Sgabeblack@google.com *****************************************************************************/
3412854Sgabeblack@google.com
3512854Sgabeblack@google.com/*****************************************************************************
3612854Sgabeblack@google.com
3712854Sgabeblack@google.com  MODIFICATION LOG - modifiers, enter your name, affiliation, date and
3812854Sgabeblack@google.com  changes you are making here.
3912854Sgabeblack@google.com
4012854Sgabeblack@google.com      Name, Affiliation, Date:
4112854Sgabeblack@google.com  Description of Modification:
4212854Sgabeblack@google.com
4312854Sgabeblack@google.com *****************************************************************************/
4412854Sgabeblack@google.com
4512854Sgabeblack@google.com
4612854Sgabeblack@google.com// $Log: sc_signed.cpp,v $
4712854Sgabeblack@google.com// Revision 1.6  2011/02/18 20:19:15  acg
4812854Sgabeblack@google.com//  Andy Goodrich: updating Copyright notice.
4912854Sgabeblack@google.com//
5012854Sgabeblack@google.com// Revision 1.5  2008/12/10 20:38:45  acg
5112854Sgabeblack@google.com//  Andy Goodrich: fixed conversion of double values to the digits vector.
5212854Sgabeblack@google.com//  The bits above the radix were not being masked off.
5312854Sgabeblack@google.com//
5412854Sgabeblack@google.com// Revision 1.4  2008/06/19 17:47:56  acg
5512854Sgabeblack@google.com//  Andy Goodrich: fixes for bugs. See 2.2.1 RELEASENOTES.
5612854Sgabeblack@google.com//
5712854Sgabeblack@google.com// Revision 1.3  2008/04/29 21:20:41  acg
5812854Sgabeblack@google.com//  Andy Goodrich: added mask to first word transferred when processing
5912854Sgabeblack@google.com//  a negative sc_signed value in sc_signed::concat_get_data().
6012854Sgabeblack@google.com//
6112854Sgabeblack@google.com// Revision 1.2  2007/11/04 21:27:00  acg
6212854Sgabeblack@google.com//  Andy Goodrich: changes to make sure the proper value is returned from
6312854Sgabeblack@google.com//  concat_get_data().
6412854Sgabeblack@google.com//
6512854Sgabeblack@google.com// Revision 1.1.1.1  2006/12/15 20:20:05  acg
6612854Sgabeblack@google.com// SystemC 2.3
6712854Sgabeblack@google.com//
6812854Sgabeblack@google.com// Revision 1.5  2006/10/23 19:32:47  acg
6912854Sgabeblack@google.com//  Andy Goodrich: further fix for incorrect value being returned from
7012854Sgabeblack@google.com//  concat_get_data. This one is in the non-aligned value code.
7112854Sgabeblack@google.com//
7212854Sgabeblack@google.com// Revision 1.3  2006/01/13 18:49:32  acg
7312854Sgabeblack@google.com// Added $Log command so that CVS check in comments are reproduced in the
7412854Sgabeblack@google.com// source.
7512854Sgabeblack@google.com//
7612854Sgabeblack@google.com
7712854Sgabeblack@google.com#include <cctype>
7812854Sgabeblack@google.com#include <cmath>
7912854Sgabeblack@google.com#include <sstream>
8012854Sgabeblack@google.com
8112854Sgabeblack@google.com#include "systemc/ext/dt/bit/sc_bv_base.hh"
8212854Sgabeblack@google.com#include "systemc/ext/dt/bit/sc_lv_base.hh"
8312854Sgabeblack@google.com#include "systemc/ext/dt/fx/sc_fix.hh"
8412854Sgabeblack@google.com#include "systemc/ext/dt/fx/scfx_other_defs.hh"
8513325Sgabeblack@google.com#include "systemc/ext/dt/int/messages.hh"
8612854Sgabeblack@google.com#include "systemc/ext/dt/int/sc_int_base.hh"
8712854Sgabeblack@google.com#include "systemc/ext/dt/int/sc_signed.hh"
8812854Sgabeblack@google.com#include "systemc/ext/dt/int/sc_uint_base.hh"
8912854Sgabeblack@google.com#include "systemc/ext/dt/int/sc_unsigned.hh"
9012854Sgabeblack@google.com#include "systemc/ext/dt/misc/sc_concatref.hh"
9112854Sgabeblack@google.com
9212854Sgabeblack@google.com// explicit template instantiations
9312854Sgabeblack@google.comnamespace sc_core
9412854Sgabeblack@google.com{
9512854Sgabeblack@google.com
9612854Sgabeblack@google.comtemplate class sc_vpool<sc_dt::sc_signed_bitref>;
9712854Sgabeblack@google.comtemplate class sc_vpool<sc_dt::sc_signed_subref>;
9812854Sgabeblack@google.com
9912854Sgabeblack@google.com} // namespace sc_core
10012854Sgabeblack@google.com
10112854Sgabeblack@google.comnamespace sc_dt
10212854Sgabeblack@google.com{
10312854Sgabeblack@google.com
10412854Sgabeblack@google.com// Pool of temporary instances:
10512854Sgabeblack@google.com
10612854Sgabeblack@google.comsc_core::sc_vpool<sc_signed_bitref> sc_signed_bitref::m_pool(9);
10712854Sgabeblack@google.comsc_core::sc_vpool<sc_signed_subref> sc_signed_subref::m_pool(9);
10812854Sgabeblack@google.com
10912854Sgabeblack@google.comvoid
11012854Sgabeblack@google.comsc_signed::invalid_init(const char *type_name, int nb) const
11112854Sgabeblack@google.com{
11212854Sgabeblack@google.com    std::stringstream msg;
11312854Sgabeblack@google.com    msg << "sc_signed("<< type_name << ") : nb = " << nb << " is not valid";
11413325Sgabeblack@google.com    SC_REPORT_ERROR(sc_core::SC_ID_INIT_FAILED_, msg.str().c_str());
11512854Sgabeblack@google.com}
11612854Sgabeblack@google.com
11712854Sgabeblack@google.com// ----------------------------------------------------------------------------
11812854Sgabeblack@google.com// SECTION: Public members - Invalid selections.
11912854Sgabeblack@google.com// ----------------------------------------------------------------------------
12012854Sgabeblack@google.com
12112854Sgabeblack@google.comvoid
12212854Sgabeblack@google.comsc_signed::invalid_index(int i) const
12312854Sgabeblack@google.com{
12412854Sgabeblack@google.com    std::stringstream msg;
12512854Sgabeblack@google.com    msg << "sc_bigint bit selection: index = " << i << " violates "
12612854Sgabeblack@google.com           "0 <= index <= " << (nbits - 1);
12713322Sgabeblack@google.com    SC_REPORT_ERROR(sc_core::SC_ID_OUT_OF_BOUNDS_, msg.str().c_str());
12812854Sgabeblack@google.com    sc_core::sc_abort(); // can't recover from here
12912854Sgabeblack@google.com}
13012854Sgabeblack@google.com
13112854Sgabeblack@google.comvoid
13212854Sgabeblack@google.comsc_signed::invalid_range(int l, int r) const
13312854Sgabeblack@google.com{
13412854Sgabeblack@google.com    std::stringstream msg;
13512854Sgabeblack@google.com    msg << "sc_bigint part selection: left = " <<
13613160Sgabeblack@google.com           l << ", right = " << r << " \n"
13712854Sgabeblack@google.com           "  violates either (" << (nbits-1) << " >= left >= 0) or "
13812854Sgabeblack@google.com           "(" << (nbits-1) << " >= right >= 0)";
13913322Sgabeblack@google.com    SC_REPORT_ERROR(sc_core::SC_ID_OUT_OF_BOUNDS_, msg.str().c_str());
14012854Sgabeblack@google.com    sc_core::sc_abort(); // can't recover from here
14112854Sgabeblack@google.com}
14212854Sgabeblack@google.com
14312854Sgabeblack@google.com
14412854Sgabeblack@google.com// ----------------------------------------------------------------------------
14512854Sgabeblack@google.com
14612854Sgabeblack@google.com// ----------------------------------------------------------------------------
14712854Sgabeblack@google.com//  SECTION: Public members.
14812854Sgabeblack@google.com// ----------------------------------------------------------------------------
14912854Sgabeblack@google.com
15012854Sgabeblack@google.com// Most public members are included from sc_nbcommon.inc. However, some
15112854Sgabeblack@google.com// concatenation support appears here to optimize between the signed and
15212854Sgabeblack@google.com// unsigned cases.
15312854Sgabeblack@google.com
15412854Sgabeblack@google.com// Insert this object's value at the specified place in a vector of biguint
15512854Sgabeblack@google.com// style values.
15612854Sgabeblack@google.com
15712854Sgabeblack@google.combool
15812854Sgabeblack@google.comsc_signed::concat_get_ctrl(sc_digit *dst_p, int low_i) const
15912854Sgabeblack@google.com{
16012854Sgabeblack@google.com    int dst_i; // Index to next word to set in dst_p.
16112854Sgabeblack@google.com    int end_i; // Index of high order word to set.
16212854Sgabeblack@google.com    int left_shift; // Amount to shift value left.
16312854Sgabeblack@google.com    sc_digit mask; // Mask for partial word sets.
16412854Sgabeblack@google.com
16512854Sgabeblack@google.com    // CALCULATE METRICS FOR DATA MOVEMENT:
16612854Sgabeblack@google.com    dst_i = low_i / BITS_PER_DIGIT;
16712854Sgabeblack@google.com    end_i = (low_i + nbits - 1) / BITS_PER_DIGIT;
16812854Sgabeblack@google.com    left_shift = low_i % BITS_PER_DIGIT;
16912854Sgabeblack@google.com
17012854Sgabeblack@google.com    // ALL DATA TO BE MOVED IS IN A SINGLE WORD:
17112854Sgabeblack@google.com    mask = ~(~0U << left_shift);
17212854Sgabeblack@google.com    dst_p[dst_i] = (dst_p[dst_i] & ~mask);
17312854Sgabeblack@google.com    dst_i++;
17412854Sgabeblack@google.com
17512854Sgabeblack@google.com    for (; dst_i <= end_i; dst_i++)
17612854Sgabeblack@google.com        dst_p[dst_i] = 0;
17712854Sgabeblack@google.com
17812854Sgabeblack@google.com    return false;
17912854Sgabeblack@google.com}
18012854Sgabeblack@google.com
18112854Sgabeblack@google.combool
18212854Sgabeblack@google.comsc_signed::concat_get_data(sc_digit *dst_p, int low_i) const
18312854Sgabeblack@google.com{
18412854Sgabeblack@google.com    sc_digit carry; // Carry bit for complements.
18512854Sgabeblack@google.com    int dst_i; // Index to next word to set in dst_p.
18612854Sgabeblack@google.com    int end_i; // Index of high order word to set.
18712854Sgabeblack@google.com    int high_i; // Index w/in word of high order bit.
18812854Sgabeblack@google.com    int left_shift; // Amount to shift value left.
18912854Sgabeblack@google.com    sc_digit left_word; // High word component for set.
19012854Sgabeblack@google.com    sc_digit mask; // Mask for partial word sets.
19112854Sgabeblack@google.com    bool result; // True if inserted non-zero data.
19212854Sgabeblack@google.com    int right_shift; // Amount to shift value right.
19312854Sgabeblack@google.com    sc_digit right_word; // Low word component for set.
19412854Sgabeblack@google.com    int src_i; // Index to next word to get from digit.
19512854Sgabeblack@google.com
19612854Sgabeblack@google.com    // CALCULATE METRICS FOR DATA MOVEMENT:
19712854Sgabeblack@google.com    dst_i = low_i / BITS_PER_DIGIT;
19812854Sgabeblack@google.com    high_i = low_i + nbits - 1;
19912854Sgabeblack@google.com    end_i = high_i / BITS_PER_DIGIT;
20012854Sgabeblack@google.com    left_shift = low_i % BITS_PER_DIGIT;
20112854Sgabeblack@google.com
20212854Sgabeblack@google.com    switch (sgn) {
20312854Sgabeblack@google.com      // POSITIVE SOURCE VALUE:
20412854Sgabeblack@google.com      case SC_POS:
20512854Sgabeblack@google.com        result = true;
20612854Sgabeblack@google.com
20712854Sgabeblack@google.com        // ALL DATA TO BE MOVED IS IN A SINGLE WORD:
20812854Sgabeblack@google.com        if (dst_i == end_i) {
20912854Sgabeblack@google.com            mask = ~(~0U << left_shift);
21012854Sgabeblack@google.com            dst_p[dst_i] = ((dst_p[dst_i] & mask) |
21112854Sgabeblack@google.com                (digit[0] << left_shift)) & DIGIT_MASK;
21212854Sgabeblack@google.com
21312854Sgabeblack@google.com        // DATA IS IN MORE THAN ONE WORD, BUT IS WORD ALIGNED:
21412854Sgabeblack@google.com        } else if (left_shift == 0) {
21512854Sgabeblack@google.com            for (src_i = 0; dst_i < end_i; dst_i++, src_i++) {
21612854Sgabeblack@google.com                dst_p[dst_i] = digit[src_i];
21712854Sgabeblack@google.com            }
21812854Sgabeblack@google.com            high_i = high_i % BITS_PER_DIGIT;
21912854Sgabeblack@google.com            mask = ~(~1U << high_i) & DIGIT_MASK;
22012854Sgabeblack@google.com            dst_p[dst_i] = digit[src_i] & mask;
22112854Sgabeblack@google.com
22212854Sgabeblack@google.com        // DATA IS IN MORE THAN ONE WORD, AND NOT WORD ALIGNED:
22312854Sgabeblack@google.com        } else {
22412854Sgabeblack@google.com            high_i = high_i % BITS_PER_DIGIT;
22512854Sgabeblack@google.com            right_shift = BITS_PER_DIGIT - left_shift;
22612854Sgabeblack@google.com            mask = ~(~0U << left_shift);
22712854Sgabeblack@google.com            right_word = digit[0];
22812854Sgabeblack@google.com            dst_p[dst_i] = (dst_p[dst_i] & mask) |
22912854Sgabeblack@google.com                ((right_word << left_shift) & DIGIT_MASK);
23012854Sgabeblack@google.com            for (src_i = 1, dst_i++; dst_i < end_i; dst_i++, src_i++) {
23112854Sgabeblack@google.com                left_word = digit[src_i];
23212854Sgabeblack@google.com                dst_p[dst_i] = ((left_word << left_shift)&DIGIT_MASK) |
23312854Sgabeblack@google.com                    (right_word >> right_shift);
23412854Sgabeblack@google.com                right_word = left_word;
23512854Sgabeblack@google.com            }
23612854Sgabeblack@google.com            left_word = (src_i < ndigits) ? digit[src_i] : 0;
23712854Sgabeblack@google.com            mask = ~(~1U << high_i) & DIGIT_MASK;
23812854Sgabeblack@google.com            dst_p[dst_i] = ((left_word << left_shift) |
23912854Sgabeblack@google.com                (right_word >> right_shift)) & mask;
24012854Sgabeblack@google.com        }
24112854Sgabeblack@google.com        break;
24212854Sgabeblack@google.com
24312854Sgabeblack@google.com      // SOURCE VALUE IS NEGATIVE:
24412854Sgabeblack@google.com      case SC_NEG:
24512854Sgabeblack@google.com        // ALL DATA TO BE MOVED IS IN A SINGLE WORD:
24612854Sgabeblack@google.com        result = true;
24712854Sgabeblack@google.com        if (dst_i == end_i) {
24812854Sgabeblack@google.com            mask = ~(~0U << nbits);
24912854Sgabeblack@google.com            right_word = ((digit[0] ^ DIGIT_MASK) + 1) & mask;
25012854Sgabeblack@google.com            mask = ~(~0U << left_shift);
25112854Sgabeblack@google.com            dst_p[dst_i] = ((dst_p[dst_i] & mask) |
25212854Sgabeblack@google.com                (right_word << left_shift)) & DIGIT_MASK;
25312854Sgabeblack@google.com
25412854Sgabeblack@google.com        // DATA IS IN MORE THAN ONE WORD, BUT IS WORD ALIGNED:
25512854Sgabeblack@google.com        } else if (left_shift == 0) {
25612854Sgabeblack@google.com            carry = 1;
25712854Sgabeblack@google.com            for (src_i = 0; dst_i < end_i; dst_i++, src_i++) {
25812854Sgabeblack@google.com                right_word = (digit[src_i] ^ DIGIT_MASK) + carry;
25912854Sgabeblack@google.com                dst_p[dst_i] = right_word &  DIGIT_MASK;
26012854Sgabeblack@google.com                carry = right_word >> BITS_PER_DIGIT;
26112854Sgabeblack@google.com            }
26212854Sgabeblack@google.com            high_i = high_i % BITS_PER_DIGIT;
26312854Sgabeblack@google.com            mask = (~(~1U << high_i)) & DIGIT_MASK;
26412854Sgabeblack@google.com            right_word = (src_i < ndigits) ?
26512854Sgabeblack@google.com                (digit[src_i] ^ DIGIT_MASK) + carry : DIGIT_MASK + carry;
26612854Sgabeblack@google.com            dst_p[dst_i] = right_word & mask;
26712854Sgabeblack@google.com
26812854Sgabeblack@google.com        // DATA IS IN MORE THAN ONE WORD, AND NOT WORD ALIGNED:
26912854Sgabeblack@google.com        } else {
27012854Sgabeblack@google.com            high_i = high_i % BITS_PER_DIGIT;
27112854Sgabeblack@google.com            right_shift = BITS_PER_DIGIT - left_shift;
27212854Sgabeblack@google.com            mask = ~(~0U << left_shift);
27312854Sgabeblack@google.com            carry = 1;
27412854Sgabeblack@google.com            right_word = (digit[0] ^ DIGIT_MASK) + carry;
27512854Sgabeblack@google.com            dst_p[dst_i] = (dst_p[dst_i] & mask) |
27612854Sgabeblack@google.com                ((right_word << left_shift) & DIGIT_MASK);
27712854Sgabeblack@google.com            carry = right_word >> BITS_PER_DIGIT;
27812854Sgabeblack@google.com            right_word &= DIGIT_MASK;
27912854Sgabeblack@google.com            for (src_i = 1, dst_i++; dst_i < end_i; dst_i++, src_i++) {
28012854Sgabeblack@google.com                left_word = (digit[src_i] ^ DIGIT_MASK) + carry;
28112854Sgabeblack@google.com                dst_p[dst_i] = ((left_word << left_shift)&DIGIT_MASK) |
28212854Sgabeblack@google.com                    (right_word >> right_shift);
28312854Sgabeblack@google.com                carry = left_word >> BITS_PER_DIGIT;
28412854Sgabeblack@google.com                right_word = left_word & DIGIT_MASK;
28512854Sgabeblack@google.com            }
28612854Sgabeblack@google.com            left_word = (src_i < ndigits) ?
28712854Sgabeblack@google.com                (digit[src_i] ^ DIGIT_MASK) + carry : carry;
28812854Sgabeblack@google.com            mask = ~(~1U << high_i) & DIGIT_MASK;
28912854Sgabeblack@google.com            dst_p[dst_i] = ((left_word << left_shift) |
29012854Sgabeblack@google.com                (right_word >> right_shift)) & mask;
29112854Sgabeblack@google.com        }
29212854Sgabeblack@google.com        break;
29312854Sgabeblack@google.com
29412854Sgabeblack@google.com      // VALUE IS ZERO:
29512854Sgabeblack@google.com      default:
29612854Sgabeblack@google.com        result = false;
29712854Sgabeblack@google.com        // ALL DATA TO BE MOVED IS IN A SINGLE WORD:
29812854Sgabeblack@google.com        if (dst_i == end_i) {
29912854Sgabeblack@google.com            mask = ~(~0U << nbits) << left_shift;
30012854Sgabeblack@google.com            dst_p[dst_i] = dst_p[dst_i] & ~mask;
30112854Sgabeblack@google.com
30212854Sgabeblack@google.com        // DATA IS IN MORE THAN ONE WORD, BUT IS WORD ALIGNED:
30312854Sgabeblack@google.com        } else if (left_shift == 0) {
30412854Sgabeblack@google.com            for (src_i = 0; dst_i < end_i; dst_i++, src_i++) {
30512854Sgabeblack@google.com                dst_p[dst_i] = 0;
30612854Sgabeblack@google.com            }
30712854Sgabeblack@google.com            dst_p[dst_i] = 0;
30812854Sgabeblack@google.com
30912854Sgabeblack@google.com        // DATA IS IN MORE THAN ONE WORD, AND NOT WORD ALIGNED:
31012854Sgabeblack@google.com        } else {
31112854Sgabeblack@google.com            mask = ~(~0U << left_shift);
31212854Sgabeblack@google.com            dst_p[dst_i] = (dst_p[dst_i] & mask);
31312854Sgabeblack@google.com            for (dst_i++; dst_i <= end_i; dst_i++) {
31412854Sgabeblack@google.com                dst_p[dst_i] = 0;
31512854Sgabeblack@google.com            }
31612854Sgabeblack@google.com        }
31712854Sgabeblack@google.com        break;
31812854Sgabeblack@google.com    }
31912854Sgabeblack@google.com    return result;
32012854Sgabeblack@google.com}
32112854Sgabeblack@google.com
32212854Sgabeblack@google.com// Return this object instance's bits as a uint64 without sign extension.
32312854Sgabeblack@google.com
32412854Sgabeblack@google.comuint64
32512854Sgabeblack@google.comsc_signed::concat_get_uint64() const
32612854Sgabeblack@google.com{
32712854Sgabeblack@google.com    uint64        result;
32812854Sgabeblack@google.com    switch (sgn) {
32912854Sgabeblack@google.com      case SC_POS:
33012854Sgabeblack@google.com        result = 0;
33112854Sgabeblack@google.com        if (ndigits > 2)
33212854Sgabeblack@google.com            result = digit[2];
33312854Sgabeblack@google.com        if (ndigits > 1)
33412854Sgabeblack@google.com            result = (result << BITS_PER_DIGIT) | digit[1];
33512854Sgabeblack@google.com        result = (result << BITS_PER_DIGIT) | digit[0];
33612854Sgabeblack@google.com        break;
33712854Sgabeblack@google.com      case SC_NEG:
33812854Sgabeblack@google.com        result = 0;
33912854Sgabeblack@google.com        if (ndigits > 2)
34012854Sgabeblack@google.com            result = digit[2];
34112854Sgabeblack@google.com        if (ndigits > 1)
34212854Sgabeblack@google.com            result = (result << BITS_PER_DIGIT) | digit[1];
34312854Sgabeblack@google.com        result = (result << BITS_PER_DIGIT) | digit[0];
34412854Sgabeblack@google.com        result = -result;
34512854Sgabeblack@google.com        if (nbits < 64) {
34612854Sgabeblack@google.com            uint64 mask = ~0;
34712854Sgabeblack@google.com            result = result & ~(mask << nbits);
34812854Sgabeblack@google.com        }
34912854Sgabeblack@google.com        break;
35012854Sgabeblack@google.com      default:
35112854Sgabeblack@google.com        result = 0;
35212854Sgabeblack@google.com        break;
35312854Sgabeblack@google.com    }
35412854Sgabeblack@google.com    return result;
35512854Sgabeblack@google.com}
35612854Sgabeblack@google.com
35712854Sgabeblack@google.com// #### OPTIMIZE
35812854Sgabeblack@google.comvoid
35912854Sgabeblack@google.comsc_signed::concat_set(int64 src, int low_i)
36012854Sgabeblack@google.com{
36112854Sgabeblack@google.com    *this = (low_i < 64) ? src >> low_i : src >> 63;
36212854Sgabeblack@google.com}
36312854Sgabeblack@google.com
36412854Sgabeblack@google.comvoid
36512854Sgabeblack@google.comsc_signed::concat_set(const sc_signed &src, int low_i)
36612854Sgabeblack@google.com{
36712854Sgabeblack@google.com    if (low_i < src.length())
36812854Sgabeblack@google.com        *this = src >> low_i;
36912854Sgabeblack@google.com    else
37012854Sgabeblack@google.com        *this = (src<0) ? (int_type)-1 : 0;
37112854Sgabeblack@google.com}
37212854Sgabeblack@google.com
37312854Sgabeblack@google.comvoid
37412854Sgabeblack@google.comsc_signed::concat_set(const sc_unsigned &src, int low_i)
37512854Sgabeblack@google.com{
37612854Sgabeblack@google.com    if (low_i < src.length())
37712854Sgabeblack@google.com        *this = src >> low_i;
37812854Sgabeblack@google.com    else
37912854Sgabeblack@google.com        *this = 0;
38012854Sgabeblack@google.com}
38112854Sgabeblack@google.com
38212854Sgabeblack@google.comvoid
38312854Sgabeblack@google.comsc_signed::concat_set(uint64 src, int low_i)
38412854Sgabeblack@google.com{
38512854Sgabeblack@google.com    *this = (low_i < 64) ? src >> low_i : 0;
38612854Sgabeblack@google.com}
38712854Sgabeblack@google.com
38812854Sgabeblack@google.com// ----------------------------------------------------------------------------
38912854Sgabeblack@google.com//  SECTION: Public members - Reduction methods.
39012854Sgabeblack@google.com// ----------------------------------------------------------------------------
39112854Sgabeblack@google.com
39212854Sgabeblack@google.combool
39312854Sgabeblack@google.comsc_signed::and_reduce() const
39412854Sgabeblack@google.com{
39512854Sgabeblack@google.com    sc_digit current; // Current digit examining.
39612854Sgabeblack@google.com    int i; // Index of digit examining.
39712854Sgabeblack@google.com
39812854Sgabeblack@google.com    if (sgn == SC_NEG) {
39912854Sgabeblack@google.com        current = (1 << BITS_PER_DIGIT);
40012854Sgabeblack@google.com        for (i = 0; i < ndigits-1; i++) {
40112854Sgabeblack@google.com            current = (current >> BITS_PER_DIGIT) + (digit[i]^DIGIT_MASK);
40212854Sgabeblack@google.com            if ((current & DIGIT_MASK) != DIGIT_MASK) return false;
40312854Sgabeblack@google.com        }
40412854Sgabeblack@google.com        current = (current >> BITS_PER_DIGIT) + (digit[i]^DIGIT_MASK);
40512854Sgabeblack@google.com        if ((current & ~(~0U << (nbits % BITS_PER_DIGIT))) ==
40612854Sgabeblack@google.com             static_cast<sc_digit>(~(~0U << (nbits % BITS_PER_DIGIT)))) {
40712854Sgabeblack@google.com            return true;
40812854Sgabeblack@google.com        }
40912854Sgabeblack@google.com    }
41012854Sgabeblack@google.com    return false;
41112854Sgabeblack@google.com}
41212854Sgabeblack@google.com
41312854Sgabeblack@google.combool
41412854Sgabeblack@google.comsc_signed::or_reduce() const
41512854Sgabeblack@google.com{
41612854Sgabeblack@google.com    return sgn == SC_ZERO ? false : true;
41712854Sgabeblack@google.com}
41812854Sgabeblack@google.com
41912854Sgabeblack@google.combool
42012854Sgabeblack@google.comsc_signed::xor_reduce() const
42112854Sgabeblack@google.com{
42212854Sgabeblack@google.com    int i; // Digit examining.
42312854Sgabeblack@google.com    int odd; // Flag for odd number of digits.
42412854Sgabeblack@google.com
42512854Sgabeblack@google.com    odd = 0;
42612854Sgabeblack@google.com    for (i = 0; i < nbits; i++)
42712854Sgabeblack@google.com        if (test(i))
42812854Sgabeblack@google.com            odd = ~odd;
42912854Sgabeblack@google.com    return odd ? true : false;
43012854Sgabeblack@google.com}
43112854Sgabeblack@google.com
43212854Sgabeblack@google.com
43312854Sgabeblack@google.com
43412854Sgabeblack@google.com// ----------------------------------------------------------------------------
43512854Sgabeblack@google.com//  SECTION: Public members - Assignment operators.
43612854Sgabeblack@google.com// ----------------------------------------------------------------------------
43712854Sgabeblack@google.com
43812854Sgabeblack@google.com// assignment operators
43912854Sgabeblack@google.com
44012854Sgabeblack@google.comconst sc_signed &
44112854Sgabeblack@google.comsc_signed::operator = (const char *a)
44212854Sgabeblack@google.com{
44312854Sgabeblack@google.com    if (a == 0) {
44413325Sgabeblack@google.com        SC_REPORT_ERROR(sc_core::SC_ID_CONVERSION_FAILED_,
44512854Sgabeblack@google.com                        "character string is zero");
44612854Sgabeblack@google.com    } else if (*a == 0) {
44713325Sgabeblack@google.com        SC_REPORT_ERROR(sc_core::SC_ID_CONVERSION_FAILED_,
44812854Sgabeblack@google.com                        "character string is empty");
44912854Sgabeblack@google.com    } else try {
45012854Sgabeblack@google.com        int len = length();
45112854Sgabeblack@google.com        sc_fix aa(a, len, len, SC_TRN, SC_WRAP, 0, SC_ON);
45212854Sgabeblack@google.com        return this->operator = (aa);
45312854Sgabeblack@google.com    } catch(const sc_core::sc_report &) {
45412854Sgabeblack@google.com        std::stringstream msg;
45512854Sgabeblack@google.com        msg << "character string '" << a << "' is not valid";
45613325Sgabeblack@google.com        SC_REPORT_ERROR(sc_core::SC_ID_CONVERSION_FAILED_, msg.str().c_str());
45712854Sgabeblack@google.com    }
45812854Sgabeblack@google.com    return *this;
45912854Sgabeblack@google.com}
46012854Sgabeblack@google.com
46112854Sgabeblack@google.comconst sc_signed &
46212854Sgabeblack@google.comsc_signed::operator=(int64 v)
46312854Sgabeblack@google.com{
46412854Sgabeblack@google.com    sgn = get_sign(v);
46512854Sgabeblack@google.com    // v >= 0 now.
46612854Sgabeblack@google.com    if (sgn == SC_ZERO) {
46712854Sgabeblack@google.com        vec_zero(ndigits, digit);
46812854Sgabeblack@google.com    } else {
46912854Sgabeblack@google.com        from_uint(ndigits, digit, (uint64)v);
47012854Sgabeblack@google.com        if (nbits <= (int)BITS_PER_INT64)
47112854Sgabeblack@google.com            convert_SM_to_2C_to_SM();
47212854Sgabeblack@google.com    }
47312854Sgabeblack@google.com    return *this;
47412854Sgabeblack@google.com}
47512854Sgabeblack@google.com
47612854Sgabeblack@google.comconst sc_signed &
47712854Sgabeblack@google.comsc_signed::operator=(uint64 v)
47812854Sgabeblack@google.com{
47912854Sgabeblack@google.com    sgn = get_sign(v);
48012854Sgabeblack@google.com    if (sgn == SC_ZERO) {
48112854Sgabeblack@google.com        vec_zero(ndigits, digit);
48212854Sgabeblack@google.com    } else {
48312854Sgabeblack@google.com        from_uint(ndigits, digit, v);
48412854Sgabeblack@google.com        if (nbits <= (int)BITS_PER_INT64)
48512854Sgabeblack@google.com            convert_SM_to_2C_to_SM();
48612854Sgabeblack@google.com    }
48712854Sgabeblack@google.com    return *this;
48812854Sgabeblack@google.com}
48912854Sgabeblack@google.com
49012854Sgabeblack@google.comconst sc_signed &
49112854Sgabeblack@google.comsc_signed::operator=(long v)
49212854Sgabeblack@google.com{
49312854Sgabeblack@google.com    sgn = get_sign(v);
49412854Sgabeblack@google.com    // v >= 0 now.
49512854Sgabeblack@google.com    if (sgn == SC_ZERO) {
49612854Sgabeblack@google.com        vec_zero(ndigits, digit);
49712854Sgabeblack@google.com    } else {
49812854Sgabeblack@google.com        from_uint(ndigits, digit, (unsigned long)v);
49912854Sgabeblack@google.com        if (nbits <= (int)BITS_PER_LONG)
50012854Sgabeblack@google.com            convert_SM_to_2C_to_SM();
50112854Sgabeblack@google.com    }
50212854Sgabeblack@google.com    return *this;
50312854Sgabeblack@google.com}
50412854Sgabeblack@google.com
50512854Sgabeblack@google.comconst sc_signed &
50612854Sgabeblack@google.comsc_signed::operator=(unsigned long v)
50712854Sgabeblack@google.com{
50812854Sgabeblack@google.com    sgn = get_sign(v);
50912854Sgabeblack@google.com    if (sgn == SC_ZERO) {
51012854Sgabeblack@google.com        vec_zero(ndigits, digit);
51112854Sgabeblack@google.com    } else {
51212854Sgabeblack@google.com        from_uint(ndigits, digit, v);
51312854Sgabeblack@google.com        if (nbits <= (int)BITS_PER_LONG)
51412854Sgabeblack@google.com            convert_SM_to_2C_to_SM();
51512854Sgabeblack@google.com    }
51612854Sgabeblack@google.com    return *this;
51712854Sgabeblack@google.com}
51812854Sgabeblack@google.com
51912854Sgabeblack@google.comconst sc_signed &
52012854Sgabeblack@google.comsc_signed::operator=(double v)
52112854Sgabeblack@google.com{
52212854Sgabeblack@google.com    is_bad_double(v);
52312854Sgabeblack@google.com    if (v < 0) {
52412854Sgabeblack@google.com        v = -v;
52512854Sgabeblack@google.com        sgn = SC_NEG;
52612854Sgabeblack@google.com    } else {
52712854Sgabeblack@google.com      sgn = SC_POS;
52812854Sgabeblack@google.com    }
52912854Sgabeblack@google.com
53012854Sgabeblack@google.com    int i = 0;
53112854Sgabeblack@google.com    while (std::floor(v) && (i < ndigits)) {
53212854Sgabeblack@google.com        digit[i++] = ((sc_digit)std::floor(remainder(v, DIGIT_RADIX))) &
53312854Sgabeblack@google.com                                           DIGIT_MASK;
53412854Sgabeblack@google.com        v /= DIGIT_RADIX;
53512854Sgabeblack@google.com    }
53612854Sgabeblack@google.com    vec_zero(i, ndigits, digit);
53712854Sgabeblack@google.com    convert_SM_to_2C_to_SM();
53812854Sgabeblack@google.com    return *this;
53912854Sgabeblack@google.com}
54012854Sgabeblack@google.com
54112854Sgabeblack@google.com
54212854Sgabeblack@google.com// ----------------------------------------------------------------------------
54312854Sgabeblack@google.com
54412854Sgabeblack@google.comconst sc_signed &
54512854Sgabeblack@google.comsc_signed::operator = (const sc_bv_base &v)
54612854Sgabeblack@google.com{
54712854Sgabeblack@google.com    int minlen = sc_min(nbits, v.length());
54812854Sgabeblack@google.com    int i = 0;
54912854Sgabeblack@google.com    for (; i < minlen; ++i) {
55012854Sgabeblack@google.com        safe_set(i, v.get_bit(i), digit);
55112854Sgabeblack@google.com    }
55212854Sgabeblack@google.com    for (; i < nbits; ++i) {
55312854Sgabeblack@google.com        safe_set(i, 0, digit); // zero-extend
55412854Sgabeblack@google.com    }
55512854Sgabeblack@google.com    convert_2C_to_SM();
55612854Sgabeblack@google.com    return *this;
55712854Sgabeblack@google.com}
55812854Sgabeblack@google.com
55912854Sgabeblack@google.comconst sc_signed &
56012854Sgabeblack@google.comsc_signed::operator = (const sc_lv_base &v)
56112854Sgabeblack@google.com{
56212854Sgabeblack@google.com    int minlen = sc_min(nbits, v.length());
56312854Sgabeblack@google.com    int i = 0;
56412854Sgabeblack@google.com    for (; i < minlen; ++i) {
56512854Sgabeblack@google.com        safe_set(i, sc_logic(v.get_bit(i)).to_bool(), digit);
56612854Sgabeblack@google.com    }
56712854Sgabeblack@google.com    for (; i < nbits; ++i) {
56812854Sgabeblack@google.com        safe_set(i, 0, digit);  // zero-extend
56912854Sgabeblack@google.com    }
57012854Sgabeblack@google.com    convert_2C_to_SM();
57112854Sgabeblack@google.com    return *this;
57212854Sgabeblack@google.com}
57312854Sgabeblack@google.com
57412854Sgabeblack@google.com
57512854Sgabeblack@google.com// explicit conversion to character string
57612854Sgabeblack@google.comconst std::string
57712854Sgabeblack@google.comsc_signed::to_string(sc_numrep numrep) const
57812854Sgabeblack@google.com{
57912854Sgabeblack@google.com    int len = length();
58012854Sgabeblack@google.com    sc_fix aa(*this, len, len, SC_TRN, SC_WRAP, 0, SC_ON);
58112854Sgabeblack@google.com    return aa.to_string(numrep);
58212854Sgabeblack@google.com}
58312854Sgabeblack@google.com
58412854Sgabeblack@google.comconst std::string
58512854Sgabeblack@google.comsc_signed::to_string(sc_numrep numrep, bool w_prefix) const
58612854Sgabeblack@google.com{
58712854Sgabeblack@google.com    int len = length();
58812854Sgabeblack@google.com    sc_fix aa(*this, len, len, SC_TRN, SC_WRAP, 0, SC_ON);
58912854Sgabeblack@google.com    return aa.to_string(numrep, w_prefix);
59012854Sgabeblack@google.com}
59112854Sgabeblack@google.com
59212854Sgabeblack@google.com
59312854Sgabeblack@google.com// ----------------------------------------------------------------------------
59412854Sgabeblack@google.com//  SECTION: Interfacing with sc_int_base
59512854Sgabeblack@google.com// ----------------------------------------------------------------------------
59612854Sgabeblack@google.com
59712854Sgabeblack@google.comconst sc_signed &
59812854Sgabeblack@google.comsc_signed::operator = (const sc_int_base &v)
59912854Sgabeblack@google.com{
60012854Sgabeblack@google.com    return operator = ((int64)v);
60112854Sgabeblack@google.com}
60212854Sgabeblack@google.com
60312854Sgabeblack@google.com
60412854Sgabeblack@google.comsc_signed
60512854Sgabeblack@google.comoperator + (const sc_unsigned &u, const sc_int_base &v)
60612854Sgabeblack@google.com{
60712854Sgabeblack@google.com    return operator + (u, static_cast<int64>(v));
60812854Sgabeblack@google.com}
60912854Sgabeblack@google.com
61012854Sgabeblack@google.comsc_signed
61112854Sgabeblack@google.comoperator + (const sc_int_base &u, const sc_unsigned &v)
61212854Sgabeblack@google.com{
61312854Sgabeblack@google.com    return operator + (static_cast<int64>(u), v);
61412854Sgabeblack@google.com}
61512854Sgabeblack@google.com
61612854Sgabeblack@google.comsc_signed
61712854Sgabeblack@google.comoperator + (const sc_signed &u, const sc_int_base &v)
61812854Sgabeblack@google.com{
61912854Sgabeblack@google.com    return operator + (u, (int64)v);
62012854Sgabeblack@google.com}
62112854Sgabeblack@google.com
62212854Sgabeblack@google.comsc_signed
62312854Sgabeblack@google.comoperator + (const sc_int_base &u, const sc_signed &v)
62412854Sgabeblack@google.com{
62512854Sgabeblack@google.com    return operator + ((int64)u, v);
62612854Sgabeblack@google.com}
62712854Sgabeblack@google.com
62812854Sgabeblack@google.comconst sc_signed &
62912854Sgabeblack@google.comsc_signed::operator += (const sc_int_base &v)
63012854Sgabeblack@google.com{
63112854Sgabeblack@google.com    return operator += ((int64)v);
63212854Sgabeblack@google.com}
63312854Sgabeblack@google.com
63412854Sgabeblack@google.com
63512854Sgabeblack@google.comsc_signed
63612854Sgabeblack@google.comoperator - (const sc_unsigned &u, const sc_int_base &v)
63712854Sgabeblack@google.com{
63812854Sgabeblack@google.com    return operator - (u, (int64)v);
63912854Sgabeblack@google.com}
64012854Sgabeblack@google.com
64112854Sgabeblack@google.comsc_signed
64212854Sgabeblack@google.comoperator - (const sc_int_base &u, const sc_unsigned &v)
64312854Sgabeblack@google.com{
64412854Sgabeblack@google.com    return operator - ((int64)u, v);
64512854Sgabeblack@google.com}
64612854Sgabeblack@google.com
64712854Sgabeblack@google.comsc_signed
64812854Sgabeblack@google.comoperator - (const sc_signed &u, const sc_int_base &v)
64912854Sgabeblack@google.com{
65012854Sgabeblack@google.com    return operator - (u, (int64)v);
65112854Sgabeblack@google.com}
65212854Sgabeblack@google.com
65312854Sgabeblack@google.comsc_signed
65412854Sgabeblack@google.comoperator - (const sc_int_base &u, const sc_signed &v)
65512854Sgabeblack@google.com{
65612854Sgabeblack@google.com    return operator - ((int64)u, v);
65712854Sgabeblack@google.com}
65812854Sgabeblack@google.com
65912854Sgabeblack@google.comconst sc_signed &
66012854Sgabeblack@google.comsc_signed::operator -= (const sc_int_base &v)
66112854Sgabeblack@google.com{
66212854Sgabeblack@google.com    return operator -= ((int64)v);
66312854Sgabeblack@google.com}
66412854Sgabeblack@google.com
66512854Sgabeblack@google.com
66612854Sgabeblack@google.comsc_signed
66712854Sgabeblack@google.comoperator * (const sc_unsigned &u, const sc_int_base &v)
66812854Sgabeblack@google.com{
66912854Sgabeblack@google.com    return operator * (u, static_cast<int64>(v));
67012854Sgabeblack@google.com}
67112854Sgabeblack@google.com
67212854Sgabeblack@google.comsc_signed
67312854Sgabeblack@google.comoperator * (const sc_int_base &u, const sc_unsigned &v)
67412854Sgabeblack@google.com{
67512854Sgabeblack@google.com    return operator * (static_cast<int64>(u), v);
67612854Sgabeblack@google.com}
67712854Sgabeblack@google.com
67812854Sgabeblack@google.comsc_signed
67912854Sgabeblack@google.comoperator * (const sc_signed &u, const sc_int_base &v)
68012854Sgabeblack@google.com{
68112854Sgabeblack@google.com    return operator * (u, (int64)v);
68212854Sgabeblack@google.com}
68312854Sgabeblack@google.com
68412854Sgabeblack@google.comsc_signed
68512854Sgabeblack@google.comoperator * (const sc_int_base &u, const sc_signed &v)
68612854Sgabeblack@google.com{
68712854Sgabeblack@google.com    return operator * ((int64)u, v);
68812854Sgabeblack@google.com}
68912854Sgabeblack@google.com
69012854Sgabeblack@google.comconst sc_signed &
69112854Sgabeblack@google.comsc_signed::operator *= (const sc_int_base &v)
69212854Sgabeblack@google.com{
69312854Sgabeblack@google.com    return operator *= ((int64)v);
69412854Sgabeblack@google.com}
69512854Sgabeblack@google.com
69612854Sgabeblack@google.com
69712854Sgabeblack@google.comsc_signed
69812854Sgabeblack@google.comoperator / (const sc_unsigned &u, const sc_int_base &v)
69912854Sgabeblack@google.com{
70012854Sgabeblack@google.com    return operator / (u, static_cast<int64>(v));
70112854Sgabeblack@google.com}
70212854Sgabeblack@google.com
70312854Sgabeblack@google.comsc_signed
70412854Sgabeblack@google.comoperator / (const sc_int_base &u, const sc_unsigned &v)
70512854Sgabeblack@google.com{
70612854Sgabeblack@google.com    return operator / (static_cast<int64>(u), v);
70712854Sgabeblack@google.com}
70812854Sgabeblack@google.com
70912854Sgabeblack@google.comsc_signed
71012854Sgabeblack@google.comoperator / (const sc_signed &u, const sc_int_base &v)
71112854Sgabeblack@google.com{
71212854Sgabeblack@google.com    return operator / (u, (int64)v);
71312854Sgabeblack@google.com}
71412854Sgabeblack@google.com
71512854Sgabeblack@google.comsc_signed
71612854Sgabeblack@google.comoperator / (const sc_int_base &u, const sc_signed &v)
71712854Sgabeblack@google.com{
71812854Sgabeblack@google.com    return operator / ((int64)u, v);
71912854Sgabeblack@google.com}
72012854Sgabeblack@google.com
72112854Sgabeblack@google.comconst sc_signed &
72212854Sgabeblack@google.comsc_signed::operator /= (const sc_int_base &v)
72312854Sgabeblack@google.com{
72412854Sgabeblack@google.com    return operator /= ((int64)v);
72512854Sgabeblack@google.com}
72612854Sgabeblack@google.com
72712854Sgabeblack@google.com
72812854Sgabeblack@google.comsc_signed
72912854Sgabeblack@google.comoperator % (const sc_unsigned &u, const sc_int_base &v)
73012854Sgabeblack@google.com{
73112854Sgabeblack@google.com    return operator % (u, static_cast<int64>(v));
73212854Sgabeblack@google.com}
73312854Sgabeblack@google.com
73412854Sgabeblack@google.comsc_signed
73512854Sgabeblack@google.comoperator % (const sc_int_base &u, const sc_unsigned &v)
73612854Sgabeblack@google.com{
73712854Sgabeblack@google.com    return operator % (static_cast<int64>(u), v);
73812854Sgabeblack@google.com}
73912854Sgabeblack@google.com
74012854Sgabeblack@google.comsc_signed
74112854Sgabeblack@google.comoperator % (const sc_signed &u, const sc_int_base &v)
74212854Sgabeblack@google.com{
74312854Sgabeblack@google.com    return operator % (u, (int64)v);
74412854Sgabeblack@google.com}
74512854Sgabeblack@google.com
74612854Sgabeblack@google.comsc_signed
74712854Sgabeblack@google.comoperator % (const sc_int_base &u, const sc_signed &v)
74812854Sgabeblack@google.com{
74912854Sgabeblack@google.com    return operator % ((int64)u, v);
75012854Sgabeblack@google.com}
75112854Sgabeblack@google.com
75212854Sgabeblack@google.comconst sc_signed &
75312854Sgabeblack@google.comsc_signed::operator %= (const sc_int_base &v)
75412854Sgabeblack@google.com{
75512854Sgabeblack@google.com    return operator %= ((int64)v);
75612854Sgabeblack@google.com}
75712854Sgabeblack@google.com
75812854Sgabeblack@google.com
75912854Sgabeblack@google.comsc_signed
76012854Sgabeblack@google.comoperator & (const sc_unsigned &u, const sc_int_base &v)
76112854Sgabeblack@google.com{
76212854Sgabeblack@google.com    return operator & (u, static_cast<int64>(v));
76312854Sgabeblack@google.com}
76412854Sgabeblack@google.com
76512854Sgabeblack@google.comsc_signed
76612854Sgabeblack@google.comoperator & (const sc_int_base &u, const sc_unsigned &v)
76712854Sgabeblack@google.com{
76812854Sgabeblack@google.com    return operator & (static_cast<int64>(u), v);
76912854Sgabeblack@google.com}
77012854Sgabeblack@google.com
77112854Sgabeblack@google.comsc_signed
77212854Sgabeblack@google.comoperator & (const sc_signed &u, const sc_int_base &v)
77312854Sgabeblack@google.com{
77412854Sgabeblack@google.com    return operator & (u, (int64)v);
77512854Sgabeblack@google.com}
77612854Sgabeblack@google.com
77712854Sgabeblack@google.comsc_signed
77812854Sgabeblack@google.comoperator & (const sc_int_base &u, const sc_signed &v)
77912854Sgabeblack@google.com{
78012854Sgabeblack@google.com    return operator & ((int64)u, v);
78112854Sgabeblack@google.com}
78212854Sgabeblack@google.com
78312854Sgabeblack@google.comconst sc_signed &
78412854Sgabeblack@google.comsc_signed::operator &= (const sc_int_base &v)
78512854Sgabeblack@google.com{
78612854Sgabeblack@google.com    return operator &= ((int64)v);
78712854Sgabeblack@google.com}
78812854Sgabeblack@google.com
78912854Sgabeblack@google.com
79012854Sgabeblack@google.comsc_signed
79112854Sgabeblack@google.comoperator | (const sc_unsigned &u, const sc_int_base &v)
79212854Sgabeblack@google.com{
79312854Sgabeblack@google.com    return operator | (u, static_cast<int64>(v));
79412854Sgabeblack@google.com}
79512854Sgabeblack@google.com
79612854Sgabeblack@google.comsc_signed
79712854Sgabeblack@google.comoperator | (const sc_int_base &u, const sc_unsigned &v)
79812854Sgabeblack@google.com{
79912854Sgabeblack@google.com    return operator | (static_cast<int64>(u), v);
80012854Sgabeblack@google.com}
80112854Sgabeblack@google.com
80212854Sgabeblack@google.comsc_signed
80312854Sgabeblack@google.comoperator | (const sc_signed& u, const sc_int_base &v)
80412854Sgabeblack@google.com{
80512854Sgabeblack@google.com    return operator|(u, (int64)v);
80612854Sgabeblack@google.com}
80712854Sgabeblack@google.com
80812854Sgabeblack@google.comsc_signed
80912854Sgabeblack@google.comoperator | (const sc_int_base &u, const sc_signed &v)
81012854Sgabeblack@google.com{
81112854Sgabeblack@google.com    return operator | ((int64)u, v);
81212854Sgabeblack@google.com}
81312854Sgabeblack@google.com
81412854Sgabeblack@google.comconst sc_signed &
81512854Sgabeblack@google.comsc_signed::operator |= (const sc_int_base &v)
81612854Sgabeblack@google.com{
81712854Sgabeblack@google.com    return operator |= ((int64)v);
81812854Sgabeblack@google.com}
81912854Sgabeblack@google.com
82012854Sgabeblack@google.com
82112854Sgabeblack@google.comsc_signed
82212854Sgabeblack@google.comoperator ^ (const sc_unsigned &u, const sc_int_base &v)
82312854Sgabeblack@google.com{
82412854Sgabeblack@google.com    return operator ^ (u, static_cast<int64>(v));
82512854Sgabeblack@google.com}
82612854Sgabeblack@google.com
82712854Sgabeblack@google.comsc_signed
82812854Sgabeblack@google.comoperator ^ (const sc_int_base &u, const sc_unsigned &v)
82912854Sgabeblack@google.com{
83012854Sgabeblack@google.com    return operator ^ (static_cast<int64>(u), v);
83112854Sgabeblack@google.com}
83212854Sgabeblack@google.com
83312854Sgabeblack@google.comsc_signed
83412854Sgabeblack@google.comoperator ^ (const sc_signed &u, const sc_int_base &v)
83512854Sgabeblack@google.com{
83612854Sgabeblack@google.com    return operator ^ (u, (int64)v);
83712854Sgabeblack@google.com}
83812854Sgabeblack@google.com
83912854Sgabeblack@google.comsc_signed
84012854Sgabeblack@google.comoperator ^ (const sc_int_base &u, const sc_signed &v)
84112854Sgabeblack@google.com{
84212854Sgabeblack@google.com    return operator ^ ((int64)u, v);
84312854Sgabeblack@google.com}
84412854Sgabeblack@google.com
84512854Sgabeblack@google.comconst sc_signed &
84612854Sgabeblack@google.comsc_signed::operator ^= (const sc_int_base &v)
84712854Sgabeblack@google.com{
84812854Sgabeblack@google.com    return operator ^= ((int64)v);
84912854Sgabeblack@google.com}
85012854Sgabeblack@google.com
85112854Sgabeblack@google.com
85212854Sgabeblack@google.comsc_signed
85312854Sgabeblack@google.comoperator << (const sc_signed &u, const sc_int_base &v)
85412854Sgabeblack@google.com{
85512854Sgabeblack@google.com    return operator << (u, (int64)v);
85612854Sgabeblack@google.com}
85712854Sgabeblack@google.com
85812854Sgabeblack@google.comconst sc_signed &
85912854Sgabeblack@google.comsc_signed::operator <<= (const sc_int_base &v)
86012854Sgabeblack@google.com{
86112854Sgabeblack@google.com    return operator <<= ((int64)v);
86212854Sgabeblack@google.com}
86312854Sgabeblack@google.com
86412854Sgabeblack@google.com
86512854Sgabeblack@google.comsc_signed
86612854Sgabeblack@google.comoperator >> (const sc_signed &u, const sc_int_base &v)
86712854Sgabeblack@google.com{
86812854Sgabeblack@google.com    return operator >> (u, (int64)v);
86912854Sgabeblack@google.com}
87012854Sgabeblack@google.com
87112854Sgabeblack@google.comconst sc_signed &
87212854Sgabeblack@google.comsc_signed::operator >>= (const sc_int_base &v)
87312854Sgabeblack@google.com{
87412854Sgabeblack@google.com    return operator >>= ((int64)v);
87512854Sgabeblack@google.com}
87612854Sgabeblack@google.com
87712854Sgabeblack@google.com
87812854Sgabeblack@google.combool
87912854Sgabeblack@google.comoperator == (const sc_signed &u, const sc_int_base &v)
88012854Sgabeblack@google.com{
88112854Sgabeblack@google.com    return operator == (u, (int64)v);
88212854Sgabeblack@google.com}
88312854Sgabeblack@google.com
88412854Sgabeblack@google.combool
88512854Sgabeblack@google.comoperator == (const sc_int_base &u, const sc_signed &v)
88612854Sgabeblack@google.com{
88712854Sgabeblack@google.com    return operator == ((int64)u, v);
88812854Sgabeblack@google.com}
88912854Sgabeblack@google.com
89012854Sgabeblack@google.com
89112854Sgabeblack@google.combool
89212854Sgabeblack@google.comoperator != (const sc_signed &u, const sc_int_base &v)
89312854Sgabeblack@google.com{
89412854Sgabeblack@google.com    return operator != (u, (int64)v);
89512854Sgabeblack@google.com}
89612854Sgabeblack@google.com
89712854Sgabeblack@google.combool
89812854Sgabeblack@google.comoperator != (const sc_int_base &u, const sc_signed &v)
89912854Sgabeblack@google.com{
90012854Sgabeblack@google.com    return operator != ((int64)u, v);
90112854Sgabeblack@google.com}
90212854Sgabeblack@google.com
90312854Sgabeblack@google.com
90412854Sgabeblack@google.combool
90512854Sgabeblack@google.comoperator < (const sc_signed &u, const sc_int_base &v)
90612854Sgabeblack@google.com{
90712854Sgabeblack@google.com    return operator < (u, (int64)v);
90812854Sgabeblack@google.com}
90912854Sgabeblack@google.com
91012854Sgabeblack@google.combool
91112854Sgabeblack@google.comoperator  <  (const sc_int_base &u, const sc_signed &v)
91212854Sgabeblack@google.com{
91312854Sgabeblack@google.com    return operator < ((int64)u, v);
91412854Sgabeblack@google.com}
91512854Sgabeblack@google.com
91612854Sgabeblack@google.com
91712854Sgabeblack@google.combool
91812854Sgabeblack@google.comoperator  <=  (const sc_signed &u, const sc_int_base &v)
91912854Sgabeblack@google.com{
92012854Sgabeblack@google.com    return operator <= (u, (int64)v);
92112854Sgabeblack@google.com}
92212854Sgabeblack@google.com
92312854Sgabeblack@google.combool
92412854Sgabeblack@google.comoperator  <=  (const sc_int_base &u, const sc_signed &v)
92512854Sgabeblack@google.com{
92612854Sgabeblack@google.com    return operator <= ((int64)u, v);
92712854Sgabeblack@google.com}
92812854Sgabeblack@google.com
92912854Sgabeblack@google.com
93012854Sgabeblack@google.combool
93112854Sgabeblack@google.comoperator  >  (const sc_signed &u, const sc_int_base &v)
93212854Sgabeblack@google.com{
93312854Sgabeblack@google.com    return operator > (u, (int64)v);
93412854Sgabeblack@google.com}
93512854Sgabeblack@google.com
93612854Sgabeblack@google.combool
93712854Sgabeblack@google.comoperator  >  (const sc_int_base &u, const sc_signed &v)
93812854Sgabeblack@google.com{
93912854Sgabeblack@google.com    return operator > ((int64)u, v);
94012854Sgabeblack@google.com}
94112854Sgabeblack@google.com
94212854Sgabeblack@google.com
94312854Sgabeblack@google.combool
94412854Sgabeblack@google.comoperator  >=  (const sc_signed &u, const sc_int_base &v)
94512854Sgabeblack@google.com{
94612854Sgabeblack@google.com    return operator >= (u, (int64)v);
94712854Sgabeblack@google.com}
94812854Sgabeblack@google.com
94912854Sgabeblack@google.combool
95012854Sgabeblack@google.comoperator  >=  (const sc_int_base &u, const sc_signed &v)
95112854Sgabeblack@google.com{
95212854Sgabeblack@google.com    return operator >= ((int64)u, v);
95312854Sgabeblack@google.com}
95412854Sgabeblack@google.com
95512854Sgabeblack@google.com
95612854Sgabeblack@google.com// ----------------------------------------------------------------------------
95712854Sgabeblack@google.com//  SECTION: Interfacing with sc_uint_base
95812854Sgabeblack@google.com// ----------------------------------------------------------------------------
95912854Sgabeblack@google.com
96012854Sgabeblack@google.comconst sc_signed &
96112854Sgabeblack@google.comsc_signed::operator = (const sc_uint_base &v)
96212854Sgabeblack@google.com{
96312854Sgabeblack@google.com    return operator = ((uint64)v);
96412854Sgabeblack@google.com}
96512854Sgabeblack@google.com
96612854Sgabeblack@google.com
96712854Sgabeblack@google.comsc_signed
96812854Sgabeblack@google.comoperator + (const sc_signed &u, const sc_uint_base &v)
96912854Sgabeblack@google.com{
97012854Sgabeblack@google.com    return operator + (u, (uint64)v);
97112854Sgabeblack@google.com}
97212854Sgabeblack@google.com
97312854Sgabeblack@google.comsc_signed
97412854Sgabeblack@google.comoperator + (const sc_uint_base &u, const sc_signed &v)
97512854Sgabeblack@google.com{
97612854Sgabeblack@google.com    return operator + ((uint64)u, v);
97712854Sgabeblack@google.com}
97812854Sgabeblack@google.com
97912854Sgabeblack@google.comconst sc_signed &
98012854Sgabeblack@google.comsc_signed::operator += (const sc_uint_base &v)
98112854Sgabeblack@google.com{
98212854Sgabeblack@google.com    return operator += ((uint64)v);
98312854Sgabeblack@google.com}
98412854Sgabeblack@google.com
98512854Sgabeblack@google.com
98612854Sgabeblack@google.comsc_signed
98712854Sgabeblack@google.comoperator - (const sc_unsigned &u, const sc_uint_base &v)
98812854Sgabeblack@google.com{
98912854Sgabeblack@google.com    return operator - (u, (uint64)v);
99012854Sgabeblack@google.com}
99112854Sgabeblack@google.com
99212854Sgabeblack@google.comsc_signed
99312854Sgabeblack@google.comoperator - (const sc_uint_base &u, const sc_unsigned &v)
99412854Sgabeblack@google.com{
99512854Sgabeblack@google.com    return operator - ((uint64)u, v);
99612854Sgabeblack@google.com}
99712854Sgabeblack@google.com
99812854Sgabeblack@google.comsc_signed
99912854Sgabeblack@google.comoperator - (const sc_signed &u, const sc_uint_base &v)
100012854Sgabeblack@google.com{
100112854Sgabeblack@google.com    return operator - (u, (uint64)v);
100212854Sgabeblack@google.com}
100312854Sgabeblack@google.com
100412854Sgabeblack@google.comsc_signed
100512854Sgabeblack@google.comoperator - (const sc_uint_base &u, const sc_signed &v)
100612854Sgabeblack@google.com{
100712854Sgabeblack@google.com    return operator - ((uint64)u, v);
100812854Sgabeblack@google.com}
100912854Sgabeblack@google.com
101012854Sgabeblack@google.comconst sc_signed &
101112854Sgabeblack@google.comsc_signed::operator -= (const sc_uint_base &v)
101212854Sgabeblack@google.com{
101312854Sgabeblack@google.com    return operator -= ((uint64)v);
101412854Sgabeblack@google.com}
101512854Sgabeblack@google.com
101612854Sgabeblack@google.com
101712854Sgabeblack@google.comsc_signed
101812854Sgabeblack@google.comoperator * (const sc_signed &u, const sc_uint_base &v)
101912854Sgabeblack@google.com{
102012854Sgabeblack@google.com    return operator * (u, (uint64)v);
102112854Sgabeblack@google.com}
102212854Sgabeblack@google.com
102312854Sgabeblack@google.comsc_signed
102412854Sgabeblack@google.comoperator * (const sc_uint_base &u, const sc_signed &v)
102512854Sgabeblack@google.com{
102612854Sgabeblack@google.com    return operator * ((uint64)u, v);
102712854Sgabeblack@google.com}
102812854Sgabeblack@google.com
102912854Sgabeblack@google.comconst sc_signed &
103012854Sgabeblack@google.comsc_signed::operator *= (const sc_uint_base &v)
103112854Sgabeblack@google.com{
103212854Sgabeblack@google.com    return operator *= ((uint64)v);
103312854Sgabeblack@google.com}
103412854Sgabeblack@google.com
103512854Sgabeblack@google.com
103612854Sgabeblack@google.comsc_signed
103712854Sgabeblack@google.comoperator / (const sc_signed &u, const sc_uint_base &v)
103812854Sgabeblack@google.com{
103912854Sgabeblack@google.com    return operator / (u, (uint64)v);
104012854Sgabeblack@google.com}
104112854Sgabeblack@google.com
104212854Sgabeblack@google.comsc_signed
104312854Sgabeblack@google.comoperator / (const sc_uint_base &u, const sc_signed &v)
104412854Sgabeblack@google.com{
104512854Sgabeblack@google.com    return operator / ((uint64)u, v);
104612854Sgabeblack@google.com}
104712854Sgabeblack@google.com
104812854Sgabeblack@google.comconst sc_signed &
104912854Sgabeblack@google.comsc_signed::operator /= (const sc_uint_base &v)
105012854Sgabeblack@google.com{
105112854Sgabeblack@google.com    return operator /= ((uint64)v);
105212854Sgabeblack@google.com}
105312854Sgabeblack@google.com
105412854Sgabeblack@google.com
105512854Sgabeblack@google.comsc_signed
105612854Sgabeblack@google.comoperator % (const sc_signed &u, const sc_uint_base &v)
105712854Sgabeblack@google.com{
105812854Sgabeblack@google.com    return operator % (u, (uint64)v);
105912854Sgabeblack@google.com}
106012854Sgabeblack@google.com
106112854Sgabeblack@google.comsc_signed
106212854Sgabeblack@google.comoperator % (const sc_uint_base &u, const sc_signed &v)
106312854Sgabeblack@google.com{
106412854Sgabeblack@google.com    return operator % ((uint64)u, v);
106512854Sgabeblack@google.com}
106612854Sgabeblack@google.com
106712854Sgabeblack@google.comconst sc_signed&
106812854Sgabeblack@google.comsc_signed::operator %= (const sc_uint_base &v)
106912854Sgabeblack@google.com{
107012854Sgabeblack@google.com    return operator %= ((uint64)v);
107112854Sgabeblack@google.com}
107212854Sgabeblack@google.com
107312854Sgabeblack@google.com
107412854Sgabeblack@google.comsc_signed
107512854Sgabeblack@google.comoperator & (const sc_signed &u, const sc_uint_base &v)
107612854Sgabeblack@google.com{
107712854Sgabeblack@google.com    return operator & (u, (uint64)v);
107812854Sgabeblack@google.com}
107912854Sgabeblack@google.com
108012854Sgabeblack@google.comsc_signed
108112854Sgabeblack@google.comoperator & (const sc_uint_base &u, const sc_signed &v)
108212854Sgabeblack@google.com{
108312854Sgabeblack@google.com    return operator & ((uint64)u, v);
108412854Sgabeblack@google.com}
108512854Sgabeblack@google.com
108612854Sgabeblack@google.comconst sc_signed &
108712854Sgabeblack@google.comsc_signed::operator &= (const sc_uint_base &v)
108812854Sgabeblack@google.com{
108912854Sgabeblack@google.com    return operator &= ((uint64)v);
109012854Sgabeblack@google.com}
109112854Sgabeblack@google.com
109212854Sgabeblack@google.com
109312854Sgabeblack@google.comsc_signed
109412854Sgabeblack@google.comoperator | (const sc_signed &u, const sc_uint_base &v)
109512854Sgabeblack@google.com{
109612854Sgabeblack@google.com    return operator | (u, (uint64)v);
109712854Sgabeblack@google.com}
109812854Sgabeblack@google.com
109912854Sgabeblack@google.comsc_signed
110012854Sgabeblack@google.comoperator | (const sc_uint_base &u, const sc_signed &v)
110112854Sgabeblack@google.com{
110212854Sgabeblack@google.com    return operator | ((uint64)u, v);
110312854Sgabeblack@google.com}
110412854Sgabeblack@google.com
110512854Sgabeblack@google.comconst sc_signed &
110612854Sgabeblack@google.comsc_signed::operator |= (const sc_uint_base &v)
110712854Sgabeblack@google.com{
110812854Sgabeblack@google.com    return operator |= ((uint64)v);
110912854Sgabeblack@google.com}
111012854Sgabeblack@google.com
111112854Sgabeblack@google.com
111212854Sgabeblack@google.comsc_signed
111312854Sgabeblack@google.comoperator ^ (const sc_signed &u, const sc_uint_base &v)
111412854Sgabeblack@google.com{
111512854Sgabeblack@google.com    return operator ^ (u, (uint64)v);
111612854Sgabeblack@google.com}
111712854Sgabeblack@google.com
111812854Sgabeblack@google.comsc_signed
111912854Sgabeblack@google.comoperator ^ (const sc_uint_base &u, const sc_signed &v)
112012854Sgabeblack@google.com{
112112854Sgabeblack@google.com    return operator ^ ((uint64)u, v);
112212854Sgabeblack@google.com}
112312854Sgabeblack@google.com
112412854Sgabeblack@google.comconst sc_signed &
112512854Sgabeblack@google.comsc_signed::operator ^= (const sc_uint_base &v)
112612854Sgabeblack@google.com{
112712854Sgabeblack@google.com    return operator ^= ((uint64)v);
112812854Sgabeblack@google.com}
112912854Sgabeblack@google.com
113012854Sgabeblack@google.com
113112854Sgabeblack@google.comsc_signed
113212854Sgabeblack@google.comoperator << (const sc_signed &u, const sc_uint_base &v)
113312854Sgabeblack@google.com{
113412854Sgabeblack@google.com    return operator << (u, (uint64)v);
113512854Sgabeblack@google.com}
113612854Sgabeblack@google.com
113712854Sgabeblack@google.comconst sc_signed &
113812854Sgabeblack@google.comsc_signed::operator <<= (const sc_uint_base &v)
113912854Sgabeblack@google.com{
114012854Sgabeblack@google.com    return operator <<= ((uint64)v);
114112854Sgabeblack@google.com}
114212854Sgabeblack@google.com
114312854Sgabeblack@google.com
114412854Sgabeblack@google.comsc_signed
114512854Sgabeblack@google.comoperator >> (const sc_signed& u, const sc_uint_base& v)
114612854Sgabeblack@google.com{
114712854Sgabeblack@google.com    return operator >> (u, (uint64)v);
114812854Sgabeblack@google.com}
114912854Sgabeblack@google.com
115012854Sgabeblack@google.comconst sc_signed &
115112854Sgabeblack@google.comsc_signed::operator >>= (const sc_uint_base& v)
115212854Sgabeblack@google.com{
115312854Sgabeblack@google.com    return operator >>= ((uint64)v);
115412854Sgabeblack@google.com}
115512854Sgabeblack@google.com
115612854Sgabeblack@google.com
115712854Sgabeblack@google.combool
115812854Sgabeblack@google.comoperator == (const sc_signed &u, const sc_uint_base &v)
115912854Sgabeblack@google.com{
116012854Sgabeblack@google.com    return operator == (u, (uint64)v);
116112854Sgabeblack@google.com}
116212854Sgabeblack@google.com
116312854Sgabeblack@google.combool
116412854Sgabeblack@google.comoperator == (const sc_uint_base &u, const sc_signed &v)
116512854Sgabeblack@google.com{
116612854Sgabeblack@google.com    return operator == ((uint64)u, v);
116712854Sgabeblack@google.com}
116812854Sgabeblack@google.com
116912854Sgabeblack@google.com
117012854Sgabeblack@google.combool
117112854Sgabeblack@google.comoperator != (const sc_signed &u, const sc_uint_base &v)
117212854Sgabeblack@google.com{
117312854Sgabeblack@google.com    return operator != (u, (uint64)v);
117412854Sgabeblack@google.com}
117512854Sgabeblack@google.com
117612854Sgabeblack@google.combool
117712854Sgabeblack@google.comoperator != (const sc_uint_base &u, const sc_signed &v)
117812854Sgabeblack@google.com{
117912854Sgabeblack@google.com    return operator != ((uint64)u, v);
118012854Sgabeblack@google.com}
118112854Sgabeblack@google.com
118212854Sgabeblack@google.com
118312854Sgabeblack@google.combool
118412854Sgabeblack@google.comoperator < (const sc_signed &u, const sc_uint_base &v)
118512854Sgabeblack@google.com{
118612854Sgabeblack@google.com    return operator < (u, (uint64)v);
118712854Sgabeblack@google.com}
118812854Sgabeblack@google.com
118912854Sgabeblack@google.combool
119012854Sgabeblack@google.comoperator < (const sc_uint_base &u, const sc_signed &v)
119112854Sgabeblack@google.com{
119212854Sgabeblack@google.com    return operator < ((uint64)u, v);
119312854Sgabeblack@google.com}
119412854Sgabeblack@google.com
119512854Sgabeblack@google.com
119612854Sgabeblack@google.combool
119712854Sgabeblack@google.comoperator <= (const sc_signed &u, const sc_uint_base &v)
119812854Sgabeblack@google.com{
119912854Sgabeblack@google.com    return operator <= (u, (uint64)v);
120012854Sgabeblack@google.com}
120112854Sgabeblack@google.com
120212854Sgabeblack@google.combool
120312854Sgabeblack@google.comoperator <= (const sc_uint_base &u, const sc_signed &v)
120412854Sgabeblack@google.com{
120512854Sgabeblack@google.com    return operator <= ((uint64)u, v);
120612854Sgabeblack@google.com}
120712854Sgabeblack@google.com
120812854Sgabeblack@google.com
120912854Sgabeblack@google.combool
121012854Sgabeblack@google.comoperator > (const sc_signed &u, const sc_uint_base &v)
121112854Sgabeblack@google.com{
121212854Sgabeblack@google.com    return operator > (u, (uint64)v);
121312854Sgabeblack@google.com}
121412854Sgabeblack@google.com
121512854Sgabeblack@google.combool
121612854Sgabeblack@google.comoperator > (const sc_uint_base &u, const sc_signed &v)
121712854Sgabeblack@google.com{
121812854Sgabeblack@google.com    return operator > ((uint64)u, v);
121912854Sgabeblack@google.com}
122012854Sgabeblack@google.com
122112854Sgabeblack@google.com
122212854Sgabeblack@google.combool
122312854Sgabeblack@google.comoperator >= (const sc_signed &u, const sc_uint_base &v)
122412854Sgabeblack@google.com{
122512854Sgabeblack@google.com    return operator >= (u, (uint64)v);
122612854Sgabeblack@google.com}
122712854Sgabeblack@google.com
122812854Sgabeblack@google.combool
122912854Sgabeblack@google.comoperator >= (const sc_uint_base &u, const sc_signed &v)
123012854Sgabeblack@google.com{
123112854Sgabeblack@google.com    return operator >= ((uint64)u, v);
123212854Sgabeblack@google.com}
123312854Sgabeblack@google.com
123412854Sgabeblack@google.com
123512854Sgabeblack@google.com// ----------------------------------------------------------------------------
123612854Sgabeblack@google.com//  SECTION: Input and output operators
123712854Sgabeblack@google.com// ----------------------------------------------------------------------------
123812854Sgabeblack@google.com
123912854Sgabeblack@google.com// Operators in this section are included from sc_nbcommon.cpp.
124012854Sgabeblack@google.com
124112854Sgabeblack@google.com
124212854Sgabeblack@google.com// ----------------------------------------------------------------------------
124312854Sgabeblack@google.com//  SECTION: Operator macros.
124412854Sgabeblack@google.com// ----------------------------------------------------------------------------
124512854Sgabeblack@google.com
124612854Sgabeblack@google.com#define CONVERT_LONG(u) \
124712854Sgabeblack@google.comsmall_type u ## s = get_sign(u); \
124812854Sgabeblack@google.comsc_digit u ## d[DIGITS_PER_ULONG]; \
124912854Sgabeblack@google.comfrom_uint(DIGITS_PER_ULONG, u ## d, (unsigned long) u);
125012854Sgabeblack@google.com
125112854Sgabeblack@google.com#define CONVERT_LONG_2(u) \
125212854Sgabeblack@google.comsc_digit u ## d[DIGITS_PER_ULONG]; \
125312854Sgabeblack@google.comfrom_uint(DIGITS_PER_ULONG, u ## d, (unsigned long) u);
125412854Sgabeblack@google.com
125512854Sgabeblack@google.com#define CONVERT_INT(u) \
125612854Sgabeblack@google.comsmall_type u ## s = get_sign(u); \
125712854Sgabeblack@google.comsc_digit u ## d[DIGITS_PER_UINT]; \
125812854Sgabeblack@google.comfrom_uint(DIGITS_PER_UINT, u ## d, (unsigned int) u);
125912854Sgabeblack@google.com
126012854Sgabeblack@google.com#define CONVERT_INT_2(u) \
126112854Sgabeblack@google.comsc_digit u ## d[DIGITS_PER_UINT]; \
126212854Sgabeblack@google.comfrom_uint(DIGITS_PER_UINT, u ## d, (unsigned int) u);
126312854Sgabeblack@google.com
126412854Sgabeblack@google.com#define CONVERT_INT64(u) \
126512854Sgabeblack@google.comsmall_type u ## s = get_sign(u); \
126612854Sgabeblack@google.comsc_digit u ## d[DIGITS_PER_UINT64]; \
126712854Sgabeblack@google.comfrom_uint(DIGITS_PER_UINT64, u ## d, (uint64) u);
126812854Sgabeblack@google.com
126912854Sgabeblack@google.com#define CONVERT_INT64_2(u) \
127012854Sgabeblack@google.comsc_digit u ## d[DIGITS_PER_UINT64]; \
127112854Sgabeblack@google.comfrom_uint(DIGITS_PER_UINT64, u ## d, (uint64) u);
127212854Sgabeblack@google.com
127312854Sgabeblack@google.com
127412854Sgabeblack@google.com// ----------------------------------------------------------------------------
127512854Sgabeblack@google.com//  SECTION: PLUS operators: +, +=, ++
127612854Sgabeblack@google.com// ----------------------------------------------------------------------------
127712854Sgabeblack@google.com
127812854Sgabeblack@google.com// Cases to consider when computing u + v:
127912854Sgabeblack@google.com// 1. 0 + v = v
128012854Sgabeblack@google.com// 2. u + 0 = u
128112854Sgabeblack@google.com// 3. if sgn(u) == sgn(v)
128212854Sgabeblack@google.com//    3.1 u + v = +(u + v) = sgn(u) * (u + v)
128312854Sgabeblack@google.com//    3.2 (-u) + (-v) = -(u + v) = sgn(u) * (u + v)
128412854Sgabeblack@google.com// 4. if sgn(u) != sgn(v)
128512854Sgabeblack@google.com//    4.1 u + (-v) = u - v = sgn(u) * (u - v)
128612854Sgabeblack@google.com//    4.2 (-u) + v = -(u - v) ==> sgn(u) * (u - v)
128712854Sgabeblack@google.com//
128812854Sgabeblack@google.com// Specialization of above cases for computing ++u or u++:
128912854Sgabeblack@google.com// 1. 0 + 1 = 1
129012854Sgabeblack@google.com// 3. u + 1 = u + 1 = sgn(u) * (u + 1)
129112854Sgabeblack@google.com// 4. (-u) + 1 = -(u - 1) = sgn(u) * (u - 1)
129212854Sgabeblack@google.com
129312854Sgabeblack@google.comsc_signed
129412854Sgabeblack@google.comoperator + (const sc_unsigned &u, const sc_signed &v)
129512854Sgabeblack@google.com{
129612854Sgabeblack@google.com    if (u.sgn == SC_ZERO) // case 1
129712854Sgabeblack@google.com        return sc_signed(v);
129812854Sgabeblack@google.com
129912854Sgabeblack@google.com    if (v.sgn == SC_ZERO) // case 2
130012854Sgabeblack@google.com        return sc_signed(u);
130112854Sgabeblack@google.com
130212854Sgabeblack@google.com    // cases 3 and 4
130312854Sgabeblack@google.com    return add_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit,
130412854Sgabeblack@google.com                             v.sgn, v.nbits, v.ndigits, v.digit);
130512854Sgabeblack@google.com}
130612854Sgabeblack@google.com
130712854Sgabeblack@google.com
130812854Sgabeblack@google.comsc_signed
130912854Sgabeblack@google.comoperator + (const sc_signed &u, const sc_unsigned &v)
131012854Sgabeblack@google.com{
131112854Sgabeblack@google.com    if (u.sgn == SC_ZERO) // case 1
131212854Sgabeblack@google.com        return sc_signed(v);
131312854Sgabeblack@google.com
131412854Sgabeblack@google.com    if (v.sgn == SC_ZERO) // case 2
131512854Sgabeblack@google.com        return sc_signed(u);
131612854Sgabeblack@google.com
131712854Sgabeblack@google.com    // cases 3 and 4
131812854Sgabeblack@google.com    return add_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit,
131912854Sgabeblack@google.com                             v.sgn, v.nbits, v.ndigits, v.digit);
132012854Sgabeblack@google.com}
132112854Sgabeblack@google.com
132212854Sgabeblack@google.com
132312854Sgabeblack@google.comsc_signed
132412854Sgabeblack@google.comoperator + (const sc_signed &u, const sc_signed &v)
132512854Sgabeblack@google.com{
132612854Sgabeblack@google.com    if (u.sgn == SC_ZERO) // case 1
132712854Sgabeblack@google.com        return sc_signed(v);
132812854Sgabeblack@google.com
132912854Sgabeblack@google.com    if (v.sgn == SC_ZERO) // case 2
133012854Sgabeblack@google.com        return sc_signed(u);
133112854Sgabeblack@google.com
133212854Sgabeblack@google.com    // cases 3 and 4
133312854Sgabeblack@google.com    return add_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit,
133412854Sgabeblack@google.com                             v.sgn, v.nbits, v.ndigits, v.digit);
133512854Sgabeblack@google.com}
133612854Sgabeblack@google.com
133712854Sgabeblack@google.com
133812854Sgabeblack@google.comsc_signed
133912854Sgabeblack@google.comoperator + (const sc_signed &u, int64 v)
134012854Sgabeblack@google.com{
134112854Sgabeblack@google.com    if (v == 0) // case 2
134212854Sgabeblack@google.com        return sc_signed(u);
134312854Sgabeblack@google.com
134412854Sgabeblack@google.com    CONVERT_INT64(v);
134512854Sgabeblack@google.com
134612854Sgabeblack@google.com    if (u.sgn == SC_ZERO) // case 1
134712854Sgabeblack@google.com        return sc_signed(vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd, false);
134812854Sgabeblack@google.com
134912854Sgabeblack@google.com    // cases 3 and 4
135012854Sgabeblack@google.com    return add_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit,
135112854Sgabeblack@google.com                             vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd);
135212854Sgabeblack@google.com}
135312854Sgabeblack@google.com
135412854Sgabeblack@google.com
135512854Sgabeblack@google.comsc_signed
135612854Sgabeblack@google.comoperator + (int64 u, const sc_signed &v)
135712854Sgabeblack@google.com{
135812854Sgabeblack@google.com    if (u == 0) // case 1
135912854Sgabeblack@google.com        return sc_signed(v);
136012854Sgabeblack@google.com
136112854Sgabeblack@google.com    CONVERT_INT64(u);
136212854Sgabeblack@google.com
136312854Sgabeblack@google.com    if (v.sgn == SC_ZERO) // case 2
136412854Sgabeblack@google.com        return sc_signed(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud, false);
136512854Sgabeblack@google.com
136612854Sgabeblack@google.com    // cases 3 and 4
136712854Sgabeblack@google.com    return add_signed_friend(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud,
136812854Sgabeblack@google.com                             v.sgn, v.nbits, v.ndigits, v.digit);
136912854Sgabeblack@google.com}
137012854Sgabeblack@google.com
137112854Sgabeblack@google.com
137212854Sgabeblack@google.comsc_signed
137312854Sgabeblack@google.comoperator + (const sc_unsigned &u, int64 v)
137412854Sgabeblack@google.com{
137512854Sgabeblack@google.com    if (v == 0) // case 2
137612854Sgabeblack@google.com        return sc_signed(u);
137712854Sgabeblack@google.com
137812854Sgabeblack@google.com    CONVERT_INT64(v);
137912854Sgabeblack@google.com
138012854Sgabeblack@google.com    if (u.sgn == SC_ZERO) // case 1
138112854Sgabeblack@google.com        return sc_signed(vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd, false);
138212854Sgabeblack@google.com
138312854Sgabeblack@google.com    // cases 3 and 4
138412854Sgabeblack@google.com    return add_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit,
138512854Sgabeblack@google.com                             vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd);
138612854Sgabeblack@google.com}
138712854Sgabeblack@google.com
138812854Sgabeblack@google.com
138912854Sgabeblack@google.comsc_signed
139012854Sgabeblack@google.comoperator + (int64 u, const sc_unsigned &v)
139112854Sgabeblack@google.com{
139212854Sgabeblack@google.com    if (u == 0) // case 1
139312854Sgabeblack@google.com        return sc_signed(v);
139412854Sgabeblack@google.com
139512854Sgabeblack@google.com    CONVERT_INT64(u);
139612854Sgabeblack@google.com
139712854Sgabeblack@google.com    if (v.sgn == SC_ZERO) // case 2
139812854Sgabeblack@google.com        return sc_signed(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud, false);
139912854Sgabeblack@google.com
140012854Sgabeblack@google.com    // cases 3 and 4
140112854Sgabeblack@google.com    return add_signed_friend(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud,
140212854Sgabeblack@google.com                             v.sgn, v.nbits, v.ndigits, v.digit);
140312854Sgabeblack@google.com}
140412854Sgabeblack@google.com
140512854Sgabeblack@google.com
140612854Sgabeblack@google.comsc_signed
140712854Sgabeblack@google.comoperator + (const sc_signed &u, uint64 v)
140812854Sgabeblack@google.com{
140912854Sgabeblack@google.com    if (v == 0) // case 2
141012854Sgabeblack@google.com        return sc_signed(u);
141112854Sgabeblack@google.com
141212854Sgabeblack@google.com    CONVERT_INT64(v);
141312854Sgabeblack@google.com
141412854Sgabeblack@google.com    if (u.sgn == SC_ZERO) // case 1
141512854Sgabeblack@google.com        return sc_signed(vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd, false);
141612854Sgabeblack@google.com
141712854Sgabeblack@google.com    // cases 3 and 4
141812854Sgabeblack@google.com    return add_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit,
141912854Sgabeblack@google.com                             vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd);
142012854Sgabeblack@google.com}
142112854Sgabeblack@google.com
142212854Sgabeblack@google.com
142312854Sgabeblack@google.comsc_signed
142412854Sgabeblack@google.comoperator + (uint64 u, const sc_signed &v)
142512854Sgabeblack@google.com{
142612854Sgabeblack@google.com    if (u == 0) // case 1
142712854Sgabeblack@google.com        return sc_signed(v);
142812854Sgabeblack@google.com
142912854Sgabeblack@google.com    CONVERT_INT64(u);
143012854Sgabeblack@google.com
143112854Sgabeblack@google.com    if (v.sgn == SC_ZERO) // case 2
143212854Sgabeblack@google.com        return sc_signed(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud, false);
143312854Sgabeblack@google.com
143412854Sgabeblack@google.com    // cases 3 and 4
143512854Sgabeblack@google.com    return add_signed_friend(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud,
143612854Sgabeblack@google.com                             v.sgn, v.nbits, v.ndigits, v.digit);
143712854Sgabeblack@google.com}
143812854Sgabeblack@google.com
143912854Sgabeblack@google.com
144012854Sgabeblack@google.comsc_signed
144112854Sgabeblack@google.comoperator + (const sc_signed &u, long v)
144212854Sgabeblack@google.com{
144312854Sgabeblack@google.com    if (v == 0) // case 2
144412854Sgabeblack@google.com        return sc_signed(u);
144512854Sgabeblack@google.com
144612854Sgabeblack@google.com    CONVERT_LONG(v);
144712854Sgabeblack@google.com
144812854Sgabeblack@google.com    if (u.sgn == SC_ZERO) // case 1
144912854Sgabeblack@google.com        return sc_signed(vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd, false);
145012854Sgabeblack@google.com
145112854Sgabeblack@google.com    // cases 3 and 4
145212854Sgabeblack@google.com    return add_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit,
145312854Sgabeblack@google.com                             vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd);
145412854Sgabeblack@google.com}
145512854Sgabeblack@google.com
145612854Sgabeblack@google.com
145712854Sgabeblack@google.comsc_signed
145812854Sgabeblack@google.comoperator + (long u, const sc_signed &v)
145912854Sgabeblack@google.com{
146012854Sgabeblack@google.com    if (u == 0) // case 1
146112854Sgabeblack@google.com        return sc_signed(v);
146212854Sgabeblack@google.com
146312854Sgabeblack@google.com    CONVERT_LONG(u);
146412854Sgabeblack@google.com
146512854Sgabeblack@google.com    if (v.sgn == SC_ZERO) // case 2
146612854Sgabeblack@google.com        return sc_signed(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud, false);
146712854Sgabeblack@google.com
146812854Sgabeblack@google.com    // cases 3 and 4
146912854Sgabeblack@google.com    return add_signed_friend(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud,
147012854Sgabeblack@google.com                             v.sgn, v.nbits, v.ndigits, v.digit);
147112854Sgabeblack@google.com}
147212854Sgabeblack@google.com
147312854Sgabeblack@google.com
147412854Sgabeblack@google.comsc_signed
147512854Sgabeblack@google.comoperator + (const sc_unsigned &u, long v)
147612854Sgabeblack@google.com{
147712854Sgabeblack@google.com    if (v == 0) // case 2
147812854Sgabeblack@google.com        return sc_signed(u);
147912854Sgabeblack@google.com
148012854Sgabeblack@google.com    CONVERT_LONG(v);
148112854Sgabeblack@google.com
148212854Sgabeblack@google.com    if (u.sgn == SC_ZERO) // case 1
148312854Sgabeblack@google.com        return sc_signed(vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd, false);
148412854Sgabeblack@google.com
148512854Sgabeblack@google.com    // cases 3 and 4
148612854Sgabeblack@google.com    return add_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit,
148712854Sgabeblack@google.com                             vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd);
148812854Sgabeblack@google.com}
148912854Sgabeblack@google.com
149012854Sgabeblack@google.com
149112854Sgabeblack@google.comsc_signed
149212854Sgabeblack@google.comoperator + (long u, const sc_unsigned &v)
149312854Sgabeblack@google.com{
149412854Sgabeblack@google.com    if (u == 0) // case 1
149512854Sgabeblack@google.com        return sc_signed(v);
149612854Sgabeblack@google.com
149712854Sgabeblack@google.com    CONVERT_LONG(u);
149812854Sgabeblack@google.com
149912854Sgabeblack@google.com    if (v.sgn == SC_ZERO) // case 2
150012854Sgabeblack@google.com        return sc_signed(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud, false);
150112854Sgabeblack@google.com
150212854Sgabeblack@google.com    // cases 3 and 4
150312854Sgabeblack@google.com    return add_signed_friend(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud,
150412854Sgabeblack@google.com                             v.sgn, v.nbits, v.ndigits, v.digit);
150512854Sgabeblack@google.com}
150612854Sgabeblack@google.com
150712854Sgabeblack@google.com
150812854Sgabeblack@google.comsc_signed
150912854Sgabeblack@google.comoperator + (const sc_signed &u, unsigned long v)
151012854Sgabeblack@google.com{
151112854Sgabeblack@google.com    if (v == 0) // case 2
151212854Sgabeblack@google.com        return sc_signed(u);
151312854Sgabeblack@google.com
151412854Sgabeblack@google.com    CONVERT_LONG(v);
151512854Sgabeblack@google.com
151612854Sgabeblack@google.com    if (u.sgn == SC_ZERO) // case 1
151712854Sgabeblack@google.com        return sc_signed(vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd, false);
151812854Sgabeblack@google.com
151912854Sgabeblack@google.com    // cases 3 and 4
152012854Sgabeblack@google.com    return add_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit,
152112854Sgabeblack@google.com                             vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd);
152212854Sgabeblack@google.com}
152312854Sgabeblack@google.com
152412854Sgabeblack@google.com
152512854Sgabeblack@google.comsc_signed
152612854Sgabeblack@google.comoperator + (unsigned long u, const sc_signed &v)
152712854Sgabeblack@google.com{
152812854Sgabeblack@google.com    if (u == 0) // case 1
152912854Sgabeblack@google.com        return sc_signed(v);
153012854Sgabeblack@google.com
153112854Sgabeblack@google.com    CONVERT_LONG(u);
153212854Sgabeblack@google.com
153312854Sgabeblack@google.com    if (v.sgn == SC_ZERO)  // case 2
153412854Sgabeblack@google.com        return sc_signed(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud, false);
153512854Sgabeblack@google.com
153612854Sgabeblack@google.com    // cases 3 and 4
153712854Sgabeblack@google.com    return add_signed_friend(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud,
153812854Sgabeblack@google.com                             v.sgn, v.nbits, v.ndigits, v.digit);
153912854Sgabeblack@google.com}
154012854Sgabeblack@google.com
154112854Sgabeblack@google.com// The rest of the operators in this section are included from
154212854Sgabeblack@google.com// sc_nbcommon.cpp.
154312854Sgabeblack@google.com
154412854Sgabeblack@google.com// ----------------------------------------------------------------------------
154512854Sgabeblack@google.com//  SECTION: MINUS operators: -, -=, --
154612854Sgabeblack@google.com// ----------------------------------------------------------------------------
154712854Sgabeblack@google.com
154812854Sgabeblack@google.com// Cases to consider when computing u + v:
154912854Sgabeblack@google.com// 1. u - 0 = u
155012854Sgabeblack@google.com// 2. 0 - v = -v
155112854Sgabeblack@google.com// 3. if sgn(u) != sgn(v)
155212854Sgabeblack@google.com//    3.1 u - (-v) = u + v = sgn(u) * (u + v)
155312854Sgabeblack@google.com//    3.2 (-u) - v = -(u + v) ==> sgn(u) * (u + v)
155412854Sgabeblack@google.com// 4. if sgn(u) == sgn(v)
155512854Sgabeblack@google.com//    4.1 u - v = +(u - v) = sgn(u) * (u - v)
155612854Sgabeblack@google.com//    4.2 (-u) - (-v) = -(u - v) = sgn(u) * (u - v)
155712854Sgabeblack@google.com//
155812854Sgabeblack@google.com// Specialization of above cases for computing --u or u--:
155912854Sgabeblack@google.com// 1. 0 - 1 = -1
156012854Sgabeblack@google.com// 3. (-u) - 1 = -(u + 1) = sgn(u) * (u + 1)
156112854Sgabeblack@google.com// 4. u - 1 = u - 1 = sgn(u) * (u - 1)
156212854Sgabeblack@google.com
156312854Sgabeblack@google.comsc_signed
156412854Sgabeblack@google.comoperator - (const sc_unsigned &u, const sc_unsigned &v)
156512854Sgabeblack@google.com{
156612854Sgabeblack@google.com    if (v.sgn == SC_ZERO)  // case 1
156712854Sgabeblack@google.com        return sc_signed(u);
156812854Sgabeblack@google.com
156912854Sgabeblack@google.com    if (u.sgn == SC_ZERO) // case 2
157012854Sgabeblack@google.com        return sc_signed(v, -v.sgn);
157112854Sgabeblack@google.com
157212854Sgabeblack@google.com    // cases 3 and 4
157312854Sgabeblack@google.com    return add_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit,
157412854Sgabeblack@google.com                             -v.sgn, v.nbits, v.ndigits, v.digit);
157512854Sgabeblack@google.com}
157612854Sgabeblack@google.com
157712854Sgabeblack@google.com
157812854Sgabeblack@google.comsc_signed
157912854Sgabeblack@google.comoperator - (const sc_unsigned &u, const sc_signed &v)
158012854Sgabeblack@google.com{
158112854Sgabeblack@google.com    if (v.sgn == SC_ZERO) // case 1
158212854Sgabeblack@google.com        return sc_signed(u);
158312854Sgabeblack@google.com
158412854Sgabeblack@google.com    if (u.sgn == SC_ZERO) // case 2
158512854Sgabeblack@google.com        return sc_signed(v, -v.sgn);
158612854Sgabeblack@google.com
158712854Sgabeblack@google.com    // cases 3 and 4
158812854Sgabeblack@google.com    return add_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit,
158912854Sgabeblack@google.com                             -v.sgn, v.nbits, v.ndigits, v.digit);
159012854Sgabeblack@google.com}
159112854Sgabeblack@google.com
159212854Sgabeblack@google.com
159312854Sgabeblack@google.comsc_signed
159412854Sgabeblack@google.comoperator - (const sc_signed &u, const sc_unsigned &v)
159512854Sgabeblack@google.com{
159612854Sgabeblack@google.com    if (v.sgn == SC_ZERO) // case 1
159712854Sgabeblack@google.com        return sc_signed(u);
159812854Sgabeblack@google.com
159912854Sgabeblack@google.com    if (u.sgn == SC_ZERO) // case 2
160012854Sgabeblack@google.com        return sc_signed(v, -v.sgn);
160112854Sgabeblack@google.com
160212854Sgabeblack@google.com    // cases 3 and 4
160312854Sgabeblack@google.com    return add_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit,
160412854Sgabeblack@google.com                             -v.sgn, v.nbits, v.ndigits, v.digit);
160512854Sgabeblack@google.com}
160612854Sgabeblack@google.com
160712854Sgabeblack@google.com
160812854Sgabeblack@google.comsc_signed
160912854Sgabeblack@google.comoperator - (const sc_signed &u, const sc_signed &v)
161012854Sgabeblack@google.com{
161112854Sgabeblack@google.com    if (v.sgn == SC_ZERO)  // case 1
161212854Sgabeblack@google.com        return sc_signed(u);
161312854Sgabeblack@google.com
161412854Sgabeblack@google.com    if (u.sgn == SC_ZERO) // case 2
161512854Sgabeblack@google.com        return sc_signed(v, -v.sgn);
161612854Sgabeblack@google.com
161712854Sgabeblack@google.com    // cases 3 and 4
161812854Sgabeblack@google.com    return add_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit,
161912854Sgabeblack@google.com                             -v.sgn, v.nbits, v.ndigits, v.digit);
162012854Sgabeblack@google.com}
162112854Sgabeblack@google.com
162212854Sgabeblack@google.com
162312854Sgabeblack@google.comsc_signed
162412854Sgabeblack@google.comoperator - (const sc_signed &u, int64 v)
162512854Sgabeblack@google.com{
162612854Sgabeblack@google.com    if (v == 0) // case 1
162712854Sgabeblack@google.com        return sc_signed(u);
162812854Sgabeblack@google.com
162912854Sgabeblack@google.com    CONVERT_INT64(v);
163012854Sgabeblack@google.com
163112854Sgabeblack@google.com    if (u.sgn == SC_ZERO) // case 2
163212854Sgabeblack@google.com        return sc_signed(-vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd, false);
163312854Sgabeblack@google.com
163412854Sgabeblack@google.com    // cases 3 and 4
163512854Sgabeblack@google.com    return add_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit,
163612854Sgabeblack@google.com                             -vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd);
163712854Sgabeblack@google.com}
163812854Sgabeblack@google.com
163912854Sgabeblack@google.com
164012854Sgabeblack@google.comsc_signed
164112854Sgabeblack@google.comoperator - (int64 u, const sc_signed &v)
164212854Sgabeblack@google.com{
164312854Sgabeblack@google.com    if (u == 0) // case 1
164412854Sgabeblack@google.com        return sc_signed(v, -v.sgn);
164512854Sgabeblack@google.com
164612854Sgabeblack@google.com    CONVERT_INT64(u);
164712854Sgabeblack@google.com
164812854Sgabeblack@google.com    if (v.sgn == SC_ZERO) // case 2
164912854Sgabeblack@google.com        return sc_signed(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud, false);
165012854Sgabeblack@google.com
165112854Sgabeblack@google.com    // cases 3 and 4
165212854Sgabeblack@google.com    return add_signed_friend(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud,
165312854Sgabeblack@google.com                             -v.sgn, v.nbits, v.ndigits, v.digit);
165412854Sgabeblack@google.com}
165512854Sgabeblack@google.com
165612854Sgabeblack@google.com
165712854Sgabeblack@google.comsc_signed
165812854Sgabeblack@google.comoperator - (const sc_unsigned &u, int64 v)
165912854Sgabeblack@google.com{
166012854Sgabeblack@google.com    if (v == 0) // case 1
166112854Sgabeblack@google.com        return sc_signed(u);
166212854Sgabeblack@google.com
166312854Sgabeblack@google.com    CONVERT_INT64(v);
166412854Sgabeblack@google.com
166512854Sgabeblack@google.com    if (u.sgn == SC_ZERO) // case 2
166612854Sgabeblack@google.com        return sc_signed(-vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd, false);
166712854Sgabeblack@google.com
166812854Sgabeblack@google.com    // cases 3 and 4
166912854Sgabeblack@google.com    return add_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit,
167012854Sgabeblack@google.com                             -vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd);
167112854Sgabeblack@google.com}
167212854Sgabeblack@google.com
167312854Sgabeblack@google.com
167412854Sgabeblack@google.comsc_signed
167512854Sgabeblack@google.comoperator - (int64 u, const sc_unsigned &v)
167612854Sgabeblack@google.com{
167712854Sgabeblack@google.com    if (u == 0) // case 1
167812854Sgabeblack@google.com        return sc_signed(v, -v.sgn);
167912854Sgabeblack@google.com
168012854Sgabeblack@google.com    CONVERT_INT64(u);
168112854Sgabeblack@google.com
168212854Sgabeblack@google.com    if (v.sgn == SC_ZERO) // case 2
168312854Sgabeblack@google.com        return sc_signed(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud, false);
168412854Sgabeblack@google.com
168512854Sgabeblack@google.com    // cases 3 and 4
168612854Sgabeblack@google.com    return add_signed_friend(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud,
168712854Sgabeblack@google.com                             -v.sgn, v.nbits, v.ndigits, v.digit);
168812854Sgabeblack@google.com}
168912854Sgabeblack@google.com
169012854Sgabeblack@google.com
169112854Sgabeblack@google.comsc_signed
169212854Sgabeblack@google.comoperator - (const sc_signed &u, uint64 v)
169312854Sgabeblack@google.com{
169412854Sgabeblack@google.com    if (v == 0) // case 1
169512854Sgabeblack@google.com        return sc_signed(u);
169612854Sgabeblack@google.com
169712854Sgabeblack@google.com    CONVERT_INT64(v);
169812854Sgabeblack@google.com
169912854Sgabeblack@google.com    if (u.sgn == SC_ZERO) // case 2
170012854Sgabeblack@google.com        return sc_signed(-vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd, false);
170112854Sgabeblack@google.com
170212854Sgabeblack@google.com    // cases 3 and 4
170312854Sgabeblack@google.com
170412854Sgabeblack@google.com    return add_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit,
170512854Sgabeblack@google.com                             -vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd);
170612854Sgabeblack@google.com}
170712854Sgabeblack@google.com
170812854Sgabeblack@google.com
170912854Sgabeblack@google.comsc_signed
171012854Sgabeblack@google.comoperator - (uint64 u, const sc_signed &v)
171112854Sgabeblack@google.com{
171212854Sgabeblack@google.com    if (u == 0) // case 1
171312854Sgabeblack@google.com        return sc_signed(v, -v.sgn);
171412854Sgabeblack@google.com
171512854Sgabeblack@google.com    CONVERT_INT64(u);
171612854Sgabeblack@google.com
171712854Sgabeblack@google.com    if (v.sgn == SC_ZERO) // case 2
171812854Sgabeblack@google.com        return sc_signed(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud, false);
171912854Sgabeblack@google.com
172012854Sgabeblack@google.com    // cases 3 and 4
172112854Sgabeblack@google.com    return add_signed_friend(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud,
172212854Sgabeblack@google.com                             -v.sgn, v.nbits, v.ndigits, v.digit);
172312854Sgabeblack@google.com}
172412854Sgabeblack@google.com
172512854Sgabeblack@google.com
172612854Sgabeblack@google.comsc_signed
172712854Sgabeblack@google.comoperator - (const sc_unsigned &u, uint64 v)
172812854Sgabeblack@google.com{
172912854Sgabeblack@google.com    if (v == 0) // case 1
173012854Sgabeblack@google.com        return sc_signed(u);
173112854Sgabeblack@google.com
173212854Sgabeblack@google.com    CONVERT_INT64(v);
173312854Sgabeblack@google.com
173412854Sgabeblack@google.com    if (u.sgn == SC_ZERO) // case 2
173512854Sgabeblack@google.com        return sc_signed(-vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd, false);
173612854Sgabeblack@google.com
173712854Sgabeblack@google.com    // cases 3 and 4
173812854Sgabeblack@google.com    return add_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit,
173912854Sgabeblack@google.com                             -vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd);
174012854Sgabeblack@google.com}
174112854Sgabeblack@google.com
174212854Sgabeblack@google.com
174312854Sgabeblack@google.comsc_signed
174412854Sgabeblack@google.comoperator - (uint64 u, const sc_unsigned &v)
174512854Sgabeblack@google.com{
174612854Sgabeblack@google.com    if (u == 0) // case 1
174712854Sgabeblack@google.com        return sc_signed(v, -v.sgn);
174812854Sgabeblack@google.com
174912854Sgabeblack@google.com    CONVERT_INT64(u);
175012854Sgabeblack@google.com
175112854Sgabeblack@google.com    if (v.sgn == SC_ZERO) // case 2
175212854Sgabeblack@google.com        return sc_signed(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud, false);
175312854Sgabeblack@google.com
175412854Sgabeblack@google.com    // cases 3 and 4
175512854Sgabeblack@google.com    return add_signed_friend(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud,
175612854Sgabeblack@google.com                             -v.sgn, v.nbits, v.ndigits, v.digit);
175712854Sgabeblack@google.com}
175812854Sgabeblack@google.com
175912854Sgabeblack@google.com
176012854Sgabeblack@google.comsc_signed
176112854Sgabeblack@google.comoperator - (const sc_signed &u, long v)
176212854Sgabeblack@google.com{
176312854Sgabeblack@google.com    if (v == 0) // case 1
176412854Sgabeblack@google.com        return sc_signed(u);
176512854Sgabeblack@google.com
176612854Sgabeblack@google.com    CONVERT_LONG(v);
176712854Sgabeblack@google.com
176812854Sgabeblack@google.com    if (u.sgn == SC_ZERO) // case 2
176912854Sgabeblack@google.com        return sc_signed(-vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd, false);
177012854Sgabeblack@google.com
177112854Sgabeblack@google.com    // cases 3 and 4
177212854Sgabeblack@google.com    return add_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit,
177312854Sgabeblack@google.com                             -vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd);
177412854Sgabeblack@google.com}
177512854Sgabeblack@google.com
177612854Sgabeblack@google.com
177712854Sgabeblack@google.comsc_signed
177812854Sgabeblack@google.comoperator - (long u, const sc_signed &v)
177912854Sgabeblack@google.com{
178012854Sgabeblack@google.com    if (u == 0) // case 1
178112854Sgabeblack@google.com        return sc_signed(v, -v.sgn);
178212854Sgabeblack@google.com
178312854Sgabeblack@google.com    CONVERT_LONG(u);
178412854Sgabeblack@google.com
178512854Sgabeblack@google.com    if (v.sgn == SC_ZERO) // case 2
178612854Sgabeblack@google.com        return sc_signed(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud, false);
178712854Sgabeblack@google.com
178812854Sgabeblack@google.com    // cases 3 and 4
178912854Sgabeblack@google.com    return add_signed_friend(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud,
179012854Sgabeblack@google.com                             -v.sgn, v.nbits, v.ndigits, v.digit);
179112854Sgabeblack@google.com}
179212854Sgabeblack@google.com
179312854Sgabeblack@google.com
179412854Sgabeblack@google.comsc_signed
179512854Sgabeblack@google.comoperator - (const sc_unsigned &u, long v)
179612854Sgabeblack@google.com{
179712854Sgabeblack@google.com    if (v == 0) // case 1
179812854Sgabeblack@google.com        return sc_signed(u);
179912854Sgabeblack@google.com
180012854Sgabeblack@google.com    CONVERT_LONG(v);
180112854Sgabeblack@google.com
180212854Sgabeblack@google.com    if (u.sgn == SC_ZERO) // case 2
180312854Sgabeblack@google.com        return sc_signed(-vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd, false);
180412854Sgabeblack@google.com
180512854Sgabeblack@google.com    // cases 3 and 4
180612854Sgabeblack@google.com    return add_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit,
180712854Sgabeblack@google.com                             -vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd);
180812854Sgabeblack@google.com}
180912854Sgabeblack@google.com
181012854Sgabeblack@google.com
181112854Sgabeblack@google.comsc_signed
181212854Sgabeblack@google.comoperator - (long u, const sc_unsigned &v)
181312854Sgabeblack@google.com{
181412854Sgabeblack@google.com    if (u == 0) // case 1
181512854Sgabeblack@google.com        return sc_signed(v, -v.sgn);
181612854Sgabeblack@google.com
181712854Sgabeblack@google.com    CONVERT_LONG(u);
181812854Sgabeblack@google.com
181912854Sgabeblack@google.com    if (v.sgn == SC_ZERO) // case 2
182012854Sgabeblack@google.com        return sc_signed(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud, false);
182112854Sgabeblack@google.com
182212854Sgabeblack@google.com    // cases 3 and 4
182312854Sgabeblack@google.com    return add_signed_friend(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud,
182412854Sgabeblack@google.com                             -v.sgn, v.nbits, v.ndigits, v.digit);
182512854Sgabeblack@google.com}
182612854Sgabeblack@google.com
182712854Sgabeblack@google.com
182812854Sgabeblack@google.comsc_signed
182912854Sgabeblack@google.comoperator - (const sc_signed &u, unsigned long v)
183012854Sgabeblack@google.com{
183112854Sgabeblack@google.com    if (v == 0) // case 1
183212854Sgabeblack@google.com        return sc_signed(u);
183312854Sgabeblack@google.com
183412854Sgabeblack@google.com    CONVERT_LONG(v);
183512854Sgabeblack@google.com
183612854Sgabeblack@google.com    if (u.sgn == SC_ZERO) // case 2
183712854Sgabeblack@google.com        return sc_signed(-vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd, false);
183812854Sgabeblack@google.com
183912854Sgabeblack@google.com    // cases 3 and 4
184012854Sgabeblack@google.com    return add_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit,
184112854Sgabeblack@google.com                             -vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd);
184212854Sgabeblack@google.com}
184312854Sgabeblack@google.com
184412854Sgabeblack@google.com
184512854Sgabeblack@google.comsc_signed
184612854Sgabeblack@google.comoperator - (unsigned long u, const sc_signed &v)
184712854Sgabeblack@google.com{
184812854Sgabeblack@google.com    if (u == 0) // case 1
184912854Sgabeblack@google.com        return sc_signed(v, -v.sgn);
185012854Sgabeblack@google.com
185112854Sgabeblack@google.com    CONVERT_LONG(u);
185212854Sgabeblack@google.com
185312854Sgabeblack@google.com    if (v.sgn == SC_ZERO) // case 2
185412854Sgabeblack@google.com        return sc_signed(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud, false);
185512854Sgabeblack@google.com
185612854Sgabeblack@google.com    // cases 3 and 4
185712854Sgabeblack@google.com    return add_signed_friend(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud,
185812854Sgabeblack@google.com                             -v.sgn, v.nbits, v.ndigits, v.digit);
185912854Sgabeblack@google.com}
186012854Sgabeblack@google.com
186112854Sgabeblack@google.com
186212854Sgabeblack@google.comsc_signed
186312854Sgabeblack@google.comoperator - (const sc_unsigned &u, unsigned long v)
186412854Sgabeblack@google.com{
186512854Sgabeblack@google.com    if (v == 0) // case 1
186612854Sgabeblack@google.com        return sc_signed(u);
186712854Sgabeblack@google.com
186812854Sgabeblack@google.com    CONVERT_LONG(v);
186912854Sgabeblack@google.com
187012854Sgabeblack@google.com    if (u.sgn == SC_ZERO) // case 2
187112854Sgabeblack@google.com        return sc_signed(-vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd, false);
187212854Sgabeblack@google.com
187312854Sgabeblack@google.com    // cases 3 and 4
187412854Sgabeblack@google.com    return add_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit,
187512854Sgabeblack@google.com                             -vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd);
187612854Sgabeblack@google.com}
187712854Sgabeblack@google.com
187812854Sgabeblack@google.com
187912854Sgabeblack@google.comsc_signed
188012854Sgabeblack@google.comoperator - (unsigned long u, const sc_unsigned &v)
188112854Sgabeblack@google.com{
188212854Sgabeblack@google.com    if (u == 0) // case 1
188312854Sgabeblack@google.com        return sc_signed(v, -v.sgn);
188412854Sgabeblack@google.com
188512854Sgabeblack@google.com    CONVERT_LONG(u);
188612854Sgabeblack@google.com
188712854Sgabeblack@google.com    if (v.sgn == SC_ZERO) // case 2
188812854Sgabeblack@google.com        return sc_signed(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud, false);
188912854Sgabeblack@google.com
189012854Sgabeblack@google.com    // cases 3 and 4
189112854Sgabeblack@google.com    return add_signed_friend(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud,
189212854Sgabeblack@google.com                             -v.sgn, v.nbits, v.ndigits, v.digit);
189312854Sgabeblack@google.com}
189412854Sgabeblack@google.com
189512854Sgabeblack@google.com// The rest of the operators in this section are included from
189612854Sgabeblack@google.com// sc_nbcommon.cpp.
189712854Sgabeblack@google.com
189812854Sgabeblack@google.com
189912854Sgabeblack@google.com// ----------------------------------------------------------------------------
190012854Sgabeblack@google.com//  SECTION: MULTIPLICATION operators: *, *=
190112854Sgabeblack@google.com// ----------------------------------------------------------------------------
190212854Sgabeblack@google.com
190312854Sgabeblack@google.com// Cases to consider when computing u * v:
190412854Sgabeblack@google.com// 1. u * 0 = 0 * v = 0
190512854Sgabeblack@google.com// 2. 1 * v = v and -1 * v = -v
190612854Sgabeblack@google.com// 3. u * 1 = u and u * -1 = -u
190712854Sgabeblack@google.com// 4. u * v = u * v
190812854Sgabeblack@google.com
190912854Sgabeblack@google.comsc_signed
191012854Sgabeblack@google.comoperator * (const sc_unsigned &u, const sc_signed &v)
191112854Sgabeblack@google.com{
191212854Sgabeblack@google.com    small_type s = mul_signs(u.sgn, v.sgn);
191312854Sgabeblack@google.com
191412854Sgabeblack@google.com    if (s == SC_ZERO) // case 1
191512854Sgabeblack@google.com        return sc_signed();
191612854Sgabeblack@google.com
191712854Sgabeblack@google.com    // cases 2-4
191812854Sgabeblack@google.com    return mul_signed_friend(s, u.nbits, u.ndigits, u.digit,
191912854Sgabeblack@google.com                             v.nbits, v.ndigits, v.digit);
192012854Sgabeblack@google.com}
192112854Sgabeblack@google.com
192212854Sgabeblack@google.com
192312854Sgabeblack@google.comsc_signed
192412854Sgabeblack@google.comoperator * (const sc_signed &u, const sc_unsigned &v)
192512854Sgabeblack@google.com{
192612854Sgabeblack@google.com    small_type s = mul_signs(u.sgn, v.sgn);
192712854Sgabeblack@google.com
192812854Sgabeblack@google.com    if (s == SC_ZERO) // case 1
192912854Sgabeblack@google.com        return sc_signed();
193012854Sgabeblack@google.com
193112854Sgabeblack@google.com    // cases 2-4
193212854Sgabeblack@google.com    return mul_signed_friend(s, u.nbits, u.ndigits, u.digit,
193312854Sgabeblack@google.com                             v.nbits, v.ndigits, v.digit);
193412854Sgabeblack@google.com}
193512854Sgabeblack@google.com
193612854Sgabeblack@google.com
193712854Sgabeblack@google.comsc_signed
193812854Sgabeblack@google.comoperator * (const sc_signed &u, const sc_signed &v)
193912854Sgabeblack@google.com{
194012854Sgabeblack@google.com    small_type s = mul_signs(u.sgn, v.sgn);
194112854Sgabeblack@google.com
194212854Sgabeblack@google.com    if (s == SC_ZERO) // case 1
194312854Sgabeblack@google.com        return sc_signed();
194412854Sgabeblack@google.com
194512854Sgabeblack@google.com    // cases 2-4
194612854Sgabeblack@google.com    return mul_signed_friend(s, u.nbits, u.ndigits, u.digit,
194712854Sgabeblack@google.com                             v.nbits, v.ndigits, v.digit);
194812854Sgabeblack@google.com}
194912854Sgabeblack@google.com
195012854Sgabeblack@google.com
195112854Sgabeblack@google.comsc_signed
195212854Sgabeblack@google.comoperator * (const sc_signed &u, int64 v)
195312854Sgabeblack@google.com{
195412854Sgabeblack@google.com    small_type s = mul_signs(u.sgn, get_sign(v));
195512854Sgabeblack@google.com
195612854Sgabeblack@google.com    if (s == SC_ZERO) // case 1
195712854Sgabeblack@google.com        return sc_signed();
195812854Sgabeblack@google.com
195912854Sgabeblack@google.com    CONVERT_INT64_2(v);
196012854Sgabeblack@google.com
196112854Sgabeblack@google.com    // cases 2-4
196212854Sgabeblack@google.com    return mul_signed_friend(s, u.nbits, u.ndigits, u.digit,
196312854Sgabeblack@google.com                             BITS_PER_UINT64, DIGITS_PER_UINT64, vd);
196412854Sgabeblack@google.com}
196512854Sgabeblack@google.com
196612854Sgabeblack@google.com
196712854Sgabeblack@google.comsc_signed
196812854Sgabeblack@google.comoperator * (int64 u, const sc_signed &v)
196912854Sgabeblack@google.com{
197012854Sgabeblack@google.com    small_type s = mul_signs(v.sgn, get_sign(u));
197112854Sgabeblack@google.com
197212854Sgabeblack@google.com    if (s == SC_ZERO) // case 1
197312854Sgabeblack@google.com        return sc_signed();
197412854Sgabeblack@google.com
197512854Sgabeblack@google.com    CONVERT_INT64_2(u);
197612854Sgabeblack@google.com
197712854Sgabeblack@google.com    // cases 2-4
197812854Sgabeblack@google.com    return mul_signed_friend(s, BITS_PER_UINT64, DIGITS_PER_UINT64, ud,
197912854Sgabeblack@google.com                             v.nbits, v.ndigits, v.digit);
198012854Sgabeblack@google.com}
198112854Sgabeblack@google.com
198212854Sgabeblack@google.com
198312854Sgabeblack@google.comsc_signed
198412854Sgabeblack@google.comoperator * (const sc_unsigned &u, int64 v)
198512854Sgabeblack@google.com{
198612854Sgabeblack@google.com    small_type s = mul_signs(u.sgn, get_sign(v));
198712854Sgabeblack@google.com
198812854Sgabeblack@google.com    if (s == SC_ZERO) // case 1
198912854Sgabeblack@google.com        return sc_signed();
199012854Sgabeblack@google.com
199112854Sgabeblack@google.com    CONVERT_INT64_2(v);
199212854Sgabeblack@google.com
199312854Sgabeblack@google.com    // cases 2-4
199412854Sgabeblack@google.com    return mul_signed_friend(s, u.nbits, u.ndigits, u.digit,
199512854Sgabeblack@google.com                             BITS_PER_UINT64, DIGITS_PER_UINT64, vd);
199612854Sgabeblack@google.com}
199712854Sgabeblack@google.com
199812854Sgabeblack@google.com
199912854Sgabeblack@google.comsc_signed
200012854Sgabeblack@google.comoperator * (int64 u, const sc_unsigned &v)
200112854Sgabeblack@google.com{
200212854Sgabeblack@google.com    small_type s = mul_signs(v.sgn, get_sign(u));
200312854Sgabeblack@google.com
200412854Sgabeblack@google.com    if (s == SC_ZERO) // case 1
200512854Sgabeblack@google.com        return sc_signed();
200612854Sgabeblack@google.com
200712854Sgabeblack@google.com    CONVERT_INT64_2(u);
200812854Sgabeblack@google.com
200912854Sgabeblack@google.com    // cases 2-4
201012854Sgabeblack@google.com    return mul_signed_friend(s, BITS_PER_UINT64, DIGITS_PER_UINT64, ud,
201112854Sgabeblack@google.com                             v.nbits, v.ndigits, v.digit);
201212854Sgabeblack@google.com}
201312854Sgabeblack@google.com
201412854Sgabeblack@google.com
201512854Sgabeblack@google.comsc_signed
201612854Sgabeblack@google.comoperator * (const sc_signed &u, uint64 v)
201712854Sgabeblack@google.com{
201812854Sgabeblack@google.com    small_type s = mul_signs(u.sgn, get_sign(v));
201912854Sgabeblack@google.com
202012854Sgabeblack@google.com    if (s == SC_ZERO) // case 1
202112854Sgabeblack@google.com        return sc_signed();
202212854Sgabeblack@google.com
202312854Sgabeblack@google.com    CONVERT_INT64_2(v);
202412854Sgabeblack@google.com
202512854Sgabeblack@google.com    // cases 2-4
202612854Sgabeblack@google.com    return mul_signed_friend(s, u.nbits, u.ndigits, u.digit,
202712854Sgabeblack@google.com                             BITS_PER_UINT64, DIGITS_PER_UINT64, vd);
202812854Sgabeblack@google.com}
202912854Sgabeblack@google.com
203012854Sgabeblack@google.com
203112854Sgabeblack@google.comsc_signed
203212854Sgabeblack@google.comoperator * (uint64 u, const sc_signed &v)
203312854Sgabeblack@google.com{
203412854Sgabeblack@google.com    small_type s = mul_signs(v.sgn, get_sign(u));
203512854Sgabeblack@google.com
203612854Sgabeblack@google.com    if (s == SC_ZERO) // case 1
203712854Sgabeblack@google.com        return sc_signed();
203812854Sgabeblack@google.com
203912854Sgabeblack@google.com    CONVERT_INT64_2(u);
204012854Sgabeblack@google.com
204112854Sgabeblack@google.com    // cases 2-4
204212854Sgabeblack@google.com    return mul_signed_friend(s, BITS_PER_UINT64, DIGITS_PER_UINT64, ud,
204312854Sgabeblack@google.com                             v.nbits, v.ndigits, v.digit);
204412854Sgabeblack@google.com}
204512854Sgabeblack@google.com
204612854Sgabeblack@google.com
204712854Sgabeblack@google.comsc_signed
204812854Sgabeblack@google.comoperator * (const sc_signed &u, long v)
204912854Sgabeblack@google.com{
205012854Sgabeblack@google.com    small_type s = mul_signs(u.sgn, get_sign(v));
205112854Sgabeblack@google.com
205212854Sgabeblack@google.com    if (s == SC_ZERO) // case 1
205312854Sgabeblack@google.com        return sc_signed();
205412854Sgabeblack@google.com
205512854Sgabeblack@google.com    CONVERT_LONG_2(v);
205612854Sgabeblack@google.com
205712854Sgabeblack@google.com    // cases 2-4
205812854Sgabeblack@google.com    return mul_signed_friend(s, u.nbits, u.ndigits, u.digit,
205912854Sgabeblack@google.com                             BITS_PER_ULONG, DIGITS_PER_ULONG, vd);
206012854Sgabeblack@google.com}
206112854Sgabeblack@google.com
206212854Sgabeblack@google.com
206312854Sgabeblack@google.comsc_signed
206412854Sgabeblack@google.comoperator * (long u, const sc_signed &v)
206512854Sgabeblack@google.com{
206612854Sgabeblack@google.com    small_type s = mul_signs(v.sgn, get_sign(u));
206712854Sgabeblack@google.com
206812854Sgabeblack@google.com    if (s == SC_ZERO) // case 1
206912854Sgabeblack@google.com        return sc_signed();
207012854Sgabeblack@google.com
207112854Sgabeblack@google.com    CONVERT_LONG_2(u);
207212854Sgabeblack@google.com
207312854Sgabeblack@google.com    // cases 2-4
207412854Sgabeblack@google.com    return mul_signed_friend(s, BITS_PER_ULONG, DIGITS_PER_ULONG, ud,
207512854Sgabeblack@google.com                             v.nbits, v.ndigits, v.digit);
207612854Sgabeblack@google.com}
207712854Sgabeblack@google.com
207812854Sgabeblack@google.com
207912854Sgabeblack@google.comsc_signed
208012854Sgabeblack@google.comoperator * (const sc_unsigned &u, long v)
208112854Sgabeblack@google.com{
208212854Sgabeblack@google.com    small_type s = mul_signs(u.sgn, get_sign(v));
208312854Sgabeblack@google.com
208412854Sgabeblack@google.com    if (s == SC_ZERO) // case 1
208512854Sgabeblack@google.com        return sc_signed();
208612854Sgabeblack@google.com
208712854Sgabeblack@google.com    CONVERT_LONG_2(v);
208812854Sgabeblack@google.com
208912854Sgabeblack@google.com    // cases 2-4
209012854Sgabeblack@google.com    return mul_signed_friend(s, u.nbits, u.ndigits, u.digit,
209112854Sgabeblack@google.com                             BITS_PER_ULONG, DIGITS_PER_ULONG, vd);
209212854Sgabeblack@google.com}
209312854Sgabeblack@google.com
209412854Sgabeblack@google.com
209512854Sgabeblack@google.comsc_signed
209612854Sgabeblack@google.comoperator * (long u, const sc_unsigned &v)
209712854Sgabeblack@google.com{
209812854Sgabeblack@google.com    small_type s = mul_signs(v.sgn, get_sign(u));
209912854Sgabeblack@google.com
210012854Sgabeblack@google.com    if (s == SC_ZERO) // case 1
210112854Sgabeblack@google.com        return sc_signed();
210212854Sgabeblack@google.com
210312854Sgabeblack@google.com    CONVERT_LONG_2(u);
210412854Sgabeblack@google.com
210512854Sgabeblack@google.com    // cases 2-4
210612854Sgabeblack@google.com    return mul_signed_friend(s, BITS_PER_ULONG, DIGITS_PER_ULONG, ud,
210712854Sgabeblack@google.com                             v.nbits, v.ndigits, v.digit);
210812854Sgabeblack@google.com}
210912854Sgabeblack@google.com
211012854Sgabeblack@google.com
211112854Sgabeblack@google.comsc_signed
211212854Sgabeblack@google.comoperator * (const sc_signed &u, unsigned long v)
211312854Sgabeblack@google.com{
211412854Sgabeblack@google.com    small_type s = mul_signs(u.sgn, get_sign(v));
211512854Sgabeblack@google.com
211612854Sgabeblack@google.com    if (s == SC_ZERO) // case 1
211712854Sgabeblack@google.com        return sc_signed();
211812854Sgabeblack@google.com
211912854Sgabeblack@google.com    CONVERT_LONG_2(v);
212012854Sgabeblack@google.com
212112854Sgabeblack@google.com    // else cases 2-4
212212854Sgabeblack@google.com    return mul_signed_friend(s, u.nbits, u.ndigits, u.digit,
212312854Sgabeblack@google.com                             BITS_PER_ULONG, DIGITS_PER_ULONG, vd);
212412854Sgabeblack@google.com}
212512854Sgabeblack@google.com
212612854Sgabeblack@google.comsc_signed
212712854Sgabeblack@google.comoperator * (unsigned long u, const sc_signed &v)
212812854Sgabeblack@google.com{
212912854Sgabeblack@google.com    small_type s = mul_signs(v.sgn, get_sign(u));
213012854Sgabeblack@google.com
213112854Sgabeblack@google.com    if (s == SC_ZERO) // case 1
213212854Sgabeblack@google.com        return sc_signed();
213312854Sgabeblack@google.com
213412854Sgabeblack@google.com    CONVERT_LONG_2(u);
213512854Sgabeblack@google.com
213612854Sgabeblack@google.com    // cases 2-4
213712854Sgabeblack@google.com    return mul_signed_friend(s, BITS_PER_ULONG, DIGITS_PER_ULONG, ud,
213812854Sgabeblack@google.com                             v.nbits, v.ndigits, v.digit);
213912854Sgabeblack@google.com}
214012854Sgabeblack@google.com
214112854Sgabeblack@google.com// The rest of the operators in this section are included from
214212854Sgabeblack@google.com// sc_nbcommon.cpp.
214312854Sgabeblack@google.com
214412854Sgabeblack@google.com
214512854Sgabeblack@google.com// ----------------------------------------------------------------------------
214612854Sgabeblack@google.com//  SECTION: DIVISION operators: /, /=
214712854Sgabeblack@google.com// ----------------------------------------------------------------------------
214812854Sgabeblack@google.com
214912854Sgabeblack@google.com// Cases to consider when finding the quotient q = floor(u/v):
215012854Sgabeblack@google.com// Note that u = q * v + r for r < q.
215112854Sgabeblack@google.com// 1. 0 / 0 or u / 0 => error
215212854Sgabeblack@google.com// 2. 0 / v => 0 = 0 * v + 0
215312854Sgabeblack@google.com// 3. u / v & &u = v => u = 1 * u + 0  - u or v can be 1 or -1
215412854Sgabeblack@google.com// 4. u / v & &u < v => u = 0 * v + u  - u can be 1 or -1
215512854Sgabeblack@google.com// 5. u / v & &u > v => u = q * v + r  - v can be 1 or -1
215612854Sgabeblack@google.com
215712854Sgabeblack@google.comsc_signed
215812854Sgabeblack@google.comoperator / (const sc_unsigned &u, const sc_signed &v)
215912854Sgabeblack@google.com{
216012854Sgabeblack@google.com    small_type s = mul_signs(u.sgn, v.sgn);
216112854Sgabeblack@google.com
216212854Sgabeblack@google.com    if (s == SC_ZERO) {
216312854Sgabeblack@google.com        div_by_zero(v.sgn); // case 1
216412854Sgabeblack@google.com        return sc_signed(); // case 2
216512854Sgabeblack@google.com    }
216612854Sgabeblack@google.com
216712854Sgabeblack@google.com    // other cases
216812854Sgabeblack@google.com    return div_signed_friend(s, u.nbits, u.ndigits, u.digit,
216912854Sgabeblack@google.com                             v.nbits, v.ndigits, v.digit);
217012854Sgabeblack@google.com}
217112854Sgabeblack@google.com
217212854Sgabeblack@google.com
217312854Sgabeblack@google.comsc_signed
217412854Sgabeblack@google.comoperator / (const sc_signed &u, const sc_unsigned &v)
217512854Sgabeblack@google.com{
217612854Sgabeblack@google.com    small_type s = mul_signs(u.sgn, v.sgn);
217712854Sgabeblack@google.com
217812854Sgabeblack@google.com    if (s == SC_ZERO) {
217912854Sgabeblack@google.com        div_by_zero(v.sgn); // case 1
218012854Sgabeblack@google.com        return sc_signed(); // case 2
218112854Sgabeblack@google.com    }
218212854Sgabeblack@google.com
218312854Sgabeblack@google.com    // other cases
218412854Sgabeblack@google.com    return div_signed_friend(s, u.nbits, u.ndigits, u.digit,
218512854Sgabeblack@google.com                             v.nbits, v.ndigits, v.digit);
218612854Sgabeblack@google.com}
218712854Sgabeblack@google.com
218812854Sgabeblack@google.com
218912854Sgabeblack@google.comsc_signed
219012854Sgabeblack@google.comoperator / (const sc_signed &u, const sc_signed &v)
219112854Sgabeblack@google.com{
219212854Sgabeblack@google.com    small_type s = mul_signs(u.sgn, v.sgn);
219312854Sgabeblack@google.com
219412854Sgabeblack@google.com    if (s == SC_ZERO) {
219512854Sgabeblack@google.com        div_by_zero(v.sgn); // case 1
219612854Sgabeblack@google.com        return sc_signed(); // case 2
219712854Sgabeblack@google.com    }
219812854Sgabeblack@google.com
219912854Sgabeblack@google.com    // other cases
220012854Sgabeblack@google.com    return div_signed_friend(s, u.nbits, u.ndigits, u.digit,
220112854Sgabeblack@google.com                             v.nbits, v.ndigits, v.digit);
220212854Sgabeblack@google.com}
220312854Sgabeblack@google.com
220412854Sgabeblack@google.com
220512854Sgabeblack@google.comsc_signed
220612854Sgabeblack@google.comoperator / (const sc_signed &u, int64 v)
220712854Sgabeblack@google.com{
220812854Sgabeblack@google.com    small_type s = mul_signs(u.sgn, get_sign(v));
220912854Sgabeblack@google.com
221012854Sgabeblack@google.com    if (s == SC_ZERO) {
221112854Sgabeblack@google.com        div_by_zero(v); // case 1
221212854Sgabeblack@google.com        return sc_signed(); // case 2
221312854Sgabeblack@google.com    }
221412854Sgabeblack@google.com
221512854Sgabeblack@google.com    CONVERT_INT64_2(v);
221612854Sgabeblack@google.com
221712854Sgabeblack@google.com    // other cases
221812854Sgabeblack@google.com    return div_signed_friend(s, u.nbits, u.ndigits, u.digit,
221912854Sgabeblack@google.com                             BITS_PER_UINT64, DIGITS_PER_UINT64, vd);
222012854Sgabeblack@google.com}
222112854Sgabeblack@google.com
222212854Sgabeblack@google.com
222312854Sgabeblack@google.comsc_signed
222412854Sgabeblack@google.comoperator / (int64 u, const sc_signed &v)
222512854Sgabeblack@google.com{
222612854Sgabeblack@google.com    small_type s = mul_signs(v.sgn, get_sign(u));
222712854Sgabeblack@google.com
222812854Sgabeblack@google.com    if (s == SC_ZERO) {
222912854Sgabeblack@google.com        div_by_zero(v.sgn); // case 1
223012854Sgabeblack@google.com        return sc_signed(); // case 2
223112854Sgabeblack@google.com    }
223212854Sgabeblack@google.com
223312854Sgabeblack@google.com    CONVERT_INT64_2(u);
223412854Sgabeblack@google.com
223512854Sgabeblack@google.com    // other cases
223612854Sgabeblack@google.com    return div_signed_friend(s, BITS_PER_UINT64, DIGITS_PER_UINT64, ud,
223712854Sgabeblack@google.com                             v.nbits, v.ndigits, v.digit);
223812854Sgabeblack@google.com}
223912854Sgabeblack@google.com
224012854Sgabeblack@google.com
224112854Sgabeblack@google.comsc_signed
224212854Sgabeblack@google.comoperator / (const sc_unsigned &u, int64 v)
224312854Sgabeblack@google.com{
224412854Sgabeblack@google.com    small_type s = mul_signs(u.sgn, get_sign(v));
224512854Sgabeblack@google.com
224612854Sgabeblack@google.com    if (s == SC_ZERO) {
224712854Sgabeblack@google.com        div_by_zero(v); // case 1
224812854Sgabeblack@google.com        return sc_signed(); // case 2
224912854Sgabeblack@google.com    }
225012854Sgabeblack@google.com
225112854Sgabeblack@google.com    CONVERT_INT64_2(v);
225212854Sgabeblack@google.com
225312854Sgabeblack@google.com    // other cases
225412854Sgabeblack@google.com    return div_signed_friend(s, u.nbits, u.ndigits, u.digit,
225512854Sgabeblack@google.com                             BITS_PER_UINT64, DIGITS_PER_UINT64, vd);
225612854Sgabeblack@google.com}
225712854Sgabeblack@google.com
225812854Sgabeblack@google.com
225912854Sgabeblack@google.comsc_signed
226012854Sgabeblack@google.comoperator / (int64 u, const sc_unsigned &v)
226112854Sgabeblack@google.com{
226212854Sgabeblack@google.com    small_type s = mul_signs(v.sgn, get_sign(u));
226312854Sgabeblack@google.com
226412854Sgabeblack@google.com    if (s == SC_ZERO) {
226512854Sgabeblack@google.com        div_by_zero(v.sgn); // case 1
226612854Sgabeblack@google.com        return sc_signed(); // case 2
226712854Sgabeblack@google.com    }
226812854Sgabeblack@google.com
226912854Sgabeblack@google.com    CONVERT_INT64_2(u);
227012854Sgabeblack@google.com
227112854Sgabeblack@google.com    // other cases
227212854Sgabeblack@google.com    return div_signed_friend(s, BITS_PER_UINT64, DIGITS_PER_UINT64, ud,
227312854Sgabeblack@google.com                             v.nbits, v.ndigits, v.digit);
227412854Sgabeblack@google.com}
227512854Sgabeblack@google.com
227612854Sgabeblack@google.com
227712854Sgabeblack@google.comsc_signed
227812854Sgabeblack@google.comoperator / (const sc_signed &u, uint64 v)
227912854Sgabeblack@google.com{
228012854Sgabeblack@google.com    small_type s = mul_signs(u.sgn, get_sign(v));
228112854Sgabeblack@google.com
228212854Sgabeblack@google.com    if (s == SC_ZERO) {
228312854Sgabeblack@google.com        div_by_zero(v); // case 1
228412854Sgabeblack@google.com        return sc_signed(); // case 2
228512854Sgabeblack@google.com    }
228612854Sgabeblack@google.com
228712854Sgabeblack@google.com    CONVERT_INT64_2(v);
228812854Sgabeblack@google.com
228912854Sgabeblack@google.com    // other cases
229012854Sgabeblack@google.com    return div_signed_friend(s, u.nbits, u.ndigits, u.digit,
229112854Sgabeblack@google.com                             BITS_PER_UINT64, DIGITS_PER_UINT64, vd);
229212854Sgabeblack@google.com}
229312854Sgabeblack@google.com
229412854Sgabeblack@google.com
229512854Sgabeblack@google.comsc_signed
229612854Sgabeblack@google.comoperator / (uint64 u, const sc_signed &v)
229712854Sgabeblack@google.com{
229812854Sgabeblack@google.com    small_type s = mul_signs(v.sgn, get_sign(u));
229912854Sgabeblack@google.com
230012854Sgabeblack@google.com    if (s == SC_ZERO) {
230112854Sgabeblack@google.com        div_by_zero(v.sgn); // case 1
230212854Sgabeblack@google.com        return sc_signed(); // case 2
230312854Sgabeblack@google.com
230412854Sgabeblack@google.com    }
230512854Sgabeblack@google.com
230612854Sgabeblack@google.com    CONVERT_INT64_2(u);
230712854Sgabeblack@google.com
230812854Sgabeblack@google.com    // other cases
230912854Sgabeblack@google.com    return div_signed_friend(s, BITS_PER_UINT64, DIGITS_PER_UINT64, ud,
231012854Sgabeblack@google.com                             v.nbits, v.ndigits, v.digit);
231112854Sgabeblack@google.com}
231212854Sgabeblack@google.com
231312854Sgabeblack@google.com
231412854Sgabeblack@google.comsc_signed
231512854Sgabeblack@google.comoperator / (const sc_signed &u, long v)
231612854Sgabeblack@google.com{
231712854Sgabeblack@google.com    small_type s = mul_signs(u.sgn, get_sign(v));
231812854Sgabeblack@google.com
231912854Sgabeblack@google.com    if (s == SC_ZERO) {
232012854Sgabeblack@google.com        div_by_zero(v); // case 1
232112854Sgabeblack@google.com        return sc_signed(); // case 2
232212854Sgabeblack@google.com    }
232312854Sgabeblack@google.com
232412854Sgabeblack@google.com    CONVERT_LONG_2(v);
232512854Sgabeblack@google.com
232612854Sgabeblack@google.com    // other cases
232712854Sgabeblack@google.com    return div_signed_friend(s, u.nbits, u.ndigits, u.digit,
232812854Sgabeblack@google.com                             BITS_PER_ULONG, DIGITS_PER_ULONG, vd);
232912854Sgabeblack@google.com}
233012854Sgabeblack@google.com
233112854Sgabeblack@google.com
233212854Sgabeblack@google.comsc_signed
233312854Sgabeblack@google.comoperator / (long u, const sc_signed &v)
233412854Sgabeblack@google.com{
233512854Sgabeblack@google.com    small_type s = mul_signs(v.sgn, get_sign(u));
233612854Sgabeblack@google.com
233712854Sgabeblack@google.com    if (s == SC_ZERO) {
233812854Sgabeblack@google.com        div_by_zero(v.sgn); // case 1
233912854Sgabeblack@google.com        return sc_signed(); // case 2
234012854Sgabeblack@google.com    }
234112854Sgabeblack@google.com
234212854Sgabeblack@google.com    CONVERT_LONG_2(u);
234312854Sgabeblack@google.com
234412854Sgabeblack@google.com    // other cases
234512854Sgabeblack@google.com    return div_signed_friend(s, BITS_PER_ULONG, DIGITS_PER_ULONG, ud,
234612854Sgabeblack@google.com                             v.nbits, v.ndigits, v.digit);
234712854Sgabeblack@google.com}
234812854Sgabeblack@google.com
234912854Sgabeblack@google.com
235012854Sgabeblack@google.comsc_signed
235112854Sgabeblack@google.comoperator / (const sc_unsigned &u, long v)
235212854Sgabeblack@google.com{
235312854Sgabeblack@google.com    small_type s = mul_signs(u.sgn, get_sign(v));
235412854Sgabeblack@google.com
235512854Sgabeblack@google.com    if (s == SC_ZERO) {
235612854Sgabeblack@google.com        div_by_zero(v); // case 1
235712854Sgabeblack@google.com        return sc_signed(); // case 2
235812854Sgabeblack@google.com    }
235912854Sgabeblack@google.com
236012854Sgabeblack@google.com    CONVERT_LONG_2(v);
236112854Sgabeblack@google.com
236212854Sgabeblack@google.com    // other cases
236312854Sgabeblack@google.com    return div_signed_friend(s, u.nbits, u.ndigits, u.digit,
236412854Sgabeblack@google.com                             BITS_PER_ULONG, DIGITS_PER_ULONG, vd);
236512854Sgabeblack@google.com}
236612854Sgabeblack@google.com
236712854Sgabeblack@google.com
236812854Sgabeblack@google.comsc_signed
236912854Sgabeblack@google.comoperator / (long u, const sc_unsigned &v)
237012854Sgabeblack@google.com{
237112854Sgabeblack@google.com    small_type s = mul_signs(v.sgn, get_sign(u));
237212854Sgabeblack@google.com
237312854Sgabeblack@google.com    if (s == SC_ZERO) {
237412854Sgabeblack@google.com        div_by_zero(v.sgn); // case 1
237512854Sgabeblack@google.com        return sc_signed(); // case 2
237612854Sgabeblack@google.com    }
237712854Sgabeblack@google.com
237812854Sgabeblack@google.com    CONVERT_LONG_2(u);
237912854Sgabeblack@google.com
238012854Sgabeblack@google.com    // other cases
238112854Sgabeblack@google.com    return div_signed_friend(s, BITS_PER_ULONG, DIGITS_PER_ULONG, ud,
238212854Sgabeblack@google.com                             v.nbits, v.ndigits, v.digit);
238312854Sgabeblack@google.com}
238412854Sgabeblack@google.com
238512854Sgabeblack@google.com
238612854Sgabeblack@google.comsc_signed
238712854Sgabeblack@google.comoperator / (const sc_signed &u, unsigned long v)
238812854Sgabeblack@google.com{
238912854Sgabeblack@google.com    small_type s = mul_signs(u.sgn, get_sign(v));
239012854Sgabeblack@google.com
239112854Sgabeblack@google.com    if (s == SC_ZERO) {
239212854Sgabeblack@google.com        div_by_zero(v); // case 1
239312854Sgabeblack@google.com        return sc_signed(); // case 2
239412854Sgabeblack@google.com    }
239512854Sgabeblack@google.com
239612854Sgabeblack@google.com    CONVERT_LONG_2(v);
239712854Sgabeblack@google.com
239812854Sgabeblack@google.com    // other cases
239912854Sgabeblack@google.com    return div_signed_friend(s, u.nbits, u.ndigits, u.digit,
240012854Sgabeblack@google.com                             BITS_PER_ULONG, DIGITS_PER_ULONG, vd);
240112854Sgabeblack@google.com}
240212854Sgabeblack@google.com
240312854Sgabeblack@google.com
240412854Sgabeblack@google.comsc_signed
240512854Sgabeblack@google.comoperator / (unsigned long u, const sc_signed &v)
240612854Sgabeblack@google.com{
240712854Sgabeblack@google.com    small_type s = mul_signs(v.sgn, get_sign(u));
240812854Sgabeblack@google.com
240912854Sgabeblack@google.com    if (s == SC_ZERO) {
241012854Sgabeblack@google.com        div_by_zero(v.sgn); // case 1
241112854Sgabeblack@google.com        return sc_signed(); // case 2
241212854Sgabeblack@google.com
241312854Sgabeblack@google.com    }
241412854Sgabeblack@google.com
241512854Sgabeblack@google.com    CONVERT_LONG_2(u);
241612854Sgabeblack@google.com
241712854Sgabeblack@google.com    // other cases
241812854Sgabeblack@google.com    return div_signed_friend(s, BITS_PER_ULONG, DIGITS_PER_ULONG, ud,
241912854Sgabeblack@google.com                             v.nbits, v.ndigits, v.digit);
242012854Sgabeblack@google.com}
242112854Sgabeblack@google.com
242212854Sgabeblack@google.com// The rest of the operators in this section are included from
242312854Sgabeblack@google.com// sc_nbcommon.cpp.
242412854Sgabeblack@google.com
242512854Sgabeblack@google.com
242612854Sgabeblack@google.com// ----------------------------------------------------------------------------
242712854Sgabeblack@google.com//  SECTION: MOD operators: %, %=.
242812854Sgabeblack@google.com// ----------------------------------------------------------------------------
242912854Sgabeblack@google.com
243012854Sgabeblack@google.com// Cases to consider when finding the remainder r = u % v:
243112854Sgabeblack@google.com// Note that u = q * v + r for r < q.
243212854Sgabeblack@google.com// 1. 0 % 0 or u % 0 => error
243312854Sgabeblack@google.com// 2. 0 % v => 0 = 0 * v + 0
243412854Sgabeblack@google.com// 3. u % v & &u = v => u = 1 * u + 0  - u or v can be 1 or -1
243512854Sgabeblack@google.com// 4. u % v & &u < v => u = 0 * v + u  - u can be 1 or -1
243612854Sgabeblack@google.com// 5. u % v & &u > v => u = q * v + r  - v can be 1 or -1
243712854Sgabeblack@google.com
243812854Sgabeblack@google.comsc_signed
243912854Sgabeblack@google.comoperator % (const sc_unsigned &u, const sc_signed &v)
244012854Sgabeblack@google.com{
244112854Sgabeblack@google.com    if ((u.sgn == SC_ZERO) || (v.sgn == SC_ZERO)) {
244212854Sgabeblack@google.com        div_by_zero(v.sgn); // case 1
244312854Sgabeblack@google.com        return sc_signed(); // case 2
244412854Sgabeblack@google.com    }
244512854Sgabeblack@google.com
244612854Sgabeblack@google.com    // other cases
244712854Sgabeblack@google.com    return mod_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit,
244812854Sgabeblack@google.com                             v.nbits, v.ndigits, v.digit);
244912854Sgabeblack@google.com}
245012854Sgabeblack@google.com
245112854Sgabeblack@google.com
245212854Sgabeblack@google.comsc_signed
245312854Sgabeblack@google.comoperator % (const sc_signed &u, const sc_unsigned &v)
245412854Sgabeblack@google.com{
245512854Sgabeblack@google.com    if ((u.sgn == SC_ZERO) || (v.sgn == SC_ZERO)) {
245612854Sgabeblack@google.com        div_by_zero(v.sgn); // case 1
245712854Sgabeblack@google.com        return sc_signed(); // case 2
245812854Sgabeblack@google.com    }
245912854Sgabeblack@google.com
246012854Sgabeblack@google.com    // other cases
246112854Sgabeblack@google.com    return mod_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit,
246212854Sgabeblack@google.com                             v.nbits, v.ndigits, v.digit);
246312854Sgabeblack@google.com}
246412854Sgabeblack@google.com
246512854Sgabeblack@google.com
246612854Sgabeblack@google.comsc_signed
246712854Sgabeblack@google.comoperator % (const sc_signed &u, const sc_signed &v)
246812854Sgabeblack@google.com{
246912854Sgabeblack@google.com    if ((u.sgn == SC_ZERO) || (v.sgn == SC_ZERO)) {
247012854Sgabeblack@google.com        div_by_zero(v.sgn); // case 1
247112854Sgabeblack@google.com        return sc_signed(); // case 2
247212854Sgabeblack@google.com    }
247312854Sgabeblack@google.com
247412854Sgabeblack@google.com    // other cases
247512854Sgabeblack@google.com    return mod_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit,
247612854Sgabeblack@google.com                             v.nbits, v.ndigits, v.digit);
247712854Sgabeblack@google.com}
247812854Sgabeblack@google.com
247912854Sgabeblack@google.com
248012854Sgabeblack@google.comsc_signed
248112854Sgabeblack@google.comoperator % (const sc_signed &u, int64 v)
248212854Sgabeblack@google.com{
248312854Sgabeblack@google.com    small_type vs = get_sign(v);
248412854Sgabeblack@google.com
248512854Sgabeblack@google.com    if ((u.sgn == SC_ZERO) || (vs == SC_ZERO)) {
248612854Sgabeblack@google.com        div_by_zero(v); // case 1
248712854Sgabeblack@google.com        return sc_signed(); // case 2
248812854Sgabeblack@google.com    }
248912854Sgabeblack@google.com
249012854Sgabeblack@google.com    CONVERT_INT64_2(v);
249112854Sgabeblack@google.com
249212854Sgabeblack@google.com    // other cases
249312854Sgabeblack@google.com    return mod_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit,
249412854Sgabeblack@google.com                             BITS_PER_UINT64, DIGITS_PER_UINT64, vd);
249512854Sgabeblack@google.com}
249612854Sgabeblack@google.com
249712854Sgabeblack@google.com
249812854Sgabeblack@google.comsc_signed
249912854Sgabeblack@google.comoperator % (int64 u, const sc_signed &v)
250012854Sgabeblack@google.com{
250112854Sgabeblack@google.com    small_type us = get_sign(u);
250212854Sgabeblack@google.com
250312854Sgabeblack@google.com    if ((us == SC_ZERO) || (v.sgn == SC_ZERO)) {
250412854Sgabeblack@google.com        div_by_zero(v.sgn); // case 1
250512854Sgabeblack@google.com        return sc_signed(); // case 2
250612854Sgabeblack@google.com    }
250712854Sgabeblack@google.com
250812854Sgabeblack@google.com    CONVERT_INT64_2(u);
250912854Sgabeblack@google.com
251012854Sgabeblack@google.com    // other cases
251112854Sgabeblack@google.com    return mod_signed_friend(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud,
251212854Sgabeblack@google.com                             v.nbits, v.ndigits, v.digit);
251312854Sgabeblack@google.com}
251412854Sgabeblack@google.com
251512854Sgabeblack@google.com
251612854Sgabeblack@google.comsc_signed
251712854Sgabeblack@google.comoperator % (const sc_unsigned &u, int64 v)
251812854Sgabeblack@google.com{
251912854Sgabeblack@google.com    small_type vs = get_sign(v);
252012854Sgabeblack@google.com
252112854Sgabeblack@google.com    if ((u.sgn == SC_ZERO) || (vs == SC_ZERO)) {
252212854Sgabeblack@google.com        div_by_zero(v); // case 1
252312854Sgabeblack@google.com        return sc_signed(); // case 2
252412854Sgabeblack@google.com    }
252512854Sgabeblack@google.com
252612854Sgabeblack@google.com    CONVERT_INT64_2(v);
252712854Sgabeblack@google.com
252812854Sgabeblack@google.com    // other cases
252912854Sgabeblack@google.com    return mod_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit,
253012854Sgabeblack@google.com                             BITS_PER_UINT64, DIGITS_PER_UINT64, vd);
253112854Sgabeblack@google.com}
253212854Sgabeblack@google.com
253312854Sgabeblack@google.com
253412854Sgabeblack@google.comsc_signed
253512854Sgabeblack@google.comoperator % (int64 u, const sc_unsigned &v)
253612854Sgabeblack@google.com{
253712854Sgabeblack@google.com    small_type us = get_sign(u);
253812854Sgabeblack@google.com
253912854Sgabeblack@google.com    if ((us == SC_ZERO) || (v.sgn == SC_ZERO)) {
254012854Sgabeblack@google.com        div_by_zero(v.sgn); // case 1
254112854Sgabeblack@google.com        return sc_signed(); // case 2
254212854Sgabeblack@google.com    }
254312854Sgabeblack@google.com
254412854Sgabeblack@google.com    CONVERT_INT64_2(u);
254512854Sgabeblack@google.com
254612854Sgabeblack@google.com    // other cases
254712854Sgabeblack@google.com    return mod_signed_friend(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud,
254812854Sgabeblack@google.com                             v.nbits, v.ndigits, v.digit);
254912854Sgabeblack@google.com}
255012854Sgabeblack@google.com
255112854Sgabeblack@google.com
255212854Sgabeblack@google.comsc_signed
255312854Sgabeblack@google.comoperator % (const sc_signed &u, uint64 v)
255412854Sgabeblack@google.com{
255512854Sgabeblack@google.com    if ((u.sgn == SC_ZERO) || (v == 0)) {
255612854Sgabeblack@google.com        div_by_zero(v); // case 1
255712854Sgabeblack@google.com        return sc_signed(); // case 2
255812854Sgabeblack@google.com    }
255912854Sgabeblack@google.com
256012854Sgabeblack@google.com    CONVERT_INT64_2(v);
256112854Sgabeblack@google.com
256212854Sgabeblack@google.com    // other cases
256312854Sgabeblack@google.com    return mod_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit,
256412854Sgabeblack@google.com                             BITS_PER_UINT64, DIGITS_PER_UINT64, vd);
256512854Sgabeblack@google.com}
256612854Sgabeblack@google.com
256712854Sgabeblack@google.com
256812854Sgabeblack@google.comsc_signed
256912854Sgabeblack@google.comoperator % (uint64 u, const sc_signed &v)
257012854Sgabeblack@google.com{
257112854Sgabeblack@google.com    if ((u == 0) || (v.sgn == SC_ZERO)) {
257212854Sgabeblack@google.com        div_by_zero(v.sgn); // case 1
257312854Sgabeblack@google.com        return sc_signed(); // case 2
257412854Sgabeblack@google.com    }
257512854Sgabeblack@google.com
257612854Sgabeblack@google.com    CONVERT_INT64(u);
257712854Sgabeblack@google.com
257812854Sgabeblack@google.com    // other cases
257912854Sgabeblack@google.com    return mod_signed_friend(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud,
258012854Sgabeblack@google.com                             v.nbits, v.ndigits, v.digit);
258112854Sgabeblack@google.com}
258212854Sgabeblack@google.com
258312854Sgabeblack@google.com
258412854Sgabeblack@google.comsc_signed
258512854Sgabeblack@google.comoperator % (const sc_signed &u, long v)
258612854Sgabeblack@google.com{
258712854Sgabeblack@google.com    small_type vs = get_sign(v);
258812854Sgabeblack@google.com
258912854Sgabeblack@google.com    if ((u.sgn == SC_ZERO) || (vs == SC_ZERO)) {
259012854Sgabeblack@google.com        div_by_zero(v); // case 1
259112854Sgabeblack@google.com        return sc_signed(); // case 2
259212854Sgabeblack@google.com    }
259312854Sgabeblack@google.com
259412854Sgabeblack@google.com    CONVERT_LONG_2(v);
259512854Sgabeblack@google.com
259612854Sgabeblack@google.com    // other cases
259712854Sgabeblack@google.com    return mod_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit,
259812854Sgabeblack@google.com                             BITS_PER_ULONG, DIGITS_PER_ULONG, vd);
259912854Sgabeblack@google.com}
260012854Sgabeblack@google.com
260112854Sgabeblack@google.com
260212854Sgabeblack@google.comsc_signed
260312854Sgabeblack@google.comoperator % (long u, const sc_signed &v)
260412854Sgabeblack@google.com{
260512854Sgabeblack@google.com    small_type us = get_sign(u);
260612854Sgabeblack@google.com
260712854Sgabeblack@google.com    if ((us == SC_ZERO) || (v.sgn == SC_ZERO)) {
260812854Sgabeblack@google.com        div_by_zero(v.sgn); // case 1
260912854Sgabeblack@google.com        return sc_signed(); // case 2
261012854Sgabeblack@google.com    }
261112854Sgabeblack@google.com
261212854Sgabeblack@google.com    CONVERT_LONG_2(u);
261312854Sgabeblack@google.com
261412854Sgabeblack@google.com    // other cases
261512854Sgabeblack@google.com    return mod_signed_friend(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud,
261612854Sgabeblack@google.com                             v.nbits, v.ndigits, v.digit);
261712854Sgabeblack@google.com}
261812854Sgabeblack@google.com
261912854Sgabeblack@google.com
262012854Sgabeblack@google.comsc_signed
262112854Sgabeblack@google.comoperator % (const sc_unsigned &u, long v)
262212854Sgabeblack@google.com{
262312854Sgabeblack@google.com
262412854Sgabeblack@google.com  small_type vs = get_sign(v);
262512854Sgabeblack@google.com
262612854Sgabeblack@google.com  if ((u.sgn == SC_ZERO) || (vs == SC_ZERO)) {
262712854Sgabeblack@google.com    div_by_zero(v); // case 1
262812854Sgabeblack@google.com    return sc_signed(); // case 2
262912854Sgabeblack@google.com  }
263012854Sgabeblack@google.com
263112854Sgabeblack@google.com  CONVERT_LONG_2(v);
263212854Sgabeblack@google.com
263312854Sgabeblack@google.com  // other cases
263412854Sgabeblack@google.com  return mod_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit,
263512854Sgabeblack@google.com                           BITS_PER_ULONG, DIGITS_PER_ULONG, vd);
263612854Sgabeblack@google.com}
263712854Sgabeblack@google.com
263812854Sgabeblack@google.com
263912854Sgabeblack@google.comsc_signed
264012854Sgabeblack@google.comoperator % (long u, const sc_unsigned &v)
264112854Sgabeblack@google.com{
264212854Sgabeblack@google.com    small_type us = get_sign(u);
264312854Sgabeblack@google.com
264412854Sgabeblack@google.com    if ((us == SC_ZERO) || (v.sgn == SC_ZERO)) {
264512854Sgabeblack@google.com        div_by_zero(v.sgn); // case 1
264612854Sgabeblack@google.com        return sc_signed(); // case 2
264712854Sgabeblack@google.com    }
264812854Sgabeblack@google.com
264912854Sgabeblack@google.com    CONVERT_LONG_2(u);
265012854Sgabeblack@google.com
265112854Sgabeblack@google.com    // other cases
265212854Sgabeblack@google.com    return mod_signed_friend(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud,
265312854Sgabeblack@google.com                             v.nbits, v.ndigits, v.digit);
265412854Sgabeblack@google.com}
265512854Sgabeblack@google.com
265612854Sgabeblack@google.com
265712854Sgabeblack@google.comsc_signed
265812854Sgabeblack@google.comoperator % (const sc_signed &u, unsigned long v)
265912854Sgabeblack@google.com{
266012854Sgabeblack@google.com    if ((u.sgn == SC_ZERO) || (v == 0)) {
266112854Sgabeblack@google.com        div_by_zero(v); // case 1
266212854Sgabeblack@google.com        return sc_signed(); // case 2
266312854Sgabeblack@google.com    }
266412854Sgabeblack@google.com
266512854Sgabeblack@google.com    CONVERT_LONG_2(v);
266612854Sgabeblack@google.com
266712854Sgabeblack@google.com    // other cases
266812854Sgabeblack@google.com    return mod_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit,
266912854Sgabeblack@google.com                             BITS_PER_ULONG, DIGITS_PER_ULONG, vd);
267012854Sgabeblack@google.com}
267112854Sgabeblack@google.com
267212854Sgabeblack@google.com
267312854Sgabeblack@google.comsc_signed
267412854Sgabeblack@google.comoperator % (unsigned long u, const sc_signed &v)
267512854Sgabeblack@google.com{
267612854Sgabeblack@google.com    if ((u == 0) || (v.sgn == SC_ZERO)) {
267712854Sgabeblack@google.com        div_by_zero(v.sgn); // case 1
267812854Sgabeblack@google.com        return sc_signed(); // case 2
267912854Sgabeblack@google.com    }
268012854Sgabeblack@google.com
268112854Sgabeblack@google.com    CONVERT_LONG(u);
268212854Sgabeblack@google.com
268312854Sgabeblack@google.com    // other cases
268412854Sgabeblack@google.com    return mod_signed_friend(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud,
268512854Sgabeblack@google.com                             v.nbits, v.ndigits, v.digit);
268612854Sgabeblack@google.com}
268712854Sgabeblack@google.com
268812854Sgabeblack@google.com// The rest of the operators in this section are included from
268912854Sgabeblack@google.com// sc_nbcommon.cpp.
269012854Sgabeblack@google.com
269112854Sgabeblack@google.com
269212854Sgabeblack@google.com// ----------------------------------------------------------------------------
269312854Sgabeblack@google.com//  SECTION: Bitwise AND operators: &, &=
269412854Sgabeblack@google.com// ----------------------------------------------------------------------------
269512854Sgabeblack@google.com
269612854Sgabeblack@google.com// Cases to consider when computing u  &v:
269712854Sgabeblack@google.com// 1. u & 0 = 0  &v = 0
269812854Sgabeblack@google.com// 2. u  &v => sgn = +
269912854Sgabeblack@google.com// 3. (-u) & (-v) => sgn = -
270012854Sgabeblack@google.com// 4. u & (-v) => sgn = +
270112854Sgabeblack@google.com// 5. (-u)  &v => sgn = +
270212854Sgabeblack@google.com
270312854Sgabeblack@google.comsc_signed
270412854Sgabeblack@google.comoperator & (const sc_unsigned &u, const sc_signed &v)
270512854Sgabeblack@google.com{
270612854Sgabeblack@google.com    if ((u.sgn == SC_ZERO) || (v.sgn == SC_ZERO)) // case 1
270712854Sgabeblack@google.com        return sc_signed();
270812854Sgabeblack@google.com
270912854Sgabeblack@google.com    // other cases
271012854Sgabeblack@google.com    return and_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit,
271112854Sgabeblack@google.com                             v.sgn, v.nbits, v.ndigits, v.digit);
271212854Sgabeblack@google.com}
271312854Sgabeblack@google.com
271412854Sgabeblack@google.com
271512854Sgabeblack@google.comsc_signed
271612854Sgabeblack@google.comoperator & (const sc_signed &u, const sc_unsigned &v)
271712854Sgabeblack@google.com{
271812854Sgabeblack@google.com    if ((u.sgn == SC_ZERO) || (v.sgn == SC_ZERO)) // case 1
271912854Sgabeblack@google.com        return sc_signed();
272012854Sgabeblack@google.com
272112854Sgabeblack@google.com    // other cases
272212854Sgabeblack@google.com    return and_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit,
272312854Sgabeblack@google.com                             v.sgn, v.nbits, v.ndigits, v.digit);
272412854Sgabeblack@google.com}
272512854Sgabeblack@google.com
272612854Sgabeblack@google.com
272712854Sgabeblack@google.comsc_signed
272812854Sgabeblack@google.comoperator & (const sc_signed &u, const sc_signed &v)
272912854Sgabeblack@google.com{
273012854Sgabeblack@google.com    if ((u.sgn == SC_ZERO) || (v.sgn == SC_ZERO)) // case 1
273112854Sgabeblack@google.com        return sc_signed();
273212854Sgabeblack@google.com
273312854Sgabeblack@google.com    // other cases
273412854Sgabeblack@google.com    return and_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit,
273512854Sgabeblack@google.com                             v.sgn, v.nbits, v.ndigits, v.digit);
273612854Sgabeblack@google.com}
273712854Sgabeblack@google.com
273812854Sgabeblack@google.com
273912854Sgabeblack@google.comsc_signed
274012854Sgabeblack@google.comoperator & (const sc_signed &u, int64 v)
274112854Sgabeblack@google.com{
274212854Sgabeblack@google.com    if ((u.sgn == SC_ZERO) || (v == 0)) // case 1
274312854Sgabeblack@google.com        return sc_signed();
274412854Sgabeblack@google.com
274512854Sgabeblack@google.com    CONVERT_INT64(v);
274612854Sgabeblack@google.com
274712854Sgabeblack@google.com    // other cases
274812854Sgabeblack@google.com    return and_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit,
274912854Sgabeblack@google.com                             vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd);
275012854Sgabeblack@google.com}
275112854Sgabeblack@google.com
275212854Sgabeblack@google.com
275312854Sgabeblack@google.comsc_signed
275412854Sgabeblack@google.comoperator & (int64 u, const sc_signed &v)
275512854Sgabeblack@google.com{
275612854Sgabeblack@google.com    if ((u == 0) || (v.sgn == SC_ZERO)) // case 1
275712854Sgabeblack@google.com        return sc_signed();
275812854Sgabeblack@google.com
275912854Sgabeblack@google.com    CONVERT_INT64(u);
276012854Sgabeblack@google.com
276112854Sgabeblack@google.com    // other cases
276212854Sgabeblack@google.com    return and_signed_friend(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud,
276312854Sgabeblack@google.com                             v.sgn, v.nbits, v.ndigits, v.digit);
276412854Sgabeblack@google.com}
276512854Sgabeblack@google.com
276612854Sgabeblack@google.com
276712854Sgabeblack@google.comsc_signed
276812854Sgabeblack@google.comoperator & (const sc_unsigned &u, int64 v)
276912854Sgabeblack@google.com{
277012854Sgabeblack@google.com    if ((u.sgn == SC_ZERO) || (v == 0)) // case 1
277112854Sgabeblack@google.com        return sc_signed();
277212854Sgabeblack@google.com
277312854Sgabeblack@google.com    CONVERT_INT64(v);
277412854Sgabeblack@google.com
277512854Sgabeblack@google.com    // other cases
277612854Sgabeblack@google.com    return and_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit,
277712854Sgabeblack@google.com                             vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd);
277812854Sgabeblack@google.com}
277912854Sgabeblack@google.com
278012854Sgabeblack@google.com
278112854Sgabeblack@google.comsc_signed
278212854Sgabeblack@google.comoperator & (int64 u, const sc_unsigned &v)
278312854Sgabeblack@google.com{
278412854Sgabeblack@google.com    if ((u == 0) || (v.sgn == SC_ZERO)) // case 1
278512854Sgabeblack@google.com        return sc_signed();
278612854Sgabeblack@google.com
278712854Sgabeblack@google.com    CONVERT_INT64(u);
278812854Sgabeblack@google.com
278912854Sgabeblack@google.com    // other cases
279012854Sgabeblack@google.com    return and_signed_friend(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud,
279112854Sgabeblack@google.com                             v.sgn, v.nbits, v.ndigits, v.digit);
279212854Sgabeblack@google.com}
279312854Sgabeblack@google.com
279412854Sgabeblack@google.com
279512854Sgabeblack@google.comsc_signed
279612854Sgabeblack@google.comoperator & (const sc_signed &u, uint64 v)
279712854Sgabeblack@google.com{
279812854Sgabeblack@google.com    if ((u.sgn == SC_ZERO) || (v == 0)) // case 1
279912854Sgabeblack@google.com        return sc_signed();
280012854Sgabeblack@google.com
280112854Sgabeblack@google.com    CONVERT_INT64(v);
280212854Sgabeblack@google.com
280312854Sgabeblack@google.com    // other cases
280412854Sgabeblack@google.com    return and_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit,
280512854Sgabeblack@google.com                             vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd);
280612854Sgabeblack@google.com}
280712854Sgabeblack@google.com
280812854Sgabeblack@google.com
280912854Sgabeblack@google.comsc_signed
281012854Sgabeblack@google.comoperator & (uint64 u, const sc_signed &v)
281112854Sgabeblack@google.com{
281212854Sgabeblack@google.com    if ((u == 0) || (v.sgn == SC_ZERO)) // case 1
281312854Sgabeblack@google.com        return sc_signed();
281412854Sgabeblack@google.com
281512854Sgabeblack@google.com    CONVERT_INT64(u);
281612854Sgabeblack@google.com
281712854Sgabeblack@google.com    // other cases
281812854Sgabeblack@google.com    return and_signed_friend(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud,
281912854Sgabeblack@google.com                             v.sgn, v.nbits, v.ndigits, v.digit);
282012854Sgabeblack@google.com}
282112854Sgabeblack@google.com
282212854Sgabeblack@google.com
282312854Sgabeblack@google.comsc_signed
282412854Sgabeblack@google.comoperator & (const sc_signed &u, long v)
282512854Sgabeblack@google.com{
282612854Sgabeblack@google.com    if ((u.sgn == SC_ZERO) || (v == 0)) // case 1
282712854Sgabeblack@google.com        return sc_signed();
282812854Sgabeblack@google.com
282912854Sgabeblack@google.com    CONVERT_LONG(v);
283012854Sgabeblack@google.com
283112854Sgabeblack@google.com    // other cases
283212854Sgabeblack@google.com    return and_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit,
283312854Sgabeblack@google.com                             vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd);
283412854Sgabeblack@google.com}
283512854Sgabeblack@google.com
283612854Sgabeblack@google.com
283712854Sgabeblack@google.comsc_signed
283812854Sgabeblack@google.comoperator & (long u, const sc_signed &v)
283912854Sgabeblack@google.com{
284012854Sgabeblack@google.com    if ((u == 0) || (v.sgn == SC_ZERO)) // case 1
284112854Sgabeblack@google.com        return sc_signed();
284212854Sgabeblack@google.com
284312854Sgabeblack@google.com    CONVERT_LONG(u);
284412854Sgabeblack@google.com
284512854Sgabeblack@google.com    // other cases
284612854Sgabeblack@google.com    return and_signed_friend(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud,
284712854Sgabeblack@google.com                             v.sgn, v.nbits, v.ndigits, v.digit);
284812854Sgabeblack@google.com}
284912854Sgabeblack@google.com
285012854Sgabeblack@google.com
285112854Sgabeblack@google.comsc_signed
285212854Sgabeblack@google.comoperator & (const sc_unsigned &u, long v)
285312854Sgabeblack@google.com{
285412854Sgabeblack@google.com    if ((u.sgn == SC_ZERO) || (v == 0)) // case 1
285512854Sgabeblack@google.com        return sc_signed();
285612854Sgabeblack@google.com
285712854Sgabeblack@google.com    CONVERT_LONG(v);
285812854Sgabeblack@google.com
285912854Sgabeblack@google.com    // other cases
286012854Sgabeblack@google.com    return and_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit,
286112854Sgabeblack@google.com                             vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd);
286212854Sgabeblack@google.com}
286312854Sgabeblack@google.com
286412854Sgabeblack@google.com
286512854Sgabeblack@google.comsc_signed
286612854Sgabeblack@google.comoperator & (long u, const sc_unsigned &v)
286712854Sgabeblack@google.com{
286812854Sgabeblack@google.com    if ((u == 0) || (v.sgn == SC_ZERO)) // case 1
286912854Sgabeblack@google.com        return sc_signed();
287012854Sgabeblack@google.com
287112854Sgabeblack@google.com    CONVERT_LONG(u);
287212854Sgabeblack@google.com
287312854Sgabeblack@google.com    // other cases
287412854Sgabeblack@google.com    return and_signed_friend(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud,
287512854Sgabeblack@google.com                             v.sgn, v.nbits, v.ndigits, v.digit);
287612854Sgabeblack@google.com}
287712854Sgabeblack@google.com
287812854Sgabeblack@google.com
287912854Sgabeblack@google.comsc_signed
288012854Sgabeblack@google.comoperator & (const sc_signed &u, unsigned long v)
288112854Sgabeblack@google.com{
288212854Sgabeblack@google.com    if ((u.sgn == SC_ZERO) || (v == 0)) // case 1
288312854Sgabeblack@google.com        return sc_signed();
288412854Sgabeblack@google.com
288512854Sgabeblack@google.com    CONVERT_LONG(v);
288612854Sgabeblack@google.com
288712854Sgabeblack@google.com    // other cases
288812854Sgabeblack@google.com    return and_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit,
288912854Sgabeblack@google.com                             vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd);
289012854Sgabeblack@google.com}
289112854Sgabeblack@google.com
289212854Sgabeblack@google.com
289312854Sgabeblack@google.comsc_signed
289412854Sgabeblack@google.comoperator & (unsigned long u, const sc_signed &v)
289512854Sgabeblack@google.com{
289612854Sgabeblack@google.com    if ((u == 0) || (v.sgn == SC_ZERO)) // case 1
289712854Sgabeblack@google.com        return sc_signed();
289812854Sgabeblack@google.com
289912854Sgabeblack@google.com    CONVERT_LONG(u);
290012854Sgabeblack@google.com
290112854Sgabeblack@google.com    // other cases
290212854Sgabeblack@google.com    return and_signed_friend(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud,
290312854Sgabeblack@google.com                             v.sgn, v.nbits, v.ndigits, v.digit);
290412854Sgabeblack@google.com}
290512854Sgabeblack@google.com
290612854Sgabeblack@google.com// The rest of the operators in this section are included from
290712854Sgabeblack@google.com// sc_nbcommon.cpp.
290812854Sgabeblack@google.com
290912854Sgabeblack@google.com
291012854Sgabeblack@google.com// ----------------------------------------------------------------------------
291112854Sgabeblack@google.com//  SECTION: Bitwise OR operators: |, |=
291212854Sgabeblack@google.com// ----------------------------------------------------------------------------
291312854Sgabeblack@google.com
291412854Sgabeblack@google.com// Cases to consider when computing u | v:
291512854Sgabeblack@google.com// 1. u | 0 = u
291612854Sgabeblack@google.com// 2. 0 | v = v
291712854Sgabeblack@google.com// 3. u | v => sgn = +
291812854Sgabeblack@google.com// 4. (-u) | (-v) => sgn = -
291912854Sgabeblack@google.com// 5. u | (-v) => sgn = -
292012854Sgabeblack@google.com// 6. (-u) | v => sgn = -
292112854Sgabeblack@google.com
292212854Sgabeblack@google.comsc_signed
292312854Sgabeblack@google.comoperator | (const sc_unsigned &u, const sc_signed &v)
292412854Sgabeblack@google.com{
292512854Sgabeblack@google.com    if (v.sgn == SC_ZERO) // case 1
292612854Sgabeblack@google.com        return sc_signed(u);
292712854Sgabeblack@google.com
292812854Sgabeblack@google.com    if (u.sgn == SC_ZERO) // case 2
292912854Sgabeblack@google.com        return sc_signed(v);
293012854Sgabeblack@google.com
293112854Sgabeblack@google.com    // other cases
293212854Sgabeblack@google.com    return or_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit,
293312854Sgabeblack@google.com                            v.sgn, v.nbits, v.ndigits, v.digit);
293412854Sgabeblack@google.com}
293512854Sgabeblack@google.com
293612854Sgabeblack@google.com
293712854Sgabeblack@google.comsc_signed
293812854Sgabeblack@google.comoperator | (const sc_signed &u, const sc_unsigned &v)
293912854Sgabeblack@google.com{
294012854Sgabeblack@google.com    if (v.sgn == SC_ZERO) // case 1
294112854Sgabeblack@google.com        return sc_signed(u);
294212854Sgabeblack@google.com
294312854Sgabeblack@google.com    if (u.sgn == SC_ZERO) // case 2
294412854Sgabeblack@google.com        return sc_signed(v);
294512854Sgabeblack@google.com
294612854Sgabeblack@google.com    // other cases
294712854Sgabeblack@google.com    return or_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit,
294812854Sgabeblack@google.com                            v.sgn, v.nbits, v.ndigits, v.digit);
294912854Sgabeblack@google.com}
295012854Sgabeblack@google.com
295112854Sgabeblack@google.com
295212854Sgabeblack@google.comsc_signed
295312854Sgabeblack@google.comoperator | (const sc_signed &u, const sc_signed &v)
295412854Sgabeblack@google.com{
295512854Sgabeblack@google.com    if (v.sgn == SC_ZERO) // case 1
295612854Sgabeblack@google.com        return sc_signed(u);
295712854Sgabeblack@google.com
295812854Sgabeblack@google.com    if (u.sgn == SC_ZERO) // case 2
295912854Sgabeblack@google.com        return sc_signed(v);
296012854Sgabeblack@google.com
296112854Sgabeblack@google.com    // other cases
296212854Sgabeblack@google.com    return or_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit,
296312854Sgabeblack@google.com                            v.sgn, v.nbits, v.ndigits, v.digit);
296412854Sgabeblack@google.com}
296512854Sgabeblack@google.com
296612854Sgabeblack@google.com
296712854Sgabeblack@google.comsc_signed
296812854Sgabeblack@google.comoperator | (const sc_signed &u, int64 v)
296912854Sgabeblack@google.com{
297012854Sgabeblack@google.com    if (v == 0) // case 1
297112854Sgabeblack@google.com        return sc_signed(u);
297212854Sgabeblack@google.com
297312854Sgabeblack@google.com    CONVERT_INT64(v);
297412854Sgabeblack@google.com
297512854Sgabeblack@google.com    if (u.sgn == SC_ZERO) // case 2
297612854Sgabeblack@google.com        return sc_signed(vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd, false);
297712854Sgabeblack@google.com
297812854Sgabeblack@google.com    // other cases
297912854Sgabeblack@google.com    return or_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit,
298012854Sgabeblack@google.com                            vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd);
298112854Sgabeblack@google.com}
298212854Sgabeblack@google.com
298312854Sgabeblack@google.com
298412854Sgabeblack@google.comsc_signed
298512854Sgabeblack@google.comoperator | (int64 u, const sc_signed &v)
298612854Sgabeblack@google.com{
298712854Sgabeblack@google.com    if (u == 0)
298812854Sgabeblack@google.com        return sc_signed(v);
298912854Sgabeblack@google.com
299012854Sgabeblack@google.com    CONVERT_INT64(u);
299112854Sgabeblack@google.com
299212854Sgabeblack@google.com    if (v.sgn == SC_ZERO)
299312854Sgabeblack@google.com        return sc_signed(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud, false);
299412854Sgabeblack@google.com
299512854Sgabeblack@google.com    // other cases
299612854Sgabeblack@google.com    return or_signed_friend(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud,
299712854Sgabeblack@google.com                            v.sgn, v.nbits, v.ndigits, v.digit);
299812854Sgabeblack@google.com}
299912854Sgabeblack@google.com
300012854Sgabeblack@google.com
300112854Sgabeblack@google.comsc_signed
300212854Sgabeblack@google.comoperator | (const sc_unsigned &u, int64 v)
300312854Sgabeblack@google.com{
300412854Sgabeblack@google.com    if (v == 0) // case 1
300512854Sgabeblack@google.com        return sc_signed(u);
300612854Sgabeblack@google.com
300712854Sgabeblack@google.com    CONVERT_INT64(v);
300812854Sgabeblack@google.com
300912854Sgabeblack@google.com    if (u.sgn == SC_ZERO) // case 2
301012854Sgabeblack@google.com        return sc_signed(vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd, false);
301112854Sgabeblack@google.com
301212854Sgabeblack@google.com    // other cases
301312854Sgabeblack@google.com    return or_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit,
301412854Sgabeblack@google.com                            vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd);
301512854Sgabeblack@google.com}
301612854Sgabeblack@google.com
301712854Sgabeblack@google.com
301812854Sgabeblack@google.comsc_signed
301912854Sgabeblack@google.comoperator | (int64 u, const sc_unsigned &v)
302012854Sgabeblack@google.com{
302112854Sgabeblack@google.com    if (u == 0)
302212854Sgabeblack@google.com        return sc_signed(v);
302312854Sgabeblack@google.com
302412854Sgabeblack@google.com    CONVERT_INT64(u);
302512854Sgabeblack@google.com
302612854Sgabeblack@google.com    if (v.sgn == SC_ZERO)
302712854Sgabeblack@google.com        return sc_signed(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud, false);
302812854Sgabeblack@google.com
302912854Sgabeblack@google.com    // other cases
303012854Sgabeblack@google.com    return or_signed_friend(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud,
303112854Sgabeblack@google.com                            v.sgn, v.nbits, v.ndigits, v.digit);
303212854Sgabeblack@google.com}
303312854Sgabeblack@google.com
303412854Sgabeblack@google.com
303512854Sgabeblack@google.comsc_signed
303612854Sgabeblack@google.comoperator | (const sc_signed &u, uint64 v)
303712854Sgabeblack@google.com{
303812854Sgabeblack@google.com    if (v == 0) // case 1
303912854Sgabeblack@google.com        return sc_signed(u);
304012854Sgabeblack@google.com
304112854Sgabeblack@google.com    CONVERT_INT64(v);
304212854Sgabeblack@google.com
304312854Sgabeblack@google.com    if (u.sgn == SC_ZERO) // case 2
304412854Sgabeblack@google.com        return sc_signed(vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd, false);
304512854Sgabeblack@google.com
304612854Sgabeblack@google.com    // other cases
304712854Sgabeblack@google.com    return or_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit,
304812854Sgabeblack@google.com                            vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd);
304912854Sgabeblack@google.com}
305012854Sgabeblack@google.com
305112854Sgabeblack@google.com
305212854Sgabeblack@google.comsc_signed
305312854Sgabeblack@google.comoperator | (uint64 u, const sc_signed &v)
305412854Sgabeblack@google.com{
305512854Sgabeblack@google.com    if (u == 0)
305612854Sgabeblack@google.com        return sc_signed(v);
305712854Sgabeblack@google.com
305812854Sgabeblack@google.com    CONVERT_INT64(u);
305912854Sgabeblack@google.com
306012854Sgabeblack@google.com    if (v.sgn == SC_ZERO)
306112854Sgabeblack@google.com        return sc_signed(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud, false);
306212854Sgabeblack@google.com
306312854Sgabeblack@google.com    // other cases
306412854Sgabeblack@google.com    return or_signed_friend(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud,
306512854Sgabeblack@google.com                            v.sgn, v.nbits, v.ndigits, v.digit);
306612854Sgabeblack@google.com}
306712854Sgabeblack@google.com
306812854Sgabeblack@google.com
306912854Sgabeblack@google.comsc_signed
307012854Sgabeblack@google.comoperator | (const sc_signed &u, long v)
307112854Sgabeblack@google.com{
307212854Sgabeblack@google.com    if (v == 0) // case 1
307312854Sgabeblack@google.com        return sc_signed(u);
307412854Sgabeblack@google.com
307512854Sgabeblack@google.com    CONVERT_LONG(v);
307612854Sgabeblack@google.com
307712854Sgabeblack@google.com    if (u.sgn == SC_ZERO) // case 2
307812854Sgabeblack@google.com        return sc_signed(vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd, false);
307912854Sgabeblack@google.com
308012854Sgabeblack@google.com    // other cases
308112854Sgabeblack@google.com    return or_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit,
308212854Sgabeblack@google.com                            vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd);
308312854Sgabeblack@google.com}
308412854Sgabeblack@google.com
308512854Sgabeblack@google.com
308612854Sgabeblack@google.comsc_signed
308712854Sgabeblack@google.comoperator | (long u, const sc_signed &v)
308812854Sgabeblack@google.com{
308912854Sgabeblack@google.com    if (u == 0)
309012854Sgabeblack@google.com        return sc_signed(v);
309112854Sgabeblack@google.com
309212854Sgabeblack@google.com    CONVERT_LONG(u);
309312854Sgabeblack@google.com
309412854Sgabeblack@google.com    if (v.sgn == SC_ZERO)
309512854Sgabeblack@google.com        return sc_signed(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud, false);
309612854Sgabeblack@google.com
309712854Sgabeblack@google.com    // other cases
309812854Sgabeblack@google.com    return or_signed_friend(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud,
309912854Sgabeblack@google.com                            v.sgn, v.nbits, v.ndigits, v.digit);
310012854Sgabeblack@google.com}
310112854Sgabeblack@google.com
310212854Sgabeblack@google.com
310312854Sgabeblack@google.comsc_signed
310412854Sgabeblack@google.comoperator | (const sc_unsigned &u, long v)
310512854Sgabeblack@google.com{
310612854Sgabeblack@google.com    if (v == 0) // case 1
310712854Sgabeblack@google.com        return sc_signed(u);
310812854Sgabeblack@google.com
310912854Sgabeblack@google.com    CONVERT_LONG(v);
311012854Sgabeblack@google.com
311112854Sgabeblack@google.com    if (u.sgn == SC_ZERO) // case 2
311212854Sgabeblack@google.com        return sc_signed(vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd, false);
311312854Sgabeblack@google.com
311412854Sgabeblack@google.com    // other cases
311512854Sgabeblack@google.com    return or_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit,
311612854Sgabeblack@google.com                            vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd);
311712854Sgabeblack@google.com}
311812854Sgabeblack@google.com
311912854Sgabeblack@google.com
312012854Sgabeblack@google.comsc_signed
312112854Sgabeblack@google.comoperator | (long u, const sc_unsigned &v)
312212854Sgabeblack@google.com{
312312854Sgabeblack@google.com    if (u == 0)
312412854Sgabeblack@google.com        return sc_signed(v);
312512854Sgabeblack@google.com
312612854Sgabeblack@google.com    CONVERT_LONG(u);
312712854Sgabeblack@google.com
312812854Sgabeblack@google.com    if (v.sgn == SC_ZERO)
312912854Sgabeblack@google.com        return sc_signed(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud, false);
313012854Sgabeblack@google.com
313112854Sgabeblack@google.com    // other cases
313212854Sgabeblack@google.com    return or_signed_friend(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud,
313312854Sgabeblack@google.com                            v.sgn, v.nbits, v.ndigits, v.digit);
313412854Sgabeblack@google.com}
313512854Sgabeblack@google.com
313612854Sgabeblack@google.com
313712854Sgabeblack@google.comsc_signed
313812854Sgabeblack@google.comoperator | (const sc_signed &u, unsigned long v)
313912854Sgabeblack@google.com{
314012854Sgabeblack@google.com    if (v == 0) // case 1
314112854Sgabeblack@google.com        return sc_signed(u);
314212854Sgabeblack@google.com
314312854Sgabeblack@google.com    CONVERT_LONG(v);
314412854Sgabeblack@google.com
314512854Sgabeblack@google.com    if (u.sgn == SC_ZERO) // case 2
314612854Sgabeblack@google.com        return sc_signed(vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd, false);
314712854Sgabeblack@google.com
314812854Sgabeblack@google.com    // other cases
314912854Sgabeblack@google.com    return or_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit,
315012854Sgabeblack@google.com                            vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd);
315112854Sgabeblack@google.com}
315212854Sgabeblack@google.com
315312854Sgabeblack@google.com
315412854Sgabeblack@google.comsc_signed
315512854Sgabeblack@google.comoperator | (unsigned long u, const sc_signed &v)
315612854Sgabeblack@google.com{
315712854Sgabeblack@google.com    if (u == 0)
315812854Sgabeblack@google.com        return sc_signed(v);
315912854Sgabeblack@google.com
316012854Sgabeblack@google.com    CONVERT_LONG(u);
316112854Sgabeblack@google.com
316212854Sgabeblack@google.com    if (v.sgn == SC_ZERO)
316312854Sgabeblack@google.com        return sc_signed(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud, false);
316412854Sgabeblack@google.com
316512854Sgabeblack@google.com    // other cases
316612854Sgabeblack@google.com    return or_signed_friend(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud,
316712854Sgabeblack@google.com                            v.sgn, v.nbits, v.ndigits, v.digit);
316812854Sgabeblack@google.com}
316912854Sgabeblack@google.com
317012854Sgabeblack@google.com// The rest of the operators in this section are included from
317112854Sgabeblack@google.com// sc_nbcommon.cpp.
317212854Sgabeblack@google.com
317312854Sgabeblack@google.com
317412854Sgabeblack@google.com// ----------------------------------------------------------------------------
317512854Sgabeblack@google.com//  SECTION: Bitwise XOR operators: ^, ^=
317612854Sgabeblack@google.com// ----------------------------------------------------------------------------
317712854Sgabeblack@google.com
317812854Sgabeblack@google.com// Cases to consider when computing u ^ v:
317912854Sgabeblack@google.com// Note that  u ^ v = (~u  &v) | (u & ~v).
318012854Sgabeblack@google.com// 1. u ^ 0 = u
318112854Sgabeblack@google.com// 2. 0 ^ v = v
318212854Sgabeblack@google.com// 3. u ^ v => sgn = +
318312854Sgabeblack@google.com// 4. (-u) ^ (-v) => sgn = -
318412854Sgabeblack@google.com// 5. u ^ (-v) => sgn = -
318512854Sgabeblack@google.com// 6. (-u) ^ v => sgn = +
318612854Sgabeblack@google.com
318712854Sgabeblack@google.comsc_signed
318812854Sgabeblack@google.comoperator ^ (const sc_unsigned &u, const sc_signed &v)
318912854Sgabeblack@google.com{
319012854Sgabeblack@google.com
319112854Sgabeblack@google.com  if (v.sgn == SC_ZERO) // case 1
319212854Sgabeblack@google.com    return sc_signed(u);
319312854Sgabeblack@google.com
319412854Sgabeblack@google.com  if (u.sgn == SC_ZERO) // case 2
319512854Sgabeblack@google.com    return sc_signed(v);
319612854Sgabeblack@google.com
319712854Sgabeblack@google.com  // other cases
319812854Sgabeblack@google.com  return xor_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit,
319912854Sgabeblack@google.com                           v.sgn, v.nbits, v.ndigits, v.digit);
320012854Sgabeblack@google.com
320112854Sgabeblack@google.com}
320212854Sgabeblack@google.com
320312854Sgabeblack@google.com
320412854Sgabeblack@google.comsc_signed
320512854Sgabeblack@google.comoperator ^ (const sc_signed &u, const sc_unsigned &v)
320612854Sgabeblack@google.com{
320712854Sgabeblack@google.com    if (v.sgn == SC_ZERO) // case 1
320812854Sgabeblack@google.com        return sc_signed(u);
320912854Sgabeblack@google.com
321012854Sgabeblack@google.com    if (u.sgn == SC_ZERO) // case 2
321112854Sgabeblack@google.com        return sc_signed(v);
321212854Sgabeblack@google.com
321312854Sgabeblack@google.com    // other cases
321412854Sgabeblack@google.com    return xor_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit,
321512854Sgabeblack@google.com                             v.sgn, v.nbits, v.ndigits, v.digit);
321612854Sgabeblack@google.com}
321712854Sgabeblack@google.com
321812854Sgabeblack@google.com
321912854Sgabeblack@google.comsc_signed
322012854Sgabeblack@google.comoperator ^ (const sc_signed &u, const sc_signed &v)
322112854Sgabeblack@google.com{
322212854Sgabeblack@google.com    if (v.sgn == SC_ZERO) // case 1
322312854Sgabeblack@google.com        return sc_signed(u);
322412854Sgabeblack@google.com
322512854Sgabeblack@google.com    if (u.sgn == SC_ZERO) // case 2
322612854Sgabeblack@google.com        return sc_signed(v);
322712854Sgabeblack@google.com
322812854Sgabeblack@google.com    // other cases
322912854Sgabeblack@google.com    return xor_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit,
323012854Sgabeblack@google.com                             v.sgn, v.nbits, v.ndigits, v.digit);
323112854Sgabeblack@google.com}
323212854Sgabeblack@google.com
323312854Sgabeblack@google.com
323412854Sgabeblack@google.comsc_signed
323512854Sgabeblack@google.comoperator ^ (const sc_signed &u, int64 v)
323612854Sgabeblack@google.com{
323712854Sgabeblack@google.com    if (v == 0) // case 1
323812854Sgabeblack@google.com        return sc_signed(u);
323912854Sgabeblack@google.com
324012854Sgabeblack@google.com    CONVERT_INT64(v);
324112854Sgabeblack@google.com
324212854Sgabeblack@google.com    if (u.sgn == SC_ZERO) // case 2
324312854Sgabeblack@google.com        return sc_signed(vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd, false);
324412854Sgabeblack@google.com
324512854Sgabeblack@google.com    // other cases
324612854Sgabeblack@google.com    return xor_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit,
324712854Sgabeblack@google.com                             vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd);
324812854Sgabeblack@google.com}
324912854Sgabeblack@google.com
325012854Sgabeblack@google.com
325112854Sgabeblack@google.comsc_signed
325212854Sgabeblack@google.comoperator ^ (int64 u, const sc_signed &v)
325312854Sgabeblack@google.com{
325412854Sgabeblack@google.com    if (u == 0)
325512854Sgabeblack@google.com        return sc_signed(v);
325612854Sgabeblack@google.com
325712854Sgabeblack@google.com    CONVERT_INT64(u);
325812854Sgabeblack@google.com
325912854Sgabeblack@google.com    if (v.sgn == SC_ZERO)
326012854Sgabeblack@google.com        return sc_signed(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud, false);
326112854Sgabeblack@google.com
326212854Sgabeblack@google.com    // other cases
326312854Sgabeblack@google.com    return xor_signed_friend(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud,
326412854Sgabeblack@google.com                             v.sgn, v.nbits, v.ndigits, v.digit);
326512854Sgabeblack@google.com}
326612854Sgabeblack@google.com
326712854Sgabeblack@google.com
326812854Sgabeblack@google.comsc_signed
326912854Sgabeblack@google.comoperator ^ (const sc_unsigned &u, int64 v)
327012854Sgabeblack@google.com{
327112854Sgabeblack@google.com    if (v == 0) // case 1
327212854Sgabeblack@google.com        return sc_signed(u);
327312854Sgabeblack@google.com
327412854Sgabeblack@google.com    CONVERT_INT64(v);
327512854Sgabeblack@google.com
327612854Sgabeblack@google.com    if (u.sgn == SC_ZERO) // case 2
327712854Sgabeblack@google.com        return sc_signed(vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd, false);
327812854Sgabeblack@google.com
327912854Sgabeblack@google.com    // other cases
328012854Sgabeblack@google.com    return xor_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit,
328112854Sgabeblack@google.com                             vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd);
328212854Sgabeblack@google.com}
328312854Sgabeblack@google.com
328412854Sgabeblack@google.com
328512854Sgabeblack@google.comsc_signed
328612854Sgabeblack@google.comoperator ^ (int64 u, const sc_unsigned &v)
328712854Sgabeblack@google.com{
328812854Sgabeblack@google.com    if (u == 0)
328912854Sgabeblack@google.com        return sc_signed(v);
329012854Sgabeblack@google.com
329112854Sgabeblack@google.com    CONVERT_INT64(u);
329212854Sgabeblack@google.com
329312854Sgabeblack@google.com    if (v.sgn == SC_ZERO)
329412854Sgabeblack@google.com        return sc_signed(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud, false);
329512854Sgabeblack@google.com
329612854Sgabeblack@google.com    // other cases
329712854Sgabeblack@google.com    return xor_signed_friend(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud,
329812854Sgabeblack@google.com                             v.sgn, v.nbits, v.ndigits, v.digit);
329912854Sgabeblack@google.com}
330012854Sgabeblack@google.com
330112854Sgabeblack@google.com
330212854Sgabeblack@google.comsc_signed
330312854Sgabeblack@google.comoperator ^ (const sc_signed &u, uint64 v)
330412854Sgabeblack@google.com{
330512854Sgabeblack@google.com    if (v == 0) // case 1
330612854Sgabeblack@google.com        return sc_signed(u);
330712854Sgabeblack@google.com
330812854Sgabeblack@google.com    CONVERT_INT64(v);
330912854Sgabeblack@google.com
331012854Sgabeblack@google.com    if (u.sgn == SC_ZERO) // case 2
331112854Sgabeblack@google.com        return sc_signed(vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd, false);
331212854Sgabeblack@google.com
331312854Sgabeblack@google.com    // other cases
331412854Sgabeblack@google.com    return xor_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit,
331512854Sgabeblack@google.com                             vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd);
331612854Sgabeblack@google.com}
331712854Sgabeblack@google.com
331812854Sgabeblack@google.comsc_signed
331912854Sgabeblack@google.comoperator ^ (uint64 u, const sc_signed &v)
332012854Sgabeblack@google.com{
332112854Sgabeblack@google.com    if (u == 0)
332212854Sgabeblack@google.com        return sc_signed(v);
332312854Sgabeblack@google.com
332412854Sgabeblack@google.com    CONVERT_INT64(u);
332512854Sgabeblack@google.com
332612854Sgabeblack@google.com    if (v.sgn == SC_ZERO)
332712854Sgabeblack@google.com        return sc_signed(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud, false);
332812854Sgabeblack@google.com
332912854Sgabeblack@google.com    // other cases
333012854Sgabeblack@google.com    return xor_signed_friend(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud,
333112854Sgabeblack@google.com                             v.sgn, v.nbits, v.ndigits, v.digit);
333212854Sgabeblack@google.com}
333312854Sgabeblack@google.com
333412854Sgabeblack@google.com
333512854Sgabeblack@google.comsc_signed
333612854Sgabeblack@google.comoperator ^ (const sc_signed &u, long v)
333712854Sgabeblack@google.com{
333812854Sgabeblack@google.com    if (v == 0) // case 1
333912854Sgabeblack@google.com        return sc_signed(u);
334012854Sgabeblack@google.com
334112854Sgabeblack@google.com    CONVERT_LONG(v);
334212854Sgabeblack@google.com
334312854Sgabeblack@google.com    if (u.sgn == SC_ZERO) // case 2
334412854Sgabeblack@google.com        return sc_signed(vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd, false);
334512854Sgabeblack@google.com
334612854Sgabeblack@google.com    // other cases
334712854Sgabeblack@google.com    return xor_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit,
334812854Sgabeblack@google.com                             vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd);
334912854Sgabeblack@google.com}
335012854Sgabeblack@google.com
335112854Sgabeblack@google.com
335212854Sgabeblack@google.comsc_signed
335312854Sgabeblack@google.comoperator ^ (long u, const sc_signed &v)
335412854Sgabeblack@google.com{
335512854Sgabeblack@google.com    if (u == 0)
335612854Sgabeblack@google.com        return sc_signed(v);
335712854Sgabeblack@google.com
335812854Sgabeblack@google.com    CONVERT_LONG(u);
335912854Sgabeblack@google.com
336012854Sgabeblack@google.com    if (v.sgn == SC_ZERO)
336112854Sgabeblack@google.com        return sc_signed(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud, false);
336212854Sgabeblack@google.com
336312854Sgabeblack@google.com    // other cases
336412854Sgabeblack@google.com    return xor_signed_friend(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud,
336512854Sgabeblack@google.com                             v.sgn, v.nbits, v.ndigits, v.digit);
336612854Sgabeblack@google.com}
336712854Sgabeblack@google.com
336812854Sgabeblack@google.com
336912854Sgabeblack@google.comsc_signed
337012854Sgabeblack@google.comoperator ^ (const sc_unsigned &u, long v)
337112854Sgabeblack@google.com{
337212854Sgabeblack@google.com    if (v == 0) // case 1
337312854Sgabeblack@google.com        return sc_signed(u);
337412854Sgabeblack@google.com
337512854Sgabeblack@google.com    CONVERT_LONG(v);
337612854Sgabeblack@google.com
337712854Sgabeblack@google.com    if (u.sgn == SC_ZERO) // case 2
337812854Sgabeblack@google.com        return sc_signed(vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd, false);
337912854Sgabeblack@google.com
338012854Sgabeblack@google.com    // other cases
338112854Sgabeblack@google.com    return xor_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit,
338212854Sgabeblack@google.com                             vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd);
338312854Sgabeblack@google.com}
338412854Sgabeblack@google.com
338512854Sgabeblack@google.com
338612854Sgabeblack@google.comsc_signed
338712854Sgabeblack@google.comoperator ^ (long u, const sc_unsigned &v)
338812854Sgabeblack@google.com{
338912854Sgabeblack@google.com    if (u == 0)
339012854Sgabeblack@google.com        return sc_signed(v);
339112854Sgabeblack@google.com
339212854Sgabeblack@google.com    CONVERT_LONG(u);
339312854Sgabeblack@google.com
339412854Sgabeblack@google.com    if (v.sgn == SC_ZERO)
339512854Sgabeblack@google.com        return sc_signed(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud, false);
339612854Sgabeblack@google.com
339712854Sgabeblack@google.com    // other cases
339812854Sgabeblack@google.com    return xor_signed_friend(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud,
339912854Sgabeblack@google.com                             v.sgn, v.nbits, v.ndigits, v.digit);
340012854Sgabeblack@google.com}
340112854Sgabeblack@google.com
340212854Sgabeblack@google.com
340312854Sgabeblack@google.comsc_signed
340412854Sgabeblack@google.comoperator ^ (const sc_signed &u, unsigned long v)
340512854Sgabeblack@google.com{
340612854Sgabeblack@google.com    if (v == 0) // case 1
340712854Sgabeblack@google.com        return sc_signed(u);
340812854Sgabeblack@google.com
340912854Sgabeblack@google.com    CONVERT_LONG(v);
341012854Sgabeblack@google.com
341112854Sgabeblack@google.com    if (u.sgn == SC_ZERO) // case 2
341212854Sgabeblack@google.com        return sc_signed(vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd, false);
341312854Sgabeblack@google.com
341412854Sgabeblack@google.com    // other cases
341512854Sgabeblack@google.com    return xor_signed_friend(u.sgn, u.nbits, u.ndigits, u.digit,
341612854Sgabeblack@google.com                             vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd);
341712854Sgabeblack@google.com}
341812854Sgabeblack@google.com
341912854Sgabeblack@google.comsc_signed
342012854Sgabeblack@google.comoperator ^ (unsigned long u, const sc_signed &v)
342112854Sgabeblack@google.com{
342212854Sgabeblack@google.com    if (u == 0)
342312854Sgabeblack@google.com        return sc_signed(v);
342412854Sgabeblack@google.com
342512854Sgabeblack@google.com    CONVERT_LONG(u);
342612854Sgabeblack@google.com
342712854Sgabeblack@google.com    if (v.sgn == SC_ZERO)
342812854Sgabeblack@google.com        return sc_signed(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud, false);
342912854Sgabeblack@google.com
343012854Sgabeblack@google.com    // other cases
343112854Sgabeblack@google.com    return xor_signed_friend(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud,
343212854Sgabeblack@google.com                             v.sgn, v.nbits, v.ndigits, v.digit);
343312854Sgabeblack@google.com}
343412854Sgabeblack@google.com
343512854Sgabeblack@google.com// The rest of the operators in this section are included from
343612854Sgabeblack@google.com// sc_nbcommon.cpp.
343712854Sgabeblack@google.com
343812854Sgabeblack@google.com
343912854Sgabeblack@google.com// ----------------------------------------------------------------------------
344012854Sgabeblack@google.com//  SECTION: Bitwise NOT operator: ~
344112854Sgabeblack@google.com// ----------------------------------------------------------------------------
344212854Sgabeblack@google.com
344312854Sgabeblack@google.com// Operators in this section are included from sc_nbcommon.cpp.
344412854Sgabeblack@google.com
344512854Sgabeblack@google.com
344612854Sgabeblack@google.com// ----------------------------------------------------------------------------
344712854Sgabeblack@google.com//  SECTION: LEFT SHIFT operators: <<, <<=
344812854Sgabeblack@google.com// ----------------------------------------------------------------------------
344912854Sgabeblack@google.com
345012854Sgabeblack@google.comsc_signed
345112854Sgabeblack@google.comoperator << (const sc_signed &u, const sc_unsigned &v)
345212854Sgabeblack@google.com{
345312854Sgabeblack@google.com    if (v.sgn == SC_ZERO)
345412854Sgabeblack@google.com        return sc_signed(u);
345512854Sgabeblack@google.com
345612854Sgabeblack@google.com    return operator << (u, v.to_ulong());
345712854Sgabeblack@google.com}
345812854Sgabeblack@google.com
345912854Sgabeblack@google.com// The rest of the operators in this section are included from
346012854Sgabeblack@google.com// sc_nbcommon.cpp.
346112854Sgabeblack@google.com
346212854Sgabeblack@google.com
346312854Sgabeblack@google.com// ----------------------------------------------------------------------------
346412854Sgabeblack@google.com//  SECTION: RIGHT SHIFT operators: >>, >>=
346512854Sgabeblack@google.com// ----------------------------------------------------------------------------
346612854Sgabeblack@google.com
346712854Sgabeblack@google.comsc_signed
346812854Sgabeblack@google.comoperator >> (const sc_signed &u, const sc_unsigned &v)
346912854Sgabeblack@google.com{
347012854Sgabeblack@google.com    if (v.sgn == SC_ZERO)
347112854Sgabeblack@google.com        return sc_signed(u);
347212854Sgabeblack@google.com
347312854Sgabeblack@google.com    return operator >> (u, v.to_ulong());
347412854Sgabeblack@google.com}
347512854Sgabeblack@google.com
347612854Sgabeblack@google.com// The rest of the operators in this section are included from
347712854Sgabeblack@google.com// sc_nbcommon.cpp.
347812854Sgabeblack@google.com
347912854Sgabeblack@google.com
348012854Sgabeblack@google.com// ----------------------------------------------------------------------------
348112854Sgabeblack@google.com//  SECTION: Unary arithmetic operators.
348212854Sgabeblack@google.com// ----------------------------------------------------------------------------
348312854Sgabeblack@google.com
348412854Sgabeblack@google.comsc_signed
348512854Sgabeblack@google.comoperator + (const sc_signed &u)
348612854Sgabeblack@google.com{
348712854Sgabeblack@google.com    return sc_signed(u);
348812854Sgabeblack@google.com}
348912854Sgabeblack@google.com
349012854Sgabeblack@google.comsc_signed
349112854Sgabeblack@google.comoperator - (const sc_signed &u)
349212854Sgabeblack@google.com{
349312854Sgabeblack@google.com    return sc_signed(u, -u.sgn);
349412854Sgabeblack@google.com}
349512854Sgabeblack@google.com
349612854Sgabeblack@google.comsc_signed
349712854Sgabeblack@google.comoperator - (const sc_unsigned &u)
349812854Sgabeblack@google.com{
349912854Sgabeblack@google.com    return sc_signed(u, -u.sgn);
350012854Sgabeblack@google.com}
350112854Sgabeblack@google.com
350212854Sgabeblack@google.com
350312854Sgabeblack@google.com// ----------------------------------------------------------------------------
350412854Sgabeblack@google.com//    SECTION: EQUAL operator: ==
350512854Sgabeblack@google.com// ----------------------------------------------------------------------------
350612854Sgabeblack@google.com
350712854Sgabeblack@google.combool
350812854Sgabeblack@google.comoperator == (const sc_signed &u, const sc_signed &v)
350912854Sgabeblack@google.com{
351012854Sgabeblack@google.com    if (u.sgn != v.sgn)
351112854Sgabeblack@google.com        return false;
351212854Sgabeblack@google.com
351312854Sgabeblack@google.com    if (&u == &v)
351412854Sgabeblack@google.com        return true;
351512854Sgabeblack@google.com
351612854Sgabeblack@google.com    if (vec_skip_and_cmp(u.ndigits, u.digit, v.ndigits, v.digit) != 0)
351712854Sgabeblack@google.com        return false;
351812854Sgabeblack@google.com
351912854Sgabeblack@google.com    return true;
352012854Sgabeblack@google.com}
352112854Sgabeblack@google.com
352212854Sgabeblack@google.com
352312854Sgabeblack@google.combool
352412854Sgabeblack@google.comoperator == (const sc_signed &u, int64 v)
352512854Sgabeblack@google.com{
352612854Sgabeblack@google.com    CONVERT_INT64(v);
352712854Sgabeblack@google.com
352812854Sgabeblack@google.com    if (u.sgn != vs)
352912854Sgabeblack@google.com        return false;
353012854Sgabeblack@google.com
353112854Sgabeblack@google.com    if (vec_skip_and_cmp(u.ndigits, u.digit, DIGITS_PER_INT64, vd) != 0)
353212854Sgabeblack@google.com        return false;
353312854Sgabeblack@google.com
353412854Sgabeblack@google.com    return true;
353512854Sgabeblack@google.com}
353612854Sgabeblack@google.com
353712854Sgabeblack@google.com
353812854Sgabeblack@google.combool
353912854Sgabeblack@google.comoperator == (int64 u, const sc_signed &v)
354012854Sgabeblack@google.com{
354112854Sgabeblack@google.com    CONVERT_INT64(u);
354212854Sgabeblack@google.com
354312854Sgabeblack@google.com    if (us != v.sgn)
354412854Sgabeblack@google.com        return false;
354512854Sgabeblack@google.com
354612854Sgabeblack@google.com    if (vec_skip_and_cmp(DIGITS_PER_INT64, ud, v.ndigits, v.digit) != 0)
354712854Sgabeblack@google.com        return false;
354812854Sgabeblack@google.com
354912854Sgabeblack@google.com    return true;
355012854Sgabeblack@google.com}
355112854Sgabeblack@google.com
355212854Sgabeblack@google.com
355312854Sgabeblack@google.combool
355412854Sgabeblack@google.comoperator == (const sc_signed &u, uint64 v)
355512854Sgabeblack@google.com{
355612854Sgabeblack@google.com    CONVERT_INT64(v);
355712854Sgabeblack@google.com
355812854Sgabeblack@google.com    if (u.sgn != vs)
355912854Sgabeblack@google.com        return false;
356012854Sgabeblack@google.com
356112854Sgabeblack@google.com    if (vec_skip_and_cmp(u.ndigits, u.digit, DIGITS_PER_INT64, vd) != 0)
356212854Sgabeblack@google.com        return false;
356312854Sgabeblack@google.com
356412854Sgabeblack@google.com    return true;
356512854Sgabeblack@google.com}
356612854Sgabeblack@google.com
356712854Sgabeblack@google.com
356812854Sgabeblack@google.combool
356912854Sgabeblack@google.comoperator == (uint64 u, const sc_signed &v)
357012854Sgabeblack@google.com{
357112854Sgabeblack@google.com    CONVERT_INT64(u);
357212854Sgabeblack@google.com
357312854Sgabeblack@google.com    if (us != v.sgn)
357412854Sgabeblack@google.com        return false;
357512854Sgabeblack@google.com
357612854Sgabeblack@google.com    if (vec_skip_and_cmp(DIGITS_PER_INT64, ud, v.ndigits, v.digit) != 0)
357712854Sgabeblack@google.com        return false;
357812854Sgabeblack@google.com
357912854Sgabeblack@google.com    return true;
358012854Sgabeblack@google.com}
358112854Sgabeblack@google.com
358212854Sgabeblack@google.com
358312854Sgabeblack@google.combool
358412854Sgabeblack@google.comoperator == (const sc_signed &u, long v)
358512854Sgabeblack@google.com{
358612854Sgabeblack@google.com    CONVERT_LONG(v);
358712854Sgabeblack@google.com
358812854Sgabeblack@google.com    if (u.sgn != vs)
358912854Sgabeblack@google.com        return false;
359012854Sgabeblack@google.com
359112854Sgabeblack@google.com    if (vec_skip_and_cmp(u.ndigits, u.digit, DIGITS_PER_LONG, vd) != 0)
359212854Sgabeblack@google.com        return false;
359312854Sgabeblack@google.com
359412854Sgabeblack@google.com    return true;
359512854Sgabeblack@google.com}
359612854Sgabeblack@google.com
359712854Sgabeblack@google.com
359812854Sgabeblack@google.combool
359912854Sgabeblack@google.comoperator == (long u, const sc_signed &v)
360012854Sgabeblack@google.com{
360112854Sgabeblack@google.com    CONVERT_LONG(u);
360212854Sgabeblack@google.com
360312854Sgabeblack@google.com    if (us != v.sgn)
360412854Sgabeblack@google.com        return false;
360512854Sgabeblack@google.com
360612854Sgabeblack@google.com    if (vec_skip_and_cmp(DIGITS_PER_LONG, ud, v.ndigits, v.digit) != 0)
360712854Sgabeblack@google.com        return false;
360812854Sgabeblack@google.com
360912854Sgabeblack@google.com    return true;
361012854Sgabeblack@google.com}
361112854Sgabeblack@google.com
361212854Sgabeblack@google.com
361312854Sgabeblack@google.combool
361412854Sgabeblack@google.comoperator == (const sc_signed &u, unsigned long v)
361512854Sgabeblack@google.com{
361612854Sgabeblack@google.com    CONVERT_LONG(v);
361712854Sgabeblack@google.com
361812854Sgabeblack@google.com    if (u.sgn != vs)
361912854Sgabeblack@google.com        return false;
362012854Sgabeblack@google.com
362112854Sgabeblack@google.com    if (vec_skip_and_cmp(u.ndigits, u.digit, DIGITS_PER_LONG, vd) != 0)
362212854Sgabeblack@google.com        return false;
362312854Sgabeblack@google.com
362412854Sgabeblack@google.com    return true;
362512854Sgabeblack@google.com}
362612854Sgabeblack@google.com
362712854Sgabeblack@google.com
362812854Sgabeblack@google.combool
362912854Sgabeblack@google.comoperator == (unsigned long u, const sc_signed &v)
363012854Sgabeblack@google.com{
363112854Sgabeblack@google.com    CONVERT_LONG(u);
363212854Sgabeblack@google.com
363312854Sgabeblack@google.com    if (us != v.sgn)
363412854Sgabeblack@google.com        return false;
363512854Sgabeblack@google.com
363612854Sgabeblack@google.com    if (vec_skip_and_cmp(DIGITS_PER_LONG, ud, v.ndigits, v.digit) != 0)
363712854Sgabeblack@google.com        return false;
363812854Sgabeblack@google.com
363912854Sgabeblack@google.com    return true;
364012854Sgabeblack@google.com}
364112854Sgabeblack@google.com
364212854Sgabeblack@google.com
364312854Sgabeblack@google.com// ----------------------------------------------------------------------------
364412854Sgabeblack@google.com//    SECTION: NOT_EQUAL operator: !=
364512854Sgabeblack@google.com// ----------------------------------------------------------------------------
364612854Sgabeblack@google.com
364712854Sgabeblack@google.com// Operators in this section are included from sc_nbcommon.cpp.
364812854Sgabeblack@google.com
364912854Sgabeblack@google.com
365012854Sgabeblack@google.com// ----------------------------------------------------------------------------
365112854Sgabeblack@google.com//    SECTION: LESS THAN operator: <
365212854Sgabeblack@google.com// ----------------------------------------------------------------------------
365312854Sgabeblack@google.com
365412854Sgabeblack@google.combool
365512854Sgabeblack@google.comoperator < (const sc_signed &u, const sc_signed &v)
365612854Sgabeblack@google.com{
365712854Sgabeblack@google.com    if (u.sgn < v.sgn)
365812854Sgabeblack@google.com        return true;
365912854Sgabeblack@google.com
366012854Sgabeblack@google.com    if (u.sgn > v.sgn)
366112854Sgabeblack@google.com        return false;
366212854Sgabeblack@google.com
366312854Sgabeblack@google.com    // u.sgn == v.sgn
366412854Sgabeblack@google.com
366512854Sgabeblack@google.com    if (&u == &v)
366612854Sgabeblack@google.com        return false;
366712854Sgabeblack@google.com
366812854Sgabeblack@google.com    if (u.sgn == SC_POS) {
366912854Sgabeblack@google.com        if (vec_skip_and_cmp(u.ndigits, u.digit, v.ndigits, v.digit) < 0)
367012854Sgabeblack@google.com            return true;
367112854Sgabeblack@google.com    } else if (u.sgn == SC_NEG) {
367212854Sgabeblack@google.com        if (vec_skip_and_cmp(u.ndigits, u.digit, v.ndigits, v.digit) > 0)
367312854Sgabeblack@google.com            return true;
367412854Sgabeblack@google.com    }
367512854Sgabeblack@google.com
367612854Sgabeblack@google.com    return false;
367712854Sgabeblack@google.com}
367812854Sgabeblack@google.com
367912854Sgabeblack@google.com
368012854Sgabeblack@google.combool
368112854Sgabeblack@google.comoperator < (const sc_signed &u, int64 v)
368212854Sgabeblack@google.com{
368312854Sgabeblack@google.com    CONVERT_INT64(v);
368412854Sgabeblack@google.com
368512854Sgabeblack@google.com    if (u.sgn < vs)
368612854Sgabeblack@google.com        return true;
368712854Sgabeblack@google.com
368812854Sgabeblack@google.com    if (u.sgn > vs)
368912854Sgabeblack@google.com        return false;
369012854Sgabeblack@google.com
369112854Sgabeblack@google.com    // u.sgn == vs
369212854Sgabeblack@google.com
369312854Sgabeblack@google.com    if (vs == SC_POS) {
369412854Sgabeblack@google.com        if (vec_skip_and_cmp(u.ndigits, u.digit, DIGITS_PER_INT64, vd) < 0)
369512854Sgabeblack@google.com            return true;
369612854Sgabeblack@google.com    } else if (vs == SC_NEG) {
369712854Sgabeblack@google.com        if (vec_skip_and_cmp(u.ndigits, u.digit, DIGITS_PER_INT64, vd) > 0)
369812854Sgabeblack@google.com            return true;
369912854Sgabeblack@google.com    }
370012854Sgabeblack@google.com
370112854Sgabeblack@google.com    return false;
370212854Sgabeblack@google.com}
370312854Sgabeblack@google.com
370412854Sgabeblack@google.com
370512854Sgabeblack@google.combool
370612854Sgabeblack@google.comoperator < (int64 u, const sc_signed &v)
370712854Sgabeblack@google.com{
370812854Sgabeblack@google.com    CONVERT_INT64(u);
370912854Sgabeblack@google.com
371012854Sgabeblack@google.com    if (us < v.sgn)
371112854Sgabeblack@google.com        return true;
371212854Sgabeblack@google.com
371312854Sgabeblack@google.com    if (us > v.sgn)
371412854Sgabeblack@google.com        return false;
371512854Sgabeblack@google.com
371612854Sgabeblack@google.com    // us == v.sgn
371712854Sgabeblack@google.com
371812854Sgabeblack@google.com    if (us == SC_POS) {
371912854Sgabeblack@google.com        if (vec_skip_and_cmp(DIGITS_PER_INT64, ud, v.ndigits, v.digit) < 0)
372012854Sgabeblack@google.com            return true;
372112854Sgabeblack@google.com    } else if (us == SC_NEG) {
372212854Sgabeblack@google.com        if (vec_skip_and_cmp(DIGITS_PER_INT64, ud, v.ndigits, v.digit) > 0)
372312854Sgabeblack@google.com            return true;
372412854Sgabeblack@google.com    }
372512854Sgabeblack@google.com
372612854Sgabeblack@google.com    return false;
372712854Sgabeblack@google.com}
372812854Sgabeblack@google.com
372912854Sgabeblack@google.com
373012854Sgabeblack@google.combool
373112854Sgabeblack@google.comoperator < (const sc_signed &u, uint64 v)
373212854Sgabeblack@google.com{
373312854Sgabeblack@google.com    CONVERT_INT64(v);
373412854Sgabeblack@google.com
373512854Sgabeblack@google.com    if (u.sgn < vs)
373612854Sgabeblack@google.com        return true;
373712854Sgabeblack@google.com
373812854Sgabeblack@google.com    if (u.sgn > vs)
373912854Sgabeblack@google.com        return false;
374012854Sgabeblack@google.com
374112854Sgabeblack@google.com    // u.sgn == vs
374212854Sgabeblack@google.com
374312854Sgabeblack@google.com    if (vs == SC_POS) {
374412854Sgabeblack@google.com        if (vec_skip_and_cmp(u.ndigits, u.digit, DIGITS_PER_INT64, vd) < 0)
374512854Sgabeblack@google.com            return true;
374612854Sgabeblack@google.com    }
374712854Sgabeblack@google.com
374812854Sgabeblack@google.com    return false;
374912854Sgabeblack@google.com}
375012854Sgabeblack@google.com
375112854Sgabeblack@google.com
375212854Sgabeblack@google.combool
375312854Sgabeblack@google.comoperator < (uint64 u, const sc_signed &v)
375412854Sgabeblack@google.com{
375512854Sgabeblack@google.com    CONVERT_INT64(u);
375612854Sgabeblack@google.com
375712854Sgabeblack@google.com    if (us < v.sgn)
375812854Sgabeblack@google.com        return true;
375912854Sgabeblack@google.com
376012854Sgabeblack@google.com    if (us > v.sgn)
376112854Sgabeblack@google.com        return false;
376212854Sgabeblack@google.com
376312854Sgabeblack@google.com    // us == v.sgn
376412854Sgabeblack@google.com
376512854Sgabeblack@google.com    if (us == SC_POS) {
376612854Sgabeblack@google.com        if (vec_skip_and_cmp(DIGITS_PER_INT64, ud, v.ndigits, v.digit) < 0)
376712854Sgabeblack@google.com            return true;
376812854Sgabeblack@google.com    }
376912854Sgabeblack@google.com
377012854Sgabeblack@google.com    return false;
377112854Sgabeblack@google.com}
377212854Sgabeblack@google.com
377312854Sgabeblack@google.com
377412854Sgabeblack@google.combool
377512854Sgabeblack@google.comoperator < (const sc_signed &u, long v)
377612854Sgabeblack@google.com{
377712854Sgabeblack@google.com    CONVERT_LONG(v);
377812854Sgabeblack@google.com
377912854Sgabeblack@google.com    if (u.sgn < vs)
378012854Sgabeblack@google.com        return true;
378112854Sgabeblack@google.com
378212854Sgabeblack@google.com    if (u.sgn > vs)
378312854Sgabeblack@google.com        return false;
378412854Sgabeblack@google.com
378512854Sgabeblack@google.com    // u.sgn == vs
378612854Sgabeblack@google.com
378712854Sgabeblack@google.com    if (vs == SC_POS) {
378812854Sgabeblack@google.com        if (vec_skip_and_cmp(u.ndigits, u.digit, DIGITS_PER_LONG, vd) < 0)
378912854Sgabeblack@google.com            return true;
379012854Sgabeblack@google.com
379112854Sgabeblack@google.com    } else if (vs == SC_NEG) {
379212854Sgabeblack@google.com        if (vec_skip_and_cmp(u.ndigits, u.digit, DIGITS_PER_LONG, vd) > 0)
379312854Sgabeblack@google.com            return true;
379412854Sgabeblack@google.com    }
379512854Sgabeblack@google.com
379612854Sgabeblack@google.com    return false;
379712854Sgabeblack@google.com}
379812854Sgabeblack@google.com
379912854Sgabeblack@google.com
380012854Sgabeblack@google.combool
380112854Sgabeblack@google.comoperator < (long u, const sc_signed &v)
380212854Sgabeblack@google.com{
380312854Sgabeblack@google.com    CONVERT_LONG(u);
380412854Sgabeblack@google.com
380512854Sgabeblack@google.com    if (us < v.sgn)
380612854Sgabeblack@google.com        return true;
380712854Sgabeblack@google.com
380812854Sgabeblack@google.com    if (us > v.sgn)
380912854Sgabeblack@google.com        return false;
381012854Sgabeblack@google.com
381112854Sgabeblack@google.com    // us == v.sgn
381212854Sgabeblack@google.com
381312854Sgabeblack@google.com    if (us == SC_POS) {
381412854Sgabeblack@google.com        if (vec_skip_and_cmp(DIGITS_PER_LONG, ud, v.ndigits, v.digit) < 0)
381512854Sgabeblack@google.com            return true;
381612854Sgabeblack@google.com    } else if (us == SC_NEG) {
381712854Sgabeblack@google.com        if (vec_skip_and_cmp(DIGITS_PER_LONG, ud, v.ndigits, v.digit) > 0)
381812854Sgabeblack@google.com            return true;
381912854Sgabeblack@google.com    }
382012854Sgabeblack@google.com
382112854Sgabeblack@google.com    return false;
382212854Sgabeblack@google.com}
382312854Sgabeblack@google.com
382412854Sgabeblack@google.com
382512854Sgabeblack@google.combool
382612854Sgabeblack@google.comoperator < (const sc_signed &u, unsigned long v)
382712854Sgabeblack@google.com{
382812854Sgabeblack@google.com    CONVERT_LONG(v);
382912854Sgabeblack@google.com
383012854Sgabeblack@google.com    if (u.sgn < vs)
383112854Sgabeblack@google.com        return true;
383212854Sgabeblack@google.com
383312854Sgabeblack@google.com    if (u.sgn > vs)
383412854Sgabeblack@google.com        return false;
383512854Sgabeblack@google.com
383612854Sgabeblack@google.com    // u.sgn == vs
383712854Sgabeblack@google.com
383812854Sgabeblack@google.com    if (vs == SC_POS) {
383912854Sgabeblack@google.com        if (vec_skip_and_cmp(u.ndigits, u.digit, DIGITS_PER_LONG, vd) < 0)
384012854Sgabeblack@google.com            return true;
384112854Sgabeblack@google.com    }
384212854Sgabeblack@google.com
384312854Sgabeblack@google.com    return false;
384412854Sgabeblack@google.com}
384512854Sgabeblack@google.com
384612854Sgabeblack@google.com
384712854Sgabeblack@google.combool
384812854Sgabeblack@google.comoperator < (unsigned long u, const sc_signed &v)
384912854Sgabeblack@google.com{
385012854Sgabeblack@google.com    CONVERT_LONG(u);
385112854Sgabeblack@google.com
385212854Sgabeblack@google.com    if (us < v.sgn)
385312854Sgabeblack@google.com        return true;
385412854Sgabeblack@google.com
385512854Sgabeblack@google.com    if (us > v.sgn)
385612854Sgabeblack@google.com        return false;
385712854Sgabeblack@google.com
385812854Sgabeblack@google.com    // us == v.sgn
385912854Sgabeblack@google.com
386012854Sgabeblack@google.com    if (us == SC_POS) {
386112854Sgabeblack@google.com        if (vec_skip_and_cmp(DIGITS_PER_LONG, ud, v.ndigits, v.digit) < 0)
386212854Sgabeblack@google.com            return true;
386312854Sgabeblack@google.com    }
386412854Sgabeblack@google.com
386512854Sgabeblack@google.com    return false;
386612854Sgabeblack@google.com}
386712854Sgabeblack@google.com
386812854Sgabeblack@google.com
386912854Sgabeblack@google.com// ---------------------------------------------------------------------------
387012854Sgabeblack@google.com//    SECTION: LESS THAN or EQUAL operator: <=
387112854Sgabeblack@google.com// ---------------------------------------------------------------------------
387212854Sgabeblack@google.com
387312854Sgabeblack@google.com// Operators in this section are included from sc_nbcommon.cpp.
387412854Sgabeblack@google.com
387512854Sgabeblack@google.com
387612854Sgabeblack@google.com// ---------------------------------------------------------------------------
387712854Sgabeblack@google.com//    SECTION: GREATER THAN operator: >
387812854Sgabeblack@google.com// ---------------------------------------------------------------------------
387912854Sgabeblack@google.com
388012854Sgabeblack@google.com// Operators in this section are included from sc_nbcommon.cpp.
388112854Sgabeblack@google.com
388212854Sgabeblack@google.com
388312854Sgabeblack@google.com// ---------------------------------------------------------------------------
388412854Sgabeblack@google.com//    SECTION: GREATER THAN or EQUAL operator: >=
388512854Sgabeblack@google.com// ---------------------------------------------------------------------------
388612854Sgabeblack@google.com
388712854Sgabeblack@google.com// Operators in this section are included from sc_nbcommon.cpp.
388812854Sgabeblack@google.com
388912854Sgabeblack@google.com
389012854Sgabeblack@google.com// ---------------------------------------------------------------------------
389112854Sgabeblack@google.com//    SECTION: Public members - Other utils.
389212854Sgabeblack@google.com// ---------------------------------------------------------------------------
389312854Sgabeblack@google.com
389412854Sgabeblack@google.combool
389512854Sgabeblack@google.comsc_signed::iszero() const
389612854Sgabeblack@google.com{
389712854Sgabeblack@google.com    if (sgn == SC_ZERO)
389812854Sgabeblack@google.com        return true;
389912854Sgabeblack@google.com    else if (sgn != SC_NOSIGN)
390012854Sgabeblack@google.com        return false;
390112854Sgabeblack@google.com    else
390212854Sgabeblack@google.com        return check_for_zero(ndigits, digit);
390312854Sgabeblack@google.com}
390412854Sgabeblack@google.com
390512854Sgabeblack@google.com
390612854Sgabeblack@google.combool
390712854Sgabeblack@google.comsc_signed::sign() const
390812854Sgabeblack@google.com{
390912854Sgabeblack@google.com    if (sgn == SC_NEG)
391012854Sgabeblack@google.com        return 1;
391112854Sgabeblack@google.com    else if (sgn != SC_NOSIGN)
391212854Sgabeblack@google.com        return 0;
391312854Sgabeblack@google.com    else
391412854Sgabeblack@google.com        return ((digit[ndigits - 1] & one_and_zeros(bit_ord(nbits - 1))) != 0);
391512854Sgabeblack@google.com}
391612854Sgabeblack@google.com
391712854Sgabeblack@google.com// The rest of the utils in this section are included from sc_nbcommon.cpp.
391812854Sgabeblack@google.com
391912854Sgabeblack@google.com
392012854Sgabeblack@google.com// ----------------------------------------------------------------------------
392112854Sgabeblack@google.com//    SECTION: Private members.
392212854Sgabeblack@google.com// ----------------------------------------------------------------------------
392312854Sgabeblack@google.com
392412854Sgabeblack@google.com// The private members in this section are included from sc_nbcommon.cpp.
392512854Sgabeblack@google.com
392612854Sgabeblack@google.com#define CLASS_TYPE sc_signed
392712854Sgabeblack@google.com#define CLASS_TYPE_STR "sc_signed"
392812854Sgabeblack@google.com
392912854Sgabeblack@google.com#define ADD_HELPER add_signed_friend
393012854Sgabeblack@google.com#define SUB_HELPER sub_signed_friend
393112854Sgabeblack@google.com#define MUL_HELPER mul_signed_friend
393212854Sgabeblack@google.com#define DIV_HELPER div_signed_friend
393312854Sgabeblack@google.com#define MOD_HELPER mod_signed_friend
393412854Sgabeblack@google.com#define AND_HELPER and_signed_friend
393512854Sgabeblack@google.com#define OR_HELPER or_signed_friend
393612854Sgabeblack@google.com#define XOR_HELPER xor_signed_friend
393712854Sgabeblack@google.com
393812854Sgabeblack@google.com#include "sc_nbfriends.inc"
393912854Sgabeblack@google.com
394012854Sgabeblack@google.com#undef SC_UNSIGNED
394112854Sgabeblack@google.com#define SC_SIGNED
394212854Sgabeblack@google.com#define IF_SC_SIGNED 1 // 1 = sc_signed
394312854Sgabeblack@google.com#define CLASS_TYPE_SUBREF sc_signed_subref_r
394412854Sgabeblack@google.com#define OTHER_CLASS_TYPE sc_unsigned
394512854Sgabeblack@google.com#define OTHER_CLASS_TYPE_SUBREF sc_unsigned_subref_r
394612854Sgabeblack@google.com
394712854Sgabeblack@google.com#define MUL_ON_HELPER mul_on_help_signed
394812854Sgabeblack@google.com#define DIV_ON_HELPER div_on_help_signed
394912854Sgabeblack@google.com#define MOD_ON_HELPER mod_on_help_signed
395012854Sgabeblack@google.com
395112854Sgabeblack@google.com#include "sc_nbcommon.inc"
395212854Sgabeblack@google.com
395312854Sgabeblack@google.com#undef MOD_ON_HELPER
395412854Sgabeblack@google.com#undef DIV_ON_HELPER
395512854Sgabeblack@google.com#undef MUL_ON_HELPER
395612854Sgabeblack@google.com
395712854Sgabeblack@google.com#undef OTHER_CLASS_TYPE_SUBREF
395812854Sgabeblack@google.com#undef OTHER_CLASS_TYPE
395912854Sgabeblack@google.com#undef CLASS_TYPE_SUBREF
396012854Sgabeblack@google.com#undef IF_SC_SIGNED
396112854Sgabeblack@google.com#undef SC_SIGNED
396212854Sgabeblack@google.com
396312854Sgabeblack@google.com#undef XOR_HELPER
396412854Sgabeblack@google.com#undef OR_HELPER
396512854Sgabeblack@google.com#undef AND_HELPER
396612854Sgabeblack@google.com#undef MOD_HELPER
396712854Sgabeblack@google.com#undef DIV_HELPER
396812854Sgabeblack@google.com#undef MUL_HELPER
396912854Sgabeblack@google.com#undef SUB_HELPER
397012854Sgabeblack@google.com#undef ADD_HELPER
397112854Sgabeblack@google.com
397212854Sgabeblack@google.com#undef CLASS_TYPE
397312854Sgabeblack@google.com#undef CLASS_TYPE_STR
397412854Sgabeblack@google.com
397512854Sgabeblack@google.com#include "sc_signed_bitref.inc"
397612854Sgabeblack@google.com#include "sc_signed_subref.inc"
397712854Sgabeblack@google.com
397812854Sgabeblack@google.com#undef CONVERT_LONG
397912854Sgabeblack@google.com#undef CONVERT_LONG_2
398012854Sgabeblack@google.com#undef CONVERT_INT64
398112854Sgabeblack@google.com#undef CONVERT_INT64_2
398212854Sgabeblack@google.com
398312854Sgabeblack@google.com} // namespace sc_dt
3984