sc_unsigned.cc revision 13322:7391057615bd
1/*****************************************************************************
2
3  Licensed to Accellera Systems Initiative Inc. (Accellera) under one or
4  more contributor license agreements.  See the NOTICE file distributed
5  with this work for additional information regarding copyright ownership.
6  Accellera licenses this file to you under the Apache License, Version 2.0
7  (the "License"); you may not use this file except in compliance with the
8  License.  You may obtain a copy of the License at
9
10    http://www.apache.org/licenses/LICENSE-2.0
11
12  Unless required by applicable law or agreed to in writing, software
13  distributed under the License is distributed on an "AS IS" BASIS,
14  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
15  implied.  See the License for the specific language governing
16  permissions and limitations under the License.
17
18 *****************************************************************************/
19
20/*****************************************************************************
21
22  sc_unsigned.cpp -- Arbitrary precision signed arithmetic.
23
24    This file includes the definitions of sc_unsigned_bitref,
25    sc_unsigned_subref, and sc_unsigned classes. The first two classes
26    are proxy classes to reference one bit and a range of bits of a
27    sc_unsigned number, respectively. This file also includes
28    sc_nbcommon.cpp and sc_nbfriends.cpp, which contain the
29    definitions shared by sc_unsigned.
30
31  Original Author: Ali Dasdan, Synopsys, Inc.
32
33 *****************************************************************************/
34
35/*****************************************************************************
36
37  MODIFICATION LOG - modifiers, enter your name, affiliation, date and
38  changes you are making here.
39
40      Name, Affiliation, Date:
41  Description of Modification:
42
43 *****************************************************************************/
44
45
46// $Log: sc_unsigned.cpp,v $
47// Revision 1.7  2011/02/18 20:19:15  acg
48//  Andy Goodrich: updating Copyright notice.
49//
50// Revision 1.6  2008/12/10 20:38:45  acg
51//  Andy Goodrich: fixed conversion of double values to the digits vector.
52//  The bits above the radix were not being masked off.
53//
54// Revision 1.5  2008/06/19 17:47:57  acg
55//  Andy Goodrich: fixes for bugs. See 2.2.1 RELEASENOTES.
56//
57// Revision 1.4  2008/06/19 16:57:57  acg
58//  Andy Goodrich: added case for negative unsigned values to the support in
59//  concate_get_data().
60//
61// Revision 1.3  2007/11/04 21:27:00  acg
62//  Andy Goodrich: changes to make sure the proper value is returned from
63//  concat_get_data().
64//
65// Revision 1.2  2007/02/22 21:35:05  acg
66//  Andy Goodrich: cleaned up comments in concat_get_ctrl and concat_get_data.
67//
68// Revision 1.1.1.1  2006/12/15 20:20:05  acg
69// SystemC 2.3
70//
71// Revision 1.4  2006/08/29 23:36:54  acg
72//  Andy Goodrich: fixed and_reduce and optimized or_reduce.
73//
74// Revision 1.3  2006/01/13 18:49:32  acg
75// Added $Log command so that CVS check in comments are reproduced in the
76// source.
77//
78
79#include <cctype>
80#include <cmath>
81#include <sstream>
82
83#include "systemc/ext/dt/bit/sc_bv_base.hh"
84#include "systemc/ext/dt/bit/sc_lv_base.hh"
85#include "systemc/ext/dt/fx/sc_ufix.hh"
86#include "systemc/ext/dt/fx/scfx_other_defs.hh"
87#include "systemc/ext/dt/int/sc_int_base.hh"
88#include "systemc/ext/dt/int/sc_signed.hh"
89#include "systemc/ext/dt/int/sc_uint_base.hh"
90#include "systemc/ext/dt/int/sc_unsigned.hh"
91#include "systemc/ext/dt/misc/sc_concatref.hh"
92#include "systemc/ext/utils/messages.hh"
93
94// explicit template instantiations
95namespace sc_core
96{
97
98template class sc_vpool<sc_dt::sc_unsigned_bitref>;
99template class sc_vpool<sc_dt::sc_unsigned_subref>;
100template class sc_vpool<sc_dt::sc_unsigned>;
101
102} // namespace sc_core
103
104namespace sc_dt
105{
106
107// Pool of temporary instances:
108//   The sc_unsigned pool is used by the concatenation support.
109//   The bit and part reference pools allow references to be returned.
110
111sc_core::sc_vpool<sc_unsigned> sc_unsigned::m_pool(8);
112sc_core::sc_vpool<sc_unsigned_bitref> sc_unsigned_bitref::m_pool(9);
113sc_core::sc_vpool<sc_unsigned_subref> sc_unsigned_subref::m_pool(9);
114
115
116void
117sc_unsigned::invalid_init(const char *type_name, int nb) const
118{
119    std::stringstream msg;
120    msg << "sc_unsigned("<< type_name << ") : nb = " << nb << " is not valid";
121    SC_REPORT_ERROR("initialization failed", msg.str().c_str());
122}
123
124
125// ----------------------------------------------------------------------------
126// SECTION: Public members - Invalid selections.
127// ----------------------------------------------------------------------------
128
129void
130sc_unsigned::invalid_index(int i) const
131{
132    std::stringstream msg;
133    msg << "sc_biguint bit selection: index = " << i << " violates "
134           "0 <= index <= " << (nbits-2);
135    SC_REPORT_ERROR(sc_core::SC_ID_OUT_OF_BOUNDS_, msg.str().c_str());
136    sc_core::sc_abort(); // can't recover from here
137}
138
139void
140sc_unsigned::invalid_range(int l, int r) const
141{
142    std::stringstream msg;
143    msg << "sc_biguint part selection: left = " <<
144           l << ", right = " << r << " \n"
145           "  violates either (" << (nbits - 2) << " >= left >= 0) or "
146           "(" << (nbits-2) << " >= right >= 0)";
147    SC_REPORT_ERROR(sc_core::SC_ID_OUT_OF_BOUNDS_, msg.str().c_str());
148    sc_core::sc_abort(); // can't recover from here
149}
150
151// ----------------------------------------------------------------------------
152//  SECTION: Public members - Concatenation support.
153// ----------------------------------------------------------------------------
154
155// Most public members are included from sc_nbcommon.inc. However, some
156// concatenation support appears here to optimize between the signed and
157// unsigned cases.
158
159
160
161// Insert this object's value at the specified place in a vector of big style
162// values.
163
164bool
165sc_unsigned::concat_get_ctrl(sc_digit *dst_p, int low_i) const
166{
167    int dst_i; // Index to next word to set in dst_p.
168    int end_i; // Index of high order word to set.
169    int left_shift; // Amount to shift value left.
170    sc_digit mask; // Mask for partial word sets.
171
172
173    // CALCULATE METRICS FOR DATA MOVEMENT:
174    dst_i = low_i / BITS_PER_DIGIT;
175    end_i = (low_i + nbits - 2) / BITS_PER_DIGIT;
176    left_shift = low_i % BITS_PER_DIGIT;
177
178    // MOVE FIRST WORD (IT MAY BE PARTIAL) AND THEN ANY OTHERS:
179    //
180    // We may "clobber" upper bits, but they will be written at some point
181    // anyway.
182
183    mask = ~(~0U << left_shift);
184    dst_p[dst_i] = (dst_p[dst_i] & ~mask);
185    dst_i++;
186
187    for (; dst_i <= end_i; dst_i++)
188        dst_p[dst_i] = 0;
189
190    return false;
191}
192
193bool
194sc_unsigned::concat_get_data(sc_digit *dst_p, int low_i) const
195{
196    sc_digit carry; // Carry for negating value.
197    int dst_i; // Index to next word to set in dst_p.
198    int end_i; // Index of high order word to set.
199    int high_i; // Index w/in word of high order bit.
200    int left_shift; // Amount to shift value left.
201    sc_digit left_word; // High word component for set.
202    sc_digit mask; // Mask for partial word sets.
203    bool result; // True if inserting non-zero data.
204    int right_shift; // Amount to shift value right.
205    sc_digit right_word; // Low word component for set.
206    int real_bits; // nbits - 1.
207    int src_i; // Index to next word to get from digit.
208
209    // CALCULATE METRICS FOR DATA MOVEMENT:
210    real_bits = nbits - 1; // Remove that extra sign bit.
211    dst_i = low_i / BITS_PER_DIGIT;
212    high_i = low_i + real_bits - 1;
213    end_i = high_i / BITS_PER_DIGIT;
214    left_shift = low_i % BITS_PER_DIGIT;
215
216    switch (sgn) {
217      // POSITIVE SOURCE VALUE:
218      case SC_POS:
219        result = true;
220
221        // ALL DATA TO BE MOVED IS IN A SINGLE WORD:
222        if (dst_i == end_i) {
223            mask = ~(~0U << left_shift);
224            dst_p[dst_i] = ((dst_p[dst_i] & mask) |
225                (digit[0] << left_shift)) & DIGIT_MASK;
226
227        // DATA IS IN MORE THAN ONE WORD, BUT IS WORD ALIGNED:
228        } else if (left_shift == 0) {
229            for (src_i = 0; dst_i < end_i; dst_i++, src_i++) {
230                dst_p[dst_i] = digit[src_i];
231            }
232            high_i = high_i % BITS_PER_DIGIT;
233            mask = ~(~1U << high_i) & DIGIT_MASK;
234            dst_p[dst_i] = digit[src_i] & mask;
235
236        // DATA IS IN MORE THAN ONE WORD, AND NOT WORD ALIGNED:
237        } else {
238            high_i = high_i % BITS_PER_DIGIT;
239            right_shift = BITS_PER_DIGIT - left_shift;
240            mask = ~(~0U << left_shift);
241            right_word = digit[0];
242            dst_p[dst_i] = (dst_p[dst_i] & mask) |
243                ((right_word << left_shift) & DIGIT_MASK);
244            for (src_i = 1, dst_i++; dst_i < end_i; dst_i++, src_i++) {
245                left_word = digit[src_i];
246                dst_p[dst_i] = ((left_word << left_shift) & DIGIT_MASK) |
247                    (right_word >> right_shift);
248                right_word = left_word;
249            }
250            left_word = (src_i < ndigits) ? digit[src_i] : 0;
251            mask = ~(~1U << high_i) & DIGIT_MASK;
252            dst_p[dst_i] = ((left_word << left_shift) |
253                (right_word >> right_shift)) & mask;
254        }
255        break;
256
257      // SOURCE VALUE IS NEGATIVE:
258      case SC_NEG:
259        // ALL DATA TO BE MOVED IS IN A SINGLE WORD:
260        result = true;
261        if (dst_i == end_i) {
262            mask = ~(~0U << nbits);
263            right_word = ((digit[0] ^ DIGIT_MASK) + 1) & mask;
264            mask = ~(~0U << left_shift);
265            dst_p[dst_i] = ((dst_p[dst_i] & mask) |
266                (right_word << left_shift)) & DIGIT_MASK;
267
268        // DATA IS IN MORE THAN ONE WORD, BUT IS WORD ALIGNED:
269
270        } else if (left_shift == 0) {
271            carry = 1;
272            for (src_i = 0; dst_i < end_i; dst_i++, src_i++) {
273                right_word = (digit[src_i] ^ DIGIT_MASK) + carry;
274                dst_p[dst_i] = right_word &  DIGIT_MASK;
275                carry = right_word >> BITS_PER_DIGIT;
276            }
277            high_i = high_i % BITS_PER_DIGIT;
278            mask = (~(~1U << high_i)) & DIGIT_MASK;
279            right_word = (src_i < ndigits) ?
280                (digit[src_i] ^ DIGIT_MASK) + carry : DIGIT_MASK + carry;
281            dst_p[dst_i] = right_word & mask;
282
283        // DATA IS IN MORE THAN ONE WORD, AND NOT WORD ALIGNED:
284        } else {
285            high_i = high_i % BITS_PER_DIGIT;
286            right_shift = BITS_PER_DIGIT - left_shift;
287            mask = ~(~0U << left_shift);
288            carry = 1;
289            right_word = (digit[0] ^ DIGIT_MASK) + carry;
290            dst_p[dst_i] = (dst_p[dst_i] & mask) |
291                ((right_word << left_shift) & DIGIT_MASK);
292            carry = right_word >> BITS_PER_DIGIT;
293            right_word &= DIGIT_MASK;
294            for (src_i = 1, dst_i++; dst_i < end_i; dst_i++, src_i++) {
295                left_word = (digit[src_i] ^ DIGIT_MASK) + carry;
296                dst_p[dst_i] = ((left_word << left_shift)&DIGIT_MASK) |
297                    (right_word >> right_shift);
298                carry = left_word >> BITS_PER_DIGIT;
299                right_word = left_word & DIGIT_MASK;
300            }
301            left_word = (src_i < ndigits) ?
302                (digit[src_i] ^ DIGIT_MASK) + carry : carry;
303            mask = ~(~1U << high_i) & DIGIT_MASK;
304            dst_p[dst_i] = ((left_word << left_shift) |
305                (right_word >> right_shift)) & mask;
306        }
307        break;
308      // VALUE IS ZERO:
309      default:
310        result = false;
311        // ALL DATA TO BE MOVED IS IN A SINGLE WORD:
312        if (dst_i == end_i) {
313            mask = ~(~0U << real_bits) << left_shift;
314            dst_p[dst_i] = dst_p[dst_i] & ~mask;
315
316        // DATA IS IN MORE THAN ONE WORD, BUT IS WORD ALIGNED:
317
318        } else if (left_shift == 0) {
319            for (src_i = 0; dst_i < end_i; dst_i++, src_i++) {
320                dst_p[dst_i] = 0;
321            }
322            dst_p[dst_i] = 0;
323
324        // DATA IS IN MORE THAN ONE WORD, AND NOT WORD ALIGNED:
325        } else {
326            mask = ~(~0U << left_shift);
327            dst_p[dst_i] = (dst_p[dst_i] & mask);
328            for (dst_i++; dst_i <= end_i; dst_i++) {
329                dst_p[dst_i] = 0;
330            }
331        }
332        break;
333    }
334    return result;
335}
336
337// Return this object instance's bits as a uint64 without sign extension.
338uint64
339sc_unsigned::concat_get_uint64() const
340{
341    uint64 result;
342
343    switch (sgn) {
344      case SC_POS:
345        result = 0;
346        if (ndigits > 2)
347            result = digit[2];
348        if (ndigits > 1)
349            result = (result << BITS_PER_DIGIT) | digit[1];
350        result = (result << BITS_PER_DIGIT) | digit[0];
351        break;
352      default:
353        result = 0;
354        break;
355    }
356    return result;
357}
358
359// #### OPTIMIZE
360void
361sc_unsigned::concat_set(int64 src, int low_i)
362{
363    *this = (low_i < 64) ? src >> low_i : src >> 63;
364}
365
366void
367sc_unsigned::concat_set(const sc_signed &src, int low_i)
368{
369    if (low_i < src.length())
370        *this = src >> low_i;
371    else
372        *this = (src < 0) ? (int_type)-1 : 0;
373}
374
375void
376sc_unsigned::concat_set(const sc_unsigned &src, int low_i)
377{
378    if (low_i < src.length())
379        *this = src >> low_i;
380    else
381        *this = 0;
382}
383
384void
385sc_unsigned::concat_set(uint64 src, int low_i)
386{
387    *this = (low_i < 64) ? src >> low_i : 0;
388}
389
390
391// ----------------------------------------------------------------------------
392//  SECTION: Public members - Reduction methods.
393// ----------------------------------------------------------------------------
394
395bool
396sc_unsigned::and_reduce() const
397{
398    int i; // Digit examining.
399
400    if (sgn == SC_ZERO)
401        return false;
402    for (i = 0; i < ndigits - 1; i++)
403        if ((digit[i] & DIGIT_MASK) != DIGIT_MASK)
404            return false;
405    if ((digit[i] & ~(~0U << ((nbits - 1) % BITS_PER_DIGIT))) ==
406         static_cast<sc_digit>(~(~0U << ((nbits - 1) % BITS_PER_DIGIT)))) {
407        return true;
408    }
409    return false;
410}
411
412bool
413sc_unsigned::or_reduce() const
414{
415        return (sgn == SC_ZERO) ? false : true;
416}
417
418bool
419sc_unsigned::xor_reduce() const
420{
421    int i; // Digit examining.
422    int odd; // Flag for odd number of digits.
423
424    odd = 0;
425    for (i = 0; i < nbits - 1; i++)
426        if (test(i))
427            odd = ~odd;
428    return odd ? true : false;
429}
430
431
432// ----------------------------------------------------------------------------
433//  SECTION: Public members - Assignment operators.
434// ----------------------------------------------------------------------------
435
436// assignment operators
437const sc_unsigned &
438sc_unsigned::operator = (const char *a)
439{
440    if (a == 0) {
441        SC_REPORT_ERROR("conversion failed",
442                        "character string is zero");
443    } else if (*a == 0) {
444        SC_REPORT_ERROR("conversion failed",
445                        "character string is empty");
446    } else try {
447        int len = length();
448        sc_ufix aa(a, len, len, SC_TRN, SC_WRAP, 0, SC_ON);
449        return this->operator = (aa);
450    } catch(const sc_core::sc_report &) {
451        std::stringstream msg;
452        msg << "character string '" << a << "' is not valid";
453        SC_REPORT_ERROR("conversion failed", msg.str().c_str());
454    }
455    return *this;
456}
457
458const sc_unsigned &
459sc_unsigned::operator = (int64 v)
460{
461    sgn = get_sign(v);
462    if (sgn == SC_ZERO) {
463        vec_zero(ndigits, digit);
464    } else {
465        from_uint(ndigits, digit, (uint64) v);
466        convert_SM_to_2C_to_SM();
467    }
468    return *this;
469}
470
471const sc_unsigned &
472sc_unsigned::operator = (uint64 v)
473{
474    if (v == 0) {
475        sgn = SC_ZERO;
476        vec_zero(ndigits, digit);
477    } else {
478        sgn = SC_POS;
479        from_uint(ndigits, digit, v);
480        convert_SM_to_2C_to_SM();
481    }
482    return *this;
483}
484
485const sc_unsigned &
486sc_unsigned::operator = (long v)
487{
488    sgn = get_sign(v);
489    if (sgn == SC_ZERO) {
490        vec_zero(ndigits, digit);
491    } else {
492        from_uint(ndigits, digit, (unsigned long)v);
493        convert_SM_to_2C_to_SM();
494    }
495    return *this;
496}
497
498const sc_unsigned &
499sc_unsigned::operator = (unsigned long v)
500{
501    if (v == 0) {
502        sgn = SC_ZERO;
503        vec_zero(ndigits, digit);
504    } else {
505        sgn = SC_POS;
506        from_uint(ndigits, digit, v);
507        convert_SM_to_2C_to_SM();
508    }
509    return *this;
510}
511
512const sc_unsigned &
513sc_unsigned::operator = (double v)
514{
515    is_bad_double(v);
516    sgn = SC_POS;
517    int i = 0;
518    while (std::floor(v) && (i < ndigits)) {
519        digit[i++] = ((sc_digit)std::floor(remainder(v, DIGIT_RADIX))) &
520            DIGIT_MASK;
521        v /= DIGIT_RADIX;
522    }
523    vec_zero(i, ndigits, digit);
524    convert_SM_to_2C_to_SM();
525    return *this;
526}
527
528
529// ----------------------------------------------------------------------------
530
531const sc_unsigned &
532sc_unsigned::operator = (const sc_bv_base &v)
533{
534    int minlen = sc_min(nbits, v.length());
535    int i = 0;
536    for (; i < minlen; ++i) {
537        safe_set(i, v.get_bit(i), digit);
538    }
539    for (; i < nbits; ++i) {
540        safe_set(i, 0, digit); // zero-extend
541    }
542    convert_2C_to_SM();
543    return *this;
544}
545
546const sc_unsigned &
547sc_unsigned::operator = (const sc_lv_base &v)
548{
549    int minlen = sc_min(nbits, v.length());
550    int i = 0;
551    for (; i < minlen; ++i) {
552        safe_set(i, sc_logic(v.get_bit(i)).to_bool(), digit);
553    }
554    for (; i < nbits; ++i) {
555        safe_set(i, 0, digit);  // zero-extend
556    }
557    convert_2C_to_SM();
558    return *this;
559}
560
561
562// explicit conversion to character string
563const std::string
564sc_unsigned::to_string(sc_numrep numrep) const
565{
566    int len = length();
567    sc_ufix aa(*this, len, len, SC_TRN, SC_WRAP, 0, SC_ON);
568    return aa.to_string(numrep);
569}
570
571const std::string
572sc_unsigned::to_string(sc_numrep numrep, bool w_prefix) const
573{
574    int len = length();
575    sc_ufix aa(*this, len, len, SC_TRN, SC_WRAP, 0, SC_ON);
576    return aa.to_string(numrep, w_prefix);
577}
578
579
580// ----------------------------------------------------------------------------
581//  SECTION: Interfacing with sc_int_base
582// ----------------------------------------------------------------------------
583
584const sc_unsigned &
585sc_unsigned::operator = (const sc_int_base &v)
586{
587    return operator = ((int64)v);
588}
589
590const sc_unsigned &
591sc_unsigned::operator += (const sc_int_base &v)
592{
593    return operator += ((int64)v);
594}
595
596const sc_unsigned &
597sc_unsigned::operator -= (const sc_int_base &v)
598{
599    return operator -= ((int64)v);
600}
601
602const sc_unsigned &
603sc_unsigned::operator *= (const sc_int_base &v)
604{
605    return operator *= ((int64)v);
606}
607
608const sc_unsigned &
609sc_unsigned::operator /= (const sc_int_base &v)
610{
611    return operator /= ((int64)v);
612}
613
614const sc_unsigned &
615sc_unsigned::operator %= (const sc_int_base &v)
616{
617    return operator %= ((int64)v);
618}
619
620const sc_unsigned &
621sc_unsigned::operator &= (const sc_int_base &v)
622{
623    return operator &= ((int64)v);
624}
625
626const sc_unsigned &
627sc_unsigned::operator |= (const sc_int_base &v)
628{
629    return operator |= ((int64)v);
630}
631
632const sc_unsigned &
633sc_unsigned::operator ^= (const sc_int_base &v)
634{
635    return operator ^= ((int64)v);
636}
637
638sc_unsigned
639operator << (const sc_unsigned &u, const sc_int_base &v)
640{
641    return operator << (u, (int64)v);
642}
643const sc_unsigned &
644sc_unsigned::operator <<= (const sc_int_base &v)
645{
646    return operator <<= ((int64)v);
647}
648
649sc_unsigned
650operator >> (const sc_unsigned&    u, const sc_int_base&  v)
651{
652    return operator >> (u, (int64)v);
653}
654const sc_unsigned &
655sc_unsigned::operator >>= (const sc_int_base&  v)
656{
657    return operator >>= ((int64)v);
658}
659
660bool
661operator == (const sc_unsigned &u, const sc_int_base &v)
662{
663    return operator == (u, (int64)v);
664}
665bool
666operator == (const sc_int_base &u, const sc_unsigned &v)
667{
668    return operator == ((int64)u, v);
669}
670
671bool
672operator != (const sc_unsigned &u, const sc_int_base &v)
673{
674    return operator != (u, (int64)v);
675}
676bool
677operator != (const sc_int_base &u, const sc_unsigned &v)
678{
679    return operator != ((int64)u, v);
680}
681
682bool
683operator < (const sc_unsigned &u, const sc_int_base &v)
684{
685    return operator < (u, (int64)v);
686}
687bool
688operator < (const sc_int_base &u, const sc_unsigned &v)
689{
690    return operator < ((int64)u, v);
691}
692
693bool
694operator <= (const sc_unsigned &u, const sc_int_base &v)
695{
696    return operator <= (u, (int64)v);
697}
698bool
699operator <= (const sc_int_base &u, const sc_unsigned &v)
700{
701    return operator <= ((int64)u, v);
702}
703
704bool
705operator > (const sc_unsigned &u, const sc_int_base &v)
706{
707    return operator > (u, (int64)v);
708}
709bool
710operator > (const sc_int_base &u, const sc_unsigned &v)
711{
712    return operator > ((int64)u, v);
713}
714
715bool
716operator >= (const sc_unsigned &u, const sc_int_base &v)
717{
718    return operator >= (u, (int64)v);
719}
720bool
721operator >= (const sc_int_base &u, const sc_unsigned &v)
722{
723    return operator >= ((int64)u, v);
724}
725
726
727// ----------------------------------------------------------------------------
728//  SECTION: Interfacing with sc_uint_base
729// ----------------------------------------------------------------------------
730
731const sc_unsigned &
732sc_unsigned::operator = (const sc_uint_base &v)
733{
734    return operator = ((uint64)v);
735}
736
737sc_unsigned
738operator + (const sc_unsigned &u, const sc_uint_base &v)
739{
740    return operator + (u, (uint64)v);
741}
742sc_unsigned
743operator + (const sc_uint_base &u, const sc_unsigned &v)
744{
745    return operator + ((uint64)u, v);
746}
747const sc_unsigned &
748sc_unsigned::operator += (const sc_uint_base &v)
749{
750    return operator += ((uint64)v);
751}
752
753const sc_unsigned &
754sc_unsigned::operator -= (const sc_uint_base &v)
755{
756    return operator -= ((uint64)v);
757}
758
759sc_unsigned
760operator * (const sc_unsigned &u, const sc_uint_base &v)
761{
762    return operator * (u, (uint64)v);
763}
764sc_unsigned
765operator * (const sc_uint_base &u, const sc_unsigned &v)
766{
767    return operator * ((uint64)u, v);
768}
769const sc_unsigned &
770sc_unsigned::operator *= (const sc_uint_base &v)
771{
772    return operator *= ((uint64)v);
773}
774
775sc_unsigned
776operator / (const sc_unsigned &u, const sc_uint_base &v)
777{
778    return operator / (u, (uint64)v);
779}
780sc_unsigned
781operator / (const sc_uint_base &u, const sc_unsigned &v)
782{
783    return operator / ((uint64)u, v);
784}
785const sc_unsigned &
786sc_unsigned::operator /= (const sc_uint_base &v)
787{
788    return operator /= ((uint64)v);
789}
790
791sc_unsigned
792operator % (const sc_unsigned &u, const sc_uint_base &v)
793{
794    return operator % (u, (uint64)v);
795}
796sc_unsigned
797operator % (const sc_uint_base &u, const sc_unsigned &v)
798{
799    return operator % ((uint64)u, v);
800}
801const sc_unsigned &
802sc_unsigned::operator %= (const sc_uint_base &v)
803{
804    return operator %= ((uint64)v);
805}
806
807sc_unsigned
808operator & (const sc_unsigned &u, const sc_uint_base &v)
809{
810    return operator & (u, (uint64)v);
811}
812sc_unsigned
813operator & (const sc_uint_base &u, const sc_unsigned &v)
814{
815    return operator & ((uint64)u, v);
816}
817const sc_unsigned &
818sc_unsigned::operator &= (const sc_uint_base &v)
819{
820    return operator &= ((uint64)v);
821}
822
823sc_unsigned
824operator | (const sc_unsigned &u, const sc_uint_base &v)
825{
826    return operator | (u, (uint64)v);
827}
828sc_unsigned
829operator | (const sc_uint_base &u, const sc_unsigned &v)
830{
831    return operator | ((uint64)u, v);
832}
833const sc_unsigned &
834sc_unsigned::operator |= (const sc_uint_base &v)
835{
836    return operator |= ((uint64)v);
837}
838
839sc_unsigned
840operator ^ (const sc_unsigned &u, const sc_uint_base &v)
841{
842    return operator ^ (u, (uint64)v);
843}
844sc_unsigned
845operator ^ (const sc_uint_base &u, const sc_unsigned &v)
846{
847    return operator ^ ((uint64)u, v);
848}
849const sc_unsigned &
850sc_unsigned::operator ^= (const sc_uint_base &v)
851{
852    return operator ^= ((uint64)v);
853}
854
855sc_unsigned
856operator << (const sc_unsigned &u, const sc_uint_base &v)
857{
858    return operator << (u, (uint64)v);
859}
860const sc_unsigned &
861sc_unsigned::operator <<= (const sc_uint_base &v)
862{
863    return operator <<= ((uint64)v);
864}
865
866sc_unsigned
867operator >> (const sc_unsigned &u, const sc_uint_base &v)
868{
869    return operator >> (u, (uint64)v);
870}
871const sc_unsigned &
872sc_unsigned::operator >>= (const sc_uint_base &v)
873{
874    return operator >>= ((uint64)v);
875}
876
877bool
878operator == (const sc_unsigned &u, const sc_uint_base &v)
879{
880    return operator == (u, (uint64)v);
881}
882bool
883operator == (const sc_uint_base &u, const sc_unsigned &v)
884{
885    return operator == ((uint64)u, v);
886}
887
888bool
889operator != (const sc_unsigned &u, const sc_uint_base &v)
890{
891    return operator != (u, (uint64)v);
892}
893bool
894operator != (const sc_uint_base &u, const sc_unsigned &v)
895{
896    return operator != ((uint64)u, v);
897}
898
899bool
900operator < (const sc_unsigned &u, const sc_uint_base &v)
901{
902    return operator < (u, (uint64)v);
903}
904bool
905operator < (const sc_uint_base &u, const sc_unsigned &v)
906{
907    return operator < ((uint64)u, v);
908}
909
910bool
911operator <= (const sc_unsigned &u, const sc_uint_base &v)
912{
913    return operator <= (u, (uint64)v);
914}
915bool
916operator <= (const sc_uint_base &u, const sc_unsigned &v)
917{
918    return operator <= ((uint64)u, v);
919}
920
921bool
922operator > (const sc_unsigned &u, const sc_uint_base &v)
923{
924    return operator > (u, (uint64)v);
925}
926bool
927operator > (const sc_uint_base &u, const sc_unsigned &v)
928{
929    return operator > ((uint64)u, v);
930}
931
932bool
933operator >= (const sc_unsigned &u, const sc_uint_base &v)
934{
935    return operator >= (u, (uint64)v);
936}
937bool
938operator >= (const sc_uint_base &u, const sc_unsigned &v)
939{
940    return operator >= ((uint64)u, v);
941}
942
943
944// ----------------------------------------------------------------------------
945//  SECTION: Input and output operators
946// ----------------------------------------------------------------------------
947
948// The operators in this section are included from sc_nbcommon.cpp.
949
950
951// ----------------------------------------------------------------------------
952//  SECTION: Operator macros.
953// ----------------------------------------------------------------------------
954
955#define CONVERT_LONG(u) \
956small_type u ## s = get_sign(u); \
957sc_digit u ## d[DIGITS_PER_ULONG]; \
958from_uint(DIGITS_PER_ULONG, u ## d, (unsigned long) u);
959
960#define CONVERT_LONG_2(u) \
961sc_digit u ## d[DIGITS_PER_ULONG]; \
962from_uint(DIGITS_PER_ULONG, u ## d, (unsigned long) u);
963
964#define CONVERT_INT(u) \
965small_type u ## s = get_sign(u); \
966sc_digit u ## d[DIGITS_PER_UINT]; \
967from_uint(DIGITS_PER_UINT, u ## d, (unsigned int) u);
968
969#define CONVERT_INT_2(u) \
970sc_digit u ## d[DIGITS_PER_UINT]; \
971from_uint(DIGITS_PER_UINT, u ## d, (unsigned int) u);
972
973#define CONVERT_INT64(u) \
974small_type u ## s = get_sign(u); \
975sc_digit u ## d[DIGITS_PER_UINT64]; \
976from_uint(DIGITS_PER_UINT64, u ## d, (uint64) u);
977
978#define CONVERT_INT64_2(u) \
979sc_digit u ## d[DIGITS_PER_UINT64]; \
980from_uint(DIGITS_PER_UINT64, u ## d, (uint64) u);
981
982
983// ----------------------------------------------------------------------------
984//  SECTION: PLUS operators: +, +=, ++
985// ----------------------------------------------------------------------------
986
987// Cases to consider when computing u + v:
988// 1. 0 + v = v
989// 2. u + 0 = u
990// 3. if sgn(u) == sgn(v)
991//    3.1 u + v = +(u + v) = sgn(u) * (u + v)
992//    3.2 (-u) + (-v) = -(u + v) = sgn(u) * (u + v)
993// 4. if sgn(u) != sgn(v)
994//    4.1 u + (-v) = u - v = sgn(u) * (u - v)
995//    4.2 (-u) + v = -(u - v) ==> sgn(u) * (u - v)
996//
997// Specialization of above cases for computing ++u or u++:
998// 1. 0 + 1 = 1
999// 3. u + 1 = u + 1 = sgn(u) * (u + 1)
1000// 4. (-u) + 1 = -(u - 1) = sgn(u) * (u - 1)
1001
1002sc_unsigned
1003operator + (const sc_unsigned &u, const sc_unsigned &v)
1004{
1005    if (u.sgn == SC_ZERO) // case 1
1006        return sc_unsigned(v);
1007
1008    if (v.sgn == SC_ZERO) // case 2
1009        return sc_unsigned(u);
1010
1011    // cases 3 and 4
1012    return add_unsigned_friend(u.sgn, u.nbits, u.ndigits, u.digit,
1013                               v.sgn, v.nbits, v.ndigits, v.digit);
1014}
1015
1016
1017sc_unsigned
1018operator + (const sc_unsigned &u, uint64 v)
1019{
1020    if (v == 0) // case 2
1021        return sc_unsigned(u);
1022
1023    CONVERT_INT64(v);
1024
1025    if (u.sgn == SC_ZERO)    // case 1
1026        return sc_unsigned(vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd, false);
1027
1028    // cases 3 and 4
1029    return add_unsigned_friend(u.sgn, u.nbits, u.ndigits, u.digit,
1030                               vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd);
1031}
1032
1033
1034sc_unsigned
1035operator + (uint64 u, const sc_unsigned &v)
1036{
1037    if (u == 0) // case 1
1038        return sc_unsigned(v);
1039
1040    CONVERT_INT64(u);
1041
1042    if (v.sgn == SC_ZERO) // case 2
1043        return sc_unsigned(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud, false);
1044
1045    // cases 3 and 4
1046    return add_unsigned_friend(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud,
1047                               v.sgn, v.nbits, v.ndigits, v.digit);
1048}
1049
1050
1051sc_unsigned
1052operator + (const sc_unsigned &u, unsigned long v)
1053{
1054    if (v == 0) // case 2
1055        return sc_unsigned(u);
1056
1057    CONVERT_LONG(v);
1058
1059    if (u.sgn == SC_ZERO) // case 1
1060        return sc_unsigned(vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd, false);
1061
1062    // cases 3 and 4
1063    return add_unsigned_friend(u.sgn, u.nbits, u.ndigits, u.digit,
1064                               vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd);
1065}
1066
1067
1068sc_unsigned
1069operator + (unsigned long u, const sc_unsigned &v)
1070{
1071    if (u == 0) // case 1
1072        return sc_unsigned(v);
1073
1074    CONVERT_LONG(u);
1075
1076    if (v.sgn == SC_ZERO) // case 2
1077        return sc_unsigned(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud, false);
1078
1079    // cases 3 and 4
1080    return add_unsigned_friend(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud,
1081                               v.sgn, v.nbits, v.ndigits, v.digit);
1082}
1083
1084// The rest of the operators in this section are included from
1085// sc_nbcommon.cpp.
1086
1087
1088// ----------------------------------------------------------------------------
1089//  SECTION: MINUS operators: -, -=, --
1090// ----------------------------------------------------------------------------
1091
1092// Cases to consider when computing u + v:
1093// 1. u - 0 = u
1094// 2. 0 - v = -v
1095// 3. if sgn(u) != sgn(v)
1096//    3.1 u - (-v) = u + v = sgn(u) * (u + v)
1097//    3.2 (-u) - v = -(u + v) ==> sgn(u) * (u + v)
1098// 4. if sgn(u) == sgn(v)
1099//    4.1 u - v = +(u - v) = sgn(u) * (u - v)
1100//    4.2 (-u) - (-v) = -(u - v) = sgn(u) * (u - v)
1101//
1102// Specialization of above cases for computing --u or u--:
1103// 1. 0 - 1 = -1
1104// 3. (-u) - 1 = -(u + 1) = sgn(u) * (u + 1)
1105// 4. u - 1 = u - 1 = sgn(u) * (u - 1)
1106
1107// The operators in this section are included from sc_nbcommon.cpp.
1108
1109
1110// ----------------------------------------------------------------------------
1111//  SECTION: MULTIPLICATION operators: *, *=
1112// ----------------------------------------------------------------------------
1113
1114// Cases to consider when computing u * v:
1115// 1. u * 0 = 0 * v = 0
1116// 2. 1 * v = v and -1 * v = -v
1117// 3. u * 1 = u and u * -1 = -u
1118// 4. u * v = u * v
1119
1120sc_unsigned
1121operator * (const sc_unsigned &u, const sc_unsigned &v)
1122{
1123    small_type s = mul_signs(u.sgn, v.sgn);
1124
1125    if (s == SC_ZERO) // case 1
1126        return sc_unsigned();
1127
1128    // cases 2-4
1129    return mul_unsigned_friend(s, u.nbits, u.ndigits, u.digit,
1130                               v.nbits, v.ndigits, v.digit);
1131}
1132
1133
1134sc_unsigned
1135operator * (const sc_unsigned &u, uint64 v)
1136{
1137    small_type s = mul_signs(u.sgn, get_sign(v));
1138
1139    if (s == SC_ZERO) // case 1
1140        return sc_unsigned();
1141
1142    CONVERT_INT64_2(v);
1143
1144    // cases 2-4
1145    return mul_unsigned_friend(s, u.nbits, u.ndigits, u.digit,
1146                               BITS_PER_UINT64, DIGITS_PER_UINT64, vd);
1147}
1148
1149
1150sc_unsigned
1151operator * (uint64 u, const sc_unsigned &v)
1152{
1153    small_type s = mul_signs(v.sgn, get_sign(u));
1154
1155    if (s == SC_ZERO) // case 1
1156        return sc_unsigned();
1157
1158    CONVERT_INT64_2(u);
1159
1160    // cases 2-4
1161    return mul_unsigned_friend(s, BITS_PER_UINT64, DIGITS_PER_UINT64, ud,
1162                               v.nbits, v.ndigits, v.digit);
1163}
1164
1165
1166sc_unsigned
1167operator * (const sc_unsigned &u, unsigned long v)
1168{
1169    small_type s = mul_signs(u.sgn, get_sign(v));
1170
1171    if (s == SC_ZERO) // case 1
1172        return sc_unsigned();
1173
1174    CONVERT_LONG_2(v);
1175
1176    // else cases 2-4
1177    return mul_unsigned_friend(s, u.nbits, u.ndigits, u.digit,
1178                               BITS_PER_ULONG, DIGITS_PER_ULONG, vd);
1179}
1180
1181sc_unsigned
1182operator * (unsigned long u, const sc_unsigned &v)
1183{
1184    small_type s = mul_signs(v.sgn, get_sign(u));
1185
1186    if (s == SC_ZERO) // case 1
1187        return sc_unsigned();
1188
1189    CONVERT_LONG_2(u);
1190
1191    // cases 2-4
1192    return mul_unsigned_friend(s, BITS_PER_ULONG, DIGITS_PER_ULONG, ud,
1193                               v.nbits, v.ndigits, v.digit);
1194}
1195
1196// The rest of the operators in this section are included from
1197// sc_nbcommon.cpp.
1198
1199
1200// ----------------------------------------------------------------------------
1201//  SECTION: DIVISION operators: /, /=
1202// ----------------------------------------------------------------------------
1203
1204// Cases to consider when finding the quotient q = floor(u/v):
1205// Note that u = q * v + r for r < q.
1206// 1. 0 / 0 or u / 0 => error
1207// 2. 0 / v => 0 = 0 * v + 0
1208// 3. u / v & &u = v => u = 1 * u + 0  - u or v can be 1 or -1
1209// 4. u / v & &u < v => u = 0 * v + u  - u can be 1 or -1
1210// 5. u / v & &u > v => u = q * v + r  - v can be 1 or -1
1211
1212sc_unsigned
1213operator / (const sc_unsigned &u, const sc_unsigned &v)
1214{
1215    small_type s = mul_signs(u.sgn, v.sgn);
1216
1217    if (s == SC_ZERO) {
1218        div_by_zero(v.sgn); // case 1
1219        return sc_unsigned(); // case 2
1220    }
1221
1222    // other cases
1223    return div_unsigned_friend(s, u.nbits, u.ndigits, u.digit,
1224                               v.nbits, v.ndigits, v.digit);
1225}
1226
1227
1228sc_unsigned
1229operator / (const sc_unsigned &u, uint64 v)
1230{
1231    small_type s = mul_signs(u.sgn, get_sign(v));
1232
1233    if (s == SC_ZERO) {
1234        div_by_zero(v); // case 1
1235        return sc_unsigned(); // case 2
1236    }
1237
1238    CONVERT_INT64_2(v);
1239
1240    // other cases
1241    return div_unsigned_friend(s, u.nbits, u.ndigits, u.digit,
1242                               BITS_PER_UINT64, DIGITS_PER_UINT64, vd);
1243}
1244
1245
1246sc_unsigned
1247operator / (uint64 u, const sc_unsigned &v)
1248{
1249    small_type s = mul_signs(v.sgn, get_sign(u));
1250
1251    if (s == SC_ZERO) {
1252        div_by_zero(v.sgn); // case 1
1253        return sc_unsigned(); // case 2
1254
1255    }
1256
1257    CONVERT_INT64_2(u);
1258
1259    // other cases
1260    return div_unsigned_friend(s, BITS_PER_UINT64, DIGITS_PER_UINT64, ud,
1261                               v.nbits, v.ndigits, v.digit);
1262}
1263
1264
1265sc_unsigned
1266operator / (const sc_unsigned &u, unsigned long v)
1267{
1268    small_type s = mul_signs(u.sgn, get_sign(v));
1269
1270    if (s == SC_ZERO) {
1271        div_by_zero(v); // case 1
1272        return sc_unsigned(); // case 2
1273    }
1274
1275    CONVERT_LONG_2(v);
1276
1277    // other cases
1278    return div_unsigned_friend(s, u.nbits, u.ndigits, u.digit,
1279                               BITS_PER_ULONG, DIGITS_PER_ULONG, vd);
1280}
1281
1282
1283sc_unsigned
1284operator / (unsigned long u, const sc_unsigned &v)
1285{
1286    small_type s = mul_signs(v.sgn, get_sign(u));
1287
1288    if (s == SC_ZERO) {
1289        div_by_zero(v.sgn); // case 1
1290        return sc_unsigned(); // case 2
1291
1292    }
1293
1294    CONVERT_LONG_2(u);
1295
1296    // other cases
1297    return div_unsigned_friend(s, BITS_PER_ULONG, DIGITS_PER_ULONG, ud,
1298                               v.nbits, v.ndigits, v.digit);
1299}
1300
1301// The rest of the operators in this section are included from
1302// sc_nbcommon.cpp.
1303
1304
1305// ----------------------------------------------------------------------------
1306//  SECTION: MOD operators: %, %=.
1307// ----------------------------------------------------------------------------
1308
1309// Cases to consider when finding the remainder r = u % v:
1310// Note that u = q * v + r for r < q.
1311// 1. 0 % 0 or u % 0 => error
1312// 2. 0 % v => 0 = 0 * v + 0
1313// 3. u % v & &u = v => u = 1 * u + 0  - u or v can be 1 or -1
1314// 4. u % v & &u < v => u = 0 * v + u  - u can be 1 or -1
1315// 5. u % v & &u > v => u = q * v + r  - v can be 1 or -1
1316
1317sc_unsigned
1318operator % (const sc_unsigned &u, const sc_unsigned &v)
1319{
1320    if ((u.sgn == SC_ZERO) || (v.sgn == SC_ZERO)) {
1321        div_by_zero(v.sgn); // case 1
1322        return sc_unsigned(); // case 2
1323    }
1324
1325    // other cases
1326    return mod_unsigned_friend(u.sgn, u.nbits, u.ndigits, u.digit,
1327                               v.nbits, v.ndigits, v.digit);
1328}
1329
1330
1331sc_unsigned
1332operator % (const sc_unsigned &u, uint64 v)
1333{
1334    if ((u.sgn == SC_ZERO) || (v == 0)) {
1335        div_by_zero(v); // case 1
1336        return sc_unsigned(); // case 2
1337    }
1338
1339    CONVERT_INT64_2(v);
1340
1341    // other cases
1342    return mod_unsigned_friend(u.sgn, u.nbits, u.ndigits, u.digit,
1343                               BITS_PER_UINT64, DIGITS_PER_UINT64, vd);
1344
1345}
1346
1347
1348sc_unsigned
1349operator % (uint64 u, const sc_unsigned &v)
1350{
1351    if ((u == 0) || (v.sgn == SC_ZERO)) {
1352        div_by_zero(v.sgn); // case 1
1353        return sc_unsigned(); // case 2
1354    }
1355
1356    CONVERT_INT64(u);
1357
1358    // other cases
1359    return mod_unsigned_friend(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud,
1360                               v.nbits, v.ndigits, v.digit);
1361}
1362
1363
1364sc_unsigned
1365operator % (const sc_unsigned &u, unsigned long v)
1366{
1367    if ((u.sgn == SC_ZERO) || (v == 0)) {
1368        div_by_zero(v); // case 1
1369        return sc_unsigned(); // case 2
1370    }
1371
1372    CONVERT_LONG_2(v);
1373
1374    // other cases
1375    return mod_unsigned_friend(u.sgn, u.nbits, u.ndigits, u.digit,
1376                               BITS_PER_ULONG, DIGITS_PER_ULONG, vd);
1377}
1378
1379
1380sc_unsigned
1381operator % (unsigned long u, const sc_unsigned &v)
1382{
1383    if ((u == 0) || (v.sgn == SC_ZERO)) {
1384        div_by_zero(v.sgn); // case 1
1385        return sc_unsigned(); // case 2
1386    }
1387
1388    CONVERT_LONG(u);
1389
1390    // other cases
1391    return mod_unsigned_friend(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud,
1392                               v.nbits, v.ndigits, v.digit);
1393}
1394
1395// The rest of the operators in this section are included from
1396// sc_nbcommon.cpp.
1397
1398
1399// ----------------------------------------------------------------------------
1400//  SECTION: Bitwise AND operators: &, &=
1401// ----------------------------------------------------------------------------
1402
1403// Cases to consider when computing u  &v:
1404// 1. u & 0 = 0  &v = 0
1405// 2. u  &v => sgn = +
1406// 3. (-u) & (-v) => sgn = -
1407// 4. u & (-v) => sgn = +
1408// 5. (-u)  &v => sgn = +
1409
1410sc_unsigned
1411operator & (const sc_unsigned &u, const sc_unsigned &v)
1412{
1413    if ((u.sgn == SC_ZERO) || (v.sgn == SC_ZERO)) // case 1
1414        return sc_unsigned();
1415
1416    // other cases
1417    return and_unsigned_friend(u.sgn, u.nbits, u.ndigits, u.digit,
1418                               v.sgn, v.nbits, v.ndigits, v.digit);
1419}
1420
1421
1422sc_unsigned
1423operator & (const sc_unsigned &u, uint64 v)
1424{
1425    if ((u.sgn == SC_ZERO) || (v == 0)) // case 1
1426        return sc_unsigned();
1427
1428    CONVERT_INT64(v);
1429
1430    // other cases
1431    return and_unsigned_friend(u.sgn, u.nbits, u.ndigits, u.digit,
1432                               vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd);
1433}
1434
1435
1436sc_unsigned
1437operator & (uint64 u, const sc_unsigned &v)
1438{
1439    if ((u == 0) || (v.sgn == SC_ZERO)) // case 1
1440        return sc_unsigned();
1441
1442    CONVERT_INT64(u);
1443
1444    // other cases
1445    return and_unsigned_friend(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud,
1446                               v.sgn, v.nbits, v.ndigits, v.digit);
1447}
1448
1449
1450sc_unsigned
1451operator & (const sc_unsigned &u, unsigned long v)
1452{
1453    if ((u.sgn == SC_ZERO) || (v == 0)) // case 1
1454        return sc_unsigned();
1455
1456    CONVERT_LONG(v);
1457
1458    // other cases
1459    return and_unsigned_friend(u.sgn, u.nbits, u.ndigits, u.digit,
1460                               vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd);
1461}
1462
1463
1464sc_unsigned
1465operator & (unsigned long u, const sc_unsigned &v)
1466{
1467    if ((u == 0) || (v.sgn == SC_ZERO)) // case 1
1468        return sc_unsigned();
1469
1470    CONVERT_LONG(u);
1471
1472    // other cases
1473    return and_unsigned_friend(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud,
1474                               v.sgn, v.nbits, v.ndigits, v.digit);
1475}
1476
1477// The rest of the operators in this section are included from
1478// sc_nbcommon.cpp.
1479
1480
1481// ----------------------------------------------------------------------------
1482//  SECTION: Bitwise OR operators: |, |=
1483// ----------------------------------------------------------------------------
1484
1485// Cases to consider when computing u | v:
1486// 1. u | 0 = u
1487// 2. 0 | v = v
1488// 3. u | v => sgn = +
1489// 4. (-u) | (-v) => sgn = -
1490// 5. u | (-v) => sgn = -
1491// 6. (-u) | v => sgn = -
1492
1493sc_unsigned
1494operator | (const sc_unsigned &u, const sc_unsigned &v)
1495{
1496    if (v.sgn == SC_ZERO) // case 1
1497        return sc_unsigned(u);
1498
1499    if (u.sgn == SC_ZERO) // case 2
1500        return sc_unsigned(v);
1501
1502    // other cases
1503    return or_unsigned_friend(u.sgn, u.nbits, u.ndigits, u.digit,
1504                              v.sgn, v.nbits, v.ndigits, v.digit);
1505}
1506
1507
1508sc_unsigned
1509operator | (const sc_unsigned &u, uint64 v)
1510{
1511    if (v == 0) // case 1
1512        return sc_unsigned(u);
1513
1514    CONVERT_INT64(v);
1515
1516    if (u.sgn == SC_ZERO) // case 2
1517        return sc_unsigned(vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd, false);
1518
1519    // other cases
1520    return or_unsigned_friend(u.sgn, u.nbits, u.ndigits, u.digit,
1521                              vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd);
1522}
1523
1524
1525sc_unsigned
1526operator | (uint64 u, const sc_unsigned &v)
1527{
1528    if (u == 0)
1529        return sc_unsigned(v);
1530
1531    CONVERT_INT64(u);
1532
1533    if (v.sgn == SC_ZERO)
1534        return sc_unsigned(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud, false);
1535
1536    // other cases
1537    return or_unsigned_friend(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud,
1538                              v.sgn, v.nbits, v.ndigits, v.digit);
1539}
1540
1541
1542sc_unsigned
1543operator | (const sc_unsigned &u, unsigned long v)
1544{
1545    if (v == 0) // case 1
1546        return sc_unsigned(u);
1547
1548    CONVERT_LONG(v);
1549
1550    if (u.sgn == SC_ZERO) // case 2
1551        return sc_unsigned(vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd, false);
1552
1553    // other cases
1554    return or_unsigned_friend(u.sgn, u.nbits, u.ndigits, u.digit,
1555                              vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd);
1556}
1557
1558
1559sc_unsigned
1560operator | (unsigned long u, const sc_unsigned &v)
1561{
1562    if (u == 0)
1563        return sc_unsigned(v);
1564
1565    CONVERT_LONG(u);
1566
1567    if (v.sgn == SC_ZERO)
1568        return sc_unsigned(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud, false);
1569
1570    // other cases
1571    return or_unsigned_friend(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud,
1572                              v.sgn, v.nbits, v.ndigits, v.digit);
1573}
1574
1575// The rest of the operators in this section are included from
1576// sc_nbcommon.cpp.
1577
1578
1579// ----------------------------------------------------------------------------
1580//  SECTION: Bitwise XOR operators: ^, ^=
1581// ----------------------------------------------------------------------------
1582
1583// Cases to consider when computing u ^ v:
1584// Note that  u ^ v = (~u  &v) | (u & ~v).
1585// 1. u ^ 0 = u
1586// 2. 0 ^ v = v
1587// 3. u ^ v => sgn = +
1588// 4. (-u) ^ (-v) => sgn = -
1589// 5. u ^ (-v) => sgn = -
1590// 6. (-u) ^ v => sgn = +
1591
1592sc_unsigned
1593operator ^ (const sc_unsigned &u, const sc_unsigned &v)
1594{
1595    if (v.sgn == SC_ZERO) // case 1
1596        return sc_unsigned(u);
1597
1598    if (u.sgn == SC_ZERO) // case 2
1599        return sc_unsigned(v);
1600
1601    // other cases
1602    return xor_unsigned_friend(u.sgn, u.nbits, u.ndigits, u.digit,
1603                               v.sgn, v.nbits, v.ndigits, v.digit);
1604}
1605
1606
1607sc_unsigned
1608operator ^ (const sc_unsigned &u, uint64 v)
1609{
1610    if (v == 0) // case 1
1611        return sc_unsigned(u);
1612
1613    CONVERT_INT64(v);
1614
1615    if (u.sgn == SC_ZERO) // case 2
1616        return sc_unsigned(vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd, false);
1617
1618    // other cases
1619    return xor_unsigned_friend(u.sgn, u.nbits, u.ndigits, u.digit,
1620                               vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd);
1621}
1622
1623sc_unsigned
1624operator ^ (uint64 u, const sc_unsigned &v)
1625{
1626    if (u == 0)
1627        return sc_unsigned(v);
1628
1629    CONVERT_INT64(u);
1630
1631    if (v.sgn == SC_ZERO)
1632        return sc_unsigned(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud, false);
1633
1634    // other cases
1635    return xor_unsigned_friend(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud,
1636                               v.sgn, v.nbits, v.ndigits, v.digit);
1637}
1638
1639
1640sc_unsigned
1641operator ^ (const sc_unsigned &u, unsigned long v)
1642{
1643    if (v == 0) // case 1
1644        return sc_unsigned(u);
1645
1646    CONVERT_LONG(v);
1647
1648    if (u.sgn == SC_ZERO) // case 2
1649        return sc_unsigned(vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd, false);
1650
1651    // other cases
1652    return xor_unsigned_friend(u.sgn, u.nbits, u.ndigits, u.digit,
1653                               vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd);
1654}
1655
1656sc_unsigned
1657operator ^ (unsigned long u, const sc_unsigned &v)
1658{
1659    if (u == 0)
1660        return sc_unsigned(v);
1661
1662    CONVERT_LONG(u);
1663
1664    if (v.sgn == SC_ZERO)
1665        return sc_unsigned(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud, false);
1666
1667    // other cases
1668    return xor_unsigned_friend(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud,
1669                               v.sgn, v.nbits, v.ndigits, v.digit);
1670}
1671
1672// The rest of the operators in this section are included from
1673// sc_nbcommon.cpp.
1674
1675
1676// ----------------------------------------------------------------------------
1677//  SECTION: Bitwise NOT operator: ~
1678// ----------------------------------------------------------------------------
1679
1680// Operators in this section are included from sc_nbcommon.cpp.
1681
1682
1683// ----------------------------------------------------------------------------
1684//  SECTION: LEFT SHIFT operators: <<, <<=
1685// ----------------------------------------------------------------------------
1686
1687sc_unsigned
1688operator << (const sc_unsigned &u, const sc_signed &v)
1689{
1690    if ((v.sgn == SC_ZERO) || (v.sgn == SC_NEG))
1691        return sc_unsigned(u);
1692
1693    return operator << (u, v.to_ulong());
1694}
1695
1696// The rest of the operators in this section are included from
1697// sc_nbcommon.cpp.
1698
1699
1700// ----------------------------------------------------------------------------
1701//    SECTION: RIGHT SHIFT operators: >>, >>=
1702// ----------------------------------------------------------------------------
1703
1704sc_unsigned
1705operator >> (const sc_unsigned &u, const sc_signed &v)
1706{
1707
1708    if ((v.sgn == SC_ZERO) || (v.sgn == SC_NEG))
1709        return sc_unsigned(u);
1710
1711    return operator >> (u, v.to_long());
1712
1713}
1714
1715// The rest of the operators in this section are included from
1716// sc_nbcommon.cpp.
1717
1718
1719// ----------------------------------------------------------------------------
1720//    SECTION: Unary arithmetic operators.
1721// ----------------------------------------------------------------------------
1722
1723sc_unsigned
1724operator + (const sc_unsigned &u)
1725{
1726    return sc_unsigned(u);
1727}
1728
1729
1730// ----------------------------------------------------------------------------
1731//    SECTION: EQUAL operator: ==
1732// ----------------------------------------------------------------------------
1733
1734bool
1735operator == (const sc_unsigned &u, const sc_unsigned &v)
1736{
1737    if (&u == &v)
1738        return true;
1739    if (compare_unsigned(u.sgn, u.nbits, u.ndigits, u.digit,
1740                         v.sgn, v.nbits, v.ndigits, v.digit) != 0) {
1741        return false;
1742    }
1743    return true;
1744}
1745
1746
1747bool
1748operator == (const sc_unsigned &u, const sc_signed &v)
1749{
1750    if (v.sgn == SC_NEG)
1751        return false;
1752    if (compare_unsigned(u.sgn, u.nbits, u.ndigits, u.digit,
1753                         v.sgn, v.nbits, v.ndigits, v.digit, 0, 1) != 0) {
1754        return false;
1755    }
1756    return true;
1757}
1758
1759
1760bool
1761operator == (const sc_signed &u, const sc_unsigned &v)
1762{
1763    if (u.sgn == SC_NEG)
1764        return false;
1765    if (compare_unsigned(u.sgn, u.nbits, u.ndigits, u.digit,
1766                         v.sgn, v.nbits, v.ndigits, v.digit, 1, 0) != 0) {
1767        return false;
1768    }
1769    return true;
1770}
1771
1772
1773bool
1774operator == (const sc_unsigned &u, int64 v)
1775{
1776    if (v < 0)
1777        return false;
1778    CONVERT_INT64(v);
1779    if (compare_unsigned(u.sgn, u.nbits, u.ndigits, u.digit,
1780                         vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd) != 0) {
1781        return false;
1782    }
1783    return true;
1784}
1785
1786
1787bool
1788operator == (int64 u, const sc_unsigned &v)
1789{
1790    if (u < 0)
1791        return false;
1792    CONVERT_INT64(u);
1793    if (compare_unsigned(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud,
1794                         v.sgn, v.nbits, v.ndigits, v.digit) != 0) {
1795        return false;
1796    }
1797    return true;
1798}
1799
1800
1801bool
1802operator == (const sc_unsigned &u, uint64 v)
1803{
1804    CONVERT_INT64(v);
1805    if (compare_unsigned(u.sgn, u.nbits, u.ndigits, u.digit,
1806                         vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd) != 0)
1807        return false;
1808    return true;
1809}
1810
1811
1812bool
1813operator == (uint64 u, const sc_unsigned &v)
1814{
1815    CONVERT_INT64(u);
1816    if (compare_unsigned(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud,
1817                         v.sgn, v.nbits, v.ndigits, v.digit) != 0)
1818        return false;
1819    return true;
1820}
1821
1822
1823bool
1824operator == (const sc_unsigned &u, long v)
1825{
1826    if (v < 0)
1827        return false;
1828    CONVERT_LONG(v);
1829    if (compare_unsigned(u.sgn, u.nbits, u.ndigits, u.digit,
1830                         vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd) != 0)
1831        return false;
1832    return true;
1833}
1834
1835
1836bool
1837operator == (long u, const sc_unsigned &v)
1838{
1839    if (u < 0)
1840        return false;
1841    CONVERT_LONG(u);
1842    if (compare_unsigned(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud,
1843                         v.sgn, v.nbits, v.ndigits, v.digit) != 0)
1844        return false;
1845    return true;
1846}
1847
1848
1849bool
1850operator == (const sc_unsigned &u, unsigned long v)
1851{
1852    CONVERT_LONG(v);
1853    if (compare_unsigned(u.sgn, u.nbits, u.ndigits, u.digit,
1854                         vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd) != 0)
1855        return false;
1856    return true;
1857}
1858
1859
1860bool
1861operator == (unsigned long u, const sc_unsigned &v)
1862{
1863    CONVERT_LONG(u);
1864    if (compare_unsigned(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud,
1865                         v.sgn, v.nbits, v.ndigits, v.digit) != 0)
1866        return false;
1867    return true;
1868}
1869
1870
1871// ----------------------------------------------------------------------------
1872//  SECTION: NOT_EQUAL operator: !=
1873// ----------------------------------------------------------------------------
1874
1875bool
1876operator != (const sc_unsigned &u, const sc_signed &v)
1877{
1878    return (!operator == (u, v));
1879}
1880
1881
1882bool
1883operator != (const sc_signed &u, const sc_unsigned &v)
1884{
1885    return (!operator == (u, v));
1886}
1887
1888// The rest of the operators in this section are included from sc_nbcommon.cpp.
1889
1890
1891// ----------------------------------------------------------------------------
1892//  SECTION: LESS THAN operator: <
1893// ----------------------------------------------------------------------------
1894
1895bool
1896operator < (const sc_unsigned &u, const sc_unsigned &v)
1897{
1898    if (&u == &v)
1899        return false;
1900    if (compare_unsigned(u.sgn, u.nbits, u.ndigits, u.digit,
1901                         v.sgn, v.nbits, v.ndigits, v.digit) < 0) {
1902        return true;
1903    }
1904    return false;
1905}
1906
1907
1908bool
1909operator < (const sc_unsigned &u, const sc_signed &v)
1910{
1911    if (v.sgn == SC_NEG)
1912        return false;
1913    if (compare_unsigned(u.sgn, u.nbits, u.ndigits, u.digit,
1914                         v.sgn, v.nbits, v.ndigits, v.digit, 0, 1) < 0) {
1915        return true;
1916    }
1917    return false;
1918}
1919
1920
1921bool
1922operator < (const sc_signed &u, const sc_unsigned &v)
1923{
1924    if (u.sgn == SC_NEG)
1925        return true;
1926    if (compare_unsigned(u.sgn, u.nbits, u.ndigits, u.digit,
1927                       v.sgn, v.nbits, v.ndigits, v.digit, 1, 0) < 0) {
1928        return true;
1929    }
1930    return false;
1931}
1932
1933
1934bool
1935operator < (const sc_unsigned &u, int64 v)
1936{
1937    if (v < 0)
1938        return false;
1939    CONVERT_INT64(v);
1940    if (compare_unsigned(u.sgn, u.nbits, u.ndigits, u.digit,
1941                         vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd) < 0) {
1942        return true;
1943    }
1944    return false;
1945}
1946
1947
1948bool
1949operator < (int64 u, const sc_unsigned &v)
1950{
1951    if (u < 0)
1952        return true;
1953    CONVERT_INT64(u);
1954    if (compare_unsigned(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud,
1955                         v.sgn, v.nbits, v.ndigits, v.digit) < 0) {
1956        return true;
1957    }
1958    return false;
1959}
1960
1961
1962bool
1963operator < (const sc_unsigned &u, uint64 v)
1964{
1965    CONVERT_INT64(v);
1966    if (compare_unsigned(u.sgn, u.nbits, u.ndigits, u.digit,
1967                         vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd) < 0) {
1968        return true;
1969    }
1970    return false;
1971}
1972
1973
1974bool
1975operator < (uint64 u, const sc_unsigned &v)
1976{
1977    CONVERT_INT64(u);
1978    if (compare_unsigned(us, BITS_PER_UINT64, DIGITS_PER_UINT64, ud,
1979                         v.sgn, v.nbits, v.ndigits, v.digit) < 0){
1980        return true;
1981    }
1982    return false;
1983}
1984
1985
1986bool
1987operator < (const sc_unsigned &u, long v)
1988{
1989    if (v < 0)
1990        return false;
1991    CONVERT_LONG(v);
1992    if (compare_unsigned(u.sgn, u.nbits, u.ndigits, u.digit,
1993                         vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd) < 0) {
1994        return true;
1995    }
1996    return false;
1997}
1998
1999
2000bool
2001operator < (long u, const sc_unsigned &v)
2002{
2003    if (u < 0)
2004        return true;
2005    CONVERT_LONG(u);
2006    if (compare_unsigned(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud,
2007                         v.sgn, v.nbits, v.ndigits, v.digit) < 0) {
2008        return true;
2009    }
2010    return false;
2011}
2012
2013
2014bool
2015operator < (const sc_unsigned &u, unsigned long v)
2016{
2017    CONVERT_LONG(v);
2018    if (compare_unsigned(u.sgn, u.nbits, u.ndigits, u.digit,
2019                         vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd) < 0) {
2020        return true;
2021    }
2022    return false;
2023}
2024
2025
2026bool
2027operator < (unsigned long u, const sc_unsigned &v)
2028{
2029    CONVERT_LONG(u);
2030    if (compare_unsigned(us, BITS_PER_ULONG, DIGITS_PER_ULONG, ud,
2031                         v.sgn, v.nbits, v.ndigits, v.digit) < 0) {
2032        return true;
2033    }
2034    return false;
2035}
2036
2037
2038// ----------------------------------------------------------------------------
2039//  SECTION: LESS THAN or EQUAL operator: <=
2040// ----------------------------------------------------------------------------
2041
2042bool
2043operator <= (const sc_unsigned &u, const sc_signed &v)
2044{
2045    return (operator < (u, v) || operator == (u, v));
2046}
2047
2048
2049bool
2050operator <= (const sc_signed &u, const sc_unsigned &v)
2051{
2052    return (operator < (u, v) || operator == (u, v));
2053}
2054
2055// The rest of the operators in this section are included from sc_nbcommon.cpp.
2056
2057
2058// ----------------------------------------------------------------------------
2059//  SECTION: GREATER THAN operator: >
2060// ----------------------------------------------------------------------------
2061
2062bool
2063operator > (const sc_unsigned &u, const sc_signed &v)
2064{
2065  return (!(operator <= (u, v)));
2066}
2067
2068
2069bool
2070operator > (const sc_signed &u, const sc_unsigned &v)
2071{
2072  return (!(operator <= (u, v)));
2073}
2074
2075// The rest of the operators in this section are included from sc_nbcommon.cpp.
2076
2077
2078// ----------------------------------------------------------------------------
2079//  SECTION: GREATER THAN or EQUAL operator: >=
2080// ----------------------------------------------------------------------------
2081
2082bool
2083operator >= (const sc_unsigned &u, const sc_signed &v)
2084{
2085  return (!(operator < (u, v)));
2086}
2087
2088
2089bool
2090operator >= (const sc_signed &u, const sc_unsigned &v)
2091{
2092  return (!(operator < (u, v)));
2093}
2094
2095// The rest of the operators in this section are included from sc_nbcommon.cpp.
2096
2097
2098// ----------------------------------------------------------------------------
2099//  SECTION: Friends
2100// ----------------------------------------------------------------------------
2101
2102// Compare u and v as unsigned and return r
2103//  r = 0 if u == v
2104//  r < 0 if u < v
2105//  r > 0 if u > v
2106
2107int
2108compare_unsigned(small_type us, int unb, int und, const sc_digit *ud,
2109                 small_type vs, int vnb, int vnd, const sc_digit *vd,
2110                 small_type if_u_signed, small_type if_v_signed)
2111{
2112    if (us == vs) {
2113        if (us == SC_ZERO) {
2114            return 0;
2115        } else {
2116            int cmp_res = vec_skip_and_cmp(und, ud, vnd, vd);
2117            if (us == SC_POS)
2118                return cmp_res;
2119            else
2120                return -cmp_res;
2121        }
2122    } else {
2123        if (us == SC_ZERO)
2124            return -vs;
2125        if (vs == SC_ZERO)
2126            return us;
2127
2128        int cmp_res;
2129        int nd = (us == SC_NEG ? und : vnd);
2130
2131#ifdef SC_MAX_NBITS
2132        sc_digit d[MAX_NDIGITS];
2133#else
2134        sc_digit *d = new sc_digit[nd];
2135#endif
2136
2137        if (us == SC_NEG) {
2138            vec_copy(nd, d, ud);
2139            vec_complement(nd, d);
2140            trim(if_u_signed, unb, nd, d);
2141            cmp_res = vec_skip_and_cmp(nd, d, vnd, vd);
2142        } else {
2143            vec_copy(nd, d, vd);
2144            vec_complement(nd, d);
2145            trim(if_v_signed, vnb, nd, d);
2146            cmp_res = vec_skip_and_cmp(und, ud, nd, d);
2147        }
2148
2149#ifndef SC_MAX_NBITS
2150        delete [] d;
2151#endif
2152
2153        return cmp_res;
2154    }
2155}
2156
2157
2158// ----------------------------------------------------------------------------
2159//  SECTION: Public members - Other utils.
2160// ----------------------------------------------------------------------------
2161
2162bool
2163sc_unsigned::iszero() const
2164{
2165    if (sgn == SC_ZERO) {
2166        return true;
2167    } else if (sgn == SC_NEG) {
2168        // A negative unsigned number can be zero, e.g., -16 in 4 bits, so
2169        // check that.
2170
2171#ifdef SC_MAX_NBITS
2172        sc_digit d[MAX_NDIGITS];
2173#else
2174        sc_digit *d = new sc_digit[ndigits];
2175#endif
2176
2177        vec_copy(ndigits, d, digit);
2178        vec_complement(ndigits, d);
2179        trim_unsigned(nbits, ndigits, d);
2180
2181        bool res = check_for_zero(ndigits, d);
2182
2183#ifndef SC_MAX_NBITS
2184        delete [] d;
2185#endif
2186
2187        return res;
2188    } else {
2189        return false;
2190    }
2191}
2192
2193// The rest of the utils in this section are included from sc_nbcommon.cpp.
2194
2195
2196// ----------------------------------------------------------------------------
2197//  SECTION: Private members.
2198// ----------------------------------------------------------------------------
2199
2200// The private members in this section are included from
2201// sc_nbcommon.cpp.
2202
2203#define CLASS_TYPE sc_unsigned
2204#define CLASS_TYPE_STR "sc_unsigned"
2205
2206#define ADD_HELPER add_unsigned_friend
2207#define SUB_HELPER sub_unsigned_friend
2208#define MUL_HELPER mul_unsigned_friend
2209#define DIV_HELPER div_unsigned_friend
2210#define MOD_HELPER mod_unsigned_friend
2211#define AND_HELPER and_unsigned_friend
2212#define OR_HELPER or_unsigned_friend
2213#define XOR_HELPER xor_unsigned_friend
2214
2215#include "sc_nbfriends.inc"
2216
2217#undef SC_SIGNED
2218#define SC_UNSIGNED
2219#define IF_SC_SIGNED 0 // 0 = sc_unsigned
2220#define CLASS_TYPE_SUBREF sc_unsigned_subref_r
2221#define OTHER_CLASS_TYPE sc_signed
2222#define OTHER_CLASS_TYPE_SUBREF sc_signed_subref_r
2223
2224#define MUL_ON_HELPER mul_on_help_unsigned
2225#define DIV_ON_HELPER div_on_help_unsigned
2226#define MOD_ON_HELPER mod_on_help_unsigned
2227
2228#include "sc_nbcommon.inc"
2229
2230#undef MOD_ON_HELPER
2231#undef DIV_ON_HELPER
2232#undef MUL_ON_HELPER
2233
2234#undef OTHER_CLASS_TYPE_SUBREF
2235#undef OTHER_CLASS_TYPE
2236#undef CLASS_TYPE_SUBREF
2237#undef IF_SC_SIGNED
2238#undef SC_UNSIGNED
2239
2240#undef XOR_HELPER
2241#undef OR_HELPER
2242#undef AND_HELPER
2243#undef MOD_HELPER
2244#undef DIV_HELPER
2245#undef MUL_HELPER
2246#undef SUB_HELPER
2247#undef ADD_HELPER
2248
2249#undef CLASS_TYPE
2250#undef CLASS_TYPE_STR
2251
2252#include "sc_unsigned_bitref.inc"
2253#include "sc_unsigned_subref.inc"
2254
2255#undef CONVERT_LONG
2256#undef CONVERT_LONG_2
2257#undef CONVERT_INT64
2258#undef CONVERT_INT64_2
2259
2260} // namespace sc_dt
2261