llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
27.98k stars 11.55k forks source link

Comparison of clang and gcc for `(a + b) / pow(a - b, 5)` calculation #78964

Open k-arrows opened 7 months ago

k-arrows commented 7 months ago

The following simulates a portion of code for a molecular dynamics application (which calculates a certain coefficient).

#include <cmath>

double foo(double off, double cut) {
    return (off + cut) / pow(off - cut, 5);
}

Consider this under -Ofast. https://godbolt.org/z/d7c8s836r

Clang is not the best compared to GCC because it does not make use of (off + cut), which is already calculated. I took Arm as an example, but the same is true for x86.

k-arrows commented 7 months ago

Clang is not the best compared to GCC because it does not make use of (off + cut), which is already calculated.

Maybe my wording was not good. When calculating a / b, it seems that clang calculates this as (1 / b) * a, even though a and b are already prepared.

k-arrows commented 7 months ago

If pow(off - cut, 5) in the program is rewritten as ((off - cut) * (off - cut) * (off - cut) * (off - cut) * (off - cut)), the suboptimality cannot be observed. So I think the problem is in the handling of pow().