
%ifdef win32
%define FUNC(x) _ %+ x
%else
%define FUNC(x) x
%endif


segment _TEXT para public use32 class=CODE

%macro EVEN 0
  movdqa xmm0, xmm2
  pminsw xmm0, xmm1
  pmaxsw xmm2, xmm1
  movdqa xmm1, xmm0
%endmacro

%macro ODD 0
  pextrw eax,  xmm2, 7
  pextrw ebx,  xmm1, 0
  pslldq xmm2, 2
  pminsw xmm0, xmm2
  pmaxsw xmm1, xmm2
  movdqa xmm2, xmm0
  psrldq xmm2, 2
  pinsrw xmm2, eax, 7
  pinsrw xmm1, ebx, 0
%endmacro

%macro ODD1 0
  pslldq xmm2, 2
  pminsw xmm0, xmm2
  pmaxsw xmm1, xmm2
  movdqa xmm2, xmm0
  psrldq xmm2, 2
%endmacro




global FUNC(sse2_sort_16)
FUNC(sse2_sort_16):

  push 	ebx
  push 	ecx

  mov 	ecx, DWORD [12+esp]

  movdqu xmm1, [ecx]
  movdqu xmm2, [ecx+16]

  pshuflw xmm1,xmm1,0xd8
  pshufhw xmm1,xmm1,0xd8
  pshufd  xmm1,xmm1,0xd8
  pshuflw xmm2,xmm2,0xd8
  pshufhw xmm2,xmm2,0xd8
  pshufd  xmm2,xmm2,0xd8
  movaps  xmm0,xmm1
  movlhps xmm1,xmm2
  movhlps xmm2,xmm0

%rep 16
   EVEN
   ODD1
%endrep

  movaps  xmm0,xmm1
  movlhps xmm1,xmm2
  movhlps xmm2,xmm0
  pshufd  xmm1,xmm1,0xd8
  pshufhw xmm1,xmm1,0xd8
  pshuflw xmm1,xmm1,0xd8
  pshufd  xmm2,xmm2,0xd8
  pshufhw xmm2,xmm2,0xd8
  pshuflw xmm2,xmm2,0xd8

  movdqu [ecx], xmm1
  movdqu [ecx+16], xmm2

  pop 	ecx
  pop 	ebx
  ret

