Bits.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. namespace Ryujinx.Graphics.Texture.Astc
  2. {
  3. internal static class Bits
  4. {
  5. public static readonly ushort[] Replicate8_16Table;
  6. public static readonly byte[] Replicate1_7Table;
  7. static Bits()
  8. {
  9. Replicate8_16Table = new ushort[0x200];
  10. Replicate1_7Table = new byte[0x200];
  11. for (int i = 0; i < 0x200; i++)
  12. {
  13. Replicate8_16Table[i] = (ushort)Replicate(i, 8, 16);
  14. Replicate1_7Table[i] = (byte)Replicate(i, 1, 7);
  15. }
  16. }
  17. public static int Replicate8_16(int value)
  18. {
  19. return Replicate8_16Table[value];
  20. }
  21. public static int Replicate1_7(int value)
  22. {
  23. return Replicate1_7Table[value];
  24. }
  25. public static int Replicate(int value, int numberBits, int toBit)
  26. {
  27. if (numberBits == 0) return 0;
  28. if (toBit == 0) return 0;
  29. int tempValue = value & ((1 << numberBits) - 1);
  30. int retValue = tempValue;
  31. int resLength = numberBits;
  32. while (resLength < toBit)
  33. {
  34. int comp = 0;
  35. if (numberBits > toBit - resLength)
  36. {
  37. int newShift = toBit - resLength;
  38. comp = numberBits - newShift;
  39. numberBits = newShift;
  40. }
  41. retValue <<= numberBits;
  42. retValue |= tempValue >> comp;
  43. resLength += numberBits;
  44. }
  45. return retValue;
  46. }
  47. // Transfers a bit as described in C.2.14
  48. public static void BitTransferSigned(ref int a, ref int b)
  49. {
  50. b >>= 1;
  51. b |= a & 0x80;
  52. a >>= 1;
  53. a &= 0x3F;
  54. if ((a & 0x20) != 0) a -= 0x40;
  55. }
  56. }
  57. }