Skip to content

Commit

Permalink
use UnmanagedCallersOnly for delegates
Browse files Browse the repository at this point in the history
  • Loading branch information
emmauss authored and GreemDev committed Jan 4, 2025
1 parent 3e5b2bd commit c8d598d
Show file tree
Hide file tree
Showing 14 changed files with 659 additions and 677 deletions.
20 changes: 10 additions & 10 deletions src/ARMeilleure/Instructions/InstEmitSimdArithmetic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ public static void Fabd_S(ArmEmitterContext context)
{
Operand res = EmitSoftFloatCall(context, nameof(SoftFloat32.FPSub), op1, op2);

return EmitUnaryMathCall(context, nameof(Math.Abs), res);
return EmitUnaryMathCall(context, nameof(MathHelper.Abs), res);
});
}
}
Expand Down Expand Up @@ -451,7 +451,7 @@ public static void Fabd_V(ArmEmitterContext context)
{
Operand res = EmitSoftFloatCall(context, nameof(SoftFloat32.FPSub), op1, op2);

return EmitUnaryMathCall(context, nameof(Math.Abs), res);
return EmitUnaryMathCall(context, nameof(MathHelper.Abs), res);
});
}
}
Expand Down Expand Up @@ -483,7 +483,7 @@ public static void Fabs_S(ArmEmitterContext context)
{
EmitScalarUnaryOpF(context, (op1) =>
{
return EmitUnaryMathCall(context, nameof(Math.Abs), op1);
return EmitUnaryMathCall(context, nameof(MathHelper.Abs), op1);
});
}
}
Expand Down Expand Up @@ -522,7 +522,7 @@ public static void Fabs_V(ArmEmitterContext context)
{
EmitVectorUnaryOpF(context, (op1) =>
{
return EmitUnaryMathCall(context, nameof(Math.Abs), op1);
return EmitUnaryMathCall(context, nameof(MathHelper.Abs), op1);
});
}
}
Expand Down Expand Up @@ -2246,7 +2246,7 @@ public static void Frintm_S(ArmEmitterContext context)
{
EmitScalarUnaryOpF(context, (op1) =>
{
return EmitUnaryMathCall(context, nameof(Math.Floor), op1);
return EmitUnaryMathCall(context, nameof(MathHelper.Floor), op1);
});
}
}
Expand All @@ -2265,7 +2265,7 @@ public static void Frintm_V(ArmEmitterContext context)
{
EmitVectorUnaryOpF(context, (op1) =>
{
return EmitUnaryMathCall(context, nameof(Math.Floor), op1);
return EmitUnaryMathCall(context, nameof(MathHelper.Floor), op1);
});
}
}
Expand Down Expand Up @@ -2322,7 +2322,7 @@ public static void Frintp_S(ArmEmitterContext context)
{
EmitScalarUnaryOpF(context, (op1) =>
{
return EmitUnaryMathCall(context, nameof(Math.Ceiling), op1);
return EmitUnaryMathCall(context, nameof(MathHelper.Ceiling), op1);
});
}
}
Expand All @@ -2341,7 +2341,7 @@ public static void Frintp_V(ArmEmitterContext context)
{
EmitVectorUnaryOpF(context, (op1) =>
{
return EmitUnaryMathCall(context, nameof(Math.Ceiling), op1);
return EmitUnaryMathCall(context, nameof(MathHelper.Ceiling), op1);
});
}
}
Expand Down Expand Up @@ -2390,7 +2390,7 @@ public static void Frintz_S(ArmEmitterContext context)
{
EmitScalarUnaryOpF(context, (op1) =>
{
return EmitUnaryMathCall(context, nameof(Math.Truncate), op1);
return EmitUnaryMathCall(context, nameof(MathHelper.Truncate), op1);
});
}
}
Expand All @@ -2409,7 +2409,7 @@ public static void Frintz_V(ArmEmitterContext context)
{
EmitVectorUnaryOpF(context, (op1) =>
{
return EmitUnaryMathCall(context, nameof(Math.Truncate), op1);
return EmitUnaryMathCall(context, nameof(MathHelper.Truncate), op1);
});
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/ARMeilleure/Instructions/InstEmitSimdArithmetic32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static void Vabs_S(ArmEmitterContext context)
}
else
{
EmitScalarUnaryOpF32(context, (op1) => EmitUnaryMathCall(context, nameof(Math.Abs), op1));
EmitScalarUnaryOpF32(context, (op1) => EmitUnaryMathCall(context, nameof(MathHelper.Abs), op1));
}
}

