InstEmitSimdHelper32.cs 46 KB

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