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

FIX: avoid overflow on overflow check in halley_step on Apple M1 #937

Closed

Conversation

mckib2
Copy link
Contributor

@mckib2 mckib2 commented Jan 31, 2023

@jzmaddock
Copy link
Collaborator

I'm sure I'm missing something obvious here, but if denom >=1 there can be no overflow, and if its < 1 then denom*numeric_limits<>::max() can't overflow either? Or is a logical branch which should never be taken being speculatively evaluated?

@mckib2
Copy link
Contributor Author

mckib2 commented Jan 31, 2023

Or is a logical branch which should never be taken being speculatively evaluated?

That's what I thought at first because we have seen that before on Apple Silicon, but splitting the expression out into two if statements (which resolved this previously) didn't resolve it locally for me and I don't know of another way to prevent the speculative execution. So if it might be executed, the change in this PR was the only thing I could find that avoided the FE_OVERFLOW on an M1 Mac Mini

// |denom| >= |num| * max_value
// RHS may overflow on Apple M1, so rearrange:
// |denom| * 1/max_value >= |num|
constexpr T inv_max_value = 1.0 / tools::max_value<T>();
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
constexpr T inv_max_value = 1.0 / tools::max_value<T>();
const T inv_max_value = 1.0 / tools::max_value<T>();

Copy link
Collaborator

Choose a reason for hiding this comment

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

Even better make that static const and then it's evaluated just the once.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wanted static constexpr, but wasn't sure that is supported everywhere we want, and indeed, it appears even constexpr is problematic. Will implement this right away

@mckib2
Copy link
Contributor Author

mckib2 commented Feb 5, 2023

Superceded by #945

@mckib2 mckib2 closed this Feb 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants