AInstEmitSimdLogical.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. using ChocolArm64.Decoder;
  2. using ChocolArm64.State;
  3. using ChocolArm64.Translation;
  4. using System;
  5. using System.Reflection.Emit;
  6. using System.Runtime.Intrinsics.X86;
  7. using static ChocolArm64.Instruction.AInstEmitSimdHelper;
  8. namespace ChocolArm64.Instruction
  9. {
  10. static partial class AInstEmit
  11. {
  12. public static void And_V(AILEmitterCtx Context)
  13. {
  14. if (AOptimizations.UseSse2)
  15. {
  16. EmitSse2Op(Context, nameof(Sse2.And));
  17. }
  18. else
  19. {
  20. EmitVectorBinaryOpZx(Context, () => Context.Emit(OpCodes.And));
  21. }
  22. }
  23. public static void Bic_V(AILEmitterCtx Context)
  24. {
  25. if (AOptimizations.UseSse2)
  26. {
  27. AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
  28. EmitLdvecWithUnsignedCast(Context, Op.Rm, Op.Size);
  29. EmitLdvecWithUnsignedCast(Context, Op.Rn, Op.Size);
  30. Type[] Types = new Type[]
  31. {
  32. VectorUIntTypesPerSizeLog2[Op.Size],
  33. VectorUIntTypesPerSizeLog2[Op.Size]
  34. };
  35. Context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.AndNot), Types));
  36. EmitStvecWithUnsignedCast(Context, Op.Rd, Op.Size);
  37. if (Op.RegisterSize == ARegisterSize.SIMD64)
  38. {
  39. EmitVectorZeroUpper(Context, Op.Rd);
  40. }
  41. }
  42. else
  43. {
  44. EmitVectorBinaryOpZx(Context, () =>
  45. {
  46. Context.Emit(OpCodes.Not);
  47. Context.Emit(OpCodes.And);
  48. });
  49. }
  50. }
  51. public static void Bic_Vi(AILEmitterCtx Context)
  52. {
  53. EmitVectorImmBinaryOp(Context, () =>
  54. {
  55. Context.Emit(OpCodes.Not);
  56. Context.Emit(OpCodes.And);
  57. });
  58. }
  59. public static void Bif_V(AILEmitterCtx Context)
  60. {
  61. EmitBitBif(Context, true);
  62. }
  63. public static void Bit_V(AILEmitterCtx Context)
  64. {
  65. EmitBitBif(Context, false);
  66. }
  67. private static void EmitBitBif(AILEmitterCtx Context, bool NotRm)
  68. {
  69. AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
  70. if (AOptimizations.UseSse2)
  71. {
  72. Type[] Types = new Type[]
  73. {
  74. VectorUIntTypesPerSizeLog2[Op.Size],
  75. VectorUIntTypesPerSizeLog2[Op.Size]
  76. };
  77. EmitLdvecWithUnsignedCast(Context, Op.Rm, Op.Size);
  78. EmitLdvecWithUnsignedCast(Context, Op.Rd, Op.Size);
  79. EmitLdvecWithUnsignedCast(Context, Op.Rn, Op.Size);
  80. Context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Xor), Types));
  81. string Name = NotRm ? nameof(Sse2.AndNot) : nameof(Sse2.And);
  82. Context.EmitCall(typeof(Sse2).GetMethod(Name, Types));
  83. EmitLdvecWithUnsignedCast(Context, Op.Rd, Op.Size);
  84. Context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Xor), Types));
  85. EmitStvecWithUnsignedCast(Context, Op.Rd, Op.Size);
  86. if (Op.RegisterSize == ARegisterSize.SIMD64)
  87. {
  88. EmitVectorZeroUpper(Context, Op.Rd);
  89. }
  90. }
  91. else
  92. {
  93. int Bytes = Op.GetBitsCount() >> 3;
  94. int Elems = Bytes >> Op.Size;
  95. for (int Index = 0; Index < Elems; Index++)
  96. {
  97. EmitVectorExtractZx(Context, Op.Rd, Index, Op.Size);
  98. EmitVectorExtractZx(Context, Op.Rn, Index, Op.Size);
  99. Context.Emit(OpCodes.Xor);
  100. EmitVectorExtractZx(Context, Op.Rm, Index, Op.Size);
  101. if (NotRm)
  102. {
  103. Context.Emit(OpCodes.Not);
  104. }
  105. Context.Emit(OpCodes.And);
  106. EmitVectorExtractZx(Context, Op.Rd, Index, Op.Size);
  107. Context.Emit(OpCodes.Xor);
  108. EmitVectorInsert(Context, Op.Rd, Index, Op.Size);
  109. }
  110. if (Op.RegisterSize == ARegisterSize.SIMD64)
  111. {
  112. EmitVectorZeroUpper(Context, Op.Rd);
  113. }
  114. }
  115. }
  116. public static void Bsl_V(AILEmitterCtx Context)
  117. {
  118. if (AOptimizations.UseSse2)
  119. {
  120. AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
  121. Type[] Types = new Type[]
  122. {
  123. VectorUIntTypesPerSizeLog2[Op.Size],
  124. VectorUIntTypesPerSizeLog2[Op.Size]
  125. };
  126. EmitLdvecWithUnsignedCast(Context, Op.Rn, Op.Size);
  127. EmitLdvecWithUnsignedCast(Context, Op.Rm, Op.Size);
  128. Context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Xor), Types));
  129. EmitLdvecWithUnsignedCast(Context, Op.Rd, Op.Size);
  130. Context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.And), Types));
  131. EmitLdvecWithUnsignedCast(Context, Op.Rm, Op.Size);
  132. Context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Xor), Types));
  133. EmitStvecWithUnsignedCast(Context, Op.Rd, Op.Size);
  134. if (Op.RegisterSize == ARegisterSize.SIMD64)
  135. {
  136. EmitVectorZeroUpper(Context, Op.Rd);
  137. }
  138. }
  139. else
  140. {
  141. EmitVectorTernaryOpZx(Context, () =>
  142. {
  143. Context.EmitSttmp();
  144. Context.EmitLdtmp();
  145. Context.Emit(OpCodes.Xor);
  146. Context.Emit(OpCodes.And);
  147. Context.EmitLdtmp();
  148. Context.Emit(OpCodes.Xor);
  149. });
  150. }
  151. }
  152. public static void Eor_V(AILEmitterCtx Context)
  153. {
  154. if (AOptimizations.UseSse2)
  155. {
  156. EmitSse2Op(Context, nameof(Sse2.Xor));
  157. }
  158. else
  159. {
  160. EmitVectorBinaryOpZx(Context, () => Context.Emit(OpCodes.Xor));
  161. }
  162. }
  163. public static void Not_V(AILEmitterCtx Context)
  164. {
  165. EmitVectorUnaryOpZx(Context, () => Context.Emit(OpCodes.Not));
  166. }
  167. public static void Orn_V(AILEmitterCtx Context)
  168. {
  169. EmitVectorBinaryOpZx(Context, () =>
  170. {
  171. Context.Emit(OpCodes.Not);
  172. Context.Emit(OpCodes.Or);
  173. });
  174. }
  175. public static void Orr_V(AILEmitterCtx Context)
  176. {
  177. if (AOptimizations.UseSse2)
  178. {
  179. EmitSse2Op(Context, nameof(Sse2.Or));
  180. }
  181. else
  182. {
  183. EmitVectorBinaryOpZx(Context, () => Context.Emit(OpCodes.Or));
  184. }
  185. }
  186. public static void Orr_Vi(AILEmitterCtx Context)
  187. {
  188. EmitVectorImmBinaryOp(Context, () => Context.Emit(OpCodes.Or));
  189. }
  190. public static void Rbit_V(AILEmitterCtx Context)
  191. {
  192. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  193. int Elems = Op.RegisterSize == ARegisterSize.SIMD128 ? 16 : 8;
  194. for (int Index = 0; Index < Elems; Index++)
  195. {
  196. EmitVectorExtractZx(Context, Op.Rn, Index, 0);
  197. Context.Emit(OpCodes.Conv_U4);
  198. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.ReverseBits8));
  199. Context.Emit(OpCodes.Conv_U8);
  200. EmitVectorInsert(Context, Op.Rd, Index, 0);
  201. }
  202. if (Op.RegisterSize == ARegisterSize.SIMD64)
  203. {
  204. EmitVectorZeroUpper(Context, Op.Rd);
  205. }
  206. }
  207. public static void Rev16_V(AILEmitterCtx Context)
  208. {
  209. EmitRev_V(Context, ContainerSize: 1);
  210. }
  211. public static void Rev32_V(AILEmitterCtx Context)
  212. {
  213. EmitRev_V(Context, ContainerSize: 2);
  214. }
  215. public static void Rev64_V(AILEmitterCtx Context)
  216. {
  217. EmitRev_V(Context, ContainerSize: 3);
  218. }
  219. private static void EmitRev_V(AILEmitterCtx Context, int ContainerSize)
  220. {
  221. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  222. if (Op.Size >= ContainerSize)
  223. {
  224. throw new InvalidOperationException();
  225. }
  226. int Bytes = Op.GetBitsCount() >> 3;
  227. int Elems = Bytes >> Op.Size;
  228. int ContainerMask = (1 << (ContainerSize - Op.Size)) - 1;
  229. for (int Index = 0; Index < Elems; Index++)
  230. {
  231. int RevIndex = Index ^ ContainerMask;
  232. EmitVectorExtractZx(Context, Op.Rn, RevIndex, Op.Size);
  233. EmitVectorInsertTmp(Context, Index, Op.Size);
  234. }
  235. Context.EmitLdvectmp();
  236. Context.EmitStvec(Op.Rd);
  237. if (Op.RegisterSize == ARegisterSize.SIMD64)
  238. {
  239. EmitVectorZeroUpper(Context, Op.Rd);
  240. }
  241. }
  242. }
  243. }