// File: /workspace/github_c_makefile_star2_O0/jonof[P]jftekwar/jfbuild/src/rg_etc1.cpp
// Function: rg_etc1::pack_etc1_block_solid_color_constrained(rg_etc1::etc1_optimizer::results&, unsigned int, unsigned char const*, rg_etc1::etc1_pack_params&, bool, rg_etc1::color_quad_u8 const*)
static uint pack_etc1_block_solid_color_constrained(
      etc1_optimizer::results& results, 
      uint num_colors, const uint8* pColor, 
      etc1_pack_params& pack_params, 
      bool use_diff,
      const color_quad_u8* pBase_color5_unscaled)
   {
      RG_ETC1_ASSERT(g_etc1_inverse_lookup[0][255]);

      (void)pack_params;    //JonoF
      static uint s_next_comp[4] = { 1, 2, 0, 1 };

      uint best_error = cUINT32_MAX, best_i = 0;
      int best_x = 0, best_packed_c1 = 0, best_packed_c2 = 0;

      // For each possible 8-bit value, there is a precomputed list of diff/inten/selector configurations that allow that 8-bit value to be encoded with no error.
      for (uint i = 0; i < 3; i++)
      {
         const uint c1 = pColor[s_next_comp[i]], c2 = pColor[s_next_comp[i + 1]];

         const int delta_range = 1;
         for (int delta = -delta_range; delta <= delta_range; delta++)
         {
            const int c_plus_delta = rg_etc1::clamp<int>(pColor[i] + delta, 0, 255);

            const uint16* pTable;
            if (!c_plus_delta)
               pTable = g_color8_to_etc_block_config_0_255[0];
            else if (c_plus_delta == 255)
               pTable = g_color8_to_etc_block_config_0_255[1];
            else
               pTable = g_color8_to_etc_block_config_1_to_254[c_plus_delta - 1];

            do
            {
               const uint x = *pTable++;
               const uint diff = x & 1;
               if (static_cast<uint>(use_diff) != diff)
               {
                  if (*pTable == 0xFFFF)
                     break;
                  continue;
               }

               if ((diff) && (pBase_color5_unscaled))
               {
                  const int p0 = (x >> 8) & 255;
                  int delta = p0 - static_cast<int>(pBase_color5_unscaled->c[i]);
                  if ((delta < cETC1ColorDeltaMin) || (delta > cETC1ColorDeltaMax))
                  {
                     if (*pTable == 0xFFFF)
                        break;
                     continue;
                  }
               }

#ifdef RG_ETC1_BUILD_DEBUG
               {
                  const uint inten = (x >> 1) & 7;
                  const uint selector = (x >> 4) & 3;
                  const uint p0 = (x >> 8) & 255;
                  RG_ETC1_ASSERT(etc1_decode_value(diff, inten, selector, p0) == (uint)c_plus_delta);
               }
#endif

               const uint16* pInverse_table = g_etc1_inverse_lookup[x & 0xFF];
               uint16 p1 = pInverse_table[c1];
               uint16 p2 = pInverse_table[c2];

               if ((diff) && (pBase_color5_unscaled))
               {
                  int delta1 = (p1 & 0xFF) - static_cast<int>(pBase_color5_unscaled->c[s_next_comp[i]]);
                  int delta2 = (p2 & 0xFF) - static_cast<int>(pBase_color5_unscaled->c[s_next_comp[i + 1]]);
                  if ((delta1 < cETC1ColorDeltaMin) || (delta1 > cETC1ColorDeltaMax) || (delta2 < cETC1ColorDeltaMin) || (delta2 > cETC1ColorDeltaMax))
                  {
                     if (*pTable == 0xFFFF)
                        break;
                     continue;
                  }
               }

               const uint trial_error = rg_etc1::square(c_plus_delta - pColor[i]) + rg_etc1::square(p1 >> 8) + rg_etc1::square(p2 >> 8);
               if (trial_error < best_error)
               {
                  best_error = trial_error;
                  best_x = x;
                  best_packed_c1 = p1 & 0xFF;
                  best_packed_c2 = p2 & 0xFF;
                  best_i = i;
                  if (!best_error)
                     goto found_perfect_match;
               }
            } while (*pTable != 0xFFFF);
         }
      }
found_perfect_match:

      if (best_error == cUINT32_MAX)
         return best_error;

      best_error *= num_colors;

      results.m_n = num_colors;
      results.m_block_color4 = !(best_x & 1);
      results.m_block_inten_table = (best_x >> 1) & 7;
      memset(results.m_pSelectors, (best_x >> 4) & 3, num_colors);

      const uint best_packed_c0 = (best_x >> 8) & 255;
      results.m_block_color_unscaled[best_i] = static_cast<uint8>(best_packed_c0);
      results.m_block_color_unscaled[s_next_comp[best_i]] = static_cast<uint8>(best_packed_c1);
      results.m_block_color_unscaled[s_next_comp[best_i + 1]] = static_cast<uint8>(best_packed_c2);
      results.m_error = best_error;
      
      return best_error;
   }

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xb0, %rsp
movb %r8b, %al
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
andb $0x1, %al
movb %al, -0x29(%rbp)
movq %r9, -0x38(%rbp)
cmpw $0x0, 0x616e00(%rip)    # 0x6b838e
je 0xa1592
jmp 0xa15b1
leaq 0x17421(%rip), %rdi     # 0xb89ba
leaq 0x16d83(%rip), %rsi     # 0xb8323
movl $0x7fb, %edx            # imm = 0x7FB
leaq 0x17489(%rip), %rcx     # 0xb8a35
callq 0x61a0
movl $0xffffffff, -0x3c(%rbp) # imm = 0xFFFFFFFF
movl $0x0, -0x40(%rbp)
movl $0x0, -0x44(%rbp)
movl $0x0, -0x48(%rbp)
movl $0x0, -0x4c(%rbp)
movl $0x0, -0x50(%rbp)
cmpl $0x3, -0x50(%rbp)
jae 0xa1904
movq -0x20(%rbp), %rax
movl -0x50(%rbp), %ecx
movl %ecx, %edx
leaq 0x4062b(%rip), %rcx     # 0xe1c20
movl (%rcx,%rdx,4), %ecx
movzbl (%rax,%rcx), %eax
movl %eax, -0x54(%rbp)
movq -0x20(%rbp), %rax
movl -0x50(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, %ecx
movl %ecx, %edx
leaq 0x4060c(%rip), %rcx     # 0xe1c20
movl (%rcx,%rdx,4), %ecx
movzbl (%rax,%rcx), %eax
movl %eax, -0x58(%rbp)
movl $0x1, -0x5c(%rbp)
movl $0xffffffff, -0x60(%rbp) # imm = 0xFFFFFFFF
cmpl $0x1, -0x60(%rbp)
jg 0xa18f4
movq -0x20(%rbp), %rax
movl -0x50(%rbp), %ecx
movzbl (%rax,%rcx), %edi
addl -0x60(%rbp), %edi
xorl %esi, %esi
movl $0xff, %edx
callq 0xa1aa0
movl %eax, -0x64(%rbp)
cmpl $0x0, -0x64(%rbp)
jne 0xa1666
leaq 0x176f0(%rip), %rax     # 0xb8d50
movq %rax, -0x70(%rbp)
jmp 0xa169d
cmpl $0xff, -0x64(%rbp)
jne 0xa1680
leaq 0x176da(%rip), %rax     # 0xb8d50
addq $0x42, %rax
movq %rax, -0x70(%rbp)
jmp 0xa169b
movl -0x64(%rbp), %eax
subl $0x1, %eax
movslq %eax, %rcx
leaq 0x17750(%rip), %rax     # 0xb8de0
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x70(%rbp)
jmp 0xa169d
jmp 0xa169f
movq -0x70(%rbp), %rax
movq %rax, %rcx
addq $0x2, %rcx
movq %rcx, -0x70(%rbp)
movzwl (%rax), %eax
movl %eax, -0x74(%rbp)
movl -0x74(%rbp), %eax
andl $0x1, %eax
movl %eax, -0x78(%rbp)
movb -0x29(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl -0x78(%rbp), %eax
je 0xa16e2
movq -0x70(%rbp), %rax
movzwl (%rax), %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
jne 0xa16dd
jmp 0xa18e4
jmp 0xa18d2
cmpl $0x0, -0x78(%rbp)
je 0xa1736
cmpq $0x0, -0x38(%rbp)
je 0xa1736
movl -0x74(%rbp), %eax
shrl $0x8, %eax
andl $0xff, %eax
movl %eax, -0x7c(%rbp)
movl -0x7c(%rbp), %eax
movq -0x38(%rbp), %rcx
movl -0x50(%rbp), %edx
movzbl (%rcx,%rdx), %ecx
subl %ecx, %eax
movl %eax, -0x80(%rbp)
cmpl $-0x4, -0x80(%rbp)
jl 0xa171c
cmpl $0x3, -0x80(%rbp)
jle 0xa1734
movq -0x70(%rbp), %rax
movzwl (%rax), %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
jne 0xa172f
jmp 0xa18e4
jmp 0xa18d2
jmp 0xa1736
movl -0x74(%rbp), %eax
andl $0xff, %eax
movl %eax, %eax
movl %eax, %ecx
leaq 0x616a47(%rip), %rax    # 0x6b8190
shlq $0x9, %rcx
addq %rcx, %rax
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rax
movl -0x54(%rbp), %ecx
movw (%rax,%rcx,2), %ax
movw %ax, -0x8a(%rbp)
movq -0x88(%rbp), %rax
movl -0x58(%rbp), %ecx
movw (%rax,%rcx,2), %ax
movw %ax, -0x8c(%rbp)
cmpl $0x0, -0x78(%rbp)
je 0xa182f
cmpq $0x0, -0x38(%rbp)
je 0xa182f
movzwl -0x8a(%rbp), %eax
andl $0xff, %eax
movq -0x38(%rbp), %rcx
movl -0x50(%rbp), %edx
movl %edx, %esi
leaq 0x4046e(%rip), %rdx     # 0xe1c20
movl (%rdx,%rsi,4), %edx
movzbl (%rcx,%rdx), %ecx
subl %ecx, %eax
movl %eax, -0x90(%rbp)
movzwl -0x8c(%rbp), %eax
andl $0xff, %eax
movq -0x38(%rbp), %rcx
movl -0x50(%rbp), %edx
addl $0x1, %edx
movl %edx, %edx
movl %edx, %esi
leaq 0x4043e(%rip), %rdx     # 0xe1c20
movl (%rdx,%rsi,4), %edx
movzbl (%rcx,%rdx), %ecx
subl %ecx, %eax
movl %eax, -0x94(%rbp)
cmpl $-0x4, -0x90(%rbp)
jl 0xa1815
cmpl $0x3, -0x90(%rbp)
jg 0xa1815
cmpl $-0x4, -0x94(%rbp)
jl 0xa1815
cmpl $0x3, -0x94(%rbp)
jle 0xa182d
movq -0x70(%rbp), %rax
movzwl (%rax), %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
jne 0xa1828
jmp 0xa18e4
jmp 0xa18d2
jmp 0xa182f
movl -0x64(%rbp), %edi
movq -0x20(%rbp), %rax
movl -0x50(%rbp), %ecx
movzbl (%rax,%rcx), %eax
subl %eax, %edi
callq 0xa2cc0
movl %eax, -0xa4(%rbp)
movzwl -0x8a(%rbp), %edi
sarl $0x8, %edi
callq 0xa2cc0
movl %eax, %ecx
movl -0xa4(%rbp), %eax
addl %ecx, %eax
movl %eax, -0xa0(%rbp)
movzwl -0x8c(%rbp), %edi
sarl $0x8, %edi
callq 0xa2cc0
movl %eax, %ecx
movl -0xa0(%rbp), %eax
addl %ecx, %eax
movl %eax, -0x98(%rbp)
movl -0x98(%rbp), %eax
cmpl -0x3c(%rbp), %eax
jae 0xa18d0
movl -0x98(%rbp), %eax
movl %eax, -0x3c(%rbp)
movl -0x74(%rbp), %eax
movl %eax, -0x44(%rbp)
movzwl -0x8a(%rbp), %eax
andl $0xff, %eax
movl %eax, -0x48(%rbp)
movzwl -0x8c(%rbp), %eax
andl $0xff, %eax
movl %eax, -0x4c(%rbp)
movl -0x50(%rbp), %eax
movl %eax, -0x40(%rbp)
cmpl $0x0, -0x3c(%rbp)
jne 0xa18ce
jmp 0xa1906
jmp 0xa18d0
jmp 0xa18d2
movq -0x70(%rbp), %rax
movzwl (%rax), %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
jne 0xa169f
jmp 0xa18e6
movl -0x60(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x60(%rbp)
jmp 0xa162c
jmp 0xa18f6
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0xa15db
jmp 0xa1906
cmpl $-0x1, -0x3c(%rbp)
jne 0xa1917
movl -0x3c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0xa1a17
movl -0x14(%rbp), %eax
imull -0x3c(%rbp), %eax
movl %eax, -0x3c(%rbp)
movl -0x14(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x10(%rax)
movl -0x44(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
setne %cl
xorb $-0x1, %cl
movq -0x10(%rbp), %rax
andb $0x1, %cl
movb %cl, 0x20(%rax)
movl -0x44(%rbp), %ecx
sarl %ecx
andl $0x7, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0xc(%rax)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
movl -0x44(%rbp), %eax
sarl $0x4, %eax
andl $0x3, %eax
movl -0x14(%rbp), %ecx
movl %ecx, %edx
movzbl %al, %esi
callq 0x62d0
movl -0x44(%rbp), %eax
sarl $0x8, %eax
andl $0xff, %eax
movl %eax, -0x9c(%rbp)
movl -0x9c(%rbp), %eax
movb %al, -0xa7(%rbp)
movq -0x10(%rbp), %rdi
addq $0x8, %rdi
movl -0x40(%rbp), %esi
callq 0xa3210
movb -0xa7(%rbp), %cl
movb %cl, (%rax)
movl -0x48(%rbp), %eax
movb %al, -0xa6(%rbp)
movq -0x10(%rbp), %rdi
addq $0x8, %rdi
movl -0x40(%rbp), %eax
movl %eax, %ecx
leaq 0x4025d(%rip), %rax     # 0xe1c20
movl (%rax,%rcx,4), %esi
callq 0xa3210
movb -0xa6(%rbp), %cl
movb %cl, (%rax)
movl -0x4c(%rbp), %eax
movb %al, -0xa5(%rbp)
movq -0x10(%rbp), %rdi
addq $0x8, %rdi
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, %eax
movl %eax, %ecx
leaq 0x4022b(%rip), %rax     # 0xe1c20
movl (%rax,%rcx,4), %esi
callq 0xa3210
movb -0xa5(%rbp), %cl
movb %cl, (%rax)
movl -0x3c(%rbp), %eax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
movl -0x3c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xb0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)
nopl (%rax)