- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic to the Top
- Bookmark
- Subscribe
- Printer Friendly Page
Microblaze typecast (U32 to float)
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
06-21-2012 04:13 PM
In my Microblaze system I have a BRAM Interface. I can read the values from this BRAM using:
Xil_In32()
This function returns an unsigned 32-bit integer .. but I want to interpret the 32 bits I get back as a float. I guess I'm asking how to do the type-cast.
This doesn't work:
u32 A;
float B;
B = (float) A;
I believe this is simply a 'cast' as opposed to a 'type-cast', and the value will be unchanged. For example if A was the number 3, then B would be 3.0. The hex number 0x00000003 inerpreted as a float is not 3 however.
Thanks,
Re: Microblaze typecast (U32 to float)
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
06-25-2012 08:01 AM
I figured this out, for anyone that is interested. The key is unions. Example:
union u_type{
u32 i
float f;
}
union u_type cnvrt;
cnvrt.i = 0x0x3F9D70A4
printf("%f",cnvrt.f): // result should be 1.23
Just make sure that whatever types are in the union have the same bit-size.
Re: Microblaze typecast (U32 to float)
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
06-25-2012 10:42 PM
Reinterpreting an 32 bit integer as a 32 bit float is well known problem that the C language standards don't seem to address. Wiki has a good article, http://en.wikipedia.org/wiki/Type_punning, on the more common approaches. Here's 3 common methods, including the union method that you use.
float reinterpret_int(u32 A)
{
float B;
B = *(float*)&A;
return(B);
}
float reinterpret_int(u32 A)
{
union u_type
{ u32 i
float f;
};
union u_type cnvrt;
cnvrt.i = A;
return(cnvrt.f);
}
float reinterpret_int(u32 A)
{
float B;
memcpy(&B, &A, 4);
return(B);
}
Both pointer recast and union appear to be shunned by C purists. Some compilers detect the operations as errors. The memcpy method moves it out of the compiler's view. The dirty deed in done out of sight in the memcpy. All depend on
- float and u32 having 32 bits,
- float and u32 have the same alignment requirements,
- same endian
- both float and u32 values are memory variables. Not constants or registers.
An assembler routine would be the most efficient as all it would do is return the incoming register...assuming 32 bit registers. But is a pretty grim job having to figure out the compiler's parameter passing convention.
I don't know of any official C language way of doing this. Definitely the best place for such an architecture dependent operation would in the compiler. Maybe someone who keeps up with C standards will have some comments.
Re: Microblaze typecast (U32 to float)
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
08-23-2012 10:42 AM
I think this will work:
u32 A;
float B;
B = *(float*) &A;











