ASoftFallback.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. private const uint Crc32RevPoly = 0xedb88320;
  28. private const uint Crc32cRevPoly = 0x82f63b78;
  29. public static uint Crc32b(uint Crc, byte Val) => Crc32 (Crc, Crc32RevPoly, Val);
  30. public static uint Crc32h(uint Crc, ushort Val) => Crc32h(Crc, Crc32RevPoly, Val);
  31. public static uint Crc32w(uint Crc, uint Val) => Crc32w(Crc, Crc32RevPoly, Val);
  32. public static uint Crc32x(uint Crc, ulong Val) => Crc32x(Crc, Crc32RevPoly, Val);
  33. public static uint Crc32cb(uint Crc, byte Val) => Crc32 (Crc, Crc32cRevPoly, Val);
  34. public static uint Crc32ch(uint Crc, ushort Val) => Crc32h(Crc, Crc32cRevPoly, Val);
  35. public static uint Crc32cw(uint Crc, uint Val) => Crc32w(Crc, Crc32cRevPoly, Val);
  36. public static uint Crc32cx(uint Crc, ulong Val) => Crc32x(Crc, Crc32cRevPoly, Val);
  37. private static uint Crc32h(uint Crc, uint Poly, ushort Val)
  38. {
  39. Crc = Crc32(Crc, Poly, (byte)(Val >> 0));
  40. Crc = Crc32(Crc, Poly, (byte)(Val >> 8));
  41. return Crc;
  42. }
  43. private static uint Crc32w(uint Crc, uint Poly, uint Val)
  44. {
  45. Crc = Crc32(Crc, Poly, (byte)(Val >> 0));
  46. Crc = Crc32(Crc, Poly, (byte)(Val >> 8));
  47. Crc = Crc32(Crc, Poly, (byte)(Val >> 16));
  48. Crc = Crc32(Crc, Poly, (byte)(Val >> 24));
  49. return Crc;
  50. }
  51. private static uint Crc32x(uint Crc, uint Poly, ulong Val)
  52. {
  53. Crc = Crc32(Crc, Poly, (byte)(Val >> 0));
  54. Crc = Crc32(Crc, Poly, (byte)(Val >> 8));
  55. Crc = Crc32(Crc, Poly, (byte)(Val >> 16));
  56. Crc = Crc32(Crc, Poly, (byte)(Val >> 24));
  57. Crc = Crc32(Crc, Poly, (byte)(Val >> 32));
  58. Crc = Crc32(Crc, Poly, (byte)(Val >> 40));
  59. Crc = Crc32(Crc, Poly, (byte)(Val >> 48));
  60. Crc = Crc32(Crc, Poly, (byte)(Val >> 56));
  61. return Crc;
  62. }
  63. private static uint Crc32(uint Crc, uint Poly, byte Val)
  64. {
  65. Crc ^= Val;
  66. for (int Bit = 7; Bit >= 0; Bit--)
  67. {
  68. uint Mask = (uint)(-(int)(Crc & 1));
  69. Crc = (Crc >> 1) ^ (Poly & Mask);
  70. }
  71. return Crc;
  72. }
  73. public static uint ReverseBits32(uint Value)
  74. {
  75. Value = ((Value & 0xaaaaaaaa) >> 1) | ((Value & 0x55555555) << 1);
  76. Value = ((Value & 0xcccccccc) >> 2) | ((Value & 0x33333333) << 2);
  77. Value = ((Value & 0xf0f0f0f0) >> 4) | ((Value & 0x0f0f0f0f) << 4);
  78. Value = ((Value & 0xff00ff00) >> 8) | ((Value & 0x00ff00ff) << 8);
  79. return (Value >> 16) | (Value << 16);
  80. }
  81. public static ulong ReverseBits64(ulong Value)
  82. {
  83. Value = ((Value & 0xaaaaaaaaaaaaaaaa) >> 1) | ((Value & 0x5555555555555555) << 1);
  84. Value = ((Value & 0xcccccccccccccccc) >> 2) | ((Value & 0x3333333333333333) << 2);
  85. Value = ((Value & 0xf0f0f0f0f0f0f0f0) >> 4) | ((Value & 0x0f0f0f0f0f0f0f0f) << 4);
  86. Value = ((Value & 0xff00ff00ff00ff00) >> 8) | ((Value & 0x00ff00ff00ff00ff) << 8);
  87. Value = ((Value & 0xffff0000ffff0000) >> 16) | ((Value & 0x0000ffff0000ffff) << 16);
  88. return (Value >> 32) | (Value << 32);
  89. }
  90. public static uint ReverseBytes16_32(uint Value) => (uint)ReverseBytes16_64(Value);
  91. public static uint ReverseBytes32_32(uint Value) => (uint)ReverseBytes32_64(Value);
  92. public static ulong ReverseBytes16_64(ulong Value) => ReverseBytes(Value, RevSize.Rev16);
  93. public static ulong ReverseBytes32_64(ulong Value) => ReverseBytes(Value, RevSize.Rev32);
  94. public static ulong ReverseBytes64(ulong Value) => ReverseBytes(Value, RevSize.Rev64);
  95. private enum RevSize
  96. {
  97. Rev16,
  98. Rev32,
  99. Rev64
  100. }
  101. private static ulong ReverseBytes(ulong Value, RevSize Size)
  102. {
  103. Value = ((Value & 0xff00ff00ff00ff00) >> 8) | ((Value & 0x00ff00ff00ff00ff) << 8);
  104. if (Size == RevSize.Rev16)
  105. {
  106. return Value;
  107. }
  108. Value = ((Value & 0xffff0000ffff0000) >> 16) | ((Value & 0x0000ffff0000ffff) << 16);
  109. if (Size == RevSize.Rev32)
  110. {
  111. return Value;
  112. }
  113. Value = ((Value & 0xffffffff00000000) >> 32) | ((Value & 0x00000000ffffffff) << 32);
  114. if (Size == RevSize.Rev64)
  115. {
  116. return Value;
  117. }
  118. throw new ArgumentException(nameof(Size));
  119. }
  120. public static long SMulHi128(long LHS, long RHS)
  121. {
  122. long Result = (long)UMulHi128((ulong)(LHS), (ulong)(RHS));
  123. if (LHS < 0) Result -= RHS;
  124. if (RHS < 0) Result -= LHS;
  125. return Result;
  126. }
  127. public static ulong UMulHi128(ulong LHS, ulong RHS)
  128. {
  129. //long multiplication
  130. //multiply 32 bits at a time in 64 bit, the result is what's carried over 64 bits.
  131. ulong LHigh = LHS >> 32;
  132. ulong LLow = LHS & 0xFFFFFFFF;
  133. ulong RHigh = RHS >> 32;
  134. ulong RLow = RHS & 0xFFFFFFFF;
  135. ulong Z2 = LLow * RLow;
  136. ulong T = LHigh * RLow + (Z2 >> 32);
  137. ulong Z1 = T & 0xFFFFFFFF;
  138. ulong Z0 = T >> 32;
  139. Z1 += LLow * RHigh;
  140. return LHigh * RHigh + Z0 + (Z1 >> 32);
  141. }
  142. }
  143. }