// File: xbai0624[P]dnn/src/ConstructLayer.cpp
// Function: ConstructLayer::InitFilters()
void ConstructLayer::InitFilters()
{
    // clear previous filter
    __activeFlag.clear();

    // init filter 2d matrix, fill true value to all elements
    if(__type == LayerType::input)
    {
        if(__layerDimension == LayerDimension::_1D)
        {
            assert(__neurons.size() == 1); // only one kernel
            auto dim = __neurons[0].Dimension();
            Filter2D f(dim.first, dim.second); // default to true
            __activeFlag.push_back(f);
        }
        else if(__layerDimension == LayerDimension::_2D)
        {
            // 2D input layer need a fake filter with dimension (number_of_fake_neurons, 1), similar like 1D
            // this is just in case one directly connect a 2D input layer to a 1D fc layer
            //
            // since filters in 2D middle layers are not used, so it is safe to code like 1D case
            assert(__neurons.size() == 1); // only one kernel
            auto dim = __neurons[0].Dimension(); // for 2D layer, the fake neurons has also been initialized
            Filter2D f(dim.first, dim.second); // default to true
            __activeFlag.push_back(f);
        }
    }
    else if(__type == LayerType::output)
    {
        assert(__neurons.size() == 1); // only one kernel
        auto dim = __neurons[0].Dimension();
        assert(dim.second == 1); // only one collum
        Filter2D f(dim.first, dim.second); // default to true
        __activeFlag.push_back(f);
    }
    else if(__type == LayerType::fullyConnected)
    {
        assert(__neurons.size() == 1);
        auto dim = __neurons[0].Dimension();
        Filter2D f(dim.first, dim.second); // default to true
        __activeFlag.push_back(f);
    }
    else if(__type == LayerType::cnn || __type == LayerType::pooling)
    {
        size_t nKernels = __weightMatrix.size();
        for(size_t i=0;i<nKernels;i++)
        {
            auto dim = __weightMatrix[i].Dimension();
            Filter2D f(dim.first, dim.second); // default to true
            __activeFlag.push_back(f);
        }
    }
}

// Assembly:
subq $0x128, %rsp            # imm = 0x128
movq %rdi, 0x120(%rsp)
movq 0x120(%rsp), %rdi
movq %rdi, 0x30(%rsp)
addq $0x108, %rdi            # imm = 0x108
callq 0x1f820
movq 0x30(%rsp), %rax
cmpl $0x3, 0xc(%rax)
jne 0x8d71
movq 0x30(%rsp), %rax
cmpl $0x0, 0x408(%rax)
jne 0x8c8a
movq 0x30(%rsp), %rdi
addq $0x40, %rdi
callq 0x1f630
cmpq $0x1, %rax
jne 0x8bcf
jmp 0x8bee
leaq 0x497f1(%rip), %rdi     # 0x523c7
leaq 0x495f5(%rip), %rsi     # 0x521d2
movl $0x208, %edx            # imm = 0x208
leaq 0x497f4(%rip), %rcx     # 0x523dd
callq 0x4170
movq 0x30(%rsp), %rdi
addq $0x40, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x1f650
movq %rax, %rdi
callq 0x1f530
movq %rax, 0x110(%rsp)
movq %rdx, 0x118(%rsp)
movq 0x110(%rsp), %rsi
movq 0x118(%rsp), %rdx
leaq 0xf8(%rsp), %rdi
movq %rdi, 0x28(%rsp)
callq 0x1f840
movq 0x30(%rsp), %rdi
movq 0x28(%rsp), %rsi
addq $0x108, %rdi            # imm = 0x108
callq 0x1f940
jmp 0x8c52
leaq 0xf8(%rsp), %rdi
callq 0x1f9b0
jmp 0x8d6c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xf0(%rsp)
movl %eax, 0xec(%rsp)
leaq 0xf8(%rsp), %rdi
callq 0x1f9b0
jmp 0x903a
movq 0x30(%rsp), %rax
cmpl $0x1, 0x408(%rax)
jne 0x8d6a
movq 0x30(%rsp), %rdi
addq $0x40, %rdi
callq 0x1f630
cmpq $0x1, %rax
jne 0x8cb2
jmp 0x8cd1
leaq 0x4970e(%rip), %rdi     # 0x523c7
leaq 0x49512(%rip), %rsi     # 0x521d2
movl $0x213, %edx            # imm = 0x213
leaq 0x49711(%rip), %rcx     # 0x523dd
callq 0x4170
movq 0x30(%rsp), %rdi
addq $0x40, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x1f650
movq %rax, %rdi
callq 0x1f530
movq %rax, 0xd8(%rsp)
movq %rdx, 0xe0(%rsp)
movq 0xd8(%rsp), %rsi
movq 0xe0(%rsp), %rdx
leaq 0xc0(%rsp), %rdi
movq %rdi, 0x20(%rsp)
callq 0x1f840
movq 0x30(%rsp), %rdi
movq 0x20(%rsp), %rsi
addq $0x108, %rdi            # imm = 0x108
callq 0x1f940
jmp 0x8d35
leaq 0xc0(%rsp), %rdi
callq 0x1f9b0
jmp 0x8d6a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xf0(%rsp)
movl %eax, 0xec(%rsp)
leaq 0xc0(%rsp), %rdi
callq 0x1f9b0
jmp 0x903a
jmp 0x8d6c
jmp 0x9032
movq 0x30(%rsp), %rax
cmpl $0x4, 0xc(%rax)
jne 0x8e7d
movq 0x30(%rsp), %rdi
addq $0x40, %rdi
callq 0x1f630
cmpq $0x1, %rax
jne 0x8d96
jmp 0x8db5
leaq 0x4962a(%rip), %rdi     # 0x523c7
leaq 0x4942e(%rip), %rsi     # 0x521d2
movl $0x21b, %edx            # imm = 0x21B
leaq 0x4962d(%rip), %rcx     # 0x523dd
callq 0x4170
movq 0x30(%rsp), %rdi
addq $0x40, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x1f650
movq %rax, %rdi
callq 0x1f530
movq %rax, 0xb0(%rsp)
movq %rdx, 0xb8(%rsp)
cmpq $0x1, 0xb8(%rsp)
jne 0x8dec
jmp 0x8e0b
leaq 0x49d4f(%rip), %rdi     # 0x52b42
leaq 0x493d8(%rip), %rsi     # 0x521d2
movl $0x21d, %edx            # imm = 0x21D
leaq 0x495d7(%rip), %rcx     # 0x523dd
callq 0x4170
movq 0xb0(%rsp), %rsi
movq 0xb8(%rsp), %rdx
leaq 0x98(%rsp), %rdi
movq %rdi, 0x18(%rsp)
callq 0x1f840
movq 0x30(%rsp), %rdi
movq 0x18(%rsp), %rsi
addq $0x108, %rdi            # imm = 0x108
callq 0x1f940
jmp 0x8e45
leaq 0x98(%rsp), %rdi
callq 0x1f9b0
jmp 0x9030
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xf0(%rsp)
movl %eax, 0xec(%rsp)
leaq 0x98(%rsp), %rdi
callq 0x1f9b0
jmp 0x903a
movq 0x30(%rsp), %rax
cmpl $0x0, 0xc(%rax)
jne 0x8f54
movq 0x30(%rsp), %rdi
addq $0x40, %rdi
callq 0x1f630
cmpq $0x1, %rax
jne 0x8ea2
jmp 0x8ec1
leaq 0x4951e(%rip), %rdi     # 0x523c7
leaq 0x49322(%rip), %rsi     # 0x521d2
movl $0x223, %edx            # imm = 0x223
leaq 0x49521(%rip), %rcx     # 0x523dd
callq 0x4170
movq 0x30(%rsp), %rdi
addq $0x40, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x1f650
movq %rax, %rdi
callq 0x1f530
movq %rax, 0x88(%rsp)
movq %rdx, 0x90(%rsp)
movq 0x88(%rsp), %rsi
movq 0x90(%rsp), %rdx
leaq 0x70(%rsp), %rdi
movq %rdi, 0x10(%rsp)
callq 0x1f840
movq 0x30(%rsp), %rdi
movq 0x10(%rsp), %rsi
addq $0x108, %rdi            # imm = 0x108
callq 0x1f940
jmp 0x8f22
leaq 0x70(%rsp), %rdi
callq 0x1f9b0
jmp 0x902e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xf0(%rsp)
movl %eax, 0xec(%rsp)
leaq 0x70(%rsp), %rdi
callq 0x1f9b0
jmp 0x903a
movq 0x30(%rsp), %rax
cmpl $0x1, 0xc(%rax)
je 0x8f6e
movq 0x30(%rsp), %rax
cmpl $0x2, 0xc(%rax)
jne 0x902c
movq 0x30(%rsp), %rdi
addq $0x60, %rdi
callq 0x1f9c0
movq %rax, 0x68(%rsp)
movq $0x0, 0x60(%rsp)
movq 0x60(%rsp), %rax
cmpq 0x68(%rsp), %rax
jae 0x902a
movq 0x30(%rsp), %rdi
addq $0x60, %rdi
movq 0x60(%rsp), %rsi
callq 0x1f9e0
movq %rax, %rdi
callq 0x3bae0
movq %rax, 0x50(%rsp)
movq %rdx, 0x58(%rsp)
movq 0x50(%rsp), %rsi
movq 0x58(%rsp), %rdx
leaq 0x38(%rsp), %rdi
movq %rdi, 0x8(%rsp)
callq 0x1f840
movq 0x30(%rsp), %rdi
movq 0x8(%rsp), %rsi
addq $0x108, %rdi            # imm = 0x108
callq 0x1f940
jmp 0x8ff0
leaq 0x38(%rsp), %rdi
callq 0x1f9b0
movq 0x60(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x60(%rsp)
jmp 0x8f8a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xf0(%rsp)
movl %eax, 0xec(%rsp)
leaq 0x38(%rsp), %rdi
callq 0x1f9b0
jmp 0x903a
jmp 0x902c
jmp 0x902e
jmp 0x9030
jmp 0x9032
addq $0x128, %rsp            # imm = 0x128
retq
movq 0xf0(%rsp), %rdi
callq 0x43f0
nopw (%rax,%rax)