Jump to content

Float number to bitfield


Scord

Recommended Posts

Hello,
 

  firstly I would like to tell that I am totally new in SystemC, but have some experience in VHDL (6 months I have been designing and learning in it). Now I have jumped from VHDL to designing in C++ using a SystemC library.

And my question is,

is there any way to convert float number to bitfield or some logical vector which would represent this number?

(I mean some function, or method or smt else).

 

(My task is to convert float number to Double precision floating point format, and with it do some arithmetical operations).

 

I have find some suggestions how to do that but none of them actually worked when I tried to build it.
Can you give me some suggestion about how to do that please, or how would you do that? I would be really grateful to anybody who could give me something to catch on.

Link to post
Share on other sites

I think I don't quite understand the issue. When you say "(My task is to convert float number to Double precision floating point format, and with it do some arithmetical operations)." you can do that easily in C

 

 float f;

double d;

 

 d = static_case<double>(f);  // or d = f;

 

If you mean that you want to be able to manipulate the individual bits of a floating point number, then you could possibly use the sc_fixed_fast data type, which is based on double - see the IEEE 1666-2011 language reference manual 7.10.3.2

 

regards

Alan

Link to post
Share on other sites

Hello, 

thank you both for your advices.

What I really need to work with this equation: Rovnica.jpg.. or any similar to it  (same structure, different numbers).. to put it simply, I need convert 100*2^(0) and 100*2^(-200) to Double precision format  ( respectivelly to have input in float or integer numbers and than convert it to bitfield or how to say it) I need to work with this number on bit level and have access to every bit (a bus).

I am sorry if I am saying something wrong, or you do not understand me but I just do not know how to convert it to bitfield (or bus)  which would represent this numbers from equation as Double precision format number. (because I need to work with it as bus and create an arythmetic logic for it ... and at the end I will need to synthetize it and implement it to FPGA)


From this
 rrr.jpg
I need to create something like this

 dsa.png

 

exponent 11bits, Normalized mantisa 52 bits. 

Normalized mantisa, I mean that mantisa (M=100) is divided 6 times by 2 (result  M=1.09375) and to exponent (already biased +1023) I added 6 bits as a result of dividing mantisa to normalized form (-200+1023+6 in binary:011 0011 1101).

I am sorry if I am expressing something wrong, but I tried somehow simply perform you what I meant by this "converting" from float to bitfield. 

Is there any way how to do it?
 

Link to post
Share on other sites

Hello, 

thank you both for your advices.

What I really need to work with this equation: Rovnica.jpg.. or any similar to it  (same structure, different numbers).. to put it simply, I need convert 100*2^(0) and 100*2^(-200) to Double precision format  ( respectivelly to have input in float or integer numbers and than convert it to bitfield or how to say it) I need to work with this number on bit level and have access to every bit (a bus).

I am sorry if I am saying something wrong, or you do not understand me but I just do not know how to convert it to bitfield (or bus)  which would represent this numbers from equation as Double precision format number. (because I need to work with it as bus and create an arythmetic logic for it ... and at the end I will need to synthetize it and implement it to FPGA)

From this

 rrr.jpg

I need to create something like this

 dsa.png

 

exponent 11bits, Normalized mantisa 52 bits. 

Normalized mantisa, I mean that mantisa (M=100) is divided 6 times by 2 (result  M=1.09375) and to exponent (already biased +1023) I added 6 bits as a result of dividing mantisa to normalized form (-200+1023+6 in binary:011 0011 1101).

I am sorry if I am expressing something wrong, but I tried somehow simply perform you what I meant by this "converting" from float to bitfield. 

Is there any way how to do it?

 

Hello Sir,

It appears that you are trying to use the IEEE 754 double/float formats

with a fixed number of bits for exponent, mantissa and sign -- is this

correct ? If yes, you would have to take a look at sample code published

in a book last year. There is no quick way to do achieve your goal. Hope

that helps.

Link to post
Share on other sites

I recommend to use the conversion as explained in this thread:

http://forums.accellera.org/topic/1637-type-casting-floating-point-nos/

 

note that the code of Philipp was untested and it contains some minor errors. Below the working code:

  double val = ...

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

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

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

Link to post
Share on other sites

Archived

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

×
×
  • Create New...