Expand All @@ -66,7 +66,7 @@ public static void Vabs_V(ArmEmitterContext context)
}
else
{
EmitVectorUnaryOpF32(context, (op1) => EmitUnaryMathCall(context, nameof(Math.Abs), op1));
EmitVectorUnaryOpF32(context, (op1) => EmitUnaryMathCall(context, nameof(MathHelper.Abs), op1));
}
}
else
Expand Down
4 changes: 2 additions & 2 deletions src/ARMeilleure/Instructions/InstEmitSimdCmp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -726,8 +726,8 @@ private static void EmitCmpOpF(ArmEmitterContext context, string name, bool scal

if (absolute)
{
ne = EmitUnaryMathCall(context, nameof(Math.Abs), ne);
me = EmitUnaryMathCall(context, nameof(Math.Abs), me);
ne = EmitUnaryMathCall(context, nameof(MathHelper.Abs), ne);
me = EmitUnaryMathCall(context, nameof(MathHelper.Abs), me);
}

Operand e = EmitSoftFloatCall(context, name, ne, me);
Expand Down
10 changes: 5 additions & 5 deletions src/ARMeilleure/Instructions/InstEmitSimdCvt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ public static void Fcvtms_Gp(ArmEmitterContext context)
}
else
{
EmitFcvt_s_Gp(context, (op1) => EmitUnaryMathCall(context, nameof(Math.Floor), op1));
EmitFcvt_s_Gp(context, (op1) => EmitUnaryMathCall(context, nameof(MathHelper.Floor), op1));
}
}

Expand All @@ -349,7 +349,7 @@ public static void Fcvtms_V(ArmEmitterContext context)
}
else
{
EmitFcvt(context, (op1) => EmitUnaryMathCall(context, nameof(Math.Floor), op1), signed: true, scalar: false);
EmitFcvt(context, (op1) => EmitUnaryMathCall(context, nameof(MathHelper.Floor), op1), signed: true, scalar: false);
}
}

Expand All @@ -365,7 +365,7 @@ public static void Fcvtmu_Gp(ArmEmitterContext context)
}
else
{
EmitFcvt_u_Gp(context, (op1) => EmitUnaryMathCall(context, nameof(Math.Floor), op1));
EmitFcvt_u_Gp(context, (op1) => EmitUnaryMathCall(context, nameof(MathHelper.Floor), op1));
}
}

Expand Down Expand Up @@ -538,7 +538,7 @@ public static void Fcvtps_Gp(ArmEmitterContext context)
}
else
{
EmitFcvt_s_Gp(context, (op1) => EmitUnaryMathCall(context, nameof(Math.Ceiling), op1));
EmitFcvt_s_Gp(context, (op1) => EmitUnaryMathCall(context, nameof(MathHelper.Ceiling), op1));
}
}

Expand All @@ -554,7 +554,7 @@ public static void Fcvtpu_Gp(ArmEmitterContext context)
}
else
{
EmitFcvt_u_Gp(context, (op1) => EmitUnaryMathCall(context, nameof(Math.Ceiling), op1));
EmitFcvt_u_Gp(context, (op1) => EmitUnaryMathCall(context, nameof(MathHelper.Ceiling), op1));
}
}

Expand Down
14 changes: 7 additions & 7 deletions src/ARMeilleure/Instructions/InstEmitSimdCvt32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -357,10 +357,10 @@ public static void Vcvt_RM(ArmEmitterContext context)
toConvert = EmitRoundMathCall(context, MidpointRounding.ToEven, toConvert);
break;
case 0b10: // Towards positive infinity
toConvert = EmitUnaryMathCall(context, nameof(Math.Ceiling), toConvert);
toConvert = EmitUnaryMathCall(context, nameof(MathHelper.Ceiling), toConvert);
break;
case 0b11: // Towards negative infinity
toConvert = EmitUnaryMathCall(context, nameof(Math.Floor), toConvert);
toConvert = EmitUnaryMathCall(context, nameof(MathHelper.Floor), toConvert);
break;
}

Expand Down Expand Up @@ -494,10 +494,10 @@ public static void Vrint_RM(ArmEmitterContext context)
toConvert = EmitRoundMathCall(context, MidpointRounding.ToEven, toConvert);
break;
case 0b10: // Towards positive infinity
toConvert = EmitUnaryMathCall(context, nameof(Math.Ceiling), toConvert);
toConvert = EmitUnaryMathCall(context, nameof(MathHelper.Ceiling), toConvert);
break;
case 0b11: // Towards negative infinity
toConvert = EmitUnaryMathCall(context, nameof(Math.Floor), toConvert);
toConvert = EmitUnaryMathCall(context, nameof(MathHelper.Floor), toConvert);
break;
}

Expand Down Expand Up @@ -534,7 +534,7 @@ public static void Vrintm_V(ArmEmitterContext context)
}
else
{
EmitVectorUnaryOpF32(context, (m) => EmitUnaryMathCall(context, nameof(Math.Floor), m));
EmitVectorUnaryOpF32(context, (m) => EmitUnaryMathCall(context, nameof(MathHelper.Floor), m));
}
}

