InstEmitSimdHelper32.cs 46 KB

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