ABitUtils.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. namespace ChocolArm64
  2. {
  3. static class ABitUtils
  4. {
  5. public static int HighestBitSet32(int Value)
  6. {
  7. for (int Bit = 31; Bit >= 0; Bit--)
  8. {
  9. if (((Value >> Bit) & 1) != 0)
  10. {
  11. return Bit;
  12. }
  13. }
  14. return -1;
  15. }
  16. private static readonly sbyte[] HbsNibbleTbl = { -1, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3 };
  17. public static int HighestBitSetNibble(int Value) => HbsNibbleTbl[Value & 0b1111];
  18. public static long Replicate(long Bits, int Size)
  19. {
  20. long Output = 0;
  21. for (int Bit = 0; Bit < 64; Bit += Size)
  22. {
  23. Output |= Bits << Bit;
  24. }
  25. return Output;
  26. }
  27. public static long FillWithOnes(int Bits)
  28. {
  29. return Bits == 64 ? -1L : (1L << Bits) - 1;
  30. }
  31. public static long RotateRight(long Bits, int Shift, int Size)
  32. {
  33. return (long)RotateRight((ulong)Bits, Shift, Size);
  34. }
  35. public static ulong RotateRight(ulong Bits, int Shift, int Size)
  36. {
  37. return (Bits >> Shift) | (Bits << (Size - Shift));
  38. }
  39. }
  40. }