Use `ReadOnlySpan` for `SpecialCRC32.Add`

This commit is contained in:
YoshiRulz 2021-10-04 10:15:04 +10:00
parent de76127f76
commit 71be5c7997
No known key found for this signature in database
GPG Key ID: C4DE31C245353FB7
2 changed files with 7 additions and 20 deletions

View File

@ -36,7 +36,7 @@ namespace BizHawk.Emulation.DiscSystem
//hash the TOC
static void AddAsBytesTo(SpecialCRC32 crc32, int i)
=> crc32.Add(BitConverter.GetBytes(i), 0, 4);
=> crc32.Add(BitConverter.GetBytes(i));
AddAsBytesTo(crc, (int)disc.TOC.Session1Format);
AddAsBytesTo(crc, disc.TOC.FirstRecordedTrackNumber);
AddAsBytesTo(crc, disc.TOC.LastRecordedTrackNumber);
@ -52,7 +52,7 @@ namespace BizHawk.Emulation.DiscSystem
for (int i = 0; i < 26; i++)
{
dsr.ReadLBA_2352(i, buffer2352, 0);
crc.Add(buffer2352, 0, 2352);
crc.Add(buffer2352);
}
return crc.Result;
@ -77,7 +77,7 @@ namespace BizHawk.Emulation.DiscSystem
for (int i = 0; i < disc.Session1.LeadoutLBA; i++)
{
dsr.ReadLBA_2352(i, buffer2352, 0);
crc.Add(buffer2352, 0, 2352);
crc.Add(buffer2352);
}
return crc.Result;
@ -131,22 +131,9 @@ namespace BizHawk.Emulation.DiscSystem
private uint current = 0xFFFFFFFF;
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="offset"/> is negative, or
/// end index (<paramref name="offset"/> + <paramref name="size"/>) is beyond the end of <paramref name="data"/>
/// </exception>
public unsafe void Add(byte[] data, int offset, int size)
public void Add(ReadOnlySpan<byte> data)
{
if (offset + size > data.Length)
throw new ArgumentOutOfRangeException();
if (offset < 0)
throw new ArgumentOutOfRangeException();
fixed (byte* pData = data)
for (int i = 0; i < size; i++)
{
byte b = pData[offset + i];
current = CRC32Table[(current ^ b) & 0xFF] ^ (current >> 8);
}
foreach (var b in data) current = CRC32Table[(current ^ b) & 0xFF] ^ (current >> 8);
}
/// <summary>

View File

@ -35,12 +35,12 @@ namespace BizHawk.Tests.Common.checksums
data = InitialiseArray();
DiscHasher.SpecialCRC32 crc32 = new();
crc32.Add(data, 0, data.Length);
crc32.Add(data);
Assert.AreEqual(EXPECTED, crc32.Result);
var dataExtra = InitialiseArrayExtra();
DiscHasher.SpecialCRC32 crc32Extra = new();
crc32Extra.Add(dataExtra, 0, dataExtra.Length);
crc32Extra.Add(dataExtra);
Assert.AreEqual(EXPECTED_EXTRA, crc32Extra.Result);
crc32.Incorporate(crc32Extra.Result, dataExtra.Length);
Assert.AreEqual(EXPECTED_COMBINED, crc32.Result);