blob: ca429944f6e24b11cc8bff2054683a9d44184b5d (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
using System.Numerics;
using System.Runtime.CompilerServices;
namespace Ryujinx.Common.Utilities
{
public static class BitfieldExtensions
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool Extract<T>(this T value, int lsb) where T : IBinaryInteger<T>
{
int bitSize = Unsafe.SizeOf<T>() * 8;
lsb &= bitSize - 1;
return !T.IsZero((value >>> lsb) & T.One);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T Extract<T>(this T value, int lsb, int length) where T : IBinaryInteger<T>
{
int bitSize = Unsafe.SizeOf<T>() * 8;
lsb &= bitSize - 1;
return (value >>> lsb) & (~T.Zero >>> (bitSize - length));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T ExtractSx<T>(this T value, int lsb, int length) where T : IBinaryInteger<T>
{
int bitSize = Unsafe.SizeOf<T>() * 8;
int shift = lsb & (bitSize - 1);
return (value << (bitSize - (shift + length))) >> (bitSize - length);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T Insert<T>(this T value, int lsb, bool toInsert) where T : IBinaryInteger<T>
{
int bitSize = Unsafe.SizeOf<T>() * 8;
lsb &= bitSize - 1;
T mask = T.One << lsb;
return (value & ~mask) | (toInsert ? mask : T.Zero);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T Insert<T>(this T value, int lsb, int length, T toInsert) where T : IBinaryInteger<T>
{
int bitSize = Unsafe.SizeOf<T>() * 8;
lsb &= bitSize - 1;
T mask = (~T.Zero >>> (bitSize - length)) << lsb;
return (value & ~mask) | ((toInsert << lsb) & mask);
}
}
}
|