// File: /workspace/github_cpp_makefile_star2_O1/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
movq %rcx, %rdi
vxorps %xmm0, %xmm0, %xmm0
vmovups %ymm0, -0x28(%rsp)
movb $0x0, -0x8(%rsp)
addq %rsi, %rdx
movq %rdx, -0x38(%rsp)
leaq 0x27f7(%rip), %rbx      # 0x5a2f0
leaq 0x1ec0(%rip), %r14      # 0x599c0
xorl %ebp, %ebp
xorl %r11d, %r11d
movq %rsi, -0x40(%rsp)
movq %rsi, %r15
leaq 0x48(%r15), %rax
cmpq -0x38(%rsp), %rax
ja 0x57d4f
vmovups %ymm0, 0x20(%r15)
vmovups %ymm0, (%r15)
movl $0x0, 0x40(%r15)
movl $0x20, %edx
xorl %r12d, %r12d
movq %rdi, -0x30(%rsp)
movq %rdi, %r13
movq $0x0, -0x50(%rsp)
xorl %esi, %esi
xorl %edi, %edi
xorl %r9d, %r9d
movl %esi, %r10d
movl %esi, -0x44(%rsp)
movl %edx, -0x48(%rsp)
movl %r11d, %edx
subl %ebp, %edx
addl $0x20, %edx
xorl %esi, %esi
xorl %ebp, %ebp
leaq (%r12,%rsi), %rax
cmpq %rax, %r8
jbe 0x57b79
incl %r10d
movl (%r13,%rsi,4), %ecx
jmp 0x57b7e
xorl %ecx, %ecx
movb $0x1, %dil
movl %ecx, %eax
cmpl $0xff, %ecx
ja 0x57b8e
movzbl (%rax,%rbx), %ecx
jmp 0x57bc0
cmpl $0xffff, %eax           # imm = 0xFFFF
ja 0x57ba1
shrl $0x8, %ecx
movzbl (%rcx,%rbx), %ecx
addl $0x8, %ecx
jmp 0x57bc0
movl %eax, %ecx
cmpl $0xffffff, %eax         # imm = 0xFFFFFF
ja 0x57bb6
shrl $0x10, %ecx
movzbl (%rcx,%rbx), %ecx
addl $0x10, %ecx
jmp 0x57bc0
shrl $0x18, %ecx
movzbl (%rcx,%rbx), %ecx
addl $0x18, %ecx
cmpl %ebp, %ecx
cmoval %ecx, %ebp
cmpl $0x1, %ebp
adcl $0x0, %ebp
testl %r11d, %r11d
je 0x57bdc
movl (%r14,%rdx,4), %ecx
notl %ecx
andl %eax, %ecx
movl %ecx, %eax
jmp 0x57be1
movl %r9d, %ecx
shll %cl, %eax
orl %eax, 0x4(%r15,%rsi,4)
incq %rsi
cmpq $0x10, %rsi
jne 0x57b66
movl %ebp, %eax
subl %r11d, %eax
movl -0x48(%rsp), %edx
movl %edx, %ecx
movl %eax, -0x54(%rsp)
subl %eax, %ecx
jae 0x57c79
movq -0x50(%rsp), %rax
movb $0x0, -0x28(%rsp,%rax)
movl %edx, %eax
movl (%r14,%rax,4), %eax
notl %eax
movl %ebp, %r10d
subl %edx, %r10d
movl $0x20, %r11d
subl %edx, %r11d
xorl %esi, %esi
leaq (%r12,%rsi), %rcx
xorl %r14d, %r14d
cmpq %rcx, %r8
jbe 0x57c3a
movl (%r13,%rsi,4), %r14d
movl 0x4(%r15,%rsi,4), %edx
andl %eax, %edx
movl %r10d, %ecx
shrl %cl, %r14d
movl %r11d, %ecx
shll %cl, %r14d
orl %edx, %r14d
movl %r14d, 0x4(%r15,%rsi,4)
incq %rsi
cmpq $0x10, %rsi
jne 0x57c29
xorl %eax, %eax
movl -0x44(%rsp), %r10d
movl -0x48(%rsp), %edx
movl %edx, %r11d
leaq 0x1d4d(%rip), %r14      # 0x599c0
movl -0x54(%rsp), %ecx
jmp 0x57cc5
je 0x57ca7
testb $0x1, %dil
jne 0x57ca7
movq -0x50(%rsp), %rax
shlq $0x4, %rax
addq $0x10, %rax
cmpq %rax, %r8
jbe 0x57ca7
movb $0x1, %al
movq -0x50(%rsp), %rsi
movl -0x54(%rsp), %r11d
movl %ecx, %edx
movl -0x54(%rsp), %ecx
jmp 0x57cbd
movq -0x50(%rsp), %rax
movl -0x54(%rsp), %ecx
movb %cl, -0x28(%rsp,%rax)
leaq 0x1(%rax), %rsi
xorl %r11d, %r11d
xorl %eax, %eax
movb %r11b, -0x28(%rsp,%rsi)
xorl %r11d, %r11d
testb %al, %al
je 0x57cef
addl %ecx, %r9d
movq -0x50(%rsp), %rcx
leaq 0x1(%rcx), %rax
addq $0x10, %r12
addq $0x40, %r13
movl %r10d, %esi
cmpq $0x1f, %rcx
movq %rax, -0x50(%rsp)
jb 0x57b52
xorl %eax, %eax
movq -0x30(%rsp), %rdi
cmpb $0x0, -0x28(%rsp,%rax)
je 0x57d0b
incq %rax
cmpq $0x20, %rax
jne 0x57cf6
movl $0x20, %eax
testl %eax, %eax
jle 0x57d2c
movl %eax, %edx
incq %rdx
xorl %eax, %eax
movb -0x2a(%rsp,%rdx), %cl
shll %cl, %eax
decb %cl
btsl %ecx, %eax
decq %rdx
cmpq $0x1, %rdx
jg 0x57d16
jmp 0x57d2e
xorl %eax, %eax
movl %eax, (%r15)
addq $0x44, %r15
movl %r10d, %eax
leaq (%rdi,%rax,4), %rdi
subq %rax, %r8
jne 0x57b0d
subq -0x40(%rsp), %r15
movq %r15, %rax
jmp 0x57d51
xorl %eax, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
vzeroupper
retq
nop