// File: /workspace/github_c_makefile_star2_O3/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 (%rdi), %r11
movzbl (%rsi), %eax
cmpb $0x1, 0x30(%r11)
jne 0x6ace0
movzbl 0x2c(%r11), %r8d
movl %eax, %r9d
subl %r8d, %r9d
movzbl 0x1(%rsi), %r8d
movzbl 0x2d(%r11), %r10d
subl %r10d, %r8d
movzbl 0x2(%rsi), %r10d
movzbl 0x2e(%r11), %r11d
subl %r11d, %r10d
cmpl %r8d, %r9d
movl %r8d, %r11d
cmovll %r9d, %r11d
cmpl %r10d, %r11d
cmovgel %r10d, %r11d
cmpl $-0x4, %r11d
jl 0x6acd7
cmpl %r8d, %r9d
cmovgl %r9d, %r8d
cmpl %r10d, %r8d
cmovlel %r10d, %r8d
cmpl $0x4, %r8d
jl 0x6ace0
movb $0x0, 0x20(%rdx)
jmp 0x6b129
cmpb $0x1, 0x8(%rsi)
movq %rsi, -0x10(%rsp)
jne 0x6ad13
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, %r10d
shll $0x4, %r10d
orl %eax, %r10d
jmp 0x6ad49
movl %eax, %r8d
shrl $0x2, %r8d
shll $0x3, %eax
orl %r8d, %eax
movzbl 0x1(%rsi), %r8d
movl %r8d, %r9d
shrl $0x2, %r9d
shll $0x3, %r8d
orl %r9d, %r8d
movzbl 0x2(%rsi), %r10d
movl %r10d, %r9d
shrl $0x2, %r9d
shll $0x3, %r10d
orl %r9d, %r10d
movl %eax, %r9d
movq %rcx, -0x8(%rsp)
movl $0xff, %eax
cmpl %eax, %r9d
cmovael %eax, %r9d
cmpl %eax, %r8d
cmovael %eax, %r8d
cmpl %eax, %r10d
cmovbl %r10d, %eax
movq $-0x1, 0x18(%rdx)
leaq 0xc(%rdx), %rcx
movq %rcx, -0x18(%rsp)
movd %r9d, %xmm0
pshufd $0x0, %xmm0, %xmm0      # xmm0 = xmm0[0,0,0,0]
movd %r8d, %xmm1
pshufd $0x0, %xmm1, %xmm1      # xmm1 = xmm1[0,0,0,0]
movd %eax, %xmm2
pshufd $0x0, %xmm2, %xmm2      # xmm2 = xmm2[0,0,0,0]
movl $0x7, %r8d
movq $-0x1, %r14
leaq 0x11fb6(%rip), %rsi     # 0x7cd60
pxor %xmm3, %xmm3
movdqa 0xca3a(%rip), %xmm4     # 0x777f0
movq %r8, %rax
shlq $0x4, %rax
movdqa (%rax,%rsi), %xmm7
movdqa %xmm7, %xmm5
paddd %xmm0, %xmm5
movdqa %xmm7, %xmm8
paddd %xmm1, %xmm8
paddd %xmm2, %xmm7
movdqa %xmm5, %xmm9
pcmpgtd %xmm3, %xmm9
pand %xmm5, %xmm9
movdqa %xmm4, %xmm6
pcmpgtd %xmm9, %xmm6
pand %xmm6, %xmm9
pandn %xmm4, %xmm6
por %xmm9, %xmm6
movdqa %xmm8, %xmm9
pcmpgtd %xmm3, %xmm9
pand %xmm8, %xmm9
movdqa %xmm4, %xmm5
pcmpgtd %xmm9, %xmm5
pand %xmm5, %xmm9
pandn %xmm4, %xmm5
por %xmm9, %xmm5
movdqa %xmm7, %xmm8
pcmpgtd %xmm3, %xmm8
pand %xmm7, %xmm8
movdqa %xmm4, %xmm7
pcmpgtd %xmm8, %xmm7
pand %xmm7, %xmm8
pandn %xmm4, %xmm7
por %xmm8, %xmm7
movdqa %xmm5, %xmm8
paddd %xmm6, %xmm5
paddd %xmm7, %xmm5
pslld $0x10, %xmm7
pslld $0x8, %xmm8
por %xmm7, %xmm8
por %xmm6, %xmm8
movdqa %xmm8, -0x38(%rsp)
movd %xmm5, %ebx
pshufd $0x55, %xmm5, %xmm6     # xmm6 = xmm5[1,1,1,1]
movd %xmm6, %eax
leal (%rax,%rbx), %r13d
movl %r13d, -0x24(%rsp)
pshufd $0xee, %xmm5, %xmm6     # xmm6 = xmm5[2,3,2,3]
movd %xmm6, %r15d
addl %r15d, %eax
movl %eax, -0x20(%rsp)
pshufd $0xff, %xmm5, %xmm5     # xmm5 = xmm5[3,3,3,3]
movd %xmm5, %ebp
addl %ebp, %r15d
movl %r15d, -0x1c(%rsp)
movq (%rdi), %rax
movq 0x88(%rdi), %r12
movq 0x10(%rax), %r11
movl 0x1c(%r12), %eax
leal (%rax,%rax), %r10d
cmpl %r13d, %r10d
jae 0x6af3b
subl %eax, %ebx
jbe 0x6aed4
cmpq %rbx, %r14
jbe 0x6b0d0
movq $0x0, 0xf0(%rdi)
movzbl -0x38(%rsp), %ebp
movzbl -0x37(%rsp), %r14d
movzbl -0x36(%rsp), %r15d
xorl %r12d, %r12d
xorl %ebx, %ebx
movzbl (%r11,%r12,4), %eax
movl %ebp, %r10d
subl %eax, %r10d
imull %r10d, %r10d
movzbl 0x1(%r11,%r12,4), %eax
movl %r14d, %r13d
subl %eax, %r13d
imull %r13d, %r13d
addl %r10d, %r13d
movzbl 0x2(%r11,%r12,4), %eax
movl %r15d, %r10d
subl %eax, %r10d
imull %r10d, %r10d
addl %r13d, %r10d
addq %r10, %rbx
incq %r12
cmpq $0x8, %r12
jne 0x6aef5
jmp 0x6b0a8
movl (%r12), %eax
leal (%rax,%rax), %r10d
cmpl %r15d, %r10d
jae 0x6afcf
movq 0x80(%rdi), %r14
xorl %r15d, %r15d
xorl %ebp, %ebp
xorl %ebx, %ebx
movl (%r12,%r15,4), %eax
addl %eax, %eax
movl %ebp, %r13d
cmpl -0x24(%rsp,%r13,4), %eax
jb 0x6af76
incl %ebp
cmpl $0x2, %ebp
jbe 0x6af60
jmp 0x6b046
movl (%r14,%r15,4), %eax
movb %bpl, 0xf0(%rdi,%rax)
movzbl -0x38(%rsp,%r13,4), %r10d
movzbl (%r11,%rax,4), %r9d
subl %r9d, %r10d
imull %r10d, %r10d
movzbl -0x37(%rsp,%r13,4), %r9d
movzbl 0x1(%r11,%rax,4), %ecx
subl %ecx, %r9d
imull %r9d, %r9d
addl %r10d, %r9d
movzbl -0x36(%rsp,%r13,4), %ecx
movzbl 0x2(%r11,%rax,4), %eax
subl %eax, %ecx
imull %ecx, %ecx
addl %r9d, %ecx
addq %rcx, %rbx
incq %r15
cmpq $0x8, %r15
jne 0x6af5a
jmp 0x6b0a8
subl %ebp, %eax
jbe 0x6afdc
cmpq %rax, %r14
jbe 0x6b0d0
movabsq $0x303030303030303, %rax # imm = 0x303030303030303
movq %rax, 0xf0(%rdi)
movzbl -0x2c(%rsp), %ebp
movzbl -0x2b(%rsp), %r14d
movzbl -0x2a(%rsp), %r15d
xorl %r12d, %r12d
xorl %ebx, %ebx
movzbl (%r11,%r12,4), %eax
movl %ebp, %r10d
subl %eax, %r10d
imull %r10d, %r10d
movzbl 0x1(%r11,%r12,4), %eax
movl %r14d, %r13d
subl %eax, %r13d
imull %r13d, %r13d
addl %r10d, %r13d
movzbl 0x2(%r11,%r12,4), %eax
movl %r15d, %r10d
subl %eax, %r10d
imull %r10d, %r10d
addl %r13d, %r10d
addq %r10, %rbx
incq %r12
cmpq $0x8, %r12
jne 0x6b003
jmp 0x6b0a8
cmpq $0x7, %r15
ja 0x6b0a8
movzbl -0x2c(%rsp), %ebp
movzbl -0x2b(%rsp), %r12d
movzbl -0x2a(%rsp), %r13d
movl (%r14,%r15,4), %eax
movb $0x3, 0xf0(%rdi,%rax)
movzbl (%r11,%rax,4), %ecx
movl %ebp, %r9d
subl %ecx, %r9d
imull %r9d, %r9d
movzbl 0x1(%r11,%rax,4), %ecx
movl %r12d, %r10d
subl %ecx, %r10d
imull %r10d, %r10d
addl %r9d, %r10d
movzbl 0x2(%r11,%rax,4), %eax
movl %r13d, %ecx
subl %eax, %ecx
imull %ecx, %ecx
addl %r10d, %ecx
addq %rcx, %rbx
incq %r15
cmpq $0x8, %r15
jne 0x6b05d
movq 0x18(%rdx), %r14
cmpq %r14, %rbx
jae 0x6b0d0
movq %rbx, 0x18(%rdx)
movl %r8d, 0x4(%rdx)
movq 0xf0(%rdi), %rax
movq %rax, 0xc(%rdx)
movb $0x1, 0x20(%rdx)
movq %rbx, %r14
testq %rbx, %rbx
je 0x6b0dc
subq $0x1, %r8
jae 0x6adb6
jmp 0x6b0df
xorl %r14d, %r14d
movq -0x10(%rsp), %rax
movl (%rax), %eax
movl %eax, (%rdx)
movq (%rdi), %rcx
movb 0x18(%rcx), %cl
movb %cl, 0x8(%rdx)
movq -0x8(%rsp), %rcx
testq %rcx, %rcx
je 0x6b129
cmpq 0x18(%rcx), %r14
jae 0x6b129
movl %eax, (%rcx)
movl 0x4(%rdx), %eax
movl %eax, 0x4(%rcx)
movb 0x8(%rdx), %al
movb %al, 0x8(%rcx)
movq -0x18(%rsp), %rax
movdqu (%rax), %xmm0
movdqu %xmm0, 0xc(%rcx)
movq 0xd(%rax), %rax
movq %rax, 0x19(%rcx)
movb $0x1, %al
jmp 0x6b12b
xorl %eax, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq