encode_x86_64.S 12.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/* 
 *  encode_x86.S
 *
 *     Copyright (C) Peter Schlaile - February 2001
 *
 *  This file is part of libdv, a free DV (IEC 61834/SMPTE 314M)
 *  codec.
 *
 *  libdv is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU Lesser Public License as published by
 *  the Free Software Foundation; either version 2.1, or (at your
 *  option) any later version.
 *   
 *  libdv is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser Public License for more details.
 *   
 *  You should have received a copy of the GNU Lesser Public License
 *  along with libdv; see the file COPYING.  If not, write to
 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
 *
 *  The libdv homepage is http://libdv.sourceforge.net/.  
 */

.data
ALLONE:		.word 1,1,1,1
VLCADDMASK:	.byte 255,0,0,0,255,0,0,0
		
30 31
.section .note.GNU-stack, "", @progbits

32 33 34
.text

.global _dv_vlc_encode_block_mmx_x86_64
35 36
.hidden _dv_vlc_encode_block_mmx_x86_64
.type   _dv_vlc_encode_block_mmx_x86_64,@function
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
_dv_vlc_encode_block_mmx_x86_64:
	
/* extern unsigned long _dv_vlc_encode_block_mmx(dv_coeff_t* coeffs,
			                	 dv_vlc_entry_t ** out); */

	/* arguments are coeffs=rdi, out=rsi */
	
	push	%rbx
	push	%rbp

	xor	%rax,%rax
	mov	%rsi,%rdx 
	mov	(%rdx),%rdx                      # o = out->coeffs
	add	$2, %rdi                         # skip the first entry, DC coeff

	mov	$63, %rcx                        # loop counter

54 55
	mov	vlc_encode_lookup@GOTPCREL(%rip), %r11
	mov	(%r11),%r11
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119

	pxor	%mm0, %mm0
	pxor	%mm2, %mm2
	movq	VLCADDMASK(%rip), %mm1
	xor	%rbp, %rbp
	sub	$8, %rdx
vlc_encode_block_mmx_loop:
	pand	%mm1, %mm0
	movw	(%rdi), %ax                     /* amp = *z */
	add	$8, %rdx                        /* point to next vlc entry */
	paddd	%mm0, %mm2
	cmpw	$0, %ax
	jz	vlc_encode_block_amp_zero
	
	addw	$255, %ax
	add	$2, %rdi                        /* z++ */
	movq	(%r11, %rax, 8), %mm0           /* get the vlc code from the table */
	movq	%mm0, (%rdx)
	dec	%rcx
	jnz	vlc_encode_block_mmx_loop
	
	pand	%mm1, %mm0
	paddd	%mm0, %mm2
	jmp     vlc_encode_block_out
	
vlc_encode_block_amp_zero:
        mov     %rcx, %rbp
        inc     %rcx
        repz    scasw                 /* scans for eax (zero) up to rcx times, increments rdi */
        jecxz   vlc_encode_block_out

	/* inline vlc_encode(int run, int amp, int sign, dv_vlc_entry_t * o) */
	/* vlc_encode_lookup + 2 * ((amp + 255) | (run << 9)); */
        movw    -2(%rdi), %ax
        sub     %rcx, %rbp                /* the run length of zeros */
	addw	$255, %ax
        shl     $9, %rbp
	or	%rbp, %rax
	
	movq	(%r11, %rax, 8), %mm0     /* vlc_encode_lookup + 2 * (...) */
	movq	%mm0, (%rdx)
	
	dec	%rcx
	jnz	vlc_encode_block_mmx_loop

	pand	%mm1, %mm0
	paddd	%mm0, %mm2
	
vlc_encode_block_out:
	movq	%mm2, %mm0
	psrlq	$32, %mm0
	paddd	%mm0, %mm2

	mov	%rsi, %rbx             /* update the output pointer */
	mov	%rdx, (%rbx)
	
	movd	%mm2, %rax             # return value, number of bits
	
	pop	%rbp
	pop	%rbx
	
	ret	

.global _dv_vlc_num_bits_block_x86_64
120 121
.hidden _dv_vlc_num_bits_block_x86_64
.type   _dv_vlc_num_bits_block_x86_64,@function
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
_dv_vlc_num_bits_block_x86_64:
	
	/* extern unsigned long _dv_vlc_num_bits_block_x86_64(dv_coeff_t* coeffs); */

	/* arguments are coeffs=rdi */
	
	push	%rbx
	push	%rbp

	xor	%rax, %rax
	xor	%rdx, %rdx
	xor	%rbx, %rbx
	xor	%rbp, %rbp
	
	add	$2, %rdi           /* skip the DC coefficient */
	mov	$63, %rcx

139 140
	mov	vlc_num_bits_lookup@GOTPCREL(%rip), %r11
	mov	(%r11),%r11
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
	
vlc_num_bits_block_x86_loop:
	movw	(%rdi), %ax        /* get the next coeffiecient */
	add	%rbx, %rdx         /* accumulate the number of VLC bits */
	cmpw	$0, %ax
	jz	vlc_num_bits_block_amp_zero
	addw	$255, %ax
	add	$2, %rdi
	movb	(%r11, %rax), %bl  /* lookup the number of VLC bits in the table */
	
	dec	%rcx
	jnz	vlc_num_bits_block_x86_loop
	
	add	%rbx, %rdx         /* accumulate the number of VLC bits */
	jmp	vlc_num_bits_block_out
	
vlc_num_bits_block_amp_zero:
        mov     %rcx, %rbp
        inc     %rcx
        repz    scasw              /* scans for eax (zero) up to rcx times, increments rdi */
        jecxz   vlc_num_bits_block_out
	
        sub     %rcx, %rbp         /* The run length of zero coefficients */
        movw    -2(%rdi), %ax      /* The first nonzero coefficient */
        shl     $9, %rbp
	addw	$255, %ax
	or 	%rbp, %rax
	movb	(%r11, %rax), %bl  /* lookup the number of VLC bits in the table */
	
	dec	%rcx
	jnz	vlc_num_bits_block_x86_loop
	add	%rbx, %rdx
	
vlc_num_bits_block_out:
	mov	%rdx, %rax              # return value
		
	pop	%rbp
	pop	%rbx
	ret	

.global _dv_vlc_encode_block_pass_1_x86_64
182 183
.hidden _dv_vlc_encode_block_pass_1_x86_64
.type   _dv_vlc_encode_block_pass_1_x86_64,@function
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
_dv_vlc_encode_block_pass_1_x86_64:

/*
extern void _dv_vlc_encode_block_pass_1_x86_64(dv_vlc_entry_t** start,        rdi
					dv_vlc_entry_t*         end,          rsi
					long*                   bit_budget,   rdx
					long*                   bit_offset,   rcx
					unsigned char*          vsbuffer);    r8
*/
	
	push	%rbx
	push	%r12
	push	%rbp
	push	%r15
	push	%rsi
	
	mov	%rsi, %r12		   # end
	mov	(%rdi), %rsi               # *start
	mov	(%rdx), %r15               # local bit_budget = *bit_budget
	mov	(%rcx), %ebp		   # *bit_offset
	mov	%rcx, %r11
	xor	%rcx,%rcx
	xor	%r10, %r10
	xor	%rax,%rax
	
vlc_encode_block_pass_1_x86_loop:	
	lodsl	(%rsi),%eax
	mov	%al, %cl                 # len
	
	sub	%rcx, %r15               # local bit_budget -= len
	jl	vlc_encode_block_pass1_x86_out

	mov	%rbp, %rbx               # bit_offset
	neg	%ecx                     # (-len)
	
	and	$7, %rbx                 # bit_offset & 7
	add	$32, %ecx                # 32-len
	
	movb	%al, %r10b               # len
	sub	%rbx, %rcx               # 32-len-(bit_offset & 7)
	
	shr	$8, %eax                 # value
	mov	%rbp, %rbx               # bit_offset
	
	shl	%cl, %eax                # value <<= 32-len-(bit_offset & 7)
	shr	$3, %rbx                 # bit_offset >> 3
	
	bswap	%eax
	add	%r8, %rbx                # vsbuffer + bit_offset >> 3
	
	add	%r10, %rbp               # bit_offset += len
	or	%eax, (%rbx)             # store value
		
	cmp	%rsi, %r12
	jnz	vlc_encode_block_pass_1_x86_loop
	
	xor	%ecx, %ecx
	add	$4, %rsi
	
vlc_encode_block_pass1_x86_out:
	sub	$4, %rsi
	add	%r15, %rcx             # + local bit_budget

	mov	%rsi, (%rdi)           # *start =
	
	mov	%ecx, (%rdx)          # *bit_budget =

	mov	%r11, %rcx
	mov	%ebp, (%rcx)           # *bit_offset =

	pop	%rsi
	pop	%r15
	pop	%rbp	
	pop	%r12
	pop	%rbx	
	ret		
		
.global _dv_classify_mmx_x86_64
262 263
.hidden _dv_classify_mmx_x86_64
.type   _dv_classify_mmx_x86_64,@function
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
_dv_classify_mmx_x86_64:

	/* extern int _dv_classify_mmx_x86_64(dv_coeff_t *    a,          rdi
	                                      unsigned short* amp_ofs,    rsi
		                              unsigned short* amp_cmp);   rdx
	*/
	
	mov	%rsi, %r11
	movq	(%r11), %mm7            # amp_ofs
	mov	%rdx, %r11
	movq	(%r11), %mm6            # amp_cmp

	mov     %rdi, %r11              # a

	movq	%mm7, %mm5
	movq	%mm6, %mm4
	
	pxor	%mm3, %mm3
	pxor	%mm2, %mm2

	movq	0*8(%r11), %mm0
	movq	1*8(%r11), %mm1
	
	paddw	%mm7, %mm0
	paddw	%mm5, %mm1
	pcmpgtw	%mm6, %mm0
	pcmpgtw %mm4, %mm1
	paddw	%mm0, %mm3 
	paddw	%mm1, %mm2 

	movq	2*8(%r11), %mm0
	movq	3*8(%r11), %mm1
	paddw	%mm7, %mm0
	paddw	%mm5, %mm1
	pcmpgtw	%mm6, %mm0
	pcmpgtw %mm4, %mm1
	paddw	%mm0, %mm3 
	paddw	%mm1, %mm2 

	movq	4*8(%r11), %mm0
	movq	5*8(%r11), %mm1
	paddw	%mm7, %mm0
	paddw	%mm5, %mm1
	pcmpgtw	%mm6, %mm0
	pcmpgtw %mm4, %mm1
	paddw	%mm0, %mm3 
	paddw	%mm1, %mm2 

	movq	6*8(%r11), %mm0
	movq	7*8(%r11), %mm1
	paddw	%mm7, %mm0
	paddw	%mm5, %mm1
	pcmpgtw	%mm6, %mm0
	pcmpgtw %mm4, %mm1
	paddw	%mm0, %mm3 
	paddw	%mm1, %mm2 

	movq	8*8(%r11), %mm0
	movq	9*8(%r11), %mm1
	paddw	%mm7, %mm0
	paddw	%mm5, %mm1
	pcmpgtw	%mm6, %mm0
	pcmpgtw %mm4, %mm1
	paddw	%mm0, %mm3 
	paddw	%mm1, %mm2 

	movq	10*8(%r11), %mm0
	movq	11*8(%r11), %mm1
	paddw	%mm7, %mm0
	paddw	%mm5, %mm1
	pcmpgtw	%mm6, %mm0
	pcmpgtw %mm4, %mm1
	paddw	%mm0, %mm3 
	paddw	%mm1, %mm2 

	movq	12*8(%r11), %mm0
	movq	13*8(%r11), %mm1
	paddw	%mm7, %mm0
	paddw	%mm5, %mm1
	pcmpgtw	%mm6, %mm0
	pcmpgtw %mm4, %mm1
	paddw	%mm0, %mm3 
	paddw	%mm1, %mm2 

	movq	14*8(%r11), %mm0
	movq	15*8(%r11), %mm1
	paddw	%mm7, %mm0
	paddw	%mm5, %mm1
	pcmpgtw	%mm6, %mm0
	pcmpgtw %mm4, %mm1
	paddw	%mm0, %mm3 
	paddw	%mm1, %mm2 
							
	paddw	%mm2, %mm3
	packsswb %mm3, %mm3

	movd	%mm3, %rax         # retun value

	ret

/* FIXME: _dv_reorder_block_mmx isn't really _that_ faster than the C version... 
	 don't know why... */
	
.global _dv_reorder_block_mmx_x86_64
368 369
.hidden _dv_reorder_block_mmx_x86_64
.type   _dv_reorder_block_mmx_x86_64,@function
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
_dv_reorder_block_mmx_x86_64:
	
/*extern int _dv_reorder_block_mmx_x86_64(dv_coeff_t *          a,               rdi
		                          const unsigned short* reorder_table);  rsi
*/
				
	push	%rbp
	push	%r12
	push	%rbx
	push	%rcx

	mov	%rdi, %r11      # a
	mov	%rsi, %r12      # reorder_table

	xor	%rbp, %rbp
	xor	%rax, %rax
	xor	%rbx, %rbx
	xor	%rcx, %rcx
	xor	%rdx, %rdx

	sub	$128, %rsp      # allocate some stack space, the redzone
	
reorder_loop:	
	
	movw	 (%r11, %rbp), %ax
	movw	2(%r11, %rbp), %bx

	movw	 (%r12, %rbp), %cx
	movw	2(%r12, %rbp), %dx

	movw	 %ax, (%rsp,%rcx)
	movw	 %bx, (%rsp,%rdx)

	movw	4(%r11, %rbp), %ax
	movw	6(%r11, %rbp), %bx

	movw	4(%r12, %rbp), %cx
	movw	6(%r12, %rbp), %dx

	movw	 %ax, (%rsp,%rcx)
	movw	 %bx, (%rsp,%rdx)

	movw	 8(%r11, %rbp), %ax
	movw	10(%r11, %rbp), %bx

	movw	8(%r12, %rbp), %cx
	movw	10(%r12, %rbp), %dx

	movw	 %ax, (%rsp,%rcx)
	movw	 %bx, (%rsp,%rdx)

	movw	12(%r11, %rbp), %ax
	movw	14(%r11, %rbp), %bx

	movw	12(%r12, %rbp), %cx
	movw	14(%r12, %rbp), %dx

	movw	 %ax, (%rsp,%rcx)
	movw	 %bx, (%rsp,%rdx)

	add	$16, %rbp
	
	cmp	$128, %rbp
	jne	reorder_loop
	
	movq	(%rsp)  , %mm0
	movq	8(%rsp) , %mm1
	movq	16(%rsp), %mm2
	movq	24(%rsp), %mm3

	movq	%mm0, (%r11)
	movq	%mm1, 8(%r11)
	movq	%mm2, 16(%r11)
	movq	%mm3, 24(%r11)

	movq	32(%rsp)   , %mm0
	movq	32+8(%rsp) , %mm1
	movq	32+16(%rsp), %mm2
	movq	32+24(%rsp), %mm3

	movq	%mm0, 32(%r11)
	movq	%mm1, 32+8(%r11)
	movq	%mm2, 32+16(%r11)
	movq	%mm3, 32+24(%r11)

	movq	64(%rsp)   , %mm0
	movq	64+8(%rsp) , %mm1
	movq	64+16(%rsp), %mm2
	movq	64+24(%rsp), %mm3

	movq	%mm0, 64(%r11)
	movq	%mm1, 64+8(%r11)
	movq	%mm2, 64+16(%r11)
	movq	%mm3, 64+24(%r11)

	movq	96(%rsp)   , %mm0
	movq	96+8(%rsp) , %mm1
	movq	96+16(%rsp), %mm2
	movq	96+24(%rsp), %mm3

	add	$128, %rsp             # restore the stack
	
	movq	%mm0, 96(%r11)
	movq	%mm1, 96+8(%r11)
	movq	%mm2, 96+16(%r11)
	movq	%mm3, 96+24(%r11)
	
	pop	%rcx
	pop	%rbx
	pop	%r12
	pop	%rbp
	ret

.global _dv_need_dct_248_mmx_x86_64_rows
484 485
.hidden _dv_need_dct_248_mmx_x86_64_rows
.type   _dv_need_dct_248_mmx_x86_64_rows,@function
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
_dv_need_dct_248_mmx_x86_64_rows:
	
	/* extern int _dv_need_dct_248_mmx_x86_64_rows(dv_coeff_t * bl);  rdi */

	mov	%rdi, %r11      # bl

	movq	(0*8+0)*2(%r11), %mm0
	movq	(0*8+4)*2(%r11), %mm1
	psubw	(1*8+0)*2(%r11), %mm0
	psubw	(1*8+4)*2(%r11), %mm1
	movq	%mm0, %mm2
	movq	%mm1, %mm3
	psraw	$15, %mm2
	psraw	$15, %mm3
	pxor	%mm2, %mm0
	pxor	%mm3, %mm1
	psubw	%mm2, %mm0
	psubw	%mm3, %mm1
	
	movq	(1*8+0)*2(%r11), %mm4
	movq	(1*8+4)*2(%r11), %mm5
	psubw	(2*8+0)*2(%r11), %mm4
	psubw	(2*8+4)*2(%r11), %mm5
	movq	%mm4, %mm6
	movq	%mm5, %mm7
	psraw	$15, %mm6
	psraw	$15, %mm7
	pxor	%mm6, %mm4
	pxor	%mm7, %mm5
	psubw	%mm6, %mm4
	psubw	%mm7, %mm5

	paddw	%mm4, %mm0
	paddw	%mm5, %mm1

	movq	(2*8+0)*2(%r11), %mm4
	movq	(2*8+4)*2(%r11), %mm5
	psubw	(3*8+0)*2(%r11), %mm4
	psubw	(3*8+4)*2(%r11), %mm5
	movq	%mm4, %mm6
	movq	%mm5, %mm7
	psraw	$15, %mm6
	psraw	$15, %mm7
	pxor	%mm6, %mm4
	pxor	%mm7, %mm5
	psubw	%mm6, %mm4
	psubw	%mm7, %mm5

	paddw	%mm4, %mm0
	paddw	%mm5, %mm1

	movq	(3*8+0)*2(%r11), %mm4
	movq	(3*8+4)*2(%r11), %mm5
	psubw	(4*8+0)*2(%r11), %mm4
	psubw	(4*8+4)*2(%r11), %mm5
	movq	%mm4, %mm6
	movq	%mm5, %mm7
	psraw	$15, %mm6
	psraw	$15, %mm7
	pxor	%mm6, %mm4
	pxor	%mm7, %mm5
	psubw	%mm6, %mm4
	psubw	%mm7, %mm5

	paddw	%mm4, %mm0
	paddw	%mm5, %mm1

	movq	(4*8+0)*2(%r11), %mm4
	movq	(4*8+4)*2(%r11), %mm5
	psubw	(5*8+0)*2(%r11), %mm4
	psubw	(5*8+4)*2(%r11), %mm5
	movq	%mm4, %mm6
	movq	%mm5, %mm7
	psraw	$15, %mm6
	psraw	$15, %mm7
	pxor	%mm6, %mm4
	pxor	%mm7, %mm5
	psubw	%mm6, %mm4
	psubw	%mm7, %mm5

	paddw	%mm4, %mm0
	paddw	%mm5, %mm1

	movq	(5*8+0)*2(%r11), %mm4
	movq	(5*8+4)*2(%r11), %mm5
	psubw	(6*8+0)*2(%r11), %mm4
	psubw	(6*8+4)*2(%r11), %mm5
	movq	%mm4, %mm6
	movq	%mm5, %mm7
	psraw	$15, %mm6
	psraw	$15, %mm7
	pxor	%mm6, %mm4
	pxor	%mm7, %mm5
	psubw	%mm6, %mm4
	psubw	%mm7, %mm5

	paddw	%mm4, %mm0
	paddw	%mm5, %mm1

	movq	(6*8+0)*2(%r11), %mm4
	movq	(6*8+4)*2(%r11), %mm5
	psubw	(7*8+0)*2(%r11), %mm4
	psubw	(7*8+4)*2(%r11), %mm5
	movq	%mm4, %mm6
	movq	%mm5, %mm7
	psraw	$15, %mm6
	psraw	$15, %mm7
	pxor	%mm6, %mm4
	pxor	%mm7, %mm5
	psubw	%mm6, %mm4
	psubw	%mm7, %mm5

	paddw	%mm4, %mm0
	paddw	%mm5, %mm1

	paddw	%mm1, %mm0
	
	pmaddwd	ALLONE(%rip), %mm0	
	movq	%mm0, %mm1
	psrlq	$32, %mm1
	paddd	%mm1, %mm0
	
	movd	%mm0, %rax

	ret