Intel® Quartus® Prime Software
Intel® Quartus® Prime Design Software, Design Entry, Synthesis, Simulation, Verification, Timing Analysis, System Design (Platform Designer, formerly Qsys)
16837 Discussions

Single precision converter for floating point

Altera_Forum
Honored Contributor II
2,989 Views

I hope someone can help with this. I've not been able to google my way out of this problem. I want to convert floating point numbers to single precision in hex and storing it in some file for initializing memory and then inputting this data to floating point IPs for further processing. Could anyone suggest any tools available to do this? 

Thanks.
0 Kudos
20 Replies
Altera_Forum
Honored Contributor II
1,948 Views

In my understanding IEEE floating point format and "single precision" are synonymous. I guess, you're referring to numbers in ASCII string representation?

0 Kudos
Altera_Forum
Honored Contributor II
1,948 Views

Here's an example of what I'm trying to do: 

Decimal Number: 6.8 

Single Precision 32-bit representation(IEEE-754) of 6.8: 40D99999
0 Kudos
Altera_Forum
Honored Contributor II
1,948 Views

I think the best way may be write a small program in your favorite programming language to perform the conversion and write the MIF file. Beware of endianess issues. 

 

Another possible alternative (I'm not familiar with FP in Verilog nor VHDL) is to do whatever you need in HDL. 

You can infer a ROM by declaring an array that is only read synchronously using the "initial" block to initialize it. 

Quartus can handle complex initialization blocks, including reading from files
0 Kudos
Altera_Forum
Honored Contributor II
1,948 Views

But isn't any such function available in Excel or any prewritten program available for anything like this? I thought this would be a fairly day to day task for most HDL coding.

0 Kudos
Altera_Forum
Honored Contributor II
1,948 Views

not a ton of DSP or floating point talk on this board, so you may try searching elsewhere. first result i found seemed good: 

 

http://babbage.cs.qc.edu/ieee-754/decimal.html
0 Kudos
Altera_Forum
Honored Contributor II
1,948 Views

@thepancake, thanks for the link. But the link only lets you enter one number at a time and then calculate it. I wanted to convert numbers in a given file to their IEEE-754 representation.

0 Kudos
Altera_Forum
Honored Contributor II
1,948 Views

One option is to use to_float function from IEEE float_pkg to convert real constants to IEEE float format in VHDL.

0 Kudos
Altera_Forum
Honored Contributor II
1,948 Views

By any luck is there any such thing in Verilog? (*Fingers Crossed*)

0 Kudos
Altera_Forum
Honored Contributor II
1,948 Views

if you write a C program to printf %x a floating point variable what happens?

0 Kudos
Altera_Forum
Honored Contributor II
1,948 Views

It just ate my long answer, so here is the short one... in 'C' 

 

float value = 6.8; 

printf("%08X",*(unsigned int *)(&value)) ; 

 

 

40D9999 

 

No conversion needed, just a reinterpretation of the representation.
0 Kudos
Altera_Forum
Honored Contributor II
1,948 Views

cool, just what i was thinking

0 Kudos
Altera_Forum
Honored Contributor II
1,948 Views

 

--- Quote Start ---  

if you write a C program to printf %x a floating point variable what happens? 

--- Quote End ---  

 

 

The compiler complains *and* you get the wrong answer. Other than that... 

 

You need to fool the compiler a little with the typecast and reference/de-reference...
0 Kudos
Altera_Forum
Honored Contributor II
1,948 Views

ah well i would have typecast after i got the complaining, then i would have stumbled upon your answer :)

0 Kudos
Altera_Forum
Honored Contributor II
1,948 Views

Thanks, works nicely.

0 Kudos
Altera_Forum
Honored Contributor II
1,948 Views

@donq, How'd you get the clue though that this will work? This really is making a fool out of the compiler!

0 Kudos
Altera_Forum
Honored Contributor II
1,948 Views

The longer version (the one that got eaten by the 'internets') explained a little more. 

 

There might be an easier way but this way does it (step by step) by extracting a pointer to the 32-bit value (&value), typecasting that pointer into a pointer to a 32-bit int (unsigned int *), then taking the value pointed to by this pointer (*). That value is passed to printf to print it as a hex value ("%08X").  

 

The best part is that all the hard work is done by the pre-processor by just controlling the way that printf looks at the value. There is really no conversion done at all, it just prints it the way it sees it, only now it sees it as an int.  

 

All the 'easy' ways generally wind up with the value being actually converted to an int (e.g. 6.8 truncates into 6) then printing the value of the int. not what you want. 

 

This way winds up only being about 6 RISK machine instructions to load the value and format string into registers, then a call to '_printf'. 

 

I've always though the 'best' code is no code at all. Let the pre-processor do all the work!:)
0 Kudos
Altera_Forum
Honored Contributor II
1,948 Views

 

--- Quote Start ---  

float value = 6.8; printf("%08X",*(unsigned int *)(&value)) ;There might be an easier way but this way does it (step by step) by extracting a pointer to the 32-bit value (&value), typecasting that pointer into a pointer to a 32-bit int (unsigned int *), then taking the value pointed to by this pointer (*). That value is passed to printf to print it as a hex value ("%08X"). 

--- Quote End ---  

I have used this solution before. If you have a good understanding of how things work at a low level, it's actually pretty straight forward. 

 

An alternative approach for the curious: 

union { float f; unsigned int i; } value; value.f = 6.8; printf("%08X", value.i);
0 Kudos
Altera_Forum
Honored Contributor II
1,948 Views

When I first saw the code I thought that the (unsigned int *) part was a mistake and I changed it to (unsigned float *). Code worked with this modification as well.

0 Kudos
Altera_Forum
Honored Contributor II
1,948 Views

 

--- Quote Start ---  

... and I changed it to (unsigned float *). ... 

--- Quote End ---  

 

 

Not exactly sure what an unsigned float is (sort of like jumbo shrimp). What compiler are you using that didn't flag this as an error of some sort? 

 

I think the compiler must have used the unsigned as an unsigned int, ignored the float part, and then treated it the same as my original. I would hope that (float *) would not work because that would put you right back where you started. 

 

The 'union' way of doing it is semantically different, but an identical end-effect. Just looking at the same value in a different way.
0 Kudos
Altera_Forum
Honored Contributor II
1,896 Views

Microsoft C Compiler, now that I see more carefully I do get this warning when compiling code: 

'unsigned' : can not be used with type 'float'
0 Kudos
Reply