Multiply by 0 optimization

后端 未结 2 408
囚心锁ツ
囚心锁ツ 2021-01-07 20:42

Suppose i have:

double f(const double *r) {
    return 0*(r[0]*r[1]);
}

should compiler be able to optimize out the segment, or does it sti

相关标签:
2条回答
  • 2021-01-07 20:53

    Depends on whether the compiler implements IEEE754. Neither C nor C++ requires that a compiler supports NaN, but IEEE754 does.

    0 讨论(0)
  • 2021-01-07 21:03

    It isn't only inf and NaN that prevent the optimization there, it's also the sign - 0.0 * something negative is -0.0, otherwise it's 0.0, so you actually have to compute the sign of r[0]*r[1].

    0 讨论(0)
提交回复
热议问题