// File: /workspace/github_cpp_makefile_star2_O3/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 $0x28, %rsp
movq %rdi, %rbx
movl 0xc(%rdi), %eax
leal -0x1(%rax), %ecx
cmpl $0x2, %ecx
jb 0xcf05
cmpl $0x4, %eax
je 0xce41
testl %eax, %eax
jne 0xd09f
movq 0x40(%rbx), %rax
movq 0x48(%rbx), %rcx
subq %rax, %rcx
cmpq $0x18, %rcx
jne 0xd061
movq (%rax), %rcx
movq 0x8(%rax), %rax
subq %rcx, %rax
je 0xd042
movq 0x8(%rcx), %rdx
subq (%rcx), %rdx
cmpq $0x8, %rdx
jne 0xd042
sarq $0x3, %rax
movabsq $-0x5555555555555555, %r15 # imm = 0xAAAAAAAAAAAAAAAB
imulq %rax, %r15
cmpq $0x1, %r15
adcq $0x0, %r15
xorl %r12d, %r12d
xorl %r13d, %r13d
movq 0x40(%rbx), %rax
movq (%rax), %rax
movq (%rax,%r12), %rax
movq (%rax), %rdi
callq 0x2089a
testb %al, %al
je 0xcef7
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 0x201b2
movq 0x40(%rbx), %rax
movq (%rax), %rax
movq (%rax,%r12), %rax
movq (%rax), %rdi
addq 0xf0(%rbx), %r14
movq %r14, %rsi
callq 0x201b8
incq %r13
addq $0x18, %r12
decq %r15
jne 0xce97
jmp 0xd033
movq 0xe0(%rbx), %rax
movq 0xd8(%rbx), %rcx
movq %rax, %rdi
subq %rcx, %rdi
movq 0x40(%rbx), %rdx
movq 0x48(%rbx), %rsi
subq %rdx, %rsi
cmpq %rdi, %rsi
jne 0xd080
movq %rdi, %r8
movq (%rdx), %rsi
movq 0x8(%rdx), %rdx
movq %rdx, 0x18(%rsp)
subq %rsi, %rdx
je 0xcf4e
movq 0x8(%rsi), %rbp
subq (%rsi), %rbp
sarq $0x3, %rbp
jmp 0xcf50
xorl %ebp, %ebp
cmpq %rcx, %rax
je 0xd033
sarq $0x3, %r8
movabsq $-0x5555555555555555, %rax # imm = 0xAAAAAAAAAAAAAAAB
imulq %rax, %r8
sarq $0x3, %rdx
imulq %rax, %rdx
cmpq $0x1, %rdx
adcq $0x0, %rdx
movq %rdx, 0x20(%rsp)
cmpq $0x1, %r8
adcq $0x0, %r8
movq %r8, 0x10(%rsp)
movq $0x0, (%rsp)
movq %rsi, 0x8(%rsp)
cmpq %rsi, 0x18(%rsp)
je 0xd018
movq (%rsp), %rax
leaq (,%rax,8), %rax
leaq (%rax,%rax,2), %r15
xorl %r14d, %r14d
testq %rbp, %rbp
je 0xd00e
leaq (,%r14,8), %rax
leaq (%rax,%rax,2), %r12
xorl %r13d, %r13d
movq 0x40(%rbx), %rax
movq (%rax,%r15), %rax
movq (%rax,%r12), %rax
movq (%rax,%r13,8), %rdi
movq 0xd8(%rbx), %rsi
addq %r15, %rsi
callq 0x201b2
movq 0x40(%rbx), %rax
movq (%rax,%r15), %rax
movq (%rax,%r12), %rax
movq (%rax,%r13,8), %rdi
movq 0xf0(%rbx), %rsi
addq %r15, %rsi
callq 0x201b8
incq %r13
cmpq %r13, %rbp
jne 0xcfc8
incq %r14
cmpq 0x20(%rsp), %r14
jne 0xcfb4
movq (%rsp), %rcx
incq %rcx
movq %rcx, (%rsp)
cmpq 0x10(%rsp), %rcx
movq 0x8(%rsp), %rsi
jne 0xcf9a
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1b032(%rip), %rdi     # 0x2807b
leaq 0x1a6fa(%rip), %rsi     # 0x2774a
leaq 0x1b2e9(%rip), %rcx     # 0x28340
movl $0xa31, %edx            # imm = 0xA31
callq 0x4160
leaq 0x1a898(%rip), %rdi     # 0x27900
leaq 0x1a6db(%rip), %rsi     # 0x2774a
leaq 0x1b2ca(%rip), %rcx     # 0x28340
movl $0xa2f, %edx            # imm = 0xA2F
callq 0x4160
leaq 0x1b2f6(%rip), %rdi     # 0x2837d
leaq 0x1a6bc(%rip), %rsi     # 0x2774a
leaq 0x1b2ab(%rip), %rcx     # 0x28340
movl $0xa41, %edx            # imm = 0xA41
callq 0x4160
movq 0x29f32(%rip), %rdi     # 0x36fd8
leaq 0x1b2ec(%rip), %rsi     # 0x28399
callq 0x4260
movq %rax, %rdi
callq 0x4100
xorl %edi, %edi
callq 0x4290
nop