// File: lucaong[P]elixir_mlx90640/src/MLX90640_API.cpp
// Function: ExtractCPParameters(unsigned short*, paramsMLX90640*)
void ExtractCPParameters(uint16_t *eeData, paramsMLX90640 *mlx90640)
{
    float alphaSP[2];
    int16_t offsetSP[2];
    float cpKv;
    float cpKta;
    uint8_t alphaScale;
    uint8_t ktaScale1;
    uint8_t kvScale;

    alphaScale = ((eeData[32] & 0xF000) >> 12) + 27;
    
    offsetSP[0] = (eeData[58] & 0x03FF);
    if (offsetSP[0] > 511)
    {
        offsetSP[0] = offsetSP[0] - 1024;
    }
    
    offsetSP[1] = (eeData[58] & 0xFC00) >> 10;
    if (offsetSP[1] > 31)
    {
        offsetSP[1] = offsetSP[1] - 64;
    }
    offsetSP[1] = offsetSP[1] + offsetSP[0]; 
    
    alphaSP[0] = (eeData[57] & 0x03FF);
    if (alphaSP[0] > 511)
    {
        alphaSP[0] = alphaSP[0] - 1024;
    }
    alphaSP[0] = alphaSP[0] /  pow(2,(double)alphaScale);
    
    alphaSP[1] = (eeData[57] & 0xFC00) >> 10;
    if (alphaSP[1] > 31)
    {
        alphaSP[1] = alphaSP[1] - 64;
    }
    alphaSP[1] = (1 + alphaSP[1]/128) * alphaSP[0];
    
    cpKta = (eeData[59] & 0x00FF);
    if (cpKta > 127)
    {
        cpKta = cpKta - 256;
    }
    ktaScale1 = ((eeData[56] & 0x00F0) >> 4) + 8;    
    mlx90640->cpKta = cpKta / pow(2,(double)ktaScale1);
    
    cpKv = (eeData[59] & 0xFF00) >> 8;
    if (cpKv > 127)
    {
        cpKv = cpKv - 256;
    }
    kvScale = (eeData[56] & 0x0F00) >> 8;
    mlx90640->cpKv = cpKv / pow(2,(double)kvScale);
       
    mlx90640->cpAlpha[0] = alphaSP[0];
    mlx90640->cpAlpha[1] = alphaSP[1];
    mlx90640->cpOffset[0] = offsetSP[0];
    mlx90640->cpOffset[1] = offsetSP[1];  
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movzwl 0x40(%rax), %eax
andl $0xf000, %eax           # imm = 0xF000
sarl $0xc, %eax
addl $0x1b, %eax
movb %al, -0x25(%rbp)
movq -0x8(%rbp), %rax
movzwl 0x74(%rax), %eax
andl $0x3ff, %eax            # imm = 0x3FF
movw %ax, -0x1c(%rbp)
movswl -0x1c(%rbp), %eax
cmpl $0x1ff, %eax            # imm = 0x1FF
jle 0x33bf
movswl -0x1c(%rbp), %eax
subl $0x400, %eax            # imm = 0x400
movw %ax, -0x1c(%rbp)
movq -0x8(%rbp), %rax
movzwl 0x74(%rax), %eax
andl $0xfc00, %eax           # imm = 0xFC00
sarl $0xa, %eax
movw %ax, -0x1a(%rbp)
movswl -0x1a(%rbp), %eax
cmpl $0x1f, %eax
jle 0x33e7
movswl -0x1a(%rbp), %eax
subl $0x40, %eax
movw %ax, -0x1a(%rbp)
movswl -0x1a(%rbp), %eax
movswl -0x1c(%rbp), %ecx
addl %ecx, %eax
movw %ax, -0x1a(%rbp)
movq -0x8(%rbp), %rax
movzwl 0x72(%rax), %eax
andl $0x3ff, %eax            # imm = 0x3FF
cvtsi2ss %eax, %xmm0
movss %xmm0, -0x18(%rbp)
movss -0x18(%rbp), %xmm0
movss 0x2c30(%rip), %xmm1     # 0x6048
ucomiss %xmm1, %xmm0
jbe 0x3433
movss -0x18(%rbp), %xmm0
movss 0x2c26(%rip), %xmm1     # 0x6050
subss %xmm1, %xmm0
movss %xmm0, -0x18(%rbp)
movss -0x18(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, -0x30(%rbp)
movzbl -0x25(%rbp), %eax
cvtsi2sd %eax, %xmm0
movl $0x2, %edi
callq 0x5ab0
movaps %xmm0, %xmm1
movsd -0x30(%rbp), %xmm0
divsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x18(%rbp)
movq -0x8(%rbp), %rax
movzwl 0x72(%rax), %eax
andl $0xfc00, %eax           # imm = 0xFC00
sarl $0xa, %eax
cvtsi2ss %eax, %xmm0
movss %xmm0, -0x14(%rbp)
movss -0x14(%rbp), %xmm0
movss 0x2bb2(%rip), %xmm1     # 0x6040
ucomiss %xmm1, %xmm0
jbe 0x34a9
movss -0x14(%rbp), %xmm0
movss 0x2ba4(%rip), %xmm1     # 0x6044
subss %xmm1, %xmm0
movss %xmm0, -0x14(%rbp)
movss -0x14(%rbp), %xmm1
movss 0x2bb6(%rip), %xmm0     # 0x606c
divss %xmm0, %xmm1
movss 0x2b4e(%rip), %xmm0     # 0x6010
addss %xmm1, %xmm0
mulss -0x18(%rbp), %xmm0
movss %xmm0, -0x14(%rbp)
movq -0x8(%rbp), %rax
movzwl 0x76(%rax), %eax
andl $0xff, %eax
cvtsi2ss %eax, %xmm0
movss %xmm0, -0x24(%rbp)
movss -0x24(%rbp), %xmm0
movss 0x2b65(%rip), %xmm1     # 0x6058
ucomiss %xmm1, %xmm0
jbe 0x350e
movss -0x24(%rbp), %xmm0
movss 0x2b57(%rip), %xmm1     # 0x605c
subss %xmm1, %xmm0
movss %xmm0, -0x24(%rbp)
movq -0x8(%rbp), %rax
movzbl 0x70(%rax), %eax
shrl $0x4, %eax
addb $0x8, %al
movb %al, -0x26(%rbp)
movss -0x24(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, -0x38(%rbp)
movzbl -0x26(%rbp), %eax
cvtsi2sd %eax, %xmm0
movl $0x2, %edi
callq 0x5ab0
movaps %xmm0, %xmm1
movsd -0x38(%rbp), %xmm0
divsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movq -0x10(%rbp), %rax
movss %xmm0, 0x20(%rax)
movq -0x8(%rbp), %rax
movzwl 0x76(%rax), %eax
andl $0xff00, %eax           # imm = 0xFF00
sarl $0x8, %eax
cvtsi2ss %eax, %xmm0
movss %xmm0, -0x20(%rbp)
movss -0x20(%rbp), %xmm0
movss 0x2adb(%rip), %xmm1     # 0x6058
ucomiss %xmm1, %xmm0
jbe 0x3598
movss -0x20(%rbp), %xmm0
movss 0x2acd(%rip), %xmm1     # 0x605c
subss %xmm1, %xmm0
movss %xmm0, -0x20(%rbp)
movq -0x8(%rbp), %rax
movzbl 0x71(%rax), %eax
andl $0xf, %eax
movb %al, -0x27(%rbp)
movss -0x20(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, -0x40(%rbp)
movzbl -0x27(%rbp), %eax
cvtsi2sd %eax, %xmm0
movl $0x2, %edi
callq 0x5ab0
movaps %xmm0, %xmm1
movsd -0x40(%rbp), %xmm0
divsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movq -0x10(%rbp), %rax
movss %xmm0, 0x1c(%rax)
movss -0x18(%rbp), %xmm0
movq -0x10(%rbp), %rax
movss %xmm0, 0x2a44(%rax)
movss -0x14(%rbp), %xmm0
movq -0x10(%rbp), %rax
movss %xmm0, 0x2a48(%rax)
movw -0x1c(%rbp), %cx
movq -0x10(%rbp), %rax
movw %cx, 0x2a4c(%rax)
movw -0x1a(%rbp), %cx
movq -0x10(%rbp), %rax
movw %cx, 0x2a4e(%rax)
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)