Jump to content
manikanta.mashetti

Type casting && Floating point nos.

Recommended Posts

hello,

 

Iam a beginner to systemC language..

 

1.In systemC ,is there any data type to represent strings ...not a c++ datatypes (char)

 

2.How to convert a float datatype to bit vector (or) logic vector (sc_bv/sc_lv) datatype and viceversa.

 

3.how to convert a fixed data type(sc_fixed/sc_fix) to the ieee 754 floating point representation..is there any standard data type to represent the floating point datatype in systemC.

 

 

Thanks in advance

Share this post


Link to post
Share on other sites

1.In systemC ,is there any data type to represent strings ...not a c++ datatypes (char)

 

char is a datatype to represent a single character.  There is the std::string datatype for strings C++, which should be used in SystemC as well.

 

2.How to convert a float datatype to bit vector (or) logic vector (sc_bv/sc_lv) datatype and viceversa.

 

There is no explicit support for these conversions provided by SystemC already.  In the proof-of-concept implementation provided by Accellera, there are the (non-standard) classes sc_dt::scfx_ieee_(double/float), which define bitfields for the individual parts of a float or double type.  With this, you should be able to do the conversion in terms of a concatenation (beware of endianness).  The following is untested:

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 );

The reverse conversion is left to the reader. ;-)

 

3.how to convert a fixed data type(sc_fixed/sc_fix) to the ieee 754 floating point representation..is there any standard data type to represent the floating point datatype in systemC.
 
C++ provides float and double datatypes already.  Usually, C++ implementations follow the IEEE 754 representation internally.  There is no explicit separate SystemC datatype for IEEE 754 floating point numbers.  You can convert sc_fix(ed) numbers via the to_float and to_double member functions.

 

Greetings from Oldenburg,

  Philipp

Share this post


Link to post
Share on other sites

Thank you philipp,

 

1.std::string str1=in1.to_string(SC_BIN) ;

 

In this in1 is a unsigned integer data type(assume) ,Iam converting this to binary format and storing in to the str1(string),but here I want to store this str1 in to the bitvector(sc_bv) ,If I do this it shows some error msgs...my task is to assign this string to another variable..which is bitvector..

 

 

2.yes sir ,what you said is corrrect but ,you are converting double to an bit vector ,which is double precision format (64 bits),if I want to use single precision floating point (32 bits) how should I do..?

 

else

 

If I want to convert  fixed point data type(sc_fixed<32,16>) to sc_bv ...how should I do...?

 

 

 

 

Thank you....

Share this post


Link to post
Share on other sites

1.std::string str1=in1.to_string(SC_BIN) ;

 

In this in1 is a unsigned integer data type(assume) ,Iam converting this to binary format and storing in to the str1(string),but here I want to store this str1 in to the bitvector(sc_bv) ,If I do this it shows some error msgs...my task is to assign this string to another variable..which is bitvector..

 

Why do you convert the unsigned integer to a string before assigning it to the bitvector?  A direct assignment should work just fine.

If you indeed need to construct/assign a bitvector from a (C++) string, you need to pass the "C string" representation to the bitvector:

in1.to_string(SC_BIN).c_str();

2. […] if I want to use single precision floating point (32 bits) how should I do..?

 

As I said in my answer, there are two bitfield classes available: scfx_ieee_double and scfx_ieee_float. For single precision, use the second one and adjust the sizes of the intermediate variables according to the 32-bit floating point representation.

 

 

If I want to convert  fixed point data type(sc_fixed<32,16>) to sc_bv ...how should I do...?

 

To do this conversion, you need to assign the value to an (unsigned) integer variable in between, as no direct conversion is provided here.  Just shift the fractional bits beyond the fixed point and then do an assignment to the bitvector afterwards.

  sc_unsigned us( fx.wl() );         // temporary unsigned integer variable of appropriate size
  us = ( fx << (fx.wl()-fx.iwl()) ); // shift to integer bits only
  sc_bv<32> bv = us;                 // assign to bitvector

See the IEEE Std. 1666-2011 SystemC standard for details on the datatype APIs in SystemC.

 

/Philipp

Share this post


Link to post
Share on other sites

Hello sir,

 

1.In my module one input,one output ,and input is a float type and the output is sc_bv(bitvector) ,I have done like this.

 

sc_in<float> in1;

sc_out<sc_bv<32>  >out1;

float temp1;

sc_bv<32> temp2;

 

 

// Inside the Method process I have written like this

 

temp1=in1;

temp2=(temp1.to_string(SC_BIN,false)).c_str();

out=temp2;

 

 

//But it is showing Error like   request for member 'to_string' in '((check *)this)->check::temp',which is of non class type 'float'.

 

