// File: /workspace/github_cpp_makefile_star2_O3/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 $0xe8, %rsp
vxorps %xmm0, %xmm0, %xmm0
vmovups %ymm0, 0x10(%rsp)
movb $0x0, 0x30(%rsp)
movl $0x0, (%rsi)
cmpq $0x4c, %rdx
jge 0x57f92
xorl %r14d, %r14d
jmp 0x5862c
movq %rcx, %r9
movq %rdi, 0x48(%rsp)
addq %rsi, %rdx
leaq 0x4(%rsi), %r14
movq %rsi, 0x58(%rsp)
leaq 0x8(%rsi), %r12
vmovss 0x247e(%rip), %xmm7     # 0x5a430
vpmovsxbq 0x1065(%rip), %ymm8 # 0x59020
vpbroadcastq 0x10bc(%rip), %ymm6 # 0x59080
vbroadcastss 0x2463(%rip), %ymm9 # 0x5a430
vpbroadcastq 0x243a(%rip), %ymm10 # 0x5a410
vpmovsxbq 0x103d(%rip), %ymm11 # 0x5901c
leaq 0x2d4a(%rip), %r10      # 0x5ad30
movq %rcx, %r13
vmovdqu %ymm6, 0xc0(%rsp)
movq %rdx, 0x8(%rsp)
vmovups %ymm9, 0xa0(%rsp)
vmovdqu %ymm10, 0x80(%rsp)
vmovups %ymm0, 0x20(%r14)
vmovups %ymm0, (%r14)
movl $0x0, 0x40(%r14)
leaq -0x200(%r8), %r11
movl $0x20, %eax
xorl %ebx, %ebx
movq %r8, %rbp
xorl %r15d, %r15d
vmovq %rbp, %xmm0
vpbroadcastq %xmm0, %ymm0
vpxor %ymm6, %ymm0, %ymm0
xorl %ecx, %ecx
vmovdqa %ymm11, %ymm2
vmovdqa %ymm8, %ymm3
vmovaps %ymm7, %ymm1
vpxor %ymm6, %ymm2, %ymm4
vpcmpgtq %ymm4, %ymm0, %ymm4
vpxor %ymm6, %ymm3, %ymm5
vpcmpgtq %ymm5, %ymm0, %ymm5
vpackssdw %ymm5, %ymm4, %ymm4
vextracti128 $0x1, %ymm4, %xmm5
vpackssdw %xmm5, %xmm4, %xmm5
vpshufd $0xd8, %xmm5, %xmm5     # xmm5 = xmm5[0,2,1,3]
vpmovzxwd %xmm5, %ymm5    # ymm5 = xmm5[0],zero,xmm5[1],zero,xmm5[2],zero,xmm5[3],zero,xmm5[4],zero,xmm5[5],zero,xmm5[6],zero,xmm5[7],zero
vpslld $0x1f, %ymm5, %ymm5
vpmaskmovd (%r13,%rcx,4), %ymm5, %ymm5
vpermq $0xd8, %ymm4, %ymm4     # ymm4 = ymm4[0,2,1,3]
vblendvps %ymm4, %ymm5, %ymm9, %ymm4
vorps %ymm1, %ymm4, %ymm1
addq $0x8, %rcx
vpaddq %ymm2, %ymm10, %ymm2
vpaddq %ymm3, %ymm10, %ymm3
cmpq $0x10, %rcx
jne 0x5804c
vextractf128 $0x1, %ymm1, %xmm0
vorps %xmm0, %xmm1, %xmm0
vshufps $0xee, %xmm0, %xmm0, %xmm1 # xmm1 = xmm0[2,3,2,3]
vorps %xmm1, %xmm0, %xmm0
vshufps $0x55, %xmm0, %xmm0, %xmm1 # xmm1 = xmm0[1,1,1,1]
vorps %xmm1, %xmm0, %xmm0
vmovd %xmm0, %ecx
cmpl $0xff, %ecx
ja 0x580d3
movzbl (%rcx,%r10), %esi
jmp 0x58108
cmpl $0xffff, %ecx           # imm = 0xFFFF
ja 0x580e8
shrl $0x8, %ecx
movzbl (%rcx,%r10), %esi
addl $0x8, %esi
jmp 0x58108
cmpl $0xffffff, %ecx         # imm = 0xFFFFFF
ja 0x580fd
shrl $0x10, %ecx
movzbl (%rcx,%r10), %esi
addl $0x10, %esi
jmp 0x58108
shrl $0x18, %ecx
movzbl (%rcx,%r10), %esi
addl $0x18, %esi
movl %eax, %edx
subl %esi, %edx
jb 0x58164
movb %sil, 0x10(%rsp,%rbx)
xorl %eax, %eax
xorl %edi, %edi
cmpq %rax, %rbp
jbe 0x58121
movl (%r13,%rax,4), %edi
movl %r15d, %ecx
shll %cl, %edi
orl %edi, (%r12,%rax,4)
incq %rax
cmpq $0x10, %rax
jne 0x58115
cmpq $0x11, %rbp
jb 0x581ee
addl %esi, %r15d
addq $-0x10, %rbp
addq $0x40, %r13
incq %rbx
movl %edx, %eax
cmpq $0x20, %rbx
jne 0x58030
movq %r10, %r15
movl $0x20, %ebx
movq %r11, %rbp
jmp 0x58169
movq %r10, %r15
movl %eax, %edx
leal -0x1(%rbx), %eax
addb %dl, 0x10(%rsp,%rax)
movl %ebx, %eax
movb $0x0, 0x10(%rsp,%rax)
leaq 0x10(%rsp), %rdi
vzeroupper
callq 0x57f24
movl %eax, (%r14)
leaq 0x44(%r14), %rax
addq $0x8c, %r14
addq $0x44, %r12
movq %r13, %r9
movq %rbp, %r8
movq 0x8(%rsp), %rdx
cmpq %rdx, %r14
movq %rax, %r14
vmovdqu 0xc0(%rsp), %ymm6
vxorps %xmm0, %xmm0, %xmm0
vmovss 0x2274(%rip), %xmm7     # 0x5a430
vpmovsxbq 0xe5b(%rip), %ymm8 # 0x59020
vmovups 0xa0(%rsp), %ymm9
vmovdqu 0x80(%rsp), %ymm10
vpmovsxbq 0xe3c(%rip), %ymm11 # 0x5901c
movq %r15, %r10
jbe 0x58009
jmp 0x57f8a
movq %r9, 0x40(%rsp)
movq %r8, 0x50(%rsp)
addb %sil, %dl
movb %dl, 0x10(%rsp,%rbx)
leal 0x1(%rbx), %eax
movb $0x0, 0x10(%rsp,%rax)
leaq 0x10(%rsp), %rdi
vzeroupper
callq 0x57f24
movl %eax, (%r14)
testq %rbp, %rbp
je 0x58247
cmpq $0x10, %rbp
movq 0x50(%rsp), %r8
vmovdqu 0xc0(%rsp), %ymm15
jne 0x58264
movq 0x58(%rsp), %rax
movl $0x0, (%rax)
subq %rax, %r14
addq $0x44, %r14
jmp 0x5862c
movl $0x40, %eax
movq 0x50(%rsp), %r8
movq 0x40(%rsp), %rcx
vmovdqu 0xc0(%rsp), %ymm15
jmp 0x583fd
cmpq $0x1, %rbp
movq %rbp, %rax
adcq $0x0, %rax
leaq -0x1(%rax), %rcx
vmovq %rcx, %xmm0
vpbroadcastq %xmm0, %ymm2
addl $0x3, %eax
andl $0x3c, %eax
vpmovsxbq 0x21f4(%rip), %xmm0 # 0x5a480
xorl %ecx, %ecx
vpxor %ymm2, %ymm15, %ymm1
vmovdqu %ymm1, 0xa0(%rsp)
vbroadcastss 0x2190(%rip), %xmm1 # 0x5a434
vmovaps %xmm1, 0x80(%rsp)
vbroadcastss 0x2182(%rip), %xmm1 # 0x5a438
vmovaps %xmm1, 0x70(%rsp)
vbroadcastss 0x2177(%rip), %xmm1 # 0x5a43c
vmovaps %xmm1, 0x60(%rsp)
vpbroadcastd 0x216c(%rip), %xmm7 # 0x5a440
vpbroadcastd 0x2167(%rip), %xmm8 # 0x5a444
vpbroadcastd 0x2162(%rip), %xmm9 # 0x5a448
vpbroadcastq 0x2129(%rip), %ymm10 # 0x5a418
vpbroadcastq 0x2128(%rip), %ymm11 # 0x5a420
vbroadcastsd 0x2127(%rip), %ymm12 # 0x5a428
vpcmpeqd %ymm6, %ymm6, %ymm6
vmovq %rcx, %xmm13
vpbroadcastq %xmm13, %ymm13
vpor 0xd29(%rip), %ymm13, %ymm13 # 0x59040
vpxor %ymm15, %ymm13, %ymm13
vpcmpgtq 0xa0(%rsp), %ymm13, %ymm13
vpxor %ymm6, %ymm13, %ymm14
vextracti128 $0x1, %ymm14, %xmm15
vpackssdw %xmm15, %xmm14, %xmm14
vpmaskmovd (%r13,%rcx,4), %xmm14, %xmm15
vpmaxud 0x80(%rsp), %xmm15, %xmm1
vpcmpeqd %xmm1, %xmm15, %xmm1
vpand %xmm1, %xmm14, %xmm1
vpminud 0x70(%rsp), %xmm15, %xmm14
vpcmpeqd %xmm14, %xmm15, %xmm14
vpmaxud 0x60(%rsp), %xmm15, %xmm3
vpcmpeqd %xmm3, %xmm15, %xmm3
vpminud %xmm7, %xmm15, %xmm2
vpcmpeqd %xmm2, %xmm15, %xmm2
vpmaxud %xmm8, %xmm15, %xmm4
vpcmpeqd %xmm4, %xmm15, %xmm4
vpminud %xmm9, %xmm15, %xmm5
vpcmpeqd %xmm5, %xmm15, %xmm5
vmovdqu 0xc0(%rsp), %ymm15
vpand %xmm3, %xmm1, %xmm3
vpand %xmm1, %xmm14, %xmm1
vpand %xmm4, %xmm3, %xmm4
vpand %xmm2, %xmm3, %xmm2
vpmovsxdq %xmm2, %ymm2
vpmovzxdq %xmm1, %ymm1    # ymm1 = xmm1[0],zero,xmm1[1],zero,xmm1[2],zero,xmm1[3],zero
vpand %ymm1, %ymm11, %ymm1
vpsubq %ymm6, %ymm1, %ymm1
vblendvpd %ymm2, %ymm12, %ymm1, %ymm1
vpmovsxdq %xmm4, %ymm2
vpmovsxdq %xmm5, %ymm3
vpaddq %ymm3, %ymm10, %ymm3
vblendvpd %ymm2, %ymm3, %ymm1, %ymm1
vmovdqa %ymm0, %ymm14
vpaddq %ymm0, %ymm1, %ymm0
addq $0x4, %rcx
cmpq %rcx, %rax
jne 0x58305
vblendvpd %ymm13, %ymm14, %ymm0, %ymm0
vextractf128 $0x1, %ymm0, %xmm1
vpaddq %xmm1, %xmm0, %xmm0
vpshufd $0xee, %xmm0, %xmm1     # xmm1 = xmm0[2,3,2,3]
vpaddq %xmm1, %xmm0, %xmm0
vmovq %xmm0, %rax
movq 0x40(%rsp), %rcx
testq %r8, %r8
je 0x5858d
leaq 0x3(%r8), %rsi
andq $-0x4, %rsi
leaq -0x1(%r8), %rdx
vmovq %rdx, %xmm0
vpbroadcastq %xmm0, %ymm2
vpxor %xmm0, %xmm0, %xmm0
xorl %edx, %edx
vpxor %ymm2, %ymm15, %ymm1
vmovdqu %ymm1, 0xa0(%rsp)
vbroadcastss 0x1ffc(%rip), %xmm1 # 0x5a434
vmovaps %xmm1, 0x80(%rsp)
vbroadcastss 0x1fee(%rip), %xmm1 # 0x5a438
vmovaps %xmm1, 0x70(%rsp)
vbroadcastss 0x1fe3(%rip), %xmm1 # 0x5a43c
vmovaps %xmm1, 0x60(%rsp)
vpbroadcastd 0x1fd8(%rip), %xmm7 # 0x5a440
vpbroadcastd 0x1fd3(%rip), %xmm8 # 0x5a444
vpbroadcastd 0x1fce(%rip), %xmm9 # 0x5a448
vpbroadcastq 0x1f95(%rip), %ymm10 # 0x5a418
vpbroadcastq 0x1f94(%rip), %ymm11 # 0x5a420
vbroadcastsd 0x1f93(%rip), %ymm12 # 0x5a428
vpcmpeqd %ymm6, %ymm6, %ymm6
vmovq %rdx, %xmm13
vpbroadcastq %xmm13, %ymm13
vpor 0xb95(%rip), %ymm13, %ymm13 # 0x59040
vpxor %ymm15, %ymm13, %ymm13
vpcmpgtq 0xa0(%rsp), %ymm13, %ymm13
vpxor %ymm6, %ymm13, %ymm14
vextracti128 $0x1, %ymm14, %xmm15
vpackssdw %xmm15, %xmm14, %xmm14
vpmaskmovd (%rcx,%rdx,4), %xmm14, %xmm15
vpmaxud 0x80(%rsp), %xmm15, %xmm1
vpcmpeqd %xmm1, %xmm15, %xmm1
vpand %xmm1, %xmm14, %xmm1
vpminud 0x70(%rsp), %xmm15, %xmm14
vpcmpeqd %xmm14, %xmm15, %xmm14
vpmaxud 0x60(%rsp), %xmm15, %xmm3
vpcmpeqd %xmm3, %xmm15, %xmm3
vpminud %xmm7, %xmm15, %xmm2
vpcmpeqd %xmm2, %xmm15, %xmm2
vpmaxud %xmm8, %xmm15, %xmm4
vpcmpeqd %xmm4, %xmm15, %xmm4
vpminud %xmm9, %xmm15, %xmm5
vpcmpeqd %xmm5, %xmm15, %xmm5
vmovdqu 0xc0(%rsp), %ymm15
vpand %xmm3, %xmm1, %xmm3
vpand %xmm1, %xmm14, %xmm1
vpand %xmm4, %xmm3, %xmm4
vpand %xmm2, %xmm3, %xmm2
vpmovsxdq %xmm2, %ymm2
vpmovzxdq %xmm1, %ymm1    # ymm1 = xmm1[0],zero,xmm1[1],zero,xmm1[2],zero,xmm1[3],zero
vpand %ymm1, %ymm11, %ymm1
vpsubq %ymm6, %ymm1, %ymm1
vblendvpd %ymm2, %ymm12, %ymm1, %ymm1
vpmovsxdq %xmm4, %ymm2
vpmovsxdq %xmm5, %ymm3
vpaddq %ymm3, %ymm10, %ymm3
vblendvpd %ymm2, %ymm3, %ymm1, %ymm1
vmovdqa %ymm0, %ymm14
vpaddq %ymm0, %ymm1, %ymm0
addq $0x4, %rdx
cmpq %rdx, %rsi
jne 0x58499
vblendvpd %ymm13, %ymm14, %ymm0, %ymm0
vextractf128 $0x1, %ymm0, %xmm1
vpaddq %xmm1, %xmm0, %xmm0
vpshufd $0xee, %xmm0, %xmm1     # xmm1 = xmm0[2,3,2,3]
vpaddq %xmm1, %xmm0, %xmm0
vmovq %xmm0, %rdx
jmp 0x5858f
xorl %edx, %edx
cmpq %rax, %rdx
jae 0x585b2
movq 0x8(%rsp), %rdx
subq %r14, %rdx
sarq $0x2, %rdx
movq 0x48(%rsp), %rdi
movq %r14, %rsi
vzeroupper
callq 0x587a0
jmp 0x5861f
xorl %eax, %eax
movl $0x0, %edx
cmpq %rax, %rbp
jbe 0x585c3
movl (%r13,%rax,4), %edx
movl %r15d, %ecx
shll %cl, %edx
xorl %edx, (%r12)
incq %rax
addq $0x4, %r12
cmpq $0x10, %rax
jne 0x585b4
leaq 0x44(%r14), %r15
movb 0x10(%rsp,%rbx), %al
leal -0x1(%rbx), %ecx
addb %al, 0x10(%rsp,%rcx)
movb $0x0, 0x10(%rsp,%rbx)
leaq 0x10(%rsp), %rdi
vzeroupper
callq 0x57f24
movl %eax, (%r14)
movq 0x8(%rsp), %rdx
subq %r14, %rdx
sarq $0x2, %rdx
movq 0x48(%rsp), %rdi
movq %r15, %rsi
movq %r13, %rcx
movq %rbp, %r8
callq 0x587a0
movq %r15, %r14
movq 0x58(%rsp), %rcx
movl %eax, (%rcx)
addq %rax, %r14
subq %rcx, %r14
movq %r14, %rax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
vzeroupper
retq