Re: (ARM) Wrong conditional codes when paired with tst instruction

2019-10-19 Thread Richard Earnshaw
On 18/10/2019 19:43, AlwaysTeachingable . wrote: > The following C code: > unsigned int wrong(unsigned int n){ > return (n%2) ? 0 : 42; > } > > should return 42 when n is odd and 0 when n is even. No. Your code returns 42 when n is even. It's equivalent to "return ((n%2) != 0) ? 0 : 42;" Now i

(ARM) Wrong conditional codes when paired with tst instruction

2019-10-18 Thread AlwaysTeachingable .
The following C code: unsigned int wrong(unsigned int n){ return (n%2) ? 0 : 42; } should return 42 when n is odd and 0 when n is even. But ARM gcc 8.2 with -O3 produces following assembly: tst r0, #1 moveq r0, #42 movne r0, #0 bx lr tst r0,#1 sets Z=1 iff r0 is even, and moveq r0,#42 executes