// File: JRLamontagne[P]cdice_sa/CDICE_doeclim.cpp
// Function: apply_calibration(DICE*, double)
void apply_calibration(DICE *dice, double this_cs)
{
	// Loop iterator
	int i;
	
	// Setup variables to find the bounds around a particular cs
	int ihigh = 0;
	int ilow = 0;
	double diffh = 999.9;
	double diffl = -999.9;
	double this_diff = 0.0;
	double cs_range = 0.0;
	double cs_diff = 0.0;
		
	// Loop through the calibration data
	for(i=0; i<dice->config.n_calpoints; i++) {

		// Find the difference between this_cs and the calibration point
		this_diff = dice->clim.cs_cal[i] - this_cs;
		
		// If this is an exact match, set the kv and alpha values and return
		if(this_diff == 0.0) {
			ilow = i;
			ihigh = i;
			break;
		}
		
		// Otherwise, test this index as a bound
		if(this_diff >= 0.0 && this_diff < diffh) {
			ihigh = i;
			diffh = this_diff;
			}
		else if(this_diff <= 0.0 && this_diff > diffl) {
			ilow = i;
			diffl = this_diff;
		}	
	}
	
	// Now that we have the bounds, calculate the kv and alpha values
	// through a simple linear-interpolation between the bound points
	if(ilow == ihigh) {
		dice->clim.kappa = dice->clim.kv_cal[ilow];
		dice->clim.alpha = dice->clim.alpha_cal[ilow];
	}
	else {
		cs_range = dice->clim.cs_cal[ihigh] - dice->clim.cs_cal[ilow];
		cs_diff = this_cs - dice->clim.cs_cal[ilow];
		dice->clim.kappa = ((cs_diff/cs_range) * (dice->clim.kv_cal[ihigh] - dice->clim.kv_cal[ilow])) + dice->clim.kv_cal[ilow];
		dice->clim.alpha = ((cs_diff/cs_range) * (dice->clim.alpha_cal[ihigh] - dice->clim.alpha_cal[ilow])) + dice->clim.alpha_cal[ilow];
	}

	return;
}

// Assembly:
movq %rdi, -0x8(%rsp)
movsd %xmm0, -0x10(%rsp)
movl $0x0, -0x18(%rsp)
movl $0x0, -0x1c(%rsp)
movsd 0x10b5(%rip), %xmm0     # 0x1c2d8
movsd %xmm0, -0x28(%rsp)
movsd 0x109f(%rip), %xmm0     # 0x1c2d0
movsd %xmm0, -0x30(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x38(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x40(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x48(%rsp)
movl $0x0, -0x14(%rsp)
movl -0x14(%rsp), %eax
movq -0x8(%rsp), %rcx
cmpl 0x30(%rcx), %eax
jge 0x1b329
movq -0x8(%rsp), %rax
movq 0x178(%rax), %rax
movslq -0x14(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
subsd -0x10(%rsp), %xmm0
movsd %xmm0, -0x38(%rsp)
movsd -0x38(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x1b2b1
jp 0x1b2b1
movl -0x14(%rsp), %eax
movl %eax, -0x1c(%rsp)
movl -0x14(%rsp), %eax
movl %eax, -0x18(%rsp)
jmp 0x1b329
movsd -0x38(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x1b2e8
movsd -0x38(%rsp), %xmm1
movsd -0x28(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x1b2e8
movl -0x14(%rsp), %eax
movl %eax, -0x18(%rsp)
movsd -0x38(%rsp), %xmm0
movsd %xmm0, -0x28(%rsp)
jmp 0x1b317
xorps %xmm0, %xmm0
ucomisd -0x38(%rsp), %xmm0
jb 0x1b315
movsd -0x38(%rsp), %xmm0
ucomisd -0x30(%rsp), %xmm0
jbe 0x1b315
movl -0x14(%rsp), %eax
movl %eax, -0x1c(%rsp)
movsd -0x38(%rsp), %xmm0
movsd %xmm0, -0x30(%rsp)
jmp 0x1b317
jmp 0x1b319
movl -0x14(%rsp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rsp)
jmp 0x1b25a
movl -0x1c(%rsp), %eax
cmpl -0x18(%rsp), %eax
jne 0x1b37e
movq -0x8(%rsp), %rax
movq 0x180(%rax), %rax
movslq -0x1c(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x8(%rsp), %rax
movsd %xmm0, 0xa0(%rax)
movq -0x8(%rsp), %rax
movq 0x188(%rax), %rax
movslq -0x1c(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x8(%rsp), %rax
movsd %xmm0, 0xa8(%rax)
jmp 0x1b498
movq -0x8(%rsp), %rax
movq 0x178(%rax), %rax
movslq -0x18(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x8(%rsp), %rax
movq 0x178(%rax), %rax
movslq -0x1c(%rsp), %rcx
subsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x40(%rsp)
movsd -0x10(%rsp), %xmm0
movq -0x8(%rsp), %rax
movq 0x178(%rax), %rax
movslq -0x1c(%rsp), %rcx
subsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x48(%rsp)
movsd -0x48(%rsp), %xmm0
divsd -0x40(%rsp), %xmm0
movq -0x8(%rsp), %rax
movq 0x180(%rax), %rax
movslq -0x18(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm2
movq -0x8(%rsp), %rax
movq 0x180(%rax), %rax
movslq -0x1c(%rsp), %rcx
subsd (%rax,%rcx,8), %xmm2
movq -0x8(%rsp), %rax
movq 0x180(%rax), %rax
movslq -0x1c(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movq -0x8(%rsp), %rax
movsd %xmm0, 0xa0(%rax)
movsd -0x48(%rsp), %xmm0
divsd -0x40(%rsp), %xmm0
movq -0x8(%rsp), %rax
movq 0x188(%rax), %rax
movslq -0x18(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm2
movq -0x8(%rsp), %rax
movq 0x188(%rax), %rax
movslq -0x1c(%rsp), %rcx
subsd (%rax,%rcx,8), %xmm2
movq -0x8(%rsp), %rax
movq 0x188(%rax), %rax
movslq -0x1c(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movq -0x8(%rsp), %rax
movsd %xmm0, 0xa8(%rax)
retq
nopl (%rax)