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

Additional Math functions #620

Merged
merged 29 commits into from Apr 1, 2019
Merged
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.
+355 −95
Diff settings

Always

Just for now

- WIP - More math functions

  • Loading branch information...
tgiphil committed Mar 31, 2019
commit 65c0c964f64a410509fcb7f4c4b29bab693d33ee
@@ -4366,13 +4366,13 @@
"FlagsUnchanged": "ZCSOP",
"FlagsUndefined": "",
"FlagsUsed": "",
"OperandCount": 2,
"OperandCount": 1,
"ResultCount": 1,
"ThreeTwoAddressConversion": "true",
"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"
}
]
},
@@ -3145,13 +3145,13 @@
"FlagsUnchanged": "ZCSOP",
"FlagsUndefined": "",
"FlagsUsed": "",
"OperandCount": 2,
"OperandCount": 1,
"ResultCount": 1,
"ThreeTwoAddressConversion": "true",
"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"
}
]
},
@@ -10,6 +10,7 @@
using Mosa.Kernel.x86;
using Mosa.Runtime.Plug;
using Mosa.Runtime.x86;
using System;

namespace Mosa.CoolWorld.x86
{
@@ -298,53 +299,55 @@ public static void ProcessInterrupt(uint interrupt, uint errorCode)
Console.BackgroundColor = back;
}

//public static void Mandelbrot()
//{
// double xmin = -2.1;
// double ymin = -1.3;
// double xmax = 1;
// double ymax = 1.3;

// int Width = 200;
// int Height = 200;

// double x, y, x1, y1, xx;

// int looper, s, z = 0;
// double intigralX, intigralY = 0.0;

// intigralX = (xmax - xmin) / Width; // Make it fill the whole window
// intigralY = (ymax - ymin) / Height;
// x = xmin;

// for (s = 1; s < Width; s++)
// {
// y = ymin;
// for (z = 1; z < Height; z++)
// {
// x1 = 0;
// y1 = 0;
// looper = 0;
// while (looper < 100 && Math.Sqrt((x1 * x1) + (y1 * y1)) < 2)
// {
// looper++;
// xx = (x1 * x1) - (y1 * y1) + x;
// y1 = 2 * x1 * y1 + y;
// x1 = xx;
// }

// // Get the percent of where the looper stopped
// double perc = looper / (100.0);
// // Get that part of a 255 scale
// int val = ((int)(perc * 255));
// // Use that number to set the color

// //map[s, z]= value;

// y += intigralY;
// }
// x += intigralX;
// }
//}
public static void Mandelbrot()
{
double xmin = -2.1;
double ymin = -1.3;
double xmax = 1;
double ymax = 1.3;

int Width = 200;
int Height = 200;

double x, y, x1, y1, xx;

int looper, s, z = 0;
double intigralX, intigralY = 0.0;

intigralX = (xmax - xmin) / Width; // Make it fill the whole window
intigralY = (ymax - ymin) / Height;
x = xmin;

for (s = 1; s < Width; s++)
{
y = ymin;
for (z = 1; z < Height; z++)
{
x1 = 0;
y1 = 0;
looper = 0;
while (looper < 100 && Math.Sqrt((x1 * x1) + (y1 * y1)) < 2)
{
looper++;
xx = (x1 * x1) - (y1 * y1) + x;
y1 = 2 * x1 * y1 + y;
x1 = xx;
}

// Get the percent of where the looper stopped
double perc = looper / (100.0);

// Get that part of a 255 scale
int val = ((int)(perc * 255));

// Use that number to set the color

//map[s, z]= value;

y += intigralY;
}
x += intigralX;
}
}
}
}
@@ -94,8 +94,8 @@ public static partial class Math
//[MethodImpl(MethodImplOptions.InternalCall)]
//public static extern double Sinh(double value);

//[MethodImpl(MethodImplOptions.InternalCall)]
//public static extern double Sqrt(double d);
[MethodImpl(MethodImplOptions.InternalCall)]
public static extern double Sqrt(double d);

//[MethodImpl(MethodImplOptions.InternalCall)]
//public static extern double Tan(double a);
@@ -85,8 +85,8 @@ public static partial class MathF
//[MethodImpl(MethodImplOptions.InternalCall)]
//public static extern float Sinh(float x);

//[MethodImpl(MethodImplOptions.InternalCall)]
//public static extern float Sqrt(float x);
[MethodImpl(MethodImplOptions.InternalCall)]
public static extern float Sqrt(float x);

//[MethodImpl(MethodImplOptions.InternalCall)]
//public static extern float Tan(float x);
@@ -15,6 +15,8 @@ public abstract class FloatingPointStage : BasePlatformTransformationStage

protected abstract bool IsLoad(BaseInstruction instruction);

