| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166 |
- using ARMeilleure.Decoders;
- using ARMeilleure.IntermediateRepresentation;
- using ARMeilleure.State;
- using ARMeilleure.Translation;
- using System;
- using System.Diagnostics;
- using static ARMeilleure.Instructions.InstEmitHelper;
- using static ARMeilleure.Instructions.InstEmitSimdHelper;
- using static ARMeilleure.IntermediateRepresentation.OperandHelper;
- namespace ARMeilleure.Instructions
- {
- using Func1I = Func<Operand, Operand>;
- static partial class InstEmit
- {
- public static void Fcvt_S(ArmEmitterContext context)
- {
- OpCodeSimd op = (OpCodeSimd)context.CurrOp;
- if (op.Size == 0 && op.Opc == 1) // Single -> Double.
- {
- if (Optimizations.UseSse2)
- {
- Operand n = GetVec(op.Rn);
- Operand res = context.AddIntrinsic(Intrinsic.X86Cvtss2sd, context.VectorZero(), n);
- context.Copy(GetVec(op.Rd), res);
- }
- else
- {
- Operand ne = context.VectorExtract(OperandType.FP32, GetVec(op.Rn), 0);
- Operand res = context.ConvertToFP(OperandType.FP64, ne);
- context.Copy(GetVec(op.Rd), context.VectorInsert(context.VectorZero(), res, 0));
- }
- }
- else if (op.Size == 1 && op.Opc == 0) // Double -> Single.
- {
- if (Optimizations.UseSse2)
- {
- Operand n = GetVec(op.Rn);
- Operand res = context.AddIntrinsic(Intrinsic.X86Cvtsd2ss, context.VectorZero(), n);
- context.Copy(GetVec(op.Rd), res);
- }
- else
- {
- Operand ne = context.VectorExtract(OperandType.FP64, GetVec(op.Rn), 0);
- Operand res = context.ConvertToFP(OperandType.FP32, ne);
- context.Copy(GetVec(op.Rd), context.VectorInsert(context.VectorZero(), res, 0));
- }
- }
- else if (op.Size == 0 && op.Opc == 3) // Single -> Half.
- {
- Operand ne = context.VectorExtract(OperandType.FP32, GetVec(op.Rn), 0);
- Delegate dlg = new _U16_F32(SoftFloat32_16.FPConvert);
- Operand res = context.Call(dlg, ne);
- res = context.ZeroExtend16(OperandType.I64, res);
- context.Copy(GetVec(op.Rd), EmitVectorInsert(context, context.VectorZero(), res, 0, 1));
- }
- else if (op.Size == 3 && op.Opc == 0) // Half -> Single.
- {
- Operand ne = EmitVectorExtractZx(context, op.Rn, 0, 1);
- Delegate dlg = new _F32_U16(SoftFloat16_32.FPConvert);
- Operand res = context.Call(dlg, ne);
- context.Copy(GetVec(op.Rd), context.VectorInsert(context.VectorZero(), res, 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.
- {
- Debug.Assert(false, $"type == {op.Size} && opc == {op.Opc}");
- }
- }
- public static void Fcvtas_Gp(ArmEmitterContext context)
- {
- EmitFcvt_s_Gp(context, (op1) => EmitRoundMathCall(context, MidpointRounding.AwayFromZero, op1));
- }
- public static void Fcvtau_Gp(ArmEmitterContext context)
- {
- EmitFcvt_u_Gp(context, (op1) => EmitRoundMathCall(context, MidpointRounding.AwayFromZero, op1));
- }
- public static void Fcvtl_V(ArmEmitterContext context)
- {
- OpCodeSimd op = (OpCodeSimd)context.CurrOp;
- int sizeF = op.Size & 1;
- if (Optimizations.UseSse2 && sizeF == 1)
- {
- Operand n = GetVec(op.Rn);
- Operand res;
- if (op.RegisterSize == RegisterSize.Simd128)
- {
- res = context.AddIntrinsic(Intrinsic.X86Movhlps, n, n);
- }
- else
- {
- res = n;
- }
- res = context.AddIntrinsic(Intrinsic.X86Cvtps2pd, res);
- context.Copy(GetVec(op.Rd), res);
- }
- else
- {
- Operand res = context.VectorZero();
- int elems = 4 >> sizeF;
- int part = op.RegisterSize == RegisterSize.Simd128 ? elems : 0;
- for (int index = 0; index < elems; index++)
- {
- if (sizeF == 0)
- {
- Operand ne = EmitVectorExtractZx(context, op.Rn, part + index, 1);
- Delegate dlg = new _F32_U16(SoftFloat16_32.FPConvert);
- Operand e = context.Call(dlg, ne);
- res = context.VectorInsert(res, e, index);
- }
- else /* if (sizeF == 1) */
- {
- Operand ne = context.VectorExtract(OperandType.FP32, GetVec(op.Rn), part + index);
- Operand e = context.ConvertToFP(OperandType.FP64, ne);
- res = context.VectorInsert(res, e, index);
- }
- }
- context.Copy(GetVec(op.Rd), res);
- }
- }
- public static void Fcvtms_Gp(ArmEmitterContext context)
- {
- EmitFcvt_s_Gp(context, (op1) => EmitUnaryMathCall(context, MathF.Floor, Math.Floor, op1));
- }
- public static void Fcvtmu_Gp(ArmEmitterContext context)
- {
- EmitFcvt_u_Gp(context, (op1) => EmitUnaryMathCall(context, MathF.Floor, Math.Floor, op1));
- }
- public static void Fcvtn_V(ArmEmitterContext context)
- {
- OpCodeSimd op = (OpCodeSimd)context.CurrOp;
- int sizeF = op.Size & 1;
- if (Optimizations.UseSse2 && sizeF == 1)
- {
- Operand d = GetVec(op.Rd);
- Operand n = GetVec(op.Rn);
- Operand res = context.AddIntrinsic(Intrinsic.X86Movlhps, d, context.VectorZero());
- Operand nInt = context.AddIntrinsic(Intrinsic.X86Cvtpd2ps, n);
- nInt = context.AddIntrinsic(Intrinsic.X86Movlhps, nInt, nInt);
- Intrinsic movInst = op.RegisterSize == RegisterSize.Simd128
- ? Intrinsic.X86Movlhps
- : Intrinsic.X86Movhlps;
- res = context.AddIntrinsic(movInst, res, nInt);
- context.Copy(GetVec(op.Rd), res);
- }
- else
- {
- OperandType type = sizeF == 0 ? OperandType.FP32 : OperandType.FP64;
- int elems = 4 >> sizeF;
- int part = op.RegisterSize == RegisterSize.Simd128 ? elems : 0;
- Operand res = part == 0 ? context.VectorZero() : context.Copy(GetVec(op.Rd));
- for (int index = 0; index < elems; index++)
- {
- Operand ne = context.VectorExtract(type, GetVec(op.Rn), 0);
- if (sizeF == 0)
- {
- Delegate dlg = new _U16_F32(SoftFloat32_16.FPConvert);
- Operand e = context.Call(dlg, ne);
- e = context.ZeroExtend16(OperandType.I64, e);
- res = EmitVectorInsert(context, res, e, part + index, 1);
- }
- else /* if (sizeF == 1) */
- {
- Operand e = context.ConvertToFP(OperandType.FP32, ne);
- res = context.VectorInsert(res, e, part + index);
- }
- }
- context.Copy(GetVec(op.Rd), res);
- }
- }
- public static void Fcvtns_S(ArmEmitterContext context)
- {
- if (Optimizations.UseSse41)
- {
- EmitSse41Fcvts(context, FPRoundingMode.ToNearest, scalar: true);
- }
- else
- {
- EmitFcvtn(context, signed: true, scalar: true);
- }
- }
- public static void Fcvtns_V(ArmEmitterContext context)
- {
- if (Optimizations.UseSse41)
- {
- EmitSse41Fcvts(context, FPRoundingMode.ToNearest, scalar: false);
- }
- else
- {
- EmitFcvtn(context, signed: true, scalar: false);
- }
- }
- public static void Fcvtnu_S(ArmEmitterContext context)
- {
- if (Optimizations.UseSse41)
- {
- EmitSse41Fcvtu(context, FPRoundingMode.ToNearest, scalar: true);
- }
- else
- {
- EmitFcvtn(context, signed: false, scalar: true);
- }
- }
- public static void Fcvtnu_V(ArmEmitterContext context)
- {
- if (Optimizations.UseSse41)
- {
- EmitSse41Fcvtu(context, FPRoundingMode.ToNearest, scalar: false);
- }
- else
- {
- EmitFcvtn(context, signed: false, scalar: false);
- }
- }
- public static void Fcvtps_Gp(ArmEmitterContext context)
- {
- EmitFcvt_s_Gp(context, (op1) => EmitUnaryMathCall(context, MathF.Ceiling, Math.Ceiling, op1));
- }
- public static void Fcvtpu_Gp(ArmEmitterContext context)
- {
- EmitFcvt_u_Gp(context, (op1) => EmitUnaryMathCall(context, MathF.Ceiling, Math.Ceiling, op1));
- }
- public static void Fcvtzs_Gp(ArmEmitterContext context)
- {
- EmitFcvt_s_Gp(context, (op1) => op1);
- }
- public static void Fcvtzs_Gp_Fixed(ArmEmitterContext context)
- {
- EmitFcvtzs_Gp_Fixed(context);
- }
- public static void Fcvtzs_S(ArmEmitterContext context)
- {
- if (Optimizations.UseSse41)
- {
- EmitSse41Fcvts(context, FPRoundingMode.TowardsZero, scalar: true);
- }
- else
- {
- EmitFcvtz(context, signed: true, scalar: true);
- }
- }
- public static void Fcvtzs_V(ArmEmitterContext context)
- {
- if (Optimizations.UseSse41)
- {
- EmitSse41Fcvts(context, FPRoundingMode.TowardsZero, scalar: false);
- }
- else
- {
- EmitFcvtz(context, signed: true, scalar: false);
- }
- }
- public static void Fcvtzs_V_Fixed(ArmEmitterContext context)
- {
- if (Optimizations.UseSse41)
- {
- EmitSse41Fcvts(context, FPRoundingMode.TowardsZero, scalar: false);
- }
- else
- {
- EmitFcvtz(context, signed: true, scalar: false);
- }
- }
- public static void Fcvtzu_Gp(ArmEmitterContext context)
- {
- EmitFcvt_u_Gp(context, (op1) => op1);
- }
- public static void Fcvtzu_Gp_Fixed(ArmEmitterContext context)
- {
- EmitFcvtzu_Gp_Fixed(context);
- }
- public static void Fcvtzu_S(ArmEmitterContext context)
- {
- if (Optimizations.UseSse41)
- {
- EmitSse41Fcvtu(context, FPRoundingMode.TowardsZero, scalar: true);
- }
- else
- {
- EmitFcvtz(context, signed: false, scalar: true);
- }
- }
- public static void Fcvtzu_V(ArmEmitterContext context)
- {
- if (Optimizations.UseSse41)
- {
- EmitSse41Fcvtu(context, FPRoundingMode.TowardsZero, scalar: false);
- }
- else
- {
- EmitFcvtz(context, signed: false, scalar: false);
- }
- }
- public static void Fcvtzu_V_Fixed(ArmEmitterContext context)
- {
- if (Optimizations.UseSse41)
- {
- EmitSse41Fcvtu(context, FPRoundingMode.TowardsZero, scalar: false);
- }
- else
- {
- EmitFcvtz(context, signed: false, scalar: false);
- }
- }
- public static void Scvtf_Gp(ArmEmitterContext context)
- {
- OpCodeSimdCvt op = (OpCodeSimdCvt)context.CurrOp;
- Operand res = GetIntOrZR(context, op.Rn);
- if (op.RegisterSize == RegisterSize.Int32)
- {
- res = context.SignExtend32(OperandType.I64, res);
- }
- res = EmitFPConvert(context, res, op.Size, signed: true);
- context.Copy(GetVec(op.Rd), context.VectorInsert(context.VectorZero(), res, 0));
- }
- public static void Scvtf_Gp_Fixed(ArmEmitterContext context)
- {
- OpCodeSimdCvt op = (OpCodeSimdCvt)context.CurrOp;
- Operand res = GetIntOrZR(context, op.Rn);
- if (op.RegisterSize == RegisterSize.Int32)
- {
- res = context.SignExtend32(OperandType.I64, res);
- }
- res = EmitFPConvert(context, res, op.Size, signed: true);
- res = EmitI2fFBitsMul(context, res, op.FBits);
- context.Copy(GetVec(op.Rd), context.VectorInsert(context.VectorZero(), res, 0));
- }
- public static void Scvtf_S(ArmEmitterContext context)
- {
- OpCodeSimd op = (OpCodeSimd)context.CurrOp;
- int sizeF = op.Size & 1;
- if (Optimizations.UseSse2 && sizeF == 0)
- {
- EmitSse2Scvtf(context, scalar: true);
- }
- else
- {
- Operand res = EmitVectorLongExtract(context, op.Rn, 0, sizeF + 2);
- res = EmitFPConvert(context, res, op.Size, signed: true);
- context.Copy(GetVec(op.Rd), context.VectorInsert(context.VectorZero(), res, 0));
- }
- }
- public static void Scvtf_V(ArmEmitterContext context)
- {
- OpCodeSimd op = (OpCodeSimd)context.CurrOp;
- int sizeF = op.Size & 1;
- if (Optimizations.UseSse2 && sizeF == 0)
- {
- EmitSse2Scvtf(context, scalar: false);
- }
- else
- {
- EmitVectorCvtf(context, signed: true);
- }
- }
- public static void Scvtf_V_Fixed(ArmEmitterContext context)
- {
- OpCodeSimd op = (OpCodeSimd)context.CurrOp;
- // sizeF == ((OpCodeSimdShImm64)op).Size - 2
- int sizeF = op.Size & 1;
- if (Optimizations.UseSse2 && sizeF == 0)
- {
- EmitSse2Scvtf(context, scalar: false);
- }
- else
- {
- EmitVectorCvtf(context, signed: true);
- }
- }
- public static void Ucvtf_Gp(ArmEmitterContext context)
- {
- OpCodeSimdCvt op = (OpCodeSimdCvt)context.CurrOp;
- Operand res = GetIntOrZR(context, op.Rn);
- res = EmitFPConvert(context, res, op.Size, signed: false);
- context.Copy(GetVec(op.Rd), context.VectorInsert(context.VectorZero(), res, 0));
- }
- public static void Ucvtf_Gp_Fixed(ArmEmitterContext context)
- {
- OpCodeSimdCvt op = (OpCodeSimdCvt)context.CurrOp;
- Operand res = GetIntOrZR(context, op.Rn);
- res = EmitFPConvert(context, res, op.Size, signed: false);
- res = EmitI2fFBitsMul(context, res, op.FBits);
- context.Copy(GetVec(op.Rd), context.VectorInsert(context.VectorZero(), res, 0));
- }
- public static void Ucvtf_S(ArmEmitterContext context)
- {
- OpCodeSimd op = (OpCodeSimd)context.CurrOp;
- int sizeF = op.Size & 1;
- if (Optimizations.UseSse2 && sizeF == 0)
- {
- EmitSse2Ucvtf(context, scalar: true);
- }
- else
- {
- Operand ne = EmitVectorLongExtract(context, op.Rn, 0, sizeF + 2);
- Operand res = EmitFPConvert(context, ne, sizeF, signed: false);
- context.Copy(GetVec(op.Rd), context.VectorInsert(context.VectorZero(), res, 0));
- }
- }
- public static void Ucvtf_V(ArmEmitterContext context)
- {
- OpCodeSimd op = (OpCodeSimd)context.CurrOp;
- int sizeF = op.Size & 1;
- if (Optimizations.UseSse2 && sizeF == 0)
- {
- EmitSse2Ucvtf(context, scalar: false);
- }
- else
- {
- EmitVectorCvtf(context, signed: false);
- }
- }
- public static void Ucvtf_V_Fixed(ArmEmitterContext context)
- {
- OpCodeSimd op = (OpCodeSimd)context.CurrOp;
- // sizeF == ((OpCodeSimdShImm)op).Size - 2
- int sizeF = op.Size & 1;
- if (Optimizations.UseSse2 && sizeF == 0)
- {
- EmitSse2Ucvtf(context, scalar: false);
- }
- else
- {
- EmitVectorCvtf(context, signed: false);
- }
- }
- private static void EmitFcvtn(ArmEmitterContext context, bool signed, bool scalar)
- {
- OpCodeSimd op = (OpCodeSimd)context.CurrOp;
- Operand res = context.VectorZero();
- Operand n = GetVec(op.Rn);
- int sizeF = op.Size & 1;
- int sizeI = sizeF + 2;
- OperandType type = sizeF == 0 ? OperandType.FP32 : OperandType.FP64;
- int elems = !scalar ? op.GetBytesCount() >> sizeI : 1;
- for (int index = 0; index < elems; index++)
- {
- Operand ne = context.VectorExtract(type, n, index);
- Operand e = EmitRoundMathCall(context, MidpointRounding.ToEven, ne);
- if (sizeF == 0)
- {
- Delegate dlg = signed
- ? (Delegate)new _S32_F32(SoftFallback.SatF32ToS32)
- : (Delegate)new _U32_F32(SoftFallback.SatF32ToU32);
- e = context.Call(dlg, e);
- e = context.ZeroExtend32(OperandType.I64, e);
- }
- else /* if (sizeF == 1) */
- {
- Delegate dlg = signed
- ? (Delegate)new _S64_F64(SoftFallback.SatF64ToS64)
- : (Delegate)new _U64_F64(SoftFallback.SatF64ToU64);
- e = context.Call(dlg, e);
- }
- res = EmitVectorInsert(context, res, e, index, sizeI);
- }
- context.Copy(GetVec(op.Rd), res);
- }
- private static void EmitFcvtz(ArmEmitterContext context, bool signed, bool scalar)
- {
- OpCodeSimd op = (OpCodeSimd)context.CurrOp;
- Operand res = context.VectorZero();
- Operand n = GetVec(op.Rn);
- int sizeF = op.Size & 1;
- int sizeI = sizeF + 2;
- OperandType type = sizeF == 0 ? OperandType.FP32 : OperandType.FP64;
- int fBits = GetFBits(context);
- int elems = !scalar ? op.GetBytesCount() >> sizeI : 1;
- for (int index = 0; index < elems; index++)
- {
- Operand ne = context.VectorExtract(type, n, index);
- Operand e = EmitF2iFBitsMul(context, ne, fBits);
- if (sizeF == 0)
- {
- Delegate dlg = signed
- ? (Delegate)new _S32_F32(SoftFallback.SatF32ToS32)
- : (Delegate)new _U32_F32(SoftFallback.SatF32ToU32);
- e = context.Call(dlg, e);
- e = context.ZeroExtend32(OperandType.I64, e);
- }
- else /* if (sizeF == 1) */
- {
- Delegate dlg = signed
- ? (Delegate)new _S64_F64(SoftFallback.SatF64ToS64)
- : (Delegate)new _U64_F64(SoftFallback.SatF64ToU64);
- e = context.Call(dlg, e);
- }
- res = EmitVectorInsert(context, res, e, index, sizeI);
- }
- context.Copy(GetVec(op.Rd), res);
- }
- private static void EmitFcvt_s_Gp(ArmEmitterContext context, Func1I emit)
- {
- EmitFcvt___Gp(context, emit, signed: true);
- }
- private static void EmitFcvt_u_Gp(ArmEmitterContext context, Func1I emit)
- {
- EmitFcvt___Gp(context, emit, signed: false);
- }
- private static void EmitFcvt___Gp(ArmEmitterContext context, Func1I emit, bool signed)
- {
- OpCodeSimdCvt op = (OpCodeSimdCvt)context.CurrOp;
- OperandType type = op.Size == 0 ? OperandType.FP32 : OperandType.FP64;
- Operand ne = context.VectorExtract(type, GetVec(op.Rn), 0);
- Operand res = signed
- ? EmitScalarFcvts(context, emit(ne), 0)
- : EmitScalarFcvtu(context, emit(ne), 0);
- SetIntOrZR(context, op.Rd, res);
- }
- private static void EmitFcvtzs_Gp_Fixed(ArmEmitterContext context)
- {
- EmitFcvtz__Gp_Fixed(context, signed: true);
- }
- private static void EmitFcvtzu_Gp_Fixed(ArmEmitterContext context)
- {
- EmitFcvtz__Gp_Fixed(context, signed: false);
- }
- private static void EmitFcvtz__Gp_Fixed(ArmEmitterContext context, bool signed)
- {
- OpCodeSimdCvt op = (OpCodeSimdCvt)context.CurrOp;
- OperandType type = op.Size == 0 ? OperandType.FP32 : OperandType.FP64;
- Operand ne = context.VectorExtract(type, GetVec(op.Rn), 0);
- Operand res = signed
- ? EmitScalarFcvts(context, ne, op.FBits)
- : EmitScalarFcvtu(context, ne, op.FBits);
- SetIntOrZR(context, op.Rd, res);
- }
- private static void EmitVectorCvtf(ArmEmitterContext context, bool signed)
- {
- OpCodeSimd op = (OpCodeSimd)context.CurrOp;
- Operand res = context.VectorZero();
- int sizeF = op.Size & 1;
- int sizeI = sizeF + 2;
- int fBits = GetFBits(context);
- int elems = op.GetBytesCount() >> sizeI;
- for (int index = 0; index < elems; index++)
- {
- Operand ne = EmitVectorLongExtract(context, op.Rn, index, sizeI);
- Operand e = EmitFPConvert(context, ne, sizeF, signed);
- e = EmitI2fFBitsMul(context, e, fBits);
- res = context.VectorInsert(res, e, index);
- }
- context.Copy(GetVec(op.Rd), res);
- }
- private static int GetFBits(ArmEmitterContext context)
- {
- if (context.CurrOp is OpCodeSimdShImm op)
- {
- return GetImmShr(op);
- }
- return 0;
- }
- private static Operand EmitFPConvert(ArmEmitterContext context, Operand value, int size, bool signed)
- {
- Debug.Assert(value.Type == OperandType.I32 || value.Type == OperandType.I64);
- Debug.Assert((uint)size < 2);
- OperandType type = size == 0 ? OperandType.FP32
- : OperandType.FP64;
- if (signed)
- {
- return context.ConvertToFP(type, value);
- }
- else
- {
- return context.ConvertToFPUI(type, value);
- }
- }
- private static Operand EmitScalarFcvts(ArmEmitterContext context, Operand value, int fBits)
- {
- Debug.Assert(value.Type == OperandType.FP32 || value.Type == OperandType.FP64);
- value = EmitF2iFBitsMul(context, value, fBits);
- if (context.CurrOp.RegisterSize == RegisterSize.Int32)
- {
- Delegate dlg = value.Type == OperandType.FP32
- ? (Delegate)new _S32_F32(SoftFallback.SatF32ToS32)
- : (Delegate)new _S32_F64(SoftFallback.SatF64ToS32);
- return context.Call(dlg, value);
- }
- else
- {
- Delegate dlg = value.Type == OperandType.FP32
- ? (Delegate)new _S64_F32(SoftFallback.SatF32ToS64)
- : (Delegate)new _S64_F64(SoftFallback.SatF64ToS64);
- return context.Call(dlg, value);
- }
- }
- private static Operand EmitScalarFcvtu(ArmEmitterContext context, Operand value, int fBits)
- {
- Debug.Assert(value.Type == OperandType.FP32 || value.Type == OperandType.FP64);
- value = EmitF2iFBitsMul(context, value, fBits);
- if (context.CurrOp.RegisterSize == RegisterSize.Int32)
- {
- Delegate dlg = value.Type == OperandType.FP32
- ? (Delegate)new _U32_F32(SoftFallback.SatF32ToU32)
- : (Delegate)new _U32_F64(SoftFallback.SatF64ToU32);
- return context.Call(dlg, value);
- }
- else
- {
- Delegate dlg = value.Type == OperandType.FP32
- ? (Delegate)new _U64_F32(SoftFallback.SatF32ToU64)
- : (Delegate)new _U64_F64(SoftFallback.SatF64ToU64);
- return context.Call(dlg, value);
- }
- }
- private static Operand EmitF2iFBitsMul(ArmEmitterContext context, Operand value, int fBits)
- {
- Debug.Assert(value.Type == OperandType.FP32 || value.Type == OperandType.FP64);
- if (fBits == 0)
- {
- return value;
- }
- if (value.Type == OperandType.FP32)
- {
- return context.Multiply(value, ConstF(MathF.Pow(2f, fBits)));
- }
- else /* if (value.Type == OperandType.FP64) */
- {
- return context.Multiply(value, ConstF(Math.Pow(2d, fBits)));
- }
- }
- private static Operand EmitI2fFBitsMul(ArmEmitterContext context, Operand value, int fBits)
- {
- Debug.Assert(value.Type == OperandType.FP32 || value.Type == OperandType.FP64);
- if (fBits == 0)
- {
- return value;
- }
- if (value.Type == OperandType.FP32)
- {
- return context.Multiply(value, ConstF(1f / MathF.Pow(2f, fBits)));
- }
- else /* if (value.Type == OperandType.FP64) */
- {
- return context.Multiply(value, ConstF(1d / Math.Pow(2d, fBits)));
- }
- }
- private static void EmitSse41Fcvts(ArmEmitterContext context, FPRoundingMode roundMode, bool scalar)
- {
- OpCodeSimd op = (OpCodeSimd)context.CurrOp;
- Operand n = GetVec(op.Rn);
- const int cmpGreaterThanOrEqual = 5;
- const int cmpOrdered = 7;
- // sizeF == ((OpCodeSimdShImm64)op).Size - 2
- int sizeF = op.Size & 1;
- if (sizeF == 0)
- {
- Operand nMask = context.AddIntrinsic(Intrinsic.X86Cmpps, n, n, Const(cmpOrdered));
- Operand nScaled = context.AddIntrinsic(Intrinsic.X86Pand, nMask, n);
- if (op is OpCodeSimdShImm fixedOp)
- {
- int fBits = GetImmShr(fixedOp);
- // BitConverter.Int32BitsToSingle(fpScaled) == MathF.Pow(2f, fBits)
- int fpScaled = 0x3F800000 + fBits * 0x800000;
- Operand scale = X86GetAllElements(context, fpScaled);
- nScaled = context.AddIntrinsic(Intrinsic.X86Mulps, nScaled, scale);
- }
- Operand nRnd = context.AddIntrinsic(Intrinsic.X86Roundps, nScaled, Const(X86GetRoundControl(roundMode)));
- Operand nInt = context.AddIntrinsic(Intrinsic.X86Cvtps2dq, nRnd);
- Operand mask = X86GetAllElements(context, 0x4F000000); // 2.14748365E9f (2147483648)
- Operand mask2 = context.AddIntrinsic(Intrinsic.X86Cmpps, nRnd, mask, Const(cmpGreaterThanOrEqual));
- Operand res = context.AddIntrinsic(Intrinsic.X86Pxor, nInt, mask2);
- if (scalar)
- {
- res = context.VectorZeroUpper96(res);
- }
- else if (op.RegisterSize == RegisterSize.Simd64)
- {
- res = context.VectorZeroUpper64(res);
- }
- context.Copy(GetVec(op.Rd), res);
- }
- else /* if (sizeF == 1) */
- {
- Operand nMask = context.AddIntrinsic(Intrinsic.X86Cmppd, n, n, Const(cmpOrdered));
- Operand nScaled = context.AddIntrinsic(Intrinsic.X86Pand, nMask, n);
- if (op is OpCodeSimdShImm fixedOp)
- {
- int fBits = GetImmShr(fixedOp);
- // BitConverter.Int64BitsToDouble(fpScaled) == Math.Pow(2d, fBits)
- long fpScaled = 0x3FF0000000000000L + fBits * 0x10000000000000L;
- Operand scale = X86GetAllElements(context, fpScaled);
- nScaled = context.AddIntrinsic(Intrinsic.X86Mulpd, nScaled, scale);
- }
- Operand nRnd = context.AddIntrinsic(Intrinsic.X86Roundpd, nScaled, Const(X86GetRoundControl(roundMode)));
- Operand high;
- if (!scalar)
- {
- high = context.AddIntrinsic(Intrinsic.X86Unpckhpd, nRnd, nRnd);
- high = context.AddIntrinsicLong(Intrinsic.X86Cvtsd2si, high);
- }
- else
- {
- high = Const(0L);
- }
- Operand low = context.AddIntrinsicLong(Intrinsic.X86Cvtsd2si, nRnd);
- Operand nInt = EmitVectorLongCreate(context, low, high);
- Operand mask = X86GetAllElements(context, 0x43E0000000000000L); // 9.2233720368547760E18d (9223372036854775808)
- Operand mask2 = context.AddIntrinsic(Intrinsic.X86Cmppd, nRnd, mask, Const(cmpGreaterThanOrEqual));
- Operand res = context.AddIntrinsic(Intrinsic.X86Pxor, nInt, mask2);
- if (scalar)
- {
- res = context.VectorZeroUpper64(res);
- }
- context.Copy(GetVec(op.Rd), res);
- }
- }
- private static void EmitSse41Fcvtu(ArmEmitterContext context, FPRoundingMode roundMode, bool scalar)
- {
- OpCodeSimd op = (OpCodeSimd)context.CurrOp;
- Operand n = GetVec(op.Rn);
- const int cmpGreaterThanOrEqual = 5;
- const int cmpGreaterThan = 6;
- const int cmpOrdered = 7;
- // sizeF == ((OpCodeSimdShImm)op).Size - 2
- int sizeF = op.Size & 1;
- if (sizeF == 0)
- {
- Operand nMask = context.AddIntrinsic(Intrinsic.X86Cmpps, n, n, Const(cmpOrdered));
- Operand nScaled = context.AddIntrinsic(Intrinsic.X86Pand, nMask, n);
- if (op is OpCodeSimdShImm fixedOp)
- {
- int fBits = GetImmShr(fixedOp);
- // BitConverter.Int32BitsToSingle(fpScaled) == MathF.Pow(2f, fBits)
- int fpScaled = 0x3F800000 + fBits * 0x800000;
- Operand scale = X86GetAllElements(context, fpScaled);
- nScaled = context.AddIntrinsic(Intrinsic.X86Mulps, nScaled, scale);
- }
- Operand nRnd = context.AddIntrinsic(Intrinsic.X86Roundps, nScaled, Const(X86GetRoundControl(roundMode)));
- Operand nRndMask = context.AddIntrinsic(Intrinsic.X86Cmpps, nRnd, context.VectorZero(), Const(cmpGreaterThan));
- Operand nRndMasked = context.AddIntrinsic(Intrinsic.X86Pand, nRnd, nRndMask);
- Operand nInt = context.AddIntrinsic(Intrinsic.X86Cvtps2dq, nRndMasked);
- Operand mask = X86GetAllElements(context, 0x4F000000); // 2.14748365E9f (2147483648)
- Operand res = context.AddIntrinsic(Intrinsic.X86Subps, nRndMasked, mask);
- Operand mask2 = context.AddIntrinsic(Intrinsic.X86Cmpps, res, context.VectorZero(), Const(cmpGreaterThan));
- Operand resMasked = context.AddIntrinsic(Intrinsic.X86Pand, res, mask2);
- res = context.AddIntrinsic(Intrinsic.X86Cvtps2dq, resMasked);
- Operand mask3 = context.AddIntrinsic(Intrinsic.X86Cmpps, resMasked, mask, Const(cmpGreaterThanOrEqual));
- res = context.AddIntrinsic(Intrinsic.X86Pxor, res, mask3);
- res = context.AddIntrinsic(Intrinsic.X86Paddd, res, nInt);
- if (scalar)
- {
- res = context.VectorZeroUpper96(res);
- }
- else if (op.RegisterSize == RegisterSize.Simd64)
- {
- res = context.VectorZeroUpper64(res);
- }
- context.Copy(GetVec(op.Rd), res);
- }
- else /* if (sizeF == 1) */
- {
- Operand nMask = context.AddIntrinsic(Intrinsic.X86Cmppd, n, n, Const(cmpOrdered));
- Operand nScaled = context.AddIntrinsic(Intrinsic.X86Pand, nMask, n);
- if (op is OpCodeSimdShImm fixedOp)
- {
- int fBits = GetImmShr(fixedOp);
- // BitConverter.Int64BitsToDouble(fpScaled) == Math.Pow(2d, fBits)
- long fpScaled = 0x3FF0000000000000L + fBits * 0x10000000000000L;
- Operand scale = X86GetAllElements(context, fpScaled);
- nScaled = context.AddIntrinsic(Intrinsic.X86Mulpd, nScaled, scale);
- }
- Operand nRnd = context.AddIntrinsic(Intrinsic.X86Roundpd, nScaled, Const(X86GetRoundControl(roundMode)));
- Operand nRndMask = context.AddIntrinsic(Intrinsic.X86Cmppd, nRnd, context.VectorZero(), Const(cmpGreaterThan));
- Operand nRndMasked = context.AddIntrinsic(Intrinsic.X86Pand, nRnd, nRndMask);
- Operand high;
- if (!scalar)
- {
- high = context.AddIntrinsic(Intrinsic.X86Unpckhpd, nRndMasked, nRndMasked);
- high = context.AddIntrinsicLong(Intrinsic.X86Cvtsd2si, high);
- }
- else
- {
- high = Const(0L);
- }
- Operand low = context.AddIntrinsicLong(Intrinsic.X86Cvtsd2si, nRndMasked);
- Operand nInt = EmitVectorLongCreate(context, low, high);
- Operand mask = X86GetAllElements(context, 0x43E0000000000000L); // 9.2233720368547760E18d (9223372036854775808)
- Operand res = context.AddIntrinsic(Intrinsic.X86Subpd, nRndMasked, mask);
- Operand mask2 = context.AddIntrinsic(Intrinsic.X86Cmppd, res, context.VectorZero(), Const(cmpGreaterThan));
- Operand resMasked = context.AddIntrinsic(Intrinsic.X86Pand, res, mask2);
- if (!scalar)
- {
- high = context.AddIntrinsic(Intrinsic.X86Unpckhpd, resMasked, resMasked);
- high = context.AddIntrinsicLong(Intrinsic.X86Cvtsd2si, high);
- }
- low = context.AddIntrinsicLong(Intrinsic.X86Cvtsd2si, resMasked);
- res = EmitVectorLongCreate(context, low, high);
- Operand mask3 = context.AddIntrinsic(Intrinsic.X86Cmppd, resMasked, mask, Const(cmpGreaterThanOrEqual));
- res = context.AddIntrinsic(Intrinsic.X86Pxor, res, mask3);
- res = context.AddIntrinsic(Intrinsic.X86Paddq, res, nInt);
- if (scalar)
- {
- res = context.VectorZeroUpper64(res);
- }
- context.Copy(GetVec(op.Rd), res);
- }
- }
- private static void EmitSse2Scvtf(ArmEmitterContext context, bool scalar)
- {
- OpCodeSimd op = (OpCodeSimd)context.CurrOp;
- Operand n = GetVec(op.Rn);
- Operand res = context.AddIntrinsic(Intrinsic.X86Cvtdq2ps, n);
- if (op is OpCodeSimdShImm fixedOp)
- {
- int fBits = GetImmShr(fixedOp);
- // BitConverter.Int32BitsToSingle(fpScaled) == 1f / MathF.Pow(2f, fBits)
- int fpScaled = 0x3F800000 - fBits * 0x800000;
- Operand scale = X86GetAllElements(context, fpScaled);
- res = context.AddIntrinsic(Intrinsic.X86Mulps, res, scale);
- }
- if (scalar)
- {
- res = context.VectorZeroUpper96(res);
- }
- else if (op.RegisterSize == RegisterSize.Simd64)
- {
- res = context.VectorZeroUpper64(res);
- }
- context.Copy(GetVec(op.Rd), res);
- }
- private static void EmitSse2Ucvtf(ArmEmitterContext context, bool scalar)
- {
- OpCodeSimd op = (OpCodeSimd)context.CurrOp;
- Operand n = GetVec(op.Rn);
- Operand res = context.AddIntrinsic(Intrinsic.X86Psrld, n, Const(16));
- res = context.AddIntrinsic(Intrinsic.X86Cvtdq2ps, res);
- Operand mask = X86GetAllElements(context, 0x47800000); // 65536.0f (1 << 16)
- res = context.AddIntrinsic(Intrinsic.X86Mulps, res, mask);
- Operand res2 = context.AddIntrinsic(Intrinsic.X86Pslld, n, Const(16));
- res2 = context.AddIntrinsic(Intrinsic.X86Psrld, res2, Const(16));
- res2 = context.AddIntrinsic(Intrinsic.X86Cvtdq2ps, res2);
- res = context.AddIntrinsic(Intrinsic.X86Addps, res, res2);
- if (op is OpCodeSimdShImm fixedOp)
- {
- int fBits = GetImmShr(fixedOp);
- // BitConverter.Int32BitsToSingle(fpScaled) == 1f / MathF.Pow(2f, fBits)
- int fpScaled = 0x3F800000 - fBits * 0x800000;
- Operand scale = X86GetAllElements(context, fpScaled);
- res = context.AddIntrinsic(Intrinsic.X86Mulps, res, scale);
- }
- if (scalar)
- {
- res = context.VectorZeroUpper96(res);
- }
- else if (op.RegisterSize == RegisterSize.Simd64)
- {
- res = context.VectorZeroUpper64(res);
- }
- context.Copy(GetVec(op.Rd), res);
- }
- private static Operand EmitVectorLongExtract(ArmEmitterContext context, int reg, int index, int size)
- {
- OperandType type = size == 3 ? OperandType.I64 : OperandType.I32;
- return context.VectorExtract(type, GetVec(reg), index);
- }
- private static Operand EmitVectorLongCreate(ArmEmitterContext context, Operand low, Operand high)
- {
- Operand vector = context.VectorCreateScalar(low);
- vector = context.VectorInsert(vector, high, 1);
- return vector;
- }
- }
- }
|