// File: /workspace/github_cpp_makefile_star2_O2/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 $0xc8, %rsp
movq %rdx, 0x58(%rsp)
movq %rsi, 0x50(%rsp)
movq 0x270(%rdi), %rdx
movq 0x30(%rdx), %rax
movq %rax, 0xa0(%rsp)
movq 0x50(%rdx), %r9
movl 0x88(%rdi), %esi
movq 0x1a8(%rdi), %r10
movq %rdi, 0x90(%rsp)
movq 0xa0(%rdi), %rax
movq (%rax), %rdi
movq 0x8(%rax), %r11
movq 0x10(%rax), %r13
leal -0x1(%rsi), %eax
movq %rax, 0x38(%rsp)
xorl %ebx, %ebx
testl %ecx, %ecx
cmovlel %ebx, %ecx
movq %rcx, 0x60(%rsp)
leal -0x3(%rsi,%rsi,2), %eax
movq %rax, 0x30(%rsp)
movq %rsi, 0x40(%rsp)
leal 0x3(%rsi,%rsi,2), %eax
movq %rax, 0x28(%rsp)
movq %rdx, 0x48(%rsp)
movq %r10, 0x88(%rsp)
movq %rdi, 0x80(%rsp)
movq %r11, 0x78(%rsp)
movq %r13, 0x70(%rsp)
cmpq 0x60(%rsp), %rbx
je 0x782b7
movq 0x50(%rsp), %rax
movq (%rax,%rbx,8), %r14
movq 0x58(%rsp), %rax
movq (%rax,%rbx,8), %rsi
cmpl $0x0, 0x48(%rdx)
movq %rbx, 0x68(%rsp)
je 0x7807d
addq 0x30(%rsp), %r14
movq 0x28(%rsp), %rax
leaq (%rax,%rax), %rbx
addq 0x40(%rdx), %rbx
addq 0x38(%rsp), %rsi
xorl %eax, %eax
pushq $-0x1
popq %rcx
movq %rcx, 0x18(%rsp)
pushq $-0x3
jmp 0x7808e
movq 0x40(%rdx), %rbx
pushq $0x1
popq %rax
pushq $0x1
popq %rcx
movq %rcx, 0x18(%rsp)
pushq $0x3
popq %rcx
movl %eax, 0x48(%rdx)
movslq %ecx, %rax
movq %rbx, 0x20(%rsp)
movq %rax, 0xb0(%rsp)
leaq (%rbx,%rax,2), %rax
movq %rax, 0xa8(%rsp)
xorl %r8d, %r8d
pushq $0x2
popq %r12
movq 0x40(%rsp), %rax
movl %eax, %ebp
movl $0x0, 0x4(%rsp)
movl $0x0, (%rsp)
xorl %ebx, %ebx
movl $0x0, 0x8(%rsp)
xorl %r15d, %r15d
xorl %ecx, %ecx
xorl %edx, %edx
xorl %eax, %eax
movq %r14, 0xb8(%rsp)
subl $0x1, %ebp
jb 0x78288
movl %r15d, 0xc(%rsp)
movl %ebx, 0x10(%rsp)
movl %r8d, 0x14(%rsp)
movq %rsi, 0xc0(%rsp)
movq 0xa8(%rsp), %r8
movswl -0x4(%r8,%r12,2), %esi
addl %esi, %eax
addl $0x8, %eax
sarl $0x4, %eax
movswl -0x2(%r8,%r12,2), %esi
addl %esi, %edx
addl $0x8, %edx
sarl $0x4, %edx
movswl (%r8,%r12,2), %esi
addl %esi, %ecx
addl $0x8, %ecx
sarl $0x4, %ecx
cltq
movslq (%r9,%rax,4), %rax
movslq %edx, %rdx
movslq (%r9,%rdx,4), %rdx
movslq %ecx, %rcx
movslq (%r9,%rcx,4), %rcx
movzbl -0x2(%r14,%r12), %esi
addq %rax, %rsi
movzbl -0x1(%r14,%r12), %eax
addq %rdx, %rax
movzbl (%r14,%r12), %edx
addq %rcx, %rdx
movzbl (%r10,%rsi), %r15d
movzbl (%r10,%rax), %r14d
movzbl (%r10,%rdx), %ebx
movl %r15d, %esi
andl $-0x8, %esi
movl %r14d, %edx
shrl $0x2, %edx
movl %ebx, %ecx
shrl $0x3, %ecx
movl %edx, %eax
shll $0x6, %eax
movq 0xa0(%rsp), %r8
addq (%r8,%rsi), %rax
movzwl (%rax,%rcx,2), %esi
testw %si, %si
jne 0x781e8
movl %r15d, %esi
shrl $0x3, %esi
leaq (%rax,%rcx,2), %rax
movq %rax, 0x98(%rsp)
movq 0x90(%rsp), %rdi
movq %r9, %r13
callq 0x78756
movq 0x78(%rsp), %r11
movq 0x80(%rsp), %rdi
movq 0x88(%rsp), %r10
movq %r13, %r9
movq 0x70(%rsp), %r13
movq 0x98(%rsp), %rax
movzwl (%rax), %esi
movzwl %si, %eax
leaq -0x1(%rax), %rcx
movq 0xc0(%rsp), %rsi
movb %cl, (%rsi)
movzbl -0x1(%rdi,%rax), %ecx
subl %ecx, %r15d
movzbl -0x1(%r11,%rax), %ecx
subl %ecx, %r14d
movzbl -0x1(%r13,%rax), %eax
subl %eax, %ebx
leal (%r15,%r15,2), %eax
addl (%rsp), %eax
movq 0x20(%rsp), %r8
movw %ax, -0x4(%r8,%r12,2)
leal (%r15,%r15,4), %eax
addl 0xc(%rsp), %eax
movl %eax, (%rsp)
leal (%r14,%r14,2), %ecx
addl 0x4(%rsp), %ecx
imull $0x7, %r15d, %eax
movw %cx, -0x2(%r8,%r12,2)
leal (%r14,%r14,4), %ecx
addl 0x8(%rsp), %ecx
movl %ecx, 0x4(%rsp)
leal (%rbx,%rbx,2), %ecx
addl 0x14(%rsp), %ecx
imull $0x7, %r14d, %edx
movw %cx, (%r8,%r12,2)
leal (%rbx,%rbx,4), %r8d
addl 0x10(%rsp), %r8d
imull $0x7, %ebx, %ecx
addq 0x18(%rsp), %rsi
addq 0xb0(%rsp), %r12
movl %r14d, 0x8(%rsp)
movq 0xb8(%rsp), %r14
jmp 0x780e6
movq 0x20(%rsp), %rax
movl (%rsp), %ecx
movw %cx, -0x4(%rax,%r12,2)
movl 0x4(%rsp), %ecx
movw %cx, -0x2(%rax,%r12,2)
movw %r8w, (%rax,%r12,2)
movq 0x68(%rsp), %rbx
incq %rbx
movq 0x48(%rsp), %rdx
jmp 0x78030
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq