InstEmitSimdHelper.cs 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885
  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 Operand EmitCountSetBits8(ArmEmitterContext context, Operand op) // "size" is 8 (SIMD&FP Inst.).
  205. {
  206. Debug.Assert(op.Type == OperandType.I32 || op.Type == OperandType.I64);
  207. Operand op0 = context.Subtract(op, context.BitwiseAnd(context.ShiftRightUI(op, Const(1)), Const(op.Type, 0x55L)));
  208. Operand c1 = Const(op.Type, 0x33L);
  209. Operand op1 = context.Add(context.BitwiseAnd(context.ShiftRightUI(op0, Const(2)), c1), context.BitwiseAnd(op0, c1));
  210. return context.BitwiseAnd(context.Add(op1, context.ShiftRightUI(op1, Const(4))), Const(op.Type, 0x0fL));
  211. }
  212. public static void EmitScalarUnaryOpF(ArmEmitterContext context, Intrinsic inst32, Intrinsic inst64)
  213. {
  214. OpCodeSimd op = (OpCodeSimd)context.CurrOp;
  215. Operand n = GetVec(op.Rn);
  216. Intrinsic inst = (op.Size & 1) != 0 ? inst64 : inst32;
  217. Operand res = context.AddIntrinsic(inst, n);
  218. if ((op.Size & 1) != 0)
  219. {
  220. res = context.VectorZeroUpper64(res);
  221. }
  222. else
  223. {
  224. res = context.VectorZeroUpper96(res);
  225. }
  226. context.Copy(GetVec(op.Rd), res);
  227. }
  228. public static void EmitScalarBinaryOpF(ArmEmitterContext context, Intrinsic inst32, Intrinsic inst64)
  229. {
  230. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  231. Operand n = GetVec(op.Rn);
  232. Operand m = GetVec(op.Rm);
  233. Intrinsic inst = (op.Size & 1) != 0 ? inst64 : inst32;
  234. Operand res = context.AddIntrinsic(inst, n, m);
  235. if ((op.Size & 1) != 0)
  236. {
  237. res = context.VectorZeroUpper64(res);
  238. }
  239. else
  240. {
  241. res = context.VectorZeroUpper96(res);
  242. }
  243. context.Copy(GetVec(op.Rd), res);
  244. }
  245. public static void EmitVectorUnaryOpF(ArmEmitterContext context, Intrinsic inst32, Intrinsic inst64)
  246. {
  247. OpCodeSimd op = (OpCodeSimd)context.CurrOp;
  248. Operand n = GetVec(op.Rn);
  249. Intrinsic inst = (op.Size & 1) != 0 ? inst64 : inst32;
  250. Operand res = context.AddIntrinsic(inst, n);
  251. if (op.RegisterSize == RegisterSize.Simd64)
  252. {
  253. res = context.VectorZeroUpper64(res);
  254. }
  255. context.Copy(GetVec(op.Rd), res);
  256. }
  257. public static void EmitVectorBinaryOpF(ArmEmitterContext context, Intrinsic inst32, Intrinsic inst64)
  258. {
  259. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  260. Operand n = GetVec(op.Rn);
  261. Operand m = GetVec(op.Rm);
  262. Intrinsic inst = (op.Size & 1) != 0 ? inst64 : inst32;
  263. Operand res = context.AddIntrinsic(inst, n, m);
  264. if (op.RegisterSize == RegisterSize.Simd64)
  265. {
  266. res = context.VectorZeroUpper64(res);
  267. }
  268. context.Copy(GetVec(op.Rd), res);
  269. }
  270. public static Operand EmitUnaryMathCall(ArmEmitterContext context, string name, Operand n)
  271. {
  272. IOpCodeSimd op = (IOpCodeSimd)context.CurrOp;
  273. MethodInfo info = (op.Size & 1) == 0
  274. ? typeof(MathF).GetMethod(name, new Type[] { typeof(float) })
  275. : typeof(Math). GetMethod(name, new Type[] { typeof(double) });
  276. return context.Call(info, n);
  277. }
  278. public static Operand EmitRoundMathCall(ArmEmitterContext context, MidpointRounding roundMode, Operand n)
  279. {
  280. IOpCodeSimd op = (IOpCodeSimd)context.CurrOp;
  281. string name = nameof(Math.Round);
  282. MethodInfo info = (op.Size & 1) == 0
  283. ? typeof(MathF).GetMethod(name, new Type[] { typeof(float), typeof(MidpointRounding) })
  284. : typeof(Math). GetMethod(name, new Type[] { typeof(double), typeof(MidpointRounding) });
  285. return context.Call(info, n, Const((int)roundMode));
  286. }
  287. public static Operand EmitSoftFloatCall(ArmEmitterContext context, string name, params Operand[] callArgs)
  288. {
  289. IOpCodeSimd op = (IOpCodeSimd)context.CurrOp;
  290. MethodInfo info = (op.Size & 1) == 0
  291. ? typeof(SoftFloat32).GetMethod(name)
  292. : typeof(SoftFloat64).GetMethod(name);
  293. return context.Call(info, callArgs);
  294. }
  295. public static void EmitScalarBinaryOpByElemF(ArmEmitterContext context, Func2I emit)
  296. {
  297. OpCodeSimdRegElemF op = (OpCodeSimdRegElemF)context.CurrOp;
  298. OperandType type = (op.Size & 1) != 0 ? OperandType.FP64 : OperandType.FP32;
  299. Operand n = context.VectorExtract(type, GetVec(op.Rn), 0);
  300. Operand m = context.VectorExtract(type, GetVec(op.Rm), op.Index);
  301. context.Copy(GetVec(op.Rd), context.VectorInsert(context.VectorZero(), emit(n, m), 0));
  302. }
  303. public static void EmitScalarTernaryOpByElemF(ArmEmitterContext context, Func3I emit)
  304. {
  305. OpCodeSimdRegElemF op = (OpCodeSimdRegElemF)context.CurrOp;
  306. OperandType type = (op.Size & 1) != 0 ? OperandType.FP64 : OperandType.FP32;
  307. Operand d = context.VectorExtract(type, GetVec(op.Rd), 0);
  308. Operand n = context.VectorExtract(type, GetVec(op.Rn), 0);
  309. Operand m = context.VectorExtract(type, GetVec(op.Rm), op.Index);
  310. context.Copy(GetVec(op.Rd), context.VectorInsert(context.VectorZero(), emit(d, n, m), 0));
  311. }
  312. public static void EmitScalarUnaryOpSx(ArmEmitterContext context, Func1I emit)
  313. {
  314. OpCodeSimd op = (OpCodeSimd)context.CurrOp;
  315. Operand n = EmitVectorExtractSx(context, op.Rn, 0, op.Size);
  316. Operand d = EmitVectorInsert(context, context.VectorZero(), emit(n), 0, op.Size);
  317. context.Copy(GetVec(op.Rd), d);
  318. }
  319. public static void EmitScalarBinaryOpSx(ArmEmitterContext context, Func2I emit)
  320. {
  321. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  322. Operand n = EmitVectorExtractSx(context, op.Rn, 0, op.Size);
  323. Operand m = EmitVectorExtractSx(context, op.Rm, 0, op.Size);
  324. Operand d = EmitVectorInsert(context, context.VectorZero(), emit(n, m), 0, op.Size);
  325. context.Copy(GetVec(op.Rd), d);
  326. }
  327. public static void EmitScalarUnaryOpZx(ArmEmitterContext context, Func1I emit)
  328. {
  329. OpCodeSimd op = (OpCodeSimd)context.CurrOp;
  330. Operand n = EmitVectorExtractZx(context, op.Rn, 0, op.Size);
  331. Operand d = EmitVectorInsert(context, context.VectorZero(), emit(n), 0, op.Size);
  332. context.Copy(GetVec(op.Rd), d);
  333. }
  334. public static void EmitScalarBinaryOpZx(ArmEmitterContext context, Func2I emit)
  335. {
  336. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  337. Operand n = EmitVectorExtractZx(context, op.Rn, 0, op.Size);
  338. Operand m = EmitVectorExtractZx(context, op.Rm, 0, op.Size);
  339. Operand d = EmitVectorInsert(context, context.VectorZero(), emit(n, m), 0, op.Size);
  340. context.Copy(GetVec(op.Rd), d);
  341. }
  342. public static void EmitScalarTernaryOpZx(ArmEmitterContext context, Func3I emit)
  343. {
  344. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  345. Operand d = EmitVectorExtractZx(context, op.Rd, 0, op.Size);
  346. Operand n = EmitVectorExtractZx(context, op.Rn, 0, op.Size);
  347. Operand m = EmitVectorExtractZx(context, op.Rm, 0, op.Size);
  348. d = EmitVectorInsert(context, context.VectorZero(), emit(d, n, m), 0, op.Size);
  349. context.Copy(GetVec(op.Rd), d);
  350. }
  351. public static void EmitScalarUnaryOpF(ArmEmitterContext context, Func1I emit)
  352. {
  353. OpCodeSimd op = (OpCodeSimd)context.CurrOp;
  354. OperandType type = (op.Size & 1) != 0 ? OperandType.FP64 : OperandType.FP32;
  355. Operand n = context.VectorExtract(type, GetVec(op.Rn), 0);
  356. context.Copy(GetVec(op.Rd), context.VectorInsert(context.VectorZero(), emit(n), 0));
  357. }
  358. public static void EmitScalarBinaryOpF(ArmEmitterContext context, Func2I emit)
  359. {
  360. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  361. OperandType type = (op.Size & 1) != 0 ? OperandType.FP64 : OperandType.FP32;
  362. Operand n = context.VectorExtract(type, GetVec(op.Rn), 0);
  363. Operand m = context.VectorExtract(type, GetVec(op.Rm), 0);
  364. context.Copy(GetVec(op.Rd), context.VectorInsert(context.VectorZero(), emit(n, m), 0));
  365. }
  366. public static void EmitScalarTernaryRaOpF(ArmEmitterContext context, Func3I emit)
  367. {
  368. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  369. OperandType type = (op.Size & 1) != 0 ? OperandType.FP64 : OperandType.FP32;
  370. Operand a = context.VectorExtract(type, GetVec(op.Ra), 0);
  371. Operand n = context.VectorExtract(type, GetVec(op.Rn), 0);
  372. Operand m = context.VectorExtract(type, GetVec(op.Rm), 0);
  373. context.Copy(GetVec(op.Rd), context.VectorInsert(context.VectorZero(), emit(a, n, m), 0));
  374. }
  375. public static void EmitVectorUnaryOpF(ArmEmitterContext context, Func1I emit)
  376. {
  377. OpCodeSimd op = (OpCodeSimd)context.CurrOp;
  378. Operand res = context.VectorZero();
  379. int sizeF = op.Size & 1;
  380. OperandType type = sizeF != 0 ? OperandType.FP64 : OperandType.FP32;
  381. int elems = op.GetBytesCount() >> sizeF + 2;
  382. for (int index = 0; index < elems; index++)
  383. {
  384. Operand ne = context.VectorExtract(type, GetVec(op.Rn), index);
  385. res = context.VectorInsert(res, emit(ne), index);
  386. }
  387. context.Copy(GetVec(op.Rd), res);
  388. }
  389. public static void EmitVectorBinaryOpF(ArmEmitterContext context, Func2I emit)
  390. {
  391. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  392. Operand res = context.VectorZero();
  393. int sizeF = op.Size & 1;
  394. OperandType type = sizeF != 0 ? OperandType.FP64 : OperandType.FP32;
  395. int elems = op.GetBytesCount() >> sizeF + 2;
  396. for (int index = 0; index < elems; index++)
  397. {
  398. Operand ne = context.VectorExtract(type, GetVec(op.Rn), index);
  399. Operand me = context.VectorExtract(type, GetVec(op.Rm), index);
  400. res = context.VectorInsert(res, emit(ne, me), index);
  401. }
  402. context.Copy(GetVec(op.Rd), res);
  403. }
  404. public static void EmitVectorTernaryOpF(ArmEmitterContext context, Func3I emit)
  405. {
  406. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  407. Operand res = context.VectorZero();
  408. int sizeF = op.Size & 1;
  409. OperandType type = sizeF != 0 ? OperandType.FP64 : OperandType.FP32;
  410. int elems = op.GetBytesCount() >> sizeF + 2;
  411. for (int index = 0; index < elems; index++)
  412. {
  413. Operand de = context.VectorExtract(type, GetVec(op.Rd), index);
  414. Operand ne = context.VectorExtract(type, GetVec(op.Rn), index);
  415. Operand me = context.VectorExtract(type, GetVec(op.Rm), index);
  416. res = context.VectorInsert(res, emit(de, ne, me), index);
  417. }
  418. context.Copy(GetVec(op.Rd), res);
  419. }
  420. public static void EmitVectorBinaryOpByElemF(ArmEmitterContext context, Func2I emit)
  421. {
  422. OpCodeSimdRegElemF op = (OpCodeSimdRegElemF)context.CurrOp;
  423. Operand res = context.VectorZero();
  424. int sizeF = op.Size & 1;
  425. OperandType type = sizeF != 0 ? OperandType.FP64 : OperandType.FP32;
  426. int elems = op.GetBytesCount() >> sizeF + 2;
  427. for (int index = 0; index < elems; index++)
  428. {
  429. Operand ne = context.VectorExtract(type, GetVec(op.Rn), index);
  430. Operand me = context.VectorExtract(type, GetVec(op.Rm), op.Index);
  431. res = context.VectorInsert(res, emit(ne, me), index);
  432. }
  433. context.Copy(GetVec(op.Rd), res);
  434. }
  435. public static void EmitVectorTernaryOpByElemF(ArmEmitterContext context, Func3I emit)
  436. {
  437. OpCodeSimdRegElemF op = (OpCodeSimdRegElemF)context.CurrOp;
  438. Operand res = context.VectorZero();
  439. int sizeF = op.Size & 1;
  440. OperandType type = sizeF != 0 ? OperandType.FP64 : OperandType.FP32;
  441. int elems = op.GetBytesCount() >> sizeF + 2;
  442. for (int index = 0; index < elems; index++)
  443. {
  444. Operand de = context.VectorExtract(type, GetVec(op.Rd), index);
  445. Operand ne = context.VectorExtract(type, GetVec(op.Rn), index);
  446. Operand me = context.VectorExtract(type, GetVec(op.Rm), op.Index);
  447. res = context.VectorInsert(res, emit(de, ne, me), index);
  448. }
  449. context.Copy(GetVec(op.Rd), res);
  450. }
  451. public static void EmitVectorUnaryOpSx(ArmEmitterContext context, Func1I emit)
  452. {
  453. OpCodeSimd op = (OpCodeSimd)context.CurrOp;
  454. Operand res = context.VectorZero();
  455. int elems = op.GetBytesCount() >> op.Size;
  456. for (int index = 0; index < elems; index++)
  457. {
  458. Operand ne = EmitVectorExtractSx(context, op.Rn, index, op.Size);
  459. res = EmitVectorInsert(context, res, emit(ne), index, op.Size);
  460. }
  461. context.Copy(GetVec(op.Rd), res);
  462. }
  463. public static void EmitVectorBinaryOpSx(ArmEmitterContext context, Func2I emit)
  464. {
  465. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  466. Operand res = context.VectorZero();
  467. int elems = op.GetBytesCount() >> op.Size;
  468. for (int index = 0; index < elems; index++)
  469. {
  470. Operand ne = EmitVectorExtractSx(context, op.Rn, index, op.Size);
  471. Operand me = EmitVectorExtractSx(context, op.Rm, index, op.Size);
  472. res = EmitVectorInsert(context, res, emit(ne, me), index, op.Size);
  473. }
  474. context.Copy(GetVec(op.Rd), res);
  475. }
  476. public static void EmitVectorTernaryOpSx(ArmEmitterContext context, Func3I emit)
  477. {
  478. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  479. Operand res = context.VectorZero();
  480. int elems = op.GetBytesCount() >> op.Size;
  481. for (int index = 0; index < elems; index++)
  482. {
  483. Operand de = EmitVectorExtractSx(context, op.Rd, index, op.Size);
  484. Operand ne = EmitVectorExtractSx(context, op.Rn, index, op.Size);
  485. Operand me = EmitVectorExtractSx(context, op.Rm, index, op.Size);
  486. res = EmitVectorInsert(context, res, emit(de, ne, me), index, op.Size);
  487. }
  488. context.Copy(GetVec(op.Rd), res);
  489. }
  490. public static void EmitVectorUnaryOpZx(ArmEmitterContext context, Func1I emit)
  491. {
  492. OpCodeSimd op = (OpCodeSimd)context.CurrOp;
  493. Operand res = context.VectorZero();
  494. int elems = op.GetBytesCount() >> op.Size;
  495. for (int index = 0; index < elems; index++)
  496. {
  497. Operand ne = EmitVectorExtractZx(context, op.Rn, index, op.Size);
  498. res = EmitVectorInsert(context, res, emit(ne), index, op.Size);
  499. }
  500. context.Copy(GetVec(op.Rd), res);
  501. }
  502. public static void EmitVectorBinaryOpZx(ArmEmitterContext context, Func2I emit)
  503. {
  504. OpCodeSimdReg op = (OpCodeSimdReg)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 = EmitVectorExtractZx(context, op.Rn, index, op.Size);
  510. Operand me = EmitVectorExtractZx(context, op.Rm, index, op.Size);
  511. res = EmitVectorInsert(context, res, emit(ne, me), index, op.Size);
  512. }
  513. context.Copy(GetVec(op.Rd), res);
  514. }
  515. public static void EmitVectorTernaryOpZx(ArmEmitterContext context, Func3I emit)
  516. {
  517. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  518. Operand res = context.VectorZero();
  519. int elems = op.GetBytesCount() >> op.Size;
  520. for (int index = 0; index < elems; index++)
  521. {
  522. Operand de = EmitVectorExtractZx(context, op.Rd, index, op.Size);
  523. Operand ne = EmitVectorExtractZx(context, op.Rn, index, op.Size);
  524. Operand me = EmitVectorExtractZx(context, op.Rm, index, op.Size);
  525. res = EmitVectorInsert(context, res, emit(de, ne, me), index, op.Size);
  526. }
  527. context.Copy(GetVec(op.Rd), res);
  528. }
  529. public static void EmitVectorBinaryOpByElemSx(ArmEmitterContext context, Func2I emit)
  530. {
  531. OpCodeSimdRegElem op = (OpCodeSimdRegElem)context.CurrOp;
  532. Operand res = context.VectorZero();
  533. Operand me = EmitVectorExtractSx(context, op.Rm, op.Index, op.Size);
  534. int elems = op.GetBytesCount() >> op.Size;
  535. for (int index = 0; index < elems; index++)
  536. {
  537. Operand ne = EmitVectorExtractSx(context, op.Rn, index, op.Size);
  538. res = EmitVectorInsert(context, res, emit(ne, me), index, op.Size);
  539. }
  540. context.Copy(GetVec(op.Rd), res);
  541. }
  542. public static void EmitVectorBinaryOpByElemZx(ArmEmitterContext context, Func2I emit)
  543. {
  544. OpCodeSimdRegElem op = (OpCodeSimdRegElem)context.CurrOp;
  545. Operand res = context.VectorZero();
  546. Operand me = EmitVectorExtractZx(context, op.Rm, op.Index, op.Size);
  547. int elems = op.GetBytesCount() >> op.Size;
  548. for (int index = 0; index < elems; index++)
  549. {
  550. Operand ne = EmitVectorExtractZx(context, op.Rn, index, op.Size);
  551. res = EmitVectorInsert(context, res, emit(ne, me), index, op.Size);
  552. }
  553. context.Copy(GetVec(op.Rd), res);
  554. }
  555. public static void EmitVectorTernaryOpByElemZx(ArmEmitterContext context, Func3I emit)
  556. {
  557. OpCodeSimdRegElem op = (OpCodeSimdRegElem)context.CurrOp;
  558. Operand res = context.VectorZero();
  559. Operand me = EmitVectorExtractZx(context, op.Rm, op.Index, op.Size);
  560. int elems = op.GetBytesCount() >> op.Size;
  561. for (int index = 0; index < elems; index++)
  562. {
  563. Operand de = EmitVectorExtractZx(context, op.Rd, index, op.Size);
  564. Operand ne = EmitVectorExtractZx(context, op.Rn, index, op.Size);
  565. res = EmitVectorInsert(context, res, emit(de, ne, me), index, op.Size);
  566. }
  567. context.Copy(GetVec(op.Rd), res);
  568. }
  569. public static void EmitVectorImmUnaryOp(ArmEmitterContext context, Func1I emit)
  570. {
  571. OpCodeSimdImm op = (OpCodeSimdImm)context.CurrOp;
  572. Operand imm = Const(op.Immediate);
  573. Operand res = context.VectorZero();
  574. int elems = op.GetBytesCount() >> op.Size;
  575. for (int index = 0; index < elems; index++)
  576. {
  577. res = EmitVectorInsert(context, res, emit(imm), index, op.Size);
  578. }
  579. context.Copy(GetVec(op.Rd), res);
  580. }
  581. public static void EmitVectorImmBinaryOp(ArmEmitterContext context, Func2I emit)
  582. {
  583. OpCodeSimdImm op = (OpCodeSimdImm)context.CurrOp;
  584. Operand imm = Const(op.Immediate);
  585. Operand res = context.VectorZero();
  586. int elems = op.GetBytesCount() >> op.Size;
  587. for (int index = 0; index < elems; index++)
  588. {
  589. Operand de = EmitVectorExtractZx(context, op.Rd, index, op.Size);
  590. res = EmitVectorInsert(context, res, emit(de, imm), index, op.Size);
  591. }
  592. context.Copy(GetVec(op.Rd), res);
  593. }
  594. public static void EmitVectorWidenRmBinaryOpSx(ArmEmitterContext context, Func2I emit)
  595. {
  596. EmitVectorWidenRmBinaryOp(context, emit, signed: true);
  597. }
  598. public static void EmitVectorWidenRmBinaryOpZx(ArmEmitterContext context, Func2I emit)
  599. {
  600. EmitVectorWidenRmBinaryOp(context, emit, signed: false);
  601. }
  602. private static void EmitVectorWidenRmBinaryOp(ArmEmitterContext context, Func2I emit, bool signed)
  603. {
  604. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  605. Operand res = context.VectorZero();
  606. int elems = 8 >> op.Size;
  607. int part = op.RegisterSize == RegisterSize.Simd128 ? elems : 0;
  608. for (int index = 0; index < elems; index++)
  609. {
  610. Operand ne = EmitVectorExtract(context, op.Rn, index, op.Size + 1, signed);
  611. Operand me = EmitVectorExtract(context, op.Rm, part + index, op.Size, signed);
  612. res = EmitVectorInsert(context, res, emit(ne, me), index, op.Size + 1);
  613. }
  614. context.Copy(GetVec(op.Rd), res);
  615. }
  616. public static void EmitVectorWidenRnRmBinaryOpSx(ArmEmitterContext context, Func2I emit)
  617. {
  618. EmitVectorWidenRnRmBinaryOp(context, emit, signed: true);
  619. }
  620. public static void EmitVectorWidenRnRmBinaryOpZx(ArmEmitterContext context, Func2I emit)
  621. {
  622. EmitVectorWidenRnRmBinaryOp(context, emit, signed: false);
  623. }
  624. private static void EmitVectorWidenRnRmBinaryOp(ArmEmitterContext context, Func2I emit, bool signed)
  625. {
  626. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  627. Operand res = context.VectorZero();
  628. int elems = 8 >> op.Size;
  629. int part = op.RegisterSize == RegisterSize.Simd128 ? elems : 0;
  630. for (int index = 0; index < elems; index++)
  631. {
  632. Operand ne = EmitVectorExtract(context, op.Rn, part + index, op.Size, signed);
  633. Operand me = EmitVectorExtract(context, op.Rm, part + index, op.Size, signed);
  634. res = EmitVectorInsert(context, res, emit(ne, me), index, op.Size + 1);
  635. }
  636. context.Copy(GetVec(op.Rd), res);
  637. }
  638. public static void EmitVectorWidenRnRmTernaryOpSx(ArmEmitterContext context, Func3I emit)
  639. {
  640. EmitVectorWidenRnRmTernaryOp(context, emit, signed: true);
  641. }
  642. public static void EmitVectorWidenRnRmTernaryOpZx(ArmEmitterContext context, Func3I emit)
  643. {
  644. EmitVectorWidenRnRmTernaryOp(context, emit, signed: false);
  645. }
  646. private static void EmitVectorWidenRnRmTernaryOp(ArmEmitterContext context, Func3I emit, bool signed)
  647. {
  648. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  649. Operand res = context.VectorZero();
  650. int elems = 8 >> op.Size;
  651. int part = op.RegisterSize == RegisterSize.Simd128 ? elems : 0;
  652. for (int index = 0; index < elems; index++)
  653. {
  654. Operand de = EmitVectorExtract(context, op.Rd, index, op.Size + 1, signed);
  655. Operand ne = EmitVectorExtract(context, op.Rn, part + index, op.Size, signed);
  656. Operand me = EmitVectorExtract(context, op.Rm, part + index, op.Size, signed);
  657. res = EmitVectorInsert(context, res, emit(de, ne, me), index, op.Size + 1);
  658. }
  659. context.Copy(GetVec(op.Rd), res);
  660. }
  661. public static void EmitVectorWidenBinaryOpByElemSx(ArmEmitterContext context, Func2I emit)
  662. {
  663. EmitVectorWidenBinaryOpByElem(context, emit, signed: true);
  664. }
  665. public static void EmitVectorWidenBinaryOpByElemZx(ArmEmitterContext context, Func2I emit)
  666. {
  667. EmitVectorWidenBinaryOpByElem(context, emit, signed: false);
  668. }
  669. private static void EmitVectorWidenBinaryOpByElem(ArmEmitterContext context, Func2I emit, bool signed)
  670. {
  671. OpCodeSimdRegElem op = (OpCodeSimdRegElem)context.CurrOp;
  672. Operand res = context.VectorZero();
  673. Operand me = EmitVectorExtract(context, op.Rm, op.Index, op.Size, signed);
  674. int elems = 8 >> op.Size;
  675. int part = op.RegisterSize == RegisterSize.Simd128 ? elems : 0;
  676. for (int index = 0; index < elems; index++)
  677. {
  678. Operand ne = EmitVectorExtract(context, op.Rn, part + index, op.Size, signed);
  679. res = EmitVectorInsert(context, res, emit(ne, me), index, op.Size + 1);
  680. }
  681. context.Copy(GetVec(op.Rd), res);
  682. }
  683. public static void EmitVectorWidenTernaryOpByElemSx(ArmEmitterContext context, Func3I emit)
  684. {
  685. EmitVectorWidenTernaryOpByElem(context, emit, signed: true);
  686. }
  687. public static void EmitVectorWidenTernaryOpByElemZx(ArmEmitterContext context, Func3I emit)
  688. {
  689. EmitVectorWidenTernaryOpByElem(context, emit, signed: false);
  690. }
  691. private static void EmitVectorWidenTernaryOpByElem(ArmEmitterContext context, Func3I emit, bool signed)
  692. {
  693. OpCodeSimdRegElem op = (OpCodeSimdRegElem)context.CurrOp;
  694. Operand res = context.VectorZero();
  695. Operand me = EmitVectorExtract(context, op.Rm, op.Index, op.Size, signed);
  696. int elems = 8 >> op.Size;
  697. int part = op.RegisterSize == RegisterSize.Simd128 ? elems : 0;
  698. for (int index = 0; index < elems; index++)
  699. {
  700. Operand de = EmitVectorExtract(context, op.Rd, index, op.Size + 1, signed);
  701. Operand ne = EmitVectorExtract(context, op.Rn, part + index, op.Size, signed);
  702. res = EmitVectorInsert(context, res, emit(de, ne, me), index, op.Size + 1);
  703. }
  704. context.Copy(GetVec(op.Rd), res);
  705. }
  706. public static void EmitVectorPairwiseOpSx(ArmEmitterContext context, Func2I emit)
  707. {
  708. EmitVectorPairwiseOp(context, emit, signed: true);
  709. }
  710. public static void EmitVectorPairwiseOpZx(ArmEmitterContext context, Func2I emit)
  711. {
  712. EmitVectorPairwiseOp(context, emit, signed: false);
  713. }
  714. private static void EmitVectorPairwiseOp(ArmEmitterContext context, Func2I emit, bool signed)
  715. {
  716. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  717. Operand res = context.VectorZero();
  718. int pairs = op.GetPairsCount() >> op.Size;
  719. for (int index = 0; index < pairs; index++)
  720. {
  721. int pairIndex = index << 1;
  722. Operand n0 = EmitVectorExtract(context, op.Rn, pairIndex, op.Size, signed);
  723. Operand n1 = EmitVectorExtract(context, op.Rn, pairIndex + 1, op.Size, signed);
  724. Operand m0 = EmitVectorExtract(context, op.Rm, pairIndex, op.Size, signed);
  725. Operand m1 = EmitVectorExtract(context, op.Rm, pairIndex + 1, op.Size, signed);
  726. res = EmitVectorInsert(context, res, emit(n0, n1), index, op.Size);
  727. res = EmitVectorInsert(context, res, emit(m0, m1), pairs + index, op.Size);
  728. }
  729. context.Copy(GetVec(op.Rd), res);
  730. }
  731. public static void EmitSsse3VectorPairwiseOp(ArmEmitterContext context, Intrinsic[] inst)
  732. {
  733. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  734. Operand n = GetVec(op.Rn);
  735. Operand m = GetVec(op.Rm);
  736. if (op.RegisterSize == RegisterSize.Simd64)
  737. {
  738. Operand zeroEvenMask = X86GetElements(context, ZeroMask, EvenMasks[op.Size]);
  739. Operand zeroOddMask = X86GetElements(context, ZeroMask, OddMasks [op.Size]);
  740. Operand mN = context.AddIntrinsic(Intrinsic.X86Punpcklqdq, n, m); // m:n
  741. Operand left = context.AddIntrinsic(Intrinsic.X86Pshufb, mN, zeroEvenMask); // 0:even from m:n
  742. Operand right = context.AddIntrinsic(Intrinsic.X86Pshufb, mN, zeroOddMask); // 0:odd from m:n
  743. context.Copy(GetVec(op.Rd), context.AddIntrinsic(inst[op.Size], left, right));
  744. }
  745. else if (op.Size < 3)
  746. {
  747. Operand oddEvenMask = X86GetElements(context, OddMasks[op.Size], EvenMasks[op.Size]);
  748. Operand oddEvenN = context.AddIntrinsic(Intrinsic.X86Pshufb, n, oddEvenMask); // odd:even from n
  749. Operand oddEvenM = context.AddIntrinsic(Intrinsic.X86Pshufb, m, oddEvenMask); // odd:even from m
  750. Operand left = context.AddIntrinsic(Intrinsic.X86Punpcklqdq, oddEvenN, oddEvenM);
  751. Operand right = context.AddIntrinsic(Intrinsic.X86Punpckhqdq, oddEvenN, oddEvenM);
  752. context.Copy(GetVec(op.Rd), context.AddIntrinsic(inst[op.Size], left, right));
  753. }
  754. else
  755. {
  756. Operand left = context.AddIntrinsic(Intrinsic.X86Punpcklqdq, n, m);
  757. Operand right = context.AddIntrinsic(Intrinsic.X86Punpckhqdq, n, m);
  758. context.Copy(GetVec(op.Rd), context.AddIntrinsic(inst[3], left, right));
  759. }
  760. }
  761. public static void EmitVectorAcrossVectorOpSx(ArmEmitterContext context, Func2I emit)
  762. {
  763. EmitVectorAcrossVectorOp(context, emit, signed: true, isLong: false);
  764. }
  765. public static void EmitVectorAcrossVectorOpZx(ArmEmitterContext context, Func2I emit)
  766. {
  767. EmitVectorAcrossVectorOp(context, emit, signed: false, isLong: false);
  768. }
  769. public static void EmitVectorLongAcrossVectorOpSx(ArmEmitterContext context, Func2I emit)
  770. {
  771. EmitVectorAcrossVectorOp(context, emit, signed: true, isLong: true);
  772. }
  773. public static void EmitVectorLongAcrossVectorOpZx(ArmEmitterContext context, Func2I emit)
  774. {
  775. EmitVectorAcrossVectorOp(context, emit, signed: false, isLong: true);
  776. }
  777. private static void EmitVectorAcrossVectorOp(
  778. ArmEmitterContext context,
  779. Func2I emit,
  780. bool signed,
  781. bool isLong)
  782. {
  783. OpCodeSimd op = (OpCodeSimd)context.CurrOp;
  784. int elems = op.GetBytesCount() >> op.Size;
  785. Operand res = EmitVectorExtract(context, op.Rn, 0, op.Size, signed);
  786. for (int index = 1; index < elems; index++)
  787. {
  788. Operand n = EmitVectorExtract(context, op.Rn, index, op.Size, signed);
  789. res = emit(res, n);
  790. }
  791. int size = isLong ? op.Size + 1 : op.Size;
  792. Operand d = EmitVectorInsert(context, context.VectorZero(), res, 0, size);
  793. context.Copy(GetVec(op.Rd), d);
  794. }
  795. public static void EmitVectorAcrossVectorOpF(ArmEmitterContext context, Func2I emit)
  796. {
  797. OpCodeSimd op = (OpCodeSimd)context.CurrOp;
  798. Debug.Assert((op.Size & 1) == 0 && op.RegisterSize == RegisterSize.Simd128);
  799. Operand res = context.VectorExtract(OperandType.FP32, GetVec(op.Rn), 0);
  800. for (int index = 1; index < 4; index++)
  801. {
  802. Operand n = context.VectorExtract(OperandType.FP32, GetVec(op.Rn), index);
  803. res = emit(res, n);
  804. }
  805. Operand d = context.VectorInsert(context.VectorZero(), res, 0);
  806. context.Copy(GetVec(op.Rd), d);
  807. }
  808. public static void EmitSse2VectorAcrossVectorOpF(ArmEmitterContext context, Func2I emit)
  809. {
  810. OpCodeSimd op = (OpCodeSimd)context.CurrOp;
  811. Debug.Assert((op.Size & 1) == 0 && op.RegisterSize == RegisterSize.Simd128);
  812. const int sm0 = 0 << 6 | 0 << 4 | 0 << 2 | 0 << 0;
  813. const int sm1 = 1 << 6 | 1 << 4 | 1 << 2 | 1 << 0;
  814. const int sm2 = 2 << 6 | 2 << 4 | 2 << 2 | 2 << 0;
  815. const int sm3 = 3 << 6 | 3 << 4 | 3 << 2 | 3 << 0;
  816. Operand nCopy = context.Copy(GetVec(op.Rn));
  817. Operand part0 = context.AddIntrinsic(Intrinsic.X86Shufps, nCopy, nCopy, Const(sm0));
  818. Operand part1 = context.AddIntrinsic(Intrinsic.X86Shufps, nCopy, nCopy, Const(sm1));
  819. Operand part2 = context.AddIntrinsic(Intrinsic.X86Shufps, nCopy, nCopy, Const(sm2));
  820. Operand part3 = context.AddIntrinsic(Intrinsic.X86Shufps, nCopy, nCopy, Const(sm3));
  821. Operand res = emit(emit(part0, part1), emit(part2, part3));
  822. context.Copy(GetVec(op.Rd), context.VectorZeroUpper96(res));
  823. }
  824. public static void EmitScalarPairwiseOpF(ArmEmitterContext context, Func2I emit)
  825. {
  826. OpCodeSimd op = (OpCodeSimd)context.CurrOp;
  827. OperandType type = (op.Size & 1) != 0 ? OperandType.FP64 : OperandType.FP32;
  828. Operand ne0 = context.VectorExtract(type, GetVec(op.Rn), 0);
  829. Operand ne1 = context.VectorExtract(type, GetVec(op.Rn), 1);
  830. Operand res = context.VectorInsert(context.VectorZero(), emit(ne0, ne1), 0);
  831. context.Copy(GetVec(op.Rd), res);
  832. }
  833. public static void EmitSse2ScalarPairwiseOpF(ArmEmitterContext context, Func2I emit)
  834. {
  835. OpCodeSimd op = (OpCodeSimd)context.CurrOp;
  836. Operand n = GetVec(op.Rn);
  837. Operand op0, op1;
  838. if ((op.Size & 1) == 0)
  839. {
  840. const int sm0 = 2 << 6 | 2 << 4 | 2 << 2 | 0 << 0;
  841. const int sm1 = 2 << 6 | 2 << 4 | 2 << 2 | 1 << 0;
  842. Operand zeroN = context.VectorZeroUpper64(n);
  843. op0 = context.AddIntrinsic(Intrinsic.X86Pshufd, zeroN, Const(sm0));
  844. op1 = context.AddIntrinsic(Intrinsic.X86Pshufd, zeroN, Const(sm1));
  845. }
  846. else /* if ((op.Size & 1) == 1) */
  847. {
  848. Operand zero = context.VectorZero();
  849. op0 = context.AddIntrinsic(Intrinsic.X86Movlhps, n, zero);
  850. op1 = context.AddIntrinsic(Intrinsic.X86Movhlps, zero, n);
  851. }
  852. context.Copy(GetVec(op.Rd), emit(op0, op1));
  853. }
  854. public static void EmitVectorPairwiseOpF(ArmEmitterContext context, Func2I emit)
  855. {
  856. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  857. Operand res = context.VectorZero();
  858. int sizeF = op.Size & 1;
  859. OperandType type = sizeF != 0 ? OperandType.FP64 : OperandType.FP32;
  860. int pairs = op.GetPairsCount() >> sizeF + 2;
  861. for (int index = 0; index < pairs; index++)
  862. {
  863. int pairIndex = index << 1;
  864. Operand n0 = context.VectorExtract(type, GetVec(op.Rn), pairIndex);
  865. Operand n1 = context.VectorExtract(type, GetVec(op.Rn), pairIndex + 1);
  866. Operand m0 = context.VectorExtract(type, GetVec(op.Rm), pairIndex);
  867. Operand m1 = context.VectorExtract(type, GetVec(op.Rm), pairIndex + 1);
  868. res = context.VectorInsert(res, emit(n0, n1), index);
  869. res = context.VectorInsert(res, emit(m0, m1), pairs + index);
  870. }
  871. context.Copy(GetVec(op.Rd), res);
  872. }
  873. public static void EmitSse2VectorPairwiseOpF(ArmEmitterContext context, Func2I emit)
  874. {
  875. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  876. Operand nCopy = context.Copy(GetVec(op.Rn));
  877. Operand mCopy = context.Copy(GetVec(op.Rm));
  878. int sizeF = op.Size & 1;
  879. if (sizeF == 0)
  880. {
  881. if (op.RegisterSize == RegisterSize.Simd64)
  882. {
  883. Operand unpck = context.AddIntrinsic(Intrinsic.X86Unpcklps, nCopy, mCopy);
  884. Operand zero = context.VectorZero();
  885. Operand part0 = context.AddIntrinsic(Intrinsic.X86Movlhps, unpck, zero);
  886. Operand part1 = context.AddIntrinsic(Intrinsic.X86Movhlps, zero, unpck);
  887. context.Copy(GetVec(op.Rd), emit(part0, part1));
  888. }
  889. else /* if (op.RegisterSize == RegisterSize.Simd128) */
  890. {
  891. const int sm0 = 2 << 6 | 0 << 4 | 2 << 2 | 0 << 0;
  892. const int sm1 = 3 << 6 | 1 << 4 | 3 << 2 | 1 << 0;
  893. Operand part0 = context.AddIntrinsic(Intrinsic.X86Shufps, nCopy, mCopy, Const(sm0));
  894. Operand part1 = context.AddIntrinsic(Intrinsic.X86Shufps, nCopy, mCopy, Const(sm1));
  895. context.Copy(GetVec(op.Rd), emit(part0, part1));
  896. }
  897. }
  898. else /* if (sizeF == 1) */
  899. {
  900. Operand part0 = context.AddIntrinsic(Intrinsic.X86Unpcklpd, nCopy, mCopy);
  901. Operand part1 = context.AddIntrinsic(Intrinsic.X86Unpckhpd, nCopy, mCopy);
  902. context.Copy(GetVec(op.Rd), emit(part0, part1));
  903. }
  904. }
  905. [Flags]
  906. public enum Mxcsr
  907. {
  908. Ftz = 1 << 15, // Flush To Zero.
  909. Um = 1 << 11, // Underflow Mask.
  910. Dm = 1 << 8, // Denormal Mask.
  911. Daz = 1 << 6 // Denormals Are Zero.
  912. }
  913. public static void EmitSseOrAvxEnterFtzAndDazModesOpF(ArmEmitterContext context, out Operand isTrue)
  914. {
  915. isTrue = context.Call(typeof(NativeInterface).GetMethod(nameof(NativeInterface.GetFpcrFz)));
  916. Operand lblTrue = Label();
  917. context.BranchIfFalse(lblTrue, isTrue);
  918. context.AddIntrinsicNoRet(Intrinsic.X86Mxcsrmb, Const((int)(Mxcsr.Ftz | Mxcsr.Um | Mxcsr.Dm | Mxcsr.Daz)));
  919. context.MarkLabel(lblTrue);
  920. }
  921. public static void EmitSseOrAvxExitFtzAndDazModesOpF(ArmEmitterContext context, Operand isTrue = default)
  922. {
  923. isTrue = isTrue == default
  924. ? context.Call(typeof(NativeInterface).GetMethod(nameof(NativeInterface.GetFpcrFz)))
  925. : isTrue;
  926. Operand lblTrue = Label();
  927. context.BranchIfFalse(lblTrue, isTrue);
  928. context.AddIntrinsicNoRet(Intrinsic.X86Mxcsrub, Const((int)(Mxcsr.Ftz | Mxcsr.Daz)));
  929. context.MarkLabel(lblTrue);
  930. }
  931. public enum CmpCondition
  932. {
  933. // Legacy Sse.
  934. Equal = 0, // Ordered, non-signaling.
  935. LessThan = 1, // Ordered, signaling.
  936. LessThanOrEqual = 2, // Ordered, signaling.
  937. UnorderedQ = 3, // Non-signaling.
  938. NotLessThan = 5, // Unordered, signaling.
  939. NotLessThanOrEqual = 6, // Unordered, signaling.
  940. OrderedQ = 7, // Non-signaling.
  941. // Vex.
  942. GreaterThanOrEqual = 13, // Ordered, signaling.
  943. GreaterThan = 14, // Ordered, signaling.
  944. OrderedS = 23 // Signaling.
  945. }
  946. [Flags]
  947. public enum SaturatingFlags
  948. {
  949. None = 0,
  950. ByElem = 1 << 0,
  951. Scalar = 1 << 1,
  952. Signed = 1 << 2,
  953. Add = 1 << 3,
  954. Sub = 1 << 4,
  955. Accumulate = 1 << 5
  956. }
  957. public static void EmitScalarSaturatingUnaryOpSx(ArmEmitterContext context, Func1I emit)
  958. {
  959. EmitSaturatingUnaryOpSx(context, emit, SaturatingFlags.Scalar | SaturatingFlags.Signed);
  960. }
  961. public static void EmitVectorSaturatingUnaryOpSx(ArmEmitterContext context, Func1I emit)
  962. {
  963. EmitSaturatingUnaryOpSx(context, emit, SaturatingFlags.Signed);
  964. }
  965. public static void EmitSaturatingUnaryOpSx(ArmEmitterContext context, Func1I emit, SaturatingFlags flags)
  966. {
  967. OpCodeSimd op = (OpCodeSimd)context.CurrOp;
  968. Operand res = context.VectorZero();
  969. bool scalar = (flags & SaturatingFlags.Scalar) != 0;
  970. int elems = !scalar ? op.GetBytesCount() >> op.Size : 1;
  971. for (int index = 0; index < elems; index++)
  972. {
  973. Operand ne = EmitVectorExtractSx(context, op.Rn, index, op.Size);
  974. Operand de;
  975. if (op.Size <= 2)
  976. {
  977. de = EmitSignedSrcSatQ(context, emit(ne), op.Size, signedDst: true);
  978. }
  979. else /* if (op.Size == 3) */
  980. {
  981. de = EmitUnarySignedSatQAbsOrNeg(context, emit(ne));
  982. }
  983. res = EmitVectorInsert(context, res, de, index, op.Size);
  984. }
  985. context.Copy(GetVec(op.Rd), res);
  986. }
  987. public static void EmitScalarSaturatingBinaryOpSx(ArmEmitterContext context, Func2I emit = null, SaturatingFlags flags = SaturatingFlags.None)
  988. {
  989. EmitSaturatingBinaryOp(context, emit, SaturatingFlags.Scalar | SaturatingFlags.Signed | flags);
  990. }
  991. public static void EmitScalarSaturatingBinaryOpZx(ArmEmitterContext context, SaturatingFlags flags)
  992. {
  993. EmitSaturatingBinaryOp(context, null, SaturatingFlags.Scalar | flags);
  994. }
  995. public static void EmitVectorSaturatingBinaryOpSx(ArmEmitterContext context, Func2I emit = null, SaturatingFlags flags = SaturatingFlags.None)
  996. {
  997. EmitSaturatingBinaryOp(context, emit, SaturatingFlags.Signed | flags);
  998. }
  999. public static void EmitVectorSaturatingBinaryOpZx(ArmEmitterContext context, SaturatingFlags flags)
  1000. {
  1001. EmitSaturatingBinaryOp(context, null, flags);
  1002. }
  1003. public static void EmitVectorSaturatingBinaryOpByElemSx(ArmEmitterContext context, Func2I emit)
  1004. {
  1005. EmitSaturatingBinaryOp(context, emit, SaturatingFlags.ByElem | SaturatingFlags.Signed);
  1006. }
  1007. public static void EmitSaturatingBinaryOp(ArmEmitterContext context, Func2I emit, SaturatingFlags flags)
  1008. {
  1009. OpCodeSimd op = (OpCodeSimd)context.CurrOp;
  1010. Operand res = context.VectorZero();
  1011. bool byElem = (flags & SaturatingFlags.ByElem) != 0;
  1012. bool scalar = (flags & SaturatingFlags.Scalar) != 0;
  1013. bool signed = (flags & SaturatingFlags.Signed) != 0;
  1014. bool add = (flags & SaturatingFlags.Add) != 0;
  1015. bool sub = (flags & SaturatingFlags.Sub) != 0;
  1016. bool accumulate = (flags & SaturatingFlags.Accumulate) != 0;
  1017. int elems = !scalar ? op.GetBytesCount() >> op.Size : 1;
  1018. if (add || sub)
  1019. {
  1020. for (int index = 0; index < elems; index++)
  1021. {
  1022. Operand de;
  1023. Operand ne = EmitVectorExtract(context, op.Rn, index, op.Size, signed);
  1024. Operand me = EmitVectorExtract(context, ((OpCodeSimdReg)op).Rm, index, op.Size, signed);
  1025. if (op.Size <= 2)
  1026. {
  1027. Operand temp = add ? context.Add(ne, me) : context.Subtract(ne, me);
  1028. de = EmitSignedSrcSatQ(context, temp, op.Size, signedDst: signed);
  1029. }
  1030. else /* if (op.Size == 3) */
  1031. {
  1032. if (add)
  1033. {
  1034. de = signed ? EmitBinarySignedSatQAdd(context, ne, me) : EmitBinaryUnsignedSatQAdd(context, ne, me);
  1035. }
  1036. else /* if (sub) */
  1037. {
  1038. de = signed ? EmitBinarySignedSatQSub(context, ne, me) : EmitBinaryUnsignedSatQSub(context, ne, me);
  1039. }
  1040. }
  1041. res = EmitVectorInsert(context, res, de, index, op.Size);
  1042. }
  1043. }
  1044. else if (accumulate)
  1045. {
  1046. for (int index = 0; index < elems; index++)
  1047. {
  1048. Operand de;
  1049. Operand ne = EmitVectorExtract(context, op.Rn, index, op.Size, !signed);
  1050. Operand me = EmitVectorExtract(context, op.Rd, index, op.Size, signed);
  1051. if (op.Size <= 2)
  1052. {
  1053. Operand temp = context.Add(ne, me);
  1054. de = EmitSignedSrcSatQ(context, temp, op.Size, signedDst: signed);
  1055. }
  1056. else /* if (op.Size == 3) */
  1057. {
  1058. de = signed ? EmitBinarySignedSatQAcc(context, ne, me) : EmitBinaryUnsignedSatQAcc(context, ne, me);
  1059. }
  1060. res = EmitVectorInsert(context, res, de, index, op.Size);
  1061. }
  1062. }
  1063. else
  1064. {
  1065. Operand me = default;
  1066. if (byElem)
  1067. {
  1068. OpCodeSimdRegElem opRegElem = (OpCodeSimdRegElem)op;
  1069. me = EmitVectorExtract(context, opRegElem.Rm, opRegElem.Index, op.Size, signed);
  1070. }
  1071. for (int index = 0; index < elems; index++)
  1072. {
  1073. Operand ne = EmitVectorExtract(context, op.Rn, index, op.Size, signed);
  1074. if (!byElem)
  1075. {
  1076. me = EmitVectorExtract(context, ((OpCodeSimdReg)op).Rm, index, op.Size, signed);
  1077. }
  1078. Operand de = EmitSignedSrcSatQ(context, emit(ne, me), op.Size, signedDst: signed);
  1079. res = EmitVectorInsert(context, res, de, index, op.Size);
  1080. }
  1081. }
  1082. context.Copy(GetVec(op.Rd), res);
  1083. }
  1084. [Flags]
  1085. public enum SaturatingNarrowFlags
  1086. {
  1087. Scalar = 1 << 0,
  1088. SignedSrc = 1 << 1,
  1089. SignedDst = 1 << 2,
  1090. ScalarSxSx = Scalar | SignedSrc | SignedDst,
  1091. ScalarSxZx = Scalar | SignedSrc,
  1092. ScalarZxZx = Scalar,
  1093. VectorSxSx = SignedSrc | SignedDst,
  1094. VectorSxZx = SignedSrc,
  1095. VectorZxZx = 0
  1096. }
  1097. public static void EmitSaturatingNarrowOp(ArmEmitterContext context, SaturatingNarrowFlags flags)
  1098. {
  1099. OpCodeSimd op = (OpCodeSimd)context.CurrOp;
  1100. bool scalar = (flags & SaturatingNarrowFlags.Scalar) != 0;
  1101. bool signedSrc = (flags & SaturatingNarrowFlags.SignedSrc) != 0;
  1102. bool signedDst = (flags & SaturatingNarrowFlags.SignedDst) != 0;
  1103. int elems = !scalar ? 8 >> op.Size : 1;
  1104. int part = !scalar && (op.RegisterSize == RegisterSize.Simd128) ? elems : 0;
  1105. Operand d = GetVec(op.Rd);
  1106. Operand res = part == 0 ? context.VectorZero() : context.Copy(d);
  1107. for (int index = 0; index < elems; index++)
  1108. {
  1109. Operand ne = EmitVectorExtract(context, op.Rn, index, op.Size + 1, signedSrc);
  1110. Operand temp = signedSrc
  1111. ? EmitSignedSrcSatQ(context, ne, op.Size, signedDst)
  1112. : EmitUnsignedSrcSatQ(context, ne, op.Size, signedDst);
  1113. res = EmitVectorInsert(context, res, temp, part + index, op.Size);
  1114. }
  1115. context.Copy(d, res);
  1116. }
  1117. // TSrc (16bit, 32bit, 64bit; signed) > TDst (8bit, 16bit, 32bit; signed, unsigned).
  1118. // long SignedSrcSignedDstSatQ(long op, int size); ulong SignedSrcUnsignedDstSatQ(long op, int size);
  1119. public static Operand EmitSignedSrcSatQ(ArmEmitterContext context, Operand op, int sizeDst, bool signedDst)
  1120. {
  1121. Debug.Assert(op.Type == OperandType.I64 && (uint)sizeDst <= 2u);
  1122. Operand lbl1 = Label();
  1123. Operand lblEnd = Label();
  1124. int eSize = 8 << sizeDst;
  1125. Operand maxT = signedDst ? Const((1L << (eSize - 1)) - 1L) : Const((1UL << eSize) - 1UL);
  1126. Operand minT = signedDst ? Const(-(1L << (eSize - 1))) : Const(0UL);
  1127. Operand res = context.Copy(context.AllocateLocal(OperandType.I64), op);
  1128. context.BranchIf(lbl1, res, maxT, Comparison.LessOrEqual);
  1129. context.Copy(res, maxT);
  1130. context.Call(typeof(NativeInterface).GetMethod(nameof(NativeInterface.SetFpsrQc)));
  1131. context.Branch(lblEnd);
  1132. context.MarkLabel(lbl1);
  1133. context.BranchIf(lblEnd, res, minT, Comparison.GreaterOrEqual);
  1134. context.Copy(res, minT);
  1135. context.Call(typeof(NativeInterface).GetMethod(nameof(NativeInterface.SetFpsrQc)));
  1136. context.Branch(lblEnd);
  1137. context.MarkLabel(lblEnd);
  1138. return res;
  1139. }
  1140. // TSrc (16bit, 32bit, 64bit; unsigned) > TDst (8bit, 16bit, 32bit; signed, unsigned).
  1141. // long UnsignedSrcSignedDstSatQ(ulong op, int size); ulong UnsignedSrcUnsignedDstSatQ(ulong op, int size);
  1142. public static Operand EmitUnsignedSrcSatQ(ArmEmitterContext context, Operand op, int sizeDst, bool signedDst)
  1143. {
  1144. Debug.Assert(op.Type == OperandType.I64 && (uint)sizeDst <= 2u);
  1145. Operand lblEnd = Label();
  1146. int eSize = 8 << sizeDst;
  1147. Operand maxL = signedDst ? Const((1L << (eSize - 1)) - 1L) : Const((1UL << eSize) - 1UL);
  1148. Operand res = context.Copy(context.AllocateLocal(OperandType.I64), op);
  1149. context.BranchIf(lblEnd, res, maxL, Comparison.LessOrEqualUI);
  1150. context.Copy(res, maxL);
  1151. context.Call(typeof(NativeInterface).GetMethod(nameof(NativeInterface.SetFpsrQc)));
  1152. context.Branch(lblEnd);
  1153. context.MarkLabel(lblEnd);
  1154. return res;
  1155. }
  1156. // long UnarySignedSatQAbsOrNeg(long op);
  1157. private static Operand EmitUnarySignedSatQAbsOrNeg(ArmEmitterContext context, Operand op)
  1158. {
  1159. Debug.Assert(op.Type == OperandType.I64);
  1160. Operand lblEnd = Label();
  1161. Operand minL = Const(long.MinValue);
  1162. Operand maxL = Const(long.MaxValue);
  1163. Operand res = context.Copy(context.AllocateLocal(OperandType.I64), op);
  1164. context.BranchIf(lblEnd, res, minL, Comparison.NotEqual);
  1165. context.Copy(res, maxL);
  1166. context.Call(typeof(NativeInterface).GetMethod(nameof(NativeInterface.SetFpsrQc)));
  1167. context.Branch(lblEnd);
  1168. context.MarkLabel(lblEnd);
  1169. return res;
  1170. }
  1171. // long BinarySignedSatQAdd(long op1, long op2);
  1172. private static Operand EmitBinarySignedSatQAdd(ArmEmitterContext context, Operand op1, Operand op2)
  1173. {
  1174. Debug.Assert(op1.Type == OperandType.I64 && op2.Type == OperandType.I64);
  1175. Operand lblEnd = Label();
  1176. Operand minL = Const(long.MinValue);
  1177. Operand maxL = Const(long.MaxValue);
  1178. Operand zero = Const(0L);
  1179. Operand res = context.Copy(context.AllocateLocal(OperandType.I64), context.Add(op1, op2));
  1180. Operand left = context.BitwiseNot(context.BitwiseExclusiveOr(op1, op2));
  1181. Operand right = context.BitwiseExclusiveOr(op1, res);
  1182. context.BranchIf(lblEnd, context.BitwiseAnd(left, right), zero, Comparison.GreaterOrEqual);
  1183. Operand isPositive = context.ICompareGreaterOrEqual(op1, zero);
  1184. context.Copy(res, context.ConditionalSelect(isPositive, maxL, minL));
  1185. context.Call(typeof(NativeInterface).GetMethod(nameof(NativeInterface.SetFpsrQc)));
  1186. context.Branch(lblEnd);
  1187. context.MarkLabel(lblEnd);
  1188. return res;
  1189. }
  1190. // ulong BinaryUnsignedSatQAdd(ulong op1, ulong op2);
  1191. private static Operand EmitBinaryUnsignedSatQAdd(ArmEmitterContext context, Operand op1, Operand op2)
  1192. {
  1193. Debug.Assert(op1.Type == OperandType.I64 && op2.Type == OperandType.I64);
  1194. Operand lblEnd = Label();
  1195. Operand maxUL = Const(ulong.MaxValue);
  1196. Operand res = context.Copy(context.AllocateLocal(OperandType.I64), context.Add(op1, op2));
  1197. context.BranchIf(lblEnd, res, op1, Comparison.GreaterOrEqualUI);
  1198. context.Copy(res, maxUL);
  1199. context.Call(typeof(NativeInterface).GetMethod(nameof(NativeInterface.SetFpsrQc)));
  1200. context.Branch(lblEnd);
  1201. context.MarkLabel(lblEnd);
  1202. return res;
  1203. }
  1204. // long BinarySignedSatQSub(long op1, long op2);
  1205. private static Operand EmitBinarySignedSatQSub(ArmEmitterContext context, Operand op1, Operand op2)
  1206. {
  1207. Debug.Assert(op1.Type == OperandType.I64 && op2.Type == OperandType.I64);
  1208. Operand lblEnd = Label();
  1209. Operand minL = Const(long.MinValue);
  1210. Operand maxL = Const(long.MaxValue);
  1211. Operand zero = Const(0L);
  1212. Operand res = context.Copy(context.AllocateLocal(OperandType.I64), context.Subtract(op1, op2));
  1213. Operand left = context.BitwiseExclusiveOr(op1, op2);
  1214. Operand right = context.BitwiseExclusiveOr(op1, res);
  1215. context.BranchIf(lblEnd, context.BitwiseAnd(left, right), zero, Comparison.GreaterOrEqual);
  1216. Operand isPositive = context.ICompareGreaterOrEqual(op1, zero);
  1217. context.Copy(res, context.ConditionalSelect(isPositive, maxL, minL));
  1218. context.Call(typeof(NativeInterface).GetMethod(nameof(NativeInterface.SetFpsrQc)));
  1219. context.Branch(lblEnd);
  1220. context.MarkLabel(lblEnd);
  1221. return res;
  1222. }
  1223. // ulong BinaryUnsignedSatQSub(ulong op1, ulong op2);
  1224. private static Operand EmitBinaryUnsignedSatQSub(ArmEmitterContext context, Operand op1, Operand op2)
  1225. {
  1226. Debug.Assert(op1.Type == OperandType.I64 && op2.Type == OperandType.I64);
  1227. Operand lblEnd = Label();
  1228. Operand zero = Const(0L);
  1229. Operand res = context.Copy(context.AllocateLocal(OperandType.I64), context.Subtract(op1, op2));
  1230. context.BranchIf(lblEnd, op1, op2, Comparison.GreaterOrEqualUI);
  1231. context.Copy(res, zero);
  1232. context.Call(typeof(NativeInterface).GetMethod(nameof(NativeInterface.SetFpsrQc)));
  1233. context.Branch(lblEnd);
  1234. context.MarkLabel(lblEnd);
  1235. return res;
  1236. }
  1237. // long BinarySignedSatQAcc(ulong op1, long op2);
  1238. private static Operand EmitBinarySignedSatQAcc(ArmEmitterContext context, Operand op1, Operand op2)
  1239. {
  1240. Debug.Assert(op1.Type == OperandType.I64 && op2.Type == OperandType.I64);
  1241. Operand lbl1 = Label();
  1242. Operand lbl2 = Label();
  1243. Operand lblEnd = Label();
  1244. Operand maxL = Const(long.MaxValue);
  1245. Operand zero = Const(0L);
  1246. Operand res = context.Copy(context.AllocateLocal(OperandType.I64), context.Add(op1, op2));
  1247. context.BranchIf(lbl1, op1, maxL, Comparison.GreaterUI);
  1248. Operand notOp2AndRes = context.BitwiseAnd(context.BitwiseNot(op2), res);
  1249. context.BranchIf(lblEnd, notOp2AndRes, zero, Comparison.GreaterOrEqual);
  1250. context.Copy(res, maxL);
  1251. context.Call(typeof(NativeInterface).GetMethod(nameof(NativeInterface.SetFpsrQc)));
  1252. context.Branch(lblEnd);
  1253. context.MarkLabel(lbl1);
  1254. context.BranchIf(lbl2, op2, zero, Comparison.Less);
  1255. context.Copy(res, maxL);
  1256. context.Call(typeof(NativeInterface).GetMethod(nameof(NativeInterface.SetFpsrQc)));
  1257. context.Branch(lblEnd);
  1258. context.MarkLabel(lbl2);
  1259. context.BranchIf(lblEnd, res, maxL, Comparison.LessOrEqualUI);
  1260. context.Copy(res, maxL);
  1261. context.Call(typeof(NativeInterface).GetMethod(nameof(NativeInterface.SetFpsrQc)));
  1262. context.Branch(lblEnd);
  1263. context.MarkLabel(lblEnd);
  1264. return res;
  1265. }
  1266. // ulong BinaryUnsignedSatQAcc(long op1, ulong op2);
  1267. private static Operand EmitBinaryUnsignedSatQAcc(ArmEmitterContext context, Operand op1, Operand op2)
  1268. {
  1269. Debug.Assert(op1.Type == OperandType.I64 && op2.Type == OperandType.I64);
  1270. Operand lbl1 = Label();
  1271. Operand lblEnd = Label();
  1272. Operand maxUL = Const(ulong.MaxValue);
  1273. Operand maxL = Const(long.MaxValue);
  1274. Operand zero = Const(0L);
  1275. Operand res = context.Copy(context.AllocateLocal(OperandType.I64), context.Add(op1, op2));
  1276. context.BranchIf(lbl1, op1, zero, Comparison.Less);
  1277. context.BranchIf(lblEnd, res, op1, Comparison.GreaterOrEqualUI);
  1278. context.Copy(res, maxUL);
  1279. context.Call(typeof(NativeInterface).GetMethod(nameof(NativeInterface.SetFpsrQc)));
  1280. context.Branch(lblEnd);
  1281. context.MarkLabel(lbl1);
  1282. context.BranchIf(lblEnd, op2, maxL, Comparison.GreaterUI);
  1283. context.BranchIf(lblEnd, res, zero, Comparison.GreaterOrEqual);
  1284. context.Copy(res, zero);
  1285. context.Call(typeof(NativeInterface).GetMethod(nameof(NativeInterface.SetFpsrQc)));
  1286. context.Branch(lblEnd);
  1287. context.MarkLabel(lblEnd);
  1288. return res;
  1289. }
  1290. public static Operand EmitFloatAbs(ArmEmitterContext context, Operand value, bool single, bool vector)
  1291. {
  1292. Operand mask;
  1293. if (single)
  1294. {
  1295. mask = vector ? X86GetAllElements(context, -0f) : X86GetScalar(context, -0f);
  1296. }
  1297. else
  1298. {
  1299. mask = vector ? X86GetAllElements(context, -0d) : X86GetScalar(context, -0d);
  1300. }
  1301. return context.AddIntrinsic(single ? Intrinsic.X86Andnps : Intrinsic.X86Andnpd, mask, value);
  1302. }
  1303. public static Operand EmitVectorExtractSx(ArmEmitterContext context, int reg, int index, int size)
  1304. {
  1305. return EmitVectorExtract(context, reg, index, size, true);
  1306. }
  1307. public static Operand EmitVectorExtractZx(ArmEmitterContext context, int reg, int index, int size)
  1308. {
  1309. return EmitVectorExtract(context, reg, index, size, false);
  1310. }
  1311. public static Operand EmitVectorExtract(ArmEmitterContext context, int reg, int index, int size, bool signed)
  1312. {
  1313. ThrowIfInvalid(index, size);
  1314. Operand res = default;
  1315. switch (size)
  1316. {
  1317. case 0:
  1318. res = context.VectorExtract8(GetVec(reg), index);
  1319. break;
  1320. case 1:
  1321. res = context.VectorExtract16(GetVec(reg), index);
  1322. break;
  1323. case 2:
  1324. res = context.VectorExtract(OperandType.I32, GetVec(reg), index);
  1325. break;
  1326. case 3:
  1327. res = context.VectorExtract(OperandType.I64, GetVec(reg), index);
  1328. break;
  1329. }
  1330. if (signed)
  1331. {
  1332. switch (size)
  1333. {
  1334. case 0: res = context.SignExtend8 (OperandType.I64, res); break;
  1335. case 1: res = context.SignExtend16(OperandType.I64, res); break;
  1336. case 2: res = context.SignExtend32(OperandType.I64, res); break;
  1337. }
  1338. }
  1339. else
  1340. {
  1341. switch (size)
  1342. {
  1343. case 0: res = context.ZeroExtend8 (OperandType.I64, res); break;
  1344. case 1: res = context.ZeroExtend16(OperandType.I64, res); break;
  1345. case 2: res = context.ZeroExtend32(OperandType.I64, res); break;
  1346. }
  1347. }
  1348. return res;
  1349. }
  1350. public static Operand EmitVectorInsert(ArmEmitterContext context, Operand vector, Operand value, int index, int size)
  1351. {
  1352. ThrowIfInvalid(index, size);
  1353. if (size < 3 && value.Type == OperandType.I64)
  1354. {
  1355. value = context.ConvertI64ToI32(value);
  1356. }
  1357. switch (size)
  1358. {
  1359. case 0: vector = context.VectorInsert8 (vector, value, index); break;
  1360. case 1: vector = context.VectorInsert16(vector, value, index); break;
  1361. case 2: vector = context.VectorInsert (vector, value, index); break;
  1362. case 3: vector = context.VectorInsert (vector, value, index); break;
  1363. }
  1364. return vector;
  1365. }
  1366. public static void ThrowIfInvalid(int index, int size)
  1367. {
  1368. if ((uint)size > 3u)
  1369. {
  1370. throw new ArgumentOutOfRangeException(nameof(size));
  1371. }
  1372. if ((uint)index >= 16u >> size)
  1373. {
  1374. throw new ArgumentOutOfRangeException(nameof(index));
  1375. }
  1376. }
  1377. }
  1378. }