Bug 20445 - garbage returned when trying to get the second word of a double value
garbage returned when trying to get the second word of a double value
Status: RESOLVED DUPLICATE of bug 21920
Product: gcc
Classification: Unclassified
Component: c
3.3.2
: P2 critical
: ---
Assigned To: Not yet assigned to anyone
:
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2005-03-12 21:18 UTC by Eijiro Sumii
Modified: 2005-07-23 22:49 UTC (History)
1 user (show)

See Also:
Host: sparcv9-sun-solaris2
Target: sparcv9-sun-solaris2
Build: sparcv9-sun-solaris2
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Eijiro Sumii 2005-03-12 21:18:34 UTC
> gcc -v
Reading specs from /usr/local/lib/gcc-lib/sparcv9-sun-solaris2/3.3.2/specs
Configured with: /usr/local/src/gnu/gcc-3.3.2/configure --enable-languages=c,c+¥
+ sparcv9-sun-solaris2
Thread model: posix
gcc version 3.3.2
> cat test.c
#include <stdio.h>

typedef int int32;

void out(int32 i) {
  printf("0x%x¥n", i);
}

double inp(void) {
  return 123.456789;
}

void getlo() {
  double d = inp();
  int32 i = *((int32 *)&d + 1);
  out(i);
}

void gethi() {
  double d = inp();
  int32 i = *(int32 *)&d;
  out(i);
}

int main() {
  gethi();
  getlo();
  return 0;
}
> gcc -m32 -O2 test.c -o test
> ./test
0x405edd3c
0xfbbb3834
> gcc -m32 -O test.c -o test
> ./test
0x405edd3c
0x7ee0b0b
Comment 1 Andrew Pinski 2005-03-12 21:23:52 UTC
You are violating C aliasing rules, try an union.  Read http://gcc.gnu.org/bugs.html which show how to 
deal with this.
Comment 2 Eijiro Sumii 2005-03-12 21:40:27 UTC
(In reply to comment #1)
> You are violating C aliasing rules, try an union.  Read
http://gcc.gnu.org/bugs.html which show how to 
> deal with this.

Oops, my double stupidity (forgetting the ISO rule _and_ overlooking the FAQ
entry)...  Apology for bothering you.
Comment 3 Andrew Pinski 2005-06-05 09:10:36 UTC
Reopening to ...
Comment 4 Andrew Pinski 2005-06-05 09:10:51 UTC
Mark as a dup of bug 21920.

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