// File: /workspace/github_c_makefile_star2_O2/DrYerzinia[P]C_DigiMode/src/psk31/psk31_wcpd.cpp
// Function: psk31_wcpd::proccess_byte(char)
bool psk31_wcpd::proccess_byte(char next_data_point){

	bool newByte = false;

	input_data[input_position] = next_data_point;

	if(input_count > input_data_width){


		int pos;
		int postmp;

		pos = input_position+1;

		int topsum1, topsum2;
		float max;

		float phasesum[(int)ninty_deg_phase_width];

		for(int i = 0; i < ninty_deg_phase_width; i++)
			phasesum[i] = 0.0f;

		for(int i = 0; i < ninty_deg_phase_width; i++){

			postmp = pos + i;
			if(postmp < 0) postmp = input_data_width + postmp;
			else if(postmp >= input_data_width) postmp = postmp - input_data_width;

			for(int j = 0; j < ninty_deg_phase_width; j++){
				phasesum[j] += input_data[postmp]*sin(((input_count+i)/ninty_deg_phase_width)*2*PI+(j*2*PI/ninty_deg_phase_width));
			}

		}

		max = -100000;
		for(int i = 0; i < ninty_deg_phase_width; i++){
			if(phasesum[i] > max){
				max = phasesum[i];
				topsum1 = i;
			}
		}

		pos = (int)floor(input_position-ninty_deg_phase_width+1.5);

		for(int i = 0; i < ninty_deg_phase_width; i++)
			phasesum[i] = 0.0f;

		for(int i = 0; i < ninty_deg_phase_width; i++){

			postmp = pos + i;
			if(postmp < 0) postmp = input_data_width + postmp;
			else if(postmp >= input_data_width) postmp = postmp - input_data_width;

			for(int j = 0; j < ninty_deg_phase_width; j++){
				phasesum[j] += input_data[postmp]*sin(((input_count+i)/ninty_deg_phase_width)*2*PI+(j*2*PI/ninty_deg_phase_width));
			}

		}

		max = -100000;
		for(int i = 0; i < ninty_deg_phase_width; i++){
			if(phasesum[i] > max){
				max = phasesum[i];
				topsum2 = i;
			}
		}

		float pd = std::abs(topsum1-topsum2);
		if(pd > ninty_deg_phase_width/2) pd = std::abs(pd-ninty_deg_phase_width);
 		phase_change_data.push_back(pd);

	}

	input_position++;
	input_count++;

	if(input_position > input_data_width) input_position = 0;

	if(phase_change_data.size() >= bitwidth/4){

		float phase_change_sum = 0.0;

		for(std::list<float>::iterator it = phase_change_data.begin(); it != phase_change_data.end(); it++){

			phase_change_sum += (*it);

		}

		if(phase_change_sum > max) max = phase_change_sum;

		int current_value;

		if(phase_change_sum > threshhold_value*max)
			current_value = 1;
		else
			current_value = 0;

		if(last == -1) last = current_value;

		if(current_value != last){

			for(int i = 0; i < floor(((float)countlast)/((float)bitwidth)+0.5); i++)
				bit_sequence.push_back(current_value);

			if(bit_sequence.size() >= 2 && bit_sequence[bit_sequence.size()-1] == 0 && bit_sequence[bit_sequence.size()-2] == 0){
				int one = 1;
				int val = 0;
				for(int i = bit_sequence.size()-3; i >= 0; i--){
					if(bit_sequence[i] == 1)
						val |= one;
					one <<= 1;
				}

				newByte = true;

				byte_sequence.push_back(getVaricode(val));
				bit_sequence.clear();
			}

			last = current_value;
			countlast = 1;

		} else countlast++;

		phase_change_data.pop_front();

	}

	return newByte;

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdi, %rbx
movq 0x60(%rdi), %rax
movl 0x50(%rdi), %ecx
movb %sil, (%rax,%rcx)
movl 0x54(%rdi), %eax
movl 0x58(%rdi), %ecx
cmpl %ecx, %eax
jbe 0x54d4
movq %rsp, -0x68(%rbp)
movl 0x50(%rbx), %eax
movq %rax, -0x38(%rbp)
cvttss2si 0x18(%rbx), %eax
movq %rsp, %r15
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
subq %rax, %r15
movq %r15, %rsp
movss 0x18(%rbx), %xmm2
xorl %eax, %eax
cvtsi2ss %eax, %xmm0
ucomiss %xmm0, %xmm2
jbe 0x5240
andl $0x0, (%r15,%rax,4)
incq %rax
jmp 0x522d
movq -0x38(%rbp), %rax
incl %eax
movq %rax, -0x38(%rbp)
xorl %ecx, %ecx
movq %rbx, -0x40(%rbp)
cvtsi2ss %ecx, %xmm0
ucomiss %xmm0, %xmm2
jbe 0x531a
movq -0x38(%rbp), %rax
movq %rcx, -0x50(%rbp)
addl %ecx, %eax
movl 0x58(%rbx), %ecx
xorl %r14d, %r14d
cmpl %ecx, %eax
movl %ecx, %edx
cmovbl %r14d, %edx
negl %edx
testl %eax, %eax
cmovsl %ecx, %edx
addl %eax, %edx
movslq %edx, %r12
xorl %r13d, %r13d
cvtsi2ss %r13d, %xmm0
ucomiss %xmm0, %xmm2
jbe 0x530b
movq -0x40(%rbp), %rbx
movl 0x54(%rbx), %eax
addl -0x50(%rbp), %eax
cvtsi2ss %rax, %xmm0
movq 0x60(%rbx), %rax
movsbl (%rax,%r12), %eax
cvtsi2sd %eax, %xmm1
movsd %xmm1, -0x60(%rbp)
divss %xmm2, %xmm0
addss %xmm0, %xmm0
cvtss2sd %xmm0, %xmm0
cvtsi2sd %r14d, %xmm1
movsd 0xe18(%rip), %xmm3      # 0x60e0
mulsd %xmm3, %xmm1
cvtss2sd %xmm2, %xmm2
divsd %xmm2, %xmm1
mulsd %xmm3, %xmm0
addsd %xmm1, %xmm0
callq 0x20a0
cvtss2sd (%r15,%r13,4), %xmm1
mulsd -0x60(%rbp), %xmm0
addsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, (%r15,%r13,4)
incq %r13
movss 0x18(%rbx), %xmm2
addl $0x2, %r14d
jmp 0x5284
movq -0x50(%rbp), %rcx
incl %ecx
movq -0x40(%rbp), %rbx
jmp 0x5250
movss 0xdf6(%rip), %xmm0      # 0x6118
xorl %eax, %eax
cvtsi2ss %eax, %xmm1
ucomiss %xmm1, %xmm2
jbe 0x5345
movss (%r15,%rax,4), %xmm1
ucomiss %xmm0, %xmm1
cmoval %eax, %ecx
maxss %xmm0, %xmm1
incq %rax
movaps %xmm1, %xmm0
jmp 0x5324
movl %ecx, -0x38(%rbp)
movl 0x50(%rbx), %eax
cvtsi2ss %rax, %xmm0
subss %xmm2, %xmm0
cvtss2sd %xmm0, %xmm0
addsd 0xd78(%rip), %xmm0      # 0x60d8
movaps %xmm2, -0x60(%rbp)
callq 0x2340
movaps -0x60(%rbp), %xmm3
xorl %eax, %eax
cvtsi2ss %eax, %xmm1
ucomiss %xmm1, %xmm3
jbe 0x5382
andl $0x0, (%r15,%rax,4)
incq %rax
jmp 0x536f
cvttsd2si %xmm0, %eax
movq %rax, -0x50(%rbp)
xorl %ecx, %ecx
movq -0x40(%rbp), %rdi
cvtsi2ss %ecx, %xmm0
ucomiss %xmm0, %xmm3
jbe 0x5453
movq -0x50(%rbp), %rax
movq %rcx, %rbx
addl %ecx, %eax
movl 0x58(%rdi), %ecx
xorl %r13d, %r13d
cmpl %ecx, %eax
movl %ecx, %edx
cmovbl %r13d, %edx
negl %edx
testl %eax, %eax
cmovsl %ecx, %edx
addl %eax, %edx
movslq %edx, %r12
xorl %r14d, %r14d
cvtsi2ss %r14d, %xmm0
ucomiss %xmm0, %xmm3
jbe 0x5449
movl 0x54(%rdi), %eax
addl %ebx, %eax
cvtsi2ss %rax, %xmm0
movq 0x60(%rdi), %rax
movsbl (%rax,%r12), %eax
cvtsi2sd %eax, %xmm1
movsd %xmm1, -0x60(%rbp)
divss %xmm3, %xmm0
addss %xmm0, %xmm0
cvtss2sd %xmm0, %xmm0
cvtsi2sd %r13d, %xmm1
movsd 0xcde(%rip), %xmm4      # 0x60e0
mulsd %xmm4, %xmm1
cvtss2sd %xmm3, %xmm2
divsd %xmm2, %xmm1
mulsd %xmm4, %xmm0
addsd %xmm1, %xmm0
callq 0x20a0
movq -0x40(%rbp), %rdi
cvtss2sd (%r15,%r14,4), %xmm1
mulsd -0x60(%rbp), %xmm0
addsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, (%r15,%r14,4)
incq %r14
movss 0x18(%rdi), %xmm3
addl $0x2, %r13d
jmp 0x53c3
movq %rbx, %rcx
incl %ecx
jmp 0x5390
movss 0xcbd(%rip), %xmm0      # 0x6118
xorl %eax, %eax
cvtsi2ss %eax, %xmm1
ucomiss %xmm1, %xmm3
jbe 0x547e
movss (%r15,%rax,4), %xmm1
ucomiss %xmm0, %xmm1
cmoval %eax, %ecx
maxss %xmm0, %xmm1
incq %rax
movaps %xmm1, %xmm0
jmp 0x545d
movl -0x38(%rbp), %edx
subl %ecx, %edx
movl %edx, %eax
negl %eax
cmovsl %edx, %eax
cvtsi2ss %eax, %xmm0
movaps %xmm0, %xmm1
subss %xmm3, %xmm1
mulss 0xc7f(%rip), %xmm3      # 0x611c
andps 0xc1c(%rip), %xmm1      # 0x60c0
cmpltss %xmm0, %xmm3
movaps %xmm3, %xmm2
andnps %xmm0, %xmm2
andps %xmm1, %xmm3
orps %xmm2, %xmm3
leaq -0x6c(%rbp), %rsi
movss %xmm3, (%rsi)
addq $0x68, %rdi
callq 0x4cdc
movq -0x40(%rbp), %rbx
movq -0x68(%rbp), %rsp
movl 0x54(%rbx), %eax
movl 0x58(%rbx), %ecx
movl 0x50(%rbx), %edx
incl %edx
movl %edx, 0x50(%rbx)
incl %eax
movl %eax, 0x54(%rbx)
cmpl %ecx, %edx
jbe 0x54e9
andl $0x0, 0x50(%rbx)
movq 0x78(%rbx), %rsi
movl 0x1c(%rbx), %ecx
pushq $0x4
popq %rdi
movl %ecx, %eax
cltd
idivl %edi
cltq
cmpq %rax, %rsi
jae 0x5507
xorl %r15d, %r15d
jmp 0x5607
leaq 0x68(%rbx), %r14
xorps %xmm0, %xmm0
movq %r14, %rax
movq (%rax), %rax
cmpq %r14, %rax
je 0x5520
addss 0x10(%rax), %xmm0
jmp 0x5511
cvtsi2ssl 0x9c(%rbx), %xmm1
ucomiss %xmm1, %xmm0
jbe 0x553e
cvttss2si %xmm0, %eax
movl %eax, 0x9c(%rbx)
cvttps2dq %xmm0, %xmm1
cvtdq2ps %xmm1, %xmm1
mulss 0x14(%rbx), %xmm1
xorl %eax, %eax
ucomiss %xmm1, %xmm0
seta %al
movl %eax, -0x44(%rbp)
movl 0x98(%rbx), %edx
cmpl $-0x1, %edx
je 0x55b0
cmpl %eax, %edx
je 0x55b6
leaq 0x80(%rbx), %r15
xorl %r13d, %r13d
leaq -0x44(%rbp), %r12
cvtsi2sd %r13d, %xmm0
movsd %xmm0, -0x60(%rbp)
cvtsi2ssl 0xa0(%rbx), %xmm0
cvtsi2ss %ecx, %xmm1
divss %xmm1, %xmm0
cvtss2sd %xmm0, %xmm0
addsd 0xb3f(%rip), %xmm0      # 0x60d0
callq 0x2340
ucomisd -0x60(%rbp), %xmm0
jbe 0x55c1
movq %r15, %rdi
movq %r12, %rsi
callq 0x4ce8
incl %r13d
movl 0x1c(%rbx), %ecx
jmp 0x556b
movl %eax, 0x98(%rbx)
incl 0xa0(%rbx)
xorl %r15d, %r15d
jmp 0x55ff
movq 0x80(%rbx), %rax
movq 0x88(%rbx), %rdx
subq %rax, %rdx
movq %rdx, %rcx
sarq $0x2, %rcx
cmpq $0x1, %rcx
movq %rbx, %rdi
jbe 0x55e9
cmpl $0x0, -0x4(%rax,%rdx)
je 0x5619
xorl %r15d, %r15d
movl -0x44(%rbp), %eax
movl %eax, 0x98(%rdi)
movl $0x1, 0xa0(%rdi)
movq %r14, %rdi
callq 0x4d06
movl %r15d, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %r15d, %r15d
cmpl $0x0, -0x8(%rax,%rdx)
jne 0x55ec
addl $-0x3, %ecx
pushq $0x1
popq %rdx
testl %ecx, %ecx
js 0x5644
movl %ecx, %esi
cmpl $0x1, (%rax,%rsi,4)
movl $0x0, %esi
cmovel %edx, %esi
orl %esi, %r15d
addl %edx, %edx
decl %ecx
jmp 0x5629
leaq 0xa8(%rdi), %r12
movl %r15d, %esi
movq %rdi, %rbx
callq 0x2fa8
leaq -0x29(%rbp), %rsi
movb %al, (%rsi)
movq %r12, %rdi
callq 0x4f0e
movq %rbx, %rdi
movq 0x80(%rbx), %rax
movb $0x1, %r15b
cmpq %rax, 0x88(%rbx)
je 0x55ec
movq %rax, 0x88(%rdi)
jmp 0x55ec
nop