Optimizing verification

This commit is contained in:
chudov
2010-04-23 19:59:42 +00:00
parent 8a36121577
commit 3f717d14c8
18 changed files with 2192 additions and 1305 deletions

View File

@@ -4,16 +4,16 @@ using System.Text;
namespace CUETools.Codecs
{
public class Crc32
public static class Crc32
{
public uint[] table = new uint[256];
public static readonly uint[] table;
public uint ComputeChecksum(uint crc, byte val)
public static uint ComputeChecksum(uint crc, byte val)
{
return (crc >> 8) ^ table[(crc & 0xff) ^ val];
}
public unsafe uint ComputeChecksum(uint crc, byte* bytes, int count)
public static unsafe uint ComputeChecksum(uint crc, byte* bytes, int count)
{
fixed (uint *t = table)
for (int i = 0; i < count; i++)
@@ -21,19 +21,19 @@ namespace CUETools.Codecs
return crc;
}
public unsafe uint ComputeChecksum(uint crc, byte[] bytes, int pos, int count)
public static unsafe uint ComputeChecksum(uint crc, byte[] bytes, int pos, int count)
{
fixed (byte* pbytes = &bytes[pos])
return ComputeChecksum(crc, pbytes, count);
}
public uint ComputeChecksum(uint crc, uint s)
public static uint ComputeChecksum(uint crc, uint s)
{
return ComputeChecksum(ComputeChecksum(ComputeChecksum(ComputeChecksum(
crc, (byte)s), (byte)(s >> 8)), (byte)(s >> 16)), (byte)(s >> 24));
}
public unsafe uint ComputeChecksum(uint crc, int * samples, int count)
public static unsafe uint ComputeChecksum(uint crc, int* samples, int count)
{
for (int i = 0; i < count; i++)
{
@@ -44,7 +44,7 @@ namespace CUETools.Codecs
return crc;
}
uint Reflect(uint val, int ch)
static uint Reflect(uint val, int ch)
{
uint value = 0;
// Swap bit 0 for bit 7
@@ -60,8 +60,66 @@ namespace CUETools.Codecs
const uint ulPolynomial = 0x04c11db7;
public Crc32()
private static readonly uint[,] combineTable;
private static readonly uint[,] substractTable;
#if need_invert_binary_matrix
static unsafe void invert_binary_matrix(uint* mat, uint* inv, int rows)
{
int cols, i, j;
uint tmp;
cols = rows;
for (i = 0; i < rows; i++) inv[i] = (1U << i);
/* First -- convert into upper triangular */
for (i = 0; i < cols; i++)
{
/* Swap rows if we ave a zero i,i element. If we can't swap, then the
matrix was not invertible */
if ((mat[i] & (1 << i)) == 0)
{
for (j = i + 1; j < rows && (mat[j] & (1 << i)) == 0; j++) ;
if (j == rows)
throw new Exception("Matrix not invertible");
tmp = mat[i]; mat[i] = mat[j]; mat[j] = tmp;
tmp = inv[i]; inv[i] = inv[j]; inv[j] = tmp;
}
/* Now for each j>i, add A_ji*Ai to Aj */
for (j = i + 1; j != rows; j++)
{
if ((mat[j] & (1 << i)) != 0)
{
mat[j] ^= mat[i];
inv[j] ^= inv[i];
}
}
}
/* Now the matrix is upper triangular. Start at the top and multiply down */
for (i = rows - 1; i >= 0; i--)
{
for (j = 0; j < i; j++)
{
if ((mat[j] & (1 << i)) != 0)
{
/* mat[j] ^= mat[i]; */
inv[j] ^= inv[i];
}
}
}
}
#endif
static unsafe Crc32()
{
table = new uint[256];
for (uint i = 0; i < table.Length; i++)
{
table[i] = Reflect(i, 8) << 24;
@@ -69,11 +127,33 @@ namespace CUETools.Codecs
table[i] = (table[i] << 1) ^ ((table[i] & (1U << 31)) == 0 ? 0 : ulPolynomial);
table[i] = Reflect(table[i], 32);
}
combineTable = new uint[GF2_DIM, GF2_DIM];
substractTable = new uint[GF2_DIM, GF2_DIM];
combineTable[0, 0] = 0xedb88320; /* CRC-32 polynomial */
substractTable[0, 31] = 0xdb710641;
for (int n = 1; n < GF2_DIM; n++)
{
combineTable[0, n] = 1U << (n - 1);
substractTable[0, n - 1] = 1U << n;
}
fixed (uint* ct = &combineTable[0, 0], st = &substractTable[0, 0])
{
//for (int i = 0; i < GF2_DIM; i++)
// st[32 + i] = ct[i];
//invert_binary_matrix(st + 32, st, GF2_DIM);
for (int i = 1; i < GF2_DIM; i++)
{
gf2_matrix_square(ct + i * 32, ct + (i - 1) * 32);
gf2_matrix_square(st + i * 32, st + (i - 1) * 32);
}
}
}
const int GF2_DIM = 32;
//const int GF2_DIM2 = 67;
private unsafe uint gf2_matrix_times(uint* mat, uint vec)
private static unsafe uint gf2_matrix_times(uint* mat, uint vec)
{
return *(mat++) * (vec & 1) ^
*(mat++) * ((vec >>= 1) & 1) ^
@@ -110,63 +190,67 @@ namespace CUETools.Codecs
}
/* ========================================================================= */
private unsafe void gf2_matrix_square(uint *square, uint *mat)
private static unsafe void gf2_matrix_square(uint *square, uint *mat)
{
for (int n = 0; n < GF2_DIM; n++)
square[n] = gf2_matrix_times(mat, mat[n]);
}
public unsafe uint Combine(uint crc1, uint crc2, long len2)
public static unsafe uint Combine(uint crc1, uint crc2, long len2)
{
int n;
uint row;
uint* even = stackalloc uint[GF2_DIM]; /* even-power-of-two zeros operator */
uint* odd = stackalloc uint[GF2_DIM]; /* odd-power-of-two zeros operator */
/* degenerate case */
if (len2 == 0)
return crc1;
if (crc1 == 0)
return crc2;
if (len2 < 0)
throw new ArgumentException("crc.Combine length cannot be negative", "len2");
/* put operator for one zero bit in odd */
odd[0] = 0xedb88320; /* CRC-32 polynomial */
row = 1;
for (n = 1; n < GF2_DIM; n++) {
odd[n] = row;
row <<= 1;
fixed (uint* ct = &combineTable[0, 0])
{
int n = 3;
do
{
/* apply zeros operator for this bit of len2 */
if ((len2 & 1) != 0)
crc1 = gf2_matrix_times(ct + 32 * n, crc1);
len2 >>= 1;
n = (n + 1) & (GF2_DIM - 1);
/* if no more bits set, then done */
} while (len2 != 0);
}
/* put operator for two zero bits in even */
gf2_matrix_square(even, odd);
/* put operator for four zero bits in odd */
gf2_matrix_square(odd, even);
/* apply len2 zeros to crc1 (first square will put the operator for one
zero byte, eight zero bits, in even) */
do {
/* apply zeros operator for this bit of len2 */
gf2_matrix_square(even, odd);
if ((len2 & 1) != 0)
crc1 = gf2_matrix_times(even, crc1);
len2 >>= 1;
/* if no more bits set, then done */
if (len2 == 0)
break;
/* another iteration of the loop with odd and even swapped */
gf2_matrix_square(odd, even);
if ((len2 & 1) != 0)
crc1 = gf2_matrix_times(odd, crc1);
len2 >>= 1;
/* if no more bits set, then done */
} while (len2 != 0);
/* return combined crc */
crc1 ^= crc2;
return crc1;
}
public static unsafe uint Substract(uint crc1, uint crc2, long len2)
{
/* degenerate case */
if (len2 == 0)
return crc1;
if (len2 < 0)
throw new ArgumentException("crc.Combine length cannot be negative", "len2");
crc1 ^= crc2;
fixed (uint* st = &substractTable[0, 0])
{
int n = 3;
do
{
/* apply zeros operator for this bit of len2 */
if ((len2 & 1) != 0)
crc1 = gf2_matrix_times(st + 32 * n, crc1);
len2 >>= 1;
n = (n + 1) & (GF2_DIM - 1);
/* if no more bits set, then done */
} while (len2 != 0);
}
/* return combined crc */
return crc1;
}
}
}