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