AInstEmitSimdLogical.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 = Context.CurrOp.GetBitsCount() >> 3;
  51. for (int Index = 0; Index < (Bytes >> Op.Size); Index++)
  52. {
  53. EmitVectorExtractZx(Context, Op.Rd, Index, Op.Size);
  54. EmitVectorExtractZx(Context, Op.Rn, Index, Op.Size);
  55. Context.Emit(OpCodes.Xor);
  56. EmitVectorExtractZx(Context, Op.Rm, Index, Op.Size);
  57. if (NotRm)
  58. {
  59. Context.Emit(OpCodes.Not);
  60. }
  61. Context.Emit(OpCodes.And);
  62. EmitVectorExtractZx(Context, Op.Rd, Index, Op.Size);
  63. Context.Emit(OpCodes.Xor);
  64. EmitVectorInsert(Context, Op.Rd, Index, Op.Size);
  65. }
  66. if (Op.RegisterSize == ARegisterSize.SIMD64)
  67. {
  68. EmitVectorZeroUpper(Context, Op.Rd);
  69. }
  70. }
  71. public static void Bsl_V(AILEmitterCtx Context)
  72. {
  73. EmitVectorTernaryOpZx(Context, () =>
  74. {
  75. Context.EmitSttmp();
  76. Context.EmitLdtmp();
  77. Context.Emit(OpCodes.Xor);
  78. Context.Emit(OpCodes.And);
  79. Context.EmitLdtmp();
  80. Context.Emit(OpCodes.Xor);
  81. });
  82. }
  83. public static void Eor_V(AILEmitterCtx Context)
  84. {
  85. if (AOptimizations.UseSse2)
  86. {
  87. EmitSse2Call(Context, nameof(Sse2.Xor));
  88. }
  89. else
  90. {
  91. EmitVectorBinaryOpZx(Context, () => Context.Emit(OpCodes.Xor));
  92. }
  93. }
  94. public static void Not_V(AILEmitterCtx Context)
  95. {
  96. EmitVectorUnaryOpZx(Context, () => Context.Emit(OpCodes.Not));
  97. }
  98. public static void Orn_V(AILEmitterCtx Context)
  99. {
  100. EmitVectorBinaryOpZx(Context, () =>
  101. {
  102. Context.Emit(OpCodes.Not);
  103. Context.Emit(OpCodes.Or);
  104. });
  105. }
  106. public static void Orr_V(AILEmitterCtx Context)
  107. {
  108. if (AOptimizations.UseSse2)
  109. {
  110. EmitSse2Call(Context, nameof(Sse2.Or));
  111. }
  112. else
  113. {
  114. EmitVectorBinaryOpZx(Context, () => Context.Emit(OpCodes.Or));
  115. }
  116. }
  117. public static void Orr_Vi(AILEmitterCtx Context)
  118. {
  119. EmitVectorImmBinaryOp(Context, () => Context.Emit(OpCodes.Or));
  120. }
  121. public static void Rev16_V(AILEmitterCtx Context)
  122. {
  123. EmitRev_V(Context, ContainerSize: 1);
  124. }
  125. public static void Rev32_V(AILEmitterCtx Context)
  126. {
  127. EmitRev_V(Context, ContainerSize: 2);
  128. }
  129. public static void Rev64_V(AILEmitterCtx Context)
  130. {
  131. EmitRev_V(Context, ContainerSize: 3);
  132. }
  133. private static void EmitRev_V(AILEmitterCtx Context, int ContainerSize)
  134. {
  135. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  136. int Bytes = Context.CurrOp.GetBitsCount() >> 3;
  137. int Elems = Bytes >> Op.Size;
  138. if (Op.Size >= ContainerSize)
  139. {
  140. throw new InvalidOperationException();
  141. }
  142. int ContainerMask = (1 << (ContainerSize - Op.Size)) - 1;
  143. for (int Index = 0; Index < (Bytes >> Op.Size); Index++)
  144. {
  145. int RevIndex = Index ^ ContainerMask;
  146. EmitVectorExtractZx(Context, Op.Rn, RevIndex, Op.Size);
  147. EmitVectorInsertTmp(Context, Index, Op.Size);
  148. }
  149. Context.EmitLdvectmp();
  150. Context.EmitStvec(Op.Rd);
  151. if (Op.RegisterSize == ARegisterSize.SIMD64)
  152. {
  153. EmitVectorZeroUpper(Context, Op.Rd);
  154. }
  155. }
  156. }
  157. }