first attempt to write inline ASM in C#, failed due to proteted ram

This commit is contained in:
Nitload
2023-04-22 03:40:36 +08:00
parent 906becc265
commit 1cb78e9e8f
78 changed files with 9595 additions and 56 deletions
Binary file not shown.
Binary file not shown.
Binary file not shown.
-5
View File
@@ -7,11 +7,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StgSharp", "StgSharp\StgSha
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StgSharpDebug", "StgSharpDebug\StgSharpDebug.csproj", "{467F2A1A-D7E2-4904-AAAD-A5998EE45C71}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Dependency", "Dependency", "{063F21D6-CDE3-4DE1-9008-2A211F59AB38}"
ProjectSection(SolutionItems) = preProject
Dependency\glfw3.dll = Dependency\glfw3.dll
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
+102
View File
@@ -0,0 +1,102 @@
using Steamworks;
using StgSharp.Control;
using StgSharp.Math;
using System;
using System.Collections.Generic;
using System.Numerics;
using System.Text;
namespace StgSharp.Geometries
{
public class Polygon6 : IPlainGeometry
{
internal Point _refOrigin;
internal Point vertex1;
internal Point vertex2;
internal Point vertex3;
internal Point vertex4;
internal Point vertex5;
internal Point vertex6;
internal readonly int _vertexCount;
public override Point RefPoint01 => vertex1;
public override Point RefPoint02 => vertex2;
public override Point RefPoint03 => vertex3;
internal GetLocationHandler movP01Operation = default;
internal GetLocationHandler movP02Operation = default;
internal GetLocationHandler movP03Operation = default;
internal GetLocationHandler movP04Operation = default;
internal GetLocationHandler movP05Operation = default;
internal GetLocationHandler movP06Operation = default;
public GetLocationHandler MovP01Operation { get => movP01Operation; }
public GetLocationHandler MovP02Operation { get => movP02Operation; }
public GetLocationHandler MovP03Operation { get => movP03Operation; }
public GetLocationHandler MovP04Operation { get => movP04Operation; }
public GetLocationHandler MovP05Operation { get => movP05Operation; }
public GetLocationHandler MovP06Operation { get => movP06Operation; }
public virtual vec3d movPoint01(uint tick)
{
return movP01Operation.Invoke(GameTimeLine.tickCounter._value);
}
public virtual vec3d movPoint02(uint tick)
{
return movP02Operation.Invoke(GameTimeLine.tickCounter._value);
}
public virtual vec3d movPoint03(uint tick)
{
return movP03Operation.Invoke(GameTimeLine.tickCounter._value);
}
public virtual vec3d movPoint04(uint tick)
{
return movP04Operation.Invoke(GameTimeLine.tickCounter._value);
}
public virtual vec3d movPoint05(uint tick)
{
return movP05Operation.Invoke(GameTimeLine.tickCounter._value);
}
public virtual vec3d movPoint06(uint tick)
{
return movP06Operation.Invoke(GameTimeLine.tickCounter._value);
}
internal override void OnRender(uint tick)
{
uint nowTick = tick - BornTime;
vertex1._position = RefOrigin._position + movPoint01(nowTick);
vertex2._position = RefOrigin._position + movPoint02(nowTick);
vertex3._position = RefOrigin._position + movPoint03(nowTick);
vertex4._position = RefOrigin._position + movPoint04(nowTick);
vertex5._position = RefOrigin._position + movPoint05(nowTick);
vertex6._position = RefOrigin._position + movPoint06(nowTick);
}
public override Line[] GetAllSides()
{
return new Line[6]
{
new Line(vertex1,vertex2),
new Line(vertex2,vertex3),
new Line(vertex3,vertex4),
new Line(vertex4,vertex5),
new Line(vertex5,vertex6),
new Line(vertex6,vertex1)
};
}
public override Plain GetPlain()
{
return new Plain(vertex1,vertex2,vertex3);
}
}
}
File diff suppressed because it is too large Load Diff
@@ -5,9 +5,9 @@ using System.Text;
using System.Reflection;
using System.Runtime.InteropServices;
namespace ssd
namespace StgSharp.Optimization
{
public class CpuID : IDisposable
public class CpuInfo : IDisposable
{
[UnmanagedFunctionPointerAttribute(CallingConvention.Cdecl)]
public delegate void CpuIDDelegate(int level, byte[] buffer);
@@ -68,6 +68,7 @@ namespace ssd
// buffer[8] = ecx
// buffer[12] = edx
// }
/*
private byte[] x86CodeBytes =
{
0x55, // push ebp
@@ -90,8 +91,10 @@ namespace ssd
0x5D, // pop ebp
0xc3 // ret
};
*/
private byte[] x64CodeBytes =
//x64
private byte[] CodeBytes =
{
0x53, // push rbx this gets clobbered by cpuid
@@ -117,27 +120,23 @@ namespace ssd
0xc3 // ret
};
public CpuID()
public CpuInfo()
{
Compile();
}
~CpuID()
~CpuInfo()
{
Dispose(false);
}
private void Compile()
{
byte[] codeBytes;
byte[] codeBytes = CodeBytes;
if (IntPtr.Size == 4)
if (IntPtr.Size != 8)
{
codeBytes = x86CodeBytes;
}
else
{
codeBytes = x64CodeBytes;
throw new Exception("This class supports 64bits OS only presently");
}
this.codePointer = VirtualAlloc(
@@ -0,0 +1,22 @@
using StgSharp.Math;
using System;
using System.Collections.Generic;
using System.Text;
namespace StgSharp.Optimization
{
public static unsafe partial class SSE
{
public static m128 ToM128(this vec2d vec)
{
return *(m128*)&vec;
}
public static m128 ToVec4D(this m128 m)
{
return *(m128*)&m;
}
}
}
@@ -0,0 +1,83 @@
using StgSharp.Math;
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using static StgSharp.Optimization.CpuInfo;
namespace StgSharp.Optimization
{
public static unsafe partial class SSE
{
public static m128 sqrtssM128Cache = new m128();
private static IntPtr sqrtssCodePointer;
private static sqrtssDelegate sqrtssDele;
private static byte[] sqrtssCodeBytes
= new byte[]
{
0x48,0xb8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, // mov rax, [m128 location]
0xf3,0x0f,0x6f,0x00, // movqdu xmm0,xmmword ptr [rax]
0xf3,0x0f,0x51,0xc0, // sqrtss xmm0,xmm0
0xf3,0x0f,0x7f,0x00 // movdqu xmmword ptr [rax],xmm0
};
[DllImport("kernel32.dll", SetLastError = true)]
private static extern IntPtr VirtualAlloc(
IntPtr lpAddress,
UIntPtr dwSize,
AllocationType flAllocationType,
MemoryProtection flProtect
);
[DllImport("kernel32")]
private static extern bool VirtualFree(
IntPtr lpAddress,
UInt32 dwSize,
UInt32 dwFreeType
);
private unsafe static void InitSqrtss()
{
var marshalPtr = Marshal.AllocHGlobal(32);
var m128ptr = new IntPtr(16*((long)marshalPtr/16+1));
m128* sqrtssCachePointer = (m128*)m128ptr.ToPointer();
sqrtssM128Cache = *sqrtssCachePointer;
byte[] sqrtssM128PointerBytes = BitConverter.GetBytes((ulong)sqrtssCachePointer);
for (int i = 0; i < sqrtssM128PointerBytes.Length; i++)
{
sqrtssCodeBytes[2+i] = sqrtssCodeBytes[7-i];
}
if (IntPtr.Size!=8)
{
throw new Exception("This SSE class support 64bits OS only presently!");
}
sqrtssCodePointer = VirtualAlloc(
IntPtr.Zero,
new UIntPtr((uint)sqrtssCodeBytes.Length),
AllocationType.COMMIT | AllocationType.RESERVE,
MemoryProtection.EXECUTE_READWRITE
);
Marshal.Copy(sqrtssCodeBytes, 0, sqrtssCodePointer, sqrtssCodeBytes.Length);
sqrtssDele = (sqrtssDelegate)Marshal.GetDelegateForFunctionPointer(sqrtssCodePointer,typeof(sqrtssDelegate));
}
public static float Sqrtss(float a)
{
sqrtssM128Cache.float0 = a;
sqrtssDele();
return sqrtssM128Cache.float0;
}
}
[UnmanagedFunctionPointerAttribute(CallingConvention.StdCall)]
internal delegate void sqrtssDelegate();
}
+6 -2
View File
@@ -4,9 +4,13 @@ using System.Text;
namespace StgSharp.Optimization
{
public unsafe class SSE
public static unsafe partial class SSE
{
static SSE()
{
//编译模拟Sqrtss指令
InitSqrtss();
}
}
}
@@ -1 +1 @@
0bd409a23d12a7e03a996538d35e30898336817d
508c7d643da3ffba0187af6df00e8fd1f0a8c855
+1 -4
View File
@@ -40,11 +40,8 @@ internal class Program
}
*/
}
Console.WriteLine(StgSharp.Optimization.SSE.Sqrtss(25.0f));
public class MyNum : INode<MyNum>
{
}
}
@@ -4,35 +4,65 @@
<name>StgSharp</name>
</assembly>
<members>
<member name="P:StgSharp.Entity.CollisionSensor">
<member name="P:StgSharp.IEntity.CollisionSensor">
<summary>
A sensor used for cheking out if a bullet hit a player or something else
</summary>
</member>
<member name="P:StgSharp.Entity.Location">
<member name="P:StgSharp.IEntity.Location">
<summary>
A 2D vector presenting the position of an enemy entity, position can also be visited by X, Y
</summary>
</member>
<member name="P:StgSharp.Entity.X">
<member name="P:StgSharp.IEntity.X">
<summary>
X position of an enenmy entity
</summary>
</member>
<member name="P:StgSharp.Entity.Y">
<member name="P:StgSharp.IEntity.Y">
<summary>
Y position of an enemy entity
</summary>
</member>
<member name="F:StgSharp.Geometries.RenderMode.BasePoint">
<member name="T:StgSharp.Geometries.UnusedVertexException">
<summary>
坐标点将基于参考原点构建
当几何体调用了过多的CalcVec方法时,产生UnusedVertexException异常
</summary>
</member>
<member name="F:StgSharp.Geometries.RenderMode.RefPoint">
<member name="T:StgSharp.Geometries.GetLocationHandler">
<summary>
坐标点将基于参考起点构建
计算位移失量,需要手动输入游戏刻以提供时间数据
</summary>
<param name="tickCounter"></param>
<returns>表示位移量的三维矢量</returns>
</member>
<member name="T:StgSharp.Geometries.IPlainGeometry">
<summary>
平面几何体。包含三角形,对称四边形,自由多边形等。最多可由16个点参数定义。
</summary>
</member>
<member name="M:StgSharp.Geometries.Parallelogram.MovVertex03(System.UInt32)">
<summary>
移动平行四边形的第三个顶点,是一个多余的方法,调用会引发多余变量异常
</summary>
<param name="tick">当前游戏刻</param>
<returns></returns>
<exception cref="T:StgSharp.Geometries.UnusedVertexException">几何体过定义</exception>
</member>
<member name="M:StgSharp.Geometries.Point.Update(System.UInt32)">
<summary>
刷新点的位置,即移动点。当该点是某个几何体的从动点时
(即点坐标由几何体其他点约束或点参数已经在创建几何体时定义)
请不要再次进行定义,程序将自行忽略此处的代码。
</summary>
<param name="tick">当前游戏刻</param>
</member>
<member name="M:StgSharp.Geometries.Rectangle.MovCenter(System.UInt32)">
<summary>
计算中心点相对于参考原点的位移
</summary>
<param name="tick">当前游戏刻</param>
<returns></returns>
</member>
<member name="M:StgSharp.Math.VectorCalc.multiple(StgSharp.Math.colum2,StgSharp.Math.vec2d)">
<summary>
@@ -1 +1 @@
69eea3b78f7825fc761f43509d32b318b370c0d6
88f0e007daf41ad68341e491760b7799a6c6cbcc
Binary file not shown.