InstEmitSimdHelper32.cs 40 KB

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