InstEmitMul32.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. using ARMeilleure.Decoders;
  2. using ARMeilleure.IntermediateRepresentation;
  3. using ARMeilleure.State;
  4. using ARMeilleure.Translation;
  5. using System;
  6. using static ARMeilleure.Instructions.InstEmitAluHelper;
  7. using static ARMeilleure.Instructions.InstEmitHelper;
  8. using static ARMeilleure.IntermediateRepresentation.Operand.Factory;
  9. namespace ARMeilleure.Instructions
  10. {
  11. static partial class InstEmit32
  12. {
  13. [Flags]
  14. private enum MullFlags
  15. {
  16. Subtract = 1,
  17. Add = 1 << 1,
  18. Signed = 1 << 2,
  19. SignedAdd = Signed | Add,
  20. SignedSubtract = Signed | Subtract
  21. }
  22. public static void Mla(ArmEmitterContext context)
  23. {
  24. OpCode32AluMla op = (OpCode32AluMla)context.CurrOp;
  25. Operand n = GetAluN(context);
  26. Operand m = GetAluM(context);
  27. Operand a = GetIntA32(context, op.Ra);
  28. Operand res = context.Add(a, context.Multiply(n, m));
  29. if (ShouldSetFlags(context))
  30. {
  31. EmitNZFlagsCheck(context, res);
  32. }
  33. EmitAluStore(context, res);
  34. }
  35. public static void Mls(ArmEmitterContext context)
  36. {
  37. OpCode32AluMla op = (OpCode32AluMla)context.CurrOp;
  38. Operand n = GetAluN(context);
  39. Operand m = GetAluM(context);
  40. Operand a = GetIntA32(context, op.Ra);
  41. Operand res = context.Subtract(a, context.Multiply(n, m));
  42. EmitAluStore(context, res);
  43. }
  44. public static void Smmla(ArmEmitterContext context)
  45. {
  46. EmitSmmul(context, MullFlags.SignedAdd);
  47. }
  48. public static void Smmls(ArmEmitterContext context)
  49. {
  50. EmitSmmul(context, MullFlags.SignedSubtract);
  51. }
  52. public static void Smmul(ArmEmitterContext context)
  53. {
  54. EmitSmmul(context, MullFlags.Signed);
  55. }
  56. private static void EmitSmmul(ArmEmitterContext context, MullFlags flags)
  57. {
  58. OpCode32AluMla op = (OpCode32AluMla)context.CurrOp;
  59. Operand n = context.SignExtend32(OperandType.I64, GetIntA32(context, op.Rn));
  60. Operand m = context.SignExtend32(OperandType.I64, GetIntA32(context, op.Rm));
  61. Operand res = context.Multiply(n, m);
  62. if (flags.HasFlag(MullFlags.Add) && op.Ra != 0xf)
  63. {
  64. res = context.Add(context.ShiftLeft(context.ZeroExtend32(OperandType.I64, GetIntA32(context, op.Ra)), Const(32)), res);
  65. }
  66. else if (flags.HasFlag(MullFlags.Subtract))
  67. {
  68. res = context.Subtract(context.ShiftLeft(context.ZeroExtend32(OperandType.I64, GetIntA32(context, op.Ra)), Const(32)), res);
  69. }
  70. if (op.R)
  71. {
  72. res = context.Add(res, Const(0x80000000L));
  73. }
  74. Operand hi = context.ConvertI64ToI32(context.ShiftRightSI(res, Const(32)));
  75. EmitGenericAluStoreA32(context, op.Rd, false, hi);
  76. }
  77. public static void Smla__(ArmEmitterContext context)
  78. {
  79. OpCode32AluMla op = (OpCode32AluMla)context.CurrOp;
  80. Operand n = GetIntA32(context, op.Rn);
  81. Operand m = GetIntA32(context, op.Rm);
  82. Operand a = GetIntA32(context, op.Ra);
  83. if (op.NHigh)
  84. {
  85. n = context.SignExtend16(OperandType.I64, context.ShiftRightUI(n, Const(16)));
  86. }
  87. else
  88. {
  89. n = context.SignExtend16(OperandType.I64, n);
  90. }
  91. if (op.MHigh)
  92. {
  93. m = context.SignExtend16(OperandType.I64, context.ShiftRightUI(m, Const(16)));
  94. }
  95. else
  96. {
  97. m = context.SignExtend16(OperandType.I64, m);
  98. }
  99. Operand res = context.Multiply(n, m);
  100. Operand toAdd = context.SignExtend32(OperandType.I64, a);
  101. res = context.Add(res, toAdd);
  102. Operand q = context.ICompareNotEqual(res, context.SignExtend32(OperandType.I64, res));
  103. res = context.ConvertI64ToI32(res);
  104. UpdateQFlag(context, q);
  105. EmitGenericAluStoreA32(context, op.Rd, false, res);
  106. }
  107. public static void Smlal(ArmEmitterContext context)
  108. {
  109. EmitMlal(context, true);
  110. }
  111. public static void Smlal__(ArmEmitterContext context)
  112. {
  113. OpCode32AluUmull op = (OpCode32AluUmull)context.CurrOp;
  114. Operand n = GetIntA32(context, op.Rn);
  115. Operand m = GetIntA32(context, op.Rm);
  116. if (op.NHigh)
  117. {
  118. n = context.SignExtend16(OperandType.I64, context.ShiftRightUI(n, Const(16)));
  119. }
  120. else
  121. {
  122. n = context.SignExtend16(OperandType.I64, n);
  123. }
  124. if (op.MHigh)
  125. {
  126. m = context.SignExtend16(OperandType.I64, context.ShiftRightUI(m, Const(16)));
  127. }
  128. else
  129. {
  130. m = context.SignExtend16(OperandType.I64, m);
  131. }
  132. Operand res = context.Multiply(n, m);
  133. Operand toAdd = context.ShiftLeft(context.ZeroExtend32(OperandType.I64, GetIntA32(context, op.RdHi)), Const(32));
  134. toAdd = context.BitwiseOr(toAdd, context.ZeroExtend32(OperandType.I64, GetIntA32(context, op.RdLo)));
  135. res = context.Add(res, toAdd);
  136. Operand hi = context.ConvertI64ToI32(context.ShiftRightUI(res, Const(32)));
  137. Operand lo = context.ConvertI64ToI32(res);
  138. EmitGenericAluStoreA32(context, op.RdHi, false, hi);
  139. EmitGenericAluStoreA32(context, op.RdLo, false, lo);
  140. }
  141. public static void Smlaw_(ArmEmitterContext context)
  142. {
  143. OpCode32AluMla op = (OpCode32AluMla)context.CurrOp;
  144. Operand n = GetIntA32(context, op.Rn);
  145. Operand m = GetIntA32(context, op.Rm);
  146. Operand a = GetIntA32(context, op.Ra);
  147. if (op.MHigh)
  148. {
  149. m = context.SignExtend16(OperandType.I64, context.ShiftRightUI(m, Const(16)));
  150. }
  151. else
  152. {
  153. m = context.SignExtend16(OperandType.I64, m);
  154. }
  155. Operand res = context.Multiply(context.SignExtend32(OperandType.I64, n), m);
  156. Operand toAdd = context.ShiftLeft(context.SignExtend32(OperandType.I64, a), Const(16));
  157. res = context.Add(res, toAdd);
  158. res = context.ShiftRightSI(res, Const(16));
  159. Operand q = context.ICompareNotEqual(res, context.SignExtend32(OperandType.I64, res));
  160. res = context.ConvertI64ToI32(res);
  161. UpdateQFlag(context, q);
  162. EmitGenericAluStoreA32(context, op.Rd, false, res);
  163. }
  164. public static void Smul__(ArmEmitterContext context)
  165. {
  166. OpCode32AluMla op = (OpCode32AluMla)context.CurrOp;
  167. Operand n = GetIntA32(context, op.Rn);
  168. Operand m = GetIntA32(context, op.Rm);
  169. if (op.NHigh)
  170. {
  171. n = context.ShiftRightSI(n, Const(16));
  172. }
  173. else
  174. {
  175. n = context.SignExtend16(OperandType.I32, n);
  176. }
  177. if (op.MHigh)
  178. {
  179. m = context.ShiftRightSI(m, Const(16));
  180. }
  181. else
  182. {
  183. m = context.SignExtend16(OperandType.I32, m);
  184. }
  185. Operand res = context.Multiply(n, m);
  186. EmitGenericAluStoreA32(context, op.Rd, false, res);
  187. }
  188. public static void Smull(ArmEmitterContext context)
  189. {
  190. OpCode32AluUmull op = (OpCode32AluUmull)context.CurrOp;
  191. Operand n = context.SignExtend32(OperandType.I64, GetIntA32(context, op.Rn));
  192. Operand m = context.SignExtend32(OperandType.I64, GetIntA32(context, op.Rm));
  193. Operand res = context.Multiply(n, m);
  194. Operand hi = context.ConvertI64ToI32(context.ShiftRightUI(res, Const(32)));
  195. Operand lo = context.ConvertI64ToI32(res);
  196. if (ShouldSetFlags(context))
  197. {
  198. EmitNZFlagsCheck(context, res);
  199. }
  200. EmitGenericAluStoreA32(context, op.RdHi, ShouldSetFlags(context), hi);
  201. EmitGenericAluStoreA32(context, op.RdLo, ShouldSetFlags(context), lo);
  202. }
  203. public static void Smulw_(ArmEmitterContext context)
  204. {
  205. OpCode32AluMla op = (OpCode32AluMla)context.CurrOp;
  206. Operand n = GetIntA32(context, op.Rn);
  207. Operand m = GetIntA32(context, op.Rm);
  208. if (op.MHigh)
  209. {
  210. m = context.SignExtend16(OperandType.I64, context.ShiftRightUI(m, Const(16)));
  211. }
  212. else
  213. {
  214. m = context.SignExtend16(OperandType.I64, m);
  215. }
  216. Operand res = context.Multiply(context.SignExtend32(OperandType.I64, n), m);
  217. res = context.ShiftRightUI(res, Const(16));
  218. res = context.ConvertI64ToI32(res);
  219. EmitGenericAluStoreA32(context, op.Rd, false, res);
  220. }
  221. public static void Umaal(ArmEmitterContext context)
  222. {
  223. OpCode32AluUmull op = (OpCode32AluUmull)context.CurrOp;
  224. Operand n = context.ZeroExtend32(OperandType.I64, GetIntA32(context, op.Rn));
  225. Operand m = context.ZeroExtend32(OperandType.I64, GetIntA32(context, op.Rm));
  226. Operand dHi = context.ZeroExtend32(OperandType.I64, GetIntA32(context, op.RdHi));
  227. Operand dLo = context.ZeroExtend32(OperandType.I64, GetIntA32(context, op.RdLo));
  228. Operand res = context.Multiply(n, m);
  229. res = context.Add(res, dHi);
  230. res = context.Add(res, dLo);
  231. Operand hi = context.ConvertI64ToI32(context.ShiftRightUI(res, Const(32)));
  232. Operand lo = context.ConvertI64ToI32(res);
  233. EmitGenericAluStoreA32(context, op.RdHi, false, hi);
  234. EmitGenericAluStoreA32(context, op.RdLo, false, lo);
  235. }
  236. public static void Umlal(ArmEmitterContext context)
  237. {
  238. EmitMlal(context, false);
  239. }
  240. public static void Umull(ArmEmitterContext context)
  241. {
  242. OpCode32AluUmull op = (OpCode32AluUmull)context.CurrOp;
  243. Operand n = context.ZeroExtend32(OperandType.I64, GetIntA32(context, op.Rn));
  244. Operand m = context.ZeroExtend32(OperandType.I64, GetIntA32(context, op.Rm));
  245. Operand res = context.Multiply(n, m);
  246. Operand hi = context.ConvertI64ToI32(context.ShiftRightUI(res, Const(32)));
  247. Operand lo = context.ConvertI64ToI32(res);
  248. if (ShouldSetFlags(context))
  249. {
  250. EmitNZFlagsCheck(context, res);
  251. }
  252. EmitGenericAluStoreA32(context, op.RdHi, ShouldSetFlags(context), hi);
  253. EmitGenericAluStoreA32(context, op.RdLo, ShouldSetFlags(context), lo);
  254. }
  255. private static void EmitMlal(ArmEmitterContext context, bool signed)
  256. {
  257. OpCode32AluUmull op = (OpCode32AluUmull)context.CurrOp;
  258. Operand n = GetIntA32(context, op.Rn);
  259. Operand m = GetIntA32(context, op.Rm);
  260. if (signed)
  261. {
  262. n = context.SignExtend32(OperandType.I64, n);
  263. m = context.SignExtend32(OperandType.I64, m);
  264. }
  265. else
  266. {
  267. n = context.ZeroExtend32(OperandType.I64, n);
  268. m = context.ZeroExtend32(OperandType.I64, m);
  269. }
  270. Operand res = context.Multiply(n, m);
  271. Operand toAdd = context.ShiftLeft(context.ZeroExtend32(OperandType.I64, GetIntA32(context, op.RdHi)), Const(32));
  272. toAdd = context.BitwiseOr(toAdd, context.ZeroExtend32(OperandType.I64, GetIntA32(context, op.RdLo)));
  273. res = context.Add(res, toAdd);
  274. Operand hi = context.ConvertI64ToI32(context.ShiftRightUI(res, Const(32)));
  275. Operand lo = context.ConvertI64ToI32(res);
  276. if (ShouldSetFlags(context))
  277. {
  278. EmitNZFlagsCheck(context, res);
  279. }
  280. EmitGenericAluStoreA32(context, op.RdHi, ShouldSetFlags(context), hi);
  281. EmitGenericAluStoreA32(context, op.RdLo, ShouldSetFlags(context), lo);
  282. }
  283. private static void UpdateQFlag(ArmEmitterContext context, Operand q)
  284. {
  285. Operand lblSkipSetQ = Label();
  286. context.BranchIfFalse(lblSkipSetQ, q);
  287. SetFlag(context, PState.QFlag, Const(1));
  288. context.MarkLabel(lblSkipSetQ);
  289. }
  290. }
  291. }