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