-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArrayCompare.cs
38 lines (37 loc) · 1.42 KB
/
ArrayCompare.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
namespace FolderCompare
{
/// <summary>
/// Based on: https://stackoverflow.com/questions/43289/comparing-two-byte-arrays-in-net
/// </summary>
static class ArrayCompare
{
public static unsafe bool CompareArraysUnsafe(byte* bytePtr1, byte* bytePtr2, int length)
{
byte* lastAddr = bytePtr1 + length;
byte* lastAddrMinus32 = lastAddr - 32;
while (bytePtr1 < lastAddrMinus32) // unroll the loop so that we are comparing 32 bytes at a time.
{
if (*(ulong*)bytePtr1 != *(ulong*)bytePtr2) return false;
if (*(ulong*)(bytePtr1 + 8) != *(ulong*)(bytePtr2 + 8)) return false;
if (*(ulong*)(bytePtr1 + 16) != *(ulong*)(bytePtr2 + 16)) return false;
if (*(ulong*)(bytePtr1 + 24) != *(ulong*)(bytePtr2 + 24)) return false;
bytePtr1 += 32;
bytePtr2 += 32;
}
while (bytePtr1 < lastAddr)
{
if (*bytePtr1 != *bytePtr2) return false;
bytePtr1++;
bytePtr2++;
}
return true;
}
public static unsafe bool CompareArraysUnsafe(byte[] array1, byte[] array2, int length)
{
fixed (byte* bytePtr1 = array1) fixed (byte* bytePtr2 = array2)
{
return CompareArraysUnsafe(bytePtr1, bytePtr2, length);
}
}
}
}