// File: JMMackenzie[P]binary_to_jass/compress_integer_elias_gamma_simd.cpp
// Function: JASS::compress_integer_elias_gamma_simd::encode(void*, unsigned long, unsigned int const*, unsigned long)
size_t compress_integer_elias_gamma_simd::encode(void *encoded, size_t encoded_buffer_length, const integer *array, size_t elements)
		{
		uint8_t encodings[33] = {0};
		uint32_t *destination = (uint32_t *)encoded;
		uint32_t *end_of_destination = (uint32_t *)((uint8_t *)encoded + encoded_buffer_length);
		uint32_t carryover = 0;
		uint32_t actual_max_width = 0;

		while (1)
			{
			/*
				Check for overflow of the output buffer
			*/
			if (destination + WORDS + 1 + 1 > end_of_destination)
				return 0;

			/*
				Zero the result memory before writing the bit paterns into it
			*/
			memset(destination, 0, (WORDS + 1) * 4);

			/*
				Get the address of the selector and move on to the payload
			*/
			uint32_t *selector = destination;
			destination++;

			/*
				Encode the next integer
			*/
			uint32_t remaining = 32;
			uint32_t cumulative_shift = 0;
			bool overflow = false;
			uint32_t integers_encoded = 0;

			/*
				Find the width of this column
			*/
			for (uint32_t slice = 0; slice < 32; slice++)
				{
				uint32_t row_start = integers_encoded;
				uint32_t max_width = 0;
				for (uint32_t word = 0; word < WORDS; word++)
					{
					size_t index = slice * WORDS + word;
					uint32_t value;

					if (index < elements)
						{
						integers_encoded++;
						value = array[index];
						}
					else
						{
						overflow = true;
						value = 0;					// if we overflow then pad with 0s (so that we don't carryover stray bits)
						}

					uint32_t width = maths::ceiling_log2(value);
					width = std::max(width, static_cast<decltype(width)>(1));					// coz ffs(0) != 1.
					max_width = std::max(max_width, width);
					if (carryover == 0)
						destination[word] |= value << cumulative_shift;
					else
						destination[word] |= value & ~high_bits[32 - (actual_max_width - carryover)];
					}
				actual_max_width = max_width;
				max_width -= carryover;
				carryover = 0;
				cumulative_shift += max_width;

				if (max_width > remaining)
					{
					/*
						We can't fit this column so take the high bits of the next set of integers
					*/
					encodings[slice] = 0;
					integers_encoded = row_start;		// Wind back to the start of this row as its about to become the start of the next block.

					/*
						Encode the remaining high bits of the current integer set into the remaining space
					*/
					for (uint32_t word = 0; word < WORDS; word++)
						{
						size_t index = slice * WORDS + word;
						uint32_t value = index < elements ? array[index] : 0;
						destination[word] &= ~high_bits[remaining];
						uint32_t shift = actual_max_width - remaining;
						destination[word] |= (value >> shift) << (32 - remaining);
						}
					carryover = remaining;
					break;
					}
				else if (max_width == remaining || overflow || (slice + 1) * WORDS >= elements)
					{
					/*
						We've filled a word without carryover.
					*/
					encodings[slice] = max_width;
					encodings[slice + 1] = 0;
					break;
					}

				remaining -= max_width;
				encodings[slice] = max_width;
				}
			*selector = compute_selector(encodings);

			destination += WORDS;
			array += integers_encoded;
			elements -= integers_encoded;
			if (elements == 0)
				break;
			}

		return  (uint8_t *)destination - (uint8_t *)encoded;
		}

