// File: /workspace/github_c_makefile_star2_O3/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 $0x30, %rsp
cmpl $0x8, 0x8(%rsi)
jne 0x6b365
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, 0x10(%rsp)
movl $0x0, 0x18(%rsp)
movq 0x10(%rsi), %rcx
leaq 0x2c(%rdi), %r14
leaq 0x3c(%rdi), %r15
xorl %edx, %edx
pxor %xmm0, %xmm0
movl $0xff, %edi
movzbl 0x2(%rcx,%rdx,4), %r8d
movzwl %r8w, %r8d
xorps %xmm2, %xmm2
cvtsi2ss %r8d, %xmm2
movd (%rcx,%rdx,4), %xmm1
movdqa %xmm1, %xmm3
punpcklbw %xmm0, %xmm3    # xmm3 = xmm3[0],xmm0[0],xmm3[1],xmm0[1],xmm3[2],xmm0[2],xmm3[3],xmm0[3],xmm3[4],xmm0[4],xmm3[5],xmm0[5],xmm3[6],xmm0[6],xmm3[7],xmm0[7]
punpcklwd %xmm0, %xmm3    # xmm3 = xmm3[0],xmm0[0],xmm3[1],xmm0[1],xmm3[2],xmm0[2],xmm3[3],xmm0[3]
cvtdq2ps %xmm3, %xmm3
movlps %xmm3, 0x20(%rsp)
movss %xmm2, 0x28(%rsp)
xorl %r9d, %r9d
movss 0x20(%rsp,%r9,4), %xmm2
addss 0x10(%rsp,%r9,4), %xmm2
movss %xmm2, 0x10(%rsp,%r9,4)
incq %r9
cmpq $0x3, %r9
jne 0x6b1c3
movd %xmm1, %r9d
movl %r9d, %r10d
andl %edi, %r10d
shrl $0x8, %r9d
andl %edi, %r9d
addl %r8d, %r10d
addl %r9d, %r10d
movw %r10w, (%r14,%rdx,2)
movl %edx, (%r15,%rdx,4)
incq %rdx
cmpq $0x8, %rdx
jne 0x6b18f
xorl %ecx, %ecx
movss 0x1148b(%rip), %xmm0    # 0x7c6a0
movss 0x10(%rsp,%rcx,4), %xmm1
mulss %xmm0, %xmm1
movss %xmm1, 0x10(%rsp,%rcx,4)
incq %rcx
cmpq $0x3, %rcx
jne 0x6b215
movq 0x10(%rsp), %rcx
movq %rcx, 0x14(%rbx)
movl 0x18(%rsp), %ecx
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
movsd 0x14(%rbx), %xmm1
movaps %xmm0, %xmm2
shufps $0x0, %xmm0, %xmm2      # xmm2 = xmm2[0,0],xmm0[0,0]
mulps %xmm1, %xmm2
divps 0xf548(%rip), %xmm2     # 0x7a7a0
addps 0x11411(%rip), %xmm2    # 0x7c670
movl %ecx, 0x1c(%rbx)
cvttps2dq %xmm2, %xmm1
movdqa %xmm1, %xmm3
psrad $0x1f, %xmm3
subps 0x1140a(%rip), %xmm2    # 0x7c680
cvttps2dq %xmm2, %xmm2
pand %xmm3, %xmm2
por %xmm1, %xmm2
pxor %xmm1, %xmm1
movd %eax, %xmm3
pshufd $0x50, %xmm3, %xmm3     # xmm3 = xmm3[0,0,1,1]
movdqa %xmm3, %xmm4
pcmpgtd %xmm2, %xmm4
pcmpgtd %xmm2, %xmm1
pand %xmm4, %xmm2
pandn %xmm3, %xmm4
por %xmm2, %xmm4
pandn %xmm4, %xmm1
movq %xmm1, 0x20(%rbx)
mulss 0x1c(%rbx), %xmm0
divss 0xf2e3(%rip), %xmm0     # 0x7a5a0
addss 0xf2c3(%rip), %xmm0     # 0x7a588
cvttss2si %xmm0, %rcx
cmpl %eax, %ecx
cmovll %ecx, %eax
xorl %edx, %edx
testl %ecx, %ecx
cmovnsl %eax, %edx
movl %edx, 0x28(%rbx)
cmpl $0x2, (%rsi)
jge 0x6b333
leaq 0x5c(%rbx), %r12
movl $0x0, (%rsp)
xorl %r13d, %r13d
movl $0x8, %edi
movq %r15, %rsi
movq %r12, %rdx
movq %r14, %rcx
xorl %r8d, %r8d
movl $0x2, %r9d
callq 0x6c73d
movq %rax, 0x80(%rbx)
cmpq %r15, %rax
cmoveq %r12, %r15
movq %r15, 0x88(%rbx)
movl (%rax,%r13,4), %ecx
movzwl (%r14,%rcx,2), %ecx
movl %ecx, (%r15,%r13,4)
incq %r13
cmpq $0x8, %r13
jne 0x6b31d
xorl %eax, %eax
movb %al, 0xa8(%rbx)
movq $0x0, 0xa0(%rbx)
movb %al, 0xc0(%rbx)
movq $-0x1, 0xb8(%rbx)
addq $0x30, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
leaq 0x11566(%rip), %rdi     # 0x7c8d2
leaq 0x1136c(%rip), %rsi     # 0x7c6df
leaq 0x11570(%rip), %rcx     # 0x7c8ea
movl $0x660, %edx            # imm = 0x660
callq 0x61b0