Jump to content

How to define `sc_fixed` variables that have word/integer lengths that are based off (but not the same size as!) those of a template parameter


monkey_nuts

Recommended Posts

Let's say I want to write a generic SystemC module which does some algorithm processing on fixed point data of various representations, so I implement it as as module template where the fixed point input/output type is specified as a template parameter. My algorithm requires more headroom for it's intermediate operations than is available by the input/output type so I'd like to declare some intermediate fixed point variables which have more integer bits but the same number of fractional bits. How do I do this? Is there any way to query the word/integer lengths of the `sc_fixed` template parameter and use/modify them to define new `sc_fixed` variables within the module. 

I've tried using the `wl()` and `iwl()` methods but these only get evaluated at runtime so can't be used as template arguments.

Here is an example of what doesn't work:

template <typename T>
SC_MODULE(proc)
{
    sc_in<bool> clk;    // Clock signal
    sc_in<bool> rst_n;  // Reset signal
    sc_in<T> in_sig;    // Input signal
    sc_out<T> out_sig;  // Output signal

    const T tttt;
    typedef sc_fixed<tttt.wl()+3, tttt.iwl()+3> internal_sig_t;
    internal_sig_t internal_sig_0, internal_sig_1;

    ...
}

Is there a way of doing this which actually works?

Note: As a workaround I could just pass the `sc_fixed` template parameters instead of the predefined type but this starts getting very messy if I have multiple input/output types with different options for not just for word/integer lengths so would prefer not to go this route.

Link to comment
Share on other sites

If not intended for direct synthesis (i.e., HLS), you could use the sc_fix and sc_ufix base classes. See IEEE-1666-2011 section 7.1 for more information. Section 7.10.7 describes the accessor methods to interrogate incoming objects. The non-"ed" classes simply use the constructor to establish the parameters.

It's all basic C++ stuff.

 

 

Link to comment
Share on other sites

Thanks for the feedback.

The design is indeed intended for synthesis (using Stratus HLS) so I'm stuck with the "...ed" classes (or actually the Stratus implementations of these which is a whole other matter).

The only appropriate accessor methods that I can see in section 7.10.7 are `wl()` and `iwl()` which, as indicated in my original post, don't work for me.  😐

Link to comment
Share on other sites

You can use C++ partial template specialization to deduce the template parameters for sc_fixed<> , and then store off the values you find.

There is an example of this (to find the dimensions of a C array) in this file:

https://github.com/hlslibs/matchlib_toolkit/blob/main/include/auto_gen_fields.h

Note the code for finding the size of single dimensional array using partial template specilization:

image.png.ea440fdf737ffbd5e2f7ed4c9810252a.png

This all should go thru HLS no problem since it all gets resolved in the front end.

-Stuart Swan

Link to comment
Share on other sites

@StuartSwan: Nice one! It took me a while to figure out the correct syntax to get this working but the following seems to do the trick:

template <class T>
class type_traits
{
public:
    static const int wl{0};
    static const int iwl{0};
};


template <int W, int I>
class type_traits< sc_fixed<W, I> >
{
public:
    static const int wl{W};
    static const int iwl{I};
};


template <typename T>
SC_MODULE(proc)
{
    sc_in<bool> clk;    // Clock signal
    sc_in<bool> rst_n;  // Reset signal
    sc_in<T> in_sig;    // Input signal
    sc_out<T> out_sig;  // Output signal

    typedef sc_fixed<type_traits<T>::wl+3, type_traits<T>::iwl()+3> internal_sig_t;
    internal_sig_t internal_sig_0, internal_sig_1;

    ...
}

It even works on the Stratus fixed point type implementations.

Many thanks for your help!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...