AInstEmitSimdLogical.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. EmitSse2Call(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. EmitVectorBinaryOpZx(Context, () =>
  26. {
  27. Context.Emit(OpCodes.Not);
  28. Context.Emit(OpCodes.And);
  29. });
  30. }
  31. public static void Bic_Vi(AILEmitterCtx Context)
  32. {
  33. EmitVectorImmBinaryOp(Context, () =>
  34. {
  35. Context.Emit(OpCodes.Not);
  36. Context.Emit(OpCodes.And);
  37. });
  38. }
  39. public static void Bif_V(AILEmitterCtx Context)
  40. {
  41. EmitBitBif(Context, true);
  42. }
  43. public static void Bit_V(AILEmitterCtx Context)
  44. {
  45. EmitBitBif(Context, false);
  46. }
  47. private static void EmitBitBif(AILEmitterCtx Context, bool NotRm)
  48. {
  49. AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
  50. int Bytes = Op.GetBitsCount() >> 3;
  51. int Elems = Bytes >> Op.Size;
  52. for (int Index = 0; Index < Elems; Index++)
  53. {
  54. EmitVectorExtractZx(Context, Op.Rd, Index, Op.Size);
  55. EmitVectorExtractZx(Context, Op.Rn, Index, Op.Size);
  56. Context.Emit(OpCodes.Xor);
  57. EmitVectorExtractZx(Context, Op.Rm, Index, Op.Size);
  58. if (NotRm)
  59. {
  60. Context.Emit(OpCodes.Not);
  61. }
  62. Context.Emit(OpCodes.And);
  63. EmitVectorExtractZx(Context, Op.Rd, Index, Op.Size);
  64. Context.Emit(OpCodes.Xor);
  65. EmitVectorInsert(Context, Op.Rd, Index, Op.Size);
  66. }
  67. if (Op.RegisterSize == ARegisterSize.SIMD64)
  68. {
  69. EmitVectorZeroUpper(Context, Op.Rd);
  70. }
  71. }
  72. public static void Bsl_V(AILEmitterCtx Context)
  73. {
  74. EmitVectorTernaryOpZx(Context, () =>
  75. {
  76. Context.EmitSttmp();
  77. Context.EmitLdtmp();
  78. Context.Emit(OpCodes.Xor);
  79. Context.Emit(OpCodes.And);
  80. Context.EmitLdtmp();
  81. Context.Emit(OpCodes.Xor);
  82. });
  83. }
  84. public static void Eor_V(AILEmitterCtx Context)
  85. {
  86. if (AOptimizations.UseSse2)
  87. {
  88. EmitSse2Call(Context, nameof(Sse2.Xor));
  89. }
  90. else
  91. {
  92. EmitVectorBinaryOpZx(Context, () => Context.Emit(OpCodes.Xor));
  93. }
  94. }
  95. public static void Not_V(AILEmitterCtx Context)
  96. {
  97. EmitVectorUnaryOpZx(Context, () => Context.Emit(OpCodes.Not));
  98. }
  99. public static void Orn_V(AILEmitterCtx Context)
  100. {
  101. EmitVectorBinaryOpZx(Context, () =>
  102. {
  103. Context.Emit(OpCodes.Not);
  104. Context.Emit(OpCodes.Or);
  105. });
  106. }
  107. public static void Orr_V(AILEmitterCtx Context)
  108. {
  109. if (AOptimizations.UseSse2)
  110. {
  111. EmitSse2Call(Context, nameof(Sse2.Or));
  112. }
  113. else
  114. {
  115. EmitVectorBinaryOpZx(Context, () => Context.Emit(OpCodes.Or));
  116. }
  117. }
  118. public static void Orr_Vi(AILEmitterCtx Context)
  119. {
  120. EmitVectorImmBinaryOp(Context, () => Context.Emit(OpCodes.Or));
  121. }
  122. public static void Rbit_V(AILEmitterCtx Context)
  123. {
  124. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  125. int Elems = Op.RegisterSize == ARegisterSize.SIMD128 ? 16 : 8;
  126. for (int Index = 0; Index < Elems; Index++)
  127. {
  128. EmitVectorExtractZx(Context, Op.Rn, Index, 0);
  129. Context.Emit(OpCodes.Conv_U4);
  130. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.ReverseBits8));
  131. Context.Emit(OpCodes.Conv_U8);
  132. EmitVectorInsert(Context, Op.Rd, Index, 0);
  133. }
  134. if (Op.RegisterSize == ARegisterSize.SIMD64)
  135. {
  136. EmitVectorZeroUpper(Context, Op.Rd);
  137. }
  138. }
  139. public static void Rev16_V(AILEmitterCtx Context)
  140. {
  141. EmitRev_V(Context, ContainerSize: 1);
  142. }
  143. public static void Rev32_V(AILEmitterCtx Context)
  144. {
  145. EmitRev_V(Context, ContainerSize: 2);
  146. }
  147. public static void Rev64_V(AILEmitterCtx Context)
  148. {
  149. EmitRev_V(Context, ContainerSize: 3);
  150. }
  151. private static void EmitRev_V(AILEmitterCtx Context, int ContainerSize)
  152. {
  153. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  154. if (Op.Size >= ContainerSize)
  155. {
  156. throw new InvalidOperationException();
  157. }
  158. int Bytes = Op.GetBitsCount() >> 3;
  159. int Elems = Bytes >> Op.Size;
  160. int ContainerMask = (1 << (ContainerSize - Op.Size)) - 1;
  161. for (int Index = 0; Index < Elems; Index++)
  162. {
  163. int RevIndex = Index ^ ContainerMask;
  164. EmitVectorExtractZx(Context, Op.Rn, RevIndex, Op.Size);
  165. EmitVectorInsertTmp(Context, Index, Op.Size);
  166. }
  167. Context.EmitLdvectmp();
  168. Context.EmitStvec(Op.Rd);
  169. if (Op.RegisterSize == ARegisterSize.SIMD64)
  170. {
  171. EmitVectorZeroUpper(Context, Op.Rd);
  172. }
  173. }
  174. }
  175. }