// File: /workspace/github_cpp_makefile_star2_O3/sundale[P]gcode/rs274ngc/interp_convert.cc
// Function: Interp::convert_threading_cycle(block_struct*, setup_struct*, double, double, double)
int Interp::convert_threading_cycle(block_pointer block, 
				    setup_pointer settings,
				    double end_x, double end_y, double end_z) {


    CHKS((settings->cutter_comp_side != OFF),
         (_("Cannot use G76 threading cycle with cutter radius compensation on")));

    CHKS((block->i_number == 0),
        (_("In G76, I must not be 0")));
    CHKS((block->j_number <= 0),
        (_("In G76, J must be greater than 0")));
    CHKS((block->k_number <= block->j_number),
        (_("In G76, K must be greater than J")));

    double start_x = settings->current_x;
    double start_y = settings->current_y;
    double start_z = settings->current_z;

    double i_number = block->i_number;
    double j_number = block->j_number;
    double k_number = block->k_number;

    if(_setup.lathe_diameter_mode){
      i_number /= 2;
      j_number /= 2;
      k_number /= 2;
    }


    int boring = 0;

    if (i_number > 0.0)
	boring = 1;

    double safe_x = start_x;
    double full_dia_depth = fabs(i_number);
    double start_depth = fabs(i_number) + fabs(j_number);
    double cut_increment = fabs(j_number);
    double full_threadheight = fabs(k_number);
    double end_depth = fabs(k_number) + fabs(i_number);

    double pitch = block->p_number;
    double compound_angle = block->q_number;
    if(compound_angle == -1) compound_angle = 0;
    compound_angle *= M_PIl/180.0;
    if(end_z > start_z) compound_angle = -compound_angle;

    int spring_cuts = block->h_flag == ON ? block->h_number: 0;

    double degression = block->r_number;
    if(degression < 1.0 || !block->r_flag) degression = 1.0;

    double taper_dist = block->e_flag? block->e_number: 0.0;
    if(taper_dist < 0.0) taper_dist = 0.0;
    double taper_pitch = taper_dist > 0.0? 
	pitch * hypot(taper_dist, full_threadheight)/taper_dist: pitch;

    if(end_z > start_z) taper_dist = -taper_dist;

    int taper_flags = block->l_number;
    if(taper_flags < 0) taper_flags = 0;

    int entry_taper = taper_flags & 1;
    int exit_taper = taper_flags & 2;

    double depth, zoff;
    int pass = 1;

    double target_z = end_z + fabs(k_number) * tan(compound_angle);

    depth = start_depth;
    zoff = (depth - full_dia_depth) * tan(compound_angle);
    while (depth < end_depth) {
	threading_pass(settings, block, boring, safe_x, depth, end_depth, start_y, 
		       start_z, zoff, taper_dist, entry_taper, exit_taper, 
		       taper_pitch, pitch, full_threadheight, target_z);
        depth = full_dia_depth + cut_increment * pow(++pass, 1.0/degression);
        zoff = (depth - full_dia_depth) * tan(compound_angle);
    } 
    // full specified depth now
    depth = end_depth;
    zoff = (depth - full_dia_depth) * tan(compound_angle);
    // cut at least once -- more if spring cuts.
    for(int i = 0; i<spring_cuts+1; i++) {
	threading_pass(settings, block, boring, safe_x, depth, end_depth, start_y, 
		       start_z, zoff, taper_dist, entry_taper, exit_taper, 
		       taper_pitch, pitch, full_threadheight, target_z);
    } 
    STRAIGHT_TRAVERSE(block->line_number, end_x, end_y, end_z, AABBCC);
    settings->current_x = end_x;
    settings->current_y = end_y;
    settings->current_z = end_z;
#undef AABBC
    return INTERP_OK;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x118, %rsp            # imm = 0x118
movq %rdi, %r15
cmpl $0x0, 0x624(%rdx)
je 0x182ea
leaq 0x1e0ac(%rip), %rdi     # 0x36391
jmp 0x187ea
movq %rsi, %r14
movsd 0x1a0(%rsi), %xmm6
xorpd %xmm3, %xmm3
ucomisd %xmm3, %xmm6
jne 0x1830d
jp 0x1830d
leaq 0x1e125(%rip), %rdi     # 0x3642d
jmp 0x187ea
movsd 0x1b0(%r14), %xmm10
ucomisd %xmm10, %xmm3
jae 0x187da
movsd 0x1c0(%r14), %xmm4
ucomisd %xmm4, %xmm10
jae 0x187e3
movq %rdx, %rbx
movsd 0x610(%rdx), %xmm5
movq 0x28c19(%rip), %rax     # 0x40f60
cmpb $0x0, 0x1e6d8(%rax)
movsd %xmm1, -0x80(%rbp)
movsd %xmm0, -0x78(%rbp)
je 0x1836f
movsd 0x19ca6(%rip), %xmm0    # 0x32008
mulsd %xmm0, %xmm6
mulsd %xmm0, %xmm10
mulsd %xmm0, %xmm4
movsd 0x600(%rbx), %xmm7
movsd 0x608(%rbx), %xmm8
xorpd %xmm0, %xmm0
xorl %r13d, %r13d
ucomisd %xmm0, %xmm6
seta %r12b
movapd 0x19c89(%rip), %xmm0    # 0x32020
movsd 0x210(%r14), %xmm9
movsd 0x220(%r14), %xmm1
movsd 0x1b4bf(%rip), %xmm3    # 0x33870
cmpeqsd %xmm1, %xmm3
andnpd %xmm1, %xmm3
movlpd %xmm3, -0x110(%rbp)
fldt 0x1bbf8(%rip)           # 0x33fc0
fmull -0x110(%rbp)
fstpl -0x108(%rbp)
movsd -0x108(%rbp), %xmm3
movapd 0x1bb2c(%rip), %xmm1    # 0x33f10
xorpd %xmm3, %xmm1
movapd %xmm5, %xmm11
cmpltsd %xmm2, %xmm11
movapd %xmm11, %xmm12
andnpd %xmm3, %xmm12
andpd %xmm11, %xmm1
movl $0x1, %r15d
cmpb $0x1, 0x190(%r14)
jne 0x1841c
movl 0x194(%r14), %r15d
incl %r15d
andpd %xmm0, %xmm4
orpd %xmm1, %xmm12
movsd 0x230(%r14), %xmm3
movsd 0x1ba6a(%rip), %xmm1    # 0x33ea0
ucomisd %xmm3, %xmm1
ja 0x18446
cmpb $0x0, 0x229(%r14)
jne 0x1844a
movapd %xmm1, %xmm3
movsd %xmm8, -0x98(%rbp)
movsd %xmm7, -0xa0(%rbp)
movsd %xmm5, -0xa8(%rbp)
andpd %xmm0, %xmm6
andpd %xmm0, %xmm10
xorpd %xmm0, %xmm0
cmpb $0x1, 0x139(%r14)
xorpd %xmm1, %xmm1
jne 0x18487
movsd 0x140(%r14), %xmm1
movsd %xmm3, -0x48(%rbp)
movsd %xmm2, -0x38(%rbp)
movapd %xmm6, %xmm2
movapd %xmm10, -0x120(%rbp)
addsd %xmm10, %xmm2
movapd %xmm2, -0x70(%rbp)
movapd %xmm4, %xmm2
movapd %xmm6, -0x100(%rbp)
addsd %xmm6, %xmm2
movapd %xmm2, -0x60(%rbp)
xorpd %xmm2, %xmm2
maxsd %xmm1, %xmm2
ucomisd %xmm0, %xmm2
movsd %xmm9, -0x50(%rbp)
movapd %xmm9, %xmm0
movapd %xmm4, -0xf0(%rbp)
movapd %xmm12, -0xc0(%rbp)
jbe 0x1852c
movapd %xmm2, %xmm0
movapd -0xf0(%rbp), %xmm1
movapd %xmm11, -0xe0(%rbp)
movapd %xmm2, -0xd0(%rbp)
callq 0xa290
movapd -0xd0(%rbp), %xmm2
movapd -0xc0(%rbp), %xmm12
movapd -0xe0(%rbp), %xmm11
mulsd -0x50(%rbp), %xmm0
divsd %xmm2, %xmm0
movsd %xmm0, -0x90(%rbp)
movb %r12b, %r13b
movl %r13d, -0x2c(%rbp)
movapd 0x1b9cd(%rip), %xmm0    # 0x33f10
xorpd %xmm2, %xmm0
andpd %xmm11, %xmm0
andnpd %xmm2, %xmm11
movl 0x1c8(%r14), %eax
xorl %r12d, %r12d
testl %eax, %eax
cmovgl %eax, %r12d
orpd %xmm0, %xmm11
movapd %xmm11, -0xe0(%rbp)
movl %r12d, %eax
andl $0x1, %eax
movl %eax, -0xd0(%rbp)
andl $0x2, %r12d
movapd %xmm12, %xmm0
callq 0xa580
mulsd -0xf0(%rbp), %xmm0
addsd -0x38(%rbp), %xmm0
movsd %xmm0, -0x88(%rbp)
movapd -0xc0(%rbp), %xmm0
callq 0xa580
movapd -0x60(%rbp), %xmm1
movapd -0x70(%rbp), %xmm2
ucomisd %xmm2, %xmm1
movsd 0x1b8df(%rip), %xmm1    # 0x33ea0
movsd %xmm1, -0x40(%rbp)
jbe 0x186c2
movapd %xmm2, %xmm5
subsd -0x100(%rbp), %xmm5
mulsd %xmm0, %xmm5
movsd -0x40(%rbp), %xmm0
divsd -0x48(%rbp), %xmm0
movsd %xmm0, -0x40(%rbp)
movl $0x2, %r13d
movsd -0x88(%rbp), %xmm0
movsd %xmm0, 0x10(%rsp)
movaps -0xf0(%rbp), %xmm0
movsd %xmm0, 0x8(%rsp)
movsd -0x50(%rbp), %xmm0
movsd %xmm0, (%rsp)
movq %rbx, %rdi
movq %r14, %rsi
movl -0x2c(%rbp), %edx
movsd -0xa0(%rbp), %xmm0
movapd %xmm2, %xmm1
movaps -0x60(%rbp), %xmm2
movsd -0x98(%rbp), %xmm3
movsd -0xa8(%rbp), %xmm4
movapd -0xe0(%rbp), %xmm6
movl -0xd0(%rbp), %ecx
movl %r12d, %r8d
movsd -0x90(%rbp), %xmm7
callq 0x18845
xorps %xmm0, %xmm0
cvtsi2sd %r13d, %xmm0
movsd -0x40(%rbp), %xmm1
callq 0xb360
mulsd -0x120(%rbp), %xmm0
movapd -0x100(%rbp), %xmm1
addsd %xmm1, %xmm0
movapd %xmm0, -0x70(%rbp)
movapd %xmm0, %xmm2
subsd %xmm1, %xmm2
movsd %xmm2, -0x48(%rbp)
movapd -0xc0(%rbp), %xmm0
callq 0xa580
movapd -0x70(%rbp), %xmm2
movapd %xmm0, %xmm5
mulsd -0x48(%rbp), %xmm5
incl %r13d
movapd -0x60(%rbp), %xmm0
ucomisd %xmm2, %xmm0
ja 0x185f1
movapd -0xc0(%rbp), %xmm0
callq 0xa580
movapd -0x60(%rbp), %xmm1
testl %r15d, %r15d
movl -0x2c(%rbp), %r13d
jle 0x1876e
movapd %xmm1, %xmm2
subsd -0x100(%rbp), %xmm2
mulsd %xmm0, %xmm2
movapd %xmm2, -0x70(%rbp)
movsd -0x88(%rbp), %xmm0
movsd %xmm0, 0x10(%rsp)
movaps -0xf0(%rbp), %xmm0
movsd %xmm0, 0x8(%rsp)
movsd -0x50(%rbp), %xmm0
movsd %xmm0, (%rsp)
movq %rbx, %rdi
movq %r14, %rsi
movl %r13d, %edx
movsd -0xa0(%rbp), %xmm0
movapd -0x60(%rbp), %xmm2
movsd -0x98(%rbp), %xmm3
movsd -0xa8(%rbp), %xmm4
movapd -0x70(%rbp), %xmm5
movapd -0xe0(%rbp), %xmm6
movl -0xd0(%rbp), %ecx
movl %r12d, %r8d
movsd -0x90(%rbp), %xmm7
callq 0x18845
movapd -0x60(%rbp), %xmm1
decl %r15d
jne 0x186f6
movl 0x1d0(%r14), %edi
movsd 0x8(%rbx), %xmm3
movsd 0x20(%rbx), %xmm4
movsd 0x38(%rbx), %xmm5
movsd 0x50(%rbx), %xmm6
movsd 0x68(%rbx), %xmm7
movsd 0x80(%rbx), %xmm0
movsd %xmm0, (%rsp)
movsd -0x78(%rbp), %xmm0
movsd -0x80(%rbp), %xmm1
movsd -0x38(%rbp), %xmm2
callq 0xa8d0
movsd -0x78(%rbp), %xmm0
movsd %xmm0, 0x600(%rbx)
movsd -0x80(%rbp), %xmm0
movsd %xmm0, 0x608(%rbx)
movsd -0x38(%rbp), %xmm0
movsd %xmm0, 0x610(%rbx)
xorl %eax, %eax
jmp 0x18833
leaq 0x1dc64(%rip), %rdi     # 0x36445
jmp 0x187ea
leaq 0x1dc7c(%rip), %rdi     # 0x36466
callq 0xa1f0
movq %r15, %rdi
movq %rax, %rsi
xorl %eax, %eax
callq 0xa850
movq 0x2875d(%rip), %rbx     # 0x40f60
leaq 0xc6d0(%rbx), %rdi
leaq 0x1dbc2(%rip), %rsi     # 0x363d3
movl $0x50, %edx
callq 0xa8a0
movw $0x0, 0xc71f(%rbx)
movl $0x1, 0xd670(%rbx)
movl $0x5, %eax
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq