// File: /workspace/github_c_makefile_star2_O2/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
andl $0x0, (%rdi)
addq $0x4, %rdi
movb $0x0, 0x144(%rbx)
xorl %r8d, %r8d
movss 0x8c28(%rip), %xmm0     # 0x6f320
movss 0x8c30(%rip), %xmm1     # 0x6f330
pxor %xmm2, %xmm2
movaps 0x8dd5(%rip), %xmm3     # 0x6f4e0
movss 0x58c1(%rip), %xmm4     # 0x6bfd4
xorl %eax, %eax
cmpq $0x10, %r8
je 0x66867
btl %r8d, %edx
jae 0x66814
leaq (,%r8,4), %r9
testb $0x1, %cl
je 0x66753
cmpb $0x0, 0x3(%rsi,%r9)
js 0x66753
orl $-0x1, 0x104(%rbx,%r8,4)
movb $0x1, 0x144(%rbx)
jmp 0x6681d
movq %r9, %r14
orq $0x1, %r14
movq %r9, %r11
orq $0x2, %r11
xorl %r10d, %r10d
cmpq %r10, %r8
je 0x667a9
btl %r10d, %edx
jae 0x667a4
movb (%rsi,%r9), %bpl
cmpb (%rsi,%r10,4), %bpl
jne 0x667a4
movb (%rsi,%r14), %bpl
cmpb 0x1(%rsi,%r10,4), %bpl
jne 0x667a4
movb (%rsi,%r11), %bpl
cmpb 0x2(%rsi,%r10,4), %bpl
jne 0x667a4
testb $0x1, %cl
je 0x66825
cmpb $0x0, 0x3(%rsi,%r10,4)
js 0x66825
incq %r10
jmp 0x66764
movzbl 0x3(%rsi,%r9), %r9d
incl %r9d
cltq
movzbl (%rsi,%r11), %r10d
cvtsi2ss %r10d, %xmm6
imulq $0xc, %rax, %r10
cvtsi2ss %r9d, %xmm5
divss %xmm0, %xmm6
mulss %xmm1, %xmm5
movzwl (%rsi,%r8,4), %r9d
movd %r9d, %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, (%rdi,%r10)
movss %xmm6, 0x8(%rdi,%r10)
movaps %xmm4, %xmm6
testb %cl, %cl
jns 0x667fd
movaps %xmm5, %xmm6
movss %xmm6, 0xc4(%rbx,%rax,4)
movl %eax, 0x104(%rbx,%r8,4)
incl %eax
movl %eax, (%rbx)
jmp 0x6681d
orl $-0x1, 0x104(%rbx,%r8,4)
incq %r8
jmp 0x66715
movzbl 0x3(%rsi,%r9), %r9d
incl %r9d
cvtsi2ss %r9d, %xmm6
mulss %xmm1, %xmm6
movaps %xmm4, %xmm5
testb %cl, %cl
jns 0x66841
movaps %xmm6, %xmm5
movslq 0x104(%rbx,%r10,4), %r9
addss 0xc4(%rbx,%r9,4), %xmm5
movss %xmm5, 0xc4(%rbx,%r9,4)
movl %r9d, 0x104(%rbx,%r8,4)
jmp 0x6681d
xorl %r14d, %r14d
xorps %xmm1, %xmm1
cltq
cmpq %rax, %r14
jge 0x668a2
movss 0xc4(%rbx,%r14,4), %xmm0
ucomiss %xmm1, %xmm0
jb 0x66889
sqrtss %xmm0, %xmm0
jmp 0x66891
callq 0x6a70
xorps %xmm1, %xmm1
movss %xmm0, 0xc4(%rbx,%r14,4)
incq %r14
movl (%rbx), %eax
jmp 0x6686d
popq %rbx
popq %r14
popq %rbp
retq
nop