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

optimize DivRem #1173

Merged
merged 3 commits into from
Sep 23, 2024
Merged

optimize DivRem #1173

merged 3 commits into from
Sep 23, 2024

Conversation

Hecate2
Copy link
Contributor

@Hecate2 Hecate2 commented Sep 19, 2024

Using MOD instead of l-l/r*r
Original implementation check and throw when the remainder overflows. However it is impossible for the remainder to overflow, because

if MinValue < 0
  abs(remainder) < abs(MinValue)
if MinValue == 0
  0 < remainder < MaxValue

Therefore I cancelled the check.
But it is possible for the quotient to overflow in a single case, when the allowed range is [-128, 127] and I call DivRem(-128, -1). I did not check and throw in this case, because it is rare and expensive, and the original implementation did not check it.

@Jim8y
Copy link
Contributor

Jim8y commented Sep 20, 2024

@Hecate2 please fix conflict

methodConvert.Push(2);
methodConvert.AddInstruction(OpCode.PICK);// r, l, l, r
methodConvert.AddInstruction(OpCode.DIV); // r, l, l/r
// TODO: for types that is restricted by range, check l/r <= MaxValue
Copy link
Contributor

Choose a reason for hiding this comment

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

Are you going to work on this todo?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No. I think we can just allow overflow.

Copy link
Contributor

Choose a reason for hiding this comment

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

Will this be the same as C# behavior?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, but only when DivRem(int.MinValue, -1)

Copy link
Contributor

Choose a reason for hiding this comment

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

then can you please explain this as comment where others can learn.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

then can you please explain this as comment where others can learn.

Done

…to fix-divRem

# Conflicts:
#	tests/Neo.Compiler.CSharp.UnitTests/TestingArtifacts/Contract_Integer.cs
@Jim8y Jim8y merged commit 9747093 into neo-project:master Sep 23, 2024
3 of 4 checks passed
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