// File: /workspace/github_cpp_makefile_star2_O3/xbai0624[P]dnn/src/Neuron.cpp
// Function: Neuron::UpdateZPooling(int)
void Neuron::UpdateZPooling(int sample_index)
{
    // pooling layer
    // should be with cnn layer, just kernel matrix all elements=1, bias = 0;
    Layer* __previousLayer = __layer->GetPrevLayer();
    auto & inputImage = __previousLayer->GetImagesActiveA(); // no tensorization needed, b/c "fc->pooling" type connection is not used at the moment
    //if(inputImage.back().OutputImageFromKernel.size() < __coord.k)
    if(inputImage[sample_index].OutputImageFromKernel.size() < __coord.k)
    {
        // output image for current sample
        // pooling layer is different with cnn layer
        // in pooling layer, kernel and 'A' images has a 1-to-1 mapping relationship
        // for pooling layer, number of kernels (previous layer)  = number of kernels (current layer)
        std::cout<<"Error: pooling operation matrix dimension not match"<<std::endl;
        exit(0);
    }
    //Images image = inputImage.back(); // images for current training sample
    Images & image = inputImage[sample_index]; // images for current training sample
    std::vector<Matrix> & images = image.OutputImageFromKernel;
    //for(auto &i: images)cout<<i<<endl;
    Matrix & kernel_image = images[__coord.k];
    //std::cout<<kernel_image<<std::endl;

    auto dim = __w->Dimension();
    size_t i_size = dim.first, j_size = dim.second;

    size_t i_start = __coord.i * i_size;
    size_t j_start = __coord.j * j_size;

    // get pooling method
    PoolingMethod __poolingMethod = __layer->GetPoolingMethod();

    double z;
    if(__poolingMethod == PoolingMethod::Max) 
    {
        z = kernel_image.MaxInSectionWithPadding(i_start, i_start + i_size, j_start, j_start+j_size);
    } 
    else if(__poolingMethod == PoolingMethod::Average) 
    {
        z = kernel_image.AverageInSectionWithPadding(i_start, i_start + i_size, j_start, j_start+j_size);
    }
    else 
    {
        std::cout<<"Error: unspported pooling method, only max and average supported."<<std::endl;
        exit(0);
    }

    //__z.push_back(z);
    __z[sample_index] = z;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %esi, %ebp
movq %rdi, %rbx
movq 0x78(%rdi), %rdi
movq (%rdi), %rax
callq *0x2c8(%rax)
movq (%rax), %rcx
movq %rax, %rdi
callq *0xc8(%rcx)
movslq %ebp, %rdx
movq (%rax), %rcx
movq %rdx, (%rsp)
leaq (%rdx,%rdx,2), %rdx
movq (%rcx,%rdx,8), %rax
movq 0x8(%rcx,%rdx,8), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
movabsq $-0x5555555555555555, %rdx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rcx, %rdx
movq 0x98(%rbx), %rcx
cmpq %rcx, %rdx
jb 0x20e86
leaq (%rcx,%rcx,2), %rcx
leaq (%rax,%rcx,8), %r15
movq 0x8(%rbx), %rdi
callq 0x1abb8
movq %rax, %r12
movq %rdx, %r14
movq 0x88(%rbx), %rbp
imulq %rax, %rbp
movq 0x90(%rbx), %r13
imulq %rdx, %r13
movq 0x78(%rbx), %rdi
movq (%rdi), %rax
callq *0x210(%rax)
movl (%rax), %eax
cmpl $0x1, %eax
je 0x20e50
testl %eax, %eax
jne 0x20e96
addq %rbp, %r12
addq %r13, %r14
movsd 0x9cae(%rip), %xmm0     # 0x2aae8
movq %r15, %rdi
movq %rbp, %rsi
movq %r12, %rdx
movq %r13, %rcx
movq %r14, %r8
callq 0x1d616
jmp 0x20e6a
addq %rbp, %r12
addq %r13, %r14
movq %r15, %rdi
movq %rbp, %rsi
movq %r12, %rdx
movq %r13, %rcx
movq %r14, %r8
callq 0x1d9b4
movq 0x48(%rbx), %rax
movq (%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x1614b(%rip), %rdi     # 0x36fd8
leaq 0x9e65(%rip), %rsi      # 0x2acf9
jmp 0x20ea4
movq 0x1613b(%rip), %rdi     # 0x36fd8
leaq 0x9e89(%rip), %rsi      # 0x2ad2d
callq 0x4260
movq %rax, %rdi
callq 0x4100
xorl %edi, %edi
callq 0x4290