From 8b02f05bd5ce1bb153dbd1dd75782613e5f36631 Mon Sep 17 00:00:00 2001 From: Kurt Date: Tue, 26 Sep 2017 23:10:31 -0700 Subject: [PATCH] Faster crc16 ccitt insert lenny face here >4x faster, relative speed is higher for larger input arrays --- PKHeX.Core/Saves/SaveUtil.cs | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/PKHeX.Core/Saves/SaveUtil.cs b/PKHeX.Core/Saves/SaveUtil.cs index a0cca1a8e..7c02f0397 100644 --- a/PKHeX.Core/Saves/SaveUtil.cs +++ b/PKHeX.Core/Saves/SaveUtil.cs @@ -608,24 +608,17 @@ public static bool GetSavesFromFolder(string folderPath, bool deep, out IEnumera /// Checksum 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); } /// Calculates the CRC16-CCITT checksum over an input byte array.