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