Jump to content

double to sc_fix(ed) conversion


scsc

Recommended Posts

I dug up this old thread on double(float) conversion to sc_bv: 

 

Apparently we can convert a double to sc_ieee_double (see the snippet below from that post) then manually convert to sc_fix(ed) from mantissa and exponent or convert to sc_fixed from the bitvector (with some truncation because the bitvector would have different bitwidth to sc_fixed. But just wonder if there are built in methods that perform the conversion to sc_fixed directly? 

double d = ...;

sc_dt::scfx_ieee_double id(d); // convert to IEEE 754 bitfield

// prepare parts
bool               sgn = id.negative();
sc_dt::sc_uint<11> exp = id.exponent();
sc_dt::sc_uint<53> mnt = ( sc_dt::uint64( id.mantissa1() ) << 20 ) | id.mantissa0();

// concatenate parts to bitvector
sc_dt::sc_bv<64> bv = ( sgn, exp, mnt );

 

Link to comment
Share on other sites

The example you are quoting is conversion to bit vector representation. LRM sections 7.10.14 and 7.10.18 specify sc_fix(ed) and there you find constructors accepting double as well as assignment operators for double. So you can do a direct assignment.

Link to comment
Share on other sites

Thanks Eyck,

I tried to use double input for an sc_fixed data type (see the attached example). Somehow the direct assignment didn't work. Or because the errors I got were from sc_port.h, which has not been used at all. So I wasn't sure the direct assignment actually works.

 

#define SC_INCLUDE_FX
#define N 25
#include <systemc.h>

using namespace sc_core;
using namespace sc_dt;
typedef sc_fix<32, 16, SC_RND> fixed_type;

double fix_fir(double _in[])
{
    
    fixed_type in[N], c[N], t[N], y;

    int i;
    double ct = 0.9987966;
    for (i = 0; i < N; i++) {
        in[i] = _in[i];
        c[i] = ct;
        ct = ct / 2;
    }

    for (i = 0; i < N; i++) {
        t[i] = c[i] * in[i];
        y += t[i];
    }
    return y;
}

int sc_main(int argc, char* argv[]) {

    double _in[25];
    double y;
    for (int i = 0; i < 25; i++) {
        _in[i] = (double)i;
    }

    y = fix_fir(_in);
    cout << "y = " << y << endl;

    return 0;
}

 

Some errors are like these:
Error (active)    E0706    expected a ',' or '>'    sc_fixed_fir   \Documents\systemc-2.3.2\src\sysc\communication\sc_port.h  386
Error (active)    E0306    default argument not at end of parameter list    sc_fixed_fir    \Documents\systemc-2.3.2\src\sysc\datatypes\fx\sc_fixed.h    61 

Line 61 of sc_fixed.h has this definition in which the default argument to sc_fixed is "int N":

// classes defined in this module
template <int W, int I, sc_q_mode Q, sc_o_mode O, int N> class sc_fixed;

Link to comment
Share on other sites

  • 2 weeks later...
9 hours ago, Eyck said:

Your problem is the


#define N 25

before including systemc.h since N is used as template parameter name in some systemc class templates.

I put your code on edaplayground: https://www.edaplayground.com/x/s6Fu with a few minor changes and there it compiles and works....

Thanks again Eyck. I confirmed that #define N was the culprit. I can run the example after changing N to something else (along with a typo in the typedef line). This is a tricky one. But the errors made sense looking back as there is "int N" in sc_fixed template

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...