| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073 |
- using ChocolArm64.Decoder;
- using ChocolArm64.State;
- using ChocolArm64.Translation;
- using System;
- using System.Reflection;
- using System.Reflection.Emit;
- using System.Runtime.CompilerServices;
- using System.Runtime.Intrinsics;
- using System.Runtime.Intrinsics.X86;
- namespace ChocolArm64.Instruction
- {
- static class AInstEmitSimdHelper
- {
- [Flags]
- public enum OperFlags
- {
- Rd = 1 << 0,
- Rn = 1 << 1,
- Rm = 1 << 2,
- Ra = 1 << 3,
- RnRm = Rn | Rm,
- RdRn = Rd | Rn,
- RaRnRm = Ra | Rn | Rm,
- RdRnRm = Rd | Rn | Rm
- }
- public static int GetImmShl(AOpCodeSimdShImm Op)
- {
- return Op.Imm - (8 << Op.Size);
- }
- public static int GetImmShr(AOpCodeSimdShImm Op)
- {
- return (8 << (Op.Size + 1)) - Op.Imm;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static void EmitSse2Call(AILEmitterCtx Context, string Name)
- {
- EmitSseCall(Context, Name, typeof(Sse2));
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static void EmitSse41Call(AILEmitterCtx Context, string Name)
- {
- EmitSseCall(Context, Name, typeof(Sse41));
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static void EmitSse42Call(AILEmitterCtx Context, string Name)
- {
- EmitSseCall(Context, Name, typeof(Sse42));
- }
- private static void EmitSseCall(AILEmitterCtx Context, string Name, Type Type)
- {
- AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
- void Ldvec(int Reg)
- {
- Context.EmitLdvec(Reg);
- switch (Op.Size)
- {
- case 0: AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorSingleToSByte)); break;
- case 1: AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorSingleToInt16)); break;
- case 2: AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorSingleToInt32)); break;
- case 3: AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorSingleToInt64)); break;
- }
- }
- Ldvec(Op.Rn);
- Type BaseType = null;
- switch (Op.Size)
- {
- case 0: BaseType = typeof(Vector128<sbyte>); break;
- case 1: BaseType = typeof(Vector128<short>); break;
- case 2: BaseType = typeof(Vector128<int>); break;
- case 3: BaseType = typeof(Vector128<long>); break;
- }
- if (Op is AOpCodeSimdReg BinOp)
- {
- Ldvec(BinOp.Rm);
- Context.EmitCall(Type.GetMethod(Name, new Type[] { BaseType, BaseType }));
- }
- else
- {
- Context.EmitCall(Type.GetMethod(Name, new Type[] { BaseType }));
- }
- switch (Op.Size)
- {
- case 0: AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorSByteToSingle)); break;
- case 1: AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorInt16ToSingle)); break;
- case 2: AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorInt32ToSingle)); break;
- case 3: AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorInt64ToSingle)); break;
- }
- Context.EmitStvec(Op.Rd);
- if (Op.RegisterSize == ARegisterSize.SIMD64)
- {
- EmitVectorZeroUpper(Context, Op.Rd);
- }
- }
- public static void EmitSseOrSse2CallF(AILEmitterCtx Context, string Name)
- {
- AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
- int SizeF = Op.Size & 1;
- void Ldvec(int Reg)
- {
- Context.EmitLdvec(Reg);
- if (SizeF == 1)
- {
- AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorSingleToDouble));
- }
- }
- Ldvec(Op.Rn);
- Type Type;
- Type BaseType;
- if (SizeF == 0)
- {
- Type = typeof(Sse);
- BaseType = typeof(Vector128<float>);
- }
- else /* if (SizeF == 1) */
- {
- Type = typeof(Sse2);
- BaseType = typeof(Vector128<double>);
- }
- if (Op is AOpCodeSimdReg BinOp)
- {
- Ldvec(BinOp.Rm);
- Context.EmitCall(Type.GetMethod(Name, new Type[] { BaseType, BaseType }));
- }
- else
- {
- Context.EmitCall(Type.GetMethod(Name, new Type[] { BaseType }));
- }
- if (SizeF == 1)
- {
- AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorDoubleToSingle));
- }
- Context.EmitStvec(Op.Rd);
- if (Op.RegisterSize == ARegisterSize.SIMD64)
- {
- EmitVectorZeroUpper(Context, Op.Rd);
- }
- }
- public static void EmitUnaryMathCall(AILEmitterCtx Context, string Name)
- {
- IAOpCodeSimd Op = (IAOpCodeSimd)Context.CurrOp;
- int SizeF = Op.Size & 1;
- MethodInfo MthdInfo;
- if (SizeF == 0)
- {
- MthdInfo = typeof(MathF).GetMethod(Name, new Type[] { typeof(float) });
- }
- else /* if (SizeF == 1) */
- {
- MthdInfo = typeof(Math).GetMethod(Name, new Type[] { typeof(double) });
- }
- Context.EmitCall(MthdInfo);
- }
- public static void EmitBinaryMathCall(AILEmitterCtx Context, string Name)
- {
- IAOpCodeSimd Op = (IAOpCodeSimd)Context.CurrOp;
- int SizeF = Op.Size & 1;
- MethodInfo MthdInfo;
- if (SizeF == 0)
- {
- MthdInfo = typeof(MathF).GetMethod(Name, new Type[] { typeof(float), typeof(float) });
- }
- else /* if (SizeF == 1) */
- {
- MthdInfo = typeof(Math).GetMethod(Name, new Type[] { typeof(double), typeof(double) });
- }
- Context.EmitCall(MthdInfo);
- }
- public static void EmitRoundMathCall(AILEmitterCtx Context, MidpointRounding RoundMode)
- {
- IAOpCodeSimd Op = (IAOpCodeSimd)Context.CurrOp;
- int SizeF = Op.Size & 1;
- Context.EmitLdc_I4((int)RoundMode);
- MethodInfo MthdInfo;
- Type[] Types = new Type[] { null, typeof(MidpointRounding) };
- Types[0] = SizeF == 0
- ? typeof(float)
- : typeof(double);
- if (SizeF == 0)
- {
- MthdInfo = typeof(MathF).GetMethod(nameof(MathF.Round), Types);
- }
- else /* if (SizeF == 1) */
- {
- MthdInfo = typeof(Math).GetMethod(nameof(Math.Round), Types);
- }
- Context.EmitCall(MthdInfo);
- }
- public static void EmitUnarySoftFloatCall(AILEmitterCtx Context, string Name)
- {
- IAOpCodeSimd Op = (IAOpCodeSimd)Context.CurrOp;
- int SizeF = Op.Size & 1;
- MethodInfo MthdInfo;
- if (SizeF == 0)
- {
- MthdInfo = typeof(ASoftFloat).GetMethod(Name, new Type[] { typeof(float) });
- }
- else /* if (SizeF == 1) */
- {
- MthdInfo = typeof(ASoftFloat).GetMethod(Name, new Type[] { typeof(double) });
- }
- Context.EmitCall(MthdInfo);
- }
- public static void EmitBinarySoftFloatCall(AILEmitterCtx Context, string Name)
- {
- IAOpCodeSimd Op = (IAOpCodeSimd)Context.CurrOp;
- int SizeF = Op.Size & 1;
- MethodInfo MthdInfo;
- if (SizeF == 0)
- {
- MthdInfo = typeof(ASoftFloat).GetMethod(Name, new Type[] { typeof(float), typeof(float) });
- }
- else /* if (SizeF == 1) */
- {
- MthdInfo = typeof(ASoftFloat).GetMethod(Name, new Type[] { typeof(double), typeof(double) });
- }
- Context.EmitCall(MthdInfo);
- }
- public static void EmitScalarBinaryOpByElemF(AILEmitterCtx Context, Action Emit)
- {
- AOpCodeSimdRegElemF Op = (AOpCodeSimdRegElemF)Context.CurrOp;
- EmitScalarOpByElemF(Context, Emit, Op.Index, Ternary: false);
- }
- public static void EmitScalarTernaryOpByElemF(AILEmitterCtx Context, Action Emit)
- {
- AOpCodeSimdRegElemF Op = (AOpCodeSimdRegElemF)Context.CurrOp;
- EmitScalarOpByElemF(Context, Emit, Op.Index, Ternary: true);
- }
- public static void EmitScalarOpByElemF(AILEmitterCtx Context, Action Emit, int Elem, bool Ternary)
- {
- AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
- int SizeF = Op.Size & 1;
- if (Ternary)
- {
- EmitVectorExtractF(Context, Op.Rd, 0, SizeF);
- }
- EmitVectorExtractF(Context, Op.Rn, 0, SizeF);
- EmitVectorExtractF(Context, Op.Rm, Elem, SizeF);
- Emit();
- EmitScalarSetF(Context, Op.Rd, SizeF);
- }
- public static void EmitScalarUnaryOpSx(AILEmitterCtx Context, Action Emit)
- {
- EmitScalarOp(Context, Emit, OperFlags.Rn, true);
- }
- public static void EmitScalarBinaryOpSx(AILEmitterCtx Context, Action Emit)
- {
- EmitScalarOp(Context, Emit, OperFlags.RnRm, true);
- }
- public static void EmitScalarUnaryOpZx(AILEmitterCtx Context, Action Emit)
- {
- EmitScalarOp(Context, Emit, OperFlags.Rn, false);
- }
- public static void EmitScalarBinaryOpZx(AILEmitterCtx Context, Action Emit)
- {
- EmitScalarOp(Context, Emit, OperFlags.RnRm, false);
- }
- public static void EmitScalarTernaryOpZx(AILEmitterCtx Context, Action Emit)
- {
- EmitScalarOp(Context, Emit, OperFlags.RdRnRm, false);
- }
- public static void EmitScalarOp(AILEmitterCtx Context, Action Emit, OperFlags Opers, bool Signed)
- {
- AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
- if (Opers.HasFlag(OperFlags.Rd))
- {
- EmitVectorExtract(Context, Op.Rd, 0, Op.Size, Signed);
- }
- if (Opers.HasFlag(OperFlags.Rn))
- {
- EmitVectorExtract(Context, Op.Rn, 0, Op.Size, Signed);
- }
- if (Opers.HasFlag(OperFlags.Rm))
- {
- EmitVectorExtract(Context, ((AOpCodeSimdReg)Op).Rm, 0, Op.Size, Signed);
- }
- Emit();
- EmitScalarSet(Context, Op.Rd, Op.Size);
- }
- public static void EmitScalarUnaryOpF(AILEmitterCtx Context, Action Emit)
- {
- EmitScalarOpF(Context, Emit, OperFlags.Rn);
- }
- public static void EmitScalarBinaryOpF(AILEmitterCtx Context, Action Emit)
- {
- EmitScalarOpF(Context, Emit, OperFlags.RnRm);
- }
- public static void EmitScalarTernaryRaOpF(AILEmitterCtx Context, Action Emit)
- {
- EmitScalarOpF(Context, Emit, OperFlags.RaRnRm);
- }
- public static void EmitScalarOpF(AILEmitterCtx Context, Action Emit, OperFlags Opers)
- {
- AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
- int SizeF = Op.Size & 1;
- if (Opers.HasFlag(OperFlags.Ra))
- {
- EmitVectorExtractF(Context, ((AOpCodeSimdReg)Op).Ra, 0, SizeF);
- }
- if (Opers.HasFlag(OperFlags.Rn))
- {
- EmitVectorExtractF(Context, Op.Rn, 0, SizeF);
- }
- if (Opers.HasFlag(OperFlags.Rm))
- {
- EmitVectorExtractF(Context, ((AOpCodeSimdReg)Op).Rm, 0, SizeF);
- }
- Emit();
- EmitScalarSetF(Context, Op.Rd, SizeF);
- }
- public static void EmitVectorUnaryOpF(AILEmitterCtx Context, Action Emit)
- {
- EmitVectorOpF(Context, Emit, OperFlags.Rn);
- }
- public static void EmitVectorBinaryOpF(AILEmitterCtx Context, Action Emit)
- {
- EmitVectorOpF(Context, Emit, OperFlags.RnRm);
- }
- public static void EmitVectorTernaryOpF(AILEmitterCtx Context, Action Emit)
- {
- EmitVectorOpF(Context, Emit, OperFlags.RdRnRm);
- }
- public static void EmitVectorOpF(AILEmitterCtx Context, Action Emit, OperFlags Opers)
- {
- AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
- int SizeF = Op.Size & 1;
- int Bytes = Op.GetBitsCount() >> 3;
- for (int Index = 0; Index < (Bytes >> SizeF + 2); Index++)
- {
- if (Opers.HasFlag(OperFlags.Rd))
- {
- EmitVectorExtractF(Context, Op.Rd, Index, SizeF);
- }
- if (Opers.HasFlag(OperFlags.Rn))
- {
- EmitVectorExtractF(Context, Op.Rn, Index, SizeF);
- }
- if (Opers.HasFlag(OperFlags.Rm))
- {
- EmitVectorExtractF(Context, ((AOpCodeSimdReg)Op).Rm, Index, SizeF);
- }
- Emit();
- EmitVectorInsertF(Context, Op.Rd, Index, SizeF);
- }
- if (Op.RegisterSize == ARegisterSize.SIMD64)
- {
- EmitVectorZeroUpper(Context, Op.Rd);
- }
- }
- public static void EmitVectorBinaryOpByElemF(AILEmitterCtx Context, Action Emit)
- {
- AOpCodeSimdRegElemF Op = (AOpCodeSimdRegElemF)Context.CurrOp;
- EmitVectorOpByElemF(Context, Emit, Op.Index, Ternary: false);
- }
- public static void EmitVectorTernaryOpByElemF(AILEmitterCtx Context, Action Emit)
- {
- AOpCodeSimdRegElemF Op = (AOpCodeSimdRegElemF)Context.CurrOp;
- EmitVectorOpByElemF(Context, Emit, Op.Index, Ternary: true);
- }
- public static void EmitVectorOpByElemF(AILEmitterCtx Context, Action Emit, int Elem, bool Ternary)
- {
- AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
- int SizeF = Op.Size & 1;
- int Bytes = Op.GetBitsCount() >> 3;
- for (int Index = 0; Index < (Bytes >> SizeF + 2); Index++)
- {
- if (Ternary)
- {
- EmitVectorExtractF(Context, Op.Rd, Index, SizeF);
- }
- EmitVectorExtractF(Context, Op.Rn, Index, SizeF);
- EmitVectorExtractF(Context, Op.Rm, Elem, SizeF);
- Emit();
- EmitVectorInsertTmpF(Context, Index, SizeF);
- }
- Context.EmitLdvectmp();
- Context.EmitStvec(Op.Rd);
- if (Op.RegisterSize == ARegisterSize.SIMD64)
- {
- EmitVectorZeroUpper(Context, Op.Rd);
- }
- }
- public static void EmitVectorUnaryOpSx(AILEmitterCtx Context, Action Emit)
- {
- EmitVectorOp(Context, Emit, OperFlags.Rn, true);
- }
- public static void EmitVectorBinaryOpSx(AILEmitterCtx Context, Action Emit)
- {
- EmitVectorOp(Context, Emit, OperFlags.RnRm, true);
- }
- public static void EmitVectorTernaryOpSx(AILEmitterCtx Context, Action Emit)
- {
- EmitVectorOp(Context, Emit, OperFlags.RdRnRm, true);
- }
- public static void EmitVectorUnaryOpZx(AILEmitterCtx Context, Action Emit)
- {
- EmitVectorOp(Context, Emit, OperFlags.Rn, false);
- }
- public static void EmitVectorBinaryOpZx(AILEmitterCtx Context, Action Emit)
- {
- EmitVectorOp(Context, Emit, OperFlags.RnRm, false);
- }
- public static void EmitVectorTernaryOpZx(AILEmitterCtx Context, Action Emit)
- {
- EmitVectorOp(Context, Emit, OperFlags.RdRnRm, false);
- }
- public static void EmitVectorOp(AILEmitterCtx Context, Action Emit, OperFlags Opers, bool Signed)
- {
- AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
- int Bytes = Op.GetBitsCount() >> 3;
- int Elems = Bytes >> Op.Size;
- for (int Index = 0; Index < Elems; Index++)
- {
- if (Opers.HasFlag(OperFlags.Rd))
- {
- EmitVectorExtract(Context, Op.Rd, Index, Op.Size, Signed);
- }
- if (Opers.HasFlag(OperFlags.Rn))
- {
- EmitVectorExtract(Context, Op.Rn, Index, Op.Size, Signed);
- }
- if (Opers.HasFlag(OperFlags.Rm))
- {
- EmitVectorExtract(Context, ((AOpCodeSimdReg)Op).Rm, Index, Op.Size, Signed);
- }
- Emit();
- EmitVectorInsert(Context, Op.Rd, Index, Op.Size);
- }
- if (Op.RegisterSize == ARegisterSize.SIMD64)
- {
- EmitVectorZeroUpper(Context, Op.Rd);
- }
- }
- public static void EmitVectorBinaryOpByElemSx(AILEmitterCtx Context, Action Emit)
- {
- AOpCodeSimdRegElem Op = (AOpCodeSimdRegElem)Context.CurrOp;
- EmitVectorOpByElem(Context, Emit, Op.Index, false, true);
- }
- public static void EmitVectorBinaryOpByElemZx(AILEmitterCtx Context, Action Emit)
- {
- AOpCodeSimdRegElem Op = (AOpCodeSimdRegElem)Context.CurrOp;
- EmitVectorOpByElem(Context, Emit, Op.Index, false, false);
- }
- public static void EmitVectorTernaryOpByElemZx(AILEmitterCtx Context, Action Emit)
- {
- AOpCodeSimdRegElem Op = (AOpCodeSimdRegElem)Context.CurrOp;
- EmitVectorOpByElem(Context, Emit, Op.Index, true, false);
- }
- public static void EmitVectorOpByElem(AILEmitterCtx Context, Action Emit, int Elem, bool Ternary, bool Signed)
- {
- AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
- int Bytes = Op.GetBitsCount() >> 3;
- int Elems = Bytes >> Op.Size;
- for (int Index = 0; Index < Elems; Index++)
- {
- if (Ternary)
- {
- EmitVectorExtract(Context, Op.Rd, Index, Op.Size, Signed);
- }
- EmitVectorExtract(Context, Op.Rn, Index, Op.Size, Signed);
- EmitVectorExtract(Context, Op.Rm, Elem, Op.Size, Signed);
- Emit();
- EmitVectorInsertTmp(Context, Index, Op.Size);
- }
- Context.EmitLdvectmp();
- Context.EmitStvec(Op.Rd);
- if (Op.RegisterSize == ARegisterSize.SIMD64)
- {
- EmitVectorZeroUpper(Context, Op.Rd);
- }
- }
- public static void EmitVectorImmUnaryOp(AILEmitterCtx Context, Action Emit)
- {
- EmitVectorImmOp(Context, Emit, false);
- }
- public static void EmitVectorImmBinaryOp(AILEmitterCtx Context, Action Emit)
- {
- EmitVectorImmOp(Context, Emit, true);
- }
- public static void EmitVectorImmOp(AILEmitterCtx Context, Action Emit, bool Binary)
- {
- AOpCodeSimdImm Op = (AOpCodeSimdImm)Context.CurrOp;
- int Bytes = Op.GetBitsCount() >> 3;
- int Elems = Bytes >> Op.Size;
- for (int Index = 0; Index < Elems; Index++)
- {
- if (Binary)
- {
- EmitVectorExtractZx(Context, Op.Rd, Index, Op.Size);
- }
- Context.EmitLdc_I8(Op.Imm);
- Emit();
- EmitVectorInsert(Context, Op.Rd, Index, Op.Size);
- }
- if (Op.RegisterSize == ARegisterSize.SIMD64)
- {
- EmitVectorZeroUpper(Context, Op.Rd);
- }
- }
- public static void EmitVectorWidenRmBinaryOpSx(AILEmitterCtx Context, Action Emit)
- {
- EmitVectorWidenRmBinaryOp(Context, Emit, true);
- }
- public static void EmitVectorWidenRmBinaryOpZx(AILEmitterCtx Context, Action Emit)
- {
- EmitVectorWidenRmBinaryOp(Context, Emit, false);
- }
- public static void EmitVectorWidenRmBinaryOp(AILEmitterCtx Context, Action Emit, bool Signed)
- {
- AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
- Context.EmitLdvec(Op.Rd);
- Context.EmitStvectmp();
- int Elems = 8 >> Op.Size;
- int Part = Op.RegisterSize == ARegisterSize.SIMD128 ? Elems : 0;
- for (int Index = 0; Index < Elems; Index++)
- {
- EmitVectorExtract(Context, Op.Rn, Index, Op.Size + 1, Signed);
- EmitVectorExtract(Context, Op.Rm, Part + Index, Op.Size, Signed);
- Emit();
- EmitVectorInsertTmp(Context, Index, Op.Size + 1);
- }
- Context.EmitLdvectmp();
- Context.EmitStvec(Op.Rd);
- }
- public static void EmitVectorWidenRnRmBinaryOpSx(AILEmitterCtx Context, Action Emit)
- {
- EmitVectorWidenRnRmOp(Context, Emit, false, true);
- }
- public static void EmitVectorWidenRnRmBinaryOpZx(AILEmitterCtx Context, Action Emit)
- {
- EmitVectorWidenRnRmOp(Context, Emit, false, false);
- }
- public static void EmitVectorWidenRnRmTernaryOpSx(AILEmitterCtx Context, Action Emit)
- {
- EmitVectorWidenRnRmOp(Context, Emit, true, true);
- }
- public static void EmitVectorWidenRnRmTernaryOpZx(AILEmitterCtx Context, Action Emit)
- {
- EmitVectorWidenRnRmOp(Context, Emit, true, false);
- }
- public static void EmitVectorWidenRnRmOp(AILEmitterCtx Context, Action Emit, bool Ternary, bool Signed)
- {
- AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
- Context.EmitLdvec(Op.Rd);
- Context.EmitStvectmp();
- int Elems = 8 >> Op.Size;
- int Part = Op.RegisterSize == ARegisterSize.SIMD128 ? Elems : 0;
- for (int Index = 0; Index < Elems; Index++)
- {
- if (Ternary)
- {
- EmitVectorExtract(Context, Op.Rd, Index, Op.Size + 1, Signed);
- }
- EmitVectorExtract(Context, Op.Rn, Part + Index, Op.Size, Signed);
- EmitVectorExtract(Context, Op.Rm, Part + Index, Op.Size, Signed);
- Emit();
- EmitVectorInsertTmp(Context, Index, Op.Size + 1);
- }
- Context.EmitLdvectmp();
- Context.EmitStvec(Op.Rd);
- }
- public static void EmitVectorPairwiseOpSx(AILEmitterCtx Context, Action Emit)
- {
- EmitVectorPairwiseOp(Context, Emit, true);
- }
- public static void EmitVectorPairwiseOpZx(AILEmitterCtx Context, Action Emit)
- {
- EmitVectorPairwiseOp(Context, Emit, false);
- }
- public static void EmitVectorPairwiseOp(AILEmitterCtx Context, Action Emit, bool Signed)
- {
- AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
- int Bytes = Op.GetBitsCount() >> 3;
- int Elems = Bytes >> Op.Size;
- int Half = Elems >> 1;
- for (int Index = 0; Index < Elems; Index++)
- {
- int Elem = (Index & (Half - 1)) << 1;
- EmitVectorExtract(Context, Index < Half ? Op.Rn : Op.Rm, Elem + 0, Op.Size, Signed);
- EmitVectorExtract(Context, Index < Half ? Op.Rn : Op.Rm, Elem + 1, Op.Size, Signed);
- Emit();
- EmitVectorInsertTmp(Context, Index, Op.Size);
- }
- Context.EmitLdvectmp();
- Context.EmitStvec(Op.Rd);
- if (Op.RegisterSize == ARegisterSize.SIMD64)
- {
- EmitVectorZeroUpper(Context, Op.Rd);
- }
- }
- public static void EmitScalarSaturatingNarrowOpSxSx(AILEmitterCtx Context, Action Emit)
- {
- EmitSaturatingNarrowOp(Context, Emit, true, true, true);
- }
- public static void EmitScalarSaturatingNarrowOpSxZx(AILEmitterCtx Context, Action Emit)
- {
- EmitSaturatingNarrowOp(Context, Emit, true, false, true);
- }
- public static void EmitScalarSaturatingNarrowOpZxZx(AILEmitterCtx Context, Action Emit)
- {
- EmitSaturatingNarrowOp(Context, Emit, false, false, true);
- }
- public static void EmitVectorSaturatingNarrowOpSxSx(AILEmitterCtx Context, Action Emit)
- {
- EmitSaturatingNarrowOp(Context, Emit, true, true, false);
- }
- public static void EmitVectorSaturatingNarrowOpSxZx(AILEmitterCtx Context, Action Emit)
- {
- EmitSaturatingNarrowOp(Context, Emit, true, false, false);
- }
- public static void EmitVectorSaturatingNarrowOpZxZx(AILEmitterCtx Context, Action Emit)
- {
- EmitSaturatingNarrowOp(Context, Emit, false, false, false);
- }
- public static void EmitSaturatingNarrowOp(
- AILEmitterCtx Context,
- Action Emit,
- bool SignedSrc,
- bool SignedDst,
- bool Scalar)
- {
- AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
- int Elems = !Scalar ? 8 >> Op.Size : 1;
- int ESize = 8 << Op.Size;
- int Part = !Scalar && (Op.RegisterSize == ARegisterSize.SIMD128) ? Elems : 0;
- long TMaxValue = SignedDst ? (1 << (ESize - 1)) - 1 : (1L << ESize) - 1L;
- long TMinValue = SignedDst ? -((1 << (ESize - 1))) : 0;
- Context.EmitLdc_I8(0L);
- Context.EmitSttmp();
- for (int Index = 0; Index < Elems; Index++)
- {
- AILLabel LblLe = new AILLabel();
- AILLabel LblGeEnd = new AILLabel();
- EmitVectorExtract(Context, Op.Rn, Index, Op.Size + 1, SignedSrc);
- Emit();
- Context.Emit(OpCodes.Dup);
- Context.EmitLdc_I8(TMaxValue);
- Context.Emit(SignedSrc ? OpCodes.Ble_S : OpCodes.Ble_Un_S, LblLe);
- Context.Emit(OpCodes.Pop);
- Context.EmitLdc_I8(TMaxValue);
- Context.EmitLdc_I8(0x8000000L);
- Context.EmitSttmp();
- Context.Emit(OpCodes.Br_S, LblGeEnd);
- Context.MarkLabel(LblLe);
- Context.Emit(OpCodes.Dup);
- Context.EmitLdc_I8(TMinValue);
- Context.Emit(SignedSrc ? OpCodes.Bge_S : OpCodes.Bge_Un_S, LblGeEnd);
- Context.Emit(OpCodes.Pop);
- Context.EmitLdc_I8(TMinValue);
- Context.EmitLdc_I8(0x8000000L);
- Context.EmitSttmp();
- Context.MarkLabel(LblGeEnd);
- if (Scalar)
- {
- EmitVectorZeroLower(Context, Op.Rd);
- }
- EmitVectorInsert(Context, Op.Rd, Part + Index, Op.Size);
- }
- if (Part == 0)
- {
- EmitVectorZeroUpper(Context, Op.Rd);
- }
- Context.EmitLdarg(ATranslatedSub.StateArgIdx);
- Context.EmitLdarg(ATranslatedSub.StateArgIdx);
- Context.EmitCallPropGet(typeof(AThreadState), nameof(AThreadState.Fpsr));
- Context.EmitLdtmp();
- Context.Emit(OpCodes.Conv_I4);
- Context.Emit(OpCodes.Or);
- Context.EmitCallPropSet(typeof(AThreadState), nameof(AThreadState.Fpsr));
- }
- public static void EmitScalarSet(AILEmitterCtx Context, int Reg, int Size)
- {
- EmitVectorZeroAll(Context, Reg);
- EmitVectorInsert(Context, Reg, 0, Size);
- }
- public static void EmitScalarSetF(AILEmitterCtx Context, int Reg, int Size)
- {
- EmitVectorZeroAll(Context, Reg);
- EmitVectorInsertF(Context, Reg, 0, Size);
- }
- public static void EmitVectorExtractSx(AILEmitterCtx Context, int Reg, int Index, int Size)
- {
- EmitVectorExtract(Context, Reg, Index, Size, true);
- }
- public static void EmitVectorExtractZx(AILEmitterCtx Context, int Reg, int Index, int Size)
- {
- EmitVectorExtract(Context, Reg, Index, Size, false);
- }
- public static void EmitVectorExtract(AILEmitterCtx Context, int Reg, int Index, int Size, bool Signed)
- {
- ThrowIfInvalid(Index, Size);
- IAOpCodeSimd Op = (IAOpCodeSimd)Context.CurrOp;
- Context.EmitLdvec(Reg);
- Context.EmitLdc_I4(Index);
- Context.EmitLdc_I4(Size);
- AVectorHelper.EmitCall(Context, Signed
- ? nameof(AVectorHelper.VectorExtractIntSx)
- : nameof(AVectorHelper.VectorExtractIntZx));
- }
- public static void EmitVectorExtractF(AILEmitterCtx Context, int Reg, int Index, int Size)
- {
- ThrowIfInvalidF(Index, Size);
- Context.EmitLdvec(Reg);
- Context.EmitLdc_I4(Index);
- if (Size == 0)
- {
- AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorExtractSingle));
- }
- else if (Size == 1)
- {
- AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorExtractDouble));
- }
- else
- {
- throw new ArgumentOutOfRangeException(nameof(Size));
- }
- }
- public static void EmitVectorZeroAll(AILEmitterCtx Context, int Rd)
- {
- EmitVectorZeroLower(Context, Rd);
- EmitVectorZeroUpper(Context, Rd);
- }
- public static void EmitVectorZeroLower(AILEmitterCtx Context, int Rd)
- {
- EmitVectorInsert(Context, Rd, 0, 3, 0);
- }
- public static void EmitVectorZeroUpper(AILEmitterCtx Context, int Rd)
- {
- EmitVectorInsert(Context, Rd, 1, 3, 0);
- }
- public static void EmitVectorInsert(AILEmitterCtx Context, int Reg, int Index, int Size)
- {
- ThrowIfInvalid(Index, Size);
- Context.EmitLdvec(Reg);
- Context.EmitLdc_I4(Index);
- Context.EmitLdc_I4(Size);
- AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorInsertInt));
- Context.EmitStvec(Reg);
- }
- public static void EmitVectorInsertTmp(AILEmitterCtx Context, int Index, int Size)
- {
- ThrowIfInvalid(Index, Size);
- Context.EmitLdvectmp();
- Context.EmitLdc_I4(Index);
- Context.EmitLdc_I4(Size);
- AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorInsertInt));
- Context.EmitStvectmp();
- }
- public static void EmitVectorInsert(AILEmitterCtx Context, int Reg, int Index, int Size, long Value)
- {
- ThrowIfInvalid(Index, Size);
- Context.EmitLdc_I8(Value);
- Context.EmitLdvec(Reg);
- Context.EmitLdc_I4(Index);
- Context.EmitLdc_I4(Size);
- AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorInsertInt));
- Context.EmitStvec(Reg);
- }
- public static void EmitVectorInsertF(AILEmitterCtx Context, int Reg, int Index, int Size)
- {
- ThrowIfInvalidF(Index, Size);
- Context.EmitLdvec(Reg);
- Context.EmitLdc_I4(Index);
- if (Size == 0)
- {
- AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorInsertSingle));
- }
- else if (Size == 1)
- {
- AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorInsertDouble));
- }
- else
- {
- throw new ArgumentOutOfRangeException(nameof(Size));
- }
- Context.EmitStvec(Reg);
- }
- public static void EmitVectorInsertTmpF(AILEmitterCtx Context, int Index, int Size)
- {
- ThrowIfInvalidF(Index, Size);
- Context.EmitLdvectmp();
- Context.EmitLdc_I4(Index);
- if (Size == 0)
- {
- AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorInsertSingle));
- }
- else if (Size == 1)
- {
- AVectorHelper.EmitCall(Context, nameof(AVectorHelper.VectorInsertDouble));
- }
- else
- {
- throw new ArgumentOutOfRangeException(nameof(Size));
- }
- Context.EmitStvectmp();
- }
- private static void ThrowIfInvalid(int Index, int Size)
- {
- if ((uint)Size > 3)
- {
- throw new ArgumentOutOfRangeException(nameof(Size));
- }
- if ((uint)Index >= 16 >> Size)
- {
- throw new ArgumentOutOfRangeException(nameof(Index));
- }
- }
- private static void ThrowIfInvalidF(int Index, int Size)
- {
- if ((uint)Size > 1)
- {
- throw new ArgumentOutOfRangeException(nameof(Size));
- }
- if ((uint)Index >= 4 >> Size)
- {
- throw new ArgumentOutOfRangeException(nameof(Index));
- }
- }
- }
- }
|