// File: tomfunkhouser[P]gaps/pkgs/jpeg/jidctred.c
// Function: jpeg_idct_2x2
GLOBAL(void)
jpeg_idct_2x2 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
	       JCOEFPTR coef_block,
	       JSAMPARRAY output_buf, JDIMENSION output_col)
{
  INT32 tmp0, tmp10, z1;
  JCOEFPTR inptr;
  ISLOW_MULT_TYPE * quantptr;
  int * wsptr;
  JSAMPROW outptr;
  JSAMPLE *range_limit = IDCT_range_limit(cinfo);
  int ctr;
  int workspace[DCTSIZE*2];	/* buffers data between passes */
  SHIFT_TEMPS

  /* Pass 1: process columns from input, store into work array. */

  inptr = coef_block;
  quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table;
  wsptr = workspace;
  for (ctr = DCTSIZE; ctr > 0; inptr++, quantptr++, wsptr++, ctr--) {
    /* Don't bother to process columns 2,4,6 */
    if (ctr == DCTSIZE-2 || ctr == DCTSIZE-4 || ctr == DCTSIZE-6)
      continue;
    if (inptr[DCTSIZE*1] == 0 && inptr[DCTSIZE*3] == 0 &&
	inptr[DCTSIZE*5] == 0 && inptr[DCTSIZE*7] == 0) {
      /* AC terms all zero; we need not examine terms 2,4,6 for 2x2 output */
      int dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]) << PASS1_BITS;
      
      wsptr[DCTSIZE*0] = dcval;
      wsptr[DCTSIZE*1] = dcval;
      
      continue;
    }
    
    /* Even part */
    
    z1 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
    tmp10 = z1 << (CONST_BITS+2);
    
    /* Odd part */

    z1 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]);
    tmp0 = MULTIPLY(z1, - FIX_0_720959822); /* sqrt(2) * (c7-c5+c3-c1) */
    z1 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]);
    tmp0 += MULTIPLY(z1, FIX_0_850430095); /* sqrt(2) * (-c1+c3+c5+c7) */
    z1 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]);
    tmp0 += MULTIPLY(z1, - FIX_1_272758580); /* sqrt(2) * (-c1+c3-c5-c7) */
    z1 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]);
    tmp0 += MULTIPLY(z1, FIX_3_624509785); /* sqrt(2) * (c1+c3+c5+c7) */

    /* Final output stage */
    
    wsptr[DCTSIZE*0] = (int) DESCALE(tmp10 + tmp0, CONST_BITS-PASS1_BITS+2);
    wsptr[DCTSIZE*1] = (int) DESCALE(tmp10 - tmp0, CONST_BITS-PASS1_BITS+2);
  }
  
  /* Pass 2: process 2 rows from work array, store into output array. */

  wsptr = workspace;
  for (ctr = 0; ctr < 2; ctr++) {
    outptr = output_buf[ctr] + output_col;
    /* It's not clear whether a zero row test is worthwhile here ... */

#ifndef NO_ZERO_ROW_TEST
    if (wsptr[1] == 0 && wsptr[3] == 0 && wsptr[5] == 0 && wsptr[7] == 0) {
      /* AC terms all zero */
      JSAMPLE dcval = range_limit[(int) DESCALE((INT32) wsptr[0], PASS1_BITS+3)
				  & RANGE_MASK];
      
      outptr[0] = dcval;
      outptr[1] = dcval;
      
      wsptr += DCTSIZE;		/* advance pointer to next row */
      continue;
    }
#endif
    
    /* Even part */
    
    tmp10 = ((INT32) wsptr[0]) << (CONST_BITS+2);
    
    /* Odd part */

    tmp0 = MULTIPLY((INT32) wsptr[7], - FIX_0_720959822) /* sqrt(2) * (c7-c5+c3-c1) */
	 + MULTIPLY((INT32) wsptr[5], FIX_0_850430095) /* sqrt(2) * (-c1+c3+c5+c7) */
	 + MULTIPLY((INT32) wsptr[3], - FIX_1_272758580) /* sqrt(2) * (-c1+c3-c5-c7) */
	 + MULTIPLY((INT32) wsptr[1], FIX_3_624509785); /* sqrt(2) * (c1+c3+c5+c7) */

    /* Final output stage */
    
    outptr[0] = range_limit[(int) DESCALE(tmp10 + tmp0,
					  CONST_BITS+PASS1_BITS+3+2)
			    & RANGE_MASK];
    outptr[1] = range_limit[(int) DESCALE(tmp10 - tmp0,
					  CONST_BITS+PASS1_BITS+3+2)
			    & RANGE_MASK];
    
    wsptr += DCTSIZE;		/* advance pointer to next row */
  }
}

// Assembly:
subq $0x48, %rsp
movq %rdi, 0x40(%rsp)
movq %rsi, 0x38(%rsp)
movq %rdx, 0x30(%rsp)
movq %rcx, 0x28(%rsp)
movl %r8d, 0x24(%rsp)
movq 0x40(%rsp), %rax
movq 0x1a8(%rax), %rax
addq $0x80, %rax
movq %rax, -0x20(%rsp)
movq 0x30(%rsp), %rax
movq %rax, (%rsp)
movq 0x38(%rsp), %rax
movq 0x58(%rax), %rax
movq %rax, -0x8(%rsp)
leaq -0x70(%rsp), %rax
movq %rax, -0x10(%rsp)
movl $0x8, -0x24(%rsp)
cmpl $0x0, -0x24(%rsp)
jle 0xf265c
cmpl $0x6, -0x24(%rsp)
je 0xf24ad
cmpl $0x4, -0x24(%rsp)
je 0xf24ad
cmpl $0x2, -0x24(%rsp)
jne 0xf24b2
jmp 0xf2624
movq (%rsp), %rax
movswl 0x10(%rax), %eax
cmpl $0x0, %eax
jne 0xf2518
movq (%rsp), %rax
movswl 0x30(%rax), %eax
cmpl $0x0, %eax
jne 0xf2518
movq (%rsp), %rax
movswl 0x50(%rax), %eax
cmpl $0x0, %eax
jne 0xf2518
movq (%rsp), %rax
movswl 0x70(%rax), %eax
cmpl $0x0, %eax
jne 0xf2518
movq (%rsp), %rax
movswl (%rax), %eax
movq -0x8(%rsp), %rcx
imull (%rcx), %eax
shll $0x2, %eax
movl %eax, -0x74(%rsp)
movl -0x74(%rsp), %ecx
movq -0x10(%rsp), %rax
movl %ecx, (%rax)
movl -0x74(%rsp), %ecx
movq -0x10(%rsp), %rax
movl %ecx, 0x20(%rax)
jmp 0xf2624
movq (%rsp), %rax
movswl (%rax), %eax
movq -0x8(%rsp), %rcx
imull (%rcx), %eax
cltq
movq %rax, 0x8(%rsp)
movq 0x8(%rsp), %rax
shlq $0xf, %rax
movq %rax, 0x10(%rsp)
movq (%rsp), %rax
movswl 0x70(%rax), %eax
movq -0x8(%rsp), %rcx
imull 0xe0(%rcx), %eax
cltq
movq %rax, 0x8(%rsp)
imulq $-0x1712, 0x8(%rsp), %rax # imm = 0xE8EE
movq %rax, 0x18(%rsp)
movq (%rsp), %rax
movswl 0x50(%rax), %eax
movq -0x8(%rsp), %rcx
imull 0xa0(%rcx), %eax
cltq
movq %rax, 0x8(%rsp)
imulq $0x1b37, 0x8(%rsp), %rax # imm = 0x1B37
addq 0x18(%rsp), %rax
movq %rax, 0x18(%rsp)
movq (%rsp), %rax
movswl 0x30(%rax), %eax
movq -0x8(%rsp), %rcx
imull 0x60(%rcx), %eax
cltq
movq %rax, 0x8(%rsp)
imulq $-0x28ba, 0x8(%rsp), %rax # imm = 0xD746
addq 0x18(%rsp), %rax
movq %rax, 0x18(%rsp)
movq (%rsp), %rax
movswl 0x10(%rax), %eax
movq -0x8(%rsp), %rcx
imull 0x20(%rcx), %eax
cltq
movq %rax, 0x8(%rsp)
imulq $0x73fc, 0x8(%rsp), %rax # imm = 0x73FC
addq 0x18(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x10(%rsp), %rax
addq 0x18(%rsp), %rax
addq $0x1000, %rax           # imm = 0x1000
sarq $0xd, %rax
movl %eax, %ecx
movq -0x10(%rsp), %rax
movl %ecx, (%rax)
movq 0x10(%rsp), %rax
subq 0x18(%rsp), %rax
addq $0x1000, %rax           # imm = 0x1000
sarq $0xd, %rax
movl %eax, %ecx
movq -0x10(%rsp), %rax
movl %ecx, 0x20(%rax)
movq (%rsp), %rax
addq $0x2, %rax
movq %rax, (%rsp)
movq -0x8(%rsp), %rax
addq $0x4, %rax
movq %rax, -0x8(%rsp)
movq -0x10(%rsp), %rax
addq $0x4, %rax
movq %rax, -0x10(%rsp)
movl -0x24(%rsp), %eax
addl $-0x1, %eax
movl %eax, -0x24(%rsp)
jmp 0xf248d
leaq -0x70(%rsp), %rax
movq %rax, -0x10(%rsp)
movl $0x0, -0x24(%rsp)
cmpl $0x2, -0x24(%rsp)
jge 0xf27e6
movq 0x28(%rsp), %rax
movslq -0x24(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movl 0x24(%rsp), %ecx
addq %rcx, %rax
movq %rax, -0x18(%rsp)
movq -0x10(%rsp), %rax
cmpl $0x0, 0x4(%rax)
jne 0xf270e
movq -0x10(%rsp), %rax
cmpl $0x0, 0xc(%rax)
jne 0xf270e
movq -0x10(%rsp), %rax
cmpl $0x0, 0x14(%rax)
jne 0xf270e
movq -0x10(%rsp), %rax
cmpl $0x0, 0x1c(%rax)
jne 0xf270e
movq -0x20(%rsp), %rax
movq -0x10(%rsp), %rcx
movslq (%rcx), %rcx
addq $0x10, %rcx
sarq $0x5, %rcx
andl $0x3ff, %ecx            # imm = 0x3FF
movslq %ecx, %rcx
movb (%rax,%rcx), %al
movb %al, -0x75(%rsp)
movb -0x75(%rsp), %cl
movq -0x18(%rsp), %rax
movb %cl, (%rax)
movb -0x75(%rsp), %cl
movq -0x18(%rsp), %rax
movb %cl, 0x1(%rax)
movq -0x10(%rsp), %rax
addq $0x20, %rax
movq %rax, -0x10(%rsp)
jmp 0xf27d6
movq -0x10(%rsp), %rax
movslq (%rax), %rax
shlq $0xf, %rax
movq %rax, 0x10(%rsp)
movq -0x10(%rsp), %rax
movslq 0x1c(%rax), %rax
imulq $-0x1712, %rax, %rax    # imm = 0xE8EE
movq -0x10(%rsp), %rcx
movslq 0x14(%rcx), %rcx
imulq $0x1b37, %rcx, %rcx     # imm = 0x1B37
addq %rcx, %rax
movq -0x10(%rsp), %rcx
movslq 0xc(%rcx), %rcx
imulq $-0x28ba, %rcx, %rcx    # imm = 0xD746
addq %rcx, %rax
movq -0x10(%rsp), %rcx
movslq 0x4(%rcx), %rcx
imulq $0x73fc, %rcx, %rcx     # imm = 0x73FC
addq %rcx, %rax
movq %rax, 0x18(%rsp)
movq -0x20(%rsp), %rax
movq 0x10(%rsp), %rcx
addq 0x18(%rsp), %rcx
addq $0x80000, %rcx          # imm = 0x80000
sarq $0x14, %rcx
andl $0x3ff, %ecx            # imm = 0x3FF
movslq %ecx, %rcx
movb (%rax,%rcx), %cl
movq -0x18(%rsp), %rax
movb %cl, (%rax)
movq -0x20(%rsp), %rax
movq 0x10(%rsp), %rcx
subq 0x18(%rsp), %rcx
addq $0x80000, %rcx          # imm = 0x80000
sarq $0x14, %rcx
andl $0x3ff, %ecx            # imm = 0x3FF
movslq %ecx, %rcx
movb (%rax,%rcx), %cl
movq -0x18(%rsp), %rax
movb %cl, 0x1(%rax)
movq -0x10(%rsp), %rax
addq $0x20, %rax
movq %rax, -0x10(%rsp)
movl -0x24(%rsp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rsp)
jmp 0xf266e
addq $0x48, %rsp
retq
nopl (%rax,%rax)