Sign In

Don't have a Xilinx account yet?

  • Choose to receive important news and product information
  • Gain access to special content
  • Personalize your web experience on Xilinx.com

Create Account

Username

Password

Forgot your password?
XClose Panel
Xilinx Home
Reply
Visitor
andrewmessier
Posts: 5
Registered: ‎06-21-2012
0

Microblaze typecast (U32 to float)

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,

 

Visitor
andrewmessier
Posts: 5
Registered: ‎06-21-2012
0

Re: Microblaze typecast (U32 to float)

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.

 

Super Contributor
norman_wong
Posts: 143
Registered: ‎05-28-2012
0

Re: Microblaze typecast (U32 to float)

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.

 

 

Newbie
mouloudrahmani
Posts: 2
Registered: ‎08-15-2012
0

Re: Microblaze typecast (U32 to float)

I think this will work:

 

u32 A;

float B;

 

B = *(float*) &A;