// File: xbai0624[P]dnn/src/Matrix.cpp
// Function: Matrix::AverageInSectionWithPadding(unsigned long, unsigned long, unsigned long, unsigned long)
double Matrix::AverageInSectionWithPadding(size_t i_start, size_t i_end, size_t j_start, size_t j_end)
{
    // find element average 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;
    };

    size_t x_range = i_end < dim.first ? i_end : dim.first;
    size_t y_range = j_end < dim.second? j_end : dim.second;

    double res =  0;
    for(size_t i=i_start;i<x_range;i++){
        for(size_t j=j_start;j<y_range;j++)
        {
            if(!in_range(i, j)) continue;
            one_hit = true;

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

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


    res = res / (int)(x_range-i_start)/(int)(y_range - j_start); 
    return res;
}

// Assembly:
subq $0x98, %rsp
movq %rdi, 0x90(%rsp)
movq %rsi, 0x88(%rsp)
movq %rdx, 0x80(%rsp)
movq %rcx, 0x78(%rsp)
movq %r8, 0x70(%rsp)
movq 0x90(%rsp), %rdi
movq %rdi, 0x18(%rsp)
callq 0x3bae0
movq %rax, 0x60(%rsp)
movq %rdx, 0x68(%rsp)
movb $0x0, 0x5f(%rsp)
movq 0x88(%rsp), %rax
movl %eax, %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, 0x27(%rsp)
jl 0x40d36
movq 0x80(%rsp), %rax
cmpq 0x88(%rsp), %rax
seta %al
movb %al, 0x27(%rsp)
movb 0x27(%rsp), %al
testb $0x1, %al
jne 0x40d40
jmp 0x40d42
jmp 0x40d61
leaq 0x146dd(%rip), %rdi     # 0x55426
leaq 0x1413b(%rip), %rsi     # 0x54e8b
movl $0x4d7, %edx            # imm = 0x4D7
leaq 0x14810(%rip), %rcx     # 0x5556c
callq 0x4170
movq 0x78(%rsp), %rax
movl %eax, %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, 0x17(%rsp)
jl 0x40d84
movq 0x70(%rsp), %rax
cmpq 0x78(%rsp), %rax
seta %al
movb %al, 0x17(%rsp)
movb 0x17(%rsp), %al
testb $0x1, %al
jne 0x40d8e
jmp 0x40d90
jmp 0x40daf
leaq 0x146b4(%rip), %rdi     # 0x5544b
leaq 0x140ed(%rip), %rsi     # 0x54e8b
movl $0x4d8, %edx            # imm = 0x4D8
leaq 0x147c2(%rip), %rcx     # 0x5556c
callq 0x4170
leaq 0x60(%rsp), %rax
movq %rax, 0x50(%rsp)
movq 0x80(%rsp), %rax
cmpq 0x60(%rsp), %rax
jae 0x40dd7
movq 0x80(%rsp), %rax
movq %rax, 0x8(%rsp)
jmp 0x40de1
movq 0x60(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x8(%rsp), %rax
movq %rax, 0x48(%rsp)
movq 0x70(%rsp), %rax
cmpq 0x68(%rsp), %rax
jae 0x40e02
movq 0x70(%rsp), %rax
movq %rax, (%rsp)
jmp 0x40e0b
movq 0x68(%rsp), %rax
movq %rax, (%rsp)
movq (%rsp), %rax
movq %rax, 0x40(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x38(%rsp)
movq 0x88(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x30(%rsp), %rax
cmpq 0x48(%rsp), %rax
jae 0x40ec0
movq 0x78(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x28(%rsp), %rax
cmpq 0x40(%rsp), %rax
jae 0x40eab
movq 0x30(%rsp), %rsi
movq 0x28(%rsp), %rdx
leaq 0x50(%rsp), %rdi
callq 0x40f40
testb $0x1, %al
jne 0x40e6a
jmp 0x40e9b
movq 0x18(%rsp), %rdi
movb $0x1, 0x5f(%rsp)
movq 0x30(%rsp), %rsi
callq 0x42670
movq %rax, %rdi
movq 0x28(%rsp), %rsi
callq 0x1fd60
movsd (%rax), %xmm0
addsd 0x38(%rsp), %xmm0
movsd %xmm0, 0x38(%rsp)
movq 0x28(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x28(%rsp)
jmp 0x40e44
jmp 0x40ead
movq 0x30(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x30(%rsp)
jmp 0x40e2a
testb $0x1, 0x5f(%rsp)
jne 0x40ef0
movq 0x2a10a(%rip), %rdi     # 0x6afd8
leaq 0x146e2(%rip), %rsi     # 0x555b7
callq 0x4230
movq %rax, %rdi
movq 0x2a0cc(%rip), %rsi     # 0x6afb0
callq 0x4260
xorl %edi, %edi
callq 0x4290
movsd 0x38(%rsp), %xmm0
movq 0x48(%rsp), %rax
subq 0x88(%rsp), %rax
cvtsi2sd %eax, %xmm1
divsd %xmm1, %xmm0
movq 0x40(%rsp), %rax
subq 0x78(%rsp), %rax
cvtsi2sd %eax, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x38(%rsp)
movsd 0x38(%rsp), %xmm0
addq $0x98, %rsp
retq
nopw %cs:(%rax,%rax)