// File: /workspace/github_c_makefile_star2_O2/jonof[P]jftekwar/jfbuild/src/rg_etc1.cpp
// Function: rg_etc1::etc1_optimizer::evaluate_solution_fast(rg_etc1::etc1_solution_coordinates const&, rg_etc1::etc1_optimizer::potential_solution&, rg_etc1::etc1_optimizer::potential_solution*)
bool etc1_optimizer::evaluate_solution_fast(const etc1_solution_coordinates& coords, potential_solution& trial_solution, potential_solution* pBest_solution)
   {
      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))
         {
            trial_solution.m_valid = false;
            return false;
         }
      }

      const color_quad_u8 base_color(coords.get_scaled_color());

      const uint n = 8;
      
      trial_solution.m_error = cUINT64_MAX;

      for (int inten_table = cETC1IntenModifierValues - 1; inten_table >= 0; --inten_table)
      {
         const int* pInten_table = g_etc1_inten_tables[inten_table];

         uint block_inten[4];
         color_quad_u8 block_colors[4];
         for (uint s = 0; s < 4; s++)
         {
            const int yd = pInten_table[s];
            color_quad_u8 block_color(base_color.r + yd, base_color.g + yd, base_color.b + yd, 0);
            block_colors[s] = block_color;
            block_inten[s] = block_color.r + block_color.g + block_color.b;
         }

         // evaluate_solution_fast() enforces/assumesd a total ordering of the input colors along the intensity (1,1,1) axis to more quickly classify the inputs to selectors.
         // The inputs colors have been presorted along the projection onto this axis, and ETC1 block colors are always ordered along the intensity axis, so this classification is fast.
         // 0   1   2   3
         //   01  12  23
         const uint block_inten_midpoints[3] = { block_inten[0] + block_inten[1], block_inten[1] + block_inten[2], block_inten[2] + block_inten[3] };

         uint64 total_error = 0;
         const color_quad_u8* pSrc_pixels = m_pParams->m_pSrc_pixels;
         if ((m_pSorted_luma[n - 1] * 2) < block_inten_midpoints[0])
         {
            if (block_inten[0] > m_pSorted_luma[n - 1])
            {
               const uint min_error = (block_inten[0] - m_pSorted_luma[n - 1]); //JonoF
               if (min_error >= trial_solution.m_error)
                  continue;
            }

            memset(&m_temp_selectors[0], 0, n);

            for (uint c = 0; c < n; c++)
               total_error += block_colors[0].squared_distance_rgb(pSrc_pixels[c]);
         }
         else if ((m_pSorted_luma[0] * 2) >= block_inten_midpoints[2])
         {
            if (m_pSorted_luma[0] > block_inten[3])
            {
               const uint min_error = (m_pSorted_luma[0] - block_inten[3]); //JonoF
               if (min_error >= trial_solution.m_error)
                  continue;
            }

            memset(&m_temp_selectors[0], 3, n);

            for (uint c = 0; c < n; c++)
               total_error += block_colors[3].squared_distance_rgb(pSrc_pixels[c]);
         }
         else
         {
            uint cur_selector = 0, c;
            for (c = 0; c < n; c++)
            {
               const uint y = m_pSorted_luma[c];
               while ((y * 2) >= block_inten_midpoints[cur_selector])
                  if (++cur_selector > 2)
                     goto done;
               const uint sorted_pixel_index = m_pSorted_luma_indices[c];
               m_temp_selectors[sorted_pixel_index] = static_cast<uint8>(cur_selector);
               total_error += block_colors[cur_selector].squared_distance_rgb(pSrc_pixels[sorted_pixel_index]);
            }
done:
            while (c < n)
            {
               const uint sorted_pixel_index = m_pSorted_luma_indices[c];
               m_temp_selectors[sorted_pixel_index] = 3;
               total_error += block_colors[3].squared_distance_rgb(pSrc_pixels[sorted_pixel_index]);
               ++c;
            }
         }

         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, n);
            trial_solution.m_valid = true;
            if (!total_error)
               break;
         }
      }
      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 $0x68, %rsp
