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