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