InstEmitSimdHelper32.cs 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098
  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.OperandHelper;
  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 EmitVectorUnaryOpI32(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 me = EmitVectorExtract32(context, op.Qm, op.Im + index, op.Size, signed);
  171. res = EmitVectorInsert(context, res, emit(me), op.Id + index, op.Size);
  172. }
  173. context.Copy(GetVecA32(op.Qd), res);
  174. }
  175. public static void EmitVectorBinaryOpI32(ArmEmitterContext context, Func2I emit, bool signed)
  176. {
  177. OpCode32SimdReg op = (OpCode32SimdReg)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 ne = EmitVectorExtract32(context, op.Qn, op.In + index, op.Size, signed);
  183. Operand me = EmitVectorExtract32(context, op.Qm, op.Im + index, op.Size, signed);
  184. res = EmitVectorInsert(context, res, emit(ne, me), op.Id + index, op.Size);
  185. }
  186. context.Copy(GetVecA32(op.Qd), res);
  187. }
  188. public static void EmitVectorBinaryLongOpI32(ArmEmitterContext context, Func2I emit, bool signed)
  189. {
  190. OpCode32SimdReg op = (OpCode32SimdReg)context.CurrOp;
  191. Operand res = context.VectorZero();
  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. if (op.Size == 2)
  198. {
  199. ne = signed ? context.SignExtend32(OperandType.I64, ne) : context.ZeroExtend32(OperandType.I64, ne);
  200. me = signed ? context.SignExtend32(OperandType.I64, me) : context.ZeroExtend32(OperandType.I64, me);
  201. }
  202. res = EmitVectorInsert(context, res, emit(ne, me), index, op.Size + 1);
  203. }
  204. context.Copy(GetVecA32(op.Qd), res);
  205. }
  206. public static void EmitVectorBinaryWideOpI32(ArmEmitterContext context, Func2I emit, bool signed)
  207. {
  208. OpCode32SimdReg op = (OpCode32SimdReg)context.CurrOp;
  209. Operand res = context.VectorZero();
  210. int elems = op.GetBytesCount() >> op.Size;
  211. for (int index = 0; index < elems; index++)
  212. {
  213. Operand ne = EmitVectorExtract32(context, op.Qn, op.In + index, op.Size + 1, signed);
  214. Operand me = EmitVectorExtract32(context, op.Qm, op.Im + index, op.Size, signed);
  215. if (op.Size == 2)
  216. {
  217. me = signed ? context.SignExtend32(OperandType.I64, me) : context.ZeroExtend32(OperandType.I64, me);
  218. }
  219. res = EmitVectorInsert(context, res, emit(ne, me), index, op.Size + 1);
  220. }
  221. context.Copy(GetVecA32(op.Qd), res);
  222. }
  223. public static void EmitVectorTernaryLongOpI32(ArmEmitterContext context, Func3I emit, bool signed)
  224. {
  225. OpCode32SimdReg op = (OpCode32SimdReg)context.CurrOp;
  226. Operand res = context.VectorZero();
  227. int elems = op.GetBytesCount() >> op.Size;
  228. for (int index = 0; index < elems; index++)
  229. {
  230. Operand de = EmitVectorExtract32(context, op.Qd, op.Id + index, op.Size + 1, signed);
  231. Operand ne = EmitVectorExtract32(context, op.Qn, op.In + index, op.Size, signed);
  232. Operand me = EmitVectorExtract32(context, op.Qm, op.Im + index, op.Size, signed);
  233. if (op.Size == 2)
  234. {
  235. ne = signed ? context.SignExtend32(OperandType.I64, ne) : context.ZeroExtend32(OperandType.I64, ne);
  236. me = signed ? context.SignExtend32(OperandType.I64, me) : context.ZeroExtend32(OperandType.I64, me);
  237. }
  238. res = EmitVectorInsert(context, res, emit(de, ne, me), index, op.Size + 1);
  239. }
  240. context.Copy(GetVecA32(op.Qd), res);
  241. }
  242. public static void EmitVectorTernaryOpI32(ArmEmitterContext context, Func3I emit, bool signed)
  243. {
  244. OpCode32SimdReg op = (OpCode32SimdReg)context.CurrOp;
  245. Operand res = GetVecA32(op.Qd);
  246. int elems = op.GetBytesCount() >> op.Size;
  247. for (int index = 0; index < elems; index++)
  248. {
  249. Operand de = EmitVectorExtract32(context, op.Qd, op.Id + index, op.Size, signed);
  250. Operand ne = EmitVectorExtract32(context, op.Qn, op.In + index, op.Size, signed);
  251. Operand me = EmitVectorExtract32(context, op.Qm, op.Im + index, op.Size, signed);
  252. res = EmitVectorInsert(context, res, emit(de, ne, me), op.Id + index, op.Size);
  253. }
  254. context.Copy(GetVecA32(op.Qd), res);
  255. }
  256. public static void EmitVectorUnaryOpSx32(ArmEmitterContext context, Func1I emit)
  257. {
  258. EmitVectorUnaryOpI32(context, emit, true);
  259. }
  260. public static void EmitVectorBinaryOpSx32(ArmEmitterContext context, Func2I emit)
  261. {
  262. EmitVectorBinaryOpI32(context, emit, true);
  263. }
  264. public static void EmitVectorTernaryOpSx32(ArmEmitterContext context, Func3I emit)
  265. {
  266. EmitVectorTernaryOpI32(context, emit, true);
  267. }
  268. public static void EmitVectorUnaryOpZx32(ArmEmitterContext context, Func1I emit)
  269. {
  270. EmitVectorUnaryOpI32(context, emit, false);
  271. }
  272. public static void EmitVectorBinaryOpZx32(ArmEmitterContext context, Func2I emit)
  273. {
  274. EmitVectorBinaryOpI32(context, emit, false);
  275. }
  276. public static void EmitVectorTernaryOpZx32(ArmEmitterContext context, Func3I emit)
  277. {
  278. EmitVectorTernaryOpI32(context, emit, false);
  279. }
  280. // Vector by scalar
  281. public static void EmitVectorByScalarOpF32(ArmEmitterContext context, Func2I emit)
  282. {
  283. OpCode32SimdRegElem op = (OpCode32SimdRegElem)context.CurrOp;
  284. int sizeF = op.Size & 1;
  285. OperandType type = sizeF != 0 ? OperandType.FP64 : OperandType.FP32;
  286. int elems = op.GetBytesCount() >> sizeF + 2;
  287. Operand m = ExtractScalar(context, type, op.Vm);
  288. Operand res = GetVecA32(op.Qd);
  289. for (int index = 0; index < elems; index++)
  290. {
  291. Operand ne = context.VectorExtract(type, GetVecA32(op.Qn), op.Fn + index);
  292. res = context.VectorInsert(res, emit(ne, m), op.Fd + index);
  293. }
  294. context.Copy(GetVecA32(op.Qd), res);
  295. }
  296. public static void EmitVectorByScalarOpI32(ArmEmitterContext context, Func2I emit, bool signed)
  297. {
  298. OpCode32SimdRegElem op = (OpCode32SimdRegElem)context.CurrOp;
  299. Operand m = ExtractElement(context, op.Vm, op.Size, signed);
  300. Operand res = GetVecA32(op.Qd);
  301. int elems = op.GetBytesCount() >> op.Size;
  302. for (int index = 0; index < elems; index++)
  303. {
  304. Operand ne = EmitVectorExtract32(context, op.Qn, op.In + index, op.Size, signed);
  305. res = EmitVectorInsert(context, res, emit(ne, m), op.Id + index, op.Size);
  306. }
  307. context.Copy(GetVecA32(op.Qd), res);
  308. }
  309. public static void EmitVectorByScalarLongOpI32(ArmEmitterContext context, Func2I emit, bool signed)
  310. {
  311. OpCode32SimdRegElem op = (OpCode32SimdRegElem)context.CurrOp;
  312. Operand m = ExtractElement(context, op.Vm, op.Size, signed);
  313. if (op.Size == 2)
  314. {
  315. m = signed ? context.SignExtend32(OperandType.I64, m) : context.ZeroExtend32(OperandType.I64, m);
  316. }
  317. Operand res = context.VectorZero();
  318. int elems = op.GetBytesCount() >> op.Size;
  319. for (int index = 0; index < elems; index++)
  320. {
  321. Operand ne = EmitVectorExtract32(context, op.Qn, op.In + index, op.Size, signed);
  322. if (op.Size == 2)
  323. {
  324. ne = signed ? context.SignExtend32(OperandType.I64, ne) : context.ZeroExtend32(OperandType.I64, ne);
  325. }
  326. res = EmitVectorInsert(context, res, emit(ne, m), index, op.Size + 1);
  327. }
  328. context.Copy(GetVecA32(op.Qd), res);
  329. }
  330. public static void EmitVectorsByScalarOpF32(ArmEmitterContext context, Func3I emit)
  331. {
  332. OpCode32SimdRegElem op = (OpCode32SimdRegElem)context.CurrOp;
  333. int sizeF = op.Size & 1;
  334. OperandType type = sizeF != 0 ? OperandType.FP64 : OperandType.FP32;
  335. int elems = op.GetBytesCount() >> sizeF + 2;
  336. Operand m = ExtractScalar(context, type, op.Vm);
  337. Operand res = GetVecA32(op.Qd);
  338. for (int index = 0; index < elems; index++)
  339. {
  340. Operand de = context.VectorExtract(type, GetVecA32(op.Qd), op.Fd + index);
  341. Operand ne = context.VectorExtract(type, GetVecA32(op.Qn), op.Fn + index);
  342. res = context.VectorInsert(res, emit(de, ne, m), op.Fd + index);
  343. }
  344. context.Copy(GetVecA32(op.Qd), res);
  345. }
  346. public static void EmitVectorsByScalarOpI32(ArmEmitterContext context, Func3I emit, bool signed)
  347. {
  348. OpCode32SimdRegElem op = (OpCode32SimdRegElem)context.CurrOp;
  349. Operand m = EmitVectorExtract32(context, op.Vm >> (4 - op.Size), op.Vm & ((1 << (4 - op.Size)) - 1), op.Size, signed);
  350. Operand res = GetVecA32(op.Qd);
  351. int elems = op.GetBytesCount() >> op.Size;
  352. for (int index = 0; index < elems; index++)
  353. {
  354. Operand de = EmitVectorExtract32(context, op.Qd, op.Id + index, op.Size, signed);
  355. Operand ne = EmitVectorExtract32(context, op.Qn, op.In + index, op.Size, signed);
  356. res = EmitVectorInsert(context, res, emit(de, ne, m), op.Id + index, op.Size);
  357. }
  358. context.Copy(GetVecA32(op.Qd), res);
  359. }
  360. // Pairwise
  361. public static void EmitVectorPairwiseOpF32(ArmEmitterContext context, Func2I emit)
  362. {
  363. OpCode32SimdReg op = (OpCode32SimdReg)context.CurrOp;
  364. int sizeF = op.Size & 1;
  365. OperandType type = sizeF != 0 ? OperandType.FP64 : OperandType.FP32;
  366. int elems = op.GetBytesCount() >> (sizeF + 2);
  367. int pairs = elems >> 1;
  368. Operand res = GetVecA32(op.Qd);
  369. Operand mvec = GetVecA32(op.Qm);
  370. Operand nvec = GetVecA32(op.Qn);
  371. for (int index = 0; index < pairs; index++)
  372. {
  373. int pairIndex = index << 1;
  374. Operand n1 = context.VectorExtract(type, nvec, op.Fn + pairIndex);
  375. Operand n2 = context.VectorExtract(type, nvec, op.Fn + pairIndex + 1);
  376. res = context.VectorInsert(res, emit(n1, n2), op.Fd + index);
  377. Operand m1 = context.VectorExtract(type, mvec, op.Fm + pairIndex);
  378. Operand m2 = context.VectorExtract(type, mvec, op.Fm + pairIndex + 1);
  379. res = context.VectorInsert(res, emit(m1, m2), op.Fd + index + pairs);
  380. }
  381. context.Copy(GetVecA32(op.Qd), res);
  382. }
  383. public static void EmitVectorPairwiseOpI32(ArmEmitterContext context, Func2I emit, bool signed)
  384. {
  385. OpCode32SimdReg op = (OpCode32SimdReg)context.CurrOp;
  386. int elems = op.GetBytesCount() >> op.Size;
  387. int pairs = elems >> 1;
  388. Operand res = GetVecA32(op.Qd);
  389. for (int index = 0; index < pairs; index++)
  390. {
  391. int pairIndex = index << 1;
  392. Operand n1 = EmitVectorExtract32(context, op.Qn, op.In + pairIndex, op.Size, signed);
  393. Operand n2 = EmitVectorExtract32(context, op.Qn, op.In + pairIndex + 1, op.Size, signed);
  394. Operand m1 = EmitVectorExtract32(context, op.Qm, op.Im + pairIndex, op.Size, signed);
  395. Operand m2 = EmitVectorExtract32(context, op.Qm, op.Im + pairIndex + 1, op.Size, signed);
  396. res = EmitVectorInsert(context, res, emit(n1, n2), op.Id + index, op.Size);
  397. res = EmitVectorInsert(context, res, emit(m1, m2), op.Id + index + pairs, op.Size);
  398. }
  399. context.Copy(GetVecA32(op.Qd), res);
  400. }
  401. // Narrow
  402. public static void EmitVectorUnaryNarrowOp32(ArmEmitterContext context, Func1I emit, bool signed = false)
  403. {
  404. OpCode32Simd op = (OpCode32Simd)context.CurrOp;
  405. int elems = 8 >> op.Size; // Size contains the target element size. (for when it becomes a doubleword)
  406. Operand res = GetVecA32(op.Qd);
  407. int id = (op.Vd & 1) << (3 - op.Size); // Target doubleword base.
  408. for (int index = 0; index < elems; index++)
  409. {
  410. Operand m = EmitVectorExtract32(context, op.Qm, index, op.Size + 1, signed);
  411. res = EmitVectorInsert(context, res, emit(m), id + index, op.Size);
  412. }
  413. context.Copy(GetVecA32(op.Qd), res);
  414. }
  415. // Intrinsic Helpers
  416. public static Operand EmitMoveDoubleWordToSide(ArmEmitterContext context, Operand input, int originalV, int targetV)
  417. {
  418. Debug.Assert(input.Type == OperandType.V128);
  419. int originalSide = originalV & 1;
  420. int targetSide = targetV & 1;
  421. if (originalSide == targetSide)
  422. {
  423. return input;
  424. }
  425. if (targetSide == 1)
  426. {
  427. return context.AddIntrinsic(Intrinsic.X86Movlhps, input, input); // Low to high.
  428. }
  429. else
  430. {
  431. return context.AddIntrinsic(Intrinsic.X86Movhlps, input, input); // High to low.
  432. }
  433. }
  434. public static Operand EmitDoubleWordInsert(ArmEmitterContext context, Operand target, Operand value, int targetV)
  435. {
  436. Debug.Assert(target.Type == OperandType.V128 && value.Type == OperandType.V128);
  437. int targetSide = targetV & 1;
  438. int shuffleMask = 2;
  439. if (targetSide == 1)
  440. {
  441. return context.AddIntrinsic(Intrinsic.X86Shufpd, target, value, Const(shuffleMask));
  442. }
  443. else
  444. {
  445. return context.AddIntrinsic(Intrinsic.X86Shufpd, value, target, Const(shuffleMask));
  446. }
  447. }
  448. public static Operand EmitScalarInsert(ArmEmitterContext context, Operand target, Operand value, int reg, bool doubleWidth)
  449. {
  450. Debug.Assert(target.Type == OperandType.V128 && value.Type == OperandType.V128);
  451. // Insert from index 0 in value to index in target.
  452. int index = reg & (doubleWidth ? 1 : 3);
  453. if (doubleWidth)
  454. {
  455. if (index == 1)
  456. {
  457. return context.AddIntrinsic(Intrinsic.X86Movlhps, target, value); // Low to high.
  458. }
  459. else
  460. {
  461. return context.AddIntrinsic(Intrinsic.X86Shufpd, value, target, Const(2)); // Low to low, keep high from original.
  462. }
  463. }
  464. else
  465. {
  466. if (Optimizations.UseSse41)
  467. {
  468. return context.AddIntrinsic(Intrinsic.X86Insertps, target, value, Const(index << 4));
  469. }
  470. else
  471. {
  472. target = EmitSwapScalar(context, target, index, doubleWidth); // Swap value to replace into element 0.
  473. target = context.AddIntrinsic(Intrinsic.X86Movss, target, value); // Move the value into element 0 of the vector.
  474. return EmitSwapScalar(context, target, index, doubleWidth); // Swap new value back to the correct index.
  475. }
  476. }
  477. }
  478. public static Operand EmitSwapScalar(ArmEmitterContext context, Operand target, int reg, bool doubleWidth)
  479. {
  480. // Index into 0, 0 into index. This swap happens at the start of an A32 scalar op if required.
  481. int index = reg & (doubleWidth ? 1 : 3);
  482. if (index == 0) return target;
  483. if (doubleWidth)
  484. {
  485. int shuffleMask = 1; // Swap top and bottom. (b0 = 1, b1 = 0)
  486. return context.AddIntrinsic(Intrinsic.X86Shufpd, target, target, Const(shuffleMask));
  487. }
  488. else
  489. {
  490. int shuffleMask = (3 << 6) | (2 << 4) | (1 << 2) | index; // Swap index and 0. (others remain)
  491. shuffleMask &= ~(3 << (index * 2));
  492. return context.AddIntrinsic(Intrinsic.X86Shufps, target, target, Const(shuffleMask));
  493. }
  494. }
  495. // Vector Operand Templates
  496. public static void EmitVectorUnaryOpSimd32(ArmEmitterContext context, Func1I vectorFunc)
  497. {
  498. OpCode32Simd op = (OpCode32Simd)context.CurrOp;
  499. Operand m = GetVecA32(op.Qm);
  500. Operand d = GetVecA32(op.Qd);
  501. if (!op.Q) // Register swap: move relevant doubleword to destination side.
  502. {
  503. m = EmitMoveDoubleWordToSide(context, m, op.Vm, op.Vd);
  504. }
  505. Operand res = vectorFunc(m);
  506. if (!op.Q) // Register insert.
  507. {
  508. res = EmitDoubleWordInsert(context, d, res, op.Vd);
  509. }
  510. context.Copy(d, res);
  511. }
  512. public static void EmitVectorUnaryOpF32(ArmEmitterContext context, Intrinsic inst32, Intrinsic inst64)
  513. {
  514. OpCode32Simd op = (OpCode32Simd)context.CurrOp;
  515. Intrinsic inst = (op.Size & 1) != 0 ? inst64 : inst32;
  516. EmitVectorUnaryOpSimd32(context, (m) => context.AddIntrinsic(inst, m));
  517. }
  518. public static void EmitVectorBinaryOpSimd32(ArmEmitterContext context, Func2I vectorFunc, int side = -1)
  519. {
  520. OpCode32SimdReg op = (OpCode32SimdReg)context.CurrOp;
  521. Operand n = GetVecA32(op.Qn);
  522. Operand m = GetVecA32(op.Qm);
  523. Operand d = GetVecA32(op.Qd);
  524. if (side == -1)
  525. {
  526. side = op.Vd;
  527. }
  528. if (!op.Q) // Register swap: move relevant doubleword to destination side.
  529. {
  530. n = EmitMoveDoubleWordToSide(context, n, op.Vn, side);
  531. m = EmitMoveDoubleWordToSide(context, m, op.Vm, side);
  532. }
  533. Operand res = vectorFunc(n, m);
  534. if (!op.Q) // Register insert.
  535. {
  536. if (side != op.Vd)
  537. {
  538. res = EmitMoveDoubleWordToSide(context, res, side, op.Vd);
  539. }
  540. res = EmitDoubleWordInsert(context, d, res, op.Vd);
  541. }
  542. context.Copy(d, res);
  543. }
  544. public static void EmitVectorBinaryOpF32(ArmEmitterContext context, Intrinsic inst32, Intrinsic inst64)
  545. {
  546. OpCode32SimdReg op = (OpCode32SimdReg)context.CurrOp;
  547. Intrinsic inst = (op.Size & 1) != 0 ? inst64 : inst32;
  548. EmitVectorBinaryOpSimd32(context, (n, m) => context.AddIntrinsic(inst, n, m));
  549. }
  550. public static void EmitVectorTernaryOpSimd32(ArmEmitterContext context, Func3I vectorFunc)
  551. {
  552. OpCode32SimdReg op = (OpCode32SimdReg)context.CurrOp;
  553. Operand n = GetVecA32(op.Qn);
  554. Operand m = GetVecA32(op.Qm);
  555. Operand d = GetVecA32(op.Qd);
  556. Operand initialD = d;
  557. if (!op.Q) // Register swap: move relevant doubleword to destination side.
  558. {
  559. n = EmitMoveDoubleWordToSide(context, n, op.Vn, op.Vd);
  560. m = EmitMoveDoubleWordToSide(context, m, op.Vm, op.Vd);
  561. }
  562. Operand res = vectorFunc(d, n, m);
  563. if (!op.Q) // Register insert.
  564. {
  565. res = EmitDoubleWordInsert(context, initialD, res, op.Vd);
  566. }
  567. context.Copy(initialD, res);
  568. }
  569. public static void EmitVectorTernaryOpF32(ArmEmitterContext context, Intrinsic inst32pt1, Intrinsic inst64pt1, Intrinsic inst32pt2, Intrinsic inst64pt2)
  570. {
  571. OpCode32SimdReg op = (OpCode32SimdReg)context.CurrOp;
  572. Intrinsic inst1 = (op.Size & 1) != 0 ? inst64pt1 : inst32pt1;
  573. Intrinsic inst2 = (op.Size & 1) != 0 ? inst64pt2 : inst32pt2;
  574. EmitVectorTernaryOpSimd32(context, (d, n, m) =>
  575. {
  576. Operand res = context.AddIntrinsic(inst1, n, m);
  577. return res = context.AddIntrinsic(inst2, d, res);
  578. });
  579. }
  580. public static void EmitScalarUnaryOpSimd32(ArmEmitterContext context, Func1I scalarFunc)
  581. {
  582. OpCode32SimdS op = (OpCode32SimdS)context.CurrOp;
  583. bool doubleSize = (op.Size & 1) != 0;
  584. int shift = doubleSize ? 1 : 2;
  585. Operand m = GetVecA32(op.Vm >> shift);
  586. Operand d = GetVecA32(op.Vd >> shift);
  587. m = EmitSwapScalar(context, m, op.Vm, doubleSize);
  588. Operand res = scalarFunc(m);
  589. // Insert scalar into vector.
  590. res = EmitScalarInsert(context, d, res, op.Vd, doubleSize);
  591. context.Copy(d, res);
  592. }
  593. public static void EmitScalarUnaryOpF32(ArmEmitterContext context, Intrinsic inst32, Intrinsic inst64)
  594. {
  595. OpCode32SimdS op = (OpCode32SimdS)context.CurrOp;
  596. Intrinsic inst = (op.Size & 1) != 0 ? inst64 : inst32;
  597. EmitScalarUnaryOpSimd32(context, (m) => (inst == 0) ? m : context.AddIntrinsic(inst, m));
  598. }
  599. public static void EmitScalarBinaryOpSimd32(ArmEmitterContext context, Func2I scalarFunc)
  600. {
  601. OpCode32SimdRegS op = (OpCode32SimdRegS)context.CurrOp;
  602. bool doubleSize = (op.Size & 1) != 0;
  603. int shift = doubleSize ? 1 : 2;
  604. Operand n = GetVecA32(op.Vn >> shift);
  605. Operand m = GetVecA32(op.Vm >> shift);
  606. Operand d = GetVecA32(op.Vd >> shift);
  607. n = EmitSwapScalar(context, n, op.Vn, doubleSize);
  608. m = EmitSwapScalar(context, m, op.Vm, doubleSize);
  609. Operand res = scalarFunc(n, m);
  610. // Insert scalar into vector.
  611. res = EmitScalarInsert(context, d, res, op.Vd, doubleSize);
  612. context.Copy(d, res);
  613. }
  614. public static void EmitScalarBinaryOpF32(ArmEmitterContext context, Intrinsic inst32, Intrinsic inst64)
  615. {
  616. OpCode32SimdRegS op = (OpCode32SimdRegS)context.CurrOp;
  617. Intrinsic inst = (op.Size & 1) != 0 ? inst64 : inst32;
  618. EmitScalarBinaryOpSimd32(context, (n, m) => context.AddIntrinsic(inst, n, m));
  619. }
  620. public static void EmitScalarTernaryOpSimd32(ArmEmitterContext context, Func3I scalarFunc)
  621. {
  622. OpCode32SimdRegS op = (OpCode32SimdRegS)context.CurrOp;
  623. bool doubleSize = (op.Size & 1) != 0;
  624. int shift = doubleSize ? 1 : 2;
  625. Operand n = GetVecA32(op.Vn >> shift);
  626. Operand m = GetVecA32(op.Vm >> shift);
  627. Operand d = GetVecA32(op.Vd >> shift);
  628. Operand initialD = d;
  629. n = EmitSwapScalar(context, n, op.Vn, doubleSize);
  630. m = EmitSwapScalar(context, m, op.Vm, doubleSize);
  631. d = EmitSwapScalar(context, d, op.Vd, doubleSize);
  632. Operand res = scalarFunc(d, n, m);
  633. // Insert scalar into vector.
  634. res = EmitScalarInsert(context, initialD, res, op.Vd, doubleSize);
  635. context.Copy(initialD, res);
  636. }
  637. public static void EmitScalarTernaryOpF32(ArmEmitterContext context, Intrinsic inst32pt1, Intrinsic inst64pt1, Intrinsic inst32pt2, Intrinsic inst64pt2)
  638. {
  639. OpCode32SimdRegS op = (OpCode32SimdRegS)context.CurrOp;
  640. bool doubleSize = (op.Size & 1) != 0;
  641. int shift = doubleSize ? 1 : 2;
  642. Intrinsic inst1 = doubleSize ? inst64pt1 : inst32pt1;
  643. Intrinsic inst2 = doubleSize ? inst64pt2 : inst32pt2;
  644. EmitScalarTernaryOpSimd32(context, (d, n, m) =>
  645. {
  646. Operand res = context.AddIntrinsic(inst1, n, m);
  647. return context.AddIntrinsic(inst2, d, res);
  648. });
  649. }
  650. // By Scalar
  651. public static void EmitVectorByScalarOpSimd32(ArmEmitterContext context, Func2I vectorFunc)
  652. {
  653. OpCode32SimdRegElem op = (OpCode32SimdRegElem)context.CurrOp;
  654. Operand n = GetVecA32(op.Qn);
  655. Operand d = GetVecA32(op.Qd);
  656. int index = op.Vm & 3;
  657. int dupeMask = (index << 6) | (index << 4) | (index << 2) | index;
  658. Operand m = GetVecA32(op.Vm >> 2);
  659. m = context.AddIntrinsic(Intrinsic.X86Shufps, m, m, Const(dupeMask));
  660. if (!op.Q) // Register swap: move relevant doubleword to destination side.
  661. {
  662. n = EmitMoveDoubleWordToSide(context, n, op.Vn, op.Vd);
  663. }
  664. Operand res = vectorFunc(n, m);
  665. if (!op.Q) // Register insert.
  666. {
  667. res = EmitDoubleWordInsert(context, d, res, op.Vd);
  668. }
  669. context.Copy(d, res);
  670. }
  671. public static void EmitVectorByScalarOpF32(ArmEmitterContext context, Intrinsic inst32, Intrinsic inst64)
  672. {
  673. OpCode32SimdRegElem op = (OpCode32SimdRegElem)context.CurrOp;
  674. Intrinsic inst = (op.Size & 1) != 0 ? inst64 : inst32;
  675. EmitVectorByScalarOpSimd32(context, (n, m) => context.AddIntrinsic(inst, n, m));
  676. }
  677. public static void EmitVectorsByScalarOpSimd32(ArmEmitterContext context, Func3I vectorFunc)
  678. {
  679. OpCode32SimdRegElem op = (OpCode32SimdRegElem)context.CurrOp;
  680. Operand n = GetVecA32(op.Qn);
  681. Operand d = GetVecA32(op.Qd);
  682. Operand initialD = d;
  683. int index = op.Vm & 3;
  684. int dupeMask = (index << 6) | (index << 4) | (index << 2) | index;
  685. Operand m = GetVecA32(op.Vm >> 2);
  686. m = context.AddIntrinsic(Intrinsic.X86Shufps, m, m, Const(dupeMask));
  687. if (!op.Q) // Register swap: move relevant doubleword to destination side.
  688. {
  689. n = EmitMoveDoubleWordToSide(context, n, op.Vn, op.Vd);
  690. }
  691. Operand res = vectorFunc(d, n, m);
  692. if (!op.Q) // Register insert.
  693. {
  694. res = EmitDoubleWordInsert(context, initialD, res, op.Vd);
  695. }
  696. context.Copy(initialD, res);
  697. }
  698. public static void EmitVectorsByScalarOpF32(ArmEmitterContext context, Intrinsic inst32pt1, Intrinsic inst64pt1, Intrinsic inst32pt2, Intrinsic inst64pt2)
  699. {
  700. OpCode32SimdRegElem op = (OpCode32SimdRegElem)context.CurrOp;
  701. Intrinsic inst1 = (op.Size & 1) != 0 ? inst64pt1 : inst32pt1;
  702. Intrinsic inst2 = (op.Size & 1) != 0 ? inst64pt2 : inst32pt2;
  703. EmitVectorsByScalarOpSimd32(context, (d, n, m) =>
  704. {
  705. Operand res = context.AddIntrinsic(inst1, n, m);
  706. return res = context.AddIntrinsic(inst2, d, res);
  707. });
  708. }
  709. // Pairwise
  710. public static void EmitSse2VectorPairwiseOpF32(ArmEmitterContext context, Intrinsic inst32)
  711. {
  712. OpCode32SimdReg op = (OpCode32SimdReg)context.CurrOp;
  713. EmitVectorBinaryOpSimd32(context, (n, m) =>
  714. {
  715. Operand unpck = context.AddIntrinsic(Intrinsic.X86Unpcklps, n, m);
  716. Operand part0 = unpck;
  717. Operand part1 = context.AddIntrinsic(Intrinsic.X86Movhlps, unpck, unpck);
  718. return context.AddIntrinsic(inst32, part0, part1);
  719. }, 0);
  720. }
  721. public static void EmitSsse3VectorPairwiseOp32(ArmEmitterContext context, Intrinsic[] inst)
  722. {
  723. OpCode32SimdReg op = (OpCode32SimdReg)context.CurrOp;
  724. EmitVectorBinaryOpSimd32(context, (n, m) =>
  725. {
  726. if (op.RegisterSize == RegisterSize.Simd64)
  727. {
  728. Operand zeroEvenMask = X86GetElements(context, ZeroMask, EvenMasks[op.Size]);
  729. Operand zeroOddMask = X86GetElements(context, ZeroMask, OddMasks[op.Size]);
  730. Operand mN = context.AddIntrinsic(Intrinsic.X86Punpcklqdq, n, m); // m:n
  731. Operand left = context.AddIntrinsic(Intrinsic.X86Pshufb, mN, zeroEvenMask); // 0:even from m:n
  732. Operand right = context.AddIntrinsic(Intrinsic.X86Pshufb, mN, zeroOddMask); // 0:odd from m:n
  733. return context.AddIntrinsic(inst[op.Size], left, right);
  734. }
  735. else if (op.Size < 3)
  736. {
  737. Operand oddEvenMask = X86GetElements(context, OddMasks[op.Size], EvenMasks[op.Size]);
  738. Operand oddEvenN = context.AddIntrinsic(Intrinsic.X86Pshufb, n, oddEvenMask); // odd:even from n
  739. Operand oddEvenM = context.AddIntrinsic(Intrinsic.X86Pshufb, m, oddEvenMask); // odd:even from m
  740. Operand left = context.AddIntrinsic(Intrinsic.X86Punpcklqdq, oddEvenN, oddEvenM);
  741. Operand right = context.AddIntrinsic(Intrinsic.X86Punpckhqdq, oddEvenN, oddEvenM);
  742. return context.AddIntrinsic(inst[op.Size], left, right);
  743. }
  744. else
  745. {
  746. Operand left = context.AddIntrinsic(Intrinsic.X86Punpcklqdq, n, m);
  747. Operand right = context.AddIntrinsic(Intrinsic.X86Punpckhqdq, n, m);
  748. return context.AddIntrinsic(inst[3], left, right);
  749. }
  750. }, 0);
  751. }
  752. // Generic Functions
  753. public static Operand EmitSoftFloatCallDefaultFpscr(ArmEmitterContext context, string name, params Operand[] callArgs)
  754. {
  755. IOpCodeSimd op = (IOpCodeSimd)context.CurrOp;
  756. MethodInfo info = (op.Size & 1) == 0
  757. ? typeof(SoftFloat32).GetMethod(name)
  758. : typeof(SoftFloat64).GetMethod(name);
  759. Array.Resize(ref callArgs, callArgs.Length + 1);
  760. callArgs[callArgs.Length - 1] = Const(1);
  761. return context.Call(info, callArgs);
  762. }
  763. public static Operand EmitVectorExtractSx32(ArmEmitterContext context, int reg, int index, int size)
  764. {
  765. return EmitVectorExtract32(context, reg, index, size, true);
  766. }
  767. public static Operand EmitVectorExtractZx32(ArmEmitterContext context, int reg, int index, int size)
  768. {
  769. return EmitVectorExtract32(context, reg, index, size, false);
  770. }
  771. public static Operand EmitVectorExtract32(ArmEmitterContext context, int reg, int index, int size, bool signed)
  772. {
  773. ThrowIfInvalid(index, size);
  774. Operand res = null;
  775. switch (size)
  776. {
  777. case 0:
  778. res = context.VectorExtract8(GetVec(reg), index);
  779. break;
  780. case 1:
  781. res = context.VectorExtract16(GetVec(reg), index);
  782. break;
  783. case 2:
  784. res = context.VectorExtract(OperandType.I32, GetVec(reg), index);
  785. break;
  786. case 3:
  787. res = context.VectorExtract(OperandType.I64, GetVec(reg), index);
  788. break;
  789. }
  790. if (signed)
  791. {
  792. switch (size)
  793. {
  794. case 0: res = context.SignExtend8(OperandType.I32, res); break;
  795. case 1: res = context.SignExtend16(OperandType.I32, res); break;
  796. }
  797. }
  798. else
  799. {
  800. switch (size)
  801. {
  802. case 0: res = context.ZeroExtend8(OperandType.I32, res); break;
  803. case 1: res = context.ZeroExtend16(OperandType.I32, res); break;
  804. }
  805. }
  806. return res;
  807. }
  808. }
  809. }