// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, %rbx
addq $0x40, %rdi
movq %rdi, 0x8(%rsp)
callq 0x11226
movq 0x3f0(%rbx), %rdi
testq %rdi, %rdi
je 0x6462
movq (%rdi), %rax
callq *0x270(%rax)
movq %rax, %r15
movq %rdx, %r14
movq 0x3f0(%rbx), %rdi
movq (%rdi), %rax
callq *0x288(%rax)
cmpq 0x18(%rbx), %rax
jne 0x6481
movq %rax, %rcx
movl %r15d, %eax
cltd
idivl 0x20(%rbx)
movl %edx, %esi
movl %eax, %edi
cmpl $0x2, %eax
pushq $0x1
popq %r8
cmovll %r8d, %edi
movl %r14d, %eax
cltd
idivl 0x28(%rbx)
cmpl $0x2, %eax
cmovll %r8d, %eax
cmpl %r8d, %esi
sbbl $-0x1, %edi
cmpl %r8d, %edx
sbbl $-0x1, %eax
movq %rdi, 0x30(%rbx)
movq %rax, 0x38(%rbx)
xorl %r15d, %r15d
leaq 0x10(%rsp), %r14
cmpq %rcx, %r15
jae 0x6453
movq 0x30(%rbx), %rsi
movq 0x38(%rbx), %rdx
movq %r14, %rdi
callq 0x1122e
xorl %r13d, %r13d
cmpq 0x30(%rbx), %r13
jae 0x642d
imulq $0x18, %r13, %r12
xorl %ebp, %ebp
cmpq 0x38(%rbx), %rbp
jae 0x6428
movl $0xa0, %edi
callq 0x4250
movq %rax, %r14
movq %rax, %rdi
callq 0x1bbb8
movq %r14, %rdi
movq %r13, %rsi
movq %rbp, %rdx
movq %r15, %rcx
callq 0x1d56c
movl 0x58(%rbx), %esi
movq %r14, %rdi
callq 0x1c312
movq 0x10(%rsp), %rax
movq (%rax,%r12), %rax
movq %r14, (%rax,%rbp,8)
incq %rbp
jmp 0x63df
incq %r13
jmp 0x63d3
movq 0x8(%rsp), %rdi
leaq 0x10(%rsp), %r14
movq %r14, %rsi
callq 0x112b2
movq %r14, %rdi
callq 0x11d84
incq %r15
movq 0x18(%rbx), %rcx
jmp 0x63b7
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1b25a(%rip), %rdi     # 0x216c3
leaq 0x1b26a(%rip), %rsi     # 0x216da
leaq 0x1b2b1(%rip), %rcx     # 0x21728
movl $0x188, %edx            # imm = 0x188
callq 0x4180
leaq 0x1b2ca(%rip), %rdi     # 0x21752
leaq 0x1b24b(%rip), %rsi     # 0x216da
leaq 0x1b292(%rip), %rcx     # 0x21728
movl $0x18f, %edx            # imm = 0x18F
callq 0x4180
jmp 0x64af
movq %rax, %rbx
movq %r14, %rdi
callq 0x4220
jmp 0x64b2
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x11d84
movq %rbx, %rdi
callq 0x43b0