// File: /workspace/github_cpp_makefile_star2_O2/btbujiangjun[P]abcdl/src/cnn/Layer.cpp
// Function: abcdl::cnn::ConvolutionLayer::initialize(abcdl::cnn::Layer*)
void ConvolutionLayer::initialize(Layer* pre_layer){
    size_t pre_rows = pre_layer->get_rows();
    size_t pre_cols = pre_layer->get_cols();

    CHECK(pre_rows > _kernal_size && pre_cols > _kernal_size);

    this->_rows = (pre_rows - _kernal_size) % _stride == 0 ? (pre_rows - _kernal_size) / _stride + 1 : (pre_rows - _kernal_size) / _stride + 2;
    this->_cols = (pre_cols - _kernal_size) % _stride == 0 ? (pre_cols - _kernal_size) / _stride + 1 : (pre_cols - _kernal_size) / _stride + 2;

    this->_in_channel_size = pre_layer->get_out_channel_size();

    size_t size = this->_in_channel_size * this->_out_channel_size;
    this->_batch_weights.reserve(size);
    this->_delta_weights.reserve(size);
	this->_weights.reserve(size);
	for(size_t i = 0; i != size; i++){
        this->_weights.push_back(new abcdl::algebra::RandomMatrix<real>(_kernal_size, _kernal_size, 0.0, 0.5));
        this->_batch_weights.push_back(new abcdl::algebra::Mat(_kernal_size, _kernal_size));
        this->_delta_weights.push_back(new abcdl::algebra::Mat(_kernal_size, _kernal_size));
	}

    //all channels shared the same bias of current layer.
	this->_bias->reset(0, this->_out_channel_size, 1);
	this->_delta_bias->reset(0, this->_out_channel_size, 1);
	this->_batch_bias->reset(0, this->_out_channel_size, 1);
    
    for(size_t i = 0; i != this->_out_channel_size; i++){
        this->_deltas.push_back(new abcdl::algebra::Mat());
        this->_activations.push_back(new abcdl::algebra::Mat());
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1b8, %rsp            # imm = 0x1B8
movq %rsi, %r14
movq %rdi, %rbx
movq 0x8(%rsi), %r12
movq 0x10(%rsi), %r15
movq 0xd8(%rdi), %rcx
cmpq %rcx, %r12
jbe 0x12e28
cmpq %rcx, %r15
jbe 0x12e28
subq %rcx, %r12
movq 0xd0(%rbx), %rsi
movq %r12, %rax
xorl %edx, %edx
divq %rsi
cmpq $0x1, %rdx
sbbq $0x0, %rax
addq $0x2, %rax
movq %rax, 0x8(%rbx)
subq %rcx, %r15
movq %r15, %rax
xorl %edx, %edx
divq %rsi
cmpq $0x1, %rdx
sbbq $0x0, %rax
addq $0x2, %rax
movq %rax, 0x10(%rbx)
movq 0x20(%r14), %r14
movq %r14, 0x18(%rbx)
imulq 0x20(%rbx), %r14
leaq 0x98(%rbx), %rdi
movq %rdi, 0x10(%rsp)
movq %r14, %rsi
callq 0x137d4
leaq 0x78(%rbx), %rdi
movq %rdi, 0x28(%rsp)
movq %r14, %rsi
callq 0x137d4
leaq 0x40(%rbx), %r13
movq %r13, %rdi
movq %r14, %rsi
callq 0x13b0a
leaq 0x30(%rsp), %rbp
pushq $0x28
popq %r15
subq $0x1, %r14
jb 0x12d4a
pushq $0x38
popq %rdi
callq 0x8310
movq 0xd8(%rbx), %rdx
andl $0x0, 0x24(%rsp)
movq %rax, %r12
andl $0x0, 0x1c(%rsp)
movl $0x3f000000, 0x20(%rsp) # imm = 0x3F000000
andl $0x0, 0x18(%rsp)
leaq 0x18(%rsp), %rax
movq %rax, (%rsp)
movq %r12, %rdi
movq %rdx, %rsi
leaq 0x24(%rsp), %rcx
leaq 0x20(%rsp), %r8
leaq 0x1c(%rsp), %r9
callq 0x19ca4
movq %r12, 0x30(%rsp)
movq %r13, %rdi
movq %rbp, %rsi
callq 0x13f9e
movq %r15, %rdi
callq 0x8310
movq %rax, %r12
movq 0xd8(%rbx), %rdx
movq %rax, %rdi
movq %rdx, %rsi
callq 0x165ea
movq %r12, 0x30(%rsp)
movq 0x10(%rsp), %rdi
movq %rbp, %rsi
callq 0xf594
movq %r15, %rdi
callq 0x8310
movq %rax, %r12
movq 0xd8(%rbx), %rdx
movq %rax, %rdi
movq %rdx, %rsi
callq 0x165ea
movq %r12, 0x30(%rsp)
movq 0x28(%rsp), %rdi
movq %rbp, %rsi
callq 0xf594
jmp 0x12c81
movq 0x58(%rbx), %rdi
leaq 0x30(%rsp), %rbp
andl $0x0, (%rbp)
movq 0x20(%rbx), %rdx
pushq $0x1
popq %r15
movq %rbp, %rsi
movq %r15, %rcx
callq 0x173e2
movq 0x90(%rbx), %rdi
andl $0x0, (%rbp)
movq 0x20(%rbx), %rdx
leaq 0x30(%rsp), %r12
movq %r12, %rsi
movq %r15, %rcx
callq 0x173e2
movq 0xb0(%rbx), %rdi
andl $0x0, (%r12)
movq 0x20(%rbx), %rdx
leaq 0x30(%rsp), %rsi
movq %r15, %rcx
callq 0x173e2
leaq 0xb8(%rbx), %rax
movq %rax, 0x10(%rsp)
leaq 0x60(%rbx), %r12
xorl %r14d, %r14d
pushq $0x28
popq %r13
cmpq 0x20(%rbx), %r14
je 0x12e16
movq %rbp, %r15
movq %r13, %rdi
callq 0x8310
movq %rax, %rbp
movq %rax, %rdi
callq 0x165d0
movq %rbp, 0x30(%rsp)
movq 0x10(%rsp), %rdi
movq %r15, %rsi
callq 0xf594
movq %r13, %rdi
callq 0x8310
movq %rax, %rbp
movq %rax, %rdi
callq 0x165d0
movq %rbp, 0x30(%rsp)
movq %r12, %rdi
movq %r15, %rsi
callq 0xf594
incq %r14
movq %r15, %rbp
jmp 0x12dbd
addq $0x1b8, %rsp            # imm = 0x1B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1df35(%rip), %rsi     # 0x30d64
leaq 0x30(%rsp), %r13
pushq $0x41
popq %rdx
pushq $0x3
popq %rcx
movq %r13, %rdi
callq 0x2d5b4
leaq 0x1df86(%rip), %rsi     # 0x30dcf
movq %r13, %rdi
callq 0x8300
leaq 0x30(%rsp), %rdi
callq 0x2d7f0
movq 0xd8(%rbx), %rcx
jmp 0x12bfa
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x2d7f0
jmp 0x12e94
jmp 0x12e78
movq %rax, %rbx
movq %rbp, %rdi
jmp 0x12e8f
jmp 0x12e89
movq %rax, %rbx
jmp 0x12e94
jmp 0x12e89
movq %rax, %rbx
movq %r12, %rdi
callq 0x82f0
movq %rbx, %rdi
callq 0x84f0