InstEmitSimdHelper32.cs 36 KB

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