I have a simple function testing if two arrays are each others inverse.
They are seemingly identical, except for a tmp
variable. One works the other doesn't. I can't for the life of me figure out why the compiler would optimize this out - if it indeed is an optimization problem (my compiler is IAR Workbench v4.30.1). Here's my code:
// this works as expected
uint8 verifyInverseBuffer(uint8 *buf, uint8 *bufi, uint32 len)
{
uint8 tmp;
for (uint32 i = 0; i < len; i++)
{
tmp = ~bufi[i];
if (buf[i] != tmp)
{
return 0;
}
}
return 1;
}
// this does NOT work as expected (I only removed the tmp!)
uint8 verifyInverseBuffer(uint8 *buf, uint8 *bufi, uint32 len)
{
for (uint32 i = 0; i < len; i++)
{
if (buf[i] != (~bufi[i]))
{
return 0;
}
}
return 1;
}
The first version of the code works, the second does not. Can anyone figure out why? Or come with some tests to probe what is wrong?
if (buf[i] != bufi[i] ^ 0xff)
, orif (buf[i] ^ bufi[i] != 0xff)
, orif (buf[i] ^ bufi[i] ^ 0xff)
. – Leontineleontyne