// File: 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:
subq $0x298, %rsp            # imm = 0x298
movq %rdi, 0x290(%rsp)
movq %rsi, 0x288(%rsp)
movq 0x290(%rsp), %rax
movq %rax, 0x70(%rsp)
movq 0x288(%rsp), %rdi
callq 0x29930
movq %rax, 0x280(%rsp)
movq 0x288(%rsp), %rdi
callq 0x29940
movq 0x70(%rsp), %rdx
movq %rax, 0x278(%rsp)
movq 0x280(%rsp), %rcx
xorl %eax, %eax
cmpq 0xd8(%rdx), %rcx
movb %al, 0x7f(%rsp)
jbe 0x28045
movq 0x70(%rsp), %rcx
movq 0x278(%rsp), %rax
cmpq 0xd8(%rcx), %rax
seta %al
movb %al, 0x7f(%rsp)
movb 0x7f(%rsp), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x28051
jmp 0x280bc
leaq 0x64d10(%rip), %rsi     # 0x8cd68
leaq 0xf0(%rsp), %rdi
movq %rdi, 0x68(%rsp)
movl $0x41, %edx
movl $0x3, %ecx
callq 0x86270
movq 0x68(%rsp), %rdi
leaq 0x64d53(%rip), %rsi     # 0x8cdd3
callq 0x29f50
jmp 0x28087
leaq 0xf0(%rsp), %rdi
callq 0x86630
jmp 0x280bc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xe8(%rsp)
movl %eax, 0xe4(%rsp)
leaq 0xf0(%rsp), %rdi
callq 0x86630
jmp 0x285bb
movq 0x70(%rsp), %rcx
movq 0x280(%rsp), %rax
subq 0xd8(%rcx), %rax
xorl %edx, %edx
divq 0xd0(%rcx)
cmpq $0x0, %rdx
jne 0x28107
movq 0x70(%rsp), %rcx
movq 0x280(%rsp), %rax
subq 0xd8(%rcx), %rax
xorl %edx, %edx
divq 0xd0(%rcx)
addq $0x1, %rax
movq %rax, 0x60(%rsp)
jmp 0x2812d
movq 0x70(%rsp), %rcx
movq 0x280(%rsp), %rax
subq 0xd8(%rcx), %rax
xorl %edx, %edx
divq 0xd0(%rcx)
addq $0x2, %rax
movq %rax, 0x60(%rsp)
movq 0x70(%rsp), %rcx
movq 0x60(%rsp), %rax
movq %rax, 0x8(%rcx)
movq 0x278(%rsp), %rax
subq 0xd8(%rcx), %rax
xorl %edx, %edx
divq 0xd0(%rcx)
cmpq $0x0, %rdx
jne 0x28181
movq 0x70(%rsp), %rcx
movq 0x278(%rsp), %rax
subq 0xd8(%rcx), %rax
xorl %edx, %edx
divq 0xd0(%rcx)
addq $0x1, %rax
movq %rax, 0x58(%rsp)
jmp 0x281a7
movq 0x70(%rsp), %rcx
movq 0x278(%rsp), %rax
subq 0xd8(%rcx), %rax
xorl %edx, %edx
divq 0xd0(%rcx)
addq $0x2, %rax
movq %rax, 0x58(%rsp)
movq 0x70(%rsp), %rax
movq 0x58(%rsp), %rcx
movq %rcx, 0x10(%rax)
movq 0x288(%rsp), %rdi
callq 0x29980
movq 0x70(%rsp), %rdi
movq %rax, 0x18(%rdi)
movq 0x18(%rdi), %rax
imulq 0x20(%rdi), %rax
movq %rax, 0xd8(%rsp)
addq $0x98, %rdi
movq 0xd8(%rsp), %rsi
callq 0x29990
movq 0x70(%rsp), %rdi
addq $0x78, %rdi
movq 0xd8(%rsp), %rsi
callq 0x29990
movq 0x70(%rsp), %rdi
addq $0x40, %rdi
movq 0xd8(%rsp), %rsi
callq 0x29f80
movq $0x0, 0xd0(%rsp)
movq 0xd0(%rsp), %rax
cmpq 0xd8(%rsp), %rax
je 0x2841c
movq 0x70(%rsp), %rax
addq $0x40, %rax
movq %rax, 0x48(%rsp)
movl $0x38, %edi
callq 0x93a0
movq %rax, %rdi
movq 0x70(%rsp), %rax
movq %rdi, %rcx
movq %rcx, 0x50(%rsp)
movq 0xd8(%rax), %rdx
movl $0x0, 0xc4(%rsp)
movl $0x3f000000, 0xc0(%rsp) # imm = 0x3F000000
movl $0x0, 0xbc(%rsp)
movl $0x0, 0xb8(%rsp)
movq %rsp, %rax
leaq 0xb8(%rsp), %rcx
movq %rcx, (%rax)
leaq 0xc4(%rsp), %rcx
leaq 0xc0(%rsp), %r8
leaq 0xbc(%rsp), %r9
movq %rdx, %rsi
callq 0x36e00
jmp 0x282c9
movq 0x48(%rsp), %rdi
movq 0x50(%rsp), %rax
movq %rax, 0xc8(%rsp)
leaq 0xc8(%rsp), %rsi
callq 0x2a090
movq 0x70(%rsp), %rax
addq $0x98, %rax
movq %rax, 0x38(%rsp)
movl $0x28, %edi
callq 0x93a0
movq %rax, %rdi
movq 0x70(%rsp), %rax
movq %rdi, %rcx
movq %rcx, 0x40(%rsp)
movq 0xd8(%rax), %rdx
movq %rdx, %rsi
callq 0x30540
jmp 0x28323
movq 0x38(%rsp), %rdi
movq 0x40(%rsp), %rax
movq %rax, 0xb0(%rsp)
leaq 0xb0(%rsp), %rsi
callq 0x178b0
movq 0x70(%rsp), %rax
addq $0x78, %rax
movq %rax, 0x28(%rsp)
movl $0x28, %edi
callq 0x93a0
movq %rax, %rdi
movq 0x70(%rsp), %rax
movq %rdi, %rcx
movq %rcx, 0x30(%rsp)
movq 0xd8(%rax), %rdx
movq %rdx, %rsi
callq 0x30540
jmp 0x2837b
movq 0x28(%rsp), %rdi
movq 0x30(%rsp), %rax
movq %rax, 0xa8(%rsp)
leaq 0xa8(%rsp), %rsi
callq 0x178b0
movq 0xd0(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xd0(%rsp)
jmp 0x28228
movq 0x50(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xe8(%rsp)
movl %eax, 0xe4(%rsp)
callq 0x9380
jmp 0x285bb
movq 0x40(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xe8(%rsp)
movl %eax, 0xe4(%rsp)
callq 0x9380
jmp 0x285bb
movq 0x30(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xe8(%rsp)
movl %eax, 0xe4(%rsp)
callq 0x9380
jmp 0x285bb
movq 0x70(%rsp), %rax
movq 0x58(%rax), %rdi
xorps %xmm0, %xmm0
movss %xmm0, 0xa4(%rsp)
movq 0x20(%rax), %rdx
leaq 0xa4(%rsp), %rsi
movl $0x1, %ecx
callq 0x31f20
movq 0x70(%rsp), %rax
movq 0x90(%rax), %rdi
xorps %xmm0, %xmm0
movss %xmm0, 0xa0(%rsp)
movq 0x20(%rax), %rdx
leaq 0xa0(%rsp), %rsi
movl $0x1, %ecx
callq 0x31f20
movq 0x70(%rsp), %rax
movq 0xb0(%rax), %rdi
xorps %xmm0, %xmm0
movss %xmm0, 0x9c(%rsp)
movq 0x20(%rax), %rdx
leaq 0x9c(%rsp), %rsi
movl $0x1, %ecx
callq 0x31f20
movq $0x0, 0x90(%rsp)
movq 0x70(%rsp), %rcx
movq 0x90(%rsp), %rax
cmpq 0x20(%rcx), %rax
je 0x285b3
movq 0x70(%rsp), %rax
addq $0xb8, %rax
movq %rax, 0x18(%rsp)
movl $0x28, %edi
callq 0x93a0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x20(%rsp)
callq 0x30500
jmp 0x284f2
movq 0x18(%rsp), %rdi
movq 0x20(%rsp), %rax
movq %rax, 0x88(%rsp)
leaq 0x88(%rsp), %rsi
callq 0x178b0
movq 0x70(%rsp), %rax
addq $0x60, %rax
movq %rax, 0x8(%rsp)
movl $0x28, %edi
callq 0x93a0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x10(%rsp)
callq 0x30500
jmp 0x2853b
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rax
movq %rax, 0x80(%rsp)
leaq 0x80(%rsp), %rsi
callq 0x178b0
movq 0x90(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x90(%rsp)
jmp 0x284af
movq 0x20(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xe8(%rsp)
movl %eax, 0xe4(%rsp)
callq 0x9380
jmp 0x285bb
movq 0x10(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xe8(%rsp)
movl %eax, 0xe4(%rsp)
callq 0x9380
jmp 0x285bb
addq $0x298, %rsp            # imm = 0x298
retq
movq 0xe8(%rsp), %rdi
callq 0x95d0
nopl (%rax,%rax)