// File: /workspace/github_cpp_makefile_star2_O2/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), %rax
movq %rdx, (%rsp)
imulq $0x18, %rdx, %rcx
movq (%rax,%rcx), %r14
movq 0x8(%rax,%rcx), %rax
subq %r14, %rax
pushq $0x18
popq %rcx
cqto
idivq %rcx
movq 0x98(%rbx), %rcx
cmpq %rcx, %rax
jb 0x1c775
imulq $0x18, %rcx, %rax
addq %rax, %r14
movq 0x8(%rbx), %rdi
callq 0x17a6a
movq %rax, %r12
movq %rdx, %r15
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 0x1c73f
testl %eax, %eax
jne 0x1c785
addq %rbp, %r12
addq %r13, %r15
movsd 0x8367(%rip), %xmm0     # 0x24a90
movq %r14, %rdi
movq %rbp, %rsi
movq %r12, %rdx
movq %r13, %rcx
movq %r15, %r8
callq 0x199c2
jmp 0x1c759
addq %rbp, %r12
addq %r13, %r15
movq %r14, %rdi
movq %rbp, %rsi
movq %r12, %rdx
movq %r13, %rcx
movq %r15, %r8
callq 0x19de6
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 0x1685c(%rip), %rdi     # 0x32fd8
leaq 0x8526(%rip), %rsi      # 0x24ca9
jmp 0x1c793
movq 0x1684c(%rip), %rdi     # 0x32fd8
leaq 0x854a(%rip), %rsi      # 0x24cdd
callq 0x4240
movq %rax, %rdi
callq 0x4130
xorl %edi, %edi
callq 0x4260
nop