// File: /workspace/github_cpp_makefile_star2_O2/xbai0624[P]dnn/src/ConstructLayer.cpp
// Function: ConstructLayer::AssignWeightsAndBiasToNeurons()
void ConstructLayer::AssignWeightsAndBiasToNeurons()
{
    // pass active weights and bias pointers to neurons
    if(__type == LayerType::fullyConnected || __type == LayerType::output) // output is also a fully connected layer
    {
        // assert(__weightMatrixActive.size() == 1); // should be equal to number of active neurons
        assert(__neurons.size() == 1);
        auto dim = __neurons[0].Dimension();
        assert(dim.second == 1);

        size_t active_i = 0;
        for(size_t i=0;i<dim.first;i++)
        {
            if(!__neurons[0][i][0]->IsActive())
                continue;
            __neurons[0][i][0] -> PassWeightPointer(&__weightMatrixActive[active_i]);
            __neurons[0][i][0] -> PassBiasPointer(&__biasVectorActive[active_i]);

            active_i++;
        }
    }
    else if(__type == LayerType::cnn || __type == LayerType::pooling)
    {
        size_t nKernel = __weightMatrixActive.size();
        assert(__neurons.size() == nKernel); // layers must match
        auto dim = __neurons[0].Dimension(); // image size (pixel)
        for(size_t k=0;k<nKernel;k++)
        {
            for(size_t i=0;i<dim.first;i++)
            {
                for(size_t j=0;j<dim.second;j++)
                {
                    __neurons[k][i][j] -> PassWeightPointer(&__weightMatrixActive[k]);
                    __neurons[k][i][j] -> PassBiasPointer(&__biasVectorActive[k]);
                }
            }
        }
    }
    else 
    {
        std::cout<<"Error: assign w&b pointers, unrecognized layer type."
            <<std::endl;
        exit(0);
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
movl 0xc(%rdi), %eax
leal -0x1(%rax), %ecx
cmpl $0x2, %ecx
jb 0xbf02
cmpl $0x4, %eax
je 0xbe6d
testl %eax, %eax
jne 0xc025
movq 0x40(%rbx), %rdi
movq 0x48(%rbx), %rax
subq %rdi, %rax
cmpq $0x18, %rax
jne 0xbfc8
callq 0x111fe
cmpq $0x1, %rdx
jne 0xc006
movq %rax, %r14
xorl %r12d, %r12d
xorl %r13d, %r13d
subq $0x1, %r14
jb 0xbfb9
movq 0x40(%rbx), %rax
movq (%rax), %rax
movq (%rax,%r12), %rax
movq (%rax), %rdi
callq 0x1c256
testb %al, %al
je 0xbefc
movq 0x40(%rbx), %rax
movq (%rax), %rax
movq (%rax,%r12), %rax
movq (%rax), %rdi
imulq $0x18, %r13, %r15
movq 0xd8(%rbx), %rsi
addq %r15, %rsi
callq 0x1bc2e
movq 0x40(%rbx), %rax
movq (%rax), %rax
movq (%rax,%r12), %rax
movq (%rax), %rdi
addq 0xf0(%rbx), %r15
movq %r15, %rsi
callq 0x1bc34
incq %r13
addq $0x18, %r12
jmp 0xbe9a
movq 0xe0(%rbx), %rcx
subq 0xd8(%rbx), %rcx
pushq $0x18
popq %rsi
movq %rcx, %rax
cqto
idivq %rsi
movq %rax, (%rsp)
movq 0x40(%rbx), %rdi
movq 0x48(%rbx), %rax
subq %rdi, %rax
cmpq %rcx, %rax
jne 0xbfe7
callq 0x111fe
movq %rax, 0x10(%rsp)
movq %rdx, %r12
xorl %eax, %eax
cmpq (%rsp), %rax
je 0xbfb9
movq %rax, 0x8(%rsp)
imulq $0x18, %rax, %rbp
xorl %r14d, %r14d
cmpq 0x10(%rsp), %r14
je 0xbfaf
imulq $0x18, %r14, %r13
xorl %r15d, %r15d
cmpq %r15, %r12
je 0xbfaa
movq 0x40(%rbx), %rax
movq (%rax,%rbp), %rax
movq (%rax,%r13), %rax
movq (%rax,%r15,8), %rdi
movq 0xd8(%rbx), %rsi
addq %rbp, %rsi
callq 0x1bc2e
movq 0x40(%rbx), %rax
movq (%rax,%rbp), %rax
movq (%rax,%r13), %rax
movq (%rax,%r15,8), %rdi
movq 0xf0(%rbx), %rsi
addq %rbp, %rsi
callq 0x1bc34
incq %r15
jmp 0xbf62
incq %r14
jmp 0xbf54
movq 0x8(%rsp), %rax
incq %rax
jmp 0xbf42
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x158c1(%rip), %rdi     # 0x21890
leaq 0x15704(%rip), %rsi     # 0x216da
leaq 0x162f3(%rip), %rcx     # 0x222d0
movl $0xa2f, %edx            # imm = 0xA2F
callq 0x4180
leaq 0x1631f(%rip), %rdi     # 0x2230d
leaq 0x156e5(%rip), %rsi     # 0x216da
leaq 0x162d4(%rip), %rcx     # 0x222d0
movl $0xa41, %edx            # imm = 0xA41
callq 0x4180
leaq 0x15ffe(%rip), %rdi     # 0x2200b
leaq 0x156c6(%rip), %rsi     # 0x216da
leaq 0x162b5(%rip), %rcx     # 0x222d0
movl $0xa31, %edx            # imm = 0xA31
callq 0x4180
movq 0x26fac(%rip), %rdi     # 0x32fd8
leaq 0x162f6(%rip), %rsi     # 0x22329
callq 0x4240
movq %rax, %rdi
callq 0x4130
xorl %edi, %edi
callq 0x4260
nop