// File: xbai0624[P]dnn/src/Neuron.cpp
// Function: Neuron::UpdateDeltaPooling(int)
void Neuron::UpdateDeltaPooling(int sample_index)
{
    // back propagate delta for pooling layer
    // this layer works the same with cnn layer; should be merged with UpdateDeltaCNN
    if(__sigmaPrime.size() <= 0) 
    {
        // error
        std::cout<<__func__<<" Error: something wrong happend in error back propagation for pooling layer."
            <<std::endl;
        std::cout<<"       sigmaPrime matrix should be already calculated."<<std::endl;
        exit(0);
    }
    double _sigma_prime = __sigmaPrime[sample_index];
    double delta_for_this_neuron = 0.;

    // get delta images from its next layer
    Layer* __nextLayer = __layer->GetNextLayer();
    std::vector<Images> & deltaVecNext = __nextLayer->GetImagesActiveDelta(); // for 2D layer, images are all active, drop out happens on kernel

    auto outputImageSizeForCurrentLayer = __layer->GetOutputImageSize(); // output image dimension of current layer

    if(__nextLayer->GetType() != LayerType::cnn && __nextLayer->GetType()!= LayerType::pooling)
    {
        // next layer is a 1D layer
        assert(deltaVecNext[sample_index].OutputImageFromKernel.size() == 1);
        // delta image from next layer
        Matrix & m_delta_next_layer = deltaVecNext[sample_index].OutputImageFromKernel[0];
        // weight matrix from next layer
        std::vector<Matrix>* v_w_next_layer = __nextLayer->GetWeightMatrix();
        // make sure dimension match
        size_t n_active_neurons_in_next_layer = m_delta_next_layer.Dimension().first;
        assert(m_delta_next_layer.Dimension().second == 1);
        assert(v_w_next_layer->size() == n_active_neurons_in_next_layer);

        // backpropagation delta; use method 2
        auto mapped_coords_in_vec = mappedCoordsInVector(outputImageSizeForCurrentLayer, __coord);
        double tmp = 0;
        for(size_t iii= 0;iii<n_active_neurons_in_next_layer;iii++)
        {
            tmp += ((*v_w_next_layer)[iii])[0][mapped_coords_in_vec.first] * m_delta_next_layer[iii][0];
        }
        delta_for_this_neuron = tmp * _sigma_prime;
    }
    else if(__nextLayer->GetType() == LayerType::cnn)
    {
        // next layer is cnn, 2 dimensional
        // vector of delta matrix from next layer
        std::vector<Matrix> & v_m_delta_next_layer = deltaVecNext[sample_index].OutputImageFromKernel;
        // vector of weight matrix from next layer
        std::vector<Matrix> * v_w_next_layer = __nextLayer->GetWeightMatrix();

        size_t C_next = v_m_delta_next_layer.size();
        assert(C_next == v_w_next_layer->size()); // make sure number of kernels = number of delta matrix in next layer

        // back propagation
        double tmp = 0;
        for(size_t d=0;d<C_next;d++) // sum all kernels
        { 
            // get d^th delta and weight matrix of next layer
            Matrix & delta = v_m_delta_next_layer[d];
            Matrix & weight = (*v_w_next_layer)[d];

            auto weight_dimension = weight.Dimension();
            auto delta_dimension =  delta.Dimension();

            for(size_t p=0; p<weight_dimension.first;p++){
                for(size_t q=0;q<weight_dimension.second;q++)
                {
                    double tmp_d = 0;
                    int delta_index_p = __coord.i - p;
                    int delta_index_q = __coord.j - q;
                    if(delta_index_p >= 0 && delta_index_q >= 0 && delta_index_p<(int)delta_dimension.first && delta_index_q<(int)delta_dimension.second )
                        tmp_d = delta[delta_index_p][delta_index_q];
                    double tmp_w = weight[p][q];
                    tmp += tmp_d * tmp_w;
                }
            }
        }

        // multiply  sigma_prime
        delta_for_this_neuron = tmp * _sigma_prime;
    }
    else if(__nextLayer->GetType() == LayerType::pooling)
    {
        // next layer is also pooling : pooling->pooling (not practical, implement for code completeness)
        // number of  delta images from next layer should = number of kernels in this layer
        std::cout<<__func__<<"Error: pooling->pooling connection is meaningless, you can increase kernel dimension instead."
            <<std::endl;
        exit(0);
    }
    else
    {
        std::cout<<__func__<<" Error: pooling layer followed by unknow type of layer, something is wrong."
            <<std::endl;
        exit(0);
    }

    // for average pooling, give the same error to each neuron where it's related.
    __delta[sample_index] = delta_for_this_neuron;
}

// Assembly:
subq $0x138, %rsp            # imm = 0x138
movq %rdi, 0x130(%rsp)
movl %esi, 0x12c(%rsp)
movq 0x130(%rsp), %rdi
movq %rdi, 0x18(%rsp)
addq $0x60, %rdi
callq 0x21e20
cmpq $0x0, %rax
ja 0x49a9c
movq 0x2158f(%rip), %rdi     # 0x6afd8
leaq 0xc751(%rip), %rsi      # 0x561a1
callq 0x4230
movq %rax, %rdi
leaq 0xc755(%rip), %rsi      # 0x561b4
callq 0x4230
movq %rax, %rdi
movq 0x21542(%rip), %rsi     # 0x6afb0
callq 0x4260
movq 0x2155e(%rip), %rdi     # 0x6afd8
leaq 0xc780(%rip), %rsi      # 0x56201
callq 0x4230
movq %rax, %rdi
movq 0x21520(%rip), %rsi     # 0x6afb0
callq 0x4260
xorl %edi, %edi
callq 0x4290
movq 0x18(%rsp), %rdi
addq $0x60, %rdi
movslq 0x12c(%rsp), %rsi
callq 0x1fd60
movq %rax, %rcx
movq 0x18(%rsp), %rax
movsd (%rcx), %xmm0
movsd %xmm0, 0x120(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x118(%rsp)
movq 0x78(%rax), %rdi
movq (%rdi), %rax
callq *0x2c0(%rax)
movq %rax, 0x110(%rsp)
movq 0x110(%rsp), %rdi
movq (%rdi), %rax
callq *0xf8(%rax)
movq %rax, %rcx
movq 0x18(%rsp), %rax
movq %rcx, 0x108(%rsp)
movq 0x78(%rax), %rdi
movq (%rdi), %rax
callq *0x270(%rax)
movq %rax, 0xf8(%rsp)
movq %rdx, 0x100(%rsp)
movq 0x110(%rsp), %rdi
movq (%rdi), %rax
callq *0x250(%rax)
cmpl $0x1, %eax
je 0x49d9e
movq 0x110(%rsp), %rdi
movq (%rdi), %rax
callq *0x250(%rax)
cmpl $0x2, %eax
je 0x49d9e
movq 0x108(%rsp), %rdi
movslq 0x12c(%rsp), %rsi
callq 0x1fd40
movq %rax, %rdi
callq 0x1f9c0
cmpq $0x1, %rax
jne 0x49b7f
jmp 0x49b9e
leaq 0xc3ce(%rip), %rdi      # 0x55f54
leaq 0xbe49(%rip), %rsi      # 0x559d6
movl $0x32d, %edx            # imm = 0x32D
leaq 0xc69f(%rip), %rcx      # 0x56238
callq 0x4170
movq 0x108(%rsp), %rdi
movslq 0x12c(%rsp), %rsi
callq 0x1fd40
movq %rax, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x1f9e0
movq %rax, 0xf0(%rsp)
movq 0x110(%rsp), %rdi
movq (%rdi), %rax
callq *0x220(%rax)
movq %rax, 0xe8(%rsp)
movq 0xf0(%rsp), %rdi
callq 0x3bae0
movq %rax, 0xd0(%rsp)
movq %rdx, 0xd8(%rsp)
movq 0xd0(%rsp), %rax
movq %rax, 0xe0(%rsp)
movq 0xf0(%rsp), %rdi
callq 0x3bae0
movq %rax, 0xc0(%rsp)
movq %rdx, 0xc8(%rsp)
cmpq $0x1, 0xc8(%rsp)
jne 0x49c37
jmp 0x49c56
leaq 0xc374(%rip), %rdi      # 0x55fb2
leaq 0xbd91(%rip), %rsi      # 0x559d6
movl $0x334, %edx            # imm = 0x334
leaq 0xc5e7(%rip), %rcx      # 0x56238
callq 0x4170
movq 0xe8(%rsp), %rdi
callq 0x1f9c0
cmpq 0xe0(%rsp), %rax
jne 0x49c6f
jmp 0x49c8e
leaq 0xc367(%rip), %rdi      # 0x55fdd
leaq 0xbd59(%rip), %rsi      # 0x559d6
movl $0x335, %edx            # imm = 0x335
leaq 0xc5af(%rip), %rcx      # 0x56238
callq 0x4170
movq 0x18(%rsp), %rsi
addq $0x88, %rsi
leaq 0xf8(%rsp), %rdi
callq 0x4a0e0
movq %rax, 0xb0(%rsp)
movq %rdx, 0xb8(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0xa8(%rsp)
movq $0x0, 0xa0(%rsp)
movq 0xa0(%rsp), %rax
cmpq 0xe0(%rsp), %rax
jae 0x49d7e
movq 0xe8(%rsp), %rdi
movq 0xa0(%rsp), %rsi
callq 0x1f9e0
movq %rax, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x3be50
movq %rax, %rdi
movq 0xb0(%rsp), %rsi
callq 0x1fd60
movsd (%rax), %xmm0
movsd %xmm0, 0x10(%rsp)
movq 0xf0(%rsp), %rdi
movq 0xa0(%rsp), %rsi
callq 0x3be50
movq %rax, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x1fd60
movsd 0x10(%rsp), %xmm0
movsd (%rax), %xmm2
movsd 0xa8(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0xa8(%rsp)
movq 0xa0(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xa0(%rsp)
jmp 0x49ccf
movsd 0xa8(%rsp), %xmm0
mulsd 0x120(%rsp), %xmm0
movsd %xmm0, 0x118(%rsp)
jmp 0x4a0a8
movq 0x110(%rsp), %rdi
movq (%rdi), %rax
callq *0x250(%rax)
cmpl $0x1, %eax
jne 0x4a020
movq 0x108(%rsp), %rdi
movslq 0x12c(%rsp), %rsi
callq 0x1fd40
movq %rax, 0x98(%rsp)
movq 0x110(%rsp), %rdi
movq (%rdi), %rax
callq *0x220(%rax)
movq %rax, 0x90(%rsp)
movq 0x98(%rsp), %rdi
callq 0x1f9c0
movq %rax, 0x88(%rsp)
movq 0x88(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x90(%rsp), %rdi
callq 0x1f9c0
movq %rax, %rcx
movq 0x8(%rsp), %rax
cmpq %rcx, %rax
jne 0x49e2c
jmp 0x49e4b
leaq 0xc1e3(%rip), %rdi      # 0x56016
leaq 0xbb9c(%rip), %rsi      # 0x559d6
movl $0x349, %edx            # imm = 0x349
leaq 0xc3f2(%rip), %rcx      # 0x56238
callq 0x4170
xorps %xmm0, %xmm0
movsd %xmm0, 0x80(%rsp)
movq $0x0, 0x78(%rsp)
movq 0x78(%rsp), %rax
cmpq 0x88(%rsp), %rax
jae 0x4a000
movq 0x98(%rsp), %rdi
movq 0x78(%rsp), %rsi
callq 0x1f9e0
movq %rax, 0x70(%rsp)
movq 0x90(%rsp), %rdi
movq 0x78(%rsp), %rsi
callq 0x1f9e0
movq %rax, 0x68(%rsp)
movq 0x68(%rsp), %rdi
callq 0x3bae0
movq %rax, 0x58(%rsp)
movq %rdx, 0x60(%rsp)
movq 0x70(%rsp), %rdi
callq 0x3bae0
movq %rax, 0x48(%rsp)
movq %rdx, 0x50(%rsp)
movq $0x0, 0x40(%rsp)
movq 0x40(%rsp), %rax
cmpq 0x58(%rsp), %rax
jae 0x49feb
movq $0x0, 0x38(%rsp)
movq 0x38(%rsp), %rax
cmpq 0x60(%rsp), %rax
jae 0x49fd6
movq 0x18(%rsp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x30(%rsp)
movq 0x88(%rax), %rcx
subq 0x40(%rsp), %rcx
movl %ecx, 0x2c(%rsp)
movq 0x90(%rax), %rax
subq 0x38(%rsp), %rax
movl %eax, 0x28(%rsp)
cmpl $0x0, 0x2c(%rsp)
jl 0x49f77
cmpl $0x0, 0x28(%rsp)
jl 0x49f77
movl 0x2c(%rsp), %eax
movq 0x48(%rsp), %rcx
cmpl %ecx, %eax
jge 0x49f77
movl 0x28(%rsp), %eax
movq 0x50(%rsp), %rcx
cmpl %ecx, %eax
jge 0x49f77
movq 0x70(%rsp), %rdi
movslq 0x2c(%rsp), %rsi
callq 0x3be50
movq %rax, %rdi
movslq 0x28(%rsp), %rsi
callq 0x1fd60
movsd (%rax), %xmm0
movsd %xmm0, 0x30(%rsp)
movq 0x68(%rsp), %rdi
movq 0x40(%rsp), %rsi
callq 0x3be50
movq %rax, %rdi
movq 0x38(%rsp), %rsi
callq 0x1fd60
movsd (%rax), %xmm0
movsd %xmm0, 0x20(%rsp)
movsd 0x30(%rsp), %xmm0
movsd 0x20(%rsp), %xmm2
movsd 0x80(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x80(%rsp)
movq 0x38(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x38(%rsp)
jmp 0x49eeb
jmp 0x49fd8
movq 0x40(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x40(%rsp)
jmp 0x49ed2
jmp 0x49fed
movq 0x78(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x78(%rsp)
jmp 0x49e60
movsd 0x80(%rsp), %xmm0
mulsd 0x120(%rsp), %xmm0
movsd %xmm0, 0x118(%rsp)
jmp 0x4a0a6
movq 0x110(%rsp), %rdi
movq (%rdi), %rax
callq *0x250(%rax)
cmpl $0x2, %eax
jne 0x4a06e
movq 0x20f9b(%rip), %rdi     # 0x6afd8
leaq 0xc15d(%rip), %rsi      # 0x561a1
callq 0x4230
movq %rax, %rdi
leaq 0xc20a(%rip), %rsi      # 0x5625d
callq 0x4230
movq %rax, %rdi
movq 0x20f4e(%rip), %rsi     # 0x6afb0
callq 0x4260
xorl %edi, %edi
callq 0x4290
movq 0x20f63(%rip), %rdi     # 0x6afd8
leaq 0xc125(%rip), %rsi      # 0x561a1
callq 0x4230
movq %rax, %rdi
leaq 0xc230(%rip), %rsi      # 0x562bb
callq 0x4230
movq %rax, %rdi
movq 0x20f16(%rip), %rsi     # 0x6afb0
callq 0x4260
xorl %edi, %edi
callq 0x4290
jmp 0x4a0a8
movq 0x18(%rsp), %rdi
movsd 0x118(%rsp), %xmm0
movsd %xmm0, (%rsp)
addq $0x30, %rdi
movslq 0x12c(%rsp), %rsi
callq 0x1fd60
movsd (%rsp), %xmm0
movsd %xmm0, (%rax)
addq $0x138, %rsp            # imm = 0x138
retq
nopl (%rax)