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 HandleMathClamp #1160

Merged
merged 6 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
59 changes: 27 additions & 32 deletions src/Neo.Compiler.CSharp/MethodConvert/System/SystemCall.Math.cs
Original file line number Diff line number Diff line change
Expand Up @@ -306,41 +306,36 @@ private static void HandleMathClamp(MethodConvert methodConvert, SemanticModel m
if (arguments is not null)
methodConvert.PrepareArgumentsForMethod(model, symbol, arguments, CallingConvention.StdCall);

var endTarget = new JumpTarget();
var exceptionTarget = new JumpTarget();
var minTarget = new JumpTarget();
var maxTarget = new JumpTarget();
methodConvert.AddInstruction(OpCode.DUP);// 5 0 10 10
methodConvert.AddInstruction(OpCode.ROT);// 5 10 10 0
methodConvert.AddInstruction(OpCode.DUP);// 5 10 10 0 0
methodConvert.AddInstruction(OpCode.ROT);// 5 10 0 0 10
methodConvert.Jump(OpCode.JMPLT, exceptionTarget);// 5 10 0
// Evaluation stack: value=5 min=0 max=10 <- top
methodConvert.AddInstruction(OpCode.OVER); // 5 0 10 0
methodConvert.AddInstruction(OpCode.OVER); // 5 0 10 0 10 <- top
methodConvert.Jump(OpCode.JMPLE, exceptionTarget); // 5 0 10 // if 0 <= 10, continue execution
//methodConvert.Push("min>max");
methodConvert.AddInstruction(OpCode.THROW);
shargon marked this conversation as resolved.
Show resolved Hide resolved
exceptionTarget.Instruction = methodConvert.AddInstruction(OpCode.NOP);
methodConvert.AddInstruction(OpCode.ROT);// 10 0 5
methodConvert.AddInstruction(OpCode.DUP);// 10 0 5 5
methodConvert.AddInstruction(OpCode.ROT);// 10 5 5 0
methodConvert.AddInstruction(OpCode.DUP);// 10 5 5 0 0
methodConvert.AddInstruction(OpCode.ROT);// 10 5 0 0 5
methodConvert.Jump(OpCode.JMPGT, minTarget);// 10 5 0
methodConvert.AddInstruction(OpCode.DROP);// 10 5
methodConvert.AddInstruction(OpCode.DUP);// 10 5 5
methodConvert.AddInstruction(OpCode.ROT);// 5 5 10
methodConvert.AddInstruction(OpCode.DUP);// 5 5 10 10
methodConvert.AddInstruction(OpCode.ROT);// 5 10 10 5
methodConvert.Jump(OpCode.JMPLT, maxTarget);// 5 10
methodConvert.AddInstruction(OpCode.DROP);
methodConvert.Jump(OpCode.JMP, endTarget);
minTarget.Instruction = methodConvert.AddInstruction(OpCode.NOP);
methodConvert.AddInstruction(OpCode.REVERSE3);
methodConvert.AddInstruction(OpCode.DROP);
methodConvert.AddInstruction(OpCode.DROP);
methodConvert.Jump(OpCode.JMP, endTarget);
maxTarget.Instruction = methodConvert.AddInstruction(OpCode.NOP);
methodConvert.AddInstruction(OpCode.SWAP);
methodConvert.AddInstruction(OpCode.DROP);
methodConvert.Jump(OpCode.JMP, endTarget);
endTarget.Instruction = methodConvert.AddInstruction(OpCode.NOP);
methodConvert.AddInstruction(OpCode.REVERSE3); // 10 0 5
Copy link
Contributor Author

@Hecate2 Hecate2 Sep 6, 2024

Choose a reason for hiding this comment

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

//We may not use MAX and MIN after REVERSE3 because it's more expensive. MAX&MIN costs 1<<3 each. See the commented codes for a cheaper but larger solution.

// MAX&MIN costs 1<<3 each; 16 Datoshi more expensive at runtime
methodConvert.AddInstruction(OpCode.MAX); // 10 5
methodConvert.AddInstruction(OpCode.MIN); // 5
//methodConvert.AddInstruction(OpCode.RET);
// Alternatively, a slightly cheaper way at runtime; 10 to 16 Datoshi
//methodConvert.AddInstruction(OpCode.OVER); // 10 0 5 0
//methodConvert.AddInstruction(OpCode.OVER); // 10 0 5 0 5
//methodConvert.Jump(OpCode.JMPGE, minTarget); // 10 0 5; should return 0 if JMPed
//methodConvert.AddInstruction(OpCode.NIP); // 10 5
//methodConvert.AddInstruction(OpCode.OVER); // 10 5 10
//methodConvert.AddInstruction(OpCode.OVER); // 10 5 10 5
//methodConvert.Jump(OpCode.JMPLE, maxTarget); // 10 5; should return 10 if JMPed
//methodConvert.AddInstruction(OpCode.NIP); // 5; should return 5
//methodConvert.AddInstruction(OpCode.RET);
//minTarget.Instruction = methodConvert.AddInstruction(OpCode.NOP); // 10 0 5; should return 0
//methodConvert.AddInstruction(OpCode.DROP); // 10 0; should return 0
//methodConvert.AddInstruction(OpCode.NIP); // 0; should return 0
//methodConvert.AddInstruction(OpCode.RET);
//maxTarget.Instruction = methodConvert.AddInstruction(OpCode.NOP); // 10 5; should return 10
//methodConvert.AddInstruction(OpCode.DROP); // 10; should return 10
//methodConvert.AddInstruction(OpCode.RET);
}

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