// File: tomfunkhouser[P]gaps/pkgs/jpeg/jchuff.c
// Function: htest_one_block
LOCAL(void)
htest_one_block (j_compress_ptr cinfo, JCOEFPTR block, int last_dc_val,
		 long dc_counts[], long ac_counts[])
{
  register int temp;
  register int nbits;
  register int k, r;
  
  /* Encode the DC coefficient difference per section F.1.2.1 */
  
  temp = block[0] - last_dc_val;
  if (temp < 0)
    temp = -temp;
  
  /* Find the number of bits needed for the magnitude of the coefficient */
  nbits = 0;
  while (temp) {
    nbits++;
    temp >>= 1;
  }
  /* Check for out-of-range coefficient values.
   * Since we're encoding a difference, the range limit is twice as much.
   */
  if (nbits > MAX_COEF_BITS+1)
    ERREXIT(cinfo, JERR_BAD_DCT_COEF);

  /* Count the Huffman symbol for the number of bits */
  dc_counts[nbits]++;
  
  /* Encode the AC coefficients per section F.1.2.2 */
  
  r = 0;			/* r = run length of zeros */
  
  for (k = 1; k < DCTSIZE2; k++) {
    if ((temp = block[jpeg_natural_order[k]]) == 0) {
      r++;
    } else {
      /* if run length > 15, must emit special run-length-16 codes (0xF0) */
      while (r > 15) {
	ac_counts[0xF0]++;
	r -= 16;
      }
      
      /* Find the number of bits needed for the magnitude of the coefficient */
      if (temp < 0)
	temp = -temp;
      
      /* Find the number of bits needed for the magnitude of the coefficient */
      nbits = 1;		/* there must be at least one 1 bit */
      while ((temp >>= 1))
	nbits++;
      /* Check for out-of-range coefficient values */
      if (nbits > MAX_COEF_BITS)
	ERREXIT(cinfo, JERR_BAD_DCT_COEF);
      
      /* Count Huffman symbol for run length / number of bits */
      ac_counts[(r << 4) + nbits]++;
      
      r = 0;
    }
  }

  /* If the last coef(s) were zero, emit an end-of-block code */
  if (r > 0)
    ac_counts[0]++;
}

// Assembly:
subq $0x38, %rsp
movq %rdi, 0x30(%rsp)
movq %rsi, 0x28(%rsp)
movl %edx, 0x24(%rsp)
movq %rcx, 0x18(%rsp)
movq %r8, 0x10(%rsp)
movq 0x28(%rsp), %rax
movswl (%rax), %eax
subl 0x24(%rsp), %eax
movl %eax, 0xc(%rsp)
cmpl $0x0, 0xc(%rsp)
jge 0xde74d
xorl %eax, %eax
subl 0xc(%rsp), %eax
movl %eax, 0xc(%rsp)
movl $0x0, 0x8(%rsp)
cmpl $0x0, 0xc(%rsp)
je 0xde773
movl 0x8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x8(%rsp)
movl 0xc(%rsp), %eax
sarl %eax
movl %eax, 0xc(%rsp)
jmp 0xde755
cmpl $0xb, 0x8(%rsp)
jle 0xde79b
movq 0x30(%rsp), %rax
movq (%rax), %rax
movl $0x6, 0x28(%rax)
movq 0x30(%rsp), %rax
movq (%rax), %rax
movq (%rax), %rax
movq 0x30(%rsp), %rdi
callq *%rax
movq 0x18(%rsp), %rax
movslq 0x8(%rsp), %rcx
movq (%rax,%rcx,8), %rdx
addq $0x1, %rdx
movq %rdx, (%rax,%rcx,8)
movl $0x0, (%rsp)
movl $0x1, 0x4(%rsp)
cmpl $0x40, 0x4(%rsp)
jge 0xde8b9
movq 0x28(%rsp), %rax
movslq 0x4(%rsp), %rdx
leaq 0x58d84(%rip), %rcx     # 0x137560
movslq (%rcx,%rdx,4), %rcx
movswl (%rax,%rcx,2), %eax
movl %eax, 0xc(%rsp)
cmpl $0x0, %eax
jne 0xde7fb
movl (%rsp), %eax
addl $0x1, %eax
movl %eax, (%rsp)
jmp 0xde8a7
jmp 0xde7fd
cmpl $0xf, (%rsp)
jle 0xde825
movq 0x10(%rsp), %rax
movq 0x780(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x780(%rax)
movl (%rsp), %eax
subl $0x10, %eax
movl %eax, (%rsp)
jmp 0xde7fd
cmpl $0x0, 0xc(%rsp)
jge 0xde836
xorl %eax, %eax
subl 0xc(%rsp), %eax
movl %eax, 0xc(%rsp)
movl $0x1, 0x8(%rsp)
movl 0xc(%rsp), %eax
sarl %eax
movl %eax, 0xc(%rsp)
cmpl $0x0, %eax
je 0xde85a
movl 0x8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x8(%rsp)
jmp 0xde83e
cmpl $0xa, 0x8(%rsp)
jle 0xde882
movq 0x30(%rsp), %rax
movq (%rax), %rax
movl $0x6, 0x28(%rax)
movq 0x30(%rsp), %rax
movq (%rax), %rax
movq (%rax), %rax
movq 0x30(%rsp), %rdi
callq *%rax
movq 0x10(%rsp), %rax
movl (%rsp), %ecx
shll $0x4, %ecx
addl 0x8(%rsp), %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rdx
addq $0x1, %rdx
movq %rdx, (%rax,%rcx,8)
movl $0x0, (%rsp)
jmp 0xde8a9
movl 0x4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4(%rsp)
jmp 0xde7c0
cmpl $0x0, (%rsp)
jle 0xde8ce
movq 0x10(%rsp), %rax
movq (%rax), %rcx
addq $0x1, %rcx
movq %rcx, (%rax)
addq $0x38, %rsp
retq
nopw %cs:(%rax,%rax)