ASoftFallback.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using ChocolArm64.Translation;
  2. using System;
  3. using System.Numerics;
  4. namespace ChocolArm64.Instruction
  5. {
  6. static class ASoftFallback
  7. {
  8. public static void EmitCall(AILEmitterCtx Context, string MthdName)
  9. {
  10. Context.EmitCall(typeof(ASoftFallback), MthdName);
  11. }
  12. public static ulong CountLeadingSigns(ulong Value, int Size)
  13. {
  14. return CountLeadingZeros((Value >> 1) ^ Value, Size - 1);
  15. }
  16. public static ulong CountLeadingZeros(ulong Value, int Size)
  17. {
  18. int HighBit = Size - 1;
  19. for (int Bit = HighBit; Bit >= 0; Bit--)
  20. {
  21. if (((Value >> Bit) & 1) != 0)
  22. {
  23. return (ulong)(HighBit - Bit);
  24. }
  25. }
  26. return (ulong)Size;
  27. }
  28. private const uint Crc32RevPoly = 0xedb88320;
  29. private const uint Crc32cRevPoly = 0x82f63b78;
  30. public static uint Crc32b(uint Crc, byte Val) => Crc32 (Crc, Crc32RevPoly, Val);
  31. public static uint Crc32h(uint Crc, ushort Val) => Crc32h(Crc, Crc32RevPoly, Val);
  32. public static uint Crc32w(uint Crc, uint Val) => Crc32w(Crc, Crc32RevPoly, Val);
  33. public static uint Crc32x(uint Crc, ulong Val) => Crc32x(Crc, Crc32RevPoly, Val);
  34. public static uint Crc32cb(uint Crc, byte Val) => Crc32 (Crc, Crc32cRevPoly, Val);
  35. public static uint Crc32ch(uint Crc, ushort Val) => Crc32h(Crc, Crc32cRevPoly, Val);
  36. public static uint Crc32cw(uint Crc, uint Val) => Crc32w(Crc, Crc32cRevPoly, Val);
  37. public static uint Crc32cx(uint Crc, ulong Val) => Crc32x(Crc, Crc32cRevPoly, Val);
  38. private static uint Crc32h(uint Crc, uint Poly, ushort Val)
  39. {
  40. Crc = Crc32(Crc, Poly, (byte)(Val >> 0));
  41. Crc = Crc32(Crc, Poly, (byte)(Val >> 8));
  42. return Crc;
  43. }
  44. private static uint Crc32w(uint Crc, uint Poly, uint Val)
  45. {
  46. Crc = Crc32(Crc, Poly, (byte)(Val >> 0));
  47. Crc = Crc32(Crc, Poly, (byte)(Val >> 8));
  48. Crc = Crc32(Crc, Poly, (byte)(Val >> 16));
  49. Crc = Crc32(Crc, Poly, (byte)(Val >> 24));
  50. return Crc;
  51. }
  52. private static uint Crc32x(uint Crc, uint Poly, ulong Val)
  53. {
  54. Crc = Crc32(Crc, Poly, (byte)(Val >> 0));
  55. Crc = Crc32(Crc, Poly, (byte)(Val >> 8));
  56. Crc = Crc32(Crc, Poly, (byte)(Val >> 16));
  57. Crc = Crc32(Crc, Poly, (byte)(Val >> 24));
  58. Crc = Crc32(Crc, Poly, (byte)(Val >> 32));
  59. Crc = Crc32(Crc, Poly, (byte)(Val >> 40));
  60. Crc = Crc32(Crc, Poly, (byte)(Val >> 48));
  61. Crc = Crc32(Crc, Poly, (byte)(Val >> 56));
  62. return Crc;
  63. }
  64. private static uint Crc32(uint Crc, uint Poly, byte Val)
  65. {
  66. Crc ^= Val;
  67. for (int Bit = 7; Bit >= 0; Bit--)
  68. {
  69. uint Mask = (uint)(-(int)(Crc & 1));
  70. Crc = (Crc >> 1) ^ (Poly & Mask);
  71. }
  72. return Crc;
  73. }
  74. public static uint ReverseBits32(uint Value)
  75. {
  76. Value = ((Value & 0xaaaaaaaa) >> 1) | ((Value & 0x55555555) << 1);
  77. Value = ((Value & 0xcccccccc) >> 2) | ((Value & 0x33333333) << 2);
  78. Value = ((Value & 0xf0f0f0f0) >> 4) | ((Value & 0x0f0f0f0f) << 4);
  79. Value = ((Value & 0xff00ff00) >> 8) | ((Value & 0x00ff00ff) << 8);
  80. return (Value >> 16) | (Value << 16);
  81. }
  82. public static ulong ReverseBits64(ulong Value)
  83. {
  84. Value = ((Value & 0xaaaaaaaaaaaaaaaa) >> 1) | ((Value & 0x5555555555555555) << 1);
  85. Value = ((Value & 0xcccccccccccccccc) >> 2) | ((Value & 0x3333333333333333) << 2);
  86. Value = ((Value & 0xf0f0f0f0f0f0f0f0) >> 4) | ((Value & 0x0f0f0f0f0f0f0f0f) << 4);
  87. Value = ((Value & 0xff00ff00ff00ff00) >> 8) | ((Value & 0x00ff00ff00ff00ff) << 8);
  88. Value = ((Value & 0xffff0000ffff0000) >> 16) | ((Value & 0x0000ffff0000ffff) << 16);
  89. return (Value >> 32) | (Value << 32);
  90. }
  91. public static uint ReverseBytes16_32(uint Value) => (uint)ReverseBytes16_64(Value);
  92. public static uint ReverseBytes32_32(uint Value) => (uint)ReverseBytes32_64(Value);
  93. public static ulong ReverseBytes16_64(ulong Value) => ReverseBytes(Value, RevSize.Rev16);
  94. public static ulong ReverseBytes32_64(ulong Value) => ReverseBytes(Value, RevSize.Rev32);
  95. public static ulong ReverseBytes64(ulong Value) => ReverseBytes(Value, RevSize.Rev64);
  96. private enum RevSize
  97. {
  98. Rev16,
  99. Rev32,
  100. Rev64
  101. }
  102. private static ulong ReverseBytes(ulong Value, RevSize Size)
  103. {
  104. Value = ((Value & 0xff00ff00ff00ff00) >> 8) | ((Value & 0x00ff00ff00ff00ff) << 8);
  105. if (Size == RevSize.Rev16)
  106. {
  107. return Value;
  108. }
  109. Value = ((Value & 0xffff0000ffff0000) >> 16) | ((Value & 0x0000ffff0000ffff) << 16);
  110. if (Size == RevSize.Rev32)
  111. {
  112. return Value;
  113. }
  114. Value = ((Value & 0xffffffff00000000) >> 32) | ((Value & 0x00000000ffffffff) << 32);
  115. if (Size == RevSize.Rev64)
  116. {
  117. return Value;
  118. }
  119. throw new ArgumentException(nameof(Size));
  120. }
  121. public static long SMulHi128(long LHS, long RHS)
  122. {
  123. return (long)(BigInteger.Multiply(LHS, RHS) >> 64);
  124. }
  125. public static ulong UMulHi128(ulong LHS, ulong RHS)
  126. {
  127. return (ulong)(BigInteger.Multiply(LHS, RHS) >> 64);
  128. }
  129. }
  130. }