jquant-mmx.asm 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. ;
  2. ; jquant.asm - sample data conversion and quantization (MMX)
  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_mmx (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_mmx)
  34. EXTN(jsimd_convsamp_mmx):
  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 mm6,mm6 ; mm6=(all 0's)
  43. pcmpeqw mm7,mm7
  44. psllw mm7,7 ; mm7={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 mm0, MMWORD [ebx+eax*SIZEOF_JSAMPLE] ; mm0=(01234567)
  54. movq mm1, MMWORD [edx+eax*SIZEOF_JSAMPLE] ; mm1=(89ABCDEF)
  55. mov ebx, JSAMPROW [esi+2*SIZEOF_JSAMPROW] ; (JSAMPLE *)
  56. mov edx, JSAMPROW [esi+3*SIZEOF_JSAMPROW] ; (JSAMPLE *)
  57. movq mm2, MMWORD [ebx+eax*SIZEOF_JSAMPLE] ; mm2=(GHIJKLMN)
  58. movq mm3, MMWORD [edx+eax*SIZEOF_JSAMPLE] ; mm3=(OPQRSTUV)
  59. movq mm4,mm0
  60. punpcklbw mm0,mm6 ; mm0=(0123)
  61. punpckhbw mm4,mm6 ; mm4=(4567)
  62. movq mm5,mm1
  63. punpcklbw mm1,mm6 ; mm1=(89AB)
  64. punpckhbw mm5,mm6 ; mm5=(CDEF)
  65. paddw mm0,mm7
  66. paddw mm4,mm7
  67. paddw mm1,mm7
  68. paddw mm5,mm7
  69. movq MMWORD [MMBLOCK(0,0,edi,SIZEOF_DCTELEM)], mm0
  70. movq MMWORD [MMBLOCK(0,1,edi,SIZEOF_DCTELEM)], mm4
  71. movq MMWORD [MMBLOCK(1,0,edi,SIZEOF_DCTELEM)], mm1
  72. movq MMWORD [MMBLOCK(1,1,edi,SIZEOF_DCTELEM)], mm5
  73. movq mm0,mm2
  74. punpcklbw mm2,mm6 ; mm2=(GHIJ)
  75. punpckhbw mm0,mm6 ; mm0=(KLMN)
  76. movq mm4,mm3
  77. punpcklbw mm3,mm6 ; mm3=(OPQR)
  78. punpckhbw mm4,mm6 ; mm4=(STUV)
  79. paddw mm2,mm7
  80. paddw mm0,mm7
  81. paddw mm3,mm7
  82. paddw mm4,mm7
  83. movq MMWORD [MMBLOCK(2,0,edi,SIZEOF_DCTELEM)], mm2
  84. movq MMWORD [MMBLOCK(2,1,edi,SIZEOF_DCTELEM)], mm0
  85. movq MMWORD [MMBLOCK(3,0,edi,SIZEOF_DCTELEM)], mm3
  86. movq MMWORD [MMBLOCK(3,1,edi,SIZEOF_DCTELEM)], mm4
  87. add esi, byte 4*SIZEOF_JSAMPROW
  88. add edi, byte 4*DCTSIZE*SIZEOF_DCTELEM
  89. dec ecx
  90. jnz short .convloop
  91. emms ; empty MMX state
  92. pop edi
  93. pop esi
  94. ; pop edx ; need not be preserved
  95. ; pop ecx ; need not be preserved
  96. pop ebx
  97. pop ebp
  98. ret
  99. ; --------------------------------------------------------------------------
  100. ;
  101. ; Quantize/descale the coefficients, and store into coef_block
  102. ;
  103. ; This implementation is based on an algorithm described in
  104. ; "How to optimize for the Pentium family of microprocessors"
  105. ; (http://www.agner.org/assem/).
  106. ;
  107. ; GLOBAL(void)
  108. ; jsimd_quantize_mmx (JCOEFPTR coef_block, DCTELEM *divisors,
  109. ; DCTELEM *workspace);
  110. ;
  111. %define RECIPROCAL(m,n,b) MMBLOCK(DCTSIZE*0+(m),(n),(b),SIZEOF_DCTELEM)
  112. %define CORRECTION(m,n,b) MMBLOCK(DCTSIZE*1+(m),(n),(b),SIZEOF_DCTELEM)
  113. %define SCALE(m,n,b) MMBLOCK(DCTSIZE*2+(m),(n),(b),SIZEOF_DCTELEM)
  114. %define SHIFT(m,n,b) MMBLOCK(DCTSIZE*3+(m),(n),(b),SIZEOF_DCTELEM)
  115. %define coef_block ebp+8 ; JCOEFPTR coef_block
  116. %define divisors ebp+12 ; DCTELEM *divisors
  117. %define workspace ebp+16 ; DCTELEM *workspace
  118. align 16
  119. global EXTN(jsimd_quantize_mmx)
  120. EXTN(jsimd_quantize_mmx):
  121. push ebp
  122. mov ebp,esp
  123. ; push ebx ; unused
  124. ; push ecx ; unused
  125. ; push edx ; need not be preserved
  126. push esi
  127. push edi
  128. mov esi, POINTER [workspace]
  129. mov edx, POINTER [divisors]
  130. mov edi, JCOEFPTR [coef_block]
  131. mov ah, 2
  132. alignx 16,7
  133. .quantloop1:
  134. mov al, DCTSIZE2/8/2
  135. alignx 16,7
  136. .quantloop2:
  137. movq mm2, MMWORD [MMBLOCK(0,0,esi,SIZEOF_DCTELEM)]
  138. movq mm3, MMWORD [MMBLOCK(0,1,esi,SIZEOF_DCTELEM)]
  139. movq mm0,mm2
  140. movq mm1,mm3
  141. psraw mm2,(WORD_BIT-1) ; -1 if value < 0, 0 otherwise
  142. psraw mm3,(WORD_BIT-1)
  143. pxor mm0,mm2 ; val = -val
  144. pxor mm1,mm3
  145. psubw mm0,mm2
  146. psubw mm1,mm3
  147. ;
  148. ; MMX is an annoyingly crappy instruction set. It has two
  149. ; misfeatures that are causing problems here:
  150. ;
  151. ; - All multiplications are signed.
  152. ;
  153. ; - The second operand for the shifts is not treated as packed.
  154. ;
  155. ;
  156. ; We work around the first problem by implementing this algorithm:
  157. ;
  158. ; unsigned long unsigned_multiply(unsigned short x, unsigned short y)
  159. ; {
  160. ; enum { SHORT_BIT = 16 };
  161. ; signed short sx = (signed short) x;
  162. ; signed short sy = (signed short) y;
  163. ; signed long sz;
  164. ;
  165. ; sz = (long) sx * (long) sy; /* signed multiply */
  166. ;
  167. ; if (sx < 0) sz += (long) sy << SHORT_BIT;
  168. ; if (sy < 0) sz += (long) sx << SHORT_BIT;
  169. ;
  170. ; return (unsigned long) sz;
  171. ; }
  172. ;
  173. ; (note that a negative sx adds _sy_ and vice versa)
  174. ;
  175. ; For the second problem, we replace the shift by a multiplication.
  176. ; Unfortunately that means we have to deal with the signed issue again.
  177. ;
  178. paddw mm0, MMWORD [CORRECTION(0,0,edx)] ; correction + roundfactor
  179. paddw mm1, MMWORD [CORRECTION(0,1,edx)]
  180. movq mm4,mm0 ; store current value for later
  181. movq mm5,mm1
  182. pmulhw mm0, MMWORD [RECIPROCAL(0,0,edx)] ; reciprocal
  183. pmulhw mm1, MMWORD [RECIPROCAL(0,1,edx)]
  184. paddw mm0,mm4 ; reciprocal is always negative (MSB=1),
  185. paddw mm1,mm5 ; so we always need to add the initial value
  186. ; (input value is never negative as we
  187. ; inverted it at the start of this routine)
  188. ; here it gets a bit tricky as both scale
  189. ; and mm0/mm1 can be negative
  190. movq mm6, MMWORD [SCALE(0,0,edx)] ; scale
  191. movq mm7, MMWORD [SCALE(0,1,edx)]
  192. movq mm4,mm0
  193. movq mm5,mm1
  194. pmulhw mm0,mm6
  195. pmulhw mm1,mm7
  196. psraw mm6,(WORD_BIT-1) ; determine if scale is negative
  197. psraw mm7,(WORD_BIT-1)
  198. pand mm6,mm4 ; and add input if it is
  199. pand mm7,mm5
  200. paddw mm0,mm6
  201. paddw mm1,mm7
  202. psraw mm4,(WORD_BIT-1) ; then check if negative input
  203. psraw mm5,(WORD_BIT-1)
  204. pand mm4, MMWORD [SCALE(0,0,edx)] ; and add scale if it is
  205. pand mm5, MMWORD [SCALE(0,1,edx)]
  206. paddw mm0,mm4
  207. paddw mm1,mm5
  208. pxor mm0,mm2 ; val = -val
  209. pxor mm1,mm3
  210. psubw mm0,mm2
  211. psubw mm1,mm3
  212. movq MMWORD [MMBLOCK(0,0,edi,SIZEOF_DCTELEM)], mm0
  213. movq MMWORD [MMBLOCK(0,1,edi,SIZEOF_DCTELEM)], mm1
  214. add esi, byte 8*SIZEOF_DCTELEM
  215. add edx, byte 8*SIZEOF_DCTELEM
  216. add edi, byte 8*SIZEOF_JCOEF
  217. dec al
  218. jnz near .quantloop2
  219. dec ah
  220. jnz near .quantloop1 ; to avoid branch misprediction
  221. emms ; empty MMX state
  222. pop edi
  223. pop esi
  224. ; pop edx ; need not be preserved
  225. ; pop ecx ; unused
  226. ; pop ebx ; unused
  227. pop ebp
  228. ret
  229. ; For some reason, the OS X linker does not honor the request to align the
  230. ; segment unless we do this.
  231. align 16