// File: /workspace/github_cpp_makefile_star2_O1/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 0x19170
leaq 0x20226(%rip), %rdi     # 0x39391
jmp 0x19670
movq %rsi, %r14
movsd 0x1a0(%rsi), %xmm6
xorpd %xmm3, %xmm3
ucomisd %xmm3, %xmm6
jne 0x19193
jp 0x19193
leaq 0x2029f(%rip), %rdi     # 0x3942d
jmp 0x19670
movsd 0x1b0(%r14), %xmm10
ucomisd %xmm10, %xmm3
jae 0x19660
movsd 0x1c0(%r14), %xmm4
ucomisd %xmm4, %xmm10
jae 0x19669
movq %rdx, %rbx
movsd 0x610(%rdx), %xmm5
movq 0x2ad93(%rip), %rax     # 0x43f60
cmpb $0x0, 0x1e6d8(%rax)
movsd %xmm1, -0x80(%rbp)
movsd %xmm0, -0x78(%rbp)
je 0x191f5
movsd 0x1be20(%rip), %xmm0    # 0x35008
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 0x1be03(%rip), %xmm0    # 0x35020
movsd 0x210(%r14), %xmm9
movsd 0x220(%r14), %xmm1
movsd 0x1d639(%rip), %xmm3    # 0x36870
cmpeqsd %xmm1, %xmm3
andnpd %xmm1, %xmm3
movlpd %xmm3, -0x110(%rbp)
fldt 0x1dd72(%rip)           # 0x36fc0
fmull -0x110(%rbp)
fstpl -0x108(%rbp)
movsd -0x108(%rbp), %xmm3
movapd 0x1dca6(%rip), %xmm1    # 0x36f10
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 0x192a2
movl 0x194(%r14), %r15d
incl %r15d
andpd %xmm0, %xmm4
orpd %xmm1, %xmm12
movsd 0x230(%r14), %xmm3
movsd 0x1dbe4(%rip), %xmm1    # 0x36ea0
ucomisd %xmm3, %xmm1
ja 0x192cc
cmpb $0x0, 0x229(%r14)
jne 0x192d0
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 0x1930d
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 0x193b2
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 0x1db47(%rip), %xmm0    # 0x36f10
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 0x1da59(%rip), %xmm1    # 0x36ea0
movsd %xmm1, -0x40(%rbp)
jbe 0x19548
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 0x196d5
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 0x19477
movapd -0xc0(%rbp), %xmm0
callq 0xa580
movapd -0x60(%rbp), %xmm1
testl %r15d, %r15d
movl -0x2c(%rbp), %r13d
jle 0x195f4
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 0x196d5
movapd -0x60(%rbp), %xmm1
decl %r15d
jne 0x1957c
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 0x196c3
leaq 0x1fdde(%rip), %rdi     # 0x39445
jmp 0x19670
leaq 0x1fdf6(%rip), %rdi     # 0x39466
callq 0xa1f0
movq %r15, %rdi
movq %rax, %rsi
xorl %eax, %eax
callq 0xa850
movq 0x2a8d7(%rip), %rbx     # 0x43f60
movl $0x0, 0xd670(%rbx)
leaq 0xc6d0(%rbx), %rdi
leaq 0x1fd32(%rip), %rsi     # 0x393d3
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