optimization/7427: gcc-3.1.1 -O2 problem for checksum calculation (powerpc)

Andrew Pinski pinskia@physics.uc.edu
Sun Jul 28 18:46:00 GMT 2002


The following reply was made to PR optimization/7427; it has been noted by GNATS.

From: Andrew Pinski <pinskia@physics.uc.edu>
To: Makoto Fujiwara <makoto@ki.nu>
Cc: gcc-gnats@gcc.gnu.org
Subject: Re: optimization/7427: gcc-3.1.1 -O2 problem for checksum calculation (powerpc)
Date: Sun, 28 Jul 2002 21:38:09 -0400

 C aliasing rules say that variables with two different types do
 not belong to the same alias set (except for char* and void*).
 
 The correct way to fix the problem is using an union:
 
 #include <stdio.h>
 
 struct buf {
 =A0 =A0 =A0 =A0 int data;
 };
 
 union buf1 {
         struct buf buf1;
         unsigned short m[sizeof(struct buf)/sizeof(short)];
 };
 
 bug(m)
 =A0 =A0 =A0 =A0 struct buf *m;
 {
 =A0 =A0 =A0 =A0 int sum =3D 0;
 =A0 =A0 =A0 =A0 union buf1 w;
 
 =A0 =A0 =A0 =A0 bzero(&w.buf1, sizeof tmp);
 =A0 =A0 =A0 =A0 w.buf1 =3D m->data;
 
 =A0 =A0 =A0 =A0 sum +=3D w.m[0];
 =A0 =A0 =A0 =A0 sum +=3D w.m[1];
 
 =A0 =A0 =A0 =A0 printf("sum =3D 0x%x\n", sum);
 
 =A0 =A0 =A0 =A0 return 0;
 }
 
 main()
 {
 =A0 =A0 =A0 =A0 struct buf m;
 
 =A0 =A0 =A0 =A0 m.data =3D 0x12345678;
 =A0 =A0 =A0 =A0 bug(&m);
 }
 
 Thanks,
 Andrew Pinski
 



More information about the Gcc-prs mailing list