InstEmitSimdHelper32.cs 42 KB

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