// File: /workspace/github_c_makefile_star2_O1/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
movq %rdx, -0x70(%rsp)
movq %rdi, -0x78(%rsp)
movq (%rdi), %r8
cmpb $0x1, 0x30(%r8)
jne 0x68f66
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 0x68f58
cmpl %edi, %eax
cmovgl %eax, %edi
cmpl %edx, %edi
cmovlel %edx, %edi
cmpl $0x4, %edi
jl 0x68f66
movq -0x70(%rsp), %rax
movb $0x0, 0x20(%rax)
jmp 0x693b1
movq %rcx, -0x10(%rsp)
movzbl (%rsi), %ecx
cmpb $0x1, 0x8(%rsi)
movq %rsi, -0x18(%rsp)
jne 0x68fa1
movl %ecx, %r15d
shll $0x4, %r15d
orl %ecx, %r15d
movzbl 0x1(%rsi), %ecx
movl %ecx, %r8d
shll $0x4, %r8d
orl %ecx, %r8d
movzbl 0x2(%rsi), %ecx
movl %ecx, %r9d
shll $0x4, %r9d
orl %ecx, %r9d
jmp 0x68fd2
movl %ecx, %eax
shrl $0x2, %eax
shll $0x3, %ecx
orl %eax, %ecx
movzbl 0x1(%rsi), %r8d
movl %r8d, %edx
shrl $0x2, %edx
shll $0x3, %r8d
orl %edx, %r8d
movzbl 0x2(%rsi), %r9d
movl %r9d, %edx
shrl $0x2, %edx
shll $0x3, %r9d
orl %edx, %r9d
movl %ecx, %r15d
movl $0xff, %r10d
cmpl %r10d, %r15d
cmovgel %r10d, %r15d
cmpl %r10d, %r8d
cmovgel %r10d, %r8d
cmpl %r10d, %r9d
cmovgel %r10d, %r9d
movq -0x70(%rsp), %rax
movq $-0x1, 0x18(%rax)
movl $0x7, %esi
leaq 0x1291a(%rip), %rbx     # 0x7b920
xorl %r14d, %r14d
movq %r8, -0x48(%rsp)
movq %r15, -0x50(%rsp)
movq %rsi, -0x58(%rsp)
xorl %ecx, %ecx
movl (%rbx,%rcx,4), %r11d
leal (%r11,%r15), %edx
leal (%r11,%r8), %esi
addl %r9d, %r11d
testl %edx, %edx
cmovlel %r14d, %edx
cmpl %r10d, %edx
cmovgel %r10d, %edx
testl %esi, %esi
cmovlel %r14d, %esi
cmpl %r10d, %esi
cmovgel %r10d, %esi
testl %r11d, %r11d
cmovlel %r14d, %r11d
cmpl %r10d, %r11d
cmovgel %r10d, %r11d
movl %esi, %edi
addl %edx, %esi
addl %r11d, %esi
shll $0x10, %r11d
shll $0x8, %edi
orl %r11d, %edi
orl %edx, %edi
movl %edi, -0x68(%rsp,%rcx,4)
movl %esi, -0x28(%rsp,%rcx,4)
incq %rcx
cmpq $0x4, %rcx
jne 0x6901a
movl -0x28(%rsp), %ecx
movl -0x24(%rsp), %edx
leal (%rdx,%rcx), %r11d
movl %r11d, -0x34(%rsp)
movl -0x20(%rsp), %esi
addl %esi, %edx
movl %edx, -0x30(%rsp)
movl -0x1c(%rsp), %edx
addl %edx, %esi
movl %esi, -0x2c(%rsp)
movq -0x78(%rsp), %rax
movq (%rax), %rdi
movq 0x88(%rax), %rax
movq 0x10(%rdi), %r12
movl 0x1c(%rax), %edi
leal (%rdi,%rdi), %ebp
cmpl %r11d, %ebp
jae 0x6913c
subl %edi, %ecx
jbe 0x690d7
movq -0x70(%rsp), %rax
movq 0x18(%rax), %rsi
xorl %edx, %edx
cmpq %rcx, %rsi
setbe %cl
jbe 0x6934c
movq -0x78(%rsp), %rax
movq $0x0, 0xf0(%rax)
movzbl -0x68(%rsp), %ecx
movzbl -0x67(%rsp), %edx
movzbl -0x66(%rsp), %esi
xorl %r11d, %r11d
xorl %r13d, %r13d
movzbl (%r12,%r11,4), %edi
movl %ecx, %ebp
subl %edi, %ebp
imull %ebp, %ebp
movzbl 0x1(%r12,%r11,4), %edi
movl %edx, %r15d
subl %edi, %r15d
imull %r15d, %r15d
addl %ebp, %r15d
movzbl 0x2(%r12,%r11,4), %edi
movl %esi, %ebp
subl %edi, %ebp
imull %ebp, %ebp
addl %r15d, %ebp
addq %rbp, %r13
incq %r11
cmpq $0x8, %r11
jne 0x690fc
jmp 0x692fb
movq %rax, -0x40(%rsp)
movl (%rax), %ecx
leal (%rcx,%rcx), %edi
cmpl %esi, %edi
jae 0x6927a
movq -0x78(%rsp), %rax
movq 0x80(%rax), %rax
movq %rax, -0x8(%rsp)
xorl %ebp, %ebp
xorl %esi, %esi
xorl %r13d, %r13d
movq -0x40(%rsp), %r8
movl (%r8,%rbp,4), %r11d
addl %r11d, %r11d
leal 0x1(%rsi), %edi
cmpl $0x4, %edi
movl $0x3, %eax
cmovbl %eax, %edi
movl %esi, %ecx
movl -0x34(%rsp,%rcx,4), %edx
cmpl %edx, %r11d
jb 0x69195
incl %esi
cmpl %esi, %edi
jne 0x69180
movl %edi, %esi
jmp 0x691ea
movq -0x8(%rsp), %rax
movl (%rax,%rbp,4), %edi
movq -0x78(%rsp), %rax
movb %sil, 0xf0(%rax,%rdi)
movzbl -0x68(%rsp,%rcx,4), %r15d
movzbl (%r12,%rdi,4), %eax
subl %eax, %r15d
imull %r15d, %r15d
movzbl -0x67(%rsp,%rcx,4), %eax
movzbl 0x1(%r12,%rdi,4), %r8d
subl %r8d, %eax
movq -0x40(%rsp), %r8
imull %eax, %eax
addl %r15d, %eax
movzbl -0x66(%rsp,%rcx,4), %ecx
movzbl 0x2(%r12,%rdi,4), %edi
subl %edi, %ecx
imull %ecx, %ecx
addl %eax, %ecx
addq %rcx, %r13
cmpl %edx, %r11d
jae 0x69201
incq %rbp
cmpq $0x8, %rbp
jne 0x6916b
movl $0x8, %ebp
cmpl $0x7, %ebp
ja 0x692fb
movq -0x78(%rsp), %rax
movq 0x80(%rax), %rcx
movzbl -0x5c(%rsp), %edx
movzbl -0x5b(%rsp), %esi
movzbl -0x5a(%rsp), %r11d
movl %ebp, %r15d
movl (%rcx,%r15,4), %eax
movq -0x78(%rsp), %rdi
movb $0x3, 0xf0(%rdi,%rax)
movzbl (%r12,%rax,4), %edi
movl %edx, %r8d
subl %edi, %r8d
imull %r8d, %r8d
movzbl 0x1(%r12,%rax,4), %edi
movl %esi, %ebp
subl %edi, %ebp
imull %ebp, %ebp
addl %r8d, %ebp
movzbl 0x2(%r12,%rax,4), %eax
movl %r11d, %edi
subl %eax, %edi
imull %edi, %edi
addl %ebp, %edi
addq %rdi, %r13
incq %r15
cmpq $0x8, %r15
jne 0x69229
jmp 0x692fb
subl %edx, %ecx
jbe 0x69295
movq -0x70(%rsp), %rax
movq 0x18(%rax), %rsi
xorl %edx, %edx
cmpq %rcx, %rsi
setbe %cl
jbe 0x6934c
movq -0x78(%rsp), %rax
movabsq $0x303030303030303, %rcx # imm = 0x303030303030303
movq %rcx, 0xf0(%rax)
movzbl -0x5c(%rsp), %ecx
movzbl -0x5b(%rsp), %edx
movzbl -0x5a(%rsp), %esi
xorl %r11d, %r11d
xorl %r13d, %r13d
movzbl (%r12,%r11,4), %edi
movl %ecx, %ebp
subl %edi, %ebp
imull %ebp, %ebp
movzbl 0x1(%r12,%r11,4), %edi
movl %edx, %r15d
subl %edi, %r15d
imull %r15d, %r15d
addl %ebp, %r15d
movzbl 0x2(%r12,%r11,4), %edi
movl %esi, %ebp
subl %edi, %ebp
imull %ebp, %ebp
addl %r15d, %ebp
addq %rbp, %r13
incq %r11
cmpq $0x8, %r11
jne 0x692c0
movq -0x70(%rsp), %rcx
cmpq 0x18(%rcx), %r13
movq -0x58(%rsp), %rsi
jae 0x6932b
movq %r13, 0x18(%rcx)
movl %esi, 0x4(%rcx)
movq -0x78(%rsp), %rax
movq 0xf0(%rax), %rax
movq %rax, 0xc(%rcx)
movb $0x1, 0x20(%rcx)
testq %r13, %r13
je 0x69358
xorl %edx, %edx
movq -0x48(%rsp), %r8
movq -0x50(%rsp), %r15
testb $0x3, %dl
jne 0x6935f
addq $-0x10, %rbx
subq $0x1, %rsi
jae 0x69013
jmp 0x6935f
movb %cl, %dl
shll $0x2, %edx
movq -0x58(%rsp), %rsi
jmp 0x69337
movl $0x2, %edx
jmp 0x6932d
movq -0x18(%rsp), %rax
movl (%rax), %eax
movq -0x70(%rsp), %rsi
movl %eax, (%rsi)
movq -0x78(%rsp), %rcx
movq (%rcx), %rcx
movb 0x18(%rcx), %cl
movb %cl, 0x8(%rsi)
movq -0x10(%rsp), %rdx
testq %rdx, %rdx
je 0x693b1
movq 0x18(%rsi), %rcx
cmpq 0x18(%rdx), %rcx
jae 0x693b1
movl %eax, (%rdx)
movl 0x4(%rsi), %eax
movl %eax, 0x4(%rdx)
movb 0x8(%rsi), %al
movb %al, 0x8(%rdx)
movups 0xc(%rsi), %xmm0
movups %xmm0, 0xc(%rdx)
movq 0x19(%rsi), %rax
movq %rax, 0x19(%rdx)
movb $0x1, %al
jmp 0x693b3
xorl %eax, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq