// File: /workspace/github_cpp_makefile_star2_O1/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 0xcef8
cmpl $0x4, %eax
je 0xceb5
testl %eax, %eax
jne 0xd110
movq 0x40(%rbx), %rcx
movq 0x48(%rbx), %rax
subq %rcx, %rax
cmpq $0x18, %rax
jne 0xd0b3
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
subq %rax, %rcx
je 0xcf55
sarq $0x3, %rcx
movabsq $-0x5555555555555555, %r15 # imm = 0xAAAAAAAAAAAAAAAB
imulq %rcx, %r15
movq 0x8(%rax), %rcx
subq (%rax), %rcx
cmpq $0x8, %rcx
sete %al
jmp 0xcf5a
movq 0xe0(%rbx), %rax
movq 0xd8(%rbx), %rcx
movq %rax, %rsi
subq %rcx, %rsi
movq 0x40(%rbx), %rdi
movq 0x48(%rbx), %rdx
subq %rdi, %rdx
cmpq %rsi, %rdx
jne 0xd0d2
movq %rsi, %r8
movabsq $-0x5555555555555555, %rdx # imm = 0xAAAAAAAAAAAAAAAB
movq (%rdi), %rsi
movq 0x8(%rdi), %rdi
subq %rsi, %rdi
je 0xcfdf
sarq $0x3, %rdi
imulq %rdx, %rdi
movq 0x8(%rsi), %r12
subq (%rsi), %r12
sarq $0x3, %r12
jmp 0xcfe4
xorl %r15d, %r15d
xorl %eax, %eax
testb %al, %al
je 0xd0f1
testq %r15, %r15
je 0xd0a4
xorl %r12d, %r12d
xorl %r13d, %r13d
movq 0x40(%rbx), %rax
movq (%rax), %rax
movq (%rax,%r12), %rax
movq (%rax), %rdi
callq 0x2097e
testb %al, %al
je 0xcfd1
movq 0x40(%rbx), %rax
movq (%rax), %rax
movq (%rax,%r12), %rax
movq (%rax), %rdi
leaq (,%r13,8), %rax
leaq (%rax,%rax,2), %r14
movq 0xd8(%rbx), %rsi
addq %r14, %rsi
callq 0x20282
movq 0x40(%rbx), %rax
movq (%rax), %rax
movq (%rax,%r12), %rax
movq (%rax), %rdi
addq 0xf0(%rbx), %r14
movq %r14, %rsi
callq 0x20288
incq %r13
addq $0x18, %r12
decq %r15
jne 0xcf71
jmp 0xd0a4
xorl %edi, %edi
xorl %r12d, %r12d
cmpq %rcx, %rax
je 0xd0a4
sarq $0x3, %r8
imulq %rdx, %r8
cmpq $0x1, %r8
adcq $0x0, %r8
movq %r8, 0x8(%rsp)
movq $0x0, (%rsp)
movq %rdi, 0x10(%rsp)
testq %rdi, %rdi
je 0xd08e
movq (%rsp), %rax
leaq (,%rax,8), %rax
leaq (%rax,%rax,2), %rbp
xorl %r14d, %r14d
testq %r12, %r12
je 0xd081
leaq (,%r14,8), %rax
leaq (%rax,%rax,2), %r13
xorl %r15d, %r15d
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 0x20282
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 0x20288
incq %r15
cmpq %r15, %r12
jne 0xd03b
incq %r14
movq 0x10(%rsp), %rdi
cmpq %rdi, %r14
jne 0xd027
movq (%rsp), %rcx
incq %rcx
movq %rcx, (%rsp)
cmpq 0x8(%rsp), %rcx
jne 0xd00f
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1a885(%rip), %rdi     # 0x2793f
leaq 0x1a689(%rip), %rsi     # 0x2774a
leaq 0x1b2b7(%rip), %rcx     # 0x2837f
movl $0xa2f, %edx            # imm = 0xA2F
callq 0x4160
leaq 0x1b2e3(%rip), %rdi     # 0x283bc
leaq 0x1a66a(%rip), %rsi     # 0x2774a
leaq 0x1b298(%rip), %rcx     # 0x2837f
movl $0xa41, %edx            # imm = 0xA41
callq 0x4160
leaq 0x1afc2(%rip), %rdi     # 0x280ba
leaq 0x1a64b(%rip), %rsi     # 0x2774a
leaq 0x1b279(%rip), %rcx     # 0x2837f
movl $0xa31, %edx            # imm = 0xA31
callq 0x4160
movq 0x29ec1(%rip), %rdi     # 0x36fd8
leaq 0x1b2ba(%rip), %rsi     # 0x283d8
callq 0x4260
movq %rax, %rdi
callq 0x4100
xorl %edi, %edi
callq 0x4290