0

I want to Apply 64 XOR operation of two byte array. Is this right approach to do with using unsafe

I have tried below approach without using unsafe. but i want little faster than this

for (int i=0; i< oldBlock.Length;i++)
{
{
 oldblock[i] ^= (newblock[i]);
}

Below XOR operation miss last bytes as below code XOR 8 bytes each time.

How to accomplish this.

static void Main(string[] args)
        {


            byte[] a = new byte[10];
            byte[] b = new byte[10];
            Random r = new Random();
            r.NextBytes(a);
            a.CopyTo(b, 0);
            XOr64(a, b);
            foreach (byte c in a)
            {
                Console.WriteLine(c);
            }


            Console.ReadKey();



        }    

public static unsafe void XOr64(byte[] oldBlock, byte[] newblock)
                {
                    try
                    {
                        fixed (byte* byteA = oldBlock)
                        fixed (byte* byteB = newblock)
                        {
                            long* ppA = (long*)byteA;
                            long* ppB = (long*)byteB;

                            for (int p = 0; p < oldBlock.Length/8; p++)
                            {
                                *ppA ^= *ppB;

                                ppA++;
                                ppB++;
                            }
                        }
                    }
                    catch
                    {

                    }



                }
4
  • 3
    Your byte array is byte[10]. For 64 byte operation the size must be a multiple of 8. Commented May 9, 2018 at 9:24
  • This kind of code will take <10ms for millions of iterations. How in the world could there be a performance issue here. Commented May 9, 2018 at 9:33
  • "but i want little faster than this" - could you give us some idea of what your performance requirements are, and what performance the working code provides (in release mode)? You could just modify your code to perform a byte-by-byte XOR for the final bytes (up to 7 of them) but this would really have to be a performance bottleneck to be worth doing, I think. (I'd also strongly advise against an empty catch block like you've got now.) Commented May 9, 2018 at 9:36
  • @FCin Yes you are right but i am doing XOr on two Image Array of new byte [1366 * 768 * 4]. Unsafe in this case little faster. as Its live screen sharing app I am just wondering is this possible Commented May 9, 2018 at 9:37

2 Answers 2

2

If the 8-byte-at-a-time aspect is working well for you and you're sure you need the extra performance, you can just extend that method to cover the remaining bytes individually - which will be at most 7 bytes:

public static unsafe void XOr64(byte[] oldBlock, byte[] newBlock)
{
    // First XOR as many 64-bit blocks as possible, for the sake of speed
    fixed (byte* byteA = oldBlock)
    fixed (byte* byteB = newBlock)
    {
        long* ppA = (long*) byteA;
        long* ppB = (long*) byteB;

        int chunks = oldBlock.Length / 8;
        for (int p = 0; p < chunks; p++)
        {
            *ppA ^= *ppB;

            ppA++;
            ppB++;
        }
    }

    // Now cover any remaining bytes one byte at a time. We've
    // already handled chunks * 8 bytes, so start there.
    for (int index = chunks * 8; index < oldBlock.Length; index++)
    {
        oldBlock[index] ^= newBlock[index];
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

Here is @Jon Skeet algorithm implemented using Span<> instead of unsafe code:

public static void Xor64(Span<byte> bytes, ReadOnlySpan<byte> mask) {
    int chunks = mask.Length / 8;
    int chunksBounds = chunks * 8;
    Xor64(MemoryMarshal.Cast<byte, long>(bytes[..chunksBounds]), MemoryMarshal.Cast<byte, long>(mask[..chunksBounds]));
    for (int i = chunksBounds;i < mask.Length;i++) {
        bytes[i] ^= mask[i];
    }
}
public static void Xor64(Span<long> longs, ReadOnlySpan<long> mask) {
    for (int i = 0;i < longs.Length;i++) {
        longs[i] ^= mask[i];
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.