// Assembly:
subq $0xc8, %rsp
movq %rdi, 0xb8(%rsp)
movq %rsi, 0xb0(%rsp)
movq %rdx, 0xa8(%rsp)
movq %rcx, 0xa0(%rsp)
movq %r8, 0x98(%rsp)
leaq 0x70(%rsp), %rdi
xorl %esi, %esi
movl $0x21, %edx
callq 0x2100
movq 0xb0(%rsp), %rax
movq %rax, 0x68(%rsp)
movq 0xb0(%rsp), %rax
addq 0xa8(%rsp), %rax
movq %rax, 0x60(%rsp)
movl $0x0, 0x5c(%rsp)
movl $0x0, 0x58(%rsp)
movq 0x68(%rsp), %rax
addq $0x40, %rax
addq $0x4, %rax
addq $0x4, %rax
cmpq 0x60(%rsp), %rax
jbe 0x3001eb
movq $0x0, 0xc0(%rsp)
jmp 0x300561
movq 0x68(%rsp), %rdi
xorl %esi, %esi
movl $0x44, %edx
callq 0x2100
movq 0x68(%rsp), %rax
movq %rax, 0x50(%rsp)
movq 0x68(%rsp), %rax
addq $0x4, %rax
movq %rax, 0x68(%rsp)
movl $0x20, 0x4c(%rsp)
movl $0x0, 0x48(%rsp)
movb $0x0, 0x47(%rsp)
movl $0x0, 0x40(%rsp)
movl $0x0, 0x3c(%rsp)
cmpl $0x20, 0x3c(%rsp)
jae 0x3004e0
movl 0x40(%rsp), %eax
movl %eax, 0x38(%rsp)
movl $0x0, 0x34(%rsp)
movl $0x0, 0x30(%rsp)
cmpl $0x10, 0x30(%rsp)
jae 0x30035e
movl 0x3c(%rsp), %eax
shll $0x4, %eax
addl 0x30(%rsp), %eax
movl %eax, %eax
movq %rax, 0x28(%rsp)
movq 0x28(%rsp), %rax
cmpq 0x98(%rsp), %rax
jae 0x3002a9
movl 0x40(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x40(%rsp)
movq 0xa0(%rsp), %rax
movq 0x28(%rsp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, 0x24(%rsp)
jmp 0x3002b6
movb $0x1, 0x47(%rsp)
movl $0x0, 0x24(%rsp)
movl 0x24(%rsp), %edi
callq 0x300580
movl %eax, 0x20(%rsp)
movl $0x1, 0x1c(%rsp)
leaq 0x20(%rsp), %rdi
leaq 0x1c(%rsp), %rsi
callq 0x300d20
movl (%rax), %eax
movl %eax, 0x20(%rsp)
leaq 0x34(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x300d20
movl (%rax), %eax
movl %eax, 0x34(%rsp)
cmpl $0x0, 0x5c(%rsp)
jne 0x300317
movl 0x24(%rsp), %edx
movl 0x48(%rsp), %ecx
shll %cl, %edx
movq 0x68(%rsp), %rax
movl 0x30(%rsp), %ecx
orl (%rax,%rcx,4), %edx
movl %edx, (%rax,%rcx,4)
jmp 0x30034c
movl 0x24(%rsp), %edx
movl 0x58(%rsp), %ecx
subl 0x5c(%rsp), %ecx
movl $0x20, %eax
subl %ecx, %eax
movl %eax, %eax
movl %eax, %ecx
leaq 0x37cb(%rip), %rax      # 0x303b00
movl (%rax,%rcx,4), %eax
xorl $-0x1, %eax
andl %eax, %edx
movq 0x68(%rsp), %rax
movl 0x30(%rsp), %ecx
orl (%rax,%rcx,4), %edx
movl %edx, (%rax,%rcx,4)
jmp 0x30034e
movl 0x30(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x30(%rsp)
jmp 0x30025c
movl 0x34(%rsp), %eax
movl %eax, 0x58(%rsp)
movl 0x5c(%rsp), %ecx
movl 0x34(%rsp), %eax
subl %ecx, %eax
movl %eax, 0x34(%rsp)
movl $0x0, 0x5c(%rsp)
movl 0x34(%rsp), %eax
addl 0x48(%rsp), %eax
movl %eax, 0x48(%rsp)
movl 0x34(%rsp), %eax
cmpl 0x4c(%rsp), %eax
jbe 0x30046d
movl 0x3c(%rsp), %eax
movb $0x0, 0x70(%rsp,%rax)
movl 0x38(%rsp), %eax
movl %eax, 0x40(%rsp)
movl $0x0, 0x18(%rsp)
cmpl $0x10, 0x18(%rsp)
jae 0x300463
movl 0x3c(%rsp), %eax
shll $0x4, %eax
addl 0x18(%rsp), %eax
movl %eax, %eax
movq %rax, 0x10(%rsp)
movq 0x10(%rsp), %rax
cmpq 0x98(%rsp), %rax
jae 0x3003f1
movq 0xa0(%rsp), %rax
movq 0x10(%rsp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, 0x4(%rsp)
jmp 0x3003f9
xorl %eax, %eax
movl %eax, 0x4(%rsp)
jmp 0x3003f9
movl 0x4(%rsp), %eax
movl %eax, 0xc(%rsp)
movl 0x4c(%rsp), %eax
movl %eax, %ecx
leaq 0x36f2(%rip), %rax      # 0x303b00
movl (%rax,%rcx,4), %edx
xorl $-0x1, %edx
movq 0x68(%rsp), %rax
movl 0x18(%rsp), %ecx
andl (%rax,%rcx,4), %edx
movl %edx, (%rax,%rcx,4)
movl 0x58(%rsp), %eax
subl 0x4c(%rsp), %eax
movl %eax, 0x8(%rsp)
movl 0xc(%rsp), %edx
movl 0x8(%rsp), %ecx
shrl %cl, %edx
movl $0x20, %ecx
subl 0x4c(%rsp), %ecx
shll %cl, %edx
movq 0x68(%rsp), %rax
movl 0x18(%rsp), %ecx
orl (%rax,%rcx,4), %edx
movl %edx, (%rax,%rcx,4)
movl 0x18(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x18(%rsp)
jmp 0x3003af
movl 0x4c(%rsp), %eax
movl %eax, 0x5c(%rsp)
jmp 0x3004e0
movl 0x34(%rsp), %eax
cmpl 0x4c(%rsp), %eax
je 0x300494
testb $0x1, 0x47(%rsp)
jne 0x300494
movl 0x3c(%rsp), %eax
addl $0x1, %eax
shll $0x4, %eax
movl %eax, %eax
cmpq 0x98(%rsp), %rax
jb 0x3004b2
movl 0x34(%rsp), %eax
movb %al, %cl
movl 0x3c(%rsp), %eax
movb %cl, 0x70(%rsp,%rax)
movl 0x3c(%rsp), %eax
addl $0x1, %eax
movl %eax, %eax
movb $0x0, 0x70(%rsp,%rax)
jmp 0x3004e0
jmp 0x3004b4
movl 0x34(%rsp), %ecx
movl 0x4c(%rsp), %eax
subl %ecx, %eax
movl %eax, 0x4c(%rsp)
movl 0x34(%rsp), %eax
movb %al, %cl
movl 0x3c(%rsp), %eax
movb %cl, 0x70(%rsp,%rax)
movl 0x3c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x3c(%rsp)
jmp 0x300239
leaq 0x70(%rsp), %rdi
callq 0x3000b0
movl %eax, %ecx
movq 0x50(%rsp), %rax
movl %ecx, (%rax)
movq 0x68(%rsp), %rax
addq $0x40, %rax
movq %rax, 0x68(%rsp)
movl 0x40(%rsp), %ecx
movq 0xa0(%rsp), %rax
movl %ecx, %ecx
shlq $0x2, %rcx
addq %rcx, %rax
movq %rax, 0xa0(%rsp)
movl 0x40(%rsp), %eax
movl %eax, %ecx
movq 0x98(%rsp), %rax
subq %rcx, %rax
movq %rax, 0x98(%rsp)
cmpq $0x0, 0x98(%rsp)
jne 0x300544
jmp 0x300549
jmp 0x3001c2
movq 0x68(%rsp), %rax
movq 0xb0(%rsp), %rcx
subq %rcx, %rax
movq %rax, 0xc0(%rsp)
movq 0xc0(%rsp), %rax
addq $0xc8, %rsp
retq
nopw %cs:(%rax,%rax)