| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382 |
- 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 (op.Size == 0 && op.Opc == 1) // Single -> Double.
- {
- if (Optimizations.UseSse2)
- {
- 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
- {
- EmitVectorExtractF(context, op.Rn, 0, 0);
- EmitFloatCast(context, 1);
- EmitScalarSetF(context, op.Rd, 1);
- }
- }
- else if (op.Size == 1 && op.Opc == 0) // Double -> Single.
- {
- if (Optimizations.UseSse2)
- {
- 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
- {
- EmitVectorExtractF(context, op.Rn, 0, 1);
- EmitFloatCast(context, 0);
- EmitScalarSetF(context, op.Rd, 0);
- }
- }
- else if (op.Size == 0 && op.Opc == 3) // Single -> Half.
- {
- EmitVectorExtractF(context, op.Rn, 0, 0);
- context.EmitLdarg(TranslatedSub.StateArgIdx);
- context.EmitCall(typeof(SoftFloat32_16), nameof(SoftFloat32_16.FPConvert));
- context.Emit(OpCodes.Conv_U8);
- EmitScalarSet(context, op.Rd, 1);
- }
- else if (op.Size == 3 && op.Opc == 0) // Half -> Single.
- {
- EmitVectorExtractZx(context, op.Rn, 0, 1);
- context.Emit(OpCodes.Conv_U2);
- context.EmitLdarg(TranslatedSub.StateArgIdx);
- context.EmitCall(typeof(SoftFloat16_32), nameof(SoftFloat16_32.FPConvert));
- EmitScalarSetF(context, op.Rd, 0);
- }
- else if (op.Size == 1 && op.Opc == 3) // Double -> Half.
- {
- throw new NotImplementedException("Double-precision to half-precision.");
- }
- else if (op.Size == 3 && op.Opc == 1) // Double -> Half.
- {
- throw new NotImplementedException("Half-precision to double-precision.");
- }
- else // Invalid encoding.
- {
- throw new InvalidOperationException($"type == {op.Size} && opc == {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);
- }
- }
- }
- }
|