InstEmitSimdHelper.cs 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583
  1. using ChocolArm64.Decoders;
  2. using ChocolArm64.State;
  3. using ChocolArm64.Translation;
  4. using System;
  5. using System.Reflection;
  6. using System.Reflection.Emit;
  7. using System.Runtime.Intrinsics;
  8. using System.Runtime.Intrinsics.X86;
  9. namespace ChocolArm64.Instructions
  10. {
  11. static class InstEmitSimdHelper
  12. {
  13. public static readonly Type[] IntTypesPerSizeLog2 = new Type[]
  14. {
  15. typeof(sbyte),
  16. typeof(short),
  17. typeof(int),
  18. typeof(long)
  19. };
  20. public static readonly Type[] UIntTypesPerSizeLog2 = new Type[]
  21. {
  22. typeof(byte),
  23. typeof(ushort),
  24. typeof(uint),
  25. typeof(ulong)
  26. };
  27. public static readonly Type[] VectorIntTypesPerSizeLog2 = new Type[]
  28. {
  29. typeof(Vector128<sbyte>),
  30. typeof(Vector128<short>),
  31. typeof(Vector128<int>),
  32. typeof(Vector128<long>)
  33. };
  34. public static readonly Type[] VectorUIntTypesPerSizeLog2 = new Type[]
  35. {
  36. typeof(Vector128<byte>),
  37. typeof(Vector128<ushort>),
  38. typeof(Vector128<uint>),
  39. typeof(Vector128<ulong>)
  40. };
  41. [Flags]
  42. public enum OperFlags
  43. {
  44. Rd = 1 << 0,
  45. Rn = 1 << 1,
  46. Rm = 1 << 2,
  47. Ra = 1 << 3,
  48. RnRm = Rn | Rm,
  49. RdRn = Rd | Rn,
  50. RaRnRm = Ra | Rn | Rm,
  51. RdRnRm = Rd | Rn | Rm
  52. }
  53. public static int GetImmShl(OpCodeSimdShImm64 op)
  54. {
  55. return op.Imm - (8 << op.Size);
  56. }
  57. public static int GetImmShr(OpCodeSimdShImm64 op)
  58. {
  59. return (8 << (op.Size + 1)) - op.Imm;
  60. }
  61. public static void EmitSse2Op(ILEmitterCtx context, string name)
  62. {
  63. EmitSseOp(context, name, typeof(Sse2));
  64. }
  65. public static void EmitSse41Op(ILEmitterCtx context, string name)
  66. {
  67. EmitSseOp(context, name, typeof(Sse41));
  68. }
  69. public static void EmitSse42Op(ILEmitterCtx context, string name)
  70. {
  71. EmitSseOp(context, name, typeof(Sse42));
  72. }
  73. private static void EmitSseOp(ILEmitterCtx context, string name, Type type)
  74. {
  75. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  76. EmitLdvecWithSignedCast(context, op.Rn, op.Size);
  77. Type baseType = VectorIntTypesPerSizeLog2[op.Size];
  78. if (op is OpCodeSimdReg64 binOp)
  79. {
  80. EmitLdvecWithSignedCast(context, binOp.Rm, op.Size);
  81. context.EmitCall(type.GetMethod(name, new Type[] { baseType, baseType }));
  82. }
  83. else
  84. {
  85. context.EmitCall(type.GetMethod(name, new Type[] { baseType }));
  86. }
  87. EmitStvecWithSignedCast(context, op.Rd, op.Size);
  88. if (op.RegisterSize == RegisterSize.Simd64)
  89. {
  90. EmitVectorZeroUpper(context, op.Rd);
  91. }
  92. }
  93. public static void EmitLdvecWithSignedCast(ILEmitterCtx context, int reg, int size)
  94. {
  95. context.EmitLdvec(reg);
  96. switch (size)
  97. {
  98. case 0: VectorHelper.EmitCall(context, nameof(VectorHelper.VectorSingleToSByte)); break;
  99. case 1: VectorHelper.EmitCall(context, nameof(VectorHelper.VectorSingleToInt16)); break;
  100. case 2: VectorHelper.EmitCall(context, nameof(VectorHelper.VectorSingleToInt32)); break;
  101. case 3: VectorHelper.EmitCall(context, nameof(VectorHelper.VectorSingleToInt64)); break;
  102. default: throw new ArgumentOutOfRangeException(nameof(size));
  103. }
  104. }
  105. public static void EmitLdvecWithCastToDouble(ILEmitterCtx context, int reg)
  106. {
  107. context.EmitLdvec(reg);
  108. VectorHelper.EmitCall(context, nameof(VectorHelper.VectorSingleToDouble));
  109. }
  110. public static void EmitStvecWithCastFromDouble(ILEmitterCtx context, int reg)
  111. {
  112. VectorHelper.EmitCall(context, nameof(VectorHelper.VectorDoubleToSingle));
  113. context.EmitStvec(reg);
  114. }
  115. public static void EmitLdvecWithUnsignedCast(ILEmitterCtx context, int reg, int size)
  116. {
  117. context.EmitLdvec(reg);
  118. switch (size)
  119. {
  120. case 0: VectorHelper.EmitCall(context, nameof(VectorHelper.VectorSingleToByte)); break;
  121. case 1: VectorHelper.EmitCall(context, nameof(VectorHelper.VectorSingleToUInt16)); break;
  122. case 2: VectorHelper.EmitCall(context, nameof(VectorHelper.VectorSingleToUInt32)); break;
  123. case 3: VectorHelper.EmitCall(context, nameof(VectorHelper.VectorSingleToUInt64)); break;
  124. default: throw new ArgumentOutOfRangeException(nameof(size));
  125. }
  126. }
  127. public static void EmitStvecWithSignedCast(ILEmitterCtx context, int reg, int size)
  128. {
  129. switch (size)
  130. {
  131. case 0: VectorHelper.EmitCall(context, nameof(VectorHelper.VectorSByteToSingle)); break;
  132. case 1: VectorHelper.EmitCall(context, nameof(VectorHelper.VectorInt16ToSingle)); break;
  133. case 2: VectorHelper.EmitCall(context, nameof(VectorHelper.VectorInt32ToSingle)); break;
  134. case 3: VectorHelper.EmitCall(context, nameof(VectorHelper.VectorInt64ToSingle)); break;
  135. default: throw new ArgumentOutOfRangeException(nameof(size));
  136. }
  137. context.EmitStvec(reg);
  138. }
  139. public static void EmitStvecWithUnsignedCast(ILEmitterCtx context, int reg, int size)
  140. {
  141. switch (size)
  142. {
  143. case 0: VectorHelper.EmitCall(context, nameof(VectorHelper.VectorByteToSingle)); break;
  144. case 1: VectorHelper.EmitCall(context, nameof(VectorHelper.VectorUInt16ToSingle)); break;
  145. case 2: VectorHelper.EmitCall(context, nameof(VectorHelper.VectorUInt32ToSingle)); break;
  146. case 3: VectorHelper.EmitCall(context, nameof(VectorHelper.VectorUInt64ToSingle)); break;
  147. default: throw new ArgumentOutOfRangeException(nameof(size));
  148. }
  149. context.EmitStvec(reg);
  150. }
  151. public static void EmitScalarSseOrSse2OpF(ILEmitterCtx context, string name)
  152. {
  153. EmitSseOrSse2OpF(context, name, true);
  154. }
  155. public static void EmitVectorSseOrSse2OpF(ILEmitterCtx context, string name)
  156. {
  157. EmitSseOrSse2OpF(context, name, false);
  158. }
  159. public static void EmitSseOrSse2OpF(ILEmitterCtx context, string name, bool scalar)
  160. {
  161. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  162. int sizeF = op.Size & 1;
  163. void Ldvec(int reg)
  164. {
  165. context.EmitLdvec(reg);
  166. if (sizeF == 1)
  167. {
  168. VectorHelper.EmitCall(context, nameof(VectorHelper.VectorSingleToDouble));
  169. }
  170. }
  171. Ldvec(op.Rn);
  172. Type type;
  173. Type baseType;
  174. if (sizeF == 0)
  175. {
  176. type = typeof(Sse);
  177. baseType = typeof(Vector128<float>);
  178. }
  179. else /* if (sizeF == 1) */
  180. {
  181. type = typeof(Sse2);
  182. baseType = typeof(Vector128<double>);
  183. }
  184. if (op is OpCodeSimdReg64 binOp)
  185. {
  186. Ldvec(binOp.Rm);
  187. context.EmitCall(type.GetMethod(name, new Type[] { baseType, baseType }));
  188. }
  189. else
  190. {
  191. context.EmitCall(type.GetMethod(name, new Type[] { baseType }));
  192. }
  193. if (sizeF == 1)
  194. {
  195. VectorHelper.EmitCall(context, nameof(VectorHelper.VectorDoubleToSingle));
  196. }
  197. context.EmitStvec(op.Rd);
  198. if (scalar)
  199. {
  200. if (sizeF == 0)
  201. {
  202. EmitVectorZero32_128(context, op.Rd);
  203. }
  204. else /* if (sizeF == 1) */
  205. {
  206. EmitVectorZeroUpper(context, op.Rd);
  207. }
  208. }
  209. else if (op.RegisterSize == RegisterSize.Simd64)
  210. {
  211. EmitVectorZeroUpper(context, op.Rd);
  212. }
  213. }
  214. public static void EmitUnaryMathCall(ILEmitterCtx context, string name)
  215. {
  216. IOpCodeSimd64 op = (IOpCodeSimd64)context.CurrOp;
  217. int sizeF = op.Size & 1;
  218. MethodInfo mthdInfo;
  219. if (sizeF == 0)
  220. {
  221. mthdInfo = typeof(MathF).GetMethod(name, new Type[] { typeof(float) });
  222. }
  223. else /* if (sizeF == 1) */
  224. {
  225. mthdInfo = typeof(Math).GetMethod(name, new Type[] { typeof(double) });
  226. }
  227. context.EmitCall(mthdInfo);
  228. }
  229. public static void EmitBinaryMathCall(ILEmitterCtx context, string name)
  230. {
  231. IOpCodeSimd64 op = (IOpCodeSimd64)context.CurrOp;
  232. int sizeF = op.Size & 1;
  233. MethodInfo mthdInfo;
  234. if (sizeF == 0)
  235. {
  236. mthdInfo = typeof(MathF).GetMethod(name, new Type[] { typeof(float), typeof(float) });
  237. }
  238. else /* if (sizeF == 1) */
  239. {
  240. mthdInfo = typeof(Math).GetMethod(name, new Type[] { typeof(double), typeof(double) });
  241. }
  242. context.EmitCall(mthdInfo);
  243. }
  244. public static void EmitRoundMathCall(ILEmitterCtx context, MidpointRounding roundMode)
  245. {
  246. IOpCodeSimd64 op = (IOpCodeSimd64)context.CurrOp;
  247. int sizeF = op.Size & 1;
  248. MethodInfo mthdInfo;
  249. if (sizeF == 0)
  250. {
  251. mthdInfo = typeof(MathF).GetMethod(nameof(MathF.Round), new Type[] { typeof(float), typeof(MidpointRounding) });
  252. }
  253. else /* if (sizeF == 1) */
  254. {
  255. mthdInfo = typeof(Math).GetMethod(nameof(Math.Round), new Type[] { typeof(double), typeof(MidpointRounding) });
  256. }
  257. context.EmitLdc_I4((int)roundMode);
  258. context.EmitCall(mthdInfo);
  259. }
  260. public static void EmitSoftFloatCall(ILEmitterCtx context, string name)
  261. {
  262. IOpCodeSimd64 op = (IOpCodeSimd64)context.CurrOp;
  263. Type type = (op.Size & 1) == 0
  264. ? typeof(SoftFloat32)
  265. : typeof(SoftFloat64);
  266. context.EmitLdarg(TranslatedSub.StateArgIdx);
  267. context.EmitCall(type, name);
  268. }
  269. public static void EmitScalarBinaryOpByElemF(ILEmitterCtx context, Action emit)
  270. {
  271. OpCodeSimdRegElemF64 op = (OpCodeSimdRegElemF64)context.CurrOp;
  272. EmitScalarOpByElemF(context, emit, op.Index, ternary: false);
  273. }
  274. public static void EmitScalarTernaryOpByElemF(ILEmitterCtx context, Action emit)
  275. {
  276. OpCodeSimdRegElemF64 op = (OpCodeSimdRegElemF64)context.CurrOp;
  277. EmitScalarOpByElemF(context, emit, op.Index, ternary: true);
  278. }
  279. public static void EmitScalarOpByElemF(ILEmitterCtx context, Action emit, int elem, bool ternary)
  280. {
  281. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  282. int sizeF = op.Size & 1;
  283. if (ternary)
  284. {
  285. EmitVectorExtractF(context, op.Rd, 0, sizeF);
  286. }
  287. EmitVectorExtractF(context, op.Rn, 0, sizeF);
  288. EmitVectorExtractF(context, op.Rm, elem, sizeF);
  289. emit();
  290. EmitScalarSetF(context, op.Rd, sizeF);
  291. }
  292. public static void EmitScalarUnaryOpSx(ILEmitterCtx context, Action emit)
  293. {
  294. EmitScalarOp(context, emit, OperFlags.Rn, true);
  295. }
  296. public static void EmitScalarBinaryOpSx(ILEmitterCtx context, Action emit)
  297. {
  298. EmitScalarOp(context, emit, OperFlags.RnRm, true);
  299. }
  300. public static void EmitScalarUnaryOpZx(ILEmitterCtx context, Action emit)
  301. {
  302. EmitScalarOp(context, emit, OperFlags.Rn, false);
  303. }
  304. public static void EmitScalarBinaryOpZx(ILEmitterCtx context, Action emit)
  305. {
  306. EmitScalarOp(context, emit, OperFlags.RnRm, false);
  307. }
  308. public static void EmitScalarTernaryOpZx(ILEmitterCtx context, Action emit)
  309. {
  310. EmitScalarOp(context, emit, OperFlags.RdRnRm, false);
  311. }
  312. public static void EmitScalarOp(ILEmitterCtx context, Action emit, OperFlags opers, bool signed)
  313. {
  314. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  315. bool rd = (opers & OperFlags.Rd) != 0;
  316. bool rn = (opers & OperFlags.Rn) != 0;
  317. bool rm = (opers & OperFlags.Rm) != 0;
  318. if (rd)
  319. {
  320. EmitVectorExtract(context, op.Rd, 0, op.Size, signed);
  321. }
  322. if (rn)
  323. {
  324. EmitVectorExtract(context, op.Rn, 0, op.Size, signed);
  325. }
  326. if (rm)
  327. {
  328. EmitVectorExtract(context, ((OpCodeSimdReg64)op).Rm, 0, op.Size, signed);
  329. }
  330. emit();
  331. EmitScalarSet(context, op.Rd, op.Size);
  332. }
  333. public static void EmitScalarUnaryOpF(ILEmitterCtx context, Action emit)
  334. {
  335. EmitScalarOpF(context, emit, OperFlags.Rn);
  336. }
  337. public static void EmitScalarBinaryOpF(ILEmitterCtx context, Action emit)
  338. {
  339. EmitScalarOpF(context, emit, OperFlags.RnRm);
  340. }
  341. public static void EmitScalarTernaryRaOpF(ILEmitterCtx context, Action emit)
  342. {
  343. EmitScalarOpF(context, emit, OperFlags.RaRnRm);
  344. }
  345. public static void EmitScalarOpF(ILEmitterCtx context, Action emit, OperFlags opers)
  346. {
  347. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  348. int sizeF = op.Size & 1;
  349. bool ra = (opers & OperFlags.Ra) != 0;
  350. bool rn = (opers & OperFlags.Rn) != 0;
  351. bool rm = (opers & OperFlags.Rm) != 0;
  352. if (ra)
  353. {
  354. EmitVectorExtractF(context, ((OpCodeSimdReg64)op).Ra, 0, sizeF);
  355. }
  356. if (rn)
  357. {
  358. EmitVectorExtractF(context, op.Rn, 0, sizeF);
  359. }
  360. if (rm)
  361. {
  362. EmitVectorExtractF(context, ((OpCodeSimdReg64)op).Rm, 0, sizeF);
  363. }
  364. emit();
  365. EmitScalarSetF(context, op.Rd, sizeF);
  366. }
  367. public static void EmitVectorUnaryOpF(ILEmitterCtx context, Action emit)
  368. {
  369. EmitVectorOpF(context, emit, OperFlags.Rn);
  370. }
  371. public static void EmitVectorBinaryOpF(ILEmitterCtx context, Action emit)
  372. {
  373. EmitVectorOpF(context, emit, OperFlags.RnRm);
  374. }
  375. public static void EmitVectorTernaryOpF(ILEmitterCtx context, Action emit)
  376. {
  377. EmitVectorOpF(context, emit, OperFlags.RdRnRm);
  378. }
  379. public static void EmitVectorOpF(ILEmitterCtx context, Action emit, OperFlags opers)
  380. {
  381. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  382. int sizeF = op.Size & 1;
  383. int bytes = op.GetBitsCount() >> 3;
  384. int elems = bytes >> sizeF + 2;
  385. bool rd = (opers & OperFlags.Rd) != 0;
  386. bool rn = (opers & OperFlags.Rn) != 0;
  387. bool rm = (opers & OperFlags.Rm) != 0;
  388. for (int index = 0; index < elems; index++)
  389. {
  390. if (rd)
  391. {
  392. EmitVectorExtractF(context, op.Rd, index, sizeF);
  393. }
  394. if (rn)
  395. {
  396. EmitVectorExtractF(context, op.Rn, index, sizeF);
  397. }
  398. if (rm)
  399. {
  400. EmitVectorExtractF(context, ((OpCodeSimdReg64)op).Rm, index, sizeF);
  401. }
  402. emit();
  403. EmitVectorInsertF(context, op.Rd, index, sizeF);
  404. }
  405. if (op.RegisterSize == RegisterSize.Simd64)
  406. {
  407. EmitVectorZeroUpper(context, op.Rd);
  408. }
  409. }
  410. public static void EmitVectorBinaryOpByElemF(ILEmitterCtx context, Action emit)
  411. {
  412. OpCodeSimdRegElemF64 op = (OpCodeSimdRegElemF64)context.CurrOp;
  413. EmitVectorOpByElemF(context, emit, op.Index, ternary: false);
  414. }
  415. public static void EmitVectorTernaryOpByElemF(ILEmitterCtx context, Action emit)
  416. {
  417. OpCodeSimdRegElemF64 op = (OpCodeSimdRegElemF64)context.CurrOp;
  418. EmitVectorOpByElemF(context, emit, op.Index, ternary: true);
  419. }
  420. public static void EmitVectorOpByElemF(ILEmitterCtx context, Action emit, int elem, bool ternary)
  421. {
  422. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  423. int sizeF = op.Size & 1;
  424. int bytes = op.GetBitsCount() >> 3;
  425. int elems = bytes >> sizeF + 2;
  426. for (int index = 0; index < elems; index++)
  427. {
  428. if (ternary)
  429. {
  430. EmitVectorExtractF(context, op.Rd, index, sizeF);
  431. }
  432. EmitVectorExtractF(context, op.Rn, index, sizeF);
  433. EmitVectorExtractF(context, op.Rm, elem, sizeF);
  434. emit();
  435. EmitVectorInsertTmpF(context, index, sizeF);
  436. }
  437. context.EmitLdvectmp();
  438. context.EmitStvec(op.Rd);
  439. if (op.RegisterSize == RegisterSize.Simd64)
  440. {
  441. EmitVectorZeroUpper(context, op.Rd);
  442. }
  443. }
  444. public static void EmitVectorUnaryOpSx(ILEmitterCtx context, Action emit)
  445. {
  446. EmitVectorOp(context, emit, OperFlags.Rn, true);
  447. }
  448. public static void EmitVectorBinaryOpSx(ILEmitterCtx context, Action emit)
  449. {
  450. EmitVectorOp(context, emit, OperFlags.RnRm, true);
  451. }
  452. public static void EmitVectorTernaryOpSx(ILEmitterCtx context, Action emit)
  453. {
  454. EmitVectorOp(context, emit, OperFlags.RdRnRm, true);
  455. }
  456. public static void EmitVectorUnaryOpZx(ILEmitterCtx context, Action emit)
  457. {
  458. EmitVectorOp(context, emit, OperFlags.Rn, false);
  459. }
  460. public static void EmitVectorBinaryOpZx(ILEmitterCtx context, Action emit)
  461. {
  462. EmitVectorOp(context, emit, OperFlags.RnRm, false);
  463. }
  464. public static void EmitVectorTernaryOpZx(ILEmitterCtx context, Action emit)
  465. {
  466. EmitVectorOp(context, emit, OperFlags.RdRnRm, false);
  467. }
  468. public static void EmitVectorOp(ILEmitterCtx context, Action emit, OperFlags opers, bool signed)
  469. {
  470. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  471. int bytes = op.GetBitsCount() >> 3;
  472. int elems = bytes >> op.Size;
  473. bool rd = (opers & OperFlags.Rd) != 0;
  474. bool rn = (opers & OperFlags.Rn) != 0;
  475. bool rm = (opers & OperFlags.Rm) != 0;
  476. for (int index = 0; index < elems; index++)
  477. {
  478. if (rd)
  479. {
  480. EmitVectorExtract(context, op.Rd, index, op.Size, signed);
  481. }
  482. if (rn)
  483. {
  484. EmitVectorExtract(context, op.Rn, index, op.Size, signed);
  485. }
  486. if (rm)
  487. {
  488. EmitVectorExtract(context, ((OpCodeSimdReg64)op).Rm, index, op.Size, signed);
  489. }
  490. emit();
  491. EmitVectorInsert(context, op.Rd, index, op.Size);
  492. }
  493. if (op.RegisterSize == RegisterSize.Simd64)
  494. {
  495. EmitVectorZeroUpper(context, op.Rd);
  496. }
  497. }
  498. public static void EmitVectorBinaryOpByElemSx(ILEmitterCtx context, Action emit)
  499. {
  500. OpCodeSimdRegElem64 op = (OpCodeSimdRegElem64)context.CurrOp;
  501. EmitVectorOpByElem(context, emit, op.Index, false, true);
  502. }
  503. public static void EmitVectorBinaryOpByElemZx(ILEmitterCtx context, Action emit)
  504. {
  505. OpCodeSimdRegElem64 op = (OpCodeSimdRegElem64)context.CurrOp;
  506. EmitVectorOpByElem(context, emit, op.Index, false, false);
  507. }
  508. public static void EmitVectorTernaryOpByElemZx(ILEmitterCtx context, Action emit)
  509. {
  510. OpCodeSimdRegElem64 op = (OpCodeSimdRegElem64)context.CurrOp;
  511. EmitVectorOpByElem(context, emit, op.Index, true, false);
  512. }
  513. public static void EmitVectorOpByElem(ILEmitterCtx context, Action emit, int elem, bool ternary, bool signed)
  514. {
  515. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  516. int bytes = op.GetBitsCount() >> 3;
  517. int elems = bytes >> op.Size;
  518. EmitVectorExtract(context, op.Rm, elem, op.Size, signed);
  519. context.EmitSttmp();
  520. for (int index = 0; index < elems; index++)
  521. {
  522. if (ternary)
  523. {
  524. EmitVectorExtract(context, op.Rd, index, op.Size, signed);
  525. }
  526. EmitVectorExtract(context, op.Rn, index, op.Size, signed);
  527. context.EmitLdtmp();
  528. emit();
  529. EmitVectorInsertTmp(context, index, op.Size);
  530. }
  531. context.EmitLdvectmp();
  532. context.EmitStvec(op.Rd);
  533. if (op.RegisterSize == RegisterSize.Simd64)
  534. {
  535. EmitVectorZeroUpper(context, op.Rd);
  536. }
  537. }
  538. public static void EmitVectorImmUnaryOp(ILEmitterCtx context, Action emit)
  539. {
  540. EmitVectorImmOp(context, emit, false);
  541. }
  542. public static void EmitVectorImmBinaryOp(ILEmitterCtx context, Action emit)
  543. {
  544. EmitVectorImmOp(context, emit, true);
  545. }
  546. public static void EmitVectorImmOp(ILEmitterCtx context, Action emit, bool binary)
  547. {
  548. OpCodeSimdImm64 op = (OpCodeSimdImm64)context.CurrOp;
  549. int bytes = op.GetBitsCount() >> 3;
  550. int elems = bytes >> op.Size;
  551. for (int index = 0; index < elems; index++)
  552. {
  553. if (binary)
  554. {
  555. EmitVectorExtractZx(context, op.Rd, index, op.Size);
  556. }
  557. context.EmitLdc_I8(op.Imm);
  558. emit();
  559. EmitVectorInsert(context, op.Rd, index, op.Size);
  560. }
  561. if (op.RegisterSize == RegisterSize.Simd64)
  562. {
  563. EmitVectorZeroUpper(context, op.Rd);
  564. }
  565. }
  566. public static void EmitVectorWidenRmBinaryOpSx(ILEmitterCtx context, Action emit)
  567. {
  568. EmitVectorWidenRmBinaryOp(context, emit, true);
  569. }
  570. public static void EmitVectorWidenRmBinaryOpZx(ILEmitterCtx context, Action emit)
  571. {
  572. EmitVectorWidenRmBinaryOp(context, emit, false);
  573. }
  574. public static void EmitVectorWidenRmBinaryOp(ILEmitterCtx context, Action emit, bool signed)
  575. {
  576. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  577. int elems = 8 >> op.Size;
  578. int part = op.RegisterSize == RegisterSize.Simd128 ? elems : 0;
  579. for (int index = 0; index < elems; index++)
  580. {
  581. EmitVectorExtract(context, op.Rn, index, op.Size + 1, signed);
  582. EmitVectorExtract(context, op.Rm, part + index, op.Size, signed);
  583. emit();
  584. EmitVectorInsertTmp(context, index, op.Size + 1);
  585. }
  586. context.EmitLdvectmp();
  587. context.EmitStvec(op.Rd);
  588. }
  589. public static void EmitVectorWidenRnRmBinaryOpSx(ILEmitterCtx context, Action emit)
  590. {
  591. EmitVectorWidenRnRmOp(context, emit, false, true);
  592. }
  593. public static void EmitVectorWidenRnRmBinaryOpZx(ILEmitterCtx context, Action emit)
  594. {
  595. EmitVectorWidenRnRmOp(context, emit, false, false);
  596. }
  597. public static void EmitVectorWidenRnRmTernaryOpSx(ILEmitterCtx context, Action emit)
  598. {
  599. EmitVectorWidenRnRmOp(context, emit, true, true);
  600. }
  601. public static void EmitVectorWidenRnRmTernaryOpZx(ILEmitterCtx context, Action emit)
  602. {
  603. EmitVectorWidenRnRmOp(context, emit, true, false);
  604. }
  605. public static void EmitVectorWidenRnRmOp(ILEmitterCtx context, Action emit, bool ternary, bool signed)
  606. {
  607. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  608. int elems = 8 >> op.Size;
  609. int part = op.RegisterSize == RegisterSize.Simd128 ? elems : 0;
  610. for (int index = 0; index < elems; index++)
  611. {
  612. if (ternary)
  613. {
  614. EmitVectorExtract(context, op.Rd, index, op.Size + 1, signed);
  615. }
  616. EmitVectorExtract(context, op.Rn, part + index, op.Size, signed);
  617. EmitVectorExtract(context, op.Rm, part + index, op.Size, signed);
  618. emit();
  619. EmitVectorInsertTmp(context, index, op.Size + 1);
  620. }
  621. context.EmitLdvectmp();
  622. context.EmitStvec(op.Rd);
  623. }
  624. public static void EmitVectorPairwiseOpSx(ILEmitterCtx context, Action emit)
  625. {
  626. EmitVectorPairwiseOp(context, emit, true);
  627. }
  628. public static void EmitVectorPairwiseOpZx(ILEmitterCtx context, Action emit)
  629. {
  630. EmitVectorPairwiseOp(context, emit, false);
  631. }
  632. public static void EmitVectorPairwiseOp(ILEmitterCtx context, Action emit, bool signed)
  633. {
  634. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  635. int words = op.GetBitsCount() >> 4;
  636. int pairs = words >> op.Size;
  637. for (int index = 0; index < pairs; index++)
  638. {
  639. int idx = index << 1;
  640. EmitVectorExtract(context, op.Rn, idx, op.Size, signed);
  641. EmitVectorExtract(context, op.Rn, idx + 1, op.Size, signed);
  642. emit();
  643. EmitVectorExtract(context, op.Rm, idx, op.Size, signed);
  644. EmitVectorExtract(context, op.Rm, idx + 1, op.Size, signed);
  645. emit();
  646. EmitVectorInsertTmp(context, pairs + index, op.Size);
  647. EmitVectorInsertTmp(context, index, op.Size);
  648. }
  649. context.EmitLdvectmp();
  650. context.EmitStvec(op.Rd);
  651. if (op.RegisterSize == RegisterSize.Simd64)
  652. {
  653. EmitVectorZeroUpper(context, op.Rd);
  654. }
  655. }
  656. public static void EmitVectorPairwiseOpF(ILEmitterCtx context, Action emit)
  657. {
  658. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  659. int sizeF = op.Size & 1;
  660. int words = op.GetBitsCount() >> 4;
  661. int pairs = words >> sizeF + 2;
  662. for (int index = 0; index < pairs; index++)
  663. {
  664. int idx = index << 1;
  665. EmitVectorExtractF(context, op.Rn, idx, sizeF);
  666. EmitVectorExtractF(context, op.Rn, idx + 1, sizeF);
  667. emit();
  668. EmitVectorExtractF(context, op.Rm, idx, sizeF);
  669. EmitVectorExtractF(context, op.Rm, idx + 1, sizeF);
  670. emit();
  671. EmitVectorInsertTmpF(context, pairs + index, sizeF);
  672. EmitVectorInsertTmpF(context, index, sizeF);
  673. }
  674. context.EmitLdvectmp();
  675. context.EmitStvec(op.Rd);
  676. if (op.RegisterSize == RegisterSize.Simd64)
  677. {
  678. EmitVectorZeroUpper(context, op.Rd);
  679. }
  680. }
  681. public static void EmitVectorPairwiseSseOrSse2OpF(ILEmitterCtx context, string name)
  682. {
  683. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  684. int sizeF = op.Size & 1;
  685. if (sizeF == 0)
  686. {
  687. if (op.RegisterSize == RegisterSize.Simd64)
  688. {
  689. Type[] types = new Type[] { typeof(Vector128<float>), typeof(Vector128<float>) };
  690. context.EmitLdvec(op.Rn);
  691. context.EmitLdvec(op.Rm);
  692. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.UnpackLow), types));
  693. context.Emit(OpCodes.Dup);
  694. context.EmitStvectmp();
  695. VectorHelper.EmitCall(context, nameof(VectorHelper.VectorSingleZero));
  696. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.MoveLowToHigh), types));
  697. VectorHelper.EmitCall(context, nameof(VectorHelper.VectorSingleZero));
  698. context.EmitLdvectmp();
  699. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.MoveHighToLow), types));
  700. context.EmitCall(typeof(Sse).GetMethod(name, types));
  701. context.EmitStvec(op.Rd);
  702. }
  703. else /* if (op.RegisterSize == RegisterSize.Simd128) */
  704. {
  705. Type[] typesSfl = new Type[] { typeof(Vector128<float>), typeof(Vector128<float>), typeof(byte) };
  706. Type[] types = new Type[] { typeof(Vector128<float>), typeof(Vector128<float>) };
  707. context.EmitLdvec(op.Rn);
  708. context.Emit(OpCodes.Dup);
  709. context.EmitStvectmp();
  710. context.EmitLdvec(op.Rm);
  711. context.Emit(OpCodes.Dup);
  712. context.EmitStvectmp2();
  713. context.EmitLdc_I4(2 << 6 | 0 << 4 | 2 << 2 | 0 << 0);
  714. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.Shuffle), typesSfl));
  715. context.EmitLdvectmp();
  716. context.EmitLdvectmp2();
  717. context.EmitLdc_I4(3 << 6 | 1 << 4 | 3 << 2 | 1 << 0);
  718. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.Shuffle), typesSfl));
  719. context.EmitCall(typeof(Sse).GetMethod(name, types));
  720. context.EmitStvec(op.Rd);
  721. }
  722. }
  723. else /* if (sizeF == 1) */
  724. {
  725. Type[] types = new Type[] { typeof(Vector128<double>), typeof(Vector128<double>) };
  726. EmitLdvecWithCastToDouble(context, op.Rn);
  727. context.Emit(OpCodes.Dup);
  728. context.EmitStvectmp();
  729. EmitLdvecWithCastToDouble(context, op.Rm);
  730. context.Emit(OpCodes.Dup);
  731. context.EmitStvectmp2();
  732. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.UnpackLow), types));
  733. context.EmitLdvectmp();
  734. context.EmitLdvectmp2();
  735. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.UnpackHigh), types));
  736. context.EmitCall(typeof(Sse2).GetMethod(name, types));
  737. EmitStvecWithCastFromDouble(context, op.Rd);
  738. }
  739. }
  740. [Flags]
  741. public enum SaturatingFlags
  742. {
  743. Scalar = 1 << 0,
  744. Signed = 1 << 1,
  745. Add = 1 << 2,
  746. Sub = 1 << 3,
  747. Accumulate = 1 << 4,
  748. ScalarSx = Scalar | Signed,
  749. ScalarZx = Scalar,
  750. VectorSx = Signed,
  751. VectorZx = 0
  752. }
  753. public static void EmitScalarSaturatingUnaryOpSx(ILEmitterCtx context, Action emit)
  754. {
  755. EmitSaturatingUnaryOpSx(context, emit, SaturatingFlags.ScalarSx);
  756. }
  757. public static void EmitVectorSaturatingUnaryOpSx(ILEmitterCtx context, Action emit)
  758. {
  759. EmitSaturatingUnaryOpSx(context, emit, SaturatingFlags.VectorSx);
  760. }
  761. public static void EmitSaturatingUnaryOpSx(ILEmitterCtx context, Action emit, SaturatingFlags flags)
  762. {
  763. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  764. bool scalar = (flags & SaturatingFlags.Scalar) != 0;
  765. int bytes = op.GetBitsCount() >> 3;
  766. int elems = !scalar ? bytes >> op.Size : 1;
  767. if (scalar)
  768. {
  769. EmitVectorZeroLowerTmp(context);
  770. }
  771. for (int index = 0; index < elems; index++)
  772. {
  773. EmitVectorExtractSx(context, op.Rn, index, op.Size);
  774. emit();
  775. if (op.Size <= 2)
  776. {
  777. EmitSatQ(context, op.Size, true, true);
  778. }
  779. else /* if (op.Size == 3) */
  780. {
  781. EmitUnarySignedSatQAbsOrNeg(context);
  782. }
  783. EmitVectorInsertTmp(context, index, op.Size);
  784. }
  785. context.EmitLdvectmp();
  786. context.EmitStvec(op.Rd);
  787. if ((op.RegisterSize == RegisterSize.Simd64) || scalar)
  788. {
  789. EmitVectorZeroUpper(context, op.Rd);
  790. }
  791. }
  792. public static void EmitScalarSaturatingBinaryOpSx(ILEmitterCtx context, SaturatingFlags flags)
  793. {
  794. EmitSaturatingBinaryOp(context, () => { }, SaturatingFlags.ScalarSx | flags);
  795. }
  796. public static void EmitScalarSaturatingBinaryOpZx(ILEmitterCtx context, SaturatingFlags flags)
  797. {
  798. EmitSaturatingBinaryOp(context, () => { }, SaturatingFlags.ScalarZx | flags);
  799. }
  800. public static void EmitVectorSaturatingBinaryOpSx(ILEmitterCtx context, SaturatingFlags flags)
  801. {
  802. EmitSaturatingBinaryOp(context, () => { }, SaturatingFlags.VectorSx | flags);
  803. }
  804. public static void EmitVectorSaturatingBinaryOpZx(ILEmitterCtx context, SaturatingFlags flags)
  805. {
  806. EmitSaturatingBinaryOp(context, () => { }, SaturatingFlags.VectorZx | flags);
  807. }
  808. public static void EmitSaturatingBinaryOp(ILEmitterCtx context, Action emit, SaturatingFlags flags)
  809. {
  810. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  811. bool scalar = (flags & SaturatingFlags.Scalar) != 0;
  812. bool signed = (flags & SaturatingFlags.Signed) != 0;
  813. bool add = (flags & SaturatingFlags.Add) != 0;
  814. bool sub = (flags & SaturatingFlags.Sub) != 0;
  815. bool accumulate = (flags & SaturatingFlags.Accumulate) != 0;
  816. int bytes = op.GetBitsCount() >> 3;
  817. int elems = !scalar ? bytes >> op.Size : 1;
  818. if (scalar)
  819. {
  820. EmitVectorZeroLowerTmp(context);
  821. }
  822. if (add || sub)
  823. {
  824. for (int index = 0; index < elems; index++)
  825. {
  826. EmitVectorExtract(context, op.Rn, index, op.Size, signed);
  827. EmitVectorExtract(context, ((OpCodeSimdReg64)op).Rm, index, op.Size, signed);
  828. if (op.Size <= 2)
  829. {
  830. context.Emit(add ? OpCodes.Add : OpCodes.Sub);
  831. EmitSatQ(context, op.Size, true, signed);
  832. }
  833. else /* if (op.Size == 3) */
  834. {
  835. if (add)
  836. {
  837. EmitBinarySatQAdd(context, signed);
  838. }
  839. else /* if (sub) */
  840. {
  841. EmitBinarySatQSub(context, signed);
  842. }
  843. }
  844. EmitVectorInsertTmp(context, index, op.Size);
  845. }
  846. }
  847. else if (accumulate)
  848. {
  849. for (int index = 0; index < elems; index++)
  850. {
  851. EmitVectorExtract(context, op.Rn, index, op.Size, !signed);
  852. EmitVectorExtract(context, op.Rd, index, op.Size, signed);
  853. if (op.Size <= 2)
  854. {
  855. context.Emit(OpCodes.Add);
  856. EmitSatQ(context, op.Size, true, signed);
  857. }
  858. else /* if (op.Size == 3) */
  859. {
  860. EmitBinarySatQAccumulate(context, signed);
  861. }
  862. EmitVectorInsertTmp(context, index, op.Size);
  863. }
  864. }
  865. else
  866. {
  867. for (int index = 0; index < elems; index++)
  868. {
  869. EmitVectorExtract(context, op.Rn, index, op.Size, signed);
  870. EmitVectorExtract(context, ((OpCodeSimdReg64)op).Rm, index, op.Size, signed);
  871. emit();
  872. EmitSatQ(context, op.Size, true, signed);
  873. EmitVectorInsertTmp(context, index, op.Size);
  874. }
  875. }
  876. context.EmitLdvectmp();
  877. context.EmitStvec(op.Rd);
  878. if ((op.RegisterSize == RegisterSize.Simd64) || scalar)
  879. {
  880. EmitVectorZeroUpper(context, op.Rd);
  881. }
  882. }
  883. [Flags]
  884. public enum SaturatingNarrowFlags
  885. {
  886. Scalar = 1 << 0,
  887. SignedSrc = 1 << 1,
  888. SignedDst = 1 << 2,
  889. ScalarSxSx = Scalar | SignedSrc | SignedDst,
  890. ScalarSxZx = Scalar | SignedSrc,
  891. ScalarZxZx = Scalar,
  892. VectorSxSx = SignedSrc | SignedDst,
  893. VectorSxZx = SignedSrc,
  894. VectorZxZx = 0
  895. }
  896. public static void EmitSaturatingNarrowOp(ILEmitterCtx context, SaturatingNarrowFlags flags)
  897. {
  898. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  899. bool scalar = (flags & SaturatingNarrowFlags.Scalar) != 0;
  900. bool signedSrc = (flags & SaturatingNarrowFlags.SignedSrc) != 0;
  901. bool signedDst = (flags & SaturatingNarrowFlags.SignedDst) != 0;
  902. int elems = !scalar ? 8 >> op.Size : 1;
  903. int part = !scalar && (op.RegisterSize == RegisterSize.Simd128) ? elems : 0;
  904. if (scalar)
  905. {
  906. EmitVectorZeroLowerTmp(context);
  907. }
  908. if (part != 0)
  909. {
  910. context.EmitLdvec(op.Rd);
  911. context.EmitStvectmp();
  912. }
  913. for (int index = 0; index < elems; index++)
  914. {
  915. EmitVectorExtract(context, op.Rn, index, op.Size + 1, signedSrc);
  916. EmitSatQ(context, op.Size, signedSrc, signedDst);
  917. EmitVectorInsertTmp(context, part + index, op.Size);
  918. }
  919. context.EmitLdvectmp();
  920. context.EmitStvec(op.Rd);
  921. if (part == 0)
  922. {
  923. EmitVectorZeroUpper(context, op.Rd);
  924. }
  925. }
  926. // TSrc (16bit, 32bit, 64bit; signed, unsigned) > TDst (8bit, 16bit, 32bit; signed, unsigned).
  927. public static void EmitSatQ(
  928. ILEmitterCtx context,
  929. int sizeDst,
  930. bool signedSrc,
  931. bool signedDst)
  932. {
  933. if (sizeDst > 2)
  934. {
  935. throw new ArgumentOutOfRangeException(nameof(sizeDst));
  936. }
  937. context.EmitLdc_I4(sizeDst);
  938. context.EmitLdarg(TranslatedSub.StateArgIdx);
  939. if (signedSrc)
  940. {
  941. SoftFallback.EmitCall(context, signedDst
  942. ? nameof(SoftFallback.SignedSrcSignedDstSatQ)
  943. : nameof(SoftFallback.SignedSrcUnsignedDstSatQ));
  944. }
  945. else
  946. {
  947. SoftFallback.EmitCall(context, signedDst
  948. ? nameof(SoftFallback.UnsignedSrcSignedDstSatQ)
  949. : nameof(SoftFallback.UnsignedSrcUnsignedDstSatQ));
  950. }
  951. }
  952. // TSrc (64bit) == TDst (64bit); signed.
  953. public static void EmitUnarySignedSatQAbsOrNeg(ILEmitterCtx context)
  954. {
  955. if (((OpCodeSimd64)context.CurrOp).Size < 3)
  956. {
  957. throw new InvalidOperationException();
  958. }
  959. context.EmitLdarg(TranslatedSub.StateArgIdx);
  960. SoftFallback.EmitCall(context, nameof(SoftFallback.UnarySignedSatQAbsOrNeg));
  961. }
  962. // TSrcs (64bit) == TDst (64bit); signed, unsigned.
  963. public static void EmitBinarySatQAdd(ILEmitterCtx context, bool signed)
  964. {
  965. if (((OpCodeSimdReg64)context.CurrOp).Size < 3)
  966. {
  967. throw new InvalidOperationException();
  968. }
  969. context.EmitLdarg(TranslatedSub.StateArgIdx);
  970. SoftFallback.EmitCall(context, signed
  971. ? nameof(SoftFallback.BinarySignedSatQAdd)
  972. : nameof(SoftFallback.BinaryUnsignedSatQAdd));
  973. }
  974. // TSrcs (64bit) == TDst (64bit); signed, unsigned.
  975. public static void EmitBinarySatQSub(ILEmitterCtx context, bool signed)
  976. {
  977. if (((OpCodeSimdReg64)context.CurrOp).Size < 3)
  978. {
  979. throw new InvalidOperationException();
  980. }
  981. context.EmitLdarg(TranslatedSub.StateArgIdx);
  982. SoftFallback.EmitCall(context, signed
  983. ? nameof(SoftFallback.BinarySignedSatQSub)
  984. : nameof(SoftFallback.BinaryUnsignedSatQSub));
  985. }
  986. // TSrcs (64bit) == TDst (64bit); signed, unsigned.
  987. public static void EmitBinarySatQAccumulate(ILEmitterCtx context, bool signed)
  988. {
  989. if (((OpCodeSimd64)context.CurrOp).Size < 3)
  990. {
  991. throw new InvalidOperationException();
  992. }
  993. context.EmitLdarg(TranslatedSub.StateArgIdx);
  994. SoftFallback.EmitCall(context, signed
  995. ? nameof(SoftFallback.BinarySignedSatQAcc)
  996. : nameof(SoftFallback.BinaryUnsignedSatQAcc));
  997. }
  998. public static void EmitScalarSet(ILEmitterCtx context, int reg, int size)
  999. {
  1000. EmitVectorZeroAll(context, reg);
  1001. EmitVectorInsert(context, reg, 0, size);
  1002. }
  1003. public static void EmitScalarSetF(ILEmitterCtx context, int reg, int size)
  1004. {
  1005. if (Optimizations.UseSse41 && size == 0)
  1006. {
  1007. //If the type is float, we can perform insertion and
  1008. //zero the upper bits with a single instruction (INSERTPS);
  1009. context.EmitLdvec(reg);
  1010. VectorHelper.EmitCall(context, nameof(VectorHelper.Sse41VectorInsertScalarSingle));
  1011. context.EmitStvec(reg);
  1012. }
  1013. else
  1014. {
  1015. EmitVectorZeroAll(context, reg);
  1016. EmitVectorInsertF(context, reg, 0, size);
  1017. }
  1018. }
  1019. public static void EmitVectorExtractSx(ILEmitterCtx context, int reg, int index, int size)
  1020. {
  1021. EmitVectorExtract(context, reg, index, size, true);
  1022. }
  1023. public static void EmitVectorExtractZx(ILEmitterCtx context, int reg, int index, int size)
  1024. {
  1025. EmitVectorExtract(context, reg, index, size, false);
  1026. }
  1027. public static void EmitVectorExtract(ILEmitterCtx context, int reg, int index, int size, bool signed)
  1028. {
  1029. ThrowIfInvalid(index, size);
  1030. context.EmitLdvec(reg);
  1031. context.EmitLdc_I4(index);
  1032. context.EmitLdc_I4(size);
  1033. VectorHelper.EmitCall(context, signed
  1034. ? nameof(VectorHelper.VectorExtractIntSx)
  1035. : nameof(VectorHelper.VectorExtractIntZx));
  1036. }
  1037. public static void EmitVectorExtractF(ILEmitterCtx context, int reg, int index, int size)
  1038. {
  1039. ThrowIfInvalidF(index, size);
  1040. context.EmitLdvec(reg);
  1041. context.EmitLdc_I4(index);
  1042. if (size == 0)
  1043. {
  1044. VectorHelper.EmitCall(context, nameof(VectorHelper.VectorExtractSingle));
  1045. }
  1046. else if (size == 1)
  1047. {
  1048. VectorHelper.EmitCall(context, nameof(VectorHelper.VectorExtractDouble));
  1049. }
  1050. else
  1051. {
  1052. throw new ArgumentOutOfRangeException(nameof(size));
  1053. }
  1054. }
  1055. public static void EmitVectorZeroAll(ILEmitterCtx context, int reg)
  1056. {
  1057. if (Optimizations.UseSse)
  1058. {
  1059. VectorHelper.EmitCall(context, nameof(VectorHelper.VectorSingleZero));
  1060. context.EmitStvec(reg);
  1061. }
  1062. else
  1063. {
  1064. EmitVectorZeroLower(context, reg);
  1065. EmitVectorZeroUpper(context, reg);
  1066. }
  1067. }
  1068. public static void EmitVectorZeroLower(ILEmitterCtx context, int reg)
  1069. {
  1070. EmitVectorInsert(context, reg, 0, 3, 0);
  1071. }
  1072. public static void EmitVectorZeroLowerTmp(ILEmitterCtx context)
  1073. {
  1074. if (Optimizations.UseSse)
  1075. {
  1076. context.EmitLdvectmp();
  1077. VectorHelper.EmitCall(context, nameof(VectorHelper.VectorSingleZero));
  1078. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.MoveHighToLow)));
  1079. context.EmitStvectmp();
  1080. }
  1081. else
  1082. {
  1083. EmitVectorInsertTmp(context, 0, 3, 0);
  1084. }
  1085. }
  1086. public static void EmitVectorZeroUpper(ILEmitterCtx context, int reg)
  1087. {
  1088. if (Optimizations.UseSse)
  1089. {
  1090. //TODO: Use Sse2.MoveScalar once it is fixed,
  1091. //as of the time of writing it just crashes the JIT (SDK 2.1.500).
  1092. /*Type[] typesMov = new Type[] { typeof(Vector128<ulong>) };
  1093. EmitLdvecWithUnsignedCast(context, reg, 3);
  1094. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.MoveScalar), typesMov));
  1095. EmitStvecWithUnsignedCast(context, reg, 3);*/
  1096. context.EmitLdvec(reg);
  1097. VectorHelper.EmitCall(context, nameof(VectorHelper.VectorSingleZero));
  1098. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.MoveLowToHigh)));
  1099. context.EmitStvec(reg);
  1100. }
  1101. else
  1102. {
  1103. EmitVectorInsert(context, reg, 1, 3, 0);
  1104. }
  1105. }
  1106. public static void EmitVectorZero32_128(ILEmitterCtx context, int reg)
  1107. {
  1108. if (!Sse.IsSupported)
  1109. {
  1110. throw new PlatformNotSupportedException();
  1111. }
  1112. VectorHelper.EmitCall(context, nameof(VectorHelper.VectorSingleZero));
  1113. context.EmitLdvec(reg);
  1114. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.MoveScalar)));
  1115. context.EmitStvec(reg);
  1116. }
  1117. public static void EmitVectorInsert(ILEmitterCtx context, int reg, int index, int size)
  1118. {
  1119. ThrowIfInvalid(index, size);
  1120. context.EmitLdvec(reg);
  1121. context.EmitLdc_I4(index);
  1122. context.EmitLdc_I4(size);
  1123. VectorHelper.EmitCall(context, nameof(VectorHelper.VectorInsertInt));
  1124. context.EmitStvec(reg);
  1125. }
  1126. public static void EmitVectorInsertTmp(ILEmitterCtx context, int index, int size)
  1127. {
  1128. ThrowIfInvalid(index, size);
  1129. context.EmitLdvectmp();
  1130. context.EmitLdc_I4(index);
  1131. context.EmitLdc_I4(size);
  1132. VectorHelper.EmitCall(context, nameof(VectorHelper.VectorInsertInt));
  1133. context.EmitStvectmp();
  1134. }
  1135. public static void EmitVectorInsert(ILEmitterCtx context, int reg, int index, int size, long value)
  1136. {
  1137. ThrowIfInvalid(index, size);
  1138. context.EmitLdc_I8(value);
  1139. context.EmitLdvec(reg);
  1140. context.EmitLdc_I4(index);
  1141. context.EmitLdc_I4(size);
  1142. VectorHelper.EmitCall(context, nameof(VectorHelper.VectorInsertInt));
  1143. context.EmitStvec(reg);
  1144. }
  1145. public static void EmitVectorInsertTmp(ILEmitterCtx context, int index, int size, long value)
  1146. {
  1147. ThrowIfInvalid(index, size);
  1148. context.EmitLdc_I8(value);
  1149. context.EmitLdvectmp();
  1150. context.EmitLdc_I4(index);
  1151. context.EmitLdc_I4(size);
  1152. VectorHelper.EmitCall(context, nameof(VectorHelper.VectorInsertInt));
  1153. context.EmitStvectmp();
  1154. }
  1155. public static void EmitVectorInsertF(ILEmitterCtx context, int reg, int index, int size)
  1156. {
  1157. ThrowIfInvalidF(index, size);
  1158. context.EmitLdvec(reg);
  1159. context.EmitLdc_I4(index);
  1160. if (size == 0)
  1161. {
  1162. VectorHelper.EmitCall(context, nameof(VectorHelper.VectorInsertSingle));
  1163. }
  1164. else if (size == 1)
  1165. {
  1166. VectorHelper.EmitCall(context, nameof(VectorHelper.VectorInsertDouble));
  1167. }
  1168. else
  1169. {
  1170. throw new ArgumentOutOfRangeException(nameof(size));
  1171. }
  1172. context.EmitStvec(reg);
  1173. }
  1174. public static void EmitVectorInsertTmpF(ILEmitterCtx context, int index, int size)
  1175. {
  1176. ThrowIfInvalidF(index, size);
  1177. context.EmitLdvectmp();
  1178. context.EmitLdc_I4(index);
  1179. if (size == 0)
  1180. {
  1181. VectorHelper.EmitCall(context, nameof(VectorHelper.VectorInsertSingle));
  1182. }
  1183. else if (size == 1)
  1184. {
  1185. VectorHelper.EmitCall(context, nameof(VectorHelper.VectorInsertDouble));
  1186. }
  1187. else
  1188. {
  1189. throw new ArgumentOutOfRangeException(nameof(size));
  1190. }
  1191. context.EmitStvectmp();
  1192. }
  1193. private static void ThrowIfInvalid(int index, int size)
  1194. {
  1195. if ((uint)size > 3u)
  1196. {
  1197. throw new ArgumentOutOfRangeException(nameof(size));
  1198. }
  1199. if ((uint)index >= 16u >> size)
  1200. {
  1201. throw new ArgumentOutOfRangeException(nameof(index));
  1202. }
  1203. }
  1204. private static void ThrowIfInvalidF(int index, int size)
  1205. {
  1206. if ((uint)size > 1u)
  1207. {
  1208. throw new ArgumentOutOfRangeException(nameof(size));
  1209. }
  1210. if ((uint)index >= 4u >> size)
  1211. {
  1212. throw new ArgumentOutOfRangeException(nameof(index));
  1213. }
  1214. }
  1215. }
  1216. }