InstEmitSimdShift.cs 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178
  1. // https://github.com/intel/ARM_NEON_2_x86_SSE/blob/master/NEON_2_SSE.h
  2. using ARMeilleure.Decoders;
  3. using ARMeilleure.IntermediateRepresentation;
  4. using ARMeilleure.Translation;
  5. using System;
  6. using System.Diagnostics;
  7. using System.Reflection;
  8. using static ARMeilleure.Instructions.InstEmitHelper;
  9. using static ARMeilleure.Instructions.InstEmitSimdHelper;
  10. using static ARMeilleure.IntermediateRepresentation.OperandHelper;
  11. namespace ARMeilleure.Instructions
  12. {
  13. using Func2I = Func<Operand, Operand, Operand>;
  14. static partial class InstEmit
  15. {
  16. #region "Masks"
  17. private static readonly long[] _masks_SliSri = new long[] // Replication masks.
  18. {
  19. 0x0101010101010101L, 0x0001000100010001L, 0x0000000100000001L, 0x0000000000000001L
  20. };
  21. #endregion
  22. public static void Rshrn_V(ArmEmitterContext context)
  23. {
  24. if (Optimizations.UseSsse3)
  25. {
  26. OpCodeSimdShImm op = (OpCodeSimdShImm)context.CurrOp;
  27. int shift = GetImmShr(op);
  28. long roundConst = 1L << (shift - 1);
  29. Operand d = GetVec(op.Rd);
  30. Operand n = GetVec(op.Rn);
  31. Operand dLow = context.VectorZeroUpper64(d);
  32. Operand mask = null;
  33. switch (op.Size + 1)
  34. {
  35. case 1: mask = X86GetAllElements(context, (int)roundConst * 0x00010001); break;
  36. case 2: mask = X86GetAllElements(context, (int)roundConst); break;
  37. case 3: mask = X86GetAllElements(context, roundConst); break;
  38. }
  39. Intrinsic addInst = X86PaddInstruction[op.Size + 1];
  40. Operand res = context.AddIntrinsic(addInst, n, mask);
  41. Intrinsic srlInst = X86PsrlInstruction[op.Size + 1];
  42. res = context.AddIntrinsic(srlInst, res, Const(shift));
  43. Operand mask2 = X86GetAllElements(context, EvenMasks[op.Size]);
  44. res = context.AddIntrinsic(Intrinsic.X86Pshufb, res, mask2);
  45. Intrinsic movInst = op.RegisterSize == RegisterSize.Simd128
  46. ? Intrinsic.X86Movlhps
  47. : Intrinsic.X86Movhlps;
  48. res = context.AddIntrinsic(movInst, dLow, res);
  49. context.Copy(d, res);
  50. }
  51. else
  52. {
  53. EmitVectorShrImmNarrowOpZx(context, round: true);
  54. }
  55. }
  56. public static void Shl_S(ArmEmitterContext context)
  57. {
  58. OpCodeSimdShImm op = (OpCodeSimdShImm)context.CurrOp;
  59. int shift = GetImmShl(op);
  60. EmitScalarUnaryOpZx(context, (op1) => context.ShiftLeft(op1, Const(shift)));
  61. }
  62. public static void Shl_V(ArmEmitterContext context)
  63. {
  64. OpCodeSimdShImm op = (OpCodeSimdShImm)context.CurrOp;
  65. int shift = GetImmShl(op);
  66. if (Optimizations.UseSse2 && op.Size > 0)
  67. {
  68. Operand n = GetVec(op.Rn);
  69. Intrinsic sllInst = X86PsllInstruction[op.Size];
  70. Operand res = context.AddIntrinsic(sllInst, n, Const(shift));
  71. if (op.RegisterSize == RegisterSize.Simd64)
  72. {
  73. res = context.VectorZeroUpper64(res);
  74. }
  75. context.Copy(GetVec(op.Rd), res);
  76. }
  77. else
  78. {
  79. EmitVectorUnaryOpZx(context, (op1) => context.ShiftLeft(op1, Const(shift)));
  80. }
  81. }
  82. public static void Shll_V(ArmEmitterContext context)
  83. {
  84. OpCodeSimd op = (OpCodeSimd)context.CurrOp;
  85. int shift = 8 << op.Size;
  86. if (Optimizations.UseSse41)
  87. {
  88. Operand n = GetVec(op.Rn);
  89. if (op.RegisterSize == RegisterSize.Simd128)
  90. {
  91. n = context.AddIntrinsic(Intrinsic.X86Psrldq, n, Const(8));
  92. }
  93. Intrinsic movsxInst = X86PmovsxInstruction[op.Size];
  94. Operand res = context.AddIntrinsic(movsxInst, n);
  95. Intrinsic sllInst = X86PsllInstruction[op.Size + 1];
  96. res = context.AddIntrinsic(sllInst, res, Const(shift));
  97. context.Copy(GetVec(op.Rd), res);
  98. }
  99. else
  100. {
  101. EmitVectorShImmWidenBinaryZx(context, (op1, op2) => context.ShiftLeft(op1, op2), shift);
  102. }
  103. }
  104. public static void Shrn_V(ArmEmitterContext context)
  105. {
  106. if (Optimizations.UseSsse3)
  107. {
  108. OpCodeSimdShImm op = (OpCodeSimdShImm)context.CurrOp;
  109. int shift = GetImmShr(op);
  110. Operand d = GetVec(op.Rd);
  111. Operand n = GetVec(op.Rn);
  112. Operand dLow = context.VectorZeroUpper64(d);
  113. Intrinsic srlInst = X86PsrlInstruction[op.Size + 1];
  114. Operand nShifted = context.AddIntrinsic(srlInst, n, Const(shift));
  115. Operand mask = X86GetAllElements(context, EvenMasks[op.Size]);
  116. Operand res = context.AddIntrinsic(Intrinsic.X86Pshufb, nShifted, mask);
  117. Intrinsic movInst = op.RegisterSize == RegisterSize.Simd128
  118. ? Intrinsic.X86Movlhps
  119. : Intrinsic.X86Movhlps;
  120. res = context.AddIntrinsic(movInst, dLow, res);
  121. context.Copy(d, res);
  122. }
  123. else
  124. {
  125. EmitVectorShrImmNarrowOpZx(context, round: false);
  126. }
  127. }
  128. public static void Sli_S(ArmEmitterContext context)
  129. {
  130. EmitSli(context, scalar: true);
  131. }
  132. public static void Sli_V(ArmEmitterContext context)
  133. {
  134. EmitSli(context, scalar: false);
  135. }
  136. public static void Sqrshl_V(ArmEmitterContext context)
  137. {
  138. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  139. Operand res = context.VectorZero();
  140. int elems = op.GetBytesCount() >> op.Size;
  141. for (int index = 0; index < elems; index++)
  142. {
  143. Operand ne = EmitVectorExtractSx(context, op.Rn, index, op.Size);
  144. Operand me = EmitVectorExtractSx(context, op.Rm, index, op.Size);
  145. Operand e = context.Call(typeof(SoftFallback).GetMethod(nameof(SoftFallback.SignedShlRegSatQ)), ne, me, Const(1), Const(op.Size));
  146. res = EmitVectorInsert(context, res, e, index, op.Size);
  147. }
  148. context.Copy(GetVec(op.Rd), res);
  149. }
  150. public static void Sqrshrn_S(ArmEmitterContext context)
  151. {
  152. EmitRoundShrImmSaturatingNarrowOp(context, ShrImmSaturatingNarrowFlags.ScalarSxSx);
  153. }
  154. public static void Sqrshrn_V(ArmEmitterContext context)
  155. {
  156. EmitRoundShrImmSaturatingNarrowOp(context, ShrImmSaturatingNarrowFlags.VectorSxSx);
  157. }
  158. public static void Sqrshrun_S(ArmEmitterContext context)
  159. {
  160. EmitRoundShrImmSaturatingNarrowOp(context, ShrImmSaturatingNarrowFlags.ScalarSxZx);
  161. }
  162. public static void Sqrshrun_V(ArmEmitterContext context)
  163. {
  164. EmitRoundShrImmSaturatingNarrowOp(context, ShrImmSaturatingNarrowFlags.VectorSxZx);
  165. }
  166. public static void Sqshl_V(ArmEmitterContext context)
  167. {
  168. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  169. Operand res = context.VectorZero();
  170. int elems = op.GetBytesCount() >> op.Size;
  171. for (int index = 0; index < elems; index++)
  172. {
  173. Operand ne = EmitVectorExtractSx(context, op.Rn, index, op.Size);
  174. Operand me = EmitVectorExtractSx(context, op.Rm, index, op.Size);
  175. Operand e = context.Call(typeof(SoftFallback).GetMethod(nameof(SoftFallback.SignedShlRegSatQ)), ne, me, Const(0), Const(op.Size));
  176. res = EmitVectorInsert(context, res, e, index, op.Size);
  177. }
  178. context.Copy(GetVec(op.Rd), res);
  179. }
  180. public static void Sqshrn_S(ArmEmitterContext context)
  181. {
  182. EmitShrImmSaturatingNarrowOp(context, ShrImmSaturatingNarrowFlags.ScalarSxSx);
  183. }
  184. public static void Sqshrn_V(ArmEmitterContext context)
  185. {
  186. EmitShrImmSaturatingNarrowOp(context, ShrImmSaturatingNarrowFlags.VectorSxSx);
  187. }
  188. public static void Sqshrun_S(ArmEmitterContext context)
  189. {
  190. EmitShrImmSaturatingNarrowOp(context, ShrImmSaturatingNarrowFlags.ScalarSxZx);
  191. }
  192. public static void Sqshrun_V(ArmEmitterContext context)
  193. {
  194. EmitShrImmSaturatingNarrowOp(context, ShrImmSaturatingNarrowFlags.VectorSxZx);
  195. }
  196. public static void Sri_S(ArmEmitterContext context)
  197. {
  198. EmitSri(context, scalar: true);
  199. }
  200. public static void Sri_V(ArmEmitterContext context)
  201. {
  202. EmitSri(context, scalar: false);
  203. }
  204. public static void Srshl_V(ArmEmitterContext context)
  205. {
  206. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  207. Operand res = context.VectorZero();
  208. int elems = op.GetBytesCount() >> op.Size;
  209. for (int index = 0; index < elems; index++)
  210. {
  211. Operand ne = EmitVectorExtractSx(context, op.Rn, index, op.Size);
  212. Operand me = EmitVectorExtractSx(context, op.Rm, index, op.Size);
  213. Operand e = context.Call(typeof(SoftFallback).GetMethod(nameof(SoftFallback.SignedShlReg)), ne, me, Const(1), Const(op.Size));
  214. res = EmitVectorInsert(context, res, e, index, op.Size);
  215. }
  216. context.Copy(GetVec(op.Rd), res);
  217. }
  218. public static void Srshr_S(ArmEmitterContext context)
  219. {
  220. EmitScalarShrImmOpSx(context, ShrImmFlags.Round);
  221. }
  222. public static void Srshr_V(ArmEmitterContext context)
  223. {
  224. OpCodeSimdShImm op = (OpCodeSimdShImm)context.CurrOp;
  225. if (Optimizations.UseSse2 && op.Size > 0 && op.Size < 3)
  226. {
  227. int shift = GetImmShr(op);
  228. int eSize = 8 << op.Size;
  229. Operand n = GetVec(op.Rn);
  230. Intrinsic sllInst = X86PsllInstruction[op.Size];
  231. Operand res = context.AddIntrinsic(sllInst, n, Const(eSize - shift));
  232. Intrinsic srlInst = X86PsrlInstruction[op.Size];
  233. res = context.AddIntrinsic(srlInst, res, Const(eSize - 1));
  234. Intrinsic sraInst = X86PsraInstruction[op.Size];
  235. Operand nSra = context.AddIntrinsic(sraInst, n, Const(shift));
  236. Intrinsic addInst = X86PaddInstruction[op.Size];
  237. res = context.AddIntrinsic(addInst, res, nSra);
  238. if (op.RegisterSize == RegisterSize.Simd64)
  239. {
  240. res = context.VectorZeroUpper64(res);
  241. }
  242. context.Copy(GetVec(op.Rd), res);
  243. }
  244. else
  245. {
  246. EmitVectorShrImmOpSx(context, ShrImmFlags.Round);
  247. }
  248. }
  249. public static void Srsra_S(ArmEmitterContext context)
  250. {
  251. EmitScalarShrImmOpSx(context, ShrImmFlags.Round | ShrImmFlags.Accumulate);
  252. }
  253. public static void Srsra_V(ArmEmitterContext context)
  254. {
  255. OpCodeSimdShImm op = (OpCodeSimdShImm)context.CurrOp;
  256. if (Optimizations.UseSse2 && op.Size > 0 && op.Size < 3)
  257. {
  258. int shift = GetImmShr(op);
  259. int eSize = 8 << op.Size;
  260. Operand d = GetVec(op.Rd);
  261. Operand n = GetVec(op.Rn);
  262. Intrinsic sllInst = X86PsllInstruction[op.Size];
  263. Operand res = context.AddIntrinsic(sllInst, n, Const(eSize - shift));
  264. Intrinsic srlInst = X86PsrlInstruction[op.Size];
  265. res = context.AddIntrinsic(srlInst, res, Const(eSize - 1));
  266. Intrinsic sraInst = X86PsraInstruction[op.Size];
  267. Operand nSra = context.AddIntrinsic(sraInst, n, Const(shift));
  268. Intrinsic addInst = X86PaddInstruction[op.Size];
  269. res = context.AddIntrinsic(addInst, res, nSra);
  270. res = context.AddIntrinsic(addInst, res, d);
  271. if (op.RegisterSize == RegisterSize.Simd64)
  272. {
  273. res = context.VectorZeroUpper64(res);
  274. }
  275. context.Copy(d, res);
  276. }
  277. else
  278. {
  279. EmitVectorShrImmOpSx(context, ShrImmFlags.Round | ShrImmFlags.Accumulate);
  280. }
  281. }
  282. public static void Sshl_V(ArmEmitterContext context)
  283. {
  284. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  285. Operand res = context.VectorZero();
  286. int elems = op.GetBytesCount() >> op.Size;
  287. for (int index = 0; index < elems; index++)
  288. {
  289. Operand ne = EmitVectorExtractSx(context, op.Rn, index, op.Size);
  290. Operand me = EmitVectorExtractSx(context, op.Rm, index, op.Size);
  291. Operand e = context.Call(typeof(SoftFallback).GetMethod(nameof(SoftFallback.SignedShlReg)), ne, me, Const(0), Const(op.Size));
  292. res = EmitVectorInsert(context, res, e, index, op.Size);
  293. }
  294. context.Copy(GetVec(op.Rd), res);
  295. }
  296. public static void Sshll_V(ArmEmitterContext context)
  297. {
  298. OpCodeSimdShImm op = (OpCodeSimdShImm)context.CurrOp;
  299. int shift = GetImmShl(op);
  300. if (Optimizations.UseSse41)
  301. {
  302. Operand n = GetVec(op.Rn);
  303. if (op.RegisterSize == RegisterSize.Simd128)
  304. {
  305. n = context.AddIntrinsic(Intrinsic.X86Psrldq, n, Const(8));
  306. }
  307. Intrinsic movsxInst = X86PmovsxInstruction[op.Size];
  308. Operand res = context.AddIntrinsic(movsxInst, n);
  309. if (shift != 0)
  310. {
  311. Intrinsic sllInst = X86PsllInstruction[op.Size + 1];
  312. res = context.AddIntrinsic(sllInst, res, Const(shift));
  313. }
  314. context.Copy(GetVec(op.Rd), res);
  315. }
  316. else
  317. {
  318. EmitVectorShImmWidenBinarySx(context, (op1, op2) => context.ShiftLeft(op1, op2), shift);
  319. }
  320. }
  321. public static void Sshr_S(ArmEmitterContext context)
  322. {
  323. EmitShrImmOp(context, ShrImmFlags.ScalarSx);
  324. }
  325. public static void Sshr_V(ArmEmitterContext context)
  326. {
  327. OpCodeSimdShImm op = (OpCodeSimdShImm)context.CurrOp;
  328. if (Optimizations.UseSse2 && op.Size > 0 && op.Size < 3)
  329. {
  330. int shift = GetImmShr(op);
  331. Operand n = GetVec(op.Rn);
  332. Intrinsic sraInst = X86PsraInstruction[op.Size];
  333. Operand res = context.AddIntrinsic(sraInst, n, Const(shift));
  334. if (op.RegisterSize == RegisterSize.Simd64)
  335. {
  336. res = context.VectorZeroUpper64(res);
  337. }
  338. context.Copy(GetVec(op.Rd), res);
  339. }
  340. else
  341. {
  342. EmitShrImmOp(context, ShrImmFlags.VectorSx);
  343. }
  344. }
  345. public static void Ssra_S(ArmEmitterContext context)
  346. {
  347. EmitScalarShrImmOpSx(context, ShrImmFlags.Accumulate);
  348. }
  349. public static void Ssra_V(ArmEmitterContext context)
  350. {
  351. OpCodeSimdShImm op = (OpCodeSimdShImm)context.CurrOp;
  352. if (Optimizations.UseSse2 && op.Size > 0 && op.Size < 3)
  353. {
  354. int shift = GetImmShr(op);
  355. Operand d = GetVec(op.Rd);
  356. Operand n = GetVec(op.Rn);
  357. Intrinsic sraInst = X86PsraInstruction[op.Size];
  358. Operand res = context.AddIntrinsic(sraInst, n, Const(shift));
  359. Intrinsic addInst = X86PaddInstruction[op.Size];
  360. res = context.AddIntrinsic(addInst, res, d);
  361. if (op.RegisterSize == RegisterSize.Simd64)
  362. {
  363. res = context.VectorZeroUpper64(res);
  364. }
  365. context.Copy(d, res);
  366. }
  367. else
  368. {
  369. EmitVectorShrImmOpSx(context, ShrImmFlags.Accumulate);
  370. }
  371. }
  372. public static void Uqrshl_V(ArmEmitterContext context)
  373. {
  374. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  375. Operand res = context.VectorZero();
  376. int elems = op.GetBytesCount() >> op.Size;
  377. for (int index = 0; index < elems; index++)
  378. {
  379. Operand ne = EmitVectorExtractZx(context, op.Rn, index, op.Size);
  380. Operand me = EmitVectorExtractZx(context, op.Rm, index, op.Size);
  381. Operand e = context.Call(typeof(SoftFallback).GetMethod(nameof(SoftFallback.UnsignedShlRegSatQ)), ne, me, Const(1), Const(op.Size));
  382. res = EmitVectorInsert(context, res, e, index, op.Size);
  383. }
  384. context.Copy(GetVec(op.Rd), res);
  385. }
  386. public static void Uqrshrn_S(ArmEmitterContext context)
  387. {
  388. EmitRoundShrImmSaturatingNarrowOp(context, ShrImmSaturatingNarrowFlags.ScalarZxZx);
  389. }
  390. public static void Uqrshrn_V(ArmEmitterContext context)
  391. {
  392. EmitRoundShrImmSaturatingNarrowOp(context, ShrImmSaturatingNarrowFlags.VectorZxZx);
  393. }
  394. public static void Uqshl_V(ArmEmitterContext context)
  395. {
  396. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  397. Operand res = context.VectorZero();
  398. int elems = op.GetBytesCount() >> op.Size;
  399. for (int index = 0; index < elems; index++)
  400. {
  401. Operand ne = EmitVectorExtractZx(context, op.Rn, index, op.Size);
  402. Operand me = EmitVectorExtractZx(context, op.Rm, index, op.Size);
  403. Operand e = context.Call(typeof(SoftFallback).GetMethod(nameof(SoftFallback.UnsignedShlRegSatQ)), ne, me, Const(0), Const(op.Size));
  404. res = EmitVectorInsert(context, res, e, index, op.Size);
  405. }
  406. context.Copy(GetVec(op.Rd), res);
  407. }
  408. public static void Uqshrn_S(ArmEmitterContext context)
  409. {
  410. EmitShrImmSaturatingNarrowOp(context, ShrImmSaturatingNarrowFlags.ScalarZxZx);
  411. }
  412. public static void Uqshrn_V(ArmEmitterContext context)
  413. {
  414. EmitShrImmSaturatingNarrowOp(context, ShrImmSaturatingNarrowFlags.VectorZxZx);
  415. }
  416. public static void Urshl_V(ArmEmitterContext context)
  417. {
  418. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  419. Operand res = context.VectorZero();
  420. int elems = op.GetBytesCount() >> op.Size;
  421. for (int index = 0; index < elems; index++)
  422. {
  423. Operand ne = EmitVectorExtractZx(context, op.Rn, index, op.Size);
  424. Operand me = EmitVectorExtractZx(context, op.Rm, index, op.Size);
  425. Operand e = context.Call(typeof(SoftFallback).GetMethod(nameof(SoftFallback.UnsignedShlReg)), ne, me, Const(1), Const(op.Size));
  426. res = EmitVectorInsert(context, res, e, index, op.Size);
  427. }
  428. context.Copy(GetVec(op.Rd), res);
  429. }
  430. public static void Urshr_S(ArmEmitterContext context)
  431. {
  432. EmitScalarShrImmOpZx(context, ShrImmFlags.Round);
  433. }
  434. public static void Urshr_V(ArmEmitterContext context)
  435. {
  436. OpCodeSimdShImm op = (OpCodeSimdShImm)context.CurrOp;
  437. if (Optimizations.UseSse2 && op.Size > 0)
  438. {
  439. int shift = GetImmShr(op);
  440. int eSize = 8 << op.Size;
  441. Operand n = GetVec(op.Rn);
  442. Intrinsic sllInst = X86PsllInstruction[op.Size];
  443. Operand res = context.AddIntrinsic(sllInst, n, Const(eSize - shift));
  444. Intrinsic srlInst = X86PsrlInstruction[op.Size];
  445. res = context.AddIntrinsic(srlInst, res, Const(eSize - 1));
  446. Operand nSrl = context.AddIntrinsic(srlInst, n, Const(shift));
  447. Intrinsic addInst = X86PaddInstruction[op.Size];
  448. res = context.AddIntrinsic(addInst, res, nSrl);
  449. if (op.RegisterSize == RegisterSize.Simd64)
  450. {
  451. res = context.VectorZeroUpper64(res);
  452. }
  453. context.Copy(GetVec(op.Rd), res);
  454. }
  455. else
  456. {
  457. EmitVectorShrImmOpZx(context, ShrImmFlags.Round);
  458. }
  459. }
  460. public static void Ursra_S(ArmEmitterContext context)
  461. {
  462. EmitScalarShrImmOpZx(context, ShrImmFlags.Round | ShrImmFlags.Accumulate);
  463. }
  464. public static void Ursra_V(ArmEmitterContext context)
  465. {
  466. OpCodeSimdShImm op = (OpCodeSimdShImm)context.CurrOp;
  467. if (Optimizations.UseSse2 && op.Size > 0)
  468. {
  469. int shift = GetImmShr(op);
  470. int eSize = 8 << op.Size;
  471. Operand d = GetVec(op.Rd);
  472. Operand n = GetVec(op.Rn);
  473. Intrinsic sllInst = X86PsllInstruction[op.Size];
  474. Operand res = context.AddIntrinsic(sllInst, n, Const(eSize - shift));
  475. Intrinsic srlInst = X86PsrlInstruction[op.Size];
  476. res = context.AddIntrinsic(srlInst, res, Const(eSize - 1));
  477. Operand nSrl = context.AddIntrinsic(srlInst, n, Const(shift));
  478. Intrinsic addInst = X86PaddInstruction[op.Size];
  479. res = context.AddIntrinsic(addInst, res, nSrl);
  480. res = context.AddIntrinsic(addInst, res, d);
  481. if (op.RegisterSize == RegisterSize.Simd64)
  482. {
  483. res = context.VectorZeroUpper64(res);
  484. }
  485. context.Copy(d, res);
  486. }
  487. else
  488. {
  489. EmitVectorShrImmOpZx(context, ShrImmFlags.Round | ShrImmFlags.Accumulate);
  490. }
  491. }
  492. public static void Ushl_V(ArmEmitterContext context)
  493. {
  494. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  495. Operand res = context.VectorZero();
  496. int elems = op.GetBytesCount() >> op.Size;
  497. for (int index = 0; index < elems; index++)
  498. {
  499. Operand ne = EmitVectorExtractZx(context, op.Rn, index, op.Size);
  500. Operand me = EmitVectorExtractSx(context, op.Rm, index << op.Size, 0);
  501. Operand e = EmitUnsignedShlRegOp(context, ne, context.ConvertI64ToI32(me), op.Size);
  502. res = EmitVectorInsert(context, res, e, index, op.Size);
  503. }
  504. context.Copy(GetVec(op.Rd), res);
  505. }
  506. public static void Ushll_V(ArmEmitterContext context)
  507. {
  508. OpCodeSimdShImm op = (OpCodeSimdShImm)context.CurrOp;
  509. int shift = GetImmShl(op);
  510. if (Optimizations.UseSse41)
  511. {
  512. Operand n = GetVec(op.Rn);
  513. if (op.RegisterSize == RegisterSize.Simd128)
  514. {
  515. n = context.AddIntrinsic(Intrinsic.X86Psrldq, n, Const(8));
  516. }
  517. Intrinsic movzxInst = X86PmovzxInstruction[op.Size];
  518. Operand res = context.AddIntrinsic(movzxInst, n);
  519. if (shift != 0)
  520. {
  521. Intrinsic sllInst = X86PsllInstruction[op.Size + 1];
  522. res = context.AddIntrinsic(sllInst, res, Const(shift));
  523. }
  524. context.Copy(GetVec(op.Rd), res);
  525. }
  526. else
  527. {
  528. EmitVectorShImmWidenBinaryZx(context, (op1, op2) => context.ShiftLeft(op1, op2), shift);
  529. }
  530. }
  531. public static void Ushr_S(ArmEmitterContext context)
  532. {
  533. EmitShrImmOp(context, ShrImmFlags.ScalarZx);
  534. }
  535. public static void Ushr_V(ArmEmitterContext context)
  536. {
  537. OpCodeSimdShImm op = (OpCodeSimdShImm)context.CurrOp;
  538. if (Optimizations.UseSse2 && op.Size > 0)
  539. {
  540. int shift = GetImmShr(op);
  541. Operand n = GetVec(op.Rn);
  542. Intrinsic srlInst = X86PsrlInstruction[op.Size];
  543. Operand res = context.AddIntrinsic(srlInst, n, Const(shift));
  544. if (op.RegisterSize == RegisterSize.Simd64)
  545. {
  546. res = context.VectorZeroUpper64(res);
  547. }
  548. context.Copy(GetVec(op.Rd), res);
  549. }
  550. else
  551. {
  552. EmitShrImmOp(context, ShrImmFlags.VectorZx);
  553. }
  554. }
  555. public static void Usra_S(ArmEmitterContext context)
  556. {
  557. EmitScalarShrImmOpZx(context, ShrImmFlags.Accumulate);
  558. }
  559. public static void Usra_V(ArmEmitterContext context)
  560. {
  561. OpCodeSimdShImm op = (OpCodeSimdShImm)context.CurrOp;
  562. if (Optimizations.UseSse2 && op.Size > 0)
  563. {
  564. int shift = GetImmShr(op);
  565. Operand d = GetVec(op.Rd);
  566. Operand n = GetVec(op.Rn);
  567. Intrinsic srlInst = X86PsrlInstruction[op.Size];
  568. Operand res = context.AddIntrinsic(srlInst, n, Const(shift));
  569. Intrinsic addInst = X86PaddInstruction[op.Size];
  570. res = context.AddIntrinsic(addInst, res, d);
  571. if (op.RegisterSize == RegisterSize.Simd64)
  572. {
  573. res = context.VectorZeroUpper64(res);
  574. }
  575. context.Copy(d, res);
  576. }
  577. else
  578. {
  579. EmitVectorShrImmOpZx(context, ShrImmFlags.Accumulate);
  580. }
  581. }
  582. [Flags]
  583. private enum ShrImmFlags
  584. {
  585. Scalar = 1 << 0,
  586. Signed = 1 << 1,
  587. Round = 1 << 2,
  588. Accumulate = 1 << 3,
  589. ScalarSx = Scalar | Signed,
  590. ScalarZx = Scalar,
  591. VectorSx = Signed,
  592. VectorZx = 0
  593. }
  594. private static void EmitScalarShrImmOpSx(ArmEmitterContext context, ShrImmFlags flags)
  595. {
  596. EmitShrImmOp(context, ShrImmFlags.ScalarSx | flags);
  597. }
  598. private static void EmitScalarShrImmOpZx(ArmEmitterContext context, ShrImmFlags flags)
  599. {
  600. EmitShrImmOp(context, ShrImmFlags.ScalarZx | flags);
  601. }
  602. private static void EmitVectorShrImmOpSx(ArmEmitterContext context, ShrImmFlags flags)
  603. {
  604. EmitShrImmOp(context, ShrImmFlags.VectorSx | flags);
  605. }
  606. private static void EmitVectorShrImmOpZx(ArmEmitterContext context, ShrImmFlags flags)
  607. {
  608. EmitShrImmOp(context, ShrImmFlags.VectorZx | flags);
  609. }
  610. private static void EmitShrImmOp(ArmEmitterContext context, ShrImmFlags flags)
  611. {
  612. OpCodeSimdShImm op = (OpCodeSimdShImm)context.CurrOp;
  613. Operand res = context.VectorZero();
  614. bool scalar = (flags & ShrImmFlags.Scalar) != 0;
  615. bool signed = (flags & ShrImmFlags.Signed) != 0;
  616. bool round = (flags & ShrImmFlags.Round) != 0;
  617. bool accumulate = (flags & ShrImmFlags.Accumulate) != 0;
  618. int shift = GetImmShr(op);
  619. long roundConst = 1L << (shift - 1);
  620. int elems = !scalar ? op.GetBytesCount() >> op.Size : 1;
  621. for (int index = 0; index < elems; index++)
  622. {
  623. Operand e = EmitVectorExtract(context, op.Rn, index, op.Size, signed);
  624. if (op.Size <= 2)
  625. {
  626. if (round)
  627. {
  628. e = context.Add(e, Const(roundConst));
  629. }
  630. e = signed ? context.ShiftRightSI(e, Const(shift)) : context.ShiftRightUI(e, Const(shift));
  631. }
  632. else /* if (op.Size == 3) */
  633. {
  634. e = EmitShrImm64(context, e, signed, round ? roundConst : 0L, shift);
  635. }
  636. if (accumulate)
  637. {
  638. Operand de = EmitVectorExtract(context, op.Rd, index, op.Size, signed);
  639. e = context.Add(e, de);
  640. }
  641. res = EmitVectorInsert(context, res, e, index, op.Size);
  642. }
  643. context.Copy(GetVec(op.Rd), res);
  644. }
  645. private static Operand EmitUnsignedShlRegOp(ArmEmitterContext context, Operand op, Operand shiftLsB, int size)
  646. {
  647. Debug.Assert(op.Type == OperandType.I64);
  648. Debug.Assert(shiftLsB.Type == OperandType.I32);
  649. Debug.Assert((uint)size < 4u);
  650. Operand negShiftLsB = context.Negate(shiftLsB);
  651. Operand isPositive = context.ICompareGreaterOrEqual(shiftLsB, Const(0));
  652. Operand shl = context.ShiftLeft (op, shiftLsB);
  653. Operand shr = context.ShiftRightUI(op, negShiftLsB);
  654. Operand res = context.ConditionalSelect(isPositive, shl, shr);
  655. Operand isOutOfRange = context.BitwiseOr(
  656. context.ICompareGreaterOrEqual(shiftLsB, Const(8 << size)),
  657. context.ICompareGreaterOrEqual(negShiftLsB, Const(8 << size)));
  658. return context.ConditionalSelect(isOutOfRange, Const(0UL), res);
  659. }
  660. private static void EmitVectorShrImmNarrowOpZx(ArmEmitterContext context, bool round)
  661. {
  662. OpCodeSimdShImm op = (OpCodeSimdShImm)context.CurrOp;
  663. int shift = GetImmShr(op);
  664. long roundConst = 1L << (shift - 1);
  665. int elems = 8 >> op.Size;
  666. int part = op.RegisterSize == RegisterSize.Simd128 ? elems : 0;
  667. Operand d = GetVec(op.Rd);
  668. Operand res = part == 0 ? context.VectorZero() : context.Copy(d);
  669. for (int index = 0; index < elems; index++)
  670. {
  671. Operand e = EmitVectorExtractZx(context, op.Rn, index, op.Size + 1);
  672. if (round)
  673. {
  674. e = context.Add(e, Const(roundConst));
  675. }
  676. e = context.ShiftRightUI(e, Const(shift));
  677. res = EmitVectorInsert(context, res, e, part + index, op.Size);
  678. }
  679. context.Copy(d, res);
  680. }
  681. [Flags]
  682. private enum ShrImmSaturatingNarrowFlags
  683. {
  684. Scalar = 1 << 0,
  685. SignedSrc = 1 << 1,
  686. SignedDst = 1 << 2,
  687. Round = 1 << 3,
  688. ScalarSxSx = Scalar | SignedSrc | SignedDst,
  689. ScalarSxZx = Scalar | SignedSrc,
  690. ScalarZxZx = Scalar,
  691. VectorSxSx = SignedSrc | SignedDst,
  692. VectorSxZx = SignedSrc,
  693. VectorZxZx = 0
  694. }
  695. private static void EmitRoundShrImmSaturatingNarrowOp(ArmEmitterContext context, ShrImmSaturatingNarrowFlags flags)
  696. {
  697. EmitShrImmSaturatingNarrowOp(context, ShrImmSaturatingNarrowFlags.Round | flags);
  698. }
  699. private static void EmitShrImmSaturatingNarrowOp(ArmEmitterContext context, ShrImmSaturatingNarrowFlags flags)
  700. {
  701. OpCodeSimdShImm op = (OpCodeSimdShImm)context.CurrOp;
  702. bool scalar = (flags & ShrImmSaturatingNarrowFlags.Scalar) != 0;
  703. bool signedSrc = (flags & ShrImmSaturatingNarrowFlags.SignedSrc) != 0;
  704. bool signedDst = (flags & ShrImmSaturatingNarrowFlags.SignedDst) != 0;
  705. bool round = (flags & ShrImmSaturatingNarrowFlags.Round) != 0;
  706. int shift = GetImmShr(op);
  707. long roundConst = 1L << (shift - 1);
  708. int elems = !scalar ? 8 >> op.Size : 1;
  709. int part = !scalar && (op.RegisterSize == RegisterSize.Simd128) ? elems : 0;
  710. Operand d = GetVec(op.Rd);
  711. Operand res = part == 0 ? context.VectorZero() : context.Copy(d);
  712. for (int index = 0; index < elems; index++)
  713. {
  714. Operand e = EmitVectorExtract(context, op.Rn, index, op.Size + 1, signedSrc);
  715. if (op.Size <= 1 || !round)
  716. {
  717. if (round)
  718. {
  719. e = context.Add(e, Const(roundConst));
  720. }
  721. e = signedSrc ? context.ShiftRightSI(e, Const(shift)) : context.ShiftRightUI(e, Const(shift));
  722. }
  723. else /* if (op.Size == 2 && round) */
  724. {
  725. e = EmitShrImm64(context, e, signedSrc, roundConst, shift); // shift <= 32
  726. }
  727. e = EmitSatQ(context, e, op.Size, signedSrc, signedDst);
  728. res = EmitVectorInsert(context, res, e, part + index, op.Size);
  729. }
  730. context.Copy(d, res);
  731. }
  732. // dst64 = (Int(src64, signed) + roundConst) >> shift;
  733. private static Operand EmitShrImm64(
  734. ArmEmitterContext context,
  735. Operand value,
  736. bool signed,
  737. long roundConst,
  738. int shift)
  739. {
  740. MethodInfo info = signed
  741. ? typeof(SoftFallback).GetMethod(nameof(SoftFallback.SignedShrImm64))
  742. : typeof(SoftFallback).GetMethod(nameof(SoftFallback.UnsignedShrImm64));
  743. return context.Call(info, value, Const(roundConst), Const(shift));
  744. }
  745. private static void EmitVectorShImmWidenBinarySx(ArmEmitterContext context, Func2I emit, int imm)
  746. {
  747. EmitVectorShImmWidenBinaryOp(context, emit, imm, signed: true);
  748. }
  749. private static void EmitVectorShImmWidenBinaryZx(ArmEmitterContext context, Func2I emit, int imm)
  750. {
  751. EmitVectorShImmWidenBinaryOp(context, emit, imm, signed: false);
  752. }
  753. private static void EmitVectorShImmWidenBinaryOp(ArmEmitterContext context, Func2I emit, int imm, bool signed)
  754. {
  755. OpCodeSimd op = (OpCodeSimd)context.CurrOp;
  756. Operand res = context.VectorZero();
  757. int elems = 8 >> op.Size;
  758. int part = op.RegisterSize == RegisterSize.Simd128 ? elems : 0;
  759. for (int index = 0; index < elems; index++)
  760. {
  761. Operand ne = EmitVectorExtract(context, op.Rn, part + index, op.Size, signed);
  762. res = EmitVectorInsert(context, res, emit(ne, Const(imm)), index, op.Size + 1);
  763. }
  764. context.Copy(GetVec(op.Rd), res);
  765. }
  766. private static void EmitSli(ArmEmitterContext context, bool scalar)
  767. {
  768. OpCodeSimdShImm op = (OpCodeSimdShImm)context.CurrOp;
  769. int shift = GetImmShl(op);
  770. ulong mask = shift != 0 ? ulong.MaxValue >> (64 - shift) : 0UL;
  771. if (Optimizations.UseSse2 && op.Size > 0)
  772. {
  773. Operand d = GetVec(op.Rd);
  774. Operand n = GetVec(op.Rn);
  775. Intrinsic sllInst = X86PsllInstruction[op.Size];
  776. Operand nShifted = context.AddIntrinsic(sllInst, n, Const(shift));
  777. Operand dMask = X86GetAllElements(context, (long)mask * _masks_SliSri[op.Size]);
  778. Operand dMasked = context.AddIntrinsic(Intrinsic.X86Pand, d, dMask);
  779. Operand res = context.AddIntrinsic(Intrinsic.X86Por, nShifted, dMasked);
  780. if ((op.RegisterSize == RegisterSize.Simd64) || scalar)
  781. {
  782. res = context.VectorZeroUpper64(res);
  783. }
  784. context.Copy(d, res);
  785. }
  786. else
  787. {
  788. Operand res = context.VectorZero();
  789. int elems = !scalar ? op.GetBytesCount() >> op.Size : 1;
  790. for (int index = 0; index < elems; index++)
  791. {
  792. Operand ne = EmitVectorExtractZx(context, op.Rn, index, op.Size);
  793. Operand neShifted = context.ShiftLeft(ne, Const(shift));
  794. Operand de = EmitVectorExtractZx(context, op.Rd, index, op.Size);
  795. Operand deMasked = context.BitwiseAnd(de, Const(mask));
  796. Operand e = context.BitwiseOr(neShifted, deMasked);
  797. res = EmitVectorInsert(context, res, e, index, op.Size);
  798. }
  799. context.Copy(GetVec(op.Rd), res);
  800. }
  801. }
  802. private static void EmitSri(ArmEmitterContext context, bool scalar)
  803. {
  804. OpCodeSimdShImm op = (OpCodeSimdShImm)context.CurrOp;
  805. int shift = GetImmShr(op);
  806. int eSize = 8 << op.Size;
  807. ulong mask = (ulong.MaxValue << (eSize - shift)) & (ulong.MaxValue >> (64 - eSize));
  808. if (Optimizations.UseSse2 && op.Size > 0)
  809. {
  810. Operand d = GetVec(op.Rd);
  811. Operand n = GetVec(op.Rn);
  812. Intrinsic srlInst = X86PsrlInstruction[op.Size];
  813. Operand nShifted = context.AddIntrinsic(srlInst, n, Const(shift));
  814. Operand dMask = X86GetAllElements(context, (long)mask * _masks_SliSri[op.Size]);
  815. Operand dMasked = context.AddIntrinsic(Intrinsic.X86Pand, d, dMask);
  816. Operand res = context.AddIntrinsic(Intrinsic.X86Por, nShifted, dMasked);
  817. if ((op.RegisterSize == RegisterSize.Simd64) || scalar)
  818. {
  819. res = context.VectorZeroUpper64(res);
  820. }
  821. context.Copy(d, res);
  822. }
  823. else
  824. {
  825. Operand res = context.VectorZero();
  826. int elems = !scalar ? op.GetBytesCount() >> op.Size : 1;
  827. for (int index = 0; index < elems; index++)
  828. {
  829. Operand ne = EmitVectorExtractZx(context, op.Rn, index, op.Size);
  830. Operand neShifted = shift != 64 ? context.ShiftRightUI(ne, Const(shift)) : Const(0UL);
  831. Operand de = EmitVectorExtractZx(context, op.Rd, index, op.Size);
  832. Operand deMasked = context.BitwiseAnd(de, Const(mask));
  833. Operand e = context.BitwiseOr(neShifted, deMasked);
  834. res = EmitVectorInsert(context, res, e, index, op.Size);
  835. }
  836. context.Copy(GetVec(op.Rd), res);
  837. }
  838. }
  839. }
  840. }