[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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



>                                            千葉市中央区長洲
>                                                    藤原  誠
あの後、更に返事を書いて、良く見直したら printf の結果が自分で
納得出来ないものになっていたのですが、

更に返事をもらってしまいました。
http://gcc.gnu.org/cgi-bin/gnatsweb.pl
に view #:  7427
と入力すれば見えるもののはずですが....

---
(藤原)


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 {
� � � � int data;
};

union buf1 {
        struct buf buf1;
        unsigned short m[sizeof(struct buf)/sizeof(short)];
};

bug(m)
� � � � struct buf *m;
{
� � � � int sum = 0;
� � � � union buf1 w;

� � � � bzero(&w.buf1, sizeof tmp);
� � � � w.buf1 = m->data;

� � � � sum += w.m[0];
� � � � sum += w.m[1];

� � � � printf("sum = 0x%x\n", sum);

� � � � return 0;
}

main()
{
� � � � struct buf m;

� � � � m.data = 0x12345678;
� � � � bug(&m);
}

Thanks,
Andrew Pinski