// File: xbai0624[P]dnn/src/ConstructLayer.cpp
// Function: ConstructLayer::InitNeuronsCNN()
void ConstructLayer::InitNeuronsCNN()
{
    // clear
    __neurons.clear();

    // get output image size of previous layer (input image size for this layer)
    assert(__prevLayer != nullptr);
    auto size_prev_layer = __prevLayer->GetOutputImageSize();
    //std::cout<<__func__<<" prev layer output image size: "<<size_prev_layer<<std::endl;

    int n_row = size_prev_layer.first;
    int n_col = size_prev_layer.second;

    assert(__cnnStride >= 1);
    // deduct output image dimension
    int x_size = (int)n_row - (int)__kernelDim.first + 1;
    int y_size = (int)n_col - (int)__kernelDim.second + 1;
    if(x_size <= 0) x_size = 1; // small images will be complemented by padding
    if(y_size <= 0) y_size = 1; // so it is safe to set size >= 1
    __outputImageSizeCNN.first = x_size;
    __outputImageSizeCNN.second = y_size;

    if(__cnnStride > 1)
    {
        __outputImageSizeCNN.first = (int)__outputImageSizeCNN.first / (int) __cnnStride + 1;
        __outputImageSizeCNN.second = (int)__outputImageSizeCNN.second / (int) __cnnStride + 1;
    }
    assert(__outputImageSizeCNN.first >=1);
    assert(__outputImageSizeCNN.second >=1);

    for(size_t k=0;k<__n_kernels_cnn;k++)
    {
        Pixel2D<Neuron*> image(__outputImageSizeCNN.first, __outputImageSizeCNN.second);
        for(size_t i=0;i<__outputImageSizeCNN.first;i++)
        {
            for(size_t j=0;j<__outputImageSizeCNN.second;j++)
            {
                Neuron *n = new Neuron();
                n->SetCoord(i, j, k); // set neuron coord
                n->SetActuationFuncType(__neuron_actuation_func_type);
                image[i][j] = n;
            }
        }
        __neurons.push_back(image);
    }
}

// Assembly:
subq $0x98, %rsp
movq %rdi, 0x90(%rsp)
movq 0x90(%rsp), %rdi
movq %rdi, 0x20(%rsp)
addq $0x40, %rdi
callq 0x1f670
movq 0x20(%rsp), %rax
cmpq $0x0, 0x3f0(%rax)
je 0x7f86
jmp 0x7fa5
leaq 0x4a22e(%rip), %rdi     # 0x521bb
leaq 0x4a23e(%rip), %rsi     # 0x521d2
movl $0x159, %edx            # imm = 0x159
leaq 0x4a249(%rip), %rcx     # 0x521e9
callq 0x4170
movq 0x20(%rsp), %rax
movq 0x3f0(%rax), %rdi
movq (%rdi), %rax
callq *0x270(%rax)
movq %rax, %rcx
movq 0x20(%rsp), %rax
movq %rcx, 0x80(%rsp)
movq %rdx, 0x88(%rsp)
movq 0x80(%rsp), %rcx
movl %ecx, 0x7c(%rsp)
movq 0x88(%rsp), %rcx
movl %ecx, 0x78(%rsp)
cmpl $0x1, 0x3e0(%rax)
jl 0x7ff5
jmp 0x8014
leaq 0x4a213(%rip), %rdi     # 0x5220f
leaq 0x4a1cf(%rip), %rsi     # 0x521d2
movl $0x160, %edx            # imm = 0x160
leaq 0x4a1da(%rip), %rcx     # 0x521e9
callq 0x4170
movq 0x20(%rsp), %rcx
movl 0x7c(%rsp), %eax
movq 0x20(%rcx), %rdx
subl %edx, %eax
addl $0x1, %eax
movl %eax, 0x74(%rsp)
movl 0x78(%rsp), %eax
movq 0x28(%rcx), %rcx
subl %ecx, %eax
addl $0x1, %eax
movl %eax, 0x70(%rsp)
cmpl $0x0, 0x74(%rsp)
jg 0x804a
movl $0x1, 0x74(%rsp)
cmpl $0x0, 0x70(%rsp)
jg 0x8059
movl $0x1, 0x70(%rsp)
movq 0x20(%rsp), %rax
movslq 0x74(%rsp), %rcx
movq %rcx, 0x30(%rax)
movslq 0x70(%rsp), %rcx
movq %rcx, 0x38(%rax)
cmpl $0x1, 0x3e0(%rax)
jle 0x80ae
movq 0x20(%rsp), %rcx
movq 0x30(%rcx), %rax
cltd
idivl 0x3e0(%rcx)
addl $0x1, %eax
cltq
movq %rax, 0x30(%rcx)
movq 0x38(%rcx), %rax
cltd
idivl 0x3e0(%rcx)
movl %eax, %ecx
movq 0x20(%rsp), %rax
addl $0x1, %ecx
movslq %ecx, %rcx
movq %rcx, 0x38(%rax)
movq 0x20(%rsp), %rax
cmpq $0x1, 0x30(%rax)
jb 0x80bc
jmp 0x80db
leaq 0x4a15d(%rip), %rdi     # 0x52220
leaq 0x4a108(%rip), %rsi     # 0x521d2
movl $0x16e, %edx            # imm = 0x16E
leaq 0x4a113(%rip), %rcx     # 0x521e9
callq 0x4170
movq 0x20(%rsp), %rax
cmpq $0x1, 0x38(%rax)
jb 0x80e9
jmp 0x8108
leaq 0x4a14f(%rip), %rdi     # 0x5223f
leaq 0x4a0db(%rip), %rsi     # 0x521d2
movl $0x16f, %edx            # imm = 0x16F
leaq 0x4a0e6(%rip), %rcx     # 0x521e9
callq 0x4170
movq $0x0, 0x68(%rsp)
movq 0x20(%rsp), %rcx
movq 0x68(%rsp), %rax
cmpq 0x18(%rcx), %rax
jae 0x8293
movq 0x20(%rsp), %rax
movq 0x30(%rax), %rsi
movq 0x38(%rax), %rdx
leaq 0x50(%rsp), %rdi
callq 0x1f690
movq $0x0, 0x48(%rsp)
movq 0x20(%rsp), %rcx
movq 0x48(%rsp), %rax
cmpq 0x30(%rcx), %rax
jae 0x8255
movq $0x0, 0x40(%rsp)
movq 0x20(%rsp), %rcx
movq 0x40(%rsp), %rax
cmpq 0x38(%rcx), %rax
jae 0x8240
movl $0xa0, %edi
callq 0x4240
movq %rax, 0x18(%rsp)
jmp 0x8187
movq 0x18(%rsp), %rdi
callq 0x474c0
jmp 0x8193
movq 0x18(%rsp), %rax
movq %rax, 0x38(%rsp)
movq 0x38(%rsp), %rdi
movq 0x48(%rsp), %rsi
movq 0x40(%rsp), %rdx
movq 0x68(%rsp), %rcx
callq 0x4a1b0
jmp 0x81b8
movq 0x20(%rsp), %rax
movq 0x38(%rsp), %rdi
movl 0x58(%rax), %esi
callq 0x48100
jmp 0x81cc
movq 0x38(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x48(%rsp), %rsi
leaq 0x50(%rsp), %rdi
callq 0x1f5c0
movq %rax, 0x10(%rsp)
jmp 0x81ec
movq 0x10(%rsp), %rdi
movq 0x40(%rsp), %rsi
callq 0x1f5f0
movq 0x8(%rsp), %rcx
movq %rcx, (%rax)
movq 0x40(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x40(%rsp)
jmp 0x8162
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x30(%rsp)
movl %eax, 0x2c(%rsp)
jmp 0x8287
movq 0x18(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x30(%rsp)
movl %eax, 0x2c(%rsp)
callq 0x4220
jmp 0x8287
jmp 0x8242
movq 0x48(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x48(%rsp)
jmp 0x8145
movq 0x20(%rsp), %rdi
addq $0x40, %rdi
leaq 0x50(%rsp), %rsi
callq 0x1f780
jmp 0x826a
leaq 0x50(%rsp), %rdi
callq 0x1f7f0
movq 0x68(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x68(%rsp)
jmp 0x8111
leaq 0x50(%rsp), %rdi
callq 0x1f7f0
jmp 0x829b
addq $0x98, %rsp
retq
movq 0x30(%rsp), %rdi
callq 0x43f0
nopw %cs:(%rax,%rax)