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
MAINT, SIMD: Handle overflow gracefully in integer division #21727
Conversation
Hey @seberg, thought of going bottom up on this one. This solves the SIMD overflows, once merged, I'll fix the scalars and arrays for other ops along with the tests. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, happy with doing this one-by-one. I am slightly wondering if/how we should backport it, but maybe that is better seen later.
LGTM modulo the test fixup. Will wait a bit in case someone more familiar with SIMD wants to have a look.
numpy/core/tests/test_umath.py
Outdated
else: | ||
a // b | ||
if fo.min and fo.min in a: | ||
with pytest.raises(FloatingPointError): | ||
with pytest.raises(RuntimeWarning): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than changing this, change the np.errstate
to include overflow='raise'
. (This is actually necessary, the warning will not be raised in release mode.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ohh ok, thanks!
if (d == 0) { | ||
npy_set_floatstatus_divbyzero(); | ||
return 0; | ||
} else if ((n == NPY_MIN_@TYPE@) && (d == -1)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
} else if ((n == NPY_MIN_@TYPE@) && (d == -1)) { | |
} | |
else { |
Maybe its clear and we don't have to repeat it? (otherwise moving the else
would be nice).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or maybe chain it like above? In any case, feel free to ignore, the newline comment if this is used more in this file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah you are right, shouldn't code after 12 AM :)
At this point I don't want to do anymore backports except straight bug fixes. |
Yeah, agreed. It may make sense to do a very minimal bug-fix that just solves the crashes. But maybe lets figure out this stuff here first and then do that? |
d34a65f
to
fe3df63
Compare
* If a types minimum value is divided by -1, an overflow will be raised and result will be set to minimum
fe3df63
to
fccb91f
Compare
npyv_@sfx@ cvtozero = npyv_select_@sfx@(error, vzero, vneg_one); | ||
warn = npyv_or_@sfx@(error, warn); | ||
warn_zero = npyv_or_@sfx@(bzero, warn_zero); | ||
warn_overflow = npyv_or_@sfx@(overflow, warn_overflow); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I hope VSX is covered as part of the CI. Else would be nice to get @seiko2plus opinion on this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, LGTM now. Will give it a bit time in case of SIMD related review.
The test failure can be ignored. |
Co-authored-by: Rafael Sousa <90851201+rafaelcfsousa@users.noreply.github.com>
Handle SIMD division overflow
Merge before: #21507
Part of: #21506
Raising this separately to not blow up the original PR on scalars.
Changes:
Before:
Now: