DecoderHelper.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using ARMeilleure.Common;
  2. namespace ARMeilleure.Decoders
  3. {
  4. static class DecoderHelper
  5. {
  6. static DecoderHelper()
  7. {
  8. Imm8ToFP32Table = BuildImm8ToFP32Table();
  9. Imm8ToFP64Table = BuildImm8ToFP64Table();
  10. }
  11. public static readonly uint[] Imm8ToFP32Table;
  12. public static readonly ulong[] Imm8ToFP64Table;
  13. private static uint[] BuildImm8ToFP32Table()
  14. {
  15. uint[] tbl = new uint[256];
  16. for (int idx = 0; idx < 256; idx++)
  17. {
  18. tbl[idx] = ExpandImm8ToFP32((uint)idx);
  19. }
  20. return tbl;
  21. }
  22. private static ulong[] BuildImm8ToFP64Table()
  23. {
  24. ulong[] tbl = new ulong[256];
  25. for (int idx = 0; idx < 256; idx++)
  26. {
  27. tbl[idx] = ExpandImm8ToFP64((ulong)idx);
  28. }
  29. return tbl;
  30. }
  31. // abcdefgh -> aBbbbbbc defgh000 00000000 00000000 (B = ~b)
  32. private static uint ExpandImm8ToFP32(uint imm)
  33. {
  34. uint MoveBit(uint bits, int from, int to)
  35. {
  36. return ((bits >> from) & 1U) << to;
  37. }
  38. return MoveBit(imm, 7, 31) | MoveBit(~imm, 6, 30) |
  39. MoveBit(imm, 6, 29) | MoveBit( imm, 6, 28) |
  40. MoveBit(imm, 6, 27) | MoveBit( imm, 6, 26) |
  41. MoveBit(imm, 6, 25) | MoveBit( imm, 5, 24) |
  42. MoveBit(imm, 4, 23) | MoveBit( imm, 3, 22) |
  43. MoveBit(imm, 2, 21) | MoveBit( imm, 1, 20) |
  44. MoveBit(imm, 0, 19);
  45. }
  46. // abcdefgh -> aBbbbbbb bbcdefgh 00000000 00000000 00000000 00000000 00000000 00000000 (B = ~b)
  47. private static ulong ExpandImm8ToFP64(ulong imm)
  48. {
  49. ulong MoveBit(ulong bits, int from, int to)
  50. {
  51. return ((bits >> from) & 1UL) << to;
  52. }
  53. return MoveBit(imm, 7, 63) | MoveBit(~imm, 6, 62) |
  54. MoveBit(imm, 6, 61) | MoveBit( imm, 6, 60) |
  55. MoveBit(imm, 6, 59) | MoveBit( imm, 6, 58) |
  56. MoveBit(imm, 6, 57) | MoveBit( imm, 6, 56) |
  57. MoveBit(imm, 6, 55) | MoveBit( imm, 6, 54) |
  58. MoveBit(imm, 5, 53) | MoveBit( imm, 4, 52) |
  59. MoveBit(imm, 3, 51) | MoveBit( imm, 2, 50) |
  60. MoveBit(imm, 1, 49) | MoveBit( imm, 0, 48);
  61. }
  62. public struct BitMask
  63. {
  64. public long WMask;
  65. public long TMask;
  66. public int Pos;
  67. public int Shift;
  68. public bool IsUndefined;
  69. public static BitMask Invalid => new BitMask { IsUndefined = true };
  70. }
  71. public static BitMask DecodeBitMask(int opCode, bool immediate)
  72. {
  73. int immS = (opCode >> 10) & 0x3f;
  74. int immR = (opCode >> 16) & 0x3f;
  75. int n = (opCode >> 22) & 1;
  76. int sf = (opCode >> 31) & 1;
  77. int length = BitUtils.HighestBitSet((~immS & 0x3f) | (n << 6));
  78. if (length < 1 || (sf == 0 && n != 0))
  79. {
  80. return BitMask.Invalid;
  81. }
  82. int size = 1 << length;
  83. int levels = size - 1;
  84. int s = immS & levels;
  85. int r = immR & levels;
  86. if (immediate && s == levels)
  87. {
  88. return BitMask.Invalid;
  89. }
  90. long wMask = BitUtils.FillWithOnes(s + 1);
  91. long tMask = BitUtils.FillWithOnes(((s - r) & levels) + 1);
  92. if (r > 0)
  93. {
  94. wMask = BitUtils.RotateRight(wMask, r, size);
  95. wMask &= BitUtils.FillWithOnes(size);
  96. }
  97. return new BitMask()
  98. {
  99. WMask = BitUtils.Replicate(wMask, size),
  100. TMask = BitUtils.Replicate(tMask, size),
  101. Pos = immS,
  102. Shift = immR
  103. };
  104. }
  105. public static long DecodeImm24_2(int opCode)
  106. {
  107. return ((long)opCode << 40) >> 38;
  108. }
  109. public static long DecodeImm26_2(int opCode)
  110. {
  111. return ((long)opCode << 38) >> 36;
  112. }
  113. public static long DecodeImmS19_2(int opCode)
  114. {
  115. return (((long)opCode << 40) >> 43) & ~3;
  116. }
  117. public static long DecodeImmS14_2(int opCode)
  118. {
  119. return (((long)opCode << 45) >> 48) & ~3;
  120. }
  121. public static bool VectorArgumentsInvalid(bool q, params int[] args)
  122. {
  123. if (q)
  124. {
  125. for (int i = 0; i < args.Length; i++)
  126. {
  127. if ((args[i] & 1) == 1)
  128. {
  129. return true;
  130. }
  131. }
  132. }
  133. return false;
  134. }
  135. }
  136. }