// File: /workspace/github_cpp_makefile_star2_O3/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), %rbp
movq 0xd8(%rdi), %rcx
cmpq %rcx, %r12
jbe 0x14186
cmpq %rcx, %rbp
jbe 0x14186
subq %rcx, %r12
movq 0xd0(%rbx), %rsi
movq %r12, %rax
xorl %edx, %edx
divq %rsi
xorl %r15d, %r15d
cmpq $0x1, %rdx
sbbq $0x0, %rax
addq $0x2, %rax
movq %rax, 0x8(%rbx)
subq %rcx, %rbp
movq %rbp, %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, 0x20(%rsp)
movq %r14, %rsi
callq 0x15000
leaq 0x78(%rbx), %rdi
movq %rdi, 0x18(%rsp)
movq %r14, %rsi
callq 0x15000
leaq 0x40(%rbx), %rdi
movq %rdi, 0x28(%rsp)
movq %r14, %rsi
callq 0x152ce
testq %r14, %r14
je 0x14075
leaq 0xc(%rsp), %r12
leaq 0x30(%rsp), %r13
movl $0x38, %edi
callq 0x8320
movq %rax, %rbp
movq 0xd8(%rbx), %rdx
movl %r15d, 0x14(%rsp)
movl $0x3f000000, 0x10(%rsp) # imm = 0x3F000000
movl %r15d, 0xc(%rsp)
movl %r15d, 0x8(%rsp)
leaq 0x8(%rsp), %rax
movq %rax, (%rsp)
movq %rbp, %rdi
movq %rdx, %rsi
leaq 0x14(%rsp), %rcx
leaq 0x10(%rsp), %r8
movq %r12, %r9
callq 0x1c276
movq %rbp, 0x30(%rsp)
movq 0x48(%rbx), %rsi
cmpq 0x50(%rbx), %rsi
je 0x13fc3
movq %rbp, (%rsi)
addq $0x8, 0x48(%rbx)
jmp 0x13fd0
movq 0x28(%rsp), %rdi
movq %r13, %rdx
callq 0x15530
movl $0x28, %edi
callq 0x8320
movq %rax, %rbp
movq 0xd8(%rbx), %rdx
movq %rax, %rdi
movq %rdx, %rsi
callq 0x181d6
movq %rbp, 0x30(%rsp)
movq 0xa0(%rbx), %rsi
cmpq 0xa8(%rbx), %rsi
je 0x14011
movq %rbp, (%rsi)
addq $0x8, 0xa0(%rbx)
jmp 0x1401e
movq 0x20(%rsp), %rdi
movq %r13, %rdx
callq 0x10156
movl $0x28, %edi
callq 0x8320
movq %rax, %rbp
movq 0xd8(%rbx), %rdx
movq %rax, %rdi
movq %rdx, %rsi
callq 0x181d6
movq %rbp, 0x30(%rsp)
movq 0x80(%rbx), %rsi
cmpq 0x88(%rbx), %rsi
je 0x1405f
movq %rbp, (%rsi)
addq $0x8, 0x80(%rbx)
jmp 0x1406c
movq 0x18(%rsp), %rdi
movq %r13, %rdx
callq 0x10156
decq %r14
jne 0x13f5e
movq 0x58(%rbx), %rdi
leaq 0x30(%rsp), %r14
movl %r15d, (%r14)
movq 0x20(%rbx), %rdx
movl $0x1, %ecx
movq %r14, %rsi
callq 0x1934e
movq 0x90(%rbx), %rdi
movl %r15d, (%r14)
movq 0x20(%rbx), %rdx
leaq 0x30(%rsp), %r14
movl $0x1, %ecx
movq %r14, %rsi
callq 0x1934e
movq 0xb0(%rbx), %rdi
movl %r15d, (%r14)
movq 0x20(%rbx), %rdx
leaq 0x30(%rsp), %rsi
movl $0x1, %ecx
callq 0x1934e
cmpq $0x0, 0x20(%rbx)
je 0x14174
leaq 0xb8(%rbx), %r14
leaq 0x60(%rbx), %r15
xorl %ebp, %ebp
leaq 0x30(%rsp), %r12
movl $0x28, %edi
callq 0x8320
movq %rax, %r13
movq %rax, %rdi
callq 0x181b8
movq %r13, 0x30(%rsp)
movq 0xc0(%rbx), %rsi
cmpq 0xc8(%rbx), %rsi
je 0x14123
movq %r13, (%rsi)
addq $0x8, 0xc0(%rbx)
jmp 0x1412e
movq %r14, %rdi
movq %r12, %rdx
callq 0x10156
movl $0x28, %edi
callq 0x8320
movq %rax, %r13
movq %rax, %rdi
callq 0x181b8
movq %r13, 0x30(%rsp)
movq 0x68(%rbx), %rsi
cmpq 0x70(%rbx), %rsi
je 0x1415c
movq %r13, (%rsi)
addq $0x8, 0x68(%rbx)
jmp 0x14167
movq %r15, %rdi
movq %r12, %rdx
callq 0x10156
incq %rbp
cmpq 0x20(%rbx), %rbp
jne 0x140ec
addq $0x1b8, %rsp            # imm = 0x1B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x25bff(%rip), %rsi     # 0x39d8c
leaq 0x30(%rsp), %r13
movq %r13, %rdi
movl $0x41, %edx
movl $0x3, %ecx
callq 0x37222
leaq 0x25c4c(%rip), %rsi     # 0x39df7
movl $0x40, %edx
movq %r13, %rdi
callq 0x8350
leaq 0x30(%rsp), %rdi
callq 0x3744a
movq 0xd8(%rbx), %rcx
jmp 0x13ec8
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x3744a
jmp 0x141fb
movq %rax, %rbx
jmp 0x141fb
jmp 0x141e4
movq %rax, %rbx
movq %r13, %rdi
jmp 0x141f6
jmp 0x141f0
jmp 0x141f0
movq %rax, %rbx
movq %rbp, %rdi
callq 0x8300
movq %rbx, %rdi
callq 0x8500
nop