// File: /workspace/github_cpp_makefile_star2_O1/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 $0xd8, %rsp
movq %rdx, 0x88(%rsp)
movq %rsi, 0x80(%rsp)
movq %rdi, 0x30(%rsp)
testl %ecx, %ecx
jle 0x85b10
movq 0x30(%rsp), %rax
movq 0x270(%rax), %rsi
movq 0x30(%rsi), %rdi
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, 0x38(%rsp)
movq 0x10(%rax), %rax
movq %rax, 0xb8(%rsp)
leal -0x1(%r9), %eax
movq %rax, 0x60(%rsp)
leal -0x3(%r9,%r9,2), %eax
movq %rax, 0x58(%rsp)
leal 0x3(%r9,%r9,2), %eax
movq %rax, 0x50(%rsp)
movl %ecx, %eax
movq %rax, 0x78(%rsp)
xorl %ebx, %ebx
movq %rsi, 0x70(%rsp)
movq %rdi, 0xb0(%rsp)
movq %r8, 0xa8(%rsp)
movq %r9, 0x68(%rsp)
movq %r10, 0xa0(%rsp)
movq %r11, 0x98(%rsp)
movq 0x80(%rsp), %rax
movq (%rax,%rbx,8), %r12
movq 0x88(%rsp), %rax
movq (%rax,%rbx,8), %r15
cmpl $0x0, 0x48(%rsi)
je 0x8589c
addq 0x58(%rsp), %r12
movq 0x50(%rsp), %rax
leaq (%rax,%rax), %rbp
addq 0x40(%rsi), %rbp
addq 0x60(%rsp), %r15
movl $0xfffffffd, %ecx       # imm = 0xFFFFFFFD
movq $-0x1, 0x40(%rsp)
xorl %edx, %edx
jmp 0x858b4
movq 0x40(%rsi), %rbp
movl $0x3, %ecx
movl $0x1, %eax
movq %rax, 0x40(%rsp)
movl $0x1, %edx
xorl %eax, %eax
movl %edx, 0x48(%rsi)
testl %r9d, %r9d
je 0x85af2
movq %r15, 0x48(%rsp)
movq %rbx, 0x90(%rsp)
movslq %ecx, %rcx
movq %rcx, 0xc8(%rsp)
leaq (%rcx,%rcx), %rsi
addq $0x2, %r12
xorl %ecx, %ecx
xorl %edx, %edx
movl $0x0, 0xc(%rsp)
xorl %r13d, %r13d
xorl %r15d, %r15d
movq $0x0, 0x18(%rsp)
movq $0x0, 0x10(%rsp)
movl $0x0, 0x8(%rsp)
movl %r9d, %ebx
movq 0x38(%rsp), %r9
movq %rsi, 0xc0(%rsp)
movq %r12, 0xd0(%rsp)
movl %ebx, 0x24(%rsp)
movl %r13d, 0x28(%rsp)
movl %r15d, 0x2c(%rsp)
movq %rsi, %r15
movswl (%rbp,%rsi), %esi
addl %esi, %eax
addl $0x8, %eax
sarl $0x4, %eax
movswl 0x2(%rbp,%r15), %esi
addl %esi, %ecx
addl $0x8, %ecx
sarl $0x4, %ecx
movswl 0x4(%rbp,%r15), %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(%r12), %esi
addq %rax, %rsi
movzbl -0x1(%r12), %eax
addq %rcx, %rax
movzbl (%r12), %ecx
addq %rdx, %rcx
movzbl (%r10,%rsi), %r15d
movzbl (%r10,%rax), %r13d
movzbl (%r10,%rcx), %ebx
movl %r15d, %eax
andl $-0x8, %eax
movl %r13d, %edx
shrl $0x2, %edx
movl %ebx, %r12d
shrl $0x3, %r12d
movl %edx, %r14d
shll $0x6, %r14d
addq (%rdi,%rax), %r14
cmpw $0x0, (%r14,%r12,2)
jne 0x859fb
movl %r15d, %esi
shrl $0x3, %esi
movq 0x30(%rsp), %rdi
movl %r12d, %ecx
callq 0x86044
movq 0x38(%rsp), %r9
movq 0x98(%rsp), %r11
movq 0xa0(%rsp), %r10
movq 0xa8(%rsp), %r8
movq 0xb0(%rsp), %rdi
leaq (%r14,%r12,2), %rax
movzwl (%rax), %eax
leaq -0x1(%rax), %rcx
movq 0x48(%rsp), %rsi
movb %cl, (%rsi)
movzbl -0x1(%r11,%rax), %ecx
subl %ecx, %r15d
movzbl -0x1(%r9,%rax), %ecx
subl %ecx, %r13d
movq 0xb8(%rsp), %rcx
movzbl -0x1(%rcx,%rax), %eax
subl %eax, %ebx
leal (%r15,%r15,2), %eax
addl 0x18(%rsp), %eax
movw %ax, (%rbp)
leal (%r15,%r15,4), %eax
addl 0xc(%rsp), %eax
movq %rax, 0x18(%rsp)
leal (%r13,%r13,2), %ecx
addl 0x10(%rsp), %ecx
leal (,%r15,8), %eax
subl %r15d, %eax
movw %cx, 0x2(%rbp)
leal (%r13,%r13,4), %ecx
addl 0x28(%rsp), %ecx
movq %rcx, 0x10(%rsp)
leal (%rbx,%rbx,2), %edx
addl 0x8(%rsp), %edx
leal (,%r13,8), %ecx
subl %r13d, %ecx
movw %dx, 0x4(%rbp)
leal (%rbx,%rbx,4), %edx
addl 0x2c(%rsp), %edx
movl %edx, 0x8(%rsp)
leal (,%rbx,8), %edx
subl %ebx, %edx
addq 0x40(%rsp), %rsi
movq %rsi, 0x48(%rsp)
movq 0xc0(%rsp), %rsi
addq %rsi, %rbp
movq 0xd0(%rsp), %r12
addq 0xc8(%rsp), %r12
movl %r15d, 0xc(%rsp)
movl %ebx, %r15d
movl 0x24(%rsp), %ebx
decl %ebx
jne 0x8591e
movl 0x8(%rsp), %eax
movq 0x70(%rsp), %rsi
movq 0x68(%rsp), %r9
movq 0x90(%rsp), %rbx
movq 0x18(%rsp), %rcx
movq 0x10(%rsp), %rdx
jmp 0x85af6
xorl %edx, %edx
xorl %ecx, %ecx
movw %cx, (%rbp)
movw %dx, 0x2(%rbp)
movw %ax, 0x4(%rbp)
incq %rbx
cmpq 0x78(%rsp), %rbx
jne 0x85855
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq