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

<ratio>: Use constexpr functions instead of TMPs #2450

Merged
merged 4 commits into from
Jan 26, 2022
Merged

<ratio>: Use constexpr functions instead of TMPs #2450

merged 4 commits into from
Jan 26, 2022

Conversation

sam20908
Copy link
Contributor

@sam20908 sam20908 commented Dec 30, 2021

Fixes #2291

Blocked on https://developercommunity.visualstudio.com/t/1202945

Why not make _Gcd a copy of std::gcd?

std::gcd includes large headers such as <xutility> and <limits>. I did not dare try to make a new header (<xratio>?) because I was not 100% sure if I can do it correctly.
The callers of _Gcd only work with intmax_t, so I figured it is easier to just update _Gcd and its callers rather than worrying about making a new header.

@sam20908 sam20908 requested a review from a team as a code owner December 30, 2021 18:52
@CaseyCarter CaseyCarter added the enhancement Something can be improved label Dec 30, 2021
Copy link
Contributor Author

@sam20908 sam20908 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The final stretch...

Copy link
Contributor

@miscco miscco left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some small nits

@sam20908
Copy link
Contributor Author

Regarding the aborts, I want to figure out what's causing the test failures, in case I need to make an entirely different approach.

@sam20908
Copy link
Contributor Author

sam20908 commented Jan 1, 2022

Closing this PR because the use of TMPs was to implement LWG-2094, which makes overflow a SFINAE error instead of emitting diagnostics. There is no way to achieve the same thing with constexpr functions.

@sam20908 sam20908 closed this Jan 1, 2022
@sam20908 sam20908 deleted the constexpr_funcs_instead_of_tmp branch January 1, 2022 15:55
@sam20908 sam20908 restored the constexpr_funcs_instead_of_tmp branch January 1, 2022 16:31
@sam20908
Copy link
Contributor Author

sam20908 commented Jan 1, 2022

After yet another discussion on Discord, this is valid, but it is blocked by the compiler not rejecting overflow in constexpr.
https://developercommunity.visualstudio.com/t/1202945

@sam20908 sam20908 reopened this Jan 1, 2022
@CaseyCarter CaseyCarter self-assigned this Jan 19, 2022
Since it is not required to be used for SFINAE, it can be made into a function, and a non-constexpr function can be called when overflow errors, triggering compiler error.
@CaseyCarter CaseyCarter self-requested a review January 23, 2022 09:02
@CaseyCarter CaseyCarter removed their assignment Jan 23, 2022

template <intmax_t _Ax, intmax_t _Bx, bool _Good, bool _Also_good>
struct _Safe_addX : integral_constant<intmax_t, _Ax + _Bx> {}; // computes _Ax + _Bx without overflow
inline void _Safe_add_integer_arithmetic_overflow_error() noexcept {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe comment as intentionally empty and not constexpr?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe call abort() to make is safe for runtime too?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually never mind, as it is ready to merge.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same pattern as:

inline void _You_see_this_error_because_arg_id_is_out_of_range() noexcept {}

I'd be fine with commenting this pattern with // intentionally not constexpr in the future, although I agree that it isn't worth resetting testing here. However, calling abort() seems excessive, and then I'd worry about the compiler actually emitting codegen for functions that aren't going to be called.

@StephanTLavavej StephanTLavavej self-assigned this Jan 26, 2022
@StephanTLavavej
Copy link
Member

I'm mirroring this to the MSVC-internal repo - please notify me if any further changes are pushed.

@StephanTLavavej StephanTLavavej merged commit f575e7b into microsoft:main Jan 26, 2022
@StephanTLavavej
Copy link
Member

Thanks for using modern constexpr to simplify this code! 🚀 😸 🎉

@sam20908 sam20908 deleted the constexpr_funcs_instead_of_tmp branch March 5, 2022 01:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Something can be improved
Projects
None yet
Development

Successfully merging this pull request may close these issues.

<ratio>: use constexpr functions instead of template-based calculation
5 participants