InstEmitSimdHelper.cs 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016
  1. using ARMeilleure.Decoders;
  2. using ARMeilleure.IntermediateRepresentation;
  3. using ARMeilleure.State;
  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.IntermediateRepresentation.Operand.Factory;
  10. namespace ARMeilleure.Instructions
  11. {
  12. using Func1I = Func<Operand, Operand>;
  13. using Func2I = Func<Operand, Operand, Operand>;
  14. using Func3I = Func<Operand, Operand, Operand, Operand>;
  15. static class InstEmitSimdHelper
  16. {
  17. #region "Masks"
  18. public static readonly long[] EvenMasks = new long[]
  19. {
  20. 14L << 56 | 12L << 48 | 10L << 40 | 08L << 32 | 06L << 24 | 04L << 16 | 02L << 8 | 00L << 0, // B
  21. 13L << 56 | 12L << 48 | 09L << 40 | 08L << 32 | 05L << 24 | 04L << 16 | 01L << 8 | 00L << 0, // H
  22. 11L << 56 | 10L << 48 | 09L << 40 | 08L << 32 | 03L << 24 | 02L << 16 | 01L << 8 | 00L << 0 // S
  23. };
  24. public static readonly long[] OddMasks = new long[]
  25. {
  26. 15L << 56 | 13L << 48 | 11L << 40 | 09L << 32 | 07L << 24 | 05L << 16 | 03L << 8 | 01L << 0, // B
  27. 15L << 56 | 14L << 48 | 11L << 40 | 10L << 32 | 07L << 24 | 06L << 16 | 03L << 8 | 02L << 0, // H
  28. 15L << 56 | 14L << 48 | 13L << 40 | 12L << 32 | 07L << 24 | 06L << 16 | 05L << 8 | 04L << 0 // S
  29. };
  30. public static readonly long ZeroMask = 128L << 56 | 128L << 48 | 128L << 40 | 128L << 32 | 128L << 24 | 128L << 16 | 128L << 8 | 128L << 0;
  31. #endregion
  32. #region "X86 SSE Intrinsics"
  33. public static readonly Intrinsic[] X86PaddInstruction = new Intrinsic[]
  34. {
  35. Intrinsic.X86Paddb,
  36. Intrinsic.X86Paddw,
  37. Intrinsic.X86Paddd,
  38. Intrinsic.X86Paddq
  39. };
  40. public static readonly Intrinsic[] X86PcmpeqInstruction = new Intrinsic[]
  41. {
  42. Intrinsic.X86Pcmpeqb,
  43. Intrinsic.X86Pcmpeqw,
  44. Intrinsic.X86Pcmpeqd,
  45. Intrinsic.X86Pcmpeqq
  46. };
  47. public static readonly Intrinsic[] X86PcmpgtInstruction = new Intrinsic[]
  48. {
  49. Intrinsic.X86Pcmpgtb,
  50. Intrinsic.X86Pcmpgtw,
  51. Intrinsic.X86Pcmpgtd,
  52. Intrinsic.X86Pcmpgtq
  53. };
  54. public static readonly Intrinsic[] X86PmaxsInstruction = new Intrinsic[]
  55. {
  56. Intrinsic.X86Pmaxsb,
  57. Intrinsic.X86Pmaxsw,
  58. Intrinsic.X86Pmaxsd
  59. };
  60. public static readonly Intrinsic[] X86PmaxuInstruction = new Intrinsic[]
  61. {
  62. Intrinsic.X86Pmaxub,
  63. Intrinsic.X86Pmaxuw,
  64. Intrinsic.X86Pmaxud
  65. };
  66. public static readonly Intrinsic[] X86PminsInstruction = new Intrinsic[]
  67. {
  68. Intrinsic.X86Pminsb,
  69. Intrinsic.X86Pminsw,
  70. Intrinsic.X86Pminsd
  71. };
  72. public static readonly Intrinsic[] X86PminuInstruction = new Intrinsic[]
  73. {
  74. Intrinsic.X86Pminub,
  75. Intrinsic.X86Pminuw,
  76. Intrinsic.X86Pminud
  77. };
  78. public static readonly Intrinsic[] X86PmovsxInstruction = new Intrinsic[]
  79. {
  80. Intrinsic.X86Pmovsxbw,
  81. Intrinsic.X86Pmovsxwd,
  82. Intrinsic.X86Pmovsxdq
  83. };
  84. public static readonly Intrinsic[] X86PmovzxInstruction = new Intrinsic[]
  85. {
  86. Intrinsic.X86Pmovzxbw,
  87. Intrinsic.X86Pmovzxwd,
  88. Intrinsic.X86Pmovzxdq
  89. };
  90. public static readonly Intrinsic[] X86PsllInstruction = new Intrinsic[]
  91. {
  92. 0,
  93. Intrinsic.X86Psllw,
  94. Intrinsic.X86Pslld,
  95. Intrinsic.X86Psllq
  96. };
  97. public static readonly Intrinsic[] X86PsraInstruction = new Intrinsic[]
  98. {
  99. 0,
  100. Intrinsic.X86Psraw,
  101. Intrinsic.X86Psrad
  102. };
  103. public static readonly Intrinsic[] X86PsrlInstruction = new Intrinsic[]
  104. {
  105. 0,
  106. Intrinsic.X86Psrlw,
  107. Intrinsic.X86Psrld,
  108. Intrinsic.X86Psrlq
  109. };
  110. public static readonly Intrinsic[] X86PsubInstruction = new Intrinsic[]
  111. {
  112. Intrinsic.X86Psubb,
  113. Intrinsic.X86Psubw,
  114. Intrinsic.X86Psubd,
  115. Intrinsic.X86Psubq
  116. };
  117. public static readonly Intrinsic[] X86PunpckhInstruction = new Intrinsic[]
  118. {
  119. Intrinsic.X86Punpckhbw,
  120. Intrinsic.X86Punpckhwd,
  121. Intrinsic.X86Punpckhdq,
  122. Intrinsic.X86Punpckhqdq
  123. };
  124. public static readonly Intrinsic[] X86PunpcklInstruction = new Intrinsic[]
  125. {
  126. Intrinsic.X86Punpcklbw,
  127. Intrinsic.X86Punpcklwd,
  128. Intrinsic.X86Punpckldq,
  129. Intrinsic.X86Punpcklqdq
  130. };
  131. #endregion
  132. public static int GetImmShl(OpCodeSimdShImm op)
  133. {
  134. return op.Imm - (8 << op.Size);
  135. }
  136. public static int GetImmShr(OpCodeSimdShImm op)
  137. {
  138. return (8 << (op.Size + 1)) - op.Imm;
  139. }
  140. public static Operand X86GetScalar(ArmEmitterContext context, float value)
  141. {
  142. return X86GetScalar(context, BitConverter.SingleToInt32Bits(value));
  143. }
  144. public static Operand X86GetScalar(ArmEmitterContext context, double value)
  145. {
  146. return X86GetScalar(context, BitConverter.DoubleToInt64Bits(value));
  147. }
  148. public static Operand X86GetScalar(ArmEmitterContext context, int value)
  149. {
  150. return context.VectorCreateScalar(Const(value));
  151. }
  152. public static Operand X86GetScalar(ArmEmitterContext context, long value)
  153. {
  154. return context.VectorCreateScalar(Const(value));
  155. }
  156. public static Operand X86GetAllElements(ArmEmitterContext context, float value)
  157. {
  158. return X86GetAllElements(context, BitConverter.SingleToInt32Bits(value));
  159. }
  160. public static Operand X86GetAllElements(ArmEmitterContext context, double value)
  161. {
  162. return X86GetAllElements(context, BitConverter.DoubleToInt64Bits(value));
  163. }
  164. public static Operand X86GetAllElements(ArmEmitterContext context, short value)
  165. {
  166. ulong value1 = (ushort)value;
  167. ulong value2 = value1 << 16 | value1;
  168. ulong value4 = value2 << 32 | value2;
  169. return X86GetAllElements(context, (long)value4);
  170. }
  171. public static Operand X86GetAllElements(ArmEmitterContext context, int value)
  172. {
  173. Operand vector = context.VectorCreateScalar(Const(value));
  174. vector = context.AddIntrinsic(Intrinsic.X86Shufps, vector, vector, Const(0));
  175. return vector;
  176. }
  177. public static Operand X86GetAllElements(ArmEmitterContext context, long value)
  178. {
  179. Operand vector = context.VectorCreateScalar(Const(value));
  180. vector = context.AddIntrinsic(Intrinsic.X86Movlhps, vector, vector);
  181. return vector;
  182. }
  183. public static Operand X86GetElements(ArmEmitterContext context, long e1, long e0)
  184. {
  185. return X86GetElements(context, (ulong)e1, (ulong)e0);
  186. }
  187. public static Operand X86GetElements(ArmEmitterContext context, ulong e1, ulong e0)
  188. {
  189. Operand vector0 = context.VectorCreateScalar(Const(e0));
  190. Operand vector1 = context.VectorCreateScalar(Const(e1));
  191. return context.AddIntrinsic(Intrinsic.X86Punpcklqdq, vector0, vector1);
  192. }
  193. public static int X86GetRoundControl(FPRoundingMode roundMode)
  194. {
  195. switch (roundMode)
  196. {
  197. case FPRoundingMode.ToNearest: return 8 | 0; // even
  198. case FPRoundingMode.TowardsPlusInfinity: return 8 | 2;
  199. case FPRoundingMode.TowardsMinusInfinity: return 8 | 1;
  200. case FPRoundingMode.TowardsZero: return 8 | 3;
  201. }
  202. throw new ArgumentException($"Invalid rounding mode \"{roundMode}\".");
  203. }
  204. public static ulong X86GetGf2p8LogicalShiftLeft(int shift)
  205. {
  206. ulong identity =
  207. (0b00000001UL << 56) |
  208. (0b00000010UL << 48) |
  209. (0b00000100UL << 40) |
  210. (0b00001000UL << 32) |
  211. (0b00010000UL << 24) |
  212. (0b00100000UL << 16) |
  213. (0b01000000UL << 8) |
  214. (0b10000000UL << 0);
  215. return shift >= 0 ? identity >> (shift * 8) : identity << (-shift * 8);
  216. }
  217. public static Operand EmitCountSetBits8(ArmEmitterContext context, Operand op) // "size" is 8 (SIMD&FP Inst.).
  218. {
  219. Debug.Assert(op.Type == OperandType.I32 || op.Type == OperandType.I64);
  220. Operand op0 = context.Subtract(op, context.BitwiseAnd(context.ShiftRightUI(op, Const(1)), Const(op.Type, 0x55L)));
  221. Operand c1 = Const(op.Type, 0x33L);
  222. Operand op1 = context.Add(context.BitwiseAnd(context.ShiftRightUI(op0, Const(2)), c1), context.BitwiseAnd(op0, c1));
  223. return context.BitwiseAnd(context.Add(op1, context.ShiftRightUI(op1, Const(4))), Const(op.Type, 0x0fL));
  224. }
  225. public static void EmitScalarUnaryOpF(ArmEmitterContext context, Intrinsic inst32, Intrinsic inst64)
  226. {
  227. OpCodeSimd op = (OpCodeSimd)context.CurrOp;
  228. Operand n = GetVec(op.Rn);
  229. Intrinsic inst = (op.Size & 1) != 0 ? inst64 : inst32;
  230. Operand res = context.AddIntrinsic(inst, n);
  231. if ((op.Size & 1) != 0)
  232. {
  233. res = context.VectorZeroUpper64(res);
  234. }
  235. else
  236. {
  237. res = context.VectorZeroUpper96(res);
  238. }
  239. context.Copy(GetVec(op.Rd), res);
  240. }
  241. public static void EmitScalarBinaryOpF(ArmEmitterContext context, Intrinsic inst32, Intrinsic inst64)
  242. {
  243. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  244. Operand n = GetVec(op.Rn);
  245. Operand m = GetVec(op.Rm);
  246. Intrinsic inst = (op.Size & 1) != 0 ? inst64 : inst32;
  247. Operand res = context.AddIntrinsic(inst, n, m);
  248. if ((op.Size & 1) != 0)
  249. {
  250. res = context.VectorZeroUpper64(res);
  251. }
  252. else
  253. {
  254. res = context.VectorZeroUpper96(res);
  255. }
  256. context.Copy(GetVec(op.Rd), res);
  257. }
  258. public static void EmitVectorUnaryOpF(ArmEmitterContext context, Intrinsic inst32, Intrinsic inst64)
  259. {
  260. OpCodeSimd op = (OpCodeSimd)context.CurrOp;
  261. Operand n = GetVec(op.Rn);
  262. Intrinsic inst = (op.Size & 1) != 0 ? inst64 : inst32;
  263. Operand res = context.AddIntrinsic(inst, n);
  264. if (op.RegisterSize == RegisterSize.Simd64)
  265. {
  266. res = context.VectorZeroUpper64(res);
  267. }
  268. context.Copy(GetVec(op.Rd), res);
  269. }
  270. public static void EmitVectorBinaryOpF(ArmEmitterContext context, Intrinsic inst32, Intrinsic inst64)
  271. {
  272. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  273. Operand n = GetVec(op.Rn);
  274. Operand m = GetVec(op.Rm);
  275. Intrinsic inst = (op.Size & 1) != 0 ? inst64 : inst32;
  276. Operand res = context.AddIntrinsic(inst, n, m);
  277. if (op.RegisterSize == RegisterSize.Simd64)
  278. {
  279. res = context.VectorZeroUpper64(res);
  280. }
  281. context.Copy(GetVec(op.Rd), res);
  282. }
  283. public static Operand EmitUnaryMathCall(ArmEmitterContext context, string name, Operand n)
  284. {
  285. IOpCodeSimd op = (IOpCodeSimd)context.CurrOp;
  286. MethodInfo info = (op.Size & 1) == 0
  287. ? typeof(MathF).GetMethod(name, new Type[] { typeof(float) })
  288. : typeof(Math). GetMethod(name, new Type[] { typeof(double) });
  289. return context.Call(info, n);
  290. }
  291. public static Operand EmitRoundMathCall(ArmEmitterContext context, MidpointRounding roundMode, Operand n)
  292. {
  293. IOpCodeSimd op = (IOpCodeSimd)context.CurrOp;
  294. string name = nameof(Math.Round);
  295. MethodInfo info = (op.Size & 1) == 0
  296. ? typeof(MathF).GetMethod(name, new Type[] { typeof(float), typeof(MidpointRounding) })
  297. : typeof(Math). GetMethod(name, new Type[] { typeof(double), typeof(MidpointRounding) });
  298. return context.Call(info, n, Const((int)roundMode));
  299. }
  300. public static Operand EmitGetRoundingMode(ArmEmitterContext context)
  301. {
  302. Operand rMode = context.ShiftLeft(GetFpFlag(FPState.RMode1Flag), Const(1));
  303. rMode = context.BitwiseOr(rMode, GetFpFlag(FPState.RMode0Flag));
  304. return rMode;
  305. }
  306. public static Operand EmitRoundByRMode(ArmEmitterContext context, Operand op)
  307. {
  308. Debug.Assert(op.Type == OperandType.FP32 || op.Type == OperandType.FP64);
  309. Operand lbl1 = Label();
  310. Operand lbl2 = Label();
  311. Operand lbl3 = Label();
  312. Operand lblEnd = Label();
  313. Operand rN = Const((int)FPRoundingMode.ToNearest);
  314. Operand rP = Const((int)FPRoundingMode.TowardsPlusInfinity);
  315. Operand rM = Const((int)FPRoundingMode.TowardsMinusInfinity);
  316. Operand res = context.AllocateLocal(op.Type);
  317. Operand rMode = EmitGetRoundingMode(context);
  318. context.BranchIf(lbl1, rMode, rN, Comparison.NotEqual);
  319. context.Copy(res, EmitRoundMathCall(context, MidpointRounding.ToEven, op));
  320. context.Branch(lblEnd);
  321. context.MarkLabel(lbl1);
  322. context.BranchIf(lbl2, rMode, rP, Comparison.NotEqual);
  323. context.Copy(res, EmitUnaryMathCall(context, nameof(Math.Ceiling), op));
  324. context.Branch(lblEnd);
  325. context.MarkLabel(lbl2);
  326. context.BranchIf(lbl3, rMode, rM, Comparison.NotEqual);
  327. context.Copy(res, EmitUnaryMathCall(context, nameof(Math.Floor), op));
  328. context.Branch(lblEnd);
  329. context.MarkLabel(lbl3);
  330. context.Copy(res, EmitUnaryMathCall(context, nameof(Math.Truncate), op));
  331. context.Branch(lblEnd);
  332. context.MarkLabel(lblEnd);
  333. return res;
  334. }
  335. public static Operand EmitSoftFloatCall(ArmEmitterContext context, string name, params Operand[] callArgs)
  336. {
  337. IOpCodeSimd op = (IOpCodeSimd)context.CurrOp;
  338. MethodInfo info = (op.Size & 1) == 0
  339. ? typeof(SoftFloat32).GetMethod(name)
  340. : typeof(SoftFloat64).GetMethod(name);
  341. context.StoreToContext();
  342. Operand res = context.Call(info, callArgs);
  343. context.LoadFromContext();
  344. return res;
  345. }
  346. public static void EmitScalarBinaryOpByElemF(ArmEmitterContext context, Func2I emit)
  347. {
  348. OpCodeSimdRegElemF op = (OpCodeSimdRegElemF)context.CurrOp;
  349. OperandType type = (op.Size & 1) != 0 ? OperandType.FP64 : OperandType.FP32;
  350. Operand n = context.VectorExtract(type, GetVec(op.Rn), 0);
  351. Operand m = context.VectorExtract(type, GetVec(op.Rm), op.Index);
  352. context.Copy(GetVec(op.Rd), context.VectorInsert(context.VectorZero(), emit(n, m), 0));
  353. }
  354. public static void EmitScalarTernaryOpByElemF(ArmEmitterContext context, Func3I emit)
  355. {
  356. OpCodeSimdRegElemF op = (OpCodeSimdRegElemF)context.CurrOp;
  357. OperandType type = (op.Size & 1) != 0 ? OperandType.FP64 : OperandType.FP32;
  358. Operand d = context.VectorExtract(type, GetVec(op.Rd), 0);
  359. Operand n = context.VectorExtract(type, GetVec(op.Rn), 0);
  360. Operand m = context.VectorExtract(type, GetVec(op.Rm), op.Index);
  361. context.Copy(GetVec(op.Rd), context.VectorInsert(context.VectorZero(), emit(d, n, m), 0));
  362. }
  363. public static void EmitScalarUnaryOpSx(ArmEmitterContext context, Func1I emit)
  364. {
  365. OpCodeSimd op = (OpCodeSimd)context.CurrOp;
  366. Operand n = EmitVectorExtractSx(context, op.Rn, 0, op.Size);
  367. Operand d = EmitVectorInsert(context, context.VectorZero(), emit(n), 0, op.Size);
  368. context.Copy(GetVec(op.Rd), d);
  369. }
  370. public static void EmitScalarBinaryOpSx(ArmEmitterContext context, Func2I emit)
  371. {
  372. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  373. Operand n = EmitVectorExtractSx(context, op.Rn, 0, op.Size);
  374. Operand m = EmitVectorExtractSx(context, op.Rm, 0, op.Size);
  375. Operand d = EmitVectorInsert(context, context.VectorZero(), emit(n, m), 0, op.Size);
  376. context.Copy(GetVec(op.Rd), d);
  377. }
  378. public static void EmitScalarUnaryOpZx(ArmEmitterContext context, Func1I emit)
  379. {
  380. OpCodeSimd op = (OpCodeSimd)context.CurrOp;
  381. Operand n = EmitVectorExtractZx(context, op.Rn, 0, op.Size);
  382. Operand d = EmitVectorInsert(context, context.VectorZero(), emit(n), 0, op.Size);
  383. context.Copy(GetVec(op.Rd), d);
  384. }
  385. public static void EmitScalarBinaryOpZx(ArmEmitterContext context, Func2I emit)
  386. {
  387. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  388. Operand n = EmitVectorExtractZx(context, op.Rn, 0, op.Size);
  389. Operand m = EmitVectorExtractZx(context, op.Rm, 0, op.Size);
  390. Operand d = EmitVectorInsert(context, context.VectorZero(), emit(n, m), 0, op.Size);
  391. context.Copy(GetVec(op.Rd), d);
  392. }
  393. public static void EmitScalarTernaryOpZx(ArmEmitterContext context, Func3I emit)
  394. {
  395. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  396. Operand d = EmitVectorExtractZx(context, op.Rd, 0, op.Size);
  397. Operand n = EmitVectorExtractZx(context, op.Rn, 0, op.Size);
  398. Operand m = EmitVectorExtractZx(context, op.Rm, 0, op.Size);
  399. d = EmitVectorInsert(context, context.VectorZero(), emit(d, n, m), 0, op.Size);
  400. context.Copy(GetVec(op.Rd), d);
  401. }
  402. public static void EmitScalarUnaryOpF(ArmEmitterContext context, Func1I emit)
  403. {
  404. OpCodeSimd op = (OpCodeSimd)context.CurrOp;
  405. OperandType type = (op.Size & 1) != 0 ? OperandType.FP64 : OperandType.FP32;
  406. Operand n = context.VectorExtract(type, GetVec(op.Rn), 0);
  407. context.Copy(GetVec(op.Rd), context.VectorInsert(context.VectorZero(), emit(n), 0));
  408. }
  409. public static void EmitScalarBinaryOpF(ArmEmitterContext context, Func2I emit)
  410. {
  411. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  412. OperandType type = (op.Size & 1) != 0 ? OperandType.FP64 : OperandType.FP32;
  413. Operand n = context.VectorExtract(type, GetVec(op.Rn), 0);
  414. Operand m = context.VectorExtract(type, GetVec(op.Rm), 0);
  415. context.Copy(GetVec(op.Rd), context.VectorInsert(context.VectorZero(), emit(n, m), 0));
  416. }
  417. public static void EmitScalarTernaryRaOpF(ArmEmitterContext context, Func3I emit)
  418. {
  419. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  420. OperandType type = (op.Size & 1) != 0 ? OperandType.FP64 : OperandType.FP32;
  421. Operand a = context.VectorExtract(type, GetVec(op.Ra), 0);
  422. Operand n = context.VectorExtract(type, GetVec(op.Rn), 0);
  423. Operand m = context.VectorExtract(type, GetVec(op.Rm), 0);
  424. context.Copy(GetVec(op.Rd), context.VectorInsert(context.VectorZero(), emit(a, n, m), 0));
  425. }
  426. public static void EmitVectorUnaryOpF(ArmEmitterContext context, Func1I emit)
  427. {
  428. OpCodeSimd op = (OpCodeSimd)context.CurrOp;
  429. Operand res = context.VectorZero();
  430. int sizeF = op.Size & 1;
  431. OperandType type = sizeF != 0 ? OperandType.FP64 : OperandType.FP32;
  432. int elems = op.GetBytesCount() >> sizeF + 2;
  433. for (int index = 0; index < elems; index++)
  434. {
  435. Operand ne = context.VectorExtract(type, GetVec(op.Rn), index);
  436. res = context.VectorInsert(res, emit(ne), index);
  437. }
  438. context.Copy(GetVec(op.Rd), res);
  439. }
  440. public static void EmitVectorBinaryOpF(ArmEmitterContext context, Func2I emit)
  441. {
  442. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  443. Operand res = context.VectorZero();
  444. int sizeF = op.Size & 1;
  445. OperandType type = sizeF != 0 ? OperandType.FP64 : OperandType.FP32;
  446. int elems = op.GetBytesCount() >> sizeF + 2;
  447. for (int index = 0; index < elems; index++)
  448. {
  449. Operand ne = context.VectorExtract(type, GetVec(op.Rn), index);
  450. Operand me = context.VectorExtract(type, GetVec(op.Rm), index);
  451. res = context.VectorInsert(res, emit(ne, me), index);
  452. }
  453. context.Copy(GetVec(op.Rd), res);
  454. }
  455. public static void EmitVectorTernaryOpF(ArmEmitterContext context, Func3I emit)
  456. {
  457. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  458. Operand res = context.VectorZero();
  459. int sizeF = op.Size & 1;
  460. OperandType type = sizeF != 0 ? OperandType.FP64 : OperandType.FP32;
  461. int elems = op.GetBytesCount() >> sizeF + 2;
  462. for (int index = 0; index < elems; index++)
  463. {
  464. Operand de = context.VectorExtract(type, GetVec(op.Rd), index);
  465. Operand ne = context.VectorExtract(type, GetVec(op.Rn), index);
  466. Operand me = context.VectorExtract(type, GetVec(op.Rm), index);
  467. res = context.VectorInsert(res, emit(de, ne, me), index);
  468. }
  469. context.Copy(GetVec(op.Rd), res);
  470. }
  471. public static void EmitVectorBinaryOpByElemF(ArmEmitterContext context, Func2I emit)
  472. {
  473. OpCodeSimdRegElemF op = (OpCodeSimdRegElemF)context.CurrOp;
  474. Operand res = context.VectorZero();
  475. int sizeF = op.Size & 1;
  476. OperandType type = sizeF != 0 ? OperandType.FP64 : OperandType.FP32;
  477. int elems = op.GetBytesCount() >> sizeF + 2;
  478. for (int index = 0; index < elems; index++)
  479. {
  480. Operand ne = context.VectorExtract(type, GetVec(op.Rn), index);
  481. Operand me = context.VectorExtract(type, GetVec(op.Rm), op.Index);
  482. res = context.VectorInsert(res, emit(ne, me), index);
  483. }
  484. context.Copy(GetVec(op.Rd), res);
  485. }
  486. public static void EmitVectorTernaryOpByElemF(ArmEmitterContext context, Func3I emit)
  487. {
  488. OpCodeSimdRegElemF op = (OpCodeSimdRegElemF)context.CurrOp;
  489. Operand res = context.VectorZero();
  490. int sizeF = op.Size & 1;
  491. OperandType type = sizeF != 0 ? OperandType.FP64 : OperandType.FP32;
  492. int elems = op.GetBytesCount() >> sizeF + 2;
  493. for (int index = 0; index < elems; index++)
  494. {
  495. Operand de = context.VectorExtract(type, GetVec(op.Rd), index);
  496. Operand ne = context.VectorExtract(type, GetVec(op.Rn), index);
  497. Operand me = context.VectorExtract(type, GetVec(op.Rm), op.Index);
  498. res = context.VectorInsert(res, emit(de, ne, me), index);
  499. }
  500. context.Copy(GetVec(op.Rd), res);
  501. }
  502. public static void EmitVectorUnaryOpSx(ArmEmitterContext context, Func1I emit)
  503. {
  504. OpCodeSimd op = (OpCodeSimd)context.CurrOp;
  505. Operand res = context.VectorZero();
  506. int elems = op.GetBytesCount() >> op.Size;
  507. for (int index = 0; index < elems; index++)
  508. {
  509. Operand ne = EmitVectorExtractSx(context, op.Rn, index, op.Size);
  510. res = EmitVectorInsert(context, res, emit(ne), index, op.Size);
  511. }
  512. context.Copy(GetVec(op.Rd), res);
  513. }
  514. public static void EmitVectorBinaryOpSx(ArmEmitterContext context, Func2I emit)
  515. {
  516. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  517. Operand res = context.VectorZero();
  518. int elems = op.GetBytesCount() >> op.Size;
  519. for (int index = 0; index < elems; index++)
  520. {
  521. Operand ne = EmitVectorExtractSx(context, op.Rn, index, op.Size);
  522. Operand me = EmitVectorExtractSx(context, op.Rm, index, op.Size);
  523. res = EmitVectorInsert(context, res, emit(ne, me), index, op.Size);
  524. }
  525. context.Copy(GetVec(op.Rd), res);
  526. }
  527. public static void EmitVectorTernaryOpSx(ArmEmitterContext context, Func3I emit)
  528. {
  529. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  530. Operand res = context.VectorZero();
  531. int elems = op.GetBytesCount() >> op.Size;
  532. for (int index = 0; index < elems; index++)
  533. {
  534. Operand de = EmitVectorExtractSx(context, op.Rd, index, op.Size);
  535. Operand ne = EmitVectorExtractSx(context, op.Rn, index, op.Size);
  536. Operand me = EmitVectorExtractSx(context, op.Rm, index, op.Size);
  537. res = EmitVectorInsert(context, res, emit(de, ne, me), index, op.Size);
  538. }
  539. context.Copy(GetVec(op.Rd), res);
  540. }
  541. public static void EmitVectorUnaryOpZx(ArmEmitterContext context, Func1I emit)
  542. {
  543. OpCodeSimd op = (OpCodeSimd)context.CurrOp;
  544. Operand res = context.VectorZero();
  545. int elems = op.GetBytesCount() >> op.Size;
  546. for (int index = 0; index < elems; index++)
  547. {
  548. Operand ne = EmitVectorExtractZx(context, op.Rn, index, op.Size);
  549. res = EmitVectorInsert(context, res, emit(ne), index, op.Size);
  550. }
  551. context.Copy(GetVec(op.Rd), res);
  552. }
  553. public static void EmitVectorBinaryOpZx(ArmEmitterContext context, Func2I emit)
  554. {
  555. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  556. Operand res = context.VectorZero();
  557. int elems = op.GetBytesCount() >> op.Size;
  558. for (int index = 0; index < elems; index++)
  559. {
  560. Operand ne = EmitVectorExtractZx(context, op.Rn, index, op.Size);
  561. Operand me = EmitVectorExtractZx(context, op.Rm, index, op.Size);
  562. res = EmitVectorInsert(context, res, emit(ne, me), index, op.Size);
  563. }
  564. context.Copy(GetVec(op.Rd), res);
  565. }
  566. public static void EmitVectorTernaryOpZx(ArmEmitterContext context, Func3I emit)
  567. {
  568. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  569. Operand res = context.VectorZero();
  570. int elems = op.GetBytesCount() >> op.Size;
  571. for (int index = 0; index < elems; index++)
  572. {
  573. Operand de = EmitVectorExtractZx(context, op.Rd, index, op.Size);
  574. Operand ne = EmitVectorExtractZx(context, op.Rn, index, op.Size);
  575. Operand me = EmitVectorExtractZx(context, op.Rm, index, op.Size);
  576. res = EmitVectorInsert(context, res, emit(de, ne, me), index, op.Size);
  577. }
  578. context.Copy(GetVec(op.Rd), res);
  579. }
  580. public static void EmitVectorBinaryOpByElemSx(ArmEmitterContext context, Func2I emit)
  581. {
  582. OpCodeSimdRegElem op = (OpCodeSimdRegElem)context.CurrOp;
  583. Operand res = context.VectorZero();
  584. Operand me = EmitVectorExtractSx(context, op.Rm, op.Index, op.Size);
  585. int elems = op.GetBytesCount() >> op.Size;
  586. for (int index = 0; index < elems; index++)
  587. {
  588. Operand ne = EmitVectorExtractSx(context, op.Rn, index, op.Size);
  589. res = EmitVectorInsert(context, res, emit(ne, me), index, op.Size);
  590. }
  591. context.Copy(GetVec(op.Rd), res);
  592. }
  593. public static void EmitVectorBinaryOpByElemZx(ArmEmitterContext context, Func2I emit)
  594. {
  595. OpCodeSimdRegElem op = (OpCodeSimdRegElem)context.CurrOp;
  596. Operand res = context.VectorZero();
  597. Operand me = EmitVectorExtractZx(context, op.Rm, op.Index, op.Size);
  598. int elems = op.GetBytesCount() >> op.Size;
  599. for (int index = 0; index < elems; index++)
  600. {
  601. Operand ne = EmitVectorExtractZx(context, op.Rn, index, op.Size);
  602. res = EmitVectorInsert(context, res, emit(ne, me), index, op.Size);
  603. }
  604. context.Copy(GetVec(op.Rd), res);
  605. }
  606. public static void EmitVectorTernaryOpByElemZx(ArmEmitterContext context, Func3I emit)
  607. {
  608. OpCodeSimdRegElem op = (OpCodeSimdRegElem)context.CurrOp;
  609. Operand res = context.VectorZero();
  610. Operand me = EmitVectorExtractZx(context, op.Rm, op.Index, op.Size);
  611. int elems = op.GetBytesCount() >> op.Size;
  612. for (int index = 0; index < elems; index++)
  613. {
  614. Operand de = EmitVectorExtractZx(context, op.Rd, index, op.Size);
  615. Operand ne = EmitVectorExtractZx(context, op.Rn, index, op.Size);
  616. res = EmitVectorInsert(context, res, emit(de, ne, me), index, op.Size);
  617. }
  618. context.Copy(GetVec(op.Rd), res);
  619. }
  620. public static void EmitVectorImmUnaryOp(ArmEmitterContext context, Func1I emit)
  621. {
  622. OpCodeSimdImm op = (OpCodeSimdImm)context.CurrOp;
  623. Operand imm = Const(op.Immediate);
  624. Operand res = context.VectorZero();
  625. int elems = op.GetBytesCount() >> op.Size;
  626. for (int index = 0; index < elems; index++)
  627. {
  628. res = EmitVectorInsert(context, res, emit(imm), index, op.Size);
  629. }
  630. context.Copy(GetVec(op.Rd), res);
  631. }
  632. public static void EmitVectorImmBinaryOp(ArmEmitterContext context, Func2I emit)
  633. {
  634. OpCodeSimdImm op = (OpCodeSimdImm)context.CurrOp;
  635. Operand imm = Const(op.Immediate);
  636. Operand res = context.VectorZero();
  637. int elems = op.GetBytesCount() >> op.Size;
  638. for (int index = 0; index < elems; index++)
  639. {
  640. Operand de = EmitVectorExtractZx(context, op.Rd, index, op.Size);
  641. res = EmitVectorInsert(context, res, emit(de, imm), index, op.Size);
  642. }
  643. context.Copy(GetVec(op.Rd), res);
  644. }
  645. public static void EmitVectorWidenRmBinaryOpSx(ArmEmitterContext context, Func2I emit)
  646. {
  647. EmitVectorWidenRmBinaryOp(context, emit, signed: true);
  648. }
  649. public static void EmitVectorWidenRmBinaryOpZx(ArmEmitterContext context, Func2I emit)
  650. {
  651. EmitVectorWidenRmBinaryOp(context, emit, signed: false);
  652. }
  653. private static void EmitVectorWidenRmBinaryOp(ArmEmitterContext context, Func2I emit, bool signed)
  654. {
  655. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  656. Operand res = context.VectorZero();
  657. int elems = 8 >> op.Size;
  658. int part = op.RegisterSize == RegisterSize.Simd128 ? elems : 0;
  659. for (int index = 0; index < elems; index++)
  660. {
  661. Operand ne = EmitVectorExtract(context, op.Rn, index, op.Size + 1, signed);
  662. Operand me = EmitVectorExtract(context, op.Rm, part + index, op.Size, signed);
  663. res = EmitVectorInsert(context, res, emit(ne, me), index, op.Size + 1);
  664. }
  665. context.Copy(GetVec(op.Rd), res);
  666. }
  667. public static void EmitVectorWidenRnRmBinaryOpSx(ArmEmitterContext context, Func2I emit)
  668. {
  669. EmitVectorWidenRnRmBinaryOp(context, emit, signed: true);
  670. }
  671. public static void EmitVectorWidenRnRmBinaryOpZx(ArmEmitterContext context, Func2I emit)
  672. {
  673. EmitVectorWidenRnRmBinaryOp(context, emit, signed: false);
  674. }
  675. private static void EmitVectorWidenRnRmBinaryOp(ArmEmitterContext context, Func2I emit, bool signed)
  676. {
  677. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  678. Operand res = context.VectorZero();
  679. int elems = 8 >> op.Size;
  680. int part = op.RegisterSize == RegisterSize.Simd128 ? elems : 0;
  681. for (int index = 0; index < elems; index++)
  682. {
  683. Operand ne = EmitVectorExtract(context, op.Rn, part + index, op.Size, signed);
  684. Operand me = EmitVectorExtract(context, op.Rm, part + index, op.Size, signed);
  685. res = EmitVectorInsert(context, res, emit(ne, me), index, op.Size + 1);
  686. }
  687. context.Copy(GetVec(op.Rd), res);
  688. }
  689. public static void EmitVectorWidenRnRmTernaryOpSx(ArmEmitterContext context, Func3I emit)
  690. {
  691. EmitVectorWidenRnRmTernaryOp(context, emit, signed: true);
  692. }
  693. public static void EmitVectorWidenRnRmTernaryOpZx(ArmEmitterContext context, Func3I emit)
  694. {
  695. EmitVectorWidenRnRmTernaryOp(context, emit, signed: false);
  696. }
  697. private static void EmitVectorWidenRnRmTernaryOp(ArmEmitterContext context, Func3I emit, bool signed)
  698. {
  699. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  700. Operand res = context.VectorZero();
  701. int elems = 8 >> op.Size;
  702. int part = op.RegisterSize == RegisterSize.Simd128 ? elems : 0;
  703. for (int index = 0; index < elems; index++)
  704. {
  705. Operand de = EmitVectorExtract(context, op.Rd, index, op.Size + 1, signed);
  706. Operand ne = EmitVectorExtract(context, op.Rn, part + index, op.Size, signed);
  707. Operand me = EmitVectorExtract(context, op.Rm, part + index, op.Size, signed);
  708. res = EmitVectorInsert(context, res, emit(de, ne, me), index, op.Size + 1);
  709. }
  710. context.Copy(GetVec(op.Rd), res);
  711. }
  712. public static void EmitVectorWidenBinaryOpByElemSx(ArmEmitterContext context, Func2I emit)
  713. {
  714. EmitVectorWidenBinaryOpByElem(context, emit, signed: true);
  715. }
  716. public static void EmitVectorWidenBinaryOpByElemZx(ArmEmitterContext context, Func2I emit)
  717. {
  718. EmitVectorWidenBinaryOpByElem(context, emit, signed: false);
  719. }
  720. private static void EmitVectorWidenBinaryOpByElem(ArmEmitterContext context, Func2I emit, bool signed)
  721. {
  722. OpCodeSimdRegElem op = (OpCodeSimdRegElem)context.CurrOp;
  723. Operand res = context.VectorZero();
  724. Operand me = EmitVectorExtract(context, op.Rm, op.Index, op.Size, signed);
  725. int elems = 8 >> op.Size;
  726. int part = op.RegisterSize == RegisterSize.Simd128 ? elems : 0;
  727. for (int index = 0; index < elems; index++)
  728. {
  729. Operand ne = EmitVectorExtract(context, op.Rn, part + index, op.Size, signed);
  730. res = EmitVectorInsert(context, res, emit(ne, me), index, op.Size + 1);
  731. }
  732. context.Copy(GetVec(op.Rd), res);
  733. }
  734. public static void EmitVectorWidenTernaryOpByElemSx(ArmEmitterContext context, Func3I emit)
  735. {
  736. EmitVectorWidenTernaryOpByElem(context, emit, signed: true);
  737. }
  738. public static void EmitVectorWidenTernaryOpByElemZx(ArmEmitterContext context, Func3I emit)
  739. {
  740. EmitVectorWidenTernaryOpByElem(context, emit, signed: false);
  741. }
  742. private static void EmitVectorWidenTernaryOpByElem(ArmEmitterContext context, Func3I emit, bool signed)
  743. {
  744. OpCodeSimdRegElem op = (OpCodeSimdRegElem)context.CurrOp;
  745. Operand res = context.VectorZero();
  746. Operand me = EmitVectorExtract(context, op.Rm, op.Index, op.Size, signed);
  747. int elems = 8 >> op.Size;
  748. int part = op.RegisterSize == RegisterSize.Simd128 ? elems : 0;
  749. for (int index = 0; index < elems; index++)
  750. {
  751. Operand de = EmitVectorExtract(context, op.Rd, index, op.Size + 1, signed);
  752. Operand ne = EmitVectorExtract(context, op.Rn, part + index, op.Size, signed);
  753. res = EmitVectorInsert(context, res, emit(de, ne, me), index, op.Size + 1);
  754. }
  755. context.Copy(GetVec(op.Rd), res);
  756. }
  757. public static void EmitVectorPairwiseOpSx(ArmEmitterContext context, Func2I emit)
  758. {
  759. EmitVectorPairwiseOp(context, emit, signed: true);
  760. }
  761. public static void EmitVectorPairwiseOpZx(ArmEmitterContext context, Func2I emit)
  762. {
  763. EmitVectorPairwiseOp(context, emit, signed: false);
  764. }
  765. private static void EmitVectorPairwiseOp(ArmEmitterContext context, Func2I emit, bool signed)
  766. {
  767. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  768. Operand res = context.VectorZero();
  769. int pairs = op.GetPairsCount() >> op.Size;
  770. for (int index = 0; index < pairs; index++)
  771. {
  772. int pairIndex = index << 1;
  773. Operand n0 = EmitVectorExtract(context, op.Rn, pairIndex, op.Size, signed);
  774. Operand n1 = EmitVectorExtract(context, op.Rn, pairIndex + 1, op.Size, signed);
  775. Operand m0 = EmitVectorExtract(context, op.Rm, pairIndex, op.Size, signed);
  776. Operand m1 = EmitVectorExtract(context, op.Rm, pairIndex + 1, op.Size, signed);
  777. res = EmitVectorInsert(context, res, emit(n0, n1), index, op.Size);
  778. res = EmitVectorInsert(context, res, emit(m0, m1), pairs + index, op.Size);
  779. }
  780. context.Copy(GetVec(op.Rd), res);
  781. }
  782. public static void EmitSsse3VectorPairwiseOp(ArmEmitterContext context, Intrinsic[] inst)
  783. {
  784. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  785. Operand n = GetVec(op.Rn);
  786. Operand m = GetVec(op.Rm);
  787. if (op.RegisterSize == RegisterSize.Simd64)
  788. {
  789. Operand zeroEvenMask = X86GetElements(context, ZeroMask, EvenMasks[op.Size]);
  790. Operand zeroOddMask = X86GetElements(context, ZeroMask, OddMasks [op.Size]);
  791. Operand mN = context.AddIntrinsic(Intrinsic.X86Punpcklqdq, n, m); // m:n
  792. Operand left = context.AddIntrinsic(Intrinsic.X86Pshufb, mN, zeroEvenMask); // 0:even from m:n
  793. Operand right = context.AddIntrinsic(Intrinsic.X86Pshufb, mN, zeroOddMask); // 0:odd from m:n
  794. context.Copy(GetVec(op.Rd), context.AddIntrinsic(inst[op.Size], left, right));
  795. }
  796. else if (op.Size < 3)
  797. {
  798. Operand oddEvenMask = X86GetElements(context, OddMasks[op.Size], EvenMasks[op.Size]);
  799. Operand oddEvenN = context.AddIntrinsic(Intrinsic.X86Pshufb, n, oddEvenMask); // odd:even from n
  800. Operand oddEvenM = context.AddIntrinsic(Intrinsic.X86Pshufb, m, oddEvenMask); // odd:even from m
  801. Operand left = context.AddIntrinsic(Intrinsic.X86Punpcklqdq, oddEvenN, oddEvenM);
  802. Operand right = context.AddIntrinsic(Intrinsic.X86Punpckhqdq, oddEvenN, oddEvenM);
  803. context.Copy(GetVec(op.Rd), context.AddIntrinsic(inst[op.Size], left, right));
  804. }
  805. else
  806. {
  807. Operand left = context.AddIntrinsic(Intrinsic.X86Punpcklqdq, n, m);
  808. Operand right = context.AddIntrinsic(Intrinsic.X86Punpckhqdq, n, m);
  809. context.Copy(GetVec(op.Rd), context.AddIntrinsic(inst[3], left, right));
  810. }
  811. }
  812. public static void EmitVectorAcrossVectorOpSx(ArmEmitterContext context, Func2I emit)
  813. {
  814. EmitVectorAcrossVectorOp(context, emit, signed: true, isLong: false);
  815. }
  816. public static void EmitVectorAcrossVectorOpZx(ArmEmitterContext context, Func2I emit)
  817. {
  818. EmitVectorAcrossVectorOp(context, emit, signed: false, isLong: false);
  819. }
  820. public static void EmitVectorLongAcrossVectorOpSx(ArmEmitterContext context, Func2I emit)
  821. {
  822. EmitVectorAcrossVectorOp(context, emit, signed: true, isLong: true);
  823. }
  824. public static void EmitVectorLongAcrossVectorOpZx(ArmEmitterContext context, Func2I emit)
  825. {
  826. EmitVectorAcrossVectorOp(context, emit, signed: false, isLong: true);
  827. }
  828. private static void EmitVectorAcrossVectorOp(
  829. ArmEmitterContext context,
  830. Func2I emit,
  831. bool signed,
  832. bool isLong)
  833. {
  834. OpCodeSimd op = (OpCodeSimd)context.CurrOp;
  835. int elems = op.GetBytesCount() >> op.Size;
  836. Operand res = EmitVectorExtract(context, op.Rn, 0, op.Size, signed);
  837. for (int index = 1; index < elems; index++)
  838. {
  839. Operand n = EmitVectorExtract(context, op.Rn, index, op.Size, signed);
  840. res = emit(res, n);
  841. }
  842. int size = isLong ? op.Size + 1 : op.Size;
  843. Operand d = EmitVectorInsert(context, context.VectorZero(), res, 0, size);
  844. context.Copy(GetVec(op.Rd), d);
  845. }
  846. public static void EmitVectorAcrossVectorOpF(ArmEmitterContext context, Func2I emit)
  847. {
  848. OpCodeSimd op = (OpCodeSimd)context.CurrOp;
  849. Debug.Assert((op.Size & 1) == 0 && op.RegisterSize == RegisterSize.Simd128);
  850. Operand res = context.VectorExtract(OperandType.FP32, GetVec(op.Rn), 0);
  851. for (int index = 1; index < 4; index++)
  852. {
  853. Operand n = context.VectorExtract(OperandType.FP32, GetVec(op.Rn), index);
  854. res = emit(res, n);
  855. }
  856. Operand d = context.VectorInsert(context.VectorZero(), res, 0);
  857. context.Copy(GetVec(op.Rd), d);
  858. }
  859. public static void EmitSse2VectorAcrossVectorOpF(ArmEmitterContext context, Func2I emit)
  860. {
  861. OpCodeSimd op = (OpCodeSimd)context.CurrOp;
  862. Debug.Assert((op.Size & 1) == 0 && op.RegisterSize == RegisterSize.Simd128);
  863. const int sm0 = 0 << 6 | 0 << 4 | 0 << 2 | 0 << 0;
  864. const int sm1 = 1 << 6 | 1 << 4 | 1 << 2 | 1 << 0;
  865. const int sm2 = 2 << 6 | 2 << 4 | 2 << 2 | 2 << 0;
  866. const int sm3 = 3 << 6 | 3 << 4 | 3 << 2 | 3 << 0;
  867. Operand nCopy = context.Copy(GetVec(op.Rn));
  868. Operand part0 = context.AddIntrinsic(Intrinsic.X86Shufps, nCopy, nCopy, Const(sm0));
  869. Operand part1 = context.AddIntrinsic(Intrinsic.X86Shufps, nCopy, nCopy, Const(sm1));
  870. Operand part2 = context.AddIntrinsic(Intrinsic.X86Shufps, nCopy, nCopy, Const(sm2));
  871. Operand part3 = context.AddIntrinsic(Intrinsic.X86Shufps, nCopy, nCopy, Const(sm3));
  872. Operand res = emit(emit(part0, part1), emit(part2, part3));
  873. context.Copy(GetVec(op.Rd), context.VectorZeroUpper96(res));
  874. }
  875. public static void EmitScalarPairwiseOpF(ArmEmitterContext context, Func2I emit)
  876. {
  877. OpCodeSimd op = (OpCodeSimd)context.CurrOp;
  878. OperandType type = (op.Size & 1) != 0 ? OperandType.FP64 : OperandType.FP32;
  879. Operand ne0 = context.VectorExtract(type, GetVec(op.Rn), 0);
  880. Operand ne1 = context.VectorExtract(type, GetVec(op.Rn), 1);
  881. Operand res = context.VectorInsert(context.VectorZero(), emit(ne0, ne1), 0);
  882. context.Copy(GetVec(op.Rd), res);
  883. }
  884. public static void EmitSse2ScalarPairwiseOpF(ArmEmitterContext context, Func2I emit)
  885. {
  886. OpCodeSimd op = (OpCodeSimd)context.CurrOp;
  887. Operand n = GetVec(op.Rn);
  888. Operand op0, op1;
  889. if ((op.Size & 1) == 0)
  890. {
  891. const int sm0 = 2 << 6 | 2 << 4 | 2 << 2 | 0 << 0;
  892. const int sm1 = 2 << 6 | 2 << 4 | 2 << 2 | 1 << 0;
  893. Operand zeroN = context.VectorZeroUpper64(n);
  894. op0 = context.AddIntrinsic(Intrinsic.X86Pshufd, zeroN, Const(sm0));
  895. op1 = context.AddIntrinsic(Intrinsic.X86Pshufd, zeroN, Const(sm1));
  896. }
  897. else /* if ((op.Size & 1) == 1) */
  898. {
  899. Operand zero = context.VectorZero();
  900. op0 = context.AddIntrinsic(Intrinsic.X86Movlhps, n, zero);
  901. op1 = context.AddIntrinsic(Intrinsic.X86Movhlps, zero, n);
  902. }
  903. context.Copy(GetVec(op.Rd), emit(op0, op1));
  904. }
  905. public static void EmitVectorPairwiseOpF(ArmEmitterContext context, Func2I emit)
  906. {
  907. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  908. Operand res = context.VectorZero();
  909. int sizeF = op.Size & 1;
  910. OperandType type = sizeF != 0 ? OperandType.FP64 : OperandType.FP32;
  911. int pairs = op.GetPairsCount() >> sizeF + 2;
  912. for (int index = 0; index < pairs; index++)
  913. {
  914. int pairIndex = index << 1;
  915. Operand n0 = context.VectorExtract(type, GetVec(op.Rn), pairIndex);
  916. Operand n1 = context.VectorExtract(type, GetVec(op.Rn), pairIndex + 1);
  917. Operand m0 = context.VectorExtract(type, GetVec(op.Rm), pairIndex);
  918. Operand m1 = context.VectorExtract(type, GetVec(op.Rm), pairIndex + 1);
  919. res = context.VectorInsert(res, emit(n0, n1), index);
  920. res = context.VectorInsert(res, emit(m0, m1), pairs + index);
  921. }
  922. context.Copy(GetVec(op.Rd), res);
  923. }
  924. public static void EmitSse2VectorPairwiseOpF(ArmEmitterContext context, Func2I emit)
  925. {
  926. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  927. Operand nCopy = context.Copy(GetVec(op.Rn));
  928. Operand mCopy = context.Copy(GetVec(op.Rm));
  929. int sizeF = op.Size & 1;
  930. if (sizeF == 0)
  931. {
  932. if (op.RegisterSize == RegisterSize.Simd64)
  933. {
  934. Operand unpck = context.AddIntrinsic(Intrinsic.X86Unpcklps, nCopy, mCopy);
  935. Operand zero = context.VectorZero();
  936. Operand part0 = context.AddIntrinsic(Intrinsic.X86Movlhps, unpck, zero);
  937. Operand part1 = context.AddIntrinsic(Intrinsic.X86Movhlps, zero, unpck);
  938. context.Copy(GetVec(op.Rd), emit(part0, part1));
  939. }
  940. else /* if (op.RegisterSize == RegisterSize.Simd128) */
  941. {
  942. const int sm0 = 2 << 6 | 0 << 4 | 2 << 2 | 0 << 0;
  943. const int sm1 = 3 << 6 | 1 << 4 | 3 << 2 | 1 << 0;
  944. Operand part0 = context.AddIntrinsic(Intrinsic.X86Shufps, nCopy, mCopy, Const(sm0));
  945. Operand part1 = context.AddIntrinsic(Intrinsic.X86Shufps, nCopy, mCopy, Const(sm1));
  946. context.Copy(GetVec(op.Rd), emit(part0, part1));
  947. }
  948. }
  949. else /* if (sizeF == 1) */
  950. {
  951. Operand part0 = context.AddIntrinsic(Intrinsic.X86Unpcklpd, nCopy, mCopy);
  952. Operand part1 = context.AddIntrinsic(Intrinsic.X86Unpckhpd, nCopy, mCopy);
  953. context.Copy(GetVec(op.Rd), emit(part0, part1));
  954. }
  955. }
  956. [Flags]
  957. public enum Mxcsr
  958. {
  959. Ftz = 1 << 15, // Flush To Zero.
  960. Um = 1 << 11, // Underflow Mask.
  961. Dm = 1 << 8, // Denormal Mask.
  962. Daz = 1 << 6 // Denormals Are Zero.
  963. }
  964. public static void EmitSseOrAvxEnterFtzAndDazModesOpF(ArmEmitterContext context, out Operand isTrue)
  965. {
  966. isTrue = GetFpFlag(FPState.FzFlag);
  967. Operand lblTrue = Label();
  968. context.BranchIfFalse(lblTrue, isTrue);
  969. context.AddIntrinsicNoRet(Intrinsic.X86Mxcsrmb, Const((int)(Mxcsr.Ftz | Mxcsr.Um | Mxcsr.Dm | Mxcsr.Daz)));
  970. context.MarkLabel(lblTrue);
  971. }
  972. public static void EmitSseOrAvxExitFtzAndDazModesOpF(ArmEmitterContext context, Operand isTrue = default)
  973. {
  974. isTrue = isTrue == default ? GetFpFlag(FPState.FzFlag) : isTrue;
  975. Operand lblTrue = Label();
  976. context.BranchIfFalse(lblTrue, isTrue);
  977. context.AddIntrinsicNoRet(Intrinsic.X86Mxcsrub, Const((int)(Mxcsr.Ftz | Mxcsr.Daz)));
  978. context.MarkLabel(lblTrue);
  979. }
  980. public enum CmpCondition
  981. {
  982. // Legacy Sse.
  983. Equal = 0, // Ordered, non-signaling.
  984. LessThan = 1, // Ordered, signaling.
  985. LessThanOrEqual = 2, // Ordered, signaling.
  986. UnorderedQ = 3, // Non-signaling.
  987. NotLessThan = 5, // Unordered, signaling.
  988. NotLessThanOrEqual = 6, // Unordered, signaling.
  989. OrderedQ = 7, // Non-signaling.
  990. // Vex.
  991. GreaterThanOrEqual = 13, // Ordered, signaling.
  992. GreaterThan = 14, // Ordered, signaling.
  993. OrderedS = 23 // Signaling.
  994. }
  995. [Flags]
  996. public enum SaturatingFlags
  997. {
  998. None = 0,
  999. ByElem = 1 << 0,
  1000. Scalar = 1 << 1,
  1001. Signed = 1 << 2,
  1002. Add = 1 << 3,
  1003. Sub = 1 << 4,
  1004. Accumulate = 1 << 5
  1005. }
  1006. public static void EmitScalarSaturatingUnaryOpSx(ArmEmitterContext context, Func1I emit)
  1007. {
  1008. EmitSaturatingUnaryOpSx(context, emit, SaturatingFlags.Scalar | SaturatingFlags.Signed);
  1009. }
  1010. public static void EmitVectorSaturatingUnaryOpSx(ArmEmitterContext context, Func1I emit)
  1011. {
  1012. EmitSaturatingUnaryOpSx(context, emit, SaturatingFlags.Signed);
  1013. }
  1014. public static void EmitSaturatingUnaryOpSx(ArmEmitterContext context, Func1I emit, SaturatingFlags flags)
  1015. {
  1016. OpCodeSimd op = (OpCodeSimd)context.CurrOp;
  1017. Operand res = context.VectorZero();
  1018. bool scalar = (flags & SaturatingFlags.Scalar) != 0;
  1019. int elems = !scalar ? op.GetBytesCount() >> op.Size : 1;
  1020. for (int index = 0; index < elems; index++)
  1021. {
  1022. Operand ne = EmitVectorExtractSx(context, op.Rn, index, op.Size);
  1023. Operand de;
  1024. if (op.Size <= 2)
  1025. {
  1026. de = EmitSignedSrcSatQ(context, emit(ne), op.Size, signedDst: true);
  1027. }
  1028. else /* if (op.Size == 3) */
  1029. {
  1030. de = EmitUnarySignedSatQAbsOrNeg(context, emit(ne));
  1031. }
  1032. res = EmitVectorInsert(context, res, de, index, op.Size);
  1033. }
  1034. context.Copy(GetVec(op.Rd), res);
  1035. }
  1036. public static void EmitScalarSaturatingBinaryOpSx(ArmEmitterContext context, Func2I emit = null, SaturatingFlags flags = SaturatingFlags.None)
  1037. {
  1038. EmitSaturatingBinaryOp(context, emit, SaturatingFlags.Scalar | SaturatingFlags.Signed | flags);
  1039. }
  1040. public static void EmitScalarSaturatingBinaryOpZx(ArmEmitterContext context, SaturatingFlags flags)
  1041. {
  1042. EmitSaturatingBinaryOp(context, null, SaturatingFlags.Scalar | flags);
  1043. }
  1044. public static void EmitVectorSaturatingBinaryOpSx(ArmEmitterContext context, Func2I emit = null, SaturatingFlags flags = SaturatingFlags.None)
  1045. {
  1046. EmitSaturatingBinaryOp(context, emit, SaturatingFlags.Signed | flags);
  1047. }
  1048. public static void EmitVectorSaturatingBinaryOpZx(ArmEmitterContext context, SaturatingFlags flags)
  1049. {
  1050. EmitSaturatingBinaryOp(context, null, flags);
  1051. }
  1052. public static void EmitVectorSaturatingBinaryOpByElemSx(ArmEmitterContext context, Func2I emit)
  1053. {
  1054. EmitSaturatingBinaryOp(context, emit, SaturatingFlags.ByElem | SaturatingFlags.Signed);
  1055. }
  1056. public static void EmitSaturatingBinaryOp(ArmEmitterContext context, Func2I emit, SaturatingFlags flags)
  1057. {
  1058. OpCodeSimd op = (OpCodeSimd)context.CurrOp;
  1059. Operand res = context.VectorZero();
  1060. bool byElem = (flags & SaturatingFlags.ByElem) != 0;
  1061. bool scalar = (flags & SaturatingFlags.Scalar) != 0;
  1062. bool signed = (flags & SaturatingFlags.Signed) != 0;
  1063. bool add = (flags & SaturatingFlags.Add) != 0;
  1064. bool sub = (flags & SaturatingFlags.Sub) != 0;
  1065. bool accumulate = (flags & SaturatingFlags.Accumulate) != 0;
  1066. int elems = !scalar ? op.GetBytesCount() >> op.Size : 1;
  1067. if (add || sub)
  1068. {
  1069. for (int index = 0; index < elems; index++)
  1070. {
  1071. Operand de;
  1072. Operand ne = EmitVectorExtract(context, op.Rn, index, op.Size, signed);
  1073. Operand me = EmitVectorExtract(context, ((OpCodeSimdReg)op).Rm, index, op.Size, signed);
  1074. if (op.Size <= 2)
  1075. {
  1076. Operand temp = add ? context.Add(ne, me) : context.Subtract(ne, me);
  1077. de = EmitSignedSrcSatQ(context, temp, op.Size, signedDst: signed);
  1078. }
  1079. else /* if (op.Size == 3) */
  1080. {
  1081. if (add)
  1082. {
  1083. de = signed ? EmitBinarySignedSatQAdd(context, ne, me) : EmitBinaryUnsignedSatQAdd(context, ne, me);
  1084. }
  1085. else /* if (sub) */
  1086. {
  1087. de = signed ? EmitBinarySignedSatQSub(context, ne, me) : EmitBinaryUnsignedSatQSub(context, ne, me);
  1088. }
  1089. }
  1090. res = EmitVectorInsert(context, res, de, index, op.Size);
  1091. }
  1092. }
  1093. else if (accumulate)
  1094. {
  1095. for (int index = 0; index < elems; index++)
  1096. {
  1097. Operand de;
  1098. Operand ne = EmitVectorExtract(context, op.Rn, index, op.Size, !signed);
  1099. Operand me = EmitVectorExtract(context, op.Rd, index, op.Size, signed);
  1100. if (op.Size <= 2)
  1101. {
  1102. Operand temp = context.Add(ne, me);
  1103. de = EmitSignedSrcSatQ(context, temp, op.Size, signedDst: signed);
  1104. }
  1105. else /* if (op.Size == 3) */
  1106. {
  1107. de = signed ? EmitBinarySignedSatQAcc(context, ne, me) : EmitBinaryUnsignedSatQAcc(context, ne, me);
  1108. }
  1109. res = EmitVectorInsert(context, res, de, index, op.Size);
  1110. }
  1111. }
  1112. else
  1113. {
  1114. Operand me = default;
  1115. if (byElem)
  1116. {
  1117. OpCodeSimdRegElem opRegElem = (OpCodeSimdRegElem)op;
  1118. me = EmitVectorExtract(context, opRegElem.Rm, opRegElem.Index, op.Size, signed);
  1119. }
  1120. for (int index = 0; index < elems; index++)
  1121. {
  1122. Operand ne = EmitVectorExtract(context, op.Rn, index, op.Size, signed);
  1123. if (!byElem)
  1124. {
  1125. me = EmitVectorExtract(context, ((OpCodeSimdReg)op).Rm, index, op.Size, signed);
  1126. }
  1127. Operand de = EmitSignedSrcSatQ(context, emit(ne, me), op.Size, signedDst: signed);
  1128. res = EmitVectorInsert(context, res, de, index, op.Size);
  1129. }
  1130. }
  1131. context.Copy(GetVec(op.Rd), res);
  1132. }
  1133. [Flags]
  1134. public enum SaturatingNarrowFlags
  1135. {
  1136. Scalar = 1 << 0,
  1137. SignedSrc = 1 << 1,
  1138. SignedDst = 1 << 2,
  1139. ScalarSxSx = Scalar | SignedSrc | SignedDst,
  1140. ScalarSxZx = Scalar | SignedSrc,
  1141. ScalarZxZx = Scalar,
  1142. VectorSxSx = SignedSrc | SignedDst,
  1143. VectorSxZx = SignedSrc,
  1144. VectorZxZx = 0
  1145. }
  1146. public static void EmitSaturatingNarrowOp(ArmEmitterContext context, SaturatingNarrowFlags flags)
  1147. {
  1148. OpCodeSimd op = (OpCodeSimd)context.CurrOp;
  1149. bool scalar = (flags & SaturatingNarrowFlags.Scalar) != 0;
  1150. bool signedSrc = (flags & SaturatingNarrowFlags.SignedSrc) != 0;
  1151. bool signedDst = (flags & SaturatingNarrowFlags.SignedDst) != 0;
  1152. int elems = !scalar ? 8 >> op.Size : 1;
  1153. int part = !scalar && (op.RegisterSize == RegisterSize.Simd128) ? elems : 0;
  1154. Operand d = GetVec(op.Rd);
  1155. Operand res = part == 0 ? context.VectorZero() : context.Copy(d);
  1156. for (int index = 0; index < elems; index++)
  1157. {
  1158. Operand ne = EmitVectorExtract(context, op.Rn, index, op.Size + 1, signedSrc);
  1159. Operand temp = signedSrc
  1160. ? EmitSignedSrcSatQ(context, ne, op.Size, signedDst)
  1161. : EmitUnsignedSrcSatQ(context, ne, op.Size, signedDst);
  1162. res = EmitVectorInsert(context, res, temp, part + index, op.Size);
  1163. }
  1164. context.Copy(d, res);
  1165. }
  1166. // long SignedSignSatQ(long op, int size);
  1167. public static Operand EmitSignedSignSatQ(ArmEmitterContext context, Operand op, int size)
  1168. {
  1169. int eSize = 8 << size;
  1170. Debug.Assert(op.Type == OperandType.I64);
  1171. Debug.Assert(eSize == 8 || eSize == 16 || eSize == 32 || eSize == 64);
  1172. Operand lbl1 = Label();
  1173. Operand lblEnd = Label();
  1174. Operand zeroL = Const(0L);
  1175. Operand maxT = Const((1L << (eSize - 1)) - 1L);
  1176. Operand minT = Const(-(1L << (eSize - 1)));
  1177. Operand res = context.Copy(context.AllocateLocal(OperandType.I64), zeroL);
  1178. context.BranchIf(lbl1, op, zeroL, Comparison.LessOrEqual);
  1179. context.Copy(res, maxT);
  1180. SetFpFlag(context, FPState.QcFlag, Const(1));
  1181. context.Branch(lblEnd);
  1182. context.MarkLabel(lbl1);
  1183. context.BranchIf(lblEnd, op, zeroL, Comparison.GreaterOrEqual);
  1184. context.Copy(res, minT);
  1185. SetFpFlag(context, FPState.QcFlag, Const(1));
  1186. context.Branch(lblEnd);
  1187. context.MarkLabel(lblEnd);
  1188. return res;
  1189. }
  1190. // private static ulong UnsignedSignSatQ(ulong op, int size);
  1191. public static Operand EmitUnsignedSignSatQ(ArmEmitterContext context, Operand op, int size)
  1192. {
  1193. int eSize = 8 << size;
  1194. Debug.Assert(op.Type == OperandType.I64);
  1195. Debug.Assert(eSize == 8 || eSize == 16 || eSize == 32 || eSize == 64);
  1196. Operand lblEnd = Label();
  1197. Operand zeroUL = Const(0UL);
  1198. Operand maxT = Const(ulong.MaxValue >> (64 - eSize));
  1199. Operand res = context.Copy(context.AllocateLocal(OperandType.I64), zeroUL);
  1200. context.BranchIf(lblEnd, op, zeroUL, Comparison.LessOrEqualUI);
  1201. context.Copy(res, maxT);
  1202. SetFpFlag(context, FPState.QcFlag, Const(1));
  1203. context.Branch(lblEnd);
  1204. context.MarkLabel(lblEnd);
  1205. return res;
  1206. }
  1207. // TSrc (16bit, 32bit, 64bit; signed) > TDst (8bit, 16bit, 32bit; signed, unsigned).
  1208. // long SignedSrcSignedDstSatQ(long op, int size); ulong SignedSrcUnsignedDstSatQ(long op, int size);
  1209. public static Operand EmitSignedSrcSatQ(ArmEmitterContext context, Operand op, int sizeDst, bool signedDst)
  1210. {
  1211. int eSizeDst = 8 << sizeDst;
  1212. Debug.Assert(op.Type == OperandType.I64);
  1213. Debug.Assert(eSizeDst == 8 || eSizeDst == 16 || eSizeDst == 32);
  1214. Operand lbl1 = Label();
  1215. Operand lblEnd = Label();
  1216. Operand maxT = signedDst ? Const((1L << (eSizeDst - 1)) - 1L) : Const((1UL << eSizeDst) - 1UL);
  1217. Operand minT = signedDst ? Const(-(1L << (eSizeDst - 1))) : Const(0UL);
  1218. Operand res = context.Copy(context.AllocateLocal(OperandType.I64), op);
  1219. context.BranchIf(lbl1, op, maxT, Comparison.LessOrEqual);
  1220. context.Copy(res, maxT);
  1221. SetFpFlag(context, FPState.QcFlag, Const(1));
  1222. context.Branch(lblEnd);
  1223. context.MarkLabel(lbl1);
  1224. context.BranchIf(lblEnd, op, minT, Comparison.GreaterOrEqual);
  1225. context.Copy(res, minT);
  1226. SetFpFlag(context, FPState.QcFlag, Const(1));
  1227. context.Branch(lblEnd);
  1228. context.MarkLabel(lblEnd);
  1229. return res;
  1230. }
  1231. // TSrc (16bit, 32bit, 64bit; unsigned) > TDst (8bit, 16bit, 32bit; signed, unsigned).
  1232. // long UnsignedSrcSignedDstSatQ(ulong op, int size); ulong UnsignedSrcUnsignedDstSatQ(ulong op, int size);
  1233. public static Operand EmitUnsignedSrcSatQ(ArmEmitterContext context, Operand op, int sizeDst, bool signedDst)
  1234. {
  1235. int eSizeDst = 8 << sizeDst;
  1236. Debug.Assert(op.Type == OperandType.I64);
  1237. Debug.Assert(eSizeDst == 8 || eSizeDst == 16 || eSizeDst == 32);
  1238. Operand lblEnd = Label();
  1239. Operand maxT = signedDst ? Const((1L << (eSizeDst - 1)) - 1L) : Const((1UL << eSizeDst) - 1UL);
  1240. Operand res = context.Copy(context.AllocateLocal(OperandType.I64), op);
  1241. context.BranchIf(lblEnd, op, maxT, Comparison.LessOrEqualUI);
  1242. context.Copy(res, maxT);
  1243. SetFpFlag(context, FPState.QcFlag, Const(1));
  1244. context.Branch(lblEnd);
  1245. context.MarkLabel(lblEnd);
  1246. return res;
  1247. }
  1248. // long UnarySignedSatQAbsOrNeg(long op);
  1249. private static Operand EmitUnarySignedSatQAbsOrNeg(ArmEmitterContext context, Operand op)
  1250. {
  1251. Debug.Assert(op.Type == OperandType.I64);
  1252. Operand lblEnd = Label();
  1253. Operand minL = Const(long.MinValue);
  1254. Operand maxL = Const(long.MaxValue);
  1255. Operand res = context.Copy(context.AllocateLocal(OperandType.I64), op);
  1256. context.BranchIf(lblEnd, op, minL, Comparison.NotEqual);
  1257. context.Copy(res, maxL);
  1258. SetFpFlag(context, FPState.QcFlag, Const(1));
  1259. context.Branch(lblEnd);
  1260. context.MarkLabel(lblEnd);
  1261. return res;
  1262. }
  1263. // long BinarySignedSatQAdd(long op1, long op2);
  1264. public static Operand EmitBinarySignedSatQAdd(ArmEmitterContext context, Operand op1, Operand op2)
  1265. {
  1266. Debug.Assert(op1.Type == OperandType.I64 && op2.Type == OperandType.I64);
  1267. Operand lblEnd = Label();
  1268. Operand minL = Const(long.MinValue);
  1269. Operand maxL = Const(long.MaxValue);
  1270. Operand zeroL = Const(0L);
  1271. Operand add = context.Add(op1, op2);
  1272. Operand res = context.Copy(context.AllocateLocal(OperandType.I64), add);
  1273. Operand left = context.BitwiseNot(context.BitwiseExclusiveOr(op1, op2));
  1274. Operand right = context.BitwiseExclusiveOr(op1, add);
  1275. context.BranchIf(lblEnd, context.BitwiseAnd(left, right), zeroL, Comparison.GreaterOrEqual);
  1276. Operand isPositive = context.ICompareGreaterOrEqual(op1, zeroL);
  1277. context.Copy(res, context.ConditionalSelect(isPositive, maxL, minL));
  1278. SetFpFlag(context, FPState.QcFlag, Const(1));
  1279. context.Branch(lblEnd);
  1280. context.MarkLabel(lblEnd);
  1281. return res;
  1282. }
  1283. // ulong BinaryUnsignedSatQAdd(ulong op1, ulong op2);
  1284. public static Operand EmitBinaryUnsignedSatQAdd(ArmEmitterContext context, Operand op1, Operand op2)
  1285. {
  1286. Debug.Assert(op1.Type == OperandType.I64 && op2.Type == OperandType.I64);
  1287. Operand lblEnd = Label();
  1288. Operand maxUL = Const(ulong.MaxValue);
  1289. Operand add = context.Add(op1, op2);
  1290. Operand res = context.Copy(context.AllocateLocal(OperandType.I64), add);
  1291. context.BranchIf(lblEnd, add, op1, Comparison.GreaterOrEqualUI);
  1292. context.Copy(res, maxUL);
  1293. SetFpFlag(context, FPState.QcFlag, Const(1));
  1294. context.Branch(lblEnd);
  1295. context.MarkLabel(lblEnd);
  1296. return res;
  1297. }
  1298. // long BinarySignedSatQSub(long op1, long op2);
  1299. public static Operand EmitBinarySignedSatQSub(ArmEmitterContext context, Operand op1, Operand op2)
  1300. {
  1301. Debug.Assert(op1.Type == OperandType.I64 && op2.Type == OperandType.I64);
  1302. Operand lblEnd = Label();
  1303. Operand minL = Const(long.MinValue);
  1304. Operand maxL = Const(long.MaxValue);
  1305. Operand zeroL = Const(0L);
  1306. Operand sub = context.Subtract(op1, op2);
  1307. Operand res = context.Copy(context.AllocateLocal(OperandType.I64), sub);
  1308. Operand left = context.BitwiseExclusiveOr(op1, op2);
  1309. Operand right = context.BitwiseExclusiveOr(op1, sub);
  1310. context.BranchIf(lblEnd, context.BitwiseAnd(left, right), zeroL, Comparison.GreaterOrEqual);
  1311. Operand isPositive = context.ICompareGreaterOrEqual(op1, zeroL);
  1312. context.Copy(res, context.ConditionalSelect(isPositive, maxL, minL));
  1313. SetFpFlag(context, FPState.QcFlag, Const(1));
  1314. context.Branch(lblEnd);
  1315. context.MarkLabel(lblEnd);
  1316. return res;
  1317. }
  1318. // ulong BinaryUnsignedSatQSub(ulong op1, ulong op2);
  1319. public static Operand EmitBinaryUnsignedSatQSub(ArmEmitterContext context, Operand op1, Operand op2)
  1320. {
  1321. Debug.Assert(op1.Type == OperandType.I64 && op2.Type == OperandType.I64);
  1322. Operand lblEnd = Label();
  1323. Operand zeroL = Const(0L);
  1324. Operand sub = context.Subtract(op1, op2);
  1325. Operand res = context.Copy(context.AllocateLocal(OperandType.I64), sub);
  1326. context.BranchIf(lblEnd, op1, op2, Comparison.GreaterOrEqualUI);
  1327. context.Copy(res, zeroL);
  1328. SetFpFlag(context, FPState.QcFlag, Const(1));
  1329. context.Branch(lblEnd);
  1330. context.MarkLabel(lblEnd);
  1331. return res;
  1332. }
  1333. // long BinarySignedSatQAcc(ulong op1, long op2);
  1334. private static Operand EmitBinarySignedSatQAcc(ArmEmitterContext context, Operand op1, Operand op2)
  1335. {
  1336. Debug.Assert(op1.Type == OperandType.I64 && op2.Type == OperandType.I64);
  1337. Operand lbl1 = Label();
  1338. Operand lbl2 = Label();
  1339. Operand lblEnd = Label();
  1340. Operand maxL = Const(long.MaxValue);
  1341. Operand zeroL = Const(0L);
  1342. Operand add = context.Add(op1, op2);
  1343. Operand res = context.Copy(context.AllocateLocal(OperandType.I64), add);
  1344. context.BranchIf(lbl1, op1, maxL, Comparison.GreaterUI);
  1345. Operand notOp2AndRes = context.BitwiseAnd(context.BitwiseNot(op2), add);
  1346. context.BranchIf(lblEnd, notOp2AndRes, zeroL, Comparison.GreaterOrEqual);
  1347. context.Copy(res, maxL);
  1348. SetFpFlag(context, FPState.QcFlag, Const(1));
  1349. context.Branch(lblEnd);
  1350. context.MarkLabel(lbl1);
  1351. context.BranchIf(lbl2, op2, zeroL, Comparison.Less);
  1352. context.Copy(res, maxL);
  1353. SetFpFlag(context, FPState.QcFlag, Const(1));
  1354. context.Branch(lblEnd);
  1355. context.MarkLabel(lbl2);
  1356. context.BranchIf(lblEnd, add, maxL, Comparison.LessOrEqualUI);
  1357. context.Copy(res, maxL);
  1358. SetFpFlag(context, FPState.QcFlag, Const(1));
  1359. context.Branch(lblEnd);
  1360. context.MarkLabel(lblEnd);
  1361. return res;
  1362. }
  1363. // ulong BinaryUnsignedSatQAcc(long op1, ulong op2);
  1364. private static Operand EmitBinaryUnsignedSatQAcc(ArmEmitterContext context, Operand op1, Operand op2)
  1365. {
  1366. Debug.Assert(op1.Type == OperandType.I64 && op2.Type == OperandType.I64);
  1367. Operand lbl1 = Label();
  1368. Operand lblEnd = Label();
  1369. Operand maxUL = Const(ulong.MaxValue);
  1370. Operand maxL = Const(long.MaxValue);
  1371. Operand zeroL = Const(0L);
  1372. Operand add = context.Add(op1, op2);
  1373. Operand res = context.Copy(context.AllocateLocal(OperandType.I64), add);
  1374. context.BranchIf(lbl1, op1, zeroL, Comparison.Less);
  1375. context.BranchIf(lblEnd, add, op1, Comparison.GreaterOrEqualUI);
  1376. context.Copy(res, maxUL);
  1377. SetFpFlag(context, FPState.QcFlag, Const(1));
  1378. context.Branch(lblEnd);
  1379. context.MarkLabel(lbl1);
  1380. context.BranchIf(lblEnd, op2, maxL, Comparison.GreaterUI);
  1381. context.BranchIf(lblEnd, add, zeroL, Comparison.GreaterOrEqual);
  1382. context.Copy(res, zeroL);
  1383. SetFpFlag(context, FPState.QcFlag, Const(1));
  1384. context.Branch(lblEnd);
  1385. context.MarkLabel(lblEnd);
  1386. return res;
  1387. }
  1388. public static Operand EmitFloatAbs(ArmEmitterContext context, Operand value, bool single, bool vector)
  1389. {
  1390. Operand mask;
  1391. if (single)
  1392. {
  1393. mask = vector ? X86GetAllElements(context, -0f) : X86GetScalar(context, -0f);
  1394. }
  1395. else
  1396. {
  1397. mask = vector ? X86GetAllElements(context, -0d) : X86GetScalar(context, -0d);
  1398. }
  1399. return context.AddIntrinsic(single ? Intrinsic.X86Andnps : Intrinsic.X86Andnpd, mask, value);
  1400. }
  1401. public static Operand EmitVectorExtractSx(ArmEmitterContext context, int reg, int index, int size)
  1402. {
  1403. return EmitVectorExtract(context, reg, index, size, true);
  1404. }
  1405. public static Operand EmitVectorExtractZx(ArmEmitterContext context, int reg, int index, int size)
  1406. {
  1407. return EmitVectorExtract(context, reg, index, size, false);
  1408. }
  1409. public static Operand EmitVectorExtract(ArmEmitterContext context, int reg, int index, int size, bool signed)
  1410. {
  1411. ThrowIfInvalid(index, size);
  1412. Operand res = default;
  1413. switch (size)
  1414. {
  1415. case 0:
  1416. res = context.VectorExtract8(GetVec(reg), index);
  1417. break;
  1418. case 1:
  1419. res = context.VectorExtract16(GetVec(reg), index);
  1420. break;
  1421. case 2:
  1422. res = context.VectorExtract(OperandType.I32, GetVec(reg), index);
  1423. break;
  1424. case 3:
  1425. res = context.VectorExtract(OperandType.I64, GetVec(reg), index);
  1426. break;
  1427. }
  1428. if (signed)
  1429. {
  1430. switch (size)
  1431. {
  1432. case 0: res = context.SignExtend8 (OperandType.I64, res); break;
  1433. case 1: res = context.SignExtend16(OperandType.I64, res); break;
  1434. case 2: res = context.SignExtend32(OperandType.I64, res); break;
  1435. }
  1436. }
  1437. else
  1438. {
  1439. switch (size)
  1440. {
  1441. case 0: res = context.ZeroExtend8 (OperandType.I64, res); break;
  1442. case 1: res = context.ZeroExtend16(OperandType.I64, res); break;
  1443. case 2: res = context.ZeroExtend32(OperandType.I64, res); break;
  1444. }
  1445. }
  1446. return res;
  1447. }
  1448. public static Operand EmitVectorInsert(ArmEmitterContext context, Operand vector, Operand value, int index, int size)
  1449. {
  1450. ThrowIfInvalid(index, size);
  1451. if (size < 3 && value.Type == OperandType.I64)
  1452. {
  1453. value = context.ConvertI64ToI32(value);
  1454. }
  1455. switch (size)
  1456. {
  1457. case 0: vector = context.VectorInsert8 (vector, value, index); break;
  1458. case 1: vector = context.VectorInsert16(vector, value, index); break;
  1459. case 2: vector = context.VectorInsert (vector, value, index); break;
  1460. case 3: vector = context.VectorInsert (vector, value, index); break;
  1461. }
  1462. return vector;
  1463. }
  1464. public static void ThrowIfInvalid(int index, int size)
  1465. {
  1466. if ((uint)size > 3u)
  1467. {
  1468. throw new ArgumentOutOfRangeException(nameof(size));
  1469. }
  1470. if ((uint)index >= 16u >> size)
  1471. {
  1472. throw new ArgumentOutOfRangeException(nameof(index));
  1473. }
  1474. }
  1475. }
  1476. }