// File: /workspace/github_cpp_makefile_star2_O2/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 0x21ac(%rip), %r13      # 0x59230
xorl %r12d, %r12d
xorl %r15d, %r15d
movq %rsi, 0x18(%rsp)
leaq 0x48(%rbp), %rax
cmpq 0x20(%rsp), %rax
ja 0x5728e
andl $0x0, 0x40(%rbp)
vxorps %xmm0, %xmm0, %xmm0
vmovups %ymm0, 0x20(%rbp)
vmovups %ymm0, (%rbp)
xorl %esi, %esi
movq %rcx, 0x28(%rsp)
movq %rcx, %rdi
xorl %edx, %edx
pushq $0x20
popq %rax
movl %eax, 0x8(%rsp)
xorl %ecx, %ecx
xorl %r14d, %r14d
cmpq $0x20, %rdx
je 0x5725b
movq %rdx, 0x10(%rsp)
shlq $0x4, %rdx
movl %r15d, %r11d
subl %r12d, %r11d
addl $0x20, %r11d
xorl %r9d, %r9d
xorl %r12d, %r12d
xorl %r10d, %r10d
movl %r14d, 0xc(%rsp)
cmpq $0x10, %r9
je 0x57190
leaq (%rsi,%r9), %rax
cmpq %rax, %rbx
jbe 0x5712f
incl %r14d
movl (%rdi,%r9,4), %r8d
cmpq $0xff, %r8
jbe 0x57135
movl %r8d, %eax
cmpl $0xffff, %r8d           # imm = 0xFFFF
ja 0x5716d
shrl $0x8, %eax
movzbl (%rax,%r13), %eax
addl $0x8, %eax
jmp 0x5713a
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 0x57160
leaq 0x17ab(%rip), %rax      # 0x58900
movl (%rax,%r11,4), %eax
notl %eax
andl %eax, %r8d
jmp 0x57163
shll %cl, %r8d
orl %r8d, 0x4(%rbp,%r9,4)
incq %r9
jmp 0x570f3
cmpl $0xffffff, %r8d         # imm = 0xFFFFFF
ja 0x57183
shrl $0x10, %eax
movzbl (%rax,%r13), %eax
addl $0x10, %eax
jmp 0x5713a
shrl $0x18, %eax
movzbl (%rax,%r13), %eax
addl $0x18, %eax
jmp 0x5713a
movl %r12d, %r8d
subl %r15d, %r8d
addl %r8d, %ecx
movl 0x8(%rsp), %r15d
movl %r15d, %eax
subl %r8d, %eax
jb 0x571e5
sete %r11b
orb %r10b, %r11b
movq 0x10(%rsp), %r10
leaq 0x1(%r10), %r9
testb $0x1, %r11b
jne 0x57248
addq $0x10, %rdx
cmpq %rdx, %rbx
jbe 0x57248
movb %r8b, 0x30(%rsp,%r10)
addq $0x10, %rsi
addq $0x40, %rdi
xorl %r15d, %r15d
movq %r9, %rdx
movl %eax, 0x8(%rsp)
jmp 0x570c8
movl 0x10(%rsp), %eax
movb $0x0, 0x30(%rsp,%rax)
movl %r15d, %eax
leaq 0x1708(%rip), %rcx      # 0x58900
movl (%rcx,%rax,4), %r8d
notl %r8d
movl %r12d, %eax
subl %r15d, %eax
pushq $0x20
popq %rdx
subl %r15d, %edx
xorl %r9d, %r9d
movl 0xc(%rsp), %r14d
cmpq $0x10, %r9
je 0x5725b
leaq (%rsi,%r9), %rcx
xorl %r10d, %r10d
cmpq %rcx, %rbx
jbe 0x57229
movl (%rdi,%r9,4), %r10d
movl 0x4(%rbp,%r9,4), %r11d
movl %eax, %ecx
shrl %cl, %r10d
movl %edx, %ecx
shll %cl, %r10d
andl %r8d, %r11d
orl %r11d, %r10d
movl %r10d, 0x4(%rbp,%r9,4)
incq %r9
jmp 0x57213
movl %r10d, %eax
movb %r8b, 0x30(%rsp,%rax)
movl %r9d, %eax
movb $0x0, 0x30(%rsp,%rax)
xorl %r15d, %r15d
leaq 0x30(%rsp), %rdi
vzeroupper
callq 0x57024
movl %eax, (%rbp)
addq $0x44, %rbp
movl %r14d, %eax
movq 0x28(%rsp), %rcx
leaq (%rcx,%rax,4), %rcx
subq %rax, %rbx
jne 0x5708f
subq 0x18(%rsp), %rbp
movq %rbp, %rax
jmp 0x57290
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
vzeroupper
retq