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.链接已复制
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 filesnot 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.htmlIt 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.--- 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...
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!:)--- 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);
--- 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.
