InstEmitSimdHelper.cs 48 KB

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