This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Optimization bug


I have found an optimization bug in egcs (I tried snapshot of Nov 1, it
also existed in gcc 2.7.2.3, in fact it was worse). Try the following code

extern unsigned long int strtoul(const char *, char **, int);
extern int printf(const char *, ...);
extern void srandom(unsigned);

unsigned calc_mp(unsigned mod)
{
	unsigned a,b,c;
	c=-1;
	a=c/mod;
	b=0-a*mod;
	if (b > mod) { a += 1; b-=mod; }
	/* a should be floor(2^32/mod) b should be 2^32-a*mod (ie 2^32 % mod) */
	return b;
}

unsigned calc_mp2(unsigned mod)
{
	unsigned a,b,c;
	c=-1;
	a=c/mod;
	b=0-a*mod;
	if (b > mod) { a += 1; b-=mod; }
	return a;
}

int main(int argc, char *argv[])
{
	unsigned x = strtoul(argv[1],0,0);
	unsigned y = calc_mp(x);
	printf("%s: %u %u\n",argv[0],x,y);
#ifdef MP2
	srandom(calc_mp2(x));/* toss away */
#endif
	return 0;
}


Here is the script I used to test it.   myegcs is egcs of Nov 1

#!/bin/sh
gcc -Wall -O9 -S -o gccop.s calc_mp.c
gcc -Wall -O9 -DMP2 -S -o gccop2.s calc_mp.c
gcc -Wall -S -o gccnoop.s calc_mp.c
gcc -Wall -DMP2 -S -o gccnoop2.s calc_mp.c
for name in gccop gccop2 gccnoop gccnoop2
do
	gcc -o $name.out $name.s
	./$name.out 1234
done
myegcs -Wall -O9 -S -o myegcsop.s calc_mp.c
myegcs -Wall -O9 -DMP2 -S -o myegcsop2.s calc_mp.c
myegcs -Wall -S -o myegcsnoop.s calc_mp.c
myegcs -Wall -DMP2 -S -o myegcsnoop2.s calc_mp.c
for name in myegcsop myegcsop2 myegcsnoop myegcsnoop2
do
	myegcs -o $name.out $name.s
	./$name.out 1234
done

and here is the output

./gccop.out: 1234 192
./gccop2.out: 1234 680     <--- strange that it got it right!
./gccnoop.out: 1234 680
./gccnoop2.out: 1234 680
./myegcsop.out: 1234 192
./myegcsop2.out: 1234 192
./myegcsnoop.out: 1234 680
./myegcsnoop2.out: 1234 680


OK So the value should indeed be 680 (2^32 mod 1234) (I am working on i386 BTW)
In addition you will find that the code for calc_mp2 is optimized rather badly.

Thanks,

Reuben


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]