Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use constexpr in default math functions where possible #7662

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

kfish
Copy link

@kfish kfish commented Jun 4, 2024

This allows some basic math, palette generation etc. to be done in constructors or at compile-time.

imgui_internal.h: Add constexpr (replace "static inline" with "static constexpr") in math functions. This also implements ImFabs and ImFmod explicitly as fabsf and fmodf are not necessarily constexpr. Other non-constexpr functions (anything involving transcendentals, sqrt etc) remain "static inline".

kfish added a commit to kfish/impop that referenced this pull request Jun 5, 2024
@ocornut
Copy link
Owner

ocornut commented Jun 7, 2024

It doesn't seem super wise to replace fabsf() and fmodf() in our context without an in-depth explanation and proof that this is going to be equivalent on many fronts?

Do you actually need e.g. ImFabsf() at compile-time?

@ocornut
Copy link
Owner

ocornut commented Jun 10, 2024

I have looked at https://github.com/kfish/impop/blob/master/include/impop_color.h to get a little more context at your use case.

My slight issue with turning everything constexpr is that this is technically another contract to fulfill for dear imgui.
I need to stress out that since this is imgui_internal.h, if there is a situation where for some reason it is needed to make one not constexpr anymore, we might have to revert it. I think it's super unlikely, but it's meant to convey we don't guarantee use of imgui_internal.h as neatly as use of imgui.h.

Which function do you think you actually need?
Since our ColorConvertRGBtoHSV() ColorConvertFloat4ToU32 etc won't be constexpr anyway, I am not sure I see how it would improve your codebase?

@kfish
Copy link
Author

kfish commented Jun 11, 2024

Hi @ocornut, thanks for looking into this. I understand that we can't guarantee correct fmodf and fabs etc. implementations on all platforms. The trivial implementations I provided might not handle errors the same way as the standard functions, for example for fmodf, "If a range error occurs due to underflow, the correct result (after rounding) is returned." There will be constexpr versions of these in C++23 (and using eg. std::fmodf not C99 fmodf) so anyone who wants constexpr versions would be better off using those in future anyway.

As my use case is fairly trivial, just looking at color values, I'll keep these definitions in impop, along with the constexpr color conversion and interpolation functions etc.

Thanks also for looking at impop, Some of it is pretty application-specific but bits and pieces could probably be merged up eventually. The app config is fairly generic but uses imgui internals; similarly the canvas can probably be replaced with the work rect and the datepicker (which is just a wrapper around ImPlot::ShowDatePicker()) could be useful to expose in implot at some point.

Anyway, happy to close this PR as is.

@kfish kfish closed this Jun 25, 2024
@ocornut
Copy link
Owner

ocornut commented Jun 25, 2024

I don’t mind merging some of this if helpful, unsure why closing? My question was mostly aiming to understand which functions you needed.

@kfish
Copy link
Author

kfish commented Jun 25, 2024

Ok no problem, I'll re-open. No rush, happy to help clean up whichever bits you find useful

@kfish kfish reopened this Jun 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants