// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x118, %rsp            # imm = 0x118
movq %rdi, 0x108(%rsp)
movq %rsi, 0x100(%rsp)
movl %edx, 0xfc(%rsp)
movq 0x108(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x8(%rax), %rdi
callq 0xa43d0
movl %eax, %ecx
movq 0x28(%rsp), %rax
movl %ecx, 0xf8(%rsp)
movq 0x8(%rax), %rdi
callq 0xab450
movq %rax, %rcx
movq 0x28(%rsp), %rax
movq %rcx, 0xf0(%rsp)
addq $0x24, %rax
movl 0xfc(%rsp), %ecx
shll $0x4, %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, 0xa8(%rsp)
movl $0x0, 0xa4(%rsp)
movl 0xa4(%rsp), %eax
cmpl 0xf8(%rsp), %eax
jge 0xa9436
movq 0xf0(%rsp), %rdi
movslq 0xa4(%rsp), %rax
imulq $0xc, %rax, %rax
addq %rax, %rdi
movq 0x100(%rsp), %rsi
callq 0xab480
movslq 0xa4(%rsp), %rax
movss %xmm0, 0xb0(%rsp,%rax,4)
movl 0xa4(%rsp), %eax
movb %al, %dl
movq 0xa8(%rsp), %rax
movslq 0xa4(%rsp), %rcx
movb %dl, (%rax,%rcx)
movl 0xa4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xa4(%rsp)
jmp 0xa93c2
movl $0x0, 0xa0(%rsp)
movl 0xa0(%rsp), %eax
cmpl 0xf8(%rsp), %eax
jge 0xa9541
movl 0xa0(%rsp), %eax
movl %eax, 0x9c(%rsp)
xorl %eax, %eax
cmpl $0x0, 0x9c(%rsp)
movb %al, 0x27(%rsp)
jle 0xa94a3
movslq 0x9c(%rsp), %rax
movss 0xb0(%rsp,%rax,4), %xmm1
movl 0x9c(%rsp), %eax
subl $0x1, %eax
cltq
movss 0xb0(%rsp,%rax,4), %xmm0
ucomiss %xmm1, %xmm0
seta %al
movb %al, 0x27(%rsp)
movb 0x27(%rsp), %al
testb $0x1, %al
jne 0xa94ad
jmp 0xa9529
movslq 0x9c(%rsp), %rax
leaq 0xb0(%rsp), %rdi
shlq $0x2, %rax
addq %rax, %rdi
movl 0x9c(%rsp), %eax
subl $0x1, %eax
cltq
leaq 0xb0(%rsp), %rsi
shlq $0x2, %rax
addq %rax, %rsi
callq 0xab4e0
movq 0xa8(%rsp), %rdi
movslq 0x9c(%rsp), %rax
addq %rax, %rdi
movq 0xa8(%rsp), %rsi
movl 0x9c(%rsp), %eax
subl $0x1, %eax
cltq
addq %rax, %rsi
callq 0xab520
movl 0x9c(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x9c(%rsp)
jmp 0xa9463
jmp 0xa952b
movl 0xa0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xa0(%rsp)
jmp 0xa9441
movl $0x0, 0x98(%rsp)
movl 0x98(%rsp), %eax
cmpl 0xfc(%rsp), %eax
jge 0xa961e
movq 0x28(%rsp), %rax
addq $0x24, %rax
movl 0x98(%rsp), %ecx
shll $0x4, %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, 0x90(%rsp)
movb $0x1, 0x8f(%rsp)
movl $0x0, 0x88(%rsp)
movl 0x88(%rsp), %eax
cmpl 0xf8(%rsp), %eax
jge 0xa95ef
movq 0xa8(%rsp), %rax
movslq 0x88(%rsp), %rcx
movzbl (%rax,%rcx), %eax
movq 0x90(%rsp), %rcx
movslq 0x88(%rsp), %rdx
movzbl (%rcx,%rdx), %ecx
cmpl %ecx, %eax
je 0xa95da
movb $0x0, 0x8f(%rsp)
jmp 0xa95ef
jmp 0xa95dc
movl 0x88(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x88(%rsp)
jmp 0xa9594
testb $0x1, 0x8f(%rsp)
je 0xa9606
movb $0x0, 0x117(%rsp)
jmp 0xa97af
jmp 0xa9608
movl 0x98(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x98(%rsp)
jmp 0xa954c
movq 0x28(%rsp), %rax
movq 0x8(%rax), %rdi
callq 0xab450
movq %rax, %rcx
movq 0x28(%rsp), %rax
movq %rcx, 0x80(%rsp)
movq 0x8(%rax), %rdi
callq 0xab460
movq %rax, 0x78(%rsp)
leaq 0x68(%rsp), %rdi
xorps %xmm0, %xmm0
callq 0xab410
movq 0x28(%rsp), %rax
movq 0x68(%rsp), %rcx
movq %rcx, 0x1a4(%rax)
movq 0x70(%rsp), %rcx
movq %rcx, 0x1ac(%rax)
movl $0x0, 0x64(%rsp)
movl 0x64(%rsp), %eax
cmpl 0xf8(%rsp), %eax
jge 0xa97a7
movq 0xa8(%rsp), %rax
movslq 0x64(%rsp), %rcx
movzbl (%rax,%rcx), %eax
movl %eax, 0x60(%rsp)
movq 0x80(%rsp), %rax
movslq 0x60(%rsp), %rcx
leaq (%rcx,%rcx,2), %rcx
leaq (%rax,%rcx,4), %rdi
callq 0xab550
movss %xmm0, 0xc(%rsp)
movq 0x80(%rsp), %rax
movslq 0x60(%rsp), %rcx
leaq (%rcx,%rcx,2), %rcx
leaq (%rax,%rcx,4), %rdi
callq 0xab560
movss %xmm0, 0x8(%rsp)
movq 0x80(%rsp), %rax
movslq 0x60(%rsp), %rcx
leaq (%rcx,%rcx,2), %rcx
leaq (%rax,%rcx,4), %rdi
callq 0xab570
movss 0x8(%rsp), %xmm1
movaps %xmm0, %xmm2
movss 0xc(%rsp), %xmm0
leaq 0x50(%rsp), %rdi
movq %rdi, 0x10(%rsp)
movss 0x96e3(%rip), %xmm3     # 0xb2e00
callq 0xab3c0
movq 0x78(%rsp), %rax
movslq 0x60(%rsp), %rcx
movss (%rax,%rcx,4), %xmm0
leaq 0x40(%rsp), %rdi
movq %rdi, 0x18(%rsp)
callq 0xab410
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rsi
callq 0xab580
movq 0x28(%rsp), %rdi
movlpd %xmm0, 0x30(%rsp)
movlpd %xmm1, 0x38(%rsp)
movq %rdi, %rax
addq $0xa4, %rax
movslq 0x64(%rsp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x30(%rsp), %rcx
movq %rcx, (%rax)
movq 0x38(%rsp), %rcx
movq %rcx, 0x8(%rax)
addq $0x1a4, %rdi            # imm = 0x1A4
leaq 0x30(%rsp), %rsi
callq 0xab5c0
movl 0x64(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x64(%rsp)
jmp 0xa967c
movb $0x1, 0x117(%rsp)
movb 0x117(%rsp), %al
andb $0x1, %al
addq $0x118, %rsp            # imm = 0x118
retq