ASoftFallback.cs 7.2 KB

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