movq %rdx, 0x8(%rsp)
movq %rdi, %r15
movq (%rdi), %r8
cmpb $0x1, 0x30(%r8)
jne 0x5fb8e
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 0x5fb80
cmpl %edi, %eax
cmovgl %eax, %edi
cmpl %edx, %edi
cmovlel %edx, %edi
cmpl $0x4, %edi
jl 0x5fb8e
movq 0x8(%rsp), %rax
movb $0x0, 0x20(%rax)
jmp 0x5fe6a
movq %rcx, 0x58(%rsp)
leaq 0x4(%rsp), %rdi
movq %rsi, 0x50(%rsp)
callq 0x61150
movq 0x8(%rsp), %rax
orq $-0x1, 0x18(%rax)
addq $0xc, %rax
movq %rax, 0x48(%rsp)
pushq $0x7
popq %r14
pushq $-0x1
popq %rax
xorl %r12d, %r12d
leaq 0x11ffa(%rip), %r13     # 0x71bc0
movl $0xff, %ebp
testl %r14d, %r14d
js 0x5fe18
movl %r14d, %ecx
shlq $0x4, %rcx
movzbl 0x4(%rsp), %edx
movzbl 0x5(%rsp), %esi
movzbl 0x6(%rsp), %edi
xorl %r8d, %r8d
cmpq $0x4, %r8
je 0x5fc56
leaq (%rcx,%r8,4), %r9
movl (%r13,%r9), %r11d
leal (%r11,%rdx), %r9d
leal (%r11,%rsi), %r10d
addl %edi, %r11d
testl %r9d, %r9d
cmovlel %r12d, %r9d
cmpl %ebp, %r9d
cmovgel %ebp, %r9d
testl %r10d, %r10d
cmovlel %r12d, %r10d
cmpl %ebp, %r10d
cmovgel %ebp, %r10d
testl %r11d, %r11d
cmovlel %r12d, %r11d
cmpl %ebp, %r11d
cmovgel %ebp, %r11d
movl %r10d, %ebx
addl %r9d, %r10d
addl %r11d, %r10d
shll $0x10, %r11d
shll $0x8, %ebx
orl %r11d, %ebx
orl %r9d, %ebx
movl %ebx, 0x20(%rsp,%r8,4)
movl %r10d, 0x30(%rsp,%r8,4)
incq %r8
jmp 0x5fbed
movl 0x30(%rsp), %ecx
movl 0x34(%rsp), %edx
leal (%rdx,%rcx), %r9d
movl %r9d, 0x14(%rsp)
movl 0x38(%rsp), %esi
addl %esi, %edx
movl %edx, 0x18(%rsp)
movl 0x3c(%rsp), %edx
addl %edx, %esi
movl %esi, 0x1c(%rsp)
movq (%r15), %r8
movq 0x88(%r15), %rdi
movq 0x10(%r8), %rbx
movl 0x1c(%rdi), %r8d
leal (%r8,%r8), %r10d
cmpl %r9d, %r10d
jae 0x5fcd9
subl %r8d, %ecx
jbe 0x5fca4
cmpq %rcx, %rax
jbe 0x5fe0e
movl %r14d, (%rsp)
andq $0x0, 0xf0(%r15)
xorl %r14d, %r14d
xorl %r13d, %r13d
cmpq $0x20, %r14
je 0x5fdd1
leaq (%rbx,%r14), %rsi
leaq 0x20(%rsp), %rdi
callq 0x615d0
movl %eax, %eax
addq %rax, %r13
addq $0x4, %r14
jmp 0x5fcb6
movl (%rdi), %ecx
leal (%rcx,%rcx), %edi
cmpl %esi, %edi
jae 0x5fd4f
movl %r14d, (%rsp)
xorl %edx, %edx
xorl %r13d, %r13d
xorl %r14d, %r14d
cmpq $0x8, %rdx
je 0x5fd9b
movq 0x88(%r15), %rax
movl (%rax,%rdx,4), %eax
addl %eax, %eax
movl %r14d, %ecx
cmpl 0x14(%rsp,%rcx,4), %eax
jb 0x5fd18
incl %r14d
cmpl $0x2, %r14d
jbe 0x5fd04
jmp 0x5fd96
movq 0x80(%r15), %rax
movl (%rax,%rdx,4), %eax
movb %r14b, 0xf0(%r15,%rax)
leaq (%rsp,%rcx,4), %rdi
addq $0x20, %rdi
leaq (%rbx,%rax,4), %rsi
movq %rdx, 0x60(%rsp)
callq 0x615d0
movq 0x60(%rsp), %rdx
movl %eax, %eax
addq %rax, %r13
incq %rdx
jmp 0x5fcee
subl %edx, %ecx
jbe 0x5fd5c
cmpq %rcx, %rax
jbe 0x5fe0e
movl %r14d, (%rsp)
movabsq $0x303030303030303, %rax # imm = 0x303030303030303
movq %rax, 0xf0(%r15)
xorl %r14d, %r14d
xorl %r13d, %r13d
cmpq $0x20, %r14
je 0x5fdd1
leaq (%rbx,%r14), %rsi
leaq 0x2c(%rsp), %rdi
callq 0x615d0
movl %eax, %eax
addq %rax, %r13
addq $0x4, %r14
jmp 0x5fd77
movl %edx, %r14d
jmp 0x5fdcb
pushq $0x8
popq %r14
jmp 0x5fdcb
movq 0x80(%r15), %rax
movl (%rax,%r14,4), %eax
movb $0x3, 0xf0(%r15,%rax)
leaq (%rbx,%rax,4), %rsi
leaq 0x2c(%rsp), %rdi
callq 0x615d0
movl %eax, %eax
addq %rax, %r13
incq %r14
cmpq $0x8, %r14
jne 0x5fda1
movq 0x8(%rsp), %rax
movq 0x18(%rax), %rax
cmpq %rax, %r13
movl (%rsp), %r14d
jae 0x5fe07
movq 0x8(%rsp), %rcx
movq %r13, 0x18(%rcx)
movl %r14d, 0x4(%rcx)
movq 0xf0(%r15), %rax
movq %rax, 0xc(%rcx)
movb $0x1, 0x20(%rcx)
movq %r13, %rax
testq %r13, %r13
je 0x5fe16
leaq 0x11db2(%rip), %r13     # 0x71bc0
decl %r14d
jmp 0x5fbcb
xorl %eax, %eax
movq 0x50(%rsp), %rcx
movl (%rcx), %ecx
movq 0x8(%rsp), %rsi
movl %ecx, (%rsi)
movq (%r15), %rdx
movb 0x18(%rdx), %dl
movb %dl, 0x8(%rsi)
movq 0x58(%rsp), %rdx
testq %rdx, %rdx
je 0x5fe6a
cmpq 0x18(%rdx), %rax
jae 0x5fe6a
movl %ecx, (%rdx)
movq 0x8(%rsp), %rcx
movl 0x4(%rcx), %eax
movl %eax, 0x4(%rdx)
movb 0x8(%rcx), %al
movb %al, 0x8(%rdx)
movq 0x48(%rsp), %rax
movups (%rax), %xmm0
movups %xmm0, 0xc(%rdx)
movq 0xd(%rax), %rax
movq %rax, 0x19(%rdx)
movb $0x1, %al
jmp 0x5fe6c
xorl %eax, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop