// File: /workspace/github_c_makefile_star2_O3/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 %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rcx, %r14
movq %rdi, %rbx
callq 0x71f40
leaq 0x20837(%rip), %rax     # 0x92c10
movq %rax, (%rbx)
testq %r14, %r14
je 0x723ee
movsd (%r14), %xmm0
movss 0x8(%r14), %xmm1
jmp 0x723fd
movaps 0x833b(%rip), %xmm0     # 0x7a730
movss 0x4d17(%rip), %xmm1     # 0x77114
movlps %xmm0, 0x14(%rbx)
movss %xmm1, 0x1c(%rbx)
movl $0x7f7fffff, 0x38(%rbx) # imm = 0x7F7FFFFF
movq 0x8(%rbx), %r12
movl (%r12), %r14d
leaq 0x4(%r12), %rdx
leaq 0xc4(%r12), %rcx
leaq 0x50(%rsp), %r15
movq %r15, %rdi
movl %r14d, %esi
callq 0x721bc
movq %r15, %rdi
callq 0x722f0
xorps %xmm11, %xmm11
testl %r14d, %r14d
jle 0x72468
movsd 0x4(%r12), %xmm10
movss 0xc(%r12), %xmm3
cmpl $0x1, %r14d
jne 0x7247d
movaps %xmm10, %xmm9
movaps %xmm3, %xmm8
movaps %xmm3, %xmm12
jmp 0x72520
xorps %xmm9, %xmm9
xorps %xmm8, %xmm8
xorps %xmm10, %xmm10
xorps %xmm12, %xmm12
jmp 0x72520
movaps %xmm0, %xmm2
shufps $0x55, %xmm0, %xmm2     # xmm2 = xmm2[1,1],xmm0[1,1]
movaps %xmm0, %xmm4
mulps %xmm10, %xmm4
shufps $0x55, %xmm4, %xmm4     # xmm4 = xmm4[1,1,1,1]
movaps %xmm10, %xmm6
mulss %xmm0, %xmm6
addss %xmm4, %xmm6
movaps %xmm3, %xmm5
mulss %xmm1, %xmm5
addss %xmm6, %xmm5
addq $0x10, %r12
decq %r14
movaps %xmm5, %xmm4
movaps %xmm3, %xmm8
movaps %xmm10, %xmm9
movss 0x4(%r12), %xmm6
mulss %xmm2, %xmm6
movss (%r12), %xmm7
mulss %xmm0, %xmm7
addss %xmm6, %xmm7
movss 0x8(%r12), %xmm12
movaps %xmm12, %xmm6
mulss %xmm1, %xmm6
addss %xmm7, %xmm6
ucomiss %xmm6, %xmm5
jbe 0x724f1
movsd (%r12), %xmm10
jmp 0x72510
ucomiss %xmm4, %xmm6
jbe 0x72509
movsd (%r12), %xmm9
movaps %xmm12, %xmm8
movaps %xmm3, %xmm12
movaps %xmm6, %xmm4
jmp 0x7250d
movaps %xmm3, %xmm12
movaps %xmm5, %xmm6
addq $0xc, %r12
movaps %xmm6, %xmm5
movaps %xmm12, %xmm3
decq %r14
jne 0x724b8
movss %xmm8, 0x8(%rsp)
movaps %xmm10, %xmm0
maxss %xmm11, %xmm0
shufps $0x55, %xmm10, %xmm10   # xmm10 = xmm10[1,1,1,1]
maxss %xmm11, %xmm10
maxss %xmm11, %xmm12
movss 0x4bcd(%rip), %xmm1     # 0x77114
minss %xmm1, %xmm0
minss %xmm1, %xmm10
minss %xmm1, %xmm12
movaps %xmm9, 0x20(%rsp)
shufps $0x55, %xmm9, %xmm9     # xmm9 = xmm9[1,1,1,1]
movaps %xmm9, 0x10(%rsp)
movss 0xc842(%rip), %xmm1     # 0x7edb0
mulss %xmm1, %xmm0
mulss 0x801d(%rip), %xmm10    # 0x7a598
mulss %xmm1, %xmm12
movss %xmm12, 0xc(%rsp)
movss 0x7ff9(%rip), %xmm1     # 0x7a588
addss %xmm1, %xmm0
addss %xmm1, %xmm10
movaps %xmm10, 0x30(%rsp)
ucomiss %xmm11, %xmm0
jbe 0x725ab
callq 0x6850
jmp 0x725b0
callq 0x63d0
movaps %xmm0, 0x40(%rsp)
movaps 0x20(%rsp), %xmm0
xorps %xmm1, %xmm1
maxss %xmm1, %xmm0
movaps %xmm0, 0x20(%rsp)
movaps 0x10(%rsp), %xmm0
maxss %xmm1, %xmm0
movaps %xmm0, 0x10(%rsp)
movss 0x8(%rsp), %xmm0
maxss %xmm1, %xmm0
movss %xmm0, 0x8(%rsp)
movss 0xc(%rsp), %xmm0
addss 0x7f96(%rip), %xmm0     # 0x7a588
movss %xmm0, 0xc(%rsp)
movaps 0x30(%rsp), %xmm0
ucomiss %xmm1, %xmm0
jbe 0x72609
callq 0x6850
jmp 0x7260e
callq 0x63d0
movaps %xmm0, 0x30(%rsp)
movaps 0x20(%rsp), %xmm0
movss 0x4af4(%rip), %xmm1     # 0x77114
minss %xmm1, %xmm0
movaps %xmm0, 0x20(%rsp)
movaps 0x10(%rsp), %xmm0
minss %xmm1, %xmm0
movaps %xmm0, 0x10(%rsp)
movss 0x8(%rsp), %xmm0
minss %xmm1, %xmm0
movss %xmm0, 0x8(%rsp)
xorps %xmm1, %xmm1
movss 0xc(%rsp), %xmm0
ucomiss %xmm1, %xmm0
jbe 0x7265c
callq 0x6850
jmp 0x72661
callq 0x63d0
movaps 0x40(%rsp), %xmm1
unpcklps 0x30(%rsp), %xmm1       # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
mulps 0xc6de(%rip), %xmm1     # 0x7ed50
mulss 0xc73a(%rip), %xmm0     # 0x7edb4
movlps %xmm1, 0x20(%rbx)
movss %xmm0, 0x28(%rbx)
movaps 0x20(%rsp), %xmm1
movss 0xc720(%rip), %xmm3     # 0x7edb0
mulss %xmm3, %xmm1
movaps 0x10(%rsp), %xmm2
mulss 0x7ef7(%rip), %xmm2     # 0x7a598
movss 0x8(%rsp), %xmm0
mulss %xmm3, %xmm0
movss %xmm0, 0x8(%rsp)
movss 0x7ecf(%rip), %xmm0     # 0x7a588
addss %xmm0, %xmm1
addss %xmm0, %xmm2
movaps %xmm2, 0x10(%rsp)
xorps %xmm0, %xmm0
ucomiss %xmm0, %xmm1
jbe 0x726d8
movaps %xmm1, %xmm0
callq 0x6850
jmp 0x726e0
movaps %xmm1, %xmm0
callq 0x63d0
movaps %xmm0, 0x20(%rsp)
movss 0x8(%rsp), %xmm0
addss 0x7e95(%rip), %xmm0     # 0x7a588
movss %xmm0, 0x8(%rsp)
xorps %xmm0, %xmm0
movaps 0x10(%rsp), %xmm1
ucomiss %xmm0, %xmm1
movaps %xmm1, %xmm0
jbe 0x72710
callq 0x6850
jmp 0x72715
callq 0x63d0
movaps %xmm0, 0x10(%rsp)
xorps %xmm1, %xmm1
movss 0x8(%rsp), %xmm0
ucomiss %xmm1, %xmm0
jbe 0x7272f
callq 0x6850
jmp 0x72734
callq 0x63d0
movaps 0x20(%rsp), %xmm1
unpcklps 0x10(%rsp), %xmm1       # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
mulps 0xc60b(%rip), %xmm1     # 0x7ed50
mulss 0xc667(%rip), %xmm0     # 0x7edb4
movlps %xmm1, 0x2c(%rbx)
movss %xmm0, 0x34(%rbx)
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq