// File: /workspace/github_c_makefile_star2_O2/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 $0x268, %rsp            # imm = 0x268
movq %rsi, 0xe0(%rsp)
movq %rdi, %r14
movq 0x8(%rdi), %rax
movl (%rax), %ebx
movaps 0xea77(%rip), %xmm0     # 0x73b40
leaq 0x200(%rsp), %rax
movaps %xmm0, (%rax)
movaps 0xea75(%rip), %xmm0     # 0x73b50
leaq 0x1f0(%rsp), %rax
movaps %xmm0, (%rax)
movaps 0xea73(%rip), %xmm0     # 0x73b60
leaq 0x1e0(%rsp), %rax
movaps %xmm0, (%rax)
xorps %xmm0, %xmm0
leaq 0x1d0(%rsp), %rax
movaps %xmm0, (%rax)
movaps 0xea63(%rip), %xmm0     # 0x73b70
leaq 0x1c0(%rsp), %rax
movaps %xmm0, (%rax)
movaps 0xea61(%rip), %xmm0     # 0x73b80
leaq 0x1b0(%rsp), %rax
movaps %xmm0, (%rax)
movaps 0xea5f(%rip), %xmm0     # 0x73b90
leaq 0x1a0(%rsp), %rax
movaps %xmm0, (%rax)
leaq 0x18(%rdi), %rsi
xorl %r15d, %r15d
xorl %edx, %edx
callq 0x64ef2
leaq 0xd0(%rsp), %rax
xorps %xmm0, %xmm0
movaps %xmm0, (%rax)
leaq 0xc0(%rsp), %rax
movaps %xmm0, (%rax)
leaq 0x1c4(%r14), %rax
movq %rax, 0xe8(%rsp)
movups 0x1c4(%r14), %xmm0
movaps %xmm0, 0xb0(%rsp)
leaq 0xa4(%r14), %rax
leaq 0x1a4(%r14), %rcx
movq %rcx, 0x108(%rsp)
leaq 0x1b4(%r14), %rcx
movq %rcx, 0x100(%rsp)
testl %ebx, %ebx
movl $0x0, %ecx
movq %rbx, 0x80(%rsp)
cmovgl %ebx, %ecx
movq %rcx, 0xf8(%rsp)
movl $0x0, 0x28(%rsp)
movl $0x0, 0x24(%rsp)
movq $0x0, 0x8(%rsp)
movq %rax, 0x78(%rsp)
movq %r14, 0xf0(%rsp)
movq 0x8(%rsp), %rcx
movl %ecx, 0x2c(%rsp)
movl %r15d, %ecx
movq %rcx, 0x8(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0xa0(%rsp)
xorl %edx, %edx
cmpq $0x1, %rdx
movq %rdx, %rcx
adcq $0x0, %rcx
cmpq 0xf8(%rsp), %rdx
je 0x6571d
testq %rdx, %rdx
je 0x65228
xorps %xmm0, %xmm0
jmp 0x6522b
movups (%rax), %xmm0
movaps %xmm0, 0x10(%rsp)
movq %rdx, 0x110(%rsp)
movq %rcx, 0x88(%rsp)
shlq $0x4, %rcx
addq %rax, %rcx
movq %rcx, 0x118(%rsp)
movq 0x108(%rsp), %rdi
leaq 0x10(%rsp), %rbx
movq %rbx, %rsi
callq 0x661dc
movlhps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0]
movups %xmm0, 0x60(%rsp)
leaq 0x60(%rsp), %rdi
leaq 0xa0(%rsp), %r13
movq %r13, %rsi
callq 0x661dc
movlhps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0]
movups %xmm0, 0x180(%rsp)
movq %rbx, %rdi
leaq 0x1e0(%rsp), %r15
movq %r15, %rsi
movq %r13, %rdx
callq 0x661ed
movlhps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0]
movaps %xmm0, 0x60(%rsp)
shufps $0x55, %xmm1, %xmm1     # xmm1 = xmm1[1,1,1,1]
movups %xmm1, 0x140(%rsp)
movq %rbx, %rdi
movq %r15, %rsi
leaq 0x180(%rsp), %rdx
callq 0x661ed
movlhps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0]
movaps %xmm0, 0x150(%rsp)
shufps $0x55, %xmm1, %xmm1     # xmm1 = xmm1[1,1,1,1]
movups %xmm1, 0x170(%rsp)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x661bb
shufps $0x55, %xmm1, %xmm1     # xmm1 = xmm1[1,1,1,1]
movups %xmm1, 0x160(%rsp)
leaq 0x140(%rsp), %rdi
leaq 0x170(%rsp), %r13
movq %r13, %rsi
callq 0x661bb
movlhps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0]
movups %xmm0, 0x50(%rsp)
leaq 0x160(%rsp), %rbx
movq %rbx, %rdi
movq %rbx, %rsi
leaq 0x50(%rsp), %r14
movq %r14, %rdx
callq 0x66226
movlhps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0]
movups %xmm0, 0x40(%rsp)
leaq 0x40(%rsp), %rdi
callq 0x66211
movlhps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0]
movups %xmm0, 0x258(%rsp)
leaq 0x60(%rsp), %r15
movq %r15, %rdi
movq %r13, %rsi
callq 0x661bb
movlhps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0]
movups %xmm0, 0x30(%rsp)
leaq 0x150(%rsp), %r13
movq %r13, %rdi
movq %rbx, %rsi
movq %rbx, %r12
leaq 0x30(%rsp), %rbp
movq %rbp, %rdx
callq 0x66226
movlhps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0]
movups %xmm0, 0x50(%rsp)
movq %r14, %rdi
leaq 0x258(%rsp), %rbx
movq %rbx, %rsi
callq 0x661bb
movlhps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0]
movups %xmm0, 0x40(%rsp)
movq %r13, %rdi
leaq 0x140(%rsp), %rsi
callq 0x661bb
movlhps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0]
movups %xmm0, 0x90(%rsp)
movq %r15, %rdi
movq %r12, %rsi
leaq 0x90(%rsp), %rdx
movq %rdx, %r12
callq 0x66226
movlhps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0]
movups %xmm0, 0x30(%rsp)
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x661bb
movlhps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0]
movups %xmm0, 0x50(%rsp)
leaq 0x1d0(%rsp), %r13
movq %r13, %rdi
leaq 0x40(%rsp), %rbx
movq %rbx, %rsi
callq 0x6625b
movlhps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0]
movups %xmm0, 0x30(%rsp)
leaq 0x1f0(%rsp), %r15
movq %r15, %rdi
movq %rbp, %rsi
callq 0x6624a
movlhps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0]
movups %xmm0, 0x40(%rsp)
movq %r13, %rdi
movq %r14, %rsi
callq 0x6625b
movlhps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0]
movups %xmm0, 0x30(%rsp)
movq %r15, %rdi
movq %rbp, %rsi
callq 0x6624a
movlhps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0]
movups %xmm0, 0x50(%rsp)
leaq 0x1b0(%rsp), %r13
movq %r13, %rdi
movq %rbx, %rsi
leaq 0x1c0(%rsp), %r15
movq %r15, %rdx
callq 0x661ed
movlhps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0]
movups %xmm0, 0x90(%rsp)
movq %r12, %rdi
callq 0x6626c
movlhps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0]
movups %xmm0, 0x30(%rsp)
movq %rbp, %rdi
leaq 0x1a0(%rsp), %rbx
movq %rbx, %rsi
callq 0x661bb
movlhps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0]
movups %xmm0, 0x40(%rsp)
movq %r13, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x661ed
movlhps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0]
movups %xmm0, 0x90(%rsp)
movq %r12, %rdi
movq %r12, %r13
callq 0x6626c
movlhps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0]
movups %xmm0, 0x30(%rsp)
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x661bb
movlhps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0]
movups %xmm0, 0x50(%rsp)
leaq 0x40(%rsp), %rdi
movq %rdi, %rsi
movq %rdi, %r12
callq 0x661bb
movlhps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0]
movups %xmm0, 0x90(%rsp)
movq %r14, %rdi
movq %r14, %rsi
callq 0x661bb
movlhps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0]
movups %xmm0, 0x120(%rsp)
leaq 0x120(%rsp), %rbx
movq %rbx, %rdi
leaq 0x170(%rsp), %rsi
callq 0x661bb
movlhps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0]
movups %xmm0, 0x130(%rsp)
movq %r13, %rdi
leaq 0x140(%rsp), %rsi
leaq 0x130(%rsp), %r15
movq %r15, %rdx
callq 0x661ed
movlhps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0]
movups %xmm0, 0x30(%rsp)
movq %r12, %rdi
movq %r14, %rsi
callq 0x661bb
movlhps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0]
movups %xmm0, 0x120(%rsp)
movq %rbx, %rdi
leaq 0x160(%rsp), %rsi
callq 0x661bb
movlhps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0]
movups %xmm0, 0x130(%rsp)
movq %r12, %rdi
leaq 0x60(%rsp), %rsi
movq %r15, %rdx
callq 0x66226
movlhps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0]
movups %xmm0, 0x90(%rsp)
movq %r14, %rdi
leaq 0x150(%rsp), %rsi
movq %r13, %rdx
callq 0x66226
movlhps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0]
movups %xmm0, 0x130(%rsp)
leaq 0x200(%rsp), %rdi
movq %r15, %rsi
movq %rbp, %rdx
callq 0x661ed
movlhps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0]
movups %xmm0, 0x120(%rsp)
movq %rbx, %rdi
movq 0x100(%rsp), %rsi
callq 0x661bb
movaps %xmm1, 0x190(%rsp)
movaps %xmm0, %xmm1
shufps $0x0, %xmm0, %xmm1      # xmm1 = xmm1[0,0],xmm0[0,0]
movups %xmm1, 0x238(%rsp)
shufps $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
movups %xmm0, 0x228(%rsp)
leaq 0x238(%rsp), %rdi
leaq 0x228(%rsp), %rsi
callq 0x662fc
movlhps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0]
movups %xmm0, 0x248(%rsp)
movaps 0x190(%rsp), %xmm0
shufps $0x0, %xmm0, %xmm0      # xmm0 = xmm0[0,0,0,0]
movups %xmm0, 0x218(%rsp)
leaq 0x248(%rsp), %rdi
leaq 0x218(%rsp), %rsi
callq 0x662fc
movlhps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0]
movaps %xmm0, %xmm1
cmpltps 0xb0(%rsp), %xmm1
movmskps %xmm1, %eax
testl %eax, %eax
movq 0x110(%rsp), %rdx
movq 0x88(%rsp), %rcx
je 0x656bc
movups 0x40(%rsp), %xmm1
movaps %xmm1, 0xd0(%rsp)
movups 0x50(%rsp), %xmm1
movaps %xmm1, 0xc0(%rsp)
movaps %xmm0, 0xb0(%rsp)
movl %ecx, 0x28(%rsp)
movl %edx, 0x24(%rsp)
movq 0x8(%rsp), %rax
movl %eax, 0x2c(%rsp)
cmpq %rcx, 0x80(%rsp)
movq 0x118(%rsp), %rax
je 0x656f2
movups (%rax), %xmm0
addps 0x10(%rsp), %xmm0
movaps %xmm0, 0x10(%rsp)
incq %rcx
movq %rcx, 0x88(%rsp)
addq $0x10, %rax
movq %rax, %rcx
jmp 0x65247
movq %rdx, %rax
shlq $0x4, %rax
movq 0x78(%rsp), %rcx
movups (%rcx,%rax), %xmm0
movq %rcx, %rax
addps 0xa0(%rsp), %xmm0
movaps %xmm0, 0xa0(%rsp)
incq %rdx
jmp 0x65205
movq 0x8(%rsp), %rax
movl 0x2c(%rsp), %ecx
cmpl %eax, %ecx
movq 0xf0(%rsp), %r14
jne 0x6577d
leal 0x1(%rax), %r15d
cmpl 0x14(%r14), %r15d
je 0x6577b
leaq 0xc0(%rsp), %rdi
leaq 0xd0(%rsp), %rsi
callq 0x661dc
movlps %xmm0, 0x10(%rsp)
movss %xmm1, 0x18(%rsp)
movq %r14, %rdi
leaq 0x10(%rsp), %rsi
movl %r15d, %edx
callq 0x64ef2
testb %al, %al
movq 0x78(%rsp), %rax
jne 0x651e7
jmp 0x65784
movl %eax, %ecx
movl %ecx, %eax
movq %rax, 0x8(%rsp)
leaq 0xb0(%rsp), %rdi
movq 0xe8(%rsp), %r15
movq %r15, %rsi
callq 0x6630d
testb %al, %al
je 0x65876
movq 0x8(%rsp), %rax
shll $0x4, %eax
cltq
addq %r14, %rax
addq $0x24, %rax
xorl %ecx, %ecx
movl 0x24(%rsp), %edi
testl %edi, %edi
movl $0x0, %edx
cmovgl %edi, %edx
cmpq %rcx, %rdx
je 0x657d8
movzbl (%rax,%rcx), %esi
movb $0x0, 0x10(%rsp,%rsi)
incq %rcx
jmp 0x657c5
movslq %edi, %rdx
movslq 0x28(%rsp), %rcx
cmpq %rcx, %rdx
jge 0x657f3
movzbl (%rax,%rdx), %esi
movb $0x2, 0x10(%rsp,%rsi)
incq %rdx
jmp 0x657e0
movslq 0x80(%rsp), %rdx
cmpq %rdx, %rcx
jge 0x6580e
movzbl (%rax,%rcx), %esi
movb $0x1, 0x10(%rsp,%rsi)
incq %rcx
jmp 0x657fb
movq 0x8(%r14), %rdi
leaq 0x10(%rsp), %rsi
leaq 0xa0(%rsp), %rbx
movq %rbx, %rdx
callq 0x668a8
movaps 0xd0(%rsp), %xmm0
leaq 0x180(%rsp), %rdi
movlps %xmm0, (%rdi)
movhlps %xmm0, %xmm0            # xmm0 = xmm0[1,1]
movss %xmm0, 0x8(%rdi)
movaps 0xc0(%rsp), %xmm0
leaq 0x60(%rsp), %rsi
movlps %xmm0, (%rsi)
movhlps %xmm0, %xmm0            # xmm0 = xmm0[1,1]
movss %xmm0, 0x8(%rsi)
movq %rbx, %rdx
movq 0xe0(%rsp), %rcx
callq 0x66350
movaps 0xb0(%rsp), %xmm0
movups %xmm0, (%r15)
addq $0x268, %rsp            # imm = 0x268
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq