// File: /workspace/github_cpp_makefile_star2_O2/myudelson[P]hmm-scalable/utils.cpp
// Function: doLog10Scale2DGentle(double**, double**, signed char, signed char)
NUMBER doLog10Scale2DGentle(NUMBER **grad, NUMBER **par, NPAR size1, NPAR size2) {
	NPAR i,j;
	NUMBER max_10_scale = 0, candidate, min_delta = 1, max_grad = 0;
	for(i=0; i<size1; i++)
		for(j=0; j<size2; j++) {
			if( fabs(grad[i][j]) < SAFETY ) // 0 gradient
				continue;
            // scale
            if(max_grad < fabs(grad[i][j]))
                max_grad = fabs(grad[i][j]);
			candidate = ceil( log10( fabs(grad[i][j]) ) );
			if(candidate > max_10_scale)
				max_10_scale = candidate;
            // delta: if grad<0 - distance to 1, if grad>0 - distance to 0
            candidate = (grad[i][j]<0)*(1-par[i][j]) + (grad[i][j]>0)*(par[i][j]) +
            ( (fabs(par[i][j])< SAFETY) || (fabs(1-par[i][j])< SAFETY) ); // these terms are there to avoid already extreme 0, 1 values
            if( candidate < min_delta)
                min_delta = candidate;
		}
    max_grad = max_grad / pow(10, max_10_scale);
    if(max_10_scale >0 )
		for(i=0; i<size1; i++)
			for(j=0; j<size2; j++)
				grad[i][j] = ( 0.95 * min_delta / max_grad) * grad[i][j] / pow(10, max_10_scale);
	return ( 0.95 * min_delta / max_grad ) / pow(10, max_10_scale);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %r14
movq %rdi, %rbx
xorl %r13d, %r13d
testb %cl, %cl
cmovlel %r13d, %ecx
testb %dl, %dl
cmovlel %r13d, %edx
movslq %edx, %r15
movzbl %cl, %r12d
movsd 0x2585c(%rip), %xmm2    # 0x2c8f0
movapd 0x25894(%rip), %xmm3    # 0x2c930
movsd 0x25854(%rip), %xmm6    # 0x2c8f8
xorpd %xmm1, %xmm1
xorpd %xmm4, %xmm4
cmpq %r15, %r13
je 0x71c4
xorl %ebp, %ebp
cmpq %rbp, %r12
je 0x71bc
movq (%rbx,%r13,8), %rax
movsd (%rax,%rbp,8), %xmm0
andpd %xmm3, %xmm0
ucomisd %xmm0, %xmm6
jbe 0x70e0
movapd %xmm1, %xmm0
movapd %xmm2, %xmm7
jmp 0x71ac
movapd %xmm0, %xmm3
maxsd %xmm4, %xmm3
movsd %xmm3, (%rsp)
movsd %xmm2, 0x8(%rsp)
movsd %xmm1, 0x10(%rsp)
callq 0x4100
callq 0x4090
movsd 0x257ed(%rip), %xmm6    # 0x2c8f8
maxsd 0x10(%rsp), %xmm0
movq (%rbx,%r13,8), %rax
movsd (%rax,%rbp,8), %xmm1
xorpd %xmm2, %xmm2
cmpltsd %xmm1, %xmm2
movapd %xmm1, %xmm3
cmpltsd 0x257d0(%rip), %xmm3    # 0x2c900
movsd 0x257b8(%rip), %xmm5    # 0x2c8f0
andpd %xmm5, %xmm3
movq (%r14,%r13,8), %rax
movsd (%rax,%rbp,8), %xmm7
movapd %xmm5, %xmm4
subsd %xmm7, %xmm4
andpd %xmm5, %xmm2
mulsd %xmm7, %xmm2
mulsd %xmm4, %xmm3
addsd %xmm2, %xmm3
movapd 0x257cb(%rip), %xmm2    # 0x2c930
andpd %xmm2, %xmm7
andpd %xmm2, %xmm4
cmpltsd %xmm6, %xmm4
andpd %xmm5, %xmm4
cmpltsd %xmm6, %xmm7
movapd %xmm7, %xmm2
andnpd %xmm4, %xmm2
andpd %xmm5, %xmm7
orpd %xmm2, %xmm7
movsd 0x8(%rsp), %xmm2
addsd %xmm3, %xmm7
ucomisd %xmm7, %xmm2
ja 0x719f
movapd %xmm2, %xmm7
movapd 0x25789(%rip), %xmm3    # 0x2c930
movsd (%rsp), %xmm4
incq %rbp
movapd %xmm0, %xmm1
movapd %xmm7, %xmm2
jmp 0x70b7
incq %r13
jmp 0x70ac
movsd 0x25744(%rip), %xmm0    # 0x2c910
movsd %xmm2, 0x8(%rsp)
movsd %xmm1, 0x10(%rsp)
movsd %xmm4, (%rsp)
callq 0x4440
movsd 0x10(%rsp), %xmm1
movsd 0x8(%rsp), %xmm2
xorpd %xmm3, %xmm3
ucomisd %xmm3, %xmm1
movsd (%rsp), %xmm3
divsd %xmm0, %xmm3
mulsd 0x25711(%rip), %xmm2    # 0x2c918
divsd %xmm3, %xmm2
movsd %xmm2, 0x8(%rsp)
jbe 0x726c
xorl %r14d, %r14d
cmpq %r15, %r14
je 0x726c
xorl %r13d, %r13d
cmpq %r13, %r12
je 0x7267
movq (%rbx,%r14,8), %rax
movsd (%rax,%r13,8), %xmm0
mulsd %xmm2, %xmm0
movsd %xmm0, (%rsp)
movsd 0x256d2(%rip), %xmm0    # 0x2c910
callq 0x4440
movsd 0x10(%rsp), %xmm1
movsd 0x8(%rsp), %xmm2
movsd (%rsp), %xmm3
divsd %xmm0, %xmm3
movq (%rbx,%r14,8), %rax
movsd %xmm3, (%rax,%r13,8)
incq %r13
jmp 0x721e
incq %r14
jmp 0x7216
movsd 0x2569c(%rip), %xmm0    # 0x2c910
callq 0x4440
movsd 0x8(%rsp), %xmm1
divsd %xmm0, %xmm1
movapd %xmm1, %xmm0
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq