// File: 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:
subq $0xc8, %rsp
movq %rdi, 0xc0(%rsp)
movq %rsi, 0xb8(%rsp)
movq %rdx, 0xb0(%rsp)
movl %ecx, 0xac(%rsp)
movq 0xc0(%rsp), %rax
movq 0x270(%rax), %rax
movq %rax, 0xa0(%rsp)
movq 0xa0(%rsp), %rax
movq 0x30(%rax), %rax
movq %rax, 0x98(%rsp)
movq 0xc0(%rsp), %rax
movl 0x88(%rax), %eax
movl %eax, 0x3c(%rsp)
movq 0xc0(%rsp), %rax
movq 0x1a8(%rax), %rax
movq %rax, 0x30(%rsp)
movq 0xa0(%rsp), %rax
movq 0x50(%rax), %rax
movq %rax, 0x28(%rsp)
movq 0xc0(%rsp), %rax
movq 0xa0(%rax), %rax
movq (%rax), %rax
movq %rax, 0x20(%rsp)
movq 0xc0(%rsp), %rax
movq 0xa0(%rax), %rax
movq 0x8(%rax), %rax
movq %rax, 0x18(%rsp)
movq 0xc0(%rsp), %rax
movq 0xa0(%rax), %rax
movq 0x10(%rax), %rax
movq %rax, 0x10(%rsp)
movl $0x0, 0x44(%rsp)
movl 0x44(%rsp), %eax
cmpl 0xac(%rsp), %eax
jge 0xf67a4
movq 0xb8(%rsp), %rax
movslq 0x44(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0x60(%rsp)
movq 0xb0(%rsp), %rax
movslq 0x44(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0x58(%rsp)
movq 0xa0(%rsp), %rax
cmpl $0x0, 0x48(%rax)
je 0xf62b5
movl 0x3c(%rsp), %eax
subl $0x1, %eax
imull $0x3, %eax, %ecx
movq 0x60(%rsp), %rax
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, 0x60(%rsp)
movl 0x3c(%rsp), %ecx
subl $0x1, %ecx
movq 0x58(%rsp), %rax
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, 0x58(%rsp)
movl $0xffffffff, 0x4c(%rsp) # imm = 0xFFFFFFFF
movl $0xfffffffd, 0x48(%rsp) # imm = 0xFFFFFFFD
movq 0xa0(%rsp), %rax
movq 0x40(%rax), %rax
movl 0x3c(%rsp), %ecx
addl $0x1, %ecx
imull $0x3, %ecx, %ecx
movl %ecx, %ecx
shlq %rcx
addq %rcx, %rax
movq %rax, 0x68(%rsp)
movq 0xa0(%rsp), %rax
movl $0x0, 0x48(%rax)
jmp 0xf62e5
movl $0x1, 0x4c(%rsp)
movl $0x3, 0x48(%rsp)
movq 0xa0(%rsp), %rax
movq 0x40(%rax), %rax
movq %rax, 0x68(%rsp)
movq 0xa0(%rsp), %rax
movl $0x1, 0x48(%rax)
movl $0x0, 0x8c(%rsp)
movl $0x0, 0x90(%rsp)
movl $0x0, 0x94(%rsp)
movl $0x0, 0x80(%rsp)
movl $0x0, 0x84(%rsp)
movl $0x0, 0x88(%rsp)
movl $0x0, 0x74(%rsp)
movl $0x0, 0x78(%rsp)
movl $0x0, 0x7c(%rsp)
movl 0x3c(%rsp), %eax
movl %eax, 0x40(%rsp)
cmpl $0x0, 0x40(%rsp)
jbe 0xf6765
movl 0x94(%rsp), %eax
movq 0x68(%rsp), %rcx
movl 0x48(%rsp), %edx
addl $0x0, %edx
movslq %edx, %rdx
movswl (%rcx,%rdx,2), %ecx
addl %ecx, %eax
addl $0x8, %eax
sarl $0x4, %eax
movl %eax, 0x94(%rsp)
movl 0x90(%rsp), %eax
movq 0x68(%rsp), %rcx
movl 0x48(%rsp), %edx
addl $0x1, %edx
movslq %edx, %rdx
movswl (%rcx,%rdx,2), %ecx
addl %ecx, %eax
addl $0x8, %eax
sarl $0x4, %eax
movl %eax, 0x90(%rsp)
movl 0x8c(%rsp), %eax
movq 0x68(%rsp), %rcx
movl 0x48(%rsp), %edx
addl $0x2, %edx
movslq %edx, %rdx
movswl (%rcx,%rdx,2), %ecx
addl %ecx, %eax
addl $0x8, %eax
sarl $0x4, %eax
movl %eax, 0x8c(%rsp)
movq 0x28(%rsp), %rax
movslq 0x94(%rsp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, 0x94(%rsp)
movq 0x28(%rsp), %rax
movslq 0x90(%rsp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, 0x90(%rsp)
movq 0x28(%rsp), %rax
movslq 0x8c(%rsp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, 0x8c(%rsp)
movq 0x60(%rsp), %rax
movzbl (%rax), %eax
addl 0x94(%rsp), %eax
movl %eax, 0x94(%rsp)
movq 0x60(%rsp), %rax
movzbl 0x1(%rax), %eax
addl 0x90(%rsp), %eax
movl %eax, 0x90(%rsp)
movq 0x60(%rsp), %rax
movzbl 0x2(%rax), %eax
addl 0x8c(%rsp), %eax
movl %eax, 0x8c(%rsp)
movq 0x30(%rsp), %rax
movslq 0x94(%rsp), %rcx
movzbl (%rax,%rcx), %eax
movl %eax, 0x94(%rsp)
movq 0x30(%rsp), %rax
movslq 0x90(%rsp), %rcx
movzbl (%rax,%rcx), %eax
movl %eax, 0x90(%rsp)
movq 0x30(%rsp), %rax
movslq 0x8c(%rsp), %rcx
movzbl (%rax,%rcx), %eax
movl %eax, 0x8c(%rsp)
movq 0x98(%rsp), %rax
movl 0x94(%rsp), %ecx
sarl $0x3, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movl 0x90(%rsp), %ecx
sarl $0x2, %ecx
movslq %ecx, %rcx
shlq $0x6, %rcx
addq %rcx, %rax
movl 0x8c(%rsp), %ecx
sarl $0x3, %ecx
movslq %ecx, %rcx
shlq %rcx
addq %rcx, %rax
movq %rax, 0x50(%rsp)
movq 0x50(%rsp), %rax
movzwl (%rax), %eax
cmpl $0x0, %eax
jne 0xf651b
movq 0xc0(%rsp), %rdi
movl 0x94(%rsp), %esi
sarl $0x3, %esi
movl 0x90(%rsp), %edx
sarl $0x2, %edx
movl 0x8c(%rsp), %ecx
sarl $0x3, %ecx
callq 0xf7730
movq 0x50(%rsp), %rax
movzwl (%rax), %eax
subl $0x1, %eax
movl %eax, 0xc(%rsp)
movl 0xc(%rsp), %eax
movb %al, %cl
movq 0x58(%rsp), %rax
movb %cl, (%rax)
movq 0x20(%rsp), %rax
movslq 0xc(%rsp), %rcx
movzbl (%rax,%rcx), %ecx
movl 0x94(%rsp), %eax
subl %ecx, %eax
movl %eax, 0x94(%rsp)
movq 0x18(%rsp), %rax
movslq 0xc(%rsp), %rcx
movzbl (%rax,%rcx), %ecx
movl 0x90(%rsp), %eax
subl %ecx, %eax
movl %eax, 0x90(%rsp)
movq 0x10(%rsp), %rax
movslq 0xc(%rsp), %rcx
movzbl (%rax,%rcx), %ecx
movl 0x8c(%rsp), %eax
subl %ecx, %eax
movl %eax, 0x8c(%rsp)
movl 0x94(%rsp), %eax
movl %eax, 0x8(%rsp)
movl 0x94(%rsp), %eax
shll %eax
movl %eax, 0x4(%rsp)
movl 0x4(%rsp), %eax
addl 0x94(%rsp), %eax
movl %eax, 0x94(%rsp)
movl 0x7c(%rsp), %eax
addl 0x94(%rsp), %eax
movw %ax, %cx
movq 0x68(%rsp), %rax
movw %cx, (%rax)
movl 0x4(%rsp), %eax
addl 0x94(%rsp), %eax
movl %eax, 0x94(%rsp)
movl 0x88(%rsp), %eax
addl 0x94(%rsp), %eax
movl %eax, 0x7c(%rsp)
movl 0x8(%rsp), %eax
movl %eax, 0x88(%rsp)
movl 0x4(%rsp), %eax
addl 0x94(%rsp), %eax
movl %eax, 0x94(%rsp)
movl 0x90(%rsp), %eax
movl %eax, 0x8(%rsp)
movl 0x90(%rsp), %eax
shll %eax
movl %eax, 0x4(%rsp)
movl 0x4(%rsp), %eax
addl 0x90(%rsp), %eax
movl %eax, 0x90(%rsp)
movl 0x78(%rsp), %eax
addl 0x90(%rsp), %eax
movw %ax, %cx
movq 0x68(%rsp), %rax
movw %cx, 0x2(%rax)
movl 0x4(%rsp), %eax
addl 0x90(%rsp), %eax
movl %eax, 0x90(%rsp)
movl 0x84(%rsp), %eax
addl 0x90(%rsp), %eax
movl %eax, 0x78(%rsp)
movl 0x8(%rsp), %eax
movl %eax, 0x84(%rsp)
movl 0x4(%rsp), %eax
addl 0x90(%rsp), %eax
movl %eax, 0x90(%rsp)
movl 0x8c(%rsp), %eax
movl %eax, 0x8(%rsp)
movl 0x8c(%rsp), %eax
shll %eax
movl %eax, 0x4(%rsp)
movl 0x4(%rsp), %eax
addl 0x8c(%rsp), %eax
movl %eax, 0x8c(%rsp)
movl 0x74(%rsp), %eax
addl 0x8c(%rsp), %eax
movw %ax, %cx
movq 0x68(%rsp), %rax
movw %cx, 0x4(%rax)
movl 0x4(%rsp), %eax
addl 0x8c(%rsp), %eax
movl %eax, 0x8c(%rsp)
movl 0x80(%rsp), %eax
addl 0x8c(%rsp), %eax
movl %eax, 0x74(%rsp)
movl 0x8(%rsp), %eax
movl %eax, 0x80(%rsp)
movl 0x4(%rsp), %eax
addl 0x8c(%rsp), %eax
movl %eax, 0x8c(%rsp)
movl 0x48(%rsp), %ecx
movq 0x60(%rsp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, 0x60(%rsp)
movl 0x4c(%rsp), %ecx
movq 0x58(%rsp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, 0x58(%rsp)
movl 0x48(%rsp), %ecx
movq 0x68(%rsp), %rax
movslq %ecx, %rcx
shlq %rcx
addq %rcx, %rax
movq %rax, 0x68(%rsp)
movl 0x40(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x40(%rsp)
jmp 0xf6347
movl 0x7c(%rsp), %eax
movw %ax, %cx
movq 0x68(%rsp), %rax
movw %cx, (%rax)
movl 0x78(%rsp), %eax
movw %ax, %cx
movq 0x68(%rsp), %rax
movw %cx, 0x2(%rax)
movl 0x74(%rsp), %eax
movw %ax, %cx
movq 0x68(%rsp), %rax
movw %cx, 0x4(%rax)
movl 0x44(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x44(%rsp)
jmp 0xf61f7
addq $0xc8, %rsp
retq
nopl (%rax)