| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444 |
- using ChocolArm64.Decoder;
- using ChocolArm64.State;
- using ChocolArm64.Translation;
- using System;
- using System.Reflection.Emit;
- using static ChocolArm64.Instruction.AInstEmitSimdHelper;
- namespace ChocolArm64.Instruction
- {
- static partial class AInstEmit
- {
- public static void Fcvt_S(AILEmitterCtx Context)
- {
- AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
- EmitVectorExtractF(Context, Op.Rn, 0, Op.Size);
- EmitFloatCast(Context, Op.Opc);
- EmitScalarSetF(Context, Op.Rd, Op.Opc);
- }
- public static void Fcvtas_Gp(AILEmitterCtx Context)
- {
- Fcvta__Gp(Context, Signed: true);
- }
- public static void Fcvtau_Gp(AILEmitterCtx Context)
- {
- Fcvta__Gp(Context, Signed: false);
- }
- public static void Fcvtms_Gp(AILEmitterCtx Context)
- {
- EmitFcvt_s_Gp(Context, nameof(Math.Floor));
- }
- public static void Fcvtps_Gp(AILEmitterCtx Context)
- {
- EmitFcvt_s_Gp(Context, nameof(Math.Ceiling));
- }
- public static void Fcvtzs_Gp(AILEmitterCtx Context)
- {
- EmitFcvtz__Gp(Context, Signed: true);
- }
- public static void Fcvtzs_Gp_Fix(AILEmitterCtx Context)
- {
- EmitFcvtz__Gp_Fix(Context, Signed: true);
- }
- public static void Fcvtzs_V(AILEmitterCtx Context)
- {
- EmitVectorFcvt(Context, Signed: true);
- }
- public static void Fcvtzu_Gp(AILEmitterCtx Context)
- {
- EmitFcvtz__Gp(Context, Signed: false);
- }
- public static void Fcvtzu_Gp_Fix(AILEmitterCtx Context)
- {
- EmitFcvtz__Gp_Fix(Context, Signed: false);
- }
- public static void Fcvtzu_V(AILEmitterCtx Context)
- {
- EmitVectorFcvt(Context, Signed: false);
- }
- public static void Scvtf_Gp(AILEmitterCtx Context)
- {
- AOpCodeSimdCvt Op = (AOpCodeSimdCvt)Context.CurrOp;
- Context.EmitLdintzr(Op.Rn);
- if (Context.CurrOp.RegisterSize == ARegisterSize.Int32)
- {
- Context.Emit(OpCodes.Conv_U4);
- }
- EmitFloatCast(Context, Op.Size);
- EmitScalarSetF(Context, Op.Rd, Op.Size);
- }
- public static void Scvtf_S(AILEmitterCtx Context)
- {
- AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
- EmitVectorExtractSx(Context, Op.Rn, 0, Op.Size + 2);
- EmitFloatCast(Context, Op.Size);
- EmitScalarSetF(Context, Op.Rd, Op.Size);
- }
- public static void Scvtf_V(AILEmitterCtx Context)
- {
- EmitVectorCvtf(Context, Signed: true);
- }
- public static void Ucvtf_Gp(AILEmitterCtx Context)
- {
- AOpCodeSimdCvt Op = (AOpCodeSimdCvt)Context.CurrOp;
- Context.EmitLdintzr(Op.Rn);
- if (Context.CurrOp.RegisterSize == ARegisterSize.Int32)
- {
- Context.Emit(OpCodes.Conv_U4);
- }
- Context.Emit(OpCodes.Conv_R_Un);
- EmitFloatCast(Context, Op.Size);
- EmitScalarSetF(Context, Op.Rd, Op.Size);
- }
- public static void Ucvtf_S(AILEmitterCtx Context)
- {
- AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
- EmitVectorExtractZx(Context, Op.Rn, 0, Op.Size + 2);
- Context.Emit(OpCodes.Conv_R_Un);
- EmitFloatCast(Context, Op.Size);
- EmitScalarSetF(Context, Op.Rd, Op.Size);
- }
- public static void Ucvtf_V(AILEmitterCtx Context)
- {
- EmitVectorCvtf(Context, Signed: false);
- }
- private static int GetFBits(AILEmitterCtx Context)
- {
- if (Context.CurrOp is AOpCodeSimdShImm Op)
- {
- return GetImmShr(Op);
- }
- return 0;
- }
- private static void EmitFloatCast(AILEmitterCtx Context, int Size)
- {
- if (Size == 0)
- {
- Context.Emit(OpCodes.Conv_R4);
- }
- else if (Size == 1)
- {
- Context.Emit(OpCodes.Conv_R8);
- }
- else
- {
- throw new ArgumentOutOfRangeException(nameof(Size));
- }
- }
- private static void Fcvta__Gp(AILEmitterCtx Context, bool Signed)
- {
- AOpCodeSimdCvt Op = (AOpCodeSimdCvt)Context.CurrOp;
- EmitVectorExtractF(Context, Op.Rn, 0, Op.Size);
- EmitRoundMathCall(Context, MidpointRounding.AwayFromZero);
- if (Signed)
- {
- EmitScalarFcvts(Context, Op.Size, 0);
- }
- else
- {
- EmitScalarFcvtu(Context, Op.Size, 0);
- }
- if (Context.CurrOp.RegisterSize == ARegisterSize.Int32)
- {
- Context.Emit(OpCodes.Conv_U8);
- }
- Context.EmitStintzr(Op.Rd);
- }
- private static void EmitFcvt_s_Gp(AILEmitterCtx Context, string Name)
- {
- AOpCodeSimdCvt Op = (AOpCodeSimdCvt)Context.CurrOp;
- EmitVectorExtractF(Context, Op.Rn, 0, Op.Size);
- EmitUnaryMathCall(Context, Name);
- EmitScalarFcvts(Context, Op.Size, 0);
- if (Context.CurrOp.RegisterSize == ARegisterSize.Int32)
- {
- Context.Emit(OpCodes.Conv_U8);
- }
- Context.EmitStintzr(Op.Rd);
- }
- private static void EmitFcvtz__Gp(AILEmitterCtx Context, bool Signed)
- {
- AOpCodeSimdCvt Op = (AOpCodeSimdCvt)Context.CurrOp;
- EmitVectorExtractF(Context, Op.Rn, 0, Op.Size);
- if (Signed)
- {
- EmitScalarFcvts(Context, Op.Size, 0);
- }
- else
- {
- EmitScalarFcvtu(Context, Op.Size, 0);
- }
- if (Context.CurrOp.RegisterSize == ARegisterSize.Int32)
- {
- Context.Emit(OpCodes.Conv_U8);
- }
- Context.EmitStintzr(Op.Rd);
- }
- private static void EmitFcvtz__Gp_Fix(AILEmitterCtx Context, bool Signed)
- {
- AOpCodeSimdCvt Op = (AOpCodeSimdCvt)Context.CurrOp;
- EmitVectorExtractF(Context, Op.Rn, 0, Op.Size);
- if (Signed)
- {
- EmitScalarFcvts(Context, Op.Size, Op.FBits);
- }
- else
- {
- EmitScalarFcvtu(Context, Op.Size, Op.FBits);
- }
- if (Context.CurrOp.RegisterSize == ARegisterSize.Int32)
- {
- Context.Emit(OpCodes.Conv_U8);
- }
- Context.EmitStintzr(Op.Rd);
- }
- private static void EmitVectorCvtf(AILEmitterCtx Context, bool Signed)
- {
- AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
- int SizeF = Op.Size & 1;
- int SizeI = SizeF + 2;
- int FBits = GetFBits(Context);
- int Bytes = Context.CurrOp.GetBitsCount() >> 3;
- for (int Index = 0; Index < (Bytes >> SizeI); Index++)
- {
- EmitVectorExtract(Context, Op.Rn, Index, SizeI, Signed);
- if (!Signed)
- {
- Context.Emit(OpCodes.Conv_R_Un);
- }
- Context.Emit(SizeF == 0
- ? OpCodes.Conv_R4
- : OpCodes.Conv_R8);
- EmitI2fFBitsMul(Context, SizeF, FBits);
- EmitVectorInsertF(Context, Op.Rd, Index, SizeF);
- }
- if (Op.RegisterSize == ARegisterSize.SIMD64)
- {
- EmitVectorZeroUpper(Context, Op.Rd);
- }
- }
- private static void EmitVectorFcvt(AILEmitterCtx Context, bool Signed)
- {
- AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
- int SizeF = Op.Size & 1;
- int SizeI = SizeF + 2;
- int FBits = GetFBits(Context);
- int Bytes = Context.CurrOp.GetBitsCount() >> 3;
- for (int Index = 0; Index < (Bytes >> SizeI); Index++)
- {
- EmitVectorExtractF(Context, Op.Rn, Index, SizeF);
- EmitF2iFBitsMul(Context, SizeF, FBits);
- if (SizeF == 0)
- {
- ASoftFallback.EmitCall(Context, Signed
- ? nameof(ASoftFallback.SatF32ToS32)
- : nameof(ASoftFallback.SatF32ToU32));
- }
- else /* if (SizeF == 1) */
- {
- ASoftFallback.EmitCall(Context, Signed
- ? nameof(ASoftFallback.SatF64ToS64)
- : nameof(ASoftFallback.SatF64ToU64));
- }
- if (SizeF == 0)
- {
- Context.Emit(OpCodes.Conv_U8);
- }
- EmitVectorInsert(Context, Op.Rd, Index, SizeI);
- }
- if (Op.RegisterSize == ARegisterSize.SIMD64)
- {
- EmitVectorZeroUpper(Context, Op.Rd);
- }
- }
- private static void EmitScalarFcvts(AILEmitterCtx Context, int Size, int FBits)
- {
- if (Size < 0 || Size > 1)
- {
- throw new ArgumentOutOfRangeException(nameof(Size));
- }
- EmitF2iFBitsMul(Context, Size, FBits);
- if (Context.CurrOp.RegisterSize == ARegisterSize.Int32)
- {
- if (Size == 0)
- {
- ASoftFallback.EmitCall(Context, nameof(ASoftFallback.SatF32ToS32));
- }
- else /* if (Size == 1) */
- {
- ASoftFallback.EmitCall(Context, nameof(ASoftFallback.SatF64ToS32));
- }
- }
- else
- {
- if (Size == 0)
- {
- ASoftFallback.EmitCall(Context, nameof(ASoftFallback.SatF32ToS64));
- }
- else /* if (Size == 1) */
- {
- ASoftFallback.EmitCall(Context, nameof(ASoftFallback.SatF64ToS64));
- }
- }
- }
- private static void EmitScalarFcvtu(AILEmitterCtx Context, int Size, int FBits)
- {
- if (Size < 0 || Size > 1)
- {
- throw new ArgumentOutOfRangeException(nameof(Size));
- }
- EmitF2iFBitsMul(Context, Size, FBits);
- if (Context.CurrOp.RegisterSize == ARegisterSize.Int32)
- {
- if (Size == 0)
- {
- ASoftFallback.EmitCall(Context, nameof(ASoftFallback.SatF32ToU32));
- }
- else /* if (Size == 1) */
- {
- ASoftFallback.EmitCall(Context, nameof(ASoftFallback.SatF64ToU32));
- }
- }
- else
- {
- if (Size == 0)
- {
- ASoftFallback.EmitCall(Context, nameof(ASoftFallback.SatF32ToU64));
- }
- else /* if (Size == 1) */
- {
- ASoftFallback.EmitCall(Context, nameof(ASoftFallback.SatF64ToU64));
- }
- }
- }
- private static void EmitF2iFBitsMul(AILEmitterCtx Context, int Size, int FBits)
- {
- if (FBits != 0)
- {
- if (Size == 0)
- {
- Context.EmitLdc_R4(MathF.Pow(2, FBits));
- }
- else if (Size == 1)
- {
- Context.EmitLdc_R8(Math.Pow(2, FBits));
- }
- else
- {
- throw new ArgumentOutOfRangeException(nameof(Size));
- }
- Context.Emit(OpCodes.Mul);
- }
- }
- private static void EmitI2fFBitsMul(AILEmitterCtx Context, int Size, int FBits)
- {
- if (FBits != 0)
- {
- if (Size == 0)
- {
- Context.EmitLdc_R4(1f / MathF.Pow(2, FBits));
- }
- else if (Size == 1)
- {
- Context.EmitLdc_R8(1 / Math.Pow(2, FBits));
- }
- else
- {
- throw new ArgumentOutOfRangeException(nameof(Size));
- }
- Context.Emit(OpCodes.Mul);
- }
- }
- }
- }
|