protected abstract bool IsIntegerToFloating(BaseInstruction instruction);

protected abstract BaseInstruction MovssLoad { get; }

protected abstract BaseInstruction MovsdLoad { get; }
@@ -38,7 +40,12 @@ protected override void Run()
if (node.OperandCount == 0)
continue;

// fixme - must be a better way
if (IsIntegerToFloating(node.Instruction) && node.Operand1.IsConstant)
{
FixConstantIntegerToFloat(node);
continue;
}

if (IsLoad(node.Instruction))
continue;

@@ -47,6 +54,25 @@ protected override void Run()
}
}

protected void FixConstantIntegerToFloat(InstructionNode node)
{
var source = node.Operand1;
var result = node.Result;

if (result.IsR4)
{
var symbol = MethodCompiler.Linker.GetConstantSymbol((float)source.ConstantUnsignedLongInteger);
var label = Operand.CreateLabel(result.Type, symbol.Name);
node.SetInstruction(MovssLoad, result, label, ConstantZero);
}
else if (result.IsR8)
{
var symbol = MethodCompiler.Linker.GetConstantSymbol((double)source.ConstantUnsignedLongInteger);
var label = Operand.CreateLabel(result.Type, symbol.Name);
node.SetInstruction(MovsdLoad, result, label, ConstantZero);
}
}

/// <summary>
/// Emits the constant operands.
/// </summary>
@@ -63,23 +89,21 @@ protected void EmitFloatingPointConstants(InstructionNode node)
if (operand.IsUnresolvedConstant)
continue;

var v1 = AllocateVirtualRegister(operand.Type);

var symbol = (operand.IsR4) ?
MethodCompiler.Linker.GetConstantSymbol(operand.ConstantSingleFloatingPoint)
: MethodCompiler.Linker.GetConstantSymbol(operand.ConstantDoubleFloatingPoint);

var s1 = Operand.CreateLabel(operand.Type, symbol.Name);

var before = new Context(node).InsertBefore();

var v1 = AllocateVirtualRegister(operand.Type);

if (operand.IsR4)
{
before.SetInstruction(MovssLoad, v1, s1, ConstantZero);
var symbol = MethodCompiler.Linker.GetConstantSymbol(operand.ConstantSingleFloatingPoint);
var label = Operand.CreateLabel(operand.Type, symbol.Name);
before.SetInstruction(MovssLoad, v1, label, ConstantZero);
}
else
{
before.SetInstruction(MovsdLoad, v1, s1, ConstantZero);
var symbol = MethodCompiler.Linker.GetConstantSymbol(operand.ConstantDoubleFloatingPoint);
var label = Operand.CreateLabel(operand.Type, symbol.Name);
before.SetInstruction(MovsdLoad, v1, label, ConstantZero);
}

node.SetOperand(i, v1);
@@ -15,7 +15,7 @@ public sealed class Sqrtss : X64Instruction
public override int ID { get { return 520; } }

internal Sqrtss()
: base(1, 2)
: base(1, 1)
{
}

@@ -26,7 +26,7 @@ internal Sqrtss()
public override void Emit(InstructionNode node, BaseCodeEmitter emitter)
{
System.Diagnostics.Debug.Assert(node.ResultCount == 1);
System.Diagnostics.Debug.Assert(node.OperandCount == 2);
System.Diagnostics.Debug.Assert(node.OperandCount == 1);
System.Diagnostics.Debug.Assert(node.Result.IsCPURegister);
System.Diagnostics.Debug.Assert(node.Operand1.IsCPURegister);
System.Diagnostics.Debug.Assert(node.Result.Register == node.Operand1.Register);
@@ -36,13 +36,13 @@ public override void Emit(InstructionNode node, BaseCodeEmitter emitter)
emitter.OpcodeEncoder.AppendBit(0b0);
emitter.OpcodeEncoder.AppendBit((node.Result.Register.RegisterCode >> 3) & 0x1);
emitter.OpcodeEncoder.AppendBit(0b0);
emitter.OpcodeEncoder.AppendBit((node.Operand2.Register.RegisterCode >> 3) & 0x1);
emitter.OpcodeEncoder.AppendBit((node.Operand1.Register.RegisterCode >> 3) & 0x1);
emitter.OpcodeEncoder.AppendByte(0xF3);
emitter.OpcodeEncoder.AppendByte(0x0F);
emitter.OpcodeEncoder.AppendByte(0x51);
emitter.OpcodeEncoder.Append2Bits(0b11);
emitter.OpcodeEncoder.Append3Bits(node.Result.Register.RegisterCode);
emitter.OpcodeEncoder.Append3Bits(node.Operand2.Register.RegisterCode);
emitter.OpcodeEncoder.Append3Bits(node.Operand1.Register.RegisterCode);
}
}
}
@@ -15,6 +15,12 @@ protected override bool IsLoad(BaseInstruction instruction)
return instruction == X64.MovsdLoad || instruction == X64.MovssLoad || instruction == X64.MovLoad32 || instruction == X64.MovLoad64;
}

