On architectures that support IEEE 754 float representation, a common trick to quickly cast an unsigned long val to a float in the range [0, 1) is the following:
unsigned long temp = (val & 0x07fffff) | 0x3f800000; return *((float *)&temp) - 1.0f; With gcc 4.0.1 using -O2 optimization this trick no longer seems to work. -- Summary: unsigned long to float conversion by casting unsigned long* to float* fails in -O2 builds. Product: gcc Version: 4.0.1 Status: UNCONFIRMED Severity: normal Priority: P2 Component: c AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: gino at dtecta dot com CC: gcc-bugs at gcc dot gnu dot org GCC build triplet: i686-pc-linux-gnu GCC host triplet: i686-pc-linux-gnu GCC target triplet: i686-pc-linux-gnu http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23421