// File: /workspace/github_cpp_makefile_star2_O2/rpavlik[P]edid-decode/parse-displayid-block.cpp
// Function: edid_state::parse_displayid_transfer_characteristics(unsigned char const*)
void edid_state::parse_displayid_transfer_characteristics(const unsigned char *x)
{
	check_displayid_datablock_revision(x[1], 0xf0, 1);

	unsigned xfer_id = x[1] >> 4;
	bool first_is_white = x[3] & 0x80;
	bool four_param = x[3] & 0x20;

	if (xfer_id) {
		printf("    Transfer Characteristics Data Block Identifier: %u\n", xfer_id);
		if (!(dispid.preparsed_color_ids & (1 << xfer_id)))
			fail("Missing Color Characteristics Data Block using Identifier %u.\n", xfer_id);
	}
	if (first_is_white)
		printf("    The first curve is the 'white' transfer characteristic\n");
	if (x[3] & 0x40)
		printf("    Individual response curves\n");

	unsigned offset = 4;
	unsigned len = x[2] - 1;

	for (unsigned i = 0; len; i++) {
		if ((x[3] & 0x80) && !i)
			printf("    White curve:      ");
		else
			printf("    Response curve #%u:",
			       i - first_is_white);
		unsigned samples = x[offset];
		if (four_param) {
			if (samples != 5)
				fail("Expected 5 samples.\n");
			printf(" A0=%u A1=%u A2=%u A3=%u Gamma=%.2f\n",
			       x[offset + 1], x[offset + 2], x[offset + 3], x[offset + 4],
			       (double)(x[offset + 5] + 100.0) / 100.0);
			samples++;
		} else {
			double sum = 0;

			// The spec is not very clear about the number of samples:
			// should this be interpreted as the actual number of
			// samples stored in this Data Block, or as the number of
			// samples in the curve, but where the last sample is not
			// actually stored since it is always 0x3ff.
			//
			// The ATP Manager interprets this as the latter, so that's
			// what we implement here.
			for (unsigned j = offset + 1; j < offset + samples; j++) {
				sum += x[j];
				printf(" %.2f", sum * 100.0 / 1023.0);
			}
			printf(" 100.00\n");
		}
		offset += samples;
		len -= samples;
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %rbx
movq %rdi, %r15
movzbl 0x1(%rsi), %esi
pushq $0x1
popq %rcx
movl $0xf0, %edx
callq 0x123f0
movzbl 0x1(%rbx), %ebp
movzbl 0x3(%rbx), %eax
movl %eax, 0x4(%rsp)
cmpl $0x10, %ebp
jb 0x13cf0
shrl $0x4, %ebp
leaq 0x1560f(%rip), %rdi     # 0x292d7
movl %ebp, %esi
xorl %eax, %eax
callq 0x3030
movzwl 0x912(%r15), %eax
btl %ebp, %eax
jb 0x13cf0
leaq 0x1562a(%rip), %rsi     # 0x2930f
xorl %edi, %edi
movl %ebp, %edx
xorl %eax, %eax
callq 0x362c
cmpb $0x0, 0x4(%rsp)
jns 0x13d03
leaq 0x17622(%rip), %rdi     # 0x2b320
callq 0x33b0
testb $0x40, 0x3(%rbx)
je 0x13d15
leaq 0x1764b(%rip), %rdi     # 0x2b35b
callq 0x33b0
movzbl 0x2(%rbx), %eax
decl %eax
movl 0x4(%rsp), %ecx
shrl $0x7, %ecx
movl %ecx, 0x8(%rsp)
pushq $0x4
popq %r13
leaq 0xba69(%rip), %r15      # 0x1f79a
xorl %ebp, %ebp
testl %eax, %eax
je 0x13e63
movl %eax, 0xc(%rsp)
cmpb $0x0, 0x3(%rbx)
setns %al
testl %ebp, %ebp
setne %cl
orb %al, %cl
je 0x13d65
movl %ebp, %esi
subl 0x8(%rsp), %esi
leaq 0x15609(%rip), %rdi     # 0x29365
xorl %eax, %eax
callq 0x3030
jmp 0x13d73
leaq 0x155e2(%rip), %rdi     # 0x2934e
xorl %eax, %eax
callq 0x3030
movl %r13d, %eax
movzbl (%rbx,%rax), %r14d
testb $0x20, 0x4(%rsp)
jne 0x13de2
leal (%r14,%r13), %r12d
incl %r13d
xorpd %xmm0, %xmm0
movsd %xmm0, 0x10(%rsp)
cmpl %r12d, %r13d
jae 0x13dd4
movzbl (%rbx,%r13), %eax
cvtsi2sd %eax, %xmm0
movsd 0x10(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x10(%rsp)
movapd %xmm1, %xmm0
mulsd 0x6263(%rip), %xmm0     # 0x1a020
divsd 0x14873(%rip), %xmm0    # 0x28638
movq %r15, %rdi
movb $0x1, %al
callq 0x3030
incq %r13
jmp 0x13d93
leaq 0x1759f(%rip), %rdi     # 0x2b37a
callq 0x33b0
jmp 0x13e52
cmpl $0x5, %r14d
je 0x13df8
xorl %edi, %edi
leaq 0x1558c(%rip), %rsi     # 0x2937d
xorl %eax, %eax
callq 0x362c
leal 0x1(%r13), %eax
movzbl (%rbx,%rax), %esi
leal 0x2(%r13), %eax
movzbl (%rbx,%rax), %edx
leal 0x3(%r13), %eax
movzbl (%rbx,%rax), %ecx
leal 0x4(%r13), %eax
movzbl (%rbx,%rax), %r8d
leal 0x5(%r13), %eax
movzbl (%rbx,%rax), %eax
cvtsi2sd %eax, %xmm0
movsd 0x61f3(%rip), %xmm1     # 0x1a020
addsd %xmm1, %xmm0
divsd %xmm1, %xmm0
leaq 0x15556(%rip), %rdi     # 0x29392
movb $0x1, %al
callq 0x3030
leal (%r14,%r13), %r12d
incl %r12d
movl %r14d, %eax
incl %eax
movl %eax, %r14d
movl 0xc(%rsp), %eax
subl %r14d, %eax
incl %ebp
movl %r12d, %r13d
jmp 0x13d33
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq