// File: wkohlenberg[P]simple_NN/src/main.cpp
// Function: Neuron::calculateHiddenGradient(std::vector<Neuron, std::allocator<Neuron>> const&, std::vector<Neuron, std::allocator<Neuron>>&)
void Neuron::calculateHiddenGradient(const Layer &nextLayer, Layer &prevLayer)
{
  double sum = 0.;
  double delta = 0.;

  for (unsigned n = 0; n < nextLayer.size() - 1; n++)   // Exclude the bias node
  {
    m_error = m_target - m_outputValue;
    delta = -(nextLayer[n].m_target - nextLayer[n].m_outputValue);
    sum += m_outputWeight[n].weight * derivativeSigmoid(nextLayer[n].m_outputValue) * delta;
  }

  for (unsigned n = 0; n < prevLayer.size()-1; n++)
  {
    m_gradient = sum * Neuron::derivativeSigmoid(m_outputValue) * prevLayer[n].m_outputValue;
    prevLayer[n].m_outputWeight[m_index].deltaWeight = m_gradient*eta;
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x38(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x20(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rdi
callq 0x51d0
movq %rax, %rcx
movq -0x40(%rbp), %rax
subq $0x1, %rcx
cmpq %rcx, %rax
jae 0x3d8e
movq -0x38(%rbp), %rax
movsd 0x18(%rax), %xmm0
subsd 0x8(%rax), %xmm0
movsd %xmm0, 0x20(%rax)
movq -0x10(%rbp), %rdi
movl -0x2c(%rbp), %eax
movl %eax, %esi
callq 0x5210
movsd 0x18(%rax), %xmm0
movsd %xmm0, -0x50(%rbp)
movq -0x10(%rbp), %rdi
movl -0x2c(%rbp), %eax
movl %eax, %esi
callq 0x5210
movsd -0x50(%rbp), %xmm0
movq -0x38(%rbp), %rdi
subsd 0x8(%rax), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd %xmm0, -0x28(%rbp)
addq $0x28, %rdi
movl -0x2c(%rbp), %eax
movl %eax, %esi
callq 0x51b0
movsd (%rax), %xmm0
movsd %xmm0, -0x48(%rbp)
movq -0x10(%rbp), %rdi
movl -0x2c(%rbp), %eax
movl %eax, %esi
callq 0x5210
movq -0x38(%rbp), %rdi
movsd 0x8(%rax), %xmm0
callq 0x3ad0
movaps %xmm0, %xmm1
movsd -0x48(%rbp), %xmm0
mulsd %xmm1, %xmm0
movsd -0x28(%rbp), %xmm2
movsd -0x20(%rbp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x20(%rbp)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x3ca3
movl $0x0, -0x30(%rbp)
movl -0x30(%rbp), %eax
movq %rax, -0x58(%rbp)
movq -0x18(%rbp), %rdi
callq 0x51d0
movq %rax, %rcx
movq -0x58(%rbp), %rax
subq $0x1, %rcx
cmpq %rcx, %rax
jae 0x3e52
movq -0x38(%rbp), %rdi
movsd -0x20(%rbp), %xmm0
movsd %xmm0, -0x70(%rbp)
movsd 0x8(%rdi), %xmm0
callq 0x3ad0
movaps %xmm0, %xmm1
movsd -0x70(%rbp), %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, -0x68(%rbp)
movq -0x18(%rbp), %rdi
movl -0x30(%rbp), %eax
movl %eax, %esi
callq 0x51f0
movsd -0x68(%rbp), %xmm0
movq %rax, %rcx
movq -0x38(%rbp), %rax
mulsd 0x8(%rcx), %xmm0
movsd %xmm0, 0x10(%rax)
movsd 0x10(%rax), %xmm0
mulsd 0x11415(%rip), %xmm0    # 0x15228
movsd %xmm0, -0x60(%rbp)
movq -0x18(%rbp), %rdi
movl -0x30(%rbp), %eax
movl %eax, %esi
callq 0x51f0
movq %rax, %rdi
movq -0x38(%rbp), %rax
addq $0x28, %rdi
movl (%rax), %eax
movl %eax, %esi
callq 0x51b0
movsd -0x60(%rbp), %xmm0
movsd %xmm0, 0x8(%rax)
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x3d95
addq $0x70, %rsp
popq %rbp
retq
nopl (%rax,%rax)