InstEmitSimdShift.cs 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192
  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.Operand.Factory;
  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 = default;
  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_S(ArmEmitterContext context)
  283. {
  284. EmitSshlOrUshl(context, signed: true, scalar: true);
  285. }
  286. public static void Sshl_V(ArmEmitterContext context)
  287. {
  288. EmitSshlOrUshl(context, signed: true, scalar: false);
  289. }
  290. public static void Sshll_V(ArmEmitterContext context)
  291. {
  292. OpCodeSimdShImm op = (OpCodeSimdShImm)context.CurrOp;
  293. int shift = GetImmShl(op);
  294. if (Optimizations.UseSse41)
  295. {
  296. Operand n = GetVec(op.Rn);
  297. if (op.RegisterSize == RegisterSize.Simd128)
  298. {
  299. n = context.AddIntrinsic(Intrinsic.X86Psrldq, n, Const(8));
  300. }
  301. Intrinsic movsxInst = X86PmovsxInstruction[op.Size];
  302. Operand res = context.AddIntrinsic(movsxInst, n);
  303. if (shift != 0)
  304. {
  305. Intrinsic sllInst = X86PsllInstruction[op.Size + 1];
  306. res = context.AddIntrinsic(sllInst, res, Const(shift));
  307. }
  308. context.Copy(GetVec(op.Rd), res);
  309. }
  310. else
  311. {
  312. EmitVectorShImmWidenBinarySx(context, (op1, op2) => context.ShiftLeft(op1, op2), shift);
  313. }
  314. }
  315. public static void Sshr_S(ArmEmitterContext context)
  316. {
  317. EmitShrImmOp(context, ShrImmFlags.ScalarSx);
  318. }
  319. public static void Sshr_V(ArmEmitterContext context)
  320. {
  321. OpCodeSimdShImm op = (OpCodeSimdShImm)context.CurrOp;
  322. if (Optimizations.UseSse2 && op.Size > 0 && op.Size < 3)
  323. {
  324. int shift = GetImmShr(op);
  325. Operand n = GetVec(op.Rn);
  326. Intrinsic sraInst = X86PsraInstruction[op.Size];
  327. Operand res = context.AddIntrinsic(sraInst, n, Const(shift));
  328. if (op.RegisterSize == RegisterSize.Simd64)
  329. {
  330. res = context.VectorZeroUpper64(res);
  331. }
  332. context.Copy(GetVec(op.Rd), res);
  333. }
  334. else
  335. {
  336. EmitShrImmOp(context, ShrImmFlags.VectorSx);
  337. }
  338. }
  339. public static void Ssra_S(ArmEmitterContext context)
  340. {
  341. EmitScalarShrImmOpSx(context, ShrImmFlags.Accumulate);
  342. }
  343. public static void Ssra_V(ArmEmitterContext context)
  344. {
  345. OpCodeSimdShImm op = (OpCodeSimdShImm)context.CurrOp;
  346. if (Optimizations.UseSse2 && op.Size > 0 && op.Size < 3)
  347. {
  348. int shift = GetImmShr(op);
  349. Operand d = GetVec(op.Rd);
  350. Operand n = GetVec(op.Rn);
  351. Intrinsic sraInst = X86PsraInstruction[op.Size];
  352. Operand res = context.AddIntrinsic(sraInst, n, Const(shift));
  353. Intrinsic addInst = X86PaddInstruction[op.Size];
  354. res = context.AddIntrinsic(addInst, res, d);
  355. if (op.RegisterSize == RegisterSize.Simd64)
  356. {
  357. res = context.VectorZeroUpper64(res);
  358. }
  359. context.Copy(d, res);
  360. }
  361. else
  362. {
  363. EmitVectorShrImmOpSx(context, ShrImmFlags.Accumulate);
  364. }
  365. }
  366. public static void Uqrshl_V(ArmEmitterContext context)
  367. {
  368. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  369. Operand res = context.VectorZero();
  370. int elems = op.GetBytesCount() >> op.Size;
  371. for (int index = 0; index < elems; index++)
  372. {
  373. Operand ne = EmitVectorExtractZx(context, op.Rn, index, op.Size);
  374. Operand me = EmitVectorExtractZx(context, op.Rm, index, op.Size);
  375. Operand e = context.Call(typeof(SoftFallback).GetMethod(nameof(SoftFallback.UnsignedShlRegSatQ)), ne, me, Const(1), Const(op.Size));
  376. res = EmitVectorInsert(context, res, e, index, op.Size);
  377. }
  378. context.Copy(GetVec(op.Rd), res);
  379. }
  380. public static void Uqrshrn_S(ArmEmitterContext context)
  381. {
  382. EmitRoundShrImmSaturatingNarrowOp(context, ShrImmSaturatingNarrowFlags.ScalarZxZx);
  383. }
  384. public static void Uqrshrn_V(ArmEmitterContext context)
  385. {
  386. EmitRoundShrImmSaturatingNarrowOp(context, ShrImmSaturatingNarrowFlags.VectorZxZx);
  387. }
  388. public static void Uqshl_V(ArmEmitterContext context)
  389. {
  390. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  391. Operand res = context.VectorZero();
  392. int elems = op.GetBytesCount() >> op.Size;
  393. for (int index = 0; index < elems; index++)
  394. {
  395. Operand ne = EmitVectorExtractZx(context, op.Rn, index, op.Size);
  396. Operand me = EmitVectorExtractZx(context, op.Rm, index, op.Size);
  397. Operand e = context.Call(typeof(SoftFallback).GetMethod(nameof(SoftFallback.UnsignedShlRegSatQ)), ne, me, Const(0), Const(op.Size));
  398. res = EmitVectorInsert(context, res, e, index, op.Size);
  399. }
  400. context.Copy(GetVec(op.Rd), res);
  401. }
  402. public static void Uqshrn_S(ArmEmitterContext context)
  403. {
  404. EmitShrImmSaturatingNarrowOp(context, ShrImmSaturatingNarrowFlags.ScalarZxZx);
  405. }
  406. public static void Uqshrn_V(ArmEmitterContext context)
  407. {
  408. EmitShrImmSaturatingNarrowOp(context, ShrImmSaturatingNarrowFlags.VectorZxZx);
  409. }
  410. public static void Urshl_V(ArmEmitterContext context)
  411. {
  412. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  413. Operand res = context.VectorZero();
  414. int elems = op.GetBytesCount() >> op.Size;
  415. for (int index = 0; index < elems; index++)
  416. {
  417. Operand ne = EmitVectorExtractZx(context, op.Rn, index, op.Size);
  418. Operand me = EmitVectorExtractZx(context, op.Rm, index, op.Size);
  419. Operand e = context.Call(typeof(SoftFallback).GetMethod(nameof(SoftFallback.UnsignedShlReg)), ne, me, Const(1), Const(op.Size));
  420. res = EmitVectorInsert(context, res, e, index, op.Size);
  421. }
  422. context.Copy(GetVec(op.Rd), res);
  423. }
  424. public static void Urshr_S(ArmEmitterContext context)
  425. {
  426. EmitScalarShrImmOpZx(context, ShrImmFlags.Round);
  427. }
  428. public static void Urshr_V(ArmEmitterContext context)
  429. {
  430. OpCodeSimdShImm op = (OpCodeSimdShImm)context.CurrOp;
  431. if (Optimizations.UseSse2 && op.Size > 0)
  432. {
  433. int shift = GetImmShr(op);
  434. int eSize = 8 << op.Size;
  435. Operand n = GetVec(op.Rn);
  436. Intrinsic sllInst = X86PsllInstruction[op.Size];
  437. Operand res = context.AddIntrinsic(sllInst, n, Const(eSize - shift));
  438. Intrinsic srlInst = X86PsrlInstruction[op.Size];
  439. res = context.AddIntrinsic(srlInst, res, Const(eSize - 1));
  440. Operand nSrl = context.AddIntrinsic(srlInst, n, Const(shift));
  441. Intrinsic addInst = X86PaddInstruction[op.Size];
  442. res = context.AddIntrinsic(addInst, res, nSrl);
  443. if (op.RegisterSize == RegisterSize.Simd64)
  444. {
  445. res = context.VectorZeroUpper64(res);
  446. }
  447. context.Copy(GetVec(op.Rd), res);
  448. }
  449. else
  450. {
  451. EmitVectorShrImmOpZx(context, ShrImmFlags.Round);
  452. }
  453. }
  454. public static void Ursra_S(ArmEmitterContext context)
  455. {
  456. EmitScalarShrImmOpZx(context, ShrImmFlags.Round | ShrImmFlags.Accumulate);
  457. }
  458. public static void Ursra_V(ArmEmitterContext context)
  459. {
  460. OpCodeSimdShImm op = (OpCodeSimdShImm)context.CurrOp;
  461. if (Optimizations.UseSse2 && op.Size > 0)
  462. {
  463. int shift = GetImmShr(op);
  464. int eSize = 8 << op.Size;
  465. Operand d = GetVec(op.Rd);
  466. Operand n = GetVec(op.Rn);
  467. Intrinsic sllInst = X86PsllInstruction[op.Size];
  468. Operand res = context.AddIntrinsic(sllInst, n, Const(eSize - shift));
  469. Intrinsic srlInst = X86PsrlInstruction[op.Size];
  470. res = context.AddIntrinsic(srlInst, res, Const(eSize - 1));
  471. Operand nSrl = context.AddIntrinsic(srlInst, n, Const(shift));
  472. Intrinsic addInst = X86PaddInstruction[op.Size];
  473. res = context.AddIntrinsic(addInst, res, nSrl);
  474. res = context.AddIntrinsic(addInst, res, d);
  475. if (op.RegisterSize == RegisterSize.Simd64)
  476. {
  477. res = context.VectorZeroUpper64(res);
  478. }
  479. context.Copy(d, res);
  480. }
  481. else
  482. {
  483. EmitVectorShrImmOpZx(context, ShrImmFlags.Round | ShrImmFlags.Accumulate);
  484. }
  485. }
  486. public static void Ushl_S(ArmEmitterContext context)
  487. {
  488. EmitSshlOrUshl(context, signed: false, scalar: true);
  489. }
  490. public static void Ushl_V(ArmEmitterContext context)
  491. {
  492. EmitSshlOrUshl(context, signed: false, scalar: false);
  493. }
  494. public static void Ushll_V(ArmEmitterContext context)
  495. {
  496. OpCodeSimdShImm op = (OpCodeSimdShImm)context.CurrOp;
  497. int shift = GetImmShl(op);
  498. if (Optimizations.UseSse41)
  499. {
  500. Operand n = GetVec(op.Rn);
  501. if (op.RegisterSize == RegisterSize.Simd128)
  502. {
  503. n = context.AddIntrinsic(Intrinsic.X86Psrldq, n, Const(8));
  504. }
  505. Intrinsic movzxInst = X86PmovzxInstruction[op.Size];
  506. Operand res = context.AddIntrinsic(movzxInst, n);
  507. if (shift != 0)
  508. {
  509. Intrinsic sllInst = X86PsllInstruction[op.Size + 1];
  510. res = context.AddIntrinsic(sllInst, res, Const(shift));
  511. }
  512. context.Copy(GetVec(op.Rd), res);
  513. }
  514. else
  515. {
  516. EmitVectorShImmWidenBinaryZx(context, (op1, op2) => context.ShiftLeft(op1, op2), shift);
  517. }
  518. }
  519. public static void Ushr_S(ArmEmitterContext context)
  520. {
  521. EmitShrImmOp(context, ShrImmFlags.ScalarZx);
  522. }
  523. public static void Ushr_V(ArmEmitterContext context)
  524. {
  525. OpCodeSimdShImm op = (OpCodeSimdShImm)context.CurrOp;
  526. if (Optimizations.UseSse2 && op.Size > 0)
  527. {
  528. int shift = GetImmShr(op);
  529. Operand n = GetVec(op.Rn);
  530. Intrinsic srlInst = X86PsrlInstruction[op.Size];
  531. Operand res = context.AddIntrinsic(srlInst, n, Const(shift));
  532. if (op.RegisterSize == RegisterSize.Simd64)
  533. {
  534. res = context.VectorZeroUpper64(res);
  535. }
  536. context.Copy(GetVec(op.Rd), res);
  537. }
  538. else
  539. {
  540. EmitShrImmOp(context, ShrImmFlags.VectorZx);
  541. }
  542. }
  543. public static void Usra_S(ArmEmitterContext context)
  544. {
  545. EmitScalarShrImmOpZx(context, ShrImmFlags.Accumulate);
  546. }
  547. public static void Usra_V(ArmEmitterContext context)
  548. {
  549. OpCodeSimdShImm op = (OpCodeSimdShImm)context.CurrOp;
  550. if (Optimizations.UseSse2 && op.Size > 0)
  551. {
  552. int shift = GetImmShr(op);
  553. Operand d = GetVec(op.Rd);
  554. Operand n = GetVec(op.Rn);
  555. Intrinsic srlInst = X86PsrlInstruction[op.Size];
  556. Operand res = context.AddIntrinsic(srlInst, n, Const(shift));
  557. Intrinsic addInst = X86PaddInstruction[op.Size];
  558. res = context.AddIntrinsic(addInst, res, d);
  559. if (op.RegisterSize == RegisterSize.Simd64)
  560. {
  561. res = context.VectorZeroUpper64(res);
  562. }
  563. context.Copy(d, res);
  564. }
  565. else
  566. {
  567. EmitVectorShrImmOpZx(context, ShrImmFlags.Accumulate);
  568. }
  569. }
  570. [Flags]
  571. private enum ShrImmFlags
  572. {
  573. Scalar = 1 << 0,
  574. Signed = 1 << 1,
  575. Round = 1 << 2,
  576. Accumulate = 1 << 3,
  577. ScalarSx = Scalar | Signed,
  578. ScalarZx = Scalar,
  579. VectorSx = Signed,
  580. VectorZx = 0
  581. }
  582. private static void EmitScalarShrImmOpSx(ArmEmitterContext context, ShrImmFlags flags)
  583. {
  584. EmitShrImmOp(context, ShrImmFlags.ScalarSx | flags);
  585. }
  586. private static void EmitScalarShrImmOpZx(ArmEmitterContext context, ShrImmFlags flags)
  587. {
  588. EmitShrImmOp(context, ShrImmFlags.ScalarZx | flags);
  589. }
  590. private static void EmitVectorShrImmOpSx(ArmEmitterContext context, ShrImmFlags flags)
  591. {
  592. EmitShrImmOp(context, ShrImmFlags.VectorSx | flags);
  593. }
  594. private static void EmitVectorShrImmOpZx(ArmEmitterContext context, ShrImmFlags flags)
  595. {
  596. EmitShrImmOp(context, ShrImmFlags.VectorZx | flags);
  597. }
  598. private static void EmitShrImmOp(ArmEmitterContext context, ShrImmFlags flags)
  599. {
  600. OpCodeSimdShImm op = (OpCodeSimdShImm)context.CurrOp;
  601. Operand res = context.VectorZero();
  602. bool scalar = (flags & ShrImmFlags.Scalar) != 0;
  603. bool signed = (flags & ShrImmFlags.Signed) != 0;
  604. bool round = (flags & ShrImmFlags.Round) != 0;
  605. bool accumulate = (flags & ShrImmFlags.Accumulate) != 0;
  606. int shift = GetImmShr(op);
  607. long roundConst = 1L << (shift - 1);
  608. int elems = !scalar ? op.GetBytesCount() >> op.Size : 1;
  609. for (int index = 0; index < elems; index++)
  610. {
  611. Operand e = EmitVectorExtract(context, op.Rn, index, op.Size, signed);
  612. if (op.Size <= 2)
  613. {
  614. if (round)
  615. {
  616. e = context.Add(e, Const(roundConst));
  617. }
  618. e = signed ? context.ShiftRightSI(e, Const(shift)) : context.ShiftRightUI(e, Const(shift));
  619. }
  620. else /* if (op.Size == 3) */
  621. {
  622. e = EmitShrImm64(context, e, signed, round ? roundConst : 0L, shift);
  623. }
  624. if (accumulate)
  625. {
  626. Operand de = EmitVectorExtract(context, op.Rd, index, op.Size, signed);
  627. e = context.Add(e, de);
  628. }
  629. res = EmitVectorInsert(context, res, e, index, op.Size);
  630. }
  631. context.Copy(GetVec(op.Rd), res);
  632. }
  633. private static Operand EmitShlRegOp(ArmEmitterContext context, Operand op, Operand shiftLsB, int size, bool signed)
  634. {
  635. Debug.Assert(op.Type == OperandType.I64);
  636. Debug.Assert(shiftLsB.Type == OperandType.I32);
  637. Debug.Assert((uint)size < 4u);
  638. Operand negShiftLsB = context.Negate(shiftLsB);
  639. Operand isInRange = context.BitwiseAnd(
  640. context.ICompareLess(shiftLsB, Const(8 << size)),
  641. context.ICompareLess(negShiftLsB, Const(8 << size)));
  642. Operand isPositive = context.ICompareGreaterOrEqual(shiftLsB, Const(0));
  643. Operand shl = context.ShiftLeft(op, shiftLsB);
  644. Operand sarOrShr = signed
  645. ? context.ShiftRightSI(op, negShiftLsB)
  646. : context.ShiftRightUI(op, negShiftLsB);
  647. Operand res = context.ConditionalSelect(isPositive, shl, sarOrShr);
  648. if (signed)
  649. {
  650. Operand isPositive2 = context.ICompareGreaterOrEqual(op, Const(0L));
  651. Operand res2 = context.ConditionalSelect(isPositive2, Const(0L), Const(-1L));
  652. res2 = context.ConditionalSelect(isPositive, Const(0L), res2);
  653. return context.ConditionalSelect(isInRange, res, res2);
  654. }
  655. else
  656. {
  657. return context.ConditionalSelect(isInRange, res, Const(0UL));
  658. }
  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 = signedSrc ? EmitSignedSrcSatQ(context, e, op.Size, signedDst) : EmitUnsignedSrcSatQ(context, e, op.Size, 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. private static void EmitSshlOrUshl(ArmEmitterContext context, bool signed, bool scalar)
  840. {
  841. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  842. Operand res = context.VectorZero();
  843. int elems = !scalar ? op.GetBytesCount() >> op.Size : 1;
  844. for (int index = 0; index < elems; index++)
  845. {
  846. Operand ne = EmitVectorExtract (context, op.Rn, index, op.Size, signed);
  847. Operand me = EmitVectorExtractSx(context, op.Rm, index << op.Size, 0);
  848. Operand e = EmitShlRegOp(context, ne, context.ConvertI64ToI32(me), op.Size, signed);
  849. res = EmitVectorInsert(context, res, e, index, op.Size);
  850. }
  851. context.Copy(GetVec(op.Rd), res);
  852. }
  853. }
  854. }