// File: tomfunkhouser[P]gaps/pkgs/jpeg/jidctred.c
// Function: jpeg_idct_4x4
GLOBAL(void)
jpeg_idct_4x4 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
	       JCOEFPTR coef_block,
	       JSAMPARRAY output_buf, JDIMENSION output_col)
{
  INT32 tmp0, tmp2, tmp10, tmp12;
  INT32 z1, z2, z3, z4;
  JCOEFPTR inptr;
  ISLOW_MULT_TYPE * quantptr;
  int * wsptr;
  JSAMPROW outptr;
  JSAMPLE *range_limit = IDCT_range_limit(cinfo);
  int ctr;
  int workspace[DCTSIZE*4];	/* 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 column 4, because second pass won't use it */
    if (ctr == DCTSIZE-4)
      continue;
    if (inptr[DCTSIZE*1] == 0 && inptr[DCTSIZE*2] == 0 &&
	inptr[DCTSIZE*3] == 0 && inptr[DCTSIZE*5] == 0 &&
	inptr[DCTSIZE*6] == 0 && inptr[DCTSIZE*7] == 0) {
      /* AC terms all zero; we need not examine term 4 for 4x4 output */
      int dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]) << PASS1_BITS;
      
      wsptr[DCTSIZE*0] = dcval;
      wsptr[DCTSIZE*1] = dcval;
      wsptr[DCTSIZE*2] = dcval;
      wsptr[DCTSIZE*3] = dcval;
      
      continue;
    }
    
    /* Even part */
    
    tmp0 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
    tmp0 <<= (CONST_BITS+1);
    
    z2 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]);
    z3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]);

    tmp2 = MULTIPLY(z2, FIX_1_847759065) + MULTIPLY(z3, - FIX_0_765366865);
    
    tmp10 = tmp0 + tmp2;
    tmp12 = tmp0 - tmp2;
    
    /* Odd part */
    
    z1 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]);
    z2 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]);
    z3 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]);
    z4 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]);
    
    tmp0 = MULTIPLY(z1, - FIX_0_211164243) /* sqrt(2) * (c3-c1) */
	 + MULTIPLY(z2, FIX_1_451774981) /* sqrt(2) * (c3+c7) */
	 + MULTIPLY(z3, - FIX_2_172734803) /* sqrt(2) * (-c1-c5) */
	 + MULTIPLY(z4, FIX_1_061594337); /* sqrt(2) * (c5+c7) */
    
    tmp2 = MULTIPLY(z1, - FIX_0_509795579) /* sqrt(2) * (c7-c5) */
	 + MULTIPLY(z2, - FIX_0_601344887) /* sqrt(2) * (c5-c1) */
	 + MULTIPLY(z3, FIX_0_899976223) /* sqrt(2) * (c3-c7) */
	 + MULTIPLY(z4, FIX_2_562915447); /* sqrt(2) * (c1+c3) */

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

  wsptr = workspace;
  for (ctr = 0; ctr < 4; 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[2] == 0 && wsptr[3] == 0 &&
	wsptr[5] == 0 && wsptr[6] == 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;
      outptr[2] = dcval;
      outptr[3] = dcval;
      
      wsptr += DCTSIZE;		/* advance pointer to next row */
      continue;
    }
#endif
    
    /* Even part */
    
    tmp0 = ((INT32) wsptr[0]) << (CONST_BITS+1);
    
    tmp2 = MULTIPLY((INT32) wsptr[2], FIX_1_847759065)
	 + MULTIPLY((INT32) wsptr[6], - FIX_0_765366865);
    
    tmp10 = tmp0 + tmp2;
    tmp12 = tmp0 - tmp2;
    
    /* Odd part */
    
    z1 = (INT32) wsptr[7];
    z2 = (INT32) wsptr[5];
    z3 = (INT32) wsptr[3];
    z4 = (INT32) wsptr[1];
    
    tmp0 = MULTIPLY(z1, - FIX_0_211164243) /* sqrt(2) * (c3-c1) */
	 + MULTIPLY(z2, FIX_1_451774981) /* sqrt(2) * (c3+c7) */
	 + MULTIPLY(z3, - FIX_2_172734803) /* sqrt(2) * (-c1-c5) */
	 + MULTIPLY(z4, FIX_1_061594337); /* sqrt(2) * (c5+c7) */
    
    tmp2 = MULTIPLY(z1, - FIX_0_509795579) /* sqrt(2) * (c7-c5) */
	 + MULTIPLY(z2, - FIX_0_601344887) /* sqrt(2) * (c5-c1) */
	 + MULTIPLY(z3, FIX_0_899976223) /* sqrt(2) * (c3-c7) */
	 + MULTIPLY(z4, FIX_2_562915447); /* sqrt(2) * (c1+c3) */

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

// Assembly:
subq $0xa8, %rsp
movq %rdi, 0xa0(%rsp)
movq %rsi, 0x98(%rsp)
movq %rdx, 0x90(%rsp)
movq %rcx, 0x88(%rsp)
movl %r8d, 0x84(%rsp)
movq 0xa0(%rsp), %rax
movq 0x1a8(%rax), %rax
addq $0x80, %rax
movq %rax, 0x18(%rsp)
movq 0x90(%rsp), %rax
movq %rax, 0x38(%rsp)
movq 0x98(%rsp), %rax
movq 0x58(%rax), %rax
movq %rax, 0x30(%rsp)
leaq -0x70(%rsp), %rax
movq %rax, 0x28(%rsp)
movl $0x8, 0x14(%rsp)
cmpl $0x0, 0x14(%rsp)
jle 0xf2168
cmpl $0x4, 0x14(%rsp)
jne 0xf1eb0
jmp 0xf212e
movq 0x38(%rsp), %rax
movswl 0x10(%rax), %eax
cmpl $0x0, %eax
jne 0xf1f57
movq 0x38(%rsp), %rax
movswl 0x20(%rax), %eax
cmpl $0x0, %eax
jne 0xf1f57
movq 0x38(%rsp), %rax
movswl 0x30(%rax), %eax
cmpl $0x0, %eax
jne 0xf1f57
movq 0x38(%rsp), %rax
movswl 0x50(%rax), %eax
cmpl $0x0, %eax
jne 0xf1f57
movq 0x38(%rsp), %rax
movswl 0x60(%rax), %eax
cmpl $0x0, %eax
jne 0xf1f57
movq 0x38(%rsp), %rax
movswl 0x70(%rax), %eax
cmpl $0x0, %eax
jne 0xf1f57
movq 0x38(%rsp), %rax
movswl (%rax), %eax
movq 0x30(%rsp), %rcx
imull (%rcx), %eax
shll $0x2, %eax
movl %eax, -0x74(%rsp)
movl -0x74(%rsp), %ecx
movq 0x28(%rsp), %rax
movl %ecx, (%rax)
movl -0x74(%rsp), %ecx
movq 0x28(%rsp), %rax
movl %ecx, 0x20(%rax)
movl -0x74(%rsp), %ecx
movq 0x28(%rsp), %rax
movl %ecx, 0x40(%rax)
movl -0x74(%rsp), %ecx
movq 0x28(%rsp), %rax
movl %ecx, 0x60(%rax)
jmp 0xf212e
movq 0x38(%rsp), %rax
movswl (%rax), %eax
movq 0x30(%rsp), %rcx
imull (%rcx), %eax
cltq
movq %rax, 0x78(%rsp)
movq 0x78(%rsp), %rax
shlq $0xe, %rax
movq %rax, 0x78(%rsp)
movq 0x38(%rsp), %rax
movswl 0x20(%rax), %eax
movq 0x30(%rsp), %rcx
imull 0x40(%rcx), %eax
cltq
movq %rax, 0x50(%rsp)
movq 0x38(%rsp), %rax
movswl 0x60(%rax), %eax
movq 0x30(%rsp), %rcx
imull 0xc0(%rcx), %eax
cltq
movq %rax, 0x48(%rsp)
imulq $0x3b21, 0x50(%rsp), %rax # imm = 0x3B21
imulq $-0x187e, 0x48(%rsp), %rcx # imm = 0xE782
addq %rcx, %rax
movq %rax, 0x70(%rsp)
movq 0x78(%rsp), %rax
addq 0x70(%rsp), %rax
movq %rax, 0x68(%rsp)
movq 0x78(%rsp), %rax
subq 0x70(%rsp), %rax
movq %rax, 0x60(%rsp)
movq 0x38(%rsp), %rax
movswl 0x70(%rax), %eax
movq 0x30(%rsp), %rcx
imull 0xe0(%rcx), %eax
cltq
movq %rax, 0x58(%rsp)
movq 0x38(%rsp), %rax
movswl 0x50(%rax), %eax
movq 0x30(%rsp), %rcx
imull 0xa0(%rcx), %eax
cltq
movq %rax, 0x50(%rsp)
movq 0x38(%rsp), %rax
movswl 0x30(%rax), %eax
movq 0x30(%rsp), %rcx
imull 0x60(%rcx), %eax
cltq
movq %rax, 0x48(%rsp)
movq 0x38(%rsp), %rax
movswl 0x10(%rax), %eax
movq 0x30(%rsp), %rcx
imull 0x20(%rcx), %eax
cltq
movq %rax, 0x40(%rsp)
imulq $-0x6c2, 0x58(%rsp), %rax # imm = 0xF93E
imulq $0x2e75, 0x50(%rsp), %rcx # imm = 0x2E75
addq %rcx, %rax
imulq $-0x4587, 0x48(%rsp), %rcx # imm = 0xBA79
addq %rcx, %rax
imulq $0x21f9, 0x40(%rsp), %rcx # imm = 0x21F9
addq %rcx, %rax
movq %rax, 0x78(%rsp)
imulq $-0x1050, 0x58(%rsp), %rax # imm = 0xEFB0
imulq $-0x133e, 0x50(%rsp), %rcx # imm = 0xECC2
addq %rcx, %rax
imulq $0x1ccd, 0x48(%rsp), %rcx # imm = 0x1CCD
addq %rcx, %rax
imulq $0x5203, 0x40(%rsp), %rcx # imm = 0x5203
addq %rcx, %rax
movq %rax, 0x70(%rsp)
movq 0x68(%rsp), %rax
addq 0x70(%rsp), %rax
addq $0x800, %rax            # imm = 0x800
sarq $0xc, %rax
movl %eax, %ecx
movq 0x28(%rsp), %rax
movl %ecx, (%rax)
movq 0x68(%rsp), %rax
subq 0x70(%rsp), %rax
addq $0x800, %rax            # imm = 0x800
sarq $0xc, %rax
movl %eax, %ecx
movq 0x28(%rsp), %rax
movl %ecx, 0x60(%rax)
movq 0x60(%rsp), %rax
addq 0x78(%rsp), %rax
addq $0x800, %rax            # imm = 0x800
sarq $0xc, %rax
movl %eax, %ecx
movq 0x28(%rsp), %rax
movl %ecx, 0x20(%rax)
movq 0x60(%rsp), %rax
subq 0x78(%rsp), %rax
addq $0x800, %rax            # imm = 0x800
sarq $0xc, %rax
movl %eax, %ecx
movq 0x28(%rsp), %rax
movl %ecx, 0x40(%rax)
movq 0x38(%rsp), %rax
addq $0x2, %rax
movq %rax, 0x38(%rsp)
movq 0x30(%rsp), %rax
addq $0x4, %rax
movq %rax, 0x30(%rsp)
movq 0x28(%rsp), %rax
addq $0x4, %rax
movq %rax, 0x28(%rsp)
movl 0x14(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x14(%rsp)
jmp 0xf1e99
leaq -0x70(%rsp), %rax
movq %rax, 0x28(%rsp)
movl $0x0, 0x14(%rsp)
cmpl $0x4, 0x14(%rsp)
jge 0xf2422
movq 0x88(%rsp), %rax
movslq 0x14(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movl 0x84(%rsp), %ecx
addq %rcx, %rax
movq %rax, 0x20(%rsp)
movq 0x28(%rsp), %rax
cmpl $0x0, 0x4(%rax)
jne 0xf225a
movq 0x28(%rsp), %rax
cmpl $0x0, 0x8(%rax)
jne 0xf225a
movq 0x28(%rsp), %rax
cmpl $0x0, 0xc(%rax)
jne 0xf225a
movq 0x28(%rsp), %rax
cmpl $0x0, 0x14(%rax)
jne 0xf225a
movq 0x28(%rsp), %rax
cmpl $0x0, 0x18(%rax)
jne 0xf225a
movq 0x28(%rsp), %rax
cmpl $0x0, 0x1c(%rax)
jne 0xf225a
movq 0x18(%rsp), %rax
movq 0x28(%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 0x20(%rsp), %rax
movb %cl, (%rax)
movb -0x75(%rsp), %cl
movq 0x20(%rsp), %rax
movb %cl, 0x1(%rax)
movb -0x75(%rsp), %cl
movq 0x20(%rsp), %rax
movb %cl, 0x2(%rax)
movb -0x75(%rsp), %cl
movq 0x20(%rsp), %rax
movb %cl, 0x3(%rax)
movq 0x28(%rsp), %rax
addq $0x20, %rax
movq %rax, 0x28(%rsp)
jmp 0xf2412
movq 0x28(%rsp), %rax
movslq (%rax), %rax
shlq $0xe, %rax
movq %rax, 0x78(%rsp)
movq 0x28(%rsp), %rax
movslq 0x8(%rax), %rax
imulq $0x3b21, %rax, %rax     # imm = 0x3B21
movq 0x28(%rsp), %rcx
movslq 0x18(%rcx), %rcx
imulq $-0x187e, %rcx, %rcx    # imm = 0xE782
addq %rcx, %rax
movq %rax, 0x70(%rsp)
movq 0x78(%rsp), %rax
addq 0x70(%rsp), %rax
movq %rax, 0x68(%rsp)
movq 0x78(%rsp), %rax
subq 0x70(%rsp), %rax
movq %rax, 0x60(%rsp)
movq 0x28(%rsp), %rax
movslq 0x1c(%rax), %rax
movq %rax, 0x58(%rsp)
movq 0x28(%rsp), %rax
movslq 0x14(%rax), %rax
movq %rax, 0x50(%rsp)
movq 0x28(%rsp), %rax
movslq 0xc(%rax), %rax
movq %rax, 0x48(%rsp)
movq 0x28(%rsp), %rax
movslq 0x4(%rax), %rax
movq %rax, 0x40(%rsp)
imulq $-0x6c2, 0x58(%rsp), %rax # imm = 0xF93E
imulq $0x2e75, 0x50(%rsp), %rcx # imm = 0x2E75
addq %rcx, %rax
imulq $-0x4587, 0x48(%rsp), %rcx # imm = 0xBA79
addq %rcx, %rax
imulq $0x21f9, 0x40(%rsp), %rcx # imm = 0x21F9
addq %rcx, %rax
movq %rax, 0x78(%rsp)
imulq $-0x1050, 0x58(%rsp), %rax # imm = 0xEFB0
imulq $-0x133e, 0x50(%rsp), %rcx # imm = 0xECC2
addq %rcx, %rax
imulq $0x1ccd, 0x48(%rsp), %rcx # imm = 0x1CCD
addq %rcx, %rax
imulq $0x5203, 0x40(%rsp), %rcx # imm = 0x5203
addq %rcx, %rax
movq %rax, 0x70(%rsp)
movq 0x18(%rsp), %rax
movq 0x68(%rsp), %rcx
addq 0x70(%rsp), %rcx
addq $0x40000, %rcx          # imm = 0x40000
sarq $0x13, %rcx
andl $0x3ff, %ecx            # imm = 0x3FF
movslq %ecx, %rcx
movb (%rax,%rcx), %cl
movq 0x20(%rsp), %rax
movb %cl, (%rax)
movq 0x18(%rsp), %rax
movq 0x68(%rsp), %rcx
subq 0x70(%rsp), %rcx
addq $0x40000, %rcx          # imm = 0x40000
sarq $0x13, %rcx
andl $0x3ff, %ecx            # imm = 0x3FF
movslq %ecx, %rcx
movb (%rax,%rcx), %cl
movq 0x20(%rsp), %rax
movb %cl, 0x3(%rax)
movq 0x18(%rsp), %rax
movq 0x60(%rsp), %rcx
addq 0x78(%rsp), %rcx
addq $0x40000, %rcx          # imm = 0x40000
sarq $0x13, %rcx
andl $0x3ff, %ecx            # imm = 0x3FF
movslq %ecx, %rcx
movb (%rax,%rcx), %cl
movq 0x20(%rsp), %rax
movb %cl, 0x1(%rax)
movq 0x18(%rsp), %rax
movq 0x60(%rsp), %rcx
subq 0x78(%rsp), %rcx
addq $0x40000, %rcx          # imm = 0x40000
sarq $0x13, %rcx
andl $0x3ff, %ecx            # imm = 0x3FF
movslq %ecx, %rcx
movb (%rax,%rcx), %cl
movq 0x20(%rsp), %rax
movb %cl, 0x2(%rax)
movq 0x28(%rsp), %rax
addq $0x20, %rax
movq %rax, 0x28(%rsp)
movl 0x14(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x14(%rsp)
jmp 0xf217a
addq $0xa8, %rsp
retq
nopw (%rax,%rax)