Jump to content

Why is length() not a static method on the sc integer types?


Recommended Posts

Hello all,


I am writing some template code which manipulates some SystemC values, and I came across a place where I am having problems deducing the bit-width of an sc_uint.  In looking over the source code for sc_uint (and sc_uint_base), I see that while the width must be specified as a template parameter, it is stored in a (non-const) member variable of every instance.  I was wondering why the length is implemented this way, and not as a static method which returns the template parameter?  I can't find any place where the width can be changed dynamically.  If the length were implemented as a reference to the template parameter, template code would be easier to write, and every object would be smaller.






Link to post
Share on other sites

The templated SystemC datatypes all build upon a "dynamic bitwidth" implementation (e.g. sc_uint_base) that has its bitwidth set upon construction.  The templates themselves only provide constructors (initializing the length of the base class), assignment operators, and related functions.


It would of course be possible to add a static member to the sc_(big)(u)int templates to provide direct access to the template argument:

template< int W >
class sc_int
   : public sc_int_base
    static const int static_length = W; // or some other name
    // ...

For these classes, an external helper can be defined easily as well:

template< typename T > struct number_traits; // undefined by default

// specialization for SystemC integer datatypes
template< int W > struct number_traits< sc_dt::sc_int<W> > { static const int length = W; };
// same for sc_uint, sc_bigint, ...

But for true generic programming, the concatenation and range classes would require additional meta programming to make this fully usable.

Secondly, as the base classes should provide access to the length() of the instances as well, this function would require to be declared virtual (as the base classes are non-templated).  This would increase the size (and eventually the runtime cost) again.

That said, if you're interested in bit true datatypes implemented with fully deduced widths etc., you might want to use the Mentor/Calypto "Algorithmic C datatypes", see http://calypto.com/en/products/catapult/overview/.


Greetings from Oldenburg,

Link to post
Share on other sites


This topic is now archived and is closed to further replies.

  • Create New...