// File: xbai0624[P]dnn/src/Matrix.cpp
// Function: Matrix::MaxInSectionWithPadding(unsigned long, unsigned long, unsigned long, unsigned long, double)
double Matrix::MaxInSectionWithPadding(size_t i_start, size_t i_end, size_t j_start, size_t j_end, double padding_value)
{
    // find max element in section [i_start, i_end), and [j_start, j_end)
    // in this code, we all follow the rule: close front and open end
    // and all counters start from 0
    //
    // if range exceeded matirx dimension, then use padding value 
    auto dim = Dimension();
    bool at_least_one_element_in_range = false;

    assert(dim.first > 0 && dim.second > 0);
    assert((int)i_start >= 0 && i_end > i_start);
    assert((int)j_start >= 0 && j_end > j_start);

    auto in_range = [&](size_t ii, size_t jj) -> bool
    {
        if((int)ii>=0 && ii<dim.first && (int)jj>=0 && jj<dim.second)
            return true;
        return false;
    };

    double res = 0.;
    if(in_range(i_start, j_start))
        res = __M[i_start][j_start];
    else
        res = padding_value;

    for(size_t i=i_start;i<i_end;i++) {
        for(size_t j=j_start;j<j_end;j++)
        {
            if(! in_range(i, j)) continue;
            at_least_one_element_in_range = true;

            if(res < __M[i][j])
                res = __M[i][j];
        }
    }
    if(!at_least_one_element_in_range)
    {
        std::cout<<__func__<<"() Error: all elements exceeded range."<<std::endl;
        exit(0);
    }

    return res;
}

// Assembly:
subq $0x88, %rsp
movq %rdi, 0x80(%rsp)
movq %rsi, 0x78(%rsp)
movq %rdx, 0x70(%rsp)
movq %rcx, 0x68(%rsp)
movq %r8, 0x60(%rsp)
movsd %xmm0, 0x58(%rsp)
movq 0x80(%rsp), %rdi
movq %rdi, 0x10(%rsp)
callq 0x3bae0
movq %rax, 0x48(%rsp)
movq %rdx, 0x50(%rsp)
movb $0x0, 0x47(%rsp)
xorl %eax, %eax
cmpq $0x0, 0x48(%rsp)
movb %al, 0x1f(%rsp)
jbe 0x40525
cmpq $0x0, 0x50(%rsp)
seta %al
movb %al, 0x1f(%rsp)
movb 0x1f(%rsp), %al
testb $0x1, %al
jne 0x4052f
jmp 0x40531
jmp 0x40550
leaq 0x14e7f(%rip), %rdi     # 0x553b7
leaq 0x1494c(%rip), %rsi     # 0x54e8b
movl $0x459, %edx            # imm = 0x459
leaq 0x14e8c(%rip), %rcx     # 0x553d7
callq 0x4170
movq 0x78(%rsp), %rax
movl %eax, %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, 0xf(%rsp)
jl 0x40573
movq 0x70(%rsp), %rax
cmpq 0x78(%rsp), %rax
seta %al
movb %al, 0xf(%rsp)
movb 0xf(%rsp), %al
testb $0x1, %al
jne 0x4057d
jmp 0x4057f
jmp 0x4059e
leaq 0x14ea0(%rip), %rdi     # 0x55426
leaq 0x148fe(%rip), %rsi     # 0x54e8b
movl $0x45a, %edx            # imm = 0x45A
leaq 0x14e3e(%rip), %rcx     # 0x553d7
callq 0x4170
movq 0x68(%rsp), %rax
movl %eax, %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, 0xe(%rsp)
jl 0x405c1
movq 0x60(%rsp), %rax
cmpq 0x68(%rsp), %rax
seta %al
movb %al, 0xe(%rsp)
movb 0xe(%rsp), %al
testb $0x1, %al
jne 0x405cb
jmp 0x405cd
jmp 0x405ec
leaq 0x14e77(%rip), %rdi     # 0x5544b
leaq 0x148b0(%rip), %rsi     # 0x54e8b
movl $0x45b, %edx            # imm = 0x45B
leaq 0x14df0(%rip), %rcx     # 0x553d7
callq 0x4170
leaq 0x48(%rsp), %rax
movq %rax, 0x38(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x30(%rsp)
movq 0x78(%rsp), %rsi
movq 0x68(%rsp), %rdx
leaq 0x38(%rsp), %rdi
callq 0x40780
testb $0x1, %al
jne 0x40619
jmp 0x40641
movq 0x10(%rsp), %rdi
movq 0x78(%rsp), %rsi
callq 0x42670
movq %rax, %rdi
movq 0x68(%rsp), %rsi
callq 0x1fd60
movsd (%rax), %xmm0
movsd %xmm0, 0x30(%rsp)
jmp 0x4064d
movsd 0x58(%rsp), %xmm0
movsd %xmm0, 0x30(%rsp)
movq 0x78(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x28(%rsp), %rax
cmpq 0x70(%rsp), %rax
jae 0x40726
movq 0x68(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x20(%rsp), %rax
cmpq 0x60(%rsp), %rax
jae 0x40711
movq 0x28(%rsp), %rsi
movq 0x20(%rsp), %rdx
leaq 0x38(%rsp), %rdi
callq 0x40780
testb $0x1, %al
jne 0x4069b
jmp 0x406fe
movq 0x10(%rsp), %rdi
movb $0x1, 0x47(%rsp)
movsd 0x30(%rsp), %xmm0
movsd %xmm0, (%rsp)
movq 0x28(%rsp), %rsi
callq 0x42670
movq %rax, %rdi
movq 0x20(%rsp), %rsi
callq 0x1fd60
movsd (%rsp), %xmm1
movsd (%rax), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x406fc
movq 0x10(%rsp), %rdi
movq 0x28(%rsp), %rsi
callq 0x42670
movq %rax, %rdi
movq 0x20(%rsp), %rsi
callq 0x1fd60
movsd (%rax), %xmm0
movsd %xmm0, 0x30(%rsp)
jmp 0x406fe
movq 0x20(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x20(%rsp)
jmp 0x40671
jmp 0x40713
movq 0x28(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x28(%rsp)
jmp 0x40657
testb $0x1, 0x47(%rsp)
jne 0x40765
movq 0x2a8a4(%rip), %rdi     # 0x6afd8
leaq 0x14d35(%rip), %rsi     # 0x55470
callq 0x4230
movq %rax, %rdi
leaq 0x14d3e(%rip), %rsi     # 0x55488
callq 0x4230
movq %rax, %rdi
movq 0x2a857(%rip), %rsi     # 0x6afb0
callq 0x4260
xorl %edi, %edi
callq 0x4290
movsd 0x30(%rsp), %xmm0
addq $0x88, %rsp
retq
nopw %cs:(%rax,%rax)