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