// File: /workspace/github_cpp_makefile_star2_O2/xbai0624[P]dnn/src/Neuron.cpp
// Function: Neuron::UpdateZCNN(int)
void Neuron::UpdateZCNN(int sample_index)
{
    // cnn layer
    // every single output image needs input from all input images
    Layer *__previousLayer = __layer->GetPrevLayer();
    auto & inputImage = __previousLayer->GetImagesActiveA(); // no tensorization needed; b/c "fc->cnn" type connection is not used at the moment

    auto w_dim = __w->Dimension();
    int stride = __layer->GetCNNStride();

    size_t i_start = __coord.i*stride;
    size_t j_start = __coord.j*stride;
    size_t i_end = i_start + w_dim.first;
    size_t j_end = j_start + w_dim.second;

    //auto &current_sample_image = (inputImage.back()).OutputImageFromKernel;
    auto &current_sample_image = (inputImage[sample_index]).OutputImageFromKernel;
    auto image_dim = current_sample_image[0].Dimension();
    if(i_end > image_dim.first || j_end > image_dim.second)
    {
        std::cout<<"Error: cnn z update: matrix dimension not match, probably due to wrong padding."<<std::endl;
        exit(0);
    }

    // compute z
    double res = 0;
    for(auto &m: current_sample_image)
    {
        for(size_t i=0;i<w_dim.first;i++){
            for(size_t j=0;j<w_dim.second;j++){
                res += m[i+i_start][j+j_start] * (*__w)[i][j];
            }
        }
    }
    double z = res + (*__b)[0][0];
    __z[sample_index] = z;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %esi, %r15d
movq %rdi, %rbp
movq 0x78(%rdi), %rdi
movq (%rdi), %rax
callq *0x2c8(%rax)
movq (%rax), %rcx
movq %rax, %rdi
callq *0xc8(%rcx)
movq %rax, %r12
movq 0x8(%rbp), %rdi
callq 0x17a6a
movq %rax, %rbx
movq %rdx, (%rsp)
movq 0x78(%rbp), %rdi
movq (%rdi), %rax
callq *0x218(%rax)
movslq %eax, %r13
movq 0x88(%rbp), %rcx
imulq %r13, %rcx
movq %rbp, 0x10(%rsp)
movq 0x90(%rbp), %r14
movq %rbx, 0x30(%rsp)
movq %rcx, 0x28(%rsp)
leaq (%rcx,%rbx), %rbp
movslq %r15d, %rcx
movq (%r12), %rbx
movq %rcx, 0x18(%rsp)
imulq $0x18, %rcx, %r12
movq (%rbx,%r12), %rdi
callq 0x17a6a
movq (%rsp), %r15
cmpq %rax, %rbp
ja 0x1c659
imulq %r13, %r14
leaq (%r14,%r15), %rax
cmpq %rdx, %rax
ja 0x1c659
addq %r12, %rbx
movq (%rbx), %r12
movq 0x8(%rbx), %rax
movq %rax, 0x20(%rsp)
shlq $0x3, %r14
xorpd %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
cmpq 0x20(%rsp), %r12
je 0x1c61f
xorl %r13d, %r13d
cmpq 0x30(%rsp), %r13
je 0x1c619
movq 0x28(%rsp), %rax
leaq (%rax,%r13), %rbp
xorl %ebx, %ebx
cmpq %rbx, %r15
je 0x1c614
movq %r12, %rdi
movq %rbp, %rsi
callq 0x17c46
movq (%rax), %rax
addq %r14, %rax
movsd (%rax,%rbx,8), %xmm0
movsd %xmm0, (%rsp)
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rdi
movq %r13, %rsi
callq 0x17c46
movq (%rax), %rax
movsd (%rsp), %xmm1
mulsd (%rax,%rbx,8), %xmm1
movsd 0x8(%rsp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x8(%rsp)
incq %rbx
jmp 0x1c5c1
incq %r13
jmp 0x1c5af
addq $0x18, %r12
jmp 0x1c5a5
movq 0x10(%rsp), %r14
movq 0x10(%r14), %rdi
xorl %esi, %esi
callq 0x17c46
movq (%rax), %rax
movsd 0x8(%rsp), %xmm0
addsd (%rax), %xmm0
movq 0x48(%r14), %rax
movq 0x18(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x16978(%rip), %rdi     # 0x32fd8
leaq 0x85f2(%rip), %rsi      # 0x24c59
callq 0x4240
movq %rax, %rdi
callq 0x4130
xorl %edi, %edi
callq 0x4260
nop