// File: /workspace/github_cpp_makefile_star2_O1/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 0xb997
movq (%rdx), %rdi
movq 0x8(%rdx), %rdx
subq %rdi, %rdx
cmpq $0x18, %rdx
jne 0xb9b6
movq %rcx, 0x58(%rsp)
movq 0x8(%rbx), %rcx
subq (%rbx), %rcx
cmpq $0x18, %rcx
jne 0xb9d5
sarq $0x3, %rax
movabsq $-0x5555555555555555, %r14 # imm = 0xAAAAAAAAAAAAAAAB
imulq %rax, %r14
callq 0x1ac7c
leaq 0x28(%rsp), %rdi
xorpd %xmm0, %xmm0
movq %rax, %rsi
callq 0x1a9ec
movq %r14, 0x20(%rsp)
testl %r14d, %r14d
jle 0xb89e
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 0x1b1b0
movq %r13, %rdi
movq %r12, %rsi
movq %r12, %rdx
callq 0x1b2b2
movq %rbp, %rdi
movq %rbx, %rsi
movq %r13, %rdx
callq 0x1aef8
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x14f80
movq %rbp, %rdi
callq 0x1acb6
movq %r13, %rdi
callq 0x1acb6
movq %r12, %rdi
callq 0x1acb6
addq $0x18, %r15
decq %r14
jne 0xb83d
movsd 0x20(%rsp), %xmm1
unpcklps 0x1b795(%rip), %xmm1    # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x1b79d(%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 0x1b88c
leaq 0x28(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x14f80
movq 0x58(%rsp), %r12
leaq 0x8(%rsp), %rdi
callq 0x1acb6
leaq 0x68(%rsp), %rdi
leaq 0x28(%rsp), %rsi
movapd 0x40(%rsp), %xmm0
callq 0x1b7d0
movapd 0x40(%rsp), %xmm0
addsd 0x1bc93(%rip), %xmm0    # 0x275a8
leaq 0x8(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0x1b88c
leaq 0x28(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x14f80
leaq 0x8(%rsp), %rdi
callq 0x1acb6
leaq 0x68(%rsp), %rdi
callq 0x1acb6
movq (%r12), %r14
movq 0x8(%r12), %r15
cmpq %r14, %r15
je 0xb96e
movq %r14, %rbx
movq %rbx, %rdi
callq 0x1acb6
addq $0x18, %rbx
cmpq %r15, %rbx
jne 0xb958
movq %r14, 0x8(%r12)
leaq 0x28(%rsp), %rsi
movq %r12, %rdi
callq 0x13a14
leaq 0x28(%rsp), %rdi
callq 0x1acb6
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1c5ed(%rip), %rdi     # 0x27f8b
leaq 0x1bda5(%rip), %rsi     # 0x2774a
leaq 0x1c5ee(%rip), %rcx     # 0x27f9a
movl $0x7ab, %edx            # imm = 0x7AB
callq 0x4160
leaq 0x1c63e(%rip), %rdi     # 0x27ffb
leaq 0x1bd86(%rip), %rsi     # 0x2774a
leaq 0x1c5cf(%rip), %rcx     # 0x27f9a
movl $0x7ad, %edx            # imm = 0x7AD
callq 0x4160
leaq 0x1c62c(%rip), %rdi     # 0x28008
leaq 0x1bd67(%rip), %rsi     # 0x2774a
leaq 0x1c5b0(%rip), %rcx     # 0x27f9a
movl $0x7ae, %edx            # imm = 0x7AE
callq 0x4160
jmp 0xba15
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x1acb6
jmp 0xba08
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
jmp 0xba43
jmp 0xba15
jmp 0xba1a
jmp 0xba15
movq %rax, %rbx
jmp 0xba48
movq %rax, %rbx
jmp 0xba3e
movq %rax, %rbx
jmp 0xba34
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
callq 0x1acb6
leaq 0x68(%rsp), %rdi
callq 0x1acb6
leaq 0x8(%rsp), %rdi
callq 0x1acb6
leaq 0x28(%rsp), %rdi
callq 0x1acb6
movq %rbx, %rdi
callq 0x43e0