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 it's more obvious that when n is odd the expression is true so
returns 0.
>
> But ARM gcc 8.2 with -O3 produces following assembly:
>
> tst r0, #1
> moveq r0, #42
> movne r0, #0
> bx lr
Which, of course, is exactly what your source code asked for.
>
> tst r0,#1 sets Z=1 iff r0 is even, and moveq r0,#42 executes iff Z=1,
> therefore
> it sets r0 to 42 when r0 is even, which is wrong given the C code above (
> it should return 0 ).
>
R.