// File: /workspace/github_c_makefile_star2_O1/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 %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x20, %rsp
cmpl $0x8, 0x8(%rsi)
jne 0x695cd
movq %rdi, %rbx
movq %rsi, (%rdi)
movq %rdx, 0x8(%rdi)
movzbl 0x18(%rsi), %eax
xorl $0x1, %eax
shll $0x4, %eax
orl $0xf, %eax
movl %eax, 0x10(%rdi)
movq $0x0, 0x8(%rsp)
movl $0x0, 0x10(%rsp)
movq 0x10(%rsi), %rcx
xorl %edx, %edx
movzbl (%rcx,%rdx,4), %edi
movzwl %di, %edi
xorps %xmm0, %xmm0
cvtsi2ss %edi, %xmm0
movzbl 0x1(%rcx,%rdx,4), %r8d
movzwl %r8w, %r8d
xorps %xmm1, %xmm1
cvtsi2ss %r8d, %xmm1
movzbl 0x2(%rcx,%rdx,4), %r9d
movzwl %r9w, %r9d
xorps %xmm2, %xmm2
cvtsi2ss %r9d, %xmm2
movss %xmm0, 0x14(%rsp)
movss %xmm1, 0x18(%rsp)
movss %xmm2, 0x1c(%rsp)
xorl %r10d, %r10d
movss 0x14(%rsp,%r10,4), %xmm0
addss 0x8(%rsp,%r10,4), %xmm0
movss %xmm0, 0x8(%rsp,%r10,4)
incq %r10
cmpq $0x3, %r10
jne 0x6944d
addl %edi, %r8d
addl %r9d, %r8d
movw %r8w, 0x2c(%rbx,%rdx,2)
movl %edx, 0x3c(%rbx,%rdx,4)
incq %rdx
cmpq $0x8, %rdx
jne 0x69406
xorl %ecx, %ecx
movss 0x11ce2(%rip), %xmm0    # 0x7b170
movss 0x8(%rsp,%rcx,4), %xmm1
mulss %xmm0, %xmm1
movss %xmm1, 0x8(%rsp,%rcx,4)
incq %rcx
cmpq $0x3, %rcx
jne 0x6948e
movq 0x8(%rsp), %rcx
movq %rcx, 0x14(%rbx)
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
movss 0x14(%rbx), %xmm3
mulss %xmm0, %xmm3
movss 0xfdec(%rip), %xmm1     # 0x792b4
divss %xmm1, %xmm3
movss 0xfdd4(%rip), %xmm2     # 0x792a8
addss %xmm2, %xmm3
cvttss2si %xmm3, %rdx
cmpl %eax, %edx
movl %eax, %edi
cmovll %edx, %edi
xorl %ecx, %ecx
testl %edx, %edx
movl 0x10(%rsp), %edx
cmovsl %ecx, %edi
movl %edx, 0x1c(%rbx)
movl %edi, 0x20(%rbx)
movss 0x18(%rbx), %xmm3
mulss %xmm0, %xmm3
divss %xmm1, %xmm3
addss %xmm2, %xmm3
cvttss2si %xmm3, %rdx
cmpl %eax, %edx
movl %eax, %edi
cmovll %edx, %edi
testl %edx, %edx
cmovsl %ecx, %edi
movl %edi, 0x24(%rbx)
mulss 0x1c(%rbx), %xmm0
divss %xmm1, %xmm0
addss %xmm2, %xmm0
cvttss2si %xmm0, %rdx
cmpl %eax, %edx
cmovll %edx, %eax
testl %edx, %edx
cmovsl %ecx, %eax
movl %eax, 0x28(%rbx)
cmpl $0x2, (%rsi)
jge 0x6959b
leaq 0x3c(%rbx), %r14
leaq 0x5c(%rbx), %r12
leaq 0x2c(%rbx), %r15
movl $0x0, (%rsp)
xorl %r13d, %r13d
movl $0x8, %edi
movq %r14, %rsi
movq %r12, %rdx
movq %r15, %rcx
xorl %r8d, %r8d
movl $0x2, %r9d
callq 0x6a9e9
movq %rax, 0x80(%rbx)
cmpq %r14, %rax
cmoveq %r12, %r14
movq %r14, 0x88(%rbx)
movl (%rax,%r13,4), %ecx
movzwl (%r15,%rcx,2), %ecx
movl %ecx, (%r14,%r13,4)
incq %r13
cmpq $0x8, %r13
jne 0x69585
xorl %eax, %eax
movb %al, 0xa8(%rbx)
movq $0x0, 0xa0(%rbx)
movb %al, 0xc0(%rbx)
movq $-0x1, 0xb8(%rbx)
addq $0x20, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
leaq 0x11dce(%rip), %rdi     # 0x7b3a2
leaq 0x11bd4(%rip), %rsi     # 0x7b1af
leaq 0x11dd8(%rip), %rcx     # 0x7b3ba
movl $0x660, %edx            # imm = 0x660
callq 0x61b0