// File: /workspace/github_cpp_makefile_star2_O3/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %r8, %rbx
movq %rsi, %rbp
vxorps %xmm0, %xmm0, %xmm0
leaq 0x30(%rsp), %rax
vmovups %ymm0, (%rax)
movb $0x0, 0x20(%rax)
addq %rsi, %rdx
movq %rdx, 0x20(%rsp)
leaq 0x270a(%rip), %r13      # 0x5a2f0
xorl %r12d, %r12d
xorl %r15d, %r15d
movq %rsi, 0x18(%rsp)
leaq 0x48(%rbp), %rax
cmpq 0x20(%rsp), %rax
ja 0x57dfb
vxorps %xmm0, %xmm0, %xmm0
vmovups %ymm0, 0x20(%rbp)
vmovups %ymm0, (%rbp)
movl $0x0, 0x40(%rbp)
movl $0x20, 0x8(%rsp)
xorl %esi, %esi
movq %rcx, 0x28(%rsp)
movq %rcx, %rdi
xorl %r9d, %r9d
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r14d, %r14d
movl %edx, 0xc(%rsp)
movq %r9, 0x10(%rsp)
movq %r9, %rdx
shlq $0x4, %rdx
movl %r15d, %r11d
subl %r12d, %r11d
addl $0x20, %r11d
xorl %r9d, %r9d
xorl %r10d, %r10d
xorl %r12d, %r12d
leaq (%rsi,%r9), %rax
cmpq %rax, %rbx
jbe 0x57c86
incl %r14d
movl (%rdi,%r9,4), %r8d
cmpq $0xff, %r8
jbe 0x57c8c
movl %r8d, %eax
cmpl $0xffff, %r8d           # imm = 0xFFFF
ja 0x57cca
shrl $0x8, %eax
movzbl (%rax,%r13), %eax
addl $0x8, %eax
jmp 0x57c91
movb $0x1, %r10b
xorl %r8d, %r8d
movzbl (%r8,%r13), %eax
cmpl %r12d, %eax
cmoval %eax, %r12d
cmpl $0x1, %r12d
adcl $0x0, %r12d
testl %r15d, %r15d
je 0x57cb7
leaq 0x1d14(%rip), %rax      # 0x599c0
movl (%rax,%r11,4), %eax
notl %eax
andl %eax, %r8d
jmp 0x57cba
shll %cl, %r8d
orl %r8d, 0x4(%rbp,%r9,4)
incq %r9
cmpq $0x10, %r9
jne 0x57c54
jmp 0x57ced
cmpl $0xffffff, %r8d         # imm = 0xFFFFFF
ja 0x57ce0
shrl $0x10, %eax
movzbl (%rax,%r13), %eax
addl $0x10, %eax
jmp 0x57c91
shrl $0x18, %eax
movzbl (%rax,%r13), %eax
addl $0x18, %eax
jmp 0x57c91
movl %r12d, %r9d
subl %r15d, %r9d
addl %r9d, %ecx
movl 0x8(%rsp), %r15d
movl %r15d, %r8d
subl %r9d, %r8d
jb 0x57d51
sete %r11b
orb %r10b, %r11b
movq 0x10(%rsp), %r10
leaq 0x1(%r10), %rax
testb $0x1, %r11b
jne 0x57db6
addq $0x10, %rdx
cmpq %rdx, %rbx
jbe 0x57db6
movb %r9b, 0x30(%rsp,%r10)
addq $0x10, %rsi
addq $0x40, %rdi
xorl %r15d, %r15d
movq %rax, %r9
movl %r14d, %edx
movl %r8d, 0x8(%rsp)
cmpq $0x20, %rax
jne 0x57c31
jmp 0x57dc8
movl 0x10(%rsp), %eax
movb $0x0, 0x30(%rsp,%rax)
movl %r15d, %eax
leaq 0x1c5c(%rip), %rcx      # 0x599c0
movl (%rcx,%rax,4), %r8d
notl %r8d
movl %r12d, %eax
subl %r15d, %eax
movl $0x20, %edx
subl %r15d, %edx
xorl %r9d, %r9d
leaq (%rsi,%r9), %rcx
xorl %r10d, %r10d
cmpq %rcx, %rbx
jbe 0x57d8c
movl (%rdi,%r9,4), %r10d
movl 0x4(%rbp,%r9,4), %r11d
andl %r8d, %r11d
movl %eax, %ecx
shrl %cl, %r10d
movl %edx, %ecx
shll %cl, %r10d
orl %r11d, %r10d
movl %r10d, 0x4(%rbp,%r9,4)
incq %r9
cmpq $0x10, %r9
jne 0x57d7c
movl 0xc(%rsp), %r14d
jmp 0x57dc8
movl %r10d, %ecx
movb %r9b, 0x30(%rsp,%rcx)
movl %eax, %eax
movb $0x0, 0x30(%rsp,%rax)
xorl %r15d, %r15d
leaq 0x30(%rsp), %rdi
vzeroupper
callq 0x57b78
movl %eax, (%rbp)
addq $0x44, %rbp
movl %r14d, %eax
movq 0x28(%rsp), %rcx
leaq (%rcx,%rax,4), %rcx
subq %rax, %rbx
jne 0x57bf1
subq 0x18(%rsp), %rbp
movq %rbp, %rax
jmp 0x57dfd
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
vzeroupper
retq
nop