jcsample-sse2.asm 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. ;
  2. ; jcsample.asm - downsampling (64-bit SSE2)
  3. ;
  4. ; Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
  5. ; Copyright (C) 2009, 2016, D. R. Commander.
  6. ; Copyright (C) 2018, Matthias Räncker.
  7. ;
  8. ; Based on the x86 SIMD extension for IJG JPEG library
  9. ; Copyright (C) 1999-2006, MIYASAKA Masaru.
  10. ; For conditions of distribution and use, see copyright notice in jsimdext.inc
  11. ;
  12. ; This file should be assembled with NASM (Netwide Assembler),
  13. ; can *not* be assembled with Microsoft's MASM or any compatible
  14. ; assembler (including Borland's Turbo Assembler).
  15. ; NASM is available from http://nasm.sourceforge.net/ or
  16. ; http://sourceforge.net/project/showfiles.php?group_id=6208
  17. %include "jsimdext.inc"
  18. ; --------------------------------------------------------------------------
  19. SECTION SEG_TEXT
  20. BITS 64
  21. ;
  22. ; Downsample pixel values of a single component.
  23. ; This version handles the common case of 2:1 horizontal and 1:1 vertical,
  24. ; without smoothing.
  25. ;
  26. ; GLOBAL(void)
  27. ; jsimd_h2v1_downsample_sse2(JDIMENSION image_width, int max_v_samp_factor,
  28. ; JDIMENSION v_samp_factor,
  29. ; JDIMENSION width_in_blocks, JSAMPARRAY input_data,
  30. ; JSAMPARRAY output_data);
  31. ;
  32. ; r10d = JDIMENSION image_width
  33. ; r11 = int max_v_samp_factor
  34. ; r12d = JDIMENSION v_samp_factor
  35. ; r13d = JDIMENSION width_in_blocks
  36. ; r14 = JSAMPARRAY input_data
  37. ; r15 = JSAMPARRAY output_data
  38. align 32
  39. GLOBAL_FUNCTION(jsimd_h2v1_downsample_sse2)
  40. EXTN(jsimd_h2v1_downsample_sse2):
  41. push rbp
  42. mov rax, rsp
  43. mov rbp, rsp
  44. collect_args 6
  45. mov ecx, r13d
  46. shl rcx, 3 ; imul rcx,DCTSIZE (rcx = output_cols)
  47. jz near .return
  48. mov edx, r10d
  49. ; -- expand_right_edge
  50. push rcx
  51. shl rcx, 1 ; output_cols * 2
  52. sub rcx, rdx
  53. jle short .expand_end
  54. mov rax, r11
  55. test rax, rax
  56. jle short .expand_end
  57. cld
  58. mov rsi, r14 ; input_data
  59. .expandloop:
  60. push rax
  61. push rcx
  62. mov rdip, JSAMPROW [rsi]
  63. add rdi, rdx
  64. mov al, JSAMPLE [rdi-1]
  65. rep stosb
  66. pop rcx
  67. pop rax
  68. add rsi, byte SIZEOF_JSAMPROW
  69. dec rax
  70. jg short .expandloop
  71. .expand_end:
  72. pop rcx ; output_cols
  73. ; -- h2v1_downsample
  74. mov eax, r12d ; rowctr
  75. test eax, eax
  76. jle near .return
  77. mov rdx, 0x00010000 ; bias pattern
  78. movd xmm7, edx
  79. pcmpeqw xmm6, xmm6
  80. pshufd xmm7, xmm7, 0x00 ; xmm7={0, 1, 0, 1, 0, 1, 0, 1}
  81. psrlw xmm6, BYTE_BIT ; xmm6={0xFF 0x00 0xFF 0x00 ..}
  82. mov rsi, r14 ; input_data
  83. mov rdi, r15 ; output_data
  84. .rowloop:
  85. push rcx
  86. push rdi
  87. push rsi
  88. mov rsip, JSAMPROW [rsi] ; inptr
  89. mov rdip, JSAMPROW [rdi] ; outptr
  90. cmp rcx, byte SIZEOF_XMMWORD
  91. jae short .columnloop
  92. .columnloop_r8:
  93. movdqa xmm0, XMMWORD [rsi+0*SIZEOF_XMMWORD]
  94. pxor xmm1, xmm1
  95. mov rcx, SIZEOF_XMMWORD
  96. jmp short .downsample
  97. .columnloop:
  98. movdqa xmm0, XMMWORD [rsi+0*SIZEOF_XMMWORD]
  99. movdqa xmm1, XMMWORD [rsi+1*SIZEOF_XMMWORD]
  100. .downsample:
  101. movdqa xmm2, xmm0
  102. movdqa xmm3, xmm1
  103. pand xmm0, xmm6
  104. psrlw xmm2, BYTE_BIT
  105. pand xmm1, xmm6
  106. psrlw xmm3, BYTE_BIT
  107. paddw xmm0, xmm2
  108. paddw xmm1, xmm3
  109. paddw xmm0, xmm7
  110. paddw xmm1, xmm7
  111. psrlw xmm0, 1
  112. psrlw xmm1, 1
  113. packuswb xmm0, xmm1
  114. movdqa XMMWORD [rdi+0*SIZEOF_XMMWORD], xmm0
  115. sub rcx, byte SIZEOF_XMMWORD ; outcol
  116. add rsi, byte 2*SIZEOF_XMMWORD ; inptr
  117. add rdi, byte 1*SIZEOF_XMMWORD ; outptr
  118. cmp rcx, byte SIZEOF_XMMWORD
  119. jae short .columnloop
  120. test rcx, rcx
  121. jnz short .columnloop_r8
  122. pop rsi
  123. pop rdi
  124. pop rcx
  125. add rsi, byte SIZEOF_JSAMPROW ; input_data
  126. add rdi, byte SIZEOF_JSAMPROW ; output_data
  127. dec rax ; rowctr
  128. jg near .rowloop
  129. .return:
  130. uncollect_args 6
  131. pop rbp
  132. ret
  133. ; --------------------------------------------------------------------------
  134. ;
  135. ; Downsample pixel values of a single component.
  136. ; This version handles the standard case of 2:1 horizontal and 2:1 vertical,
  137. ; without smoothing.
  138. ;
  139. ; GLOBAL(void)
  140. ; jsimd_h2v2_downsample_sse2(JDIMENSION image_width, int max_v_samp_factor,
  141. ; JDIMENSION v_samp_factor,
  142. ; JDIMENSION width_in_blocks, JSAMPARRAY input_data,
  143. ; JSAMPARRAY output_data);
  144. ;
  145. ; r10d = JDIMENSION image_width
  146. ; r11 = int max_v_samp_factor
  147. ; r12d = JDIMENSION v_samp_factor
  148. ; r13d = JDIMENSION width_in_blocks
  149. ; r14 = JSAMPARRAY input_data
  150. ; r15 = JSAMPARRAY output_data
  151. align 32
  152. GLOBAL_FUNCTION(jsimd_h2v2_downsample_sse2)
  153. EXTN(jsimd_h2v2_downsample_sse2):
  154. push rbp
  155. mov rax, rsp
  156. mov rbp, rsp
  157. collect_args 6
  158. mov ecx, r13d
  159. shl rcx, 3 ; imul rcx,DCTSIZE (rcx = output_cols)
  160. jz near .return
  161. mov edx, r10d
  162. ; -- expand_right_edge
  163. push rcx
  164. shl rcx, 1 ; output_cols * 2
  165. sub rcx, rdx
  166. jle short .expand_end
  167. mov rax, r11
  168. test rax, rax
  169. jle short .expand_end
  170. cld
  171. mov rsi, r14 ; input_data
  172. .expandloop:
  173. push rax
  174. push rcx
  175. mov rdip, JSAMPROW [rsi]
  176. add rdi, rdx
  177. mov al, JSAMPLE [rdi-1]
  178. rep stosb
  179. pop rcx
  180. pop rax
  181. add rsi, byte SIZEOF_JSAMPROW
  182. dec rax
  183. jg short .expandloop
  184. .expand_end:
  185. pop rcx ; output_cols
  186. ; -- h2v2_downsample
  187. mov eax, r12d ; rowctr
  188. test rax, rax
  189. jle near .return
  190. mov rdx, 0x00020001 ; bias pattern
  191. movd xmm7, edx
  192. pcmpeqw xmm6, xmm6
  193. pshufd xmm7, xmm7, 0x00 ; xmm7={1, 2, 1, 2, 1, 2, 1, 2}
  194. psrlw xmm6, BYTE_BIT ; xmm6={0xFF 0x00 0xFF 0x00 ..}
  195. mov rsi, r14 ; input_data
  196. mov rdi, r15 ; output_data
  197. .rowloop:
  198. push rcx
  199. push rdi
  200. push rsi
  201. mov rdxp, JSAMPROW [rsi+0*SIZEOF_JSAMPROW] ; inptr0
  202. mov rsip, JSAMPROW [rsi+1*SIZEOF_JSAMPROW] ; inptr1
  203. mov rdip, JSAMPROW [rdi] ; outptr
  204. cmp rcx, byte SIZEOF_XMMWORD
  205. jae short .columnloop
  206. .columnloop_r8:
  207. movdqa xmm0, XMMWORD [rdx+0*SIZEOF_XMMWORD]
  208. movdqa xmm1, XMMWORD [rsi+0*SIZEOF_XMMWORD]
  209. pxor xmm2, xmm2
  210. pxor xmm3, xmm3
  211. mov rcx, SIZEOF_XMMWORD
  212. jmp short .downsample
  213. .columnloop:
  214. movdqa xmm0, XMMWORD [rdx+0*SIZEOF_XMMWORD]
  215. movdqa xmm1, XMMWORD [rsi+0*SIZEOF_XMMWORD]
  216. movdqa xmm2, XMMWORD [rdx+1*SIZEOF_XMMWORD]
  217. movdqa xmm3, XMMWORD [rsi+1*SIZEOF_XMMWORD]
  218. .downsample:
  219. movdqa xmm4, xmm0
  220. movdqa xmm5, xmm1
  221. pand xmm0, xmm6
  222. psrlw xmm4, BYTE_BIT
  223. pand xmm1, xmm6
  224. psrlw xmm5, BYTE_BIT
  225. paddw xmm0, xmm4
  226. paddw xmm1, xmm5
  227. movdqa xmm4, xmm2
  228. movdqa xmm5, xmm3
  229. pand xmm2, xmm6
  230. psrlw xmm4, BYTE_BIT
  231. pand xmm3, xmm6
  232. psrlw xmm5, BYTE_BIT
  233. paddw xmm2, xmm4
  234. paddw xmm3, xmm5
  235. paddw xmm0, xmm1
  236. paddw xmm2, xmm3
  237. paddw xmm0, xmm7
  238. paddw xmm2, xmm7
  239. psrlw xmm0, 2
  240. psrlw xmm2, 2
  241. packuswb xmm0, xmm2
  242. movdqa XMMWORD [rdi+0*SIZEOF_XMMWORD], xmm0
  243. sub rcx, byte SIZEOF_XMMWORD ; outcol
  244. add rdx, byte 2*SIZEOF_XMMWORD ; inptr0
  245. add rsi, byte 2*SIZEOF_XMMWORD ; inptr1
  246. add rdi, byte 1*SIZEOF_XMMWORD ; outptr
  247. cmp rcx, byte SIZEOF_XMMWORD
  248. jae near .columnloop
  249. test rcx, rcx
  250. jnz near .columnloop_r8
  251. pop rsi
  252. pop rdi
  253. pop rcx
  254. add rsi, byte 2*SIZEOF_JSAMPROW ; input_data
  255. add rdi, byte 1*SIZEOF_JSAMPROW ; output_data
  256. dec rax ; rowctr
  257. jg near .rowloop
  258. .return:
  259. uncollect_args 6
  260. pop rbp
  261. ret
  262. ; For some reason, the OS X linker does not honor the request to align the
  263. ; segment unless we do this.
  264. align 32