Skip to content
Permalink
Browse files

Additional Math functions (#620)

* - Minor update

* - WIP

* - WIP

* - WIP

* - WIP

* - WIP

* - WIP

* - Fix

* - Initial service component

* - WIP

* - WIP

* - WIP

* - WIP

* - WIP

* - WIP

* - WIP

* - WIP

* - WIP

* - WIP

* - WIP

* - WIP - More math functions

* - WIP - More math functions

* - WIP - More math functions

* - WIP - More math functions

* - WIP - More math functions
  • Loading branch information...
tgiphil committed Apr 1, 2019
1 parent 8a452af commit 97d2bcdb58cc22823da0014da6e1c50a0961d2da
Showing with 2,734 additions and 164 deletions.
  1. BIN Documentation/Screenshots/MOSA Launcher.png
  2. +9 −9 Source/Data/X64Instructions.json
  3. +9 −9 Source/Data/X86Instructions.json
  4. +80 −12 Source/Mosa.Compiler.Framework/BuiltInOptimizations.cs
  5. +10 −0 Source/Mosa.Compiler.Framework/ConstantOperand.cs
  6. +28 −12 Source/Mosa.Compiler.Framework/Operand.cs
  7. +51 −48 Source/Mosa.CoolWorld.x86/Boot.cs
  8. +9 −0 Source/Mosa.Korlib/Mosa.Korlib.csproj
  9. +51 −0 Source/Mosa.Korlib/System/ArithmeticException.cs
  10. +279 −1 Source/Mosa.Korlib/System/Decimal.cs
  11. +12 −1 Source/Mosa.Korlib/System/Double.cs
  12. +78 −0 Source/Mosa.Korlib/System/IConvertible.cs
  13. +24 −0 Source/Mosa.Korlib/System/ICustomFormatter.cs
  14. +112 −0 Source/Mosa.Korlib/System/Math.CoreCLR.cs
  15. +1,029 −0 Source/Mosa.Korlib/System/Math.cs
  16. +103 −0 Source/Mosa.Korlib/System/MathF.CoreCLR.cs
  17. +390 −0 Source/Mosa.Korlib/System/MathF.cs
  18. +15 −0 Source/Mosa.Korlib/System/MidpointRounding.cs
  19. +42 −0 Source/Mosa.Korlib/System/OverflowException.cs
  20. +14 −2 Source/Mosa.Korlib/System/Single.cs
  21. +35 −11 Source/Mosa.Platform.Intel/Stages/FloatingPointStage.cs
  22. +4 −9 Source/Mosa.Platform.x64/Instructions/PXor.cs
  23. +4 −9 Source/Mosa.Platform.x64/Instructions/Sqrtsd.cs
  24. +4 −9 Source/Mosa.Platform.x64/Instructions/Sqrtss.cs
  25. +6 −0 Source/Mosa.Platform.x64/Stages/FloatingPointStage.cs
  26. +4 −8 Source/Mosa.Platform.x86/Architecture.cs
  27. +3 −8 Source/Mosa.Platform.x86/Instructions/PXor.cs
  28. +3 −8 Source/Mosa.Platform.x86/Instructions/Sqrtsd.cs
  29. +3 −8 Source/Mosa.Platform.x86/Instructions/Sqrtss.cs
  30. +18 −0 Source/Mosa.Platform.x86/Intrinsic/Roundsd2Negative.cs
  31. +18 −0 Source/Mosa.Platform.x86/Intrinsic/Roundsd2Positive.cs
  32. +18 −0 Source/Mosa.Platform.x86/Intrinsic/Roundss2Negative.cs
  33. +18 −0 Source/Mosa.Platform.x86/Intrinsic/Roundss2Positive.cs
  34. +18 −0 Source/Mosa.Platform.x86/Intrinsic/Sqrtsd.cs
  35. +18 −0 Source/Mosa.Platform.x86/Intrinsic/Sqrtss.cs
  36. +6 −0 Source/Mosa.Platform.x86/Mosa.Platform.x86.csproj
  37. +5 −0 Source/Mosa.Platform.x86/Stages/FloatingPointStage.cs
  38. +2 −0 Source/Mosa.Plug.Korlib.x86/Mosa.Plug.Korlib.x86.csproj
  39. +58 −0 Source/Mosa.Plug.Korlib.x86/System/Math.CoreCLR.cs
  40. +46 −0 Source/Mosa.Plug.Korlib.x86/System/MathF.CoreCLR.cs
  41. +2 −0 Source/Mosa.Plug.Korlib/Mosa.Plug.Korlib.csproj
  42. +45 −0 Source/Mosa.Plug.Korlib/System/Math.CoreCLR.cs
  43. +33 −0 Source/Mosa.Plug.Korlib/System/MathF.CoreCLR.cs
  44. +18 −0 Source/Mosa.Runtime.x86/Native.cs
Binary file not shown.
@@ -3912,13 +3912,13 @@
"FlagsUnchanged": "ZCSOP",
"FlagsUndefined": "",
"FlagsUsed": "",
"OperandCount": 2,
"OperandCount": 1,
"ResultCount": 1,
"ThreeTwoAddressConversion": "true",
"ThreeTwoAddressConversion": "false",
"OpcodeEncoding": [
{
"Condition": "",
"Encoding": "[x64-rex32],operand-prefix=0x66,opcode=0x0F,opcode2=0xEF,mod=11,reg=reg3:r,rm=reg3:o2,rex.r=reg4x:r,rex.x=0,rex.b=reg4x:o2"
"Encoding": "[x64-rex32],operand-prefix=0x66,opcode=0x0F,opcode2=0xEF,mod=11,reg=reg3:r,rm=reg3:o1,rex.r=reg4x:r,rex.x=0,rex.b=reg4x:o1"
}
]
},
@@ -4366,13 +4366,13 @@
"FlagsUnchanged": "ZCSOP",
"FlagsUndefined": "",
"FlagsUsed": "",
"OperandCount": 2,
"OperandCount": 1,
"ResultCount": 1,
"ThreeTwoAddressConversion": "true",
"ThreeTwoAddressConversion": "false",
"OpcodeEncoding": [
{
"Condition": "",
"Encoding": "[x64-rex32],opcode=0xF3,opcode2=0x0F,opcode3=0x51,mod=11,reg=reg3:r,rm=reg3:o2,rex.r=reg4x:r,rex.x=0,rex.b=reg4x:o2"
"Encoding": "[x64-rex32],opcode=0xF3,opcode2=0x0F,opcode3=0x51,mod=11,reg=reg3:r,rm=reg3:o1,rex.r=reg4x:r,rex.x=0,rex.b=reg4x:o1"
}
]
},
@@ -4387,13 +4387,13 @@
"FlagsUnchanged": "ZCSOP",
"FlagsUndefined": "",
"FlagsUsed": "",
"OperandCount": 2,
"OperandCount": 1,
"ResultCount": 1,
"ThreeTwoAddressConversion": "true",
"ThreeTwoAddressConversion": "false",
"OpcodeEncoding": [
{
"Condition": "",
"Encoding": "[x64-rex32],opcode=0xF2,opcode2=0x0F,opcode3=0x51,mod=11,reg=reg3:r,rm=reg3:o2,rex.r=reg4x:r,rex.x=0,rex.b=reg4x:o2"
"Encoding": "[x64-rex32],opcode=0xF2,opcode2=0x0F,opcode3=0x51,mod=11,reg=reg3:r,rm=reg3:o1,rex.r=reg4x:r,rex.x=0,rex.b=reg4x:o1"
}
]
},
@@ -2829,13 +2829,13 @@
"FlagsUnchanged": "ZCSOP",
"FlagsUndefined": "",
"FlagsUsed": "",
"OperandCount": 2,
"OperandCount": 1,
"ResultCount": 1,
"ThreeTwoAddressConversion": "true",
"ThreeTwoAddressConversion": "false",
"OpcodeEncoding": [
{
"Condition": "",
"Encoding": "[x86],operand-prefix=0x66,opcode=0x0F,opcode2=0xEF,mod=11,reg=reg3:r,rm=reg3:o2"
"Encoding": "[x86],operand-prefix=0x66,opcode=0x0F,opcode2=0xEF,mod=11,reg=reg3:r,rm=reg3:o1"
}
]
},
@@ -3124,13 +3124,13 @@
"FlagsUnchanged": "ZCSOP",
"FlagsUndefined": "",
"FlagsUsed": "",
"OperandCount": 2,
"OperandCount": 1,
"ResultCount": 1,
"ThreeTwoAddressConversion": "true",
"ThreeTwoAddressConversion": "false",
"OpcodeEncoding": [
{
"Condition": "",
"Encoding": "[x86],opcode=0xF3,opcode2=0x0F,opcode3=0x51,mod=11,reg=reg3:r,rm=reg3:o2"
"Encoding": "[x86],opcode=0xF3,opcode2=0x0F,opcode3=0x51,mod=11,reg=reg3:r,rm=reg3:o1"
}
]
},
@@ -3145,13 +3145,13 @@
"FlagsUnchanged": "ZCSOP",
"FlagsUndefined": "",
"FlagsUsed": "",
"OperandCount": 2,
"OperandCount": 1,
"ResultCount": 1,
"ThreeTwoAddressConversion": "true",
"ThreeTwoAddressConversion": "false",
"OpcodeEncoding": [
{
"Condition": "",
"Encoding": "[x86],opcode=0xF2,opcode2=0x0F,opcode3=0x51,mod=11,reg=reg3:r,rm=reg3:o2"
"Encoding": "[x86],opcode=0xF2,opcode2=0x0F,opcode3=0x51,mod=11,reg=reg3:r,rm=reg3:o1"
}
]
},
@@ -15,7 +15,7 @@ public static SimpleInstruction ConstantFoldingAndStrengthReductionInteger(Instr

return new SimpleInstruction()
{
Instruction = GetMoveInteger(node.Result),
Instruction = GetMove(node.Result),
Result = node.Result,
Operand1 = operand
};
@@ -118,6 +118,22 @@ private static Operand ConstantFolding1Integer(InstructionNode node)
{
return ConstantOperand.Create(result.Type, (uint)op1.ConstantUnsignedLongInteger);
}
else if (instruction == IRInstruction.ConvertInt32ToFloatR4)
{
return ConstantOperand.Create(result.Type, (float)op1.ConstantUnsignedLongInteger);
}
else if (instruction == IRInstruction.ConvertInt32ToFloatR8)
{
return ConstantOperand.Create(result.Type, (double)op1.ConstantUnsignedLongInteger);
}
else if (instruction == IRInstruction.ConvertInt64ToFloatR4)
{
return ConstantOperand.Create(result.Type, (float)op1.ConstantUnsignedLongInteger);
}
else if (instruction == IRInstruction.ConvertInt64ToFloatR8)
{
return ConstantOperand.Create(result.Type, (double)op1.ConstantUnsignedLongInteger);
}

return null;
}
@@ -143,6 +159,14 @@ private static Operand ConstantFolding2Integer(InstructionNode node)
{
return ConstantOperand.Create(result.Type, op1.ConstantUnsignedLongInteger + op2.ConstantUnsignedLongInteger);
}
else if (instruction == IRInstruction.AddFloatR4)
{
return ConstantOperand.Create(result.Type, (op1.ConstantDoubleFloatingPoint + op2.ConstantDoubleFloatingPoint));
}
else if (instruction == IRInstruction.AddFloatR8)
{
return ConstantOperand.Create(result.Type, op1.ConstantDoubleFloatingPoint + op2.ConstantDoubleFloatingPoint);
}
else if (instruction == IRInstruction.Sub32)
{
return ConstantOperand.Create(result.Type, (op1.ConstantUnsignedLongInteger - op2.ConstantUnsignedLongInteger) & 0xFFFFFFFF);
@@ -151,6 +175,14 @@ private static Operand ConstantFolding2Integer(InstructionNode node)
{
return ConstantOperand.Create(result.Type, op1.ConstantUnsignedLongInteger - op2.ConstantUnsignedLongInteger);
}
else if (instruction == IRInstruction.SubFloatR4)
{
return ConstantOperand.Create(result.Type, op1.ConstantSingleFloatingPoint + op2.ConstantSingleFloatingPoint);
}
else if (instruction == IRInstruction.SubFloatR8)
{
return ConstantOperand.Create(result.Type, op1.ConstantSingleFloatingPoint + op2.ConstantSingleFloatingPoint);
}
else if (instruction == IRInstruction.LogicalAnd32)
{
return ConstantOperand.Create(result.Type, op1.ConstantUnsignedLongInteger & op2.ConstantUnsignedLongInteger & 0xFFFFFFFF);
@@ -183,6 +215,14 @@ private static Operand ConstantFolding2Integer(InstructionNode node)
{
return ConstantOperand.Create(result.Type, op1.ConstantUnsignedLongInteger * op2.ConstantUnsignedLongInteger);
}
else if (instruction == IRInstruction.MulFloatR4)
{
return ConstantOperand.Create(result.Type, (op1.ConstantSingleFloatingPoint * op2.ConstantSingleFloatingPoint));
}
else if (instruction == IRInstruction.MulFloatR8)
{
return ConstantOperand.Create(result.Type, op1.ConstantDoubleFloatingPoint * op2.ConstantDoubleFloatingPoint);
}
else if (instruction == IRInstruction.DivUnsigned32 && !op2.IsConstantZero)
{
return ConstantOperand.Create(result.Type, (op1.ConstantUnsignedLongInteger / op2.ConstantUnsignedLongInteger) & 0xFFFFFFFF);
@@ -199,6 +239,14 @@ private static Operand ConstantFolding2Integer(InstructionNode node)
{
return ConstantOperand.Create(result.Type, (ulong)(op1.ConstantSignedLongInteger / op2.ConstantSignedLongInteger));
}
else if (instruction == IRInstruction.DivFloatR4 && !op2.IsConstantZero)
{
return ConstantOperand.Create(result.Type, op1.ConstantSingleFloatingPoint / op2.ConstantSingleFloatingPoint);
}
else if (instruction == IRInstruction.DivFloatR8 && !op2.IsConstantZero)
{
return ConstantOperand.Create(result.Type, op1.ConstantDoubleFloatingPoint / op2.ConstantDoubleFloatingPoint);
}
else if (instruction == IRInstruction.ArithShiftRight32)
{
return ConstantOperand.Create(result.Type, ((ulong)(((long)op1.ConstantUnsignedLongInteger) >> (int)op2.ConstantUnsignedLongInteger)) & 0xFFFFFFFF);
@@ -239,6 +287,14 @@ private static Operand ConstantFolding2Integer(InstructionNode node)
{
return ConstantOperand.Create(result.Type, op1.ConstantUnsignedLongInteger % op2.ConstantUnsignedLongInteger);
}
else if (instruction == IRInstruction.RemFloatR4 && !op2.IsConstantZero)
{
return ConstantOperand.Create(result.Type, op1.ConstantSingleFloatingPoint % op2.ConstantSingleFloatingPoint);
}
else if (instruction == IRInstruction.RemFloatR8 && !op2.IsConstantZero)
{
return ConstantOperand.Create(result.Type, op1.ConstantDoubleFloatingPoint % op2.ConstantDoubleFloatingPoint);
}
else if (instruction == IRInstruction.To64)
{
return ConstantOperand.Create(result.Type, op2.ConstantUnsignedLongInteger << 32 | op1.ConstantUnsignedLongInteger);
@@ -307,19 +363,19 @@ private static Operand StrengthReductionInteger(InstructionNode node)
var op1 = node.Operand1;
var op2 = node.Operand2;

if ((instruction == IRInstruction.Add32 || instruction == IRInstruction.Add64) && op1.IsConstantZero)
if ((instruction == IRInstruction.Add32 || instruction == IRInstruction.Add64 || instruction == IRInstruction.AddFloatR4 || instruction == IRInstruction.AddFloatR8) && op1.IsConstantZero)
{
return op2;
}
else if ((instruction == IRInstruction.Add32 || instruction == IRInstruction.Add64) && op2.IsConstantZero)
else if ((instruction == IRInstruction.Add32 || instruction == IRInstruction.Add64 || instruction == IRInstruction.AddFloatR4 || instruction == IRInstruction.AddFloatR8) && op2.IsConstantZero)
{
return op1;
}
else if ((instruction == IRInstruction.Sub32 || instruction == IRInstruction.Sub64) && op2.IsConstantZero)
else if ((instruction == IRInstruction.Sub32 || instruction == IRInstruction.Sub64 || instruction == IRInstruction.SubFloatR4 || instruction == IRInstruction.SubFloatR8) && op2.IsConstantZero)
{
return op1;
}
else if ((instruction == IRInstruction.Sub32 || instruction == IRInstruction.Sub64) && (op1 == op2))
else if ((instruction == IRInstruction.Sub32 || instruction == IRInstruction.Sub64 || instruction == IRInstruction.SubFloatR4 || instruction == IRInstruction.SubFloatR8) && (op1 == op2))
{
return ConstantOperand.Create(result.Type, 0);
}
@@ -339,31 +395,31 @@ private static Operand StrengthReductionInteger(InstructionNode node)
{
return ConstantOperand.Create(result.Type, 0);
}
else if ((instruction == IRInstruction.MulSigned32 || instruction == IRInstruction.MulUnsigned32 || instruction == IRInstruction.MulSigned64 || instruction == IRInstruction.MulUnsigned64) && (op1.IsConstantZero || op2.IsConstantZero))
else if ((instruction == IRInstruction.MulSigned32 || instruction == IRInstruction.MulUnsigned32 || instruction == IRInstruction.MulSigned64 || instruction == IRInstruction.MulUnsigned64 || instruction == IRInstruction.MulFloatR4 || instruction == IRInstruction.MulFloatR8) && (op1.IsConstantZero || op2.IsConstantZero))
{
return ConstantOperand.Create(result.Type, 0);
}
else if ((instruction == IRInstruction.MulSigned32 || instruction == IRInstruction.MulUnsigned32 || instruction == IRInstruction.MulSigned64 || instruction == IRInstruction.MulUnsigned64) && op1.IsConstantOne)
else if ((instruction == IRInstruction.MulSigned32 || instruction == IRInstruction.MulUnsigned32 || instruction == IRInstruction.MulSigned64 || instruction == IRInstruction.MulUnsigned64 || instruction == IRInstruction.MulFloatR4 || instruction == IRInstruction.MulFloatR8) && op1.IsConstantOne)
{
return op2;
}
else if ((instruction == IRInstruction.MulSigned32 || instruction == IRInstruction.MulUnsigned32 || instruction == IRInstruction.MulSigned64 || instruction == IRInstruction.MulUnsigned64) && op2.IsConstantOne)
else if ((instruction == IRInstruction.MulSigned32 || instruction == IRInstruction.MulUnsigned32 || instruction == IRInstruction.MulSigned64 || instruction == IRInstruction.MulUnsigned64 || instruction == IRInstruction.MulFloatR4 || instruction == IRInstruction.MulFloatR8) && op2.IsConstantOne)
{
return op1;
}
else if ((node.Instruction == IRInstruction.DivSigned32 || node.Instruction == IRInstruction.DivUnsigned32 || node.Instruction == IRInstruction.DivSigned64 || node.Instruction == IRInstruction.DivUnsigned64) && op2.IsConstantOne)
else if ((node.Instruction == IRInstruction.DivSigned32 || node.Instruction == IRInstruction.DivUnsigned32 || node.Instruction == IRInstruction.DivSigned64 || node.Instruction == IRInstruction.DivUnsigned64 || instruction == IRInstruction.DivFloatR4 || instruction == IRInstruction.DivFloatR8) && op2.IsConstantOne)
{
return op1;
}
else if ((node.Instruction == IRInstruction.DivSigned32 || node.Instruction == IRInstruction.DivUnsigned32 || node.Instruction == IRInstruction.DivSigned64 || node.Instruction == IRInstruction.DivUnsigned64) && op1.IsConstantZero)
else if ((node.Instruction == IRInstruction.DivSigned32 || node.Instruction == IRInstruction.DivUnsigned32 || node.Instruction == IRInstruction.DivSigned64 || node.Instruction == IRInstruction.DivUnsigned64 || instruction == IRInstruction.DivFloatR4 || instruction == IRInstruction.DivFloatR8) && op1.IsConstantZero)
{
return ConstantOperand.Create(result.Type, 0);
}
else if ((node.Instruction == IRInstruction.DivSigned32 || node.Instruction == IRInstruction.DivUnsigned32 || node.Instruction == IRInstruction.DivSigned64 || node.Instruction == IRInstruction.DivUnsigned64) && op1 == op2)
else if ((node.Instruction == IRInstruction.DivSigned32 || node.Instruction == IRInstruction.DivUnsigned32 || node.Instruction == IRInstruction.DivSigned64 || node.Instruction == IRInstruction.DivUnsigned64 || instruction == IRInstruction.DivFloatR4 || instruction == IRInstruction.DivFloatR8) && op1 == op2)
{
return ConstantOperand.Create(result.Type, 1);
}
else if ((node.Instruction == IRInstruction.RemUnsigned32 || node.Instruction == IRInstruction.RemUnsigned64) && op2.IsConstantOne)
else if ((node.Instruction == IRInstruction.RemUnsigned32 || node.Instruction == IRInstruction.RemUnsigned64 || instruction == IRInstruction.RemFloatR4 || instruction == IRInstruction.RemFloatR8) && op2.IsConstantOne)
{
return ConstantOperand.Create(result.Type, 0);
}
@@ -776,6 +832,18 @@ private static BaseInstruction GetMoveInteger(Operand operand)
return operand.Is64BitInteger ? (BaseInstruction)IRInstruction.MoveInt64 : IRInstruction.MoveInt32;
}

private static BaseInstruction GetMove(Operand operand)
{
if (operand.IsR4)
return IRInstruction.MoveFloatR4;
else if (operand.IsR8)
return IRInstruction.MoveFloatR8;
else if (operand.Is64BitInteger)
return IRInstruction.MoveInt64;
else
return IRInstruction.MoveInt32;
}

#endregion Helpers
}
}
@@ -28,6 +28,16 @@ public static Operand Create(MosaType type, uint value)
return Operand.CreateConstant(type, value);
}

public static Operand Create(MosaType type, float value)
{
return Operand.CreateConstant(type, value);
}

public static Operand Create(MosaType type, double value)
{
return Operand.CreateConstant(type, value);
}

#endregion Constant Helper Methods
}
}
@@ -477,24 +477,18 @@ private Operand(ShiftType shiftType)
/// <exception cref="CompilerException"></exception>
public static Operand CreateConstant(MosaType type, ulong value)
{
var operand = new Operand(type)
if (type.IsReferenceType && value != 0)
{
throw new CompilerException();
}

return new Operand(type)
{
IsConstant = true,
ConstantUnsignedLongInteger = value,
IsNull = (type.IsReferenceType && value == 0),
IsResolved = true
};
if (type.IsReferenceType && value != 0)
{
throw new CompilerException();
}

//if (!(operand.IsInteger || operand.IsBoolean || operand.IsChar || operand.IsPointer || operand.IsReferenceType))
//{
// throw new CompilerException();
//}

return operand;
}

/// <summary>
@@ -510,6 +504,28 @@ public static Operand CreateConstant(MosaType type, long value)
return CreateConstant(type, (ulong)value);
}

public static Operand CreateConstant(MosaType type, float value)
{
return new Operand(type)
{
IsConstant = true,
ConstantSingleFloatingPoint = value,
IsNull = false,
IsResolved = true
};
}

public static Operand CreateConstant(MosaType type, double value)
{
return new Operand(type)
{
IsConstant = true,
ConstantDoubleFloatingPoint = value,
IsNull = false,
IsResolved = true
};
}

/// <summary>
/// Creates a new constant <see cref="Operand" /> for the given integral value.
/// </summary>
Oops, something went wrong.

0 comments on commit 97d2bcd

Please sign in to comment.
You can’t perform that action at this time.