protected override bool IsIntegerToFloating(BaseInstruction instruction)
{
return instruction == X64.Cvtsi2sd64 || instruction == X64.Cvtsi2ss64 ||
instruction == X64.Cvtsi2sd32 || instruction == X64.Cvtsi2ss32;
}

protected override BaseInstruction MovssLoad { get { return X64.MovssLoad; } }

protected override BaseInstruction MovsdLoad { get { return X64.MovsdLoad; } }
@@ -349,15 +349,11 @@ public override void InsertCompoundCopy(MethodCompiler methodCompiler, Context c
/// <param name="source">The source.</param>
public override void InsertExchangeInstruction(Context context, Operand destination, Operand source)
{
if (source.IsR4)
if (source.IsR8 || source.IsR4)
{
// TODO
throw new CompilerException("R4 not implemented in InsertExchangeInstruction method");
}
else if (source.IsR8)
{
// TODO
throw new CompilerException("R8 not implemented in InsertExchangeInstruction method");
context.AppendInstruction(X86.PXor, destination, source);
context.AppendInstruction(X86.PXor, source, destination);
context.AppendInstruction(X86.PXor, destination, source);
}
else
{
@@ -15,7 +15,7 @@ public sealed class Sqrtsd : X86Instruction
public override int ID { get { return 297; } }

internal Sqrtsd()
: base(1, 2)
: base(1, 1)
{
}

@@ -26,7 +26,7 @@ internal Sqrtsd()
public override void Emit(InstructionNode node, BaseCodeEmitter emitter)
{
System.Diagnostics.Debug.Assert(node.ResultCount == 1);
System.Diagnostics.Debug.Assert(node.OperandCount == 2);
System.Diagnostics.Debug.Assert(node.OperandCount == 1);
System.Diagnostics.Debug.Assert(node.Result.IsCPURegister);
System.Diagnostics.Debug.Assert(node.Operand1.IsCPURegister);
System.Diagnostics.Debug.Assert(node.Result.Register == node.Operand1.Register);
@@ -36,7 +36,7 @@ public override void Emit(InstructionNode node, BaseCodeEmitter emitter)
emitter.OpcodeEncoder.AppendByte(0x51);
emitter.OpcodeEncoder.Append2Bits(0b11);
emitter.OpcodeEncoder.Append3Bits(node.Result.Register.RegisterCode);
emitter.OpcodeEncoder.Append3Bits(node.Operand2.Register.RegisterCode);
emitter.OpcodeEncoder.Append3Bits(node.Operand1.Register.RegisterCode);
}
}
}
@@ -0,0 +1,18 @@
// Copyright (c) MOSA Project. Licensed under the New BSD License.

using Mosa.Compiler.Framework;

namespace Mosa.Platform.x86.Intrinsic
{
/// <summary>
/// IntrinsicMethods
/// </summary>
static partial class IntrinsicMethods
{
[IntrinsicMethod("Mosa.Platform.x86.Intrinsic:Roundsd2Negative")]
private static void Roundsd2Negative(Context context, MethodCompiler methodCompiler)
{
context.SetInstruction(X86.Roundsd, context.Result, context.Operand1, methodCompiler.CreateConstant((byte)1));
}
}
}
@@ -0,0 +1,18 @@
// Copyright (c) MOSA Project. Licensed under the New BSD License.

using Mosa.Compiler.Framework;

namespace Mosa.Platform.x86.Intrinsic
{
/// <summary>
/// IntrinsicMethods
/// </summary>
static partial class IntrinsicMethods
{
[IntrinsicMethod("Mosa.Platform.x86.Intrinsic:Roundsd2Positive")]
private static void Roundsd2Positive(Context context, MethodCompiler methodCompiler)
{
context.SetInstruction(X86.Roundsd, context.Result, context.Operand1, methodCompiler.CreateConstant((byte)2));
}
}
}
@@ -0,0 +1,18 @@
// Copyright (c) MOSA Project. Licensed under the New BSD License.

using Mosa.Compiler.Framework;

namespace Mosa.Platform.x86.Intrinsic
{
/// <summary>
/// IntrinsicMethods
/// </summary>
static partial class IntrinsicMethods
{
[IntrinsicMethod("Mosa.Platform.x86.Intrinsic:Roundss2Negative")]
private static void Roundss2Negative(Context context, MethodCompiler methodCompiler)
{
context.SetInstruction(X86.Roundss, context.Result, context.Operand1, methodCompiler.CreateConstant((byte)1));
}
}
}
Oops, something went wrong.
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.