Archive for December, 2011

convert 16 bit “half float” to 32 bit float

12/01/2011

See also:
IEEE 754 single-precision binary floating-point format: binary32

16-bit Floating-Point Rules

float convert16to32float(unsigned int Xi_Value)
{ 
 //TODO:test, imporve, optimize..
  int exp = (Xi_Value>>10) & 0x01f;
  float g=0;
  unsigned int *i=(unsigned int *) & g;
  Xi_Value &= 0xffff;
  *i=0;
  *i |= Xi_Value << 16 & 0x80000000; //sign
  *i |= (exp-15+127) << 23; //exponent 
  *i |= (Xi_Value & 0x3FF) << 13; //value
  return g;
}
Advertisements