// File: /workspace/github_cpp_makefile_star2_O1/ATOMIC-LICHESS[P]ATOMKRAFT/tuning.cpp
// Function: VarTuning::update_vars(ResultPGN)
void VarTuning::update_vars(ResultPGN winner) {
	tune_t sign = (winner == WHITE_WINS ? 1 : -1);
	++updateCount;
	
	for (int k = 0; k < size; ++k) {
		*var[k].valueptr = var[k].value + (sign * var[k].delta * var[k].applyFactor);
		var[k].value = *var[k].valueptr;
		var[k].varSum += var[k].value;
		var[k].mean = var[k].varSum / updateCount;
		var[k].sdevSum += ((var[k].value - var[k].mean) * (var[k].value - var[k].mean));
		var[k].sdev = sqrt(var[k].sdevSum / updateCount);
		
		var[k].history[var[k].historyIndex] = var[k].value;
		++var[k].historyIndex;
		
		if (var[k].historyIndex >= TUNE_HISTORY_SIZE) {
			var[k].historyIndex = 0;
		}
		
		tune_t mean = 0;
		for (int i = 0; i < TUNE_HISTORY_SIZE; ++i) {
			mean += (var[k].history[i] / TUNE_HISTORY_SIZE);
		}
		
		if (k == 0) cout << "mean: " << mean << endl;
		
		tune_t sdev = 0;
		for (int i = 0; i < TUNE_HISTORY_SIZE; ++i) {
			sdev += ((var[k].history[i] - mean) * (var[k].history[i] - mean) / TUNE_HISTORY_SIZE); 
		}
		var[k].historySdev = sqrt(sdev);
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
xorl %r13d, %r13d
negl %esi
movl $0x0, %eax
sbbl %eax, %eax
incl 0x29cc4(%rdi)
cmpl $0x0, 0x29cc0(%rdi)
jle 0x2a782
movq %rdi, %rbx
orl $0x1, %eax
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x10(%rsp)
movsd 0x883f(%rip), %xmm3     # 0x32e08
movq %rdi, %rbp
xorl %r15d, %r15d
imulq $0x6b0, %r15, %r14      # imm = 0x6B0
movsd 0x18(%rbx,%r14), %xmm0
mulsd 0x10(%rsp), %xmm0
mulsd 0x30(%rbx,%r14), %xmm0
addsd 0x8(%rbx,%r14), %xmm0
movq (%rbx,%r14), %rax
movsd %xmm0, (%rax)
movsd %xmm0, 0x8(%rbx,%r14)
movsd 0x40(%rbx,%r14), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x40(%rbx,%r14)
xorps %xmm2, %xmm2
cvtsi2sdl 0x29cc4(%rbx), %xmm2
divsd %xmm2, %xmm1
movsd %xmm1, 0x50(%rbx,%r14)
subsd %xmm1, %xmm0
mulsd %xmm0, %xmm0
addsd 0x48(%rbx,%r14), %xmm0
movsd %xmm0, 0x48(%rbx,%r14)
divsd %xmm2, %xmm0
ucomisd 0x87c6(%rip), %xmm0     # 0x32e10
jb 0x2a652
sqrtsd %xmm0, %xmm0
jmp 0x2a65f
callq 0x5300
movsd 0x87a9(%rip), %xmm3     # 0x32e08
addq %rbx, %r14
movsd %xmm0, 0x58(%r14)
movsd 0x8(%r14), %xmm0
movslq 0x60(%r14), %rax
leal 0x1(%rax), %ecx
cmpq $0xc7, %rax
cmovgel %r13d, %ecx
movsd %xmm0, 0x68(%r14,%rax,8)
movl %ecx, 0x60(%r14)
xorpd %xmm2, %xmm2
movl $0xd, %eax
movsd (%rbp,%rax,8), %xmm0
divsd %xmm3, %xmm0
addsd %xmm0, %xmm2
incq %rax
cmpq $0xd5, %rax
jne 0x2a693
testq %r15, %r15
jne 0x2a718
movl $0x6, %edx
movq 0x118e3(%rip), %r12     # 0x3bfa0
movq %r12, %rdi
leaq 0x8769(%rip), %rsi      # 0x32e30
movsd %xmm2, 0x8(%rsp)
callq 0x5460
movq %r12, %rdi
movsd 0x8(%rsp), %xmm0
callq 0x56b0
movq %rax, %r12
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x53c0
movsbl %al, %esi
movq %r12, %rdi
callq 0x5050
movq %rax, %rdi
callq 0x5270
movsd 0x8(%rsp), %xmm2
movsd 0x86f0(%rip), %xmm3     # 0x32e08
xorpd %xmm0, %xmm0
movl $0xd, %eax
movsd (%rbp,%rax,8), %xmm1
subsd %xmm2, %xmm1
mulsd %xmm1, %xmm1
divsd %xmm3, %xmm1
addsd %xmm1, %xmm0
incq %rax
cmpq $0xd5, %rax
jne 0x2a721
ucomisd 0x86c6(%rip), %xmm0     # 0x32e10
jb 0x2a752
sqrtsd %xmm0, %xmm0
jmp 0x2a75f
callq 0x5300
movsd 0x86a9(%rip), %xmm3     # 0x32e08
movsd %xmm0, 0x6a8(%r14)
incq %r15
movslq 0x29cc0(%rbx), %rax
addq $0x6b0, %rbp            # imm = 0x6B0
cmpq %rax, %r15
jl 0x2a5cf
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop