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

ENH: Add overflow handling for negative integers scalar multiplication #21793

Merged
merged 10 commits into from Jul 17, 2022

Conversation

Micky774
Copy link
Contributor

@Micky774 Micky774 commented Jun 18, 2022

Related Issues/PRs

Related to #21506

Follow-up to #21648

Changes

Adds checks for multiplication overflow for signed integers, in particular when they are negative.

Comments

N/A

@Micky774 Micky774 changed the title Mult overflow ENH: Add overflow handling for negative integers scalar multiplication Jun 18, 2022
@seberg
Copy link
Member

seberg commented Jun 19, 2022

I am wondering slightly whether we should care that npy_mul_with_overflow_intp is currently almost only used in places where we really don't care about negative values, because its main purpose is/was for shape calculations.
Likely, it just doesn't matter, though, and this is good.

@Micky774
Copy link
Contributor Author

I am wondering slightly whether we should care that npy_mul_with_overflow_intp is currently almost only used in places where we really don't care about negative values, because its main purpose is/was for shape calculations. Likely, it just doesn't matter, though, and this is good.

I currently have it treated as an "unsigned" value for that exact reason. It doesn't break any tests so hopefully it isn't problematic.

@seberg
Copy link
Member

seberg commented Jun 29, 2022

Ah, I had missed that. That works, since intp is only an alias (so it doesn't exist as a "scalar"). However, I it also seems confusing to use the same name/pattern.

I don't have a super good idea right away though, one would be to rename the intp version to npy_mul_sizes_with_overflow (and maybe add assert(a >= 0 && b >= 0). The use in scalarmath.c is already an oversight due to the >= 0 constraint, and that was added by Julian, who wrote the original I think :).

@seberg
Copy link
Member

seberg commented Jul 7, 2022

@Micky774 did you prefer keeping it as is, or rather introduce a new name for the intp version? I am slightly leaning to that right now. (even if it is a bit annoying code wise.)

@Micky774
Copy link
Contributor Author

Micky774 commented Jul 7, 2022

@Micky774 did you prefer keeping it as is, or rather introduce a new name for the intp version? I am slightly leaning to that right now. (even if it is a bit annoying code wise.)

I can adopt your suggestion and write a separate function for it. Apologies for the delays, I usually work on numpy stuff towards the end of the week :)

@Micky774
Copy link
Contributor Author

@seberg I think this is ready now, there's just a problem w/ Travis CI

@seberg
Copy link
Member

seberg commented Jul 17, 2022

Agreed, thanks @Micky774!

@seberg seberg merged commit 4156ae2 into numpy:main Jul 17, 2022
@Micky774 Micky774 deleted the mult_overflow branch July 17, 2022 18:53
rgommers added a commit to rgommers/numpy that referenced this pull request Nov 12, 2022
assert() only takes one argument.
This was recently introduced, in commit 4156ae2 (numpygh-21793)
rgommers added a commit that referenced this pull request Nov 12, 2022
assert() only takes one argument.
This was recently introduced, in commit 4156ae2 (gh-21793)

(cherry picked from commit 3a6d290)
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

2 participants