// File: /workspace/github_cpp_makefile_star2_O3/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 $0xd8, %rsp
movq %rcx, 0x8(%rsp)
movq %rdx, 0x18(%rsp)
movq (%rsi), %rax
movq %rsi, 0x28(%rsp)
movq 0x8(%rsi), %r14
subq %rax, %r14
je 0xbd53
movq 0x8(%rax), %r12
subq (%rax), %r12
movabsq $-0x5555555555555555, %rbx # imm = 0xAAAAAAAAAAAAAAAB
sarq $0x3, %r12
imulq %rbx, %r12
testl %r12d, %r12d
jle 0xbd72
movq 0x18(%rsp), %rax
movq (%rax), %rdi
movq 0x8(%rax), %rax
subq %rdi, %rax
shrq $0x3, %rax
imull $0xaaaaaaab, %eax, %eax # imm = 0xAAAAAAAB
cmpl %r12d, %eax
jne 0xbd91
callq 0x1abb8
cmpq $0x1, %rax
jne 0xbdb0
cmpq $0x1, %rdx
jne 0xbdb0
sarq $0x3, %r14
imulq %rbx, %r14
movq %r14, 0x10(%rsp)
movq %r14, %xmm0
punpckldq 0x1b5a9(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x1b5b1(%rip), %xmm0    # 0x27050
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movapd %xmm1, 0x40(%rsp)
movq 0x8(%rsp), %rax
movq (%rax), %r14
movq 0x8(%rax), %r15
cmpq %r14, %r15
je 0xbadf
movq %r14, %rbx
movq %rbx, %rdi
callq 0x1abf2
addq $0x18, %rbx
cmpq %r15, %rbx
jne 0xbac5
movq 0x8(%rsp), %rax
movq %r14, 0x8(%rax)
movsd 0x1bac1(%rip), %xmm0    # 0x275a8
addsd 0x40(%rsp), %xmm0
movsd %xmm0, 0x60(%rsp)
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andq %rax, %r12
movq 0x10(%rsp), %rcx
andq %rax, %rcx
movq %rcx, 0x58(%rsp)
xorl %eax, %eax
leaq 0xa8(%rsp), %r14
leaq 0x90(%rsp), %r13
leaq 0x78(%rsp), %rbp
movq %r12, 0x68(%rsp)
movq %rax, 0x20(%rsp)
leaq (,%rax,8), %rax
leaq (%rax,%rax,2), %rbx
movq 0x18(%rsp), %rax
movq (%rax), %rdi
addq %rbx, %rdi
xorl %esi, %esi
callq 0x1adb2
movq (%rax), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x70(%rsp)
movl $0x1, %esi
movl $0x1, %edx
xorpd %xmm0, %xmm0
movq %r14, %rdi
callq 0x1a92c
movq 0x28(%rsp), %rax
movq (%rax), %rax
addq (%rax), %rbx
movq %rbx, %rdi
callq 0x1abb8
movq %rax, 0x38(%rsp)
movq %rdx, 0x30(%rsp)
cmpl $0x0, 0x10(%rsp)
jle 0xbc35
movq 0x20(%rsp), %rax
leaq (%rax,%rax,2), %r15
shlq $0x3, %r15
movq 0x58(%rsp), %r12
xorl %ebx, %ebx
movq 0x28(%rsp), %rax
movq (%rax), %rax
movq (%rax,%rbx), %rsi
addq %r15, %rsi
movq %r13, %rdi
movsd 0x70(%rsp), %xmm0
callq 0x1b650
movq %rbp, %rdi
movq %r13, %rsi
movq %r13, %rdx
callq 0x1b1ee
movq %r13, %rdi
movq %rbp, %rsi
callq 0x14e2c
movq %rbp, %rdi
callq 0x1abf2
movq %r13, %rdi
xorl %esi, %esi
movq 0x38(%rsp), %rdx
xorl %ecx, %ecx
movq 0x30(%rsp), %r8
callq 0x1d774
leaq 0xa8(%rsp), %r14
movq %rbp, %rdi
movq %r14, %rsi
callq 0x1b3f2
movq %r14, %rdi
movq %rbp, %rsi
callq 0x14e2c
movq %rbp, %rdi
callq 0x1abf2
movq %r13, %rdi
callq 0x1abf2
addq $0x18, %rbx
decq %r12
jne 0xbba6
leaq 0xc0(%rsp), %rbx
movq %rbx, %rdi
leaq 0xa8(%rsp), %r14
movq %r14, %rsi
movapd 0x40(%rsp), %xmm0
callq 0x1b7c8
movsd 0x38(%rsp), %xmm1
movsd 0x1b3dc(%rip), %xmm0    # 0x27040
unpcklps %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0],xmm1[1],xmm0[1]
movapd 0x1b3e1(%rip), %xmm0    # 0x27050
subpd %xmm0, %xmm1
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x1b7c8
movsd 0x30(%rsp), %xmm1
unpcklps 0x1b3a9(%rip), %xmm1    # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x1b3b1(%rip), %xmm1    # 0x27050
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
movq %r13, %rdi
movq %rbp, %rsi
callq 0x1b7c8
movq %r14, %rdi
movq %r13, %rsi
callq 0x14e2c
movq 0x68(%rsp), %r12
movq %r13, %rdi
callq 0x1abf2
movq %rbp, %rdi
callq 0x1abf2
movq %rbx, %rdi
callq 0x1abf2
movq %rbp, %rdi
movq %r14, %rsi
movapd 0x40(%rsp), %xmm0
callq 0x1b70c
movq %r13, %rdi
movq %rbp, %rsi
movsd 0x60(%rsp), %xmm0
callq 0x1b7c8
movq %r14, %rdi
movq %r13, %rsi
callq 0x14e2c
movq %r13, %rdi
callq 0x1abf2
movq %rbp, %rdi
callq 0x1abf2
movq 0x8(%rsp), %rdi
movq %r14, %rsi
callq 0x138a4
movq %r14, %rdi
callq 0x1abf2
movq 0x20(%rsp), %rax
incq %rax
cmpq %r12, %rax
jne 0xbb24
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1c1f2(%rip), %rdi     # 0x27f4c
leaq 0x1b9e9(%rip), %rsi     # 0x2774a
leaq 0x1c286(%rip), %rcx     # 0x27fee
movl $0x7cc, %edx            # imm = 0x7CC
callq 0x4160
leaq 0x1c2d6(%rip), %rdi     # 0x2804f
leaq 0x1b9ca(%rip), %rsi     # 0x2774a
leaq 0x1c267(%rip), %rcx     # 0x27fee
movl $0x7ce, %edx            # imm = 0x7CE
callq 0x4160
leaq 0x1c2c3(%rip), %rdi     # 0x2805b
leaq 0x1b9ab(%rip), %rsi     # 0x2774a
leaq 0x1c248(%rip), %rcx     # 0x27fee
movl $0x7d0, %edx            # imm = 0x7D0
callq 0x4160
leaq 0x1c2b2(%rip), %rdi     # 0x28069
leaq 0x1b98c(%rip), %rsi     # 0x2774a
leaq 0x1c229(%rip), %rcx     # 0x27fee
movl $0x7d3, %edx            # imm = 0x7D3
callq 0x4160
jmp 0xbe32
movq %rax, %rbx
jmp 0xbdf5
movq %rax, %rbx
jmp 0xbdeb
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
callq 0x1abf2
leaq 0x78(%rsp), %rdi
callq 0x1abf2
leaq 0xc0(%rsp), %rdi
jmp 0xbe42
jmp 0xbe32
movq %rax, %rbx
jmp 0xbe16
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
callq 0x1abf2
leaq 0x78(%rsp), %rdi
jmp 0xbe42
jmp 0xbe32
jmp 0xbe21
movq %rax, %rbx
leaq 0x78(%rsp), %rdi
callq 0x1abf2
jmp 0xbe3a
jmp 0xbe37
movq %rax, %rbx
jmp 0xbe47
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
callq 0x1abf2
leaq 0xa8(%rsp), %rdi
callq 0x1abf2
movq %rbx, %rdi
callq 0x43e0