// File: 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:
subq $0x48, %rsp
movq %rdi, 0x40(%rsp)
movl %esi, 0x3c(%rsp)
movq 0x40(%rsp), %rax
movq %rax, (%rsp)
movl 0x3c(%rsp), %esi
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movl $0x1, %edx
cmpl $0x0, %esi
cmovel %edx, %ecx
cvtsi2sd %ecx, %xmm0
movsd %xmm0, 0x30(%rsp)
movl 0x29cc4(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x29cc4(%rax)
movl $0x0, 0x2c(%rsp)
movq (%rsp), %rcx
movl 0x2c(%rsp), %eax
cmpl 0x29cc0(%rcx), %eax
jge 0x52357
movq (%rsp), %rax
movslq 0x2c(%rsp), %rcx
imulq $0x6b0, %rcx, %rdx      # imm = 0x6B0
movq %rax, %rcx
addq %rdx, %rcx
movsd 0x8(%rcx), %xmm1
movsd 0x30(%rsp), %xmm0
movslq 0x2c(%rsp), %rcx
imulq $0x6b0, %rcx, %rdx      # imm = 0x6B0
movq %rax, %rcx
addq %rdx, %rcx
mulsd 0x18(%rcx), %xmm0
movslq 0x2c(%rsp), %rcx
imulq $0x6b0, %rcx, %rdx      # imm = 0x6B0
movq %rax, %rcx
addq %rdx, %rcx
movsd 0x30(%rcx), %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movslq 0x2c(%rsp), %rcx
imulq $0x6b0, %rcx, %rdx      # imm = 0x6B0
movq %rax, %rcx
addq %rdx, %rcx
movq (%rcx), %rcx
movsd %xmm0, (%rcx)
movslq 0x2c(%rsp), %rcx
imulq $0x6b0, %rcx, %rdx      # imm = 0x6B0
movq %rax, %rcx
addq %rdx, %rcx
movq (%rcx), %rcx
movsd (%rcx), %xmm0
movslq 0x2c(%rsp), %rcx
imulq $0x6b0, %rcx, %rdx      # imm = 0x6B0
movq %rax, %rcx
addq %rdx, %rcx
movsd %xmm0, 0x8(%rcx)
movslq 0x2c(%rsp), %rcx
imulq $0x6b0, %rcx, %rdx      # imm = 0x6B0
movq %rax, %rcx
addq %rdx, %rcx
movsd 0x8(%rcx), %xmm0
movslq 0x2c(%rsp), %rcx
imulq $0x6b0, %rcx, %rdx      # imm = 0x6B0
movq %rax, %rcx
addq %rdx, %rcx
addsd 0x40(%rcx), %xmm0
movsd %xmm0, 0x40(%rcx)
movslq 0x2c(%rsp), %rcx
imulq $0x6b0, %rcx, %rdx      # imm = 0x6B0
movq %rax, %rcx
addq %rdx, %rcx
movsd 0x40(%rcx), %xmm0
cvtsi2sdl 0x29cc4(%rax), %xmm1
divsd %xmm1, %xmm0
movslq 0x2c(%rsp), %rcx
imulq $0x6b0, %rcx, %rdx      # imm = 0x6B0
movq %rax, %rcx
addq %rdx, %rcx
movsd %xmm0, 0x50(%rcx)
movslq 0x2c(%rsp), %rcx
imulq $0x6b0, %rcx, %rdx      # imm = 0x6B0
movq %rax, %rcx
addq %rdx, %rcx
movsd 0x8(%rcx), %xmm0
movslq 0x2c(%rsp), %rcx
imulq $0x6b0, %rcx, %rdx      # imm = 0x6B0
movq %rax, %rcx
addq %rdx, %rcx
subsd 0x50(%rcx), %xmm0
movslq 0x2c(%rsp), %rcx
imulq $0x6b0, %rcx, %rdx      # imm = 0x6B0
movq %rax, %rcx
addq %rdx, %rcx
movsd 0x8(%rcx), %xmm2
movslq 0x2c(%rsp), %rcx
imulq $0x6b0, %rcx, %rdx      # imm = 0x6B0
movq %rax, %rcx
addq %rdx, %rcx
subsd 0x50(%rcx), %xmm2
movslq 0x2c(%rsp), %rcx
imulq $0x6b0, %rcx, %rdx      # imm = 0x6B0
movq %rax, %rcx
addq %rdx, %rcx
movsd 0x48(%rcx), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x48(%rcx)
movslq 0x2c(%rsp), %rcx
imulq $0x6b0, %rcx, %rdx      # imm = 0x6B0
movq %rax, %rcx
addq %rdx, %rcx
movsd 0x48(%rcx), %xmm0
cvtsi2sdl 0x29cc4(%rax), %xmm1
divsd %xmm1, %xmm0
callq 0x63c0
movq (%rsp), %rax
movslq 0x2c(%rsp), %rcx
imulq $0x6b0, %rcx, %rdx      # imm = 0x6B0
movq %rax, %rcx
addq %rdx, %rcx
movsd %xmm0, 0x58(%rcx)
movslq 0x2c(%rsp), %rcx
imulq $0x6b0, %rcx, %rdx      # imm = 0x6B0
movq %rax, %rcx
addq %rdx, %rcx
movsd 0x8(%rcx), %xmm0
movslq 0x2c(%rsp), %rcx
imulq $0x6b0, %rcx, %rdx      # imm = 0x6B0
movq %rax, %rcx
addq %rdx, %rcx
movslq 0x2c(%rsp), %rdx
imulq $0x6b0, %rdx, %rsi      # imm = 0x6B0
movq %rax, %rdx
addq %rsi, %rdx
movslq 0x60(%rdx), %rdx
movsd %xmm0, 0x68(%rcx,%rdx,8)
movslq 0x2c(%rsp), %rcx
imulq $0x6b0, %rcx, %rdx      # imm = 0x6B0
movq %rax, %rcx
addq %rdx, %rcx
movl 0x60(%rcx), %edx
addl $0x1, %edx
movl %edx, 0x60(%rcx)
movslq 0x2c(%rsp), %rcx
imulq $0x6b0, %rcx, %rcx      # imm = 0x6B0
addq %rcx, %rax
cmpl $0xc8, 0x60(%rax)
jl 0x52201
movq (%rsp), %rax
movslq 0x2c(%rsp), %rcx
imulq $0x6b0, %rcx, %rcx      # imm = 0x6B0
addq %rcx, %rax
movl $0x0, 0x60(%rax)
xorps %xmm0, %xmm0
movsd %xmm0, 0x20(%rsp)
movl $0x0, 0x1c(%rsp)
cmpl $0xc8, 0x1c(%rsp)
jge 0x5225f
movq (%rsp), %rax
movslq 0x2c(%rsp), %rcx
imulq $0x6b0, %rcx, %rcx      # imm = 0x6B0
addq %rcx, %rax
movslq 0x1c(%rsp), %rcx
movsd 0x68(%rax,%rcx,8), %xmm0
movsd 0x9b76(%rip), %xmm1     # 0x5bdb8
divsd %xmm1, %xmm0
addsd 0x20(%rsp), %xmm0
movsd %xmm0, 0x20(%rsp)
movl 0x1c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c(%rsp)
jmp 0x52212
cmpl $0x0, 0x2c(%rsp)
jne 0x52296
movq 0x19d4b(%rip), %rdi     # 0x6bfb8
leaq 0x9b4c(%rip), %rsi      # 0x5bdc0
callq 0x64b0
movq %rax, %rdi
movsd 0x20(%rsp), %xmm0
callq 0x6a10
movq %rax, %rdi
movq 0x19cff(%rip), %rsi     # 0x6bf90
callq 0x6520
xorps %xmm0, %xmm0
movsd %xmm0, 0x10(%rsp)
movl $0x0, 0xc(%rsp)
cmpl $0xc8, 0xc(%rsp)
jge 0x52321
movq (%rsp), %rax
movslq 0x2c(%rsp), %rcx
imulq $0x6b0, %rcx, %rdx      # imm = 0x6B0
movq %rax, %rcx
addq %rdx, %rcx
movslq 0xc(%rsp), %rdx
movsd 0x68(%rcx,%rdx,8), %xmm0
subsd 0x20(%rsp), %xmm0
movslq 0x2c(%rsp), %rcx
imulq $0x6b0, %rcx, %rcx      # imm = 0x6B0
addq %rcx, %rax
movslq 0xc(%rsp), %rcx
movsd 0x68(%rax,%rcx,8), %xmm1
subsd 0x20(%rsp), %xmm1
mulsd %xmm1, %xmm0
movsd 0x9ab4(%rip), %xmm1     # 0x5bdb8
divsd %xmm1, %xmm0
addsd 0x10(%rsp), %xmm0
movsd %xmm0, 0x10(%rsp)
movl 0xc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc(%rsp)
jmp 0x522a7
movsd 0x10(%rsp), %xmm0
callq 0x63c0
movq (%rsp), %rax
movslq 0x2c(%rsp), %rcx
imulq $0x6b0, %rcx, %rcx      # imm = 0x6B0
addq %rcx, %rax
movsd %xmm0, 0x6a8(%rax)
movl 0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2c(%rsp)
jmp 0x51f8b
addq $0x48, %rsp
retq
nopl (%rax)