// File: /workspace/github_cpp_makefile_star2_O1/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 $0xf8, %rsp
movq %rcx, %r11
movq %rdi, 0x38(%rsp)
vxorps %xmm0, %xmm0, %xmm0
vmovups %ymm0, (%rsp)
movb $0x0, 0x20(%rsp)
addq %rsi, %rdx
leaq 0x4(%rsi), %r9
movq %rsi, 0x40(%rsp)
movl $0x0, (%rsi)
vmovss 0x2541(%rip), %xmm6     # 0x5a430
vpmovsxbq 0x1128(%rip), %ymm7 # 0x59020
vpbroadcastq 0x117f(%rip), %ymm10 # 0x59080
vbroadcastss 0x2526(%rip), %ymm8 # 0x5a430
vpbroadcastq 0x24fd(%rip), %ymm9 # 0x5a410
vpmovsxbq 0x1100(%rip), %ymm11 # 0x5901c
leaq 0x2e0d(%rip), %r10      # 0x5ad30
vpcmpeqd %ymm12, %ymm12, %ymm12
vmovdqu %ymm10, 0xb0(%rsp)
vmovups %ymm8, 0x90(%rsp)
vmovdqu %ymm9, 0x70(%rsp)
movq %rdx, 0x30(%rsp)
leaq 0x48(%r9), %rcx
cmpq %rdx, %rcx
ja 0x585ae
vxorps %xmm0, %xmm0, %xmm0
vmovups %ymm0, 0x20(%r9)
vmovups %ymm0, (%r9)
movl $0x0, 0x40(%r9)
leaq 0x4(%r9), %r13
subq %r9, %rdx
sarq $0x2, %rdx
movq %rdx, 0x50(%rsp)
leaq 0x3(%r8), %rcx
andq $-0x4, %rcx
movq %rcx, 0x48(%rsp)
leaq -0x1(%r8), %rcx
vmovq %rcx, %xmm0
vpbroadcastq %xmm0, %ymm13
movl $0x20, %ecx
xorl %ebx, %ebx
movq %r11, %rbp
movq %r8, %r15
xorl %edx, %edx
movq %r8, 0x68(%rsp)
movq %r11, 0x60(%rsp)
vmovdqu %ymm13, 0xd0(%rsp)
vmovq %r15, %xmm0
vpbroadcastq %xmm0, %ymm0
vpxor %ymm0, %ymm10, %ymm0
xorl %esi, %esi
vmovdqa %ymm11, %ymm2
vmovdqa %ymm7, %ymm3
vmovaps %ymm6, %ymm1
vpxor %ymm2, %ymm10, %ymm4
vpcmpgtq %ymm4, %ymm0, %ymm4
vpxor %ymm3, %ymm10, %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 (%rbp,%rsi,4), %ymm5, %ymm5
vpermq $0xd8, %ymm4, %ymm4     # ymm4 = ymm4[0,2,1,3]
vblendvps %ymm4, %ymm5, %ymm8, %ymm4
vorps %ymm1, %ymm4, %ymm1
addq $0x8, %rsi
vpaddq %ymm2, %ymm9, %ymm2
vpaddq %ymm3, %ymm9, %ymm3
cmpq $0x10, %rsi
jne 0x57fd2
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, %esi
cmpl $0xff, %esi
ja 0x58059
movzbl (%rsi,%r10), %r12d
jmp 0x58091
cmpl $0xffff, %esi           # imm = 0xFFFF
ja 0x5806f
shrl $0x8, %esi
movzbl (%rsi,%r10), %r12d
addl $0x8, %r12d
jmp 0x58091
cmpl $0xffffff, %esi         # imm = 0xFFFFFF
ja 0x58085
shrl $0x10, %esi
movzbl (%rsi,%r10), %r12d
addl $0x10, %r12d
jmp 0x58091
shrl $0x18, %esi
movzbl (%rsi,%r10), %r12d
addl $0x18, %r12d
movl %ecx, %r14d
subl %r12d, %r14d
jae 0x580ab
movl $0x4, %edi
xorl %esi, %esi
movl %ecx, %r14d
movl %edx, %r12d
jmp 0x5851b
movb %r12b, (%rsp,%rbx)
xorl %esi, %esi
xorl %edi, %edi
cmpq %rsi, %r15
jbe 0x580bc
movl (%rbp,%rsi,4), %edi
movl %edx, %ecx
shll %cl, %edi
orl %edi, (%r13,%rsi,4)
incq %rsi
cmpq $0x10, %rsi
jne 0x580b1
addl %edx, %r12d
cmpq $0x11, %r15
jae 0x58117
addb %r14b, (%rsp,%rbx)
movb $0x0, 0x1(%rsp,%rbx)
xorl %eax, %eax
cmpb $0x0, (%rsp,%rax)
je 0x580f6
incq %rax
cmpq $0x20, %rax
jne 0x580e2
movl $0x20, %eax
testl %eax, %eax
jle 0x58129
movl %eax, %esi
incq %rsi
xorl %eax, %eax
movb -0x2(%rsp,%rsi), %cl
shll %cl, %eax
decb %cl
btsl %ecx, %eax
decq %rsi
cmpq $0x1, %rsi
jg 0x58101
jmp 0x5812b
addq $-0x10, %r15
addq $0x40, %rbp
xorl %edi, %edi
movb $0x1, %sil
jmp 0x5851b
xorl %eax, %eax
movl %eax, (%r9)
testq %r15, %r15
je 0x58160
cmpq $0x10, %r15
jne 0x5816f
movq 0x40(%rsp), %rcx
movl $0x0, (%rcx)
movq %r13, %rax
subq %rcx, %rax
addq $0x40, %rax
movl $0x10, %r15d
movl $0x1, %edi
xorl %esi, %esi
jmp 0x5851b
movl $0x40, %eax
movq 0x48(%rsp), %rsi
jmp 0x582bf
leal 0x3(%r15), %eax
andl $0x3c, %eax
leaq -0x1(%r15), %rcx
vmovq %rcx, %xmm0
vpbroadcastq %xmm0, %ymm0
xorl %ecx, %ecx
vpmovsxbq 0x22f1(%rip), %xmm1 # 0x5a480
movq 0x48(%rsp), %rsi
vmovq %rcx, %xmm2
vpbroadcastq %xmm2, %ymm2
vpor %ymm2, %ymm11, %ymm2
vpxor %ymm0, %ymm10, %ymm3
vpxor %ymm2, %ymm10, %ymm2
vpcmpgtq %ymm3, %ymm2, %ymm2
vpxor %ymm2, %ymm12, %ymm3
vextracti128 $0x1, %ymm3, %xmm4
vpackssdw %xmm4, %xmm3, %xmm3
vpmaskmovd (%rbp,%rcx,4), %xmm3, %xmm4
vpbroadcastd 0x2267(%rip), %xmm5 # 0x5a434
vpmaxud %xmm5, %xmm4, %xmm5
vpcmpeqd %xmm5, %xmm4, %xmm5
vpbroadcastd 0x2259(%rip), %xmm6 # 0x5a438
vpand %xmm5, %xmm3, %xmm3
vpminud %xmm6, %xmm4, %xmm5
vpbroadcastd 0x224b(%rip), %xmm6 # 0x5a43c
vpcmpeqd %xmm5, %xmm4, %xmm5
vpmaxud %xmm6, %xmm4, %xmm6
vpcmpeqd %xmm6, %xmm4, %xmm6
vpand %xmm6, %xmm3, %xmm6
vpbroadcastd 0x2235(%rip), %xmm7 # 0x5a440
vpminud %xmm7, %xmm4, %xmm7
vpcmpeqd %xmm7, %xmm4, %xmm7
vpbroadcastd 0x2227(%rip), %xmm8 # 0x5a444
vpmaxud %xmm8, %xmm4, %xmm8
vpcmpeqd %xmm4, %xmm8, %xmm8
vpand %xmm6, %xmm8, %xmm8
vpmovsxdq %xmm8, %ymm8
vpbroadcastd 0x2210(%rip), %xmm9 # 0x5a448
vpminud %xmm9, %xmm4, %xmm9
vpcmpeqd %xmm4, %xmm9, %xmm4
vpbroadcastq 0x21ce(%rip), %ymm9 # 0x5a418
vpmovsxdq %xmm4, %ymm4
vpaddq %ymm4, %ymm9, %ymm4
vpand %xmm5, %xmm3, %xmm3
vpand %xmm7, %xmm6, %xmm5
vpmovsxdq %xmm5, %ymm5
vpmovzxdq %xmm3, %ymm3    # ymm3 = xmm3[0],zero,xmm3[1],zero,xmm3[2],zero,xmm3[3],zero
vpbroadcastq 0x21b2(%rip), %ymm6 # 0x5a420
vpand %ymm6, %ymm3, %ymm3
vpsubq %ymm12, %ymm3, %ymm3
vbroadcastsd 0x21a8(%rip), %ymm6 # 0x5a428
vblendvpd %ymm5, %ymm6, %ymm3, %ymm3
vblendvpd %ymm8, %ymm4, %ymm3, %ymm4
vmovdqa %ymm1, %ymm3
vpaddq %ymm1, %ymm4, %ymm1
addq $0x4, %rcx
cmpq %rcx, %rax
jne 0x58194
vblendvpd %ymm2, %ymm3, %ymm1, %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
testq %r8, %r8
je 0x583fb
vpxor %xmm0, %xmm0, %xmm0
xorl %ecx, %ecx
vmovq %rcx, %xmm1
vpbroadcastq %xmm1, %ymm1
vpor %ymm1, %ymm11, %ymm1
vpxor %ymm10, %ymm13, %ymm2
vpxor %ymm1, %ymm10, %ymm1
vpcmpgtq %ymm2, %ymm1, %ymm1
vpxor %ymm1, %ymm12, %ymm2
vextracti128 $0x1, %ymm2, %xmm3
vpackssdw %xmm3, %xmm2, %xmm2
vpmaskmovd (%r11,%rcx,4), %xmm2, %xmm3
vpbroadcastd 0x212d(%rip), %xmm4 # 0x5a434
vpmaxud %xmm4, %xmm3, %xmm4
vpcmpeqd %xmm4, %xmm3, %xmm4
vpbroadcastd 0x211f(%rip), %xmm5 # 0x5a438
vpand %xmm4, %xmm2, %xmm2
vpminud %xmm5, %xmm3, %xmm4
vpbroadcastd 0x2111(%rip), %xmm5 # 0x5a43c
vpcmpeqd %xmm4, %xmm3, %xmm4
vpmaxud %xmm5, %xmm3, %xmm5
vpcmpeqd %xmm5, %xmm3, %xmm5
vpand %xmm5, %xmm2, %xmm5
vpbroadcastd 0x20fb(%rip), %xmm6 # 0x5a440
vpminud %xmm6, %xmm3, %xmm6
vpcmpeqd %xmm6, %xmm3, %xmm6
vpbroadcastd 0x20ed(%rip), %xmm7 # 0x5a444
vpmaxud %xmm7, %xmm3, %xmm7
vpcmpeqd %xmm7, %xmm3, %xmm7
vpand %xmm7, %xmm5, %xmm7
vpmovsxdq %xmm7, %ymm7
vpbroadcastd 0x20d6(%rip), %xmm8 # 0x5a448
vpminud %xmm8, %xmm3, %xmm8
vpcmpeqd %xmm3, %xmm8, %xmm3
vpbroadcastq 0x2094(%rip), %ymm8 # 0x5a418
vpmovsxdq %xmm3, %ymm3
vpaddq %ymm3, %ymm8, %ymm3
vpand %xmm4, %xmm2, %xmm2
vpand %xmm6, %xmm5, %xmm4
vpmovsxdq %xmm4, %ymm4
vpmovzxdq %xmm2, %ymm2    # ymm2 = xmm2[0],zero,xmm2[1],zero,xmm2[2],zero,xmm2[3],zero
vpbroadcastq 0x2078(%rip), %ymm5 # 0x5a420
vpand %ymm5, %ymm2, %ymm2
vpsubq %ymm12, %ymm2, %ymm2
vbroadcastsd 0x206e(%rip), %ymm5 # 0x5a428
vblendvpd %ymm4, %ymm5, %ymm2, %ymm2
vblendvpd %ymm7, %ymm3, %ymm2, %ymm3
vmovdqa %ymm0, %ymm2
vpaddq %ymm0, %ymm3, %ymm0
addq $0x4, %rcx
cmpq %rcx, %rsi
jne 0x582ce
vblendvpd %ymm1, %ymm2, %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, %rcx
jmp 0x583fd
xorl %ecx, %ecx
cmpq %rax, %rcx
jae 0x58425
movq 0x38(%rsp), %rdi
movq %r9, %rsi
movq 0x50(%rsp), %rdx
movq %r11, %rcx
movq %r9, %r13
vzeroupper
callq 0x58724
movq %r13, %r9
jmp 0x584b6
xorl %eax, %eax
xorl %esi, %esi
cmpq %rax, %r15
jbe 0x58432
movl (%rbp,%rax,4), %esi
movl %edx, %ecx
shll %cl, %esi
xorl %esi, (%r13,%rax,4)
incq %rax
cmpq $0x10, %rax
jne 0x58427
movb (%rsp,%rbx), %al
leal -0x1(%rbx), %ecx
addb %al, (%rsp,%rcx)
addq $0x40, %r13
movb $0x0, (%rsp,%rbx)
xorl %eax, %eax
cmpb $0x0, (%rsp,%rax)
je 0x5846b
incq %rax
cmpq $0x20, %rax
jne 0x58457
movl $0x20, %eax
testl %eax, %eax
jle 0x5848c
movl %eax, %edx
incq %rdx
xorl %eax, %eax
movb -0x2(%rsp,%rdx), %cl
shll %cl, %eax
decb %cl
btsl %ecx, %eax
decq %rdx
cmpq $0x1, %rdx
jg 0x58476
jmp 0x5848e
xorl %eax, %eax
movl %eax, (%r9)
movq 0x38(%rsp), %rdi
movq %r13, %rsi
movq 0x50(%rsp), %rdx
movq %rbp, %rcx
movq %r15, %r8
movq %r9, 0x58(%rsp)
vzeroupper
callq 0x58724
movq 0x58(%rsp), %r9
movq 0x40(%rsp), %rcx
movl %eax, (%rcx)
addq %r13, %rax
subq %rcx, %rax
movl $0x1, %edi
xorl %esi, %esi
vmovss 0x1f5e(%rip), %xmm6     # 0x5a430
vpmovsxbq 0xb45(%rip), %ymm7 # 0x59020
vmovdqu 0xb0(%rsp), %ymm10
vmovups 0x90(%rsp), %ymm8
vmovdqu 0x70(%rsp), %ymm9
vpmovsxbq 0xb20(%rip), %ymm11 # 0x5901c
leaq 0x282d(%rip), %r10      # 0x5ad30
vpcmpeqd %ymm12, %ymm12, %ymm12
movq 0x68(%rsp), %r8
movq 0x60(%rsp), %r11
vmovdqu 0xd0(%rsp), %ymm13
testl %edi, %edi
jne 0x58539
incq %rbx
movl %r12d, %edx
movl %r14d, %ecx
cmpq $0x20, %rbx
jne 0x57fb6
movl $0x20, %ebx
jmp 0x5853e
cmpl $0x4, %edi
jne 0x58587
leal -0x1(%rbx), %ecx
addb %r14b, (%rsp,%rcx)
movl %ebx, %ecx
movb $0x0, (%rsp,%rcx)
xorl %ecx, %ecx
movq 0x30(%rsp), %rdx
cmpb $0x0, (%rsp,%rcx)
je 0x58566
incq %rcx
cmpq $0x20, %rcx
jne 0x58552
movl $0x20, %ecx
testl %ecx, %ecx
jle 0x5858e
movl %ecx, %esi
incq %rsi
xorl %edi, %edi
movb -0x2(%rsp,%rsi), %cl
shll %cl, %edi
decb %cl
btsl %ecx, %edi
decq %rsi
cmpq $0x1, %rsi
jg 0x58571
jmp 0x58590
movq 0x30(%rsp), %rdx
jmp 0x5859a
xorl %edi, %edi
movl %edi, (%r9)
addq $0x40, %r13
movb $0x1, %sil
movq %r13, %r9
movq %r15, %r8
movq %rbp, %r11
testb %sil, %sil
jne 0x57f45
jmp 0x585b0
xorl %eax, %eax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
vzeroupper
retq
nop