| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344 |
- using ChocolArm64.Decoders;
- using ChocolArm64.State;
- using ChocolArm64.Translation;
- using System;
- using System.Reflection.Emit;
- using System.Runtime.Intrinsics;
- using System.Runtime.Intrinsics.X86;
- using static ChocolArm64.Instructions.InstEmitSimdHelper;
- namespace ChocolArm64.Instructions
- {
- static partial class InstEmit
- {
- public static void Fcvt_S(ILEmitterCtx context)
- {
- OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
- if (Optimizations.UseSse2)
- {
- if (op.Size == 1 && op.Opc == 0)
- {
- //Double -> Single.
- Type[] typesCvt = new Type[] { typeof(Vector128<float>), typeof(Vector128<double>) };
- VectorHelper.EmitCall(context, nameof(VectorHelper.VectorSingleZero));
- context.EmitLdvec(op.Rn);
- context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ConvertScalarToVector128Single), typesCvt));
- context.EmitStvec(op.Rd);
- }
- else if (op.Size == 0 && op.Opc == 1)
- {
- //Single -> Double.
- Type[] typesCvt = new Type[] { typeof(Vector128<double>), typeof(Vector128<float>) };
- VectorHelper.EmitCall(context, nameof(VectorHelper.VectorSingleZero));
- context.EmitLdvec(op.Rn);
- context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ConvertScalarToVector128Double), typesCvt));
- context.EmitStvec(op.Rd);
- }
- else
- {
- //Invalid encoding.
- throw new InvalidOperationException();
- }
- }
- else
- {
- EmitVectorExtractF(context, op.Rn, 0, op.Size);
- EmitFloatCast(context, op.Opc);
- EmitScalarSetF(context, op.Rd, op.Opc);
- }
- }
- public static void Fcvtas_Gp(ILEmitterCtx context)
- {
- EmitFcvt_s_Gp(context, () => EmitRoundMathCall(context, MidpointRounding.AwayFromZero));
- }
- public static void Fcvtau_Gp(ILEmitterCtx context)
- {
- EmitFcvt_u_Gp(context, () => EmitRoundMathCall(context, MidpointRounding.AwayFromZero));
- }
- public static void Fcvtl_V(ILEmitterCtx context)
- {
- OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
- int sizeF = op.Size & 1;
- if (Optimizations.UseSse2 && sizeF == 1)
- {
- Type[] typesCvt = new Type[] { typeof(Vector128<float>) };
- context.EmitLdvec(op.Rn);
- if (op.RegisterSize == RegisterSize.Simd128)
- {
- context.EmitLdvec(op.Rn);
- context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.MoveHighToLow)));
- }
- context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ConvertToVector128Double), typesCvt));
- context.EmitStvec(op.Rd);
- }
- else
- {
- int elems = 4 >> sizeF;
- int part = op.RegisterSize == RegisterSize.Simd128 ? elems : 0;
- for (int index = 0; index < elems; index++)
- {
- if (sizeF == 0)
- {
- EmitVectorExtractZx(context, op.Rn, part + index, 1);
- context.Emit(OpCodes.Conv_U2);
- context.EmitLdarg(TranslatedSub.StateArgIdx);
- context.EmitCall(typeof(SoftFloat16_32), nameof(SoftFloat16_32.FPConvert));
- }
- else /* if (sizeF == 1) */
- {
- EmitVectorExtractF(context, op.Rn, part + index, 0);
- context.Emit(OpCodes.Conv_R8);
- }
- EmitVectorInsertTmpF(context, index, sizeF);
- }
- context.EmitLdvectmp();
- context.EmitStvec(op.Rd);
- }
- }
- public static void Fcvtms_Gp(ILEmitterCtx context)
- {
- EmitFcvt_s_Gp(context, () => EmitUnaryMathCall(context, nameof(Math.Floor)));
- }
- public static void Fcvtmu_Gp(ILEmitterCtx context)
- {
- EmitFcvt_u_Gp(context, () => EmitUnaryMathCall(context, nameof(Math.Floor)));
- }
- public static void Fcvtn_V(ILEmitterCtx context)
- {
- OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
- int sizeF = op.Size & 1;
- if (Optimizations.UseSse2 && sizeF == 1)
- {
- Type[] typesCvt = new Type[] { typeof(Vector128<double>) };
- string nameMov = op.RegisterSize == RegisterSize.Simd128
- ? nameof(Sse.MoveLowToHigh)
- : nameof(Sse.MoveHighToLow);
- context.EmitLdvec(op.Rd);
- VectorHelper.EmitCall(context, nameof(VectorHelper.VectorSingleZero));
- context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.MoveLowToHigh)));
- context.EmitLdvec(op.Rn);
- context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ConvertToVector128Single), typesCvt));
- context.Emit(OpCodes.Dup);
- context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.MoveLowToHigh)));
- context.EmitCall(typeof(Sse).GetMethod(nameMov));
- context.EmitStvec(op.Rd);
- }
- else
- {
- int elems = 4 >> sizeF;
- int part = op.RegisterSize == RegisterSize.Simd128 ? elems : 0;
- if (part != 0)
- {
- context.EmitLdvec(op.Rd);
- context.EmitStvectmp();
- }
- for (int index = 0; index < elems; index++)
- {
- EmitVectorExtractF(context, op.Rn, index, sizeF);
- if (sizeF == 0)
- {
- context.EmitLdarg(TranslatedSub.StateArgIdx);
- context.EmitCall(typeof(SoftFloat32_16), nameof(SoftFloat32_16.FPConvert));
- context.Emit(OpCodes.Conv_U8);
- EmitVectorInsertTmp(context, part + index, 1);
- }
- else /* if (sizeF == 1) */
- {
- context.Emit(OpCodes.Conv_R4);
- EmitVectorInsertTmpF(context, part + index, 0);
- }
- }
- context.EmitLdvectmp();
- context.EmitStvec(op.Rd);
- if (part == 0)
- {
- EmitVectorZeroUpper(context, op.Rd);
- }
- }
- }
- public static void Fcvtns_S(ILEmitterCtx context)
- {
- if (Optimizations.UseSse41)
- {
- EmitSse41Fcvt_Signed(context, RoundMode.ToNearest, scalar: true);
- }
- else
- {
- EmitFcvtn(context, signed: true, scalar: true);
- }
- }
- public static void Fcvtns_V(ILEmitterCtx context)
- {
- if (Optimizations.UseSse41)
- {
- EmitSse41Fcvt_Signed(context, RoundMode.ToNearest, scalar: false);
- }
- else
- {
- EmitFcvtn(context, signed: true, scalar: false);
- }
- }
- public static void Fcvtnu_S(ILEmitterCtx context)
- {
- if (Optimizations.UseSse41)
- {
- EmitSse41Fcvt_Unsigned(context, RoundMode.ToNearest, scalar: true);
- }
- else
- {
- EmitFcvtn(context, signed: false, scalar: true);
- }
- }
- public static void Fcvtnu_V(ILEmitterCtx context)
- {
- if (Optimizations.UseSse41)
- {
- EmitSse41Fcvt_Unsigned(context, RoundMode.ToNearest, scalar: false);
- }
- else
- {
- EmitFcvtn(context, signed: false, scalar: false);
- }
- }
- public static void Fcvtps_Gp(ILEmitterCtx context)
- {
- EmitFcvt_s_Gp(context, () => EmitUnaryMathCall(context, nameof(Math.Ceiling)));
- }
- public static void Fcvtpu_Gp(ILEmitterCtx context)
- {
- EmitFcvt_u_Gp(context, () => EmitUnaryMathCall(context, nameof(Math.Ceiling)));
- }
- public static void Fcvtzs_Gp(ILEmitterCtx context)
- {
- EmitFcvt_s_Gp(context, () => { });
- }
- public static void Fcvtzs_Gp_Fixed(ILEmitterCtx context)
- {
- EmitFcvtzs_Gp_Fixed(context);
- }
- public static void Fcvtzs_S(ILEmitterCtx context)
- {
- if (Optimizations.UseSse41)
- {
- EmitSse41Fcvt_Signed(context, RoundMode.TowardsZero, scalar: true);
- }
- else
- {
- EmitFcvtz(context, signed: true, scalar: true);
- }
- }
- public static void Fcvtzs_V(ILEmitterCtx context)
- {
- if (Optimizations.UseSse41)
- {
- EmitSse41Fcvt_Signed(context, RoundMode.TowardsZero, scalar: false);
- }
- else
- {
- EmitFcvtz(context, signed: true, scalar: false);
- }
- }
- public static void Fcvtzs_V_Fixed(ILEmitterCtx context)
- {
- if (Optimizations.UseSse41)
- {
- EmitSse41Fcvt_Signed(context, RoundMode.TowardsZero, scalar: false);
- }
- else
- {
- EmitFcvtz(context, signed: true, scalar: false);
- }
- }
- public static void Fcvtzu_Gp(ILEmitterCtx context)
- {
- EmitFcvt_u_Gp(context, () => { });
- }
- public static void Fcvtzu_Gp_Fixed(ILEmitterCtx context)
- {
- EmitFcvtzu_Gp_Fixed(context);
- }
- public static void Fcvtzu_S(ILEmitterCtx context)
- {
- if (Optimizations.UseSse41)
- {
- EmitSse41Fcvt_Unsigned(context, RoundMode.TowardsZero, scalar: true);
- }
- else
- {
- EmitFcvtz(context, signed: false, scalar: true);
- }
- }
- public static void Fcvtzu_V(ILEmitterCtx context)
- {
- if (Optimizations.UseSse41)
- {
- EmitSse41Fcvt_Unsigned(context, RoundMode.TowardsZero, scalar: false);
- }
- else
- {
- EmitFcvtz(context, signed: false, scalar: false);
- }
- }
- public static void Fcvtzu_V_Fixed(ILEmitterCtx context)
- {
- if (Optimizations.UseSse41)
- {
- EmitSse41Fcvt_Unsigned(context, RoundMode.TowardsZero, scalar: false);
- }
- else
- {
- EmitFcvtz(context, signed: false, scalar: false);
- }
- }
- public static void Scvtf_Gp(ILEmitterCtx context)
- {
- OpCodeSimdCvt64 op = (OpCodeSimdCvt64)context.CurrOp;
- context.EmitLdintzr(op.Rn);
- if (context.CurrOp.RegisterSize == RegisterSize.Int32)
- {
- context.Emit(OpCodes.Conv_I4);
- }
- EmitFloatCast(context, op.Size);
- EmitScalarSetF(context, op.Rd, op.Size);
- }
- public static void Scvtf_Gp_Fixed(ILEmitterCtx context)
- {
- OpCodeSimdCvt64 op = (OpCodeSimdCvt64)context.CurrOp;
- context.EmitLdintzr(op.Rn);
- if (context.CurrOp.RegisterSize == RegisterSize.Int32)
- {
- context.Emit(OpCodes.Conv_I4);
- }
- EmitFloatCast(context, op.Size);
- EmitI2fFBitsMul(context, op.Size, op.FBits);
- EmitScalarSetF(context, op.Rd, op.Size);
- }
- public static void Scvtf_S(ILEmitterCtx context)
- {
- OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
- int sizeF = op.Size & 1;
- if (Optimizations.UseSse2 && sizeF == 0)
- {
- EmitSse2cvtF_Signed(context, scalar: true);
- }
- else
- {
- EmitVectorExtractSx(context, op.Rn, 0, sizeF + 2);
- EmitFloatCast(context, sizeF);
- EmitScalarSetF(context, op.Rd, sizeF);
- }
- }
- public static void Scvtf_V(ILEmitterCtx context)
- {
- OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
- int sizeF = op.Size & 1;
- if (Optimizations.UseSse2 && sizeF == 0)
- {
- EmitSse2cvtF_Signed(context, scalar: false);
- }
- else
- {
- EmitVectorCvtf(context, signed: true);
- }
- }
- public static void Scvtf_V_Fixed(ILEmitterCtx context)
- {
- OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
- // sizeF == ((OpCodeSimdShImm64)op).Size - 2
- int sizeF = op.Size & 1;
- if (Optimizations.UseSse2 && sizeF == 0)
- {
- EmitSse2cvtF_Signed(context, scalar: false);
- }
- else
- {
- EmitVectorCvtf(context, signed: true);
- }
- }
- public static void Ucvtf_Gp(ILEmitterCtx context)
- {
- OpCodeSimdCvt64 op = (OpCodeSimdCvt64)context.CurrOp;
- context.EmitLdintzr(op.Rn);
- if (context.CurrOp.RegisterSize == RegisterSize.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_Gp_Fixed(ILEmitterCtx context)
- {
- OpCodeSimdCvt64 op = (OpCodeSimdCvt64)context.CurrOp;
- context.EmitLdintzr(op.Rn);
- if (context.CurrOp.RegisterSize == RegisterSize.Int32)
- {
- context.Emit(OpCodes.Conv_U4);
- }
- context.Emit(OpCodes.Conv_R_Un);
- EmitFloatCast(context, op.Size);
- EmitI2fFBitsMul(context, op.Size, op.FBits);
- EmitScalarSetF(context, op.Rd, op.Size);
- }
- public static void Ucvtf_S(ILEmitterCtx context)
- {
- OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
- int sizeF = op.Size & 1;
- if (Optimizations.UseSse2 && sizeF == 0)
- {
- EmitSse2cvtF_Unsigned(context, scalar: true);
- }
- else
- {
- EmitVectorExtractZx(context, op.Rn, 0, sizeF + 2);
- context.Emit(OpCodes.Conv_R_Un);
- EmitFloatCast(context, sizeF);
- EmitScalarSetF(context, op.Rd, sizeF);
- }
- }
- public static void Ucvtf_V(ILEmitterCtx context)
- {
- OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
- int sizeF = op.Size & 1;
- if (Optimizations.UseSse2 && sizeF == 0)
- {
- EmitSse2cvtF_Unsigned(context, scalar: false);
- }
- else
- {
- EmitVectorCvtf(context, signed: false);
- }
- }
- public static void Ucvtf_V_Fixed(ILEmitterCtx context)
- {
- OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
- // sizeF == ((OpCodeSimdShImm64)op).Size - 2
- int sizeF = op.Size & 1;
- if (Optimizations.UseSse2 && sizeF == 0)
- {
- EmitSse2cvtF_Unsigned(context, scalar: false);
- }
- else
- {
- EmitVectorCvtf(context, signed: false);
- }
- }
- private static void EmitFcvtn(ILEmitterCtx context, bool signed, bool scalar)
- {
- OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
- int sizeF = op.Size & 1;
- int sizeI = sizeF + 2;
- int bytes = op.GetBitsCount() >> 3;
- int elems = !scalar ? bytes >> sizeI : 1;
- for (int index = 0; index < elems; index++)
- {
- EmitVectorExtractF(context, op.Rn, index, sizeF);
- EmitRoundMathCall(context, MidpointRounding.ToEven);
- if (sizeF == 0)
- {
- VectorHelper.EmitCall(context, signed
- ? nameof(VectorHelper.SatF32ToS32)
- : nameof(VectorHelper.SatF32ToU32));
- context.Emit(OpCodes.Conv_U8);
- }
- else /* if (sizeF == 1) */
- {
- VectorHelper.EmitCall(context, signed
- ? nameof(VectorHelper.SatF64ToS64)
- : nameof(VectorHelper.SatF64ToU64));
- }
- if (scalar)
- {
- EmitVectorZeroAll(context, op.Rd);
- }
- EmitVectorInsert(context, op.Rd, index, sizeI);
- }
- if (op.RegisterSize == RegisterSize.Simd64)
- {
- EmitVectorZeroUpper(context, op.Rd);
- }
- }
- private static void EmitFcvtz(ILEmitterCtx context, bool signed, bool scalar)
- {
- OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
- int sizeF = op.Size & 1;
- int sizeI = sizeF + 2;
- int fBits = GetFBits(context);
- int bytes = op.GetBitsCount() >> 3;
- int elems = !scalar ? bytes >> sizeI : 1;
- for (int index = 0; index < elems; index++)
- {
- EmitVectorExtractF(context, op.Rn, index, sizeF);
- EmitF2iFBitsMul(context, sizeF, fBits);
- if (sizeF == 0)
- {
- VectorHelper.EmitCall(context, signed
- ? nameof(VectorHelper.SatF32ToS32)
- : nameof(VectorHelper.SatF32ToU32));
- context.Emit(OpCodes.Conv_U8);
- }
- else /* if (sizeF == 1) */
- {
- VectorHelper.EmitCall(context, signed
- ? nameof(VectorHelper.SatF64ToS64)
- : nameof(VectorHelper.SatF64ToU64));
- }
- if (scalar)
- {
- EmitVectorZeroAll(context, op.Rd);
- }
- EmitVectorInsert(context, op.Rd, index, sizeI);
- }
- if (op.RegisterSize == RegisterSize.Simd64)
- {
- EmitVectorZeroUpper(context, op.Rd);
- }
- }
- private static void EmitFcvt_s_Gp(ILEmitterCtx context, Action emit)
- {
- EmitFcvt___Gp(context, emit, true);
- }
- private static void EmitFcvt_u_Gp(ILEmitterCtx context, Action emit)
- {
- EmitFcvt___Gp(context, emit, false);
- }
- private static void EmitFcvt___Gp(ILEmitterCtx context, Action emit, bool signed)
- {
- OpCodeSimdCvt64 op = (OpCodeSimdCvt64)context.CurrOp;
- EmitVectorExtractF(context, op.Rn, 0, op.Size);
- emit();
- if (signed)
- {
- EmitScalarFcvts(context, op.Size, 0);
- }
- else
- {
- EmitScalarFcvtu(context, op.Size, 0);
- }
- if (context.CurrOp.RegisterSize == RegisterSize.Int32)
- {
- context.Emit(OpCodes.Conv_U8);
- }
- context.EmitStintzr(op.Rd);
- }
- private static void EmitFcvtzs_Gp_Fixed(ILEmitterCtx context)
- {
- EmitFcvtz__Gp_Fixed(context, true);
- }
- private static void EmitFcvtzu_Gp_Fixed(ILEmitterCtx context)
- {
- EmitFcvtz__Gp_Fixed(context, false);
- }
- private static void EmitFcvtz__Gp_Fixed(ILEmitterCtx context, bool signed)
- {
- OpCodeSimdCvt64 op = (OpCodeSimdCvt64)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 == RegisterSize.Int32)
- {
- context.Emit(OpCodes.Conv_U8);
- }
- context.EmitStintzr(op.Rd);
- }
- private static void EmitVectorCvtf(ILEmitterCtx context, bool signed)
- {
- OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
- int sizeF = op.Size & 1;
- int sizeI = sizeF + 2;
- int fBits = GetFBits(context);
- int bytes = op.GetBitsCount() >> 3;
- int elems = bytes >> sizeI;
- for (int index = 0; index < elems; index++)
- {
- EmitVectorExtract(context, op.Rn, index, sizeI, signed);
- if (!signed)
- {
- context.Emit(OpCodes.Conv_R_Un);
- }
- EmitFloatCast(context, sizeF);
- EmitI2fFBitsMul(context, sizeF, fBits);
- EmitVectorInsertF(context, op.Rd, index, sizeF);
- }
- if (op.RegisterSize == RegisterSize.Simd64)
- {
- EmitVectorZeroUpper(context, op.Rd);
- }
- }
- private static int GetFBits(ILEmitterCtx context)
- {
- if (context.CurrOp is OpCodeSimdShImm64 op)
- {
- return GetImmShr(op);
- }
- return 0;
- }
- private static void EmitFloatCast(ILEmitterCtx 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 EmitScalarFcvts(ILEmitterCtx context, int size, int fBits)
- {
- if (size < 0 || size > 1)
- {
- throw new ArgumentOutOfRangeException(nameof(size));
- }
- EmitF2iFBitsMul(context, size, fBits);
- if (context.CurrOp.RegisterSize == RegisterSize.Int32)
- {
- if (size == 0)
- {
- VectorHelper.EmitCall(context, nameof(VectorHelper.SatF32ToS32));
- }
- else /* if (size == 1) */
- {
- VectorHelper.EmitCall(context, nameof(VectorHelper.SatF64ToS32));
- }
- }
- else
- {
- if (size == 0)
- {
- VectorHelper.EmitCall(context, nameof(VectorHelper.SatF32ToS64));
- }
- else /* if (size == 1) */
- {
- VectorHelper.EmitCall(context, nameof(VectorHelper.SatF64ToS64));
- }
- }
- }
- private static void EmitScalarFcvtu(ILEmitterCtx context, int size, int fBits)
- {
- if (size < 0 || size > 1)
- {
- throw new ArgumentOutOfRangeException(nameof(size));
- }
- EmitF2iFBitsMul(context, size, fBits);
- if (context.CurrOp.RegisterSize == RegisterSize.Int32)
- {
- if (size == 0)
- {
- VectorHelper.EmitCall(context, nameof(VectorHelper.SatF32ToU32));
- }
- else /* if (size == 1) */
- {
- VectorHelper.EmitCall(context, nameof(VectorHelper.SatF64ToU32));
- }
- }
- else
- {
- if (size == 0)
- {
- VectorHelper.EmitCall(context, nameof(VectorHelper.SatF32ToU64));
- }
- else /* if (size == 1) */
- {
- VectorHelper.EmitCall(context, nameof(VectorHelper.SatF64ToU64));
- }
- }
- }
- private static void EmitF2iFBitsMul(ILEmitterCtx context, int size, int fBits)
- {
- if (fBits != 0)
- {
- if (size == 0)
- {
- context.EmitLdc_R4(MathF.Pow(2f, fBits));
- }
- else if (size == 1)
- {
- context.EmitLdc_R8(Math.Pow(2d, fBits));
- }
- else
- {
- throw new ArgumentOutOfRangeException(nameof(size));
- }
- context.Emit(OpCodes.Mul);
- }
- }
- private static void EmitI2fFBitsMul(ILEmitterCtx context, int size, int fBits)
- {
- if (fBits != 0)
- {
- if (size == 0)
- {
- context.EmitLdc_R4(1f / MathF.Pow(2f, fBits));
- }
- else if (size == 1)
- {
- context.EmitLdc_R8(1d / Math.Pow(2d, fBits));
- }
- else
- {
- throw new ArgumentOutOfRangeException(nameof(size));
- }
- context.Emit(OpCodes.Mul);
- }
- }
- private static void EmitSse41Fcvt_Signed(ILEmitterCtx context, RoundMode roundMode, bool scalar)
- {
- OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
- // sizeF == ((OpCodeSimdShImm64)op).Size - 2
- int sizeF = op.Size & 1;
- if (sizeF == 0)
- {
- Type[] types = new Type[] { typeof(Vector128<float>), typeof(Vector128<float>) };
- Type[] typesRndCvt = new Type[] { typeof(Vector128<float>) };
- Type[] typesSav = new Type[] { typeof(int) };
- context.EmitLdvec(op.Rn);
- context.EmitLdvec(op.Rn);
- context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.CompareOrdered), types));
- context.EmitLdvec(op.Rn);
- context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.And), types));
- if (op is OpCodeSimdShImm64 fixedOp)
- {
- int fBits = GetImmShr(fixedOp);
- // BitConverter.Int32BitsToSingle(fpScaled) == MathF.Pow(2f, fBits)
- int fpScaled = 0x3F800000 + fBits * 0x800000;
- context.EmitLdc_I4(fpScaled);
- context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetAllVector128), typesSav));
- context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.Multiply), types));
- }
- context.EmitCall(typeof(Sse41).GetMethod(GetVectorSse41NameRnd(roundMode), typesRndCvt));
- context.EmitStvectmp();
- context.EmitLdvectmp();
- context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ConvertToVector128Int32), typesRndCvt));
- context.EmitLdvectmp();
- context.EmitLdc_I4(0x4F000000); // 2.14748365E9f (2147483648)
- context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetAllVector128), typesSav));
- context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.CompareGreaterThanOrEqual), types));
- context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.Xor), types));
- context.EmitStvec(op.Rd);
- if (scalar)
- {
- EmitVectorZero32_128(context, op.Rd);
- }
- else if (op.RegisterSize == RegisterSize.Simd64)
- {
- EmitVectorZeroUpper(context, op.Rd);
- }
- }
- else /* if (sizeF == 1) */
- {
- Type[] types = new Type[] { typeof(Vector128<double>), typeof(Vector128<double>) };
- Type[] typesRndCvt = new Type[] { typeof(Vector128<double>) };
- Type[] typesSv = new Type[] { typeof(long), typeof(long) };
- Type[] typesSav = new Type[] { typeof(long) };
- context.EmitLdvec(op.Rn);
- context.EmitLdvec(op.Rn);
- context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.CompareOrdered), types));
- context.EmitLdvec(op.Rn);
- context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.And), types));
- if (op is OpCodeSimdShImm64 fixedOp)
- {
- int fBits = GetImmShr(fixedOp);
- // BitConverter.Int64BitsToDouble(fpScaled) == Math.Pow(2d, fBits)
- long fpScaled = 0x3FF0000000000000L + fBits * 0x10000000000000L;
- context.EmitLdc_I8(fpScaled);
- context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetAllVector128), typesSav));
- context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Multiply), types));
- }
- context.EmitCall(typeof(Sse41).GetMethod(GetVectorSse41NameRnd(roundMode), typesRndCvt));
- context.EmitStvectmp();
- if (!scalar)
- {
- context.EmitLdvectmp();
- context.EmitLdvectmp();
- context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.UnpackHigh), types));
- context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ConvertToInt64), typesRndCvt));
- }
- else
- {
- context.EmitLdc_I8(0L);
- }
- context.EmitLdvectmp();
- context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ConvertToInt64), typesRndCvt));
- context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetVector128), typesSv));
- context.EmitLdvectmp();
- context.EmitLdc_I8(0x43E0000000000000L); // 9.2233720368547760E18d (9223372036854775808)
- context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetAllVector128), typesSav));
- context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.CompareGreaterThanOrEqual), types));
- context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Xor), types));
- context.EmitStvec(op.Rd);
- if (scalar)
- {
- EmitVectorZeroUpper(context, op.Rd);
- }
- }
- }
- private static void EmitSse41Fcvt_Unsigned(ILEmitterCtx context, RoundMode roundMode, bool scalar)
- {
- OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
- // sizeF == ((OpCodeSimdShImm64)op).Size - 2
- int sizeF = op.Size & 1;
- if (sizeF == 0)
- {
- Type[] types = new Type[] { typeof(Vector128<float>), typeof(Vector128<float>) };
- Type[] typesAdd = new Type[] { typeof(Vector128<int>), typeof(Vector128<int>) };
- Type[] typesRndCvt = new Type[] { typeof(Vector128<float>) };
- Type[] typesSav = new Type[] { typeof(int) };
- context.EmitLdvec(op.Rn);
- context.EmitLdvec(op.Rn);
- context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.CompareOrdered), types));
- context.EmitLdvec(op.Rn);
- context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.And), types));
- if (op is OpCodeSimdShImm64 fixedOp)
- {
- int fBits = GetImmShr(fixedOp);
- // BitConverter.Int32BitsToSingle(fpScaled) == MathF.Pow(2f, fBits)
- int fpScaled = 0x3F800000 + fBits * 0x800000;
- context.EmitLdc_I4(fpScaled);
- context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetAllVector128), typesSav));
- context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.Multiply), types));
- }
- context.EmitCall(typeof(Sse41).GetMethod(GetVectorSse41NameRnd(roundMode), typesRndCvt));
- context.Emit(OpCodes.Dup);
- VectorHelper.EmitCall(context, nameof(VectorHelper.VectorSingleZero));
- context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.CompareGreaterThan), types));
- context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.And), types));
- context.EmitStvectmp();
- context.EmitLdvectmp();
- context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ConvertToVector128Int32), typesRndCvt));
- context.EmitLdvectmp();
- context.EmitLdc_I4(0x4F000000); // 2.14748365E9f (2147483648)
- context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetAllVector128), typesSav));
- context.EmitStvectmp2();
- context.EmitLdvectmp2();
- context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.Subtract), types));
- context.Emit(OpCodes.Dup);
- VectorHelper.EmitCall(context, nameof(VectorHelper.VectorSingleZero));
- context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.CompareGreaterThan), types));
- context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.And), types));
- context.EmitStvectmp();
- context.EmitLdvectmp();
- context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ConvertToVector128Int32), typesRndCvt));
- context.EmitLdvectmp();
- context.EmitLdvectmp2();
- context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.CompareGreaterThanOrEqual), types));
- context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.Xor), types));
- context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Add), typesAdd));
- context.EmitStvec(op.Rd);
- if (scalar)
- {
- EmitVectorZero32_128(context, op.Rd);
- }
- else if (op.RegisterSize == RegisterSize.Simd64)
- {
- EmitVectorZeroUpper(context, op.Rd);
- }
- }
- else /* if (sizeF == 1) */
- {
- Type[] types = new Type[] { typeof(Vector128<double>), typeof(Vector128<double>) };
- Type[] typesAdd = new Type[] { typeof(Vector128<long>), typeof(Vector128<long>) };
- Type[] typesRndCvt = new Type[] { typeof(Vector128<double>) };
- Type[] typesSv = new Type[] { typeof(long), typeof(long) };
- Type[] typesSav = new Type[] { typeof(long) };
- context.EmitLdvec(op.Rn);
- context.EmitLdvec(op.Rn);
- context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.CompareOrdered), types));
- context.EmitLdvec(op.Rn);
- context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.And), types));
- if (op is OpCodeSimdShImm64 fixedOp)
- {
- int fBits = GetImmShr(fixedOp);
- // BitConverter.Int64BitsToDouble(fpScaled) == Math.Pow(2d, fBits)
- long fpScaled = 0x3FF0000000000000L + fBits * 0x10000000000000L;
- context.EmitLdc_I8(fpScaled);
- context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetAllVector128), typesSav));
- context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Multiply), types));
- }
- context.EmitCall(typeof(Sse41).GetMethod(GetVectorSse41NameRnd(roundMode), typesRndCvt));
- context.Emit(OpCodes.Dup);
- VectorHelper.EmitCall(context, nameof(VectorHelper.VectorSingleZero));
- context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.CompareGreaterThan), types));
- context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.And), types));
- context.EmitStvectmp();
- if (!scalar)
- {
- context.EmitLdvectmp();
- context.EmitLdvectmp();
- context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.UnpackHigh), types));
- context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ConvertToInt64), typesRndCvt));
- }
- else
- {
- context.EmitLdc_I8(0L);
- }
- context.EmitLdvectmp();
- context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ConvertToInt64), typesRndCvt));
- context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetVector128), typesSv));
- context.EmitLdvectmp();
- context.EmitLdc_I8(0x43E0000000000000L); // 9.2233720368547760E18d (9223372036854775808)
- context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetAllVector128), typesSav));
- context.EmitStvectmp2();
- context.EmitLdvectmp2();
- context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Subtract), types));
- context.Emit(OpCodes.Dup);
- VectorHelper.EmitCall(context, nameof(VectorHelper.VectorSingleZero));
- context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.CompareGreaterThan), types));
- context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.And), types));
- context.EmitStvectmp();
- if (!scalar)
- {
- context.EmitLdvectmp();
- context.EmitLdvectmp();
- context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.UnpackHigh), types));
- context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ConvertToInt64), typesRndCvt));
- }
- else
- {
- context.EmitLdc_I8(0L);
- }
- context.EmitLdvectmp();
- context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ConvertToInt64), typesRndCvt));
- context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetVector128), typesSv));
- context.EmitLdvectmp();
- context.EmitLdvectmp2();
- context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.CompareGreaterThanOrEqual), types));
- context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Xor), types));
- context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Add), typesAdd));
- context.EmitStvec(op.Rd);
- if (scalar)
- {
- EmitVectorZeroUpper(context, op.Rd);
- }
- }
- }
- private static void EmitSse2cvtF_Signed(ILEmitterCtx context, bool scalar)
- {
- OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
- Type[] typesMul = new Type[] { typeof(Vector128<float>), typeof(Vector128<float>) };
- Type[] typesCvt = new Type[] { typeof(Vector128<int>) };
- Type[] typesSav = new Type[] { typeof(int) };
- context.EmitLdvec(op.Rn);
- context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ConvertToVector128Single), typesCvt));
- if (op is OpCodeSimdShImm64 fixedOp)
- {
- int fBits = GetImmShr(fixedOp);
- // BitConverter.Int32BitsToSingle(fpScaled) == 1f / MathF.Pow(2f, fBits)
- int fpScaled = 0x3F800000 - fBits * 0x800000;
- context.EmitLdc_I4(fpScaled);
- context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetAllVector128), typesSav));
- context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.Multiply), typesMul));
- }
- context.EmitStvec(op.Rd);
- if (scalar)
- {
- EmitVectorZero32_128(context, op.Rd);
- }
- else if (op.RegisterSize == RegisterSize.Simd64)
- {
- EmitVectorZeroUpper(context, op.Rd);
- }
- }
- private static void EmitSse2cvtF_Unsigned(ILEmitterCtx context, bool scalar)
- {
- OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
- Type[] typesMulAdd = new Type[] { typeof(Vector128<float>), typeof(Vector128<float>) };
- Type[] typesSrlSll = new Type[] { typeof(Vector128<int>), typeof(byte) };
- Type[] typesCvt = new Type[] { typeof(Vector128<int>) };
- Type[] typesSav = new Type[] { typeof(int) };
- context.EmitLdvec(op.Rn);
- context.EmitLdc_I4(16);
- context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ShiftRightLogical), typesSrlSll));
- context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ConvertToVector128Single), typesCvt));
- context.EmitLdc_I4(0x47800000); // 65536.0f (1 << 16)
- context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetAllVector128), typesSav));
- context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.Multiply), typesMulAdd));
- context.EmitLdvec(op.Rn);
- context.EmitLdc_I4(16);
- context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ShiftLeftLogical), typesSrlSll));
- context.EmitLdc_I4(16);
- context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ShiftRightLogical), typesSrlSll));
- context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ConvertToVector128Single), typesCvt));
- context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.Add), typesMulAdd));
- if (op is OpCodeSimdShImm64 fixedOp)
- {
- int fBits = GetImmShr(fixedOp);
- // BitConverter.Int32BitsToSingle(fpScaled) == 1f / MathF.Pow(2f, fBits)
- int fpScaled = 0x3F800000 - fBits * 0x800000;
- context.EmitLdc_I4(fpScaled);
- context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetAllVector128), typesSav));
- context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.Multiply), typesMulAdd));
- }
- context.EmitStvec(op.Rd);
- if (scalar)
- {
- EmitVectorZero32_128(context, op.Rd);
- }
- else if (op.RegisterSize == RegisterSize.Simd64)
- {
- EmitVectorZeroUpper(context, op.Rd);
- }
- }
- 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)
- {
- case RoundMode.ToNearest:
- return nameof(Sse41.RoundToNearestInteger); // even
- case RoundMode.TowardsPlusInfinity:
- return nameof(Sse41.RoundToPositiveInfinity);
- case RoundMode.TowardsMinusInfinity:
- return nameof(Sse41.RoundToNegativeInfinity);
- default: /* case RoundMode.TowardsZero: */
- return nameof(Sse41.RoundToZero);
- }
- }
- }
- }
|