BitUtils.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using System;
  2. using System.Diagnostics;
  3. using System.Numerics;
  4. using System.Runtime.CompilerServices;
  5. namespace Ryujinx.Graphics.Nvdec.Vp9.Common
  6. {
  7. internal static class BitUtils
  8. {
  9. // FIXME: Enable inlining here after AVX2 gather bug is fixed.
  10. // [MethodImpl(MethodImplOptions.AggressiveInlining)]
  11. public static byte ClipPixel(int val)
  12. {
  13. return (byte)((val > 255) ? 255 : (val < 0) ? 0 : val);
  14. }
  15. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  16. public static ushort ClipPixelHighbd(int val, int bd)
  17. {
  18. return bd switch
  19. {
  20. 10 => (ushort)Math.Clamp(val, 0, 1023),
  21. 12 => (ushort)Math.Clamp(val, 0, 4095),
  22. _ => (ushort)Math.Clamp(val, 0, 255)
  23. };
  24. }
  25. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  26. public static int RoundPowerOfTwo(int value, int n)
  27. {
  28. return (value + (1 << (n - 1))) >> n;
  29. }
  30. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  31. public static long RoundPowerOfTwo(long value, int n)
  32. {
  33. return (value + (1L << (n - 1))) >> n;
  34. }
  35. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  36. public static int AlignPowerOfTwo(int value, int n)
  37. {
  38. return (value + ((1 << n) - 1)) & ~((1 << n) - 1);
  39. }
  40. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  41. private static int GetMsb(uint n)
  42. {
  43. Debug.Assert(n != 0);
  44. return 31 ^ BitOperations.LeadingZeroCount(n);
  45. }
  46. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  47. public static int GetUnsignedBits(uint numValues)
  48. {
  49. return numValues > 0 ? GetMsb(numValues) + 1 : 0;
  50. }
  51. }
  52. }