// File: /workspace/github_c_makefile_star2_O3/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 %rbx
movq 0x8(%rdi), %rax
movl (%rax), %ecx
movl %edx, %r8d
shll $0x4, %r8d
movslq %r8d, %r8
addq %rdi, %r8
addq $0x24, %r8
testl %ecx, %ecx
jle 0x70b41
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, -0x40(%rsp,%r9,4)
movb %r9b, (%r8,%r9)
incq %r9
addq $0xc, %rax
cmpq %r9, %rcx
jne 0x70aa1
testl %ecx, %ecx
jle 0x70b41
movq $-0x1, %rax
xorl %esi, %esi
movq %rax, %r9
testl %esi, %esi
je 0x70b36
movss -0x3c(%rsp,%r9,4), %xmm0
movl %r9d, %r10d
movss -0x40(%rsp,%r10,4), %xmm1
ucomiss %xmm0, %xmm1
jbe 0x70b36
movss %xmm1, -0x3c(%rsp,%r9,4)
movss %xmm0, -0x40(%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 0x70af0
incq %rsi
incq %rax
cmpq %rcx, %rsi
jne 0x70ae9
testl %edx, %edx
jle 0x70b84
leaq 0x24(%rdi), %rsi
movl %edx, %edx
xorl %r9d, %r9d
xorl %eax, %eax
testl %ecx, %ecx
jle 0x70c0b
xorl %r10d, %r10d
movb (%r8,%r10), %r11b
cmpb (%rsi,%r10), %r11b
jne 0x70b72
incq %r10
cmpq %r10, %rcx
jne 0x70b5b
jmp 0x70c07
incq %r9
cmpq %rdx, %r9
setae %al
addq $0x10, %rsi
cmpq %rdx, %r9
jne 0x70b50
movq 0x8(%rdi), %rdx
xorps %xmm0, %xmm0
movups %xmm0, 0x1a4(%rdi)
movb $0x1, %al
testl %ecx, %ecx
jle 0x70c07
leaq 0x4(%rdx), %rsi
leaq 0x1a4(%rdi), %r9
addq $0xac, %rdi
xorl %r10d, %r10d
movzbl (%r8,%r10), %r11d
leaq (%r11,%r11,2), %rbx
movss 0xc4(%rdx,%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 0x70bad
andb $0x1, %al
popq %rbx
retq
xorl %eax, %eax
jmp 0x70c07
nop