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