// File: /workspace/github_cpp_makefile_star2_O2/JMMackenzie[P]binary_to_jass/compress_integer_elias_gamma_simd_vb.cpp
// Function: JASS::compress_integer_elias_gamma_simd_vb::encode(void*, unsigned long, unsigned int const*, unsigned long)
size_t compress_integer_elias_gamma_simd_vb::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);

		/*
			Store the length of the variable byte part
		*/
		*destination++ = 0;			// store as a 4-byte integer to start with (variable byte encode it later)
		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 = WORD_WIDTH;
			uint32_t cumulative_shift = 0;
			const integer *array_at_start_of_codeword = array;
			size_t elements_at_start_of_codeword = elements;

			/*
				Pack into the next word
			*/
			uint32_t slice;
			for (slice = 0; slice < 32; slice++)
				{
				/*
					Find the width of this column
				*/
				uint32_t max_width = 1;
				for (uint32_t word = 0; word < WORDS; word++)
					max_width |= word < elements ? array[word] : 1;

				max_width = maths::ceiling_log2(max_width);

				if (max_width > remaining)
					break;			// move on to the next code word
				else
					{
					/*
						Pack them in because there's room
					*/
					encodings[slice] = max_width;
					for (uint32_t word = 0; word < WORDS; word++)
						destination[word] |= (word < elements ? array[word] : 0) << cumulative_shift;
					cumulative_shift += max_width;
					remaining -= max_width;

					/*
						Check for end of input
					*/
					if (WORDS >= elements)
						{
						/*
							Pack the last selector
						*/
						encodings[slice] += remaining;
						encodings[slice + 1] = 0;
						*selector = compute_selector(encodings);

						if (WORDS == elements)
							{
							uint32_t *vb_length_locaton = (uint32_t *)encoded;
							*vb_length_locaton = 0;
							return (uint8_t *)(destination + WORDS) - (uint8_t *)encoded;						// we've finished the encoding
							}
						else
							{
							/*
								We're at end of input so either re-code the final column as variable byte or re-code the entire word.
							*/

							/*
								Check to see how many bits of the current word we're using.
							*/
							size_t elias_size = WORDS * (WORD_WIDTH / 8);		// size (in bytes) of the codeword up to the last column
							for (size_t current = 0; current < elements; current++)
								elias_size += bytes_needed_for(array[current]);

							/*
								Check to see how much space it would take with variable byte
							*/
							size_t vbyte_size = 0;
							for (size_t current = 0; current < elements_at_start_of_codeword; current++)
								vbyte_size += bytes_needed_for(array_at_start_of_codeword[current]);

							if (vbyte_size < elias_size)
								{
								// re-code the codeword
								vbyte_size = compress_integer_variable_byte::encode(selector, end_of_destination - selector, array_at_start_of_codeword, elements_at_start_of_codeword);
								destination = selector;
								}
							else
								{
								// re-code the final column
								for (uint32_t word = 0; word < WORDS; word++)
									destination[word] ^= (word < elements ? array[word] : 0) << (cumulative_shift - max_width);// becuase we've alread incremente cumulative_shift
								destination += WORDS;
								encodings[slice - 1] += encodings[slice];
								encodings[slice] = 0;
								*selector = compute_selector(encodings);
								vbyte_size = compress_integer_variable_byte::encode(destination, end_of_destination - selector, array, elements);
								}
							uint32_t *vb_length_locaton = (uint32_t *)encoded;
							*vb_length_locaton = vbyte_size;

							return (uint8_t *)destination + vbyte_size - (uint8_t *)encoded;						// we've finished the encoding
							}
						}
					elements -= WORDS;
					array += WORDS;
					}
				}
			/*
				Didn't fit!  Finish this codeword by padding the last width to the full width of the block (i.e. add the spare bits)
			*/
			encodings[slice - 1] += remaining;
			encodings[slice] = 0;

			*selector = compute_selector(encodings);
			destination += WORDS;
			}

		return 0;			// can't happen
		}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %r8, 0x8(%rsp)
movq %rcx, (%rsp)
movq %rdi, 0x28(%rsp)
vxorps %xmm0, %xmm0, %xmm0
leaq 0x30(%rsp), %rax
vmovups %ymm0, (%rax)
movb $0x0, 0x20(%rax)
addq %rsi, %rdx
leaq 0x4(%rsi), %r9
andl $0x0, (%rsi)
movq %rsi, 0x10(%rsp)
leaq 0x8(%rsi), %r14
pushq $0x20
popq %r11
pushq $0x1
popq %r12
leaq 0x279d(%rip), %rbx      # 0x59bb0
movq %rdx, 0x20(%rsp)
leaq 0x48(%r9), %rax
cmpq %rdx, %rax
ja 0x576bd
andl $0x0, 0x40(%r9)
vmovups %ymm0, 0x20(%r9)
vmovups %ymm0, (%r9)
movq 0x8(%rsp), %rbp
leaq -0x200(%rbp), %rax
xorl %r10d, %r10d
movl %r11d, %ecx
xorl %r15d, %r15d
movq (%rsp), %r13
cmpq $0x20, %r10
je 0x57503
xorl %esi, %esi
movl %r12d, %edx
cmpq $0x10, %rsi
je 0x57477
movl %r12d, %edi
cmpq %rsi, %rbp
jbe 0x57470
movl (%r13,%rsi,4), %edi
orl %edi, %edx
incq %rsi
jmp 0x5745d
movl %edx, %esi
cmpl $0xff, %edx
ja 0x57487
movzbl (%rsi,%rbx), %edx
jmp 0x574b9
cmpl $0xffff, %edx           # imm = 0xFFFF
ja 0x5749b
shrl $0x8, %edx
movzbl (%rdx,%rbx), %edx
addl $0x8, %edx
jmp 0x574b9
cmpl $0xffffff, %edx         # imm = 0xFFFFFF
ja 0x574af
shrl $0x10, %esi
movzbl (%rsi,%rbx), %edx
addl $0x10, %edx
jmp 0x574b9
shrl $0x18, %esi
movzbl (%rsi,%rbx), %edx
addl $0x18, %edx
movl %ecx, %esi
subl %edx, %esi
jb 0x57511
movb %dl, 0x30(%rsp,%r10)
xorl %edi, %edi
cmpq $0x10, %rdi
je 0x574e8
xorl %r8d, %r8d
cmpq %rdi, %rbp
jbe 0x574d9
movl (%r13,%rdi,4), %r8d
movl %r15d, %ecx
shll %cl, %r8d
orl %r8d, (%r14,%rdi,4)
incq %rdi
jmp 0x574c6
cmpq $0x11, %rbp
jb 0x57563
addl %edx, %r15d
addq $-0x10, %rbp
addq $0x40, %r13
incq %r10
movl %esi, %ecx
jmp 0x5744e
movq %r9, %r15
movq %rax, %rbp
movl %r11d, %ebx
movl %r11d, %r10d
jmp 0x57517
movl %r11d, %ebx
movq %r9, %r15
leal -0x1(%r10), %eax
addb %cl, 0x30(%rsp,%rax)
movl %r10d, %eax
movb $0x0, 0x30(%rsp,%rax)
leaq 0x30(%rsp), %rdi
vzeroupper
callq 0x57398
movq %r15, %r9
movl %eax, (%r15)
addq $0x44, %r9
addq $0x44, %r14
movq %rbp, 0x8(%rsp)
movq %r13, (%rsp)
movq 0x20(%rsp), %rdx
vxorps %xmm0, %xmm0, %xmm0
movl %ebx, %r11d
leaq 0x2652(%rip), %rbx      # 0x59bb0
jmp 0x57418
addb %dl, %sil
movb %sil, 0x30(%rsp,%r10)
movq %r10, 0x60(%rsp)
leal 0x1(%r10), %eax
movb $0x0, 0x30(%rsp,%rax)
leaq 0x30(%rsp), %rdi
movq %r9, 0x18(%rsp)
vzeroupper
callq 0x57398
movq 0x18(%rsp), %rcx
movl %eax, (%rcx)
cmpq $0x10, %rbp
jne 0x575ac
movq 0x10(%rsp), %rax
andl $0x0, (%rax)
subq %rax, %rcx
addq $0x44, %rcx
jmp 0x576a8
pushq $0x40
popq %rbx
xorl %r12d, %r12d
cmpq %r12, %rbp
je 0x575c9
movl (%r13,%r12,4), %edi
callq 0x57752
addq %rax, %rbx
incq %r12
jmp 0x575b2
movq %rbx, 0x58(%rsp)
xorl %r12d, %r12d
xorl %ebx, %ebx
cmpq %rbx, 0x8(%rsp)
je 0x575ee
movq (%rsp), %rax
movl (%rax,%rbx,4), %edi
callq 0x57752
addq %rax, %r12
incq %rbx
jmp 0x575d3
cmpq 0x58(%rsp), %r12
jae 0x57626
movq 0x18(%rsp), %rbx
movq 0x20(%rsp), %rdx
subq %rbx, %rdx
sarq $0x2, %rdx
movq 0x28(%rsp), %rdi
movq %rbx, %rsi
movq (%rsp), %rcx
movq 0x8(%rsp), %r8
callq 0x57884
movq %rbx, %rcx
movq 0x10(%rsp), %rbx
jmp 0x576a0
xorl %eax, %eax
movq 0x10(%rsp), %rbx
movq 0x18(%rsp), %rsi
movq 0x60(%rsp), %rdi
cmpq $0x10, %rax
je 0x5765a
xorl %edx, %edx
cmpq %rax, %rbp
jbe 0x57649
movl (%r13,%rax,4), %edx
movl %r15d, %ecx
shll %cl, %edx
xorl %edx, (%r14)
incq %rax
addq $0x4, %r14
jmp 0x57637
leaq 0x44(%rsi), %r14
movb 0x30(%rsp,%rdi), %al
leal -0x1(%rdi), %ecx
addb %al, 0x30(%rsp,%rcx)
movb $0x0, 0x30(%rsp,%rdi)
leaq 0x30(%rsp), %rdi
movq %rsi, %r15
callq 0x57398
movl %eax, (%r15)
movq 0x20(%rsp), %rdx
subq %r15, %rdx
sarq $0x2, %rdx
movq 0x28(%rsp), %rdi
movq %r14, %rsi
movq %r13, %rcx
movq %rbp, %r8
callq 0x57884
movq %r14, %rcx
movl %eax, (%rbx)
addq %rax, %rcx
subq %rbx, %rcx
movq %rcx, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
vzeroupper
retq
xorl %ecx, %ecx
jmp 0x576a8
nop