jchuff-sse2.asm 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. ;
  2. ; jchuff-sse2.asm - Huffman entropy encoding (64-bit SSE2)
  3. ;
  4. ; Copyright (C) 2009-2011, 2014-2016, D. R. Commander.
  5. ; Copyright (C) 2015, Matthieu Darbois.
  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. ;
  17. ; This file contains an SSE2 implementation for Huffman coding of one block.
  18. ; The following code is based directly on jchuff.c; see jchuff.c for more
  19. ; details.
  20. %include "jsimdext.inc"
  21. ; --------------------------------------------------------------------------
  22. SECTION SEG_CONST
  23. alignz 32
  24. GLOBAL_DATA(jconst_huff_encode_one_block)
  25. EXTN(jconst_huff_encode_one_block):
  26. %include "jpeg_nbits_table.inc"
  27. alignz 32
  28. ; --------------------------------------------------------------------------
  29. SECTION SEG_TEXT
  30. BITS 64
  31. ; These macros perform the same task as the emit_bits() function in the
  32. ; original libjpeg code. In addition to reducing overhead by explicitly
  33. ; inlining the code, additional performance is achieved by taking into
  34. ; account the size of the bit buffer and waiting until it is almost full
  35. ; before emptying it. This mostly benefits 64-bit platforms, since 6
  36. ; bytes can be stored in a 64-bit bit buffer before it has to be emptied.
  37. %macro EMIT_BYTE 0
  38. sub put_bits, 8 ; put_bits -= 8;
  39. mov rdx, put_buffer
  40. mov ecx, put_bits
  41. shr rdx, cl ; c = (JOCTET)GETJOCTET(put_buffer >> put_bits);
  42. mov byte [buffer], dl ; *buffer++ = c;
  43. add buffer, 1
  44. cmp dl, 0xFF ; need to stuff a zero byte?
  45. jne %%.EMIT_BYTE_END
  46. mov byte [buffer], 0 ; *buffer++ = 0;
  47. add buffer, 1
  48. %%.EMIT_BYTE_END:
  49. %endmacro
  50. %macro PUT_BITS 1
  51. add put_bits, ecx ; put_bits += size;
  52. shl put_buffer, cl ; put_buffer = (put_buffer << size);
  53. or put_buffer, %1
  54. %endmacro
  55. %macro CHECKBUF31 0
  56. cmp put_bits, 32 ; if (put_bits > 31) {
  57. jl %%.CHECKBUF31_END
  58. EMIT_BYTE
  59. EMIT_BYTE
  60. EMIT_BYTE
  61. EMIT_BYTE
  62. %%.CHECKBUF31_END:
  63. %endmacro
  64. %macro CHECKBUF47 0
  65. cmp put_bits, 48 ; if (put_bits > 47) {
  66. jl %%.CHECKBUF47_END
  67. EMIT_BYTE
  68. EMIT_BYTE
  69. EMIT_BYTE
  70. EMIT_BYTE
  71. EMIT_BYTE
  72. EMIT_BYTE
  73. %%.CHECKBUF47_END:
  74. %endmacro
  75. %macro EMIT_BITS 2
  76. CHECKBUF47
  77. mov ecx, %2
  78. PUT_BITS %1
  79. %endmacro
  80. %macro kloop_prepare 37 ;(ko, jno0, ..., jno31, xmm0, xmm1, xmm2, xmm3)
  81. pxor xmm8, xmm8 ; __m128i neg = _mm_setzero_si128();
  82. pxor xmm9, xmm9 ; __m128i neg = _mm_setzero_si128();
  83. pxor xmm10, xmm10 ; __m128i neg = _mm_setzero_si128();
  84. pxor xmm11, xmm11 ; __m128i neg = _mm_setzero_si128();
  85. pinsrw %34, word [r12 + %2 * SIZEOF_WORD], 0 ; xmm_shadow[0] = block[jno0];
  86. pinsrw %35, word [r12 + %10 * SIZEOF_WORD], 0 ; xmm_shadow[8] = block[jno8];
  87. pinsrw %36, word [r12 + %18 * SIZEOF_WORD], 0 ; xmm_shadow[16] = block[jno16];
  88. pinsrw %37, word [r12 + %26 * SIZEOF_WORD], 0 ; xmm_shadow[24] = block[jno24];
  89. pinsrw %34, word [r12 + %3 * SIZEOF_WORD], 1 ; xmm_shadow[1] = block[jno1];
  90. pinsrw %35, word [r12 + %11 * SIZEOF_WORD], 1 ; xmm_shadow[9] = block[jno9];
  91. pinsrw %36, word [r12 + %19 * SIZEOF_WORD], 1 ; xmm_shadow[17] = block[jno17];
  92. pinsrw %37, word [r12 + %27 * SIZEOF_WORD], 1 ; xmm_shadow[25] = block[jno25];
  93. pinsrw %34, word [r12 + %4 * SIZEOF_WORD], 2 ; xmm_shadow[2] = block[jno2];
  94. pinsrw %35, word [r12 + %12 * SIZEOF_WORD], 2 ; xmm_shadow[10] = block[jno10];
  95. pinsrw %36, word [r12 + %20 * SIZEOF_WORD], 2 ; xmm_shadow[18] = block[jno18];
  96. pinsrw %37, word [r12 + %28 * SIZEOF_WORD], 2 ; xmm_shadow[26] = block[jno26];
  97. pinsrw %34, word [r12 + %5 * SIZEOF_WORD], 3 ; xmm_shadow[3] = block[jno3];
  98. pinsrw %35, word [r12 + %13 * SIZEOF_WORD], 3 ; xmm_shadow[11] = block[jno11];
  99. pinsrw %36, word [r12 + %21 * SIZEOF_WORD], 3 ; xmm_shadow[19] = block[jno19];
  100. pinsrw %37, word [r12 + %29 * SIZEOF_WORD], 3 ; xmm_shadow[27] = block[jno27];
  101. pinsrw %34, word [r12 + %6 * SIZEOF_WORD], 4 ; xmm_shadow[4] = block[jno4];
  102. pinsrw %35, word [r12 + %14 * SIZEOF_WORD], 4 ; xmm_shadow[12] = block[jno12];
  103. pinsrw %36, word [r12 + %22 * SIZEOF_WORD], 4 ; xmm_shadow[20] = block[jno20];
  104. pinsrw %37, word [r12 + %30 * SIZEOF_WORD], 4 ; xmm_shadow[28] = block[jno28];
  105. pinsrw %34, word [r12 + %7 * SIZEOF_WORD], 5 ; xmm_shadow[5] = block[jno5];
  106. pinsrw %35, word [r12 + %15 * SIZEOF_WORD], 5 ; xmm_shadow[13] = block[jno13];
  107. pinsrw %36, word [r12 + %23 * SIZEOF_WORD], 5 ; xmm_shadow[21] = block[jno21];
  108. pinsrw %37, word [r12 + %31 * SIZEOF_WORD], 5 ; xmm_shadow[29] = block[jno29];
  109. pinsrw %34, word [r12 + %8 * SIZEOF_WORD], 6 ; xmm_shadow[6] = block[jno6];
  110. pinsrw %35, word [r12 + %16 * SIZEOF_WORD], 6 ; xmm_shadow[14] = block[jno14];
  111. pinsrw %36, word [r12 + %24 * SIZEOF_WORD], 6 ; xmm_shadow[22] = block[jno22];
  112. pinsrw %37, word [r12 + %32 * SIZEOF_WORD], 6 ; xmm_shadow[30] = block[jno30];
  113. pinsrw %34, word [r12 + %9 * SIZEOF_WORD], 7 ; xmm_shadow[7] = block[jno7];
  114. pinsrw %35, word [r12 + %17 * SIZEOF_WORD], 7 ; xmm_shadow[15] = block[jno15];
  115. pinsrw %36, word [r12 + %25 * SIZEOF_WORD], 7 ; xmm_shadow[23] = block[jno23];
  116. %if %1 != 32
  117. pinsrw %37, word [r12 + %33 * SIZEOF_WORD], 7 ; xmm_shadow[31] = block[jno31];
  118. %else
  119. pinsrw %37, ebx, 7 ; xmm_shadow[31] = block[jno31];
  120. %endif
  121. pcmpgtw xmm8, %34 ; neg = _mm_cmpgt_epi16(neg, x1);
  122. pcmpgtw xmm9, %35 ; neg = _mm_cmpgt_epi16(neg, x1);
  123. pcmpgtw xmm10, %36 ; neg = _mm_cmpgt_epi16(neg, x1);
  124. pcmpgtw xmm11, %37 ; neg = _mm_cmpgt_epi16(neg, x1);
  125. paddw %34, xmm8 ; x1 = _mm_add_epi16(x1, neg);
  126. paddw %35, xmm9 ; x1 = _mm_add_epi16(x1, neg);
  127. paddw %36, xmm10 ; x1 = _mm_add_epi16(x1, neg);
  128. paddw %37, xmm11 ; x1 = _mm_add_epi16(x1, neg);
  129. pxor %34, xmm8 ; x1 = _mm_xor_si128(x1, neg);
  130. pxor %35, xmm9 ; x1 = _mm_xor_si128(x1, neg);
  131. pxor %36, xmm10 ; x1 = _mm_xor_si128(x1, neg);
  132. pxor %37, xmm11 ; x1 = _mm_xor_si128(x1, neg);
  133. pxor xmm8, %34 ; neg = _mm_xor_si128(neg, x1);
  134. pxor xmm9, %35 ; neg = _mm_xor_si128(neg, x1);
  135. pxor xmm10, %36 ; neg = _mm_xor_si128(neg, x1);
  136. pxor xmm11, %37 ; neg = _mm_xor_si128(neg, x1);
  137. movdqa XMMWORD [t1 + %1 * SIZEOF_WORD], %34 ; _mm_storeu_si128((__m128i *)(t1 + ko), x1);
  138. movdqa XMMWORD [t1 + (%1 + 8) * SIZEOF_WORD], %35 ; _mm_storeu_si128((__m128i *)(t1 + ko + 8), x1);
  139. movdqa XMMWORD [t1 + (%1 + 16) * SIZEOF_WORD], %36 ; _mm_storeu_si128((__m128i *)(t1 + ko + 16), x1);
  140. movdqa XMMWORD [t1 + (%1 + 24) * SIZEOF_WORD], %37 ; _mm_storeu_si128((__m128i *)(t1 + ko + 24), x1);
  141. movdqa XMMWORD [t2 + %1 * SIZEOF_WORD], xmm8 ; _mm_storeu_si128((__m128i *)(t2 + ko), neg);
  142. movdqa XMMWORD [t2 + (%1 + 8) * SIZEOF_WORD], xmm9 ; _mm_storeu_si128((__m128i *)(t2 + ko + 8), neg);
  143. movdqa XMMWORD [t2 + (%1 + 16) * SIZEOF_WORD], xmm10 ; _mm_storeu_si128((__m128i *)(t2 + ko + 16), neg);
  144. movdqa XMMWORD [t2 + (%1 + 24) * SIZEOF_WORD], xmm11 ; _mm_storeu_si128((__m128i *)(t2 + ko + 24), neg);
  145. %endmacro
  146. ;
  147. ; Encode a single block's worth of coefficients.
  148. ;
  149. ; GLOBAL(JOCTET *)
  150. ; jsimd_huff_encode_one_block_sse2(working_state *state, JOCTET *buffer,
  151. ; JCOEFPTR block, int last_dc_val,
  152. ; c_derived_tbl *dctbl, c_derived_tbl *actbl)
  153. ;
  154. ; r10 = working_state *state
  155. ; r11 = JOCTET *buffer
  156. ; r12 = JCOEFPTR block
  157. ; r13d = int last_dc_val
  158. ; r14 = c_derived_tbl *dctbl
  159. ; r15 = c_derived_tbl *actbl
  160. %define t1 rbp - (DCTSIZE2 * SIZEOF_WORD)
  161. %define t2 t1 - (DCTSIZE2 * SIZEOF_WORD)
  162. %define put_buffer r8
  163. %define put_bits r9d
  164. %define buffer rax
  165. align 32
  166. GLOBAL_FUNCTION(jsimd_huff_encode_one_block_sse2)
  167. EXTN(jsimd_huff_encode_one_block_sse2):
  168. push rbp
  169. mov rax, rsp ; rax = original rbp
  170. sub rsp, byte 4
  171. and rsp, byte (-SIZEOF_XMMWORD) ; align to 128 bits
  172. mov [rsp], rax
  173. mov rbp, rsp ; rbp = aligned rbp
  174. lea rsp, [t2]
  175. push_xmm 4
  176. collect_args 6
  177. push rbx
  178. mov buffer, r11 ; r11 is now sratch
  179. mov put_buffer, MMWORD [r10+16] ; put_buffer = state->cur.put_buffer;
  180. mov put_bits, dword [r10+24] ; put_bits = state->cur.put_bits;
  181. push r10 ; r10 is now scratch
  182. ; Encode the DC coefficient difference per section F.1.2.1
  183. movsx edi, word [r12] ; temp = temp2 = block[0] - last_dc_val;
  184. sub edi, r13d ; r13 is not used anymore
  185. mov ebx, edi
  186. ; This is a well-known technique for obtaining the absolute value
  187. ; without a branch. It is derived from an assembly language technique
  188. ; presented in "How to Optimize for the Pentium Processors",
  189. ; Copyright (c) 1996, 1997 by Agner Fog.
  190. mov esi, edi
  191. sar esi, 31 ; temp3 = temp >> (CHAR_BIT * sizeof(int) - 1);
  192. xor edi, esi ; temp ^= temp3;
  193. sub edi, esi ; temp -= temp3;
  194. ; For a negative input, want temp2 = bitwise complement of abs(input)
  195. ; This code assumes we are on a two's complement machine
  196. add ebx, esi ; temp2 += temp3;
  197. ; Find the number of bits needed for the magnitude of the coefficient
  198. lea r11, [rel jpeg_nbits_table]
  199. movzx rdi, byte [r11 + rdi] ; nbits = JPEG_NBITS(temp);
  200. ; Emit the Huffman-coded symbol for the number of bits
  201. mov r11d, INT [r14 + rdi * 4] ; code = dctbl->ehufco[nbits];
  202. movzx esi, byte [r14 + rdi + 1024] ; size = dctbl->ehufsi[nbits];
  203. EMIT_BITS r11, esi ; EMIT_BITS(code, size)
  204. ; Mask off any extra bits in code
  205. mov esi, 1
  206. mov ecx, edi
  207. shl esi, cl
  208. dec esi
  209. and ebx, esi ; temp2 &= (((JLONG)1)<<nbits) - 1;
  210. ; Emit that number of bits of the value, if positive,
  211. ; or the complement of its magnitude, if negative.
  212. EMIT_BITS rbx, edi ; EMIT_BITS(temp2, nbits)
  213. ; Prepare data
  214. xor ebx, ebx
  215. kloop_prepare 0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, \
  216. 18, 11, 4, 5, 12, 19, 26, 33, 40, 48, 41, 34, \
  217. 27, 20, 13, 6, 7, 14, 21, 28, 35, \
  218. xmm0, xmm1, xmm2, xmm3
  219. kloop_prepare 32, 42, 49, 56, 57, 50, 43, 36, 29, 22, 15, 23, \
  220. 30, 37, 44, 51, 58, 59, 52, 45, 38, 31, 39, 46, \
  221. 53, 60, 61, 54, 47, 55, 62, 63, 63, \
  222. xmm4, xmm5, xmm6, xmm7
  223. pxor xmm8, xmm8
  224. pcmpeqw xmm0, xmm8 ; tmp0 = _mm_cmpeq_epi16(tmp0, zero);
  225. pcmpeqw xmm1, xmm8 ; tmp1 = _mm_cmpeq_epi16(tmp1, zero);
  226. pcmpeqw xmm2, xmm8 ; tmp2 = _mm_cmpeq_epi16(tmp2, zero);
  227. pcmpeqw xmm3, xmm8 ; tmp3 = _mm_cmpeq_epi16(tmp3, zero);
  228. pcmpeqw xmm4, xmm8 ; tmp4 = _mm_cmpeq_epi16(tmp4, zero);
  229. pcmpeqw xmm5, xmm8 ; tmp5 = _mm_cmpeq_epi16(tmp5, zero);
  230. pcmpeqw xmm6, xmm8 ; tmp6 = _mm_cmpeq_epi16(tmp6, zero);
  231. pcmpeqw xmm7, xmm8 ; tmp7 = _mm_cmpeq_epi16(tmp7, zero);
  232. packsswb xmm0, xmm1 ; tmp0 = _mm_packs_epi16(tmp0, tmp1);
  233. packsswb xmm2, xmm3 ; tmp2 = _mm_packs_epi16(tmp2, tmp3);
  234. packsswb xmm4, xmm5 ; tmp4 = _mm_packs_epi16(tmp4, tmp5);
  235. packsswb xmm6, xmm7 ; tmp6 = _mm_packs_epi16(tmp6, tmp7);
  236. pmovmskb r11d, xmm0 ; index = ((uint64_t)_mm_movemask_epi8(tmp0)) << 0;
  237. pmovmskb r12d, xmm2 ; index = ((uint64_t)_mm_movemask_epi8(tmp2)) << 16;
  238. pmovmskb r13d, xmm4 ; index = ((uint64_t)_mm_movemask_epi8(tmp4)) << 32;
  239. pmovmskb r14d, xmm6 ; index = ((uint64_t)_mm_movemask_epi8(tmp6)) << 48;
  240. shl r12, 16
  241. shl r14, 16
  242. or r11, r12
  243. or r13, r14
  244. shl r13, 32
  245. or r11, r13
  246. not r11 ; index = ~index;
  247. ;mov MMWORD [ t1 + DCTSIZE2 * SIZEOF_WORD ], r11
  248. ;jmp .EFN
  249. mov r13d, INT [r15 + 240 * 4] ; code_0xf0 = actbl->ehufco[0xf0];
  250. movzx r14d, byte [r15 + 1024 + 240] ; size_0xf0 = actbl->ehufsi[0xf0];
  251. lea rsi, [t1]
  252. .BLOOP:
  253. bsf r12, r11 ; r = __builtin_ctzl(index);
  254. jz .ELOOP
  255. mov rcx, r12
  256. lea rsi, [rsi+r12*2] ; k += r;
  257. shr r11, cl ; index >>= r;
  258. movzx rdi, word [rsi] ; temp = t1[k];
  259. lea rbx, [rel jpeg_nbits_table]
  260. movzx rdi, byte [rbx + rdi] ; nbits = JPEG_NBITS(temp);
  261. .BRLOOP:
  262. cmp r12, 16 ; while (r > 15) {
  263. jl .ERLOOP
  264. EMIT_BITS r13, r14d ; EMIT_BITS(code_0xf0, size_0xf0)
  265. sub r12, 16 ; r -= 16;
  266. jmp .BRLOOP
  267. .ERLOOP:
  268. ; Emit Huffman symbol for run length / number of bits
  269. CHECKBUF31 ; uses rcx, rdx
  270. shl r12, 4 ; temp3 = (r << 4) + nbits;
  271. add r12, rdi
  272. mov ebx, INT [r15 + r12 * 4] ; code = actbl->ehufco[temp3];
  273. movzx ecx, byte [r15 + r12 + 1024] ; size = actbl->ehufsi[temp3];
  274. PUT_BITS rbx
  275. ;EMIT_CODE(code, size)
  276. movsx ebx, word [rsi-DCTSIZE2*2] ; temp2 = t2[k];
  277. ; Mask off any extra bits in code
  278. mov rcx, rdi
  279. mov rdx, 1
  280. shl rdx, cl
  281. dec rdx
  282. and rbx, rdx ; temp2 &= (((JLONG)1)<<nbits) - 1;
  283. PUT_BITS rbx ; PUT_BITS(temp2, nbits)
  284. shr r11, 1 ; index >>= 1;
  285. add rsi, 2 ; ++k;
  286. jmp .BLOOP
  287. .ELOOP:
  288. ; If the last coef(s) were zero, emit an end-of-block code
  289. lea rdi, [t1 + (DCTSIZE2-1) * 2] ; r = DCTSIZE2-1-k;
  290. cmp rdi, rsi ; if (r > 0) {
  291. je .EFN
  292. mov ebx, INT [r15] ; code = actbl->ehufco[0];
  293. movzx r12d, byte [r15 + 1024] ; size = actbl->ehufsi[0];
  294. EMIT_BITS rbx, r12d
  295. .EFN:
  296. pop r10
  297. ; Save put_buffer & put_bits
  298. mov MMWORD [r10+16], put_buffer ; state->cur.put_buffer = put_buffer;
  299. mov dword [r10+24], put_bits ; state->cur.put_bits = put_bits;
  300. pop rbx
  301. uncollect_args 6
  302. pop_xmm 4
  303. mov rsp, rbp ; rsp <- aligned rbp
  304. pop rsp ; rsp <- original rbp
  305. pop rbp
  306. ret
  307. ; For some reason, the OS X linker does not honor the request to align the
  308. ; segment unless we do this.
  309. align 32