Expand Down Expand Up @@ -574,7 +574,7 @@ public static void Vrintp_V(ArmEmitterContext context)
}
else
{
EmitVectorUnaryOpF32(context, (m) => EmitUnaryMathCall(context, nameof(Math.Ceiling), m));
EmitVectorUnaryOpF32(context, (m) => EmitUnaryMathCall(context, nameof(MathHelper.Ceiling), m));
}
}

Expand Down Expand Up @@ -613,7 +613,7 @@ public static void Vrint_Z(ArmEmitterContext context)
}
else
{
EmitScalarUnaryOpF32(context, (op1) => EmitUnaryMathCall(context, nameof(Math.Truncate), op1));
EmitScalarUnaryOpF32(context, (op1) => EmitUnaryMathCall(context, nameof(MathHelper.Truncate), op1));
}
}

Expand Down
16 changes: 8 additions & 8 deletions src/ARMeilleure/Instructions/InstEmitSimdHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -460,8 +460,8 @@ public static Operand EmitUnaryMathCall(ArmEmitterContext context, string name,
IOpCodeSimd op = (IOpCodeSimd)context.CurrOp;

MethodInfo info = (op.Size & 1) == 0
? typeof(MathF).GetMethod(name, new Type[] { typeof(float) })
: typeof(Math).GetMethod(name, new Type[] { typeof(double) });
? typeof(MathHelperF).GetMethod(name, new Type[] { typeof(float) })
: typeof(MathHelper).GetMethod(name, new Type[] { typeof(double) });

return context.Call(info, n);
}
Expand All @@ -470,11 +470,11 @@ public static Operand EmitRoundMathCall(ArmEmitterContext context, MidpointRound
{
IOpCodeSimd op = (IOpCodeSimd)context.CurrOp;

string name = nameof(Math.Round);
string name = nameof(MathHelper.Round);

MethodInfo info = (op.Size & 1) == 0
? typeof(MathF).GetMethod(name, new Type[] { typeof(float), typeof(MidpointRounding) })
: typeof(Math).GetMethod(name, new Type[] { typeof(double), typeof(MidpointRounding) });
? typeof(MathHelperF).GetMethod(name, new Type[] { typeof(float), typeof(int) })
: typeof(MathHelper).GetMethod(name, new Type[] { typeof(double), typeof(int) });

return context.Call(info, n, Const((int)roundMode));
}
Expand Down Expand Up @@ -510,16 +510,16 @@ public static Operand EmitRoundByRMode(ArmEmitterContext context, Operand op)

context.MarkLabel(lbl1);
context.BranchIf(lbl2, rMode, rP, Comparison.NotEqual);
context.Copy(res, EmitUnaryMathCall(context, nameof(Math.Ceiling), op));
context.Copy(res, EmitUnaryMathCall(context, nameof(MathHelper.Ceiling), op));
context.Branch(lblEnd);

context.MarkLabel(lbl2);
context.BranchIf(lbl3, rMode, rM, Comparison.NotEqual);
context.Copy(res, EmitUnaryMathCall(context, nameof(Math.Floor), op));
context.Copy(res, EmitUnaryMathCall(context, nameof(MathHelper.Floor), op));
context.Branch(lblEnd);

context.MarkLabel(lbl3);
context.Copy(res, EmitUnaryMathCall(context, nameof(Math.Truncate), op));
context.Copy(res, EmitUnaryMathCall(context, nameof(MathHelper.Truncate), op));
context.Branch(lblEnd);

context.MarkLabel(lblEnd);
Expand Down
75 changes: 75 additions & 0 deletions src/ARMeilleure/Instructions/MathHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace ARMeilleure.Instructions
{
static class MathHelper
{
[UnmanagedCallersOnly]
public static double Abs(double value)
{
return Math.Abs(value);
}

[UnmanagedCallersOnly]
public static double Ceiling(double value)
{
return Math.Ceiling(value);
}

[UnmanagedCallersOnly]
public static double Floor(double value)
{
return Math.Floor(value);
}

[UnmanagedCallersOnly]
public static double Round(double value, int mode)
{
return Math.Round(value, (MidpointRounding)mode);
}

[UnmanagedCallersOnly]
public static double Truncate(double value)
{
return Math.Truncate(value);
}
}

static class MathHelperF
{
[UnmanagedCallersOnly]
public static float Abs(float value)
{
return MathF.Abs(value);
}

[UnmanagedCallersOnly]
public static float Ceiling(float value)
{
return MathF.Ceiling(value);
}

[UnmanagedCallersOnly]
public static float Floor(float value)
{
return MathF.Floor(value);
}

[UnmanagedCallersOnly]
public static float Round(float value, int mode)
{
return MathF.Round(value, (MidpointRounding)mode);
}

[UnmanagedCallersOnly]
public static float Truncate(float value)
{
return MathF.Truncate(value);
}
}
}
Loading

0 comments on commit c8d598d

Please sign in to comment.