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