// File: xbai0624[P]dnn/src/ConstructLayer.cpp
// Function: ConstructLayer::InitNeuronsPooling()
void ConstructLayer::InitNeuronsPooling()
{
    // clear
    __neurons.clear();

    // get output image size of previous layer (input image size for this layer)
    assert(__prevLayer != nullptr);
    auto size_prev_layer = __prevLayer->GetOutputImageSize();
    //std::cout<<__func__<<" prev layer output image size: "<<size_prev_layer<<std::endl;

    int n_row = size_prev_layer.first;
    int n_col = size_prev_layer.second;

    assert(__prevLayer->GetNumberOfKernelsCNN() == __n_kernels_cnn);

    // deduct output image dimension
    //     !!! NOTE !!! pooling layer is different with cnn layer
    //     !!!          pooling layer kernel should have no coverage overlap on the input image
    int x_size = (int)n_row / (int)__kernelDim.first;
    int y_size = (int)n_col / (int)__kernelDim.second;
    if(x_size <= 0) x_size = 1; // small images will be complemented by padding
    if(y_size <= 0) y_size = 1; // so it is safe to set size >= 1

    if((int)n_row%(int)__kernelDim.first != 0) x_size += 1; // residue will be complemented by padding
    if((int)n_col%(int)__kernelDim.second != 0) y_size += 1; // residue will be complemented by padding

    // pooling layer borrowed variabe '__outputImageSizeCNN', to reduce memory
    __outputImageSizeCNN.first = x_size;
    __outputImageSizeCNN.second = y_size;
    assert(__outputImageSizeCNN.first >=1);
    assert(__outputImageSizeCNN.second >=1);

    for(size_t k=0;k<__n_kernels_cnn;k++)
    {
        Pixel2D<Neuron*> image(__outputImageSizeCNN.first, __outputImageSizeCNN.second);
        for(size_t i=0;i<__outputImageSizeCNN.first;i++)
        {
            for(size_t j=0;j<__outputImageSizeCNN.second;j++)
            {
                Neuron *n = new Neuron();
                n->SetCoord(i, j, k); // set neuron coord
                n->SetActuationFuncType(__neuron_actuation_func_type);
                image[i][j] = n;
            }
        }
        __neurons.push_back(image);
    }
}

// Assembly:
subq $0x98, %rsp
movq %rdi, 0x90(%rsp)
movq 0x90(%rsp), %rdi
movq %rdi, 0x20(%rsp)
addq $0x40, %rdi
callq 0x1f670
movq 0x20(%rsp), %rax
cmpq $0x0, 0x3f0(%rax)
je 0x82e6
jmp 0x8305
leaq 0x49ece(%rip), %rdi     # 0x521bb
leaq 0x49ede(%rip), %rsi     # 0x521d2
movl $0x188, %edx            # imm = 0x188
leaq 0x49f5f(%rip), %rcx     # 0x5225f
callq 0x4170
movq 0x20(%rsp), %rax
movq 0x3f0(%rax), %rdi
movq (%rdi), %rax
callq *0x270(%rax)
movq %rax, %rcx
movq 0x20(%rsp), %rax
movq %rcx, 0x80(%rsp)
movq %rdx, 0x88(%rsp)
movq 0x80(%rsp), %rcx
movl %ecx, 0x7c(%rsp)
movq 0x88(%rsp), %rcx
movl %ecx, 0x78(%rsp)
movq 0x3f0(%rax), %rdi
movq (%rdi), %rax
callq *0x288(%rax)
movq 0x20(%rsp), %rcx
cmpq 0x18(%rcx), %rax
jne 0x8367
jmp 0x8386
leaq 0x49f1b(%rip), %rdi     # 0x52289
leaq 0x49e5d(%rip), %rsi     # 0x521d2
movl $0x18f, %edx            # imm = 0x18F
leaq 0x49ede(%rip), %rcx     # 0x5225f
callq 0x4170
movq 0x20(%rsp), %rcx
movl 0x7c(%rsp), %eax
movq 0x20(%rcx), %rdx
movl %edx, %esi
cltd
idivl %esi
movl %eax, 0x74(%rsp)
movl 0x78(%rsp), %eax
movq 0x28(%rcx), %rcx
cltd
idivl %ecx
movl %eax, 0x70(%rsp)
cmpl $0x0, 0x74(%rsp)
jg 0x83ba
movl $0x1, 0x74(%rsp)
cmpl $0x0, 0x70(%rsp)
jg 0x83c9
movl $0x1, 0x70(%rsp)
movq 0x20(%rsp), %rcx
movl 0x7c(%rsp), %eax
movq 0x20(%rcx), %rcx
cltd
idivl %ecx
cmpl $0x0, %edx
je 0x83e9
movl 0x74(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x74(%rsp)
movq 0x20(%rsp), %rcx
movl 0x78(%rsp), %eax
movq 0x28(%rcx), %rcx
cltd
idivl %ecx
cmpl $0x0, %edx
je 0x8409
movl 0x70(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x70(%rsp)
movq 0x20(%rsp), %rax
movslq 0x74(%rsp), %rcx
movq %rcx, 0x30(%rax)
movslq 0x70(%rsp), %rcx
movq %rcx, 0x38(%rax)
cmpq $0x1, 0x30(%rax)
jb 0x8429
jmp 0x8448
leaq 0x49df0(%rip), %rdi     # 0x52220
leaq 0x49d9b(%rip), %rsi     # 0x521d2
movl $0x19f, %edx            # imm = 0x19F
leaq 0x49e1c(%rip), %rcx     # 0x5225f
callq 0x4170
movq 0x20(%rsp), %rax
cmpq $0x1, 0x38(%rax)
jb 0x8456
jmp 0x8475
leaq 0x49de2(%rip), %rdi     # 0x5223f
leaq 0x49d6e(%rip), %rsi     # 0x521d2
movl $0x1a0, %edx            # imm = 0x1A0
leaq 0x49def(%rip), %rcx     # 0x5225f
callq 0x4170
movq $0x0, 0x68(%rsp)
movq 0x20(%rsp), %rcx
movq 0x68(%rsp), %rax
cmpq 0x18(%rcx), %rax
jae 0x8600
movq 0x20(%rsp), %rax
movq 0x30(%rax), %rsi
movq 0x38(%rax), %rdx
leaq 0x50(%rsp), %rdi
callq 0x1f690
movq $0x0, 0x48(%rsp)
movq 0x20(%rsp), %rcx
movq 0x48(%rsp), %rax
cmpq 0x30(%rcx), %rax
jae 0x85c2
movq $0x0, 0x40(%rsp)
movq 0x20(%rsp), %rcx
movq 0x40(%rsp), %rax
cmpq 0x38(%rcx), %rax
jae 0x85ad
movl $0xa0, %edi
callq 0x4240
movq %rax, 0x18(%rsp)
jmp 0x84f4
movq 0x18(%rsp), %rdi
callq 0x474c0
jmp 0x8500
movq 0x18(%rsp), %rax
movq %rax, 0x38(%rsp)
movq 0x38(%rsp), %rdi
movq 0x48(%rsp), %rsi
movq 0x40(%rsp), %rdx
movq 0x68(%rsp), %rcx
callq 0x4a1b0
jmp 0x8525
movq 0x20(%rsp), %rax
movq 0x38(%rsp), %rdi
movl 0x58(%rax), %esi
callq 0x48100
jmp 0x8539
movq 0x38(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x48(%rsp), %rsi
leaq 0x50(%rsp), %rdi
callq 0x1f5c0
movq %rax, 0x10(%rsp)
jmp 0x8559
movq 0x10(%rsp), %rdi
movq 0x40(%rsp), %rsi
callq 0x1f5f0
movq 0x8(%rsp), %rcx
movq %rcx, (%rax)
movq 0x40(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x40(%rsp)
jmp 0x84cf
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x30(%rsp)
movl %eax, 0x2c(%rsp)
jmp 0x85f4
movq 0x18(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x30(%rsp)
movl %eax, 0x2c(%rsp)
callq 0x4220
jmp 0x85f4
jmp 0x85af
movq 0x48(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x48(%rsp)
jmp 0x84b2
movq 0x20(%rsp), %rdi
addq $0x40, %rdi
leaq 0x50(%rsp), %rsi
callq 0x1f780
jmp 0x85d7
leaq 0x50(%rsp), %rdi
callq 0x1f7f0
movq 0x68(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x68(%rsp)
jmp 0x847e
leaq 0x50(%rsp), %rdi
callq 0x1f7f0
jmp 0x8608
addq $0x98, %rsp
retq
movq 0x30(%rsp), %rdi
callq 0x43f0
nopw %cs:(%rax,%rax)