- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
In my understanding IEEE floating point format and "single precision" are synonymous. I guess, you're referring to numbers in ASCII string representation?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
@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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
One option is to use to_float function from IEEE float_pkg to convert real constants to IEEE float format in VHDL.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
By any luck is there any such thing in Verilog? (*Fingers Crossed*)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
if you write a C program to printf %x a floating point variable what happens?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
cool, just what i was thinking
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
--- 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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ah well i would have typecast after i got the complaining, then i would have stumbled upon your answer :)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks, works nicely.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
@donq, How'd you get the clue though that this will work? This really is making a fool out of the compiler!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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!:)- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
--- 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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
--- 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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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'- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page