// File: /workspace/github_c_makefile_star2_O1/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 %rbx
movq 0x8(%rdi), %rax
movslq (%rax), %rcx
movl %edx, %r8d
shll $0x4, %r8d
movslq %r8d, %r8
addq %rdi, %r8
addq $0x24, %r8
testq %rcx, %rcx
jle 0x6ed97
addq $0xc, %rax
xorl %r9d, %r9d
movss -0x8(%rax), %xmm0
movss -0x4(%rax), %xmm1
mulss 0x4(%rsi), %xmm1
mulss (%rsi), %xmm0
addss %xmm1, %xmm0
movss (%rax), %xmm1
mulss 0x8(%rsi), %xmm1
addss %xmm0, %xmm1
movss %xmm1, -0x48(%rsp,%r9,4)
movb %r9b, (%r8,%r9)
incq %r9
addq $0xc, %rax
cmpq %r9, %rcx
jne 0x6ed5c
testl %ecx, %ecx
jle 0x6edfc
movq $-0x1, %rax
xorl %esi, %esi
movq %rax, %r9
testl %esi, %esi
je 0x6edf1
movss -0x44(%rsp,%r9,4), %xmm0
movl %r9d, %r10d
movss -0x48(%rsp,%r10,4), %xmm1
ucomiss %xmm0, %xmm1
jbe 0x6edf1
movss %xmm1, -0x44(%rsp,%r9,4)
movss %xmm0, -0x48(%rsp,%r10,4)
movb 0x1(%r8,%r9), %r11b
movb (%r8,%r10), %bl
movb %bl, 0x1(%r8,%r9)
movb %r11b, (%r8,%r10)
leaq -0x1(%r9), %r10
incq %r9
cmpq $0x1, %r9
movq %r10, %r9
jg 0x6edab
incq %rsi
incq %rax
cmpq %rcx, %rsi
jne 0x6eda4
testl %edx, %edx
setle %al
jle 0x6ee47
leaq 0x24(%rdi), %rsi
movl %edx, %r9d
movl %ecx, %r10d
xorl %r11d, %r11d
movl %eax, %edx
testl %ecx, %ecx
jle 0x6eecc
xorl %ebx, %ebx
movb (%r8,%rbx), %bpl
cmpb (%rsi,%rbx), %bpl
jne 0x6ee33
incq %rbx
cmpq %rbx, %r10
jne 0x6ee1c
jmp 0x6eeca
incq %r11
cmpq %r9, %r11
setae %dl
addq $0x10, %rsi
cmpq %r9, %r11
jne 0x6ee12
jmp 0x6ee49
movl %eax, %edx
movq 0x8(%rdi), %rax
xorps %xmm0, %xmm0
movups %xmm0, 0x1a4(%rdi)
testl %ecx, %ecx
jle 0x6eeca
leaq 0x4(%rax), %rsi
leaq 0x1a4(%rdi), %r9
addq $0xac, %rdi
xorl %r10d, %r10d
movzbl (%r8,%r10), %r11d
leaq (%r11,%r11,2), %rbx
movss 0xc4(%rax,%r11,4), %xmm0
movsd (%rsi,%rbx,4), %xmm1
movaps %xmm0, %xmm2
shufps $0x0, %xmm0, %xmm2      # xmm2 = xmm2[0,0],xmm0[0,0]
mulps %xmm1, %xmm2
movss 0x8(%rsi,%rbx,4), %xmm1
mulss %xmm0, %xmm1
movaps %xmm1, %xmm3
unpcklps %xmm0, %xmm3            # xmm3 = xmm3[0],xmm0[0],xmm3[1],xmm0[1]
movaps %xmm2, %xmm4
movlhps %xmm3, %xmm4            # xmm4 = xmm4[0],xmm3[0]
movups %xmm4, -0x8(%rdi)
movups (%r9), %xmm3
movlhps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0]
shufps $0x24, %xmm0, %xmm2     # xmm2 = xmm2[0,1],xmm0[2,0]
addps %xmm3, %xmm2
movups %xmm2, (%r9)
incq %r10
addq $0x10, %rdi
cmpq %r10, %rcx
jne 0x6ee70
movl %edx, %eax
andb $0x1, %al
popq %rbx
popq %rbp
retq
nop