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

	unsigned cie_year = (x[1] & 0x80) ? 1976 : 1931;
	unsigned xfer_id = (x[1] >> 3) & 0x0f;
	unsigned num_whitepoints = x[3] & 0x0f;
	unsigned num_primaries = (x[3] >> 4) & 0x07;
	bool temporal_color = x[3] & 0x80;
	unsigned offset = 4;

	printf("    Uses %s color\n", temporal_color ? "temporal" : "spatial");
	printf("    Uses %u CIE (x, y) coordinates\n", cie_year);
	if (xfer_id) {
		printf("    Associated with Transfer Characteristics Data Block with Identifier %u\n", xfer_id);
		if (!(dispid.preparsed_xfer_ids & (1 << xfer_id)))
			fail("Missing Transfer Characteristics Data Block with Identifier %u.\n", xfer_id);
	}
	if (!num_primaries) {
		printf("    Uses color space %s\n",
		       x[4] >= ARRAY_SIZE(std_colorspace_ids) ? "Reserved" :
								std_colorspace_ids[x[4]]);
		offset++;
	}
	for (unsigned i = 0; i < num_primaries; i++) {
		unsigned idx = offset + 3 * i;

		printf("    Primary #%u: (%.4f, %.4f)\n", i,
		       fp2d(x[idx] | ((x[idx + 1] & 0x0f) << 8)),
		       fp2d(((x[idx + 1] & 0xf0) >> 4) | (x[idx + 2] << 4)));
	}
	offset += 3 * num_primaries;
	for (unsigned i = 0; i < num_whitepoints; i++) {
		unsigned idx = offset + 3 * i;

		printf("    White point #%u: (%.4f, %.4f)\n", i,
		       fp2d(x[idx] | ((x[idx + 1] & 0x0f) << 8)),
		       fp2d(((x[idx + 1] & 0xf0) >> 4) | (x[idx + 2] << 4)));
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %rbx
movq %rdi, %r14
movzbl 0x1(%rsi), %esi
pushq $0x1
popq %rcx
movl $0xf8, %edx
callq 0x123f0
movzbl 0x1(%rbx), %ebp
testb %bpl, %bpl
movl $0x78b, %eax            # imm = 0x78B
movl $0x7b8, %r15d           # imm = 0x7B8
cmovnsl %eax, %r15d
shrl $0x3, %ebp
movzbl 0x3(%rbx), %r12d
movl %r12d, %r13d
shrl $0x4, %r13d
andl $0x7, %r13d
testb %r12b, %r12b
leaq 0x16562(%rip), %rax     # 0x28d35
leaq 0x16552(%rip), %rsi     # 0x28d2c
cmovnsq %rax, %rsi
leaq 0x16534(%rip), %rdi     # 0x28d19
xorl %eax, %eax
callq 0x3030
leaq 0x1654a(%rip), %rdi     # 0x28d3d
movl %r15d, %esi
xorl %eax, %eax
callq 0x3030
andl $0xf, %ebp
je 0x12831
leaq 0x16558(%rip), %rdi     # 0x28d61
movl %ebp, %esi
xorl %eax, %eax
callq 0x3030
movzwl 0x914(%r14), %eax
btl %ebp, %eax
jb 0x12831
leaq 0x16587(%rip), %rsi     # 0x28dad
xorl %edi, %edi
movl %ebp, %edx
xorl %eax, %eax
callq 0x362c
andl $0xf, %r12d
testl %r13d, %r13d
je 0x1283e
pushq $0x4
jmp 0x1286f
movzbl 0x4(%rbx), %eax
cmpq $0x8, %rax
jbe 0x12851
leaq 0x18d71(%rip), %rsi     # 0x2b5c0
jmp 0x1285f
leaq 0x16118(%rip), %rcx     # 0x28970
movslq (%rcx,%rax,4), %rsi
addq %rcx, %rsi
leaq 0x16588(%rip), %rdi     # 0x28dee
xorl %eax, %eax
callq 0x3030
pushq $0x5
popq %rbp
movq %rbx, (%rsp)
addq %rbp, %rbx
addq $0x2, %rbx
xorl %r14d, %r14d
leaq 0x16582(%rip), %r15     # 0x28e07
cmpq %r14, %r13
je 0x128d5
movzbl -0x2(%rbx), %eax
movzbl -0x1(%rbx), %ecx
movl %ecx, %edx
andl $0xf, %edx
shll $0x8, %edx
orl %eax, %edx
cvtsi2sd %edx, %xmm0
movsd 0x116e0(%rip), %xmm2    # 0x23f88
mulsd %xmm2, %xmm0
shrl $0x4, %ecx
movzbl (%rbx), %eax
shll $0x4, %eax
orl %ecx, %eax
cvtsi2sd %eax, %xmm1
mulsd %xmm2, %xmm1
movq %r15, %rdi
movl %r14d, %esi
movb $0x2, %al
callq 0x3030
incq %r14
addq $0x3, %rbx
jmp 0x12885
leaq (,%r13,2), %rax
addq %r13, %rax
addq %rax, %rbp
movq (%rsp), %rax
leaq (%rax,%rbp), %r15
addq $0x2, %r15
xorl %ebx, %ebx
leaq 0x1652e(%rip), %r14     # 0x28e26
cmpq %rbx, %r12
je 0x1294a
movzbl -0x2(%r15), %eax
movzbl -0x1(%r15), %ecx
movl %ecx, %edx
andl $0xf, %edx
shll $0x8, %edx
orl %eax, %edx
cvtsi2sd %edx, %xmm0
movsd 0x1166b(%rip), %xmm2    # 0x23f88
mulsd %xmm2, %xmm0
shrl $0x4, %ecx
movzbl (%r15), %eax
shll $0x4, %eax
orl %ecx, %eax
cvtsi2sd %eax, %xmm1
mulsd %xmm2, %xmm1
movq %r14, %rdi
movl %ebx, %esi
movb $0x2, %al
callq 0x3030
incq %rbx
addq $0x3, %r15
jmp 0x128f8
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop