From 16d3472fc67e2b8e7e69273ce93ecc52f170fbeb Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Thu, 7 Nov 2024 02:29:51 -0500 Subject: [PATCH] Consolidate cases in fast transform --- SabreTools.Hashing/Crc/CrcTable.cs | 49 +++++++++++------------------- 1 file changed, 18 insertions(+), 31 deletions(-) diff --git a/SabreTools.Hashing/Crc/CrcTable.cs b/SabreTools.Hashing/Crc/CrcTable.cs index 6d7a77c..e3a946f 100644 --- a/SabreTools.Hashing/Crc/CrcTable.cs +++ b/SabreTools.Hashing/Crc/CrcTable.cs @@ -168,25 +168,21 @@ namespace SabreTools.Hashing.Crc if (_processBitwise) return false; - // Check for optimizable transformations - if (_definition.Width == 16 && _definition.ReflectIn) - { - TransformBlockFastReflect(ref hash, data, offset, length); - return true; - } - if (_definition.Width == 32 && _definition.ReflectIn) + // Only certain widths can be optimized + if (System.Array.IndexOf([16, 32, 64], _definition.Width) == -1) + return false; + + // All reflection-in implementations share an optimized path + if (_definition.ReflectIn) { TransformBlockFastReflect(ref hash, data, offset, length); return true; } + + // CRC-32 with no reflection-in has can be optimized if (_definition.Width == 32 && !_definition.ReflectIn) { - TransformBlockFast32NoReflect(ref hash, data, offset, length); - return true; - } - if (_definition.Width == 64 && _definition.ReflectIn) - { - TransformBlockFastReflect(ref hash, data, offset, length); + TransformBlockFastNoReflect(ref hash, data, offset, length); return true; } @@ -237,16 +233,16 @@ namespace SabreTools.Hashing.Crc /// /// Optimized transformation for 32-bit CRC with no reflection /// - private void TransformBlockFast32NoReflect(ref ulong hash, byte[] data, int offset, int length) + private void TransformBlockFastNoReflect(ref ulong hash, byte[] data, int offset, int length) { // Process on a copy of the hash ulong local = hash; // Process aligned data - if (length > 8) + if (length > 4) { - long end = offset + (length & ~(uint)7); - length &= 7; + long end = offset + (length & ~(uint)3); + length &= 3; while (offset < end) { @@ -255,21 +251,12 @@ namespace SabreTools.Hashing.Crc + (data[offset + 2] << 8 ) + (data[offset + 1] << 16) + (data[offset + 0] << 24)); - ulong high = (uint)( - + (data[offset + 7] << 32) - + (data[offset + 6] << 40) - + (data[offset + 5] << 48) - + (data[offset + 4] << 56)); - offset += 8; + offset += 4; - local = _table[4, (byte)(low )] - ^ _table[5, (byte)(low >> 8 )] - ^ _table[6, (byte)(low >> 16 )] - ^ _table[7, (byte)(low >> 24 )] - ^ _table[0, (byte)(high )] - ^ _table[1, (byte)(high >> 8 )] - ^ _table[2, (byte)(high >> 16)] - ^ _table[3, (byte)(high >> 24)]; + local = _table[0, (byte)(low )] + ^ _table[1, (byte)(low >> 8 )] + ^ _table[2, (byte)(low >> 16)] + ^ _table[3, (byte)(low >> 24)]; } }