'#include once "window9.bi"

sub FastCopy(byval pTarget as any ptr, byval pSource as any ptr, byval nBytes as integer)  export
  asm
  mov   edi,dword ptr [pTarget]
  mov   esi,dword ptr [pSource]
  mov   ecx,dword ptr [nBytes]

  shr   ecx,1
  jnc   copysse_2
  movsb

copysse_2:
  shr   ecx,1
  jnc   copysse_4
  movsw

copysse_4:
  shr   ecx,1
  jnc   copysse_8
  movsd

copysse_8:
  shr   ecx,1
  jnc   copysse_16
  movq  mm0,[esi  ]
  movq  [edi],mm0
  lea   esi,[esi+8]
  lea   edi,[edi+8]

copysse_16:
  shr   ecx,1
  jnc   copysse_32
  movq  mm0,[esi   ]
  movq  [edi  ],mm0
  movq  mm0,[esi+ 8]
  movq  [edi+8],mm0
  lea   esi,[esi+16]
  lea   edi,[edi+16]

copysse_32:
  shr   ecx,1
  jnc   copysse_64
  movq  mm0,[esi   ]
  movq  [edi  ],mm0
  movq  mm0,[esi+ 8]
  movq  [edi+8],mm0
  movq  mm0,[esi+16]
  movq  [edi+16],mm0
  movq  mm0,[esi+24]
  movq  [edi+24],mm0
  lea   esi,[esi+32]
  lea   edi,[edi+32]

copysse_64:
  jecxz copysse_end
  copysse_loop:
    movq   mm0,[esi+ 0]
    movntq [edi+ 0],mm0
    movq   mm1,[esi+ 8]
    movntq [edi+ 8],mm1
    movq   mm2,[esi+16]
    movntq [edi+16],mm2
    movq   mm3,[esi+24]
    movntq [edi+24],mm3
    movq   mm4,[esi+32]
    movntq [edi+32],mm4
    movq   mm5,[esi+40]
    movntq [edi+40],mm5
    movq   mm6,[esi+48]
    movntq [edi+48],mm6
    movq   mm7,[esi+56]
    movntq [edi+56],mm7
    lea    esi,[esi+64]
    lea    edi,[edi+64]
    dec ecx
  jnz copysse_loop

  copysse_end:
  emms
  end asm
end sub
