// File: /workspace/github_c_makefile_star2_O3/calmopyrin[P]tedplay/Filter.cpp
// Function: Filter::reCalcWindowTable()
void Filter::reCalcWindowTable()
{
	int i;
	const double pi = 4 * atan(1.0);
	const double f = fc_;
	double gain = double(1 << precision_) - 1.0; // 4095.0
	double kernelSum = 0;
	
	// #intervals between time steps for resampling
	timeStep = 1U;

	windowTableSize = order_ * int(timeStep);
	int midorder = windowTableSize / 2;
	double *dblCoeffs = new double[windowTableSize + 1];

	if (sampleHistory_) delete [] sampleHistory_;
	sampleHistory_ = new int [windowTableSize + 1];

	if (windowTable_) delete [] windowTable_;
	windowTable_ = new int[windowTableSize + 1];

	// this is used for resampling
	for (i = 0; i < windowTableSize; i++) {
		double j = i - midorder;
		double x = pi * j / double(timeStep);

		// Blackman has better stop-band attenuation
		double blackman = 0.42 + 0.5 * cos(2 * x / double(windowTableSize)) 
			- 0.08*cos(4 * pi * j / double(windowTableSize));
		// Hamming has 20% faster roll-off
		double hamming = 0.54 + 0.46 * cos(2 * x / double(windowTableSize));
		// von Hann is in between
		double vonHann = 0.5 * (1.0 + cos(2 * x / double(windowTableSize)));
		// Blackman-Harris window
		double a0 = 0.35875, a1 = 0.48229, a2 = 0.14128, a3 = 0.01168;
		double blackmanHarris = a0 + a1 * cos(2 * x / double(windowTableSize)) 
			- a2 * cos(4 * x / double(windowTableSize))
			+ a3 * cos(6 * x / double(windowTableSize));

		double c = (j == 0) ? 2.0 * f : sin(2.0 * f * x ) / x;

		c *= vonHann;
		dblCoeffs[i] = c;
		kernelSum += c;
	}

	for (i = 0; i < windowTableSize; i++) {
		double rebasedCoeff = dblCoeffs[i] / kernelSum * gain * mixingVolume;
		windowTable_[i] = (int)(rebasedCoeff + 0.5);
		sampleHistory_[i] = 0;
	}
	// Now decrease filter order if side coeffs are zero...
	i = 0;
	while (0 == windowTable_[i] && i < midorder) i++;
	if (i) {
		int k;
		int drop = i;

		i = 0;
		for(k = drop; k <= midorder; k++, i++)
			windowTable_[i] = windowTable_[k];
		order_ = order_ - drop * 2;

		if (i > 1) {
			int *b = windowTable_ + i - 2;
			do {
				windowTable_[i] = *b--;
			} while (++i < order_);
		}
		midorder = (order_ - 1) / 2;
	}
	windowTableSize = order_ * int(timeStep);
	if (!windowTableSize) {
		windowTableSize = 1;
		windowTable_[0] = int(gain);
		windowTable_[1] = 0;
	}

	sampleBufPtr_ = 0;
	sampleBufMask_ = (midorder << 1) + 1;

	delete [] dblCoeffs;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %rbx
movsd 0x30(%rdi), %xmm0
movsd %xmm0, 0x18(%rsp)
movb 0x40(%rdi), %cl
movl $0x1, %eax
movl %eax, 0x38(%rdi)
shll %cl, %eax
movl %eax, 0x8(%rsp)
movslq 0x8(%rdi), %r13
movl %r13d, %r12d
shrl $0x1f, %r12d
addl %r13d, %r12d
cmpq $-0x1, %r13
movl %r13d, 0x18(%rdi)
leaq 0x8(,%r13,8), %rdi
movq $-0x1, %r15
cmovlq %r15, %rdi
callq 0x3040
movq %rax, %r14
movq 0x20(%rbx), %rdi
testq %rdi, %rdi
je 0x83c4
callq 0x32a0
movslq 0x18(%rbx), %r13
movq %r13, %rbp
incq %rbp
xorps %xmm0, %xmm0
cvtsi2sdl 0x8(%rsp), %xmm0
movsd %xmm0, 0x10(%rsp)
sarl %r12d
shlq $0x2, %rbp
cmpl $-0x1, %r13d
cmovgeq %rbp, %r15
movq %r15, %rdi
callq 0x3040
movq %rax, 0x20(%rbx)
movq 0x10(%rbx), %rdi
testq %rdi, %rdi
je 0x840e
callq 0x32a0
movslq 0x18(%rbx), %r13
leaq 0x4(,%r13,4), %rbp
movsd 0x10(%rsp), %xmm0
addsd 0x7c14(%rip), %xmm0     # 0x10030
movsd %xmm0, 0x10(%rsp)
cmpl $-0x1, %r13d
movq $-0x1, %rdi
cmovgeq %rbp, %rdi
callq 0x3040
movq %rax, 0x10(%rbx)
testl %r13d, %r13d
jle 0x860e
movsd 0x18(%rsp), %xmm0
addsd %xmm0, %xmm0
movsd %xmm0, 0x18(%rsp)
movl %r12d, %r15d
negl %r15d
xorpd %xmm3, %xmm3
xorl %ebp, %ebp
movsd %xmm3, 0x30(%rsp)
leal (%r15,%rbp), %eax
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
movsd %xmm1, 0x20(%rsp)
movl 0x38(%rbx), %eax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
mulsd 0x7baf(%rip), %xmm1     # 0x10038
divsd %xmm0, %xmm1
movsd %xmm1, 0x8(%rsp)
movapd %xmm1, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x28(%rsp)
xorps %xmm1, %xmm1
cvtsi2sd %r13d, %xmm1
divsd %xmm1, %xmm0
callq 0x33a0
movsd 0x20(%rsp), %xmm1
mulsd 0x7b80(%rip), %xmm1     # 0x10040
xorps %xmm0, %xmm0
cvtsi2sdl 0x18(%rbx), %xmm0
divsd %xmm0, %xmm1
movapd %xmm1, %xmm0
callq 0x33a0
xorps %xmm1, %xmm1
cvtsi2sdl 0x18(%rbx), %xmm1
movsd 0x28(%rsp), %xmm0
divsd %xmm1, %xmm0
callq 0x33a0
xorps %xmm1, %xmm1
cvtsi2sdl 0x18(%rbx), %xmm1
movsd 0x28(%rsp), %xmm0
divsd %xmm1, %xmm0
callq 0x33a0
addsd 0x6b05(%rip), %xmm0     # 0xf010
mulsd 0x7b35(%rip), %xmm0     # 0x10048
movsd %xmm0, 0x20(%rsp)
xorps %xmm1, %xmm1
cvtsi2sdl 0x18(%rbx), %xmm1
movsd 0x28(%rsp), %xmm0
divsd %xmm1, %xmm0
callq 0x33a0
movsd 0x8(%rsp), %xmm0
mulsd 0x7b12(%rip), %xmm0     # 0x10050
xorps %xmm1, %xmm1
cvtsi2sdl 0x18(%rbx), %xmm1
divsd %xmm1, %xmm0
callq 0x33a0
movsd 0x8(%rsp), %xmm0
mulsd 0x7afb(%rip), %xmm0     # 0x10058
xorps %xmm1, %xmm1
cvtsi2sdl 0x18(%rbx), %xmm1
divsd %xmm1, %xmm0
callq 0x33a0
movsd 0x18(%rsp), %xmm0
cmpq %rbp, %r12
je 0x8590
movsd 0x18(%rsp), %xmm0
mulsd 0x8(%rsp), %xmm0
callq 0x30a0
divsd 0x8(%rsp), %xmm0
movsd 0x20(%rsp), %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, (%r14,%rbp,8)
movsd 0x30(%rsp), %xmm3
addsd %xmm1, %xmm3
incq %rbp
movslq 0x18(%rbx), %r13
cmpq %r13, %rbp
jl 0x845f
movq 0x10(%rbx), %rax
testl %r13d, %r13d
jle 0x860e
movsd 0x48(%rbx), %xmm0
movq 0x20(%rbx), %rcx
xorl %edx, %edx
movsd 0x10(%rsp), %xmm2
movsd 0x7a6c(%rip), %xmm4     # 0x10048
movsd (%r14,%rdx,8), %xmm1
divsd %xmm3, %xmm1
mulsd %xmm2, %xmm1
mulsd %xmm0, %xmm1
addsd %xmm4, %xmm1
cvttsd2si %xmm1, %esi
movl %esi, (%rax,%rdx,4)
movl $0x0, (%rcx,%rdx,4)
incq %rdx
movslq 0x18(%rbx), %rsi
cmpq %rsi, %rdx
jl 0x85dc
jmp 0x8614
movsd 0x10(%rsp), %xmm2
movslq %r12d, %rdi
movl $0x4, %edx
movl $0xfffffffe, %ecx       # imm = 0xFFFFFFFE
xorl %r8d, %r8d
leaq 0x1(%r8), %rsi
addq $-0x4, %rdx
addl $0x2, %ecx
cmpq %rdi, %r8
jge 0x863e
cmpl $0x0, (%rax,%r8,4)
movq %rsi, %r8
je 0x8624
cmpq $0x1, %rsi
jne 0x8649
movl 0x8(%rbx), %edx
jmp 0x86bb
leaq -0x1(%rsi), %rdi
cmpl %edi, %r12d
jge 0x865c
movl 0x8(%rbx), %edx
subl %ecx, %edx
movl %edx, 0x8(%rbx)
jmp 0x86ab
movq %rax, %rdi
subq %rdx, %rdi
subq %rsi, %r12
addq $0x2, %r12
movl %r12d, %edx
xorl %esi, %esi
movl (%rdi), %r8d
movl %r8d, (%rax,%rsi,4)
incq %rsi
addq $0x4, %rdi
cmpq %rsi, %rdx
jne 0x866e
movl 0x8(%rbx), %edx
subl %ecx, %edx
movl %edx, 0x8(%rbx)
cmpl $0x2, %r12d
jb 0x86ab
movl %r12d, %ecx
leaq -0x2(%rcx), %rsi
movl (%rax,%rsi,4), %edx
movl %edx, (%rax,%rcx,4)
incq %rcx
movslq 0x8(%rbx), %rdx
decq %rsi
cmpq %rdx, %rcx
jl 0x8696
leal -0x1(%rdx), %ecx
shrl $0x1f, %ecx
leal (%rdx,%rcx), %r12d
decl %r12d
sarl %r12d
imull 0x38(%rbx), %edx
movl %edx, 0x18(%rbx)
testl %edx, %edx
jne 0x86da
movl $0x1, 0x18(%rbx)
cvttsd2si %xmm2, %ecx
movl %ecx, (%rax)
movl $0x0, 0x4(%rax)
movl $0x0, 0x28(%rbx)
leal 0x1(,%r12,2), %eax
movl %eax, 0x2c(%rbx)
movq %r14, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x32a0