// File: /workspace/github_c_makefile_star2_O2/jonof[P]jftekwar/jfbuild/libsquish/clusterfit.cpp
// Function: squish::ClusterFit::ConstructOrdering(squish::Vec3 const&, int)
bool ClusterFit::ConstructOrdering( Vec3 const& axis, int iteration )
{
    // cache some values
    int const count = m_colours->GetCount();
    Vec3 const* values = m_colours->GetPoints();

    // build the list of dot products
    float dps[16];
    u8* order = ( u8* )m_order + 16*iteration;
    for( int i = 0; i < count; ++i )
    {
        dps[i] = Dot( values[i], axis );
        order[i] = ( u8 )i;
    }

    // stable sort using them
    for( int i = 0; i < count; ++i )
    {
        for( int j = i; j > 0 && dps[j] < dps[j - 1]; --j )
        {
            std::swap( dps[j], dps[j - 1] );
            std::swap( order[j], order[j - 1] );
        }
    }

    // check this ordering is unique
    for( int it = 0; it < iteration; ++it )
    {
        u8 const* prev = ( u8* )m_order + 16*it;
        bool same = true;
        for( int i = 0; i < count; ++i )
        {
            if( order[i] != prev[i] )
            {
                same = false;
                break;
            }
        }
        if( same )
            return false;
    }

    // copy the ordering and weight all the points
    Vec3 const* unweighted = m_colours->GetPoints();
    float const* weights = m_colours->GetWeights();
    m_xsum_wsum = VEC4_CONST( 0.0f );
    for( int i = 0; i < count; ++i )
    {
        int j = order[i];
        Vec4 p( unweighted[j].X(), unweighted[j].Y(), unweighted[j].Z(), 1.0f );
        Vec4 w( weights[j] );
        Vec4 x = p*w;
        m_points_weights[i] = x;
        m_xsum_wsum += x;
    }
    return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rsi, %r15
movq 0x8(%rdi), %r12
movl (%r12), %r13d
addq $0x4, %r12
leaq 0x24(%rdi), %rbx
movl %edx, 0x4(%rsp)
movl %edx, %eax
shll $0x4, %eax
cltq
movq %rdi, 0x8(%rsp)
leaq (%rdi,%rax), %rbp
addq $0x24, %rbp
xorl %r14d, %r14d
testl %r13d, %r13d
cmovlel %r14d, %r13d
cmpq %r14, %r13
je 0x64f5d
movq %r12, %rdi
movq %r15, %rsi
callq 0x66196
movss %xmm0, 0x50(%rsp,%r14,4)
movb %r14b, (%rbp,%r14)
incq %r14
addq $0xc, %r12
jmp 0x64f38
xorl %eax, %eax
movq %rax, %rcx
cmpq %r13, %rax
je 0x64f99
testq %rcx, %rcx
jle 0x64f94
movss 0x4c(%rsp,%rcx,4), %xmm1
movss 0x50(%rsp,%rcx,4), %xmm0
ucomiss %xmm0, %xmm1
jbe 0x64f94
movss %xmm1, 0x50(%rsp,%rcx,4)
movss %xmm0, 0x4c(%rsp,%rcx,4)
rolw $0x8, -0x1(%rbp,%rcx)
decq %rcx
jmp 0x64f67
incq %rax
jmp 0x64f5f
xorl %r12d, %r12d
movl 0x4(%rsp), %ecx
testl %ecx, %ecx
movl $0x0, %eax
cmovgl %ecx, %eax
cmpq %rax, %r12
je 0x64fd3
xorl %ecx, %ecx
cmpq %rcx, %r13
je 0x65083
movb (%rbp,%rcx), %dl
leaq 0x1(%rcx), %rsi
cmpb (%rbx,%rcx), %dl
movq %rsi, %rcx
je 0x64fb1
incq %r12
addq $0x10, %rbx
jmp 0x64faa
movq 0x8(%rsp), %r14
movq 0x8(%r14), %rax
movq %rax, 0x18(%rsp)
leaq 0x4(%rax), %rbx
leaq 0x1a4(%r14), %rax
movq %rax, 0x10(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x1a4(%r14)
addq $0xa4, %r14
xorl %r15d, %r15d
cmpq %r15, %r13
je 0x65083
movzbl (%rbp,%r15), %eax
imulq $0xc, %rax, %rcx
movss 0x8(%rbx,%rcx), %xmm0
movsd (%rbx,%rcx), %xmm1
movsd %xmm1, 0x20(%rsp)
movss %xmm0, 0x28(%rsp)
movl $0x3f800000, 0x2c(%rsp) # imm = 0x3F800000
movq 0x18(%rsp), %rcx
movss 0xc4(%rcx,%rax,4), %xmm0
shufps $0x0, %xmm0, %xmm0      # xmm0 = xmm0[0,0,0,0]
movaps %xmm0, 0x40(%rsp)
leaq 0x20(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x661bb
movlhps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0]
movups %xmm0, 0x30(%rsp)
movups 0x30(%rsp), %xmm0
movups %xmm0, (%r14)
movq 0x10(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x661cc
incq %r15
addq $0x10, %r14
jmp 0x65006
movslq 0x4(%rsp), %rax
cmpq %rax, %r12
setge %al
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq