// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/jpeg/jquant2.c
// Function: pass2_fs_dither
METHODDEF(void)
pass2_fs_dither (j_decompress_ptr cinfo,
		 JSAMPARRAY input_buf, JSAMPARRAY output_buf, int num_rows)
/* This version performs Floyd-Steinberg dithering */
{
  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
  hist3d histogram = cquantize->histogram;
  register LOCFSERROR cur0, cur1, cur2;	/* current error or pixel value */
  LOCFSERROR belowerr0, belowerr1, belowerr2; /* error for pixel below cur */
  LOCFSERROR bpreverr0, bpreverr1, bpreverr2; /* error for below/prev col */
  register FSERRPTR errorptr;	/* => fserrors[] at column before current */
  JSAMPROW inptr;		/* => current input pixel */
  JSAMPROW outptr;		/* => current output pixel */
  histptr cachep;
  int dir;			/* +1 or -1 depending on direction */
  int dir3;			/* 3*dir, for advancing inptr & errorptr */
  int row;
  JDIMENSION col;
  JDIMENSION width = cinfo->output_width;
  JSAMPLE *range_limit = cinfo->sample_range_limit;
  int *error_limit = cquantize->error_limiter;
  JSAMPROW colormap0 = cinfo->colormap[0];
  JSAMPROW colormap1 = cinfo->colormap[1];
  JSAMPROW colormap2 = cinfo->colormap[2];
  SHIFT_TEMPS

  for (row = 0; row < num_rows; row++) {
    inptr = input_buf[row];
    outptr = output_buf[row];
    if (cquantize->on_odd_row) {
      /* work right to left in this row */
      inptr += (width-1) * 3;	/* so point to rightmost pixel */
      outptr += width-1;
      dir = -1;
      dir3 = -3;
      errorptr = cquantize->fserrors + (width+1)*3; /* => entry after last column */
      cquantize->on_odd_row = FALSE; /* flip for next time */
    } else {
      /* work left to right in this row */
      dir = 1;
      dir3 = 3;
      errorptr = cquantize->fserrors; /* => entry before first real column */
      cquantize->on_odd_row = TRUE; /* flip for next time */
    }
    /* Preset error values: no error propagated to first pixel from left */
    cur0 = cur1 = cur2 = 0;
    /* and no error propagated to row below yet */
    belowerr0 = belowerr1 = belowerr2 = 0;
    bpreverr0 = bpreverr1 = bpreverr2 = 0;

    for (col = width; col > 0; col--) {
      /* curN holds the error propagated from the previous pixel on the
       * current line.  Add the error propagated from the previous line
       * to form the complete error correction term for this pixel, and
       * round the error term (which is expressed * 16) to an integer.
       * RIGHT_SHIFT rounds towards minus infinity, so adding 8 is correct
       * for either sign of the error value.
       * Note: errorptr points to *previous* column's array entry.
       */
      cur0 = RIGHT_SHIFT(cur0 + errorptr[dir3+0] + 8, 4);
      cur1 = RIGHT_SHIFT(cur1 + errorptr[dir3+1] + 8, 4);
      cur2 = RIGHT_SHIFT(cur2 + errorptr[dir3+2] + 8, 4);
      /* Limit the error using transfer function set by init_error_limit.
       * See comments with init_error_limit for rationale.
       */
      cur0 = error_limit[cur0];
      cur1 = error_limit[cur1];
      cur2 = error_limit[cur2];
      /* Form pixel value + error, and range-limit to 0..MAXJSAMPLE.
       * The maximum error is +- MAXJSAMPLE (or less with error limiting);
       * this sets the required size of the range_limit array.
       */
      cur0 += GETJSAMPLE(inptr[0]);
      cur1 += GETJSAMPLE(inptr[1]);
      cur2 += GETJSAMPLE(inptr[2]);
      cur0 = GETJSAMPLE(range_limit[cur0]);
      cur1 = GETJSAMPLE(range_limit[cur1]);
      cur2 = GETJSAMPLE(range_limit[cur2]);
      /* Index into the cache with adjusted pixel value */
      cachep = & histogram[cur0>>C0_SHIFT][cur1>>C1_SHIFT][cur2>>C2_SHIFT];
      /* If we have not seen this color before, find nearest colormap */
      /* entry and update the cache */
      if (*cachep == 0)
	fill_inverse_cmap(cinfo, cur0>>C0_SHIFT,cur1>>C1_SHIFT,cur2>>C2_SHIFT);
      /* Now emit the colormap index for this cell */
      { register int pixcode = *cachep - 1;
	*outptr = (JSAMPLE) pixcode;
	/* Compute representation error for this pixel */
	cur0 -= GETJSAMPLE(colormap0[pixcode]);
	cur1 -= GETJSAMPLE(colormap1[pixcode]);
	cur2 -= GETJSAMPLE(colormap2[pixcode]);
      }
      /* Compute error fractions to be propagated to adjacent pixels.
       * Add these into the running sums, and simultaneously shift the
       * next-line error sums left by 1 column.
       */
      { register LOCFSERROR bnexterr, delta;

	bnexterr = cur0;	/* Process component 0 */
	delta = cur0 * 2;
	cur0 += delta;		/* form error * 3 */
	errorptr[0] = (FSERROR) (bpreverr0 + cur0);
	cur0 += delta;		/* form error * 5 */
	bpreverr0 = belowerr0 + cur0;
	belowerr0 = bnexterr;
	cur0 += delta;		/* form error * 7 */
	bnexterr = cur1;	/* Process component 1 */
	delta = cur1 * 2;
	cur1 += delta;		/* form error * 3 */
	errorptr[1] = (FSERROR) (bpreverr1 + cur1);
	cur1 += delta;		/* form error * 5 */
	bpreverr1 = belowerr1 + cur1;
	belowerr1 = bnexterr;
	cur1 += delta;		/* form error * 7 */
	bnexterr = cur2;	/* Process component 2 */
	delta = cur2 * 2;
	cur2 += delta;		/* form error * 3 */
	errorptr[2] = (FSERROR) (bpreverr2 + cur2);
	cur2 += delta;		/* form error * 5 */
	bpreverr2 = belowerr2 + cur2;
	belowerr2 = bnexterr;
	cur2 += delta;		/* form error * 7 */
      }
      /* At this point curN contains the 7/16 error value to be propagated
       * to the next pixel on the current line, and all the errors for the
       * next line have been shifted over.  We are therefore ready to move on.
       */
      inptr += dir3;		/* Advance pixel pointers to next column */
      outptr += dir;
      errorptr += dir3;		/* advance errorptr to current column */
    }
    /* Post-loop cleanup: we must unload the final error values into the
     * final fserrors[] entry.  Note we need not unload belowerrN because
     * it is for the dummy column before or after the actual array.
     */
    errorptr[0] = (FSERROR) bpreverr0; /* unload prev errs into array */
    errorptr[1] = (FSERROR) bpreverr1;
    errorptr[2] = (FSERROR) bpreverr2;
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movq %rdx, 0x70(%rsp)
movq %rsi, 0x68(%rsp)
movq %rdi, 0x18(%rsp)
testl %ecx, %ecx
jle 0x84d14
movq 0x18(%rsp), %rax
movq 0x270(%rax), %rsi
movq 0x30(%rsi), %rdx
movq %rdx, 0xa8(%rsp)
movq 0x50(%rsi), %r8
movl 0x88(%rax), %r9d
movq 0x1a8(%rax), %r10
movq 0xa0(%rax), %rax
movq (%rax), %r11
movq 0x8(%rax), %rdx
movq %rdx, 0x20(%rsp)
movq 0x10(%rax), %rax
movq %rax, 0xa0(%rsp)
leal -0x1(%r9), %eax
movq %rax, 0x48(%rsp)
leal -0x3(%r9,%r9,2), %eax
movq %rax, 0x40(%rsp)
leal 0x3(%r9,%r9,2), %eax
movq %rax, 0x38(%rsp)
movl %ecx, %eax
movq %rax, 0x60(%rsp)
xorl %ebx, %ebx
movq %rsi, 0x58(%rsp)
movq %r8, 0x90(%rsp)
movq %r9, 0x50(%rsp)
movq %r10, 0x88(%rsp)
movq %r11, 0x80(%rsp)
movq 0x68(%rsp), %rax
movq (%rax,%rbx,8), %rdi
movq 0x70(%rsp), %rax
movq (%rax,%rbx,8), %r14
cmpl $0x0, 0x48(%rsi)
je 0x84ab9
addq 0x40(%rsp), %rdi
movq 0x38(%rsp), %rax
leaq (%rax,%rax), %r15
addq 0x40(%rsi), %r15
addq 0x48(%rsp), %r14
movl $0xfffffffd, %ecx       # imm = 0xFFFFFFFD
movq $-0x1, 0x28(%rsp)
xorl %edx, %edx
jmp 0x84ad1
movq 0x40(%rsi), %r15
movl $0x3, %ecx
movl $0x1, %eax
movq %rax, 0x28(%rsp)
movl $0x1, %edx
xorl %eax, %eax
movl %edx, 0x48(%rsi)
testl %r9d, %r9d
je 0x84cf3
movq %r14, 0x30(%rsp)
movq %rbx, 0x78(%rsp)
movslq %ecx, %rcx
movq %rcx, 0xb8(%rsp)
leaq (%rcx,%rcx), %rsi
addq $0x2, %rdi
xorl %ecx, %ecx
xorl %edx, %edx
movl $0x0, (%rsp)
movl $0x0, 0x4(%rsp)
xorl %r13d, %r13d
movq $0x0, 0x8(%rsp)
xorl %r14d, %r14d
xorl %ebp, %ebp
movl %r9d, %ebx
movq 0x20(%rsp), %r9
movq %rsi, 0xb0(%rsp)
movl %ebx, 0x10(%rsp)
movq %rdi, 0xc0(%rsp)
movl %r13d, 0x14(%rsp)
movq %rsi, %r13
movswl (%r15,%rsi), %esi
addl %esi, %eax
addl $0x8, %eax
sarl $0x4, %eax
movswl 0x2(%r15,%r13), %esi
addl %esi, %ecx
addl $0x8, %ecx
sarl $0x4, %ecx
movswl 0x4(%r15,%r13), %esi
addl %esi, %edx
addl $0x8, %edx
sarl $0x4, %edx
cltq
movslq (%r8,%rax,4), %rax
movslq %ecx, %rcx
movslq (%r8,%rcx,4), %rcx
movslq %edx, %rdx
movslq (%r8,%rdx,4), %rdx
movzbl -0x2(%rdi), %esi
addq %rax, %rsi
movzbl -0x1(%rdi), %eax
addq %rcx, %rax
movzbl (%rdi), %ecx
addq %rdx, %rcx
movzbl (%r10,%rsi), %r12d
movzbl (%r10,%rax), %ebx
movzbl (%r10,%rcx), %r13d
movl %r12d, %esi
andl $-0x8, %esi
movl %ebx, %edx
shrl $0x2, %edx
movl %r13d, %ecx
shrl $0x3, %ecx
movl %edx, %eax
shll $0x6, %eax
movq 0xa8(%rsp), %rdi
addq (%rdi,%rsi), %rax
movzwl (%rax,%rcx,2), %esi
testw %si, %si
jne 0x84c13
movl %r12d, %esi
shrl $0x3, %esi
leaq (%rax,%rcx,2), %rax
movq %rax, 0x98(%rsp)
movq 0x18(%rsp), %rdi
callq 0x85248
movq 0x20(%rsp), %r9
movq 0x80(%rsp), %r11
movq 0x88(%rsp), %r10
movq 0x90(%rsp), %r8
movq 0x98(%rsp), %rax
movzwl (%rax), %esi
movzwl %si, %eax
leaq -0x1(%rax), %rcx
movq 0x30(%rsp), %rsi
movb %cl, (%rsi)
movzbl -0x1(%r11,%rax), %ecx
subl %ecx, %r12d
movzbl -0x1(%r9,%rax), %ecx
subl %ecx, %ebx
movq 0xa0(%rsp), %rcx
movzbl -0x1(%rcx,%rax), %eax
subl %eax, %r13d
leal (%r12,%r12,2), %eax
addl 0x8(%rsp), %eax
movw %ax, (%r15)
leal (%r12,%r12,4), %eax
addl (%rsp), %eax
movq %rax, 0x8(%rsp)
leal (%rbx,%rbx,2), %ecx
addl %r14d, %ecx
leal (,%r12,8), %eax
subl %r12d, %eax
movw %cx, 0x2(%r15)
leal (%rbx,%rbx,4), %r14d
addl 0x4(%rsp), %r14d
leal (%r13,%r13,2), %edx
addl %ebp, %edx
leal (,%rbx,8), %ecx
subl %ebx, %ecx
movw %dx, 0x4(%r15)
leal (%r13,%r13,4), %ebp
addl 0x14(%rsp), %ebp
leal (,%r13,8), %edx
subl %r13d, %edx
addq 0x28(%rsp), %rsi
movq %rsi, 0x30(%rsp)
movq 0xb0(%rsp), %rsi
addq %rsi, %r15
movq 0xc0(%rsp), %rdi
addq 0xb8(%rsp), %rdi
movl %r12d, (%rsp)
movl %ebx, 0x4(%rsp)
movl 0x10(%rsp), %ebx
decl %ebx
jne 0x84b30
movl %ebp, %eax
movq 0x58(%rsp), %rsi
movq 0x50(%rsp), %r9
movq 0x78(%rsp), %rbx
movq 0x8(%rsp), %rcx
jmp 0x84cf8
xorl %r14d, %r14d
xorl %ecx, %ecx
movw %cx, (%r15)
movw %r14w, 0x2(%r15)
movw %ax, 0x4(%r15)
incq %rbx
cmpq 0x60(%rsp), %rbx
jne 0x84a78
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq