This is the mail archive of the gcc-bugs@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: Is this a bug?


Hi Neil,

Thanks for the explanation.

My original problem was this,

#include<stdio.h>

int main(void)
{
int r, i;

i = 0;
i = ++i % 2;
printf("test 1 : i = %d\n", i);

i = 0;
i = i++ % 2;
printf("test 2 : i = %d\n", i);

return 0;
}

Test 2 gives 0. I was expecting 1. Should it not?

Here is the disassembled version, maybe you can tell
what's going on. I don't know much about assembly code.

i = 0;
1083c: c0 27 bf e8 clr [ %fp + -24 ]
i = ++i % 2;
10840: d0 07 bf e8 ld [ %fp + -24 ], %o0
10844: 92 02 20 01 add %o0, 1, %o1
10848: 90 10 00 09 mov %o1, %o0
1084c: d0 27 bf e8 st %o0, [ %fp + -24 ]
10850: 93 3a 20 1f sra %o0, 0x1f, %o1
10854: 95 32 60 1f srl %o1, 0x1f, %o2
10858: 92 02 00 0a add %o0, %o2, %o1
1085c: 95 3a 60 01 sra %o1, 1, %o2
10860: 92 10 00 0a mov %o2, %o1
10864: 95 2a 60 01 sll %o1, 1, %o2
10868: 90 22 00 0a sub %o0, %o2, %o0
1086c: d0 27 bf e8 st %o0, [ %fp + -24 ]
printf("test 1 : i = %d\n", i);
10870: 13 00 00 42 sethi %hi(0x10800), %o1
10874: 90 12 61 78 or %o1, 0x178, %o0 ! 10978 <_lib_version+0x8>
10878: d2 07 bf e8 ld [ %fp + -24 ], %o1
1087c: 40 00 40 6d call 20a30 <_PROCEDURE_LINKAGE_TABLE_+0x6c>
10880: 01 00 00 00 nop

i = 0;
10884: c0 27 bf e8 clr [ %fp + -24 ]
i = i++ % 2;
10888: d0 07 bf e8 ld [ %fp + -24 ], %o0
1088c: 92 02 20 01 add %o0, 1, %o1
10890: d2 27 bf e8 st %o1, [ %fp + -24 ]
10894: 93 3a 20 1f sra %o0, 0x1f, %o1
10898: 95 32 60 1f srl %o1, 0x1f, %o2
1089c: 92 02 00 0a add %o0, %o2, %o1
108a0: 95 3a 60 01 sra %o1, 1, %o2
108a4: 92 10 00 0a mov %o2, %o1
108a8: 95 2a 60 01 sll %o1, 1, %o2
108ac: 90 22 00 0a sub %o0, %o2, %o0
108b0: d0 27 bf e8 st %o0, [ %fp + -24 ]
printf("test 2 : i = %d\n", i);
108b4: 13 00 00 42 sethi %hi(0x10800), %o1
108b8: 90 12 61 90 or %o1, 0x190, %o0 ! 10990 <_lib_version+0x20>
108bc: d2 07 bf e8 ld [ %fp + -24 ], %o1
108c0: 40 00 40 5c call 20a30 <_PROCEDURE_LINKAGE_TABLE_+0x6c>
108c4: 01 00 00 00 nop

return 0;


Thanks for your help.

Songtao





At 09:45 PM 1/2/2003 +0000, Neil Booth wrote:
Songtao Chen wrote:-

> Hi Neil,
>
> Thanks for replying.
>
> Can you tell me why the parentheses are irrelevant,
> any doc to support that?

For the same reason that (2) is the same as 2, and not 3.  Like I said,
post-increment returns the value before incrementing.

Neil.

>
> Songtao
>
> At 09:23 PM 1/2/2003 +0000, Neil Booth wrote:
> >Songtao Chen wrote:-
> >
> >> Hi there,
> >>
> >> It appears to be a bug to me.
> >
> >No, i++ returns the value before increment.  Parentheses are irrelevant.
> >
> >Neil.
>






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