// File: /workspace/github_cpp_makefile_star2_O2/xbai0624[P]dnn/src/ConstructLayer.cpp
// Function: ConstructLayer::GetSigmaSquareFor2DLayer(std::vector<Images, std::allocator<Images>>&, Images&, Images&)
void ConstructLayer::GetSigmaSquareFor2DLayer(std::vector<Images> &source, Images &mu, Images &sigma_square)
{
    // get \sigma_B^2
    // --- 2D layer

    double batch_size = source.size();
    assert(batch_size > 0);
    int nKernel = source[0].OutputImageFromKernel.size();
    assert(nKernel > 0);
    int _n = mu.OutputImageFromKernel.size();
    assert(_n == nKernel);

    auto dim = mu.OutputImageFromKernel[0].Dimension();
    assert(dim.first == 1 && dim.second == 1);

    sigma_square.OutputImageFromKernel.clear();

    for(int i=0;i<nKernel;i++)
    {
        double average = (mu.OutputImageFromKernel[i])[0][0];
        Matrix tmp(dim, 0);
        auto kernel_image_dim = source[0].OutputImageFromKernel[i].Dimension();

        for(int ii=0;ii<(int)batch_size;ii++)
        {
            Matrix diff = source[ii].OutputImageFromKernel[i] - average;
            diff = diff^diff;
            tmp = tmp + diff.SumInSection(0, kernel_image_dim.first, 0, kernel_image_dim.second);
        }

        tmp  = tmp/batch_size / (double)kernel_image_dim.first / (double)kernel_image_dim.second; // = tmp / n / (p x q)
        tmp = tmp * batch_size / (batch_size - 1.0); // Var[x]^2 = E(\sigma)^2 * m / (m-1)

        sigma_square.OutputImageFromKernel.push_back(tmp);
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movq %rcx, (%rsp)
movq %rdx, 0x8(%rsp)
movq (%rsi), %r8
movq %rsi, 0x10(%rsp)
movq 0x8(%rsi), %rdi
movq %rdi, %rax
subq %r8, %rax
pushq $0x18
popq %rcx
cqto
idivq %rcx
cmpq %r8, %rdi
je 0xaf7e
movq %rax, %rbx
movq 0x8(%r8), %rax
subq (%r8), %rax
cqto
idivq %rcx
movq %rax, %r15
testl %r15d, %r15d
jle 0xaf9d
movq 0x8(%rsp), %rax
movq (%rax), %rdi
movq 0x8(%rax), %rax
subq %rdi, %rax
cqto
idivq %rcx
cmpl %r15d, %eax
jne 0xafbc
callq 0x17a6a
cmpq $0x1, %rax
jne 0xafdb
cmpq $0x1, %rdx
jne 0xafdb
movq %rbx, %xmm0
punpckldq 0x16317(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x1631f(%rip), %xmm0    # 0x21030
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movapd %xmm1, 0x20(%rsp)
movq (%rsp), %rdi
callq 0x11394
movsd 0x16804(%rip), %xmm0    # 0x21538
addsd 0x20(%rsp), %xmm0
movsd %xmm0, 0x40(%rsp)
xorl %ecx, %ecx
testl %ebx, %ebx
movl $0x0, %eax
cmovgl %ebx, %eax
andl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
imulq $0x18, %rax, %rax
movq %rax, 0x60(%rsp)
leaq 0xb0(%rsp), %rbp
leaq 0x68(%rsp), %r14
leaq 0x80(%rsp), %r12
movq %r15, 0x38(%rsp)
cmpq %r15, %rcx
je 0xaf6c
movq %rcx, 0x48(%rsp)
imulq $0x18, %rcx, %rbx
movq 0x8(%rsp), %rax
movq (%rax), %rdi
addq %rbx, %rdi
xorl %esi, %esi
callq 0x17c46
movq (%rax), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x50(%rsp)
xorpd %xmm0, %xmm0
movq %rbp, %rdi
pushq $0x1
popq %rdx
movq %rdx, %rsi
callq 0x1787a
movq 0x10(%rsp), %rax
movq (%rax), %rax
movq (%rax), %rdi
movq %rbx, 0x58(%rsp)
addq %rbx, %rdi
callq 0x17a6a
movq %rdx, 0x18(%rsp)
movq %rax, %rbx
xorl %r13d, %r13d
cmpq %r13, 0x60(%rsp)
je 0xae6d
movq 0x10(%rsp), %rax
movq (%rax), %rax
movq (%rax,%r13), %rsi
addq 0x58(%rsp), %rsi
movq %r12, %rdi
movsd 0x50(%rsp), %xmm0
callq 0x18270
movq %r14, %rdi
movq %r12, %rsi
movq %r12, %rdx
callq 0x17f64
movq %r12, %rdi
movq %r14, %rsi
callq 0x125b4
movq %r14, %rdi
callq 0x17a9a
movq %r12, %rdi
xorl %esi, %esi
movq %rbx, %rdx
xorl %ecx, %ecx
movq 0x18(%rsp), %r8
callq 0x19b90
movq %rbp, %r15
movq %r14, %rdi
movq %rbp, %rsi
callq 0x180cc
movq %r15, %rdi
movq %r14, %rsi
callq 0x125b4
movq %r14, %rdi
callq 0x17a9a
movq %r12, %rdi
callq 0x17a9a
addq $0x18, %r13
jmp 0xaddc
leaq 0x98(%rsp), %r15
movq %r15, %rdi
movq %rbp, %rsi
movapd 0x20(%rsp), %xmm0
callq 0x18348
movq %rbx, %xmm1
movq 0x1618d(%rip), %xmm0    # 0x21020
punpckldq %xmm0, %xmm1    # xmm1 = xmm1[0],xmm0[0],xmm1[1],xmm0[1]
movapd 0x16191(%rip), %xmm0    # 0x21030
subpd %xmm0, %xmm1
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
movq %r14, %rdi
movq %r15, %rsi
callq 0x18348
movsd 0x18(%rsp), %xmm1
unpcklps 0x16159(%rip), %xmm1    # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x16161(%rip), %xmm1    # 0x21030
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
movq %r12, %rdi
movq %r14, %rsi
callq 0x18348
movq %rbp, %rdi
movq %r12, %rsi
callq 0x125b4
movq %r12, %rdi
callq 0x17a9a
movq %r14, %rdi
callq 0x17a9a
movq %r15, %rdi
callq 0x17a9a
movq %r14, %rdi
movq %rbp, %rsi
movapd 0x20(%rsp), %xmm0
callq 0x182dc
movq %r12, %rdi
movq %r14, %rsi
movsd 0x40(%rsp), %xmm0
callq 0x18348
movq %rbp, %rdi
movq %r12, %rsi
callq 0x125b4
movq %r12, %rdi
callq 0x17a9a
movq %r14, %rdi
callq 0x17a9a
movq (%rsp), %rdi
movq %rbp, %rsi
callq 0x1139c
movq %rbp, %rdi
callq 0x17a9a
movq 0x48(%rsp), %rcx
incq %rcx
movq 0x38(%rsp), %r15
jmp 0xad76
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x16f57(%rip), %rdi     # 0x21edc
leaq 0x1674e(%rip), %rsi     # 0x216da
leaq 0x16feb(%rip), %rcx     # 0x21f7e
movl $0x7cc, %edx            # imm = 0x7CC
callq 0x4180
leaq 0x1703b(%rip), %rdi     # 0x21fdf
leaq 0x1672f(%rip), %rsi     # 0x216da
leaq 0x16fcc(%rip), %rcx     # 0x21f7e
movl $0x7ce, %edx            # imm = 0x7CE
callq 0x4180
leaq 0x17028(%rip), %rdi     # 0x21feb
leaq 0x16710(%rip), %rsi     # 0x216da
leaq 0x16fad(%rip), %rcx     # 0x21f7e
movl $0x7d0, %edx            # imm = 0x7D0
callq 0x4180
leaq 0x17017(%rip), %rdi     # 0x21ff9
leaq 0x166f1(%rip), %rsi     # 0x216da
leaq 0x16f8e(%rip), %rcx     # 0x21f7e
movl $0x7d3, %edx            # imm = 0x7D3
callq 0x4180
movq %rax, %rbx
jmp 0xb00f
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
callq 0x17a9a
leaq 0x68(%rsp), %rdi
jmp 0xb06d
jmp 0xb05b
movq %rax, %rbx
jmp 0xb03c
movq %rax, %rbx
jmp 0xb032
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
callq 0x17a9a
leaq 0x68(%rsp), %rdi
callq 0x17a9a
leaq 0x98(%rsp), %rdi
jmp 0xb06d
jmp 0xb05b
jmp 0xb05b
jmp 0xb04c
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
callq 0x17a9a
jmp 0xb065
movq %rax, %rbx
jmp 0xb072
jmp 0xb062
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
callq 0x17a9a
leaq 0xb0(%rsp), %rdi
callq 0x17a9a
movq %rbx, %rdi
callq 0x43b0
nop