// File: /workspace/github_c_makefile_star2_O3/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
pushq %rax
movq %rdx, -0x68(%rsp)
movb $0x0, 0x20(%rdx)
movq %rdi, -0x60(%rsp)
movq (%rdi), %rdi
movzbl (%rsi), %eax
cmpb $0x1, 0x30(%rdi)
jne 0x6a94d
movzbl 0x2c(%rdi), %r8d
movl %eax, %edx
subl %r8d, %edx
movzbl 0x1(%rsi), %r8d
movzbl 0x2d(%rdi), %r9d
subl %r9d, %r8d
movzbl 0x2(%rsi), %r9d
movzbl 0x2e(%rdi), %edi
subl %edi, %r9d
cmpl %r8d, %edx
movl %r8d, %edi
cmovll %edx, %edi
cmpl %r9d, %edi
cmovgel %r9d, %edi
cmpl $-0x4, %edi
jl 0x6ac5e
cmpl %r8d, %edx
cmovgl %edx, %r8d
cmpl %r9d, %r8d
cmovlel %r9d, %r8d
cmpl $0x3, %r8d
jg 0x6ac5e
cmpb $0x1, 0x8(%rsi)
movq %rcx, -0x20(%rsp)
movq %rsi, -0x28(%rsp)
jne 0x6a982
movl %eax, %r8d
shll $0x4, %r8d
orl %eax, %r8d
movzbl 0x1(%rsi), %eax
movl %eax, %edi
shll $0x4, %edi
orl %eax, %edi
movzbl 0x2(%rsi), %eax
movl %eax, %r9d
shll $0x4, %r9d
orl %eax, %r9d
jmp 0x6a9af
movl %eax, %ecx
shrl $0x2, %ecx
shll $0x3, %eax
orl %ecx, %eax
movzbl 0x1(%rsi), %edi
movl %edi, %ecx
shrl $0x2, %ecx
shll $0x3, %edi
orl %ecx, %edi
movzbl 0x2(%rsi), %r9d
movl %r9d, %ecx
shrl $0x2, %ecx
shll $0x3, %r9d
orl %ecx, %r9d
movl %eax, %r8d
movl $0xff, %r11d
cmpl %r11d, %r8d
cmovael %r11d, %r8d
cmpl %r11d, %edi
cmovael %r11d, %edi
cmpl %r11d, %r9d
cmovael %r11d, %r9d
movq -0x68(%rsp), %rax
movq $-0x1, 0x18(%rax)
addq $0xc, %rax
movq %rax, -0x30(%rsp)
leaq 0x12379(%rip), %r10     # 0x7cd60
xorl %ebx, %ebx
xorl %edx, %edx
movq %rdi, -0x10(%rsp)
movq %r8, -0x18(%rsp)
movl %r9d, -0x54(%rsp)
movq %rdx, -0x8(%rsp)
xorl %eax, %eax
movl (%r10,%rax,4), %ecx
leal (%rcx,%r8), %edx
leal (%rcx,%rdi), %esi
addl %r9d, %ecx
testl %edx, %edx
cmovlel %ebx, %edx
cmpl %r11d, %edx
cmovgel %r11d, %edx
testl %esi, %esi
cmovlel %ebx, %esi
cmpl %r11d, %esi
cmovgel %r11d, %esi
movb %dl, -0x80(%rsp,%rax,4)
testl %ecx, %ecx
cmovlel %ebx, %ecx
movb %sil, -0x7f(%rsp,%rax,4)
cmpl %r11d, %ecx
cmovgel %r11d, %ecx
movb %cl, -0x7e(%rsp,%rax,4)
movb $0x0, -0x7d(%rsp,%rax,4)
incq %rax
cmpq $0x4, %rax
jne 0x6aa01
movq %r10, (%rsp)
movq -0x60(%rsp), %rax
movq (%rax), %rax
movq 0x10(%rax), %r13
movzbl -0x80(%rsp), %eax
movl %eax, -0x34(%rsp)
movzbl -0x7f(%rsp), %eax
movl %eax, -0x38(%rsp)
movzbl -0x7e(%rsp), %eax
movl %eax, -0x3c(%rsp)
movzbl -0x7c(%rsp), %eax
movl %eax, -0x40(%rsp)
movzbl -0x7b(%rsp), %eax
movl %eax, -0x44(%rsp)
movzbl -0x7a(%rsp), %eax
movl %eax, -0x48(%rsp)
movzbl -0x78(%rsp), %eax
movl %eax, -0x4c(%rsp)
movzbl -0x77(%rsp), %eax
movl %eax, -0x50(%rsp)
movzbl -0x76(%rsp), %edi
movzbl -0x74(%rsp), %r8d
movzbl -0x73(%rsp), %r9d
movzbl -0x72(%rsp), %ebx
xorl %r14d, %r14d
xorl %r10d, %r10d
movq %r10, %r12
movzbl (%r13,%r14,4), %esi
movl %esi, %eax
subl -0x34(%rsp), %eax
imull %eax, %eax
movzbl 0x1(%r13,%r14,4), %edx
movl %edx, %ebp
subl -0x38(%rsp), %ebp
imull %ebp, %ebp
addl %eax, %ebp
movzbl 0x2(%r13,%r14,4), %r10d
movl %r10d, %r15d
subl -0x3c(%rsp), %r15d
imull %r15d, %r15d
addl %ebp, %r15d
movl %esi, %eax
subl -0x40(%rsp), %eax
imull %eax, %eax
movl %edx, %ebp
subl -0x44(%rsp), %ebp
imull %ebp, %ebp
addl %eax, %ebp
movl %r10d, %eax
subl -0x48(%rsp), %eax
imull %eax, %eax
addl %ebp, %eax
xorl %ebp, %ebp
cmpl %r15d, %eax
setb %bpl
cmovael %r15d, %eax
movl %esi, %r15d
subl -0x4c(%rsp), %r15d
imull %r15d, %r15d
movl %edx, %ecx
subl -0x50(%rsp), %ecx
imull %ecx, %ecx
addl %r15d, %ecx
movl %r10d, %r15d
subl %edi, %r15d
imull %r15d, %r15d
addl %ecx, %r15d
cmpl %eax, %r15d
cmovael %eax, %r15d
movl $0x2, %eax
cmovbl %eax, %ebp
subl %r8d, %esi
imull %esi, %esi
subl %r9d, %edx
imull %edx, %edx
addl %esi, %edx
subl %ebx, %r10d
imull %r10d, %r10d
addl %edx, %r10d
cmpl %r15d, %r10d
cmovael %r15d, %r10d
movl $0x3, %eax
cmovbl %eax, %ebp
movq -0x60(%rsp), %rax
movb %bpl, 0xf0(%rax,%r14)
addq %r12, %r10
movq -0x68(%rsp), %rax
movq 0x18(%rax), %rcx
cmpq %rcx, %r10
jae 0x6abb1
leaq 0x1(%r14), %rax
cmpq $0x7, %r14
movq %rax, %r14
jb 0x6aac2
cmpq %rcx, %r10
movq -0x8(%rsp), %rdx
jae 0x6abde
movq -0x68(%rsp), %rcx
movq %r10, 0x18(%rcx)
movl %edx, 0x4(%rcx)
movq -0x60(%rsp), %rax
movq 0xf0(%rax), %rax
movq %rax, 0xc(%rcx)
movb $0x1, 0x20(%rcx)
movq %r10, %rcx
incq %rdx
movq (%rsp), %r10
addq $0x10, %r10
cmpq $0x8, %rdx
movq -0x10(%rsp), %rdi
movq -0x18(%rsp), %r8
movl -0x54(%rsp), %r9d
movl $0x0, %ebx
jne 0x6a9fa
movq -0x28(%rsp), %rax
movl (%rax), %eax
movq -0x68(%rsp), %rsi
movl %eax, (%rsi)
movq -0x60(%rsp), %rdx
movq (%rdx), %rdx
movb 0x18(%rdx), %dl
movb %dl, 0x8(%rsi)
movq -0x20(%rsp), %rdx
testq %rdx, %rdx
je 0x6ac5e
cmpq 0x18(%rdx), %rcx
jae 0x6ac5e
movl %eax, (%rdx)
movq -0x68(%rsp), %rcx
movl 0x4(%rcx), %eax
movl %eax, 0x4(%rdx)
movb 0x8(%rcx), %al
movb %al, 0x8(%rdx)
movq -0x30(%rsp), %rax
movups (%rax), %xmm0
movups %xmm0, 0xc(%rdx)
movq 0xd(%rax), %rax
movq %rax, 0x19(%rdx)
movb $0x1, %al
jmp 0x6ac60
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop