// File: /workspace/github_c_makefile_star2_O2/zhiyb[P]png2tlg/png2tlg.cpp
// Function: CompressValuesGolomb(TLG6BitStream&, char*, int)
void CompressValuesGolomb(TLG6BitStream &bs, char *buf, int size)
{
	// golomb encoding, -- http://oku.edu.mie-u.ac.jp/~okumura/compression/golomb/

	// run-length golomb method
	bs.PutValue(buf[0]?1:0, 1); // initial value state

	int count;

	int n = TVP_TLG6_GOLOMB_N_COUNT - 1; // 個数のカウンタ
	int a = 0; // 予測誤差の絶対値の和

	count = 0;
	for(int i = 0; i < size; i++)
	{
		if(buf[i])
		{
			// write zero count
			if(count) bs.PutGamma(count);

			// count non-zero values
			count = 0;
			int ii;
			for(ii = i; ii < size; ii++)
			{
				if(buf[ii]) count++; else break;
			}

			// write non-zero count
			bs.PutGamma(count);

			// write non-zero values
			for(; i < ii; i++)
			{
				int e = buf[i];
#ifdef WRITE_VSTXT
				fprintf(vstxt, "%d ", e);
#endif
				int k = TVPTLG6GolombBitLengthTable[a][n];
				int m = ((e >= 0) ? 2*e : -2*e-1) - 1;
				int store_limit = bs.GetBytePos() + GOLOMB_GIVE_UP_BYTES;
				bool put1 = true;
				for(int c = (m >> k); c > 0; c--)
				{
					if(store_limit == bs.GetBytePos())
					{
						bs.PutValue(m >> k, 8);
#ifdef WRITE_VSTXT
						fprintf(vstxt, "[ %d ] ", m>>k);
#endif
						put1 = false;
						break;
					}
					bs.Put1Bit(0);
				}
				if(store_limit == bs.GetBytePos())
				{
					bs.PutValue(m >> k, 8);
#ifdef WRITE_VSTXT
					fprintf(vstxt, "[ %d ] ", m>>k);
#endif
					put1 = false;
				}
				if(put1) bs.Put1Bit(1);
				bs.PutValue(m, k);
				a += (m>>1);
				if (--n < 0) {
					a >>= 1; n = TVP_TLG6_GOLOMB_N_COUNT - 1;
				}
			}

#ifdef WRITE_VSTXT
			fprintf(vstxt, "\n");
#endif

			i = ii - 1;
			count = 0;
		}
		else
		{
			// zero
			count ++;
		}
	}

	if(count) bs.PutGamma(count);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %edx, %ebp
movq %rsi, %r14
movq %rdi, %rbx
xorl %esi, %esi
cmpb $0x0, (%r14)
setne %sil
pushq $0x1
popq %rdx
callq 0xf0e8
movslq %ebp, %rax
pushq $0x3
popq %rcx
xorl %edx, %edx
xorl %r13d, %r13d
movl %ecx, %r12d
movl %ebp, 0xc(%rsp)
movq %rax, 0x18(%rsp)
movslq %edx, %r15
xorl %esi, %esi
cmpl %ebp, %r15d
jge 0x48d6
cmpb $0x0, (%r14,%r15)
jne 0x4778
incl %esi
incq %r15
jmp 0x4761
testl %esi, %esi
je 0x478d
movq %rbx, %rdi
callq 0xf114
movq 0x18(%rsp), %rax
movl 0xc(%rsp), %ebp
xorl %esi, %esi
movq %r15, %rcx
cmpq %rax, %rcx
jge 0x47a5
cmpb $0x0, (%r14,%rcx)
je 0x47a7
incl %esi
incq %rcx
jmp 0x4792
jmp 0x47aa
movq %rcx, %rbp
movq %rbx, %rdi
callq 0xf114
movslq %ebp, %rax
movq %rax, 0x30(%rsp)
movq %rbp, %rdx
movq %r14, 0x20(%rsp)
movq %rbp, 0x28(%rsp)
cmpq 0x30(%rsp), %r15
jge 0x48c8
movq %r15, 0x40(%rsp)
movsbl (%r14,%r15), %eax
movl %r13d, 0x10(%rsp)
movslq %r13d, %rcx
movq %r12, 0x38(%rsp)
movslq %r12d, %rdx
leaq 0x10a3d(%rip), %rsi     # 0x15230
leaq (%rsi,%rcx,4), %rcx
movsbl (%rdx,%rcx), %ecx
movl %eax, %edx
addl %eax, %edx
sarb $0x7, %al
movsbl %al, %r12d
xorl %edx, %r12d
decl %r12d
movl %r12d, %r13d
movl %ecx, 0x14(%rsp)
sarl %cl, %r13d
movl 0x8(%rbx), %eax
addl $0x4, %eax
movslq %eax, %rbp
movl %r13d, %r14d
movq 0x8(%rbx), %rax
movslq %r13d, %r15
testl %r14d, %r14d
jle 0x4842
movq %rbx, %rdi
cmpq %rbp, %rax
je 0x485d
xorl %esi, %esi
callq 0xf16a
decl %r14d
jmp 0x4822
cmpq %rbp, %rax
movq 0x20(%rsp), %r14
movl 0x10(%rsp), %ebp
je 0x4877
movq %rbx, %rdi
pushq $0x1
popq %rsi
callq 0xf16a
jmp 0x4885
movq %r15, %rsi
pushq $0x8
popq %rdx
callq 0xf0e8
cmpq %rbp, 0x8(%rbx)
movq 0x20(%rsp), %r14
movl 0x10(%rsp), %ebp
jne 0x4885
movq %rbx, %rdi
movq %r15, %rsi
pushq $0x8
popq %rdx
callq 0xf0e8
movslq %r12d, %rsi
movq %rbx, %rdi
movl 0x14(%rsp), %edx
callq 0xf0e8
sarl %r12d
addl %ebp, %r12d
movq 0x38(%rsp), %rax
testl %eax, %eax
leal -0x1(%rax), %eax
setle %cl
pushq $0x3
popq %rdx
cmovlel %edx, %eax
sarl %cl, %r12d
movq 0x40(%rsp), %r15
incq %r15
movl %r12d, %r13d
movl %eax, %r12d
movq 0x28(%rsp), %rdx
jmp 0x47c7
movl 0xc(%rsp), %ebp
movq 0x18(%rsp), %rax
jmp 0x475c
testl %esi, %esi
je 0x48f0
movq %rbx, %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xf114
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq