User account creation filtered due to spam.

Bug 23421 - unsigned long to float conversion by casting unsigned long* to float* fails in -O2 builds.
Summary: unsigned long to float conversion by casting unsigned long* to float* fails i...
Status: RESOLVED DUPLICATE of bug 21920
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 4.0.1
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
Depends on:
Reported: 2005-08-16 12:28 UTC by Gino van den Bergen
Modified: 2005-08-16 12:35 UTC (History)
1 user (show)

See Also:
Host: i686-pc-linux-gnu
Target: i686-pc-linux-gnu
Build: i686-pc-linux-gnu
Known to work:
Known to fail:
Last reconfirmed:

stand-alone c example program (278 bytes, text/x-csrc)
2005-08-16 12:31 UTC, Gino van den Bergen

Note You need to log in before you can comment on or make changes to this bug.
Description Gino van den Bergen 2005-08-16 12:28:53 UTC
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.
Comment 1 Gino van den Bergen 2005-08-16 12:31:54 UTC
Created attachment 9502 [details]
stand-alone c example program

The file contains a failing "frand32" function and a workaround using a union.
Comment 2 Andrew Pinski 2005-08-16 12:35:12 UTC
Yes because you are violating C aliasing rules.

*** This bug has been marked as a duplicate of 21920 ***