// File: /workspace/github_cpp_makefile_star2_O2/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 %r12d, %r12d
negl %esi
movl $0x0, %eax
sbbl %eax, %eax
orl $0x1, %eax
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x10(%rsp)
movq %rdi, %rbx
incl 0x29cc4(%rdi)
pushq $0xd
popq %r13
movsd 0x7d82(%rip), %xmm3     # 0x2ac88
movq %rdi, %rbp
xorl %r14d, %r14d
movslq 0x29cc0(%rbx), %rax
cmpq %rax, %r14
jge 0x23097
imulq $0x6b0, %r14, %r15      # imm = 0x6B0
movsd 0x18(%rbx,%r15), %xmm0
mulsd 0x10(%rsp), %xmm0
mulsd 0x30(%rbx,%r15), %xmm0
addsd 0x8(%rbx,%r15), %xmm0
movq (%rbx,%r15), %rax
movsd %xmm0, (%rax)
movsd %xmm0, 0x8(%rbx,%r15)
movsd 0x40(%rbx,%r15), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x40(%rbx,%r15)
cvtsi2sdl 0x29cc4(%rbx), %xmm2
divsd %xmm2, %xmm1
movsd %xmm1, 0x50(%rbx,%r15)
subsd %xmm1, %xmm0
mulsd %xmm0, %xmm0
addsd 0x48(%rbx,%r15), %xmm0
movsd %xmm0, 0x48(%rbx,%r15)
divsd %xmm2, %xmm0
ucomisd 0x7cfc(%rip), %xmm0     # 0x2ac90
jb 0x22f9c
sqrtsd %xmm0, %xmm0
jmp 0x22fa9
callq 0x52f0
movsd 0x7cdf(%rip), %xmm3     # 0x2ac88
addq %rbx, %r15
movsd %xmm0, 0x58(%r15)
movsd 0x8(%r15), %xmm0
movslq 0x60(%r15), %rax
leal 0x1(%rax), %ecx
cmpq $0xc7, %rax
cmovgel %r12d, %ecx
movsd %xmm0, 0x68(%r15,%rax,8)
movl %ecx, 0x60(%r15)
xorpd %xmm2, %xmm2
movq %r13, %rax
cmpq $0xd5, %rax
je 0x22ff6
movsd (%rbp,%rax,8), %xmm0
divsd %xmm3, %xmm0
addsd %xmm0, %xmm2
incq %rax
jmp 0x22fdb
testq %r14, %r14
jne 0x23038
movq 0x11f9e(%rip), %rdi     # 0x34fa0
leaq 0x7ca7(%rip), %rsi      # 0x2acb0
movsd %xmm2, 0x8(%rsp)
callq 0x53b0
movq %rax, %rdi
movsd 0x8(%rsp), %xmm0
callq 0x56a0
movq %rax, %rdi
callq 0x51d0
movsd 0x8(%rsp), %xmm2
movsd 0x7c50(%rip), %xmm3     # 0x2ac88
xorpd %xmm0, %xmm0
movq %r13, %rax
cmpq $0xd5, %rax
je 0x23062
movsd (%rbp,%rax,8), %xmm1
subsd %xmm2, %xmm1
mulsd %xmm1, %xmm1
divsd %xmm3, %xmm1
addsd %xmm1, %xmm0
incq %rax
jmp 0x2303f
ucomisd 0x7c26(%rip), %xmm0     # 0x2ac90
jb 0x23072
sqrtsd %xmm0, %xmm0
jmp 0x2307f
callq 0x52f0
movsd 0x7c09(%rip), %xmm3     # 0x2ac88
movsd %xmm0, 0x6a8(%r15)
incq %r14
addq $0x6b0, %rbp            # imm = 0x6B0
jmp 0x22f0c
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq