// File: 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:
subq $0x88, %rsp
movq %rdi, 0x80(%rsp)
movq 0x80(%rsp), %rax
movq %rax, 0x28(%rsp)
cmpl $0x0, 0xc(%rax)
je 0x137e1
movq 0x28(%rsp), %rax
cmpl $0x4, 0xc(%rax)
jne 0x13993
movq 0x28(%rsp), %rdi
addq $0x40, %rdi
callq 0x1f630
cmpq $0x1, %rax
jne 0x137f7
jmp 0x13816
leaq 0x3ebc9(%rip), %rdi     # 0x523c7
leaq 0x3e9cd(%rip), %rsi     # 0x521d2
movl $0xa2f, %edx            # imm = 0xA2F
leaq 0x3f5f6(%rip), %rcx     # 0x52e07
callq 0x4170
movq 0x28(%rsp), %rdi
addq $0x40, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x1f650
movq %rax, %rdi
callq 0x1f530
movq %rax, 0x70(%rsp)
movq %rdx, 0x78(%rsp)
cmpq $0x1, 0x78(%rsp)
jne 0x13844
jmp 0x13863
leaq 0x3f2f7(%rip), %rdi     # 0x52b42
leaq 0x3e980(%rip), %rsi     # 0x521d2
movl $0xa31, %edx            # imm = 0xA31
leaq 0x3f5a9(%rip), %rcx     # 0x52e07
callq 0x4170
movq $0x0, 0x68(%rsp)
movq $0x0, 0x60(%rsp)
movq 0x60(%rsp), %rax
cmpq 0x70(%rsp), %rax
jae 0x1398e
movq 0x28(%rsp), %rdi
addq $0x40, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x1f650
movq %rax, %rdi
movq 0x60(%rsp), %rsi
callq 0x1f5c0
movq %rax, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x1f5f0
movq (%rax), %rdi
callq 0x47fe0
testb $0x1, %al
jne 0x138c1
jmp 0x1397b
movq 0x28(%rsp), %rdi
addq $0x40, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x1f650
movq %rax, %rdi
movq 0x60(%rsp), %rsi
callq 0x1f5c0
movq %rax, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x1f5f0
movq 0x28(%rsp), %rdi
movq (%rax), %rax
movq %rax, 0x18(%rsp)
addq $0xd8, %rdi
movq 0x68(%rsp), %rsi
callq 0x1f9e0
movq 0x18(%rsp), %rdi
movq %rax, %rsi
callq 0x47600
movq 0x28(%rsp), %rdi
addq $0x40, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x1f650
movq %rax, %rdi
movq 0x60(%rsp), %rsi
callq 0x1f5c0
movq %rax, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x1f5f0
movq 0x28(%rsp), %rdi
movq (%rax), %rax
movq %rax, 0x20(%rsp)
addq $0xf0, %rdi
movq 0x68(%rsp), %rsi
callq 0x1f9e0
movq 0x20(%rsp), %rdi
movq %rax, %rsi
callq 0x47620
movq 0x68(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x68(%rsp)
movq 0x60(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x60(%rsp)
jmp 0x13875
jmp 0x13b82
movq 0x28(%rsp), %rax
cmpl $0x1, 0xc(%rax)
je 0x139ad
movq 0x28(%rsp), %rax
cmpl $0x2, 0xc(%rax)
jne 0x13b57
movq 0x28(%rsp), %rdi
addq $0xd8, %rdi
callq 0x1f9c0
movq 0x28(%rsp), %rdi
movq %rax, 0x58(%rsp)
addq $0x40, %rdi
callq 0x1f630
cmpq 0x58(%rsp), %rax
jne 0x139da
jmp 0x139f9
leaq 0x3f463(%rip), %rdi     # 0x52e44
leaq 0x3e7ea(%rip), %rsi     # 0x521d2
movl $0xa41, %edx            # imm = 0xA41
leaq 0x3f413(%rip), %rcx     # 0x52e07
callq 0x4170
movq 0x28(%rsp), %rdi
addq $0x40, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x1f650
movq %rax, %rdi
callq 0x1f530
movq %rax, 0x48(%rsp)
movq %rdx, 0x50(%rsp)
movq $0x0, 0x40(%rsp)
movq 0x40(%rsp), %rax
cmpq 0x58(%rsp), %rax
jae 0x13b55
movq $0x0, 0x38(%rsp)
movq 0x38(%rsp), %rax
cmpq 0x48(%rsp), %rax
jae 0x13b40
movq $0x0, 0x30(%rsp)
movq 0x30(%rsp), %rax
cmpq 0x50(%rsp), %rax
jae 0x13b2b
movq 0x28(%rsp), %rdi
addq $0x40, %rdi
movq 0x40(%rsp), %rsi
callq 0x1f650
movq %rax, %rdi
movq 0x38(%rsp), %rsi
callq 0x1f5c0
movq %rax, %rdi
movq 0x30(%rsp), %rsi
callq 0x1f5f0
movq 0x28(%rsp), %rdi
movq (%rax), %rax
movq %rax, 0x8(%rsp)
addq $0xd8, %rdi
movq 0x40(%rsp), %rsi
callq 0x1f9e0
movq 0x8(%rsp), %rdi
movq %rax, %rsi
callq 0x47600
movq 0x28(%rsp), %rdi
addq $0x40, %rdi
movq 0x40(%rsp), %rsi
callq 0x1f650
movq %rax, %rdi
movq 0x38(%rsp), %rsi
callq 0x1f5c0
movq %rax, %rdi
movq 0x30(%rsp), %rsi
callq 0x1f5f0
movq 0x28(%rsp), %rdi
movq (%rax), %rax
movq %rax, 0x10(%rsp)
addq $0xf0, %rdi
movq 0x40(%rsp), %rsi
callq 0x1f9e0
movq 0x10(%rsp), %rdi
movq %rax, %rsi
callq 0x47620
movq 0x30(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x30(%rsp)
jmp 0x13a58
jmp 0x13b2d
movq 0x38(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x38(%rsp)
jmp 0x13a3f
jmp 0x13b42
movq 0x40(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x40(%rsp)
jmp 0x13a26
jmp 0x13b80
movq 0x5747a(%rip), %rdi     # 0x6afd8
leaq 0x3f2fb(%rip), %rsi     # 0x52e60
callq 0x4230
movq %rax, %rdi
movq 0x5743c(%rip), %rsi     # 0x6afb0
callq 0x4260
xorl %edi, %edi
callq 0x4290
jmp 0x13b82
addq $0x88, %rsp
retq
nopw (%rax,%rax)