jcsample-sse2.asm 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. ;
  2. ; jcsample.asm - downsampling (SSE2)
  3. ;
  4. ; Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
  5. ; Copyright (C) 2016, 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. %include "jsimdext.inc"
  17. ; --------------------------------------------------------------------------
  18. SECTION SEG_TEXT
  19. BITS 32
  20. ;
  21. ; Downsample pixel values of a single component.
  22. ; This version handles the common case of 2:1 horizontal and 1:1 vertical,
  23. ; without smoothing.
  24. ;
  25. ; GLOBAL(void)
  26. ; jsimd_h2v1_downsample_sse2(JDIMENSION image_width, int max_v_samp_factor,
  27. ; JDIMENSION v_samp_factor,
  28. ; JDIMENSION width_in_blocks, JSAMPARRAY input_data,
  29. ; JSAMPARRAY output_data);
  30. ;
  31. %define img_width(b) (b) + 8 ; JDIMENSION image_width
  32. %define max_v_samp(b) (b) + 12 ; int max_v_samp_factor
  33. %define v_samp(b) (b) + 16 ; JDIMENSION v_samp_factor
  34. %define width_blks(b) (b) + 20 ; JDIMENSION width_in_blocks
  35. %define input_data(b) (b) + 24 ; JSAMPARRAY input_data
  36. %define output_data(b) (b) + 28 ; JSAMPARRAY output_data
  37. align 32
  38. GLOBAL_FUNCTION(jsimd_h2v1_downsample_sse2)
  39. EXTN(jsimd_h2v1_downsample_sse2):
  40. push ebp
  41. mov ebp, esp
  42. ; push ebx ; unused
  43. ; push ecx ; need not be preserved
  44. ; push edx ; need not be preserved
  45. push esi
  46. push edi
  47. mov ecx, JDIMENSION [width_blks(ebp)]
  48. shl ecx, 3 ; imul ecx,DCTSIZE (ecx = output_cols)
  49. jz near .return
  50. mov edx, JDIMENSION [img_width(ebp)]
  51. ; -- expand_right_edge
  52. push ecx
  53. shl ecx, 1 ; output_cols * 2
  54. sub ecx, edx
  55. jle short .expand_end
  56. mov eax, INT [max_v_samp(ebp)]
  57. test eax, eax
  58. jle short .expand_end
  59. cld
  60. mov esi, JSAMPARRAY [input_data(ebp)] ; input_data
  61. alignx 16, 7
  62. .expandloop:
  63. push eax
  64. push ecx
  65. mov edi, JSAMPROW [esi]
  66. add edi, edx
  67. mov al, JSAMPLE [edi-1]
  68. rep stosb
  69. pop ecx
  70. pop eax
  71. add esi, byte SIZEOF_JSAMPROW
  72. dec eax
  73. jg short .expandloop
  74. .expand_end:
  75. pop ecx ; output_cols
  76. ; -- h2v1_downsample
  77. mov eax, JDIMENSION [v_samp(ebp)] ; rowctr
  78. test eax, eax
  79. jle near .return
  80. mov edx, 0x00010000 ; bias pattern
  81. movd xmm7, edx
  82. pcmpeqw xmm6, xmm6
  83. pshufd xmm7, xmm7, 0x00 ; xmm7={0, 1, 0, 1, 0, 1, 0, 1}
  84. psrlw xmm6, BYTE_BIT ; xmm6={0xFF 0x00 0xFF 0x00 ..}
  85. mov esi, JSAMPARRAY [input_data(ebp)] ; input_data
  86. mov edi, JSAMPARRAY [output_data(ebp)] ; output_data
  87. alignx 16, 7
  88. .rowloop:
  89. push ecx
  90. push edi
  91. push esi
  92. mov esi, JSAMPROW [esi] ; inptr
  93. mov edi, JSAMPROW [edi] ; outptr
  94. cmp ecx, byte SIZEOF_XMMWORD
  95. jae short .columnloop
  96. alignx 16, 7
  97. .columnloop_r8:
  98. movdqa xmm0, XMMWORD [esi+0*SIZEOF_XMMWORD]
  99. pxor xmm1, xmm1
  100. mov ecx, SIZEOF_XMMWORD
  101. jmp short .downsample
  102. alignx 16, 7
  103. .columnloop:
  104. movdqa xmm0, XMMWORD [esi+0*SIZEOF_XMMWORD]
  105. movdqa xmm1, XMMWORD [esi+1*SIZEOF_XMMWORD]
  106. .downsample:
  107. movdqa xmm2, xmm0
  108. movdqa xmm3, xmm1
  109. pand xmm0, xmm6
  110. psrlw xmm2, BYTE_BIT
  111. pand xmm1, xmm6
  112. psrlw xmm3, BYTE_BIT
  113. paddw xmm0, xmm2
  114. paddw xmm1, xmm3
  115. paddw xmm0, xmm7
  116. paddw xmm1, xmm7
  117. psrlw xmm0, 1
  118. psrlw xmm1, 1
  119. packuswb xmm0, xmm1
  120. movdqa XMMWORD [edi+0*SIZEOF_XMMWORD], xmm0
  121. sub ecx, byte SIZEOF_XMMWORD ; outcol
  122. add esi, byte 2*SIZEOF_XMMWORD ; inptr
  123. add edi, byte 1*SIZEOF_XMMWORD ; outptr
  124. cmp ecx, byte SIZEOF_XMMWORD
  125. jae short .columnloop
  126. test ecx, ecx
  127. jnz short .columnloop_r8
  128. pop esi
  129. pop edi
  130. pop ecx
  131. add esi, byte SIZEOF_JSAMPROW ; input_data
  132. add edi, byte SIZEOF_JSAMPROW ; output_data
  133. dec eax ; rowctr
  134. jg near .rowloop
  135. .return:
  136. pop edi
  137. pop esi
  138. ; pop edx ; need not be preserved
  139. ; pop ecx ; need not be preserved
  140. ; pop ebx ; unused
  141. pop ebp
  142. ret
  143. ; --------------------------------------------------------------------------
  144. ;
  145. ; Downsample pixel values of a single component.
  146. ; This version handles the standard case of 2:1 horizontal and 2:1 vertical,
  147. ; without smoothing.
  148. ;
  149. ; GLOBAL(void)
  150. ; jsimd_h2v2_downsample_sse2(JDIMENSION image_width, int max_v_samp_factor,
  151. ; JDIMENSION v_samp_factor,
  152. ; JDIMENSION width_in_blocks, JSAMPARRAY input_data,
  153. ; JSAMPARRAY output_data);
  154. ;
  155. %define img_width(b) (b) + 8 ; JDIMENSION image_width
  156. %define max_v_samp(b) (b) + 12 ; int max_v_samp_factor
  157. %define v_samp(b) (b) + 16 ; JDIMENSION v_samp_factor
  158. %define width_blks(b) (b) + 20 ; JDIMENSION width_in_blocks
  159. %define input_data(b) (b) + 24 ; JSAMPARRAY input_data
  160. %define output_data(b) (b) + 28 ; JSAMPARRAY output_data
  161. align 32
  162. GLOBAL_FUNCTION(jsimd_h2v2_downsample_sse2)
  163. EXTN(jsimd_h2v2_downsample_sse2):
  164. push ebp
  165. mov ebp, esp
  166. ; push ebx ; unused
  167. ; push ecx ; need not be preserved
  168. ; push edx ; need not be preserved
  169. push esi
  170. push edi
  171. mov ecx, JDIMENSION [width_blks(ebp)]
  172. shl ecx, 3 ; imul ecx,DCTSIZE (ecx = output_cols)
  173. jz near .return
  174. mov edx, JDIMENSION [img_width(ebp)]
  175. ; -- expand_right_edge
  176. push ecx
  177. shl ecx, 1 ; output_cols * 2
  178. sub ecx, edx
  179. jle short .expand_end
  180. mov eax, INT [max_v_samp(ebp)]
  181. test eax, eax
  182. jle short .expand_end
  183. cld
  184. mov esi, JSAMPARRAY [input_data(ebp)] ; input_data
  185. alignx 16, 7
  186. .expandloop:
  187. push eax
  188. push ecx
  189. mov edi, JSAMPROW [esi]
  190. add edi, edx
  191. mov al, JSAMPLE [edi-1]
  192. rep stosb
  193. pop ecx
  194. pop eax
  195. add esi, byte SIZEOF_JSAMPROW
  196. dec eax
  197. jg short .expandloop
  198. .expand_end:
  199. pop ecx ; output_cols
  200. ; -- h2v2_downsample
  201. mov eax, JDIMENSION [v_samp(ebp)] ; rowctr
  202. test eax, eax
  203. jle near .return
  204. mov edx, 0x00020001 ; bias pattern
  205. movd xmm7, edx
  206. pcmpeqw xmm6, xmm6
  207. pshufd xmm7, xmm7, 0x00 ; xmm7={1, 2, 1, 2, 1, 2, 1, 2}
  208. psrlw xmm6, BYTE_BIT ; xmm6={0xFF 0x00 0xFF 0x00 ..}
  209. mov esi, JSAMPARRAY [input_data(ebp)] ; input_data
  210. mov edi, JSAMPARRAY [output_data(ebp)] ; output_data
  211. alignx 16, 7
  212. .rowloop:
  213. push ecx
  214. push edi
  215. push esi
  216. mov edx, JSAMPROW [esi+0*SIZEOF_JSAMPROW] ; inptr0
  217. mov esi, JSAMPROW [esi+1*SIZEOF_JSAMPROW] ; inptr1
  218. mov edi, JSAMPROW [edi] ; outptr
  219. cmp ecx, byte SIZEOF_XMMWORD
  220. jae short .columnloop
  221. alignx 16, 7
  222. .columnloop_r8:
  223. movdqa xmm0, XMMWORD [edx+0*SIZEOF_XMMWORD]
  224. movdqa xmm1, XMMWORD [esi+0*SIZEOF_XMMWORD]
  225. pxor xmm2, xmm2
  226. pxor xmm3, xmm3
  227. mov ecx, SIZEOF_XMMWORD
  228. jmp short .downsample
  229. alignx 16, 7
  230. .columnloop:
  231. movdqa xmm0, XMMWORD [edx+0*SIZEOF_XMMWORD]
  232. movdqa xmm1, XMMWORD [esi+0*SIZEOF_XMMWORD]
  233. movdqa xmm2, XMMWORD [edx+1*SIZEOF_XMMWORD]
  234. movdqa xmm3, XMMWORD [esi+1*SIZEOF_XMMWORD]
  235. .downsample:
  236. movdqa xmm4, xmm0
  237. movdqa xmm5, xmm1
  238. pand xmm0, xmm6
  239. psrlw xmm4, BYTE_BIT
  240. pand xmm1, xmm6
  241. psrlw xmm5, BYTE_BIT
  242. paddw xmm0, xmm4
  243. paddw xmm1, xmm5
  244. movdqa xmm4, xmm2
  245. movdqa xmm5, xmm3
  246. pand xmm2, xmm6
  247. psrlw xmm4, BYTE_BIT
  248. pand xmm3, xmm6
  249. psrlw xmm5, BYTE_BIT
  250. paddw xmm2, xmm4
  251. paddw xmm3, xmm5
  252. paddw xmm0, xmm1
  253. paddw xmm2, xmm3
  254. paddw xmm0, xmm7
  255. paddw xmm2, xmm7
  256. psrlw xmm0, 2
  257. psrlw xmm2, 2
  258. packuswb xmm0, xmm2
  259. movdqa XMMWORD [edi+0*SIZEOF_XMMWORD], xmm0
  260. sub ecx, byte SIZEOF_XMMWORD ; outcol
  261. add edx, byte 2*SIZEOF_XMMWORD ; inptr0
  262. add esi, byte 2*SIZEOF_XMMWORD ; inptr1
  263. add edi, byte 1*SIZEOF_XMMWORD ; outptr
  264. cmp ecx, byte SIZEOF_XMMWORD
  265. jae near .columnloop
  266. test ecx, ecx
  267. jnz near .columnloop_r8
  268. pop esi
  269. pop edi
  270. pop ecx
  271. add esi, byte 2*SIZEOF_JSAMPROW ; input_data
  272. add edi, byte 1*SIZEOF_JSAMPROW ; output_data
  273. dec eax ; rowctr
  274. jg near .rowloop
  275. .return:
  276. pop edi
  277. pop esi
  278. ; pop edx ; need not be preserved
  279. ; pop ecx ; need not be preserved
  280. ; pop ebx ; unused
  281. pop ebp
  282. ret
  283. ; For some reason, the OS X linker does not honor the request to align the
  284. ; segment unless we do this.
  285. align 32