jchuff-sse2.asm 20 KB

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