InstEmitSimdHelper.cs 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493
  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 static ARMeilleure.Instructions.InstEmitHelper;
  8. using static ARMeilleure.IntermediateRepresentation.OperandHelper;
  9. namespace ARMeilleure.Instructions
  10. {
  11. using Func1I = Func<Operand, Operand>;
  12. using Func2I = Func<Operand, Operand, Operand>;
  13. using Func3I = Func<Operand, Operand, Operand, Operand>;
  14. static class InstEmitSimdHelper
  15. {
  16. #region "X86 SSE Intrinsics"
  17. public static readonly Intrinsic[] X86PaddInstruction = new Intrinsic[]
  18. {
  19. Intrinsic.X86Paddb,
  20. Intrinsic.X86Paddw,
  21. Intrinsic.X86Paddd,
  22. Intrinsic.X86Paddq
  23. };
  24. public static readonly Intrinsic[] X86PcmpeqInstruction = new Intrinsic[]
  25. {
  26. Intrinsic.X86Pcmpeqb,
  27. Intrinsic.X86Pcmpeqw,
  28. Intrinsic.X86Pcmpeqd,
  29. Intrinsic.X86Pcmpeqq
  30. };
  31. public static readonly Intrinsic[] X86PcmpgtInstruction = new Intrinsic[]
  32. {
  33. Intrinsic.X86Pcmpgtb,
  34. Intrinsic.X86Pcmpgtw,
  35. Intrinsic.X86Pcmpgtd,
  36. Intrinsic.X86Pcmpgtq
  37. };
  38. public static readonly Intrinsic[] X86PmaxsInstruction = new Intrinsic[]
  39. {
  40. Intrinsic.X86Pmaxsb,
  41. Intrinsic.X86Pmaxsw,
  42. Intrinsic.X86Pmaxsd
  43. };
  44. public static readonly Intrinsic[] X86PmaxuInstruction = new Intrinsic[]
  45. {
  46. Intrinsic.X86Pmaxub,
  47. Intrinsic.X86Pmaxuw,
  48. Intrinsic.X86Pmaxud
  49. };
  50. public static readonly Intrinsic[] X86PminsInstruction = new Intrinsic[]
  51. {
  52. Intrinsic.X86Pminsb,
  53. Intrinsic.X86Pminsw,
  54. Intrinsic.X86Pminsd
  55. };
  56. public static readonly Intrinsic[] X86PminuInstruction = new Intrinsic[]
  57. {
  58. Intrinsic.X86Pminub,
  59. Intrinsic.X86Pminuw,
  60. Intrinsic.X86Pminud
  61. };
  62. public static readonly Intrinsic[] X86PmovsxInstruction = new Intrinsic[]
  63. {
  64. Intrinsic.X86Pmovsxbw,
  65. Intrinsic.X86Pmovsxwd,
  66. Intrinsic.X86Pmovsxdq
  67. };
  68. public static readonly Intrinsic[] X86PmovzxInstruction = new Intrinsic[]
  69. {
  70. Intrinsic.X86Pmovzxbw,
  71. Intrinsic.X86Pmovzxwd,
  72. Intrinsic.X86Pmovzxdq
  73. };
  74. public static readonly Intrinsic[] X86PsllInstruction = new Intrinsic[]
  75. {
  76. 0,
  77. Intrinsic.X86Psllw,
  78. Intrinsic.X86Pslld,
  79. Intrinsic.X86Psllq
  80. };
  81. public static readonly Intrinsic[] X86PsraInstruction = new Intrinsic[]
  82. {
  83. 0,
  84. Intrinsic.X86Psraw,
  85. Intrinsic.X86Psrad
  86. };
  87. public static readonly Intrinsic[] X86PsrlInstruction = new Intrinsic[]
  88. {
  89. 0,
  90. Intrinsic.X86Psrlw,
  91. Intrinsic.X86Psrld,
  92. Intrinsic.X86Psrlq
  93. };
  94. public static readonly Intrinsic[] X86PsubInstruction = new Intrinsic[]
  95. {
  96. Intrinsic.X86Psubb,
  97. Intrinsic.X86Psubw,
  98. Intrinsic.X86Psubd,
  99. Intrinsic.X86Psubq
  100. };
  101. public static readonly Intrinsic[] X86PunpckhInstruction = new Intrinsic[]
  102. {
  103. Intrinsic.X86Punpckhbw,
  104. Intrinsic.X86Punpckhwd,
  105. Intrinsic.X86Punpckhdq,
  106. Intrinsic.X86Punpckhqdq
  107. };
  108. public static readonly Intrinsic[] X86PunpcklInstruction = new Intrinsic[]
  109. {
  110. Intrinsic.X86Punpcklbw,
  111. Intrinsic.X86Punpcklwd,
  112. Intrinsic.X86Punpckldq,
  113. Intrinsic.X86Punpcklqdq
  114. };
  115. #endregion
  116. public static int GetImmShl(OpCodeSimdShImm op)
  117. {
  118. return op.Imm - (8 << op.Size);
  119. }
  120. public static int GetImmShr(OpCodeSimdShImm op)
  121. {
  122. return (8 << (op.Size + 1)) - op.Imm;
  123. }
  124. public static Operand X86GetScalar(ArmEmitterContext context, float value)
  125. {
  126. return X86GetScalar(context, BitConverter.SingleToInt32Bits(value));
  127. }
  128. public static Operand X86GetScalar(ArmEmitterContext context, double value)
  129. {
  130. return X86GetScalar(context, BitConverter.DoubleToInt64Bits(value));
  131. }
  132. public static Operand X86GetScalar(ArmEmitterContext context, int value)
  133. {
  134. return context.VectorCreateScalar(Const(value));
  135. }
  136. public static Operand X86GetScalar(ArmEmitterContext context, long value)
  137. {
  138. return context.VectorCreateScalar(Const(value));
  139. }
  140. public static Operand X86GetAllElements(ArmEmitterContext context, float value)
  141. {
  142. return X86GetAllElements(context, BitConverter.SingleToInt32Bits(value));
  143. }
  144. public static Operand X86GetAllElements(ArmEmitterContext context, double value)
  145. {
  146. return X86GetAllElements(context, BitConverter.DoubleToInt64Bits(value));
  147. }
  148. public static Operand X86GetAllElements(ArmEmitterContext context, int value)
  149. {
  150. Operand vector = context.VectorCreateScalar(Const(value));
  151. vector = context.AddIntrinsic(Intrinsic.X86Shufps, vector, vector, Const(0));
  152. return vector;
  153. }
  154. public static Operand X86GetAllElements(ArmEmitterContext context, long value)
  155. {
  156. Operand vector = context.VectorCreateScalar(Const(value));
  157. vector = context.AddIntrinsic(Intrinsic.X86Movlhps, vector, vector);
  158. return vector;
  159. }
  160. public static int X86GetRoundControl(FPRoundingMode roundMode)
  161. {
  162. switch (roundMode)
  163. {
  164. case FPRoundingMode.ToNearest: return 8 | 0;
  165. case FPRoundingMode.TowardsPlusInfinity: return 8 | 2;
  166. case FPRoundingMode.TowardsMinusInfinity: return 8 | 1;
  167. case FPRoundingMode.TowardsZero: return 8 | 3;
  168. }
  169. throw new ArgumentException($"Invalid rounding mode \"{roundMode}\".");
  170. }
  171. public static void EmitScalarUnaryOpF(ArmEmitterContext context, Intrinsic inst32, Intrinsic inst64)
  172. {
  173. OpCodeSimd op = (OpCodeSimd)context.CurrOp;
  174. Operand n = GetVec(op.Rn);
  175. Intrinsic inst = (op.Size & 1) != 0 ? inst64 : inst32;
  176. Operand res = context.AddIntrinsic(inst, n);
  177. if ((op.Size & 1) != 0)
  178. {
  179. res = context.VectorZeroUpper64(res);
  180. }
  181. else
  182. {
  183. res = context.VectorZeroUpper96(res);
  184. }
  185. context.Copy(GetVec(op.Rd), res);
  186. }
  187. public static void EmitScalarBinaryOpF(ArmEmitterContext context, Intrinsic inst32, Intrinsic inst64)
  188. {
  189. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  190. Operand n = GetVec(op.Rn);
  191. Operand m = GetVec(op.Rm);
  192. Intrinsic inst = (op.Size & 1) != 0 ? inst64 : inst32;
  193. Operand res = context.AddIntrinsic(inst, n, m);
  194. if ((op.Size & 1) != 0)
  195. {
  196. res = context.VectorZeroUpper64(res);
  197. }
  198. else
  199. {
  200. res = context.VectorZeroUpper96(res);
  201. }
  202. context.Copy(GetVec(op.Rd), res);
  203. }
  204. public static void EmitVectorUnaryOpF(ArmEmitterContext context, Intrinsic inst32, Intrinsic inst64)
  205. {
  206. OpCodeSimd op = (OpCodeSimd)context.CurrOp;
  207. Operand n = GetVec(op.Rn);
  208. Intrinsic inst = (op.Size & 1) != 0 ? inst64 : inst32;
  209. Operand res = context.AddIntrinsic(inst, n);
  210. if (op.RegisterSize == RegisterSize.Simd64)
  211. {
  212. res = context.VectorZeroUpper64(res);
  213. }
  214. context.Copy(GetVec(op.Rd), res);
  215. }
  216. public static void EmitVectorBinaryOpF(ArmEmitterContext context, Intrinsic inst32, Intrinsic inst64)
  217. {
  218. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  219. Operand n = GetVec(op.Rn);
  220. Operand m = GetVec(op.Rm);
  221. Intrinsic inst = (op.Size & 1) != 0 ? inst64 : inst32;
  222. Operand res = context.AddIntrinsic(inst, n, m);
  223. if (op.RegisterSize == RegisterSize.Simd64)
  224. {
  225. res = context.VectorZeroUpper64(res);
  226. }
  227. context.Copy(GetVec(op.Rd), res);
  228. }
  229. public static Operand EmitUnaryMathCall(ArmEmitterContext context, _F32_F32 f32, _F64_F64 f64, Operand n)
  230. {
  231. IOpCodeSimd op = (IOpCodeSimd)context.CurrOp;
  232. return (op.Size & 1) == 0 ? context.Call(f32, n) : context.Call(f64, n);
  233. }
  234. public static Operand EmitRoundMathCall(ArmEmitterContext context, MidpointRounding roundMode, Operand n)
  235. {
  236. IOpCodeSimd op = (IOpCodeSimd)context.CurrOp;
  237. Delegate dlg;
  238. if ((op.Size & 1) == 0)
  239. {
  240. dlg = new _F32_F32_MidpointRounding(MathF.Round);
  241. }
  242. else /* if ((op.Size & 1) == 1) */
  243. {
  244. dlg = new _F64_F64_MidpointRounding(Math.Round);
  245. }
  246. return context.Call(dlg, n, Const((int)roundMode));
  247. }
  248. public static Operand EmitSoftFloatCall(
  249. ArmEmitterContext context,
  250. _F32_F32 f32,
  251. _F64_F64 f64,
  252. params Operand[] callArgs)
  253. {
  254. IOpCodeSimd op = (IOpCodeSimd)context.CurrOp;
  255. Delegate dlg = (op.Size & 1) == 0 ? (Delegate)f32 : (Delegate)f64;
  256. return context.Call(dlg, callArgs);
  257. }
  258. public static Operand EmitSoftFloatCall(
  259. ArmEmitterContext context,
  260. _F32_F32_F32 f32,
  261. _F64_F64_F64 f64,
  262. params Operand[] callArgs)
  263. {
  264. IOpCodeSimd op = (IOpCodeSimd)context.CurrOp;
  265. Delegate dlg = (op.Size & 1) == 0 ? (Delegate)f32 : (Delegate)f64;
  266. return context.Call(dlg, callArgs);
  267. }
  268. public static Operand EmitSoftFloatCall(
  269. ArmEmitterContext context,
  270. _F32_F32_F32_F32 f32,
  271. _F64_F64_F64_F64 f64,
  272. params Operand[] callArgs)
  273. {
  274. IOpCodeSimd op = (IOpCodeSimd)context.CurrOp;
  275. Delegate dlg = (op.Size & 1) == 0 ? (Delegate)f32 : (Delegate)f64;
  276. return context.Call(dlg, callArgs);
  277. }
  278. public static void EmitScalarBinaryOpByElemF(ArmEmitterContext context, Func2I emit)
  279. {
  280. OpCodeSimdRegElemF op = (OpCodeSimdRegElemF)context.CurrOp;
  281. OperandType type = (op.Size & 1) != 0 ? OperandType.FP64 : OperandType.FP32;
  282. Operand n = context.VectorExtract(type, GetVec(op.Rn), 0);
  283. Operand m = context.VectorExtract(type, GetVec(op.Rm), op.Index);
  284. context.Copy(GetVec(op.Rd), context.VectorInsert(context.VectorZero(), emit(n, m), 0));
  285. }
  286. public static void EmitScalarTernaryOpByElemF(ArmEmitterContext context, Func3I emit)
  287. {
  288. OpCodeSimdRegElemF op = (OpCodeSimdRegElemF)context.CurrOp;
  289. OperandType type = (op.Size & 1) != 0 ? OperandType.FP64 : OperandType.FP32;
  290. Operand d = context.VectorExtract(type, GetVec(op.Rd), 0);
  291. Operand n = context.VectorExtract(type, GetVec(op.Rn), 0);
  292. Operand m = context.VectorExtract(type, GetVec(op.Rm), op.Index);
  293. context.Copy(GetVec(op.Rd), context.VectorInsert(context.VectorZero(), emit(d, n, m), 0));
  294. }
  295. public static void EmitScalarUnaryOpSx(ArmEmitterContext context, Func1I emit)
  296. {
  297. OpCodeSimd op = (OpCodeSimd)context.CurrOp;
  298. Operand n = EmitVectorExtractSx(context, op.Rn, 0, op.Size);
  299. Operand d = EmitVectorInsert(context, context.VectorZero(), emit(n), 0, op.Size);
  300. context.Copy(GetVec(op.Rd), d);
  301. }
  302. public static void EmitScalarBinaryOpSx(ArmEmitterContext context, Func2I emit)
  303. {
  304. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  305. Operand n = EmitVectorExtractSx(context, op.Rn, 0, op.Size);
  306. Operand m = EmitVectorExtractSx(context, op.Rm, 0, op.Size);
  307. Operand d = EmitVectorInsert(context, context.VectorZero(), emit(n, m), 0, op.Size);
  308. context.Copy(GetVec(op.Rd), d);
  309. }
  310. public static void EmitScalarUnaryOpZx(ArmEmitterContext context, Func1I emit)
  311. {
  312. OpCodeSimd op = (OpCodeSimd)context.CurrOp;
  313. Operand n = EmitVectorExtractZx(context, op.Rn, 0, op.Size);
  314. Operand d = EmitVectorInsert(context, context.VectorZero(), emit(n), 0, op.Size);
  315. context.Copy(GetVec(op.Rd), d);
  316. }
  317. public static void EmitScalarBinaryOpZx(ArmEmitterContext context, Func2I emit)
  318. {
  319. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  320. Operand n = EmitVectorExtractZx(context, op.Rn, 0, op.Size);
  321. Operand m = EmitVectorExtractZx(context, op.Rm, 0, op.Size);
  322. Operand d = EmitVectorInsert(context, context.VectorZero(), emit(n, m), 0, op.Size);
  323. context.Copy(GetVec(op.Rd), d);
  324. }
  325. public static void EmitScalarTernaryOpZx(ArmEmitterContext context, Func3I emit)
  326. {
  327. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  328. Operand d = EmitVectorExtractZx(context, op.Rd, 0, op.Size);
  329. Operand n = EmitVectorExtractZx(context, op.Rn, 0, op.Size);
  330. Operand m = EmitVectorExtractZx(context, op.Rm, 0, op.Size);
  331. d = EmitVectorInsert(context, context.VectorZero(), emit(d, n, m), 0, op.Size);
  332. context.Copy(GetVec(op.Rd), d);
  333. }
  334. public static void EmitScalarUnaryOpF(ArmEmitterContext context, Func1I emit)
  335. {
  336. OpCodeSimd op = (OpCodeSimd)context.CurrOp;
  337. OperandType type = (op.Size & 1) != 0 ? OperandType.FP64 : OperandType.FP32;
  338. Operand n = context.VectorExtract(type, GetVec(op.Rn), 0);
  339. context.Copy(GetVec(op.Rd), context.VectorInsert(context.VectorZero(), emit(n), 0));
  340. }
  341. public static void EmitScalarBinaryOpF(ArmEmitterContext context, Func2I emit)
  342. {
  343. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  344. OperandType type = (op.Size & 1) != 0 ? OperandType.FP64 : OperandType.FP32;
  345. Operand n = context.VectorExtract(type, GetVec(op.Rn), 0);
  346. Operand m = context.VectorExtract(type, GetVec(op.Rm), 0);
  347. context.Copy(GetVec(op.Rd), context.VectorInsert(context.VectorZero(), emit(n, m), 0));
  348. }
  349. public static void EmitScalarTernaryRaOpF(ArmEmitterContext context, Func3I emit)
  350. {
  351. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  352. OperandType type = (op.Size & 1) != 0 ? OperandType.FP64 : OperandType.FP32;
  353. Operand a = context.VectorExtract(type, GetVec(op.Ra), 0);
  354. Operand n = context.VectorExtract(type, GetVec(op.Rn), 0);
  355. Operand m = context.VectorExtract(type, GetVec(op.Rm), 0);
  356. context.Copy(GetVec(op.Rd), context.VectorInsert(context.VectorZero(), emit(a, n, m), 0));
  357. }
  358. public static void EmitVectorUnaryOpF(ArmEmitterContext context, Func1I emit)
  359. {
  360. OpCodeSimd op = (OpCodeSimd)context.CurrOp;
  361. Operand res = context.VectorZero();
  362. int sizeF = op.Size & 1;
  363. OperandType type = sizeF != 0 ? OperandType.FP64 : OperandType.FP32;
  364. int elems = op.GetBytesCount() >> sizeF + 2;
  365. for (int index = 0; index < elems; index++)
  366. {
  367. Operand ne = context.VectorExtract(type, GetVec(op.Rn), index);
  368. res = context.VectorInsert(res, emit(ne), index);
  369. }
  370. context.Copy(GetVec(op.Rd), res);
  371. }
  372. public static void EmitVectorBinaryOpF(ArmEmitterContext context, Func2I emit)
  373. {
  374. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  375. Operand res = context.VectorZero();
  376. int sizeF = op.Size & 1;
  377. OperandType type = sizeF != 0 ? OperandType.FP64 : OperandType.FP32;
  378. int elems = op.GetBytesCount() >> sizeF + 2;
  379. for (int index = 0; index < elems; index++)
  380. {
  381. Operand ne = context.VectorExtract(type, GetVec(op.Rn), index);
  382. Operand me = context.VectorExtract(type, GetVec(op.Rm), index);
  383. res = context.VectorInsert(res, emit(ne, me), index);
  384. }
  385. context.Copy(GetVec(op.Rd), res);
  386. }
  387. public static void EmitVectorTernaryOpF(ArmEmitterContext context, Func3I emit)
  388. {
  389. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  390. Operand res = context.VectorZero();
  391. int sizeF = op.Size & 1;
  392. OperandType type = sizeF != 0 ? OperandType.FP64 : OperandType.FP32;
  393. int elems = op.GetBytesCount() >> sizeF + 2;
  394. for (int index = 0; index < elems; index++)
  395. {
  396. Operand de = context.VectorExtract(type, GetVec(op.Rd), index);
  397. Operand ne = context.VectorExtract(type, GetVec(op.Rn), index);
  398. Operand me = context.VectorExtract(type, GetVec(op.Rm), index);
  399. res = context.VectorInsert(res, emit(de, ne, me), index);
  400. }
  401. context.Copy(GetVec(op.Rd), res);
  402. }
  403. public static void EmitVectorBinaryOpByElemF(ArmEmitterContext context, Func2I emit)
  404. {
  405. OpCodeSimdRegElemF op = (OpCodeSimdRegElemF)context.CurrOp;
  406. Operand res = context.VectorZero();
  407. int sizeF = op.Size & 1;
  408. OperandType type = sizeF != 0 ? OperandType.FP64 : OperandType.FP32;
  409. int elems = op.GetBytesCount() >> sizeF + 2;
  410. for (int index = 0; index < elems; index++)
  411. {
  412. Operand ne = context.VectorExtract(type, GetVec(op.Rn), index);
  413. Operand me = context.VectorExtract(type, GetVec(op.Rm), op.Index);
  414. res = context.VectorInsert(res, emit(ne, me), index);
  415. }
  416. context.Copy(GetVec(op.Rd), res);
  417. }
  418. public static void EmitVectorTernaryOpByElemF(ArmEmitterContext context, Func3I emit)
  419. {
  420. OpCodeSimdRegElemF op = (OpCodeSimdRegElemF)context.CurrOp;
  421. Operand res = context.VectorZero();
  422. int sizeF = op.Size & 1;
  423. OperandType type = sizeF != 0 ? OperandType.FP64 : OperandType.FP32;
  424. int elems = op.GetBytesCount() >> sizeF + 2;
  425. for (int index = 0; index < elems; index++)
  426. {
  427. Operand de = context.VectorExtract(type, GetVec(op.Rd), index);
  428. Operand ne = context.VectorExtract(type, GetVec(op.Rn), index);
  429. Operand me = context.VectorExtract(type, GetVec(op.Rm), op.Index);
  430. res = context.VectorInsert(res, emit(de, ne, me), index);
  431. }
  432. context.Copy(GetVec(op.Rd), res);
  433. }
  434. public static void EmitVectorUnaryOpSx(ArmEmitterContext context, Func1I emit)
  435. {
  436. OpCodeSimd op = (OpCodeSimd)context.CurrOp;
  437. Operand res = context.VectorZero();
  438. int elems = op.GetBytesCount() >> op.Size;
  439. for (int index = 0; index < elems; index++)
  440. {
  441. Operand ne = EmitVectorExtractSx(context, op.Rn, index, op.Size);
  442. res = EmitVectorInsert(context, res, emit(ne), index, op.Size);
  443. }
  444. context.Copy(GetVec(op.Rd), res);
  445. }
  446. public static void EmitVectorBinaryOpSx(ArmEmitterContext context, Func2I emit)
  447. {
  448. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  449. Operand res = context.VectorZero();
  450. int elems = op.GetBytesCount() >> op.Size;
  451. for (int index = 0; index < elems; index++)
  452. {
  453. Operand ne = EmitVectorExtractSx(context, op.Rn, index, op.Size);
  454. Operand me = EmitVectorExtractSx(context, op.Rm, index, op.Size);
  455. res = EmitVectorInsert(context, res, emit(ne, me), index, op.Size);
  456. }
  457. context.Copy(GetVec(op.Rd), res);
  458. }
  459. public static void EmitVectorTernaryOpSx(ArmEmitterContext context, Func3I emit)
  460. {
  461. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  462. Operand res = context.VectorZero();
  463. int elems = op.GetBytesCount() >> op.Size;
  464. for (int index = 0; index < elems; index++)
  465. {
  466. Operand de = EmitVectorExtractSx(context, op.Rd, index, op.Size);
  467. Operand ne = EmitVectorExtractSx(context, op.Rn, index, op.Size);
  468. Operand me = EmitVectorExtractSx(context, op.Rm, index, op.Size);
  469. res = EmitVectorInsert(context, res, emit(de, ne, me), index, op.Size);
  470. }
  471. context.Copy(GetVec(op.Rd), res);
  472. }
  473. public static void EmitVectorUnaryOpZx(ArmEmitterContext context, Func1I emit)
  474. {
  475. OpCodeSimd op = (OpCodeSimd)context.CurrOp;
  476. Operand res = context.VectorZero();
  477. int elems = op.GetBytesCount() >> op.Size;
  478. for (int index = 0; index < elems; index++)
  479. {
  480. Operand ne = EmitVectorExtractZx(context, op.Rn, index, op.Size);
  481. res = EmitVectorInsert(context, res, emit(ne), index, op.Size);
  482. }
  483. context.Copy(GetVec(op.Rd), res);
  484. }
  485. public static void EmitVectorBinaryOpZx(ArmEmitterContext context, Func2I emit)
  486. {
  487. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  488. Operand res = context.VectorZero();
  489. int elems = op.GetBytesCount() >> op.Size;
  490. for (int index = 0; index < elems; index++)
  491. {
  492. Operand ne = EmitVectorExtractZx(context, op.Rn, index, op.Size);
  493. Operand me = EmitVectorExtractZx(context, op.Rm, index, op.Size);
  494. res = EmitVectorInsert(context, res, emit(ne, me), index, op.Size);
  495. }
  496. context.Copy(GetVec(op.Rd), res);
  497. }
  498. public static void EmitVectorTernaryOpZx(ArmEmitterContext context, Func3I emit)
  499. {
  500. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  501. Operand res = context.VectorZero();
  502. int elems = op.GetBytesCount() >> op.Size;
  503. for (int index = 0; index < elems; index++)
  504. {
  505. Operand de = EmitVectorExtractZx(context, op.Rd, index, op.Size);
  506. Operand ne = EmitVectorExtractZx(context, op.Rn, index, op.Size);
  507. Operand me = EmitVectorExtractZx(context, op.Rm, index, op.Size);
  508. res = EmitVectorInsert(context, res, emit(de, ne, me), index, op.Size);
  509. }
  510. context.Copy(GetVec(op.Rd), res);
  511. }
  512. public static void EmitVectorBinaryOpByElemSx(ArmEmitterContext context, Func2I emit)
  513. {
  514. OpCodeSimdRegElem op = (OpCodeSimdRegElem)context.CurrOp;
  515. Operand res = context.VectorZero();
  516. Operand me = EmitVectorExtractSx(context, op.Rm, op.Index, op.Size);
  517. int elems = op.GetBytesCount() >> op.Size;
  518. for (int index = 0; index < elems; index++)
  519. {
  520. Operand ne = EmitVectorExtractSx(context, op.Rn, index, op.Size);
  521. res = EmitVectorInsert(context, res, emit(ne, me), index, op.Size);
  522. }
  523. context.Copy(GetVec(op.Rd), res);
  524. }
  525. public static void EmitVectorBinaryOpByElemZx(ArmEmitterContext context, Func2I emit)
  526. {
  527. OpCodeSimdRegElem op = (OpCodeSimdRegElem)context.CurrOp;
  528. Operand res = context.VectorZero();
  529. Operand me = EmitVectorExtractZx(context, op.Rm, op.Index, op.Size);
  530. int elems = op.GetBytesCount() >> op.Size;
  531. for (int index = 0; index < elems; index++)
  532. {
  533. Operand ne = EmitVectorExtractZx(context, op.Rn, index, op.Size);
  534. res = EmitVectorInsert(context, res, emit(ne, me), index, op.Size);
  535. }
  536. context.Copy(GetVec(op.Rd), res);
  537. }
  538. public static void EmitVectorTernaryOpByElemZx(ArmEmitterContext context, Func3I emit)
  539. {
  540. OpCodeSimdRegElem op = (OpCodeSimdRegElem)context.CurrOp;
  541. Operand res = context.VectorZero();
  542. Operand me = EmitVectorExtractZx(context, op.Rm, op.Index, op.Size);
  543. int elems = op.GetBytesCount() >> op.Size;
  544. for (int index = 0; index < elems; index++)
  545. {
  546. Operand de = EmitVectorExtractZx(context, op.Rd, index, op.Size);
  547. Operand ne = EmitVectorExtractZx(context, op.Rn, index, op.Size);
  548. res = EmitVectorInsert(context, res, emit(de, ne, me), index, op.Size);
  549. }
  550. context.Copy(GetVec(op.Rd), res);
  551. }
  552. public static void EmitVectorImmUnaryOp(ArmEmitterContext context, Func1I emit)
  553. {
  554. OpCodeSimdImm op = (OpCodeSimdImm)context.CurrOp;
  555. Operand imm = Const(op.Immediate);
  556. Operand res = context.VectorZero();
  557. int elems = op.GetBytesCount() >> op.Size;
  558. for (int index = 0; index < elems; index++)
  559. {
  560. res = EmitVectorInsert(context, res, emit(imm), index, op.Size);
  561. }
  562. context.Copy(GetVec(op.Rd), res);
  563. }
  564. public static void EmitVectorImmBinaryOp(ArmEmitterContext context, Func2I emit)
  565. {
  566. OpCodeSimdImm op = (OpCodeSimdImm)context.CurrOp;
  567. Operand imm = Const(op.Immediate);
  568. Operand res = context.VectorZero();
  569. int elems = op.GetBytesCount() >> op.Size;
  570. for (int index = 0; index < elems; index++)
  571. {
  572. Operand de = EmitVectorExtractZx(context, op.Rd, index, op.Size);
  573. res = EmitVectorInsert(context, res, emit(de, imm), index, op.Size);
  574. }
  575. context.Copy(GetVec(op.Rd), res);
  576. }
  577. public static void EmitVectorWidenRmBinaryOpSx(ArmEmitterContext context, Func2I emit)
  578. {
  579. EmitVectorWidenRmBinaryOp(context, emit, signed: true);
  580. }
  581. public static void EmitVectorWidenRmBinaryOpZx(ArmEmitterContext context, Func2I emit)
  582. {
  583. EmitVectorWidenRmBinaryOp(context, emit, signed: false);
  584. }
  585. private static void EmitVectorWidenRmBinaryOp(ArmEmitterContext context, Func2I emit, bool signed)
  586. {
  587. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  588. Operand res = context.VectorZero();
  589. int elems = 8 >> op.Size;
  590. int part = op.RegisterSize == RegisterSize.Simd128 ? elems : 0;
  591. for (int index = 0; index < elems; index++)
  592. {
  593. Operand ne = EmitVectorExtract(context, op.Rn, index, op.Size + 1, signed);
  594. Operand me = EmitVectorExtract(context, op.Rm, part + index, op.Size, signed);
  595. res = EmitVectorInsert(context, res, emit(ne, me), index, op.Size + 1);
  596. }
  597. context.Copy(GetVec(op.Rd), res);
  598. }
  599. public static void EmitVectorWidenRnRmBinaryOpSx(ArmEmitterContext context, Func2I emit)
  600. {
  601. EmitVectorWidenRnRmBinaryOp(context, emit, signed: true);
  602. }
  603. public static void EmitVectorWidenRnRmBinaryOpZx(ArmEmitterContext context, Func2I emit)
  604. {
  605. EmitVectorWidenRnRmBinaryOp(context, emit, signed: false);
  606. }
  607. private static void EmitVectorWidenRnRmBinaryOp(ArmEmitterContext context, Func2I emit, bool signed)
  608. {
  609. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  610. Operand res = context.VectorZero();
  611. int elems = 8 >> op.Size;
  612. int part = op.RegisterSize == RegisterSize.Simd128 ? elems : 0;
  613. for (int index = 0; index < elems; index++)
  614. {
  615. Operand ne = EmitVectorExtract(context, op.Rn, part + index, op.Size, signed);
  616. Operand me = EmitVectorExtract(context, op.Rm, part + index, op.Size, signed);
  617. res = EmitVectorInsert(context, res, emit(ne, me), index, op.Size + 1);
  618. }
  619. context.Copy(GetVec(op.Rd), res);
  620. }
  621. public static void EmitVectorWidenRnRmTernaryOpSx(ArmEmitterContext context, Func3I emit)
  622. {
  623. EmitVectorWidenRnRmTernaryOp(context, emit, signed: true);
  624. }
  625. public static void EmitVectorWidenRnRmTernaryOpZx(ArmEmitterContext context, Func3I emit)
  626. {
  627. EmitVectorWidenRnRmTernaryOp(context, emit, signed: false);
  628. }
  629. private static void EmitVectorWidenRnRmTernaryOp(ArmEmitterContext context, Func3I emit, bool signed)
  630. {
  631. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  632. Operand res = context.VectorZero();
  633. int elems = 8 >> op.Size;
  634. int part = op.RegisterSize == RegisterSize.Simd128 ? elems : 0;
  635. for (int index = 0; index < elems; index++)
  636. {
  637. Operand de = EmitVectorExtract(context, op.Rd, index, op.Size + 1, signed);
  638. Operand ne = EmitVectorExtract(context, op.Rn, part + index, op.Size, signed);
  639. Operand me = EmitVectorExtract(context, op.Rm, part + index, op.Size, signed);
  640. res = EmitVectorInsert(context, res, emit(de, ne, me), index, op.Size + 1);
  641. }
  642. context.Copy(GetVec(op.Rd), res);
  643. }
  644. public static void EmitVectorWidenBinaryOpByElemSx(ArmEmitterContext context, Func2I emit)
  645. {
  646. EmitVectorWidenBinaryOpByElem(context, emit, signed: true);
  647. }
  648. public static void EmitVectorWidenBinaryOpByElemZx(ArmEmitterContext context, Func2I emit)
  649. {
  650. EmitVectorWidenBinaryOpByElem(context, emit, signed: false);
  651. }
  652. private static void EmitVectorWidenBinaryOpByElem(ArmEmitterContext context, Func2I emit, bool signed)
  653. {
  654. OpCodeSimdRegElem op = (OpCodeSimdRegElem)context.CurrOp;
  655. Operand res = context.VectorZero();
  656. Operand me = EmitVectorExtract(context, op.Rm, op.Index, op.Size, signed);
  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, part + index, op.Size, signed);
  662. res = EmitVectorInsert(context, res, emit(ne, me), index, op.Size + 1);
  663. }
  664. context.Copy(GetVec(op.Rd), res);
  665. }
  666. public static void EmitVectorWidenTernaryOpByElemSx(ArmEmitterContext context, Func3I emit)
  667. {
  668. EmitVectorWidenTernaryOpByElem(context, emit, signed: true);
  669. }
  670. public static void EmitVectorWidenTernaryOpByElemZx(ArmEmitterContext context, Func3I emit)
  671. {
  672. EmitVectorWidenTernaryOpByElem(context, emit, signed: false);
  673. }
  674. private static void EmitVectorWidenTernaryOpByElem(ArmEmitterContext context, Func3I emit, bool signed)
  675. {
  676. OpCodeSimdRegElem op = (OpCodeSimdRegElem)context.CurrOp;
  677. Operand res = context.VectorZero();
  678. Operand me = EmitVectorExtract(context, op.Rm, op.Index, op.Size, signed);
  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 de = EmitVectorExtract(context, op.Rd, index, op.Size + 1, signed);
  684. Operand ne = EmitVectorExtract(context, op.Rn, part + index, op.Size, signed);
  685. res = EmitVectorInsert(context, res, emit(de, ne, me), index, op.Size + 1);
  686. }
  687. context.Copy(GetVec(op.Rd), res);
  688. }
  689. public static void EmitVectorPairwiseOpSx(ArmEmitterContext context, Func2I emit)
  690. {
  691. EmitVectorPairwiseOp(context, emit, signed: true);
  692. }
  693. public static void EmitVectorPairwiseOpZx(ArmEmitterContext context, Func2I emit)
  694. {
  695. EmitVectorPairwiseOp(context, emit, signed: false);
  696. }
  697. private static void EmitVectorPairwiseOp(ArmEmitterContext context, Func2I emit, bool signed)
  698. {
  699. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  700. Operand res = context.VectorZero();
  701. int pairs = op.GetPairsCount() >> op.Size;
  702. for (int index = 0; index < pairs; index++)
  703. {
  704. int pairIndex = index << 1;
  705. Operand n0 = EmitVectorExtract(context, op.Rn, pairIndex, op.Size, signed);
  706. Operand n1 = EmitVectorExtract(context, op.Rn, pairIndex + 1, op.Size, signed);
  707. Operand m0 = EmitVectorExtract(context, op.Rm, pairIndex, op.Size, signed);
  708. Operand m1 = EmitVectorExtract(context, op.Rm, pairIndex + 1, op.Size, signed);
  709. res = EmitVectorInsert(context, res, emit(n0, n1), index, op.Size);
  710. res = EmitVectorInsert(context, res, emit(m0, m1), pairs + index, op.Size);
  711. }
  712. context.Copy(GetVec(op.Rd), res);
  713. }
  714. public static void EmitVectorAcrossVectorOpSx(ArmEmitterContext context, Func2I emit)
  715. {
  716. EmitVectorAcrossVectorOp(context, emit, signed: true, isLong: false);
  717. }
  718. public static void EmitVectorAcrossVectorOpZx(ArmEmitterContext context, Func2I emit)
  719. {
  720. EmitVectorAcrossVectorOp(context, emit, signed: false, isLong: false);
  721. }
  722. public static void EmitVectorLongAcrossVectorOpSx(ArmEmitterContext context, Func2I emit)
  723. {
  724. EmitVectorAcrossVectorOp(context, emit, signed: true, isLong: true);
  725. }
  726. public static void EmitVectorLongAcrossVectorOpZx(ArmEmitterContext context, Func2I emit)
  727. {
  728. EmitVectorAcrossVectorOp(context, emit, signed: false, isLong: true);
  729. }
  730. private static void EmitVectorAcrossVectorOp(
  731. ArmEmitterContext context,
  732. Func2I emit,
  733. bool signed,
  734. bool isLong)
  735. {
  736. OpCodeSimd op = (OpCodeSimd)context.CurrOp;
  737. int elems = op.GetBytesCount() >> op.Size;
  738. Operand res = EmitVectorExtract(context, op.Rn, 0, op.Size, signed);
  739. for (int index = 1; index < elems; index++)
  740. {
  741. Operand n = EmitVectorExtract(context, op.Rn, index, op.Size, signed);
  742. res = emit(res, n);
  743. }
  744. int size = isLong ? op.Size + 1 : op.Size;
  745. Operand d = EmitVectorInsert(context, context.VectorZero(), res, 0, size);
  746. context.Copy(GetVec(op.Rd), d);
  747. }
  748. public static void EmitVectorPairwiseOpF(ArmEmitterContext context, Func2I emit)
  749. {
  750. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  751. Operand res = context.VectorZero();
  752. int sizeF = op.Size & 1;
  753. OperandType type = sizeF != 0 ? OperandType.FP64 : OperandType.FP32;
  754. int pairs = op.GetPairsCount() >> sizeF + 2;
  755. for (int index = 0; index < pairs; index++)
  756. {
  757. int pairIndex = index << 1;
  758. Operand n0 = context.VectorExtract(type, GetVec(op.Rn), pairIndex);
  759. Operand n1 = context.VectorExtract(type, GetVec(op.Rn), pairIndex + 1);
  760. Operand m0 = context.VectorExtract(type, GetVec(op.Rm), pairIndex);
  761. Operand m1 = context.VectorExtract(type, GetVec(op.Rm), pairIndex + 1);
  762. res = context.VectorInsert(res, emit(n0, n1), index);
  763. res = context.VectorInsert(res, emit(m0, m1), pairs + index);
  764. }
  765. context.Copy(GetVec(op.Rd), res);
  766. }
  767. public static void EmitVectorPairwiseOpF(ArmEmitterContext context, Intrinsic inst32, Intrinsic inst64)
  768. {
  769. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  770. Operand n = GetVec(op.Rn);
  771. Operand m = GetVec(op.Rm);
  772. int sizeF = op.Size & 1;
  773. if (sizeF == 0)
  774. {
  775. if (op.RegisterSize == RegisterSize.Simd64)
  776. {
  777. Operand unpck = context.AddIntrinsic(Intrinsic.X86Unpcklps, n, m);
  778. Operand zero = context.VectorZero();
  779. Operand part0 = context.AddIntrinsic(Intrinsic.X86Movlhps, unpck, zero);
  780. Operand part1 = context.AddIntrinsic(Intrinsic.X86Movhlps, zero, unpck);
  781. context.Copy(GetVec(op.Rd), context.AddIntrinsic(inst32, part0, part1));
  782. }
  783. else /* if (op.RegisterSize == RegisterSize.Simd128) */
  784. {
  785. const int sm0 = 2 << 6 | 0 << 4 | 2 << 2 | 0 << 0;
  786. const int sm1 = 3 << 6 | 1 << 4 | 3 << 2 | 1 << 0;
  787. Operand part0 = context.AddIntrinsic(Intrinsic.X86Shufps, n, m, Const(sm0));
  788. Operand part1 = context.AddIntrinsic(Intrinsic.X86Shufps, n, m, Const(sm1));
  789. context.Copy(GetVec(op.Rd), context.AddIntrinsic(inst32, part0, part1));
  790. }
  791. }
  792. else /* if (sizeF == 1) */
  793. {
  794. Operand part0 = context.AddIntrinsic(Intrinsic.X86Unpcklpd, n, m);
  795. Operand part1 = context.AddIntrinsic(Intrinsic.X86Unpckhpd, n, m);
  796. context.Copy(GetVec(op.Rd), context.AddIntrinsic(inst64, part0, part1));
  797. }
  798. }
  799. public enum CmpCondition
  800. {
  801. // Legacy Sse.
  802. Equal = 0, // Ordered, non-signaling.
  803. LessThan = 1, // Ordered, signaling.
  804. LessThanOrEqual = 2, // Ordered, signaling.
  805. UnorderedQ = 3, // Non-signaling.
  806. NotLessThan = 5, // Unordered, signaling.
  807. NotLessThanOrEqual = 6, // Unordered, signaling.
  808. OrderedQ = 7, // Non-signaling.
  809. // Vex.
  810. GreaterThanOrEqual = 13, // Ordered, signaling.
  811. GreaterThan = 14, // Ordered, signaling.
  812. OrderedS = 23 // Signaling.
  813. }
  814. [Flags]
  815. public enum SaturatingFlags
  816. {
  817. Scalar = 1 << 0,
  818. Signed = 1 << 1,
  819. Add = 1 << 2,
  820. Sub = 1 << 3,
  821. Accumulate = 1 << 4,
  822. ScalarSx = Scalar | Signed,
  823. ScalarZx = Scalar,
  824. VectorSx = Signed,
  825. VectorZx = 0
  826. }
  827. public static void EmitScalarSaturatingUnaryOpSx(ArmEmitterContext context, Func1I emit)
  828. {
  829. EmitSaturatingUnaryOpSx(context, emit, SaturatingFlags.ScalarSx);
  830. }
  831. public static void EmitVectorSaturatingUnaryOpSx(ArmEmitterContext context, Func1I emit)
  832. {
  833. EmitSaturatingUnaryOpSx(context, emit, SaturatingFlags.VectorSx);
  834. }
  835. private static void EmitSaturatingUnaryOpSx(ArmEmitterContext context, Func1I emit, SaturatingFlags flags)
  836. {
  837. OpCodeSimd op = (OpCodeSimd)context.CurrOp;
  838. Operand res = context.VectorZero();
  839. bool scalar = (flags & SaturatingFlags.Scalar) != 0;
  840. int elems = !scalar ? op.GetBytesCount() >> op.Size : 1;
  841. for (int index = 0; index < elems; index++)
  842. {
  843. Operand ne = EmitVectorExtractSx(context, op.Rn, index, op.Size);
  844. Operand de;
  845. if (op.Size <= 2)
  846. {
  847. de = EmitSatQ(context, emit(ne), op.Size, signedSrc: true, signedDst: true);
  848. }
  849. else /* if (op.Size == 3) */
  850. {
  851. de = EmitUnarySignedSatQAbsOrNeg(context, emit(ne));
  852. }
  853. res = EmitVectorInsert(context, res, de, index, op.Size);
  854. }
  855. context.Copy(GetVec(op.Rd), res);
  856. }
  857. public static void EmitScalarSaturatingBinaryOpSx(ArmEmitterContext context, SaturatingFlags flags)
  858. {
  859. EmitSaturatingBinaryOp(context, null, SaturatingFlags.ScalarSx | flags);
  860. }
  861. public static void EmitScalarSaturatingBinaryOpZx(ArmEmitterContext context, SaturatingFlags flags)
  862. {
  863. EmitSaturatingBinaryOp(context, null, SaturatingFlags.ScalarZx | flags);
  864. }
  865. public static void EmitVectorSaturatingBinaryOpSx(ArmEmitterContext context, SaturatingFlags flags)
  866. {
  867. EmitSaturatingBinaryOp(context, null, SaturatingFlags.VectorSx | flags);
  868. }
  869. public static void EmitVectorSaturatingBinaryOpZx(ArmEmitterContext context, SaturatingFlags flags)
  870. {
  871. EmitSaturatingBinaryOp(context, null, SaturatingFlags.VectorZx | flags);
  872. }
  873. public static void EmitSaturatingBinaryOp(ArmEmitterContext context, Func2I emit, SaturatingFlags flags)
  874. {
  875. OpCodeSimd op = (OpCodeSimd)context.CurrOp;
  876. Operand res = context.VectorZero();
  877. bool scalar = (flags & SaturatingFlags.Scalar) != 0;
  878. bool signed = (flags & SaturatingFlags.Signed) != 0;
  879. bool add = (flags & SaturatingFlags.Add) != 0;
  880. bool sub = (flags & SaturatingFlags.Sub) != 0;
  881. bool accumulate = (flags & SaturatingFlags.Accumulate) != 0;
  882. int elems = !scalar ? op.GetBytesCount() >> op.Size : 1;
  883. if (add || sub)
  884. {
  885. OpCodeSimdReg opReg = (OpCodeSimdReg)op;
  886. for (int index = 0; index < elems; index++)
  887. {
  888. Operand de;
  889. Operand ne = EmitVectorExtract(context, opReg.Rn, index, op.Size, signed);
  890. Operand me = EmitVectorExtract(context, opReg.Rm, index, op.Size, signed);
  891. if (op.Size <= 2)
  892. {
  893. Operand temp = add ? context.Add (ne, me)
  894. : context.Subtract(ne, me);
  895. de = EmitSatQ(context, temp, op.Size, signedSrc: true, signedDst: signed);
  896. }
  897. else if (add) /* if (op.Size == 3) */
  898. {
  899. de = EmitBinarySatQAdd(context, ne, me, signed);
  900. }
  901. else /* if (sub) */
  902. {
  903. de = EmitBinarySatQSub(context, ne, me, signed);
  904. }
  905. res = EmitVectorInsert(context, res, de, index, op.Size);
  906. }
  907. }
  908. else if (accumulate)
  909. {
  910. for (int index = 0; index < elems; index++)
  911. {
  912. Operand de;
  913. Operand ne = EmitVectorExtract(context, op.Rn, index, op.Size, !signed);
  914. Operand me = EmitVectorExtract(context, op.Rd, index, op.Size, signed);
  915. if (op.Size <= 2)
  916. {
  917. Operand temp = context.Add(ne, me);
  918. de = EmitSatQ(context, temp, op.Size, signedSrc: true, signedDst: signed);
  919. }
  920. else /* if (op.Size == 3) */
  921. {
  922. de = EmitBinarySatQAccumulate(context, ne, me, signed);
  923. }
  924. res = EmitVectorInsert(context, res, de, index, op.Size);
  925. }
  926. }
  927. else
  928. {
  929. OpCodeSimdReg opReg = (OpCodeSimdReg)op;
  930. for (int index = 0; index < elems; index++)
  931. {
  932. Operand ne = EmitVectorExtract(context, opReg.Rn, index, op.Size, signed);
  933. Operand me = EmitVectorExtract(context, opReg.Rm, index, op.Size, signed);
  934. Operand de = EmitSatQ(context, emit(ne, me), op.Size, true, signed);
  935. res = EmitVectorInsert(context, res, de, index, op.Size);
  936. }
  937. }
  938. context.Copy(GetVec(op.Rd), res);
  939. }
  940. [Flags]
  941. public enum SaturatingNarrowFlags
  942. {
  943. Scalar = 1 << 0,
  944. SignedSrc = 1 << 1,
  945. SignedDst = 1 << 2,
  946. ScalarSxSx = Scalar | SignedSrc | SignedDst,
  947. ScalarSxZx = Scalar | SignedSrc,
  948. ScalarZxZx = Scalar,
  949. VectorSxSx = SignedSrc | SignedDst,
  950. VectorSxZx = SignedSrc,
  951. VectorZxZx = 0
  952. }
  953. public static void EmitSaturatingNarrowOp(ArmEmitterContext context, SaturatingNarrowFlags flags)
  954. {
  955. OpCodeSimd op = (OpCodeSimd)context.CurrOp;
  956. bool scalar = (flags & SaturatingNarrowFlags.Scalar) != 0;
  957. bool signedSrc = (flags & SaturatingNarrowFlags.SignedSrc) != 0;
  958. bool signedDst = (flags & SaturatingNarrowFlags.SignedDst) != 0;
  959. int elems = !scalar ? 8 >> op.Size : 1;
  960. int part = !scalar && (op.RegisterSize == RegisterSize.Simd128) ? elems : 0;
  961. Operand res = part == 0 ? context.VectorZero() : context.Copy(GetVec(op.Rd));
  962. for (int index = 0; index < elems; index++)
  963. {
  964. Operand ne = EmitVectorExtract(context, op.Rn, index, op.Size + 1, signedSrc);
  965. Operand temp = EmitSatQ(context, ne, op.Size, signedSrc, signedDst);
  966. res = EmitVectorInsert(context, res, temp, part + index, op.Size);
  967. }
  968. context.Copy(GetVec(op.Rd), res);
  969. }
  970. // TSrc (16bit, 32bit, 64bit; signed, unsigned) > TDst (8bit, 16bit, 32bit; signed, unsigned).
  971. public static Operand EmitSatQ(ArmEmitterContext context, Operand op, int sizeDst, bool signedSrc, bool signedDst)
  972. {
  973. if ((uint)sizeDst > 2u)
  974. {
  975. throw new ArgumentOutOfRangeException(nameof(sizeDst));
  976. }
  977. Delegate dlg;
  978. if (signedSrc)
  979. {
  980. dlg = signedDst
  981. ? (Delegate)new _S64_S64_S32(SoftFallback.SignedSrcSignedDstSatQ)
  982. : (Delegate)new _U64_S64_S32(SoftFallback.SignedSrcUnsignedDstSatQ);
  983. }
  984. else
  985. {
  986. dlg = signedDst
  987. ? (Delegate)new _S64_U64_S32(SoftFallback.UnsignedSrcSignedDstSatQ)
  988. : (Delegate)new _U64_U64_S32(SoftFallback.UnsignedSrcUnsignedDstSatQ);
  989. }
  990. return context.Call(dlg, op, Const(sizeDst));
  991. }
  992. // TSrc (64bit) == TDst (64bit); signed.
  993. public static Operand EmitUnarySignedSatQAbsOrNeg(ArmEmitterContext context, Operand op)
  994. {
  995. Debug.Assert(((OpCodeSimd)context.CurrOp).Size == 3, "Invalid element size.");
  996. return context.Call(new _S64_S64(SoftFallback.UnarySignedSatQAbsOrNeg), op);
  997. }
  998. // TSrcs (64bit) == TDst (64bit); signed, unsigned.
  999. public static Operand EmitBinarySatQAdd(ArmEmitterContext context, Operand op1, Operand op2, bool signed)
  1000. {
  1001. Debug.Assert(((OpCodeSimd)context.CurrOp).Size == 3, "Invalid element size.");
  1002. Delegate dlg = signed
  1003. ? (Delegate)new _S64_S64_S64(SoftFallback.BinarySignedSatQAdd)
  1004. : (Delegate)new _U64_U64_U64(SoftFallback.BinaryUnsignedSatQAdd);
  1005. return context.Call(dlg, op1, op2);
  1006. }
  1007. // TSrcs (64bit) == TDst (64bit); signed, unsigned.
  1008. public static Operand EmitBinarySatQSub(ArmEmitterContext context, Operand op1, Operand op2, bool signed)
  1009. {
  1010. Debug.Assert(((OpCodeSimd)context.CurrOp).Size == 3, "Invalid element size.");
  1011. Delegate dlg = signed
  1012. ? (Delegate)new _S64_S64_S64(SoftFallback.BinarySignedSatQSub)
  1013. : (Delegate)new _U64_U64_U64(SoftFallback.BinaryUnsignedSatQSub);
  1014. return context.Call(dlg, op1, op2);
  1015. }
  1016. // TSrcs (64bit) == TDst (64bit); signed, unsigned.
  1017. public static Operand EmitBinarySatQAccumulate(ArmEmitterContext context, Operand op1, Operand op2, bool signed)
  1018. {
  1019. Debug.Assert(((OpCodeSimd)context.CurrOp).Size == 3, "Invalid element size.");
  1020. Delegate dlg = signed
  1021. ? (Delegate)new _S64_U64_S64(SoftFallback.BinarySignedSatQAcc)
  1022. : (Delegate)new _U64_S64_U64(SoftFallback.BinaryUnsignedSatQAcc);
  1023. return context.Call(dlg, op1, op2);
  1024. }
  1025. public static Operand EmitVectorExtractSx(ArmEmitterContext context, int reg, int index, int size)
  1026. {
  1027. return EmitVectorExtract(context, reg, index, size, true);
  1028. }
  1029. public static Operand EmitVectorExtractZx(ArmEmitterContext context, int reg, int index, int size)
  1030. {
  1031. return EmitVectorExtract(context, reg, index, size, false);
  1032. }
  1033. public static Operand EmitVectorExtract(ArmEmitterContext context, int reg, int index, int size, bool signed)
  1034. {
  1035. ThrowIfInvalid(index, size);
  1036. Operand res = null;
  1037. switch (size)
  1038. {
  1039. case 0:
  1040. res = context.VectorExtract8(GetVec(reg), index);
  1041. break;
  1042. case 1:
  1043. res = context.VectorExtract16(GetVec(reg), index);
  1044. break;
  1045. case 2:
  1046. res = context.VectorExtract(OperandType.I32, GetVec(reg), index);
  1047. break;
  1048. case 3:
  1049. res = context.VectorExtract(OperandType.I64, GetVec(reg), index);
  1050. break;
  1051. }
  1052. if (signed)
  1053. {
  1054. switch (size)
  1055. {
  1056. case 0: res = context.SignExtend8 (OperandType.I64, res); break;
  1057. case 1: res = context.SignExtend16(OperandType.I64, res); break;
  1058. case 2: res = context.SignExtend32(OperandType.I64, res); break;
  1059. }
  1060. }
  1061. else
  1062. {
  1063. switch (size)
  1064. {
  1065. case 0: res = context.ZeroExtend8 (OperandType.I64, res); break;
  1066. case 1: res = context.ZeroExtend16(OperandType.I64, res); break;
  1067. case 2: res = context.ZeroExtend32(OperandType.I64, res); break;
  1068. }
  1069. }
  1070. return res;
  1071. }
  1072. public static Operand EmitVectorInsert(ArmEmitterContext context, Operand vector, Operand value, int index, int size)
  1073. {
  1074. ThrowIfInvalid(index, size);
  1075. if (size < 3)
  1076. {
  1077. value = context.ConvertI64ToI32(value);
  1078. }
  1079. switch (size)
  1080. {
  1081. case 0: vector = context.VectorInsert8 (vector, value, index); break;
  1082. case 1: vector = context.VectorInsert16(vector, value, index); break;
  1083. case 2: vector = context.VectorInsert (vector, value, index); break;
  1084. case 3: vector = context.VectorInsert (vector, value, index); break;
  1085. }
  1086. return vector;
  1087. }
  1088. private static void ThrowIfInvalid(int index, int size)
  1089. {
  1090. if ((uint)size > 3u)
  1091. {
  1092. throw new ArgumentOutOfRangeException(nameof(size));
  1093. }
  1094. if ((uint)index >= 16u >> size)
  1095. {
  1096. throw new ArgumentOutOfRangeException(nameof(index));
  1097. }
  1098. }
  1099. }
  1100. }