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

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

    auto dim = source[0].OutputImageFromKernel[0].Dimension();
    Matrix tmp_sigma_square(dim, 0);

    Matrix & average = mu.OutputImageFromKernel[0];

    for(int i=0;i<(int)batch_size;i++)
    {
        Matrix & tmp_kernel_image = source[i].OutputImageFromKernel[0];

        Matrix _t1 = tmp_kernel_image - average;
        Matrix _t1_square = _t1^_t1;

        tmp_sigma_square = tmp_sigma_square + _t1_square;
    }

    tmp_sigma_square = tmp_sigma_square / batch_size;
    tmp_sigma_square = tmp_sigma_square * batch_size / (batch_size - 1.); // Var[x]^2 = E(\sigma)^2 * m / (m-1)

    sigma_square.OutputImageFromKernel.clear();
    sigma_square.OutputImageFromKernel.push_back(tmp_sigma_square);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rdx, %rbx
movq (%rsi), %rdx
movq %rsi, 0x40(%rsp)
movq 0x8(%rsi), %rax
subq %rdx, %rax
je 0xb92d
movq (%rdx), %rdi
movq 0x8(%rdx), %rdx
subq %rdi, %rdx
cmpq $0x18, %rdx
jne 0xb94c
movq %rcx, 0x58(%rsp)
movq 0x8(%rbx), %rcx
subq (%rbx), %rcx
cmpq $0x18, %rcx
jne 0xb96b
sarq $0x3, %rax
movabsq $-0x5555555555555555, %r14 # imm = 0xAAAAAAAAAAAAAAAB
imulq %rax, %r14
callq 0x1abb8
leaq 0x28(%rsp), %rdi
xorpd %xmm0, %xmm0
movq %rax, %rsi
callq 0x1a92c
movq %r14, 0x20(%rsp)
testl %r14d, %r14d
jle 0xb834
movq (%rbx), %rax
movq %rax, 0x60(%rsp)
movq 0x20(%rsp), %r14
andl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
xorl %r15d, %r15d
leaq 0x8(%rsp), %r12
leaq 0x68(%rsp), %r13
leaq 0x80(%rsp), %rbp
leaq 0x28(%rsp), %rbx
movq 0x40(%rsp), %rax
movq (%rax), %rax
movq (%rax,%r15), %rsi
movq %r12, %rdi
movq 0x60(%rsp), %rdx
callq 0x1b0ec
movq %r13, %rdi
movq %r12, %rsi
movq %r12, %rdx
callq 0x1b1ee
movq %rbp, %rdi
movq %rbx, %rsi
movq %r13, %rdx
callq 0x1ae34
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x14e2c
movq %rbp, %rdi
callq 0x1abf2
movq %r13, %rdi
callq 0x1abf2
movq %r12, %rdi
callq 0x1abf2
addq $0x18, %r15
decq %r14
jne 0xb7d3
movsd 0x20(%rsp), %xmm1
unpcklps 0x1b7ff(%rip), %xmm1    # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x1b807(%rip), %xmm1    # 0x27050
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
leaq 0x8(%rsp), %rdi
leaq 0x28(%rsp), %rsi
movapd %xmm0, 0x40(%rsp)
callq 0x1b7c8
leaq 0x28(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x14e2c
movq 0x58(%rsp), %r12
leaq 0x8(%rsp), %rdi
callq 0x1abf2
leaq 0x68(%rsp), %rdi
leaq 0x28(%rsp), %rsi
movapd 0x40(%rsp), %xmm0
callq 0x1b70c
movapd 0x40(%rsp), %xmm0
addsd 0x1bcfd(%rip), %xmm0    # 0x275a8
leaq 0x8(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0x1b7c8
leaq 0x28(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x14e2c
leaq 0x8(%rsp), %rdi
callq 0x1abf2
leaq 0x68(%rsp), %rdi
callq 0x1abf2
movq (%r12), %r14
movq 0x8(%r12), %r15
cmpq %r14, %r15
je 0xb904
movq %r14, %rbx
movq %rbx, %rdi
callq 0x1abf2
addq $0x18, %rbx
cmpq %r15, %rbx
jne 0xb8ee
movq %r14, 0x8(%r12)
leaq 0x28(%rsp), %rsi
movq %r12, %rdi
callq 0x138a4
leaq 0x28(%rsp), %rdi
callq 0x1abf2
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1c618(%rip), %rdi     # 0x27f4c
leaq 0x1be0f(%rip), %rsi     # 0x2774a
leaq 0x1c619(%rip), %rcx     # 0x27f5b
movl $0x7ab, %edx            # imm = 0x7AB
callq 0x4160
leaq 0x1c669(%rip), %rdi     # 0x27fbc
leaq 0x1bdf0(%rip), %rsi     # 0x2774a
leaq 0x1c5fa(%rip), %rcx     # 0x27f5b
movl $0x7ad, %edx            # imm = 0x7AD
callq 0x4160
leaq 0x1c657(%rip), %rdi     # 0x27fc9
leaq 0x1bdd1(%rip), %rsi     # 0x2774a
leaq 0x1c5db(%rip), %rcx     # 0x27f5b
movl $0x7ae, %edx            # imm = 0x7AE
callq 0x4160
jmp 0xb9ab
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x1abf2
jmp 0xb99e
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
jmp 0xb9d9
jmp 0xb9ab
jmp 0xb9b0
jmp 0xb9ab
movq %rax, %rbx
jmp 0xb9de
movq %rax, %rbx
jmp 0xb9d4
movq %rax, %rbx
jmp 0xb9ca
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
callq 0x1abf2
leaq 0x68(%rsp), %rdi
callq 0x1abf2
leaq 0x8(%rsp), %rdi
callq 0x1abf2
leaq 0x28(%rsp), %rdi
callq 0x1abf2
movq %rbx, %rdi
callq 0x43e0