// File: sundale[P]gcode/rs274ngc/interp_find.cc
// Function: Interp::unwrap_rotary(double*, double, double, double, char)
int Interp::unwrap_rotary(double *r, double sign_of, double commanded, double current, char axis) {
    double result;
    int neg = copysign(1.0, sign_of) < 0.0;
    CHKS((sign_of <= -360.0 || sign_of >= 360.0), (_("Invalid absolute position %5.2f for wrapped rotary axis %c")), sign_of, axis);
    
    double d = floor(current/360.0);
    result = fabs(commanded) + (d*360.0);
    if(!neg && result < current) result += 360.0;
    if(neg && result > current) result -= 360.0;
    *r = result;

    return INTERP_OK;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movb %dl, %al
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movsd %xmm0, -0x20(%rbp)
movsd %xmm1, -0x28(%rbp)
movsd %xmm2, -0x30(%rbp)
movb %al, -0x31(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x58(%rbp)
movsd -0x20(%rbp), %xmm1
movaps 0x25cc8(%rip), %xmm0    # 0x7ca70
pand %xmm0, %xmm1
movaps 0x25ccd(%rip), %xmm0    # 0x7ca80
por %xmm0, %xmm1
xorps %xmm0, %xmm0
ucomisd %xmm1, %xmm0
seta %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x44(%rbp)
movsd 0x2205f(%rip), %xmm0    # 0x78e30
ucomisd -0x20(%rbp), %xmm0
jae 0x56def
movsd -0x20(%rbp), %xmm0
movsd 0x22053(%rip), %xmm1    # 0x78e38
ucomisd %xmm1, %xmm0
jb 0x56ecd
jmp 0x56df1
leaq 0x22c89(%rip), %rdi     # 0x79a81
callq 0x10380
movq -0x58(%rbp), %rdi
movq %rax, %rsi
movsd -0x20(%rbp), %xmm0
movsbl -0x31(%rbp), %edx
movb $0x1, %al
callq 0x10df0
movq 0x30145(%rip), %rax     # 0x86f60
movl $0x0, 0xd670(%rax)
movq 0x30134(%rip), %rax     # 0x86f60
movslq 0xd670(%rax), %rax
movq 0x30126(%rip), %rdi     # 0x86f60
addq $0xc6d0, %rdi           # imm = 0xC6D0
imulq $0x50, %rax, %rax
addq %rax, %rdi
leaq 0x25c49(%rip), %rsi     # 0x7ca98
movl $0x50, %edx
callq 0x10840
movq 0x30100(%rip), %rax     # 0x86f60
movslq 0xd670(%rax), %rcx
movq 0x300f2(%rip), %rax     # 0x86f60
addq $0xc6d0, %rax           # imm = 0xC6D0
imulq $0x50, %rcx, %rcx
addq %rcx, %rax
movb $0x0, 0x4f(%rax)
movq 0x300da(%rip), %rax     # 0x86f60
movl 0xd670(%rax), %ecx
addl $0x1, %ecx
movq 0x300ca(%rip), %rax     # 0x86f60
movl %ecx, 0xd670(%rax)
movq 0x300bd(%rip), %rax     # 0x86f60
movslq 0xd670(%rax), %rcx
movq 0x300af(%rip), %rax     # 0x86f60
addq $0xc6d0, %rax           # imm = 0xC6D0
imulq $0x50, %rcx, %rcx
addq %rcx, %rax
movb $0x0, (%rax)
movl $0x5, -0x4(%rbp)
jmp 0x56f78
jmp 0x56ecf
movsd -0x30(%rbp), %xmm0
movsd 0x21f5c(%rip), %xmm1    # 0x78e38
divsd %xmm1, %xmm0
callq 0x11e50
movsd %xmm0, -0x50(%rbp)
movsd -0x28(%rbp), %xmm1
movaps 0x2012a(%rip), %xmm0    # 0x77020
pand %xmm0, %xmm1
movsd -0x50(%rbp), %xmm0
movsd 0x21f31(%rip), %xmm2    # 0x78e38
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x40(%rbp)
cmpl $0x0, -0x44(%rbp)
jne 0x56f3c
movsd -0x40(%rbp), %xmm1
movsd -0x30(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x56f3c
movsd 0x21f06(%rip), %xmm0    # 0x78e38
addsd -0x40(%rbp), %xmm0
movsd %xmm0, -0x40(%rbp)
cmpl $0x0, -0x44(%rbp)
je 0x56f64
movsd -0x40(%rbp), %xmm0
ucomisd -0x30(%rbp), %xmm0
jbe 0x56f64
movsd -0x40(%rbp), %xmm0
movsd 0x21edd(%rip), %xmm1    # 0x78e38
subsd %xmm1, %xmm0
movsd %xmm0, -0x40(%rbp)
movsd -0x40(%rbp), %xmm0
movq -0x18(%rbp), %rax
movsd %xmm0, (%rax)
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)