// File: /workspace/github_c_makefile_star2_O1/jonof[P]jftekwar/jfbuild/libsquish/clusterfit.cpp
// Function: squish::ClusterFit::Compress3(void*)
void ClusterFit::Compress3( void* block )
{
    // declare variables
    int const count = m_colours->GetCount();
    Vec4 const two = VEC4_CONST( 2.0 );
    Vec4 const one = VEC4_CONST( 1.0f );
    Vec4 const half_half2( 0.5f, 0.5f, 0.5f, 0.25f );
    Vec4 const zero = VEC4_CONST( 0.0f );
    Vec4 const half = VEC4_CONST( 0.5f );
    Vec4 const grid( 31.0f, 63.0f, 31.0f, 0.0f );
    Vec4 const gridrcp( 1.0f/31.0f, 1.0f/63.0f, 1.0f/31.0f, 0.0f );

    // prepare an ordering using the principle axis
    ConstructOrdering( m_principle, 0 );

    // check all possible clusters and iterate on the total order
    Vec4 beststart = VEC4_CONST( 0.0f );
    Vec4 bestend = VEC4_CONST( 0.0f );
    Vec4 besterror = m_besterror;
    u8 bestindices[16];
    int bestiteration = 0;
    int besti = 0, bestj = 0;

    // loop over iterations (we avoid the case that all points in first or last cluster)
    for( int iterationIndex = 0;; )
    {
        // first cluster [0,i) is at the start
        Vec4 part0 = VEC4_CONST( 0.0f );
        for( int i = 0; i < count; ++i )
        {
            // second cluster [i,j) is half along
            Vec4 part1 = ( i == 0 ) ? m_points_weights[0] : VEC4_CONST( 0.0f );
            int jmin = ( i == 0 ) ? 1 : i;
            for( int j = jmin;; )
            {
                // last cluster [j,count) is at the end
                Vec4 part2 = m_xsum_wsum - part1 - part0;

                // compute least squares terms directly
                Vec4 alphax_sum = MultiplyAdd( part1, half_half2, part0 );
                Vec4 alpha2_sum = alphax_sum.SplatW();

                Vec4 betax_sum = MultiplyAdd( part1, half_half2, part2 );
                Vec4 beta2_sum = betax_sum.SplatW();

                Vec4 alphabeta_sum = ( part1*half_half2 ).SplatW();

                // compute the least-squares optimal points
                Vec4 factor = Reciprocal( NegativeMultiplySubtract( alphabeta_sum, alphabeta_sum, alpha2_sum*beta2_sum ) );
                Vec4 a = NegativeMultiplySubtract( betax_sum, alphabeta_sum, alphax_sum*beta2_sum )*factor;
                Vec4 b = NegativeMultiplySubtract( alphax_sum, alphabeta_sum, betax_sum*alpha2_sum )*factor;

                // clamp to the grid
                a = Min( one, Max( zero, a ) );
                b = Min( one, Max( zero, b ) );
                a = Truncate( MultiplyAdd( grid, a, half ) )*gridrcp;
                b = Truncate( MultiplyAdd( grid, b, half ) )*gridrcp;

                // compute the error (we skip the constant xxsum)
                Vec4 e1 = MultiplyAdd( a*a, alpha2_sum, b*b*beta2_sum );
                Vec4 e2 = NegativeMultiplySubtract( a, alphax_sum, a*b*alphabeta_sum );
                Vec4 e3 = NegativeMultiplySubtract( b, betax_sum, e2 );
                Vec4 e4 = MultiplyAdd( two, e3, e1 );

                // apply the metric to the error term
                Vec4 e5 = e4*m_metric;
                Vec4 error = e5.SplatX() + e5.SplatY() + e5.SplatZ();

                // keep the solution if it wins
                if( CompareAnyLessThan( error, besterror ) )
                {
                    beststart = a;
                    bestend = b;
                    besti = i;
                    bestj = j;
                    besterror = error;
                    bestiteration = iterationIndex;
                }

                // advance
                if( j == count )
                    break;
                part1 += m_points_weights[j];
                ++j;
            }

            // advance
            part0 += m_points_weights[i];
        }

        // stop if we didn't improve in this iteration
        if( bestiteration != iterationIndex )
            break;

        // advance if possible
        ++iterationIndex;
        if( iterationIndex == m_iterationCount )
            break;

        // stop if a new iteration is an ordering that has already been tried
        Vec3 axis = ( bestend - beststart ).GetVec3();
        if( !ConstructOrdering( axis, iterationIndex ) )
            break;
    }

    // save the block if necessary
    if( CompareAnyLessThan( besterror, m_besterror ) )
    {
        // remap the indices
        u8 const* order = ( u8* )m_order + 16*bestiteration;

        u8 unordered[16];
        for( int m = 0; m < besti; ++m )
            unordered[order[m]] = 0;
        for( int m = besti; m < bestj; ++m )
            unordered[order[m]] = 2;
        for( int m = bestj; m < count; ++m )
            unordered[order[m]] = 1;

        m_colours->RemapIndices( unordered, bestindices );

        // save the block
        WriteColourBlock3( beststart.GetVec3(), bestend.GetVec3(), bestindices, block );

        // save the error
        m_besterror = besterror;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1d8, %rsp            # imm = 0x1D8
movq %rsi, 0xe8(%rsp)
movq %rdi, %rbx
movq 0x8(%rdi), %rax
movl (%rax), %ebp
movslq %ebp, %rax
movq %rax, 0xe0(%rsp)
leaq 0x18(%rdi), %rsi
xorl %r14d, %r14d
xorl %edx, %edx
callq 0x6ed36
movsd 0x1c4(%rbx), %xmm0
movaps %xmm0, 0x10(%rsp)
movsd 0x1cc(%rbx), %xmm0
movaps %xmm0, 0xa0(%rsp)
leaq 0xa4(%rbx), %r13
xorps %xmm5, %xmm5
leaq 0x20(%rsp), %r12
xorps %xmm0, %xmm0
movaps %xmm0, 0x80(%rsp)
movaps %xmm0, 0x70(%rsp)
movl $0x0, 0xc(%rsp)
movl $0x0, 0x8(%rsp)
movl $0x0, 0x4(%rsp)
movaps %xmm0, 0x60(%rsp)
movaps %xmm0, 0x50(%rsp)
movq %r14, 0x38(%rsp)
testl %ebp, %ebp
jle 0x6f47d
xorps %xmm0, %xmm0
movaps %xmm0, 0x90(%rsp)
xorl %r14d, %r14d
movaps %xmm0, 0x40(%rsp)
testq %r14, %r14
je 0x6ef98
xorps %xmm6, %xmm6
xorps %xmm1, %xmm1
jmp 0x6efa8
movsd 0xa4(%rbx), %xmm6
movsd 0xac(%rbx), %xmm1
cmpl $0x2, %r14d
movl $0x1, %r15d
cmovael %r14d, %r15d
movaps 0x40(%rsp), %xmm0
shufps $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
movaps %xmm0, 0x150(%rsp)
movsd 0x1a4(%rbx), %xmm0
movsd 0x1ac(%rbx), %xmm3
subps %xmm6, %xmm0
subps %xmm1, %xmm3
movaps 0x40(%rsp), %xmm2
subps %xmm2, %xmm3
movaps %xmm1, 0xf0(%rsp)
mulps 0xe89c(%rip), %xmm1     # 0x7d890
movaps %xmm1, %xmm7
addps %xmm3, %xmm7
shufps $0x1, %xmm2, %xmm3      # xmm3 = xmm3[1,0],xmm2[0,0]
shufps $0xe2, %xmm2, %xmm3     # xmm3 = xmm3[2,0],xmm2[2,3]
addps %xmm1, %xmm3
movaps %xmm1, %xmm4
shufps $0x55, %xmm1, %xmm4     # xmm4 = xmm4[1,1],xmm1[1,1]
movaps 0x150(%rsp), %xmm9
addss %xmm4, %xmm9
movaps %xmm3, %xmm2
movaps %xmm3, %xmm8
movaps %xmm3, 0x1a0(%rsp)
shufps $0x55, %xmm3, %xmm2     # xmm2 = xmm2[1,1],xmm3[1,1]
movaps %xmm9, %xmm1
movaps %xmm9, %xmm10
movaps %xmm9, 0x140(%rsp)
mulss %xmm2, %xmm1
movaps %xmm2, %xmm9
movaps %xmm4, %xmm2
mulss %xmm4, %xmm2
subss %xmm2, %xmm1
movss 0x6f3b(%rip), %xmm3     # 0x75f94
movaps %xmm3, %xmm11
divss %xmm1, %xmm11
movaps %xmm8, %xmm1
mulss %xmm9, %xmm1
movaps %xmm9, 0x170(%rsp)
movaps %xmm4, %xmm2
movaps %xmm7, 0x190(%rsp)
mulss %xmm7, %xmm2
subss %xmm2, %xmm1
mulss %xmm11, %xmm1
mulss %xmm7, %xmm10
movaps %xmm4, %xmm2
movaps %xmm4, %xmm7
movaps %xmm4, 0x110(%rsp)
mulss %xmm8, %xmm2
subss %xmm2, %xmm10
maxss %xmm5, %xmm1
minss %xmm3, %xmm1
movss 0xe877(%rip), %xmm2     # 0x7d930
mulss %xmm2, %xmm1
movaps 0x90(%rsp), %xmm4
subps %xmm4, %xmm0
movaps %xmm6, 0x100(%rsp)
movaps 0xe7c9(%rip), %xmm2     # 0x7d8a0
mulps %xmm2, %xmm6
movaps %xmm2, %xmm8
addps %xmm6, %xmm4
movaps %xmm4, 0x120(%rsp)
addps %xmm0, %xmm6
movaps %xmm6, 0x130(%rsp)
movaps %xmm4, %xmm0
mulps %xmm9, %xmm0
movaps %xmm7, %xmm2
mulps %xmm6, %xmm2
subps %xmm2, %xmm0
movss 0xa19c(%rip), %xmm2     # 0x792a8
addss %xmm2, %xmm1
movaps 0xe7a9(%rip), %xmm2     # 0x7d8c0
shufps $0x1, %xmm1, %xmm2      # xmm2 = xmm2[1,0],xmm1[0,0]
mulss %xmm11, %xmm10
shufps $0x0, %xmm11, %xmm11    # xmm11 = xmm11[0,0,0,0]
movaps %xmm11, 0xc0(%rsp)
mulps %xmm11, %xmm0
xorps %xmm1, %xmm1
maxps %xmm1, %xmm0
movaps 0xe731(%rip), %xmm1     # 0x7d870
minps %xmm1, %xmm0
movsd 0xe766(%rip), %xmm1     # 0x7d8b0
mulps %xmm1, %xmm0
addps %xmm8, %xmm0
shufps $0x24, %xmm2, %xmm0     # xmm0 = xmm0[0,1],xmm2[2,0]
movups %xmm0, 0x20(%rsp)
maxss %xmm5, %xmm10
minss %xmm3, %xmm10
movaps %xmm10, 0xb0(%rsp)
movq %r12, %rdi
callq 0x6fe86
movaps %xmm0, %xmm3
movaps %xmm1, %xmm2
movaps 0x140(%rsp), %xmm0
shufps $0x0, %xmm0, %xmm0      # xmm0 = xmm0[0,0,0,0]
movaps %xmm0, 0x180(%rsp)
mulps 0x130(%rsp), %xmm0
movaps 0x120(%rsp), %xmm1
mulps 0x110(%rsp), %xmm1
subps %xmm1, %xmm0
movaps 0xb0(%rsp), %xmm1
mulss 0xe776(%rip), %xmm1     # 0x7d930
mulps 0xc0(%rsp), %xmm0
addss 0xa0de(%rip), %xmm1     # 0x792a8
movaps %xmm1, %xmm4
movaps 0xe6ec(%rip), %xmm1     # 0x7d8c0
shufps $0x1, %xmm4, %xmm1      # xmm1 = xmm1[1,0],xmm4[0,0]
maxps 0x7471(%rip), %xmm0     # 0x76650
minps 0xe68a(%rip), %xmm0     # 0x7d870
mulps 0xe6c3(%rip), %xmm0     # 0x7d8b0
addps 0xe6ac(%rip), %xmm0     # 0x7d8a0
shufps $0x24, %xmm1, %xmm0     # xmm0 = xmm0[0,1],xmm1[2,0]
movsd 0xe6d0(%rip), %xmm1     # 0x7d8d0
mulps %xmm1, %xmm3
movaps %xmm3, 0xc0(%rsp)
movups %xmm0, 0x20(%rsp)
movaps %xmm2, 0x160(%rsp)
movss 0xe714(%rip), %xmm0     # 0x7d934
mulss %xmm0, %xmm2
movaps %xmm2, 0xb0(%rsp)
movq %r12, %rdi
callq 0x6fe86
movaps 0xb0(%rsp), %xmm13
movaps 0xc0(%rsp), %xmm8
mulps 0xe683(%rip), %xmm0     # 0x7d8d0
movaps %xmm1, %xmm2
mulss 0xe6dc(%rip), %xmm2     # 0x7d934
movaps %xmm8, %xmm4
mulss %xmm8, %xmm4
movaps %xmm0, %xmm3
mulss %xmm0, %xmm3
movaps 0x170(%rsp), %xmm12
mulss %xmm12, %xmm3
mulss 0x140(%rsp), %xmm4
addss %xmm3, %xmm4
movaps %xmm8, %xmm3
mulss %xmm0, %xmm3
movaps 0x110(%rsp), %xmm11
mulss %xmm11, %xmm3
movaps 0x120(%rsp), %xmm9
movaps %xmm9, %xmm5
mulss %xmm8, %xmm5
subss %xmm5, %xmm3
movaps 0x130(%rsp), %xmm7
movaps %xmm7, %xmm5
mulss %xmm0, %xmm5
subss %xmm5, %xmm3
addss %xmm3, %xmm3
addss %xmm4, %xmm3
movaps %xmm8, %xmm4
movss %xmm13, %xmm4           # xmm4 = xmm13[0],xmm4[1,2,3]
movaps %xmm0, %xmm5
movss %xmm2, %xmm5            # xmm5 = xmm2[0],xmm5[1,2,3]
movaps 0x190(%rsp), %xmm6
shufps $0x1, %xmm6, %xmm7      # xmm7 = xmm7[1,0],xmm6[0,0]
shufps $0xe2, %xmm6, %xmm7     # xmm7 = xmm7[2,0],xmm6[2,3]
movaps %xmm4, %xmm6
mulps %xmm5, %xmm6
mulps %xmm5, %xmm7
movaps %xmm7, %xmm10
mulps %xmm5, %xmm5
mulps %xmm12, %xmm5
movaps 0x1a0(%rsp), %xmm7
shufps $0x1, %xmm7, %xmm9      # xmm9 = xmm9[1,0],xmm7[0,0]
shufps $0xe2, %xmm7, %xmm9     # xmm9 = xmm9[2,0],xmm7[2,3]
mulps %xmm4, %xmm9
mulps %xmm4, %xmm4
mulps 0x180(%rsp), %xmm4
addps %xmm5, %xmm4
mulps %xmm11, %xmm6
subps %xmm9, %xmm6
subps %xmm10, %xmm6
mulss 0x1b4(%rbx), %xmm3
addps %xmm6, %xmm6
addps %xmm4, %xmm6
movsd 0x1b8(%rbx), %xmm5
shufps $0xe1, %xmm5, %xmm5     # xmm5 = xmm5[1,0,2,3]
mulps %xmm6, %xmm5
movaps %xmm5, %xmm4
shufps $0x55, %xmm5, %xmm4     # xmm4 = xmm4[1,1],xmm5[1,1]
addss %xmm3, %xmm4
addss %xmm5, %xmm4
shufps $0x0, %xmm4, %xmm4      # xmm4 = xmm4[0,0,0,0]
movaps 0x10(%rsp), %xmm3
unpcklpd 0xa0(%rsp), %xmm3       # xmm3 = xmm3[0],mem[0]
movaps %xmm4, %xmm5
cmpltps %xmm3, %xmm5
movmskps %xmm5, %eax
xorps %xmm5, %xmm5
testl %eax, %eax
je 0x6f3d5
shufps $0x55, %xmm1, %xmm1     # xmm1 = xmm1[1,1,1,1]
mulss %xmm5, %xmm1
unpcklps %xmm1, %xmm2            # xmm2 = xmm2[0],xmm1[0],xmm2[1],xmm1[1]
movaps 0x160(%rsp), %xmm1
shufps $0x55, %xmm1, %xmm1     # xmm1 = xmm1[1,1,1,1]
mulss %xmm5, %xmm1
unpcklps %xmm1, %xmm13           # xmm13 = xmm13[0],xmm1[0],xmm13[1],xmm1[1]
movaps %xmm4, 0x10(%rsp)
movaps %xmm4, 0xa0(%rsp)
movaps %xmm0, 0x80(%rsp)
movaps %xmm2, 0x70(%rsp)
movaps %xmm8, 0x60(%rsp)
movaps %xmm13, 0x50(%rsp)
movl %r15d, 0xc(%rsp)
movl %r14d, 0x8(%rsp)
movq 0x38(%rsp), %rax
movl %eax, 0x4(%rsp)
cmpl %ebp, %r15d
jne 0x6f3ef
movl %r15d, %eax
movaps 0x100(%rsp), %xmm6
movaps 0xf0(%rsp), %xmm1
jmp 0x6f421
movslq %r15d, %rax
shlq $0x4, %rax
movsd (%r13,%rax), %xmm0
movsd 0x8(%r13,%rax), %xmm1
movaps 0x100(%rsp), %xmm6
addps %xmm0, %xmm6
movaps 0xf0(%rsp), %xmm0
addps %xmm1, %xmm0
movaps %xmm0, %xmm1
leal 0x1(%r15), %eax
cmpl %ebp, %r15d
movl %eax, %r15d
jne 0x6efc7
movq %r14, %rax
shlq $0x4, %rax
movss (%r13,%rax), %xmm0
movaps 0x90(%rsp), %xmm1
addss %xmm1, %xmm0
shufps $0x55, %xmm1, %xmm1     # xmm1 = xmm1[1,1,1,1]
addss 0x4(%r13,%rax), %xmm1
unpcklps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movsd 0x8(%r13,%rax), %xmm1
movaps 0x40(%rsp), %xmm2
addps %xmm1, %xmm2
movaps %xmm2, 0x40(%rsp)
incq %r14
movaps %xmm0, 0x90(%rsp)
cmpq %rbp, %r14
jne 0x6ef8b
movq 0x38(%rsp), %r14
cmpl %r14d, 0x4(%rsp)
jne 0x6f492
incl %r14d
cmpl 0x14(%rbx), %r14d
jne 0x6f496
xorl %eax, %eax
jmp 0x6f4ca
movaps 0x80(%rsp), %xmm0
subps 0x60(%rsp), %xmm0
movaps 0x70(%rsp), %xmm1
subss 0x50(%rsp), %xmm1
movlps %xmm0, 0x20(%rsp)
movss %xmm1, 0x28(%rsp)
movq %rbx, %rdi
movq %r12, %rsi
movl %r14d, %edx
callq 0x6ed36
xorps %xmm5, %xmm5
testb %al, %al
jne 0x6ef6b
movups 0x1c4(%rbx), %xmm0
movaps 0x10(%rsp), %xmm1
unpcklpd 0xa0(%rsp), %xmm1       # xmm1 = xmm1[0],mem[0]
movaps %xmm1, 0x10(%rsp)
cmpltps %xmm0, %xmm1
movmskps %xmm1, %eax
testl %eax, %eax
je 0x6f5e0
movl 0x4(%rsp), %eax
shll $0x4, %eax
cltq
addq %rbx, %rax
addq $0x24, %rax
movl 0x8(%rsp), %r8d
testl %r8d, %r8d
jle 0x6f52e
movl %r8d, %ecx
xorl %edx, %edx
movzbl (%rax,%rdx), %esi
movb $0x0, 0xd0(%rsp,%rsi)
incq %rdx
cmpq %rdx, %rcx
jne 0x6f51a
movl 0xc(%rsp), %edi
cmpl %edi, %r8d
jge 0x6f551
movslq %r8d, %rcx
movslq %edi, %rdx
movzbl (%rax,%rcx), %esi
movb $0x2, 0xd0(%rsp,%rsi)
incq %rcx
cmpq %rcx, %rdx
jne 0x6f53d
cmpl %ebp, %edi
movq 0xe0(%rsp), %rsi
jge 0x6f574
movslq %edi, %rcx
movzbl (%rax,%rcx), %edx
movb $0x1, 0xd0(%rsp,%rdx)
incq %rcx
cmpq %rcx, %rsi
jne 0x6f560
movq 0x8(%rbx), %rdi
leaq 0xd0(%rsp), %rsi
leaq 0x20(%rsp), %r15
movq %r15, %rdx
callq 0x704a6
leaq 0x1c8(%rsp), %rdi
movaps 0x60(%rsp), %xmm0
movlps %xmm0, (%rdi)
movaps 0x50(%rsp), %xmm0
movss %xmm0, 0x8(%rdi)
leaq 0x1b8(%rsp), %rsi
movaps 0x80(%rsp), %xmm0
movlps %xmm0, (%rsi)
movaps 0x70(%rsp), %xmm0
movss %xmm0, 0x8(%rsi)
movq %r15, %rdx
movq 0xe8(%rsp), %rcx
callq 0x6ff28
movaps 0x10(%rsp), %xmm0
movups %xmm0, 0x1c4(%rbx)
addq $0x1d8, %rsp            # imm = 0x1D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq