InstEmitSimdHelper.cs 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641
  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, ternary: false, signed: true);
  502. }
  503. public static void EmitVectorBinaryOpByElemZx(ILEmitterCtx context, Action emit)
  504. {
  505. OpCodeSimdRegElem64 op = (OpCodeSimdRegElem64)context.CurrOp;
  506. EmitVectorOpByElem(context, emit, op.Index, ternary: false, signed: false);
  507. }
  508. public static void EmitVectorTernaryOpByElemZx(ILEmitterCtx context, Action emit)
  509. {
  510. OpCodeSimdRegElem64 op = (OpCodeSimdRegElem64)context.CurrOp;
  511. EmitVectorOpByElem(context, emit, op.Index, ternary: true, signed: 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 EmitVectorWidenBinaryOpByElemSx(ILEmitterCtx context, Action emit)
  625. {
  626. OpCodeSimdRegElem64 op = (OpCodeSimdRegElem64)context.CurrOp;
  627. EmitVectorWidenOpByElem(context, emit, op.Index, ternary: false, signed: true);
  628. }
  629. public static void EmitVectorWidenBinaryOpByElemZx(ILEmitterCtx context, Action emit)
  630. {
  631. OpCodeSimdRegElem64 op = (OpCodeSimdRegElem64)context.CurrOp;
  632. EmitVectorWidenOpByElem(context, emit, op.Index, ternary: false, signed: false);
  633. }
  634. public static void EmitVectorWidenTernaryOpByElemSx(ILEmitterCtx context, Action emit)
  635. {
  636. OpCodeSimdRegElem64 op = (OpCodeSimdRegElem64)context.CurrOp;
  637. EmitVectorWidenOpByElem(context, emit, op.Index, ternary: true, signed: true);
  638. }
  639. public static void EmitVectorWidenTernaryOpByElemZx(ILEmitterCtx context, Action emit)
  640. {
  641. OpCodeSimdRegElem64 op = (OpCodeSimdRegElem64)context.CurrOp;
  642. EmitVectorWidenOpByElem(context, emit, op.Index, ternary: true, signed: false);
  643. }
  644. public static void EmitVectorWidenOpByElem(ILEmitterCtx context, Action emit, int elem, bool ternary, bool signed)
  645. {
  646. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  647. int elems = 8 >> op.Size;
  648. int part = op.RegisterSize == RegisterSize.Simd128 ? elems : 0;
  649. EmitVectorExtract(context, op.Rm, elem, op.Size, signed);
  650. context.EmitSttmp();
  651. for (int index = 0; index < elems; index++)
  652. {
  653. if (ternary)
  654. {
  655. EmitVectorExtract(context, op.Rd, index, op.Size + 1, signed);
  656. }
  657. EmitVectorExtract(context, op.Rn, part + index, op.Size, signed);
  658. context.EmitLdtmp();
  659. emit();
  660. EmitVectorInsertTmp(context, index, op.Size + 1);
  661. }
  662. context.EmitLdvectmp();
  663. context.EmitStvec(op.Rd);
  664. }
  665. public static void EmitVectorPairwiseOpSx(ILEmitterCtx context, Action emit)
  666. {
  667. EmitVectorPairwiseOp(context, emit, true);
  668. }
  669. public static void EmitVectorPairwiseOpZx(ILEmitterCtx context, Action emit)
  670. {
  671. EmitVectorPairwiseOp(context, emit, false);
  672. }
  673. public static void EmitVectorPairwiseOp(ILEmitterCtx context, Action emit, bool signed)
  674. {
  675. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  676. int words = op.GetBitsCount() >> 4;
  677. int pairs = words >> op.Size;
  678. for (int index = 0; index < pairs; index++)
  679. {
  680. int idx = index << 1;
  681. EmitVectorExtract(context, op.Rn, idx, op.Size, signed);
  682. EmitVectorExtract(context, op.Rn, idx + 1, op.Size, signed);
  683. emit();
  684. EmitVectorExtract(context, op.Rm, idx, op.Size, signed);
  685. EmitVectorExtract(context, op.Rm, idx + 1, op.Size, signed);
  686. emit();
  687. EmitVectorInsertTmp(context, pairs + index, op.Size);
  688. EmitVectorInsertTmp(context, index, op.Size);
  689. }
  690. context.EmitLdvectmp();
  691. context.EmitStvec(op.Rd);
  692. if (op.RegisterSize == RegisterSize.Simd64)
  693. {
  694. EmitVectorZeroUpper(context, op.Rd);
  695. }
  696. }
  697. public static void EmitVectorPairwiseOpF(ILEmitterCtx context, Action emit)
  698. {
  699. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  700. int sizeF = op.Size & 1;
  701. int words = op.GetBitsCount() >> 4;
  702. int pairs = words >> sizeF + 2;
  703. for (int index = 0; index < pairs; index++)
  704. {
  705. int idx = index << 1;
  706. EmitVectorExtractF(context, op.Rn, idx, sizeF);
  707. EmitVectorExtractF(context, op.Rn, idx + 1, sizeF);
  708. emit();
  709. EmitVectorExtractF(context, op.Rm, idx, sizeF);
  710. EmitVectorExtractF(context, op.Rm, idx + 1, sizeF);
  711. emit();
  712. EmitVectorInsertTmpF(context, pairs + index, sizeF);
  713. EmitVectorInsertTmpF(context, index, sizeF);
  714. }
  715. context.EmitLdvectmp();
  716. context.EmitStvec(op.Rd);
  717. if (op.RegisterSize == RegisterSize.Simd64)
  718. {
  719. EmitVectorZeroUpper(context, op.Rd);
  720. }
  721. }
  722. public static void EmitVectorPairwiseSseOrSse2OpF(ILEmitterCtx context, string name)
  723. {
  724. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  725. int sizeF = op.Size & 1;
  726. if (sizeF == 0)
  727. {
  728. if (op.RegisterSize == RegisterSize.Simd64)
  729. {
  730. Type[] types = new Type[] { typeof(Vector128<float>), typeof(Vector128<float>) };
  731. context.EmitLdvec(op.Rn);
  732. context.EmitLdvec(op.Rm);
  733. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.UnpackLow), types));
  734. context.Emit(OpCodes.Dup);
  735. context.EmitStvectmp();
  736. VectorHelper.EmitCall(context, nameof(VectorHelper.VectorSingleZero));
  737. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.MoveLowToHigh), types));
  738. VectorHelper.EmitCall(context, nameof(VectorHelper.VectorSingleZero));
  739. context.EmitLdvectmp();
  740. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.MoveHighToLow), types));
  741. context.EmitCall(typeof(Sse).GetMethod(name, types));
  742. context.EmitStvec(op.Rd);
  743. }
  744. else /* if (op.RegisterSize == RegisterSize.Simd128) */
  745. {
  746. Type[] typesSfl = new Type[] { typeof(Vector128<float>), typeof(Vector128<float>), typeof(byte) };
  747. Type[] types = new Type[] { typeof(Vector128<float>), typeof(Vector128<float>) };
  748. context.EmitLdvec(op.Rn);
  749. context.Emit(OpCodes.Dup);
  750. context.EmitStvectmp();
  751. context.EmitLdvec(op.Rm);
  752. context.Emit(OpCodes.Dup);
  753. context.EmitStvectmp2();
  754. context.EmitLdc_I4(2 << 6 | 0 << 4 | 2 << 2 | 0 << 0);
  755. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.Shuffle), typesSfl));
  756. context.EmitLdvectmp();
  757. context.EmitLdvectmp2();
  758. context.EmitLdc_I4(3 << 6 | 1 << 4 | 3 << 2 | 1 << 0);
  759. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.Shuffle), typesSfl));
  760. context.EmitCall(typeof(Sse).GetMethod(name, types));
  761. context.EmitStvec(op.Rd);
  762. }
  763. }
  764. else /* if (sizeF == 1) */
  765. {
  766. Type[] types = new Type[] { typeof(Vector128<double>), typeof(Vector128<double>) };
  767. EmitLdvecWithCastToDouble(context, op.Rn);
  768. context.Emit(OpCodes.Dup);
  769. context.EmitStvectmp();
  770. EmitLdvecWithCastToDouble(context, op.Rm);
  771. context.Emit(OpCodes.Dup);
  772. context.EmitStvectmp2();
  773. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.UnpackLow), types));
  774. context.EmitLdvectmp();
  775. context.EmitLdvectmp2();
  776. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.UnpackHigh), types));
  777. context.EmitCall(typeof(Sse2).GetMethod(name, types));
  778. EmitStvecWithCastFromDouble(context, op.Rd);
  779. }
  780. }
  781. [Flags]
  782. public enum SaturatingFlags
  783. {
  784. Scalar = 1 << 0,
  785. Signed = 1 << 1,
  786. Add = 1 << 2,
  787. Sub = 1 << 3,
  788. Accumulate = 1 << 4,
  789. ScalarSx = Scalar | Signed,
  790. ScalarZx = Scalar,
  791. VectorSx = Signed,
  792. VectorZx = 0
  793. }
  794. public static void EmitScalarSaturatingUnaryOpSx(ILEmitterCtx context, Action emit)
  795. {
  796. EmitSaturatingUnaryOpSx(context, emit, SaturatingFlags.ScalarSx);
  797. }
  798. public static void EmitVectorSaturatingUnaryOpSx(ILEmitterCtx context, Action emit)
  799. {
  800. EmitSaturatingUnaryOpSx(context, emit, SaturatingFlags.VectorSx);
  801. }
  802. public static void EmitSaturatingUnaryOpSx(ILEmitterCtx context, Action emit, SaturatingFlags flags)
  803. {
  804. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  805. bool scalar = (flags & SaturatingFlags.Scalar) != 0;
  806. int bytes = op.GetBitsCount() >> 3;
  807. int elems = !scalar ? bytes >> op.Size : 1;
  808. if (scalar)
  809. {
  810. EmitVectorZeroLowerTmp(context);
  811. }
  812. for (int index = 0; index < elems; index++)
  813. {
  814. EmitVectorExtractSx(context, op.Rn, index, op.Size);
  815. emit();
  816. if (op.Size <= 2)
  817. {
  818. EmitSatQ(context, op.Size, true, true);
  819. }
  820. else /* if (op.Size == 3) */
  821. {
  822. EmitUnarySignedSatQAbsOrNeg(context);
  823. }
  824. EmitVectorInsertTmp(context, index, op.Size);
  825. }
  826. context.EmitLdvectmp();
  827. context.EmitStvec(op.Rd);
  828. if ((op.RegisterSize == RegisterSize.Simd64) || scalar)
  829. {
  830. EmitVectorZeroUpper(context, op.Rd);
  831. }
  832. }
  833. public static void EmitScalarSaturatingBinaryOpSx(ILEmitterCtx context, SaturatingFlags flags)
  834. {
  835. EmitSaturatingBinaryOp(context, () => { }, SaturatingFlags.ScalarSx | flags);
  836. }
  837. public static void EmitScalarSaturatingBinaryOpZx(ILEmitterCtx context, SaturatingFlags flags)
  838. {
  839. EmitSaturatingBinaryOp(context, () => { }, SaturatingFlags.ScalarZx | flags);
  840. }
  841. public static void EmitVectorSaturatingBinaryOpSx(ILEmitterCtx context, SaturatingFlags flags)
  842. {
  843. EmitSaturatingBinaryOp(context, () => { }, SaturatingFlags.VectorSx | flags);
  844. }
  845. public static void EmitVectorSaturatingBinaryOpZx(ILEmitterCtx context, SaturatingFlags flags)
  846. {
  847. EmitSaturatingBinaryOp(context, () => { }, SaturatingFlags.VectorZx | flags);
  848. }
  849. public static void EmitSaturatingBinaryOp(ILEmitterCtx context, Action emit, SaturatingFlags flags)
  850. {
  851. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  852. bool scalar = (flags & SaturatingFlags.Scalar) != 0;
  853. bool signed = (flags & SaturatingFlags.Signed) != 0;
  854. bool add = (flags & SaturatingFlags.Add) != 0;
  855. bool sub = (flags & SaturatingFlags.Sub) != 0;
  856. bool accumulate = (flags & SaturatingFlags.Accumulate) != 0;
  857. int bytes = op.GetBitsCount() >> 3;
  858. int elems = !scalar ? bytes >> op.Size : 1;
  859. if (scalar)
  860. {
  861. EmitVectorZeroLowerTmp(context);
  862. }
  863. if (add || sub)
  864. {
  865. for (int index = 0; index < elems; index++)
  866. {
  867. EmitVectorExtract(context, op.Rn, index, op.Size, signed);
  868. EmitVectorExtract(context, ((OpCodeSimdReg64)op).Rm, index, op.Size, signed);
  869. if (op.Size <= 2)
  870. {
  871. context.Emit(add ? OpCodes.Add : OpCodes.Sub);
  872. EmitSatQ(context, op.Size, true, signed);
  873. }
  874. else /* if (op.Size == 3) */
  875. {
  876. if (add)
  877. {
  878. EmitBinarySatQAdd(context, signed);
  879. }
  880. else /* if (sub) */
  881. {
  882. EmitBinarySatQSub(context, signed);
  883. }
  884. }
  885. EmitVectorInsertTmp(context, index, op.Size);
  886. }
  887. }
  888. else if (accumulate)
  889. {
  890. for (int index = 0; index < elems; index++)
  891. {
  892. EmitVectorExtract(context, op.Rn, index, op.Size, !signed);
  893. EmitVectorExtract(context, op.Rd, index, op.Size, signed);
  894. if (op.Size <= 2)
  895. {
  896. context.Emit(OpCodes.Add);
  897. EmitSatQ(context, op.Size, true, signed);
  898. }
  899. else /* if (op.Size == 3) */
  900. {
  901. EmitBinarySatQAccumulate(context, signed);
  902. }
  903. EmitVectorInsertTmp(context, index, op.Size);
  904. }
  905. }
  906. else
  907. {
  908. for (int index = 0; index < elems; index++)
  909. {
  910. EmitVectorExtract(context, op.Rn, index, op.Size, signed);
  911. EmitVectorExtract(context, ((OpCodeSimdReg64)op).Rm, index, op.Size, signed);
  912. emit();
  913. EmitSatQ(context, op.Size, true, signed);
  914. EmitVectorInsertTmp(context, index, op.Size);
  915. }
  916. }
  917. context.EmitLdvectmp();
  918. context.EmitStvec(op.Rd);
  919. if ((op.RegisterSize == RegisterSize.Simd64) || scalar)
  920. {
  921. EmitVectorZeroUpper(context, op.Rd);
  922. }
  923. }
  924. [Flags]
  925. public enum SaturatingNarrowFlags
  926. {
  927. Scalar = 1 << 0,
  928. SignedSrc = 1 << 1,
  929. SignedDst = 1 << 2,
  930. ScalarSxSx = Scalar | SignedSrc | SignedDst,
  931. ScalarSxZx = Scalar | SignedSrc,
  932. ScalarZxZx = Scalar,
  933. VectorSxSx = SignedSrc | SignedDst,
  934. VectorSxZx = SignedSrc,
  935. VectorZxZx = 0
  936. }
  937. public static void EmitSaturatingNarrowOp(ILEmitterCtx context, SaturatingNarrowFlags flags)
  938. {
  939. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  940. bool scalar = (flags & SaturatingNarrowFlags.Scalar) != 0;
  941. bool signedSrc = (flags & SaturatingNarrowFlags.SignedSrc) != 0;
  942. bool signedDst = (flags & SaturatingNarrowFlags.SignedDst) != 0;
  943. int elems = !scalar ? 8 >> op.Size : 1;
  944. int part = !scalar && (op.RegisterSize == RegisterSize.Simd128) ? elems : 0;
  945. if (scalar)
  946. {
  947. EmitVectorZeroLowerTmp(context);
  948. }
  949. if (part != 0)
  950. {
  951. context.EmitLdvec(op.Rd);
  952. context.EmitStvectmp();
  953. }
  954. for (int index = 0; index < elems; index++)
  955. {
  956. EmitVectorExtract(context, op.Rn, index, op.Size + 1, signedSrc);
  957. EmitSatQ(context, op.Size, signedSrc, signedDst);
  958. EmitVectorInsertTmp(context, part + index, op.Size);
  959. }
  960. context.EmitLdvectmp();
  961. context.EmitStvec(op.Rd);
  962. if (part == 0)
  963. {
  964. EmitVectorZeroUpper(context, op.Rd);
  965. }
  966. }
  967. // TSrc (16bit, 32bit, 64bit; signed, unsigned) > TDst (8bit, 16bit, 32bit; signed, unsigned).
  968. public static void EmitSatQ(
  969. ILEmitterCtx context,
  970. int sizeDst,
  971. bool signedSrc,
  972. bool signedDst)
  973. {
  974. if (sizeDst > 2)
  975. {
  976. throw new ArgumentOutOfRangeException(nameof(sizeDst));
  977. }
  978. context.EmitLdc_I4(sizeDst);
  979. context.EmitLdarg(TranslatedSub.StateArgIdx);
  980. if (signedSrc)
  981. {
  982. SoftFallback.EmitCall(context, signedDst
  983. ? nameof(SoftFallback.SignedSrcSignedDstSatQ)
  984. : nameof(SoftFallback.SignedSrcUnsignedDstSatQ));
  985. }
  986. else
  987. {
  988. SoftFallback.EmitCall(context, signedDst
  989. ? nameof(SoftFallback.UnsignedSrcSignedDstSatQ)
  990. : nameof(SoftFallback.UnsignedSrcUnsignedDstSatQ));
  991. }
  992. }
  993. // TSrc (64bit) == TDst (64bit); signed.
  994. public static void EmitUnarySignedSatQAbsOrNeg(ILEmitterCtx context)
  995. {
  996. if (((OpCodeSimd64)context.CurrOp).Size < 3)
  997. {
  998. throw new InvalidOperationException();
  999. }
  1000. context.EmitLdarg(TranslatedSub.StateArgIdx);
  1001. SoftFallback.EmitCall(context, nameof(SoftFallback.UnarySignedSatQAbsOrNeg));
  1002. }
  1003. // TSrcs (64bit) == TDst (64bit); signed, unsigned.
  1004. public static void EmitBinarySatQAdd(ILEmitterCtx context, bool signed)
  1005. {
  1006. if (((OpCodeSimdReg64)context.CurrOp).Size < 3)
  1007. {
  1008. throw new InvalidOperationException();
  1009. }
  1010. context.EmitLdarg(TranslatedSub.StateArgIdx);
  1011. SoftFallback.EmitCall(context, signed
  1012. ? nameof(SoftFallback.BinarySignedSatQAdd)
  1013. : nameof(SoftFallback.BinaryUnsignedSatQAdd));
  1014. }
  1015. // TSrcs (64bit) == TDst (64bit); signed, unsigned.
  1016. public static void EmitBinarySatQSub(ILEmitterCtx context, bool signed)
  1017. {
  1018. if (((OpCodeSimdReg64)context.CurrOp).Size < 3)
  1019. {
  1020. throw new InvalidOperationException();
  1021. }
  1022. context.EmitLdarg(TranslatedSub.StateArgIdx);
  1023. SoftFallback.EmitCall(context, signed
  1024. ? nameof(SoftFallback.BinarySignedSatQSub)
  1025. : nameof(SoftFallback.BinaryUnsignedSatQSub));
  1026. }
  1027. // TSrcs (64bit) == TDst (64bit); signed, unsigned.
  1028. public static void EmitBinarySatQAccumulate(ILEmitterCtx context, bool signed)
  1029. {
  1030. if (((OpCodeSimd64)context.CurrOp).Size < 3)
  1031. {
  1032. throw new InvalidOperationException();
  1033. }
  1034. context.EmitLdarg(TranslatedSub.StateArgIdx);
  1035. SoftFallback.EmitCall(context, signed
  1036. ? nameof(SoftFallback.BinarySignedSatQAcc)
  1037. : nameof(SoftFallback.BinaryUnsignedSatQAcc));
  1038. }
  1039. public static void EmitScalarSet(ILEmitterCtx context, int reg, int size)
  1040. {
  1041. EmitVectorZeroAll(context, reg);
  1042. EmitVectorInsert(context, reg, 0, size);
  1043. }
  1044. public static void EmitScalarSetF(ILEmitterCtx context, int reg, int size)
  1045. {
  1046. if (Optimizations.UseSse41 && size == 0)
  1047. {
  1048. //If the type is float, we can perform insertion and
  1049. //zero the upper bits with a single instruction (INSERTPS);
  1050. context.EmitLdvec(reg);
  1051. VectorHelper.EmitCall(context, nameof(VectorHelper.Sse41VectorInsertScalarSingle));
  1052. context.EmitStvec(reg);
  1053. }
  1054. else
  1055. {
  1056. EmitVectorZeroAll(context, reg);
  1057. EmitVectorInsertF(context, reg, 0, size);
  1058. }
  1059. }
  1060. public static void EmitVectorExtractSx(ILEmitterCtx context, int reg, int index, int size)
  1061. {
  1062. EmitVectorExtract(context, reg, index, size, true);
  1063. }
  1064. public static void EmitVectorExtractZx(ILEmitterCtx context, int reg, int index, int size)
  1065. {
  1066. EmitVectorExtract(context, reg, index, size, false);
  1067. }
  1068. public static void EmitVectorExtract(ILEmitterCtx context, int reg, int index, int size, bool signed)
  1069. {
  1070. ThrowIfInvalid(index, size);
  1071. context.EmitLdvec(reg);
  1072. context.EmitLdc_I4(index);
  1073. context.EmitLdc_I4(size);
  1074. VectorHelper.EmitCall(context, signed
  1075. ? nameof(VectorHelper.VectorExtractIntSx)
  1076. : nameof(VectorHelper.VectorExtractIntZx));
  1077. }
  1078. public static void EmitVectorExtractF(ILEmitterCtx context, int reg, int index, int size)
  1079. {
  1080. ThrowIfInvalidF(index, size);
  1081. context.EmitLdvec(reg);
  1082. context.EmitLdc_I4(index);
  1083. if (size == 0)
  1084. {
  1085. VectorHelper.EmitCall(context, nameof(VectorHelper.VectorExtractSingle));
  1086. }
  1087. else if (size == 1)
  1088. {
  1089. VectorHelper.EmitCall(context, nameof(VectorHelper.VectorExtractDouble));
  1090. }
  1091. else
  1092. {
  1093. throw new ArgumentOutOfRangeException(nameof(size));
  1094. }
  1095. }
  1096. public static void EmitVectorZeroAll(ILEmitterCtx context, int reg)
  1097. {
  1098. if (Optimizations.UseSse)
  1099. {
  1100. VectorHelper.EmitCall(context, nameof(VectorHelper.VectorSingleZero));
  1101. context.EmitStvec(reg);
  1102. }
  1103. else
  1104. {
  1105. EmitVectorZeroLower(context, reg);
  1106. EmitVectorZeroUpper(context, reg);
  1107. }
  1108. }
  1109. public static void EmitVectorZeroLower(ILEmitterCtx context, int reg)
  1110. {
  1111. EmitVectorInsert(context, reg, 0, 3, 0);
  1112. }
  1113. public static void EmitVectorZeroLowerTmp(ILEmitterCtx context)
  1114. {
  1115. if (Optimizations.UseSse)
  1116. {
  1117. context.EmitLdvectmp();
  1118. VectorHelper.EmitCall(context, nameof(VectorHelper.VectorSingleZero));
  1119. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.MoveHighToLow)));
  1120. context.EmitStvectmp();
  1121. }
  1122. else
  1123. {
  1124. EmitVectorInsertTmp(context, 0, 3, 0);
  1125. }
  1126. }
  1127. public static void EmitVectorZeroUpper(ILEmitterCtx context, int reg)
  1128. {
  1129. if (Optimizations.UseSse)
  1130. {
  1131. //TODO: Use Sse2.MoveScalar once it is fixed,
  1132. //as of the time of writing it just crashes the JIT (SDK 2.1.503).
  1133. /*Type[] typesMov = new Type[] { typeof(Vector128<ulong>) };
  1134. EmitLdvecWithUnsignedCast(context, reg, 3);
  1135. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.MoveScalar), typesMov));
  1136. EmitStvecWithUnsignedCast(context, reg, 3);*/
  1137. context.EmitLdvec(reg);
  1138. VectorHelper.EmitCall(context, nameof(VectorHelper.VectorSingleZero));
  1139. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.MoveLowToHigh)));
  1140. context.EmitStvec(reg);
  1141. }
  1142. else
  1143. {
  1144. EmitVectorInsert(context, reg, 1, 3, 0);
  1145. }
  1146. }
  1147. public static void EmitVectorZero32_128(ILEmitterCtx context, int reg)
  1148. {
  1149. if (!Sse.IsSupported)
  1150. {
  1151. throw new PlatformNotSupportedException();
  1152. }
  1153. VectorHelper.EmitCall(context, nameof(VectorHelper.VectorSingleZero));
  1154. context.EmitLdvec(reg);
  1155. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.MoveScalar)));
  1156. context.EmitStvec(reg);
  1157. }
  1158. public static void EmitVectorInsert(ILEmitterCtx context, int reg, int index, int size)
  1159. {
  1160. ThrowIfInvalid(index, size);
  1161. context.EmitLdvec(reg);
  1162. context.EmitLdc_I4(index);
  1163. context.EmitLdc_I4(size);
  1164. VectorHelper.EmitCall(context, nameof(VectorHelper.VectorInsertInt));
  1165. context.EmitStvec(reg);
  1166. }
  1167. public static void EmitVectorInsertTmp(ILEmitterCtx context, int index, int size)
  1168. {
  1169. ThrowIfInvalid(index, size);
  1170. context.EmitLdvectmp();
  1171. context.EmitLdc_I4(index);
  1172. context.EmitLdc_I4(size);
  1173. VectorHelper.EmitCall(context, nameof(VectorHelper.VectorInsertInt));
  1174. context.EmitStvectmp();
  1175. }
  1176. public static void EmitVectorInsert(ILEmitterCtx context, int reg, int index, int size, long value)
  1177. {
  1178. ThrowIfInvalid(index, size);
  1179. context.EmitLdc_I8(value);
  1180. context.EmitLdvec(reg);
  1181. context.EmitLdc_I4(index);
  1182. context.EmitLdc_I4(size);
  1183. VectorHelper.EmitCall(context, nameof(VectorHelper.VectorInsertInt));
  1184. context.EmitStvec(reg);
  1185. }
  1186. public static void EmitVectorInsertTmp(ILEmitterCtx context, int index, int size, long value)
  1187. {
  1188. ThrowIfInvalid(index, size);
  1189. context.EmitLdc_I8(value);
  1190. context.EmitLdvectmp();
  1191. context.EmitLdc_I4(index);
  1192. context.EmitLdc_I4(size);
  1193. VectorHelper.EmitCall(context, nameof(VectorHelper.VectorInsertInt));
  1194. context.EmitStvectmp();
  1195. }
  1196. public static void EmitVectorInsertF(ILEmitterCtx context, int reg, int index, int size)
  1197. {
  1198. ThrowIfInvalidF(index, size);
  1199. context.EmitLdvec(reg);
  1200. context.EmitLdc_I4(index);
  1201. if (size == 0)
  1202. {
  1203. VectorHelper.EmitCall(context, nameof(VectorHelper.VectorInsertSingle));
  1204. }
  1205. else if (size == 1)
  1206. {
  1207. VectorHelper.EmitCall(context, nameof(VectorHelper.VectorInsertDouble));
  1208. }
  1209. else
  1210. {
  1211. throw new ArgumentOutOfRangeException(nameof(size));
  1212. }
  1213. context.EmitStvec(reg);
  1214. }
  1215. public static void EmitVectorInsertTmpF(ILEmitterCtx context, int index, int size)
  1216. {
  1217. ThrowIfInvalidF(index, size);
  1218. context.EmitLdvectmp();
  1219. context.EmitLdc_I4(index);
  1220. if (size == 0)
  1221. {
  1222. VectorHelper.EmitCall(context, nameof(VectorHelper.VectorInsertSingle));
  1223. }
  1224. else if (size == 1)
  1225. {
  1226. VectorHelper.EmitCall(context, nameof(VectorHelper.VectorInsertDouble));
  1227. }
  1228. else
  1229. {
  1230. throw new ArgumentOutOfRangeException(nameof(size));
  1231. }
  1232. context.EmitStvectmp();
  1233. }
  1234. private static void ThrowIfInvalid(int index, int size)
  1235. {
  1236. if ((uint)size > 3u)
  1237. {
  1238. throw new ArgumentOutOfRangeException(nameof(size));
  1239. }
  1240. if ((uint)index >= 16u >> size)
  1241. {
  1242. throw new ArgumentOutOfRangeException(nameof(index));
  1243. }
  1244. }
  1245. private static void ThrowIfInvalidF(int index, int size)
  1246. {
  1247. if ((uint)size > 1u)
  1248. {
  1249. throw new ArgumentOutOfRangeException(nameof(size));
  1250. }
  1251. if ((uint)index >= 4u >> size)
  1252. {
  1253. throw new ArgumentOutOfRangeException(nameof(index));
  1254. }
  1255. }
  1256. }
  1257. }