InstEmitSimdHelper32.cs 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268
  1. using ARMeilleure.Decoders;
  2. using ARMeilleure.IntermediateRepresentation;
  3. using ARMeilleure.Translation;
  4. using System;
  5. using System.Diagnostics;
  6. using System.Reflection;
  7. using static ARMeilleure.Instructions.InstEmitHelper;
  8. using static ARMeilleure.Instructions.InstEmitSimdHelper;
  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 InstEmitSimdHelper32
  16. {
  17. public static (int, int) GetQuadwordAndSubindex(int index, RegisterSize size)
  18. {
  19. switch (size)
  20. {
  21. case RegisterSize.Simd128:
  22. return (index >> 1, 0);
  23. case RegisterSize.Simd64:
  24. case RegisterSize.Int64:
  25. return (index >> 1, index & 1);
  26. case RegisterSize.Int32:
  27. return (index >> 2, index & 3);
  28. }
  29. throw new ArgumentException("Unrecognized Vector Register Size.");
  30. }
  31. public static Operand ExtractScalar(ArmEmitterContext context, OperandType type, int reg)
  32. {
  33. Debug.Assert(type != OperandType.V128);
  34. if (type == OperandType.FP64 || type == OperandType.I64)
  35. {
  36. // From dreg.
  37. return context.VectorExtract(type, GetVecA32(reg >> 1), reg & 1);
  38. }
  39. else
  40. {
  41. // From sreg.
  42. return context.VectorExtract(type, GetVecA32(reg >> 2), reg & 3);
  43. }
  44. }
  45. public static void InsertScalar(ArmEmitterContext context, int reg, Operand value)
  46. {
  47. Debug.Assert(value.Type != OperandType.V128);
  48. Operand vec, insert;
  49. if (value.Type == OperandType.FP64 || value.Type == OperandType.I64)
  50. {
  51. // From dreg.
  52. vec = GetVecA32(reg >> 1);
  53. insert = context.VectorInsert(vec, value, reg & 1);
  54. }
  55. else
  56. {
  57. // From sreg.
  58. vec = GetVecA32(reg >> 2);
  59. insert = context.VectorInsert(vec, value, reg & 3);
  60. }
  61. context.Copy(vec, insert);
  62. }
  63. public static Operand ExtractElement(ArmEmitterContext context, int reg, int size, bool signed)
  64. {
  65. return EmitVectorExtract32(context, reg >> (4 - size), reg & ((16 >> size) - 1), size, signed);
  66. }
  67. public static void EmitVectorImmUnaryOp32(ArmEmitterContext context, Func1I emit)
  68. {
  69. IOpCode32SimdImm op = (IOpCode32SimdImm)context.CurrOp;
  70. Operand imm = Const(op.Immediate);
  71. int elems = op.Elems;
  72. (int index, int subIndex) = GetQuadwordAndSubindex(op.Vd, op.RegisterSize);
  73. Operand vec = GetVecA32(index);
  74. Operand res = vec;
  75. for (int item = 0; item < elems; item++)
  76. {
  77. res = EmitVectorInsert(context, res, emit(imm), item + subIndex * elems, op.Size);
  78. }
  79. context.Copy(vec, res);
  80. }
  81. public static void EmitScalarUnaryOpF32(ArmEmitterContext context, Func1I emit)
  82. {
  83. OpCode32SimdS op = (OpCode32SimdS)context.CurrOp;
  84. OperandType type = (op.Size & 1) != 0 ? OperandType.FP64 : OperandType.FP32;
  85. Operand m = ExtractScalar(context, type, op.Vm);
  86. InsertScalar(context, op.Vd, emit(m));
  87. }
  88. public static void EmitScalarBinaryOpF32(ArmEmitterContext context, Func2I emit)
  89. {
  90. OpCode32SimdRegS op = (OpCode32SimdRegS)context.CurrOp;
  91. OperandType type = (op.Size & 1) != 0 ? OperandType.FP64 : OperandType.FP32;
  92. Operand n = ExtractScalar(context, type, op.Vn);
  93. Operand m = ExtractScalar(context, type, op.Vm);
  94. InsertScalar(context, op.Vd, emit(n, m));
  95. }
  96. public static void EmitScalarBinaryOpI32(ArmEmitterContext context, Func2I emit)
  97. {
  98. OpCode32SimdRegS op = (OpCode32SimdRegS)context.CurrOp;
  99. OperandType type = (op.Size & 1) != 0 ? OperandType.I64 : OperandType.I32;
  100. if (op.Size < 2)
  101. {
  102. throw new NotSupportedException("Cannot perform a scalar SIMD operation on integers smaller than 32 bits.");
  103. }
  104. Operand n = ExtractScalar(context, type, op.Vn);
  105. Operand m = ExtractScalar(context, type, op.Vm);
  106. InsertScalar(context, op.Vd, emit(n, m));
  107. }
  108. public static void EmitScalarTernaryOpF32(ArmEmitterContext context, Func3I emit)
  109. {
  110. OpCode32SimdRegS op = (OpCode32SimdRegS)context.CurrOp;
  111. OperandType type = (op.Size & 1) != 0 ? OperandType.FP64 : OperandType.FP32;
  112. Operand a = ExtractScalar(context, type, op.Vd);
  113. Operand n = ExtractScalar(context, type, op.Vn);
  114. Operand m = ExtractScalar(context, type, op.Vm);
  115. InsertScalar(context, op.Vd, emit(a, n, m));
  116. }
  117. public static void EmitVectorUnaryOpF32(ArmEmitterContext context, Func1I emit)
  118. {
  119. OpCode32Simd op = (OpCode32Simd)context.CurrOp;
  120. int sizeF = op.Size & 1;
  121. OperandType type = sizeF != 0 ? OperandType.FP64 : OperandType.FP32;
  122. int elems = op.GetBytesCount() >> sizeF + 2;
  123. Operand res = GetVecA32(op.Qd);
  124. for (int index = 0; index < elems; index++)
  125. {
  126. Operand me = context.VectorExtract(type, GetVecA32(op.Qm), op.Fm + index);
  127. res = context.VectorInsert(res, emit(me), op.Fd + index);
  128. }
  129. context.Copy(GetVecA32(op.Qd), res);
  130. }
  131. public static void EmitVectorBinaryOpF32(ArmEmitterContext context, Func2I emit)
  132. {
  133. OpCode32SimdReg op = (OpCode32SimdReg)context.CurrOp;
  134. int sizeF = op.Size & 1;
  135. OperandType type = sizeF != 0 ? OperandType.FP64 : OperandType.FP32;
  136. int elems = op.GetBytesCount() >> (sizeF + 2);
  137. Operand res = GetVecA32(op.Qd);
  138. for (int index = 0; index < elems; index++)
  139. {
  140. Operand ne = context.VectorExtract(type, GetVecA32(op.Qn), op.Fn + index);
  141. Operand me = context.VectorExtract(type, GetVecA32(op.Qm), op.Fm + index);
  142. res = context.VectorInsert(res, emit(ne, me), op.Fd + index);
  143. }
  144. context.Copy(GetVecA32(op.Qd), res);
  145. }
  146. public static void EmitVectorTernaryOpF32(ArmEmitterContext context, Func3I emit)
  147. {
  148. OpCode32SimdReg op = (OpCode32SimdReg)context.CurrOp;
  149. int sizeF = op.Size & 1;
  150. OperandType type = sizeF != 0 ? OperandType.FP64 : OperandType.FP32;
  151. int elems = op.GetBytesCount() >> sizeF + 2;
  152. Operand res = GetVecA32(op.Qd);
  153. for (int index = 0; index < elems; index++)
  154. {
  155. Operand de = context.VectorExtract(type, GetVecA32(op.Qd), op.Fd + index);
  156. Operand ne = context.VectorExtract(type, GetVecA32(op.Qn), op.Fn + index);
  157. Operand me = context.VectorExtract(type, GetVecA32(op.Qm), op.Fm + index);
  158. res = context.VectorInsert(res, emit(de, ne, me), op.Fd + index);
  159. }
  160. context.Copy(GetVecA32(op.Qd), res);
  161. }
  162. // Integer
  163. public static void EmitVectorUnaryAccumulateOpI32(ArmEmitterContext context, Func1I emit, bool signed)
  164. {
  165. OpCode32Simd op = (OpCode32Simd)context.CurrOp;
  166. Operand res = GetVecA32(op.Qd);
  167. int elems = op.GetBytesCount() >> op.Size;
  168. for (int index = 0; index < elems; index++)
  169. {
  170. Operand de = EmitVectorExtract32(context, op.Qd, op.Id + index, op.Size, signed);
  171. Operand me = EmitVectorExtract32(context, op.Qm, op.Im + index, op.Size, signed);
  172. res = EmitVectorInsert(context, res, context.Add(de, emit(me)), op.Id + index, op.Size);
  173. }
  174. context.Copy(GetVecA32(op.Qd), res);
  175. }
  176. public static void EmitVectorUnaryOpI32(ArmEmitterContext context, Func1I emit, bool signed)
  177. {
  178. OpCode32Simd op = (OpCode32Simd)context.CurrOp;
  179. Operand res = GetVecA32(op.Qd);
  180. int elems = op.GetBytesCount() >> op.Size;
  181. for (int index = 0; index < elems; index++)
  182. {
  183. Operand me = EmitVectorExtract32(context, op.Qm, op.Im + index, op.Size, signed);
  184. res = EmitVectorInsert(context, res, emit(me), op.Id + index, op.Size);
  185. }
  186. context.Copy(GetVecA32(op.Qd), res);
  187. }
  188. public static void EmitVectorBinaryOpI32(ArmEmitterContext context, Func2I emit, bool signed)
  189. {
  190. OpCode32SimdReg op = (OpCode32SimdReg)context.CurrOp;
  191. Operand res = GetVecA32(op.Qd);
  192. int elems = op.GetBytesCount() >> op.Size;
  193. for (int index = 0; index < elems; index++)
  194. {
  195. Operand ne = EmitVectorExtract32(context, op.Qn, op.In + index, op.Size, signed);
  196. Operand me = EmitVectorExtract32(context, op.Qm, op.Im + index, op.Size, signed);
  197. res = EmitVectorInsert(context, res, emit(ne, me), op.Id + index, op.Size);
  198. }
  199. context.Copy(GetVecA32(op.Qd), res);
  200. }
  201. public static void EmitVectorBinaryLongOpI32(ArmEmitterContext context, Func2I emit, bool signed)
  202. {
  203. OpCode32SimdReg op = (OpCode32SimdReg)context.CurrOp;
  204. Operand res = context.VectorZero();
  205. int elems = op.GetBytesCount() >> op.Size;
  206. for (int index = 0; index < elems; index++)
  207. {
  208. Operand ne = EmitVectorExtract32(context, op.Qn, op.In + index, op.Size, signed);
  209. Operand me = EmitVectorExtract32(context, op.Qm, op.Im + index, op.Size, signed);
  210. if (op.Size == 2)
  211. {
  212. ne = signed ? context.SignExtend32(OperandType.I64, ne) : context.ZeroExtend32(OperandType.I64, ne);
  213. me = signed ? context.SignExtend32(OperandType.I64, me) : context.ZeroExtend32(OperandType.I64, me);
  214. }
  215. res = EmitVectorInsert(context, res, emit(ne, me), index, op.Size + 1);
  216. }
  217. context.Copy(GetVecA32(op.Qd), res);
  218. }
  219. public static void EmitVectorBinaryWideOpI32(ArmEmitterContext context, Func2I emit, bool signed)
  220. {
  221. OpCode32SimdReg op = (OpCode32SimdReg)context.CurrOp;
  222. Operand res = context.VectorZero();
  223. int elems = op.GetBytesCount() >> op.Size;
  224. for (int index = 0; index < elems; index++)
  225. {
  226. Operand ne = EmitVectorExtract32(context, op.Qn, op.In + index, op.Size + 1, signed);
  227. Operand me = EmitVectorExtract32(context, op.Qm, op.Im + index, op.Size, signed);
  228. if (op.Size == 2)
  229. {
  230. me = signed ? context.SignExtend32(OperandType.I64, me) : context.ZeroExtend32(OperandType.I64, me);
  231. }
  232. res = EmitVectorInsert(context, res, emit(ne, me), index, op.Size + 1);
  233. }
  234. context.Copy(GetVecA32(op.Qd), res);
  235. }
  236. public static void EmitVectorImmBinaryQdQmOpZx32(ArmEmitterContext context, Func2I emit)
  237. {
  238. EmitVectorImmBinaryQdQmOpI32(context, emit, false);
  239. }
  240. public static void EmitVectorImmBinaryQdQmOpSx32(ArmEmitterContext context, Func2I emit)
  241. {
  242. EmitVectorImmBinaryQdQmOpI32(context, emit, true);
  243. }
  244. public static void EmitVectorImmBinaryQdQmOpI32(ArmEmitterContext context, Func2I emit, bool signed)
  245. {
  246. OpCode32SimdShImm op = (OpCode32SimdShImm)context.CurrOp;
  247. Operand res = GetVecA32(op.Qd);
  248. int elems = op.GetBytesCount() >> op.Size;
  249. for (int index = 0; index < elems; index++)
  250. {
  251. Operand de = EmitVectorExtract32(context, op.Qd, op.Id + index, op.Size, signed);
  252. Operand me = EmitVectorExtract32(context, op.Qm, op.Im + index, op.Size, signed);
  253. res = EmitVectorInsert(context, res, emit(de, me), op.Id + index, op.Size);
  254. }
  255. context.Copy(GetVecA32(op.Qd), res);
  256. }
  257. public static void EmitVectorTernaryLongOpI32(ArmEmitterContext context, Func3I emit, bool signed)
  258. {
  259. OpCode32SimdReg op = (OpCode32SimdReg)context.CurrOp;
  260. Operand res = context.VectorZero();
  261. int elems = op.GetBytesCount() >> op.Size;
  262. for (int index = 0; index < elems; index++)
  263. {
  264. Operand de = EmitVectorExtract32(context, op.Qd, op.Id + index, op.Size + 1, signed);
  265. Operand ne = EmitVectorExtract32(context, op.Qn, op.In + index, op.Size, signed);
  266. Operand me = EmitVectorExtract32(context, op.Qm, op.Im + index, op.Size, signed);
  267. if (op.Size == 2)
  268. {
  269. ne = signed ? context.SignExtend32(OperandType.I64, ne) : context.ZeroExtend32(OperandType.I64, ne);
  270. me = signed ? context.SignExtend32(OperandType.I64, me) : context.ZeroExtend32(OperandType.I64, me);
  271. }
  272. res = EmitVectorInsert(context, res, emit(de, ne, me), index, op.Size + 1);
  273. }
  274. context.Copy(GetVecA32(op.Qd), res);
  275. }
  276. public static void EmitVectorTernaryOpI32(ArmEmitterContext context, Func3I emit, bool signed)
  277. {
  278. OpCode32SimdReg op = (OpCode32SimdReg)context.CurrOp;
  279. Operand res = GetVecA32(op.Qd);
  280. int elems = op.GetBytesCount() >> op.Size;
  281. for (int index = 0; index < elems; index++)
  282. {
  283. Operand de = EmitVectorExtract32(context, op.Qd, op.Id + index, op.Size, signed);
  284. Operand ne = EmitVectorExtract32(context, op.Qn, op.In + index, op.Size, signed);
  285. Operand me = EmitVectorExtract32(context, op.Qm, op.Im + index, op.Size, signed);
  286. res = EmitVectorInsert(context, res, emit(de, ne, me), op.Id + index, op.Size);
  287. }
  288. context.Copy(GetVecA32(op.Qd), res);
  289. }
  290. public static void EmitVectorUnaryOpSx32(ArmEmitterContext context, Func1I emit)
  291. {
  292. EmitVectorUnaryOpI32(context, emit, true);
  293. }
  294. public static void EmitVectorUnaryOpSx32(ArmEmitterContext context, Func1I emit, bool accumulate)
  295. {
  296. if (accumulate)
  297. {
  298. EmitVectorUnaryAccumulateOpI32(context, emit, true);
  299. }
  300. else
  301. {
  302. EmitVectorUnaryOpI32(context, emit, true);
  303. }
  304. }
  305. public static void EmitVectorBinaryOpSx32(ArmEmitterContext context, Func2I emit)
  306. {
  307. EmitVectorBinaryOpI32(context, emit, true);
  308. }
  309. public static void EmitVectorTernaryOpSx32(ArmEmitterContext context, Func3I emit)
  310. {
  311. EmitVectorTernaryOpI32(context, emit, true);
  312. }
  313. public static void EmitVectorUnaryOpZx32(ArmEmitterContext context, Func1I emit)
  314. {
  315. EmitVectorUnaryOpI32(context, emit, false);
  316. }
  317. public static void EmitVectorUnaryOpZx32(ArmEmitterContext context, Func1I emit, bool accumulate)
  318. {
  319. if (accumulate)
  320. {
  321. EmitVectorUnaryAccumulateOpI32(context, emit, false);
  322. }
  323. else
  324. {
  325. EmitVectorUnaryOpI32(context, emit, false);
  326. }
  327. }
  328. public static void EmitVectorBinaryOpZx32(ArmEmitterContext context, Func2I emit)
  329. {
  330. EmitVectorBinaryOpI32(context, emit, false);
  331. }
  332. public static void EmitVectorTernaryOpZx32(ArmEmitterContext context, Func3I emit)
  333. {
  334. EmitVectorTernaryOpI32(context, emit, false);
  335. }
  336. // Vector by scalar
  337. public static void EmitVectorByScalarOpF32(ArmEmitterContext context, Func2I emit)
  338. {
  339. OpCode32SimdRegElem op = (OpCode32SimdRegElem)context.CurrOp;
  340. int sizeF = op.Size & 1;
  341. OperandType type = sizeF != 0 ? OperandType.FP64 : OperandType.FP32;
  342. int elems = op.GetBytesCount() >> sizeF + 2;
  343. Operand m = ExtractScalar(context, type, op.Vm);
  344. Operand res = GetVecA32(op.Qd);
  345. for (int index = 0; index < elems; index++)
  346. {
  347. Operand ne = context.VectorExtract(type, GetVecA32(op.Qn), op.Fn + index);
  348. res = context.VectorInsert(res, emit(ne, m), op.Fd + index);
  349. }
  350. context.Copy(GetVecA32(op.Qd), res);
  351. }
  352. public static void EmitVectorByScalarOpI32(ArmEmitterContext context, Func2I emit, bool signed)
  353. {
  354. OpCode32SimdRegElem op = (OpCode32SimdRegElem)context.CurrOp;
  355. Operand m = ExtractElement(context, op.Vm, op.Size, signed);
  356. Operand res = GetVecA32(op.Qd);
  357. int elems = op.GetBytesCount() >> op.Size;
  358. for (int index = 0; index < elems; index++)
  359. {
  360. Operand ne = EmitVectorExtract32(context, op.Qn, op.In + index, op.Size, signed);
  361. res = EmitVectorInsert(context, res, emit(ne, m), op.Id + index, op.Size);
  362. }
  363. context.Copy(GetVecA32(op.Qd), res);
  364. }
  365. public static void EmitVectorByScalarLongOpI32(ArmEmitterContext context, Func2I emit, bool signed)
  366. {
  367. OpCode32SimdRegElem op = (OpCode32SimdRegElem)context.CurrOp;
  368. Operand m = ExtractElement(context, op.Vm, op.Size, signed);
  369. if (op.Size == 2)
  370. {
  371. m = signed ? context.SignExtend32(OperandType.I64, m) : context.ZeroExtend32(OperandType.I64, m);
  372. }
  373. Operand res = context.VectorZero();
  374. int elems = op.GetBytesCount() >> op.Size;
  375. for (int index = 0; index < elems; index++)
  376. {
  377. Operand ne = EmitVectorExtract32(context, op.Qn, op.In + index, op.Size, signed);
  378. if (op.Size == 2)
  379. {
  380. ne = signed ? context.SignExtend32(OperandType.I64, ne) : context.ZeroExtend32(OperandType.I64, ne);
  381. }
  382. res = EmitVectorInsert(context, res, emit(ne, m), index, op.Size + 1);
  383. }
  384. context.Copy(GetVecA32(op.Qd), res);
  385. }
  386. public static void EmitVectorsByScalarOpF32(ArmEmitterContext context, Func3I emit)
  387. {
  388. OpCode32SimdRegElem op = (OpCode32SimdRegElem)context.CurrOp;
  389. int sizeF = op.Size & 1;
  390. OperandType type = sizeF != 0 ? OperandType.FP64 : OperandType.FP32;
  391. int elems = op.GetBytesCount() >> sizeF + 2;
  392. Operand m = ExtractScalar(context, type, op.Vm);
  393. Operand res = GetVecA32(op.Qd);
  394. for (int index = 0; index < elems; index++)
  395. {
  396. Operand de = context.VectorExtract(type, GetVecA32(op.Qd), op.Fd + index);
  397. Operand ne = context.VectorExtract(type, GetVecA32(op.Qn), op.Fn + index);
  398. res = context.VectorInsert(res, emit(de, ne, m), op.Fd + index);
  399. }
  400. context.Copy(GetVecA32(op.Qd), res);
  401. }
  402. public static void EmitVectorsByScalarOpI32(ArmEmitterContext context, Func3I emit, bool signed)
  403. {
  404. OpCode32SimdRegElem op = (OpCode32SimdRegElem)context.CurrOp;
  405. Operand m = EmitVectorExtract32(context, op.Vm >> (4 - op.Size), op.Vm & ((1 << (4 - op.Size)) - 1), op.Size, signed);
  406. Operand res = GetVecA32(op.Qd);
  407. int elems = op.GetBytesCount() >> op.Size;
  408. for (int index = 0; index < elems; index++)
  409. {
  410. Operand de = EmitVectorExtract32(context, op.Qd, op.Id + index, op.Size, signed);
  411. Operand ne = EmitVectorExtract32(context, op.Qn, op.In + index, op.Size, signed);
  412. res = EmitVectorInsert(context, res, emit(de, ne, m), op.Id + index, op.Size);
  413. }
  414. context.Copy(GetVecA32(op.Qd), res);
  415. }
  416. // Pairwise
  417. public static void EmitVectorPairwiseOpF32(ArmEmitterContext context, Func2I emit)
  418. {
  419. OpCode32SimdReg op = (OpCode32SimdReg)context.CurrOp;
  420. int sizeF = op.Size & 1;
  421. OperandType type = sizeF != 0 ? OperandType.FP64 : OperandType.FP32;
  422. int elems = op.GetBytesCount() >> (sizeF + 2);
  423. int pairs = elems >> 1;
  424. Operand res = GetVecA32(op.Qd);
  425. Operand mvec = GetVecA32(op.Qm);
  426. Operand nvec = GetVecA32(op.Qn);
  427. for (int index = 0; index < pairs; index++)
  428. {
  429. int pairIndex = index << 1;
  430. Operand n1 = context.VectorExtract(type, nvec, op.Fn + pairIndex);
  431. Operand n2 = context.VectorExtract(type, nvec, op.Fn + pairIndex + 1);
  432. res = context.VectorInsert(res, emit(n1, n2), op.Fd + index);
  433. Operand m1 = context.VectorExtract(type, mvec, op.Fm + pairIndex);
  434. Operand m2 = context.VectorExtract(type, mvec, op.Fm + pairIndex + 1);
  435. res = context.VectorInsert(res, emit(m1, m2), op.Fd + index + pairs);
  436. }
  437. context.Copy(GetVecA32(op.Qd), res);
  438. }
  439. public static void EmitVectorPairwiseOpI32(ArmEmitterContext context, Func2I emit, bool signed)
  440. {
  441. OpCode32SimdReg op = (OpCode32SimdReg)context.CurrOp;
  442. int elems = op.GetBytesCount() >> op.Size;
  443. int pairs = elems >> 1;
  444. Operand res = GetVecA32(op.Qd);
  445. for (int index = 0; index < pairs; index++)
  446. {
  447. int pairIndex = index << 1;
  448. Operand n1 = EmitVectorExtract32(context, op.Qn, op.In + pairIndex, op.Size, signed);
  449. Operand n2 = EmitVectorExtract32(context, op.Qn, op.In + pairIndex + 1, op.Size, signed);
  450. Operand m1 = EmitVectorExtract32(context, op.Qm, op.Im + pairIndex, op.Size, signed);
  451. Operand m2 = EmitVectorExtract32(context, op.Qm, op.Im + pairIndex + 1, op.Size, signed);
  452. res = EmitVectorInsert(context, res, emit(n1, n2), op.Id + index, op.Size);
  453. res = EmitVectorInsert(context, res, emit(m1, m2), op.Id + index + pairs, op.Size);
  454. }
  455. context.Copy(GetVecA32(op.Qd), res);
  456. }
  457. public static void EmitVectorPairwiseLongOpI32(ArmEmitterContext context, Func2I emit, bool signed)
  458. {
  459. OpCode32Simd op = (OpCode32Simd)context.CurrOp;
  460. int elems = (op.Q ? 16 : 8) >> op.Size;
  461. int pairs = elems >> 1;
  462. int id = (op.Vd & 1) * pairs;
  463. Operand res = GetVecA32(op.Qd);
  464. for (int index = 0; index < pairs; index++)
  465. {
  466. int pairIndex = index << 1;
  467. Operand m1 = EmitVectorExtract32(context, op.Qm, op.Im + pairIndex, op.Size, signed);
  468. Operand m2 = EmitVectorExtract32(context, op.Qm, op.Im + pairIndex + 1, op.Size, signed);
  469. if (op.Size == 2)
  470. {
  471. m1 = signed ? context.SignExtend32(OperandType.I64, m1) : context.ZeroExtend32(OperandType.I64, m1);
  472. m2 = signed ? context.SignExtend32(OperandType.I64, m2) : context.ZeroExtend32(OperandType.I64, m2);
  473. }
  474. res = EmitVectorInsert(context, res, emit(m1, m2), id + index, op.Size + 1);
  475. }
  476. context.Copy(GetVecA32(op.Qd), res);
  477. }
  478. // Narrow
  479. public static void EmitVectorUnaryNarrowOp32(ArmEmitterContext context, Func1I emit, bool signed = false)
  480. {
  481. OpCode32Simd op = (OpCode32Simd)context.CurrOp;
  482. int elems = 8 >> op.Size; // Size contains the target element size. (for when it becomes a doubleword)
  483. Operand res = GetVecA32(op.Qd);
  484. int id = (op.Vd & 1) << (3 - op.Size); // Target doubleword base.
  485. for (int index = 0; index < elems; index++)
  486. {
  487. Operand m = EmitVectorExtract32(context, op.Qm, index, op.Size + 1, signed);
  488. res = EmitVectorInsert(context, res, emit(m), id + index, op.Size);
  489. }
  490. context.Copy(GetVecA32(op.Qd), res);
  491. }
  492. // Intrinsic Helpers
  493. public static Operand EmitMoveDoubleWordToSide(ArmEmitterContext context, Operand input, int originalV, int targetV)
  494. {
  495. Debug.Assert(input.Type == OperandType.V128);
  496. int originalSide = originalV & 1;
  497. int targetSide = targetV & 1;
  498. if (originalSide == targetSide)
  499. {
  500. return input;
  501. }
  502. if (targetSide == 1)
  503. {
  504. return context.AddIntrinsic(Intrinsic.X86Movlhps, input, input); // Low to high.
  505. }
  506. else
  507. {
  508. return context.AddIntrinsic(Intrinsic.X86Movhlps, input, input); // High to low.
  509. }
  510. }
  511. public static Operand EmitDoubleWordInsert(ArmEmitterContext context, Operand target, Operand value, int targetV)
  512. {
  513. Debug.Assert(target.Type == OperandType.V128 && value.Type == OperandType.V128);
  514. int targetSide = targetV & 1;
  515. int shuffleMask = 2;
  516. if (targetSide == 1)
  517. {
  518. return context.AddIntrinsic(Intrinsic.X86Shufpd, target, value, Const(shuffleMask));
  519. }
  520. else
  521. {
  522. return context.AddIntrinsic(Intrinsic.X86Shufpd, value, target, Const(shuffleMask));
  523. }
  524. }
  525. public static Operand EmitScalarInsert(ArmEmitterContext context, Operand target, Operand value, int reg, bool doubleWidth)
  526. {
  527. Debug.Assert(target.Type == OperandType.V128 && value.Type == OperandType.V128);
  528. // Insert from index 0 in value to index in target.
  529. int index = reg & (doubleWidth ? 1 : 3);
  530. if (doubleWidth)
  531. {
  532. if (index == 1)
  533. {
  534. return context.AddIntrinsic(Intrinsic.X86Movlhps, target, value); // Low to high.
  535. }
  536. else
  537. {
  538. return context.AddIntrinsic(Intrinsic.X86Shufpd, value, target, Const(2)); // Low to low, keep high from original.
  539. }
  540. }
  541. else
  542. {
  543. if (Optimizations.UseSse41)
  544. {
  545. return context.AddIntrinsic(Intrinsic.X86Insertps, target, value, Const(index << 4));
  546. }
  547. else
  548. {
  549. target = EmitSwapScalar(context, target, index, doubleWidth); // Swap value to replace into element 0.
  550. target = context.AddIntrinsic(Intrinsic.X86Movss, target, value); // Move the value into element 0 of the vector.
  551. return EmitSwapScalar(context, target, index, doubleWidth); // Swap new value back to the correct index.
  552. }
  553. }
  554. }
  555. public static Operand EmitSwapScalar(ArmEmitterContext context, Operand target, int reg, bool doubleWidth)
  556. {
  557. // Index into 0, 0 into index. This swap happens at the start of an A32 scalar op if required.
  558. int index = reg & (doubleWidth ? 1 : 3);
  559. if (index == 0) return target;
  560. if (doubleWidth)
  561. {
  562. int shuffleMask = 1; // Swap top and bottom. (b0 = 1, b1 = 0)
  563. return context.AddIntrinsic(Intrinsic.X86Shufpd, target, target, Const(shuffleMask));
  564. }
  565. else
  566. {
  567. int shuffleMask = (3 << 6) | (2 << 4) | (1 << 2) | index; // Swap index and 0. (others remain)
  568. shuffleMask &= ~(3 << (index * 2));
  569. return context.AddIntrinsic(Intrinsic.X86Shufps, target, target, Const(shuffleMask));
  570. }
  571. }
  572. // Vector Operand Templates
  573. public static void EmitVectorUnaryOpSimd32(ArmEmitterContext context, Func1I vectorFunc)
  574. {
  575. OpCode32Simd op = (OpCode32Simd)context.CurrOp;
  576. Operand m = GetVecA32(op.Qm);
  577. Operand d = GetVecA32(op.Qd);
  578. if (!op.Q) // Register swap: move relevant doubleword to destination side.
  579. {
  580. m = EmitMoveDoubleWordToSide(context, m, op.Vm, op.Vd);
  581. }
  582. Operand res = vectorFunc(m);
  583. if (!op.Q) // Register insert.
  584. {
  585. res = EmitDoubleWordInsert(context, d, res, op.Vd);
  586. }
  587. context.Copy(d, res);
  588. }
  589. public static void EmitVectorUnaryOpF32(ArmEmitterContext context, Intrinsic inst32, Intrinsic inst64)
  590. {
  591. OpCode32Simd op = (OpCode32Simd)context.CurrOp;
  592. Intrinsic inst = (op.Size & 1) != 0 ? inst64 : inst32;
  593. EmitVectorUnaryOpSimd32(context, (m) => context.AddIntrinsic(inst, m));
  594. }
  595. public static void EmitVectorBinaryOpSimd32(ArmEmitterContext context, Func2I vectorFunc, int side = -1)
  596. {
  597. OpCode32SimdReg op = (OpCode32SimdReg)context.CurrOp;
  598. Operand n = GetVecA32(op.Qn);
  599. Operand m = GetVecA32(op.Qm);
  600. Operand d = GetVecA32(op.Qd);
  601. if (side == -1)
  602. {
  603. side = op.Vd;
  604. }
  605. if (!op.Q) // Register swap: move relevant doubleword to destination side.
  606. {
  607. n = EmitMoveDoubleWordToSide(context, n, op.Vn, side);
  608. m = EmitMoveDoubleWordToSide(context, m, op.Vm, side);
  609. }
  610. Operand res = vectorFunc(n, m);
  611. if (!op.Q) // Register insert.
  612. {
  613. if (side != op.Vd)
  614. {
  615. res = EmitMoveDoubleWordToSide(context, res, side, op.Vd);
  616. }
  617. res = EmitDoubleWordInsert(context, d, res, op.Vd);
  618. }
  619. context.Copy(d, res);
  620. }
  621. public static void EmitVectorBinaryOpF32(ArmEmitterContext context, Intrinsic inst32, Intrinsic inst64)
  622. {
  623. OpCode32SimdReg op = (OpCode32SimdReg)context.CurrOp;
  624. Intrinsic inst = (op.Size & 1) != 0 ? inst64 : inst32;
  625. EmitVectorBinaryOpSimd32(context, (n, m) => context.AddIntrinsic(inst, n, m));
  626. }
  627. public static void EmitVectorTernaryOpSimd32(ArmEmitterContext context, Func3I vectorFunc)
  628. {
  629. OpCode32SimdReg op = (OpCode32SimdReg)context.CurrOp;
  630. Operand n = GetVecA32(op.Qn);
  631. Operand m = GetVecA32(op.Qm);
  632. Operand d = GetVecA32(op.Qd);
  633. Operand initialD = d;
  634. if (!op.Q) // Register swap: move relevant doubleword to destination side.
  635. {
  636. n = EmitMoveDoubleWordToSide(context, n, op.Vn, op.Vd);
  637. m = EmitMoveDoubleWordToSide(context, m, op.Vm, op.Vd);
  638. }
  639. Operand res = vectorFunc(d, n, m);
  640. if (!op.Q) // Register insert.
  641. {
  642. res = EmitDoubleWordInsert(context, initialD, res, op.Vd);
  643. }
  644. context.Copy(initialD, res);
  645. }
  646. public static void EmitVectorTernaryOpF32(ArmEmitterContext context, Intrinsic inst32pt1, Intrinsic inst64pt1, Intrinsic inst32pt2, Intrinsic inst64pt2)
  647. {
  648. OpCode32SimdReg op = (OpCode32SimdReg)context.CurrOp;
  649. Intrinsic inst1 = (op.Size & 1) != 0 ? inst64pt1 : inst32pt1;
  650. Intrinsic inst2 = (op.Size & 1) != 0 ? inst64pt2 : inst32pt2;
  651. EmitVectorTernaryOpSimd32(context, (d, n, m) =>
  652. {
  653. Operand res = context.AddIntrinsic(inst1, n, m);
  654. return res = context.AddIntrinsic(inst2, d, res);
  655. });
  656. }
  657. public static void EmitVectorTernaryOpF32(ArmEmitterContext context, Intrinsic inst32)
  658. {
  659. OpCode32SimdReg op = (OpCode32SimdReg)context.CurrOp;
  660. Debug.Assert((op.Size & 1) == 0);
  661. EmitVectorTernaryOpSimd32(context, (d, n, m) =>
  662. {
  663. return context.AddIntrinsic(inst32, d, n, m);
  664. });
  665. }
  666. public static void EmitScalarUnaryOpSimd32(ArmEmitterContext context, Func1I scalarFunc)
  667. {
  668. OpCode32SimdS op = (OpCode32SimdS)context.CurrOp;
  669. bool doubleSize = (op.Size & 1) != 0;
  670. int shift = doubleSize ? 1 : 2;
  671. Operand m = GetVecA32(op.Vm >> shift);
  672. Operand d = GetVecA32(op.Vd >> shift);
  673. m = EmitSwapScalar(context, m, op.Vm, doubleSize);
  674. Operand res = scalarFunc(m);
  675. // Insert scalar into vector.
  676. res = EmitScalarInsert(context, d, res, op.Vd, doubleSize);
  677. context.Copy(d, res);
  678. }
  679. public static void EmitScalarUnaryOpF32(ArmEmitterContext context, Intrinsic inst32, Intrinsic inst64)
  680. {
  681. OpCode32SimdS op = (OpCode32SimdS)context.CurrOp;
  682. Intrinsic inst = (op.Size & 1) != 0 ? inst64 : inst32;
  683. EmitScalarUnaryOpSimd32(context, (m) => (inst == 0) ? m : context.AddIntrinsic(inst, m));
  684. }
  685. public static void EmitScalarBinaryOpSimd32(ArmEmitterContext context, Func2I scalarFunc)
  686. {
  687. OpCode32SimdRegS op = (OpCode32SimdRegS)context.CurrOp;
  688. bool doubleSize = (op.Size & 1) != 0;
  689. int shift = doubleSize ? 1 : 2;
  690. Operand n = GetVecA32(op.Vn >> shift);
  691. Operand m = GetVecA32(op.Vm >> shift);
  692. Operand d = GetVecA32(op.Vd >> shift);
  693. n = EmitSwapScalar(context, n, op.Vn, doubleSize);
  694. m = EmitSwapScalar(context, m, op.Vm, doubleSize);
  695. Operand res = scalarFunc(n, m);
  696. // Insert scalar into vector.
  697. res = EmitScalarInsert(context, d, res, op.Vd, doubleSize);
  698. context.Copy(d, res);
  699. }
  700. public static void EmitScalarBinaryOpF32(ArmEmitterContext context, Intrinsic inst32, Intrinsic inst64)
  701. {
  702. OpCode32SimdRegS op = (OpCode32SimdRegS)context.CurrOp;
  703. Intrinsic inst = (op.Size & 1) != 0 ? inst64 : inst32;
  704. EmitScalarBinaryOpSimd32(context, (n, m) => context.AddIntrinsic(inst, n, m));
  705. }
  706. public static void EmitScalarTernaryOpSimd32(ArmEmitterContext context, Func3I scalarFunc)
  707. {
  708. OpCode32SimdRegS op = (OpCode32SimdRegS)context.CurrOp;
  709. bool doubleSize = (op.Size & 1) != 0;
  710. int shift = doubleSize ? 1 : 2;
  711. Operand n = GetVecA32(op.Vn >> shift);
  712. Operand m = GetVecA32(op.Vm >> shift);
  713. Operand d = GetVecA32(op.Vd >> shift);
  714. Operand initialD = d;
  715. n = EmitSwapScalar(context, n, op.Vn, doubleSize);
  716. m = EmitSwapScalar(context, m, op.Vm, doubleSize);
  717. d = EmitSwapScalar(context, d, op.Vd, doubleSize);
  718. Operand res = scalarFunc(d, n, m);
  719. // Insert scalar into vector.
  720. res = EmitScalarInsert(context, initialD, res, op.Vd, doubleSize);
  721. context.Copy(initialD, res);
  722. }
  723. public static void EmitScalarTernaryOpF32(ArmEmitterContext context, Intrinsic inst32, Intrinsic inst64)
  724. {
  725. OpCode32SimdRegS op = (OpCode32SimdRegS)context.CurrOp;
  726. bool doubleSize = (op.Size & 1) != 0;
  727. Intrinsic inst = doubleSize ? inst64 : inst32;
  728. EmitScalarTernaryOpSimd32(context, (d, n, m) =>
  729. {
  730. return context.AddIntrinsic(inst, d, n, m);
  731. });
  732. }
  733. public static void EmitScalarTernaryOpF32(
  734. ArmEmitterContext context,
  735. Intrinsic inst32pt1,
  736. Intrinsic inst64pt1,
  737. Intrinsic inst32pt2,
  738. Intrinsic inst64pt2,
  739. bool isNegD = false)
  740. {
  741. OpCode32SimdRegS op = (OpCode32SimdRegS)context.CurrOp;
  742. bool doubleSize = (op.Size & 1) != 0;
  743. Intrinsic inst1 = doubleSize ? inst64pt1 : inst32pt1;
  744. Intrinsic inst2 = doubleSize ? inst64pt2 : inst32pt2;
  745. EmitScalarTernaryOpSimd32(context, (d, n, m) =>
  746. {
  747. Operand res = context.AddIntrinsic(inst1, n, m);
  748. if (isNegD)
  749. {
  750. Operand mask = doubleSize
  751. ? X86GetScalar(context, -0d)
  752. : X86GetScalar(context, -0f);
  753. d = doubleSize
  754. ? context.AddIntrinsic(Intrinsic.X86Xorpd, mask, d)
  755. : context.AddIntrinsic(Intrinsic.X86Xorps, mask, d);
  756. }
  757. return context.AddIntrinsic(inst2, d, res);
  758. });
  759. }
  760. // By Scalar
  761. public static void EmitVectorByScalarOpSimd32(ArmEmitterContext context, Func2I vectorFunc)
  762. {
  763. OpCode32SimdRegElem op = (OpCode32SimdRegElem)context.CurrOp;
  764. Operand n = GetVecA32(op.Qn);
  765. Operand d = GetVecA32(op.Qd);
  766. int index = op.Vm & 3;
  767. int dupeMask = (index << 6) | (index << 4) | (index << 2) | index;
  768. Operand m = GetVecA32(op.Vm >> 2);
  769. m = context.AddIntrinsic(Intrinsic.X86Shufps, m, m, Const(dupeMask));
  770. if (!op.Q) // Register swap: move relevant doubleword to destination side.
  771. {
  772. n = EmitMoveDoubleWordToSide(context, n, op.Vn, op.Vd);
  773. }
  774. Operand res = vectorFunc(n, m);
  775. if (!op.Q) // Register insert.
  776. {
  777. res = EmitDoubleWordInsert(context, d, res, op.Vd);
  778. }
  779. context.Copy(d, res);
  780. }
  781. public static void EmitVectorByScalarOpF32(ArmEmitterContext context, Intrinsic inst32, Intrinsic inst64)
  782. {
  783. OpCode32SimdRegElem op = (OpCode32SimdRegElem)context.CurrOp;
  784. Intrinsic inst = (op.Size & 1) != 0 ? inst64 : inst32;
  785. EmitVectorByScalarOpSimd32(context, (n, m) => context.AddIntrinsic(inst, n, m));
  786. }
  787. public static void EmitVectorsByScalarOpSimd32(ArmEmitterContext context, Func3I vectorFunc)
  788. {
  789. OpCode32SimdRegElem op = (OpCode32SimdRegElem)context.CurrOp;
  790. Operand n = GetVecA32(op.Qn);
  791. Operand d = GetVecA32(op.Qd);
  792. Operand initialD = d;
  793. int index = op.Vm & 3;
  794. int dupeMask = (index << 6) | (index << 4) | (index << 2) | index;
  795. Operand m = GetVecA32(op.Vm >> 2);
  796. m = context.AddIntrinsic(Intrinsic.X86Shufps, m, m, Const(dupeMask));
  797. if (!op.Q) // Register swap: move relevant doubleword to destination side.
  798. {
  799. n = EmitMoveDoubleWordToSide(context, n, op.Vn, op.Vd);
  800. }
  801. Operand res = vectorFunc(d, n, m);
  802. if (!op.Q) // Register insert.
  803. {
  804. res = EmitDoubleWordInsert(context, initialD, res, op.Vd);
  805. }
  806. context.Copy(initialD, res);
  807. }
  808. public static void EmitVectorsByScalarOpF32(ArmEmitterContext context, Intrinsic inst32pt1, Intrinsic inst64pt1, Intrinsic inst32pt2, Intrinsic inst64pt2)
  809. {
  810. OpCode32SimdRegElem op = (OpCode32SimdRegElem)context.CurrOp;
  811. Intrinsic inst1 = (op.Size & 1) != 0 ? inst64pt1 : inst32pt1;
  812. Intrinsic inst2 = (op.Size & 1) != 0 ? inst64pt2 : inst32pt2;
  813. EmitVectorsByScalarOpSimd32(context, (d, n, m) =>
  814. {
  815. Operand res = context.AddIntrinsic(inst1, n, m);
  816. return res = context.AddIntrinsic(inst2, d, res);
  817. });
  818. }
  819. // Pairwise
  820. public static void EmitSse2VectorPairwiseOpF32(ArmEmitterContext context, Intrinsic inst32)
  821. {
  822. OpCode32SimdReg op = (OpCode32SimdReg)context.CurrOp;
  823. EmitVectorBinaryOpSimd32(context, (n, m) =>
  824. {
  825. Operand unpck = context.AddIntrinsic(Intrinsic.X86Unpcklps, n, m);
  826. Operand part0 = unpck;
  827. Operand part1 = context.AddIntrinsic(Intrinsic.X86Movhlps, unpck, unpck);
  828. return context.AddIntrinsic(inst32, part0, part1);
  829. }, 0);
  830. }
  831. public static void EmitSsse3VectorPairwiseOp32(ArmEmitterContext context, Intrinsic[] inst)
  832. {
  833. OpCode32SimdReg op = (OpCode32SimdReg)context.CurrOp;
  834. EmitVectorBinaryOpSimd32(context, (n, m) =>
  835. {
  836. if (op.RegisterSize == RegisterSize.Simd64)
  837. {
  838. Operand zeroEvenMask = X86GetElements(context, ZeroMask, EvenMasks[op.Size]);
  839. Operand zeroOddMask = X86GetElements(context, ZeroMask, OddMasks[op.Size]);
  840. Operand mN = context.AddIntrinsic(Intrinsic.X86Punpcklqdq, n, m); // m:n
  841. Operand left = context.AddIntrinsic(Intrinsic.X86Pshufb, mN, zeroEvenMask); // 0:even from m:n
  842. Operand right = context.AddIntrinsic(Intrinsic.X86Pshufb, mN, zeroOddMask); // 0:odd from m:n
  843. return context.AddIntrinsic(inst[op.Size], left, right);
  844. }
  845. else if (op.Size < 3)
  846. {
  847. Operand oddEvenMask = X86GetElements(context, OddMasks[op.Size], EvenMasks[op.Size]);
  848. Operand oddEvenN = context.AddIntrinsic(Intrinsic.X86Pshufb, n, oddEvenMask); // odd:even from n
  849. Operand oddEvenM = context.AddIntrinsic(Intrinsic.X86Pshufb, m, oddEvenMask); // odd:even from m
  850. Operand left = context.AddIntrinsic(Intrinsic.X86Punpcklqdq, oddEvenN, oddEvenM);
  851. Operand right = context.AddIntrinsic(Intrinsic.X86Punpckhqdq, oddEvenN, oddEvenM);
  852. return context.AddIntrinsic(inst[op.Size], left, right);
  853. }
  854. else
  855. {
  856. Operand left = context.AddIntrinsic(Intrinsic.X86Punpcklqdq, n, m);
  857. Operand right = context.AddIntrinsic(Intrinsic.X86Punpckhqdq, n, m);
  858. return context.AddIntrinsic(inst[3], left, right);
  859. }
  860. }, 0);
  861. }
  862. // Generic Functions
  863. public static Operand EmitSoftFloatCallDefaultFpscr(ArmEmitterContext context, string name, params Operand[] callArgs)
  864. {
  865. IOpCodeSimd op = (IOpCodeSimd)context.CurrOp;
  866. MethodInfo info = (op.Size & 1) == 0
  867. ? typeof(SoftFloat32).GetMethod(name)
  868. : typeof(SoftFloat64).GetMethod(name);
  869. Array.Resize(ref callArgs, callArgs.Length + 1);
  870. callArgs[callArgs.Length - 1] = Const(1);
  871. context.StoreToContext();
  872. Operand res = context.Call(info, callArgs);
  873. context.LoadFromContext();
  874. return res;
  875. }
  876. public static Operand EmitVectorExtractSx32(ArmEmitterContext context, int reg, int index, int size)
  877. {
  878. return EmitVectorExtract32(context, reg, index, size, true);
  879. }
  880. public static Operand EmitVectorExtractZx32(ArmEmitterContext context, int reg, int index, int size)
  881. {
  882. return EmitVectorExtract32(context, reg, index, size, false);
  883. }
  884. public static Operand EmitVectorExtract32(ArmEmitterContext context, int reg, int index, int size, bool signed)
  885. {
  886. ThrowIfInvalid(index, size);
  887. Operand res = default;
  888. switch (size)
  889. {
  890. case 0:
  891. res = context.VectorExtract8(GetVec(reg), index);
  892. break;
  893. case 1:
  894. res = context.VectorExtract16(GetVec(reg), index);
  895. break;
  896. case 2:
  897. res = context.VectorExtract(OperandType.I32, GetVec(reg), index);
  898. break;
  899. case 3:
  900. res = context.VectorExtract(OperandType.I64, GetVec(reg), index);
  901. break;
  902. }
  903. if (signed)
  904. {
  905. switch (size)
  906. {
  907. case 0: res = context.SignExtend8(OperandType.I32, res); break;
  908. case 1: res = context.SignExtend16(OperandType.I32, res); break;
  909. }
  910. }
  911. else
  912. {
  913. switch (size)
  914. {
  915. case 0: res = context.ZeroExtend8(OperandType.I32, res); break;
  916. case 1: res = context.ZeroExtend16(OperandType.I32, res); break;
  917. }
  918. }
  919. return res;
  920. }
  921. public static Operand EmitPolynomialMultiply(ArmEmitterContext context, Operand op1, Operand op2, int eSize)
  922. {
  923. Debug.Assert(eSize <= 32);
  924. Operand result = eSize == 32 ? Const(0L) : Const(0);
  925. if (eSize == 32)
  926. {
  927. op1 = context.ZeroExtend32(OperandType.I64, op1);
  928. op2 = context.ZeroExtend32(OperandType.I64, op2);
  929. }
  930. for (int i = 0; i < eSize; i++)
  931. {
  932. Operand mask = context.BitwiseAnd(op1, Const(op1.Type, 1L << i));
  933. result = context.BitwiseExclusiveOr(result, context.Multiply(op2, mask));
  934. }
  935. return result;
  936. }
  937. }
  938. }