Faster crc16 ccitt

insert lenny face here
>4x faster, relative speed is higher for larger input arrays
This commit is contained in:
Kurt 2017-09-26 23:10:31 -07:00
parent 9699cdefae
commit 8b02f05bd5

View File

@ -608,24 +608,17 @@ public static bool GetSavesFromFolder(string folderPath, bool deep, out IEnumera
/// <returns>Checksum</returns>
public static ushort CRC16_CCITT(byte[] data, int start, int length)
{
const ushort init = 0xFFFF;
const ushort poly = 0x1021;
ushort crc = init;
byte top = 0xFF;
byte bot = 0xFF;
int end = start + length;
for (int i = start; i < end; i++)
{
byte b = data[i];
crc ^= (ushort)(b << 8);
for (int j = 0; j < 8; j++)
{
bool flag = (crc & 0x8000) > 0;
crc <<= 1;
if (flag)
crc ^= poly;
}
var x = data[i] ^ top;
x ^= (x >> 4);
top = (byte) (bot ^ (x >> 3) ^ (x << 4));
bot = (byte) (x ^ (x << 5));
}
return crc;
return (ushort)(top << 8 | bot);
}
/// <summary>Calculates the CRC16-CCITT checksum over an input byte array.</summary>