// File: /workspace/github_c_makefile_star2_O2/kilobyte[P]colormatch/color_distance.c
// Function: deltaE2000
double deltaE2000(double *lab1, double *lab2)
{
    double Lstd = *lab1;
    double astd = *(lab1+1);
    double bstd = *(lab1+2);

    double Lsample = *lab2;
    double asample = *(lab2+1);
    double bsample = *(lab2+2);

    double Cabstd= sqrt(astd*astd+bstd*bstd);
    double Cabsample= sqrt(asample*asample+bsample*bsample);

    double Cabarithmean= (Cabstd + Cabsample)/2.0;

    double G= 0.5*( 1.0 - sqrt( pow(Cabarithmean,7.0)/(pow(Cabarithmean,7.0) + pow(25.0,7.0))));

    double apstd= (1.0+G)*astd; // aprime in paper
    double apsample= (1.0+G)*asample; // aprime in paper
    double Cpsample= sqrt(apsample*apsample+bsample*bsample);

    double Cpstd= sqrt(apstd*apstd+bstd*bstd);
    // Compute product of chromas
    double Cpprod= (Cpsample*Cpstd);


    // Ensure hue is between 0 and 2pi
    double hpstd= atan2(bstd,apstd);
    if (hpstd<0) hpstd+= 2.0*pi;  // rollover ones that come -ve

    double hpsample= atan2(bsample,apsample);
    if (hpsample<0) hpsample+= 2.0*pi;
    if ( (fabs(apsample)+fabs(bsample))==0.0)  hpsample= 0.0;

    double dL= (Lsample-Lstd);
    double dC= (Cpsample-Cpstd);

    // Computation of hue difference
    double dhp= (hpsample-hpstd);
    if (dhp>pi)  dhp-= 2.0*pi;
    if (dhp<-pi) dhp+= 2.0*pi;
    // set chroma difference to zero if the product of chromas is zero
    if (Cpprod == 0.0) dhp= 0.0;

    // Note that the defining equations actually need
    // signed Hue and chroma differences which is different
    // from prior color difference formulae

    double dH= 2.0*sqrt(Cpprod)*sin(dhp/2.0);
    //%dH2 = 4*Cpprod.*(sin(dhp/2)).^2;

    // weighting functions
    double Lp= (Lsample+Lstd)/2.0;
    double Cp= (Cpstd+Cpsample)/2.0;

    // Average Hue Computation
    // This is equivalent to that in the paper but simpler programmatically.
    // Note average hue is computed in radians and converted to degrees only
    // where needed
    double hp= (hpstd+hpsample)/2.0;
    // Identify positions for which abs hue diff exceeds 180 degrees
    if ( fabs(hpstd-hpsample)  > pi ) hp-= pi;
    // rollover ones that come -ve
    if (hp<0) hp+= 2.0*pi;

    // Check if one of the chroma values is zero, in which case set
    // mean hue to the sum which is equivalent to other value
    if (Cpprod==0.0) hp= hpsample+hpstd;

    double Lpm502= (Lp-50.0)*(Lp-50.0);;
    double Sl= 1.0+0.015*Lpm502/sqrt(20.0+Lpm502);
    double Sc= 1.0+0.045*Cp;
    double T= 1.0 - 0.17*cos(hp - pi/6.0) + 0.24*cos(2.0*hp) + 0.32*cos(3.0*hp+pi/30.0) - 0.20*cos(4.0*hp-63.0*pi/180.0);
    double Sh= 1.0 + 0.015*Cp*T;
    double delthetarad= (30.0*pi/180.0)*exp(- pow(( (180.0/pi*hp-275.0)/25.0),2.0));
    double Rc=  2.0*sqrt(pow(Cp,7.0)/(pow(Cp,7.0) + pow(25.0,7.0)));
    double RT= -sin(2.0*delthetarad)*Rc;

    // The CIE 00 color difference
    return sqrt( pow((dL/Sl),2.0) + pow((dC/Sc),2.0) + pow((dH/Sh),2.0) + RT*(dC/Sc)*(dH/Sh) );
}

// Assembly:
subq $0xb8, %rsp
movsd (%rdi), %xmm0
movsd %xmm0, 0x78(%rsp)
movsd 0x10(%rdi), %xmm1
movsd (%rsi), %xmm0
movaps %xmm0, 0xa0(%rsp)
movsd 0x10(%rsi), %xmm0
movapd %xmm0, 0x10(%rsp)
movapd %xmm0, %xmm2
movapd %xmm1, 0x20(%rsp)
unpcklpd %xmm1, %xmm2            # xmm2 = xmm2[0],xmm1[0]
mulpd %xmm2, %xmm2
movsd 0x8(%rsi), %xmm0
movhpd 0x8(%rdi), %xmm0        # xmm0 = xmm0[0],mem[0]
movapd %xmm0, 0x30(%rsp)
mulpd %xmm0, %xmm0
movapd %xmm2, 0x60(%rsp)
addpd %xmm2, %xmm0
sqrtpd %xmm0, %xmm1
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
mulsd 0xc73(%rip), %xmm0      # 0x20b0
movapd %xmm0, 0x50(%rsp)
movsd 0xc6d(%rip), %xmm1      # 0x20b8
callq 0x1040
movsd %xmm0, 0x40(%rsp)
movapd 0x50(%rsp), %xmm0
movsd 0xc54(%rip), %xmm1      # 0x20b8
callq 0x1040
addsd 0xc4f(%rip), %xmm0      # 0x20c0
movsd 0x40(%rsp), %xmm1
divsd %xmm0, %xmm1
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
movapd %xmm1, %xmm0
jb 0x148f
sqrtsd %xmm0, %xmm0
jmp 0x1494
callq 0x10f0
movsd 0xc2c(%rip), %xmm2      # 0x20c8
movapd %xmm2, %xmm1
subsd %xmm0, %xmm1
mulsd 0xc04(%rip), %xmm1      # 0x20b0
addsd %xmm2, %xmm1
unpcklpd %xmm1, %xmm1            # xmm1 = xmm1[0,0]
movapd 0x30(%rsp), %xmm2
mulpd %xmm1, %xmm2
movapd %xmm2, %xmm0
movapd %xmm2, 0x30(%rsp)
mulpd %xmm2, %xmm0
addpd 0x60(%rsp), %xmm0
sqrtpd %xmm0, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
movapd %xmm0, 0x60(%rsp)
movapd %xmm1, 0x50(%rsp)
mulsd %xmm1, %xmm0
movapd %xmm0, 0x40(%rsp)
movapd %xmm2, %xmm1
unpckhpd %xmm2, %xmm1            # xmm1 = xmm1[1],xmm2[1]
movapd 0x20(%rsp), %xmm0
callq 0x1090
movsd 0xbc1(%rip), %xmm2      # 0x20d0
movapd %xmm0, %xmm1
addsd %xmm2, %xmm1
movapd %xmm0, %xmm3
xorpd %xmm2, %xmm2
cmpltsd %xmm2, %xmm3
andpd %xmm3, %xmm1
andnpd %xmm0, %xmm3
orpd %xmm1, %xmm3
movapd %xmm3, 0x20(%rsp)
movapd 0x10(%rsp), %xmm0
movaps 0x30(%rsp), %xmm1
callq 0x1090
movapd %xmm0, %xmm1
movsd 0xb7e(%rip), %xmm4      # 0x20d0
addsd %xmm4, %xmm1
movapd %xmm0, %xmm2
xorpd %xmm3, %xmm3
cmpltsd %xmm3, %xmm2
andpd %xmm2, %xmm1
andnpd %xmm0, %xmm2
orpd %xmm1, %xmm2
movapd 0xcc9(%rip), %xmm0      # 0x2240
movapd 0x30(%rsp), %xmm1
andpd %xmm0, %xmm1
movapd 0x10(%rsp), %xmm5
andpd %xmm0, %xmm5
addsd %xmm1, %xmm5
cmpeqsd %xmm3, %xmm5
andnpd %xmm2, %xmm5
movapd %xmm5, 0x10(%rsp)
movapd %xmm5, %xmm0
subsd 0x20(%rsp), %xmm0
movsd 0xb28(%rip), %xmm1      # 0x20d8
addsd %xmm0, %xmm1
movsd 0xb24(%rip), %xmm6      # 0x20e0
movapd %xmm6, %xmm2
cmpltsd %xmm0, %xmm2
andpd %xmm2, %xmm1
andnpd %xmm0, %xmm2
orpd %xmm1, %xmm2
movapd 0x40(%rsp), %xmm1
movapd %xmm4, %xmm0
addsd %xmm2, %xmm0
movapd %xmm2, %xmm5
cmpltsd 0xafc(%rip), %xmm5      # 0x20e8
andpd %xmm5, %xmm0
andnpd %xmm2, %xmm5
orpd %xmm0, %xmm5
ucomisd %xmm3, %xmm1
jb 0x1604
sqrtsd %xmm1, %xmm0
jmp 0x162d
movapd %xmm1, %xmm0
movapd %xmm5, 0x80(%rsp)
callq 0x10f0
movsd 0xac2(%rip), %xmm6      # 0x20e0
movapd 0x80(%rsp), %xmm5
movapd 0x40(%rsp), %xmm1
movapd %xmm0, 0x30(%rsp)
mulsd 0xa75(%rip), %xmm5      # 0x20b0
xorpd %xmm3, %xmm3
ucomisd %xmm3, %xmm1
jne 0x164b
jp 0x164b
xorpd %xmm5, %xmm5
movapd 0x20(%rsp), %xmm4
movapd %xmm4, %xmm0
movapd 0x10(%rsp), %xmm2
addsd %xmm2, %xmm0
movapd %xmm0, %xmm1
mulsd 0xa45(%rip), %xmm1      # 0x20b0
subsd %xmm2, %xmm4
andpd 0xbc9(%rip), %xmm4      # 0x2240
movsd 0xa69(%rip), %xmm2      # 0x20e8
addsd %xmm1, %xmm2
cmpltsd %xmm4, %xmm6
andpd %xmm6, %xmm2
andnpd %xmm1, %xmm6
orpd %xmm2, %xmm6
movsd 0xa34(%rip), %xmm1      # 0x20d0
addsd %xmm6, %xmm1
movapd %xmm6, %xmm2
cmpltsd %xmm3, %xmm2
andpd %xmm2, %xmm1
andnpd %xmm6, %xmm2
orpd %xmm1, %xmm2
jne 0x16bd
jp 0x16bd
movapd %xmm0, %xmm2
movapd %xmm2, 0x10(%rsp)
movapd %xmm5, %xmm0
callq 0x10c0
movapd %xmm0, 0x40(%rsp)
movapd 0x50(%rsp), %xmm3
movapd %xmm3, %xmm4
movapd 0x60(%rsp), %xmm1
addsd %xmm1, %xmm4
movsd 0x9c2(%rip), %xmm2      # 0x20b0
mulsd %xmm2, %xmm4
movsd %xmm4, 0x20(%rsp)
movsd 0x78(%rsp), %xmm0
addsd 0xa0(%rsp), %xmm0
mulsd %xmm2, %xmm0
movapd 0x30(%rsp), %xmm2
addsd %xmm2, %xmm2
movapd %xmm2, 0x30(%rsp)
subsd %xmm3, %xmm1
movapd %xmm1, 0x60(%rsp)
addsd 0x9c3(%rip), %xmm0      # 0x20f0
mulsd %xmm0, %xmm0
movsd 0x9bf(%rip), %xmm2      # 0x20f8
movapd %xmm0, %xmm1
mulsd %xmm2, %xmm1
addsd 0x9b7(%rip), %xmm0      # 0x2100
sqrtsd %xmm0, %xmm0
divsd %xmm0, %xmm1
movsd 0x96f(%rip), %xmm2      # 0x20c8
addsd %xmm2, %xmm1
movapd %xmm1, 0x50(%rsp)
movsd 0x99d(%rip), %xmm0      # 0x2108
mulsd %xmm4, %xmm0
addsd %xmm2, %xmm0
movsd %xmm0, 0x80(%rsp)
movsd 0x98c(%rip), %xmm0      # 0x2110
addsd 0x10(%rsp), %xmm0
callq 0x1070
mulsd 0x981(%rip), %xmm0      # 0x2118
addsd 0x929(%rip), %xmm0      # 0x20c8
movsd %xmm0, 0x8(%rsp)
movapd 0x10(%rsp), %xmm0
addsd %xmm0, %xmm0
callq 0x1070
mulsd 0x964(%rip), %xmm0      # 0x2120
addsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
movsd 0x958(%rip), %xmm0      # 0x2128
mulsd 0x10(%rsp), %xmm0
addsd 0x952(%rip), %xmm0      # 0x2130
callq 0x1070
mulsd 0x94d(%rip), %xmm0      # 0x2138
addsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
movsd 0x941(%rip), %xmm0      # 0x2140
mulsd 0x10(%rsp), %xmm0
addsd 0x93b(%rip), %xmm0      # 0x2148
callq 0x1070
mulsd 0x936(%rip), %xmm0      # 0x2150
addsd 0x8(%rsp), %xmm0
movsd 0x8d0(%rip), %xmm1      # 0x20f8
mulsd 0x20(%rsp), %xmm1
mulsd %xmm0, %xmm1
movapd %xmm1, %xmm0
movapd 0x10(%rsp), %xmm1
mulsd 0x914(%rip), %xmm1      # 0x2158
addsd 0x914(%rip), %xmm1      # 0x2160
divsd 0x914(%rip), %xmm1      # 0x2168
addsd 0x86c(%rip), %xmm0      # 0x20c8
movapd %xmm0, 0x10(%rsp)
movapd 0x9e6(%rip), %xmm0      # 0x2250
xorpd %xmm1, %xmm0
mulsd %xmm1, %xmm0
callq 0x1100
mulsd 0x8f1(%rip), %xmm0      # 0x2170
movsd %xmm0, 0x8(%rsp)
movsd 0x82b(%rip), %xmm1      # 0x20b8
movsd 0x20(%rsp), %xmm0
callq 0x1040
movsd %xmm0, 0x98(%rsp)
movsd 0x20(%rsp), %xmm0
movsd 0x809(%rip), %xmm1      # 0x20b8
callq 0x1040
addsd 0x804(%rip), %xmm0      # 0x20c0
movsd 0x98(%rsp), %xmm1
divsd %xmm0, %xmm1
movapd %xmm1, %xmm0
ucomisd 0x8a3(%rip), %xmm1      # 0x2178
jb 0x18dd
sqrtsd %xmm0, %xmm0
jmp 0x18e2
callq 0x10f0
addsd %xmm0, %xmm0
movsd %xmm0, 0x20(%rsp)
movsd 0x8(%rsp), %xmm0
addsd %xmm0, %xmm0
callq 0x10c0
movapd %xmm0, %xmm1
mulsd 0x20(%rsp), %xmm1
movapd 0x60(%rsp), %xmm5
divsd 0x80(%rsp), %xmm5
movapd %xmm5, %xmm2
mulsd %xmm5, %xmm2
unpcklpd 0x30(%rsp), %xmm0       # xmm0 = xmm0[0],mem[0]
unpcklpd 0x40(%rsp), %xmm3       # xmm3 = xmm3[0],mem[0]
mulpd %xmm0, %xmm3
movapd 0xa0(%rsp), %xmm4
subsd 0x78(%rsp), %xmm4
shufpd $0x2, %xmm3, %xmm4      # xmm4 = xmm4[0],xmm3[1]
movapd 0x50(%rsp), %xmm0
unpcklpd 0x10(%rsp), %xmm0       # xmm0 = xmm0[0],mem[0]
divpd %xmm0, %xmm4
movapd %xmm4, %xmm0
mulpd %xmm4, %xmm0
addsd %xmm0, %xmm2
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm2, %xmm0
mulsd %xmm1, %xmm5
unpckhpd %xmm4, %xmm4            # xmm4 = xmm4[1,1]
mulsd %xmm5, %xmm4
subsd %xmm4, %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x198a
sqrtsd %xmm0, %xmm0
addq $0xb8, %rsp
retq
addq $0xb8, %rsp
jmp 0x10f0