// File: /workspace/github_c_makefile_star2_O3/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 0x85de(%rip), %xmm0     # 0x7a5a0
movss 0x85e6(%rip), %xmm1     # 0x7a5b0
movaps 0x87cf(%rip), %xmm2     # 0x7a7a0
movss 0x513b(%rip), %xmm3     # 0x77114
xorl %r8d, %r8d
btl %edi, %edx
jae 0x7206d
testb $0x1, %cl
je 0x72008
cmpb $0x0, 0x3(%rsi,%rdi,4)
js 0x72008
movl $0xffffffff, 0x104(%rbx,%rdi,4) # imm = 0xFFFFFFFF
movb $0x1, 0x144(%rbx)
jmp 0x72100
testq %rdi, %rdi
je 0x7207d
leaq (,%rdi,4), %r11
leaq 0x1(,%rdi,4), %r9
leaq 0x2(,%rdi,4), %r10
xorl %r14d, %r14d
btl %r14d, %edx
jae 0x72063
movb (%rsi,%r11), %bpl
cmpb (%rsi,%r14,4), %bpl
jne 0x72063
movb (%rsi,%r9), %bpl
cmpb 0x1(%rsi,%r14,4), %bpl
jne 0x72063
movb (%rsi,%r10), %bpl
cmpb 0x2(%rsi,%r14,4), %bpl
jne 0x72063
testb $0x1, %cl
je 0x7210f
cmpb $0x0, 0x3(%rsi,%r14,4)
js 0x7210f
incq %r14
cmpq %r14, %rdi
jne 0x72028
jmp 0x7208c
movl $0xffffffff, 0x104(%rbx,%rdi,4) # imm = 0xFFFFFFFF
jmp 0x72100
movl $0x1, %r9d
movl $0x2, %r10d
xorl %r11d, %r11d
movzbl 0x3(%rsi,%r11), %ebp
incl %ebp
movzbl (%rsi,%r11), %r11d
movzbl (%rsi,%r9), %r9d
movzbl (%rsi,%r10), %r10d
xorps %xmm5, %xmm5
cvtsi2ss %r10d, %xmm5
divss %xmm0, %xmm5
xorps %xmm4, %xmm4
cvtsi2ss %ebp, %xmm4
mulss %xmm1, %xmm4
movslq %r8d, %r8
leaq (%r8,%r8,2), %r10
movd %r11d, %xmm6
pinsrw $0x2, %r9d, %xmm6
cvtdq2ps %xmm6, %xmm6
divps %xmm2, %xmm6
movlps %xmm6, (%rax,%r10,4)
movss %xmm5, 0x8(%rax,%r10,4)
movaps %xmm3, %xmm5
testb %cl, %cl
jns 0x720e8
movaps %xmm4, %xmm5
movss %xmm5, 0xc4(%rbx,%r8,4)
movl %r8d, 0x104(%rbx,%rdi,4)
incl %r8d
movl %r8d, (%rbx)
incq %rdi
cmpq $0x10, %rdi
jne 0x71fdc
jmp 0x72154
movzbl 0x3(%rsi,%r11), %r9d
incl %r9d
xorps %xmm5, %xmm5
cvtsi2ss %r9d, %xmm5
mulss %xmm1, %xmm5
movaps %xmm3, %xmm4
testb %cl, %cl
jns 0x7212e
movaps %xmm5, %xmm4
movslq 0x104(%rbx,%r14,4), %r9
addss 0xc4(%rbx,%r9,4), %xmm4
movss %xmm4, 0xc4(%rbx,%r9,4)
movl %r9d, 0x104(%rbx,%rdi,4)
jmp 0x72100
testl %r8d, %r8d
jle 0x72191
xorl %r14d, %r14d
xorps %xmm1, %xmm1
movss 0xc4(%rbx,%r14,4), %xmm0
ucomiss %xmm1, %xmm0
jb 0x72174
sqrtss %xmm0, %xmm0
jmp 0x7217c
callq 0x6a70
xorps %xmm1, %xmm1
movss %xmm0, 0xc4(%rbx,%r14,4)
incq %r14
movslq (%rbx), %rax
cmpq %rax, %r14
jl 0x7215f
popq %rbx
popq %r14
popq %rbp
retq