// File: /workspace/github_c_makefile_star2_O1/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
movb $0x0, 0x20(%rdx)
movq %rdi, -0x60(%rsp)
movq (%rdi), %r9
cmpb $0x1, 0x30(%r9)
jne 0x68be5
movzbl (%rsi), %eax
movzbl 0x2c(%r9), %edi
subl %edi, %eax
movzbl 0x1(%rsi), %edi
movzbl 0x2d(%r9), %r8d
subl %r8d, %edi
movzbl 0x2(%rsi), %r8d
movzbl 0x2e(%r9), %r9d
subl %r9d, %r8d
cmpl %edi, %eax
movl %edi, %r9d
cmovll %eax, %r9d
cmpl %r8d, %r9d
cmovgel %r8d, %r9d
cmpl $-0x4, %r9d
jl 0x68ee6
cmpl %edi, %eax
cmovgl %eax, %edi
cmpl %r8d, %edi
cmovlel %r8d, %edi
cmpl $0x3, %edi
jg 0x68ee6
movzbl (%rsi), %eax
cmpb $0x1, 0x8(%rsi)
movq %rcx, -0x28(%rsp)
movq %rsi, -0x30(%rsp)
jne 0x68c20
movl %eax, %r9d
shll $0x4, %r9d
orl %eax, %r9d
movzbl 0x1(%rsi), %eax
movl %eax, %r8d
shll $0x4, %r8d
orl %eax, %r8d
movzbl 0x2(%rsi), %eax
movl %eax, %r11d
shll $0x4, %r11d
orl %eax, %r11d
jmp 0x68c51
movl %eax, %ecx
shrl $0x2, %ecx
shll $0x3, %eax
orl %ecx, %eax
movzbl 0x1(%rsi), %r8d
movl %r8d, %ecx
shrl $0x2, %ecx
shll $0x3, %r8d
orl %ecx, %r8d
movzbl 0x2(%rsi), %r11d
movl %r11d, %ecx
shrl $0x2, %ecx
shll $0x3, %r11d
orl %ecx, %r11d
movl %eax, %r9d
movl $0xff, %r10d
cmpl %r10d, %r9d
cmovgel %r10d, %r9d
cmpl %r10d, %r8d
cmovgel %r10d, %r8d
cmpl %r10d, %r11d
cmovgel %r10d, %r11d
movq $-0x1, 0x18(%rdx)
leaq 0x12c35(%rip), %rbx     # 0x7b8b0
xorl %r14d, %r14d
xorl %ecx, %ecx
movq %r8, -0x18(%rsp)
movq %r9, -0x20(%rsp)
movl %r11d, -0x58(%rsp)
movq %rcx, -0x10(%rsp)
xorl %eax, %eax
movl (%rbx,%rax,4), %ecx
leal (%rcx,%r9), %edi
leal (%rcx,%r8), %esi
addl %r11d, %ecx
testl %edi, %edi
cmovlel %r14d, %edi
cmpl %r10d, %edi
cmovgel %r10d, %edi
testl %esi, %esi
cmovlel %r14d, %esi
cmpl %r10d, %esi
cmovgel %r10d, %esi
movb %dil, -0x78(%rsp,%rax,4)
testl %ecx, %ecx
cmovlel %r14d, %ecx
movb %sil, -0x77(%rsp,%rax,4)
cmpl %r10d, %ecx
cmovgel %r10d, %ecx
movb %cl, -0x76(%rsp,%rax,4)
movb $0x0, -0x75(%rsp,%rax,4)
incq %rax
cmpq $0x4, %rax
jne 0x68c96
movq %rbx, -0x8(%rsp)
movq -0x60(%rsp), %rax
movq (%rax), %rax
movq 0x10(%rax), %r12
movzbl -0x78(%rsp), %eax
movl %eax, -0x34(%rsp)
movzbl -0x77(%rsp), %eax
movl %eax, -0x38(%rsp)
movzbl -0x76(%rsp), %eax
movl %eax, -0x3c(%rsp)
movzbl -0x74(%rsp), %eax
movl %eax, -0x40(%rsp)
movzbl -0x73(%rsp), %eax
movl %eax, -0x44(%rsp)
movzbl -0x72(%rsp), %eax
movl %eax, -0x48(%rsp)
movzbl -0x70(%rsp), %eax
movl %eax, -0x4c(%rsp)
movzbl -0x6f(%rsp), %eax
movl %eax, -0x50(%rsp)
movzbl -0x6e(%rsp), %eax
movl %eax, -0x54(%rsp)
movzbl -0x6c(%rsp), %r8d
movzbl -0x6b(%rsp), %r9d
movzbl -0x6a(%rsp), %r11d
xorl %ebx, %ebx
xorl %r13d, %r13d
movq %r13, %r15
movzbl (%r12,%rbx,4), %esi
movl %esi, %eax
subl -0x34(%rsp), %eax
imull %eax, %eax
movzbl 0x1(%r12,%rbx,4), %edi
movl %edi, %ebp
subl -0x38(%rsp), %ebp
imull %ebp, %ebp
addl %eax, %ebp
movzbl 0x2(%r12,%rbx,4), %r13d
movl %r13d, %r14d
subl -0x3c(%rsp), %r14d
imull %r14d, %r14d
addl %ebp, %r14d
movl %esi, %eax
subl -0x40(%rsp), %eax
imull %eax, %eax
movl %edi, %ebp
subl -0x44(%rsp), %ebp
imull %ebp, %ebp
addl %eax, %ebp
movl %r13d, %eax
subl -0x48(%rsp), %eax
imull %eax, %eax
addl %ebp, %eax
xorl %ebp, %ebp
cmpl %r14d, %eax
setb %bpl
cmovael %r14d, %eax
movl %esi, %r14d
subl -0x4c(%rsp), %r14d
imull %r14d, %r14d
movl %edi, %ecx
subl -0x50(%rsp), %ecx
imull %ecx, %ecx
addl %r14d, %ecx
movl %r13d, %r14d
subl -0x54(%rsp), %r14d
imull %r14d, %r14d
addl %ecx, %r14d
cmpl %eax, %r14d
cmovael %eax, %r14d
movl $0x2, %eax
cmovbl %eax, %ebp
subl %r8d, %esi
imull %esi, %esi
subl %r9d, %edi
imull %edi, %edi
addl %esi, %edi
subl %r11d, %r13d
imull %r13d, %r13d
addl %edi, %r13d
cmpl %r14d, %r13d
cmovael %r14d, %r13d
movl $0x3, %eax
cmovbl %eax, %ebp
movq -0x60(%rsp), %rax
movb %bpl, 0xf0(%rax,%rbx)
addq %r15, %r13
cmpq 0x18(%rdx), %r13
jae 0x68e48
leaq 0x1(%rbx), %rax
cmpq $0x7, %rbx
movq %rax, %rbx
jb 0x68d60
cmpq 0x18(%rdx), %r13
movq -0x10(%rsp), %rcx
jae 0x68e6e
movq %r13, 0x18(%rdx)
movl %ecx, 0x4(%rdx)
movq -0x60(%rsp), %rax
movq 0xf0(%rax), %rax
movq %rax, 0xc(%rdx)
movb $0x1, 0x20(%rdx)
incq %rcx
movq -0x8(%rsp), %rbx
addq $0x10, %rbx
cmpq $0x8, %rcx
movq -0x18(%rsp), %r8
movq -0x20(%rsp), %r9
movl -0x58(%rsp), %r11d
movl $0x0, %r14d
jne 0x68c8f
movq -0x30(%rsp), %rax
movl (%rax), %eax
movl %eax, (%rdx)
movq -0x60(%rsp), %rcx
movq (%rcx), %rcx
movb 0x18(%rcx), %cl
movb %cl, 0x8(%rdx)
movq -0x28(%rsp), %rsi
testq %rsi, %rsi
je 0x68ee6
movq 0x18(%rdx), %rcx
cmpq 0x18(%rsi), %rcx
jae 0x68ee6
movl %eax, (%rsi)
movl 0x4(%rdx), %eax
movl %eax, 0x4(%rsi)
movb 0x8(%rdx), %al
movb %al, 0x8(%rsi)
movups 0xc(%rdx), %xmm0
movups %xmm0, 0xc(%rsi)
movq 0x19(%rdx), %rax
movq %rax, 0x19(%rsi)
movb $0x1, %al
jmp 0x68ee8
xorl %eax, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop