// File: 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
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x40(%rbp)
movq -0x10(%rbp), %rax
movl $0xf8, %edx
movl $0x1, %ecx
movzbl 0x1(%rax), %esi
callq 0x21b00
movq -0x10(%rbp), %rax
movzbl 0x1(%rax), %edx
andl $0x80, %edx
movl $0x78b, %eax            # imm = 0x78B
movl $0x7b8, %ecx            # imm = 0x7B8
cmpl $0x0, %edx
cmovnel %ecx, %eax
movl %eax, -0x14(%rbp)
movq -0x10(%rbp), %rax
movzbl 0x1(%rax), %eax
sarl $0x3, %eax
andl $0xf, %eax
movl %eax, -0x18(%rbp)
movq -0x10(%rbp), %rax
movzbl 0x3(%rax), %eax
andl $0xf, %eax
movl %eax, -0x1c(%rbp)
movq -0x10(%rbp), %rax
movzbl 0x3(%rax), %eax
sarl $0x4, %eax
andl $0x7, %eax
movl %eax, -0x20(%rbp)
movq -0x10(%rbp), %rax
movzbl 0x3(%rax), %eax
andl $0x80, %eax
cmpl $0x0, %eax
setne %al
andb $0x1, %al
movb %al, -0x21(%rbp)
movl $0x4, -0x28(%rbp)
movb -0x21(%rbp), %cl
leaq 0x1c238(%rip), %rsi     # 0x3e3e1
leaq 0x1c228(%rip), %rax     # 0x3e3d8
testb $0x1, %cl
cmovneq %rax, %rsi
leaq 0x1c207(%rip), %rdi     # 0x3e3c5
movb $0x0, %al
callq 0x4050
movl -0x14(%rbp), %esi
leaq 0x1c21a(%rip), %rdi     # 0x3e3e9
movb $0x0, %al
callq 0x4050
cmpl $0x0, -0x18(%rbp)
je 0x22220
movl -0x18(%rbp), %esi
leaq 0x1c227(%rip), %rdi     # 0x3e40d
movb $0x0, %al
callq 0x4050
movq -0x40(%rbp), %rax
movzwl 0x914(%rax), %eax
movl -0x18(%rbp), %ecx
movl $0x1, %edx
shll %cl, %edx
movl %edx, %ecx
andl %ecx, %eax
cmpl $0x0, %eax
jne 0x2221e
movl -0x18(%rbp), %edx
xorl %edi, %edi
leaq 0x1c242(%rip), %rsi     # 0x3e459
movb $0x0, %al
callq 0x47d0
jmp 0x22220
cmpl $0x0, -0x20(%rbp)
jne 0x22275
movq -0x10(%rbp), %rax
movzbl 0x4(%rax), %eax
cmpq $0x9, %rax
jb 0x22241
leaq 0x19934(%rip), %rax     # 0x3bb6f
movq %rax, -0x48(%rbp)
jmp 0x2225a
movq -0x10(%rbp), %rax
movzbl 0x4(%rax), %eax
movl %eax, %ecx
leaq 0x296fe(%rip), %rax     # 0x4b950
movq (%rax,%rcx,8), %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rsi
leaq 0x1c235(%rip), %rdi     # 0x3e49a
movb $0x0, %al
callq 0x4050
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0x20(%rbp), %eax
jae 0x22326
movl -0x28(%rbp), %eax
imull $0x3, -0x2c(%rbp), %ecx
addl %ecx, %eax
movl %eax, -0x30(%rbp)
movl -0x2c(%rbp), %eax
movl %eax, -0x54(%rbp)
movq -0x10(%rbp), %rax
movl -0x30(%rbp), %ecx
movzbl (%rax,%rcx), %eax
movq -0x10(%rbp), %rcx
movl -0x30(%rbp), %edx
addl $0x1, %edx
movl %edx, %edx
movzbl (%rcx,%rdx), %ecx
andl $0xf, %ecx
shll $0x8, %ecx
orl %ecx, %eax
movzwl %ax, %edi
callq 0x223f0
movsd %xmm0, -0x50(%rbp)
movq -0x10(%rbp), %rax
movl -0x30(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, %ecx
movzbl (%rax,%rcx), %eax
andl $0xf0, %eax
sarl $0x4, %eax
movq -0x10(%rbp), %rcx
movl -0x30(%rbp), %edx
addl $0x2, %edx
movl %edx, %edx
movzbl (%rcx,%rdx), %ecx
shll $0x4, %ecx
orl %ecx, %eax
movzwl %ax, %edi
callq 0x223f0
movl -0x54(%rbp), %esi
movaps %xmm0, %xmm1
movsd -0x50(%rbp), %xmm0
leaq 0x1c1a2(%rip), %rdi     # 0x3e4b3
movb $0x2, %al
callq 0x4050
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x2227c
imull $0x3, -0x20(%rbp), %eax
addl -0x28(%rbp), %eax
movl %eax, -0x28(%rbp)
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jae 0x223e1
movl -0x28(%rbp), %eax
imull $0x3, -0x34(%rbp), %ecx
addl %ecx, %eax
movl %eax, -0x38(%rbp)
movl -0x34(%rbp), %eax
movl %eax, -0x64(%rbp)
movq -0x10(%rbp), %rax
movl -0x38(%rbp), %ecx
movzbl (%rax,%rcx), %eax
movq -0x10(%rbp), %rcx
movl -0x38(%rbp), %edx
addl $0x1, %edx
movl %edx, %edx
movzbl (%rcx,%rdx), %ecx
andl $0xf, %ecx
shll $0x8, %ecx
orl %ecx, %eax
movzwl %ax, %edi
callq 0x223f0
movsd %xmm0, -0x60(%rbp)
movq -0x10(%rbp), %rax
movl -0x38(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, %ecx
movzbl (%rax,%rcx), %eax
andl $0xf0, %eax
sarl $0x4, %eax
movq -0x10(%rbp), %rcx
movl -0x38(%rbp), %edx
addl $0x2, %edx
movl %edx, %edx
movzbl (%rcx,%rdx), %ecx
shll $0x4, %ecx
orl %ecx, %eax
movzwl %ax, %edi
callq 0x223f0
movl -0x64(%rbp), %esi
movaps %xmm0, %xmm1
movsd -0x60(%rbp), %xmm0
leaq 0x1c106(%rip), %rdi     # 0x3e4d2
movb $0x2, %al
callq 0x4050
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x22337
addq $0x70, %rsp
popq %rbp
retq
nopw (%rax,%rax)