// File: 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:
subq $0xe8, %rsp
movq %rdi, 0xd8(%rsp)
movq %rsi, 0xd0(%rsp)
movq %rdx, 0xc8(%rsp)
movq %rcx, 0xc0(%rsp)
movq %r8, 0xb8(%rsp)
movq 0xd8(%rsp), %rax
movq %rax, 0x10(%rsp)
leaq 0x90(%rsp), %rdi
xorl %esi, %esi
movl $0x21, %edx
callq 0x2100
movq 0xd0(%rsp), %rax
movq %rax, 0x88(%rsp)
movq 0xd0(%rsp), %rax
addq 0xc8(%rsp), %rax
movq %rax, 0x80(%rsp)
movq 0x88(%rsp), %rax
movq %rax, %rcx
addq $0x4, %rcx
movq %rcx, 0x88(%rsp)
movl $0x0, (%rax)
movq 0x88(%rsp), %rax
addq $0x40, %rax
addq $0x4, %rax
addq $0x4, %rax
cmpq 0x80(%rsp), %rax
jbe 0x300fa4
movq $0x0, 0xe0(%rsp)
jmp 0x301477
movq 0x88(%rsp), %rdi
xorl %esi, %esi
movl $0x44, %edx
callq 0x2100
movq 0x88(%rsp), %rax
movq %rax, 0x78(%rsp)
movq 0x88(%rsp), %rax
addq $0x4, %rax
movq %rax, 0x88(%rsp)
movl $0x20, 0x74(%rsp)
movl $0x0, 0x70(%rsp)
movq 0xc0(%rsp), %rax
movq %rax, 0x68(%rsp)
movq 0xb8(%rsp), %rax
movq %rax, 0x60(%rsp)
movl $0x0, 0x5c(%rsp)
cmpl $0x20, 0x5c(%rsp)
jae 0x30141e
movl $0x1, 0x58(%rsp)
movl $0x0, 0x54(%rsp)
cmpl $0x10, 0x54(%rsp)
jae 0x301074
movl 0x54(%rsp), %eax
cmpq 0xb8(%rsp), %rax
jae 0x301050
movq 0xc0(%rsp), %rax
movl 0x54(%rsp), %ecx
movl (%rax,%rcx,4), %eax
movl %eax, 0xc(%rsp)
jmp 0x30105b
movl $0x1, %eax
movl %eax, 0xc(%rsp)
jmp 0x30105b
movl 0xc(%rsp), %eax
orl 0x58(%rsp), %eax
movl %eax, 0x58(%rsp)
movl 0x54(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x54(%rsp)
jmp 0x301026
movl 0x58(%rsp), %edi
callq 0x301490
movl %eax, 0x58(%rsp)
movl 0x58(%rsp), %eax
cmpl 0x74(%rsp), %eax
jbe 0x301090
jmp 0x30141e
movl 0x58(%rsp), %eax
movb %al, %cl
movl 0x5c(%rsp), %eax
movb %cl, 0x90(%rsp,%rax)
movl $0x0, 0x50(%rsp)
cmpl $0x10, 0x50(%rsp)
jae 0x301104
movl 0x50(%rsp), %eax
cmpq 0xb8(%rsp), %rax
jae 0x3010d3
movq 0xc0(%rsp), %rax
movl 0x50(%rsp), %ecx
movl (%rax,%rcx,4), %eax
movl %eax, 0x8(%rsp)
jmp 0x3010db
xorl %eax, %eax
movl %eax, 0x8(%rsp)
jmp 0x3010db
movl 0x8(%rsp), %edx
movl 0x70(%rsp), %ecx
shll %cl, %edx
movq 0x88(%rsp), %rax
movl 0x50(%rsp), %ecx
orl (%rax,%rcx,4), %edx
movl %edx, (%rax,%rcx,4)
movl 0x50(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x50(%rsp)
jmp 0x3010a9
movl 0x58(%rsp), %eax
addl 0x70(%rsp), %eax
movl %eax, 0x70(%rsp)
movl 0x58(%rsp), %ecx
movl 0x74(%rsp), %eax
subl %ecx, %eax
movl %eax, 0x74(%rsp)
movl $0x10, %eax
cmpq 0xb8(%rsp), %rax
jb 0x3013e4
movl 0x74(%rsp), %edx
movl 0x5c(%rsp), %eax
movzbl 0x90(%rsp,%rax), %ecx
addl %edx, %ecx
movb %cl, 0x90(%rsp,%rax)
movl 0x5c(%rsp), %eax
addl $0x1, %eax
movl %eax, %eax
movb $0x0, 0x90(%rsp,%rax)
leaq 0x90(%rsp), %rdi
callq 0x300e40
movl %eax, %ecx
movq 0x78(%rsp), %rax
movl %ecx, (%rax)
movl $0x10, %eax
cmpq 0xb8(%rsp), %rax
jne 0x3011bc
movq 0xd0(%rsp), %rax
movq %rax, 0x48(%rsp)
movq 0x48(%rsp), %rax
movl $0x0, (%rax)
movq 0x88(%rsp), %rax
addq $0x40, %rax
movq 0xd0(%rsp), %rcx
subq %rcx, %rax
movq %rax, 0xe0(%rsp)
jmp 0x301477
movq $0x40, 0x40(%rsp)
movq $0x0, 0x38(%rsp)
movq 0x38(%rsp), %rax
cmpq 0xb8(%rsp), %rax
jae 0x30120c
movq 0xc0(%rsp), %rax
movq 0x38(%rsp), %rcx
movl (%rax,%rcx,4), %edi
callq 0x301990
addq 0x40(%rsp), %rax
movq %rax, 0x40(%rsp)
movq 0x38(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x38(%rsp)
jmp 0x3011ce
movq $0x0, 0x30(%rsp)
movq $0x0, 0x28(%rsp)
movq 0x28(%rsp), %rax
cmpq 0x60(%rsp), %rax
jae 0x301256
movq 0x68(%rsp), %rax
movq 0x28(%rsp), %rcx
movl (%rax,%rcx,4), %edi
callq 0x301990
addq 0x30(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x28(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x28(%rsp)
jmp 0x30121e
movq 0x30(%rsp), %rax
cmpq 0x40(%rsp), %rax
jae 0x3012a6
movq 0x10(%rsp), %rdi
movq 0x78(%rsp), %rsi
movq 0x80(%rsp), %rdx
movq 0x78(%rsp), %rax
subq %rax, %rdx
sarq $0x2, %rdx
movq 0x68(%rsp), %rcx
movq 0x60(%rsp), %r8
callq 0x301dc0
movq %rax, 0x30(%rsp)
movq 0x78(%rsp), %rax
movq %rax, 0x88(%rsp)
jmp 0x3013a4
movl $0x0, 0x24(%rsp)
cmpl $0x10, 0x24(%rsp)
jae 0x30130d
movl 0x24(%rsp), %eax
cmpq 0xb8(%rsp), %rax
jae 0x3012d8
movq 0xc0(%rsp), %rax
movl 0x24(%rsp), %ecx
movl (%rax,%rcx,4), %eax
movl %eax, 0x4(%rsp)
jmp 0x3012e0
xorl %eax, %eax
movl %eax, 0x4(%rsp)
jmp 0x3012e0
movl 0x4(%rsp), %edx
movl 0x70(%rsp), %ecx
subl 0x58(%rsp), %ecx
shll %cl, %edx
movq 0x88(%rsp), %rax
movl 0x24(%rsp), %ecx
xorl (%rax,%rcx,4), %edx
movl %edx, (%rax,%rcx,4)
movl 0x24(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x24(%rsp)
jmp 0x3012ae
movq 0x88(%rsp), %rax
addq $0x40, %rax
movq %rax, 0x88(%rsp)
movl 0x5c(%rsp), %eax
movzbl 0x90(%rsp,%rax), %edx
movl 0x5c(%rsp), %eax
subl $0x1, %eax
movl %eax, %eax
movzbl 0x90(%rsp,%rax), %ecx
addl %edx, %ecx
movb %cl, 0x90(%rsp,%rax)
movl 0x5c(%rsp), %eax
movb $0x0, 0x90(%rsp,%rax)
leaq 0x90(%rsp), %rdi
callq 0x300e40
movq 0x10(%rsp), %rdi
movl %eax, %ecx
movq 0x78(%rsp), %rax
movl %ecx, (%rax)
movq 0x88(%rsp), %rsi
movq 0x80(%rsp), %rdx
movq 0x78(%rsp), %rax
subq %rax, %rdx
sarq $0x2, %rdx
movq 0xc0(%rsp), %rcx
movq 0xb8(%rsp), %r8
callq 0x301dc0
movq %rax, 0x30(%rsp)
movq 0xd0(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x30(%rsp), %rax
movl %eax, %ecx
movq 0x18(%rsp), %rax
movl %ecx, (%rax)
movq 0x88(%rsp), %rax
addq 0x30(%rsp), %rax
movq 0xd0(%rsp), %rcx
subq %rcx, %rax
movq %rax, 0xe0(%rsp)
jmp 0x301477
movq 0xb8(%rsp), %rax
subq $0x10, %rax
movq %rax, 0xb8(%rsp)
movq 0xc0(%rsp), %rax
addq $0x40, %rax
movq %rax, 0xc0(%rsp)
jmp 0x30140e
movl 0x5c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x5c(%rsp)
jmp 0x30100b
movl 0x74(%rsp), %edx
movl 0x5c(%rsp), %eax
subl $0x1, %eax
movl %eax, %eax
movzbl 0x90(%rsp,%rax), %ecx
addl %edx, %ecx
movb %cl, 0x90(%rsp,%rax)
movl 0x5c(%rsp), %eax
movb $0x0, 0x90(%rsp,%rax)
leaq 0x90(%rsp), %rdi
callq 0x300e40
movl %eax, %ecx
movq 0x78(%rsp), %rax
movl %ecx, (%rax)
movq 0x88(%rsp), %rax
addq $0x40, %rax
movq %rax, 0x88(%rsp)
jmp 0x300f75
movq 0xe0(%rsp), %rax
addq $0xe8, %rsp
retq
nopw (%rax,%rax)