Bug 23913 - float/int binary conversion -- "-O2 -march=i686"
Summary: float/int binary conversion -- "-O2 -march=i686"
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
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2005-09-16 11:09 UTC by Lars Christian Jensen
Modified: 2005-09-16 14:18 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments
.i file that triggers the bug. (447 bytes, application/octet-stream)
2005-09-16 11:12 UTC, Lars Christian Jensen
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Lars Christian Jensen 2005-09-16 11:09:30 UTC
Full example files are here:
http://www.pvv.ntnu.no/~larschri/cast-bug/

The compiled program fails to do binary conversion between float and int. Both
sizeof(int) and sizeof(float) is 4, so the two floats below should have the same
binary representation after being written/read to/from the integer object.

float f1 = 4.5;
int i =    *((int*)&f1);
float f2 = *((float*)&i);
assert(f1 == f2);

$ gcc --version
gcc (GCC) 4.0.1 20050727 (Red Hat 4.0.1-5)
Copyright (C) 2005 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ gcc -O2 -march=i686 cast-bug.c
$ ./a.out
a.out: cast-bug.c:8: main: Assertion `f1 == f2' failed.
Aborted
Comment 1 Lars Christian Jensen 2005-09-16 11:12:33 UTC
Created attachment 9739 [details]
.i file that triggers the bug.
Comment 2 Eric Botcazou 2005-09-16 11:16:32 UTC
Your code violates ISO C aliasing rules.  See the docs about -fstrict-aliasing.
Comment 3 Lars Christian Jensen 2005-09-16 11:34:15 UTC
I see - thanks!

I didn't use the -Wall options when changing from C++ to C. g++ had the same
problem, but didn't give any warnings with -Wall.
Comment 4 Lars Christian Jensen 2005-09-16 12:12:32 UTC
I have filed this enhancment ticket: [Bug c++/23915]
Comment 5 Andrew Pinski 2005-09-16 14:18:05 UTC
Reopening to ...
Comment 6 Andrew Pinski 2005-09-16 14:18:34 UTC
Mark as a dup of bug 21920.

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