Sse optimized the Scalar & Vector fp-to-fp conversion instructions (MNPZ & IX); added the related Tests (AMNPZ & IX). Small refactoring of existing instructions. (#676)

* Nit.

* Update InstEmitSimdCvt.cs

* Update VectorHelper.cs

* Update InstEmitSimdArithmetic.cs

* Update CpuTestSimd.cs

* Superseded.
This commit is contained in:
LDj3SNuD 2019-04-26 00:58:29 +02:00 committed by jduncanator
parent f62521ef11
commit 041a5e0301
4 changed files with 408 additions and 92 deletions

View file

@ -1382,13 +1382,10 @@ namespace ChocolArm64.Instructions
public static void Frinta_S(ILEmitterCtx context) public static void Frinta_S(ILEmitterCtx context)
{ {
OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp; EmitScalarUnaryOpF(context, () =>
{
EmitVectorExtractF(context, op.Rn, 0, op.Size);
EmitRoundMathCall(context, MidpointRounding.AwayFromZero); EmitRoundMathCall(context, MidpointRounding.AwayFromZero);
});
EmitScalarSetF(context, op.Rd, op.Size);
} }
public static void Frinta_V(ILEmitterCtx context) public static void Frinta_V(ILEmitterCtx context)
@ -1403,6 +1400,26 @@ namespace ChocolArm64.Instructions
{ {
OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp; OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
if (Optimizations.UseSse41)
{
VectorHelper.EmitCall(context, nameof(VectorHelper.VectorSingleZero));
context.EmitLdvec(op.Rn);
context.EmitLdarg(TranslatedSub.StateArgIdx);
if (op.Size == 0)
{
VectorHelper.EmitCall(context, nameof(VectorHelper.Sse41ScalarRoundF));
}
else /* if (op.Size == 1) */
{
VectorHelper.EmitCall(context, nameof(VectorHelper.Sse41ScalarRound));
}
context.EmitStvec(op.Rd);
}
else
{
EmitScalarUnaryOpF(context, () => EmitScalarUnaryOpF(context, () =>
{ {
context.EmitLdarg(TranslatedSub.StateArgIdx); context.EmitLdarg(TranslatedSub.StateArgIdx);
@ -1411,16 +1428,13 @@ namespace ChocolArm64.Instructions
{ {
VectorHelper.EmitCall(context, nameof(VectorHelper.RoundF)); VectorHelper.EmitCall(context, nameof(VectorHelper.RoundF));
} }
else if (op.Size == 1) else /* if (op.Size == 1) */
{ {
VectorHelper.EmitCall(context, nameof(VectorHelper.Round)); VectorHelper.EmitCall(context, nameof(VectorHelper.Round));
} }
else
{
throw new InvalidOperationException();
}
}); });
} }
}
public static void Frinti_V(ILEmitterCtx context) public static void Frinti_V(ILEmitterCtx context)
{ {
@ -1428,6 +1442,30 @@ namespace ChocolArm64.Instructions
int sizeF = op.Size & 1; int sizeF = op.Size & 1;
if (Optimizations.UseSse41)
{
context.EmitLdvec(op.Rn);
context.EmitLdarg(TranslatedSub.StateArgIdx);
if (sizeF == 0)
{
VectorHelper.EmitCall(context, nameof(VectorHelper.Sse41VectorRoundF));
}
else /* if (sizeF == 1) */
{
VectorHelper.EmitCall(context, nameof(VectorHelper.Sse41VectorRound));
}
context.EmitStvec(op.Rd);
if (sizeF == 0 && op.RegisterSize == RegisterSize.Simd64)
{
EmitVectorZeroUpper(context, op.Rd);
}
}
else
{
EmitVectorUnaryOpF(context, () => EmitVectorUnaryOpF(context, () =>
{ {
context.EmitLdarg(TranslatedSub.StateArgIdx); context.EmitLdarg(TranslatedSub.StateArgIdx);
@ -1436,72 +1474,128 @@ namespace ChocolArm64.Instructions
{ {
VectorHelper.EmitCall(context, nameof(VectorHelper.RoundF)); VectorHelper.EmitCall(context, nameof(VectorHelper.RoundF));
} }
else if (sizeF == 1) else /* if (sizeF == 1) */
{ {
VectorHelper.EmitCall(context, nameof(VectorHelper.Round)); VectorHelper.EmitCall(context, nameof(VectorHelper.Round));
} }
else
{
throw new InvalidOperationException();
}
}); });
} }
}
public static void Frintm_S(ILEmitterCtx context) public static void Frintm_S(ILEmitterCtx context)
{
if (Optimizations.UseSse41)
{
EmitSse41Frint(context, RoundMode.TowardsMinusInfinity, scalar: true);
}
else
{ {
EmitScalarUnaryOpF(context, () => EmitScalarUnaryOpF(context, () =>
{ {
EmitUnaryMathCall(context, nameof(Math.Floor)); EmitUnaryMathCall(context, nameof(Math.Floor));
}); });
} }
}
public static void Frintm_V(ILEmitterCtx context) public static void Frintm_V(ILEmitterCtx context)
{
if (Optimizations.UseSse41)
{
EmitSse41Frint(context, RoundMode.TowardsMinusInfinity, scalar: false);
}
else
{ {
EmitVectorUnaryOpF(context, () => EmitVectorUnaryOpF(context, () =>
{ {
EmitUnaryMathCall(context, nameof(Math.Floor)); EmitUnaryMathCall(context, nameof(Math.Floor));
}); });
} }
}
public static void Frintn_S(ILEmitterCtx context) public static void Frintn_S(ILEmitterCtx context)
{ {
OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp; if (Optimizations.UseSse41)
{
EmitVectorExtractF(context, op.Rn, 0, op.Size); EmitSse41Frint(context, RoundMode.ToNearest, scalar: true);
}
else
{
EmitScalarUnaryOpF(context, () =>
{
EmitRoundMathCall(context, MidpointRounding.ToEven); EmitRoundMathCall(context, MidpointRounding.ToEven);
});
EmitScalarSetF(context, op.Rd, op.Size); }
} }
public static void Frintn_V(ILEmitterCtx context) public static void Frintn_V(ILEmitterCtx context)
{
if (Optimizations.UseSse41)
{
EmitSse41Frint(context, RoundMode.ToNearest, scalar: false);
}
else
{ {
EmitVectorUnaryOpF(context, () => EmitVectorUnaryOpF(context, () =>
{ {
EmitRoundMathCall(context, MidpointRounding.ToEven); EmitRoundMathCall(context, MidpointRounding.ToEven);
}); });
} }
}
public static void Frintp_S(ILEmitterCtx context) public static void Frintp_S(ILEmitterCtx context)
{
if (Optimizations.UseSse41)
{
EmitSse41Frint(context, RoundMode.TowardsPlusInfinity, scalar: true);
}
else
{ {
EmitScalarUnaryOpF(context, () => EmitScalarUnaryOpF(context, () =>
{ {
EmitUnaryMathCall(context, nameof(Math.Ceiling)); EmitUnaryMathCall(context, nameof(Math.Ceiling));
}); });
} }
}
public static void Frintp_V(ILEmitterCtx context) public static void Frintp_V(ILEmitterCtx context)
{
if (Optimizations.UseSse41)
{
EmitSse41Frint(context, RoundMode.TowardsPlusInfinity, scalar: false);
}
else
{ {
EmitVectorUnaryOpF(context, () => EmitVectorUnaryOpF(context, () =>
{ {
EmitUnaryMathCall(context, nameof(Math.Ceiling)); EmitUnaryMathCall(context, nameof(Math.Ceiling));
}); });
} }
}
public static void Frintx_S(ILEmitterCtx context) public static void Frintx_S(ILEmitterCtx context)
{ {
OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp; OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
if (Optimizations.UseSse41)
{
VectorHelper.EmitCall(context, nameof(VectorHelper.VectorSingleZero));
context.EmitLdvec(op.Rn);
context.EmitLdarg(TranslatedSub.StateArgIdx);
if (op.Size == 0)
{
VectorHelper.EmitCall(context, nameof(VectorHelper.Sse41ScalarRoundF));
}
else /* if (op.Size == 1) */
{
VectorHelper.EmitCall(context, nameof(VectorHelper.Sse41ScalarRound));
}
context.EmitStvec(op.Rd);
}
else
{
EmitScalarUnaryOpF(context, () => EmitScalarUnaryOpF(context, () =>
{ {
context.EmitLdarg(TranslatedSub.StateArgIdx); context.EmitLdarg(TranslatedSub.StateArgIdx);
@ -1510,55 +1604,89 @@ namespace ChocolArm64.Instructions
{ {
VectorHelper.EmitCall(context, nameof(VectorHelper.RoundF)); VectorHelper.EmitCall(context, nameof(VectorHelper.RoundF));
} }
else if (op.Size == 1) else /* if (op.Size == 1) */
{ {
VectorHelper.EmitCall(context, nameof(VectorHelper.Round)); VectorHelper.EmitCall(context, nameof(VectorHelper.Round));
} }
else
{
throw new InvalidOperationException();
}
}); });
} }
}
public static void Frintx_V(ILEmitterCtx context) public static void Frintx_V(ILEmitterCtx context)
{ {
OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp; OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
int sizeF = op.Size & 1;
if (Optimizations.UseSse41)
{
context.EmitLdvec(op.Rn);
context.EmitLdarg(TranslatedSub.StateArgIdx);
if (sizeF == 0)
{
VectorHelper.EmitCall(context, nameof(VectorHelper.Sse41VectorRoundF));
}
else /* if (sizeF == 1) */
{
VectorHelper.EmitCall(context, nameof(VectorHelper.Sse41VectorRound));
}
context.EmitStvec(op.Rd);
if (sizeF == 0 && op.RegisterSize == RegisterSize.Simd64)
{
EmitVectorZeroUpper(context, op.Rd);
}
}
else
{
EmitVectorUnaryOpF(context, () => EmitVectorUnaryOpF(context, () =>
{ {
context.EmitLdarg(TranslatedSub.StateArgIdx); context.EmitLdarg(TranslatedSub.StateArgIdx);
if (op.Size == 0) if (sizeF == 0)
{ {
VectorHelper.EmitCall(context, nameof(VectorHelper.RoundF)); VectorHelper.EmitCall(context, nameof(VectorHelper.RoundF));
} }
else if (op.Size == 1) else /* if (sizeF == 1) */
{ {
VectorHelper.EmitCall(context, nameof(VectorHelper.Round)); VectorHelper.EmitCall(context, nameof(VectorHelper.Round));
} }
else
{
throw new InvalidOperationException();
}
}); });
} }
}
public static void Frintz_S(ILEmitterCtx context) public static void Frintz_S(ILEmitterCtx context)
{
if (Optimizations.UseSse41)
{
EmitSse41Frint(context, RoundMode.TowardsZero, scalar: true);
}
else
{ {
EmitScalarUnaryOpF(context, () => EmitScalarUnaryOpF(context, () =>
{ {
EmitUnaryMathCall(context, nameof(Math.Truncate)); EmitUnaryMathCall(context, nameof(Math.Truncate));
}); });
} }
}
public static void Frintz_V(ILEmitterCtx context) public static void Frintz_V(ILEmitterCtx context)
{
if (Optimizations.UseSse41)
{
EmitSse41Frint(context, RoundMode.TowardsZero, scalar: false);
}
else
{ {
EmitVectorUnaryOpF(context, () => EmitVectorUnaryOpF(context, () =>
{ {
EmitUnaryMathCall(context, nameof(Math.Truncate)); EmitUnaryMathCall(context, nameof(Math.Truncate));
}); });
} }
}
public static void Frsqrte_S(ILEmitterCtx context) public static void Frsqrte_S(ILEmitterCtx context)
{ {
@ -3542,6 +3670,44 @@ namespace ChocolArm64.Instructions
} }
} }
private static void EmitSse41Frint(ILEmitterCtx context, RoundMode roundMode, bool scalar)
{
OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
if (scalar)
{
Type[] typesRnd = op.Size == 0
? new Type[] { typeof(Vector128<float>), typeof(Vector128<float>) }
: new Type[] { typeof(Vector128<double>), typeof(Vector128<double>) };
VectorHelper.EmitCall(context, nameof(VectorHelper.VectorSingleZero));
context.EmitLdvec(op.Rn);
context.EmitCall(typeof(Sse41).GetMethod(GetScalarSse41NameRnd(roundMode), typesRnd));
context.EmitStvec(op.Rd);
}
else
{
int sizeF = op.Size & 1;
Type[] typesRnd = sizeF == 0
? new Type[] { typeof(Vector128<float>) }
: new Type[] { typeof(Vector128<double>) };
context.EmitLdvec(op.Rn);
context.EmitCall(typeof(Sse41).GetMethod(GetVectorSse41NameRnd(roundMode), typesRnd));
context.EmitStvec(op.Rd);
if (sizeF == 0 && op.RegisterSize == RegisterSize.Simd64)
{
EmitVectorZeroUpper(context, op.Rd);
}
}
}
private static void EmitSse41Mul_AddSub(ILEmitterCtx context, string nameAddSub = null) private static void EmitSse41Mul_AddSub(ILEmitterCtx context, string nameAddSub = null)
{ {
OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp; OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;

View file

@ -898,7 +898,7 @@ namespace ChocolArm64.Instructions
context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.Multiply), types)); context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.Multiply), types));
} }
context.EmitCall(typeof(Sse41).GetMethod(GetSse41NameRnd(roundMode), typesRndCvt)); context.EmitCall(typeof(Sse41).GetMethod(GetVectorSse41NameRnd(roundMode), typesRndCvt));
context.EmitStvectmp(); context.EmitStvectmp();
context.EmitLdvectmp(); context.EmitLdvectmp();
@ -954,7 +954,7 @@ namespace ChocolArm64.Instructions
context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Multiply), types)); context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Multiply), types));
} }
context.EmitCall(typeof(Sse41).GetMethod(GetSse41NameRnd(roundMode), typesRndCvt)); context.EmitCall(typeof(Sse41).GetMethod(GetVectorSse41NameRnd(roundMode), typesRndCvt));
context.EmitStvectmp(); context.EmitStvectmp();
@ -1032,7 +1032,7 @@ namespace ChocolArm64.Instructions
context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.Multiply), types)); context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.Multiply), types));
} }
context.EmitCall(typeof(Sse41).GetMethod(GetSse41NameRnd(roundMode), typesRndCvt)); context.EmitCall(typeof(Sse41).GetMethod(GetVectorSse41NameRnd(roundMode), typesRndCvt));
context.Emit(OpCodes.Dup); context.Emit(OpCodes.Dup);
@ -1120,7 +1120,7 @@ namespace ChocolArm64.Instructions
context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Multiply), types)); context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Multiply), types));
} }
context.EmitCall(typeof(Sse41).GetMethod(GetSse41NameRnd(roundMode), typesRndCvt)); context.EmitCall(typeof(Sse41).GetMethod(GetVectorSse41NameRnd(roundMode), typesRndCvt));
context.Emit(OpCodes.Dup); context.Emit(OpCodes.Dup);
@ -1305,23 +1305,39 @@ namespace ChocolArm64.Instructions
} }
} }
private static string GetSse41NameRnd(RoundMode roundMode) private static string GetScalarSse41NameRnd(RoundMode roundMode)
{
switch (roundMode)
{
case RoundMode.ToNearest:
return nameof(Sse41.RoundToNearestIntegerScalar); // even
case RoundMode.TowardsPlusInfinity:
return nameof(Sse41.RoundToPositiveInfinityScalar);
case RoundMode.TowardsMinusInfinity:
return nameof(Sse41.RoundToNegativeInfinityScalar);
default: /* case RoundMode.TowardsZero: */
return nameof(Sse41.RoundToZeroScalar);
}
}
private static string GetVectorSse41NameRnd(RoundMode roundMode)
{ {
switch (roundMode) switch (roundMode)
{ {
case RoundMode.ToNearest: case RoundMode.ToNearest:
return nameof(Sse41.RoundToNearestInteger); // even return nameof(Sse41.RoundToNearestInteger); // even
case RoundMode.TowardsMinusInfinity:
return nameof(Sse41.RoundToNegativeInfinity);
case RoundMode.TowardsPlusInfinity: case RoundMode.TowardsPlusInfinity:
return nameof(Sse41.RoundToPositiveInfinity); return nameof(Sse41.RoundToPositiveInfinity);
case RoundMode.TowardsZero: case RoundMode.TowardsMinusInfinity:
return nameof(Sse41.RoundToZero); return nameof(Sse41.RoundToNegativeInfinity);
default: throw new ArgumentException(nameof(roundMode)); default: /* case RoundMode.TowardsZero: */
return nameof(Sse41.RoundToZero);
} }
} }
} }

