InstEmitSimdHelper32.cs 39 KB

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