Forum Discussion
Altera_Forum
Honored Contributor
15 years agoMost modern systems do use the standard IEEE floating point formats. But that isn't completely universal.
Likely issues that will catch out the unwary are whether denormalised encodings are used for values near zero, and (on embedded systems especially) the support of NaN and Infinity. For double precision (64bit) there is the additional issue of word order, usually this will match the byte order - but, for instance, ARM have done an FP unit where this wasn't the case (now depracated). More problematic are issues when you access the same memory as different types. For instance:int get_float(float f)
{
char buffer = { 1, 1, 1, 1};
int *a = (void *)&buffer;
float *b = (void *)&buffer;
*b = f;
return *a;
} can legitimately be compiled to code the returns the constant 0x01010101 as the compiler can reorder the *b and *a since they refer to different types (neither of which is char), and then optimise away the rest of the function. With gcc, inserting 'asm volatile("":::"memory")' between the two lines will enforce the expected behaviour. This statement is also useful to force the instruction sequence when trying to avoid cpu stalls following memory reads, and to reduce the number of live registers.