jquanti-sse2-64.asm 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. ;
  2. ; jquanti.asm - sample data conversion and quantization (64-bit SSE2)
  3. ;
  4. ; Copyright 2009 Pierre Ossman <[email protected]> for Cendio AB
  5. ; Copyright (C) 2009, D. R. Commander.
  6. ;
  7. ; Based on the x86 SIMD extension for IJG JPEG library
  8. ; Copyright (C) 1999-2006, MIYASAKA Masaru.
  9. ; For conditions of distribution and use, see copyright notice in jsimdext.inc
  10. ;
  11. ; This file should be assembled with NASM (Netwide Assembler),
  12. ; can *not* be assembled with Microsoft's MASM or any compatible
  13. ; assembler (including Borland's Turbo Assembler).
  14. ; NASM is available from http://nasm.sourceforge.net/ or
  15. ; http://sourceforge.net/project/showfiles.php?group_id=6208
  16. ;
  17. ; [TAB8]
  18. %include "jsimdext.inc"
  19. %include "jdct.inc"
  20. ; --------------------------------------------------------------------------
  21. SECTION SEG_TEXT
  22. BITS 64
  23. ;
  24. ; Load data into workspace, applying unsigned->signed conversion
  25. ;
  26. ; GLOBAL(void)
  27. ; jsimd_convsamp_sse2 (JSAMPARRAY sample_data, JDIMENSION start_col,
  28. ; DCTELEM *workspace);
  29. ;
  30. ; r10 = JSAMPARRAY sample_data
  31. ; r11 = JDIMENSION start_col
  32. ; r12 = DCTELEM *workspace
  33. align 16
  34. global EXTN(jsimd_convsamp_sse2)
  35. EXTN(jsimd_convsamp_sse2):
  36. push rbp
  37. mov rax,rsp
  38. mov rbp,rsp
  39. collect_args
  40. push rbx
  41. pxor xmm6,xmm6 ; xmm6=(all 0's)
  42. pcmpeqw xmm7,xmm7
  43. psllw xmm7,7 ; xmm7={0xFF80 0xFF80 0xFF80 0xFF80 ..}
  44. mov rsi, r10
  45. mov eax, r11d
  46. mov rdi, r12
  47. mov rcx, DCTSIZE/4
  48. .convloop:
  49. mov rbx, JSAMPROW [rsi+0*SIZEOF_JSAMPROW] ; (JSAMPLE *)
  50. mov rdx, JSAMPROW [rsi+1*SIZEOF_JSAMPROW] ; (JSAMPLE *)
  51. movq xmm0, XMM_MMWORD [rbx+rax*SIZEOF_JSAMPLE] ; xmm0=(01234567)
  52. movq xmm1, XMM_MMWORD [rdx+rax*SIZEOF_JSAMPLE] ; xmm1=(89ABCDEF)
  53. mov rbx, JSAMPROW [rsi+2*SIZEOF_JSAMPROW] ; (JSAMPLE *)
  54. mov rdx, JSAMPROW [rsi+3*SIZEOF_JSAMPROW] ; (JSAMPLE *)
  55. movq xmm2, XMM_MMWORD [rbx+rax*SIZEOF_JSAMPLE] ; xmm2=(GHIJKLMN)
  56. movq xmm3, XMM_MMWORD [rdx+rax*SIZEOF_JSAMPLE] ; xmm3=(OPQRSTUV)
  57. punpcklbw xmm0,xmm6 ; xmm0=(01234567)
  58. punpcklbw xmm1,xmm6 ; xmm1=(89ABCDEF)
  59. paddw xmm0,xmm7
  60. paddw xmm1,xmm7
  61. punpcklbw xmm2,xmm6 ; xmm2=(GHIJKLMN)
  62. punpcklbw xmm3,xmm6 ; xmm3=(OPQRSTUV)
  63. paddw xmm2,xmm7
  64. paddw xmm3,xmm7
  65. movdqa XMMWORD [XMMBLOCK(0,0,rdi,SIZEOF_DCTELEM)], xmm0
  66. movdqa XMMWORD [XMMBLOCK(1,0,rdi,SIZEOF_DCTELEM)], xmm1
  67. movdqa XMMWORD [XMMBLOCK(2,0,rdi,SIZEOF_DCTELEM)], xmm2
  68. movdqa XMMWORD [XMMBLOCK(3,0,rdi,SIZEOF_DCTELEM)], xmm3
  69. add rsi, byte 4*SIZEOF_JSAMPROW
  70. add rdi, byte 4*DCTSIZE*SIZEOF_DCTELEM
  71. dec rcx
  72. jnz short .convloop
  73. pop rbx
  74. uncollect_args
  75. pop rbp
  76. ret
  77. ; --------------------------------------------------------------------------
  78. ;
  79. ; Quantize/descale the coefficients, and store into coef_block
  80. ;
  81. ; This implementation is based on an algorithm described in
  82. ; "How to optimize for the Pentium family of microprocessors"
  83. ; (http://www.agner.org/assem/).
  84. ;
  85. ; GLOBAL(void)
  86. ; jsimd_quantize_sse2 (JCOEFPTR coef_block, DCTELEM *divisors,
  87. ; DCTELEM *workspace);
  88. ;
  89. %define RECIPROCAL(m,n,b) XMMBLOCK(DCTSIZE*0+(m),(n),(b),SIZEOF_DCTELEM)
  90. %define CORRECTION(m,n,b) XMMBLOCK(DCTSIZE*1+(m),(n),(b),SIZEOF_DCTELEM)
  91. %define SCALE(m,n,b) XMMBLOCK(DCTSIZE*2+(m),(n),(b),SIZEOF_DCTELEM)
  92. ; r10 = JCOEFPTR coef_block
  93. ; r11 = DCTELEM *divisors
  94. ; r12 = DCTELEM *workspace
  95. align 16
  96. global EXTN(jsimd_quantize_sse2)
  97. EXTN(jsimd_quantize_sse2):
  98. push rbp
  99. mov rax,rsp
  100. mov rbp,rsp
  101. collect_args
  102. mov rsi, r12
  103. mov rdx, r11
  104. mov rdi, r10
  105. mov rax, DCTSIZE2/32
  106. .quantloop:
  107. movdqa xmm4, XMMWORD [XMMBLOCK(0,0,rsi,SIZEOF_DCTELEM)]
  108. movdqa xmm5, XMMWORD [XMMBLOCK(1,0,rsi,SIZEOF_DCTELEM)]
  109. movdqa xmm6, XMMWORD [XMMBLOCK(2,0,rsi,SIZEOF_DCTELEM)]
  110. movdqa xmm7, XMMWORD [XMMBLOCK(3,0,rsi,SIZEOF_DCTELEM)]
  111. movdqa xmm0,xmm4
  112. movdqa xmm1,xmm5
  113. movdqa xmm2,xmm6
  114. movdqa xmm3,xmm7
  115. psraw xmm4,(WORD_BIT-1)
  116. psraw xmm5,(WORD_BIT-1)
  117. psraw xmm6,(WORD_BIT-1)
  118. psraw xmm7,(WORD_BIT-1)
  119. pxor xmm0,xmm4
  120. pxor xmm1,xmm5
  121. pxor xmm2,xmm6
  122. pxor xmm3,xmm7
  123. psubw xmm0,xmm4 ; if (xmm0 < 0) xmm0 = -xmm0;
  124. psubw xmm1,xmm5 ; if (xmm1 < 0) xmm1 = -xmm1;
  125. psubw xmm2,xmm6 ; if (xmm2 < 0) xmm2 = -xmm2;
  126. psubw xmm3,xmm7 ; if (xmm3 < 0) xmm3 = -xmm3;
  127. paddw xmm0, XMMWORD [CORRECTION(0,0,rdx)] ; correction + roundfactor
  128. paddw xmm1, XMMWORD [CORRECTION(1,0,rdx)]
  129. paddw xmm2, XMMWORD [CORRECTION(2,0,rdx)]
  130. paddw xmm3, XMMWORD [CORRECTION(3,0,rdx)]
  131. pmulhuw xmm0, XMMWORD [RECIPROCAL(0,0,rdx)] ; reciprocal
  132. pmulhuw xmm1, XMMWORD [RECIPROCAL(1,0,rdx)]
  133. pmulhuw xmm2, XMMWORD [RECIPROCAL(2,0,rdx)]
  134. pmulhuw xmm3, XMMWORD [RECIPROCAL(3,0,rdx)]
  135. pmulhuw xmm0, XMMWORD [SCALE(0,0,rdx)] ; scale
  136. pmulhuw xmm1, XMMWORD [SCALE(1,0,rdx)]
  137. pmulhuw xmm2, XMMWORD [SCALE(2,0,rdx)]
  138. pmulhuw xmm3, XMMWORD [SCALE(3,0,rdx)]
  139. pxor xmm0,xmm4
  140. pxor xmm1,xmm5
  141. pxor xmm2,xmm6
  142. pxor xmm3,xmm7
  143. psubw xmm0,xmm4
  144. psubw xmm1,xmm5
  145. psubw xmm2,xmm6
  146. psubw xmm3,xmm7
  147. movdqa XMMWORD [XMMBLOCK(0,0,rdi,SIZEOF_DCTELEM)], xmm0
  148. movdqa XMMWORD [XMMBLOCK(1,0,rdi,SIZEOF_DCTELEM)], xmm1
  149. movdqa XMMWORD [XMMBLOCK(2,0,rdi,SIZEOF_DCTELEM)], xmm2
  150. movdqa XMMWORD [XMMBLOCK(3,0,rdi,SIZEOF_DCTELEM)], xmm3
  151. add rsi, byte 32*SIZEOF_DCTELEM
  152. add rdx, byte 32*SIZEOF_DCTELEM
  153. add rdi, byte 32*SIZEOF_JCOEF
  154. dec rax
  155. jnz near .quantloop
  156. uncollect_args
  157. pop rbp
  158. ret
  159. ; For some reason, the OS X linker does not honor the request to align the
  160. ; segment unless we do this.
  161. align 16