// File: 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:
subq $0x98, %rsp
movq %rdi, 0x90(%rsp)
movq %rsi, 0x88(%rsp)
movq %rdx, 0x80(%rsp)
movl %ecx, 0x7c(%rsp)
movq 0x90(%rsp), %rax
movq 0x270(%rax), %rax
movq %rax, 0x70(%rsp)
movq 0x90(%rsp), %rax
movl 0x90(%rax), %eax
movl %eax, 0x28(%rsp)
movq 0x90(%rsp), %rax
movl 0x88(%rax), %eax
movl %eax, 0x10(%rsp)
movq 0x90(%rsp), %rax
movq 0x1a8(%rax), %rax
movq %rax, 0x8(%rsp)
movl $0x0, 0x18(%rsp)
movl 0x18(%rsp), %eax
cmpl 0x7c(%rsp), %eax
jge 0xf55bd
movq 0x80(%rsp), %rax
movslq 0x18(%rsp), %rcx
movq (%rax,%rcx,8), %rdi
movl 0x10(%rsp), %eax
movl %eax, %esi
shlq $0x0, %rsi
callq 0xf9470
movl $0x0, 0x1c(%rsp)
movl 0x1c(%rsp), %eax
cmpl 0x28(%rsp), %eax
jge 0xf558a
movq 0x88(%rsp), %rax
movslq 0x18(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movslq 0x1c(%rsp), %rcx
addq %rcx, %rax
movq %rax, 0x48(%rsp)
movq 0x80(%rsp), %rax
movslq 0x18(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0x40(%rsp)
movq 0x70(%rsp), %rax
cmpl $0x0, 0x90(%rax)
je 0xf53c5
movl 0x10(%rsp), %ecx
subl $0x1, %ecx
imull 0x28(%rsp), %ecx
movq 0x48(%rsp), %rax
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, 0x48(%rsp)
movl 0x10(%rsp), %ecx
subl $0x1, %ecx
movq 0x40(%rsp), %rax
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, 0x40(%rsp)
movl $0xffffffff, 0x24(%rsp) # imm = 0xFFFFFFFF
xorl %eax, %eax
subl 0x28(%rsp), %eax
movl %eax, 0x20(%rsp)
movq 0x70(%rsp), %rax
movslq 0x1c(%rsp), %rcx
movq 0x70(%rax,%rcx,8), %rax
movl 0x10(%rsp), %ecx
addl $0x1, %ecx
movl %ecx, %ecx
shlq %rcx
addq %rcx, %rax
movq %rax, 0x50(%rsp)
jmp 0xf53e9
movl $0x1, 0x24(%rsp)
movl 0x28(%rsp), %eax
movl %eax, 0x20(%rsp)
movq 0x70(%rsp), %rax
movslq 0x1c(%rsp), %rcx
movq 0x70(%rax,%rcx,8), %rax
movq %rax, 0x50(%rsp)
movq 0x70(%rsp), %rax
movq 0x30(%rax), %rax
movslq 0x1c(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0x38(%rsp)
movq 0x70(%rsp), %rax
movq 0x20(%rax), %rax
movslq 0x1c(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0x30(%rsp)
movl $0x0, 0x6c(%rsp)
movl $0x0, 0x64(%rsp)
movl $0x0, 0x68(%rsp)
movl 0x10(%rsp), %eax
movl %eax, 0x14(%rsp)
cmpl $0x0, 0x14(%rsp)
jbe 0xf556b
movl 0x6c(%rsp), %eax
movq 0x50(%rsp), %rcx
movslq 0x24(%rsp), %rdx
movswl (%rcx,%rdx,2), %ecx
addl %ecx, %eax
addl $0x8, %eax
sarl $0x4, %eax
movl %eax, 0x6c(%rsp)
movq 0x48(%rsp), %rax
movzbl (%rax), %eax
addl 0x6c(%rsp), %eax
movl %eax, 0x6c(%rsp)
movq 0x8(%rsp), %rax
movslq 0x6c(%rsp), %rcx
movzbl (%rax,%rcx), %eax
movl %eax, 0x6c(%rsp)
movq 0x38(%rsp), %rax
movslq 0x6c(%rsp), %rcx
movzbl (%rax,%rcx), %eax
movl %eax, 0x2c(%rsp)
movl 0x2c(%rsp), %eax
movzbl %al, %edx
movq 0x40(%rsp), %rax
movzbl (%rax), %ecx
addl %edx, %ecx
movb %cl, (%rax)
movq 0x30(%rsp), %rax
movslq 0x2c(%rsp), %rcx
movzbl (%rax,%rcx), %ecx
movl 0x6c(%rsp), %eax
subl %ecx, %eax
movl %eax, 0x6c(%rsp)
movl 0x6c(%rsp), %eax
movl %eax, 0x60(%rsp)
movl 0x6c(%rsp), %eax
shll %eax
movl %eax, 0x5c(%rsp)
movl 0x5c(%rsp), %eax
addl 0x6c(%rsp), %eax
movl %eax, 0x6c(%rsp)
movl 0x64(%rsp), %eax
addl 0x6c(%rsp), %eax
movw %ax, %cx
movq 0x50(%rsp), %rax
movw %cx, (%rax)
movl 0x5c(%rsp), %eax
addl 0x6c(%rsp), %eax
movl %eax, 0x6c(%rsp)
movl 0x68(%rsp), %eax
addl 0x6c(%rsp), %eax
movl %eax, 0x64(%rsp)
movl 0x60(%rsp), %eax
movl %eax, 0x68(%rsp)
movl 0x5c(%rsp), %eax
addl 0x6c(%rsp), %eax
movl %eax, 0x6c(%rsp)
movl 0x20(%rsp), %ecx
movq 0x48(%rsp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, 0x48(%rsp)
movl 0x24(%rsp), %ecx
movq 0x40(%rsp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, 0x40(%rsp)
movl 0x24(%rsp), %ecx
movq 0x50(%rsp), %rax
movslq %ecx, %rcx
shlq %rcx
addq %rcx, %rax
movq %rax, 0x50(%rsp)
movl 0x14(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x14(%rsp)
jmp 0xf5437
movl 0x64(%rsp), %eax
movw %ax, %cx
movq 0x50(%rsp), %rax
movw %cx, (%rax)
movl 0x1c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c(%rsp)
jmp 0xf530d
movq 0x70(%rsp), %rax
movl 0x90(%rax), %edx
movl $0x1, %ecx
xorl %eax, %eax
cmpl $0x0, %edx
cmovnel %eax, %ecx
movq 0x70(%rsp), %rax
movl %ecx, 0x90(%rax)
movl 0x18(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x18(%rsp)
jmp 0xf52d7
addq $0x98, %rsp
retq
nopw %cs:(%rax,%rax)