this post was submitted on 10 Apr 2025
11 points (100.0% liked)

C++

1976 readers
11 users here now

The center for all discussion and news regarding C++.

Rules

founded 2 years ago
MODERATORS
 

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.

you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 2 points 1 week 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);