jchuff-sse2.asm 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. ;
  2. ; jchuff-sse2.asm - Huffman entropy encoding (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. ;
  21. ; [TAB8]
  22. %include "jsimdext.inc"
  23. ; --------------------------------------------------------------------------
  24. SECTION SEG_CONST
  25. alignz 16
  26. global EXTN(jconst_huff_encode_one_block)
  27. EXTN(jconst_huff_encode_one_block):
  28. %include "jpeg_nbits_table.inc"
  29. alignz 16
  30. ; --------------------------------------------------------------------------
  31. SECTION SEG_TEXT
  32. BITS 32
  33. ; These macros perform the same task as the emit_bits() function in the
  34. ; original libjpeg code. In addition to reducing overhead by explicitly
  35. ; inlining the code, additional performance is achieved by taking into
  36. ; account the size of the bit buffer and waiting until it is almost full
  37. ; before emptying it. This mostly benefits 64-bit platforms, since 6
  38. ; bytes can be stored in a 64-bit bit buffer before it has to be emptied.
  39. %macro EMIT_BYTE 0
  40. sub put_bits, 8 ; put_bits -= 8;
  41. mov edx, put_buffer
  42. mov ecx, put_bits
  43. shr edx, cl ; c = (JOCTET)GETJOCTET(put_buffer >> put_bits);
  44. mov byte [eax], dl ; *buffer++ = c;
  45. add eax, 1
  46. cmp dl, 0xFF ; need to stuff a zero byte?
  47. jne %%.EMIT_BYTE_END
  48. mov byte [eax], 0 ; *buffer++ = 0;
  49. add eax, 1
  50. %%.EMIT_BYTE_END:
  51. %endmacro
  52. %macro PUT_BITS 1
  53. add put_bits, ecx ; put_bits += size;
  54. shl put_buffer, cl ; put_buffer = (put_buffer << size);
  55. or put_buffer, %1
  56. %endmacro
  57. %macro CHECKBUF15 0
  58. cmp put_bits, 16 ; if (put_bits > 31) {
  59. jl %%.CHECKBUF15_END
  60. mov eax, POINTER [esp+buffer]
  61. EMIT_BYTE
  62. EMIT_BYTE
  63. mov POINTER [esp+buffer], eax
  64. %%.CHECKBUF15_END:
  65. %endmacro
  66. %macro EMIT_BITS 1
  67. PUT_BITS %1
  68. CHECKBUF15
  69. %endmacro
  70. %macro kloop_prepare 37 ;(ko, jno0, ..., jno31, xmm0, xmm1, xmm2, xmm3)
  71. pxor xmm4, xmm4 ; __m128i neg = _mm_setzero_si128();
  72. pxor xmm5, xmm5 ; __m128i neg = _mm_setzero_si128();
  73. pxor xmm6, xmm6 ; __m128i neg = _mm_setzero_si128();
  74. pxor xmm7, xmm7 ; __m128i neg = _mm_setzero_si128();
  75. pinsrw %34, word [esi + %2 * SIZEOF_WORD], 0 ; xmm_shadow[0] = block[jno0];
  76. pinsrw %35, word [esi + %10 * SIZEOF_WORD], 0 ; xmm_shadow[8] = block[jno8];
  77. pinsrw %36, word [esi + %18 * SIZEOF_WORD], 0 ; xmm_shadow[16] = block[jno16];
  78. pinsrw %37, word [esi + %26 * SIZEOF_WORD], 0 ; xmm_shadow[24] = block[jno24];
  79. pinsrw %34, word [esi + %3 * SIZEOF_WORD], 1 ; xmm_shadow[1] = block[jno1];
  80. pinsrw %35, word [esi + %11 * SIZEOF_WORD], 1 ; xmm_shadow[9] = block[jno9];
  81. pinsrw %36, word [esi + %19 * SIZEOF_WORD], 1 ; xmm_shadow[17] = block[jno17];
  82. pinsrw %37, word [esi + %27 * SIZEOF_WORD], 1 ; xmm_shadow[25] = block[jno25];
  83. pinsrw %34, word [esi + %4 * SIZEOF_WORD], 2 ; xmm_shadow[2] = block[jno2];
  84. pinsrw %35, word [esi + %12 * SIZEOF_WORD], 2 ; xmm_shadow[10] = block[jno10];
  85. pinsrw %36, word [esi + %20 * SIZEOF_WORD], 2 ; xmm_shadow[18] = block[jno18];
  86. pinsrw %37, word [esi + %28 * SIZEOF_WORD], 2 ; xmm_shadow[26] = block[jno26];
  87. pinsrw %34, word [esi + %5 * SIZEOF_WORD], 3 ; xmm_shadow[3] = block[jno3];
  88. pinsrw %35, word [esi + %13 * SIZEOF_WORD], 3 ; xmm_shadow[11] = block[jno11];
  89. pinsrw %36, word [esi + %21 * SIZEOF_WORD], 3 ; xmm_shadow[19] = block[jno19];
  90. pinsrw %37, word [esi + %29 * SIZEOF_WORD], 3 ; xmm_shadow[27] = block[jno27];
  91. pinsrw %34, word [esi + %6 * SIZEOF_WORD], 4 ; xmm_shadow[4] = block[jno4];
  92. pinsrw %35, word [esi + %14 * SIZEOF_WORD], 4 ; xmm_shadow[12] = block[jno12];
  93. pinsrw %36, word [esi + %22 * SIZEOF_WORD], 4 ; xmm_shadow[20] = block[jno20];
  94. pinsrw %37, word [esi + %30 * SIZEOF_WORD], 4 ; xmm_shadow[28] = block[jno28];
  95. pinsrw %34, word [esi + %7 * SIZEOF_WORD], 5 ; xmm_shadow[5] = block[jno5];
  96. pinsrw %35, word [esi + %15 * SIZEOF_WORD], 5 ; xmm_shadow[13] = block[jno13];
  97. pinsrw %36, word [esi + %23 * SIZEOF_WORD], 5 ; xmm_shadow[21] = block[jno21];
  98. pinsrw %37, word [esi + %31 * SIZEOF_WORD], 5 ; xmm_shadow[29] = block[jno29];
  99. pinsrw %34, word [esi + %8 * SIZEOF_WORD], 6 ; xmm_shadow[6] = block[jno6];
  100. pinsrw %35, word [esi + %16 * SIZEOF_WORD], 6 ; xmm_shadow[14] = block[jno14];
  101. pinsrw %36, word [esi + %24 * SIZEOF_WORD], 6 ; xmm_shadow[22] = block[jno22];
  102. pinsrw %37, word [esi + %32 * SIZEOF_WORD], 6 ; xmm_shadow[30] = block[jno30];
  103. pinsrw %34, word [esi + %9 * SIZEOF_WORD], 7 ; xmm_shadow[7] = block[jno7];
  104. pinsrw %35, word [esi + %17 * SIZEOF_WORD], 7 ; xmm_shadow[15] = block[jno15];
  105. pinsrw %36, word [esi + %25 * SIZEOF_WORD], 7 ; xmm_shadow[23] = block[jno23];
  106. %if %1 != 32
  107. pinsrw %37, word [esi + %33 * SIZEOF_WORD], 7 ; xmm_shadow[31] = block[jno31];
  108. %else
  109. pinsrw %37, ecx, 7 ; xmm_shadow[31] = block[jno31];
  110. %endif
  111. pcmpgtw xmm4, %34 ; neg = _mm_cmpgt_epi16(neg, x1);
  112. pcmpgtw xmm5, %35 ; neg = _mm_cmpgt_epi16(neg, x1);
  113. pcmpgtw xmm6, %36 ; neg = _mm_cmpgt_epi16(neg, x1);
  114. pcmpgtw xmm7, %37 ; neg = _mm_cmpgt_epi16(neg, x1);
  115. paddw %34, xmm4 ; x1 = _mm_add_epi16(x1, neg);
  116. paddw %35, xmm5 ; x1 = _mm_add_epi16(x1, neg);
  117. paddw %36, xmm6 ; x1 = _mm_add_epi16(x1, neg);
  118. paddw %37, xmm7 ; x1 = _mm_add_epi16(x1, neg);
  119. pxor %34, xmm4 ; x1 = _mm_xor_si128(x1, neg);
  120. pxor %35, xmm5 ; x1 = _mm_xor_si128(x1, neg);
  121. pxor %36, xmm6 ; x1 = _mm_xor_si128(x1, neg);
  122. pxor %37, xmm7 ; x1 = _mm_xor_si128(x1, neg);
  123. pxor xmm4, %34 ; neg = _mm_xor_si128(neg, x1);
  124. pxor xmm5, %35 ; neg = _mm_xor_si128(neg, x1);
  125. pxor xmm6, %36 ; neg = _mm_xor_si128(neg, x1);
  126. pxor xmm7, %37 ; neg = _mm_xor_si128(neg, x1);
  127. movdqa XMMWORD [esp + t1 + %1 * SIZEOF_WORD], %34 ; _mm_storeu_si128((__m128i *)(t1 + ko), x1);
  128. movdqa XMMWORD [esp + t1 + (%1 + 8) * SIZEOF_WORD], %35 ; _mm_storeu_si128((__m128i *)(t1 + ko + 8), x1);
  129. movdqa XMMWORD [esp + t1 + (%1 + 16) * SIZEOF_WORD], %36 ; _mm_storeu_si128((__m128i *)(t1 + ko + 16), x1);
  130. movdqa XMMWORD [esp + t1 + (%1 + 24) * SIZEOF_WORD], %37 ; _mm_storeu_si128((__m128i *)(t1 + ko + 24), x1);
  131. movdqa XMMWORD [esp + t2 + %1 * SIZEOF_WORD], xmm4 ; _mm_storeu_si128((__m128i *)(t2 + ko), neg);
  132. movdqa XMMWORD [esp + t2 + (%1 + 8) * SIZEOF_WORD], xmm5 ; _mm_storeu_si128((__m128i *)(t2 + ko + 8), neg);
  133. movdqa XMMWORD [esp + t2 + (%1 + 16) * SIZEOF_WORD], xmm6 ; _mm_storeu_si128((__m128i *)(t2 + ko + 16), neg);
  134. movdqa XMMWORD [esp + t2 + (%1 + 24) * SIZEOF_WORD], xmm7 ; _mm_storeu_si128((__m128i *)(t2 + ko + 24), neg);
  135. %endmacro
  136. ;
  137. ; Encode a single block's worth of coefficients.
  138. ;
  139. ; GLOBAL(JOCTET*)
  140. ; jsimd_huff_encode_one_block_sse2 (working_state *state, JOCTET *buffer,
  141. ; JCOEFPTR block, int last_dc_val,
  142. ; c_derived_tbl *dctbl, c_derived_tbl *actbl)
  143. ;
  144. ; eax + 8 = working_state *state
  145. ; eax + 12 = JOCTET *buffer
  146. ; eax + 16 = JCOEFPTR block
  147. ; eax + 20 = int last_dc_val
  148. ; eax + 24 = c_derived_tbl *dctbl
  149. ; eax + 28 = c_derived_tbl *actbl
  150. %define pad 6*SIZEOF_DWORD ; Align to 16 bytes
  151. %define t1 pad
  152. %define t2 t1+(DCTSIZE2*SIZEOF_WORD)
  153. %define block t2+(DCTSIZE2*SIZEOF_WORD)
  154. %define actbl block+SIZEOF_DWORD
  155. %define buffer actbl+SIZEOF_DWORD
  156. %define temp buffer+SIZEOF_DWORD
  157. %define temp2 temp+SIZEOF_DWORD
  158. %define temp3 temp2+SIZEOF_DWORD
  159. %define temp4 temp3+SIZEOF_DWORD
  160. %define temp5 temp4+SIZEOF_DWORD
  161. %define gotptr temp5+SIZEOF_DWORD ; void *gotptr
  162. %define put_buffer ebx
  163. %define put_bits edi
  164. align 16
  165. global EXTN(jsimd_huff_encode_one_block_sse2)
  166. EXTN(jsimd_huff_encode_one_block_sse2):
  167. push ebp
  168. mov eax,esp ; eax = original ebp
  169. sub esp, byte 4
  170. and esp, byte (-SIZEOF_XMMWORD) ; align to 128 bits
  171. mov [esp],eax
  172. mov ebp,esp ; ebp = aligned ebp
  173. sub esp, temp5+9*SIZEOF_DWORD-pad
  174. push ebx
  175. push ecx
  176. ; push edx ; need not be preserved
  177. push esi
  178. push edi
  179. push ebp
  180. mov esi, POINTER [eax+8] ; (working_state *state)
  181. mov put_buffer, DWORD [esi+8] ; put_buffer = state->cur.put_buffer;
  182. mov put_bits, DWORD [esi+12] ; put_bits = state->cur.put_bits;
  183. push esi ; esi is now scratch
  184. get_GOT edx ; get GOT address
  185. movpic POINTER [esp+gotptr], edx ; save GOT address
  186. mov ecx, POINTER [eax+28]
  187. mov edx, POINTER [eax+16]
  188. mov esi, POINTER [eax+12]
  189. mov POINTER [esp+actbl], ecx
  190. mov POINTER [esp+block], edx
  191. mov POINTER [esp+buffer], esi
  192. ; Encode the DC coefficient difference per section F.1.2.1
  193. mov esi, POINTER [esp+block] ; block
  194. movsx ecx, word [esi] ; temp = temp2 = block[0] - last_dc_val;
  195. sub ecx, DWORD [eax+20]
  196. mov esi, ecx
  197. ; This is a well-known technique for obtaining the absolute value
  198. ; without a branch. It is derived from an assembly language technique
  199. ; presented in "How to Optimize for the Pentium Processors",
  200. ; Copyright (c) 1996, 1997 by Agner Fog.
  201. mov edx, ecx
  202. sar edx, 31 ; temp3 = temp >> (CHAR_BIT * sizeof(int) - 1);
  203. xor ecx, edx ; temp ^= temp3;
  204. sub ecx, edx ; temp -= temp3;
  205. ; For a negative input, want temp2 = bitwise complement of abs(input)
  206. ; This code assumes we are on a two's complement machine
  207. add esi, edx ; temp2 += temp3;
  208. mov DWORD [esp+temp], esi ; backup temp2 in temp
  209. ; Find the number of bits needed for the magnitude of the coefficient
  210. movpic ebp, POINTER [esp+gotptr] ; load GOT address (ebp)
  211. movzx edx, byte [GOTOFF(ebp, jpeg_nbits_table + ecx)] ; nbits = JPEG_NBITS(temp);
  212. mov DWORD [esp+temp2], edx ; backup nbits in temp2
  213. ; Emit the Huffman-coded symbol for the number of bits
  214. mov ebp, POINTER [eax+24] ; After this point, arguments are not accessible anymore
  215. mov eax, INT [ebp + edx * 4] ; code = dctbl->ehufco[nbits];
  216. movzx ecx, byte [ebp + edx + 1024] ; size = dctbl->ehufsi[nbits];
  217. EMIT_BITS eax ; EMIT_BITS(code, size)
  218. mov ecx, DWORD [esp+temp2] ; restore nbits
  219. ; Mask off any extra bits in code
  220. mov eax, 1
  221. shl eax, cl
  222. dec eax
  223. and eax, DWORD [esp+temp] ; temp2 &= (((JLONG) 1)<<nbits) - 1;
  224. ; Emit that number of bits of the value, if positive,
  225. ; or the complement of its magnitude, if negative.
  226. EMIT_BITS eax ; EMIT_BITS(temp2, nbits)
  227. ; Prepare data
  228. xor ecx, ecx
  229. mov esi, POINTER [esp+block]
  230. kloop_prepare 0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, \
  231. 18, 11, 4, 5, 12, 19, 26, 33, 40, 48, 41, 34, \
  232. 27, 20, 13, 6, 7, 14, 21, 28, 35, \
  233. xmm0, xmm1, xmm2, xmm3
  234. kloop_prepare 32, 42, 49, 56, 57, 50, 43, 36, 29, 22, 15, 23, \
  235. 30, 37, 44, 51, 58, 59, 52, 45, 38, 31, 39, 46, \
  236. 53, 60, 61, 54, 47, 55, 62, 63, 63, \
  237. xmm0, xmm1, xmm2, xmm3
  238. pxor xmm7, xmm7
  239. movdqa xmm0, XMMWORD [esp + t1 + 0 * SIZEOF_WORD] ; __m128i tmp0 = _mm_loadu_si128((__m128i *)(t1 + 0));
  240. movdqa xmm1, XMMWORD [esp + t1 + 8 * SIZEOF_WORD] ; __m128i tmp1 = _mm_loadu_si128((__m128i *)(t1 + 8));
  241. movdqa xmm2, XMMWORD [esp + t1 + 16 * SIZEOF_WORD] ; __m128i tmp2 = _mm_loadu_si128((__m128i *)(t1 + 16));
  242. movdqa xmm3, XMMWORD [esp + t1 + 24 * SIZEOF_WORD] ; __m128i tmp3 = _mm_loadu_si128((__m128i *)(t1 + 24));
  243. pcmpeqw xmm0, xmm7 ; tmp0 = _mm_cmpeq_epi16(tmp0, zero);
  244. pcmpeqw xmm1, xmm7 ; tmp1 = _mm_cmpeq_epi16(tmp1, zero);
  245. pcmpeqw xmm2, xmm7 ; tmp2 = _mm_cmpeq_epi16(tmp2, zero);
  246. pcmpeqw xmm3, xmm7 ; tmp3 = _mm_cmpeq_epi16(tmp3, zero);
  247. packsswb xmm0, xmm1 ; tmp0 = _mm_packs_epi16(tmp0, tmp1);
  248. packsswb xmm2, xmm3 ; tmp2 = _mm_packs_epi16(tmp2, tmp3);
  249. pmovmskb edx, xmm0 ; index = ((uint64_t)_mm_movemask_epi8(tmp0)) << 0;
  250. pmovmskb ecx, xmm2 ; index = ((uint64_t)_mm_movemask_epi8(tmp2)) << 16;
  251. shl ecx, 16
  252. or edx, ecx
  253. not edx ; index = ~index;
  254. lea esi, [esp+t1]
  255. mov ebp, POINTER [esp+actbl] ; ebp = actbl
  256. .BLOOP:
  257. bsf ecx, edx ; r = __builtin_ctzl(index);
  258. jz .ELOOP
  259. lea esi, [esi+ecx*2] ; k += r;
  260. shr edx, cl ; index >>= r;
  261. mov DWORD [esp+temp3], edx
  262. .BRLOOP:
  263. cmp ecx, 16 ; while (r > 15) {
  264. jl .ERLOOP
  265. sub ecx, 16 ; r -= 16;
  266. mov DWORD [esp+temp], ecx
  267. mov eax, INT [ebp + 240 * 4] ; code_0xf0 = actbl->ehufco[0xf0];
  268. movzx ecx, byte [ebp + 1024 + 240] ; size_0xf0 = actbl->ehufsi[0xf0];
  269. EMIT_BITS eax ; EMIT_BITS(code_0xf0, size_0xf0)
  270. mov ecx, DWORD [esp+temp]
  271. jmp .BRLOOP
  272. .ERLOOP:
  273. movsx eax, word [esi] ; temp = t1[k];
  274. movpic edx, POINTER [esp+gotptr] ; load GOT address (edx)
  275. movzx eax, byte [GOTOFF(edx, jpeg_nbits_table + eax)] ; nbits = JPEG_NBITS(temp);
  276. mov DWORD [esp+temp2], eax
  277. ; Emit Huffman symbol for run length / number of bits
  278. shl ecx, 4 ; temp3 = (r << 4) + nbits;
  279. add ecx, eax
  280. mov eax, INT [ebp + ecx * 4] ; code = actbl->ehufco[temp3];
  281. movzx ecx, byte [ebp + ecx + 1024] ; size = actbl->ehufsi[temp3];
  282. EMIT_BITS eax
  283. movsx edx, word [esi+DCTSIZE2*2] ; temp2 = t2[k];
  284. ; Mask off any extra bits in code
  285. mov ecx, DWORD [esp+temp2]
  286. mov eax, 1
  287. shl eax, cl
  288. dec eax
  289. and eax, edx ; temp2 &= (((JLONG) 1)<<nbits) - 1;
  290. EMIT_BITS eax ; PUT_BITS(temp2, nbits)
  291. mov edx, DWORD [esp+temp3]
  292. add esi, 2 ; ++k;
  293. shr edx, 1 ; index >>= 1;
  294. jmp .BLOOP
  295. .ELOOP:
  296. movdqa xmm0, XMMWORD [esp + t1 + 32 * SIZEOF_WORD] ; __m128i tmp0 = _mm_loadu_si128((__m128i *)(t1 + 0));
  297. movdqa xmm1, XMMWORD [esp + t1 + 40 * SIZEOF_WORD] ; __m128i tmp1 = _mm_loadu_si128((__m128i *)(t1 + 8));
  298. movdqa xmm2, XMMWORD [esp + t1 + 48 * SIZEOF_WORD] ; __m128i tmp2 = _mm_loadu_si128((__m128i *)(t1 + 16));
  299. movdqa xmm3, XMMWORD [esp + t1 + 56 * SIZEOF_WORD] ; __m128i tmp3 = _mm_loadu_si128((__m128i *)(t1 + 24));
  300. pcmpeqw xmm0, xmm7 ; tmp0 = _mm_cmpeq_epi16(tmp0, zero);
  301. pcmpeqw xmm1, xmm7 ; tmp1 = _mm_cmpeq_epi16(tmp1, zero);
  302. pcmpeqw xmm2, xmm7 ; tmp2 = _mm_cmpeq_epi16(tmp2, zero);
  303. pcmpeqw xmm3, xmm7 ; tmp3 = _mm_cmpeq_epi16(tmp3, zero);
  304. packsswb xmm0, xmm1 ; tmp0 = _mm_packs_epi16(tmp0, tmp1);
  305. packsswb xmm2, xmm3 ; tmp2 = _mm_packs_epi16(tmp2, tmp3);
  306. pmovmskb edx, xmm0 ; index = ((uint64_t)_mm_movemask_epi8(tmp0)) << 0;
  307. pmovmskb ecx, xmm2 ; index = ((uint64_t)_mm_movemask_epi8(tmp2)) << 16;
  308. shl ecx, 16
  309. or edx, ecx
  310. not edx ; index = ~index;
  311. lea eax, [esp + t1 + (DCTSIZE2/2) * 2]
  312. sub eax, esi
  313. shr eax, 1
  314. bsf ecx, edx ; r = __builtin_ctzl(index);
  315. jz .ELOOP2
  316. shr edx, cl ; index >>= r;
  317. add ecx, eax
  318. lea esi, [esi+ecx*2] ; k += r;
  319. mov DWORD [esp+temp3], edx
  320. jmp .BRLOOP2
  321. .BLOOP2:
  322. bsf ecx, edx ; r = __builtin_ctzl(index);
  323. jz .ELOOP2
  324. lea esi, [esi+ecx*2] ; k += r;
  325. shr edx, cl ; index >>= r;
  326. mov DWORD [esp+temp3], edx
  327. .BRLOOP2:
  328. cmp ecx, 16 ; while (r > 15) {
  329. jl .ERLOOP2
  330. sub ecx, 16 ; r -= 16;
  331. mov DWORD [esp+temp], ecx
  332. mov eax, INT [ebp + 240 * 4] ; code_0xf0 = actbl->ehufco[0xf0];
  333. movzx ecx, byte [ebp + 1024 + 240] ; size_0xf0 = actbl->ehufsi[0xf0];
  334. EMIT_BITS eax ; EMIT_BITS(code_0xf0, size_0xf0)
  335. mov ecx, DWORD [esp+temp]
  336. jmp .BRLOOP2
  337. .ERLOOP2:
  338. movsx eax, word [esi] ; temp = t1[k];
  339. bsr eax, eax ; nbits = 32 - __builtin_clz(temp);
  340. inc eax
  341. mov DWORD [esp+temp2], eax
  342. ; Emit Huffman symbol for run length / number of bits
  343. shl ecx, 4 ; temp3 = (r << 4) + nbits;
  344. add ecx, eax
  345. mov eax, INT [ebp + ecx * 4] ; code = actbl->ehufco[temp3];
  346. movzx ecx, byte [ebp + ecx + 1024] ; size = actbl->ehufsi[temp3];
  347. EMIT_BITS eax
  348. movsx edx, word [esi+DCTSIZE2*2] ; temp2 = t2[k];
  349. ; Mask off any extra bits in code
  350. mov ecx, DWORD [esp+temp2]
  351. mov eax, 1
  352. shl eax, cl
  353. dec eax
  354. and eax, edx ; temp2 &= (((JLONG) 1)<<nbits) - 1;
  355. EMIT_BITS eax ; PUT_BITS(temp2, nbits)
  356. mov edx, DWORD [esp+temp3]
  357. add esi, 2 ; ++k;
  358. shr edx, 1 ; index >>= 1;
  359. jmp .BLOOP2
  360. .ELOOP2:
  361. ; If the last coef(s) were zero, emit an end-of-block code
  362. lea edx, [esp + t1 + (DCTSIZE2-1) * 2] ; r = DCTSIZE2-1-k;
  363. cmp edx, esi ; if (r > 0) {
  364. je .EFN
  365. mov eax, INT [ebp] ; code = actbl->ehufco[0];
  366. movzx ecx, byte [ebp + 1024] ; size = actbl->ehufsi[0];
  367. EMIT_BITS eax
  368. .EFN:
  369. mov eax, [esp+buffer]
  370. pop esi
  371. ; Save put_buffer & put_bits
  372. mov DWORD [esi+8], put_buffer ; state->cur.put_buffer = put_buffer;
  373. mov DWORD [esi+12], put_bits ; state->cur.put_bits = put_bits;
  374. pop ebp
  375. pop edi
  376. pop esi
  377. ; pop edx ; need not be preserved
  378. pop ecx
  379. pop ebx
  380. mov esp,ebp ; esp <- aligned ebp
  381. pop esp ; esp <- original ebp
  382. pop ebp
  383. ret
  384. ; For some reason, the OS X linker does not honor the request to align the
  385. ; segment unless we do this.
  386. align 16