private static unsafe int ApplyMask(byte[] toMask, int toMaskOffset, int mask, int maskIndex, long count)
{
Debug.Assert(toMaskOffset <= toMask.Length - count, $"Unexpected inputs: {toMaskOffset}, {toMask.Length}, {count}");
Debug.Assert(maskIndex < sizeof(int), $"Unexpected {nameof(maskIndex)}: {maskIndex}");
byte* maskPtr = (byte*)&mask;
fixed (byte* toMaskPtr = toMask)
{
byte* p = toMaskPtr + toMaskOffset;
byte* end = p + count;
while (p < end)
{
*p++ ^= maskPtr[maskIndex];
maskIndex = (maskIndex + 1) & 3; // & 3 == faster % MaskLength
}
return maskIndex;
}
}