// File: /workspace/github_cpp_makefile_star2_O1/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 $0x38, %rsp
movl %ecx, %ebx
movl %edx, %ebp
movq %rdi, %r14
movzbl %bpl, %eax
movq %rax, 0x30(%rsp)
movzbl %bl, %r13d
testb %bpl, %bpl
jle 0x97fa
movq %rsi, %r15
movl %ebp, 0x2c(%rsp)
movsd 0x2b254(%rip), %xmm4    # 0x348f0
xorl %ebp, %ebp
movapd 0x2b28a(%rip), %xmm2    # 0x34930
movsd 0x2b24a(%rip), %xmm6    # 0x348f8
xorpd %xmm0, %xmm0
xorpd %xmm3, %xmm3
testb %bl, %bl
jle 0x97d8
xorl %r12d, %r12d
movapd %xmm0, %xmm1
movapd %xmm4, %xmm5
movapd %xmm3, %xmm7
movq (%r14,%rbp,8), %rax
movsd (%rax,%r12,8), %xmm0
andpd %xmm2, %xmm0
ucomisd %xmm0, %xmm6
jbe 0x96f2
movapd %xmm7, %xmm3
movapd %xmm5, %xmm4
movapd %xmm1, %xmm0
jmp 0x97c0
movapd %xmm0, %xmm2
maxsd %xmm1, %xmm2
movsd %xmm2, (%rsp)
movsd %xmm5, 0x10(%rsp)
movsd %xmm7, 0x8(%rsp)
callq 0x4110
callq 0x4090
movsd 0x2b1db(%rip), %xmm6    # 0x348f8
movapd %xmm0, %xmm3
maxsd 0x8(%rsp), %xmm3
movq (%r14,%rbp,8), %rax
movsd (%rax,%r12,8), %xmm0
xorpd %xmm1, %xmm1
cmpltsd %xmm0, %xmm1
cmpltsd 0x2b1bd(%rip), %xmm0    # 0x34900
movsd 0x2b1a5(%rip), %xmm5    # 0x348f0
andpd %xmm5, %xmm0
movq (%r15,%rbp,8), %rax
movsd (%rax,%r12,8), %xmm4
movapd %xmm5, %xmm2
subsd %xmm4, %xmm2
andpd %xmm5, %xmm1
mulsd %xmm4, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movapd 0x2b1b7(%rip), %xmm1    # 0x34930
andpd %xmm1, %xmm4
andpd %xmm1, %xmm2
cmpltsd %xmm6, %xmm2
andpd %xmm5, %xmm2
cmpltsd %xmm6, %xmm4
movapd %xmm4, %xmm1
andnpd %xmm2, %xmm1
andpd %xmm5, %xmm4
movsd 0x10(%rsp), %xmm2
orpd %xmm1, %xmm4
addsd %xmm0, %xmm4
ucomisd %xmm4, %xmm2
ja 0x97b3
movapd %xmm2, %xmm4
movsd (%rsp), %xmm0
movapd 0x2b170(%rip), %xmm2    # 0x34930
incq %r12
movapd %xmm0, %xmm1
movapd %xmm4, %xmm5
movapd %xmm3, %xmm7
cmpq %r12, %r13
jne 0x96cd
incq %rbp
cmpq 0x30(%rsp), %rbp
jne 0x96b6
mulsd 0x2b12a(%rip), %xmm4    # 0x34918
movapd %xmm4, 0x10(%rsp)
movl 0x2c(%rsp), %ebp
jmp 0x9810
movsd 0x2b116(%rip), %xmm0    # 0x34918
movapd %xmm0, 0x10(%rsp)
xorpd %xmm3, %xmm3
xorpd %xmm0, %xmm0
movsd %xmm0, (%rsp)
movsd %xmm3, 0x8(%rsp)
movsd 0x2b0ed(%rip), %xmm0    # 0x34910
movapd %xmm3, %xmm1
callq 0x4420
movsd 0x8(%rsp), %xmm1
movsd (%rsp), %xmm2
divsd %xmm0, %xmm2
ucomisd 0x2b0bd(%rip), %xmm1    # 0x34900
setbe %al
movapd 0x10(%rsp), %xmm0
divsd %xmm2, %xmm0
movapd %xmm0, 0x10(%rsp)
testb %bpl, %bpl
setle %cl
orb %al, %cl
jne 0x98c1
xorl %r15d, %r15d
testb %bl, %bl
movapd 0x10(%rsp), %xmm0
jle 0x98b7
xorl %r12d, %r12d
movq (%r14,%r15,8), %rax
movsd (%rax,%r12,8), %xmm2
mulsd %xmm0, %xmm2
movsd %xmm2, (%rsp)
movsd 0x2b085(%rip), %xmm0    # 0x34910
callq 0x4420
movsd 0x8(%rsp), %xmm1
movsd (%rsp), %xmm2
divsd %xmm0, %xmm2
movapd 0x10(%rsp), %xmm0
movq (%r14,%r15,8), %rax
movsd %xmm2, (%rax,%r12,8)
incq %r12
cmpq %r12, %r13
jne 0x9870
incq %r15
cmpq 0x30(%rsp), %r15
jne 0x9863
movsd 0x2b047(%rip), %xmm0    # 0x34910
callq 0x4420
movapd 0x10(%rsp), %xmm1
divsd %xmm0, %xmm1
movapd %xmm1, %xmm0
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq