2018-02-17 13:06:11 -08:00
|
|
|
using ChocolArm64.Decoder;
|
|
|
|
using ChocolArm64.State;
|
|
|
|
using ChocolArm64.Translation;
|
|
|
|
using System;
|
|
|
|
using System.Reflection.Emit;
|
|
|
|
|
2018-02-20 09:39:03 -08:00
|
|
|
using static ChocolArm64.Instruction.AInstEmitAluHelper;
|
2018-02-17 13:06:11 -08:00
|
|
|
using static ChocolArm64.Instruction.AInstEmitSimdHelper;
|
|
|
|
|
|
|
|
namespace ChocolArm64.Instruction
|
|
|
|
{
|
|
|
|
static partial class AInstEmit
|
|
|
|
{
|
|
|
|
public static void Cmeq_V(AILEmitterCtx Context)
|
|
|
|
{
|
|
|
|
EmitVectorCmp(Context, OpCodes.Beq_S);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void Cmge_V(AILEmitterCtx Context)
|
|
|
|
{
|
|
|
|
EmitVectorCmp(Context, OpCodes.Bge_S);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void Cmgt_V(AILEmitterCtx Context)
|
|
|
|
{
|
|
|
|
EmitVectorCmp(Context, OpCodes.Bgt_S);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void Cmhi_V(AILEmitterCtx Context)
|
|
|
|
{
|
|
|
|
EmitVectorCmp(Context, OpCodes.Bgt_Un_S);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void Cmhs_V(AILEmitterCtx Context)
|
|
|
|
{
|
|
|
|
EmitVectorCmp(Context, OpCodes.Bge_Un_S);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void Cmle_V(AILEmitterCtx Context)
|
|
|
|
{
|
|
|
|
EmitVectorCmp(Context, OpCodes.Ble_S);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void Cmlt_V(AILEmitterCtx Context)
|
|
|
|
{
|
|
|
|
EmitVectorCmp(Context, OpCodes.Blt_S);
|
|
|
|
}
|
|
|
|
|
2018-03-02 14:21:54 -08:00
|
|
|
public static void Cmtst_V(AILEmitterCtx Context)
|
|
|
|
{
|
|
|
|
AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
|
|
|
|
|
|
|
|
int Bytes = Context.CurrOp.GetBitsCount() >> 3;
|
|
|
|
|
|
|
|
ulong SzMask = ulong.MaxValue >> (64 - (8 << Op.Size));
|
|
|
|
|
|
|
|
for (int Index = 0; Index < (Bytes >> Op.Size); Index++)
|
|
|
|
{
|
|
|
|
EmitVectorExtractZx(Context, Op.Rn, Index, Op.Size);
|
|
|
|
EmitVectorExtractZx(Context, Op.Rm, Index, Op.Size);
|
|
|
|
|
|
|
|
AILLabel LblTrue = new AILLabel();
|
|
|
|
AILLabel LblEnd = new AILLabel();
|
|
|
|
|
|
|
|
Context.Emit(OpCodes.And);
|
|
|
|
|
|
|
|
Context.EmitLdc_I4(0);
|
|
|
|
|
|
|
|
Context.Emit(OpCodes.Bne_Un_S, LblTrue);
|
|
|
|
|
|
|
|
EmitVectorInsert(Context, Op.Rd, Index, Op.Size, 0);
|
|
|
|
|
|
|
|
Context.Emit(OpCodes.Br_S, LblEnd);
|
|
|
|
|
|
|
|
Context.MarkLabel(LblTrue);
|
|
|
|
|
|
|
|
EmitVectorInsert(Context, Op.Rd, Index, Op.Size, (long)SzMask);
|
|
|
|
|
|
|
|
Context.MarkLabel(LblEnd);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Op.RegisterSize == ARegisterSize.SIMD64)
|
|
|
|
{
|
|
|
|
EmitVectorZeroUpper(Context, Op.Rd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-17 13:06:11 -08:00
|
|
|
public static void Fccmp_S(AILEmitterCtx Context)
|
|
|
|
{
|
|
|
|
AOpCodeSimdFcond Op = (AOpCodeSimdFcond)Context.CurrOp;
|
|
|
|
|
|
|
|
AILLabel LblTrue = new AILLabel();
|
|
|
|
AILLabel LblEnd = new AILLabel();
|
|
|
|
|
|
|
|
Context.EmitCondBranch(LblTrue, Op.Cond);
|
|
|
|
|
2018-02-20 09:39:03 -08:00
|
|
|
EmitSetNZCV(Context, Op.NZCV);
|
2018-02-17 13:06:11 -08:00
|
|
|
|
2018-02-20 09:39:03 -08:00
|
|
|
Context.Emit(OpCodes.Br, LblEnd);
|
2018-02-17 13:06:11 -08:00
|
|
|
|
|
|
|
Context.MarkLabel(LblTrue);
|
|
|
|
|
|
|
|
Fcmp_S(Context);
|
|
|
|
|
|
|
|
Context.MarkLabel(LblEnd);
|
|
|
|
}
|
|
|
|
|
2018-02-20 09:39:03 -08:00
|
|
|
public static void Fccmpe_S(AILEmitterCtx Context)
|
|
|
|
{
|
|
|
|
Fccmp_S(Context);
|
|
|
|
}
|
|
|
|
|
2018-04-05 19:28:12 -07:00
|
|
|
public static void Fcmeq_S(AILEmitterCtx Context)
|
|
|
|
{
|
|
|
|
EmitScalarFcmp(Context, OpCodes.Beq_S);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void Fcmeq_V(AILEmitterCtx Context)
|
|
|
|
{
|
|
|
|
EmitVectorFcmp(Context, OpCodes.Beq_S);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void Fcmge_S(AILEmitterCtx Context)
|
|
|
|
{
|
|
|
|
EmitScalarFcmp(Context, OpCodes.Bge_S);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void Fcmge_V(AILEmitterCtx Context)
|
|
|
|
{
|
|
|
|
EmitVectorFcmp(Context, OpCodes.Bge_S);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void Fcmgt_S(AILEmitterCtx Context)
|
|
|
|
{
|
|
|
|
EmitScalarFcmp(Context, OpCodes.Bgt_S);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void Fcmgt_V(AILEmitterCtx Context)
|
|
|
|
{
|
|
|
|
EmitVectorFcmp(Context, OpCodes.Bgt_S);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void Fcmhi_S(AILEmitterCtx Context)
|
|
|
|
{
|
|
|
|
EmitScalarFcmp(Context, OpCodes.Bgt_Un_S);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void Fcmhi_V(AILEmitterCtx Context)
|
|
|
|
{
|
|
|
|
EmitVectorFcmp(Context, OpCodes.Bgt_Un_S);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void Fcmhs_S(AILEmitterCtx Context)
|
|
|
|
{
|
|
|
|
EmitScalarFcmp(Context, OpCodes.Bge_Un_S);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void Fcmhs_V(AILEmitterCtx Context)
|
|
|
|
{
|
|
|
|
EmitVectorFcmp(Context, OpCodes.Bge_Un_S);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void Fcmle_S(AILEmitterCtx Context)
|
|
|
|
{
|
|
|
|
EmitScalarFcmp(Context, OpCodes.Ble_S);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void Fcmle_V(AILEmitterCtx Context)
|
|
|
|
{
|
|
|
|
EmitVectorFcmp(Context, OpCodes.Ble_S);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void Fcmlt_S(AILEmitterCtx Context)
|
|
|
|
{
|
|
|
|
EmitScalarFcmp(Context, OpCodes.Blt_S);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void Fcmlt_V(AILEmitterCtx Context)
|
|
|
|
{
|
|
|
|
EmitVectorFcmp(Context, OpCodes.Blt_S);
|
|
|
|
}
|
|
|
|
|
2018-02-17 13:06:11 -08:00
|
|
|
public static void Fcmp_S(AILEmitterCtx Context)
|
|
|
|
{
|
|
|
|
AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
|
|
|
|
|
|
|
|
bool CmpWithZero = !(Op is AOpCodeSimdFcond) ? Op.Bit3 : false;
|
|
|
|
|
2018-04-05 19:28:12 -07:00
|
|
|
//Handle NaN case.
|
|
|
|
//If any number is NaN, then NZCV = 0011.
|
2018-02-20 09:39:03 -08:00
|
|
|
if (CmpWithZero)
|
|
|
|
{
|
|
|
|
EmitNaNCheck(Context, Op.Rn);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
EmitNaNCheck(Context, Op.Rn);
|
|
|
|
EmitNaNCheck(Context, Op.Rm);
|
|
|
|
|
|
|
|
Context.Emit(OpCodes.Or);
|
|
|
|
}
|
|
|
|
|
|
|
|
AILLabel LblNaN = new AILLabel();
|
|
|
|
AILLabel LblEnd = new AILLabel();
|
|
|
|
|
|
|
|
Context.Emit(OpCodes.Brtrue_S, LblNaN);
|
|
|
|
|
2018-02-17 13:06:11 -08:00
|
|
|
void EmitLoadOpers()
|
|
|
|
{
|
|
|
|
EmitVectorExtractF(Context, Op.Rn, 0, Op.Size);
|
|
|
|
|
|
|
|
if (CmpWithZero)
|
|
|
|
{
|
2018-04-05 19:28:12 -07:00
|
|
|
if (Op.Size == 0)
|
|
|
|
{
|
|
|
|
Context.EmitLdc_R4(0);
|
|
|
|
}
|
|
|
|
else /* if (SizeF == 1) */
|
|
|
|
{
|
|
|
|
Context.EmitLdc_R8(0);
|
|
|
|
}
|
2018-02-17 13:06:11 -08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
EmitVectorExtractF(Context, Op.Rm, 0, Op.Size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Z = Rn == Rm
|
|
|
|
EmitLoadOpers();
|
2018-02-20 09:39:03 -08:00
|
|
|
|
2018-02-17 13:06:11 -08:00
|
|
|
Context.Emit(OpCodes.Ceq);
|
|
|
|
Context.Emit(OpCodes.Dup);
|
|
|
|
|
|
|
|
Context.EmitStflg((int)APState.ZBit);
|
|
|
|
|
|
|
|
//C = Rn >= Rm
|
|
|
|
EmitLoadOpers();
|
|
|
|
|
|
|
|
Context.Emit(OpCodes.Cgt);
|
|
|
|
Context.Emit(OpCodes.Or);
|
|
|
|
|
|
|
|
Context.EmitStflg((int)APState.CBit);
|
|
|
|
|
|
|
|
//N = Rn < Rm
|
|
|
|
EmitLoadOpers();
|
|
|
|
|
|
|
|
Context.Emit(OpCodes.Clt);
|
|
|
|
|
|
|
|
Context.EmitStflg((int)APState.NBit);
|
|
|
|
|
2018-02-20 09:39:03 -08:00
|
|
|
//V = 0
|
|
|
|
Context.EmitLdc_I4(0);
|
2018-02-17 13:06:11 -08:00
|
|
|
|
2018-02-20 09:39:03 -08:00
|
|
|
Context.EmitStflg((int)APState.VBit);
|
2018-02-17 13:06:11 -08:00
|
|
|
|
2018-02-20 09:39:03 -08:00
|
|
|
Context.Emit(OpCodes.Br_S, LblEnd);
|
2018-02-17 13:06:11 -08:00
|
|
|
|
2018-02-20 09:39:03 -08:00
|
|
|
Context.MarkLabel(LblNaN);
|
2018-02-17 13:06:11 -08:00
|
|
|
|
2018-02-20 09:39:03 -08:00
|
|
|
EmitSetNZCV(Context, 0b0011);
|
2018-02-17 13:06:11 -08:00
|
|
|
|
2018-02-20 09:39:03 -08:00
|
|
|
Context.MarkLabel(LblEnd);
|
2018-02-17 13:06:11 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
public static void Fcmpe_S(AILEmitterCtx Context)
|
|
|
|
{
|
|
|
|
Fcmp_S(Context);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void EmitNaNCheck(AILEmitterCtx Context, int Reg)
|
|
|
|
{
|
|
|
|
IAOpCodeSimd Op = (IAOpCodeSimd)Context.CurrOp;
|
|
|
|
|
|
|
|
EmitVectorExtractF(Context, Reg, 0, Op.Size);
|
|
|
|
|
|
|
|
if (Op.Size == 0)
|
|
|
|
{
|
|
|
|
Context.EmitCall(typeof(float), nameof(float.IsNaN));
|
|
|
|
}
|
|
|
|
else if (Op.Size == 1)
|
|
|
|
{
|
|
|
|
Context.EmitCall(typeof(double), nameof(double.IsNaN));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new InvalidOperationException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void EmitVectorCmp(AILEmitterCtx Context, OpCode ILOp)
|
|
|
|
{
|
|
|
|
AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
|
|
|
|
|
|
|
|
int Bytes = Context.CurrOp.GetBitsCount() >> 3;
|
|
|
|
|
|
|
|
ulong SzMask = ulong.MaxValue >> (64 - (8 << Op.Size));
|
|
|
|
|
|
|
|
for (int Index = 0; Index < (Bytes >> Op.Size); Index++)
|
|
|
|
{
|
|
|
|
EmitVectorExtractSx(Context, Op.Rn, Index, Op.Size);
|
|
|
|
|
|
|
|
if (Op is AOpCodeSimdReg BinOp)
|
|
|
|
{
|
|
|
|
EmitVectorExtractSx(Context, BinOp.Rm, Index, Op.Size);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Context.EmitLdc_I8(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
AILLabel LblTrue = new AILLabel();
|
|
|
|
AILLabel LblEnd = new AILLabel();
|
|
|
|
|
|
|
|
Context.Emit(ILOp, LblTrue);
|
|
|
|
|
|
|
|
EmitVectorInsert(Context, Op.Rd, Index, Op.Size, 0);
|
|
|
|
|
|
|
|
Context.Emit(OpCodes.Br_S, LblEnd);
|
|
|
|
|
|
|
|
Context.MarkLabel(LblTrue);
|
|
|
|
|
|
|
|
EmitVectorInsert(Context, Op.Rd, Index, Op.Size, (long)SzMask);
|
|
|
|
|
|
|
|
Context.MarkLabel(LblEnd);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Op.RegisterSize == ARegisterSize.SIMD64)
|
|
|
|
{
|
|
|
|
EmitVectorZeroUpper(Context, Op.Rd);
|
|
|
|
}
|
|
|
|
}
|
2018-04-05 19:28:12 -07:00
|
|
|
|
|
|
|
private static void EmitScalarFcmp(AILEmitterCtx Context, OpCode ILOp)
|
|
|
|
{
|
|
|
|
AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
|
|
|
|
|
|
|
|
int SizeF = Op.Size & 1;
|
|
|
|
|
|
|
|
EmitFcmp(Context, ILOp, 0);
|
|
|
|
|
|
|
|
EmitScalarSetF(Context, Op.Rd, SizeF);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void EmitVectorFcmp(AILEmitterCtx Context, OpCode ILOp)
|
|
|
|
{
|
|
|
|
AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
|
|
|
|
|
|
|
|
int SizeF = Op.Size & 1;
|
|
|
|
|
|
|
|
int Bytes = Context.CurrOp.GetBitsCount() >> 3;
|
|
|
|
|
|
|
|
for (int Index = 0; Index < Bytes >> SizeF + 2; Index++)
|
|
|
|
{
|
|
|
|
EmitFcmp(Context, ILOp, Index);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Op.RegisterSize == ARegisterSize.SIMD64)
|
|
|
|
{
|
|
|
|
EmitVectorZeroUpper(Context, Op.Rd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void EmitFcmp(AILEmitterCtx Context, OpCode ILOp, int Index)
|
|
|
|
{
|
|
|
|
AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
|
|
|
|
|
|
|
|
int SizeF = Op.Size & 1;
|
|
|
|
|
|
|
|
ulong SzMask = ulong.MaxValue >> (64 - (32 << SizeF));
|
|
|
|
|
|
|
|
EmitVectorExtractF(Context, Op.Rn, Index, SizeF);
|
|
|
|
|
|
|
|
if (Op is AOpCodeSimdReg BinOp)
|
|
|
|
{
|
|
|
|
EmitVectorExtractF(Context, BinOp.Rm, Index, SizeF);
|
|
|
|
}
|
|
|
|
else if (SizeF == 0)
|
|
|
|
{
|
|
|
|
Context.EmitLdc_R4(0);
|
|
|
|
}
|
|
|
|
else /* if (SizeF == 1) */
|
|
|
|
{
|
|
|
|
Context.EmitLdc_R8(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
AILLabel LblTrue = new AILLabel();
|
|
|
|
AILLabel LblEnd = new AILLabel();
|
|
|
|
|
|
|
|
Context.Emit(ILOp, LblTrue);
|
|
|
|
|
|
|
|
EmitVectorInsert(Context, Op.Rd, Index, SizeF + 2, 0);
|
|
|
|
|
|
|
|
Context.Emit(OpCodes.Br_S, LblEnd);
|
|
|
|
|
|
|
|
Context.MarkLabel(LblTrue);
|
|
|
|
|
|
|
|
EmitVectorInsert(Context, Op.Rd, Index, SizeF + 2, (long)SzMask);
|
|
|
|
|
|
|
|
Context.MarkLabel(LblEnd);
|
|
|
|
}
|
2018-02-17 13:06:11 -08:00
|
|
|
}
|
|
|
|
}
|