jcsample-sse2.asm 10 KB

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