mirror of
https://github.com/claunia/cuetools.net.git
synced 2025-12-16 18:14:25 +00:00
More code cleanup after decoding optimization
This commit is contained in:
@@ -1,42 +1,159 @@
|
||||
using System;
|
||||
namespace CUETools.Codecs
|
||||
{
|
||||
public class Crc16
|
||||
public static class Crc16
|
||||
{
|
||||
ushort[] table = new ushort[256];
|
||||
const int GF2_DIM = 16;
|
||||
public static ushort[] table = new ushort[256];
|
||||
private static readonly ushort[,] combineTable = new ushort[GF2_DIM, GF2_DIM];
|
||||
private static readonly ushort[,] substractTable = new ushort[GF2_DIM, GF2_DIM];
|
||||
|
||||
public unsafe ushort ComputeChecksum(byte[] bytes, int pos, int count)
|
||||
public static unsafe ushort ComputeChecksum(ushort crc, byte[] bytes, int pos, int count)
|
||||
{
|
||||
fixed (byte* bs = bytes)
|
||||
return ComputeChecksum(bs + pos, count);
|
||||
return ComputeChecksum(crc, bs + pos, count);
|
||||
}
|
||||
|
||||
public unsafe ushort ComputeChecksum(byte* bytes, int count)
|
||||
public static unsafe ushort ComputeChecksum(ushort crc, byte* bytes, int count)
|
||||
{
|
||||
ushort crc = 0;
|
||||
fixed (ushort* t = table)
|
||||
for (int i = count; i > 0; i--)
|
||||
crc = (ushort)((crc << 8) ^ t[(crc >> 8) ^ *(bytes++)]);
|
||||
return crc;
|
||||
}
|
||||
|
||||
public Crc16()
|
||||
const ushort polynomial = 0x8005;
|
||||
const ushort reversePolynomial = 0xa001;
|
||||
const ushort reversePolynomial2 = 0x4003;
|
||||
|
||||
static unsafe Crc16()
|
||||
{
|
||||
int bits = 16;
|
||||
int poly16 = 0x8005;
|
||||
int poly = (poly16 + (1 << bits));
|
||||
for (ushort i = 0; i < table.Length; i++)
|
||||
{
|
||||
int crc = i;
|
||||
for (int j = 0; j < bits; j++)
|
||||
{
|
||||
if ((crc & (1U << (bits - 1))) != 0)
|
||||
crc = ((crc << 1) ^ poly);
|
||||
else
|
||||
crc <<= 1;
|
||||
}
|
||||
//table[i] = (crc & ((1<<bits)-1));
|
||||
table[i] = (ushort)(crc & 0xffff);
|
||||
}
|
||||
}
|
||||
}
|
||||
int poly = (polynomial + (1 << GF2_DIM));
|
||||
for (ushort i = 0; i < table.Length; i++)
|
||||
{
|
||||
int crc = i;
|
||||
for (int j = 0; j < GF2_DIM; j++)
|
||||
{
|
||||
if ((crc & (1U << (GF2_DIM - 1))) != 0)
|
||||
crc = ((crc << 1) ^ poly);
|
||||
else
|
||||
crc <<= 1;
|
||||
}
|
||||
table[i] = (ushort)(crc & ((1 << GF2_DIM) - 1));
|
||||
}
|
||||
|
||||
combineTable[0, 0] = reversePolynomial;
|
||||
substractTable[0, GF2_DIM - 1] = reversePolynomial2;
|
||||
for (int n = 1; n < GF2_DIM; n++)
|
||||
{
|
||||
combineTable[0, n] = (ushort)(1 << (n - 1));
|
||||
substractTable[0, n - 1] = (ushort)(1 << n);
|
||||
}
|
||||
|
||||
fixed (ushort* 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 * GF2_DIM, ct + (i - 1) * GF2_DIM);
|
||||
gf2_matrix_square(st + i * GF2_DIM, st + (i - 1) * GF2_DIM);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static unsafe ushort gf2_matrix_times(ushort* mat, ushort uvec)
|
||||
{
|
||||
int vec = ((int) uvec) << 16;
|
||||
return (ushort)(
|
||||
(*(mat++) & ((vec << 15) >> 31)) ^
|
||||
(*(mat++) & ((vec << 14) >> 31)) ^
|
||||
(*(mat++) & ((vec << 13) >> 31)) ^
|
||||
(*(mat++) & ((vec << 12) >> 31)) ^
|
||||
(*(mat++) & ((vec << 11) >> 31)) ^
|
||||
(*(mat++) & ((vec << 10) >> 31)) ^
|
||||
(*(mat++) & ((vec << 09) >> 31)) ^
|
||||
(*(mat++) & ((vec << 08) >> 31)) ^
|
||||
(*(mat++) & ((vec << 07) >> 31)) ^
|
||||
(*(mat++) & ((vec << 06) >> 31)) ^
|
||||
(*(mat++) & ((vec << 05) >> 31)) ^
|
||||
(*(mat++) & ((vec << 04) >> 31)) ^
|
||||
(*(mat++) & ((vec << 03) >> 31)) ^
|
||||
(*(mat++) & ((vec << 02) >> 31)) ^
|
||||
(*(mat++) & ((vec << 01) >> 31)) ^
|
||||
(*(mat++) & (vec >> 31)));
|
||||
}
|
||||
|
||||
private static unsafe void gf2_matrix_square(ushort* square, ushort* mat)
|
||||
{
|
||||
for (int n = 0; n < GF2_DIM; n++)
|
||||
square[n] = gf2_matrix_times(mat, mat[n]);
|
||||
}
|
||||
|
||||
public static unsafe ushort Combine(ushort crc1, ushort crc2, long len2)
|
||||
{
|
||||
crc1 = (ushort)Crc32.Reflect(crc1, 16);
|
||||
crc2 = (ushort)Crc32.Reflect(crc2, 16);
|
||||
|
||||
/* 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");
|
||||
|
||||
fixed (ushort* 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 + GF2_DIM * n, crc1);
|
||||
len2 >>= 1;
|
||||
n = (n + 1) & (GF2_DIM - 1);
|
||||
/* if no more bits set, then done */
|
||||
} while (len2 != 0);
|
||||
}
|
||||
|
||||
/* return combined crc */
|
||||
crc1 ^= crc2;
|
||||
crc1 = (ushort)Crc32.Reflect(crc1, 16);
|
||||
return crc1;
|
||||
}
|
||||
|
||||
public static unsafe ushort Substract(ushort crc1, ushort crc2, long len2)
|
||||
{
|
||||
crc1 = (ushort)Crc32.Reflect(crc1, 16);
|
||||
crc2 = (ushort)Crc32.Reflect(crc2, 16);
|
||||
/* degenerate case */
|
||||
if (len2 == 0)
|
||||
return crc1;
|
||||
if (len2 < 0)
|
||||
throw new ArgumentException("crc.Combine length cannot be negative", "len2");
|
||||
|
||||
crc1 ^= crc2;
|
||||
|
||||
fixed (ushort* 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 + GF2_DIM * n, crc1);
|
||||
len2 >>= 1;
|
||||
n = (n + 1) & (GF2_DIM - 1);
|
||||
/* if no more bits set, then done */
|
||||
} while (len2 != 0);
|
||||
}
|
||||
|
||||
/* return combined crc */
|
||||
crc1 = (ushort)Crc32.Reflect(crc1, 16);
|
||||
return crc1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,7 +42,7 @@ namespace CUETools.Codecs
|
||||
return crc;
|
||||
}
|
||||
|
||||
static uint Reflect(uint val, int ch)
|
||||
internal static uint Reflect(uint val, int ch)
|
||||
{
|
||||
uint value = 0;
|
||||
// Swap bit 0 for bit 7
|
||||
@@ -56,7 +56,9 @@ namespace CUETools.Codecs
|
||||
return value;
|
||||
}
|
||||
|
||||
const uint ulPolynomial = 0x04c11db7;
|
||||
const uint uPolynomial = 0x04c11db7;
|
||||
const uint uReversePolynomial = 0xedb88320;
|
||||
const uint uReversePolynomial2 = 0xdb710641;
|
||||
|
||||
private static readonly uint[,] combineTable;
|
||||
private static readonly uint[,] substractTable;
|
||||
@@ -122,13 +124,13 @@ namespace CUETools.Codecs
|
||||
{
|
||||
table[i] = Reflect(i, 8) << 24;
|
||||
for (int j = 0; j < 8; j++)
|
||||
table[i] = (table[i] << 1) ^ ((table[i] & (1U << 31)) == 0 ? 0 : ulPolynomial);
|
||||
table[i] = (table[i] << 1) ^ ((table[i] & (1U << 31)) == 0 ? 0 : uPolynomial);
|
||||
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;
|
||||
combineTable[0, 0] = uReversePolynomial;
|
||||
substractTable[0, 31] = uReversePolynomial2;
|
||||
for (int n = 1; n < GF2_DIM; n++)
|
||||
{
|
||||
combineTable[0, n] = 1U << (n - 1);
|
||||
@@ -151,41 +153,44 @@ namespace CUETools.Codecs
|
||||
const int GF2_DIM = 32;
|
||||
//const int GF2_DIM2 = 67;
|
||||
|
||||
private static unsafe uint gf2_matrix_times(uint* mat, uint vec)
|
||||
{
|
||||
return *(mat++) * (vec & 1) ^
|
||||
*(mat++) * ((vec >>= 1) & 1) ^
|
||||
*(mat++) * ((vec >>= 1) & 1) ^
|
||||
*(mat++) * ((vec >>= 1) & 1) ^
|
||||
*(mat++) * ((vec >>= 1) & 1) ^
|
||||
*(mat++) * ((vec >>= 1) & 1) ^
|
||||
*(mat++) * ((vec >>= 1) & 1) ^
|
||||
*(mat++) * ((vec >>= 1) & 1) ^
|
||||
*(mat++) * ((vec >>= 1) & 1) ^
|
||||
*(mat++) * ((vec >>= 1) & 1) ^
|
||||
*(mat++) * ((vec >>= 1) & 1) ^
|
||||
*(mat++) * ((vec >>= 1) & 1) ^
|
||||
*(mat++) * ((vec >>= 1) & 1) ^
|
||||
*(mat++) * ((vec >>= 1) & 1) ^
|
||||
*(mat++) * ((vec >>= 1) & 1) ^
|
||||
*(mat++) * ((vec >>= 1) & 1) ^
|
||||
*(mat++) * ((vec >>= 1) & 1) ^
|
||||
*(mat++) * ((vec >>= 1) & 1) ^
|
||||
*(mat++) * ((vec >>= 1) & 1) ^
|
||||
*(mat++) * ((vec >>= 1) & 1) ^
|
||||
*(mat++) * ((vec >>= 1) & 1) ^
|
||||
*(mat++) * ((vec >>= 1) & 1) ^
|
||||
*(mat++) * ((vec >>= 1) & 1) ^
|
||||
*(mat++) * ((vec >>= 1) & 1) ^
|
||||
*(mat++) * ((vec >>= 1) & 1) ^
|
||||
*(mat++) * ((vec >>= 1) & 1) ^
|
||||
*(mat++) * ((vec >>= 1) & 1) ^
|
||||
*(mat++) * ((vec >>= 1) & 1) ^
|
||||
*(mat++) * ((vec >>= 1) & 1) ^
|
||||
*(mat++) * ((vec >>= 1) & 1) ^
|
||||
*(mat++) * ((vec >>= 1) & 1) ^
|
||||
*(mat++) * ((vec >>= 1) & 1);
|
||||
}
|
||||
private static unsafe uint gf2_matrix_times(uint* umat, uint uvec)
|
||||
{
|
||||
int vec = (int)uvec;
|
||||
int* mat = (int*)umat;
|
||||
return (uint)(
|
||||
(*(mat++) & ((vec << 31) >> 31)) ^
|
||||
(*(mat++) & ((vec << 30) >> 31)) ^
|
||||
(*(mat++) & ((vec << 29) >> 31)) ^
|
||||
(*(mat++) & ((vec << 28) >> 31)) ^
|
||||
(*(mat++) & ((vec << 27) >> 31)) ^
|
||||
(*(mat++) & ((vec << 26) >> 31)) ^
|
||||
(*(mat++) & ((vec << 25) >> 31)) ^
|
||||
(*(mat++) & ((vec << 24) >> 31)) ^
|
||||
(*(mat++) & ((vec << 23) >> 31)) ^
|
||||
(*(mat++) & ((vec << 22) >> 31)) ^
|
||||
(*(mat++) & ((vec << 21) >> 31)) ^
|
||||
(*(mat++) & ((vec << 20) >> 31)) ^
|
||||
(*(mat++) & ((vec << 19) >> 31)) ^
|
||||
(*(mat++) & ((vec << 18) >> 31)) ^
|
||||
(*(mat++) & ((vec << 17) >> 31)) ^
|
||||
(*(mat++) & ((vec << 16) >> 31)) ^
|
||||
(*(mat++) & ((vec << 15) >> 31)) ^
|
||||
(*(mat++) & ((vec << 14) >> 31)) ^
|
||||
(*(mat++) & ((vec << 13) >> 31)) ^
|
||||
(*(mat++) & ((vec << 12) >> 31)) ^
|
||||
(*(mat++) & ((vec << 11) >> 31)) ^
|
||||
(*(mat++) & ((vec << 10) >> 31)) ^
|
||||
(*(mat++) & ((vec << 09) >> 31)) ^
|
||||
(*(mat++) & ((vec << 08) >> 31)) ^
|
||||
(*(mat++) & ((vec << 07) >> 31)) ^
|
||||
(*(mat++) & ((vec << 06) >> 31)) ^
|
||||
(*(mat++) & ((vec << 05) >> 31)) ^
|
||||
(*(mat++) & ((vec << 04) >> 31)) ^
|
||||
(*(mat++) & ((vec << 03) >> 31)) ^
|
||||
(*(mat++) & ((vec << 02) >> 31)) ^
|
||||
(*(mat++) & ((vec << 01) >> 31)) ^
|
||||
(*(mat++) & (vec >> 31)));
|
||||
}
|
||||
|
||||
/* ========================================================================= */
|
||||
private static unsafe void gf2_matrix_square(uint *square, uint *mat)
|
||||
|
||||
Reference in New Issue
Block a user