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]
Other format: [Raw text]

Re: gcc miscompiling duff's device (probaby two different bugs)


On 03/02/2010 10:34 AM, Pjotr Kourzanov wrote:

>> int duff4_fails(char * dst,const char * src,const size_t n)
>> {
>>   const size_t rem=n % 4, a=rem + (!rem)*4;
>>   char * d=dst+=a;
>>   const char * s=src+=a;
>>   /* gcc bug? dst+=n; */
>>   
>>     switch (rem) {
>>     case 0:  for(dst+=n;d<dst;d+=4,s+=4) {
>>     /*case 0:*/	d[-4]=s[-4];
>>     case 3:	d[-3]=s[-3];
>>     case 2:	d[-2]=s[-2];
>>     case 1:	d[-1]=s[-1];
>> 	      }
>>     }
>> 	return 0;
>> }
>> The first time around the loop the initializer (d+=n) is jumped around, so
>> d == dst.  At the end of the loop, d+=4, so d > dst.  Therefore the loop
>> exits.
> 
>   And its wrong since it shouldn't jump around the initializer.

Sure it should.  On entry to that loop, rem == 3.

Andrew.


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