// File: /workspace/github_c_makefile_star2_O3/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 $0x1c8, %rsp            # imm = 0x1C8
movq %rsi, 0xc8(%rsp)
movq %rdi, %rbx
movq 0x8(%rdi), %rax
movl (%rax), %ebp
movslq %ebp, %rax
movq %rax, 0xc0(%rsp)
leaq 0x18(%rdi), %rsi
xorl %r12d, %r12d
xorl %edx, %edx
callq 0x70a7a
movsd 0x1c4(%rbx), %xmm0
movaps %xmm0, 0x10(%rsp)
movsd 0x1cc(%rbx), %xmm0
movaps %xmm0, 0x80(%rsp)
leaq 0xa4(%rbx), %rax
movq %rax, 0xd8(%rsp)
leaq 0xac(%rbx), %rax
movq %rax, 0xd0(%rsp)
xorps %xmm5, %xmm5
leaq 0x20(%rsp), %r15
xorps %xmm0, %xmm0
movaps %xmm0, 0x60(%rsp)
movaps %xmm0, 0x50(%rsp)
movl $0x0, 0x8(%rsp)
movl $0x0, 0x4(%rsp)
xorl %ecx, %ecx
movl %ecx, 0xc(%rsp)
movl %r12d, %eax
movq %rax, 0x38(%rsp)
testl %ebp, %ebp
jle 0x711a1
xorps %xmm0, %xmm0
movaps %xmm0, 0x70(%rsp)
xorl %r14d, %r14d
movaps %xmm0, 0x40(%rsp)
cmpq $0x1, %r14
movq %r14, %r12
adcq $0x0, %r12
testq %r14, %r14
je 0x70ce9
xorps %xmm6, %xmm6
xorps %xmm1, %xmm1
jmp 0x70cf9
movsd 0xa4(%rbx), %xmm6
movsd 0xac(%rbx), %xmm1
movq %r12, %r13
shlq $0x4, %r13
addq 0xd0(%rsp), %r13
movaps 0x40(%rsp), %xmm0
shufps $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
movaps %xmm0, 0x120(%rsp)
movsd 0x1a4(%rbx), %xmm0
movsd 0x1ac(%rbx), %xmm3
subps %xmm6, %xmm0
subps %xmm1, %xmm3
movaps 0x40(%rsp), %xmm2
subps %xmm2, %xmm3
movaps %xmm1, 0x140(%rsp)
mulps 0xdfda(%rip), %xmm1     # 0x7ed20
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 0x120(%rsp), %xmm9
addss %xmm4, %xmm9
movaps %xmm3, %xmm2
movaps %xmm3, %xmm8
movaps %xmm3, 0x190(%rsp)
shufps $0x55, %xmm3, %xmm2     # xmm2 = xmm2[1,1],xmm3[1,1]
movaps %xmm9, %xmm1
movaps %xmm9, %xmm10
movaps %xmm9, 0x110(%rsp)
mulss %xmm2, %xmm1
movaps %xmm2, %xmm9
movaps %xmm4, %xmm2
mulss %xmm4, %xmm2
subss %xmm2, %xmm1
movss 0x6369(%rip), %xmm3     # 0x77114
movaps %xmm3, %xmm11
divss %xmm1, %xmm11
movaps %xmm8, %xmm1
mulss %xmm9, %xmm1
movaps %xmm9, 0x160(%rsp)
movaps %xmm4, %xmm2
movaps %xmm7, 0x180(%rsp)
mulss %xmm7, %xmm2
subss %xmm2, %xmm1
mulss %xmm11, %xmm1
mulss %xmm7, %xmm10
movaps %xmm4, %xmm2
movaps %xmm4, %xmm7
movaps %xmm4, 0xe0(%rsp)
mulss %xmm8, %xmm2
subss %xmm2, %xmm10
maxss %xmm5, %xmm1
minss %xmm3, %xmm1
movss 0xdfa5(%rip), %xmm2     # 0x7edb0
mulss %xmm2, %xmm1
movaps 0x70(%rsp), %xmm4
subps %xmm4, %xmm0
movaps %xmm6, 0x150(%rsp)
movaps 0xb84a(%rip), %xmm2     # 0x7c670
mulps %xmm2, %xmm6
movaps %xmm2, %xmm8
addps %xmm6, %xmm4
movaps %xmm4, 0xf0(%rsp)
addps %xmm0, %xmm6
movaps %xmm6, 0x100(%rsp)
movaps %xmm4, %xmm0
mulps %xmm9, %xmm0
movaps %xmm7, %xmm2
mulps %xmm6, %xmm2
subps %xmm2, %xmm0
movss 0x972d(%rip), %xmm2     # 0x7a588
addss %xmm2, %xmm1
movaps 0xdeda(%rip), %xmm2     # 0x7ed40
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, 0xa0(%rsp)
mulps %xmm11, %xmm0
xorps %xmm1, %xmm1
maxps %xmm1, %xmm0
movaps 0x98a2(%rip), %xmm1     # 0x7a730
minps %xmm1, %xmm0
movsd 0xde97(%rip), %xmm1     # 0x7ed30
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, 0x90(%rsp)
movq %r15, %rdi
callq 0x71b71
movaps %xmm0, %xmm3
movaps %xmm1, %xmm2
movaps 0x110(%rsp), %xmm0
shufps $0x0, %xmm0, %xmm0      # xmm0 = xmm0[0,0,0,0]
movaps %xmm0, 0x170(%rsp)
mulps 0x100(%rsp), %xmm0
movaps 0xf0(%rsp), %xmm1
mulps 0xe0(%rsp), %xmm1
subps %xmm1, %xmm0
movaps 0x90(%rsp), %xmm1
mulss 0xdea7(%rip), %xmm1     # 0x7edb0
mulps 0xa0(%rsp), %xmm0
addss 0x966f(%rip), %xmm1     # 0x7a588
movaps %xmm1, %xmm4
movaps 0xde1d(%rip), %xmm1     # 0x7ed40
shufps $0x1, %xmm4, %xmm1      # xmm1 = xmm1[1,0],xmm4[0,0]
maxps 0x6992(%rip), %xmm0     # 0x778c0
minps 0x97fb(%rip), %xmm0     # 0x7a730
mulps 0xddf4(%rip), %xmm0     # 0x7ed30
addps 0xb72d(%rip), %xmm0     # 0x7c670
shufps $0x24, %xmm1, %xmm0     # xmm0 = xmm0[0,1],xmm1[2,0]
movsd 0xde01(%rip), %xmm1     # 0x7ed50
mulps %xmm1, %xmm3
movaps %xmm3, 0xa0(%rsp)
movups %xmm0, 0x20(%rsp)
movaps %xmm2, 0x130(%rsp)
movss 0xde45(%rip), %xmm0     # 0x7edb4
mulss %xmm0, %xmm2
movaps %xmm2, 0x90(%rsp)
movq %r15, %rdi
callq 0x71b71
movaps 0x90(%rsp), %xmm13
movaps 0xa0(%rsp), %xmm8
mulps 0xddb4(%rip), %xmm0     # 0x7ed50
movaps %xmm1, %xmm2
mulss 0xde0d(%rip), %xmm2     # 0x7edb4
movaps %xmm8, %xmm4
mulss %xmm8, %xmm4
movaps %xmm0, %xmm3
mulss %xmm0, %xmm3
movaps 0x160(%rsp), %xmm12
mulss %xmm12, %xmm3
mulss 0x110(%rsp), %xmm4
addss %xmm3, %xmm4
movaps %xmm8, %xmm3
mulss %xmm0, %xmm3
movaps 0xe0(%rsp), %xmm11
mulss %xmm11, %xmm3
movaps 0xf0(%rsp), %xmm9
movaps %xmm9, %xmm5
mulss %xmm8, %xmm5
subss %xmm5, %xmm3
movaps 0x100(%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 0x180(%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 0x190(%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 0x170(%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 0x80(%rsp), %xmm3       # xmm3 = xmm3[0],mem[0]
movaps %xmm4, %xmm5
cmpltps %xmm3, %xmm5
movmskps %xmm5, %eax
xorps %xmm5, %xmm5
testl %eax, %eax
je 0x7111d
shufps $0x55, %xmm1, %xmm1     # xmm1 = xmm1[1,1,1,1]
mulss %xmm5, %xmm1
movaps 0x130(%rsp), %xmm3
shufps $0x55, %xmm3, %xmm3     # xmm3 = xmm3[1,1,1,1]
mulss %xmm5, %xmm3
unpcklps %xmm1, %xmm2            # xmm2 = xmm2[0],xmm1[0],xmm2[1],xmm1[1]
unpcklps %xmm3, %xmm13           # xmm13 = xmm13[0],xmm3[0],xmm13[1],xmm3[1]
movlhps %xmm13, %xmm8           # xmm8 = xmm8[0],xmm13[0]
movlhps %xmm2, %xmm0            # xmm0 = xmm0[0],xmm2[0]
movaps %xmm4, 0x10(%rsp)
movaps %xmm4, 0x80(%rsp)
movaps %xmm0, 0x60(%rsp)
movaps %xmm8, 0x50(%rsp)
movl %r12d, 0x8(%rsp)
movl %r14d, 0x4(%rsp)
movq 0x38(%rsp), %rax
movl %eax, 0xc(%rsp)
cmpq %r12, %rbp
je 0x71153
movsd -0x8(%r13), %xmm0
movsd (%r13), %xmm1
movaps 0x150(%rsp), %xmm6
addps %xmm0, %xmm6
movaps 0x140(%rsp), %xmm0
addps %xmm1, %xmm0
movaps %xmm0, %xmm1
incq %r12
addq $0x10, %r13
jmp 0x70d19
movq %r14, %rax
shlq $0x4, %rax
movq 0xd8(%rsp), %rcx
movss (%rcx,%rax), %xmm0
movaps 0x70(%rsp), %xmm1
addss %xmm1, %xmm0
shufps $0x55, %xmm1, %xmm1     # xmm1 = xmm1[1,1,1,1]
addss 0x4(%rcx,%rax), %xmm1
unpcklps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movsd 0x8(%rcx,%rax), %xmm1
movaps 0x40(%rsp), %xmm2
addps %xmm1, %xmm2
movaps %xmm2, 0x40(%rsp)
incq %r14
movaps %xmm0, 0x70(%rsp)
cmpq %rbp, %r14
jne 0x70cd1
movq 0x38(%rsp), %r14
movl 0xc(%rsp), %ecx
cmpl %r14d, %ecx
jne 0x711f2
leal 0x1(%r14), %r12d
cmpl 0x14(%rbx), %r12d
je 0x711ef
movaps 0x60(%rsp), %xmm0
subps 0x50(%rsp), %xmm0
movlps %xmm0, 0x20(%rsp)
movhlps %xmm0, %xmm0            # xmm0 = xmm0[1,1]
movss %xmm0, 0x28(%rsp)
movq %rbx, %rdi
movq %r15, %rsi
movl %r12d, %edx
callq 0x70a7a
xorps %xmm5, %xmm5
movl %r14d, %ecx
testb %al, %al
jne 0x70cad
jmp 0x711f2
movl %r14d, %ecx
movups 0x1c4(%rbx), %xmm0
movaps 0x10(%rsp), %xmm1
unpcklpd 0x80(%rsp), %xmm1       # xmm1 = xmm1[0],mem[0]
movaps %xmm1, 0x10(%rsp)
cmpltps %xmm0, %xmm1
movmskps %xmm1, %eax
testl %eax, %eax
je 0x712f6
shll $0x4, %ecx
movslq %ecx, %rax
addq %rbx, %rax
addq $0x24, %rax
movl 0x4(%rsp), %r8d
testl %r8d, %r8d
jle 0x7124b
movl %r8d, %ecx
xorl %edx, %edx
movzbl (%rax,%rdx), %esi
movb $0x0, 0xb0(%rsp,%rsi)
incq %rdx
cmpq %rdx, %rcx
jne 0x71237
movl 0x8(%rsp), %edi
cmpl %edi, %r8d
jge 0x7126e
movslq %r8d, %rcx
movslq %edi, %rdx
movzbl (%rax,%rcx), %esi
movb $0x2, 0xb0(%rsp,%rsi)
incq %rcx
cmpq %rcx, %rdx
jne 0x7125a
cmpl %ebp, %edi
movq 0xc0(%rsp), %rsi
jge 0x71291
movslq %edi, %rcx
movzbl (%rax,%rcx), %edx
movb $0x1, 0xb0(%rsp,%rdx)
incq %rcx
cmpq %rcx, %rsi
jne 0x7127d
movq 0x8(%rbx), %rdi
leaq 0xb0(%rsp), %rsi
leaq 0x20(%rsp), %r15
movq %r15, %rdx
callq 0x72196
leaq 0x1b8(%rsp), %rdi
movaps 0x50(%rsp), %xmm0
movlps %xmm0, (%rdi)
movhlps %xmm0, %xmm0            # xmm0 = xmm0[1,1]
movss %xmm0, 0x8(%rdi)
leaq 0x1a8(%rsp), %rsi
movaps 0x60(%rsp), %xmm0
movlps %xmm0, (%rsi)
movhlps %xmm0, %xmm0            # xmm0 = xmm0[1,1]
movss %xmm0, 0x8(%rsi)
movq %r15, %rdx
movq 0xc8(%rsp), %rcx
callq 0x71c14
movaps 0x10(%rsp), %xmm0
movups %xmm0, 0x1c4(%rbx)
addq $0x1c8, %rsp            # imm = 0x1C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq