I’m actually currently working on changing all the functions defined in math.h to use the builtins unconditionally. libc++ only supports gcc and clang and both implement builtins for all the math.h functions. The only things in the library missing to make them constexpr is to declare them constexpr. Clang would have to make the C declarations constexpr, since libc++ doesn’t have any control over them and make the builtins constexpr. See ⚙ D134584 [libc++] Rewrite <cmath> for how it should look in the end. I’m splitting it up into multiple patches according to @ldionne’s comment.
The only things in the library missing to make them constexpr is to declare them constexpr
It is only if the builtin has the constexpr realization. Not all builtins support being “constant evaluated” now.
For example I supported constexpr __builtin_fmax recently: rG0edff6faa266, therefore we now can declare fmax constexpr (after your patch), but every other function needs to be reviewed separately.
(A constexpr builtin is a function that is evaluated right in Clang frontend, as you can see in the patch)
Right, these aren’t templates, so clang actually diagnoses when a functions isn’t constexpr. I guess we have to add some mechanism to conditionally make the functions constexpr until all supported compilers have full support. I think GCC already has most of them constexpr. Would it be possible to add a preprocessor check whether a builtin can be constant evaluated, similar to __has_builtin(...)? Maybe __is_builtin_constexpr(...)? That will probably come in handy for other things too.
Nice idea! With __is_builtin_constexpr (or __has_constexpr_builtin) the contributors will not need to do the 3rd step
Contributors declare functions in cmath/math.h as constexpr.
because we could use a macro that conditionally declares the function as constexpr like _LIBCPP_CONSTEXPR_CXX23_IF_BUILTIN_CONSTEXPR(__builtin_fmax)
I’ll research how to add __is_builtin_constexpr (currently we don’t have a register for constexpr builtins).
Also with this preprocessor check we could add a test to see which builtins are not yet constexpr, but have to be constexpr to complete support for p0533r9.
I don’t think we need to have that discussion now, as most of the hard floating point functions were left out by WG21, but in the long term i noticed that, the folks writing llvm libc decided to make all their cmath function correctly rounded.
My hope is that we could reuse their work inside of clang to provide the constexpr implementation of trigonometrics etc builtins. what do you think?
It would make clang more consistent with gcc which already has this builtin constexpr, and making them correctly rounded is the only realistic solution as
behaving like the target is not really possible or realistic
behaving like the host (as Hubert points out) is also undesirable
Thanks! I should have named the topic as How do we plan to implement P0533R9 in Clang. I meant not the whole <cmath> and <cstdlib> but the part of these that are mentioned in the paper =)
Submitted a patch request that introduces a test that indicates progress on this paper (checks exactly same functions that are in the paper) https://reviews.llvm.org/D136538#3877255
Now we have a whole workflow connecting ExprConstant.cpp, Builtins.def, __has_constexpr_builtin, the new test, and conditionally constexpr functions in libcxx after @philnik refactors cmath.