//I also tried std::string temp2 instead of bit vector..(shows two number of errors.)

 

//what i observed is to_string is applicable to systemC datatypes..But my problem is like above How should I solve this type of conversion...

 

Please give the suggestions...

Thank you.

Share this post


Link to post
Share on other sites

1.In my module one input,one output ,and input is a float type and the output is sc_bv(bitvector) ,I have done like this.

 

I've sketched the double to bitvector conversion in an earlier answer already:

 

There is no explicit support for these conversions provided by SystemC already.  In the proof-of-concept implementation provided by Accellera, there are the (non-standard) classes sc_dt::scfx_ieee_(double/float), which define bitfields for the individual parts of a float or double type.  With this, you should be able to do the conversion in terms of a concatenation (beware of endianness).  The following is untested:

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 );

 

To do it analogously for 32-bit floats, just use sc_dt::scfx_ieee_float instead of sc_dt::scfx_ieee_double, adjust the sizes of the exponent and mantissa variables according to the 32-bit IEEE 754 representation, and read the mantissa value by a single call to the "mantissa()" function of sc_dt::scfx_ieee_float, instead of the two calls to mantissa0/1.

 

Generally speaking, I would suggest to start with a good C++ book before delving deeper into SystemC.  The new one from Bjarne Stroustrup is a very good one for starters.

 

Greetings from Oldenburg,

  Philipp

Share this post


Link to post
Share on other sites

This I got it sir,suppose if I want to do the reverse conversion (bitvector to float).

how should I proceed...Is there any non standard classes like above...

or give me some hint I will try to solve..

thank you.

Hello Sir,

Please use a to_int() method on the bit vector, and assign

the resulting integer to a float. Please note that SystemC

is just a C++ library - nothing more. So whatever works for

standard C++, also works for SystemC. More than anything

else, experiment a bit and that will improve your understanding

of SystemC. Hope that helps.

 

 

so

Share this post


Link to post
Share on other sites

hello,

 

If I do like that ,bit vector to integer conversion is fine,but integer to float it doesn't work correctly..

 

Eg:

 

If I want to store 125.2345...If i do conversion from (float -> bitvector -> integer -> float) ,float stores only 125.0

 

but I want to retrieve back same data(125.2345).

Share this post


Link to post
Share on other sites

Thank you philipp,

 

1.std::string str1=in1.to_string(SC_BIN) ;

 

In this in1 is a unsigned integer data type(assume) ,Iam converting this to binary format and storing in to the str1(string),but here I want to store this str1 in to the bitvector(sc_bv) ,If I do this it shows some error msgs...my task is to assign this string to another variable..which is bitvector..

 

 

2.yes sir ,what you said is corrrect but ,you are converting double to an bit vector ,which is double precision format (64 bits),if I want to use single precision floating point (32 bits) how should I do..?

 

else

 

If I want to convert  fixed point data type(sc_fixed<32,16>) to sc_bv ...how should I do...?

 

 

 

 

Thank you....

Hello Sir,

May I suggest that you clear your own confusion ?

The SystemC methods are working as designed.

When you store a floating point number as a bit

vector, how is the decimal point stored ? If it is

stored as a IEEE 754 format number, then the 

mantissa, exponent etc., are clearly defined,

and then you would have to do a conversion

from IEEE format to a decimal number. The

IEEE format is special, and an ordinary bit

vector would not represent it correctly. You would

have to first convert the IEEE number to a 

float.

Share this post


Link to post
Share on other sites

This I got it sir,suppose if I want to do the reverse conversion (bitvector to float).

how should I proceed...Is there any non standard classes like above...

or give me some hint I will try to solve..

 

The sc_dt::scfx_ieee_float class supports setting the individual parts via member functions as well.

Therefore, you can do the bitvector to float conversion by using the following algorithm sketch:

  1. Extract the corresponding bits from vector by using the "range()" functions for sign, mantissa and exponent
  2. Convert these ranges to integers, e.g. by using the to_(u)int() functions
  3. Set the individual parts of an sc_dt::scfx_ieee_float object to these integers
  4. Assign/copy the sc_dt::scfx_ieee_float object to a plain float.

/Philipp

Share this post


Link to post
Share on other sites

Hie Philipp,

I got a clear Idea now ..and I got a solution for my problem....but

If I use this nonstandard classes ,Is my code is synthesisable.?

what is exactly nonstandard classes...

Thank You...

Hello Sir,

You are absolutely right about your concerns regarding

use of non-standard SystemC classes, specifically if they

can be synthesized. The alternative is to use standard

bit vectors and the methods defined for them, to achieve

your goal. A new book on SystemC has full worked out

examples about converting from decimal to IEEE 754

2008 format floating point numbers. These examples

use standard bit vectors only. Hope that helps.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×