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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -488,21 +488,23 @@ private static void HandleMathBigIntegerDivRem(MethodConvert methodConvert, Sema
methodConvert.ConvertExpression(model, instanceExpression);
if (arguments is not null)
methodConvert.PrepareArgumentsForMethod(model, symbol, arguments);
// r, l -> l/r, l%r
// Perform division
methodConvert.AddInstruction(OpCode.DUP);
methodConvert.AddInstruction(OpCode.ROT);
methodConvert.AddInstruction(OpCode.TUCK);
methodConvert.AddInstruction(OpCode.DIV);
methodConvert.AddInstruction(OpCode.DUP); // r, l, l
methodConvert.Push(2);
methodConvert.AddInstruction(OpCode.PICK);// r, l, l, r
methodConvert.AddInstruction(OpCode.DIV); // r, l, l/r
// For types that is restricted by range, there should be l/r <= MaxValue
// However it's only possible to get l/r == MaxValue + 1 when l/r > MaxValue
// and it's impossible to get l/r < MinValue
// Therefore we ignore this case; l/r <= MaxValue is not checked

// Calculate remainder
methodConvert.AddInstruction(OpCode.DUP);
methodConvert.AddInstruction(OpCode.ROT);
methodConvert.AddInstruction(OpCode.MUL);
methodConvert.AddInstruction(OpCode.ROT);
methodConvert.AddInstruction(OpCode.SWAP);
methodConvert.AddInstruction(OpCode.SUB);
methodConvert.AddInstruction(OpCode.REVERSE3); // l/r, l, r
methodConvert.AddInstruction(OpCode.MOD); // l/r, l%r
methodConvert.AddInstruction(OpCode.PUSH2);
methodConvert.AddInstruction(OpCode.PACK);
// It's impossible to get l%r out of range
}

