// File: /workspace/github_c_makefile_star2_O2/jonof[P]jftekwar/jfbuild/libsquish/rangefit.cpp
// Function: squish::RangeFit::RangeFit(squish::ColourSet const*, int, float*)
RangeFit::RangeFit( ColourSet const* colours, int flags, float* metric )
  : ColourFit( colours, flags )
{
    // initialise the metric (old perceptual = 0.2126f, 0.7152f, 0.0722f)
    if( metric )
        m_metric = Vec3( metric[0], metric[1], metric[2] );
    else
        m_metric = Vec3( 1.0f );

    // initialise the best error
    m_besterror = FLT_MAX;

    // cache some values
    int const count = m_colours->GetCount();
    Vec3 const* values = m_colours->GetPoints();
    float const* weights = m_colours->GetWeights();

    // get the covariance matrix
    Sym3x3 covariance = ComputeWeightedCovariance( count, values, weights );

    // compute the principle component
    Vec3 principle = ComputePrincipleComponent( covariance );

    // get the min and max range as the codebook endpoints
    Vec3 start( 0.0f );
    Vec3 end( 0.0f );
    if( count > 0 )
    {
        float min, max;

        // compute the range
        start = end = values[0];
        min = max = Dot( values[0], principle );
        for( int i = 1; i < count; ++i )
        {
            float val = Dot( values[i], principle );
            if( val < min )
            {
                start = values[i];
                min = val;
            }
            else if( val > max )
            {
                end = values[i];
                max = val;
            }
        }
    }

    // clamp the output to [0, 1]
    Vec3 const one( 1.0f );
    Vec3 const zero( 0.0f );
    start = Min( one, Max( zero, start ) );
    end = Min( one, Max( zero, end ) );

    // clamp to the grid and save
    Vec3 const grid( 31.0f, 63.0f, 31.0f );
    Vec3 const gridrcp( 1.0f/31.0f, 1.0f/63.0f, 1.0f/31.0f );
    Vec3 const half( 0.5f );
    m_start = Truncate( grid*start + half )*gridrcp;
    m_end = Truncate( grid*end + half )*gridrcp;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rcx, %r14
movq %rdi, %rbx
callq 0x66678
leaq 0x24ff0(%rip), %rax     # 0x8bbf0
movq %rax, (%rbx)
testq %r14, %r14
je 0x66c15
movsd (%r14), %xmm0
movss 0x8(%r14), %xmm1
jmp 0x66c24
movaps 0x8884(%rip), %xmm0     # 0x6f4a0
movss 0x53b0(%rip), %xmm1     # 0x6bfd4
movlps %xmm0, 0x14(%rbx)
movss %xmm1, 0x1c(%rbx)
movl $0x7f7fffff, 0x38(%rbx) # imm = 0x7F7FFFFF
movq 0x8(%rbx), %r13
movslq (%r13), %r14
leaq 0x4(%r13), %r15
leaq 0xc4(%r13), %rcx
leaq 0x50(%rsp), %r12
movq %r12, %rdi
movl %r14d, %esi
movq %r15, %rdx
callq 0x668d0
movq %r12, %rdi
callq 0x66a17
andq $0x0, 0x20(%rsp)
andl $0x0, 0x28(%rsp)
andq $0x0, 0x10(%rsp)
andl $0x0, 0x18(%rsp)
testq %r14, %r14
jle 0x66d46
movl 0x8(%r15), %ecx
leaq 0x10(%rsp), %rax
movl %ecx, 0x8(%rax)
movq (%r15), %rcx
movq %rcx, (%rax)
movq (%r15), %rdx
movl 0x8(%r15), %esi
leaq 0x20(%rsp), %rcx
movl %esi, 0x8(%rcx)
movq %rdx, (%rcx)
movaps %xmm0, %xmm2
shufps $0x55, %xmm0, %xmm2     # xmm2 = xmm2[1,1],xmm0[1,1]
movaps %xmm2, %xmm3
mulss 0x8(%r13), %xmm3
movss 0x4(%r13), %xmm4
mulss %xmm0, %xmm4
addss %xmm3, %xmm4
movss 0xc(%r13), %xmm3
mulss %xmm1, %xmm3
addss %xmm4, %xmm3
imulq $0xc, %r14, %rdx
addq $-0xc, %rdx
addq $0x10, %r13
xorl %esi, %esi
movaps %xmm3, %xmm4
cmpq %rsi, %rdx
je 0x66d46
movss 0x4(%r13,%rsi), %xmm5
mulss %xmm2, %xmm5
movss (%r13,%rsi), %xmm7
mulss %xmm0, %xmm7
addss %xmm5, %xmm7
movss 0x8(%r13,%rsi), %xmm6
mulss %xmm1, %xmm6
addss %xmm7, %xmm6
ucomiss %xmm6, %xmm4
jbe 0x66d20
movq %rcx, %rdi
movaps %xmm3, %xmm5
movaps %xmm6, %xmm4
jmp 0x66d2b
ucomiss %xmm3, %xmm6
movq %rax, %rdi
movaps %xmm6, %xmm5
jbe 0x66d40
leaq (%rsi,%r13), %r8
movl 0x8(%r8), %r9d
movl %r9d, 0x8(%rdi)
movq (%r8), %r8
movq %r8, (%rdi)
movaps %xmm5, %xmm3
addq $0xc, %rsi
jmp 0x66ce2
movsd 0x8752(%rip), %xmm0     # 0x6f4a0
leaq 0x40(%rsp), %r12
movsd %xmm0, (%r12)
leaq 0x30(%rsp), %r13
andq $0x0, (%r13)
movl $0x3f800000, 0x8(%r12)  # imm = 0x3F800000
andl $0x0, 0x8(%r13)
leaq 0x20(%rsp), %r15
movq %r13, %rdi
movq %r15, %rsi
callq 0x67279
movq %rsp, %rbp
movlps %xmm0, (%rbp)
movss %xmm1, 0x8(%rbp)
movq %r12, %rdi
movq %rbp, %rsi
callq 0x67263
movlps %xmm0, (%r15)
movss %xmm1, 0x8(%r15)
leaq 0x10(%rsp), %r14
movq %r13, %rdi
movq %r14, %rsi
callq 0x67279
movlps %xmm0, (%rbp)
movss %xmm1, 0x8(%rbp)
movq %rsp, %r13
movq %r12, %rdi
movq %r13, %rsi
callq 0x67263
movlps %xmm0, (%r14)
movss %xmm1, 0x8(%r14)
movss 0x8(%r15), %xmm0
mulss 0xcdef(%rip), %xmm0     # 0x73bd0
movsd (%r15), %xmm1
mulps 0xcdf3(%rip), %xmm1     # 0x73be0
addps 0xa5bc(%rip), %xmm1     # 0x713b0
addss 0x850c(%rip), %xmm0     # 0x6f308
movlps %xmm1, (%r13)
movss %xmm0, 0x8(%r13)
movq %rsp, %r15
movq %r15, %rdi
callq 0x672a5
mulps 0xcdf7(%rip), %xmm0     # 0x73c10
mulss 0xcdff(%rip), %xmm1     # 0x73c20
movlps %xmm0, 0x20(%rbx)
movss %xmm1, 0x28(%rbx)
movss 0xcd9e(%rip), %xmm1     # 0x73bd0
mulss 0x8(%r14), %xmm1
movsd (%r14), %xmm0
mulps 0xcd9c(%rip), %xmm0     # 0x73be0
addps 0xa565(%rip), %xmm0     # 0x713b0
addss 0x84b5(%rip), %xmm1     # 0x6f308
movlps %xmm0, (%r15)
movss %xmm1, 0x8(%r15)
movq %rsp, %rdi
callq 0x672a5
mulps 0xcda4(%rip), %xmm0     # 0x73c10
mulss 0xcdac(%rip), %xmm1     # 0x73c20
movlps %xmm0, 0x2c(%rbx)
movss %xmm1, 0x34(%rbx)
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq