// File: /workspace/github_c_makefile_star2_O1/jonof[P]jftekwar/jfbuild/libsquish/colourset.cpp
// Function: squish::ColourSet::ColourSet(unsigned char const*, int, int)
ColourSet::ColourSet( u8 const* rgba, int mask, int flags )
  : m_count( 0 ),
    m_transparent( false )
{
    // check the compression mode for dxt1
    bool isDxt1 = ( ( flags & kDxt1 ) != 0 );
    bool weightByAlpha = ( ( flags & kWeightColourByAlpha ) != 0 );

    // create the minimal set
    for( int i = 0; i < 16; ++i )
    {
        // check this pixel is enabled
        int bit = 1 << i;
        if( ( mask & bit ) == 0 )
        {
            m_remap[i] = -1;
            continue;
        }

        // check for transparent pixels when using dxt1
        if( isDxt1 && rgba[4*i + 3] < 128 )
        {
            m_remap[i] = -1;
            m_transparent = true;
            continue;
        }

        // loop over previous points for a match
        for( int j = 0;; ++j )
        {
            // allocate a new point
            if( j == i )
            {
                // normalise coordinates to [0,1]
                float x = ( float )rgba[4*i] / 255.0f;
                float y = ( float )rgba[4*i + 1] / 255.0f;
                float z = ( float )rgba[4*i + 2] / 255.0f;

                // ensure there is always non-zero weight even for zero alpha
                float w = ( float )( rgba[4*i + 3] + 1 ) / 256.0f;

                // add the point
                m_points[m_count] = Vec3( x, y, z );
                m_weights[m_count] = ( weightByAlpha ? w : 1.0f );
                m_remap[i] = m_count;

                // advance
                ++m_count;
                break;
            }

            // check for a match
            int oldbit = 1 << j;
            bool match = ( ( mask & oldbit ) != 0 )
                && ( rgba[4*i] == rgba[4*j] )
                && ( rgba[4*i + 1] == rgba[4*j + 1] )
                && ( rgba[4*i + 2] == rgba[4*j + 2] )
                && ( rgba[4*j + 3] >= 128 || !isDxt1 );
            if( match )
            {
                // get the index of the match
                int index = m_remap[j];

                // ensure there is always non-zero weight even for zero alpha
                float w = ( float )( rgba[4*i + 3] + 1 ) / 256.0f;

                // map to this point and increase the weight
                m_weights[index] += ( weightByAlpha ? w : 1.0f );
                m_remap[i] = index;
                break;
            }
        }
    }

    // square root the weights
    for( int i = 0; i < m_count; ++i )
        m_weights[i] = std::sqrt( m_weights[i] );
}

// Assembly:
pushq %rbp
pushq %r14
pushq %rbx
movq %rdi, %rbx
movl $0x0, (%rdi)
leaq 0x4(%rdi), %rax
movb $0x0, 0x144(%rdi)
xorl %edi, %edi
movss 0x8fd2(%rip), %xmm0     # 0x792b4
movss 0x8fe6(%rip), %xmm1     # 0x792d0
pxor %xmm2, %xmm2
movaps 0xd68b(%rip), %xmm3     # 0x7d980
movss 0x5c97(%rip), %xmm4     # 0x75f94
xorl %r8d, %r8d
btl %edi, %edx
jae 0x70405
testb $0x1, %cl
je 0x7032c
cmpb $0x0, 0x3(%rsi,%rdi,4)
js 0x7032c
movl $0xffffffff, 0x104(%rbx,%rdi,4) # imm = 0xFFFFFFFF
movb $0x1, 0x144(%rbx)
jmp 0x70410
testq %rdi, %rdi
je 0x7038f
leaq (,%rdi,4), %r10
leaq 0x1(,%rdi,4), %r11
leaq 0x2(,%rdi,4), %r14
xorl %r9d, %r9d
btl %r9d, %edx
jae 0x70387
movb (%rsi,%r10), %bpl
cmpb (%rsi,%r9,4), %bpl
jne 0x70387
movb (%rsi,%r11), %bpl
cmpb 0x1(%rsi,%r9,4), %bpl
jne 0x70387
movb (%rsi,%r14), %bpl
cmpb 0x2(%rsi,%r9,4), %bpl
jne 0x70387
testb $0x1, %cl
je 0x7041f
cmpb $0x0, 0x3(%rsi,%r9,4)
js 0x7041f
incq %r9
cmpq %r9, %rdi
jne 0x7034c
movzbl 0x3(%rsi,%rdi,4), %r9d
incl %r9d
movzbl 0x2(%rsi,%rdi,4), %r10d
xorps %xmm6, %xmm6
cvtsi2ss %r10d, %xmm6
divss %xmm0, %xmm6
xorps %xmm5, %xmm5
cvtsi2ss %r9d, %xmm5
mulss %xmm1, %xmm5
movslq %r8d, %r8
leaq (%r8,%r8,2), %r9
movzwl (%rsi,%rdi,4), %r10d
movd %r10d, %xmm7
punpcklbw %xmm2, %xmm7    # xmm7 = xmm7[0],xmm2[0],xmm7[1],xmm2[1],xmm7[2],xmm2[2],xmm7[3],xmm2[3],xmm7[4],xmm2[4],xmm7[5],xmm2[5],xmm7[6],xmm2[6],xmm7[7],xmm2[7]
punpcklwd %xmm2, %xmm7    # xmm7 = xmm7[0],xmm2[0],xmm7[1],xmm2[1],xmm7[2],xmm2[2],xmm7[3],xmm2[3]
cvtdq2ps %xmm7, %xmm7
divps %xmm3, %xmm7
movlps %xmm7, (%rax,%r9,4)
movss %xmm6, 0x8(%rax,%r9,4)
movaps %xmm4, %xmm6
testb %cl, %cl
jns 0x703eb
movaps %xmm5, %xmm6
movss %xmm6, 0xc4(%rbx,%r8,4)
movl %r8d, 0x104(%rbx,%rdi,4)
incl %r8d
movl %r8d, (%rbx)
jmp 0x70410
movl $0xffffffff, 0x104(%rbx,%rdi,4) # imm = 0xFFFFFFFF
incq %rdi
cmpq $0x10, %rdi
jne 0x70300
jmp 0x70464
movzbl 0x3(%rsi,%r10), %r10d
incl %r10d
xorps %xmm6, %xmm6
cvtsi2ss %r10d, %xmm6
mulss %xmm1, %xmm6
movaps %xmm4, %xmm5
testb %cl, %cl
jns 0x7043e
movaps %xmm6, %xmm5
movslq 0x104(%rbx,%r9,4), %r9
addss 0xc4(%rbx,%r9,4), %xmm5
movss %xmm5, 0xc4(%rbx,%r9,4)
movl %r9d, 0x104(%rbx,%rdi,4)
jmp 0x70410
cmpl $0x0, (%rbx)
jle 0x704a1
xorl %r14d, %r14d
xorps %xmm1, %xmm1
movss 0xc4(%rbx,%r14,4), %xmm0
ucomiss %xmm1, %xmm0
jb 0x70484
sqrtss %xmm0, %xmm0
jmp 0x7048c
callq 0x6a70
xorps %xmm1, %xmm1
movss %xmm0, 0xc4(%rbx,%r14,4)
incq %r14
movslq (%rbx), %rax
cmpq %rax, %r14
jl 0x7046f
popq %rbx
popq %r14
popq %rbp
retq