// File: xdsopl[P]FEC/testbench.cc
// Function: main
int main()
{
	std::random_device rd;
	std::default_random_engine generator(rd());
	std::uniform_int_distribution<uint8_t> distribution(0, 255);
	std::vector<uint8_t> data(65471*16);
	std::generate(data.begin(), data.end(), std::bind(distribution, generator));
	if (1) {
		BoseChaudhuriHocquenghem<6, 1, 5, GF::Types<4, 0b10011, uint8_t>> bch({0b10011, 0b11111, 0b00111});
		uint8_t code[15] = { 1, 1, 0, 0, 1 };
		uint8_t target[15] = { 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0 };
		test_bch("NASA INTRO BCH(15, 5) T=3", bch, code, target, data);
	}
	if (1) {
		ReedSolomon<4, 0, GF::Types<4, 0b10011, uint8_t>> rs;
		uint8_t code[15] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
		uint8_t target[15] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 3, 3, 12, 12 };
		test_rs("BBC WHP031 RS(15, 11) T=2", rs, code, target, data);
	}
	if (1) {
		ReedSolomon<16, 0, GF::Types<8, 0b100011101, uint8_t>> rs;
		uint8_t code[255], target[255];
		for (int i = 0; i < 239; ++i)
			target[i] = code[i] = i + 1;
		uint8_t parity[16] = { 1, 126, 147, 48, 155, 224, 3, 157, 29, 226, 40, 114, 61, 30, 244, 75 };
		for (int i = 0; i < 16; ++i)
			target[239+i] = parity[i];
		test_rs("DVB-T RS(255, 239) T=8", rs, code, target, data);
	}
	if (1) {
		BoseChaudhuriHocquenghem<24, 1, 65343, GF::Types<16, 0b10000000000101101, uint16_t>> bch({0b10000000000101101, 0b10000000101110011, 0b10000111110111101, 0b10101101001010101, 0b10001111100101111, 0b11111011110110101, 0b11010111101100101, 0b10111001101100111, 0b10000111010100001, 0b10111010110100111, 0b10011101000101101, 0b10001101011100011});
		uint16_t code[65535], target[65535];
		for (int i = 0, s = 0; i < 65343; ++i, s=(s*(s*s*51767+71287)+35149)&0xffffff)
			target[i] = code[i] = (s^=s>>7)&1;
		uint16_t parity[192] = { 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0 };
		for (int i = 0; i < 192; ++i)
			target[65343+i] = parity[i];
		test_bch("DVB-S2 FULL BCH(65535, 65343) T=12", bch, code, target, data);
	}
	if (1) {
		ReedSolomon<64, 1, GF::Types<16, 0b10001000000001011, uint16_t>> rs;
		uint16_t code[65535], target[65535];
		for (int i = 0; i < 65471; ++i)
			target[i] = code[i] = i + 1;
		uint16_t parity[64] = { 25271, 26303, 22052, 31318, 31233, 6076, 40148, 29468, 47507, 32655, 12404, 13265, 23901, 38403, 50967, 50433, 40818, 226, 62296, 23636, 56393, 12952, 11476, 44416, 518, 50014, 10037, 57582, 33421, 42654, 54025, 7157, 4826, 52148, 17167, 23294, 6427, 40953, 11168, 35305, 18209, 1868, 39971, 54928, 27566, 1424, 4846, 25347, 34710, 42190, 56452, 21859, 49805, 28028, 41657, 25756, 22014, 24479, 28758, 17438, 12976, 61743, 46735, 1557 };
		for (int i = 0; i < 64; ++i)
			target[65471+i] = parity[i];
		test_rs("FUN RS(65535, 65471) T=32", rs, code, target, data);
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x81c10, %rsp          # imm = 0x81C10
movl $0x0, -0x4(%rbp)
leaq -0x1390(%rbp), %rdi
movq %rdi, -0x81bd8(%rbp)
callq 0x2d20
movq -0x81bd8(%rbp), %rdi
callq 0x2dc0
movl %eax, -0x81bd0(%rbp)
jmp 0x2489
movl -0x81bd0(%rbp), %eax
movl %eax, %eax
movl %eax, %esi
leaq -0x1398(%rbp), %rdi
callq 0x2de0
jmp 0x24a1
leaq -0x13a6(%rbp), %rdi
xorl %esi, %esi
movl $0xff, %edx
callq 0x2e10
jmp 0x24b6
leaq -0x13c1(%rbp), %rdi
movq %rdi, -0x81be0(%rbp)
callq 0x2e40
movq -0x81be0(%rbp), %rdx
leaq -0x13c0(%rbp), %rdi
movl $0xffbf0, %esi          # imm = 0xFFBF0
callq 0x2e60
jmp 0x24e3
leaq -0x13c1(%rbp), %rdi
callq 0x2ed0
leaq -0x13c0(%rbp), %rdi
movq %rdi, -0x81be8(%rbp)
callq 0x2f50
movq -0x81be8(%rbp), %rdi
movq %rax, -0x13d0(%rbp)
callq 0x2f80
movq %rax, -0x13d8(%rbp)
leaq -0x13e8(%rbp), %rdi
leaq -0x13a6(%rbp), %rsi
leaq -0x1398(%rbp), %rdx
callq 0x2fb0
jmp 0x2538
movq -0x13d0(%rbp), %rdi
movq -0x13d8(%rbp), %rsi
leaq -0x13e8(%rbp), %rdx
callq 0x2ef0
jmp 0x2554
movl $0x13, -0x1414(%rbp)
movl $0x1f, -0x1410(%rbp)
movl $0x7, -0x140c(%rbp)
leaq -0x1414(%rbp), %rax
movq %rax, -0x1408(%rbp)
movq $0x3, -0x1400(%rbp)
movq -0x1408(%rbp), %rsi
movq -0x1400(%rbp), %rdx
leaq -0x13f3(%rbp), %rdi
callq 0x2ff0
jmp 0x25a7
movabsq $0x100000101, %rax      # imm = 0x100000101
movq %rax, -0x1428(%rbp)
movq $0x0, -0x1421(%rbp)
movabsq $0x1000101010100, %rcx  # imm = 0x1000101010100
movq %rcx, -0x1431(%rbp)
movq %rax, -0x1438(%rbp)
leaq -0x1459(%rbp), %rdi
movq %rdi, -0x81bf0(%rbp)
callq 0x22d0
movq -0x81bf0(%rbp), %rdx
leaq 0x16c84(%rip), %rsi     # 0x19280
leaq -0x1458(%rbp), %rdi
callq 0x48a0
jmp 0x260a
leaq -0x1458(%rbp), %rdi
leaq -0x13f3(%rbp), %rsi
leaq -0x1428(%rbp), %rdx
leaq -0x1438(%rbp), %rcx
leaq -0x13c0(%rbp), %r8
callq 0x33d0
jmp 0x2634
leaq -0x1458(%rbp), %rdi
callq 0x20f0
leaq -0x1459(%rbp), %rdi
callq 0x2200
leaq -0x145e(%rbp), %rdi
callq 0x4950
jmp 0x265a
movabsq $0x807060504030201, %rax # imm = 0x807060504030201
movq %rax, -0x1470(%rbp)
movq $0xb0a0908, -0x1469(%rbp) # imm = 0xB0A0908
movabsq $0xc0c03030b0a0908, %rcx # imm = 0xC0C03030B0A0908
movq %rcx, -0x1479(%rbp)
movq %rax, -0x1480(%rbp)
leaq -0x14a1(%rbp), %rdi
movq %rdi, -0x81bf8(%rbp)
callq 0x22d0
movq -0x81bf8(%rbp), %rdx
leaq 0x16beb(%rip), %rsi     # 0x1929a
leaq -0x14a0(%rbp), %rdi
callq 0x48a0
jmp 0x26bd
leaq -0x14a0(%rbp), %rdi
leaq -0x145e(%rbp), %rsi
leaq -0x1470(%rbp), %rdx
leaq -0x1480(%rbp), %rcx
leaq -0x13c0(%rbp), %r8
callq 0x4c10
jmp 0x26e7
leaq -0x14a0(%rbp), %rdi
callq 0x20f0
leaq -0x14a1(%rbp), %rdi
callq 0x2200
leaq -0x14b2(%rbp), %rdi
callq 0x6050
jmp 0x270d
movl $0x0, -0x16c4(%rbp)
cmpl $0xef, -0x16c4(%rbp)
jge 0x2836
movl -0x16c4(%rbp), %eax
addl $0x1, %eax
movb %al, %cl
movslq -0x16c4(%rbp), %rax
movb %cl, -0x15c0(%rbp,%rax)
movslq -0x16c4(%rbp), %rax
movb %cl, -0x16c0(%rbp,%rax)
movl -0x16c4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x16c4(%rbp)
jmp 0x2717
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x13a0(%rbp)
movl %eax, -0x13a4(%rbp)
jmp 0x2d03
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x13a0(%rbp)
movl %eax, -0x13a4(%rbp)
leaq -0x13c1(%rbp), %rdi
callq 0x2ed0
jmp 0x2d03
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x13a0(%rbp)
movl %eax, -0x13a4(%rbp)
jmp 0x2cf7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x13a0(%rbp)
movl %eax, -0x13a4(%rbp)
jmp 0x27e2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x13a0(%rbp)
movl %eax, -0x13a4(%rbp)
leaq -0x1458(%rbp), %rdi
callq 0x20f0
leaq -0x1459(%rbp), %rdi
callq 0x2200
jmp 0x2cf7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x13a0(%rbp)
movl %eax, -0x13a4(%rbp)
jmp 0x2825
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x13a0(%rbp)
movl %eax, -0x13a4(%rbp)
leaq -0x14a0(%rbp), %rdi
callq 0x20f0
leaq -0x14a1(%rbp), %rdi
callq 0x2200
jmp 0x2cf7
movq 0x1a133(%rip), %rax     # 0x1c970
movq %rax, -0x16e0(%rbp)
movq 0x1a12d(%rip), %rax     # 0x1c978
movq %rax, -0x16d8(%rbp)
movl $0x0, -0x16e4(%rbp)
cmpl $0x10, -0x16e4(%rbp)
jge 0x2898
movslq -0x16e4(%rbp), %rax
movb -0x16e0(%rbp,%rax), %cl
movl -0x16e4(%rbp), %eax
addl $0xef, %eax
cltq
movb %cl, -0x16c0(%rbp,%rax)
movl -0x16e4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x16e4(%rbp)
jmp 0x285c
leaq -0x1709(%rbp), %rdi
movq %rdi, -0x81c00(%rbp)
callq 0x22d0
movq -0x81c00(%rbp), %rdx
leaq 0x169fb(%rip), %rsi     # 0x192b4
leaq -0x1708(%rbp), %rdi
callq 0x48a0
jmp 0x28c7
leaq -0x1708(%rbp), %rdi
leaq -0x14b2(%rbp), %rsi
leaq -0x15c0(%rbp), %rdx
leaq -0x16c0(%rbp), %rcx
leaq -0x13c0(%rbp), %r8
callq 0x6320
jmp 0x28f1
leaq -0x1708(%rbp), %rdi
callq 0x20f0
leaq -0x1709(%rbp), %rdi
callq 0x2200
movups 0x1675c(%rip), %xmm0    # 0x1906c
movaps %xmm0, -0x18b0(%rbp)
movups 0x1673e(%rip), %xmm0    # 0x1905c
movaps %xmm0, -0x18c0(%rbp)
movups 0x16720(%rip), %xmm0    # 0x1904c
movaps %xmm0, -0x18d0(%rbp)
leaq -0x18d0(%rbp), %rax
movq %rax, -0x18a0(%rbp)
movq $0xc, -0x1898(%rbp)
movq -0x18a0(%rbp), %rsi
movq -0x1898(%rbp), %rdx
leaq -0x188c(%rbp), %rdi
callq 0x77b0
jmp 0x2968
movl $0x0, -0x418d4(%rbp)
movl $0x0, -0x418d8(%rbp)
cmpl $0xff3f, -0x418d4(%rbp) # imm = 0xFF3F
jge 0x2a4e
movl -0x418d8(%rbp), %eax
sarl $0x7, %eax
xorl -0x418d8(%rbp), %eax
movl %eax, -0x418d8(%rbp)
andl $0x1, %eax
movw %ax, %cx
movslq -0x418d4(%rbp), %rax
movw %cx, -0x218d0(%rbp,%rax,2)
movslq -0x418d4(%rbp), %rax
movw %cx, -0x418d0(%rbp,%rax,2)
movl -0x418d4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x418d4(%rbp)
movl -0x418d8(%rbp), %eax
movl -0x418d8(%rbp), %ecx
imull -0x418d8(%rbp), %ecx
imull $0xca37, %ecx, %ecx     # imm = 0xCA37
addl $0x11677, %ecx          # imm = 0x11677
imull %ecx, %eax
addl $0x894d, %eax           # imm = 0x894D
andl $0xffffff, %eax         # imm = 0xFFFFFF
movl %eax, -0x418d8(%rbp)
jmp 0x297c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x13a0(%rbp)
movl %eax, -0x13a4(%rbp)
jmp 0x2a3d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x13a0(%rbp)
movl %eax, -0x13a4(%rbp)
leaq -0x1708(%rbp), %rdi
callq 0x20f0
leaq -0x1709(%rbp), %rdi
callq 0x2200
jmp 0x2cf7
leaq -0x41a60(%rbp), %rdi
leaq 0x16624(%rip), %rsi     # 0x19080
movl $0x180, %edx            # imm = 0x180
callq 0x2160
movl $0x0, -0x41a64(%rbp)
cmpl $0xc0, -0x41a64(%rbp)
jge 0x2ab1
movslq -0x41a64(%rbp), %rax
movw -0x41a60(%rbp,%rax,2), %cx
movl -0x41a64(%rbp), %eax
addl $0xff3f, %eax           # imm = 0xFF3F
cltq
movw %cx, -0x418d0(%rbp,%rax,2)
movl -0x41a64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x41a64(%rbp)
jmp 0x2a70
leaq -0x41a89(%rbp), %rdi
movq %rdi, -0x81c08(%rbp)
callq 0x22d0
movq -0x81c08(%rbp), %rdx
leaq 0x167f9(%rip), %rsi     # 0x192cb
leaq -0x41a88(%rbp), %rdi
callq 0x48a0
jmp 0x2ae0
leaq -0x41a88(%rbp), %rdi
leaq -0x188c(%rbp), %rsi
leaq -0x218d0(%rbp), %rdx
leaq -0x418d0(%rbp), %rcx
leaq -0x13c0(%rbp), %r8
callq 0x7bd0
jmp 0x2b0a
leaq -0x41a88(%rbp), %rdi
callq 0x20f0
leaq -0x41a89(%rbp), %rdi
callq 0x2200
leaq -0x41b0c(%rbp), %rdi
callq 0x9150
jmp 0x2b30
movl $0x0, -0x81b14(%rbp)
cmpl $0xffbf, -0x81b14(%rbp) # imm = 0xFFBF
jge 0x2bc4
movl -0x81b14(%rbp), %eax
addl $0x1, %eax
movw %ax, %cx
movslq -0x81b14(%rbp), %rax
movw %cx, -0x61b10(%rbp,%rax,2)
movslq -0x81b14(%rbp), %rax
movw %cx, -0x81b10(%rbp,%rax,2)
movl -0x81b14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x81b14(%rbp)
jmp 0x2b3a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x13a0(%rbp)
movl %eax, -0x13a4(%rbp)
jmp 0x2bb3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x13a0(%rbp)
movl %eax, -0x13a4(%rbp)
leaq -0x41a88(%rbp), %rdi
callq 0x20f0
leaq -0x41a89(%rbp), %rdi
callq 0x2200
jmp 0x2cf7
leaq -0x81ba0(%rbp), %rdi
leaq 0x1662e(%rip), %rsi     # 0x19200
movl $0x80, %edx
callq 0x2160
movl $0x0, -0x81ba4(%rbp)
cmpl $0x40, -0x81ba4(%rbp)
jge 0x2c24
movslq -0x81ba4(%rbp), %rax
movw -0x81ba0(%rbp,%rax,2), %cx
movl -0x81ba4(%rbp), %eax
addl $0xffbf, %eax           # imm = 0xFFBF
cltq
movw %cx, -0x81b10(%rbp,%rax,2)
movl -0x81ba4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x81ba4(%rbp)
jmp 0x2be6
leaq -0x81bc9(%rbp), %rdi
movq %rdi, -0x81c10(%rbp)
callq 0x22d0
movq -0x81c10(%rbp), %rdx
leaq 0x166a9(%rip), %rsi     # 0x192ee
leaq -0x81bc8(%rbp), %rdi
callq 0x48a0
jmp 0x2c53
leaq -0x81bc8(%rbp), %rdi
leaq -0x41b0c(%rbp), %rsi
leaq -0x61b10(%rbp), %rdx
leaq -0x81b10(%rbp), %rcx
leaq -0x13c0(%rbp), %r8
callq 0x9550
jmp 0x2c7d
leaq -0x81bc8(%rbp), %rdi
callq 0x20f0
leaq -0x81bc9(%rbp), %rdi
callq 0x2200
leaq -0x13c0(%rbp), %rdi
callq 0xaab0
leaq -0x1390(%rbp), %rdi
callq 0xab10
movl -0x4(%rbp), %eax
addq $0x81c10, %rsp          # imm = 0x81C10
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x13a0(%rbp)
movl %eax, -0x13a4(%rbp)
jmp 0x2ceb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x13a0(%rbp)
movl %eax, -0x13a4(%rbp)
leaq -0x81bc8(%rbp), %rdi
callq 0x20f0
leaq -0x81bc9(%rbp), %rdi
callq 0x2200
leaq -0x13c0(%rbp), %rdi
callq 0xaab0
leaq -0x1390(%rbp), %rdi
callq 0xab10
movq -0x13a0(%rbp), %rdi
callq 0x22c0
nopl (%rax,%rax)