SystemC 3.0.0
Accellera SystemC proof-of-concept library
sc_unsigned.h
Go to the documentation of this file.
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.h -- Arbitrary precision unsigned 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.
28
29 An sc_signed number has the sign-magnitude representation
30 internally. However, its interface guarantees a 2's-complement
31 representation. The sign-magnitude representation is chosen
32 because of its efficiency: The sc_signed and sc_unsigned types are
33 optimized for arithmetic rather than bitwise operations. For
34 arithmetic operations, the sign-magnitude representation performs
35 better.
36
37 It is also important to note that an sc_unsigned number with n
38 bits is equivalent to an sc_signed non-negative number with n + 1
39 bits.
40
41 For the behavior of operators, we have two semantics: the old and
42 new. The most important difference between these two semantics is
43 that the old semantics is closer to C/C++ semantics in that the
44 result type of a binary operator on unsigned and signed arguments
45 is unsigned; the new semantics, on the other hand, requires the
46 result type be signed. The new semantics is required by the VSIA
47 C/C++ data types standard. We have implemented the new semantics.
48
49 Original Author: Ali Dasdan, Synopsys, Inc.
50
51 *****************************************************************************/
52
53/*****************************************************************************
54
55 MODIFICATION LOG - modifiers, enter your name, affiliation, date and
56 changes you are making here.
57
58 Name, Affiliation, Date:
59 Description of Modification:
60
61 *****************************************************************************/
62
63// $Log: sc_unsigned.h,v $
64// Revision 1.4 2011/08/24 22:05:46 acg
65// Torsten Maehne: initialization changes to remove warnings.
66//
67// Revision 1.3 2011/02/18 20:19:15 acg
68// Andy Goodrich: updating Copyright notice.
69//
70// Revision 1.2 2009/02/28 00:26:26 acg
71// Andy Goodrich: bug fixes.
72//
73// Revision 1.1.1.1 2006/12/15 20:20:05 acg
74// SystemC 2.3
75//
76// Revision 1.5 2006/05/08 17:50:02 acg
77// Andy Goodrich: Added David Long's declarations for friend operators,
78// functions, and methods, to keep the Microsoft compiler happy.
79//
80// Revision 1.4 2006/03/13 20:25:27 acg
81// Andy Goodrich: Addition of function declarations, e.g., xor_signed_friend()
82// to keep gcc 4.x happy.
83//
84// Revision 1.3 2006/01/13 18:49:32 acg
85// Added $Log command so that CVS check in comments are reproduced in the
86// source.
87//
88
89#ifndef SC_UNSIGNED_H
90#define SC_UNSIGNED_H
91
92
102
103
104namespace sc_dt
105{
106
107// classes defined in this module
108class sc_unsigned_bitref_r;
109class sc_unsigned_bitref;
110class sc_unsigned_subref_r;
111class sc_unsigned_subref;
112class sc_concatref;
113class sc_unsigned;
114
115// forward class declarations
116class sc_bv_base;
117class sc_lv_base;
118class sc_int_base;
119class sc_uint_base;
120class sc_int_subref_r;
121class sc_uint_subref_r;
122class sc_signed;
123class sc_signed_subref_r;
124class sc_fxval;
125class sc_fxval_fast;
126class sc_fxnum;
127class sc_fxnum_fast;
128
129// Operators that need to be declared so they can be friends:
130
131#define SCFP inline
132#include "sc_unsigned_friends.h"
133#undef SCFP
134
135 // Unary arithmetic operators
136
139
140 // Bitwise NOT operator (unary).
141
143
144// ----------------------------------------------------------------------------
145// CLASS : sc_unsigned_bitref_r
146//
147// Proxy class for sc_unsigned bit selection (r-value only).
148// ----------------------------------------------------------------------------
149
151{
152 friend class sc_unsigned;
153
154protected:
155
156 // construction and initialization:
157
158 sc_unsigned_bitref_r() : sc_value_base(), m_index(0), m_obj_p(0)
159 {}
160
161 void initialize( const sc_unsigned* obj_p, int index_ )
162 {
163 m_obj_p = const_cast<sc_unsigned*>( obj_p );
164 m_index = index_;
165 }
166
167public:
168
169 // destructor
170
172 {}
173
174 // copy constructor
175
177 : sc_value_base(a), m_index( a.m_index ), m_obj_p( a.m_obj_p )
178 {}
179
180 // capacity
181
182 int length() const
183 { return 1; }
184
185
186 // implicit conversion to bool
187
188 operator uint64 () const;
189 bool operator ! () const;
190 bool operator ~ () const;
191
192
193 // explicit conversions
194
195 bool value() const
196 { return operator uint64(); }
197
198 bool to_bool() const
199 { return operator uint64(); }
200
201
202 // concatenation support
203
204 virtual int concat_length(bool* xz_present_p) const
205 { if ( xz_present_p ) *xz_present_p = false; return 1; }
206 virtual uint64 concat_get_uint64() const
207 { return (uint64)operator uint64(); }
208 virtual bool concat_get_ctrl( sc_digit* dst_p, int low_i ) const
209 {
210 int bit_mask = 1 << SC_BIT_INDEX(low_i);
211 int word_i = SC_DIGIT_INDEX(low_i);
212 dst_p[word_i] &= ~bit_mask;
213 return false;
214 }
215 virtual bool concat_get_data( sc_digit* dst_p, int low_i ) const
216 {
217 int bit_mask = 1 << SC_BIT_INDEX(low_i);
218 bool result; // True if non-zero.
219 int word_i = SC_DIGIT_INDEX(low_i);
220 if ( operator uint64() )
221 {
222 dst_p[word_i] |= bit_mask;
223 result = true;
224 }
225 else
226 {
227 dst_p[word_i] &= ~bit_mask;
228 result = false;
229 }
230 return result;
231 }
232
233
234 // other methods
235
236 void print( ::std::ostream& os = ::std::cout ) const
237 { os << to_bool(); }
238
239protected:
240
243
244private: // disabled
245 const sc_unsigned_bitref_r& operator = ( const sc_unsigned_bitref_r& );
246};
247
248
249
250inline
251::std::ostream&
252operator<<( ::std::ostream&, const sc_unsigned_bitref_r& );
253
254
255// ----------------------------------------------------------------------------
256// CLASS : sc_unsigned_bitref
257//
258// Proxy class for sc_unsigned bit selection (r-value and l-value).
259// ----------------------------------------------------------------------------
260
262 : public sc_unsigned_bitref_r
263{
264 friend class sc_unsigned;
266
267
268protected: // construction
269
271 {}
272
273public:
274
275 // copy constructor
276
279 {}
280
281
282 // assignment operators
283
284 const sc_unsigned_bitref& operator = ( const sc_unsigned_bitref_r& );
285 const sc_unsigned_bitref& operator = ( const sc_unsigned_bitref& );
286 const sc_unsigned_bitref& operator = ( bool );
287
291
292 // concatenation methods
293
294 virtual void concat_set(int64 src, int low_i);
295 virtual void concat_set(const sc_signed& src, int low_i);
296 virtual void concat_set(const sc_unsigned& src, int low_i);
297 virtual void concat_set(uint64 src, int low_i);
298
299
300 // other methods
301
302 void scan( ::std::istream& is = ::std::cin );
303
304};
305
306
307
308inline
309::std::istream&
310operator>>( ::std::istream&, sc_unsigned_bitref& );
311
312
313// ----------------------------------------------------------------------------
314// CLASS : sc_unsigned_subref_r
315//
316// Proxy class for sc_unsigned part selection (r-value only).
317// ----------------------------------------------------------------------------
318
320{
321 friend class sc_signed;
322 friend class sc_unsigned;
323 friend class sc_unsigned_signal;
324
325protected:
326
327 // constructor
328
329 sc_unsigned_subref_r() : sc_value_base(), m_left(0), m_obj_p(0), m_right(0)
330 {}
331
332 void initialize( const sc_unsigned* obj_p, int left_, int right_ )
333 {
334 m_obj_p = const_cast<sc_unsigned*>( obj_p );
335 m_left = left_;
336 m_right = right_;
337 }
338
339public:
340
341 // destructor
342
344 {}
345
346
347 // copy constructor
348
350 : sc_value_base(a), m_left( a.m_left ), m_obj_p( a.m_obj_p ),
351 m_right( a.m_right )
352 {}
353
354
355 // capacity
356
357 int length() const
358 { return m_left >= m_right ? (m_left-m_right+1) : (m_right-m_left+1 ); }
359
360
361 // implicit conversion to sc_unsigned
362
363 operator sc_unsigned () const;
364
365
366 // explicit conversions
367
368 double to_double() const;
369 inline int to_int() const;
370 inline unsigned int to_uint() const;
371 inline long to_long() const;
372 inline unsigned long to_ulong() const;
373 inline int64 to_int64() const;
374 inline uint64 to_uint64() const;
375
376
377 // explicit conversion to character string
378
379 const std::string to_string( sc_numrep numrep = SC_DEC ) const;
380 const std::string to_string( sc_numrep numrep, bool w_prefix ) const;
381
382
383 // shift operators
384
385 inline sc_unsigned operator<<( int v ) const;
386 inline sc_unsigned operator<<(const sc_signed& v) const;
387 inline sc_unsigned operator<<(const sc_unsigned& v) const;
388
389 inline sc_unsigned operator>>( int v ) const;
390 inline sc_unsigned operator>>(const sc_signed& v) const;
391 inline sc_unsigned operator>>(const sc_unsigned& v) const;
392
393 // concatenation support
394
395 virtual int concat_length(bool* xz_present_p) const
396 {
397 if ( xz_present_p ) *xz_present_p = false;
398 return m_left - m_right + 1;
399 }
400 virtual uint64 concat_get_uint64() const;
401 virtual bool concat_get_ctrl( sc_digit* dst_p, int low_i ) const;
402 virtual bool concat_get_data( sc_digit* dst_p, int low_i ) const;
403
404 // reduce methods
405
406 bool and_reduce() const;
407 bool nand_reduce() const;
408 bool or_reduce() const;
409 bool nor_reduce() const;
410 bool xor_reduce() const ;
411 bool xnor_reduce() const;
412
413 // other methods
414
415 void print( ::std::ostream& os = ::std::cout ) const
416 { os << to_string(sc_io_base(os,SC_DEC),sc_io_show_base(os)); }
417 protected:
419
420protected:
421
422 int m_left; // Left-most bit in this part selection.
423 sc_unsigned* m_obj_p; // Target of this part selection.
424 int m_right; // Right-most bit in this part selection.
425
426private: // disabled
427 const sc_unsigned_subref_r& operator = ( const sc_unsigned_subref_r& );
428};
429
430// functional notation for the reduce methods
431
432inline bool and_reduce( const sc_unsigned_subref_r& a ) {
433 return a.and_reduce();
434}
435
436inline bool nand_reduce( const sc_unsigned_subref_r& a ) {
437 return a.nand_reduce();
438}
439
440inline bool or_reduce( const sc_unsigned_subref_r& a ) {
441 return a.or_reduce();
442}
443
444inline bool nor_reduce( const sc_unsigned_subref_r& a ) {
445 return a.nor_reduce();
446}
447
448inline bool xor_reduce( const sc_unsigned_subref_r& a ) {
449 return a.xor_reduce();
450}
451
452inline bool xnor_reduce( const sc_unsigned_subref_r& a ) {
453 return a.xnor_reduce();
454}
455
456
457inline
458::std::ostream&
459operator<<( ::std::ostream&, const sc_unsigned_subref_r& );
460
461
462
463// ----------------------------------------------------------------------------
464// CLASS : sc_unsigned_subref
465//
466// Proxy class for sc_unsigned part selection (r-value and l-value).
467// ----------------------------------------------------------------------------
468
470 : public sc_unsigned_subref_r
471{
472 friend class sc_unsigned;
474
475
476 // constructor
477
478protected:
480 {}
481
482public:
483
484 // copy constructor
485
488 {}
489
490 // assignment operators
491
492 const sc_unsigned_subref& operator = ( const sc_unsigned_subref_r& a );
493 const sc_unsigned_subref& operator = ( const sc_unsigned_subref& a );
494 const sc_unsigned_subref& operator = ( const sc_unsigned& a );
495
496 template<class T>
497 const sc_unsigned_subref& operator = ( const sc_generic_base<T>& a );
498 const sc_unsigned_subref& operator = ( const sc_signed_subref_r& a );
499 const sc_unsigned_subref& operator = ( const sc_signed& a );
500
501 const sc_unsigned_subref& operator = ( const char* a );
502 const sc_unsigned_subref& operator = ( unsigned long a );
503 const sc_unsigned_subref& operator = ( long a );
504
505 const sc_unsigned_subref& operator = ( unsigned int a )
506 { return operator = ( (unsigned long) a ); }
507
508 const sc_unsigned_subref& operator = ( int a )
509 { return operator = ( (long) a ); }
510
511 const sc_unsigned_subref& operator = ( uint64 a );
512 const sc_unsigned_subref& operator = ( int64 a );
513 const sc_unsigned_subref& operator = ( double a );
514 const sc_unsigned_subref& operator = ( const sc_int_base& a );
515 const sc_unsigned_subref& operator = ( const sc_uint_base& a );
516 const sc_unsigned_subref& operator = ( const sc_bv_base& );
517 const sc_unsigned_subref& operator = ( const sc_lv_base& );
518
519 // concatenation methods
520
521 virtual void concat_set(int64 src, int low_i);
522 virtual void concat_set(const sc_signed& src, int low_i);
523 virtual void concat_set(const sc_unsigned& src, int low_i);
524 virtual void concat_set(uint64 src, int low_i);
525
526 // other methods
527
528 void scan( ::std::istream& is = ::std::cin );
529
530};
531
532
533inline
534::std::istream&
535operator>>( ::std::istream&, sc_unsigned_subref& );
536
537
538// ----------------------------------------------------------------------------
539// CLASS : sc_unsigned
540//
541// Arbitrary precision unsigned number.
542// ----------------------------------------------------------------------------
543
545{
546 friend class sc_concatref;
548 friend class sc_unsigned_bitref;
550 friend class sc_unsigned_subref;
551 friend class sc_signed;
552 friend class sc_signed_subref;
553 friend class sc_signed_subref_r;
554 template<int W> friend class sc_bigint;
555 template<int W> friend class sc_biguint;
556 template<int W> friend class sc_int;
557 template<int W> friend class sc_uint;
558
559 // Needed for types using sc_unsigned.
560 typedef bool elemtype;
561
562public:
563 enum { SIGNED=0 };
564
565public:
566
567 // constructors
568
569 explicit inline sc_unsigned( int nb = sc_length_param().len() );
570#if !defined(SC_BIGINT_CONFIG_BASE_CLASS_HAS_STORAGE)
571 explicit inline sc_unsigned( int nb, sc_digit* digits_p );
572#endif // !defined(SC_BIGINT_CONFIG_BASE_CLASS_HAS_STORAGE)
573 sc_unsigned( const sc_unsigned& v );
574 sc_unsigned( const sc_signed& v );
575 template<class T>
576 explicit sc_unsigned( const sc_generic_base<T>& v );
577 explicit sc_unsigned( const sc_bv_base& v );
578 explicit sc_unsigned( const sc_lv_base& v );
579 explicit sc_unsigned( const sc_int_subref_r& v );
580 explicit sc_unsigned( const sc_uint_subref_r& v );
581 explicit sc_unsigned( const sc_signed_subref_r& v );
582 explicit sc_unsigned( const sc_unsigned_subref_r& v );
583
584
585
586 // assignment operators
587
588 const sc_unsigned& operator = (const sc_unsigned& v);
589 inline const sc_unsigned& operator = (const sc_unsigned_subref_r& a );
590
591 template<class T>
592 const sc_unsigned& operator = ( const sc_generic_base<T>& a )
593 { a->to_sc_unsigned(*this); return *this; }
594
595 const sc_unsigned& operator = (const sc_signed& v);
596 inline const sc_unsigned& operator = (const sc_signed_subref_r& a );
597
598 const sc_unsigned& operator = ( const char* v);
599 const sc_unsigned& operator = ( int64 v);
600 const sc_unsigned& operator = ( uint64 v);
601 const sc_unsigned& operator = ( long v);
602 const sc_unsigned& operator = ( unsigned long v);
603
604 const sc_unsigned& operator = ( int v)
605 { return operator=((long) v); }
606
607 const sc_unsigned& operator = ( unsigned int v)
608 { return operator=((unsigned long) v); }
609
610 const sc_unsigned& operator = ( double v);
611 const sc_unsigned& operator = ( const sc_int_base& v);
612 const sc_unsigned& operator = ( const sc_uint_base& v);
613
614 const sc_unsigned& operator = ( const sc_bv_base& );
615 const sc_unsigned& operator = ( const sc_lv_base& );
616
617#ifdef SC_INCLUDE_FX
618 const sc_unsigned& operator = ( const sc_fxval& );
619 const sc_unsigned& operator = ( const sc_fxval_fast& );
620 const sc_unsigned& operator = ( const sc_fxnum& );
621 const sc_unsigned& operator = ( const sc_fxnum_fast& );
622#endif
623
624
625 // destructor:
626
628 {
629 if ( digit_is_allocated() ) { delete [] digit; }
630 }
631
632 // Concatenation support:
633
634 sc_digit* get_raw() const { return digit; }
635 virtual int concat_length(bool* xz_present_p) const
636 { if ( xz_present_p ) *xz_present_p = false; return nbits-1; }
637 virtual bool concat_get_ctrl( sc_digit* dst_p, int low_i ) const;
638 virtual bool concat_get_data( sc_digit* dst_p, int low_i ) const;
639 virtual uint64 concat_get_uint64() const;
640 virtual void concat_set(int64 src, int low_i);
641 virtual void concat_set(const sc_signed& src, int low_i);
642 virtual void concat_set(const sc_unsigned& src, int low_i);
643 virtual void concat_set(uint64 src, int low_i);
644
645 // Increment operators.
646
647 sc_unsigned& operator ++ ();
648 inline sc_unsigned operator ++ (int);
649
650 // Decrement operators.
651
652 sc_unsigned& operator -- ();
653 inline sc_unsigned operator -- (int);
654
655 void invalid_init( const char* type_name, int nb ) const;
656
657 // bit selection
658
659 inline void check_index( int i ) const
660 { if ( (i < 0) || (i >= nbits-1) ) invalid_index(i); }
661
662 void invalid_index( int i ) const;
663
665 {
667 return pool.allocate();
668 }
669
670 sc_unsigned_bitref& operator [] ( int i )
671 {
672 check_index(i);
673 sc_unsigned_bitref* result_p = temporary_bitref();
674 result_p->initialize( this, i );
675 return *result_p;
676 }
677
678 const sc_unsigned_bitref_r& operator [] ( int i ) const
679 {
680 check_index(i);
681 sc_unsigned_bitref* result_p = temporary_bitref();
682 result_p->initialize( this, i );
683 return *result_p;
684 }
685
687 {
688 check_index(i);
689 sc_unsigned_bitref* result_p = temporary_bitref();
690 result_p->initialize( this, i );
691 return *result_p;
692 }
693
694 const sc_unsigned_bitref_r& bit( int i ) const
695 {
696 check_index(i);
697 sc_unsigned_bitref* result_p = temporary_bitref();
698 result_p->initialize( this, i );
699 return *result_p;
700 }
701
702
703 // part selection
704
705 // Subref operators. Help access the range of bits from the ith to
706 // jth. These indices have arbitrary precedence with respect to each
707 // other, i.e., we can have i <= j or i > j. Note the equivalence
708 // between range(i, j) and operator (i, j). Also note that
709 // operator (i, i) returns an unsigned number that corresponds to the
710 // bit operator [i], so these two forms are not the same.
711
712 inline void check_range( int l, int r ) const
713 {
714 if ( l < r )
715 {
716 if ( (l < 0) || (r >= nbits-1) ) invalid_range(l,r);
717 }
718 else
719 {
720 if ( (r < 0) || (l >= nbits-1) ) invalid_range(l,r);
721 }
722 }
723
724 void invalid_range( int l, int r ) const;
725
727 {
729 return pool.allocate();
730 }
731
732 sc_unsigned_subref& range( int i, int j )
733 {
734 check_range(i,j);
735 sc_unsigned_subref* result_p = temporary_subref();
736 result_p->initialize( this, i, j );
737 return *result_p;
738 }
739
740 const sc_unsigned_subref_r& range( int i, int j ) const
741 {
742 check_range(i,j);
743 sc_unsigned_subref* result_p = temporary_subref();
744 result_p->initialize( this, i, j );
745 return *result_p;
746 }
747
748 sc_unsigned_subref& operator () ( int i, int j )
749 {
750 check_range(i,j);
751 sc_unsigned_subref* result_p = temporary_subref();
752 result_p->initialize( this, i, j );
753 return *result_p;
754 }
755
756 const sc_unsigned_subref_r& operator () ( int i, int j ) const
757 {
758 check_range(i,j);
759 sc_unsigned_subref* result_p = temporary_subref();
760 result_p->initialize( this, i, j );
761 return *result_p;
762 }
763
764 // temporary for concatenation:
765
767 {
768 static sc_core::sc_vpool<sc_unsigned> pool(9);
769 return pool.allocate();
770 }
771
772 // explicit conversions
773
774 inline int to_int() const;
775 inline unsigned int to_uint() const;
776 inline long to_long() const;
777 inline unsigned long to_ulong() const;
778 inline int64 to_int64() const;
779 inline uint64 to_uint64() const;
780 double to_double() const;
781
782 //inline operator bool() const { return to_uint64(); }
783
784#ifdef SC_DT_DEPRECATED
785 int to_signed() const
786 { return to_int(); }
787
788 unsigned int to_unsigned() const
789 { return to_uint(); }
790#endif
791
792 // explicit conversion to character string
793
794 const std::string to_string( sc_numrep numrep = SC_DEC ) const;
795 const std::string to_string( sc_numrep numrep, bool w_prefix ) const;
796
797 // Print functions. dump prints the internals of the class.
798
799 void print( ::std::ostream& os = ::std::cout ) const
800 { os << to_string(sc_io_base(os,SC_DEC),sc_io_show_base(os)); }
801
802 void scan( ::std::istream& is = ::std::cin );
803
804 void dump( ::std::ostream& os = ::std::cout ) const;
805
806
807 // Functions to find various properties.
808 int length() const { return nbits - 1; } // Bit width.
809 bool iszero() const; // Is the number zero?
810 bool sign() const { return 0; } // Sign.
811
812 // reduce methods
813
814 bool and_reduce() const;
815
816 bool nand_reduce() const
817 { return ( ! and_reduce() ); }
818
819 bool or_reduce() const;
820
821 bool nor_reduce() const
822 { return ( ! or_reduce() ); }
823
824 bool xor_reduce() const;
825
826 bool xnor_reduce() const
827 { return ( ! xor_reduce() ); }
828
829
830 // Functions to access individual bits.
831
832 // Set the ith bit with 1.
833 inline
834 void
835 set(int i)
836 {
837 if (check_if_outside(i))
838 return;
839
840 int bit_num = SC_BIT_INDEX(i);
841 int digit_num = SC_DIGIT_INDEX(i);
842
843 digit[digit_num] |= one_and_zeros(bit_num);
844 digit[digit_num] = SC_MASK_DIGIT(digit[digit_num]);
845 }
846
847
848 // Set the ith bit with 0, i.e., clear the ith bit.
849 inline
850 void
851 clear(int i)
852 {
853 if (check_if_outside(i))
854 return;
855
856 int bit_num = SC_BIT_INDEX(i);
857 int digit_num = SC_DIGIT_INDEX(i);
858
859 digit[digit_num] &= ~(one_and_zeros(bit_num));
860 digit[digit_num] = SC_MASK_DIGIT(digit[digit_num]);
861 }
862
863 // Return true if the bit i is 1, false otherwise. If i is outside the
864 // bounds, return 1/0 according to the sign of the number by assuming
865 // that the number has infinite length.
866
867 inline
868 bool
869 test(int i) const
870 {
871 if (check_if_outside(i))
872 return 0;
873
874 int bit_num = SC_BIT_INDEX(i);
875 int digit_num = SC_DIGIT_INDEX(i);
876
877 return ((digit[digit_num] & one_and_zeros(bit_num)) != 0);
878 }
879
880 void set(int i, bool v) // Set the ith bit to v.
881 { if (v) set(i); else clear(i); }
882 void invert(int i) // Negate the ith bit.
883 { if (test(i)) clear(i); else set(i); }
884
885 // Make the number equal to its mirror image.
886 void reverse();
887
888 // Get/set a packed bit representation of the number.
889 void get_packed_rep(sc_digit *buf) const;
891
892 /*
893 The comparison of the old and new semantics are as follows:
894
895 Let s = sc_signed,
896 u = sc_unsigned,
897 un = { uint64, unsigned long, unsigned int },
898 sn = { int64, long, int, char* }, and
899 OP = { +, -, *, /, % }.
900
901 Old semantics: New semantics:
902 u OP u -> u u OP u -> u
903 s OP u -> u s OP u -> s
904 u OP s -> u u OP s -> s
905 s OP s -> s s OP s -> s
906
907 u OP un = un OP u -> u u OP un = un OP u -> u
908 u OP sn = sn OP u -> u u OP sn = sn OP u -> s
909
910 s OP un = un OP s -> s s OP un = un OP s -> s
911 s OP sn = sn OP s -> s s OP sn = sn OP s -> s
912
913 In the new semantics, the result is u if both operands are u; the
914 result is s otherwise. The only exception is subtraction. The result
915 of a subtraction is always s.
916
917 The old semantics is like C/C++ semantics on integer types; the
918 new semantics is due to the VSIA C/C++ data types standard.
919 */
920
921 // FRIEND DECLARATIONS:
922
923# define SCFP friend
924# include "sc_unsigned_friends.h"
925# undef SCFP
926
927 // SELF-REFERENCING OPERATORS:
928 //
929 inline sc_unsigned& operator += (const sc_signed& v);
930 inline sc_unsigned& operator += (const sc_unsigned& v);
931 inline sc_unsigned& operator += (int64 v);
932 inline sc_unsigned& operator += (uint64 v);
933 inline sc_unsigned& operator += (long v);
934 inline sc_unsigned& operator += (unsigned long v);
935 inline sc_unsigned& operator += (int v);
936 inline sc_unsigned& operator += (unsigned int v);
937 inline sc_unsigned& operator += (const sc_int_base& v);
938 inline sc_unsigned& operator += (const sc_uint_base& v);
939
940 inline sc_unsigned& operator -= (const sc_signed& v);
941 inline sc_unsigned& operator -= (const sc_unsigned& v);
942 inline sc_unsigned& operator -= (int64 v);
943 inline sc_unsigned& operator -= (uint64 v);
944 inline sc_unsigned& operator -= (long v);
945 inline sc_unsigned& operator -= (unsigned long v);
946 inline sc_unsigned& operator -= (int v);
947 inline sc_unsigned& operator -= (unsigned int v);
948 inline sc_unsigned& operator -= (const sc_int_base& v);
949 inline sc_unsigned& operator -= (const sc_uint_base& v);
950
951 inline sc_unsigned& operator *= (const sc_signed& v);
952 inline sc_unsigned& operator *= (const sc_unsigned& v);
953 inline sc_unsigned& operator *= (int64 v);
954 inline sc_unsigned& operator *= (uint64 v);
955 inline sc_unsigned& operator *= (long v);
956 inline sc_unsigned& operator *= (unsigned long v);
957 inline sc_unsigned& operator *= (int v);
958 inline sc_unsigned& operator *= (unsigned int v);
959 inline sc_unsigned& operator *= (const sc_int_base& v);
960 inline sc_unsigned& operator *= (const sc_uint_base& v);
961
962 inline sc_unsigned& operator /= (const sc_signed& v);
963 inline sc_unsigned& operator /= (const sc_unsigned& v);
964 inline sc_unsigned& operator /= (int64 v);
965 inline sc_unsigned& operator /= (uint64 v);
966 inline sc_unsigned& operator /= (long v);
967 inline sc_unsigned& operator /= (unsigned long v);
968 inline sc_unsigned& operator /= (int v);
969 inline sc_unsigned& operator /= (unsigned int v);
970 inline sc_unsigned& operator /= (const sc_int_base& v);
971 inline sc_unsigned& operator /= (const sc_uint_base& v);
972
973 inline sc_unsigned& operator %= (const sc_signed& v);
974 inline sc_unsigned& operator %= (const sc_unsigned& v);
975 inline sc_unsigned& operator %= (int64 v);
976 inline sc_unsigned& operator %= (uint64 v);
977 inline sc_unsigned& operator %= (long v);
978 inline sc_unsigned& operator %= (unsigned long v);
979 inline sc_unsigned& operator %= (int v);
980 inline sc_unsigned& operator %= (unsigned int v);
981 inline sc_unsigned& operator %= (const sc_int_base& v);
982 inline sc_unsigned& operator %= (const sc_uint_base& v);
983
988 inline sc_unsigned& operator &= (long v);
989 inline sc_unsigned& operator &= (unsigned long v);
990 inline sc_unsigned& operator &= (int v);
991 inline sc_unsigned& operator &= (unsigned int v);
994
999 inline sc_unsigned& operator |= (long v);
1000 inline sc_unsigned& operator |= (unsigned long v);
1002 inline sc_unsigned& operator |= (unsigned int v);
1005
1010 inline sc_unsigned& operator ^= (long v);
1011 inline sc_unsigned& operator ^= (unsigned long v);
1013 inline sc_unsigned& operator ^= (unsigned int v);
1016
1017 // LEFT SHIFT operators:
1018
1019 inline
1021 operator<<(int v) const
1022 {
1023 if (v <= 0)
1024 return sc_unsigned(*this);
1025
1026 int nb = nbits + v;
1027 int nd = DIV_CEIL(nb);
1028 sc_unsigned result(nb, false);
1029
1030 vector_shift_left( ndigits, digit, nd, result.digit, v );
1031 result.adjust_hod();
1032
1033 return result;
1034 }
1035
1036 sc_unsigned operator<<(const sc_signed& v) const;
1037 sc_unsigned operator<<(const sc_unsigned& v) const { return operator<<( v.to_int() ); }
1038 sc_unsigned operator<<(int64 v) const { return operator<<( (int)v ); }
1039 sc_unsigned operator<<(uint64 v) const { return operator<<( (int)v ); }
1040 sc_unsigned operator<<(long v) const { return operator<<( (int)v ); }
1041 sc_unsigned operator<<(unsigned long v) const { return operator<<( (int)v ); }
1042 sc_unsigned operator<<(unsigned int v) const { return operator<<( (int)v ); }
1043
1045 operator<<=(int v)
1046 {
1047 if (v <= 0)
1048 return *this;
1049
1050 vector_shift_left( ndigits, digit, v );
1051 adjust_hod();
1052
1053 return *this;
1054 }
1055 const sc_unsigned& operator<<=(const sc_signed& v);
1056 const sc_unsigned& operator<<=(const sc_unsigned& v) { return operator<<=( v.to_int() ); }
1057 const sc_unsigned& operator<<=(int64 v) { return operator<<=((int) v); }
1058 const sc_unsigned& operator<<=(uint64 v) { return operator<<=((int) v); }
1059 const sc_unsigned& operator<<=(long v) { return operator<<=((int) v); }
1060 const sc_unsigned& operator<<=(unsigned long v) { return operator<<=((int) v); }
1061 const sc_unsigned& operator<<=(unsigned int v) { return operator<<=((int) v); }
1062
1063 // RIGHT SHIFT operators:
1064
1065 inline
1067 operator>>(int v) const
1068 {
1069 if (v <= 0) {
1070 return sc_unsigned(*this);
1071 }
1072 int nb = nbits - v;
1073
1074 // If we shift off the end return the sign bit.
1075
1076 if ( 0 >= nb ) {
1077 sc_unsigned result(1, true);
1078 return result;
1079 }
1080
1081 // Return a value that is the width of the shifted value:
1082
1083 sc_unsigned result(nb, false);
1084 if ( nbits < 33 ) {
1085 result.digit[0] = (int)digit[0] >> v;
1086 }
1087 else if ( nbits < 65 ) {
1088 int64 tmp = digit[1];
1089 tmp = (tmp << 32) | digit[0];
1090 tmp = tmp >> v;
1091 result.digit[0] = (sc_digit)tmp;
1092 if ( nb > 32 ) {
1093 result.digit[1] = (tmp >>32);
1094 }
1095 }
1096 else {
1097 vector_extract(digit, result.digit, nbits-1, v);
1098 }
1099 result.adjust_hod();
1100 return result;
1101 }
1102
1103 sc_unsigned operator>>(const sc_signed& v) const;
1104 sc_unsigned operator>>(const sc_unsigned& v) const { return operator>>( v.to_int() ); }
1105 sc_unsigned operator>>(int64 v) const { return operator>>( (int)v ); }
1106 sc_unsigned operator>>(uint64 v) const { return operator>>( (int)v ); }
1107 sc_unsigned operator>>(long v) const { return operator>>( (int)v ); }
1108 sc_unsigned operator>>(unsigned long v) const { return operator>>( (int)v ); }
1109 sc_unsigned operator>>(unsigned int v) const { return operator>>( (int)v ); }
1110
1111 const sc_unsigned&
1113 {
1114 if (v <= 0)
1115 return *this;
1116 vector_shift_right(ndigits, digit, v, 0);
1117 return *this;
1118 }
1119 const sc_unsigned& operator>>=(const sc_signed& v);
1120 const sc_unsigned& operator>>=(const sc_unsigned& v) { return operator>>=(v.to_int()); }
1121 const sc_unsigned& operator>>=(int64 v) { return operator>>=((int)v); }
1122 const sc_unsigned& operator>>=(uint64 v) { return operator>>=((int)v); }
1123 const sc_unsigned& operator>>=(long v) { return operator>>=((int)v); }
1124 const sc_unsigned& operator>>=(unsigned long v) { return operator>>=((int)v); }
1125 const sc_unsigned& operator>>=(unsigned int v) { return operator>>=((int)v); }
1126
1127 // Unary arithmetic operators
1128
1131
1132 // Bitwise NOT operator (unary).
1133
1134 friend SC_API sc_signed operator ~ (const sc_unsigned& u);
1135
1136protected:
1137
1138 int nbits; // number of bits in use.
1139 int ndigits; // number words in 'digits'
1140 sc_digit *digit; // storage for our value.
1141 sc_digit base_vec[SC_BASE_VEC_DIGITS>0?SC_BASE_VEC_DIGITS:1]; // make small values faster.
1142
1143#if !defined(SC_BIGINT_CONFIG_BASE_CLASS_HAS_STORAGE)
1144 bool m_free; // true if should free 'digit'.
1145public:
1146 inline bool digit_is_allocated() const { return m_free; }
1147#else
1148public:
1149 inline bool digit_is_allocated() const { return digit != (sc_digit*)base_vec; }
1150#endif
1151
1152#if defined(SC_BIGINT_CONFIG_TEMPLATE_CLASS_HAS_NO_BASE_CLASS)
1153
1154public: // Temporary object support:
1155
1156 #define SC_UNSIGNED_TEMPS_N (1 << 15) // SC_UNSIGNED_TEMPS_N must be a power of 2.
1157
1158 static sc_unsigned m_temporaries[SC_UNSIGNED_TEMPS_N];
1159 static size_t m_temporaries_i;
1160
1161 static inline sc_unsigned& allocate_temporary( int nb, sc_digit* digits_p )
1162 {
1163 sc_unsigned* result_p = &m_temporaries[m_temporaries_i];
1164 m_temporaries_i = (m_temporaries_i + 1) & (SC_UNSIGNED_TEMPS_N-1);
1165 result_p->digit = digits_p;
1166 result_p->nbits = num_bits(nb);
1167 result_p->ndigits = DIV_CEIL(result_p->nbits);
1168#if !defined(SC_BIGINT_CONFIG_BASE_CLASS_HAS_STORAGE)
1169 result_p->m_free = false;
1170#endif
1171 return *result_p;
1172 }
1173
1174#endif // defined(SC_BIGINT_CONFIG_TEMPLATE_CLASS_HAS_NO_BASE_CLASS)
1175
1176 inline void adjust_hod()
1177 {
1178 sc_digit tmp = 0;
1179 tmp = ~tmp;
1180 unsigned shift = SC_BIT_INDEX( nbits-1 );
1181 digit[ndigits-1] &= ~(tmp << shift);
1182 }
1183
1184public: // back door access:
1185 inline int get_actual_length() const { return nbits; }
1186 inline sc_digit* get_digits() const { return digit; }
1187 inline sc_digit* get_digits() { return digit; }
1188 inline int get_digits_n() const { return ndigits; }
1189 inline int get_hod() const { return ndigits-1; }
1190
1191private:
1192 // Private constructors:
1193
1194 // Constructor for sc_biguint<W>
1195
1196 explicit sc_unsigned( int nb, bool zero );
1197
1198 // Create a copy of v with sign s.
1199 sc_unsigned(const sc_unsigned& v, small_type s);
1200 sc_unsigned(const sc_signed& v, small_type s);
1201
1202 // Private member functions. The called functions are inline functions.
1203
1204 static int num_bits(int nb) { return nb + 1; }
1205
1206 bool check_if_outside(int bit_num) const;
1207
1208 void makezero() { vector_zero( 0, ndigits, digit ); }
1209
1210 public: // sc_ac back door:
1211 sc_digit* get_raw() { return digit; }
1212 int get_raw_nbits() const { return nbits; }
1213};
1214
1215
1216// functional notation for the reduce methods
1217
1218inline bool and_reduce( const sc_unsigned& a ) { return a.and_reduce(); }
1219
1220inline bool nand_reduce( const sc_unsigned& a ) { return a.nand_reduce(); }
1221
1222inline bool or_reduce( const sc_unsigned& a ) { return a.or_reduce(); }
1223
1224inline bool nor_reduce( const sc_unsigned& a ) { return a.nor_reduce(); }
1225
1226inline bool xor_reduce( const sc_unsigned& a ) { return a.xor_reduce(); }
1227
1228inline bool xnor_reduce( const sc_unsigned& a ) { return a.xnor_reduce(); }
1229
1230
1231
1232
1233inline
1234::std::ostream&
1235operator<<( ::std::ostream&, const sc_unsigned& );
1236
1237inline
1238::std::istream&
1239operator>>( ::std::istream&, sc_unsigned& );
1240
1241
1242// IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
1243
1244// ----------------------------------------------------------------------------
1245// CLASS : sc_unsigned_bitref_r
1246//
1247// Proxy class for sc_unsigned bit selection (r-value only).
1248// ----------------------------------------------------------------------------
1249
1250
1251inline
1252::std::ostream&
1253operator<<( ::std::ostream& os, const sc_unsigned_bitref_r& a )
1254{
1255 a.print( os );
1256 return os;
1257}
1258
1259
1260// ----------------------------------------------------------------------------
1261// CLASS : sc_unsigned_bitref
1262//
1263// Proxy class for sc_unsigned bit selection (r-value and l-value).
1264// ----------------------------------------------------------------------------
1265
1266template<class T>
1268 const sc_generic_base<T>& a )
1269{
1270 sc_unsigned temp( length() );
1271 a->to_sc_unsigned(temp);
1272 return *this = temp;
1273}
1274
1275inline
1276::std::istream&
1277operator>>( ::std::istream& is, sc_unsigned_bitref& a )
1278{
1279 a.scan( is );
1280 return is;
1281}
1282
1283
1284// ----------------------------------------------------------------------------
1285// CLASS : sc_unsigned_subref_r
1286//
1287// Proxy class for sc_unsigned part selection (r-value only).
1288// ----------------------------------------------------------------------------
1289
1290// reduce methods
1291
1293{
1294 const sc_unsigned* target_p = m_obj_p;
1295 for ( int i = m_right; i <= m_left; i++ )
1296 if ( !target_p->test(i) ) return false;
1297 return true;
1298}
1299
1301{
1302 return !and_reduce();
1303}
1304
1306{
1307 const sc_unsigned* target_p = m_obj_p;
1308 for ( int i = m_right; i <= m_left; i++ )
1309 if ( target_p->test(i) ) return true;
1310 return false;
1311}
1312
1314{
1315 return !or_reduce();
1316}
1317
1319{
1320 int odd;
1321 const sc_unsigned* target_p = m_obj_p;
1322 odd = 0;
1323 for ( int i = m_right; i <= m_left; i++ )
1324 if ( target_p->test(i) ) odd = ~odd;
1325 return odd ? true : false;
1326}
1327
1329{
1330 return !xor_reduce();
1331}
1332
1333
1334inline
1335::std::ostream&
1336operator<<( ::std::ostream& os, const sc_unsigned_subref_r& a )
1337{
1338 a.print( os );
1339 return os;
1340}
1341
1342
1343// ----------------------------------------------------------------------------
1344// CLASS : sc_unsigned_subref
1345//
1346// Proxy class for sc_unsigned part selection (r-value and l-value).
1347// ----------------------------------------------------------------------------
1348
1349// assignment operators
1350
1351inline
1352const sc_unsigned_subref&
1354{
1355 sc_unsigned aa( length() );
1356 return ( *this = aa = a );
1357}
1358
1359
1360inline
1361::std::istream&
1362operator>>( ::std::istream& is, sc_unsigned_subref& a )
1363{
1364 a.scan( is );
1365 return is;
1366}
1367
1368
1369
1370// ----------------------------------------------------------------------------
1371// CLASS : sc_unsigned
1372//
1373// Arbitrary precision signed number.
1374// ----------------------------------------------------------------------------
1375
1376template<class T>
1378{
1379 int nb = v->length();
1380 if( nb > 0 ) {
1381 nbits = num_bits( nb );
1382 } else {
1383 char msg[BUFSIZ];
1384 std::snprintf(msg, sizeof(msg),
1385 "sc_unsigned( sc_generic_base<T> ) : nb = %d is not valid", nb);
1387 }
1389 if ( ndigits > SC_BASE_VEC_DIGITS ) {
1390 digit = new sc_digit[ndigits];
1391 SC_FREE_DIGIT(true)
1392 }
1393 else {
1394 digit = base_vec;
1395 SC_FREE_DIGIT(false)
1396 }
1397 makezero();
1398 v->to_sc_unsigned(*this);
1399}
1400
1401
1402inline
1403::std::ostream&
1404operator<<( ::std::ostream& os, const sc_unsigned& a )
1405{
1406 a.print( os );
1407 return os;
1408}
1409
1410inline
1411::std::istream&
1412operator>>( ::std::istream& is, sc_unsigned& a )
1413{
1414 a.scan( is );
1415 return is;
1416}
1417
1418inline
1419uint64
1421{
1422 int right = m_right;
1423 if ( right > m_left ) { return to_uint64_reversed(); }
1424 sc_digit* digits = m_obj_p->get_raw();
1425 int adjust = right + 63;
1426 int left = ( adjust < m_left ) ? adjust : m_left;
1427 int left_hob;
1428 int left_i = SC_DIGIT_INDEX(left);
1429 sc_digit mask;
1430 int right_i = SC_DIGIT_INDEX(right);
1431 int right_lob;
1432 unsigned long long result;
1433 switch( left_i - right_i )
1434 {
1435 case 0: // all in same word.
1436 mask = ~((sc_digit)-2<<(left-right));
1437 right_lob = SC_BIT_INDEX(right);
1438 result = (digits[right_i] >> right_lob) & mask;
1439 break;
1440 case 1: // in two words
1441 left_hob = SC_BIT_INDEX(left);
1442 right_lob = SC_BIT_INDEX(right);
1443 mask = ~(((sc_digit)-2)<<left_hob);
1444 result = digits[left_i]&mask;
1445 result = (result << (BITS_PER_DIGIT-right_lob)) |
1446 (digits[right_i]>>right_lob);
1447 break;
1448 default: // in three words
1449 left_hob = SC_BIT_INDEX(left);
1450 right_lob = SC_BIT_INDEX(right);
1451 mask = ~(((sc_digit)-2)<<left_hob);
1452 result = (digits[left_i]&mask);
1453 result = (result << BITS_PER_DIGIT) | digits[right_i+1];
1454 result = (result << (BITS_PER_DIGIT-right_lob)) |
1455 (digits[right_i]>>right_lob);
1456 break;
1457 }
1458 return result;
1459}
1460
1461inline
1462int64
1464{
1465 return (int64)to_uint64();
1466}
1467
1468
1469inline
1470int
1472{
1473 return (int)to_uint64();
1474}
1475
1476inline
1477unsigned int
1479{
1480 return (unsigned int)to_uint64();
1481}
1482
1483inline
1484long
1486{
1487 return (long)to_uint64();
1488}
1489
1490inline
1491unsigned long
1493{
1494 return (unsigned long)to_uint64();
1495}
1496
1497// +----------------------------------------------------------------------------
1498// |"sc_unsigned::to_XXXX"
1499// |
1500// | These functions return an object instance's value as the requested
1501// | native C++ type.
1502// |
1503// | Notes:
1504// | (1) These are set up for BITS_PER_DIGIT == 32.
1505// | Result:
1506// | Native C++ type containing the object instance's value.
1507// +----------------------------------------------------------------------------
1508inline
1509int64
1511{
1512 int64 result;
1513
1514 if ( ndigits == 1 ) {
1515 result = digit[0];
1516 }
1517 else {
1518 result = ( (int64)digit[1] << BITS_PER_DIGIT ) | digit[0];
1519 }
1520 return result;
1521}
1522
1523inline
1524uint64
1526{
1527 uint64 result;
1528
1529 if ( ndigits == 1 ) {
1530 result = digit[0];
1531 }
1532 else {
1533 result = ( (uint64)digit[1] << BITS_PER_DIGIT ) | digit[0];
1534 }
1535 return result;
1536}
1537
1538inline
1539long
1541{
1542 long result;
1543
1544 if ( sizeof(long) < 5 ) {
1545 result = digit[0];
1546 }
1547 else {
1548 if ( ndigits == 1 ) {
1549 result = digit[0];
1550 }
1551 else {
1552 result = ( (uint64)digit[1] << BITS_PER_DIGIT ) | digit[0];
1553 }
1554 }
1555 return result;
1556}
1557
1558
1559inline
1560unsigned long
1562{
1563 unsigned long result;
1564
1565 if ( sizeof(unsigned long) < 5 ) {
1566 result = digit[0];
1567 }
1568 else {
1569 if ( ndigits == 1 ) {
1570 result = digit[0];
1571 }
1572 else {
1573 result = ( (uint64)digit[1] << BITS_PER_DIGIT ) | digit[0];
1574 }
1575 }
1576 return result;
1577}
1578
1579
1580inline
1581int
1583{
1584 int result;
1585
1586 result = (int)digit[0];
1587 return result;
1588}
1589
1590
1591inline
1592unsigned int
1594{
1595 unsigned int result;
1596
1597 result = (unsigned int)digit[0];
1598 return result;
1599}
1600
1601
1602// +----------------------------------------------------------------------------
1603// |"sc_unsigned::sc_unsigned"
1604// |
1605// | This is the object constructor for sc_biguint<W>.
1606// |
1607// | Arguments:
1608// | nb = number of bits the object instance needs to support.
1609// | zero = true if the object's digits should be zeroed.
1610// +----------------------------------------------------------------------------
1611
1612inline
1613sc_unsigned::sc_unsigned( int nb, bool zero ) :
1614 nbits(nb+1), ndigits( DIV_CEIL(nb+1) )
1615{
1616 if ( ndigits <= SC_BASE_VEC_DIGITS ) {
1617 digit = base_vec;
1618 SC_FREE_DIGIT(false)
1619 }
1620 else {
1621 digit = new sc_digit[ndigits];
1622 SC_FREE_DIGIT(true)
1623 }
1624 if ( zero ) {
1625 makezero();
1626 }
1627}
1628
1629#if !defined(SC_BIGINT_CONFIG_BASE_CLASS_HAS_STORAGE)
1630// +----------------------------------------------------------------------------
1631// |"sc_unsigned::sc_unsigned"
1632// |
1633// | This is the object constructor from sc_bigint<W>. It uses the supplied
1634// | value buffer, that will already have been initialized.
1635// |
1636// | Arguments:
1637// | nb = number of bits the object instance needs to support.
1638// | digits_p = storage from sc_bigint<W> to use as our storage.
1639// +----------------------------------------------------------------------------
1640inline
1642 nbits(nb+1), ndigits( DIV_CEIL(nb+1) )
1643{
1644# if defined(SC_BIGINT_CONFIG_TEMPLATE_CLASS_HAS_STORAGE)
1645 if ( ndigits <= SC_BASE_VEC_DIGITS ) {
1646 digit = base_vec;
1647 }
1648 else {
1649 digit = digits_p;
1650 }
1651 SC_FREE_DIGIT(false)
1652# else // TEMPLATE_CLASS_HAS_NO_BASE_CLASS
1653 digit = digits_p;
1654 SC_FREE_DIGIT(false)
1655# endif
1656}
1657#endif // !defined(SC_BIGINT_CONFIG_BASE_CLASS_HAS_STORAGE)
1658
1659// +----------------------------------------------------------------------------
1660// |"sc_unsigned::sc_unsigned"
1661// |
1662// | This is the explicit object constructor for this class.
1663// |
1664// | Arguments:
1665// | nb = number of bits the object instance needs to support.
1666// | zero = true if the object's digits should be zeroed.
1667// +----------------------------------------------------------------------------
1668
1669inline
1671 sc_value_base(), nbits(), ndigits(), digit()
1672{
1673 if( nb > 0 ) {
1674 nbits = num_bits( nb );
1675 } else {
1676 char msg[BUFSIZ];
1677 std::snprintf(msg, sizeof(msg), "%s::%s( int nb ) : nb = %d is not valid",
1678 "sc_unsigned", "sc_unsigned", nb );
1680 }
1682 if ( ndigits > SC_BASE_VEC_DIGITS ) {
1683 digit = new sc_digit[ndigits];
1684 SC_FREE_DIGIT(true)
1685 }
1686 else {
1687 digit = base_vec;
1688 SC_FREE_DIGIT(false)
1689 }
1690 makezero();
1691}
1692
1693
1694} // namespace sc_dt
1695
1696#endif
#define SC_DIGIT_INDEX(BIT_INDEX)
Definition: sc_nbdefs.h:186
#define DIV_CEIL(x)
Definition: sc_nbdefs.h:212
#define SC_BIT_INDEX(BIT)
Definition: sc_nbdefs.h:185
#define SC_MASK_DIGIT(v)
Definition: sc_nbdefs.h:187
#define BITS_PER_DIGIT
Definition: sc_nbdefs.h:164
#define SC_FREE_DIGIT(FLAG)
Definition: sc_nbdefs.h:119
#define SC_BASE_VEC_DIGITS
Definition: sc_nbdefs.h:127
#define SC_API
Definition: sc_cmnhdr.h:148
#define SC_REPORT_ERROR(msg_type, msg)
Definition: sc_report.h:217
const char SC_ID_INIT_FAILED_[]
sc_numrep sc_io_base(systemc_ostream &stream_object, sc_numrep def_base)
Definition: sc_string.h:108
X & operator|=(sc_proxy< X > &px, const sc_proxy< Y > &py)
Definition: sc_lv_base.h:474
bool sc_io_show_base(systemc_ostream &stream_object)
Definition: sc_string.h:113
int small_type
Definition: sc_nbdefs.h:137
sc_proxy< X >::value_type or_reduce(const sc_proxy< X > &a)
Definition: sc_proxy.h:1496
sc_proxy< X >::value_type xor_reduce(const sc_proxy< X > &a)
Definition: sc_proxy.h:1512
const sc_big_op_info< WL, true, WR, true >::add_result operator+(const sc_bigint< WL > &left, const sc_bigint< WR > &right)
Definition: sc_big_ops.h:125
sc_numrep
Definition: sc_string.h:56
@ SC_DEC
Definition: sc_string.h:60
X & operator^=(sc_proxy< X > &px, const sc_proxy< Y > &py)
Definition: sc_lv_base.h:613
sc_carry one_and_zeros(int n)
Definition: sc_nbutils.h:115
unsigned long long uint64
Definition: sc_nbdefs.h:216
void vector_extract(const sc_digit *source_p, sc_digit *destination_p, const int high_bit, const int low_bit)
const sc_big_op_info< WL, true, WR, true >::sub_result operator-(const sc_bigint< WL > &left, const sc_bigint< WR > &right)
Definition: sc_big_ops.h:884
void vector_zero(int from_i, int to_i, sc_digit *target_p)
sc_proxy< X >::value_type nor_reduce(const sc_proxy< X > &a)
Definition: sc_proxy.h:1504
SC_API std::string to_string(sc_enc)
sc_lv_base reverse(const sc_proxy< X > &x)
Definition: sc_lv_base.h:852
X & operator&=(sc_proxy< X > &px, const sc_proxy< Y > &py)
Definition: sc_lv_base.h:340
unsigned int sc_digit
Definition: sc_nbdefs.h:161
inline::std::istream & operator>>(::std::istream &is, sc_bit &a)
Definition: sc_bit.h:396
sc_proxy< X >::value_type and_reduce(const sc_proxy< X > &a)
Definition: sc_proxy.h:1480
void vector_shift_left(const int from_digits_n, const sc_digit *from_p, const int to_digits_n, sc_digit *to_p, const int shift_n)
inline::std::ostream & operator<<(::std::ostream &os, const sc_bit &a)
Definition: sc_bit.h:388
void vector_shift_right(const int target_n, sc_digit *target_p, int bits_n, const sc_digit fill)
sc_proxy< X >::value_type xnor_reduce(const sc_proxy< X > &a)
Definition: sc_proxy.h:1520
sc_proxy< X >::value_type nand_reduce(const sc_proxy< X > &a)
Definition: sc_proxy.h:1488
sc_bit operator~(const sc_bit &a)
Definition: sc_bit.h:312
long long int64
Definition: sc_nbdefs.h:215
virtual bool concat_get_data(sc_digit *dst_p, int low_i) const
Definition: sc_unsigned.h:215
void initialize(const sc_unsigned *obj_p, int index_)
Definition: sc_unsigned.h:161
virtual int concat_length(bool *xz_present_p) const
Definition: sc_unsigned.h:204
sc_unsigned_bitref_r(const sc_unsigned_bitref_r &a)
Definition: sc_unsigned.h:176
virtual bool concat_get_ctrl(sc_digit *dst_p, int low_i) const
Definition: sc_unsigned.h:208
void print(::std::ostream &os=::std::cout) const
Definition: sc_unsigned.h:236
virtual uint64 concat_get_uint64() const
Definition: sc_unsigned.h:206
virtual void concat_set(const sc_unsigned &src, int low_i)
sc_unsigned_bitref(const sc_unsigned_bitref &a)
Definition: sc_unsigned.h:277
virtual void concat_set(const sc_signed &src, int low_i)
virtual void concat_set(int64 src, int low_i)
void scan(::std::istream &is=::std::cin)
virtual void concat_set(uint64 src, int low_i)
unsigned long to_ulong() const
Definition: sc_unsigned.h:1492
const std::string to_string(sc_numrep numrep=SC_DEC) const
const std::string to_string(sc_numrep numrep, bool w_prefix) const
virtual int concat_length(bool *xz_present_p) const
Definition: sc_unsigned.h:395
virtual bool concat_get_ctrl(sc_digit *dst_p, int low_i) const
sc_unsigned_subref_r(const sc_unsigned_subref_r &a)
Definition: sc_unsigned.h:349
uint64 to_uint64_reversed() const
virtual uint64 concat_get_uint64() const
unsigned int to_uint() const
Definition: sc_unsigned.h:1478
void initialize(const sc_unsigned *obj_p, int left_, int right_)
Definition: sc_unsigned.h:332
void print(::std::ostream &os=::std::cout) const
Definition: sc_unsigned.h:415
virtual bool concat_get_data(sc_digit *dst_p, int low_i) const
virtual void concat_set(uint64 src, int low_i)
const sc_unsigned_subref & operator=(const sc_unsigned_subref_r &a)
sc_unsigned_subref(const sc_unsigned_subref &a)
Definition: sc_unsigned.h:486
virtual void concat_set(const sc_signed &src, int low_i)
void scan(::std::istream &is=::std::cin)
virtual void concat_set(int64 src, int low_i)
virtual void concat_set(const sc_unsigned &src, int low_i)
sc_unsigned operator<<(int64 v) const
Definition: sc_unsigned.h:1038
sc_unsigned operator>>(unsigned int v) const
Definition: sc_unsigned.h:1109
const std::string to_string(sc_numrep numrep, bool w_prefix) const
void check_range(int l, int r) const
Definition: sc_unsigned.h:712
sc_digit * get_raw() const
Definition: sc_unsigned.h:634
sc_unsigned operator>>(int64 v) const
Definition: sc_unsigned.h:1105
sc_unsigned operator<<(uint64 v) const
Definition: sc_unsigned.h:1039
const sc_unsigned & operator>>=(unsigned int v)
Definition: sc_unsigned.h:1125
bool iszero() const
bool test(int i) const
Definition: sc_unsigned.h:869
sc_unsigned_bitref * temporary_bitref() const
Definition: sc_unsigned.h:664
bool nor_reduce() const
Definition: sc_unsigned.h:821
int get_actual_length() const
Definition: sc_unsigned.h:1185
const sc_unsigned_subref_r & range(int i, int j) const
Definition: sc_unsigned.h:740
sc_digit * get_raw()
Definition: sc_unsigned.h:1211
bool xnor_reduce() const
Definition: sc_unsigned.h:826
unsigned long to_ulong() const
Definition: sc_unsigned.h:1561
sc_unsigned operator<<(const sc_unsigned &v) const
Definition: sc_unsigned.h:1037
const sc_unsigned & operator>>=(long v)
Definition: sc_unsigned.h:1123
void invert(int i)
Definition: sc_unsigned.h:882
unsigned int to_uint() const
Definition: sc_unsigned.h:1593
const sc_unsigned & operator>>=(int64 v)
Definition: sc_unsigned.h:1121
virtual bool concat_get_ctrl(sc_digit *dst_p, int low_i) const
virtual void concat_set(int64 src, int low_i)
virtual void concat_set(uint64 src, int low_i)
static sc_unsigned * temporary()
Definition: sc_unsigned.h:766
sc_unsigned operator<<(unsigned long v) const
Definition: sc_unsigned.h:1041
sc_unsigned operator>>(unsigned long v) const
Definition: sc_unsigned.h:1108
sc_digit base_vec[SC_BASE_VEC_DIGITS >0?SC_BASE_VEC_DIGITS:1]
Definition: sc_unsigned.h:1141
void invalid_init(const char *type_name, int nb) const
sc_digit * get_digits() const
Definition: sc_unsigned.h:1186
const sc_unsigned & operator<<=(const sc_unsigned &v)
Definition: sc_unsigned.h:1056
void invalid_range(int l, int r) const
virtual uint64 concat_get_uint64() const
int64 to_int64() const
Definition: sc_unsigned.h:1510
virtual int concat_length(bool *xz_present_p) const
Definition: sc_unsigned.h:635
bool digit_is_allocated() const
Definition: sc_unsigned.h:1146
sc_unsigned_subref * temporary_subref() const
Definition: sc_unsigned.h:726
int get_raw_nbits() const
Definition: sc_unsigned.h:1212
const sc_unsigned & operator<<=(uint64 v)
Definition: sc_unsigned.h:1058
bool xor_reduce() const
void invalid_index(int i) const
sc_unsigned operator<<(long v) const
Definition: sc_unsigned.h:1040
const sc_unsigned & operator<<=(unsigned int v)
Definition: sc_unsigned.h:1061
void clear(int i)
Definition: sc_unsigned.h:851
void set(int i, bool v)
Definition: sc_unsigned.h:880
void check_index(int i) const
Definition: sc_unsigned.h:659
virtual void concat_set(const sc_unsigned &src, int low_i)
sc_unsigned operator>>(const sc_unsigned &v) const
Definition: sc_unsigned.h:1104
int get_hod() const
Definition: sc_unsigned.h:1189
sc_unsigned operator>>(uint64 v) const
Definition: sc_unsigned.h:1106
const std::string to_string(sc_numrep numrep=SC_DEC) const
void dump(::std::ostream &os=::std::cout) const
const sc_unsigned & operator<<=(long v)
Definition: sc_unsigned.h:1059
bool sign() const
Definition: sc_unsigned.h:810
void get_packed_rep(sc_digit *buf) const
const sc_unsigned & operator<<=(int64 v)
Definition: sc_unsigned.h:1057
const sc_unsigned & operator>>=(uint64 v)
Definition: sc_unsigned.h:1122
bool and_reduce() const
sc_unsigned operator>>(long v) const
Definition: sc_unsigned.h:1107
void scan(::std::istream &is=::std::cin)
int get_digits_n() const
Definition: sc_unsigned.h:1188
const sc_unsigned & operator>>=(int v)
Definition: sc_unsigned.h:1112
const sc_unsigned_bitref_r & bit(int i) const
Definition: sc_unsigned.h:694
sc_unsigned_subref & range(int i, int j)
Definition: sc_unsigned.h:732
sc_unsigned_bitref & bit(int i)
Definition: sc_unsigned.h:686
virtual bool concat_get_data(sc_digit *dst_p, int low_i) const
int to_int() const
Definition: sc_unsigned.h:1582
bool or_reduce() const
sc_unsigned operator<<(unsigned int v) const
Definition: sc_unsigned.h:1042
sc_unsigned(int nb=sc_length_param().len())
Definition: sc_unsigned.h:1670
void print(::std::ostream &os=::std::cout) const
Definition: sc_unsigned.h:799
uint64 to_uint64() const
Definition: sc_unsigned.h:1525
sc_digit * get_digits()
Definition: sc_unsigned.h:1187
const sc_unsigned & operator>>=(const sc_unsigned &v)
Definition: sc_unsigned.h:1120
const sc_unsigned & operator>>=(unsigned long v)
Definition: sc_unsigned.h:1124
long to_long() const
Definition: sc_unsigned.h:1540
virtual void concat_set(const sc_signed &src, int low_i)
void set_packed_rep(sc_digit *buf)
void set(int i)
Definition: sc_unsigned.h:835
int length() const
Definition: sc_unsigned.h:808
bool nand_reduce() const
Definition: sc_unsigned.h:816
sc_unsigned operator>>(int v) const
Definition: sc_unsigned.h:1067
const sc_unsigned & operator<<=(unsigned long v)
Definition: sc_unsigned.h:1060