//implement HandleBigIntegerLeadingZeroCount
Expand Down
224 changes: 8 additions & 216 deletions src/Neo.Compiler.CSharp/MethodConvert/System/SystemCall.Math.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,250 +53,42 @@ private static void HandleMathMin(MethodConvert methodConvert, SemanticModel mod

private static void HandleMathByteDivRem(MethodConvert methodConvert, SemanticModel model, IMethodSymbol symbol, ExpressionSyntax? instanceExpression, IReadOnlyList<SyntaxNode>? arguments)
{
if (instanceExpression is not null)
methodConvert.ConvertExpression(model, instanceExpression);
if (arguments is not null)
methodConvert.PrepareArgumentsForMethod(model, symbol, arguments);
JumpTarget endTarget = new();
// Perform division
methodConvert.AddInstruction(OpCode.DUP);
methodConvert.AddInstruction(OpCode.ROT);
methodConvert.AddInstruction(OpCode.TUCK);
methodConvert.AddInstruction(OpCode.DIV);

// Calculate remainder
methodConvert.AddInstruction(OpCode.DUP);
methodConvert.AddInstruction(OpCode.ROT);
methodConvert.AddInstruction(OpCode.MUL);
methodConvert.AddInstruction(OpCode.ROT);
methodConvert.AddInstruction(OpCode.SWAP);
methodConvert.AddInstruction(OpCode.SUB);
methodConvert.AddInstruction(OpCode.DUP);
methodConvert.Push(byte.MinValue);
methodConvert.Push(new BigInteger(byte.MaxValue) + 1);
methodConvert.AddInstruction(OpCode.WITHIN);
methodConvert.Jump(OpCode.JMPIF, endTarget);
methodConvert.AddInstruction(OpCode.THROW);
endTarget.Instruction = methodConvert.AddInstruction(OpCode.NOP);
methodConvert.AddInstruction(OpCode.PUSH2);
methodConvert.AddInstruction(OpCode.PACK);
HandleMathBigIntegerDivRem(methodConvert, model, symbol, instanceExpression, arguments);
}

private static void HandleMathSByteDivRem(MethodConvert methodConvert, SemanticModel model, IMethodSymbol symbol, ExpressionSyntax? instanceExpression, IReadOnlyList<SyntaxNode>? arguments)
{
if (instanceExpression is not null)
methodConvert.ConvertExpression(model, instanceExpression);
if (arguments is not null)
methodConvert.PrepareArgumentsForMethod(model, symbol, arguments);
JumpTarget endTarget = new();
// Perform division
methodConvert.AddInstruction(OpCode.DUP);
methodConvert.AddInstruction(OpCode.ROT);
methodConvert.AddInstruction(OpCode.TUCK);
methodConvert.AddInstruction(OpCode.DIV);

// Calculate remainder
methodConvert.AddInstruction(OpCode.DUP);
methodConvert.AddInstruction(OpCode.ROT);
methodConvert.AddInstruction(OpCode.MUL);
methodConvert.AddInstruction(OpCode.ROT);
methodConvert.AddInstruction(OpCode.SWAP);
methodConvert.AddInstruction(OpCode.SUB);
methodConvert.AddInstruction(OpCode.DUP);
methodConvert.Push(sbyte.MinValue);
methodConvert.Push(new BigInteger(sbyte.MaxValue) + 1);
methodConvert.AddInstruction(OpCode.WITHIN);
methodConvert.Jump(OpCode.JMPIF, endTarget);
methodConvert.AddInstruction(OpCode.THROW);
endTarget.Instruction = methodConvert.AddInstruction(OpCode.NOP);
methodConvert.AddInstruction(OpCode.PUSH2);
methodConvert.AddInstruction(OpCode.PACK);
HandleMathBigIntegerDivRem(methodConvert, model, symbol, instanceExpression, arguments);
}

private static void HandleMathShortDivRem(MethodConvert methodConvert, SemanticModel model, IMethodSymbol symbol, ExpressionSyntax? instanceExpression, IReadOnlyList<SyntaxNode>? arguments)
{
if (instanceExpression is not null)
methodConvert.ConvertExpression(model, instanceExpression);
if (arguments is not null)
methodConvert.PrepareArgumentsForMethod(model, symbol, arguments);
JumpTarget endTarget = new();
// Perform division
methodConvert.AddInstruction(OpCode.DUP);
methodConvert.AddInstruction(OpCode.ROT);
methodConvert.AddInstruction(OpCode.TUCK);
methodConvert.AddInstruction(OpCode.DIV);

// Calculate remainder
methodConvert.AddInstruction(OpCode.DUP);
methodConvert.AddInstruction(OpCode.ROT);
methodConvert.AddInstruction(OpCode.MUL);
methodConvert.AddInstruction(OpCode.ROT);
methodConvert.AddInstruction(OpCode.SWAP);
methodConvert.AddInstruction(OpCode.SUB);
methodConvert.AddInstruction(OpCode.DUP);
methodConvert.Push(short.MinValue);
methodConvert.Push(new BigInteger(short.MaxValue) + 1);
methodConvert.AddInstruction(OpCode.WITHIN);
methodConvert.Jump(OpCode.JMPIF, endTarget);
methodConvert.AddInstruction(OpCode.THROW);
endTarget.Instruction = methodConvert.AddInstruction(OpCode.NOP);
methodConvert.AddInstruction(OpCode.PUSH2);
methodConvert.AddInstruction(OpCode.PACK);
HandleMathBigIntegerDivRem(methodConvert, model, symbol, instanceExpression, arguments);
}

private static void HandleMathUShortDivRem(MethodConvert methodConvert, SemanticModel model, IMethodSymbol symbol, ExpressionSyntax? instanceExpression, IReadOnlyList<SyntaxNode>? arguments)
{
if (instanceExpression is not null)
methodConvert.ConvertExpression(model, instanceExpression);
if (arguments is not null)
methodConvert.PrepareArgumentsForMethod(model, symbol, arguments);
JumpTarget endTarget = new();
// Perform division
methodConvert.AddInstruction(OpCode.DUP);
methodConvert.AddInstruction(OpCode.ROT);
methodConvert.AddInstruction(OpCode.TUCK);
methodConvert.AddInstruction(OpCode.DIV);

// Calculate remainder
methodConvert.AddInstruction(OpCode.DUP);
methodConvert.AddInstruction(OpCode.ROT);
methodConvert.AddInstruction(OpCode.MUL);
methodConvert.AddInstruction(OpCode.ROT);
methodConvert.AddInstruction(OpCode.SWAP);
methodConvert.AddInstruction(OpCode.SUB);
methodConvert.AddInstruction(OpCode.DUP);
methodConvert.Push(ushort.MinValue);
methodConvert.Push(new BigInteger(ushort.MaxValue) + 1);
methodConvert.AddInstruction(OpCode.WITHIN);
methodConvert.Jump(OpCode.JMPIF, endTarget);
methodConvert.AddInstruction(OpCode.THROW);
endTarget.Instruction = methodConvert.AddInstruction(OpCode.NOP);
methodConvert.AddInstruction(OpCode.PUSH2);
methodConvert.AddInstruction(OpCode.PACK);
HandleMathBigIntegerDivRem(methodConvert, model, symbol, instanceExpression, arguments);
}

private static void HandleMathIntDivRem(MethodConvert methodConvert, SemanticModel model, IMethodSymbol symbol, ExpressionSyntax? instanceExpression, IReadOnlyList<SyntaxNode>? arguments)
{
if (instanceExpression is not null)
methodConvert.ConvertExpression(model, instanceExpression);
if (arguments is not null)
methodConvert.PrepareArgumentsForMethod(model, symbol, arguments);
JumpTarget endTarget = new();
// Perform division
methodConvert.AddInstruction(OpCode.DUP);
methodConvert.AddInstruction(OpCode.ROT);
methodConvert.AddInstruction(OpCode.TUCK);
methodConvert.AddInstruction(OpCode.DIV);

// Calculate remainder
methodConvert.AddInstruction(OpCode.DUP);
methodConvert.AddInstruction(OpCode.ROT);
methodConvert.AddInstruction(OpCode.MUL);
methodConvert.AddInstruction(OpCode.ROT);
methodConvert.AddInstruction(OpCode.SWAP);
methodConvert.AddInstruction(OpCode.SUB);
methodConvert.AddInstruction(OpCode.DUP);
methodConvert.Push(int.MinValue);
methodConvert.Push(new BigInteger(int.MaxValue) + 1);
methodConvert.AddInstruction(OpCode.WITHIN);
methodConvert.Jump(OpCode.JMPIF, endTarget);
methodConvert.AddInstruction(OpCode.THROW);
endTarget.Instruction = methodConvert.AddInstruction(OpCode.NOP);
methodConvert.AddInstruction(OpCode.PUSH2);
methodConvert.AddInstruction(OpCode.PACK);
HandleMathBigIntegerDivRem(methodConvert, model, symbol, instanceExpression, arguments);
}

private static void HandleMathUIntDivRem(MethodConvert methodConvert, SemanticModel model, IMethodSymbol symbol, ExpressionSyntax? instanceExpression, IReadOnlyList<SyntaxNode>? arguments)
{
if (instanceExpression is not null)
methodConvert.ConvertExpression(model, instanceExpression);
if (arguments is not null)
methodConvert.PrepareArgumentsForMethod(model, symbol, arguments);
JumpTarget endTarget = new();
// Perform division
methodConvert.AddInstruction(OpCode.DUP);
methodConvert.AddInstruction(OpCode.ROT);
methodConvert.AddInstruction(OpCode.TUCK);
methodConvert.AddInstruction(OpCode.DIV);

// Calculate remainder
methodConvert.AddInstruction(OpCode.DUP);
methodConvert.AddInstruction(OpCode.ROT);
methodConvert.AddInstruction(OpCode.MUL);
methodConvert.AddInstruction(OpCode.ROT);
methodConvert.AddInstruction(OpCode.SWAP);
methodConvert.AddInstruction(OpCode.SUB);
methodConvert.AddInstruction(OpCode.DUP);
methodConvert.Push(uint.MinValue);
methodConvert.Push(new BigInteger(uint.MaxValue) + 1);
methodConvert.AddInstruction(OpCode.WITHIN);
methodConvert.Jump(OpCode.JMPIF, endTarget);
methodConvert.AddInstruction(OpCode.THROW);
endTarget.Instruction = methodConvert.AddInstruction(OpCode.NOP);
methodConvert.AddInstruction(OpCode.PUSH2);
methodConvert.AddInstruction(OpCode.PACK);
HandleMathBigIntegerDivRem(methodConvert, model, symbol, instanceExpression, arguments);
}

private static void HandleMathLongDivRem(MethodConvert methodConvert, SemanticModel model, IMethodSymbol symbol, ExpressionSyntax? instanceExpression, IReadOnlyList<SyntaxNode>? arguments)
{
if (instanceExpression is not null)
methodConvert.ConvertExpression(model, instanceExpression);
if (arguments is not null)
methodConvert.PrepareArgumentsForMethod(model, symbol, arguments);
JumpTarget endTarget = new();
// Perform division
methodConvert.AddInstruction(OpCode.DUP);
methodConvert.AddInstruction(OpCode.ROT);
methodConvert.AddInstruction(OpCode.TUCK);
methodConvert.AddInstruction(OpCode.DIV);

// Calculate remainder
methodConvert.AddInstruction(OpCode.DUP);
methodConvert.AddInstruction(OpCode.ROT);
methodConvert.AddInstruction(OpCode.MUL);
methodConvert.AddInstruction(OpCode.ROT);
methodConvert.AddInstruction(OpCode.SWAP);
methodConvert.AddInstruction(OpCode.SUB);
methodConvert.AddInstruction(OpCode.DUP);
methodConvert.Push(long.MinValue);
methodConvert.Push(new BigInteger(long.MaxValue) + 1);
methodConvert.AddInstruction(OpCode.WITHIN);
methodConvert.Jump(OpCode.JMPIF, endTarget);
methodConvert.AddInstruction(OpCode.THROW);
endTarget.Instruction = methodConvert.AddInstruction(OpCode.NOP);
methodConvert.AddInstruction(OpCode.PUSH2);
methodConvert.AddInstruction(OpCode.PACK);
HandleMathBigIntegerDivRem(methodConvert, model, symbol, instanceExpression, arguments);
}

private static void HandleMathULongDivRem(MethodConvert methodConvert, SemanticModel model, IMethodSymbol symbol, ExpressionSyntax? instanceExpression, IReadOnlyList<SyntaxNode>? arguments)
{
if (instanceExpression is not null)
methodConvert.ConvertExpression(model, instanceExpression);
if (arguments is not null)
methodConvert.PrepareArgumentsForMethod(model, symbol, arguments);
JumpTarget endTarget = new();
// Perform division
methodConvert.AddInstruction(OpCode.DUP);
methodConvert.AddInstruction(OpCode.ROT);
methodConvert.AddInstruction(OpCode.TUCK);
methodConvert.AddInstruction(OpCode.DIV);

// Calculate remainder
methodConvert.AddInstruction(OpCode.DUP);
methodConvert.AddInstruction(OpCode.ROT);
methodConvert.AddInstruction(OpCode.MUL);
methodConvert.AddInstruction(OpCode.ROT);
methodConvert.AddInstruction(OpCode.SWAP);
methodConvert.AddInstruction(OpCode.SUB);
methodConvert.AddInstruction(OpCode.DUP);
methodConvert.Push(ulong.MinValue);
methodConvert.Push(new BigInteger(ulong.MaxValue) + 1);
methodConvert.AddInstruction(OpCode.WITHIN);
methodConvert.Jump(OpCode.JMPIF, endTarget);
methodConvert.AddInstruction(OpCode.THROW);
endTarget.Instruction = methodConvert.AddInstruction(OpCode.NOP);
methodConvert.AddInstruction(OpCode.PUSH2);
methodConvert.AddInstruction(OpCode.PACK);
HandleMathBigIntegerDivRem(methodConvert, model, symbol, instanceExpression, arguments);
}

private static void HandleMathClamp(MethodConvert methodConvert, SemanticModel model, IMethodSymbol symbol, ExpressionSyntax? instanceExpression, IReadOnlyList<SyntaxNode>? arguments)
Expand Down
Loading
Loading