// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/jpeg/jquant1.c
// Function: quantize_fs_dither
METHODDEF(void)
quantize_fs_dither (j_decompress_ptr cinfo, JSAMPARRAY input_buf,
		    JSAMPARRAY output_buf, int num_rows)
/* General case, with Floyd-Steinberg dithering */
{
  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
  register LOCFSERROR cur;	/* current error or pixel value */
  LOCFSERROR belowerr;		/* error for pixel below cur */
  LOCFSERROR bpreverr;		/* error for below/prev col */
  LOCFSERROR bnexterr;		/* error for below/next col */
  LOCFSERROR delta;
  register FSERRPTR errorptr;	/* => fserrors[] at column before current */
  register JSAMPROW input_ptr;
  register JSAMPROW output_ptr;
  JSAMPROW colorindex_ci;
  JSAMPROW colormap_ci;
  int pixcode;
  int nc = cinfo->out_color_components;
  int dir;			/* 1 for left-to-right, -1 for right-to-left */
  int dirnc;			/* dir * nc */
  int ci;
  int row;
  JDIMENSION col;
  JDIMENSION width = cinfo->output_width;
  JSAMPLE *range_limit = cinfo->sample_range_limit;
  SHIFT_TEMPS

  for (row = 0; row < num_rows; row++) {
    /* Initialize output values to 0 so can process components separately */
    jzero_far((void FAR *) output_buf[row],
	      (size_t) (width * SIZEOF(JSAMPLE)));
    for (ci = 0; ci < nc; ci++) {
      input_ptr = input_buf[row] + ci;
      output_ptr = output_buf[row];
      if (cquantize->on_odd_row) {
	/* work right to left in this row */
	input_ptr += (width-1) * nc; /* so point to rightmost pixel */
	output_ptr += width-1;
	dir = -1;
	dirnc = -nc;
	errorptr = cquantize->fserrors[ci] + (width+1); /* => entry after last column */
      } else {
	/* work left to right in this row */
	dir = 1;
	dirnc = nc;
	errorptr = cquantize->fserrors[ci]; /* => entry before first column */
      }
      colorindex_ci = cquantize->colorindex[ci];
      colormap_ci = cquantize->sv_colormap[ci];
      /* Preset error values: no error propagated to first pixel from left */
      cur = 0;
      /* and no error propagated to row below yet */
      belowerr = bpreverr = 0;

      for (col = width; col > 0; col--) {
	/* cur 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.
	 */
	cur = RIGHT_SHIFT(cur + errorptr[dir] + 8, 4);
	/* Form pixel value + error, and range-limit to 0..MAXJSAMPLE.
	 * The maximum error is +- MAXJSAMPLE; this sets the required size
	 * of the range_limit array.
	 */
	cur += GETJSAMPLE(*input_ptr);
	cur = GETJSAMPLE(range_limit[cur]);
	/* Select output value, accumulate into output code for this pixel */
	pixcode = GETJSAMPLE(colorindex_ci[cur]);
	*output_ptr += (JSAMPLE) pixcode;
	/* Compute actual representation error at this pixel */
	/* Note: we can do this even though we don't have the final */
	/* pixel code, because the colormap is orthogonal. */
	cur -= GETJSAMPLE(colormap_ci[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.
	 */
	bnexterr = cur;
	delta = cur * 2;
	cur += delta;		/* form error * 3 */
	errorptr[0] = (FSERROR) (bpreverr + cur);
	cur += delta;		/* form error * 5 */
	bpreverr = belowerr + cur;
	belowerr = bnexterr;
	cur += delta;		/* form error * 7 */
	/* At this point cur 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.
	 */
	input_ptr += dirnc;	/* advance input ptr to next column */
	output_ptr += dir;	/* advance output ptr to next column */
	errorptr += dir;	/* advance errorptr to current column */
      }
      /* Post-loop cleanup: we must unload the final error value into the
       * final fserrors[] entry.  Note we need not unload belowerr because
       * it is for the dummy column before or after the actual array.
       */
      errorptr[0] = (FSERROR) bpreverr; /* unload prev err into array */
    }
    cquantize->on_odd_row = (cquantize->on_odd_row ? FALSE : TRUE);
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdx, 0x8(%rsp)
movq %rsi, 0x50(%rsp)
testl %ecx, %ecx
jle 0x8422f
movl 0x88(%rdi), %ebx
movl 0x90(%rdi), %r14d
movq 0x1a8(%rdi), %r13
movq 0x270(%rdi), %r12
leal -0x1(%rbx), %eax
movq %rax, 0x28(%rsp)
imull %r14d, %eax
movq %rax, 0x20(%rsp)
movl %r14d, %eax
negl %eax
movl %eax, 0x4(%rsp)
leal 0x1(%rbx), %eax
movq %rax, 0x18(%rsp)
movl %ecx, %eax
movq %rax, 0x10(%rsp)
xorl %ebp, %ebp
movq %rbx, 0x40(%rsp)
movq %r14, 0x38(%rsp)
movq %r12, 0x30(%rsp)
movq 0x8(%rsp), %rax
movq (%rax,%rbp,8), %rdi
movq %rbx, %rsi
callq 0x85d17
testl %r14d, %r14d
jle 0x8420b
xorl %eax, %eax
movq %rbp, 0x48(%rsp)
movq 0x50(%rsp), %rcx
movq (%rcx,%rbp,8), %rcx
addq %rax, %rcx
movq 0x8(%rsp), %rdx
movq (%rdx,%rbp,8), %rsi
cmpl $0x0, 0x90(%r12)
je 0x84151
addq 0x20(%rsp), %rcx
addq 0x28(%rsp), %rsi
movq 0x18(%rsp), %rdx
addq %rdx, %rdx
addq 0x70(%r12,%rax,8), %rdx
movq $-0x1, %rdi
movl 0x4(%rsp), %r10d
jmp 0x8415e
movq 0x70(%r12,%rax,8), %rdx
movl $0x1, %edi
movl %r14d, %r10d
xorl %r15d, %r15d
testl %ebx, %ebx
je 0x841fb
movq 0x20(%r12), %r9
movq 0x30(%r12), %r8
movq (%r8,%rax,8), %r8
movq (%r9,%rax,8), %r9
movslq %r10d, %r10
leaq (%rdi,%rdi), %r11
xorl %r14d, %r14d
xorl %r12d, %r12d
movswl (%rdx,%rdi,2), %ebp
addl %r15d, %ebp
addl $0x8, %ebp
sarl $0x4, %ebp
movzbl (%rcx), %r15d
movslq %ebp, %rbp
addq %r15, %rbp
movzbl (%r13,%rbp), %ebp
movzbl (%r8,%rbp), %r15d
addb %r15b, (%rsi)
movzbl (%r9,%r15), %r15d
subl %r15d, %ebp
leal (%rbp,%rbp,2), %r15d
addl %r12d, %r15d
movw %r15w, (%rdx)
leal (%rbp,%rbp,4), %r12d
addl %r14d, %r12d
leal (,%rbp,8), %r15d
subl %ebp, %r15d
addq %r10, %rcx
addq %rdi, %rsi
addq %r11, %rdx
movl %ebp, %r14d
decl %ebx
jne 0x84188
movl %r12d, %r15d
movq 0x40(%rsp), %rbx
movq 0x38(%rsp), %r14
movq 0x30(%rsp), %r12
movq 0x48(%rsp), %rbp
movw %r15w, (%rdx)
incq %rax
cmpq %r14, %rax
jne 0x8410c
xorl %eax, %eax
cmpl $0x0, 0x90(%r12)
sete %al
movl %eax, 0x90(%r12)
incq %rbp
cmpq 0x10(%rsp), %rbp
jne 0x840eb
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop