[SDL] Portable float packing/unpacking.

Ryan C. Gordon icculus at icculus.org
Mon Feb 13 16:46:25 PST 2006


>>a. compiler option like -fno-strict-aliasing
>>b. use a union
>>c. compiler-specific hack: __attribute__((mayalias))
>>d. use (char*) to access the memory
> 
> Also remember that none of these will help when the variable is
> optimized away into the FPU stack (though volatile may help there,
> I'm not sure). :)

My understanding is that any of the four choices above will squash 
aliasing problems and force the variable off the FPU on GCC. Casting to 
char was a concession to all the piles of legacy code...of the four, the 
GCC developers seem to encourage unions.

In the case of a serialize function, this might just make a copy of the 
float onto the stack when shuffling it into the union and leave it on 
the FPU too for further processing.

The volatile keyword should, I would think, disable any significant FPU 
work on that variable, or force it to store out after each 
operation...but I'm really not sure either, now that I think about it.  :)

For what it's worth, I've used serialize functions with considerably 
less thought put into them across VS.NET 2002/2003/2005, Metrowerks C++, 
and GCC on countless CPU architectures and OSes, and not had problems, 
so it's probably not worth losing too much sleep over it.

(Until your program is inexplicably crashing, of course!)

--ryan.





More information about the SDL mailing list