// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xe0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0xc8(%rbp)
movq (%rax), %rax
testb $0x1, 0x30(%rax)
je 0x9f53a
movq -0xc8(%rbp), %rcx
movq -0x18(%rbp), %rax
movzbl (%rax), %eax
movq (%rcx), %rdx
movzbl 0x2c(%rdx), %edx
subl %edx, %eax
movl %eax, -0x2c(%rbp)
movq -0x18(%rbp), %rax
movzbl 0x1(%rax), %eax
movq (%rcx), %rdx
movzbl 0x2d(%rdx), %edx
subl %edx, %eax
movl %eax, -0x30(%rbp)
movq -0x18(%rbp), %rax
movzbl 0x2(%rax), %eax
movq (%rcx), %rcx
movzbl 0x2e(%rcx), %ecx
subl %ecx, %eax
movl %eax, -0x34(%rbp)
movl -0x2c(%rbp), %edi
movl -0x30(%rbp), %esi
movl -0x34(%rbp), %edx
callq 0xa2c60
cmpl $-0x4, %eax
jl 0x9f527
movl -0x2c(%rbp), %edi
movl -0x30(%rbp), %esi
movl -0x34(%rbp), %edx
callq 0xa2c90
cmpl $0x3, %eax
jle 0x9f538
movq -0x20(%rbp), %rax
movb $0x0, 0x20(%rax)
movb $0x0, -0x1(%rbp)
jmp 0x9fa5f
jmp 0x9f53a
movq -0x18(%rbp), %rsi
leaq -0x38(%rbp), %rdi
callq 0xa2070
movl $0x8, -0x3c(%rbp)
movq -0x20(%rbp), %rax
movq $-0x1, 0x18(%rax)
movl $0x7, -0x40(%rbp)
cmpl $0x0, -0x40(%rbp)
jl 0x9f9fa
movslq -0x40(%rbp), %rcx
leaq 0x196ca(%rip), %rax     # 0xb8c40
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, -0x48(%rbp)
leaq -0x70(%rbp), %rax
movq %rax, %rcx
addq $0x10, %rcx
movq %rcx, -0xd8(%rbp)
movq %rax, -0xd0(%rbp)
movq -0xd0(%rbp), %rdi
movq %rdi, -0xe0(%rbp)
callq 0xa1c30
movq -0xe0(%rbp), %rax
movq -0xd8(%rbp), %rcx
addq $0x4, %rax
cmpq %rcx, %rax
movq %rax, -0xd0(%rbp)
jne 0x9f59a
movl $0x0, -0x74(%rbp)
cmpl $0x4, -0x74(%rbp)
jae 0x9f63f
movq -0x48(%rbp), %rax
movl -0x74(%rbp), %ecx
movl (%rax,%rcx,4), %eax
movl %eax, -0x78(%rbp)
movzbl -0x38(%rbp), %esi
addl -0x78(%rbp), %esi
movzbl -0x37(%rbp), %edx
addl -0x78(%rbp), %edx
movzbl -0x36(%rbp), %ecx
addl -0x78(%rbp), %ecx
leaq -0x7c(%rbp), %rdi
xorl %r8d, %r8d
callq 0xa2d20
movl -0x74(%rbp), %eax
leaq -0x70(%rbp), %rdi
shlq $0x2, %rax
addq %rax, %rdi
leaq -0x7c(%rbp), %rsi
callq 0xa1ff0
movzbl -0x7c(%rbp), %ecx
movzbl -0x7b(%rbp), %eax
addl %eax, %ecx
movzbl -0x7a(%rbp), %eax
addl %eax, %ecx
movl -0x74(%rbp), %eax
movl %ecx, -0x60(%rbp,%rax,4)
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0x9f5d2
movq -0xc8(%rbp), %rax
movl -0x60(%rbp), %ecx
addl -0x5c(%rbp), %ecx
movl %ecx, -0x88(%rbp)
movl -0x5c(%rbp), %ecx
addl -0x58(%rbp), %ecx
movl %ecx, -0x84(%rbp)
movl -0x58(%rbp), %ecx
addl -0x54(%rbp), %ecx
movl %ecx, -0x80(%rbp)
movq $0x0, -0x90(%rbp)
movq (%rax), %rcx
movq 0x10(%rcx), %rcx
movq %rcx, -0x98(%rbp)
movq 0x88(%rax), %rax
movl 0x1c(%rax), %eax
shll %eax
cmpl -0x88(%rbp), %eax
jae 0x9f74f
movq -0xc8(%rbp), %rcx
movl -0x60(%rbp), %eax
movq 0x88(%rcx), %rcx
cmpl 0x1c(%rcx), %eax
jbe 0x9f6df
movq -0xc8(%rbp), %rcx
movl -0x60(%rbp), %eax
movq 0x88(%rcx), %rcx
subl 0x1c(%rcx), %eax
movl %eax, -0x9c(%rbp)
movl -0x9c(%rbp), %eax
movq -0x20(%rbp), %rcx
cmpq 0x18(%rcx), %rax
jb 0x9f6dd
jmp 0x9f9ec
jmp 0x9f6df
movq -0xc8(%rbp), %rdi
addq $0xf0, %rdi
xorl %esi, %esi
movl $0x8, %edx
callq 0x62d0
movl $0x0, -0xa0(%rbp)
cmpl $0x8, -0xa0(%rbp)
jae 0x9f74a
leaq -0x70(%rbp), %rdi
movq -0x98(%rbp), %rsi
movl -0xa0(%rbp), %eax
shlq $0x2, %rax
addq %rax, %rsi
callq 0xa2d60
movl %eax, %eax
addq -0x90(%rbp), %rax
movq %rax, -0x90(%rbp)
movl -0xa0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa0(%rbp)
jmp 0x9f703
jmp 0x9f994
movq -0xc8(%rbp), %rax
movq 0x88(%rax), %rax
movl (%rax), %eax
shll %eax
cmpl -0x80(%rbp), %eax
jb 0x9f826
movq -0xc8(%rbp), %rax
movq 0x88(%rax), %rax
movl (%rax), %eax
cmpl -0x54(%rbp), %eax
jbe 0x9f7af
movq -0xc8(%rbp), %rax
movq 0x88(%rax), %rax
movl (%rax), %eax
subl -0x54(%rbp), %eax
movl %eax, -0xa4(%rbp)
movl -0xa4(%rbp), %eax
movq -0x20(%rbp), %rcx
cmpq 0x18(%rcx), %rax
jb 0x9f7ad
jmp 0x9f9ec
jmp 0x9f7af
movq -0xc8(%rbp), %rdi
addq $0xf0, %rdi
movl $0x3, %esi
movl $0x8, %edx
callq 0x62d0
movl $0x0, -0xa8(%rbp)
cmpl $0x8, -0xa8(%rbp)
jae 0x9f821
leaq -0x70(%rbp), %rdi
addq $0xc, %rdi
movq -0x98(%rbp), %rsi
movl -0xa8(%rbp), %eax
shlq $0x2, %rax
addq %rax, %rsi
callq 0xa2d60
movl %eax, %eax
addq -0x90(%rbp), %rax
movq %rax, -0x90(%rbp)
movl -0xa8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa8(%rbp)
jmp 0x9f7d6
jmp 0x9f992
movl $0x0, -0xac(%rbp)
movl $0x0, -0xb0(%rbp)
cmpl $0x8, -0xb0(%rbp)
jae 0x9f916
movq -0xc8(%rbp), %rax
movq 0x88(%rax), %rax
movl -0xb0(%rbp), %ecx
movl (%rax,%rcx,4), %eax
movl %eax, -0xb4(%rbp)
movl -0xb4(%rbp), %eax
shll %eax
movl -0xac(%rbp), %ecx
cmpl -0x88(%rbp,%rcx,4), %eax
jb 0x9f896
movl -0xac(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xac(%rbp)
cmpl $0x2, %eax
jbe 0x9f894
jmp 0x9f918
jmp 0x9f864
movq -0xc8(%rbp), %rax
movq 0x80(%rax), %rcx
movl -0xb0(%rbp), %edx
movl (%rcx,%rdx,4), %ecx
movl %ecx, -0xb8(%rbp)
movl -0xac(%rbp), %ecx
movb %cl, %dl
movl -0xb8(%rbp), %ecx
movb %dl, 0xf0(%rax,%rcx)
movl -0xac(%rbp), %eax
leaq -0x70(%rbp), %rdi
shlq $0x2, %rax
addq %rax, %rdi
movq -0x98(%rbp), %rsi
movl -0xb8(%rbp), %eax
shlq $0x2, %rax
addq %rax, %rsi
callq 0xa2d60
movl %eax, %eax
addq -0x90(%rbp), %rax
movq %rax, -0x90(%rbp)
movl -0xb0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb0(%rbp)
jmp 0x9f83a
jmp 0x9f918
jmp 0x9f91a
cmpl $0x8, -0xb0(%rbp)
jae 0x9f990
movq -0xc8(%rbp), %rax
movq 0x80(%rax), %rcx
movl -0xb0(%rbp), %edx
movl (%rcx,%rdx,4), %ecx
movl %ecx, -0xbc(%rbp)
movl -0xbc(%rbp), %ecx
movb $0x3, 0xf0(%rax,%rcx)
leaq -0x70(%rbp), %rdi
addq $0xc, %rdi
movq -0x98(%rbp), %rsi
movl -0xbc(%rbp), %eax
shlq $0x2, %rax
addq %rax, %rsi
callq 0xa2d60
movl %eax, %eax
addq -0x90(%rbp), %rax
movq %rax, -0x90(%rbp)
movl -0xb0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb0(%rbp)
jmp 0x9f91a
jmp 0x9f992
jmp 0x9f994
movq -0x90(%rbp), %rax
movq -0x20(%rbp), %rcx
cmpq 0x18(%rcx), %rax
jae 0x9f9ea
movq -0xc8(%rbp), %rcx
movq -0x90(%rbp), %rdx
movq -0x20(%rbp), %rax
movq %rdx, 0x18(%rax)
movl -0x40(%rbp), %edx
movq -0x20(%rbp), %rax
movl %edx, 0x4(%rax)
movq -0x20(%rbp), %rax
movq 0xf0(%rcx), %rcx
movq %rcx, 0xc(%rax)
movq -0x20(%rbp), %rax
movb $0x1, 0x20(%rax)
cmpq $0x0, -0x90(%rbp)
jne 0x9f9e8
jmp 0x9f9fa
jmp 0x9f9ea
jmp 0x9f9ec
movl -0x40(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x9f561
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdi
callq 0xa1ff0
movq -0xc8(%rbp), %rax
movq (%rax), %rax
movb 0x18(%rax), %cl
movq -0x20(%rbp), %rax
andb $0x1, %cl
movb %cl, 0x8(%rax)
movb $0x0, -0xbd(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0x9fa54
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x28(%rbp), %rcx
cmpq 0x18(%rcx), %rax
jae 0x9fa52
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rdi
callq 0xa2cd0
movb $0x1, -0xbd(%rbp)
jmp 0x9fa54
movb -0xbd(%rbp), %al
andb $0x1, %al
movb %al, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0xe0, %rsp
popq %rbp
retq
nopl (%rax)