// File: tomfunkhouser[P]gaps/pkgs/jpeg/jquant2.c
// Function: update_box
LOCAL(void)
update_box (j_decompress_ptr cinfo, boxptr boxp)
/* Shrink the min/max bounds of a box to enclose only nonzero elements, */
/* and recompute its volume and population */
{
  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
  hist3d histogram = cquantize->histogram;
  histptr histp;
  int c0,c1,c2;
  int c0min,c0max,c1min,c1max,c2min,c2max;
  INT32 dist0,dist1,dist2;
  long ccount;
  
  c0min = boxp->c0min;  c0max = boxp->c0max;
  c1min = boxp->c1min;  c1max = boxp->c1max;
  c2min = boxp->c2min;  c2max = boxp->c2max;
  
  if (c0max > c0min)
    for (c0 = c0min; c0 <= c0max; c0++)
      for (c1 = c1min; c1 <= c1max; c1++) {
	histp = & histogram[c0][c1][c2min];
	for (c2 = c2min; c2 <= c2max; c2++)
	  if (*histp++ != 0) {
	    boxp->c0min = c0min = c0;
	    goto have_c0min;
	  }
      }
 have_c0min:
  if (c0max > c0min)
    for (c0 = c0max; c0 >= c0min; c0--)
      for (c1 = c1min; c1 <= c1max; c1++) {
	histp = & histogram[c0][c1][c2min];
	for (c2 = c2min; c2 <= c2max; c2++)
	  if (*histp++ != 0) {
	    boxp->c0max = c0max = c0;
	    goto have_c0max;
	  }
      }
 have_c0max:
  if (c1max > c1min)
    for (c1 = c1min; c1 <= c1max; c1++)
      for (c0 = c0min; c0 <= c0max; c0++) {
	histp = & histogram[c0][c1][c2min];
	for (c2 = c2min; c2 <= c2max; c2++)
	  if (*histp++ != 0) {
	    boxp->c1min = c1min = c1;
	    goto have_c1min;
	  }
      }
 have_c1min:
  if (c1max > c1min)
    for (c1 = c1max; c1 >= c1min; c1--)
      for (c0 = c0min; c0 <= c0max; c0++) {
	histp = & histogram[c0][c1][c2min];
	for (c2 = c2min; c2 <= c2max; c2++)
	  if (*histp++ != 0) {
	    boxp->c1max = c1max = c1;
	    goto have_c1max;
	  }
      }
 have_c1max:
  if (c2max > c2min)
    for (c2 = c2min; c2 <= c2max; c2++)
      for (c0 = c0min; c0 <= c0max; c0++) {
	histp = & histogram[c0][c1min][c2];
	for (c1 = c1min; c1 <= c1max; c1++, histp += HIST_C2_ELEMS)
	  if (*histp != 0) {
	    boxp->c2min = c2min = c2;
	    goto have_c2min;
	  }
      }
 have_c2min:
  if (c2max > c2min)
    for (c2 = c2max; c2 >= c2min; c2--)
      for (c0 = c0min; c0 <= c0max; c0++) {
	histp = & histogram[c0][c1min][c2];
	for (c1 = c1min; c1 <= c1max; c1++, histp += HIST_C2_ELEMS)
	  if (*histp != 0) {
	    boxp->c2max = c2max = c2;
	    goto have_c2max;
	  }
      }
 have_c2max:

  /* Update box volume.
   * We use 2-norm rather than real volume here; this biases the method
   * against making long narrow boxes, and it has the side benefit that
   * a box is splittable iff norm > 0.
   * Since the differences are expressed in histogram-cell units,
   * we have to shift back to JSAMPLE units to get consistent distances;
   * after which, we scale according to the selected distance scale factors.
   */
  dist0 = ((c0max - c0min) << C0_SHIFT) * C0_SCALE;
  dist1 = ((c1max - c1min) << C1_SHIFT) * C1_SCALE;
  dist2 = ((c2max - c2min) << C2_SHIFT) * C2_SCALE;
  boxp->volume = dist0*dist0 + dist1*dist1 + dist2*dist2;
  
  /* Now scan remaining volume of box and compute population */
  ccount = 0;
  for (c0 = c0min; c0 <= c0max; c0++)
    for (c1 = c1min; c1 <= c1max; c1++) {
      histp = & histogram[c0][c1][c2min];
      for (c2 = c2min; c2 <= c2max; c2++, histp++)
	if (*histp != 0) {
	  ccount++;
	}
    }
  boxp->colorcount = ccount;
}

// Assembly:
movq %rdi, -0x8(%rsp)
movq %rsi, -0x10(%rsp)
movq -0x8(%rsp), %rax
movq 0x270(%rax), %rax
movq %rax, -0x18(%rsp)
movq -0x18(%rsp), %rax
movq 0x30(%rax), %rax
movq %rax, -0x20(%rsp)
movq -0x10(%rsp), %rax
movl (%rax), %eax
movl %eax, -0x38(%rsp)
movq -0x10(%rsp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x3c(%rsp)
movq -0x10(%rsp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x40(%rsp)
movq -0x10(%rsp), %rax
movl 0xc(%rax), %eax
movl %eax, -0x44(%rsp)
movq -0x10(%rsp), %rax
movl 0x10(%rax), %eax
movl %eax, -0x48(%rsp)
movq -0x10(%rsp), %rax
movl 0x14(%rax), %eax
movl %eax, -0x4c(%rsp)
movl -0x3c(%rsp), %eax
cmpl -0x38(%rsp), %eax
jle 0xf6bc5
movl -0x38(%rsp), %eax
movl %eax, -0x2c(%rsp)
movl -0x2c(%rsp), %eax
cmpl -0x3c(%rsp), %eax
jg 0xf6bc3
movl -0x40(%rsp), %eax
movl %eax, -0x30(%rsp)
movl -0x30(%rsp), %eax
cmpl -0x44(%rsp), %eax
jg 0xf6bb1
movq -0x20(%rsp), %rax
movslq -0x2c(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x30(%rsp), %rcx
shlq $0x6, %rcx
addq %rcx, %rax
movslq -0x48(%rsp), %rcx
shlq %rcx
addq %rcx, %rax
movq %rax, -0x28(%rsp)
movl -0x48(%rsp), %eax
movl %eax, -0x34(%rsp)
movl -0x34(%rsp), %eax
cmpl -0x4c(%rsp), %eax
jg 0xf6b9f
movq -0x28(%rsp), %rax
movq %rax, %rcx
addq $0x2, %rcx
movq %rcx, -0x28(%rsp)
movzwl (%rax), %eax
cmpl $0x0, %eax
je 0xf6b90
movl -0x2c(%rsp), %ecx
movl %ecx, -0x38(%rsp)
movq -0x10(%rsp), %rax
movl %ecx, (%rax)
jmp 0xf6bc7
jmp 0xf6b92
movl -0x34(%rsp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rsp)
jmp 0xf6b5c
jmp 0xf6ba1
movl -0x30(%rsp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rsp)
jmp 0xf6b1c
jmp 0xf6bb3
movl -0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rsp)
jmp 0xf6b06
jmp 0xf6bc5
jmp 0xf6bc7
movl -0x3c(%rsp), %eax
cmpl -0x38(%rsp), %eax
jle 0xf6c9d
movl -0x3c(%rsp), %eax
movl %eax, -0x2c(%rsp)
movl -0x2c(%rsp), %eax
cmpl -0x38(%rsp), %eax
jl 0xf6c9b
movl -0x40(%rsp), %eax
movl %eax, -0x30(%rsp)
movl -0x30(%rsp), %eax
cmpl -0x44(%rsp), %eax
jg 0xf6c89
movq -0x20(%rsp), %rax
movslq -0x2c(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x30(%rsp), %rcx
shlq $0x6, %rcx
addq %rcx, %rax
movslq -0x48(%rsp), %rcx
shlq %rcx
addq %rcx, %rax
movq %rax, -0x28(%rsp)
movl -0x48(%rsp), %eax
movl %eax, -0x34(%rsp)
movl -0x34(%rsp), %eax
cmpl -0x4c(%rsp), %eax
jg 0xf6c77
movq -0x28(%rsp), %rax
movq %rax, %rcx
addq $0x2, %rcx
movq %rcx, -0x28(%rsp)
movzwl (%rax), %eax
cmpl $0x0, %eax
je 0xf6c68
movl -0x2c(%rsp), %ecx
movl %ecx, -0x3c(%rsp)
movq -0x10(%rsp), %rax
movl %ecx, 0x4(%rax)
jmp 0xf6c9f
jmp 0xf6c6a
movl -0x34(%rsp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rsp)
jmp 0xf6c33
jmp 0xf6c79
movl -0x30(%rsp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rsp)
jmp 0xf6bf3
jmp 0xf6c8b
movl -0x2c(%rsp), %eax
addl $-0x1, %eax
movl %eax, -0x2c(%rsp)
jmp 0xf6bdd
jmp 0xf6c9d
jmp 0xf6c9f
movl -0x44(%rsp), %eax
cmpl -0x40(%rsp), %eax
jle 0xf6d75
movl -0x40(%rsp), %eax
movl %eax, -0x30(%rsp)
movl -0x30(%rsp), %eax
cmpl -0x44(%rsp), %eax
jg 0xf6d73
movl -0x38(%rsp), %eax
movl %eax, -0x2c(%rsp)
movl -0x2c(%rsp), %eax
cmpl -0x3c(%rsp), %eax
jg 0xf6d61
movq -0x20(%rsp), %rax
movslq -0x2c(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x30(%rsp), %rcx
shlq $0x6, %rcx
addq %rcx, %rax
movslq -0x48(%rsp), %rcx
shlq %rcx
addq %rcx, %rax
movq %rax, -0x28(%rsp)
movl -0x48(%rsp), %eax
movl %eax, -0x34(%rsp)
movl -0x34(%rsp), %eax
cmpl -0x4c(%rsp), %eax
jg 0xf6d4f
movq -0x28(%rsp), %rax
movq %rax, %rcx
addq $0x2, %rcx
movq %rcx, -0x28(%rsp)
movzwl (%rax), %eax
cmpl $0x0, %eax
je 0xf6d40
movl -0x30(%rsp), %ecx
movl %ecx, -0x40(%rsp)
movq -0x10(%rsp), %rax
movl %ecx, 0x8(%rax)
jmp 0xf6d77
jmp 0xf6d42
movl -0x34(%rsp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rsp)
jmp 0xf6d0b
jmp 0xf6d51
movl -0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rsp)
jmp 0xf6ccb
jmp 0xf6d63
movl -0x30(%rsp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rsp)
jmp 0xf6cb5
jmp 0xf6d75
jmp 0xf6d77
movl -0x44(%rsp), %eax
cmpl -0x40(%rsp), %eax
jle 0xf6e4d
movl -0x44(%rsp), %eax
movl %eax, -0x30(%rsp)
movl -0x30(%rsp), %eax
cmpl -0x40(%rsp), %eax
jl 0xf6e4b
movl -0x38(%rsp), %eax
movl %eax, -0x2c(%rsp)
movl -0x2c(%rsp), %eax
cmpl -0x3c(%rsp), %eax
jg 0xf6e39
movq -0x20(%rsp), %rax
movslq -0x2c(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x30(%rsp), %rcx
shlq $0x6, %rcx
addq %rcx, %rax
movslq -0x48(%rsp), %rcx
shlq %rcx
addq %rcx, %rax
movq %rax, -0x28(%rsp)
movl -0x48(%rsp), %eax
movl %eax, -0x34(%rsp)
movl -0x34(%rsp), %eax
cmpl -0x4c(%rsp), %eax
jg 0xf6e27
movq -0x28(%rsp), %rax
movq %rax, %rcx
addq $0x2, %rcx
movq %rcx, -0x28(%rsp)
movzwl (%rax), %eax
cmpl $0x0, %eax
je 0xf6e18
movl -0x30(%rsp), %ecx
movl %ecx, -0x44(%rsp)
movq -0x10(%rsp), %rax
movl %ecx, 0xc(%rax)
jmp 0xf6e4f
jmp 0xf6e1a
movl -0x34(%rsp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rsp)
jmp 0xf6de3
jmp 0xf6e29
movl -0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rsp)
jmp 0xf6da3
jmp 0xf6e3b
movl -0x30(%rsp), %eax
addl $-0x1, %eax
movl %eax, -0x30(%rsp)
jmp 0xf6d8d
jmp 0xf6e4d
jmp 0xf6e4f
movl -0x4c(%rsp), %eax
cmpl -0x48(%rsp), %eax
jle 0xf6f27
movl -0x48(%rsp), %eax
movl %eax, -0x34(%rsp)
movl -0x34(%rsp), %eax
cmpl -0x4c(%rsp), %eax
jg 0xf6f25
movl -0x38(%rsp), %eax
movl %eax, -0x2c(%rsp)
movl -0x2c(%rsp), %eax
cmpl -0x3c(%rsp), %eax
jg 0xf6f13
movq -0x20(%rsp), %rax
movslq -0x2c(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x40(%rsp), %rcx
shlq $0x6, %rcx
addq %rcx, %rax
movslq -0x34(%rsp), %rcx
shlq %rcx
addq %rcx, %rax
movq %rax, -0x28(%rsp)
movl -0x40(%rsp), %eax
movl %eax, -0x30(%rsp)
movl -0x30(%rsp), %eax
cmpl -0x44(%rsp), %eax
jg 0xf6f01
movq -0x28(%rsp), %rax
movzwl (%rax), %eax
cmpl $0x0, %eax
je 0xf6ee4
movl -0x34(%rsp), %ecx
movl %ecx, -0x48(%rsp)
movq -0x10(%rsp), %rax
movl %ecx, 0x10(%rax)
jmp 0xf6f29
jmp 0xf6ee6
movl -0x30(%rsp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rsp)
movq -0x28(%rsp), %rax
addq $0x40, %rax
movq %rax, -0x28(%rsp)
jmp 0xf6ebb
jmp 0xf6f03
movl -0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rsp)
jmp 0xf6e7b
jmp 0xf6f15
movl -0x34(%rsp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rsp)
jmp 0xf6e65
jmp 0xf6f27
jmp 0xf6f29
movl -0x4c(%rsp), %eax
cmpl -0x48(%rsp), %eax
jle 0xf7001
movl -0x4c(%rsp), %eax
movl %eax, -0x34(%rsp)
movl -0x34(%rsp), %eax
cmpl -0x48(%rsp), %eax
jl 0xf6fff
movl -0x38(%rsp), %eax
movl %eax, -0x2c(%rsp)
movl -0x2c(%rsp), %eax
cmpl -0x3c(%rsp), %eax
jg 0xf6fed
movq -0x20(%rsp), %rax
movslq -0x2c(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x40(%rsp), %rcx
shlq $0x6, %rcx
addq %rcx, %rax
movslq -0x34(%rsp), %rcx
shlq %rcx
addq %rcx, %rax
movq %rax, -0x28(%rsp)
movl -0x40(%rsp), %eax
movl %eax, -0x30(%rsp)
movl -0x30(%rsp), %eax
cmpl -0x44(%rsp), %eax
jg 0xf6fdb
movq -0x28(%rsp), %rax
movzwl (%rax), %eax
cmpl $0x0, %eax
je 0xf6fbe
movl -0x34(%rsp), %ecx
movl %ecx, -0x4c(%rsp)
movq -0x10(%rsp), %rax
movl %ecx, 0x14(%rax)
jmp 0xf7003
jmp 0xf6fc0
movl -0x30(%rsp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rsp)
movq -0x28(%rsp), %rax
addq $0x40, %rax
movq %rax, -0x28(%rsp)
jmp 0xf6f95
jmp 0xf6fdd
movl -0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rsp)
jmp 0xf6f55
jmp 0xf6fef
movl -0x34(%rsp), %eax
addl $-0x1, %eax
movl %eax, -0x34(%rsp)
jmp 0xf6f3f
jmp 0xf7001
jmp 0xf7003
movl -0x3c(%rsp), %eax
subl -0x38(%rsp), %eax
shll $0x3, %eax
shll %eax
cltq
movq %rax, -0x58(%rsp)
movl -0x44(%rsp), %eax
subl -0x40(%rsp), %eax
shll $0x2, %eax
imull $0x3, %eax, %eax
cltq
movq %rax, -0x60(%rsp)
movl -0x4c(%rsp), %eax
subl -0x48(%rsp), %eax
shll $0x3, %eax
shll $0x0, %eax
cltq
movq %rax, -0x68(%rsp)
movq -0x58(%rsp), %rcx
imulq -0x58(%rsp), %rcx
movq -0x60(%rsp), %rax
imulq -0x60(%rsp), %rax
addq %rax, %rcx
movq -0x68(%rsp), %rax
imulq -0x68(%rsp), %rax
addq %rax, %rcx
movq -0x10(%rsp), %rax
movq %rcx, 0x18(%rax)
movq $0x0, -0x70(%rsp)
movl -0x38(%rsp), %eax
movl %eax, -0x2c(%rsp)
movl -0x2c(%rsp), %eax
cmpl -0x3c(%rsp), %eax
jg 0xf713e
movl -0x40(%rsp), %eax
movl %eax, -0x30(%rsp)
movl -0x30(%rsp), %eax
cmpl -0x44(%rsp), %eax
jg 0xf712c
movq -0x20(%rsp), %rax
movslq -0x2c(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x30(%rsp), %rcx
shlq $0x6, %rcx
addq %rcx, %rax
movslq -0x48(%rsp), %rcx
shlq %rcx
addq %rcx, %rax
movq %rax, -0x28(%rsp)
movl -0x48(%rsp), %eax
movl %eax, -0x34(%rsp)
movl -0x34(%rsp), %eax
cmpl -0x4c(%rsp), %eax
jg 0xf711a
movq -0x28(%rsp), %rax
movzwl (%rax), %eax
cmpl $0x0, %eax
je 0xf70fd
movq -0x70(%rsp), %rax
addq $0x1, %rax
movq %rax, -0x70(%rsp)
jmp 0xf70ff
movl -0x34(%rsp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rsp)
movq -0x28(%rsp), %rax
addq $0x2, %rax
movq %rax, -0x28(%rsp)
jmp 0xf70d8
jmp 0xf711c
movl -0x30(%rsp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rsp)
jmp 0xf7098
jmp 0xf712e
movl -0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rsp)
jmp 0xf7082
movq -0x70(%rsp), %rcx
movq -0x10(%rsp), %rax
movq %rcx, 0x20(%rax)
retq
nopl (%rax)