
%ifdef win32
%define FUNC(x) _ %+ x
%else
%define FUNC(x) x
%endif


segment _TEXT para public use32 class=CODE


global FUNC(sse2_sort_16)
FUNC(sse2_sort_16):

  push 	edx

  mov 	edx, DWORD [8+esp]

  movdqu xmm1, [edx]
  movdqu xmm2, [edx+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

  
  movdqa xmm0, xmm2
  pminsw xmm0, xmm1
  pmaxsw xmm2, xmm1
  movdqa xmm1, xmm0
  
  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

  


  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 [edx], xmm1
  movdqu [edx+16], xmm2


  pop 	ebx
  ret

