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_nbcommon.cpp -- Functions common to both sc_signed and sc_unsigned.
23                     This file is included in sc_signed.cpp and
24                     sc_unsigned.cpp after the macros are defined accordingly.
25                     For example, sc_signed.cpp will first define CLASS_TYPE
26                     as sc_signed before including this file. This file like
27                     sc_nbfriends.cpp and sc_nbexterns.cpp is created in order
28                     to ensure only one version of each function, regardless
29                     of the class that they interface to.
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// ----------------------------------------------------------------------------
47//  SECTION : Public members
48// ----------------------------------------------------------------------------
49
50// Create a CLASS_TYPE number with nb bits.
51CLASS_TYPE::CLASS_TYPE( int nb ) :
52    sc_value_base(), sgn(), nbits(), ndigits(), digit()
53{
54    sgn = default_sign();
55    if( nb > 0 ) {
56	nbits = num_bits( nb );
57    } else {
58	char msg[BUFSIZ];
59	std::sprintf( msg, "%s::%s( int nb ) : nb = %d is not valid",
60		 CLASS_TYPE_STR, CLASS_TYPE_STR, nb );
61	SC_REPORT_ERROR( sc_core::SC_ID_INIT_FAILED_, msg );
62    }
63    ndigits = DIV_CEIL(nbits);
64#ifdef SC_MAX_NBITS
65    test_bound(nb);
66#else
67    digit = new sc_digit[ndigits];
68#endif
69    makezero();
70}
71
72
73// Create a copy of v with sgn s. v is of the same type.
74CLASS_TYPE::CLASS_TYPE(const CLASS_TYPE& v) :
75    sc_value_base(v), sgn(v.sgn), nbits(v.nbits), ndigits(v.ndigits), digit()
76{
77#ifndef SC_MAX_NBITS
78  digit = new sc_digit[ndigits];
79#endif
80
81  vec_copy(ndigits, digit, v.digit);
82}
83
84
85// Create a copy of v where v is of the different type.
86CLASS_TYPE::CLASS_TYPE(const OTHER_CLASS_TYPE& v) :
87    sc_value_base(v), sgn(v.sgn), nbits(num_bits(v.nbits)), ndigits(), digit()
88{
89#if (IF_SC_SIGNED == 1)
90  ndigits = v.ndigits;
91#else
92  ndigits = DIV_CEIL(nbits);
93#endif
94
95#ifndef SC_MAX_NBITS
96  digit = new sc_digit[ndigits];
97#endif
98
99  copy_digits(v.nbits, v.ndigits, v.digit);
100}
101
102// Create a copy of v where v is an sign-less instance.
103CLASS_TYPE::CLASS_TYPE(const sc_bv_base& v) :
104    sc_value_base(), sgn(), nbits(), ndigits(), digit()
105{
106    int nb = v.length();
107    sgn = default_sign();
108    if( nb > 0 ) {
109        nbits = num_bits( nb );
110    } else {
111        char msg[BUFSIZ];
112        std::sprintf( msg, "%s::%s( sc_bv_base ) : nb = %d is not valid",
113                 CLASS_TYPE_STR, CLASS_TYPE_STR, nb );
114        SC_REPORT_ERROR( sc_core::SC_ID_INIT_FAILED_, msg );
115    }
116    ndigits = DIV_CEIL(nbits);
117#   ifdef SC_MAX_NBITS
118        test_bound(nb);
119#    else
120        digit = new sc_digit[ndigits];
121#    endif
122    makezero();
123    *this = v;
124}
125
126CLASS_TYPE::CLASS_TYPE(const sc_lv_base& v) :
127    sc_value_base(), sgn(), nbits(), ndigits(), digit()
128{
129    int nb = v.length();
130    sgn = default_sign();
131    if( nb > 0 ) {
132        nbits = num_bits( nb );
133    } else {
134        char msg[BUFSIZ];
135        std::sprintf( msg, "%s::%s( sc_lv_base ) : nb = %d is not valid",
136                 CLASS_TYPE_STR, CLASS_TYPE_STR, nb );
137        SC_REPORT_ERROR( sc_core::SC_ID_INIT_FAILED_, msg );
138    }
139    ndigits = DIV_CEIL(nbits);
140#   ifdef SC_MAX_NBITS
141        test_bound(nb);
142#    else
143        digit = new sc_digit[ndigits];
144#    endif
145    makezero();
146    *this = v;
147}
148
149CLASS_TYPE::CLASS_TYPE(const sc_int_subref_r& v) :
150    sc_value_base(v), sgn(), nbits(), ndigits(), digit()
151{
152    int nb = v.length();
153    sgn = default_sign();
154    if( nb > 0 ) {
155        nbits = num_bits( nb );
156    } else {
157        char msg[BUFSIZ];
158        std::sprintf( msg, "%s::%s( sc_int_subref ) : nb = %d is not valid",
159                 CLASS_TYPE_STR, CLASS_TYPE_STR, nb );
160        SC_REPORT_ERROR( sc_core::SC_ID_INIT_FAILED_, msg );
161    }
162    ndigits = DIV_CEIL(nbits);
163#   ifdef SC_MAX_NBITS
164        test_bound(nb);
165#    else
166        digit = new sc_digit[ndigits];
167#    endif
168    makezero();
169    *this = v.to_uint64();
170}
171
172CLASS_TYPE::CLASS_TYPE(const sc_uint_subref_r& v) :
173    sc_value_base(v), sgn(), nbits(), ndigits(), digit()
174{
175    int nb = v.length();
176    sgn = default_sign();
177    if( nb > 0 ) {
178        nbits = num_bits( nb );
179    } else {
180        char msg[BUFSIZ];
181        std::sprintf( msg, "%s::%s( sc_uint_subref ) : nb = %d is not valid",
182                 CLASS_TYPE_STR, CLASS_TYPE_STR, nb );
183        SC_REPORT_ERROR( sc_core::SC_ID_INIT_FAILED_, msg );
184    }
185    ndigits = DIV_CEIL(nbits);
186#   ifdef SC_MAX_NBITS
187        test_bound(nb);
188#    else
189        digit = new sc_digit[ndigits];
190#    endif
191    makezero();
192    *this = v.to_uint64();
193}
194
195CLASS_TYPE::CLASS_TYPE(const sc_signed_subref_r& v) :
196    sc_value_base(v), sgn(), nbits(), ndigits(), digit()
197{
198    int nb = v.length();
199    sgn = default_sign();
200    if( nb > 0 ) {
201        nbits = num_bits( nb );
202    } else {
203        char msg[BUFSIZ];
204        std::sprintf( msg, "%s::%s( sc_signed_subref ) : nb = %d is not valid",
205                 CLASS_TYPE_STR, CLASS_TYPE_STR, nb );
206        SC_REPORT_ERROR( sc_core::SC_ID_INIT_FAILED_, msg );
207    }
208    ndigits = DIV_CEIL(nbits);
209#   ifdef SC_MAX_NBITS
210        test_bound(nb);
211#    else
212        digit = new sc_digit[ndigits];
213#    endif
214    makezero();
215    *this = sc_unsigned(v.m_obj_p, v.m_left, v.m_right);
216}
217
218CLASS_TYPE::CLASS_TYPE(const sc_unsigned_subref_r& v) :
219    sc_value_base(v), sgn(), nbits(), ndigits(), digit()
220{
221    int nb = v.length();
222    sgn = default_sign();
223    if( nb > 0 ) {
224        nbits = num_bits( nb );
225    } else {
226        char msg[BUFSIZ];
227        std::sprintf( msg, "%s::%s( sc_unsigned_subref ) : nb = %d is not valid",
228                 CLASS_TYPE_STR, CLASS_TYPE_STR, nb );
229        SC_REPORT_ERROR( sc_core::SC_ID_INIT_FAILED_, msg );
230    }
231    ndigits = DIV_CEIL(nbits);
232#   ifdef SC_MAX_NBITS
233        test_bound(nb);
234#    else
235        digit = new sc_digit[ndigits];
236#    endif
237    makezero();
238    *this = sc_unsigned(v.m_obj_p, v.m_left, v.m_right);
239}
240
241// ----------------------------------------------------------------------------
242//  SECTION: Public members - Concatenation support.
243// ----------------------------------------------------------------------------
244
245
246// ----------------------------------------------------------------------------
247//  SECTION: Public members - Assignment operators.
248// ----------------------------------------------------------------------------
249
250// Assignment from v of the same type.
251const CLASS_TYPE&
252CLASS_TYPE::operator=(const CLASS_TYPE& v)
253{
254  if (this != &v) {
255
256    sgn = v.sgn;
257
258    if (sgn == SC_ZERO)
259      vec_zero(ndigits, digit);
260
261    else
262      copy_digits(v.nbits, v.ndigits, v.digit);
263
264  }
265
266  return *this;
267}
268
269
270// Assignment from v of the different type.
271const CLASS_TYPE&
272CLASS_TYPE::operator=(const OTHER_CLASS_TYPE& v)
273{
274  sgn = v.sgn;
275
276  if (sgn == SC_ZERO)
277    vec_zero(ndigits, digit);
278
279  else
280    copy_digits(v.nbits, v.ndigits, v.digit);
281
282  return *this;
283}
284
285
286// Assignment from an sc_unsigned_subref_r
287const CLASS_TYPE&
288CLASS_TYPE::operator=(const sc_unsigned_subref_r& v)
289{
290  return operator=(sc_unsigned(v));
291}
292
293
294// Assignment from an sc_signed_subref_r
295const CLASS_TYPE&
296CLASS_TYPE::operator=(const sc_signed_subref_r& v)
297{
298  return operator=(sc_unsigned(v));
299}
300
301
302// ----------------------------------------------------------------------------
303//  SECTION: Input and output operators
304// ----------------------------------------------------------------------------
305
306void
307CLASS_TYPE::scan( ::std::istream& is )
308{
309    std::string s;
310    is >> s;
311    *this = s.c_str();
312}
313
314
315// ----------------------------------------------------------------------------
316//  SECTION: PLUS operators: +, +=, ++
317// ----------------------------------------------------------------------------
318
319// Cases to consider when computing u + v:
320// 1. 0 + v = v
321// 2. u + 0 = u
322// 3. if sgn(u) == sgn(v)
323//    3.1 u + v = +(u + v) = sgn(u) * (u + v)
324//    3.2 (-u) + (-v) = -(u + v) = sgn(u) * (u + v)
325// 4. if sgn(u) != sgn(v)
326//    4.1 u + (-v) = u - v = sgn(u) * (u - v)
327//    4.2 (-u) + v = -(u - v) ==> sgn(u) * (u - v)
328//
329// Specialization of above cases for computing ++u or u++:
330// 1. 0 + 1 = 1
331// 3. u + 1 = u + 1 = sgn(u) * (u + 1)
332// 4. (-u) + 1 = -(u - 1) = sgn(u) * (u - 1)
333
334const CLASS_TYPE&
335CLASS_TYPE::operator+=(const CLASS_TYPE& v)
336{
337  // u = *this
338
339  if (sgn == SC_ZERO) // case 1
340    return (*this = v);
341
342  if (v.sgn == SC_ZERO) // case 2
343    return *this;
344
345  // cases 3 and 4
346  add_on_help(sgn, nbits, ndigits, digit,
347              v.sgn, v.nbits, v.ndigits, v.digit);
348
349  convert_SM_to_2C_to_SM();
350
351  return *this;
352}
353
354
355const CLASS_TYPE&
356CLASS_TYPE::operator+=(const OTHER_CLASS_TYPE& v)
357{
358  // u = *this
359
360  if (sgn == SC_ZERO) // case 1
361    return (*this = v);
362
363  if (v.sgn == SC_ZERO) // case 2
364    return *this;
365
366  // cases 3 and 4
367  add_on_help(sgn, nbits, ndigits, digit,
368              v.sgn, v.nbits, v.ndigits, v.digit);
369
370  convert_SM_to_2C_to_SM();
371
372  return *this;
373}
374
375
376CLASS_TYPE&
377CLASS_TYPE::operator++() // prefix
378{
379    *this = *this + 1;
380    return *this;
381}
382
383
384const CLASS_TYPE
385CLASS_TYPE::operator++(int) // postfix
386{
387  // Copy digit into d.
388
389#ifdef SC_MAX_NBITS
390  sc_digit d[MAX_NDIGITS];
391#else
392  sc_digit *d = new sc_digit[ndigits];
393#endif
394
395  small_type s = sgn;
396
397  vec_copy(ndigits, d, digit);
398
399  *this = *this + 1;
400
401  return CLASS_TYPE(s, nbits, ndigits, d);
402}
403
404
405const CLASS_TYPE&
406CLASS_TYPE::operator+=(int64 v)
407{
408  // u = *this
409
410  if (sgn == SC_ZERO)  // case 1
411    return (*this = v);
412
413  if (v == 0) // case 2
414    return *this;
415
416  CONVERT_INT64(v);
417
418  // cases 3 and 4
419  add_on_help(sgn, nbits, ndigits, digit,
420              vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd);
421
422  convert_SM_to_2C_to_SM();
423
424  return *this;
425}
426
427
428const CLASS_TYPE&
429CLASS_TYPE::operator+=(uint64 v)
430{
431  // u = *this
432
433  if (sgn == SC_ZERO)  // case 1
434    return (*this = v);
435
436  if (v == 0)  // case 2
437    return *this;
438
439  CONVERT_INT64(v);
440
441  // cases 3 and 4
442  add_on_help(sgn, nbits, ndigits, digit,
443              vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd);
444
445  convert_SM_to_2C_to_SM();
446
447  return *this;
448}
449
450
451const CLASS_TYPE&
452CLASS_TYPE::operator+=(long v)
453{
454  // u = *this
455
456  if (sgn == SC_ZERO)  // case 1
457    return (*this = v);
458
459  if (v == 0) // case 2
460    return *this;
461
462  CONVERT_LONG(v);
463
464  // cases 3 and 4
465  add_on_help(sgn, nbits, ndigits, digit,
466              vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd);
467
468  convert_SM_to_2C_to_SM();
469
470  return *this;
471}
472
473
474const CLASS_TYPE&
475CLASS_TYPE::operator+=(unsigned long v)
476{
477  // u = *this
478
479  if (sgn == SC_ZERO)  // case 1
480    return (*this = v);
481
482  if (v == 0)  // case 2
483    return *this;
484
485  CONVERT_LONG(v);
486
487  // cases 3 and 4
488  add_on_help(sgn, nbits, ndigits, digit,
489              vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd);
490
491  convert_SM_to_2C_to_SM();
492
493  return *this;
494}
495
496
497// ----------------------------------------------------------------------------
498//  SECTION: MINUS operators: -, -=, --
499// ----------------------------------------------------------------------------
500
501// Cases to consider when computing u + v:
502// 1. u - 0 = u
503// 2. 0 - v = -v
504// 3. if sgn(u) != sgn(v)
505//    3.1 u - (-v) = u + v = sgn(u) * (u + v)
506//    3.2 (-u) - v = -(u + v) ==> sgn(u) * (u + v)
507// 4. if sgn(u) == sgn(v)
508//    4.1 u - v = +(u - v) = sgn(u) * (u - v)
509//    4.2 (-u) - (-v) = -(u - v) = sgn(u) * (u - v)
510//
511// Specialization of above cases for computing --u or u--:
512// 1. 0 - 1 = -1
513// 3. (-u) - 1 = -(u + 1) = sgn(u) * (u + 1)
514// 4. u - 1 = u - 1 = sgn(u) * (u - 1)
515
516const CLASS_TYPE&
517CLASS_TYPE::operator-=(const CLASS_TYPE& v)
518{
519  // u = *this
520
521  if (v.sgn == SC_ZERO)  // case 1
522    return *this;
523
524  if (sgn == SC_ZERO) { // case 2
525
526    sgn = -v.sgn;
527    copy_digits(v.nbits, v.ndigits, v.digit);
528
529  }
530  else {
531
532    // cases 3 and 4
533    add_on_help(sgn, nbits, ndigits, digit,
534                -v.sgn, v.nbits, v.ndigits, v.digit);
535
536    convert_SM_to_2C_to_SM();
537
538  }
539
540  return *this;
541}
542
543
544const CLASS_TYPE&
545CLASS_TYPE::operator-=(const OTHER_CLASS_TYPE& v)
546{
547  // u = *this
548
549  if (v.sgn == SC_ZERO)  // case 1
550    return *this;
551
552  if (sgn == SC_ZERO) { // case 2
553
554    sgn = -v.sgn;
555    copy_digits(v.nbits, v.ndigits, v.digit);
556
557  }
558  else {
559
560    // cases 3 and 4
561    add_on_help(sgn, nbits, ndigits, digit,
562                -v.sgn, v.nbits, v.ndigits, v.digit);
563
564    convert_SM_to_2C_to_SM();
565
566  }
567
568  return *this;
569}
570
571
572CLASS_TYPE&
573CLASS_TYPE::operator--() // prefix
574{
575    *this = *this - 1;
576    return *this;
577}
578
579
580const CLASS_TYPE
581CLASS_TYPE::operator--(int) // postfix
582{
583  // Copy digit into d.
584
585#ifdef SC_MAX_NBITS
586  sc_digit d[MAX_NDIGITS];
587#else
588  sc_digit *d = new sc_digit[ndigits];
589#endif
590
591  small_type s = sgn;
592
593  vec_copy(ndigits, d, digit);
594
595  *this = *this - 1;
596
597  return CLASS_TYPE(s, nbits, ndigits, d);
598}
599
600
601const CLASS_TYPE&
602CLASS_TYPE::operator-=(int64 v)
603{
604  // u = *this
605
606  if (v == 0) // case 1
607    return *this;
608
609  if (sgn == SC_ZERO) // case 2
610    return (*this = -v);
611
612  CONVERT_INT64(v);
613
614  // cases 3 and 4
615  add_on_help(sgn, nbits, ndigits, digit,
616              -vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd);
617
618  convert_SM_to_2C_to_SM();
619
620  return *this;
621}
622
623
624const CLASS_TYPE&
625CLASS_TYPE::operator-=(uint64 v)
626{
627  // u = *this
628
629  if (v == 0) // case 1
630    return *this;
631
632  int64 v2 = (int64) v;
633
634  if (sgn == SC_ZERO) // case 2
635    return (*this = -v2);
636
637  CONVERT_INT64(v);
638
639  // cases 3 and 4
640  add_on_help(sgn, nbits, ndigits, digit,
641              -vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd);
642
643  convert_SM_to_2C_to_SM();
644
645  return *this;
646}
647
648
649const CLASS_TYPE&
650CLASS_TYPE::operator-=(long v)
651{
652  // u = *this
653
654  if (v == 0) // case 1
655    return *this;
656
657  if (sgn == SC_ZERO) // case 2
658    return (*this = -v);
659
660  CONVERT_LONG(v);
661
662  // cases 3 and 4
663  add_on_help(sgn, nbits, ndigits, digit,
664              -vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd);
665
666  convert_SM_to_2C_to_SM();
667
668  return *this;
669}
670
671
672const CLASS_TYPE&
673CLASS_TYPE::operator-=(unsigned long v)
674{
675  // u = *this
676
677  if (v == 0) // case 1
678    return *this;
679
680  long v2 = (long) v;
681
682  if (sgn == SC_ZERO) // case 2
683    return (*this = -v2);
684
685  CONVERT_LONG(v);
686
687  // cases 3 and 4
688  add_on_help(sgn, nbits, ndigits, digit,
689              -vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd);
690
691  convert_SM_to_2C_to_SM();
692
693  return *this;
694}
695
696
697// ----------------------------------------------------------------------------
698//  SECTION: MULTIPLICATION operators: *, *=
699// ----------------------------------------------------------------------------
700
701// Cases to consider when computing u * v:
702// 1. u * 0 = 0 * v = 0
703// 2. 1 * v = v and -1 * v = -v
704// 3. u * 1 = u and u * -1 = -u
705// 4. u * v = u * v
706
707const CLASS_TYPE&
708CLASS_TYPE::operator*=(const CLASS_TYPE& v)
709{
710  // u = *this
711
712  sgn = mul_signs(sgn, v.sgn);
713
714  if (sgn == SC_ZERO) // case 1
715    vec_zero(ndigits, digit);
716
717  else
718    // cases 2-4
719    MUL_ON_HELPER(sgn,  nbits, ndigits, digit,
720                  v.nbits, v.ndigits, v.digit);
721
722  return *this;
723}
724
725
726const CLASS_TYPE&
727CLASS_TYPE::operator*=(const OTHER_CLASS_TYPE& v)
728{
729  // u = *this
730
731  sgn = mul_signs(sgn, v.sgn);
732
733  if (sgn == SC_ZERO) // case 1
734    vec_zero(ndigits, digit);
735
736  else
737    // cases 2-4
738    MUL_ON_HELPER(sgn,  nbits, ndigits, digit,
739                  v.nbits, v.ndigits, v.digit);
740
741  return *this;
742}
743
744
745const CLASS_TYPE&
746CLASS_TYPE::operator*=(int64 v)
747{
748  // u = *this
749
750  sgn = mul_signs(sgn, get_sign(v));
751
752  if (sgn == SC_ZERO) // case 1
753    vec_zero(ndigits, digit);
754
755  else {  // cases 2-4
756
757    CONVERT_INT64_2(v);
758
759    MUL_ON_HELPER(sgn,  nbits, ndigits, digit,
760                  BITS_PER_UINT64, DIGITS_PER_UINT64, vd);
761
762  }
763
764  return *this;
765}
766
767
768const CLASS_TYPE&
769CLASS_TYPE::operator*=(uint64 v)
770{
771  // u = *this
772
773  sgn = mul_signs(sgn, get_sign(v));
774
775  if (sgn == SC_ZERO) // case 1
776    vec_zero(ndigits, digit);
777
778  else { // cases 2-4
779
780    CONVERT_INT64_2(v);
781
782    MUL_ON_HELPER(sgn,  nbits, ndigits, digit,
783                  BITS_PER_UINT64, DIGITS_PER_UINT64, vd);
784
785  }
786
787  return *this;
788}
789
790
791const CLASS_TYPE&
792CLASS_TYPE::operator*=(long v)
793{
794  // u = *this
795
796  sgn = mul_signs(sgn, get_sign(v));
797
798  if (sgn == SC_ZERO) // case 1
799    vec_zero(ndigits, digit);
800
801  else {   // cases 2-4
802
803    CONVERT_LONG_2(v);
804
805    MUL_ON_HELPER(sgn,  nbits, ndigits, digit,
806                  BITS_PER_ULONG, DIGITS_PER_ULONG, vd);
807
808  }
809
810  return *this;
811}
812
813
814const CLASS_TYPE&
815CLASS_TYPE::operator*=(unsigned long v)
816{
817  // u = *this
818
819  sgn = mul_signs(sgn, get_sign(v));
820
821  if (sgn == SC_ZERO) // case 1
822    vec_zero(ndigits, digit);
823
824  else {  // cases 2-4
825
826    CONVERT_LONG_2(v);
827
828    MUL_ON_HELPER(sgn,  nbits, ndigits, digit,
829                  BITS_PER_ULONG, DIGITS_PER_ULONG, vd);
830
831  }
832
833  return *this;
834}
835
836
837// ----------------------------------------------------------------------------
838//  SECTION: DIVISION operators: /, /=
839// ----------------------------------------------------------------------------
840
841// Cases to consider when finding the quotient q = floor(u/v):
842// Note that u = q * v + r for r < q.
843// 1. 0 / 0 or u / 0 => error
844// 2. 0 / v => 0 = 0 * v + 0
845// 3. u / v && u = v => u = 1 * u + 0  - u or v can be 1 or -1
846// 4. u / v && u < v => u = 0 * v + u  - u can be 1 or -1
847// 5. u / v && u > v => u = q * v + r  - v can be 1 or -1
848
849const CLASS_TYPE&
850CLASS_TYPE::operator/=(const CLASS_TYPE& v)
851{
852  sgn = mul_signs(sgn, v.sgn);
853
854  if (sgn == SC_ZERO) {
855
856    div_by_zero(v.sgn); // case 1
857    vec_zero(ndigits, digit);  // case 2
858
859  }
860  else  // other cases
861    DIV_ON_HELPER(sgn, nbits, ndigits, digit,
862                  v.nbits, v.ndigits, v.digit);
863
864  return *this;
865}
866
867
868const CLASS_TYPE&
869CLASS_TYPE::operator/=(const OTHER_CLASS_TYPE& v)
870{
871  sgn = mul_signs(sgn, v.sgn);
872
873  if (sgn == SC_ZERO) {
874
875    div_by_zero(v.sgn); // case 1
876    vec_zero(ndigits, digit);  // case 2
877
878  }
879  else  // other cases
880    DIV_ON_HELPER(sgn, nbits, ndigits, digit,
881                  v.nbits, v.ndigits, v.digit);
882
883  return *this;
884}
885
886
887const CLASS_TYPE&
888CLASS_TYPE::operator/=(int64 v)
889{
890  // u = *this
891
892  sgn = mul_signs(sgn, get_sign(v));
893
894  if (sgn == SC_ZERO) {
895
896    div_by_zero(v);  // case 1
897    vec_zero(ndigits, digit); // case 2
898
899  }
900  else {
901
902    CONVERT_INT64_2(v);
903
904    // other cases
905    DIV_ON_HELPER(sgn, nbits, ndigits, digit,
906                  BITS_PER_UINT64, DIGITS_PER_UINT64, vd);
907
908  }
909
910  return *this;
911}
912
913
914const CLASS_TYPE&
915CLASS_TYPE::operator/=(uint64 v)
916{
917  // u = *this
918
919  sgn = mul_signs(sgn, get_sign(v));
920
921  if (sgn == SC_ZERO) {
922
923    div_by_zero(v);  // case 1
924    vec_zero(ndigits, digit);  // case 2
925
926  }
927  else {
928
929    CONVERT_INT64_2(v);
930
931    // other cases
932    DIV_ON_HELPER(sgn, nbits, ndigits, digit,
933                  BITS_PER_UINT64, DIGITS_PER_UINT64, vd);
934
935  }
936
937  return *this;
938}
939
940
941const CLASS_TYPE&
942CLASS_TYPE::operator/=(long v)
943{
944  // u = *this
945
946  sgn = mul_signs(sgn, get_sign(v));
947
948  if (sgn == SC_ZERO) {
949
950    div_by_zero(v);  // case 1
951    vec_zero(ndigits, digit); // case 2
952
953  }
954  else {
955
956    CONVERT_LONG_2(v);
957
958    // other cases
959    DIV_ON_HELPER(sgn,  nbits, ndigits, digit,
960                  BITS_PER_ULONG, DIGITS_PER_ULONG, vd);
961
962  }
963
964  return *this;
965}
966
967
968const CLASS_TYPE&
969CLASS_TYPE::operator/=(unsigned long v)
970{
971  // u = *this
972
973  sgn = mul_signs(sgn, get_sign(v));
974
975  if (sgn == SC_ZERO) {
976
977    div_by_zero(v);  // case 1
978    vec_zero(ndigits, digit);  // case 2
979
980  }
981  else {
982
983    CONVERT_LONG_2(v);
984
985    // other cases
986    DIV_ON_HELPER(sgn,  nbits, ndigits, digit,
987                  BITS_PER_ULONG, DIGITS_PER_ULONG, vd);
988
989  }
990
991  return *this;
992}
993
994
995// ----------------------------------------------------------------------------
996//  SECTION: MOD operators: %, %=.
997// ----------------------------------------------------------------------------
998
999// Cases to consider when finding the remainder r = u % v:
1000// Note that u = q * v + r for r < q.
1001// 1. 0 % 0 or u % 0 => error
1002// 2. 0 % v => 0 = 0 * v + 0
1003// 3. u % v && u = v => u = 1 * u + 0  - u or v can be 1 or -1
1004// 4. u % v && u < v => u = 0 * v + u  - u can be 1 or -1
1005// 5. u % v && u > v => u = q * v + r  - v can be 1 or -1
1006
1007const CLASS_TYPE&
1008CLASS_TYPE::operator%=(const CLASS_TYPE& v)
1009{
1010  if ((sgn == SC_ZERO) || (v.sgn == SC_ZERO)) {
1011
1012    div_by_zero(v.sgn);  // case 1
1013    vec_zero(ndigits, digit);  // case 2
1014
1015  }
1016  else // other cases
1017    MOD_ON_HELPER(sgn, nbits, ndigits, digit,
1018                  v.nbits, v.ndigits, v.digit);
1019
1020  return *this;
1021}
1022
1023
1024const CLASS_TYPE&
1025CLASS_TYPE::operator%=(const OTHER_CLASS_TYPE& v)
1026{
1027  if ((sgn == SC_ZERO) || (v.sgn == SC_ZERO)) {
1028
1029    div_by_zero(v.sgn);  // case 1
1030    vec_zero(ndigits, digit);  // case 2
1031
1032  }
1033  else  // other cases
1034    MOD_ON_HELPER(sgn, nbits, ndigits, digit,
1035                  v.nbits, v.ndigits, v.digit);
1036
1037  return *this;
1038}
1039
1040
1041const CLASS_TYPE&
1042CLASS_TYPE::operator%=(int64 v)
1043{
1044  small_type vs = get_sign(v);
1045
1046  if ((sgn == SC_ZERO) || (vs == SC_ZERO)) {
1047
1048    div_by_zero(v);  // case 1
1049    vec_zero(ndigits, digit);  // case 2
1050
1051  }
1052  else {
1053
1054    CONVERT_INT64_2(v);
1055
1056    // other cases
1057    MOD_ON_HELPER(sgn, nbits, ndigits, digit,
1058                  BITS_PER_UINT64, DIGITS_PER_UINT64, vd);
1059
1060  }
1061
1062  return *this;
1063}
1064
1065
1066const CLASS_TYPE&
1067CLASS_TYPE::operator%=(uint64 v)
1068{
1069  if ((sgn == SC_ZERO) || (v == 0)) {
1070
1071    div_by_zero(v);  // case 1
1072    vec_zero(ndigits, digit);  // case 2
1073
1074  }
1075  else {
1076
1077    CONVERT_INT64_2(v);
1078
1079    // other cases
1080    MOD_ON_HELPER(sgn, nbits, ndigits, digit,
1081                  BITS_PER_UINT64, DIGITS_PER_UINT64, vd);
1082
1083  }
1084
1085  return *this;
1086}
1087
1088
1089const CLASS_TYPE&
1090CLASS_TYPE::operator%=(long v)
1091{
1092  small_type vs = get_sign(v);
1093
1094  if ((sgn == SC_ZERO) || (vs == SC_ZERO)) {
1095
1096    div_by_zero(v);  // case 1
1097    vec_zero(ndigits, digit);  // case 2
1098
1099  }
1100  else {
1101
1102    CONVERT_LONG_2(v);
1103
1104    // other cases
1105    MOD_ON_HELPER(sgn, nbits, ndigits, digit,
1106                  BITS_PER_ULONG, DIGITS_PER_ULONG, vd);
1107
1108  }
1109
1110  return *this;
1111}
1112
1113
1114const CLASS_TYPE&
1115CLASS_TYPE::operator%=(unsigned long v)
1116{
1117  if ((sgn == SC_ZERO) || (v == 0)) {
1118
1119    div_by_zero(v);  // case 1
1120    vec_zero(ndigits, digit);  // case 2
1121
1122  }
1123  else {
1124
1125    CONVERT_LONG_2(v);
1126
1127    // other cases
1128    MOD_ON_HELPER(sgn, nbits, ndigits, digit,
1129                  BITS_PER_ULONG, DIGITS_PER_ULONG, vd);
1130
1131  }
1132
1133  return *this;
1134}
1135
1136
1137// ----------------------------------------------------------------------------
1138//  SECTION: Bitwise AND operators: &, &=
1139// ----------------------------------------------------------------------------
1140
1141// Cases to consider when computing u & v:
1142// 1. u & 0 = 0 & v = 0
1143// 2. u & v => sgn = +
1144// 3. (-u) & (-v) => sgn = -
1145// 4. u & (-v) => sgn = +
1146// 5. (-u) & v => sgn = +
1147
1148const CLASS_TYPE&
1149CLASS_TYPE::operator&=(const CLASS_TYPE& v)
1150{
1151  // u = *this
1152
1153  if ((sgn == SC_ZERO) || (v.sgn == SC_ZERO)) // case 1
1154    makezero();
1155
1156  else  {  // other cases
1157
1158    and_on_help(sgn, nbits, ndigits, digit,
1159                v.sgn, v.nbits, v.ndigits, v.digit);
1160
1161    convert_2C_to_SM();
1162
1163  }
1164
1165  return *this;
1166}
1167
1168
1169const CLASS_TYPE&
1170CLASS_TYPE::operator&=(const OTHER_CLASS_TYPE& v)
1171{
1172  // u = *this
1173
1174  if ((sgn == SC_ZERO) || (v.sgn == SC_ZERO)) // case 1
1175    makezero();
1176
1177  else {  // other cases
1178
1179    and_on_help(sgn, nbits, ndigits, digit,
1180                v.sgn, v.nbits, v.ndigits, v.digit);
1181
1182    convert_2C_to_SM();
1183
1184  }
1185
1186  return *this;
1187}
1188
1189
1190const CLASS_TYPE&
1191CLASS_TYPE::operator&=(int64 v)
1192{
1193  // u = *this
1194
1195  if ((sgn == SC_ZERO) || (v == 0)) // case 1
1196    makezero();
1197
1198  else {    // other cases
1199
1200    CONVERT_INT64(v);
1201
1202    and_on_help(sgn, nbits, ndigits, digit,
1203                vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd);
1204
1205    convert_2C_to_SM();
1206
1207  }
1208
1209  return *this;
1210}
1211
1212
1213const CLASS_TYPE&
1214CLASS_TYPE::operator&=(uint64 v)
1215{
1216  // u = *this
1217
1218  if ((sgn == SC_ZERO) || (v == 0))  // case 1
1219    makezero();
1220
1221  else {  // other cases
1222
1223    CONVERT_INT64(v);
1224
1225    and_on_help(sgn, nbits, ndigits, digit,
1226                vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd);
1227
1228    convert_2C_to_SM();
1229
1230  }
1231
1232  return *this;
1233}
1234
1235
1236const CLASS_TYPE&
1237CLASS_TYPE::operator&=(long v)
1238{
1239  // u = *this
1240
1241  if ((sgn == SC_ZERO) || (v == 0))  // case 1
1242    makezero();
1243
1244  else {      // other cases
1245
1246    CONVERT_LONG(v);
1247
1248    and_on_help(sgn, nbits, ndigits, digit,
1249                vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd);
1250
1251    convert_2C_to_SM();
1252
1253  }
1254
1255  return *this;
1256}
1257
1258
1259const CLASS_TYPE&
1260CLASS_TYPE::operator&=(unsigned long v)
1261{
1262  // u = *this
1263
1264  if ((sgn == SC_ZERO) || (v == 0))  // case 1
1265    makezero();
1266
1267  else {  // other cases
1268
1269    CONVERT_LONG(v);
1270
1271    and_on_help(sgn, nbits, ndigits, digit,
1272                vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd);
1273
1274    convert_2C_to_SM();
1275
1276  }
1277
1278  return *this;
1279}
1280
1281
1282// ----------------------------------------------------------------------------
1283//  SECTION: Bitwise OR operators: |, |=
1284// ----------------------------------------------------------------------------
1285
1286// Cases to consider when computing u | v:
1287// 1. u | 0 = u
1288// 2. 0 | v = v
1289// 3. u | v => sgn = +
1290// 4. (-u) | (-v) => sgn = -
1291// 5. u | (-v) => sgn = -
1292// 6. (-u) | v => sgn = -
1293
1294const CLASS_TYPE&
1295CLASS_TYPE::operator|=(const CLASS_TYPE& v)
1296{
1297  if (v.sgn == SC_ZERO)  // case 1
1298    return *this;
1299
1300  if (sgn == SC_ZERO)  // case 2
1301    return (*this = v);
1302
1303  // other cases
1304  or_on_help(sgn, nbits, ndigits, digit,
1305             v.sgn, v.nbits, v.ndigits, v.digit);
1306
1307  convert_2C_to_SM();
1308
1309  return *this;
1310}
1311
1312
1313const CLASS_TYPE&
1314CLASS_TYPE::operator|=(const OTHER_CLASS_TYPE& v)
1315{
1316  if (v.sgn == SC_ZERO)  // case 1
1317    return *this;
1318
1319  if (sgn == SC_ZERO)  // case 2
1320    return (*this = v);
1321
1322  // other cases
1323  or_on_help(sgn, nbits, ndigits, digit,
1324             v.sgn, v.nbits, v.ndigits, v.digit);
1325
1326  convert_2C_to_SM();
1327
1328  return *this;
1329}
1330
1331
1332const CLASS_TYPE&
1333CLASS_TYPE::operator|=(int64 v)
1334{
1335  if (v == 0)  // case 1
1336    return *this;
1337
1338  if (sgn == SC_ZERO)  // case 2
1339    return (*this = v);
1340
1341  // other cases
1342
1343  CONVERT_INT64(v);
1344
1345  or_on_help(sgn, nbits, ndigits, digit,
1346             vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd);
1347
1348  convert_2C_to_SM();
1349
1350  return *this;
1351}
1352
1353
1354const CLASS_TYPE&
1355CLASS_TYPE::operator|=(uint64 v)
1356{
1357  if (v == 0)  // case 1
1358    return *this;
1359
1360  if (sgn == SC_ZERO)  // case 2
1361    return (*this = v);
1362
1363  // other cases
1364
1365  CONVERT_INT64(v);
1366
1367  or_on_help(sgn, nbits, ndigits, digit,
1368             vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd);
1369
1370  convert_2C_to_SM();
1371
1372  return *this;
1373}
1374
1375
1376const CLASS_TYPE&
1377CLASS_TYPE::operator|=(long v)
1378{
1379  if (v == 0)  // case 1
1380    return *this;
1381
1382  if (sgn == SC_ZERO)  // case 2
1383    return (*this = v);
1384
1385  // other cases
1386
1387  CONVERT_LONG(v);
1388
1389  or_on_help(sgn, nbits, ndigits, digit,
1390             vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd);
1391
1392  convert_2C_to_SM();
1393
1394  return *this;
1395}
1396
1397
1398const CLASS_TYPE&
1399CLASS_TYPE::operator|=(unsigned long v)
1400{
1401  if (v == 0)  // case 1
1402    return *this;
1403
1404  if (sgn == SC_ZERO)  // case 2
1405    return (*this = v);
1406
1407  // other cases
1408
1409  CONVERT_LONG(v);
1410
1411  or_on_help(sgn, nbits, ndigits, digit,
1412             vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd);
1413
1414  convert_2C_to_SM();
1415
1416  return *this;
1417}
1418
1419
1420// ----------------------------------------------------------------------------
1421//  SECTION: Bitwise XOR operators: ^, ^=
1422// ----------------------------------------------------------------------------
1423
1424// Cases to consider when computing u ^ v:
1425// Note that  u ^ v = (~u & v) | (u & ~v).
1426// 1. u ^ 0 = u
1427// 2. 0 ^ v = v
1428// 3. u ^ v => sgn = +
1429// 4. (-u) ^ (-v) => sgn = -
1430// 5. u ^ (-v) => sgn = -
1431// 6. (-u) ^ v => sgn = +
1432
1433const CLASS_TYPE&
1434CLASS_TYPE::operator^=(const CLASS_TYPE& v)
1435{
1436  // u = *this
1437
1438  if (v.sgn == SC_ZERO)  // case 1
1439    return *this;
1440
1441  if (sgn == SC_ZERO)  // case 2
1442    return (*this = v);
1443
1444  // other cases
1445  xor_on_help(sgn, nbits, ndigits, digit,
1446              v.sgn, v.nbits, v.ndigits, v.digit);
1447
1448  convert_2C_to_SM();
1449
1450  return *this;
1451}
1452
1453
1454const CLASS_TYPE&
1455CLASS_TYPE::operator^=(const OTHER_CLASS_TYPE& v)
1456{
1457  // u = *this
1458
1459  if (v.sgn == SC_ZERO)  // case 1
1460    return *this;
1461
1462  if (sgn == SC_ZERO)  // case 2
1463    return (*this = v);
1464
1465  // other cases
1466  xor_on_help(sgn, nbits, ndigits, digit,
1467              v.sgn, v.nbits, v.ndigits, v.digit);
1468
1469  convert_2C_to_SM();
1470
1471  return *this;
1472}
1473
1474
1475const CLASS_TYPE&
1476CLASS_TYPE::operator^=(int64 v)
1477{
1478  if (v == 0)  // case 1
1479    return *this;
1480
1481  if (sgn == SC_ZERO)  // case 2
1482    return (*this = v);
1483
1484  // other cases
1485
1486  CONVERT_INT64(v);
1487
1488  xor_on_help(sgn, nbits, ndigits, digit,
1489              vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd);
1490
1491  convert_2C_to_SM();
1492
1493  return *this;
1494}
1495
1496
1497const CLASS_TYPE&
1498CLASS_TYPE::operator^=(uint64 v)
1499{
1500  if (v == 0)  // case 1
1501    return *this;
1502
1503  if (sgn == SC_ZERO)  // case 2
1504    return (*this = v);
1505
1506  // other cases
1507
1508  CONVERT_INT64(v);
1509
1510  xor_on_help(sgn, nbits, ndigits, digit,
1511              vs, BITS_PER_UINT64, DIGITS_PER_UINT64, vd);
1512
1513  convert_2C_to_SM();
1514
1515  return *this;
1516}
1517
1518
1519const CLASS_TYPE&
1520CLASS_TYPE::operator^=(long v)
1521{
1522  if (v == 0)  // case 1
1523    return *this;
1524
1525  if (sgn == SC_ZERO)  // case 2
1526    return (*this = v);
1527
1528  // other cases
1529
1530  CONVERT_LONG(v);
1531
1532  xor_on_help(sgn, nbits, ndigits, digit,
1533              vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd);
1534
1535  convert_2C_to_SM();
1536
1537  return *this;
1538}
1539
1540
1541const CLASS_TYPE&
1542CLASS_TYPE::operator^=(unsigned long v)
1543{
1544  if (v == 0)  // case 1
1545    return *this;
1546
1547  if (sgn == SC_ZERO)  // case 2
1548    return (*this = v);
1549
1550  // other cases
1551
1552  CONVERT_LONG(v);
1553
1554  xor_on_help(sgn, nbits, ndigits, digit,
1555              vs, BITS_PER_ULONG, DIGITS_PER_ULONG, vd);
1556
1557  convert_2C_to_SM();
1558
1559  return *this;
1560}
1561
1562
1563// ----------------------------------------------------------------------------
1564//  SECTION: Bitwise NOT operator: ~
1565// ----------------------------------------------------------------------------
1566
1567CLASS_TYPE
1568operator~(const CLASS_TYPE& u)
1569{
1570  small_type s = u.sgn;
1571
1572  if (s == SC_ZERO) {
1573
1574    sc_digit d = 1;
1575    return CLASS_TYPE(SC_NEG, u.nbits, 1, &d, false);
1576
1577  }
1578
1579  int nd = u.ndigits;
1580
1581#ifdef SC_MAX_NBITS
1582  sc_digit d[MAX_NDIGITS];
1583#else
1584  sc_digit *d = new sc_digit[nd];
1585#endif
1586
1587  vec_copy(nd, d, u.digit);
1588
1589  if (s == SC_POS) {
1590
1591    s = SC_NEG;
1592    vec_add_small_on(nd, d, 1);
1593
1594  }
1595  else {
1596
1597    s = SC_POS;
1598    vec_sub_small_on(nd, d, 1);
1599
1600    if (check_for_zero(nd, d))
1601      s = SC_ZERO;
1602
1603  }
1604
1605  return CLASS_TYPE(s, u.nbits, nd, d);
1606}
1607
1608
1609// ----------------------------------------------------------------------------
1610//  SECTION: LEFT SHIFT operators: <<, <<=
1611// ----------------------------------------------------------------------------
1612
1613CLASS_TYPE
1614operator<<(const CLASS_TYPE& u, const CLASS_TYPE& v)
1615{
1616  if (v.sgn == SC_ZERO)
1617    return CLASS_TYPE(u);
1618
1619#ifdef SC_SIGNED
1620  if (v.sgn == SC_NEG)
1621    return CLASS_TYPE(u);
1622#endif
1623
1624  return operator<<(u, v.to_ulong());
1625}
1626
1627
1628const CLASS_TYPE&
1629CLASS_TYPE::operator<<=(const CLASS_TYPE& v)
1630{
1631  if (v.sgn == SC_ZERO)
1632    return *this;
1633
1634#ifdef SC_SIGNED
1635  if (v.sgn == SC_NEG)
1636    return *this;
1637#endif
1638
1639  return operator<<=(v.to_ulong());
1640}
1641
1642
1643const CLASS_TYPE&
1644CLASS_TYPE::operator<<=(const OTHER_CLASS_TYPE& v)
1645{
1646  if (v.sgn == SC_ZERO)
1647    return *this;
1648
1649#ifdef SC_UNSIGNED
1650  if (v.sgn == SC_NEG)
1651    return *this;
1652#endif
1653
1654  return operator<<=(v.to_ulong());
1655}
1656
1657
1658CLASS_TYPE
1659operator<<(const CLASS_TYPE& u, int64 v)
1660{
1661  if (v <= 0)
1662    return CLASS_TYPE(u);
1663
1664  return operator<<(u, (unsigned long) v);
1665}
1666
1667
1668CLASS_TYPE
1669operator<<(const CLASS_TYPE& u, uint64 v)
1670{
1671  if (v == 0)
1672    return CLASS_TYPE(u);
1673
1674  return operator<<(u, (unsigned long) v);
1675}
1676
1677
1678const CLASS_TYPE&
1679CLASS_TYPE::operator<<=(int64 v)
1680{
1681  if (v <= 0)
1682    return *this;
1683
1684  return operator<<=((unsigned long) v);
1685}
1686
1687
1688const CLASS_TYPE&
1689CLASS_TYPE::operator<<=(uint64 v)
1690{
1691  if (v == 0)
1692    return *this;
1693
1694  return operator<<=((unsigned long) v);
1695}
1696
1697
1698CLASS_TYPE
1699operator<<(const CLASS_TYPE& u, long v)
1700{
1701  if (v <= 0)
1702    return CLASS_TYPE(u);
1703
1704  return operator<<(u, (unsigned long) v);
1705}
1706
1707CLASS_TYPE
1708operator<<(const CLASS_TYPE& u, unsigned long v)
1709{
1710  if (v == 0)
1711    return CLASS_TYPE(u);
1712
1713  if (u.sgn == SC_ZERO)
1714    return CLASS_TYPE(u);
1715
1716  int nb = u.nbits + v;
1717  int nd = DIV_CEIL(nb);
1718
1719#ifdef SC_MAX_NBITS
1720  test_bound(nb);
1721  sc_digit d[MAX_NDIGITS];
1722#else
1723  sc_digit *d = new sc_digit[nd];
1724#endif
1725
1726  vec_copy_and_zero(nd, d, u.ndigits, u.digit);
1727
1728  convert_SM_to_2C(u.sgn, nd, d);
1729
1730  vec_shift_left(nd, d, v);
1731
1732  small_type s = convert_signed_2C_to_SM(nb, nd, d);
1733
1734  return CLASS_TYPE(s, nb, nd, d);
1735}
1736
1737
1738const CLASS_TYPE&
1739CLASS_TYPE::operator<<=(long v)
1740{
1741  if (v <= 0)
1742    return *this;
1743
1744  return operator<<=((unsigned long) v);
1745}
1746
1747
1748const CLASS_TYPE&
1749CLASS_TYPE::operator<<=(unsigned long v)
1750{
1751  if (v == 0)
1752    return *this;
1753
1754  if (sgn == SC_ZERO)
1755    return *this;
1756
1757  convert_SM_to_2C();
1758
1759  vec_shift_left(ndigits, digit, v);
1760
1761  convert_2C_to_SM();
1762
1763  return *this;
1764}
1765
1766
1767// ----------------------------------------------------------------------------
1768//  SECTION: RIGHT SHIFT operators: >>, >>=
1769// ----------------------------------------------------------------------------
1770
1771CLASS_TYPE
1772operator>>(const CLASS_TYPE& u, const CLASS_TYPE& v)
1773{
1774  if (v.sgn == SC_ZERO)
1775    return CLASS_TYPE(u);
1776
1777#ifdef SC_SIGNED
1778  if (v.sgn == SC_NEG)
1779    return CLASS_TYPE(u);
1780#endif
1781
1782  return operator>>(u, v.to_long());
1783}
1784
1785
1786const CLASS_TYPE&
1787CLASS_TYPE::operator>>=(const CLASS_TYPE& v)
1788{
1789  if (v.sgn == SC_ZERO)
1790    return *this;
1791
1792#ifdef SC_SIGNED
1793  if (v.sgn == SC_NEG)
1794    return *this;
1795#endif
1796
1797  return operator>>=(v.to_long());
1798}
1799
1800
1801const CLASS_TYPE&
1802CLASS_TYPE::operator>>=(const OTHER_CLASS_TYPE& v)
1803{
1804  if (v.sgn == SC_ZERO)
1805    return *this;
1806
1807#ifdef SC_UNSIGNED
1808  if (v.sgn == SC_NEG)
1809    return *this;
1810#endif
1811
1812  return operator>>=(v.to_ulong());
1813}
1814
1815
1816CLASS_TYPE
1817operator>>(const CLASS_TYPE& u, int64 v)
1818{
1819  if (v <= 0)
1820    return CLASS_TYPE(u);
1821
1822  return operator>>(u, (unsigned long) v);
1823}
1824
1825
1826CLASS_TYPE
1827operator>>(const CLASS_TYPE& u, uint64 v)
1828{
1829  if (v == 0)
1830    return CLASS_TYPE(u);
1831
1832  return operator>>(u, (unsigned long) v);
1833}
1834
1835const CLASS_TYPE&
1836CLASS_TYPE::operator>>=(int64 v)
1837{
1838  if (v <= 0)
1839    return *this;
1840
1841  return operator>>=((unsigned long) v);
1842}
1843
1844
1845const CLASS_TYPE&
1846CLASS_TYPE::operator>>=(uint64 v)
1847{
1848  if (v == 0)
1849    return *this;
1850
1851  return operator>>=((unsigned long) v);
1852}
1853
1854
1855CLASS_TYPE
1856operator>>(const CLASS_TYPE& u, long v)
1857{
1858  if (v <= 0)
1859    return CLASS_TYPE(u);
1860
1861  return operator>>(u, (unsigned long) v);
1862}
1863
1864
1865CLASS_TYPE
1866operator>>(const CLASS_TYPE& u, unsigned long v)
1867{
1868  if (v == 0)
1869    return CLASS_TYPE(u);
1870
1871  if (u.sgn == SC_ZERO)
1872    return CLASS_TYPE(u);
1873
1874  int nb = u.nbits;
1875  int nd = u.ndigits;
1876
1877#ifdef SC_MAX_NBITS
1878  sc_digit d[MAX_NDIGITS];
1879#else
1880  sc_digit *d = new sc_digit[nd];
1881#endif
1882
1883  vec_copy(nd, d, u.digit);
1884
1885  convert_SM_to_2C(u.sgn, nd, d);
1886
1887  if (u.sgn == SC_NEG)
1888    vec_shift_right(nd, d, v, DIGIT_MASK);
1889  else
1890    vec_shift_right(nd, d, v, 0);
1891
1892  small_type s = convert_signed_2C_to_SM(nb, nd, d);
1893
1894  return CLASS_TYPE(s, nb, nd, d);
1895}
1896
1897
1898const CLASS_TYPE&
1899CLASS_TYPE::operator>>=(long v)
1900{
1901  if (v <= 0)
1902    return *this;
1903
1904  return operator>>=((unsigned long) v);
1905}
1906
1907
1908const CLASS_TYPE&
1909CLASS_TYPE::operator>>=(unsigned long v)
1910{
1911  if (v == 0)
1912    return *this;
1913
1914  if (sgn == SC_ZERO)
1915    return *this;
1916
1917  convert_SM_to_2C();
1918
1919  if (sgn == SC_NEG)
1920    vec_shift_right(ndigits, digit, v, DIGIT_MASK);
1921  else
1922    vec_shift_right(ndigits, digit, v, 0);
1923
1924  convert_2C_to_SM();
1925
1926  return *this;
1927}
1928
1929
1930// ----------------------------------------------------------------------------
1931//  SECTION: EQUAL TO operator: ==
1932// ----------------------------------------------------------------------------
1933
1934// Defined in the sc_signed.cpp and sc_unsigned.cpp.
1935
1936
1937// ----------------------------------------------------------------------------
1938//  SECTION: NOT_EQUAL operator: !=
1939// ----------------------------------------------------------------------------
1940
1941bool
1942operator!=(const CLASS_TYPE& u, const CLASS_TYPE& v)
1943{
1944  return (! operator==(u, v));
1945}
1946
1947
1948bool
1949operator!=(const CLASS_TYPE& u, int64 v)
1950{
1951  return (! operator==(u, v));
1952}
1953
1954
1955bool
1956operator!=(int64 u, const CLASS_TYPE& v)
1957{
1958  return (! operator==(u, v));
1959}
1960
1961
1962bool
1963operator!=(const CLASS_TYPE& u, uint64 v)
1964{
1965  return (! operator==(u, v));
1966}
1967
1968
1969bool
1970operator!=(uint64 u, const CLASS_TYPE& v)
1971{
1972  return (! operator==(u, v));
1973}
1974
1975
1976bool
1977operator!=(const CLASS_TYPE& u, long v)
1978{
1979  return (! operator==(u, v));
1980}
1981
1982
1983bool
1984operator!=(long u, const CLASS_TYPE& v)
1985{
1986  return (! operator==(u, v));
1987}
1988
1989
1990bool
1991operator!=(const CLASS_TYPE& u, unsigned long v)
1992{
1993  return (! operator==(u, v));
1994}
1995
1996
1997bool
1998operator!=(unsigned long u, const CLASS_TYPE& v)
1999{
2000  return (! operator==(u, v));
2001}
2002
2003
2004// ----------------------------------------------------------------------------
2005//  SECTION: LESS THAN operator: <
2006// ----------------------------------------------------------------------------
2007
2008// Defined in the sc_signed.cpp and sc_unsigned.cpp.
2009
2010
2011// ----------------------------------------------------------------------------
2012//  SECTION: LESS THAN or EQUAL operator: <=
2013// ----------------------------------------------------------------------------
2014
2015bool
2016operator<=(const CLASS_TYPE& u, const CLASS_TYPE& v)
2017{
2018  return (operator<(u, v) || operator==(u, v));
2019}
2020
2021
2022bool
2023operator<=(const CLASS_TYPE& u, int64 v)
2024{
2025  return (operator<(u, v) || operator==(u, v));
2026}
2027
2028
2029bool
2030operator<=(int64 u, const CLASS_TYPE& v)
2031{
2032  return (operator<(u, v) || operator==(u, v));
2033}
2034
2035
2036bool
2037operator<=(const CLASS_TYPE& u, uint64 v)
2038{
2039  return (operator<(u, v) || operator==(u, v));
2040}
2041
2042
2043bool
2044operator<=(uint64 u, const CLASS_TYPE& v)
2045{
2046  return (operator<(u, v) || operator==(u, v));
2047}
2048
2049
2050bool
2051operator<=(const CLASS_TYPE& u, long v)
2052{
2053  return (operator<(u, v) || operator==(u, v));
2054}
2055
2056
2057bool
2058operator<=(long u, const CLASS_TYPE& v)
2059{
2060  return (operator<(u, v) || operator==(u, v));
2061}
2062
2063
2064bool
2065operator<=(const CLASS_TYPE& u, unsigned long v)
2066{
2067  return (operator<(u, v) || operator==(u, v));
2068}
2069
2070
2071bool
2072operator<=(unsigned long u, const CLASS_TYPE& v)
2073{
2074  return (operator<(u, v) || operator==(u, v));
2075}
2076
2077
2078// ----------------------------------------------------------------------------
2079//  SECTION: GREATER THAN operator: >
2080// ----------------------------------------------------------------------------
2081
2082bool
2083operator>(const CLASS_TYPE& u, const CLASS_TYPE& v)
2084{
2085  return (! (operator<=(u, v)));
2086}
2087
2088
2089bool
2090operator>(const CLASS_TYPE& u, int64 v)
2091{
2092  return (! (operator<=(u, v)));
2093}
2094
2095
2096bool
2097operator>(int64 u, const CLASS_TYPE& v)
2098{
2099  return (! (operator<=(u, v)));
2100}
2101
2102
2103bool
2104operator>(const CLASS_TYPE& u, uint64 v)
2105{
2106  return (! (operator<=(u, v)));
2107}
2108
2109
2110bool
2111operator>(uint64 u, const CLASS_TYPE& v)
2112{
2113  return (! (operator<=(u, v)));
2114}
2115
2116
2117bool
2118operator>(const CLASS_TYPE& u, long v)
2119{
2120  return (! (operator<=(u, v)));
2121}
2122
2123
2124bool
2125operator>(long u, const CLASS_TYPE& v)
2126{
2127  return (! (operator<=(u, v)));
2128}
2129
2130
2131bool
2132operator>(const CLASS_TYPE& u, unsigned long v)
2133{
2134  return (! (operator<=(u, v)));
2135}
2136
2137
2138bool
2139operator>(unsigned long u, const CLASS_TYPE& v)
2140{
2141  return (! (operator<=(u, v)));
2142}
2143
2144
2145// ----------------------------------------------------------------------------
2146//  SECTION: GREATER THAN or EQUAL operator: >=
2147// ----------------------------------------------------------------------------
2148
2149bool
2150operator>=(const CLASS_TYPE& u, const CLASS_TYPE& v)
2151{
2152  return (! (operator<(u, v)));
2153}
2154
2155
2156bool
2157operator>=(const CLASS_TYPE& u, int64 v)
2158{
2159  return (! (operator<(u, v)));
2160}
2161
2162
2163bool
2164operator>=(int64 u, const CLASS_TYPE& v)
2165{
2166  return (! (operator<(u, v)));
2167}
2168
2169
2170bool
2171operator>=(const CLASS_TYPE& u, uint64 v)
2172{
2173  return (! (operator<(u, v)));
2174}
2175
2176
2177bool
2178operator>=(uint64 u, const CLASS_TYPE& v)
2179{
2180  return (! (operator<(u, v)));
2181}
2182
2183
2184bool
2185operator>=(const CLASS_TYPE& u, long v)
2186{
2187  return (! (operator<(u, v)));
2188}
2189
2190
2191bool
2192operator>=(long u, const CLASS_TYPE& v)
2193{
2194  return (! (operator<(u, v)));
2195}
2196
2197
2198bool
2199operator>=(const CLASS_TYPE& u, unsigned long v)
2200{
2201  return (! (operator<(u, v)));
2202}
2203
2204
2205bool
2206operator>=(unsigned long u, const CLASS_TYPE& v)
2207{
2208  return (! (operator<(u, v)));
2209}
2210
2211
2212// ----------------------------------------------------------------------------
2213//  SECTION: Public members - Other utils.
2214// ----------------------------------------------------------------------------
2215
2216// Convert to int64, long, or int.
2217#define TO_INTX(RET_TYPE, UP_RET_TYPE)   \
2218                                                             \
2219if (sgn == SC_ZERO)                                          \
2220return 0;                                                    \
2221                                                             \
2222int vnd = sc_min((int)DIGITS_PER_ ## UP_RET_TYPE, ndigits); \
2223                                                             \
2224RET_TYPE v = 0;                                              \
2225while (--vnd >= 0)                                           \
2226v = (v << BITS_PER_DIGIT) + digit[vnd];                      \
2227                                                             \
2228if (sgn == SC_NEG)                                           \
2229return -v;                                                   \
2230else                                                         \
2231return v;
2232
2233
2234int64
2235CLASS_TYPE::to_int64() const
2236{
2237  TO_INTX(int64, INT64);
2238}
2239
2240
2241long
2242CLASS_TYPE::to_long() const
2243{
2244  TO_INTX(long, LONG);
2245}
2246
2247
2248int
2249CLASS_TYPE::to_int() const
2250{
2251  TO_INTX(int, INT);
2252}
2253
2254
2255// Convert to unsigned int64, unsigned long or unsigned
2256// int. to_uint64, to_ulong, and to_uint have the same body except for
2257// the type of v defined inside.
2258uint64
2259CLASS_TYPE::to_uint64() const
2260{
2261  if (sgn == SC_ZERO)
2262    return 0;
2263
2264  int vnd = sc_min((int)DIGITS_PER_INT64, ndigits);
2265
2266  uint64 v = 0;
2267
2268  if (sgn == SC_NEG) {
2269
2270#ifdef SC_MAX_NBITS
2271    sc_digit d[MAX_NDIGITS];
2272#else
2273    sc_digit *d = new sc_digit[ndigits];
2274#endif
2275
2276    vec_copy(ndigits, d, digit);
2277
2278    convert_SM_to_2C_trimmed(IF_SC_SIGNED, sgn, nbits, ndigits, d);
2279
2280    while (--vnd >= 0)
2281      v = (v << BITS_PER_DIGIT) + d[vnd];
2282
2283#ifndef SC_MAX_NBITS
2284    delete [] d;
2285#endif
2286
2287  }
2288  else {
2289
2290    while (--vnd >= 0)
2291      v = (v << BITS_PER_DIGIT) + digit[vnd];
2292
2293  }
2294
2295  return v;
2296}
2297
2298
2299unsigned long
2300CLASS_TYPE::to_ulong() const
2301{
2302  if (sgn == SC_ZERO)
2303    return 0;
2304
2305  int vnd = sc_min((int)DIGITS_PER_LONG, ndigits);
2306
2307  unsigned long v = 0;
2308
2309  if (sgn == SC_NEG) {
2310
2311#ifdef SC_MAX_NBITS
2312    sc_digit d[MAX_NDIGITS];
2313#else
2314    sc_digit *d = new sc_digit[ndigits];
2315#endif
2316
2317    vec_copy(ndigits, d, digit);
2318
2319    convert_SM_to_2C_trimmed(IF_SC_SIGNED, sgn, nbits, ndigits, d);
2320
2321    while (--vnd >= 0)
2322      v = (v << BITS_PER_DIGIT) + d[vnd];
2323
2324#ifndef SC_MAX_NBITS
2325    delete [] d;
2326#endif
2327
2328  }
2329  else {
2330
2331    while (--vnd >= 0)
2332      v = (v << BITS_PER_DIGIT) + digit[vnd];
2333
2334  }
2335
2336  return v;
2337}
2338
2339
2340unsigned int
2341CLASS_TYPE::to_uint() const
2342{
2343  if (sgn == SC_ZERO)
2344    return 0;
2345
2346  int vnd = sc_min((int)DIGITS_PER_INT, ndigits);
2347
2348  unsigned int v = 0;
2349
2350  if (sgn == SC_NEG) {
2351
2352#ifdef SC_MAX_NBITS
2353    sc_digit d[MAX_NDIGITS];
2354#else
2355    sc_digit *d = new sc_digit[ndigits];
2356#endif
2357
2358    vec_copy(ndigits, d, digit);
2359
2360    convert_SM_to_2C_trimmed(IF_SC_SIGNED, sgn, nbits, ndigits, d);
2361
2362    while (--vnd >= 0)
2363      v = (v << BITS_PER_DIGIT) + d[vnd];
2364
2365#ifndef SC_MAX_NBITS
2366    delete [] d;
2367#endif
2368
2369  }
2370  else {
2371
2372    while (--vnd >= 0)
2373      v = (v << BITS_PER_DIGIT) + digit[vnd];
2374
2375  }
2376
2377  return v;
2378}
2379
2380
2381// Convert to double.
2382double
2383CLASS_TYPE::to_double() const
2384{
2385  if (sgn == SC_ZERO)
2386    return (double) 0.0;
2387
2388  int vnd = ndigits;
2389
2390  double v = 0.0;
2391  while (--vnd >= 0)
2392    v = v * DIGIT_RADIX + digit[vnd];
2393
2394  if (sgn == SC_NEG)
2395    return -v;
2396  else
2397    return v;
2398}
2399
2400
2401// Return true if the bit i is 1, false otherwise. If i is outside the
2402// bounds, return 1/0 according to the sign of the number by assuming
2403// that the number has infinite length.
2404
2405bool
2406CLASS_TYPE::test(int i) const
2407{
2408#ifdef SC_SIGNED
2409  if (check_if_outside(i)) {
2410    if (sgn == SC_NEG)
2411      return 1;
2412    else
2413      return 0;
2414  }
2415#else
2416  if (check_if_outside(i))
2417    return 0;
2418#endif
2419
2420  int bit_num = bit_ord(i);
2421  int digit_num = digit_ord(i);
2422
2423  if (sgn == SC_NEG) {
2424
2425#ifdef SC_MAX_NBITS
2426    sc_digit d[MAX_NDIGITS];
2427#else
2428    sc_digit *d = new sc_digit[ndigits];
2429#endif
2430
2431    vec_copy(ndigits, d, digit);
2432    vec_complement(ndigits, d);
2433    bool val = ((d[digit_num] & one_and_zeros(bit_num)) != 0);
2434
2435#ifndef SC_MAX_NBITS
2436    delete [] d;
2437#endif
2438
2439    return val;
2440
2441  }
2442  else
2443    return ((digit[digit_num] & one_and_zeros(bit_num)) != 0);
2444}
2445
2446
2447// Set the ith bit with 1.
2448void
2449CLASS_TYPE::set(int i)
2450{
2451  if (check_if_outside(i))
2452    return;
2453
2454  int bit_num = bit_ord(i);
2455  int digit_num = digit_ord(i);
2456
2457  convert_SM_to_2C();
2458  digit[digit_num] |= one_and_zeros(bit_num);
2459  digit[digit_num] &= DIGIT_MASK; // Needed to zero the overflow bits.
2460  convert_2C_to_SM();
2461}
2462
2463
2464// Set the ith bit with 0, i.e., clear the ith bit.
2465void
2466CLASS_TYPE::clear(int i)
2467{
2468  if (check_if_outside(i))
2469    return;
2470
2471  int bit_num = bit_ord(i);
2472  int digit_num = digit_ord(i);
2473
2474  convert_SM_to_2C();
2475  digit[digit_num] &= ~(one_and_zeros(bit_num));
2476  digit[digit_num] &= DIGIT_MASK; // Needed to zero the overflow bits.
2477  convert_2C_to_SM();
2478}
2479
2480
2481// Create a mirror image of the number.
2482void
2483CLASS_TYPE::reverse()
2484{
2485  convert_SM_to_2C();
2486  vec_reverse(length(), ndigits, digit, length() - 1);
2487  convert_2C_to_SM();
2488}
2489
2490
2491// Get a packed bit representation of the number.
2492void
2493CLASS_TYPE::get_packed_rep(sc_digit *buf) const
2494{
2495  int buf_ndigits = (length() - 1) / BITS_PER_DIGIT_TYPE + 1;
2496
2497  // Initialize buf to zero.
2498  vec_zero(buf_ndigits, buf);
2499
2500  if (sgn == SC_ZERO)
2501    return;
2502
2503  const sc_digit *digit_or_d;
2504#ifdef SC_MAX_NBITS
2505  sc_digit d[MAX_NDIGITS];
2506#else
2507  sc_digit *d = new sc_digit[ndigits];
2508#endif
2509
2510  if (sgn == SC_POS)
2511    digit_or_d = digit;
2512
2513  else
2514  {
2515    // If sgn is negative, we have to convert digit to its 2's
2516    // complement. Since this function is const, we can not do it on
2517    // digit. Since buf doesn't have overflow bits, we cannot also do
2518    // it on buf. Thus, we have to do the complementation on a copy of
2519    // digit, i.e., on d.
2520
2521    vec_copy(ndigits, d, digit);
2522    vec_complement(ndigits, d);
2523
2524    buf[buf_ndigits - 1] = ~((sc_digit) 0);
2525
2526    digit_or_d = d;
2527
2528  }
2529
2530  // Copy the bits from digit to buf. The division and mod operations
2531  // below can be converted to addition/subtraction and comparison
2532  // operations at the expense of complicating the code. We can do it
2533  // if we see any performance problems.
2534
2535  for (int i = length() - 1; i >= 0; --i) {
2536
2537    if ((digit_or_d[digit_ord(i)] & one_and_zeros(bit_ord(i))) != 0) // Test.
2538
2539      buf[i / BITS_PER_DIGIT_TYPE] |=
2540        one_and_zeros(i % BITS_PER_DIGIT_TYPE); // Set.
2541
2542    else
2543
2544      buf[i / BITS_PER_DIGIT_TYPE] &=
2545        ~(one_and_zeros(i % BITS_PER_DIGIT_TYPE));  // Clear.
2546
2547  }
2548
2549#ifndef SC_MAX_NBITS
2550    delete[] d;
2551#endif
2552}
2553
2554
2555// Set a packed bit representation of the number.
2556void
2557CLASS_TYPE::set_packed_rep(sc_digit *buf)
2558{
2559  // Initialize digit to zero.
2560  vec_zero(ndigits, digit);
2561
2562  // Copy the bits from buf to digit.
2563  for (int i = length() - 1; i >= 0; --i) {
2564
2565    if ((buf[i / BITS_PER_DIGIT_TYPE] &
2566         one_and_zeros(i % BITS_PER_DIGIT_TYPE)) != 0) // Test.
2567
2568      digit[digit_ord(i)] |= one_and_zeros(bit_ord(i));     // Set.
2569
2570    else
2571
2572      digit[digit_ord(i)] &= ~(one_and_zeros(bit_ord(i)));  // Clear
2573
2574  }
2575
2576  convert_2C_to_SM();
2577}
2578
2579
2580// ----------------------------------------------------------------------------
2581//  SECTION: Private members.
2582// ----------------------------------------------------------------------------
2583
2584// Create a copy of v with sgn s.
2585CLASS_TYPE::CLASS_TYPE(const CLASS_TYPE& v, small_type s) :
2586    sc_value_base(v), sgn(s), nbits(v.nbits), ndigits(v.ndigits), digit()
2587{
2588#ifndef SC_MAX_NBITS
2589  digit = new sc_digit[ndigits];
2590#endif
2591
2592  vec_copy(ndigits, digit, v.digit);
2593}
2594
2595
2596// Create a copy of v where v is of the different type.
2597CLASS_TYPE::CLASS_TYPE(const OTHER_CLASS_TYPE& v, small_type s) :
2598    sc_value_base(v), sgn(s), nbits(num_bits(v.nbits)), ndigits(), digit()
2599{
2600#if (IF_SC_SIGNED == 1)
2601  ndigits = v.ndigits;
2602#else
2603  ndigits = DIV_CEIL(nbits);
2604#endif
2605
2606#ifndef SC_MAX_NBITS
2607  digit = new sc_digit[ndigits];
2608#endif
2609
2610  copy_digits(v.nbits, v.ndigits, v.digit);
2611}
2612
2613
2614// Create a signed number with (s, nb, nd, d) as its attributes (as
2615// defined in class CLASS_TYPE). If alloc is set, delete d.
2616CLASS_TYPE::CLASS_TYPE(small_type s, int nb,
2617                       int nd, sc_digit *d,
2618                       bool alloc) :
2619    sc_value_base(), sgn(s), nbits(num_bits(nb)), ndigits(), digit()
2620{
2621  ndigits = DIV_CEIL(nbits);
2622
2623#ifndef SC_MAX_NBITS
2624  digit = new sc_digit[ndigits];
2625#endif
2626
2627  if (ndigits <= nd)
2628    vec_copy(ndigits, digit, d);
2629  else
2630    vec_copy_and_zero(ndigits, digit, nd, d);
2631
2632#ifndef SC_MAX_NBITS
2633  if (alloc)
2634    delete [] d;
2635#endif
2636}
2637
2638// This constructor is mainly used in finding a "range" of bits from a
2639// number of type CLASS_TYPE. The function range(l, r) can have
2640// arbitrary precedence between l and r. If l is smaller than r, then
2641// the output is the reverse of range(r, l).
2642CLASS_TYPE::CLASS_TYPE(const CLASS_TYPE* u, int l, int r) :
2643    sc_value_base(), sgn(), nbits(), ndigits(), digit()
2644{
2645  bool reversed = false;
2646
2647  if( l < r ) {
2648    reversed = true;
2649    int tmp = l;
2650    l = r;
2651    r = tmp;
2652  }
2653
2654  // at this point, l >= r
2655
2656  // make sure that l and r point to the bits of u
2657  r = sc_max( r, 0 );
2658  l = sc_min( l, u->nbits - 1 );
2659
2660  nbits = num_bits( l - r + 1 );
2661
2662  // nbits can still be <= 0 because l and r have just been updated
2663  // with the bounds of u.
2664
2665  // if u == 0 or the range is out of bounds, return 0
2666  if( u->sgn == SC_ZERO || nbits <= num_bits( 0 ) ) {
2667    sgn = SC_ZERO;
2668    if( nbits <= num_bits( 0 ) ) {
2669      nbits = 1;
2670    }
2671    ndigits = DIV_CEIL( nbits );
2672#ifndef SC_MAX_NBITS
2673    digit = new sc_digit[ndigits];
2674#endif
2675    vec_zero( ndigits, digit );
2676    return;
2677  }
2678
2679  // The rest will be executed if u is not zero.
2680
2681  ndigits = DIV_CEIL(nbits);
2682
2683  // The number of bits up to and including l and r, respectively.
2684  int nl = l + 1;
2685  int nr = r + 1;
2686
2687  // The indices of the digits that have lth and rth bits, respectively.
2688  int left_digit = DIV_CEIL(nl) - 1;
2689  int right_digit = DIV_CEIL(nr) - 1;
2690
2691  int nd;
2692
2693  // The range is performed on the 2's complement representation, so
2694  // first get the indices for that.
2695  if (u->sgn == SC_NEG)
2696    nd = left_digit + 1;
2697  else
2698    nd = left_digit - right_digit + 1;
2699
2700  // Allocate memory for the range.
2701#ifdef SC_MAX_NBITS
2702  sc_digit d[MAX_NDIGITS];
2703#else
2704  digit = new sc_digit[ndigits];
2705  sc_digit *d = new sc_digit[nd];
2706#endif
2707
2708  // Getting the range on the 2's complement representation.
2709  if (u->sgn == SC_NEG) {
2710
2711    vec_copy(nd, d, u->digit);
2712    vec_complement(nd, d);  // d = -d;
2713    vec_shift_right(nd, d, r, DIGIT_MASK);
2714
2715  }
2716  else {
2717
2718    for (int i = right_digit; i <= left_digit; ++i)
2719      d[i - right_digit] = u->digit[i];
2720
2721    vec_shift_right(nd, d, r - right_digit * BITS_PER_DIGIT, 0);
2722
2723  }
2724
2725  vec_zero(ndigits, digit);
2726
2727  if (! reversed)
2728    vec_copy(sc_min(nd, ndigits), digit, d);
2729
2730  else {
2731
2732    // If l < r, i.e., reversed is set, reverse the bits of digit.  d
2733    // will be used as a temporary store. The following code tries to
2734    // minimize the use of bit_ord and digit_ord, which use mod and
2735    // div operators. Since these operators are function calls to
2736    // standard library routines, they are slow. The main idea in
2737    // reversing is "read bits out of d from left to right and push
2738    // them into digit using right shifting."
2739
2740    // Take care of the last digit.
2741    int nd_less_1 = nd - 1;
2742
2743    // Deletions will start from the left end and move one position
2744    // after each deletion.
2745    sc_digit del_mask = one_and_zeros(bit_ord(l - r));
2746
2747    while (del_mask) {
2748      vec_shift_right(ndigits, digit, 1, ((d[nd_less_1] & del_mask) != 0));
2749      del_mask >>= 1;
2750    }
2751
2752    // Take care of the other digits if any.
2753
2754    // Insertion to digit will always occur at the left end.
2755    sc_digit ins_mask = one_and_zeros(BITS_PER_DIGIT - 1);
2756
2757    for (int j = nd - 2; j >= 0; --j) { // j = nd - 2
2758
2759      // Deletions will start from the left end and move one position
2760      // after each deletion.
2761      del_mask = ins_mask;
2762
2763      while (del_mask) {
2764        vec_shift_right(ndigits, digit, 1, ((d[j] & del_mask) != 0));
2765        del_mask >>= 1;
2766      }
2767    }
2768
2769    if (u->sgn == SC_NEG)
2770      vec_shift_right(ndigits, digit,
2771                      ndigits * BITS_PER_DIGIT - length(), DIGIT_MASK);
2772    else
2773      vec_shift_right(ndigits, digit,
2774                      ndigits * BITS_PER_DIGIT - length(), 0);
2775
2776
2777  }  // if reversed.
2778
2779  convert_2C_to_SM();
2780
2781#ifndef SC_MAX_NBITS
2782  delete [] d;
2783#endif
2784}
2785
2786// This constructor is mainly used in finding a "range" of bits from a
2787// number of type OTHER_CLASS_TYPE. The function range(l, r) can have
2788// arbitrary precedence between l and r. If l is smaller than r, then
2789// the output is the reverse of range(r, l).
2790CLASS_TYPE::CLASS_TYPE(const OTHER_CLASS_TYPE* u, int l, int r) :
2791    sc_value_base(), sgn(), nbits(), ndigits(), digit()
2792{
2793  bool reversed = false;
2794
2795  if( l < r ) {
2796    reversed = true;
2797    int tmp = l;
2798    l = r;
2799    r = tmp;
2800  }
2801
2802  // at this point, l >= r
2803
2804  // make sure that l and r point to the bits of u
2805  r = sc_max( r, 0 );
2806  l = sc_min( l, u->nbits - 1 );
2807
2808  nbits = num_bits( l - r + 1 );
2809
2810  // nbits can still be <= 0 because l and r have just been updated
2811  // with the bounds of u.
2812
2813  // if u == 0 or the range is out of bounds, return 0
2814  if( u->sgn == SC_ZERO || nbits <= num_bits( 0 ) ) {
2815    sgn = SC_ZERO;
2816    if( nbits <= num_bits( 0 ) ) {
2817      nbits = 1;
2818    }
2819    ndigits = DIV_CEIL( nbits );
2820#ifndef SC_MAX_NBITS
2821    digit = new sc_digit[ndigits];
2822#endif
2823    vec_zero( ndigits, digit );
2824    return;
2825  }
2826
2827  // The rest will be executed if u is not zero.
2828
2829  ndigits = DIV_CEIL(nbits);
2830
2831  // The number of bits up to and including l and r, respectively.
2832  int nl = l + 1;
2833  int nr = r + 1;
2834
2835  // The indices of the digits that have lth and rth bits, respectively.
2836  int left_digit = DIV_CEIL(nl) - 1;
2837  int right_digit = DIV_CEIL(nr) - 1;
2838
2839  int nd;
2840
2841  // The range is performed on the 2's complement representation, so
2842  // first get the indices for that.
2843  if (u->sgn == SC_NEG)
2844    nd = left_digit + 1;
2845  else
2846    nd = left_digit - right_digit + 1;
2847
2848  // Allocate memory for the range.
2849#ifdef SC_MAX_NBITS
2850  sc_digit d[MAX_NDIGITS];
2851#else
2852  digit = new sc_digit[ndigits];
2853  sc_digit *d = new sc_digit[nd];
2854#endif
2855
2856  // Getting the range on the 2's complement representation.
2857  if (u->sgn == SC_NEG) {
2858
2859    vec_copy(nd, d, u->digit);
2860    vec_complement(nd, d);  // d = -d;
2861    vec_shift_right(nd, d, r, DIGIT_MASK);
2862
2863  }
2864  else {
2865
2866    for (int i = right_digit; i <= left_digit; ++i)
2867      d[i - right_digit] = u->digit[i];
2868
2869    vec_shift_right(nd, d, r - right_digit * BITS_PER_DIGIT, 0);
2870
2871  }
2872
2873  vec_zero(ndigits, digit);
2874
2875  if (! reversed)
2876    vec_copy(sc_min(nd, ndigits), digit, d);
2877
2878  else {
2879
2880    // If l < r, i.e., reversed is set, reverse the bits of digit.  d
2881    // will be used as a temporary store. The following code tries to
2882    // minimize the use of bit_ord and digit_ord, which use mod and
2883    // div operators. Since these operators are function calls to
2884    // standard library routines, they are slow. The main idea in
2885    // reversing is "read bits out of d from left to right and push
2886    // them into digit using right shifting."
2887
2888    // Take care of the last digit.
2889    int nd_less_1 = nd - 1;
2890
2891    // Deletions will start from the left end and move one position
2892    // after each deletion.
2893    sc_digit del_mask = one_and_zeros(bit_ord(l - r));
2894
2895    while (del_mask) {
2896      vec_shift_right(ndigits, digit, 1, ((d[nd_less_1] & del_mask) != 0));
2897      del_mask >>= 1;
2898    }
2899
2900    // Take care of the other digits if any.
2901
2902    // Insertion to digit will always occur at the left end.
2903    sc_digit ins_mask = one_and_zeros(BITS_PER_DIGIT - 1);
2904
2905    for (int j = nd - 2; j >= 0; --j) { // j = nd - 2
2906
2907      // Deletions will start from the left end and move one position
2908      // after each deletion.
2909      del_mask = ins_mask;
2910
2911      while (del_mask) {
2912        vec_shift_right(ndigits, digit, 1, ((d[j] & del_mask) != 0));
2913        del_mask >>= 1;
2914      }
2915    }
2916
2917    if (u->sgn == SC_NEG)
2918      vec_shift_right(ndigits, digit,
2919                      ndigits * BITS_PER_DIGIT - length(), DIGIT_MASK);
2920    else
2921      vec_shift_right(ndigits, digit,
2922                      ndigits * BITS_PER_DIGIT - length(), 0);
2923
2924
2925  }  // if reversed.
2926
2927  convert_2C_to_SM();
2928
2929#ifndef SC_MAX_NBITS
2930  delete [] d;
2931#endif
2932}
2933
2934
2935// Print out all the physical attributes.
2936void
2937CLASS_TYPE::dump(::std::ostream& os) const
2938{
2939  // Save the current setting, and set the base to decimal.
2940#if defined(__MINGW32__)
2941  std::_Ios_Fmtflags old_flags = os.setf(::std::ios::dec,::std::ios::basefield);
2942#else
2943  fmtflags old_flags = os.setf(::std::ios::dec, ::std::ios::basefield);
2944#endif
2945
2946  os << "width = " << length() << ::std::endl;
2947  os << "value = " << *this << ::std::endl;
2948  os << "bits  = ";
2949
2950  int len = length();
2951
2952  for (int i = len - 1; i >= 0; --i) {
2953
2954    os << "01"[test(i)];
2955    if (--len % 4 == 0)
2956      os << " ";
2957
2958  }
2959
2960  os << ::std::endl;
2961
2962  // Restore old_flags.
2963  os.setf(old_flags, ::std::ios::basefield);
2964}
2965
2966
2967// Checks to see if bit_num is out of bounds.
2968bool
2969CLASS_TYPE::check_if_outside(int bit_num) const
2970{
2971  if ((bit_num < 0) || (num_bits(bit_num) >= nbits)) {
2972
2973#ifdef DEBUG_SYSTEMC
2974      if( bit_num < 0 || bit_num >= nbits ) {
2975	  char msg[BUFSIZ];
2976	  std::sprintf( msg, "%s::check_if_outside( int bit_num ) : "
2977		   "bit_num = %d is out of bounds",
2978		   CLASS_TYPE_STR, bit_num );
2979	  SC_REPORT_WARNING( sc_core::SC_ID_OUT_OF_BOUNDS_, msg );
2980      }
2981#endif
2982
2983    return true;
2984  }
2985
2986  return false;
2987}
2988
2989// End of file.
2990