jquanti-sse2.asm 7.1 KB

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