View file

@ -237,7 +237,9 @@ namespace ChocolArm64.Instructions
{ {
IOpCodeSimd64 op = (IOpCodeSimd64)context.CurrOp; IOpCodeSimd64 op = (IOpCodeSimd64)context.CurrOp;
Type type = (op.Size & 1) == 0 int sizeF = op.Size & 1;
Type type = sizeF == 0
? typeof(SoftFloat32) ? typeof(SoftFloat32)
: typeof(SoftFloat64); : typeof(SoftFloat64);

View file

@ -93,30 +93,162 @@ namespace ChocolArm64.Instructions
value <= ulong.MinValue ? ulong.MinValue : (ulong)value; value <= ulong.MinValue ? ulong.MinValue : (ulong)value;
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double Round(double value, CpuThreadState state) public static double Round(double value, CpuThreadState state)
{ {
switch (state.FPRoundingMode()) RoundMode roundMode = state.FPRoundingMode();
if (roundMode == RoundMode.ToNearest)
{ {
case RoundMode.ToNearest: return Math.Round (value); return Math.Round(value); // even
case RoundMode.TowardsPlusInfinity: return Math.Ceiling (value); }
case RoundMode.TowardsMinusInfinity: return Math.Floor (value); else if (roundMode == RoundMode.TowardsPlusInfinity)
case RoundMode.TowardsZero: return Math.Truncate(value); {
} return Math.Ceiling(value);
}
throw new InvalidOperationException(); else if (roundMode == RoundMode.TowardsMinusInfinity)
{
return Math.Floor(value);
}
else /* if (roundMode == RoundMode.TowardsZero) */
{
return Math.Truncate(value);
}
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float RoundF(float value, CpuThreadState state) public static float RoundF(float value, CpuThreadState state)
{ {
switch (state.FPRoundingMode()) RoundMode roundMode = state.FPRoundingMode();
if (roundMode == RoundMode.ToNearest)
{ {
case RoundMode.ToNearest: return MathF.Round (value); return MathF.Round(value); // even
case RoundMode.TowardsPlusInfinity: return MathF.Ceiling (value); }
case RoundMode.TowardsMinusInfinity: return MathF.Floor (value); else if (roundMode == RoundMode.TowardsPlusInfinity)
case RoundMode.TowardsZero: return MathF.Truncate(value); {
return MathF.Ceiling(value);
}
else if (roundMode == RoundMode.TowardsMinusInfinity)
{
return MathF.Floor(value);
}
else /* if (roundMode == RoundMode.TowardsZero) */
{
return MathF.Truncate(value);
}
} }
throw new InvalidOperationException(); [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector128<double> Sse41ScalarRound(Vector128<double> upper, Vector128<double> value, CpuThreadState state)
{
if (!Sse41.IsSupported)
{
throw new PlatformNotSupportedException();
}
RoundMode roundMode = state.FPRoundingMode();
if (roundMode == RoundMode.ToNearest)
{
return Sse41.RoundToNearestIntegerScalar(upper, value); // even
}
else if (roundMode == RoundMode.TowardsPlusInfinity)
{
return Sse41.RoundToPositiveInfinityScalar(upper, value);
}
else if (roundMode == RoundMode.TowardsMinusInfinity)
{
return Sse41.RoundToNegativeInfinityScalar(upper, value);
}
else /* if (roundMode == RoundMode.TowardsZero) */
{
return Sse41.RoundToZeroScalar(upper, value);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector128<float> Sse41ScalarRoundF(Vector128<float> upper, Vector128<float> value, CpuThreadState state)
{
if (!Sse41.IsSupported)
{
throw new PlatformNotSupportedException();
}
RoundMode roundMode = state.FPRoundingMode();
if (roundMode == RoundMode.ToNearest)
{
return Sse41.RoundToNearestIntegerScalar(upper, value); // even
}
else if (roundMode == RoundMode.TowardsPlusInfinity)
{
return Sse41.RoundToPositiveInfinityScalar(upper, value);
}
else if (roundMode == RoundMode.TowardsMinusInfinity)
{
return Sse41.RoundToNegativeInfinityScalar(upper, value);
}
else /* if (roundMode == RoundMode.TowardsZero) */
{
return Sse41.RoundToZeroScalar(upper, value);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector128<double> Sse41VectorRound(Vector128<double> value, CpuThreadState state)
{
if (!Sse41.IsSupported)
{
throw new PlatformNotSupportedException();
}
RoundMode roundMode = state.FPRoundingMode();
if (roundMode == RoundMode.ToNearest)
{
return Sse41.RoundToNearestInteger(value); // even
}
else if (roundMode == RoundMode.TowardsPlusInfinity)
{
return Sse41.RoundToPositiveInfinity(value);
}
else if (roundMode == RoundMode.TowardsMinusInfinity)
{
return Sse41.RoundToNegativeInfinity(value);
}
else /* if (roundMode == RoundMode.TowardsZero) */
{
return Sse41.RoundToZero(value);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector128<float> Sse41VectorRoundF(Vector128<float> value, CpuThreadState state)
{
if (!Sse41.IsSupported)
{
throw new PlatformNotSupportedException();
}
RoundMode roundMode = state.FPRoundingMode();
if (roundMode == RoundMode.ToNearest)
{
return Sse41.RoundToNearestInteger(value); // even
}
else if (roundMode == RoundMode.TowardsPlusInfinity)
{
return Sse41.RoundToPositiveInfinity(value);
}
else if (roundMode == RoundMode.TowardsMinusInfinity)
{
return Sse41.RoundToNegativeInfinity(value);
}
else /* if (roundMode == RoundMode.TowardsZero) */
{
return Sse41.RoundToZero(value);
}
} }
public static Vector128<float> Tbl1_V64( public static Vector128<float> Tbl1_V64(