// File: xbai0624[P]dnn/src/Matrix.cpp
// Function: Matrix::SumInSectionWithPadding(unsigned long, unsigned long, unsigned long, unsigned long)
double Matrix::SumInSectionWithPadding(size_t i_start, size_t i_end, size_t j_start, size_t j_end)
{
    // find element sum 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
    auto dim = Dimension();
    bool one_hit = false;

    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;
    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;
            one_hit = true;

            res += __M[i][j];
        }
    }

    if(!one_hit)
    {
        std::cout<<"Error: Sum in matrix section with padding: all elements exceeded range."<<std::endl;
        exit(0);
    }

    return res;
}

// Assembly:
subq $0x78, %rsp
movq %rdi, 0x70(%rsp)
movq %rsi, 0x68(%rsp)
movq %rdx, 0x60(%rsp)
movq %rcx, 0x58(%rsp)
movq %r8, 0x50(%rsp)
movq 0x70(%rsp), %rdi
movq %rdi, 0x8(%rsp)
callq 0x3bae0
movq %rax, 0x40(%rsp)
movq %rdx, 0x48(%rsp)
movb $0x0, 0x3f(%rsp)
movq 0x68(%rsp), %rax
movl %eax, %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, 0x17(%rsp)
jl 0x4097e
movq 0x60(%rsp), %rax
cmpq 0x68(%rsp), %rax
seta %al
movb %al, 0x17(%rsp)
movb 0x17(%rsp), %al
testb $0x1, %al
jne 0x40988
jmp 0x4098a
jmp 0x409a9
leaq 0x14a95(%rip), %rdi     # 0x55426
leaq 0x144f3(%rip), %rsi     # 0x54e8b
movl $0x49a, %edx            # imm = 0x49A
leaq 0x14b39(%rip), %rcx     # 0x554dd
callq 0x4170
movq 0x58(%rsp), %rax
movl %eax, %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, 0x7(%rsp)
jl 0x409cc
movq 0x50(%rsp), %rax
cmpq 0x58(%rsp), %rax
seta %al
movb %al, 0x7(%rsp)
movb 0x7(%rsp), %al
testb $0x1, %al
jne 0x409d6
jmp 0x409d8
jmp 0x409f7
leaq 0x14a6c(%rip), %rdi     # 0x5544b
leaq 0x144a5(%rip), %rsi     # 0x54e8b
movl $0x49b, %edx            # imm = 0x49B
leaq 0x14aeb(%rip), %rcx     # 0x554dd
callq 0x4170
leaq 0x40(%rsp), %rax
movq %rax, 0x30(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x28(%rsp)
movq 0x68(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x20(%rsp), %rax
cmpq 0x60(%rsp), %rax
jae 0x40aaa
movq 0x58(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x18(%rsp), %rax
cmpq 0x50(%rsp), %rax
jae 0x40a95
movq 0x20(%rsp), %rsi
movq 0x18(%rsp), %rdx
leaq 0x30(%rsp), %rdi
callq 0x40af0
testb $0x1, %al
jne 0x40a54
jmp 0x40a85
movq 0x8(%rsp), %rdi
movb $0x1, 0x3f(%rsp)
movq 0x20(%rsp), %rsi
callq 0x42670
movq %rax, %rdi
movq 0x18(%rsp), %rsi
callq 0x1fd60
movsd (%rax), %xmm0
addsd 0x28(%rsp), %xmm0
movsd %xmm0, 0x28(%rsp)
movq 0x18(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x18(%rsp)
jmp 0x40a2e
jmp 0x40a97
movq 0x20(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x20(%rsp)
jmp 0x40a14
testb $0x1, 0x3f(%rsp)
jne 0x40ada
movq 0x2a520(%rip), %rdi     # 0x6afd8
leaq 0x14a65(%rip), %rsi     # 0x55524
callq 0x4230
movq %rax, %rdi
movq 0x2a4e2(%rip), %rsi     # 0x6afb0
callq 0x4260
xorl %edi, %edi
callq 0x4290
movsd 0x28(%rsp), %xmm0
addq $0x78, %rsp
retq
nopw %cs:(%rax,%rax)