// File: /workspace/github_cpp_makefile_star2_O3/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 $0x48, %rsp
movl %esi, %r13d
movq %rdi, %r14
movq 0x78(%rdi), %rdi
movq (%rdi), %rax
callq *0x2c8(%rax)
movq (%rax), %rcx
movq %rax, %rdi
callq *0xc8(%rcx)
movq %rax, %r12
movq 0x8(%r14), %rdi
callq 0x1abb8
movq %rax, %r15
movq %rdx, 0x8(%rsp)
movq 0x78(%r14), %rdi
movq (%rdi), %rax
callq *0x218(%rax)
movslq %eax, %rbx
movq 0x88(%r14), %rcx
imulq %rbx, %rcx
movq %r14, 0x10(%rsp)
movq 0x90(%r14), %r14
movq %r15, 0x18(%rsp)
movq %rcx, 0x38(%rsp)
leaq (%rcx,%r15), %rbp
movslq %r13d, %r15
movq (%r12), %r12
leaq (%r15,%r15,2), %r13
movq (%r12,%r13,8), %rdi
callq 0x1abb8
movq 0x8(%rsp), %rcx
cmpq %rax, %rbp
ja 0x20d60
imulq %rbx, %r14
leaq (%r14,%rcx), %rax
cmpq %rdx, %rax
ja 0x20d60
movq %r15, 0x20(%rsp)
leaq (%r12,%r13,8), %rax
movq (%rax), %rbp
movq 0x8(%rax), %rax
movq %rax, 0x28(%rsp)
cmpq %rax, %rbp
je 0x20d1e
shlq $0x3, %r14
movq %r14, 0x30(%rsp)
xorpd %xmm0, %xmm0
movsd %xmm0, (%rsp)
movq 0x10(%rsp), %r15
movq 0x30(%rsp), %r12
cmpq $0x0, 0x18(%rsp)
je 0x20d0d
xorl %r13d, %r13d
testq %rcx, %rcx
je 0x20cf5
movq 0x38(%rsp), %rax
addq %r13, %rax
movq %rax, 0x40(%rsp)
xorl %ebx, %ebx
movsd (%rsp), %xmm0
movsd %xmm0, (%rsp)
movq %rbp, %rdi
movq 0x40(%rsp), %rsi
movq %rcx, %r14
callq 0x1adb2
movq (%rax), %rax
addq %r12, %rax
movsd (%rax,%rbx,8), %xmm0
movsd %xmm0, 0x8(%rsp)
movq 0x8(%r15), %rdi
movq %r13, %rsi
callq 0x1adb2
movsd (%rsp), %xmm0
movq %r14, %rcx
movq (%rax), %rax
movsd 0x8(%rsp), %xmm1
mulsd (%rax,%rbx,8), %xmm1
addsd %xmm1, %xmm0
incq %rbx
cmpq %rbx, %r14
jne 0x20c9f
jmp 0x20cfa
movsd (%rsp), %xmm0
movsd %xmm0, (%rsp)
incq %r13
cmpq 0x18(%rsp), %r13
jne 0x20c86
addq $0x18, %rbp
cmpq 0x28(%rsp), %rbp
jne 0x20c77
jmp 0x20d27
xorpd %xmm0, %xmm0
movsd %xmm0, (%rsp)
movq 0x10(%rsp), %rbx
movq 0x10(%rbx), %rdi
xorl %esi, %esi
callq 0x1adb2
movq (%rax), %rax
movsd (%rsp), %xmm0
addsd (%rax), %xmm0
movq 0x48(%rbx), %rax
movq 0x20(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x16271(%rip), %rdi     # 0x36fd8
leaq 0x9f3b(%rip), %rsi      # 0x2aca9
callq 0x4260
movq %rax, %rdi
callq 0x4100
xorl %edi, %edi
callq 0x4290