// File: /workspace/github_cpp_makefile_star2_O2/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 $0x68, %rsp
movq %rdx, 0x18(%rsp)
movq %rsi, 0x48(%rsp)
movq 0x270(%rdi), %r14
movl 0x88(%rdi), %eax
movl 0x90(%rdi), %edx
movq 0x1a8(%rdi), %rbx
leal -0x1(%rax), %esi
movq %rsi, 0x38(%rsp)
imull %edx, %esi
movq %rsi, 0x30(%rsp)
movl %edx, %esi
negl %esi
movl %esi, 0xc(%rsp)
movq %rax, 0x10(%rsp)
incl %eax
movq %rax, 0x28(%rsp)
xorl %r15d, %r15d
testl %edx, %edx
movl $0x0, %eax
movl %edx, 0x8(%rsp)
cmovgl %edx, %eax
movq %rax, 0x58(%rsp)
testl %ecx, %ecx
cmovlel %r15d, %ecx
movq %rcx, 0x20(%rsp)
movq %r14, 0x40(%rsp)
cmpq 0x20(%rsp), %r15
je 0x77858
movq 0x18(%rsp), %rax
movq (%rax,%r15,8), %rdi
movq 0x10(%rsp), %rsi
callq 0x791cd
xorl %r11d, %r11d
movq %r15, 0x50(%rsp)
cmpq 0x58(%rsp), %r11
je 0x7783c
movq 0x48(%rsp), %rax
movq (%rax,%r15,8), %rcx
addq %r11, %rcx
movq 0x18(%rsp), %rax
movq (%rax,%r15,8), %rdx
cmpl $0x0, 0x90(%r14)
je 0x77791
addq 0x30(%rsp), %rcx
addq 0x38(%rsp), %rdx
movq 0x28(%rsp), %rax
leaq (%rax,%rax), %rsi
addq 0x70(%r14,%r11,8), %rsi
pushq $-0x1
popq %rdi
movl 0xc(%rsp), %r10d
jmp 0x7779e
movq 0x70(%r14,%r11,8), %rsi
pushq $0x1
popq %rdi
movl 0x8(%rsp), %r10d
movq 0x20(%r14), %r9
movq 0x30(%r14), %r8
movq (%r8,%r11,8), %r8
movq %r11, 0x60(%rsp)
movq (%r9,%r11,8), %r9
movslq %r10d, %r11
leaq (%rsi,%rdi,2), %r14
xorl %r10d, %r10d
xorl %r12d, %r12d
movq 0x10(%rsp), %rax
movl %eax, %r15d
xorl %ebp, %ebp
xorl %r13d, %r13d
subl $0x1, %r15d
jb 0x77820
movswl (%r14,%r10,2), %eax
addl %r13d, %eax
addl $0x8, %eax
sarl $0x4, %eax
movzbl (%rcx), %r13d
cltq
addq %r13, %rax
movzbl (%rbx,%rax), %eax
movzbl (%r8,%rax), %r13d
addb %r13b, (%rdx,%r10)
movzbl (%r9,%r13), %r13d
subl %r13d, %eax
leal (%rax,%rax,2), %r13d
addl %r12d, %r13d
movw %r13w, (%rsi,%r10,2)
leal (%rax,%rax,4), %r12d
addl %ebp, %r12d
imull $0x7, %eax, %r13d
addq %r11, %rcx
addq %rdi, %r10
movl %eax, %ebp
jmp 0x777cd
movw %r12w, (%rsi,%r10,2)
movq 0x60(%rsp), %r11
incq %r11
movq 0x40(%rsp), %r14
movq 0x50(%rsp), %r15
jmp 0x77745
xorl %eax, %eax
cmpl $0x0, 0x90(%r14)
sete %al
movl %eax, 0x90(%r14)
incq %r15
jmp 0x7771f
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop