// File: /workspace/github_cpp_makefile_star2_O3/xbai0624[P]dnn/src/ConstructLayer.cpp
// Function: ConstructLayer::BackwardPropagateForSample(int)
void ConstructLayer::BackwardPropagateForSample(int sample_index)
{
    // backward propagation:
    //     ---) compute delta for this layer
    //     ---) only after all samples in this batch finished forward propagation, one can do this backward propagation

    if(__type == LayerType::output)
    {
        ComputeCostInOutputLayerForCurrentSample(sample_index);
    } 
    else 
    {
        for(size_t k=0;k<__neurons.size();k++)
        {
            auto dim = __neurons[k].Dimension();
            for(size_t i=0;i<dim.first;i++)  {
                for(size_t j=0;j<dim.second;j++) {
                    if(__neurons[k][i][j]->IsActive())
                    {
                        __neurons[k][i][j] -> BackwardPropagateForSample(sample_index);
                        /*
                           if(__type == LayerType::fullyConnected)
                           {
                           cout<<__func__<<" fc layer neuron coord: "<<__neurons[k][i][j]->GetCoord()<<endl;
                           }
                           else if(__type == LayerType::cnn)
                           {
                           cout<<__func__<<" cnn layer neuron coord: "<<__neurons[k][i][j]->GetCoord()<<endl;
                           }
                           */
                    }
                }
            }
        }
        // when propagation for this layer is done, update the Delta matrices
        UpdateImagesDelta(sample_index);
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, %r14
cmpl $0x4, 0xc(%rdi)
jne 0x837b
movq (%r14), %rax
movq 0x58(%rax), %rax
movq %r14, %rdi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmpq *%rax
movl %esi, 0x4(%rsp)
movq 0x40(%r14), %rax
movq 0x48(%r14), %rcx
cmpq %rax, %rcx
je 0x8471
xorl %esi, %esi
movabsq $-0x5555555555555555, %rdi # imm = 0xAAAAAAAAAAAAAAAB
leaq (%rsi,%rsi,2), %r13
movq (%rax,%r13,8), %rdx
movq 0x8(%rax,%r13,8), %r8
subq %rdx, %r8
je 0x8457
movq %rsi, 0x8(%rsp)
movq (%rdx), %rax
movq 0x8(%rdx), %r15
movq %r15, 0x10(%rsp)
movq %rax, 0x18(%rsp)
subq %rax, %r15
sarq $0x3, %r15
sarq $0x3, %r8
imulq %rdi, %r8
cmpq $0x1, %r8
adcq $0x0, %r8
xorl %r12d, %r12d
movq %r8, 0x20(%rsp)
movq 0x10(%rsp), %rax
cmpq 0x18(%rsp), %rax
je 0x8433
leaq (%r12,%r12,2), %rbp
xorl %ebx, %ebx
movq 0x40(%r14), %rax
movq (%rax,%r13,8), %rax
movq (%rax,%rbp,8), %rax
movq (%rax,%rbx,8), %rdi
callq 0x2089a
testb %al, %al
je 0x842b
movq 0x40(%r14), %rax
movq (%rax,%r13,8), %rax
movq (%rax,%rbp,8), %rax
movq (%rax,%rbx,8), %rdi
movl 0x4(%rsp), %esi
callq 0x2063e
incq %rbx
cmpq %rbx, %r15
jne 0x83f9
incq %r12
movq 0x20(%rsp), %r8
cmpq %r8, %r12
jne 0x83e7
movq 0x40(%r14), %rax
movq 0x48(%r14), %rcx
movq 0x8(%rsp), %rsi
movabsq $-0x5555555555555555, %rdi # imm = 0xAAAAAAAAAAAAAAAB
incq %rsi
movq %rcx, %rdx
subq %rax, %rdx
sarq $0x3, %rdx
imulq %rdi, %rdx
cmpq %rdx, %rsi
jb 0x839c
movq %r14, %rdi
movl 0x4(%rsp), %esi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x848c
nop