// File: /workspace/github_c_makefile_star2_O0/jonof[P]jftekwar/jfbuild/src/rg_etc1.cpp
// Function: rg_etc1::etc1_optimizer::init(rg_etc1::etc1_optimizer::params const&, rg_etc1::etc1_optimizer::results&)
void etc1_optimizer::init(const params& p, results& r)
   {
      // This version is hardcoded for 8 pixel subblocks.
      RG_ETC1_ASSERT(p.m_num_src_pixels == 8);
      
      m_pParams = &p;
      m_pResult = &r;
                  
      const uint n = 8;
      
      m_limit = m_pParams->m_use_color4 ? 15 : 31;

      vec3F avg_color(0.0f);

      for (uint i = 0; i < n; i++)
      {
         const color_quad_u8& c = m_pParams->m_pSrc_pixels[i];
         const vec3F fc(c.r, c.g, c.b);

         avg_color += fc;

         m_luma[i] = static_cast<uint16>(c.r + c.g + c.b);
         m_sorted_luma[0][i] = i;
      }
      avg_color *= (1.0f / static_cast<float>(n));
      m_avg_color = avg_color;

      m_br = rg_etc1::clamp<int>(static_cast<uint>(m_avg_color[0] * m_limit / 255.0f + .5f), 0, m_limit);
      m_bg = rg_etc1::clamp<int>(static_cast<uint>(m_avg_color[1] * m_limit / 255.0f + .5f), 0, m_limit);
      m_bb = rg_etc1::clamp<int>(static_cast<uint>(m_avg_color[2] * m_limit / 255.0f + .5f), 0, m_limit);

      if (m_pParams->m_quality <= cMediumQuality)
      {
         m_pSorted_luma_indices = indirect_radix_sort(n, m_sorted_luma[0], m_sorted_luma[1], m_luma, 0, sizeof(m_luma[0]), false);
         m_pSorted_luma = m_sorted_luma[0];
         if (m_pSorted_luma_indices == m_sorted_luma[0])
            m_pSorted_luma = m_sorted_luma[1];
      
         for (uint i = 0; i < n; i++)
            m_pSorted_luma[i] = m_luma[m_pSorted_luma_indices[i]];
      }
      
      m_best_solution.m_coords.clear();
      m_best_solution.m_valid = false;
      m_best_solution.m_error = cUINT64_MAX;
   }

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x8, 0x8(%rax)
jne 0x9fa98
jmp 0x9fab7
leaq 0x18a77(%rip), %rdi     # 0xb8516
leaq 0x1887d(%rip), %rsi     # 0xb8323
movl $0x660, %edx            # imm = 0x660
leaq 0x18a7c(%rip), %rcx     # 0xb852e
callq 0x61a0
movq -0x50(%rbp), %rax
movq -0x10(%rbp), %rcx
movq %rcx, (%rax)
movq -0x18(%rbp), %rcx
movq %rcx, 0x8(%rax)
movl $0x8, -0x1c(%rbp)
movq (%rax), %rcx
movb 0x18(%rcx), %sil
movl $0x1f, %ecx
movl $0xf, %edx
testb $0x1, %sil
cmovnel %edx, %ecx
movl %ecx, 0x10(%rax)
leaq -0x28(%rbp), %rdi
xorps %xmm0, %xmm0
callq 0xa2180
movl $0x0, -0x2c(%rbp)
cmpl $0x8, -0x2c(%rbp)
jae 0x9fb92
movq -0x50(%rbp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
movl -0x2c(%rbp), %ecx
leaq (%rax,%rcx,4), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movzbl (%rax), %ecx
cvtsi2ss %ecx, %xmm0
movzbl 0x1(%rax), %ecx
cvtsi2ss %ecx, %xmm1
movzbl 0x2(%rax), %eax
cvtsi2ss %eax, %xmm2
leaq -0x44(%rbp), %rdi
callq 0xa21b0
leaq -0x28(%rbp), %rdi
leaq -0x44(%rbp), %rsi
callq 0xa21f0
movq -0x50(%rbp), %rax
movq -0x38(%rbp), %rcx
movzbl (%rcx), %ecx
movq -0x38(%rbp), %rdx
movzbl 0x1(%rdx), %edx
addl %edx, %ecx
movq -0x38(%rbp), %rdx
movzbl 0x2(%rdx), %edx
addl %edx, %ecx
movw %cx, %dx
movl -0x2c(%rbp), %ecx
movw %dx, 0x2c(%rax,%rcx,2)
movl -0x2c(%rbp), %edx
movl -0x2c(%rbp), %ecx
movl %edx, 0x3c(%rax,%rcx,4)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x9faff
leaq -0x28(%rbp), %rdi
movss 0x1874a(%rip), %xmm0    # 0xb82e8
callq 0xa2240
movq -0x50(%rbp), %rax
movq %rax, %rdi
addq $0x14, %rdi
movq %rdi, -0x60(%rbp)
movl -0x20(%rbp), %ecx
movl %ecx, 0x1c(%rax)
movq -0x28(%rbp), %rcx
movq %rcx, 0x14(%rax)
xorl %esi, %esi
movl %esi, -0x64(%rbp)
callq 0xa2130
movl -0x64(%rbp), %esi
movq -0x50(%rbp), %rax
movl 0x10(%rax), %edx
cvtsi2ss %edx, %xmm1
mulss %xmm1, %xmm0
movss 0x169f4(%rip), %xmm1    # 0xb65d8
movss %xmm1, -0x58(%rbp)
divss %xmm1, %xmm0
movss 0x169d3(%rip), %xmm1    # 0xb65c8
movss %xmm1, -0x54(%rbp)
addss %xmm1, %xmm0
cvttss2si %xmm0, %rax
movl %eax, %edi
callq 0xa1aa0
movq -0x60(%rbp), %rdi
movl %eax, %ecx
movq -0x50(%rbp), %rax
movl %ecx, 0x20(%rax)
movl $0x1, %esi
callq 0xa2130
movl -0x64(%rbp), %esi
movss -0x58(%rbp), %xmm2
movss -0x54(%rbp), %xmm1
movq -0x50(%rbp), %rax
movl 0x10(%rax), %edx
cvtsi2ss %edx, %xmm3
mulss %xmm3, %xmm0
divss %xmm2, %xmm0
addss %xmm1, %xmm0
cvttss2si %xmm0, %rax
movl %eax, %edi
callq 0xa1aa0
movq -0x60(%rbp), %rdi
movl %eax, %ecx
movq -0x50(%rbp), %rax
movl %ecx, 0x24(%rax)
movl $0x2, %esi
callq 0xa2130
movss -0x58(%rbp), %xmm2
movss -0x54(%rbp), %xmm1
movq -0x50(%rbp), %rax
cvtsi2ssl 0x10(%rax), %xmm3
mulss %xmm3, %xmm0
divss %xmm2, %xmm0
addss %xmm1, %xmm0
cvttss2si %xmm0, %rcx
movl %ecx, %edi
movl 0x10(%rax), %edx
xorl %esi, %esi
callq 0xa1aa0
movl %eax, %ecx
movq -0x50(%rbp), %rax
movl %ecx, 0x28(%rax)
movq (%rax), %rax
cmpl $0x1, (%rax)
jg 0x9fd5d
movq -0x50(%rbp), %rcx
movq %rcx, %rsi
addq $0x3c, %rsi
movq %rcx, %rdx
addq $0x3c, %rdx
addq $0x20, %rdx
addq $0x2c, %rcx
movl $0x8, %edi
xorl %r8d, %r8d
movl $0x2, %r9d
movl $0x0, (%rsp)
callq 0xa2290
movq -0x50(%rbp), %rcx
movq %rax, 0x80(%rcx)
movq %rcx, %rax
addq $0x3c, %rax
movq %rax, 0x88(%rcx)
movq 0x80(%rcx), %rax
addq $0x3c, %rcx
cmpq %rcx, %rax
jne 0x9fd20
movq -0x50(%rbp), %rax
movq %rax, %rcx
addq $0x3c, %rcx
addq $0x20, %rcx
movq %rcx, 0x88(%rax)
movl $0x0, -0x48(%rbp)
cmpl $0x8, -0x48(%rbp)
jae 0x9fd5b
movq -0x50(%rbp), %rax
movq 0x80(%rax), %rcx
movl -0x48(%rbp), %edx
movl (%rcx,%rdx,4), %ecx
movzwl 0x2c(%rax,%rcx,2), %edx
movq 0x88(%rax), %rax
movl -0x48(%rbp), %ecx
movl %edx, (%rax,%rcx,4)
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x9fd27
jmp 0x9fd5d
movq -0x50(%rbp), %rdi
addq $0xa0, %rdi
callq 0xa2c30
movq -0x50(%rbp), %rax
movb $0x0, 0xc0(%rax)
movq $-0x1, 0xb8(%rax)
addq $0x70, %rsp
popq %rbp
retq
nopl (%rax)