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

Add: add more Biginteger methods #840

Merged
merged 10 commits into from
Dec 19, 2023
57 changes: 57 additions & 0 deletions src/Neo.Compiler.CSharp/MethodConvert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4199,6 +4199,63 @@ private bool TryProcessSystemMethods(SemanticModel model, IMethodSymbol symbol,
PrepareArgumentsForMethod(model, symbol, arguments, CallingConvention.StdCall);
AddInstruction(OpCode.MODPOW);
return true;
case "System.Numerics.BigInteger.Add(System.Numerics.BigInteger, System.Numerics.BigInteger)":
if (arguments is not null)
PrepareArgumentsForMethod(model, symbol, arguments, CallingConvention.StdCall);
AddInstruction(OpCode.ADD);
return true;
case "System.Numerics.BigInteger.Subtract(System.Numerics.BigInteger, System.Numerics.BigInteger)":
if (arguments is not null)
PrepareArgumentsForMethod(model, symbol, arguments, CallingConvention.StdCall);
AddInstruction(OpCode.SUB);
return true;
case "System.Numerics.BigInteger.Negate(System.Numerics.BigInteger)":
if (arguments is not null)
PrepareArgumentsForMethod(model, symbol, arguments, CallingConvention.StdCall);
AddInstruction(OpCode.NEGATE);
return true;
case "System.Numerics.BigInteger.Multiply(System.Numerics.BigInteger, System.Numerics.BigInteger)":
if (arguments is not null)
PrepareArgumentsForMethod(model, symbol, arguments, CallingConvention.StdCall);
AddInstruction(OpCode.MUL);
return true;
case "System.Numerics.BigInteger.Divide(System.Numerics.BigInteger, System.Numerics.BigInteger)":
if (arguments is not null)
PrepareArgumentsForMethod(model, symbol, arguments, CallingConvention.StdCall);
AddInstruction(OpCode.DIV);
return true;
case "System.Numerics.BigInteger.Remainder(System.Numerics.BigInteger, System.Numerics.BigInteger)":
if (arguments is not null)
PrepareArgumentsForMethod(model, symbol, arguments, CallingConvention.StdCall);
AddInstruction(OpCode.MOD);
return true;
case "System.Numerics.BigInteger.Compare(System.Numerics.BigInteger, System.Numerics.BigInteger)":
if (arguments is not null)
PrepareArgumentsForMethod(model, symbol, arguments, CallingConvention.StdCall);
// if left < right return -1;
// if left = right return 0;
// if left > right return 1;
AddInstruction(OpCode.SUB);
AddInstruction(OpCode.SIGN);
return true;
case "System.Numerics.BigInteger.GreatestCommonDivisor(System.Numerics.BigInteger, System.Numerics.BigInteger)":
if (arguments is not null)
PrepareArgumentsForMethod(model, symbol, arguments, CallingConvention.StdCall);
JumpTarget whileTarget = new();
JumpTarget gcdTarget = new();
whileTarget.Instruction = AddInstruction(OpCode.DUP);
AddInstruction(OpCode.PUSH0);
AddInstruction(OpCode.EQUAL);
Jump(OpCode.JMPIF, gcdTarget);
AddInstruction(OpCode.DUP);
AddInstruction(OpCode.REVERSE3);
AddInstruction(OpCode.SWAP);
AddInstruction(OpCode.MOD);
Jump(OpCode.JMP, whileTarget);
gcdTarget.Instruction = AddInstruction(OpCode.NOP);
AddInstruction(OpCode.DROP);
AddInstruction(OpCode.ABS);
shargon marked this conversation as resolved.
Show resolved Hide resolved
return true;
case "System.Numerics.BigInteger.ToByteArray()":
if (instanceExpression is not null)
ConvertExpression(model, instanceExpression);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,5 +123,45 @@ public static bool testIsEven(BigInteger input)
{
return input.IsEven;
}

public static BigInteger TestAdd(BigInteger x, BigInteger y)
{
return BigInteger.Add(x, y);
}

public static BigInteger TestSubtract(BigInteger x, BigInteger y)
{
return BigInteger.Subtract(x, y);
}

public static BigInteger TestNegate(BigInteger x)
{
return BigInteger.Negate(x);
}

public static BigInteger TestMultiply(BigInteger x, BigInteger y)
{
return BigInteger.Multiply(x, y);
}

public static BigInteger TestDivide(BigInteger x, BigInteger y)
{
return BigInteger.Divide(x, y);
}

public static BigInteger TestRemainder(BigInteger x, BigInteger y)
{
return BigInteger.Remainder(x, y);
}

public static int TestCompare(BigInteger x, BigInteger y)
{
return BigInteger.Compare(x, y);
}

public static BigInteger GreatestCommonDivisor(BigInteger x, BigInteger y)
{
return BigInteger.GreatestCommonDivisor(x, y);
}
}
}
Loading