// File: /workspace/github_c_makefile_star2_O2/jonof[P]jftekwar/jfbuild/src/rg_etc1.cpp
// Function: rg_etc1::etc1_optimizer::evaluate_solution(rg_etc1::etc1_solution_coordinates const&, rg_etc1::etc1_optimizer::potential_solution&, rg_etc1::etc1_optimizer::potential_solution*)
bool etc1_optimizer::evaluate_solution(const etc1_solution_coordinates& coords, potential_solution& trial_solution, potential_solution* pBest_solution)
   {
      trial_solution.m_valid = false;

      if (m_pParams->m_constrain_against_base_color5)
      {
         const int dr = coords.m_unscaled_color.r - m_pParams->m_base_color5.r;
         const int dg = coords.m_unscaled_color.g - m_pParams->m_base_color5.g;
         const int db = coords.m_unscaled_color.b - m_pParams->m_base_color5.b;

         if ((rg_etc1::minimum(dr, dg, db) < cETC1ColorDeltaMin) || (rg_etc1::maximum(dr, dg, db) > cETC1ColorDeltaMax))
            return false;
      }

      const color_quad_u8 base_color(coords.get_scaled_color());
      
      const uint n = 8;
            
      trial_solution.m_error = cUINT64_MAX;
            
      for (uint inten_table = 0; inten_table < cETC1IntenModifierValues; inten_table++)
      {
         const int* pInten_table = g_etc1_inten_tables[inten_table];

         color_quad_u8 block_colors[4];
         for (uint s = 0; s < 4; s++)
         {
            const int yd = pInten_table[s];
            block_colors[s].set(base_color.r + yd, base_color.g + yd, base_color.b + yd, 0);
         }
         
         uint64 total_error = 0;
         
         const color_quad_u8* pSrc_pixels = m_pParams->m_pSrc_pixels;
         for (uint c = 0; c < n; c++)
         {
            const color_quad_u8& src_pixel = *pSrc_pixels++;
            
            uint best_selector_index = 0;
            uint best_error = rg_etc1::square(src_pixel.r - block_colors[0].r) + rg_etc1::square(src_pixel.g - block_colors[0].g) + rg_etc1::square(src_pixel.b - block_colors[0].b);

            uint trial_error = rg_etc1::square(src_pixel.r - block_colors[1].r) + rg_etc1::square(src_pixel.g - block_colors[1].g) + rg_etc1::square(src_pixel.b - block_colors[1].b);
            if (trial_error < best_error)
            {
               best_error = trial_error;
               best_selector_index = 1;
            }

            trial_error = rg_etc1::square(src_pixel.r - block_colors[2].r) + rg_etc1::square(src_pixel.g - block_colors[2].g) + rg_etc1::square(src_pixel.b - block_colors[2].b);
            if (trial_error < best_error)
            {
               best_error = trial_error;
               best_selector_index = 2;
            }

            trial_error = rg_etc1::square(src_pixel.r - block_colors[3].r) + rg_etc1::square(src_pixel.g - block_colors[3].g) + rg_etc1::square(src_pixel.b - block_colors[3].b);
            if (trial_error < best_error)
            {
               best_error = trial_error;
               best_selector_index = 3;
            }

            m_temp_selectors[c] = static_cast<uint8>(best_selector_index);

            total_error += best_error;
            if (total_error >= trial_solution.m_error)
               break;
         }
         
         if (total_error < trial_solution.m_error)
         {
            trial_solution.m_error = total_error;
            trial_solution.m_coords.m_inten_table = inten_table;
            memcpy(trial_solution.m_selectors, m_temp_selectors, 8);
            trial_solution.m_valid = true;
         }
      }
      trial_solution.m_coords.m_unscaled_color = coords.m_unscaled_color;
      trial_solution.m_coords.m_color4 = m_pParams->m_use_color4;

      bool success = false;
      if (pBest_solution)
      {
         if (trial_solution.m_error < pBest_solution->m_error)
         {
            *pBest_solution = trial_solution;
            success = true;
         }
      }

      return success;
   }

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rdx, %r14
movb $0x0, 0x20(%rdx)
movq %rdi, 0x18(%rsp)
movq (%rdi), %r8
cmpb $0x1, 0x30(%r8)
jne 0x5f835
movzbl (%rsi), %eax
movzbl 0x2c(%r8), %edx
subl %edx, %eax
movzbl 0x1(%rsi), %edi
movzbl 0x2d(%r8), %edx
subl %edx, %edi
movzbl 0x2(%rsi), %edx
movzbl 0x2e(%r8), %r8d
subl %r8d, %edx
cmpl %edi, %eax
movl %edi, %r8d
cmovll %eax, %r8d
cmpl %edx, %r8d
cmovgel %edx, %r8d
cmpl $-0x4, %r8d
jl 0x5fb05
cmpl %edi, %eax
cmovgl %eax, %edi
cmpl %edx, %edi
cmovlel %edx, %edi
cmpl $0x3, %edi
jg 0x5fb05
movq %rcx, 0x68(%rsp)
leaq 0x84(%rsp), %r13
movq %r13, %rdi
movq %rsi, 0x60(%rsp)
callq 0x61150
orq $-0x1, 0x18(%r14)
movzbl (%r13), %ecx
movzbl 0x1(%r13), %edx
movzbl 0x2(%r13), %esi
leaq 0xc(%r14), %rax
movq %rax, 0x58(%rsp)
pushq $-0x1
popq %rax
leaq 0x1234a(%rip), %rdi     # 0x71bc0
xorl %r8d, %r8d
movl $0xff, %r11d
xorl %r9d, %r9d
movq %r14, 0x50(%rsp)
movq %rcx, 0x70(%rsp)
cmpq $0x8, %r9
je 0x5fab4
movq %r9, 0x78(%rsp)
xorl %r9d, %r9d
cmpq $0x4, %r9
je 0x5f8f4
movl (%rdi,%r9,4), %r10d
leal (%r10,%rcx), %ebx
leal (%r10,%rdx), %ebp
addl %esi, %r10d
testl %ebx, %ebx
cmovlel %r8d, %ebx
cmpl %r11d, %ebx
cmovgel %r11d, %ebx
testl %ebp, %ebp
cmovlel %r8d, %ebp
cmpl %r11d, %ebp
cmovgel %r11d, %ebp
testl %r10d, %r10d
cmovlel %r8d, %r10d
movb %bl, (%rsp,%r9,4)
cmpl %r11d, %r10d
cmovgel %r11d, %r10d
movb %bpl, 0x1(%rsp,%r9,4)
movb %r10b, 0x2(%rsp,%r9,4)
movb $0x0, 0x3(%rsp,%r9,4)
incq %r9
jmp 0x5f89e
movq 0x18(%rsp), %rcx
movq (%rcx), %r9
movq 0x10(%r9), %rbx
movzbl (%rsp), %ecx
movl %ecx, 0x4c(%rsp)
movzbl 0x1(%rsp), %ecx
movl %ecx, 0x48(%rsp)
movzbl 0x2(%rsp), %ecx
movl %ecx, 0x44(%rsp)
movzbl 0x4(%rsp), %ecx
movl %ecx, 0x40(%rsp)
movzbl 0x5(%rsp), %ecx
movl %ecx, 0x3c(%rsp)
movzbl 0x6(%rsp), %ecx
movl %ecx, 0x38(%rsp)
movzbl 0x8(%rsp), %ecx
movl %ecx, 0x34(%rsp)
movzbl 0x9(%rsp), %ecx
movl %ecx, 0x30(%rsp)
movzbl 0xa(%rsp), %ecx
movl %ecx, 0x2c(%rsp)
movzbl 0xc(%rsp), %ecx
movl %ecx, 0x28(%rsp)
movzbl 0xd(%rsp), %ecx
movl %ecx, 0x24(%rsp)
movzbl 0xe(%rsp), %ecx
movl %ecx, 0x20(%rsp)
xorl %r12d, %r12d
xorl %ebp, %ebp
cmpq $0x8, %r12
je 0x5fa74
movzbl (%rbx,%r12,4), %r9d
movl %r9d, %eax
subl 0x4c(%rsp), %eax
imull %eax, %eax
movzbl 0x1(%rbx,%r12,4), %ecx
movl %ecx, %r10d
subl 0x48(%rsp), %r10d
imull %r10d, %r10d
addl %eax, %r10d
movzbl 0x2(%rbx,%r12,4), %eax
movl %eax, %r14d
subl 0x44(%rsp), %r14d
imull %r14d, %r14d
addl %r10d, %r14d
movl %r9d, %r10d
subl 0x40(%rsp), %r10d
imull %r10d, %r10d
movl %ecx, %r13d
subl 0x3c(%rsp), %r13d
imull %r13d, %r13d
addl %r10d, %r13d
movl %eax, %r15d
subl 0x38(%rsp), %r15d
imull %r15d, %r15d
addl %r13d, %r15d
xorl %r10d, %r10d
cmpl %r14d, %r15d
setb %r10b
cmovael %r14d, %r15d
movl %r9d, %r14d
subl 0x34(%rsp), %r14d
imull %r14d, %r14d
movl %ecx, %r13d
subl 0x30(%rsp), %r13d
imull %r13d, %r13d
addl %r14d, %r13d
movl %eax, %r14d
subl 0x2c(%rsp), %r14d
imull %r14d, %r14d
addl %r13d, %r14d
cmpl %r15d, %r14d
cmovael %r15d, %r14d
pushq $0x2
popq %r15
cmovbl %r15d, %r10d
subl 0x28(%rsp), %r9d
imull %r9d, %r9d
subl 0x24(%rsp), %ecx
imull %ecx, %ecx
addl %r9d, %ecx
subl 0x20(%rsp), %eax
imull %eax, %eax
addl %ecx, %eax
cmpl %r14d, %eax
cmovael %r14d, %eax
pushq $0x3
popq %rcx
cmovbl %ecx, %r10d
movq 0x18(%rsp), %rcx
movb %r10b, 0xf0(%rcx,%r12)
addq %rax, %rbp
movq 0x50(%rsp), %r14
movq 0x18(%r14), %rax
cmpq %rax, %rbp
jae 0x5fa79
incq %r12
jmp 0x5f970
movq 0x50(%rsp), %r14
cmpq %rax, %rbp
movq 0x70(%rsp), %rcx
movq 0x78(%rsp), %r9
jae 0x5faa8
movq %rbp, 0x18(%r14)
movl %r9d, 0x4(%r14)
movq 0x18(%rsp), %rax
movq 0xf0(%rax), %rax
movq %rax, 0xc(%r14)
movb $0x1, 0x20(%r14)
movq %rbp, %rax
incq %r9
addq $0x10, %rdi
jmp 0x5f88c
movq 0x60(%rsp), %rcx
movl (%rcx), %ecx
movl %ecx, (%r14)
movq 0x18(%rsp), %rdx
movq (%rdx), %rdx
movb 0x18(%rdx), %dl
movb %dl, 0x8(%r14)
movq 0x68(%rsp), %rdx
testq %rdx, %rdx
je 0x5fb05
cmpq 0x18(%rdx), %rax
jae 0x5fb05
movl %ecx, (%rdx)
movl 0x4(%r14), %eax
movl %eax, 0x4(%rdx)
movb 0x8(%r14), %al
movb %al, 0x8(%rdx)
movq 0x58(%rsp), %rax
movups (%rax), %xmm0
movups %xmm0, 0xc(%rdx)
movq 0xd(%rax), %rax
movq %rax, 0x19(%rdx)
movb $0x1, %al
jmp 0x5fb07
xorl %eax, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop