Results 1 to 6 of 6

Thread: copy WORD to BYTE array

  1. #1

    Thread Starter
    Lively Member Agilaz's Avatar
    Join Date
    Jun 2006
    Posts
    98

    copy WORD to BYTE array

    Hi, i need the fastest way to copy the lowbytes of large WORD arrays into BYTE arrays for an exported function in a dll.

    this is what i have so far...

    Code:
    void  __stdcall CopyIntArray(unsigned char * ucDest, unsigned short * usSource, unsigned int * uiLen)
    {
    
    	__asm
    	{
    
    		mov esi, usSource
    		mov edi, ucDest
    		mov eax, uiLen
    		mov eax, [eax]
    		mov ebx, eax
    		shr eax, 2
    		jz SHORT lendDW
    lloopDW:	
    		mov ecx, [esi+4]
    		mov dl, cl
    		shr ecx, 8
    		mov dh, ch
    		shl edx, 16
    		mov ecx, [esi]
    		mov dl, cl
    		shr ecx, 8
    		mov dh, ch
    		mov [edi], edx
    		add esi, 8
    		add edi, 4
    		dec eax
    		jnz SHORT lloopDW
    lendDW:
    		and ebx, 3
    		jz SHORT lend
    lloopB:
    		mov cx, [esi]
    		mov [edi+eax], cl
    		inc eax
    		add esi, 2
    		cmp ebx, eax
    		jnz Short lloopB
    lend:
    
    	}
    
    }
    the code works fine, but is there a faster way of doing this?

  2. #2
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: copy WORD to BYTE array

    Hmmm - pseudo-code, but:

    point at the start of the word array
    point at the start of the byte array
    load a byte from the word array
    store the byte to the byte array
    inc the byte pointer
    inc the word pointer by 2
    loop

    (If the word array is in low-byte-at-high-address order, initially point at the word array + 1)
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  3. #3

    Thread Starter
    Lively Member Agilaz's Avatar
    Join Date
    Jun 2006
    Posts
    98

    Re: copy WORD to BYTE array

    that's that's the first thing i tried and what the second loop does. but it's slower. the reason is most likely that you have more memory read/write operations and more loop iterations that way. i already optimized the code to read 2 dwords from the word array and write one dword to the byte array inside the first loop. the only way to make it faster seems to be to reduce the slow memory read/write operations, but i have no idea how to reduce them even more.

  4. #4
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: copy WORD to BYTE array

    The only thing faster than memory r/w is register r/w. Get a processor with a few gigs of register space.

    Seriously, other than a register push being faster than a store, I can't think of anything.
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  5. #5

    Thread Starter
    Lively Member Agilaz's Avatar
    Join Date
    Jun 2006
    Posts
    98

    Re: copy WORD to BYTE array

    a few gigs of register space where can i oder that processor?

    i guess i can live with this code if this is already the fastet way.

    thanks Al42

  6. #6
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: copy WORD to BYTE array

    you could try to implement an old tric - I'll leave the coding to you, but you could save the SP, point it to the top of the destination, point another register to the top of the source, pop the register, transfer the low byte to the SP, pop the register, transfer the low byte to the high byte of the SP, push the SP, etc. Saves having to inc one register.
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width