ASoftFallback.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using ChocolArm64.Translation;
  2. using System;
  3. namespace ChocolArm64.Instruction
  4. {
  5. static class ASoftFallback
  6. {
  7. public static void EmitCall(AILEmitterCtx Context, string MthdName)
  8. {
  9. Context.EmitCall(typeof(ASoftFallback), MthdName);
  10. }
  11. public static ulong CountLeadingSigns(ulong Value, int Size)
  12. {
  13. return CountLeadingZeros((Value >> 1) ^ Value, Size - 1);
  14. }
  15. public static ulong CountLeadingZeros(ulong Value, int Size)
  16. {
  17. int HighBit = Size - 1;
  18. for (int Bit = HighBit; Bit >= 0; Bit--)
  19. {
  20. if (((Value >> Bit) & 1) != 0)
  21. {
  22. return (ulong)(HighBit - Bit);
  23. }
  24. }
  25. return (ulong)Size;
  26. }
  27. public static uint CountSetBits8(uint Value)
  28. {
  29. Value = ((Value >> 1) & 0x55) + (Value & 0x55);
  30. Value = ((Value >> 2) & 0x33) + (Value & 0x33);
  31. return (Value >> 4) + (Value & 0x0f);
  32. }
  33. private const uint Crc32RevPoly = 0xedb88320;
  34. private const uint Crc32cRevPoly = 0x82f63b78;
  35. public static uint Crc32b(uint Crc, byte Val) => Crc32 (Crc, Crc32RevPoly, Val);
  36. public static uint Crc32h(uint Crc, ushort Val) => Crc32h(Crc, Crc32RevPoly, Val);
  37. public static uint Crc32w(uint Crc, uint Val) => Crc32w(Crc, Crc32RevPoly, Val);
  38. public static uint Crc32x(uint Crc, ulong Val) => Crc32x(Crc, Crc32RevPoly, Val);
  39. public static uint Crc32cb(uint Crc, byte Val) => Crc32 (Crc, Crc32cRevPoly, Val);
  40. public static uint Crc32ch(uint Crc, ushort Val) => Crc32h(Crc, Crc32cRevPoly, Val);
  41. public static uint Crc32cw(uint Crc, uint Val) => Crc32w(Crc, Crc32cRevPoly, Val);
  42. public static uint Crc32cx(uint Crc, ulong Val) => Crc32x(Crc, Crc32cRevPoly, Val);
  43. private static uint Crc32h(uint Crc, uint Poly, ushort Val)
  44. {
  45. Crc = Crc32(Crc, Poly, (byte)(Val >> 0));
  46. Crc = Crc32(Crc, Poly, (byte)(Val >> 8));
  47. return Crc;
  48. }
  49. private static uint Crc32w(uint Crc, uint Poly, uint Val)
  50. {
  51. Crc = Crc32(Crc, Poly, (byte)(Val >> 0));
  52. Crc = Crc32(Crc, Poly, (byte)(Val >> 8));
  53. Crc = Crc32(Crc, Poly, (byte)(Val >> 16));
  54. Crc = Crc32(Crc, Poly, (byte)(Val >> 24));
  55. return Crc;
  56. }
  57. private static uint Crc32x(uint Crc, uint Poly, ulong Val)
  58. {
  59. Crc = Crc32(Crc, Poly, (byte)(Val >> 0));
  60. Crc = Crc32(Crc, Poly, (byte)(Val >> 8));
  61. Crc = Crc32(Crc, Poly, (byte)(Val >> 16));
  62. Crc = Crc32(Crc, Poly, (byte)(Val >> 24));
  63. Crc = Crc32(Crc, Poly, (byte)(Val >> 32));
  64. Crc = Crc32(Crc, Poly, (byte)(Val >> 40));
  65. Crc = Crc32(Crc, Poly, (byte)(Val >> 48));
  66. Crc = Crc32(Crc, Poly, (byte)(Val >> 56));
  67. return Crc;
  68. }
  69. private static uint Crc32(uint Crc, uint Poly, byte Val)
  70. {
  71. Crc ^= Val;
  72. for (int Bit = 7; Bit >= 0; Bit--)
  73. {
  74. uint Mask = (uint)(-(int)(Crc & 1));
  75. Crc = (Crc >> 1) ^ (Poly & Mask);
  76. }
  77. return Crc;
  78. }
  79. public static uint ReverseBits8(uint Value)
  80. {
  81. Value = ((Value & 0xaa) >> 1) | ((Value & 0x55) << 1);
  82. Value = ((Value & 0xcc) >> 2) | ((Value & 0x33) << 2);
  83. return (Value >> 4) | ((Value & 0x0f) << 4);
  84. }
  85. public static uint ReverseBits32(uint Value)
  86. {
  87. Value = ((Value & 0xaaaaaaaa) >> 1) | ((Value & 0x55555555) << 1);
  88. Value = ((Value & 0xcccccccc) >> 2) | ((Value & 0x33333333) << 2);
  89. Value = ((Value & 0xf0f0f0f0) >> 4) | ((Value & 0x0f0f0f0f) << 4);
  90. Value = ((Value & 0xff00ff00) >> 8) | ((Value & 0x00ff00ff) << 8);
  91. return (Value >> 16) | (Value << 16);
  92. }
  93. public static ulong ReverseBits64(ulong Value)
  94. {
  95. Value = ((Value & 0xaaaaaaaaaaaaaaaa) >> 1 ) | ((Value & 0x5555555555555555) << 1 );
  96. Value = ((Value & 0xcccccccccccccccc) >> 2 ) | ((Value & 0x3333333333333333) << 2 );
  97. Value = ((Value & 0xf0f0f0f0f0f0f0f0) >> 4 ) | ((Value & 0x0f0f0f0f0f0f0f0f) << 4 );
  98. Value = ((Value & 0xff00ff00ff00ff00) >> 8 ) | ((Value & 0x00ff00ff00ff00ff) << 8 );
  99. Value = ((Value & 0xffff0000ffff0000) >> 16) | ((Value & 0x0000ffff0000ffff) << 16);
  100. return (Value >> 32) | (Value << 32);
  101. }
  102. public static uint ReverseBytes16_32(uint Value) => (uint)ReverseBytes16_64(Value);
  103. public static uint ReverseBytes32_32(uint Value) => (uint)ReverseBytes32_64(Value);
  104. public static ulong ReverseBytes16_64(ulong Value) => ReverseBytes(Value, RevSize.Rev16);
  105. public static ulong ReverseBytes32_64(ulong Value) => ReverseBytes(Value, RevSize.Rev32);
  106. public static ulong ReverseBytes64(ulong Value) => ReverseBytes(Value, RevSize.Rev64);
  107. private enum RevSize
  108. {
  109. Rev16,
  110. Rev32,
  111. Rev64
  112. }
  113. private static ulong ReverseBytes(ulong Value, RevSize Size)
  114. {
  115. Value = ((Value & 0xff00ff00ff00ff00) >> 8) | ((Value & 0x00ff00ff00ff00ff) << 8);
  116. if (Size == RevSize.Rev16)
  117. {
  118. return Value;
  119. }
  120. Value = ((Value & 0xffff0000ffff0000) >> 16) | ((Value & 0x0000ffff0000ffff) << 16);
  121. if (Size == RevSize.Rev32)
  122. {
  123. return Value;
  124. }
  125. Value = ((Value & 0xffffffff00000000) >> 32) | ((Value & 0x00000000ffffffff) << 32);
  126. if (Size == RevSize.Rev64)
  127. {
  128. return Value;
  129. }
  130. throw new ArgumentException(nameof(Size));
  131. }
  132. public static long SMulHi128(long LHS, long RHS)
  133. {
  134. long Result = (long)UMulHi128((ulong)(LHS), (ulong)(RHS));
  135. if (LHS < 0) Result -= RHS;
  136. if (RHS < 0) Result -= LHS;
  137. return Result;
  138. }
  139. public static ulong UMulHi128(ulong LHS, ulong RHS)
  140. {
  141. //long multiplication
  142. //multiply 32 bits at a time in 64 bit, the result is what's carried over 64 bits.
  143. ulong LHigh = LHS >> 32;
  144. ulong LLow = LHS & 0xFFFFFFFF;
  145. ulong RHigh = RHS >> 32;
  146. ulong RLow = RHS & 0xFFFFFFFF;
  147. ulong Z2 = LLow * RLow;
  148. ulong T = LHigh * RLow + (Z2 >> 32);
  149. ulong Z1 = T & 0xFFFFFFFF;
  150. ulong Z0 = T >> 32;
  151. Z1 += LLow * RHigh;
  152. return LHigh * RHigh + Z0 + (Z1 >> 32);
  153. }
  154. }
  155. }