MikeLankamp / fpm

C++ header-only fixed-point math library
https://mikelankamp.github.io/fpm
MIT License
649 stars 79 forks source link

static constexpr on local variables #64

Closed grimaldini closed 1 month ago

grimaldini commented 1 month ago

I noticed there's a few inline functions in math.hpp and that you are using constexpr variables within functions. Should you be using static constexpr variables at a function level, so that that gets optimized at -O0? If you make those changes then you can probably replace the inline function with constexpr functions as well.

MikeLankamp commented 1 month ago

Hi @grimaldini, if I make the function-local constexpr variables static, then yes, that seems to save an instruction in -O0 (or so godbolt shows me). However, it introduces a new global variable for each variable that's made static (in -O0).

Honestly, the single instruction cost sounds better to me, and it's only -O0 anyway.

I can't make those inline functions constexpr anyway, because static or not, variable declarations are not allowed in constexpr functions in C++11 (which fpm supports as minimum).

So I'm afraid I will have to leave it as-is.