InstEmitSimdShift.cs 38 KB

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