public static void EmitCall(ILEmitterCtx context, string mthdName) { context.EmitCall(typeof(VectorHelper), mthdName); }
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))); EmitLdvecWithCastToDouble(context, 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); } } }
private static void EmitVectorUnzip(ILEmitterCtx context, int part) { OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp; if (Optimizations.UseSsse3) { Type[] typesSve = new Type[] { typeof(long), typeof(long) }; string nameUpk = part == 0 ? nameof(Sse2.UnpackLow) : nameof(Sse2.UnpackHigh); if (op.RegisterSize == RegisterSize.Simd128) { context.EmitLdvec(op.Rn); // value if (op.Size < 3) { context.EmitLdc_I8(_masksE1_TrnUzp [op.Size]); // maskE1 context.EmitLdc_I8(_masksE0_TrnUzpXtn[op.Size]); // maskE0 context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetVector128), typesSve)); context.EmitCall(typeof(Ssse3).GetMethod(nameof(Ssse3.Shuffle), GetTypesSflUpk(0))); } context.EmitLdvec(op.Rm); // value if (op.Size < 3) { context.EmitLdc_I8(_masksE1_TrnUzp [op.Size]); // maskE1 context.EmitLdc_I8(_masksE0_TrnUzpXtn[op.Size]); // maskE0 context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetVector128), typesSve)); context.EmitCall(typeof(Ssse3).GetMethod(nameof(Ssse3.Shuffle), GetTypesSflUpk(0))); } context.EmitCall(typeof(Sse2).GetMethod(nameUpk, GetTypesSflUpk(3))); context.EmitStvec(op.Rd); } else { context.EmitLdvec(op.Rn); context.EmitLdvec(op.Rm); context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.UnpackLow), GetTypesSflUpk(op.Size))); // value if (op.Size < 2) { context.EmitLdc_I8(_masksE1_Uzp[op.Size]); // maskE1 context.EmitLdc_I8(_masksE0_Uzp[op.Size]); // maskE0 context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetVector128), typesSve)); context.EmitCall(typeof(Ssse3).GetMethod(nameof(Ssse3.Shuffle), GetTypesSflUpk(0))); } VectorHelper.EmitCall(context, nameof(VectorHelper.VectorSingleZero)); context.EmitCall(typeof(Sse2).GetMethod(nameUpk, GetTypesSflUpk(3))); context.EmitStvec(op.Rd); } } else { int words = op.GetBitsCount() >> 4; int pairs = words >> op.Size; for (int index = 0; index < pairs; index++) { int idx = index << 1; EmitVectorExtractZx(context, op.Rn, idx + part, op.Size); EmitVectorExtractZx(context, op.Rm, idx + part, op.Size); EmitVectorInsertTmp(context, pairs + index, op.Size); EmitVectorInsertTmp(context, index, op.Size); } context.EmitLdvectmp(); context.EmitStvec(op.Rd); if (op.RegisterSize == RegisterSize.Simd64) { EmitVectorZeroUpper(context, op.Rd); } } }
public static void EmitCall(ILEmitterCtx context, string name64, string name128) { bool isSimd64 = context.CurrOp.RegisterSize == RegisterSize.Simd64; context.EmitCall(typeof(VectorHelper), isSimd64 ? name64 : name128); }
public static void EmitVectorPairwiseSseOrSse2OpF(ILEmitterCtx context, string name) { OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp; int sizeF = op.Size & 1; if (sizeF == 0) { if (op.RegisterSize == RegisterSize.Simd64) { Type[] types = new Type[] { typeof(Vector128 <float>), typeof(Vector128 <float>) }; context.EmitLdvec(op.Rn); context.EmitLdvec(op.Rm); context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.UnpackLow), types)); context.Emit(OpCodes.Dup); context.EmitStvectmp(); VectorHelper.EmitCall(context, nameof(VectorHelper.VectorSingleZero)); context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.MoveLowToHigh), types)); VectorHelper.EmitCall(context, nameof(VectorHelper.VectorSingleZero)); context.EmitLdvectmp(); context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.MoveHighToLow), types)); context.EmitCall(typeof(Sse).GetMethod(name, types)); context.EmitStvec(op.Rd); } else /* if (op.RegisterSize == RegisterSize.Simd128) */ { Type[] typesSfl = new Type[] { typeof(Vector128 <float>), typeof(Vector128 <float>), typeof(byte) }; Type[] types = new Type[] { typeof(Vector128 <float>), typeof(Vector128 <float>) }; context.EmitLdvec(op.Rn); context.Emit(OpCodes.Dup); context.EmitStvectmp(); context.EmitLdvec(op.Rm); context.Emit(OpCodes.Dup); context.EmitStvectmp2(); context.EmitLdc_I4(2 << 6 | 0 << 4 | 2 << 2 | 0 << 0); context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.Shuffle), typesSfl)); context.EmitLdvectmp(); context.EmitLdvectmp2(); context.EmitLdc_I4(3 << 6 | 1 << 4 | 3 << 2 | 1 << 0); context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.Shuffle), typesSfl)); context.EmitCall(typeof(Sse).GetMethod(name, types)); context.EmitStvec(op.Rd); } } else /* if (sizeF == 1) */ { Type[] types = new Type[] { typeof(Vector128 <double>), typeof(Vector128 <double>) }; EmitLdvecWithCastToDouble(context, op.Rn); context.Emit(OpCodes.Dup); context.EmitStvectmp(); EmitLdvecWithCastToDouble(context, op.Rm); context.Emit(OpCodes.Dup); context.EmitStvectmp2(); context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.UnpackLow), types)); context.EmitLdvectmp(); context.EmitLdvectmp2(); context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.UnpackHigh), types)); context.EmitCall(typeof(Sse2).GetMethod(name, types)); EmitStvecWithCastFromDouble(context, op.Rd); } }
public static void Tbl_V(ILEmitterCtx context) { OpCodeSimdTbl64 op = (OpCodeSimdTbl64)context.CurrOp; if (Optimizations.UseSsse3) { Type[] typesCmpSflSub = new Type[] { typeof(Vector128 <sbyte>), typeof(Vector128 <sbyte>) }; Type[] typesOr = new Type[] { typeof(Vector128 <long>), typeof(Vector128 <long>) }; Type[] typesSav = new Type[] { typeof(long) }; context.EmitLdvec(op.Rn); context.EmitLdvec(op.Rm); context.EmitLdc_I8(0x0F0F0F0F0F0F0F0FL); context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetAllVector128), typesSav)); context.EmitStvectmp2(); context.EmitLdvectmp2(); context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.CompareGreaterThan), typesCmpSflSub)); context.EmitLdvec(op.Rm); context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Or), typesOr)); context.EmitCall(typeof(Ssse3).GetMethod(nameof(Ssse3.Shuffle), typesCmpSflSub)); for (int index = 1; index < op.Size; index++) { context.EmitLdvec((op.Rn + index) & 0x1F); context.EmitLdvec(op.Rm); context.EmitLdc_I8(0x1010101010101010L * index); context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetAllVector128), typesSav)); context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Subtract), typesCmpSflSub)); context.EmitStvectmp(); context.EmitLdvectmp(); context.EmitLdvectmp2(); context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.CompareGreaterThan), typesCmpSflSub)); context.EmitLdvectmp(); context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Or), typesOr)); context.EmitCall(typeof(Ssse3).GetMethod(nameof(Ssse3.Shuffle), typesCmpSflSub)); context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Or), typesOr)); } context.EmitStvec(op.Rd); if (op.RegisterSize == RegisterSize.Simd64) { EmitVectorZeroUpper(context, op.Rd); } } else { context.EmitLdvec(op.Rm); for (int index = 0; index < op.Size; index++) { context.EmitLdvec((op.Rn + index) & 0x1F); } switch (op.Size) { case 1: VectorHelper.EmitCall(context, nameof(VectorHelper.Tbl1_V64), nameof(VectorHelper.Tbl1_V128)); break; case 2: VectorHelper.EmitCall(context, nameof(VectorHelper.Tbl2_V64), nameof(VectorHelper.Tbl2_V128)); break; case 3: VectorHelper.EmitCall(context, nameof(VectorHelper.Tbl3_V64), nameof(VectorHelper.Tbl3_V128)); break; case 4: VectorHelper.EmitCall(context, nameof(VectorHelper.Tbl4_V64), nameof(VectorHelper.Tbl4_V128)); break; default: throw new InvalidOperationException(); } context.EmitStvec(op.Rd); } }
public static void Xtn_V(ILEmitterCtx context) { OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp; if (Optimizations.UseSsse3) { long[] masks = new long[] { 14L << 56 | 12L << 48 | 10L << 40 | 08L << 32 | 06L << 24 | 04L << 16 | 02L << 8 | 00L << 0, 13L << 56 | 12L << 48 | 09L << 40 | 08L << 32 | 05L << 24 | 04L << 16 | 01L << 8 | 00L << 0, 11L << 56 | 10L << 48 | 09L << 40 | 08L << 32 | 03L << 24 | 02L << 16 | 01L << 8 | 00L << 0 }; Type[] typesMov = new Type[] { typeof(Vector128 <float>), typeof(Vector128 <float>) }; Type[] typesSfl = new Type[] { typeof(Vector128 <sbyte>), typeof(Vector128 <sbyte>) }; Type[] typesSve = new Type[] { typeof(long), typeof(long) }; 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), typesMov)); EmitLdvecWithSignedCast(context, op.Rn, 0); context.EmitLdc_I8(masks[op.Size]); context.Emit(OpCodes.Dup); context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetVector128), typesSve)); context.EmitCall(typeof(Ssse3).GetMethod(nameof(Ssse3.Shuffle), typesSfl)); context.EmitCall(typeof(Sse).GetMethod(nameMov, typesMov)); context.EmitStvec(op.Rd); } else { int elems = 8 >> op.Size; int part = op.RegisterSize == RegisterSize.Simd128 ? elems : 0; if (part != 0) { context.EmitLdvec(op.Rd); context.EmitStvectmp(); } for (int index = 0; index < elems; index++) { EmitVectorExtractZx(context, op.Rn, index, op.Size + 1); EmitVectorInsertTmp(context, part + index, op.Size); } context.EmitLdvectmp(); context.EmitStvec(op.Rd); if (part == 0) { EmitVectorZeroUpper(context, op.Rd); } } }
private static void EmitStore(ILEmitterCtx context, AccessType accType, bool pair) { OpCodeMemEx64 op = (OpCodeMemEx64)context.CurrOp; bool ordered = (accType & AccessType.Ordered) != 0; bool exclusive = (accType & AccessType.Exclusive) != 0; if (ordered) { EmitBarrier(context); } if (exclusive) { ILLabel lblEx = new ILLabel(); ILLabel lblEnd = new ILLabel(); context.EmitLdarg(TranslatedSub.StateArgIdx); context.EmitLdint(op.Rn); context.EmitPrivateCall(typeof(CpuThreadState), nameof(CpuThreadState.CheckExclusiveAddress)); context.Emit(OpCodes.Brtrue_S, lblEx); //Address check failed, set error right away and do not store anything. context.EmitLdc_I4(1); context.EmitStintzr(op.Rs); context.Emit(OpCodes.Br, lblEnd); //Address check passsed. context.MarkLabel(lblEx); context.EmitLdarg(TranslatedSub.MemoryArgIdx); context.EmitLdint(op.Rn); context.EmitLdarg(TranslatedSub.StateArgIdx); context.EmitCallPrivatePropGet(typeof(CpuThreadState), nameof(CpuThreadState.ExclusiveValueLow)); void EmitCast() { //The input should be always int64. switch (op.Size) { case 0: context.Emit(OpCodes.Conv_U1); break; case 1: context.Emit(OpCodes.Conv_U2); break; case 2: context.Emit(OpCodes.Conv_U4); break; } } EmitCast(); if (pair) { context.EmitLdarg(TranslatedSub.StateArgIdx); context.EmitCallPrivatePropGet(typeof(CpuThreadState), nameof(CpuThreadState.ExclusiveValueHigh)); EmitCast(); context.EmitLdintzr(op.Rt); EmitCast(); context.EmitLdintzr(op.Rt2); EmitCast(); switch (op.Size) { case 2: context.EmitPrivateCall(typeof(MemoryManager), nameof(MemoryManager.AtomicCompareExchange2xInt32)); break; case 3: context.EmitPrivateCall(typeof(MemoryManager), nameof(MemoryManager.AtomicCompareExchangeInt128)); break; default: throw new InvalidOperationException($"Invalid store size of {1 << op.Size} bytes."); } } else { context.EmitLdintzr(op.Rt); EmitCast(); switch (op.Size) { case 0: context.EmitCall(typeof(MemoryManager), nameof(MemoryManager.AtomicCompareExchangeByte)); break; case 1: context.EmitCall(typeof(MemoryManager), nameof(MemoryManager.AtomicCompareExchangeInt16)); break; case 2: context.EmitCall(typeof(MemoryManager), nameof(MemoryManager.AtomicCompareExchangeInt32)); break; case 3: context.EmitCall(typeof(MemoryManager), nameof(MemoryManager.AtomicCompareExchangeInt64)); break; default: throw new InvalidOperationException($"Invalid store size of {1 << op.Size} bytes."); } } //The value returned is a bool, true if the values compared //were equal and the new value was written, false otherwise. //We need to invert this result, as on ARM 1 indicates failure, //and 0 success on those instructions. context.EmitLdc_I4(1); context.Emit(OpCodes.Xor); context.Emit(OpCodes.Dup); context.Emit(OpCodes.Conv_U8); context.EmitStintzr(op.Rs); //Only clear the exclusive monitor if the store was successful (Rs = false). context.Emit(OpCodes.Brtrue_S, lblEnd); Clrex(context); context.MarkLabel(lblEnd); } else { void EmitWrite(int rt, long offset) { context.EmitLdarg(TranslatedSub.MemoryArgIdx); context.EmitLdint(op.Rn); if (offset != 0) { context.EmitLdc_I8(offset); context.Emit(OpCodes.Add); } context.EmitLdintzr(rt); EmitWriteCall(context, op.Size); } EmitWrite(op.Rt, 0); if (pair) { EmitWrite(op.Rt2, 1 << op.Size); } } }
private static void EmitSse41Fcvt_Signed(ILEmitterCtx context, RoundMode roundMode, bool isFixed, bool scalar) { OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp; // sizeF == ((OpCodeSimdShImm64)op).Size - 2 int sizeF = op.Size & 1; if (sizeF == 0) { Type[] typesCmpMul = new Type[] { typeof(Vector128 <float>), typeof(Vector128 <float>) }; Type[] typesAndXor = new Type[] { typeof(Vector128 <long>), typeof(Vector128 <long>) }; 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), typesCmpMul)); context.EmitLdvec(op.Rn); context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.And), typesAndXor)); if (isFixed) { int fBits = GetImmShr((OpCodeSimdShImm64)op); // BitConverter.Int32BitsToSingle(fpScaled) == MathF.Pow(2f, fBits) int fpScaled = 0x40000000 + (fBits - 1) * 0x800000; context.EmitLdc_I4(fpScaled); context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetAllVector128), typesSav)); context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.Multiply), typesCmpMul)); } context.EmitCall(typeof(Sse41).GetMethod(GetSse41NameRnd(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), typesCmpMul)); context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Xor), typesAndXor)); 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[] typesCmpMulUpk = new Type[] { typeof(Vector128 <double>), typeof(Vector128 <double>) }; Type[] typesAndXor = 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), typesCmpMulUpk)); context.EmitLdvec(op.Rn); context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.And), typesAndXor)); if (isFixed) { int fBits = GetImmShr((OpCodeSimdShImm64)op); // BitConverter.Int64BitsToDouble(fpScaled) == Math.Pow(2d, fBits) long fpScaled = 0x4000000000000000L + (fBits - 1) * 0x10000000000000L; context.EmitLdc_I8(fpScaled); context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetAllVector128), typesSav)); context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Multiply), typesCmpMulUpk)); } context.EmitCall(typeof(Sse41).GetMethod(GetSse41NameRnd(roundMode), typesRndCvt)); context.EmitStvectmp(); context.EmitLdvectmp(); context.EmitLdvectmp(); context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.UnpackHigh), typesCmpMulUpk)); context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ConvertToInt64), typesRndCvt)); 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), typesCmpMulUpk)); context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Xor), typesAndXor)); context.EmitStvec(op.Rd); if (scalar) { EmitVectorZeroUpper(context, op.Rd); } } }
public static void EmitSseOrSse2OpF(ILEmitterCtx context, string name, bool scalar) { OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp; int sizeF = op.Size & 1; void Ldvec(int reg) { context.EmitLdvec(reg); if (sizeF == 1) { VectorHelper.EmitCall(context, nameof(VectorHelper.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 OpCodeSimdReg64 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) { VectorHelper.EmitCall(context, nameof(VectorHelper.VectorDoubleToSingle)); } context.EmitStvec(op.Rd); if (scalar) { if (sizeF == 0) { EmitVectorZero32_128(context, op.Rd); } else /* if (SizeF == 1) */ { EmitVectorZeroUpper(context, op.Rd); } } else if (op.RegisterSize == RegisterSize.Simd64) { EmitVectorZeroUpper(context, op.Rd); } }
private static void EmitSse41Fcvt_Signed_Gp(ILEmitterCtx context, RoundMode roundMode, bool isFixed) { OpCodeSimdCvt64 op = (OpCodeSimdCvt64)context.CurrOp; if (op.Size == 0) { Type[] typesCmpMul = new Type[] { typeof(Vector128 <float>), typeof(Vector128 <float>) }; Type[] typesAnd = new Type[] { typeof(Vector128 <long>), typeof(Vector128 <long>) }; Type[] typesRndCvt = new Type[] { typeof(Vector128 <float>) }; Type[] typesCvt = new Type[] { typeof(Vector128 <int>) }; Type[] typesSav = new Type[] { typeof(int) }; //string nameCvt; int fpMaxVal; if (op.RegisterSize == RegisterSize.Int32) { //nameCvt = nameof(Sse.ConvertToInt32); fpMaxVal = 0x4F000000; // 2.14748365E9f (2147483648) } else { //nameCvt = nameof(Sse.ConvertToInt64); fpMaxVal = 0x5F000000; // 9.223372E18f (9223372036854775808) } context.EmitLdvec(op.Rn); context.EmitLdvec(op.Rn); context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.CompareOrdered), typesCmpMul)); context.EmitLdvec(op.Rn); context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.And), typesAnd)); if (isFixed) { // BitConverter.Int32BitsToSingle(fpScaled) == MathF.Pow(2f, op.FBits) int fpScaled = 0x40000000 + (op.FBits - 1) * 0x800000; context.EmitLdc_I4(fpScaled); context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetAllVector128), typesSav)); context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.Multiply), typesCmpMul)); } context.EmitCall(typeof(Sse41).GetMethod(GetSse41NameRnd(roundMode), typesRndCvt)); context.EmitStvectmp(); context.EmitLdvectmp(); // TODO: Use Sse.ConvertToInt64 once it is fixed (in .NET Core 3.0), // remove the following if/else and uncomment the code. //context.EmitCall(typeof(Sse).GetMethod(nameCvt, typesRndCvt)); if (op.RegisterSize == RegisterSize.Int32) { context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.ConvertToInt32), typesRndCvt)); } else { context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ConvertToVector128Double), typesRndCvt)); context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ConvertToInt64), new Type[] { typeof(Vector128 <double>) })); } context.EmitLdvectmp(); context.EmitLdc_I4(fpMaxVal); context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetAllVector128), typesSav)); context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.CompareGreaterThanOrEqual), typesCmpMul)); context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ConvertToInt32), typesCvt)); if (op.RegisterSize == RegisterSize.Int32) { context.Emit(OpCodes.Xor); context.Emit(OpCodes.Conv_U8); } else { context.Emit(OpCodes.Conv_I8); context.Emit(OpCodes.Xor); } context.EmitStintzr(op.Rd); } else /* if (op.Size == 1) */ { Type[] typesCmpMul = new Type[] { typeof(Vector128 <double>), typeof(Vector128 <double>) }; Type[] typesAnd = new Type[] { typeof(Vector128 <long>), typeof(Vector128 <long>) }; Type[] typesRndCvt = new Type[] { typeof(Vector128 <double>) }; Type[] typesCvt = new Type[] { typeof(Vector128 <int>) }; Type[] typesSav = new Type[] { typeof(long) }; string nameCvt; long fpMaxVal; if (op.RegisterSize == RegisterSize.Int32) { nameCvt = nameof(Sse2.ConvertToInt32); fpMaxVal = 0x41E0000000000000L; // 2147483648.0000000d (2147483648) } else { nameCvt = nameof(Sse2.ConvertToInt64); fpMaxVal = 0x43E0000000000000L; // 9.2233720368547760E18d (9223372036854775808) } context.EmitLdvec(op.Rn); context.EmitLdvec(op.Rn); context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.CompareOrdered), typesCmpMul)); context.EmitLdvec(op.Rn); context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.And), typesAnd)); if (isFixed) { // BitConverter.Int64BitsToDouble(fpScaled) == Math.Pow(2d, op.FBits) long fpScaled = 0x4000000000000000L + (op.FBits - 1) * 0x10000000000000L; context.EmitLdc_I8(fpScaled); context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetAllVector128), typesSav)); context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Multiply), typesCmpMul)); } context.EmitCall(typeof(Sse41).GetMethod(GetSse41NameRnd(roundMode), typesRndCvt)); context.EmitStvectmp(); context.EmitLdvectmp(); context.EmitCall(typeof(Sse2).GetMethod(nameCvt, typesRndCvt)); context.EmitLdvectmp(); context.EmitLdc_I8(fpMaxVal); context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetAllVector128), typesSav)); context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.CompareGreaterThanOrEqual), typesCmpMul)); context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ConvertToInt32), typesCvt)); if (op.RegisterSize == RegisterSize.Int32) { context.Emit(OpCodes.Xor); context.Emit(OpCodes.Conv_U8); } else { context.Emit(OpCodes.Conv_I8); context.Emit(OpCodes.Xor); } context.EmitStintzr(op.Rd); } }
public static void EmitCall(ILEmitterCtx context, string mthdName) { context.EmitCall(typeof(SoftFallback), mthdName); }
public static void Xtn_V(ILEmitterCtx context) { OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp; int elems = 8 >> op.Size; int part = op.RegisterSize == RegisterSize.Simd128 ? elems : 0; if (Optimizations.UseSse41 && op.Size < 2) { void EmitZeroVector() { switch (op.Size) { case 0: VectorHelper.EmitCall(context, nameof(VectorHelper.VectorInt16Zero)); break; case 1: VectorHelper.EmitCall(context, nameof(VectorHelper.VectorInt32Zero)); break; } } //For XTN, first operand is source, second operand is 0. //For XTN2, first operand is 0, second operand is source. if (part != 0) { EmitZeroVector(); } EmitLdvecWithSignedCast(context, op.Rn, op.Size + 1); //Set mask to discard the upper half of the wide elements. switch (op.Size) { case 0: context.EmitLdc_I4(0x00ff); break; case 1: context.EmitLdc_I4(0x0000ffff); break; } Type wideType = IntTypesPerSizeLog2[op.Size + 1]; context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetAllVector128), new Type[] { wideType })); wideType = VectorIntTypesPerSizeLog2[op.Size + 1]; Type[] wideTypes = new Type[] { wideType, wideType }; context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.And), wideTypes)); if (part == 0) { EmitZeroVector(); } //Pack values with signed saturation, the signed saturation shouldn't //saturate anything since the upper bits were masked off. Type sseType = op.Size == 0 ? typeof(Sse2) : typeof(Sse41); context.EmitCall(sseType.GetMethod(nameof(Sse2.PackUnsignedSaturate), wideTypes)); if (part != 0) { //For XTN2, we additionally need to discard the upper bits //of the target register and OR the result with it. EmitVectorZeroUpper(context, op.Rd); EmitLdvecWithUnsignedCast(context, op.Rd, op.Size); Type narrowType = VectorUIntTypesPerSizeLog2[op.Size]; context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Or), new Type[] { narrowType, narrowType })); } EmitStvecWithUnsignedCast(context, op.Rd, op.Size); } else { if (part != 0) { context.EmitLdvec(op.Rd); context.EmitStvectmp(); } for (int index = 0; index < elems; index++) { EmitVectorExtractZx(context, op.Rn, index, op.Size + 1); EmitVectorInsertTmp(context, part + index, op.Size); } context.EmitLdvectmp(); context.EmitStvec(op.Rd); if (part == 0) { EmitVectorZeroUpper(context, op.Rd); } } }
private static void EmitFcmpOrFcmpe(ILEmitterCtx context, bool signalNaNs) { OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp; bool cmpWithZero = !(op is OpCodeSimdFcond64) ? op.Bit3 : false; if (Optimizations.FastFP && Optimizations.UseSse2) { if (op.Size == 0) { Type[] typesCmp = new Type[] { typeof(Vector128 <float>), typeof(Vector128 <float>) }; ILLabel lblNaN = new ILLabel(); ILLabel lblEnd = new ILLabel(); context.EmitLdvec(op.Rn); context.Emit(OpCodes.Dup); context.EmitStvectmp(); if (cmpWithZero) { VectorHelper.EmitCall(context, nameof(VectorHelper.VectorSingleZero)); } else { context.EmitLdvec(op.Rm); } context.Emit(OpCodes.Dup); context.EmitStvectmp2(); context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.CompareOrderedScalar), typesCmp)); VectorHelper.EmitCall(context, nameof(VectorHelper.VectorSingleZero)); context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.CompareEqualOrderedScalar), typesCmp)); context.Emit(OpCodes.Brtrue_S, lblNaN); context.EmitLdc_I4(0); context.EmitLdvectmp(); context.EmitLdvectmp2(); context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.CompareGreaterThanOrEqualOrderedScalar), typesCmp)); context.EmitLdvectmp(); context.EmitLdvectmp2(); context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.CompareEqualOrderedScalar), typesCmp)); context.EmitLdvectmp(); context.EmitLdvectmp2(); context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.CompareLessThanOrderedScalar), typesCmp)); context.EmitStflg((int)PState.NBit); context.EmitStflg((int)PState.ZBit); context.EmitStflg((int)PState.CBit); context.EmitStflg((int)PState.VBit); context.Emit(OpCodes.Br_S, lblEnd); context.MarkLabel(lblNaN); context.EmitLdc_I4(1); context.Emit(OpCodes.Dup); context.EmitLdc_I4(0); context.Emit(OpCodes.Dup); context.EmitStflg((int)PState.NBit); context.EmitStflg((int)PState.ZBit); context.EmitStflg((int)PState.CBit); context.EmitStflg((int)PState.VBit); context.MarkLabel(lblEnd); } else /* if (op.Size == 1) */ { Type[] typesCmp = new Type[] { typeof(Vector128 <double>), typeof(Vector128 <double>) }; ILLabel lblNaN = new ILLabel(); ILLabel lblEnd = new ILLabel(); EmitLdvecWithCastToDouble(context, op.Rn); context.Emit(OpCodes.Dup); context.EmitStvectmp(); if (cmpWithZero) { VectorHelper.EmitCall(context, nameof(VectorHelper.VectorDoubleZero)); } else { EmitLdvecWithCastToDouble(context, op.Rm); } context.Emit(OpCodes.Dup); context.EmitStvectmp2(); context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.CompareOrderedScalar), typesCmp)); VectorHelper.EmitCall(context, nameof(VectorHelper.VectorDoubleZero)); context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.CompareEqualOrderedScalar), typesCmp)); context.Emit(OpCodes.Brtrue_S, lblNaN); context.EmitLdc_I4(0); context.EmitLdvectmp(); context.EmitLdvectmp2(); context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.CompareGreaterThanOrEqualOrderedScalar), typesCmp)); context.EmitLdvectmp(); context.EmitLdvectmp2(); context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.CompareEqualOrderedScalar), typesCmp)); context.EmitLdvectmp(); context.EmitLdvectmp2(); context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.CompareLessThanOrderedScalar), typesCmp)); context.EmitStflg((int)PState.NBit); context.EmitStflg((int)PState.ZBit); context.EmitStflg((int)PState.CBit); context.EmitStflg((int)PState.VBit); context.Emit(OpCodes.Br_S, lblEnd); context.MarkLabel(lblNaN); context.EmitLdc_I4(1); context.Emit(OpCodes.Dup); context.EmitLdc_I4(0); context.Emit(OpCodes.Dup); context.EmitStflg((int)PState.NBit); context.EmitStflg((int)PState.ZBit); context.EmitStflg((int)PState.CBit); context.EmitStflg((int)PState.VBit); context.MarkLabel(lblEnd); } } else { EmitVectorExtractF(context, op.Rn, 0, op.Size); if (cmpWithZero) { if (op.Size == 0) { context.EmitLdc_R4(0f); } else /* if (op.Size == 1) */ { context.EmitLdc_R8(0d); } } else { EmitVectorExtractF(context, op.Rm, 0, op.Size); } context.EmitLdc_I4(!signalNaNs ? 0 : 1); EmitSoftFloatCall(context, nameof(SoftFloat32.FPCompare)); EmitSetNzcv(context); } }
private static void EmitVectorZip(ILEmitterCtx context, int part) { OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp; if (Optimizations.UseSse2) { EmitLdvecWithUnsignedCast(context, op.Rn, op.Size); EmitLdvecWithUnsignedCast(context, op.Rm, op.Size); Type[] types = new Type[] { VectorUIntTypesPerSizeLog2[op.Size], VectorUIntTypesPerSizeLog2[op.Size] }; string name = part == 0 || (part != 0 && op.RegisterSize == RegisterSize.Simd64) ? nameof(Sse2.UnpackLow) : nameof(Sse2.UnpackHigh); context.EmitCall(typeof(Sse2).GetMethod(name, types)); if (op.RegisterSize == RegisterSize.Simd64 && part != 0) { context.EmitLdc_I4(8); Type[] shTypes = new Type[] { VectorUIntTypesPerSizeLog2[op.Size], typeof(byte) }; context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ShiftRightLogical128BitLane), shTypes)); } EmitStvecWithUnsignedCast(context, op.Rd, op.Size); if (op.RegisterSize == RegisterSize.Simd64 && part == 0) { EmitVectorZeroUpper(context, op.Rd); } } else { int words = op.GetBitsCount() >> 4; int pairs = words >> op.Size; int Base = part != 0 ? pairs : 0; for (int index = 0; index < pairs; index++) { int idx = index << 1; EmitVectorExtractZx(context, op.Rn, Base + index, op.Size); EmitVectorExtractZx(context, op.Rm, Base + index, op.Size); EmitVectorInsertTmp(context, idx + 1, op.Size); EmitVectorInsertTmp(context, idx, op.Size); } context.EmitLdvectmp(); context.EmitStvec(op.Rd); if (op.RegisterSize == RegisterSize.Simd64) { EmitVectorZeroUpper(context, op.Rd); } } }
private static void EmitCmpSseOrSse2OpF(ILEmitterCtx context, string name, bool scalar, bool isLeOrLt = false) { OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp; int sizeF = op.Size & 1; if (sizeF == 0) { Type[] types = new Type[] { typeof(Vector128 <float>), typeof(Vector128 <float>) }; if (!isLeOrLt) { context.EmitLdvec(op.Rn); } if (op is OpCodeSimdReg64 binOp) { context.EmitLdvec(binOp.Rm); } else { VectorHelper.EmitCall(context, nameof(VectorHelper.VectorSingleZero)); } if (isLeOrLt) { context.EmitLdvec(op.Rn); } context.EmitCall(typeof(Sse).GetMethod(name, 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>) }; if (!isLeOrLt) { EmitLdvecWithCastToDouble(context, op.Rn); } if (op is OpCodeSimdReg64 binOp) { EmitLdvecWithCastToDouble(context, binOp.Rm); } else { VectorHelper.EmitCall(context, nameof(VectorHelper.VectorDoubleZero)); } if (isLeOrLt) { EmitLdvecWithCastToDouble(context, op.Rn); } context.EmitCall(typeof(Sse2).GetMethod(name, types)); EmitStvecWithCastFromDouble(context, op.Rd); if (scalar) { EmitVectorZeroUpper(context, op.Rd); } } }
private static void EmitBitBif(ILEmitterCtx context, bool notRm) { OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp; if (Optimizations.UseSse2) { Type[] types = new Type[] { VectorUIntTypesPerSizeLog2[op.Size], VectorUIntTypesPerSizeLog2[op.Size] }; EmitLdvecWithUnsignedCast(context, op.Rm, op.Size); EmitLdvecWithUnsignedCast(context, op.Rd, op.Size); EmitLdvecWithUnsignedCast(context, op.Rn, op.Size); context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Xor), types)); string name = notRm ? nameof(Sse2.AndNot) : nameof(Sse2.And); context.EmitCall(typeof(Sse2).GetMethod(name, types)); EmitLdvecWithUnsignedCast(context, op.Rd, op.Size); context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Xor), types)); EmitStvecWithUnsignedCast(context, op.Rd, op.Size); if (op.RegisterSize == RegisterSize.Simd64) { EmitVectorZeroUpper(context, op.Rd); } } else { int bytes = op.GetBitsCount() >> 3; int elems = bytes >> op.Size; for (int index = 0; index < elems; index++) { EmitVectorExtractZx(context, op.Rd, index, op.Size); EmitVectorExtractZx(context, op.Rn, index, op.Size); context.Emit(OpCodes.Xor); EmitVectorExtractZx(context, op.Rm, index, op.Size); if (notRm) { context.Emit(OpCodes.Not); } context.Emit(OpCodes.And); EmitVectorExtractZx(context, op.Rd, index, op.Size); context.Emit(OpCodes.Xor); EmitVectorInsert(context, op.Rd, index, op.Size); } if (op.RegisterSize == RegisterSize.Simd64) { EmitVectorZeroUpper(context, op.Rd); } } }
public static void Ext_V(ILEmitterCtx context) { OpCodeSimdExt64 op = (OpCodeSimdExt64)context.CurrOp; if (Optimizations.UseSse2) { Type[] typesShs = new Type[] { typeof(Vector128 <byte>), typeof(byte) }; Type[] typesOr = new Type[] { typeof(Vector128 <byte>), typeof(Vector128 <byte>) }; context.EmitLdvec(op.Rn); if (op.RegisterSize == RegisterSize.Simd64) { VectorHelper.EmitCall(context, nameof(VectorHelper.VectorSingleZero)); context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.MoveLowToHigh))); } context.EmitLdc_I4(op.Imm4); context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ShiftRightLogical128BitLane), typesShs)); context.EmitLdvec(op.Rm); context.EmitLdc_I4((op.RegisterSize == RegisterSize.Simd64 ? 8 : 16) - op.Imm4); context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ShiftLeftLogical128BitLane), typesShs)); if (op.RegisterSize == RegisterSize.Simd64) { VectorHelper.EmitCall(context, nameof(VectorHelper.VectorSingleZero)); context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.MoveLowToHigh))); } context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Or), typesOr)); context.EmitStvec(op.Rd); } else { int bytes = op.GetBitsCount() >> 3; int position = op.Imm4; for (int index = 0; index < bytes; index++) { int reg = op.Imm4 + index < bytes ? op.Rn : op.Rm; if (position == bytes) { position = 0; } EmitVectorExtractZx(context, reg, position++, 0); EmitVectorInsertTmp(context, index, 0); } context.EmitLdvectmp(); context.EmitStvec(op.Rd); if (op.RegisterSize == RegisterSize.Simd64) { EmitVectorZeroUpper(context, op.Rd); } } }
private static void EmitReadCall(ILEmitterCtx context, Extension ext, int size) { bool isSimd = GetIsSimd(context); string name = null; if (size < 0 || size > (isSimd ? 4 : 3)) { throw new ArgumentOutOfRangeException(nameof(size)); } if (isSimd) { switch (size) { case 0: name = nameof(MemoryManager.ReadVector8); break; case 1: name = nameof(MemoryManager.ReadVector16); break; case 2: name = nameof(MemoryManager.ReadVector32); break; case 3: name = nameof(MemoryManager.ReadVector64); break; case 4: name = nameof(MemoryManager.ReadVector128); break; } } else { switch (size) { case 0: name = nameof(MemoryManager.ReadByte); break; case 1: name = nameof(MemoryManager.ReadUInt16); break; case 2: name = nameof(MemoryManager.ReadUInt32); break; case 3: name = nameof(MemoryManager.ReadUInt64); break; } } context.EmitCall(typeof(MemoryManager), name); if (!isSimd) { if (ext == Extension.Sx32 || ext == Extension.Sx64) { switch (size) { case 0: context.Emit(OpCodes.Conv_I1); break; case 1: context.Emit(OpCodes.Conv_I2); break; case 2: context.Emit(OpCodes.Conv_I4); break; } } if (size < 3) { context.Emit(ext == Extension.Sx64 ? OpCodes.Conv_I8 : OpCodes.Conv_U8); } } }