I am trying to calculate a motor position from an encoder, however that does not really matter. I just checked godbolt.org to see if my inline function compiled into a single multiplication and was pretty disappointed that it didn’t. I used -O3 for maximum optimization.

Does somebody know why it does not compile into a single function, or what I had to change to achieve that? You can find my code here: https://godbolt.org/z/qT9srfPT1 .

I know that it does not really matter, but I am curious.

  • Speiser0@feddit.org
    link
    fedilink
    arrow-up
    2
    ·
    19 days ago

    The compiler can’t replace every floating point division with a multiplication with the reciprocal. And it can’t assume associativity. See also -ffast-math.

    You can do this: return (double)steps * (2.0 * PI / 4096.0);