// File: /workspace/github_c_makefile_star2_O1/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 0x70260
leaq 0x214f3(%rip), %rax     # 0x91c10
movq %rax, (%rbx)
testq %r14, %r14
je 0x70732
movsd (%r14), %xmm0
movss 0x8(%r14), %xmm1
jmp 0x70741
movaps 0xd137(%rip), %xmm0     # 0x7d870
movss 0x5853(%rip), %xmm1     # 0x75f94
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 0x704cc
movq %r15, %rdi
callq 0x7060d
xorps %xmm11, %xmm11
testl %r14d, %r14d
jle 0x707ac
movsd 0x4(%r12), %xmm10
movss 0xc(%r12), %xmm3
cmpl $0x1, %r14d
jne 0x707c1
movaps %xmm10, %xmm9
movaps %xmm3, %xmm8
movaps %xmm3, %xmm12
jmp 0x70867
xorps %xmm9, %xmm9
xorps %xmm8, %xmm8
xorps %xmm10, %xmm10
xorps %xmm12, %xmm12
jmp 0x70867
movaps %xmm0, %xmm2
shufps $0x55, %xmm0, %xmm2     # xmm2 = xmm2[1,1],xmm0[1,1]
movss 0x8(%r12), %xmm4
mulss %xmm2, %xmm4
movss 0x4(%r12), %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 0x70838
movsd (%r12), %xmm10
jmp 0x70857
ucomiss %xmm4, %xmm6
jbe 0x70850
movsd (%r12), %xmm9
movaps %xmm12, %xmm8
movaps %xmm3, %xmm12
movaps %xmm6, %xmm4
jmp 0x70854
movaps %xmm3, %xmm12
movaps %xmm5, %xmm6
addq $0xc, %r12
movaps %xmm6, %xmm5
movaps %xmm12, %xmm3
decq %r14
jne 0x707ff
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 0x5706(%rip), %xmm1     # 0x75f94
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 0xd07b(%rip), %xmm1     # 0x7d930
mulss %xmm1, %xmm0
mulss 0x89fa(%rip), %xmm10    # 0x792bc
mulss %xmm1, %xmm12
movss %xmm12, 0xc(%rsp)
movss 0x89d2(%rip), %xmm1     # 0x792a8
addss %xmm1, %xmm0
addss %xmm1, %xmm10
movaps %xmm10, 0x30(%rsp)
ucomiss %xmm11, %xmm0
jbe 0x708f2
callq 0x6850
jmp 0x708f7
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 0x896f(%rip), %xmm0     # 0x792a8
movss %xmm0, 0xc(%rsp)
movaps 0x30(%rsp), %xmm0
ucomiss %xmm1, %xmm0
jbe 0x70950
callq 0x6850
jmp 0x70955
callq 0x63d0
movaps %xmm0, 0x30(%rsp)
movaps 0x20(%rsp), %xmm0
movss 0x562d(%rip), %xmm1     # 0x75f94
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 0x709a3
callq 0x6850
jmp 0x709a8
callq 0x63d0
movaps 0x40(%rsp), %xmm1
unpcklps 0x30(%rsp), %xmm1       # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
mulps 0xcf17(%rip), %xmm1     # 0x7d8d0
mulss 0xcf73(%rip), %xmm0     # 0x7d934
movlps %xmm1, 0x20(%rbx)
movss %xmm0, 0x28(%rbx)
movaps 0x20(%rsp), %xmm1
movss 0xcf59(%rip), %xmm3     # 0x7d930
mulss %xmm3, %xmm1
movaps 0x10(%rsp), %xmm2
mulss 0x88d4(%rip), %xmm2     # 0x792bc
movss 0x8(%rsp), %xmm0
mulss %xmm3, %xmm0
movss %xmm0, 0x8(%rsp)
movss 0x88a8(%rip), %xmm0     # 0x792a8
addss %xmm0, %xmm1
addss %xmm0, %xmm2
movaps %xmm2, 0x10(%rsp)
xorps %xmm0, %xmm0
ucomiss %xmm0, %xmm1
jbe 0x70a1f
movaps %xmm1, %xmm0
callq 0x6850
jmp 0x70a27
movaps %xmm1, %xmm0
callq 0x63d0
movaps %xmm0, 0x20(%rsp)
movss 0x8(%rsp), %xmm0
addss 0x886e(%rip), %xmm0     # 0x792a8
movss %xmm0, 0x8(%rsp)
xorps %xmm0, %xmm0
movaps 0x10(%rsp), %xmm1
ucomiss %xmm0, %xmm1
movaps %xmm1, %xmm0
jbe 0x70a57
callq 0x6850
jmp 0x70a5c
callq 0x63d0
movaps %xmm0, 0x10(%rsp)
xorps %xmm1, %xmm1
movss 0x8(%rsp), %xmm0
ucomiss %xmm1, %xmm0
jbe 0x70a76
callq 0x6850
jmp 0x70a7b
callq 0x63d0
movaps 0x20(%rsp), %xmm1
unpcklps 0x10(%rsp), %xmm1       # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
mulps 0xce44(%rip), %xmm1     # 0x7d8d0
mulss 0xcea0(%rip), %xmm0     # 0x7d934
movlps %xmm1, 0x2c(%rbx)
movss %xmm0, 0x34(%rbx)
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
nop