// File: /workspace/github_cpp_makefile_star2_O1/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), %rax
cmpq %rax, %r12
jbe 0x14424
cmpq %rax, %rbp
jbe 0x14424
movq 0xd0(%rbx), %rcx
movq 0xd8(%rbx), %rsi
subq %rsi, %r12
movq %r12, %rax
xorl %edx, %edx
divq %rcx
xorl %r15d, %r15d
cmpq $0x1, %rdx
sbbq $0x0, %rax
addq $0x2, %rax
movq %rax, 0x8(%rbx)
subq %rsi, %rbp
movq %rbp, %rax
xorl %edx, %edx
divq %rcx
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 0x1529a
leaq 0x78(%rbx), %rdi
movq %rdi, 0x18(%rsp)
movq %r14, %rsi
callq 0x1529a
leaq 0x40(%rbx), %rdi
movq %rdi, 0x28(%rsp)
movq %r14, %rsi
callq 0x1555e
testq %r14, %r14
je 0x14313
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)
movb $0x1, %r12b
leaq 0x8(%rsp), %rax
movq %rax, (%rsp)
movq %rbp, %rdi
movq %rdx, %rsi
leaq 0x14(%rsp), %rcx
leaq 0x10(%rsp), %r8
leaq 0xc(%rsp), %r9
callq 0x1c51e
movq %rbp, 0x30(%rsp)
movq 0x48(%rbx), %rsi
cmpq 0x50(%rbx), %rsi
je 0x1425e
movq %rbp, (%rsi)
addq $0x8, 0x48(%rbx)
jmp 0x1426e
xorl %r12d, %r12d
movq 0x28(%rsp), %rdi
movq %r13, %rdx
callq 0x157b6
movl $0x28, %edi
callq 0x8320
movq %rax, %rbp
movq 0xd8(%rbx), %rdx
movq %rax, %rdi
movq %rdx, %rsi
callq 0x1845e
movq %rbp, 0x30(%rsp)
movq 0xa0(%rbx), %rsi
cmpq 0xa8(%rbx), %rsi
je 0x142af
movq %rbp, (%rsi)
addq $0x8, 0xa0(%rbx)
jmp 0x142bc
movq 0x20(%rsp), %rdi
movq %r13, %rdx
callq 0x10430
movl $0x28, %edi
callq 0x8320
movq %rax, %rbp
movq 0xd8(%rbx), %rdx
movq %rax, %rdi
movq %rdx, %rsi
callq 0x1845e
movq %rbp, 0x30(%rsp)
movq 0x80(%rbx), %rsi
cmpq 0x88(%rbx), %rsi
je 0x142fd
movq %rbp, (%rsi)
addq $0x8, 0x80(%rbx)
jmp 0x1430a
movq 0x18(%rsp), %rdi
movq %r13, %rdx
callq 0x10430
decq %r14
jne 0x141f4
movq 0x58(%rbx), %rdi
leaq 0x30(%rsp), %r14
movl %r15d, (%r14)
movq 0x20(%rbx), %rdx
movl $0x1, %ecx
movq %r14, %rsi
callq 0x195d8
movq 0x90(%rbx), %rdi
movl %r15d, (%r14)
movq 0x20(%rbx), %rdx
leaq 0x30(%rsp), %r14
movl $0x1, %ecx
movq %r14, %rsi
callq 0x195d8
movq 0xb0(%rbx), %rdi
movl %r15d, (%r14)
movq 0x20(%rbx), %rdx
leaq 0x30(%rsp), %rsi
movl $0x1, %ecx
callq 0x195d8
cmpq $0x0, 0x20(%rbx)
je 0x14412
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 0x18440
movq %r13, 0x30(%rsp)
movq 0xc0(%rbx), %rsi
cmpq 0xc8(%rbx), %rsi
je 0x143c1
movq %r13, (%rsi)
addq $0x8, 0xc0(%rbx)
jmp 0x143cc
movq %r14, %rdi
movq %r12, %rdx
callq 0x10430
movl $0x28, %edi
callq 0x8320
movq %rax, %r13
movq %rax, %rdi
callq 0x18440
movq %r13, 0x30(%rsp)
movq 0x68(%rbx), %rsi
cmpq 0x70(%rbx), %rsi
je 0x143fa
movq %r13, (%rsi)
addq $0x8, 0x68(%rbx)
jmp 0x14405
movq %r15, %rdi
movq %r12, %rdx
callq 0x10430
incq %rbp
cmpq 0x20(%rbx), %rbp
jne 0x1438a
addq $0x1b8, %rsp            # imm = 0x1B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x25961(%rip), %rsi     # 0x39d8c
leaq 0x30(%rsp), %r13
movq %r13, %rdi
movl $0x41, %edx
movl $0x3, %ecx
callq 0x373d6
leaq 0x259ae(%rip), %rsi     # 0x39df7
movl $0x40, %edx
movq %r13, %rdi
callq 0x8350
leaq 0x30(%rsp), %rdi
callq 0x375fe
jmp 0x1415c
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x375fe
jmp 0x14495
jmp 0x14476
movq %rax, %rbx
movq %r13, %rdi
jmp 0x14490
jmp 0x14480
movq %rax, %rbx
jmp 0x1448d
movq %rax, %rbx
testb %r12b, %r12b
je 0x14495
movq %rbp, %rdi
callq 0x8300
movq %rbx, %rdi
callq 0x8500
nop