InstEmitSimdHelper.cs 49 KB

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