2018-09-08 19:51:50 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections;
|
|
|
|
|
|
2019-10-13 08:02:07 +02:00
|
|
|
|
namespace Ryujinx.Graphics.Texture.Astc
|
2018-09-08 19:51:50 +02:00
|
|
|
|
{
|
|
|
|
|
public class BitArrayStream
|
|
|
|
|
{
|
|
|
|
|
public BitArray BitsArray;
|
|
|
|
|
|
|
|
|
|
public int Position { get; private set; }
|
|
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
|
public BitArrayStream(BitArray bitArray)
|
2018-09-08 19:51:50 +02:00
|
|
|
|
{
|
2019-03-04 02:45:25 +01:00
|
|
|
|
BitsArray = bitArray;
|
2018-09-08 19:51:50 +02:00
|
|
|
|
Position = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
|
public short ReadBits(int length)
|
2018-09-08 19:51:50 +02:00
|
|
|
|
{
|
2019-03-04 02:45:25 +01:00
|
|
|
|
int retValue = 0;
|
|
|
|
|
for (int i = Position; i < Position + length; i++)
|
2018-09-08 19:51:50 +02:00
|
|
|
|
{
|
|
|
|
|
if (BitsArray[i])
|
|
|
|
|
{
|
2019-03-04 02:45:25 +01:00
|
|
|
|
retValue |= 1 << (i - Position);
|
2018-09-08 19:51:50 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
|
Position += length;
|
|
|
|
|
return (short)retValue;
|
2018-09-08 19:51:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
|
public int ReadBits(int start, int end)
|
2018-09-08 19:51:50 +02:00
|
|
|
|
{
|
2019-03-04 02:45:25 +01:00
|
|
|
|
int retValue = 0;
|
|
|
|
|
for (int i = start; i <= end; i++)
|
2018-09-08 19:51:50 +02:00
|
|
|
|
{
|
|
|
|
|
if (BitsArray[i])
|
|
|
|
|
{
|
2019-03-04 02:45:25 +01:00
|
|
|
|
retValue |= 1 << (i - start);
|
2018-09-08 19:51:50 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
|
return retValue;
|
2018-09-08 19:51:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
|
public int ReadBit(int index)
|
2018-09-08 19:51:50 +02:00
|
|
|
|
{
|
2019-03-04 02:45:25 +01:00
|
|
|
|
return Convert.ToInt32(BitsArray[index]);
|
2018-09-08 19:51:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
|
public void WriteBits(int value, int length)
|
2018-09-08 19:51:50 +02:00
|
|
|
|
{
|
2019-03-04 02:45:25 +01:00
|
|
|
|
for (int i = Position; i < Position + length; i++)
|
2018-09-08 19:51:50 +02:00
|
|
|
|
{
|
2019-03-04 02:45:25 +01:00
|
|
|
|
BitsArray[i] = ((value >> (i - Position)) & 1) != 0;
|
2018-09-08 19:51:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
|
Position += length;
|
2018-09-08 19:51:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public byte[] ToByteArray()
|
|
|
|
|
{
|
2019-03-04 02:45:25 +01:00
|
|
|
|
byte[] retArray = new byte[(BitsArray.Length + 7) / 8];
|
|
|
|
|
BitsArray.CopyTo(retArray, 0);
|
|
|
|
|
return retArray;
|
2018-09-08 19:51:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
|
public static int Replicate(int value, int numberBits, int toBit)
|
2018-09-08 19:51:50 +02:00
|
|
|
|
{
|
2019-03-04 02:45:25 +01:00
|
|
|
|
if (numberBits == 0) return 0;
|
|
|
|
|
if (toBit == 0) return 0;
|
2018-09-08 19:51:50 +02:00
|
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
|
int tempValue = value & ((1 << numberBits) - 1);
|
|
|
|
|
int retValue = tempValue;
|
|
|
|
|
int resLength = numberBits;
|
2018-09-08 19:51:50 +02:00
|
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
|
while (resLength < toBit)
|
2018-09-08 19:51:50 +02:00
|
|
|
|
{
|
2019-03-04 02:45:25 +01:00
|
|
|
|
int comp = 0;
|
|
|
|
|
if (numberBits > toBit - resLength)
|
2018-09-08 19:51:50 +02:00
|
|
|
|
{
|
2019-03-04 02:45:25 +01:00
|
|
|
|
int newShift = toBit - resLength;
|
|
|
|
|
comp = numberBits - newShift;
|
|
|
|
|
numberBits = newShift;
|
2018-09-08 19:51:50 +02:00
|
|
|
|
}
|
2019-03-04 02:45:25 +01:00
|
|
|
|
retValue <<= numberBits;
|
|
|
|
|
retValue |= tempValue >> comp;
|
|
|
|
|
resLength += numberBits;
|
2018-09-08 19:51:50 +02:00
|
|
|
|
}
|
2019-03-04 02:45:25 +01:00
|
|
|
|
return retValue;
|
2018-09-08 19:51:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
|
public static int PopCnt(int number)
|
2018-09-08 19:51:50 +02:00
|
|
|
|
{
|
2019-03-04 02:45:25 +01:00
|
|
|
|
int counter;
|
|
|
|
|
for (counter = 0; number != 0; counter++)
|
2018-09-08 19:51:50 +02:00
|
|
|
|
{
|
2019-03-04 02:45:25 +01:00
|
|
|
|
number &= number - 1;
|
2018-09-08 19:51:50 +02:00
|
|
|
|
}
|
2019-03-04 02:45:25 +01:00
|
|
|
|
return counter;
|
2018-09-08 19:51:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void Swap<T>(ref T lhs, ref T rhs)
|
|
|
|
|
{
|
2019-03-04 02:45:25 +01:00
|
|
|
|
T temp = lhs;
|
2018-09-08 19:51:50 +02:00
|
|
|
|
lhs = rhs;
|
2019-03-04 02:45:25 +01:00
|
|
|
|
rhs = temp;
|
2018-09-08 19:51:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Transfers a bit as described in C.2.14
|
|
|
|
|
public static void BitTransferSigned(ref int a, ref int b)
|
|
|
|
|
{
|
|
|
|
|
b >>= 1;
|
|
|
|
|
b |= a & 0x80;
|
|
|
|
|
a >>= 1;
|
|
|
|
|
a &= 0x3F;
|
|
|
|
|
if ((a & 0x20) != 0) a -= 0x40;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|