Solved

Perform XOR operation on two binary blocks of memory ?

Posted on 2002-06-16
10
762 Views
Last Modified: 2010-05-18
Hello Experts,

How to perform a XOR operation on two binary blocks of memory?

Thanks!

P.S: The two memory blocks have the same size(about 2mb) but the data inside is different
0
Comment
Question by:navy_seal
  • 4
  • 2
  • 2
  • +2
10 Comments
 
LVL 11

Assisted Solution

by:robert_marquardt
robert_marquardt earned 147 total points
ID: 7083037
As simple as a for loop.

for I := 0 to SizeOf(Buffer1) do
  Buffer1[I] := Buffer1[I] xor Buffer2[I];

This may be a bit inefficient if the buffer elements are Chars.
Try using Cardinals as buffer elements.
0
 
LVL 7

Accepted Solution

by:
Cynna earned 148 total points
ID: 7083308
navy_seal,

I dig up somebodys old code. It's quite fast:

procedure MemBlockXOR(const Source1, Source2, Destination: Pointer;
                            Blocksize: Integer); assembler;
asm
  push edi
  push esi
  push ebx
  mov esi,eax
  mov edi,ecx
  mov ecx,Blocksize;
  mov eax,[esi]
  and eax,[edx]
  mov [edi],eax
  mov eax,[esi+4]
  and eax,[edx+4]
  mov [edi+4],eax
  add esi,ecx
  add edx,ecx
  add edi,ecx
  shr ecx,3
  test ecx,ecx
  jz @@ending
  neg ecx
@@doit:
  mov eax,[esi+ecx*8]
  mov ebx,[esi+ecx*8+4]
  xor eax,[edx+ecx*8]
  xor ebx,[edx+ecx*8+4]
  mov [edi+ecx*8],eax
  mov [edi+ecx*8+4],ebx
  inc ecx
  jnz @@doit
@@ending:
  pop ebx
  pop esi
  pop edi
end;


I tested it with:

procedure TForm1.Button1Click(Sender: TObject);
var Block1, Block2, Block3: Pointer;
    Size: Integer;
    t0: DWord;
begin
  try
     // Must be multiple of 4-bytes:    
     Size:=16*1024*1024; //16Mb
     GetMem(Block1, Size);
     GetMem(Block2, Size);
     GetMem(Block3, Size);
     FillMemory(Block1, Size, 2);
     FillMemory(Block2, Size, 3);
     t0:=GetTickCount;
       MemBlockXOR(Block1, Block2, Block3, Size);
     Button1.Caption:=IntToStr(GetTickCount-t0)+' ms';
  finally
     FreeMem(Block1);
     FreeMem(Block2);
     FreeMem(Block3);
  end;
end;

On my Athlon XP 1.4Ghz it XORed those 16Mb in 0.13 sec.

0
 
LVL 11

Expert Comment

by:robert_marquardt
ID: 7083333
I would guess that Delphi generates similar code.
The assembler code does a loop unroll to handle 2 DWORDs at once. That makes it "a multiple of 8-bytes".
The speed factor against my loop should be around 2x.
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 7

Expert Comment

by:Cynna
ID: 7083336
navy_seal,

I dig up somebodys old code. It's quite fast:

procedure MemBlockXOR(const Source1, Source2, Destination: Pointer;
                            Blocksize: Integer); assembler;
asm
  push edi
  push esi
  push ebx
  mov esi,eax
  mov edi,ecx
  mov ecx,Blocksize;
  mov eax,[esi]
  and eax,[edx]
  mov [edi],eax
  mov eax,[esi+4]
  and eax,[edx+4]
  mov [edi+4],eax
  add esi,ecx
  add edx,ecx
  add edi,ecx
  shr ecx,3
  test ecx,ecx
  jz @@ending
  neg ecx
@@doit:
  mov eax,[esi+ecx*8]
  mov ebx,[esi+ecx*8+4]
  xor eax,[edx+ecx*8]
  xor ebx,[edx+ecx*8+4]
  mov [edi+ecx*8],eax
  mov [edi+ecx*8+4],ebx
  inc ecx
  jnz @@doit
@@ending:
  pop ebx
  pop esi
  pop edi
end;


I tested it with:

procedure TForm1.Button1Click(Sender: TObject);
var Block1, Block2, Block3: Pointer;
    Size: Integer;
    t0: DWord;
begin
  try
     // Must be multiple of 4-bytes:    
     Size:=16*1024*1024; //16Mb
     GetMem(Block1, Size);
     GetMem(Block2, Size);
     GetMem(Block3, Size);
     FillMemory(Block1, Size, 2);
     FillMemory(Block2, Size, 3);
     t0:=GetTickCount;
       MemBlockXOR(Block1, Block2, Block3, Size);
     Button1.Caption:=IntToStr(GetTickCount-t0)+' ms';
  finally
     FreeMem(Block1);
     FreeMem(Block2);
     FreeMem(Block3);
  end;
end;

On my Athlon XP 1.4Ghz it XORed those 16Mb in 0.13 sec.

0
 
LVL 7

Expert Comment

by:Cynna
ID: 7083340
OOPS,
Sorry for double post.

robert_marquardt, yes it is.
0
 

Author Comment

by:navy_seal
ID: 7084020
Thanks guys,ill try it out, which solution is faster will get the points :-)
0
 

Author Comment

by:navy_seal
ID: 7084913
sorry Cynna ,but I found the same code that you gave me 2 days a go, so I would give points only If you could give code that is faster

here is where I found it:

http://groups.google.com/groups?hl=en&lr=&frame=right&th=a0343ed619e02a7a&seekm=7nbvmi%24qtf1%40forums.borland.com#s

they are a bit different
0
 
LVL 7

Expert Comment

by:Cynna
ID: 7086284
navy_seal,

Yes, its quite possible I saved it from Google some time ago. Anyway, as far as I can see, this code couldn't be turned up much faster, so you can count me out of the race...
0
 

Expert Comment

by:CleanupPing
ID: 9343194
navy_seal:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 5

Expert Comment

by:Lukasz Lach
ID: 9453405
navy_seal,
No comment has been added lately (17 days), so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area for this question:

RECOMMENDATION: split points between robert_marquardt http:#7083333 and Cynna http:#7083336

Please leave any comments here within 7 days.

-- Please DO NOT accept this comment as an answer ! --

Thanks,

anAKiN
EE Cleanup Volunteer
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question