小编Sup*_*pAl的帖子

编译器按位不运算的优化

我有一个简单的函数测试,如果两个数组彼此相反。它们似乎是相同的,只是tmp变量不同。一个有效,另一个无效。我一辈子都无法弄清楚为什么编译器会对此进行优化-如果确实存在优化问题(我的编译器是IAR Workbench v4.30.1)。这是我的代码:

// 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]))
    { …
Run Code Online (Sandbox Code Playgroud)

c c++ embedded iar

31
推荐指数
3
解决办法
1180
查看次数

标签 统计

c ×1

c++ ×1

embedded ×1

iar ×1