// File: /workspace/github_cpp_makefile_star2_O2/rpavlik[P]edid-decode/calc-gtf-cvt.cpp
// Function: edid_state::calc_gtf_mode(unsigned int, unsigned int, double, bool, gtf_ip_parm, bool, bool, double, double, double, double)
timings edid_state::calc_gtf_mode(unsigned h_pixels, unsigned v_lines,
				  double ip_freq_rqd, bool int_rqd,
				  enum gtf_ip_parm ip_parm, bool margins_rqd,
				  bool secondary, double C, double M, double K, double J)
{
	timings t = {};
	/* C' and M' are part of the Blanking Duty Cycle computation */
	double C_PRIME = ((C - J) * K / 256.0) + J;
	double M_PRIME = K / 256.0 * M;

	double h_pixels_rnd = round(h_pixels / CELL_GRAN) * CELL_GRAN;
	double v_lines_rnd = int_rqd ? round(v_lines / 2.0) : v_lines;
	unsigned hor_margin = margins_rqd ?
		round(h_pixels_rnd * MARGIN_PERC / 100.0 / CELL_GRAN)  * CELL_GRAN : 0;
	unsigned vert_margin = margins_rqd ? round(MARGIN_PERC / 100.0 * v_lines_rnd) : 0;
	double interlace = int_rqd ? 0.5 : 0;
	double total_active_pixels = h_pixels_rnd + hor_margin * 2;

	t.hact = h_pixels_rnd;
	t.vact = v_lines;
	t.interlaced = int_rqd;

	double pixel_freq;
	double h_blank_pixels;
	double total_pixels;
	double v_sync_bp;

	if (ip_parm == gtf_ip_vert_freq) {
		// vertical frame frequency (Hz)
		double v_field_rate_rqd = int_rqd ? ip_freq_rqd * 2 : ip_freq_rqd;
		double h_period_est = ((1.0 / v_field_rate_rqd) - GTF_MIN_VSYNC_BP / 1000000.0) /
			(v_lines_rnd + vert_margin * 2 + GTF_MIN_PORCH + interlace) * 1000000.0;
		v_sync_bp = round(GTF_MIN_VSYNC_BP / h_period_est);
		double total_v_lines = v_lines_rnd + vert_margin * 2 +
			v_sync_bp + interlace + GTF_MIN_PORCH;
		double v_field_rate_est = 1.0 / h_period_est / total_v_lines * 1000000.0;
		double h_period = h_period_est / (v_field_rate_rqd / v_field_rate_est);
		double ideal_duty_cycle = C_PRIME - (M_PRIME * h_period / 1000.0);
		h_blank_pixels = round(total_active_pixels * ideal_duty_cycle /
				       (100.0 - ideal_duty_cycle) /
				       (2 * CELL_GRAN)) * 2 * CELL_GRAN;
		total_pixels = total_active_pixels + h_blank_pixels;
		pixel_freq = total_pixels / h_period;
	} else if (ip_parm == gtf_ip_hor_freq) {
		// horizontal frequency (kHz)
		double h_freq = ip_freq_rqd;
		v_sync_bp = round(GTF_MIN_VSYNC_BP * h_freq / 1000.0);
		double ideal_duty_cycle = C_PRIME - (M_PRIME / h_freq);
		h_blank_pixels = round(total_active_pixels * ideal_duty_cycle /
				       (100.0 - ideal_duty_cycle) /
				       (2 * CELL_GRAN)) * 2 * CELL_GRAN;
		total_pixels = total_active_pixels + h_blank_pixels;
		pixel_freq = total_pixels * h_freq / 1000.0;
	} else {
		// pixel clock rate (MHz)
		pixel_freq = ip_freq_rqd;
		double ideal_h_period =
			((C_PRIME - 100.0) +
			 sqrt(((100.0 - C_PRIME) * (100.0 - C_PRIME) +
			       (0.4 * M_PRIME * (total_active_pixels + hor_margin * 2) /
				pixel_freq)))) / 2.0 / M_PRIME * 1000.0;
		double ideal_duty_cycle = C_PRIME - (M_PRIME * ideal_h_period) / 1000.0;
		h_blank_pixels = round(total_active_pixels * ideal_duty_cycle /
				       (100.0 - ideal_duty_cycle) /
				       (2 * CELL_GRAN)) * 2 * CELL_GRAN;
		total_pixels = total_active_pixels + h_blank_pixels;
		double h_freq = pixel_freq / total_pixels * 1000.0;
		v_sync_bp = round(GTF_MIN_VSYNC_BP * h_freq / 1000.0);
	}

	double v_back_porch = v_sync_bp - GTF_V_SYNC_RQD;

	t.vbp = v_back_porch;
	t.vsync = GTF_V_SYNC_RQD;
	t.vfp = GTF_MIN_PORCH;
	t.pixclk_khz = round(1000.0 * pixel_freq);
	t.hsync = round(GTF_H_SYNC_PERC / 100.0 * total_pixels / CELL_GRAN) * CELL_GRAN;
	t.hfp = (h_blank_pixels / 2.0) - t.hsync;
	t.hbp = t.hfp + t.hsync;
	t.hborder = hor_margin;
	t.vborder = vert_margin;
	t.pos_pol_hsync = secondary;
	t.pos_pol_vsync = !secondary;
	t.rb = secondary ? RB_GTF : RB_NONE;
	return t;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movsd %xmm2, 0x38(%rsp)
movl %r9d, %r14d
movl %r8d, %ebp
movsd %xmm0, 0x18(%rsp)
movl %ecx, %r15d
movq %rdi, %rbx
movb 0x80(%rsp), %r13b
xorps %xmm0, %xmm0
movups %xmm0, 0x44(%rdi)
movups %xmm0, 0x38(%rdi)
movups %xmm0, 0x28(%rdi)
movups %xmm0, 0x18(%rdi)
movups %xmm0, 0x8(%rdi)
movsd %xmm4, 0x30(%rsp)
subsd %xmm4, %xmm1
movsd %xmm3, 0x20(%rsp)
mulsd %xmm3, %xmm1
movsd %xmm1, 0x10(%rsp)
movl %edx, %eax
cvtsi2sd %rax, %xmm0
mulsd 0xaca6(%rip), %xmm0     # 0x23f78
callq 0x3100
movapd %xmm0, %xmm3
movl %r15d, %eax
cvtsi2sd %rax, %xmm0
testl %ebp, %ebp
je 0x19300
mulsd 0xd21(%rip), %xmm0      # 0x1a010
movsd %xmm3, 0x8(%rsp)
callq 0x3100
movsd 0x8(%rsp), %xmm3
movsd %xmm0, 0x28(%rsp)
movsd 0x10(%rsp), %xmm4
movsd 0xac4c(%rip), %xmm0     # 0x23f60
mulsd %xmm0, %xmm4
mulsd 0x140b0(%rip), %xmm3    # 0x2d3d0
movsd 0x20(%rsp), %xmm5
mulsd %xmm0, %xmm5
xorl %r12d, %r12d
movl $0x0, %ecx
testb %r13b, %r13b
je 0x193a1
movsd 0x14099(%rip), %xmm0    # 0x2d3d8
mulsd %xmm3, %xmm0
divsd 0xcd5(%rip), %xmm0      # 0x1a020
mulsd 0xac25(%rip), %xmm0     # 0x23f78
movsd %xmm3, 0x8(%rsp)
movsd %xmm4, 0x10(%rsp)
movsd %xmm5, 0x20(%rsp)
callq 0x3100
mulsd 0x1405e(%rip), %xmm0    # 0x2d3d0
cvttsd2si %xmm0, %r12
movsd 0x14061(%rip), %xmm0    # 0x2d3e0
mulsd 0x28(%rsp), %xmm0
callq 0x3100
movsd 0x20(%rsp), %xmm5
movsd 0x10(%rsp), %xmm4
movsd 0x8(%rsp), %xmm3
cvttsd2si %xmm0, %rcx
movb 0x88(%rsp), %r13b
addsd 0x30(%rsp), %xmm4
leal (%r12,%r12), %eax
cvtsi2sd %rax, %xmm1
mulsd 0x38(%rsp), %xmm5
cvttsd2si %xmm3, %rax
addsd %xmm1, %xmm3
movl %eax, (%rbx)
movl %r15d, 0x4(%rbx)
movb %bpl, 0x18(%rbx)
cmpl $0x2, %r14d
movsd %xmm3, 0x8(%rsp)
je 0x193f9
cmpl $0x1, %r14d
jne 0x194a9
testb %bpl, %bpl
jne 0x19501
xorpd %xmm2, %xmm2
jmp 0x19509
movq %rcx, %r14
movsd 0x13fe4(%rip), %xmm0    # 0x2d3e8
mulsd 0x18(%rsp), %xmm0
divsd 0xc06(%rip), %xmm0      # 0x1a018
movsd %xmm4, 0x10(%rsp)
movsd %xmm5, 0x20(%rsp)
callq 0x3100
movsd %xmm0, 0x38(%rsp)
movsd 0x20(%rsp), %xmm0
divsd 0x18(%rsp), %xmm0
movsd 0x10(%rsp), %xmm1
subsd %xmm0, %xmm1
movsd 0xbd9(%rip), %xmm0      # 0x1a020
subsd %xmm1, %xmm0
mulsd 0x8(%rsp), %xmm1
divsd %xmm0, %xmm1
mulsd 0xf1fb(%rip), %xmm1     # 0x28658
movapd %xmm1, %xmm0
callq 0x3100
movsd 0x38(%rsp), %xmm1
movapd %xmm0, %xmm2
movsd 0x18(%rsp), %xmm0
addsd %xmm2, %xmm2
mulsd 0x13f4e(%rip), %xmm2    # 0x2d3d0
movsd %xmm2, 0x10(%rsp)
movsd 0x8(%rsp), %xmm3
addsd %xmm2, %xmm3
movsd %xmm3, 0x8(%rsp)
mulsd %xmm3, %xmm0
divsd 0xb74(%rip), %xmm0      # 0x1a018
jmp 0x19728
movsd 0x13f57(%rip), %xmm7    # 0x2d408
addsd %xmm4, %xmm7
movsd 0xb63(%rip), %xmm6      # 0x1a020
movapd %xmm6, %xmm0
subsd %xmm4, %xmm0
movsd 0x13f43(%rip), %xmm2    # 0x2d410
mulsd %xmm5, %xmm2
addsd %xmm3, %xmm1
mulsd %xmm2, %xmm1
divsd 0x18(%rsp), %xmm1
mulsd %xmm0, %xmm0
addsd %xmm1, %xmm0
xorpd %xmm1, %xmm1
movq %rcx, %r14
ucomisd %xmm1, %xmm0
jb 0x19659
sqrtsd %xmm0, %xmm0
jmp 0x19690
movsd 0xb07(%rip), %xmm2      # 0x1a010
movsd 0x18(%rsp), %xmm0
movsd %xmm4, 0x10(%rsp)
movsd %xmm5, 0x20(%rsp)
je 0x19521
addsd %xmm0, %xmm0
movsd %xmm0, 0x18(%rsp)
movsd 0x51a9(%rip), %xmm3     # 0x1e6d8
movapd %xmm3, %xmm1
divsd %xmm0, %xmm1
addsd 0x13eb1(%rip), %xmm1    # 0x2d3f0
movq %rcx, %r14
leal (%rcx,%rcx), %eax
cvtsi2sd %rax, %xmm0
movsd 0x28(%rsp), %xmm4
addsd %xmm0, %xmm4
movsd %xmm4, 0x28(%rsp)
movapd %xmm4, %xmm0
addsd %xmm3, %xmm0
addsd %xmm2, %xmm0
divsd %xmm0, %xmm1
mulsd 0x13e86(%rip), %xmm1    # 0x2d3f8
movsd %xmm1, 0x30(%rsp)
movsd 0x13e68(%rip), %xmm0    # 0x2d3e8
divsd %xmm1, %xmm0
movsd %xmm2, 0x40(%rsp)
callq 0x3100
movsd %xmm0, 0x38(%rsp)
movsd 0x28(%rsp), %xmm1
addsd %xmm0, %xmm1
addsd 0x40(%rsp), %xmm1
movsd 0x512b(%rip), %xmm0     # 0x1e6d8
addsd %xmm0, %xmm1
movapd %xmm1, %xmm2
movsd 0x30(%rsp), %xmm1
divsd %xmm1, %xmm0
divsd %xmm2, %xmm0
mulsd 0x13e2d(%rip), %xmm0    # 0x2d3f8
movsd 0x18(%rsp), %xmm2
divsd %xmm0, %xmm2
divsd %xmm2, %xmm1
movsd %xmm1, 0x30(%rsp)
movsd 0x20(%rsp), %xmm0
mulsd %xmm1, %xmm0
divsd 0x13e0f(%rip), %xmm0    # 0x2d400
movsd 0x10(%rsp), %xmm2
addsd %xmm0, %xmm2
movsd 0x8(%rsp), %xmm0
mulsd %xmm2, %xmm0
movsd 0xa13(%rip), %xmm1      # 0x1a020
subsd %xmm2, %xmm1
divsd %xmm1, %xmm0
mulsd 0xf03b(%rip), %xmm0     # 0x28658
callq 0x3100
movsd 0x38(%rsp), %xmm1
addsd %xmm0, %xmm0
mulsd 0x13d9c(%rip), %xmm0    # 0x2d3d0
movsd %xmm0, 0x10(%rsp)
movsd 0x8(%rsp), %xmm2
addsd %xmm0, %xmm2
movsd %xmm2, 0x8(%rsp)
movapd %xmm2, %xmm0
divsd 0x30(%rsp), %xmm0
jmp 0x19728
movsd %xmm4, 0x10(%rsp)
movsd %xmm5, 0x20(%rsp)
movsd %xmm7, 0x28(%rsp)
callq 0x31e0
movsd 0x28(%rsp), %xmm7
movsd 0x9a2(%rip), %xmm6      # 0x1a020
movsd 0x20(%rsp), %xmm5
movsd 0x10(%rsp), %xmm4
movsd 0x8(%rsp), %xmm3
addsd %xmm0, %xmm7
mulsd 0x13d7c(%rip), %xmm7    # 0x2d418
divsd %xmm5, %xmm7
movsd 0x970(%rip), %xmm0      # 0x1a018
mulsd %xmm0, %xmm7
mulsd %xmm5, %xmm7
divsd %xmm0, %xmm7
addsd %xmm7, %xmm4
mulsd %xmm4, %xmm3
subsd %xmm4, %xmm6
divsd %xmm6, %xmm3
mulsd 0xef8c(%rip), %xmm3     # 0x28658
movapd %xmm3, %xmm0
callq 0x3100
addsd %xmm0, %xmm0
mulsd 0x13cef(%rip), %xmm0    # 0x2d3d0
movsd %xmm0, 0x10(%rsp)
movsd 0x8(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd 0x18(%rsp), %xmm0
movsd %xmm1, 0x8(%rsp)
divsd %xmm1, %xmm0
movsd 0x90f(%rip), %xmm1      # 0x1a018
mulsd %xmm1, %xmm0
mulsd 0x13cd3(%rip), %xmm0    # 0x2d3e8
divsd %xmm1, %xmm0
callq 0x3100
movapd %xmm0, %xmm1
movsd 0x18(%rsp), %xmm0
addsd 0x13cf0(%rip), %xmm1    # 0x2d420
cvttsd2si %xmm1, %eax
movl %eax, 0x34(%rbx)
movabsq $0x300000001, %rax      # imm = 0x300000001
movq %rax, 0x2c(%rbx)
mulsd 0x8cb(%rip), %xmm0      # 0x1a018
callq 0x3100
cvttsd2si %xmm0, %rax
movl %eax, 0x10(%rbx)
movsd 0x8(%rsp), %xmm0
mulsd 0x13cc0(%rip), %xmm0    # 0x2d428
mulsd 0xa808(%rip), %xmm0     # 0x23f78
callq 0x3100
mulsd 0x13c53(%rip), %xmm0    # 0x2d3d0
cvttsd2si %xmm0, %rax
movsd 0x10(%rsp), %xmm1
mulsd 0x880(%rip), %xmm1      # 0x1a010
movl %eax, %ecx
cvtsi2sd %rcx, %xmm0
movl %eax, 0x20(%rbx)
subsd %xmm0, %xmm1
cvttsd2si %xmm1, %ecx
movl %ecx, 0x1c(%rbx)
addl %eax, %ecx
movl %ecx, 0x24(%rbx)
movl %r12d, 0x3c(%rbx)
movl %r14d, 0x40(%rbx)
movb %r13b, 0x28(%rbx)
movzbl %r13b, %eax
xorb $0x1, %r13b
movb %r13b, 0x38(%rbx)
shll $0x2, %eax
movl %eax, 0x14(%rbx)
movq %rbx, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq