// File: ashlall[P]StreamStats/ChiSquareExperiments/./../SourceCode/ChiSquare/ChiSquareContinuous.cpp
// Function: ChiSquareContinuous::calculate_statistic_ifNormal(int, double, double)
double ChiSquareContinuous::calculate_statistic_ifNormal(int num_buckets, double mean, double SD)
{	
  int stream_size = quantile_sketch->get_stream_size();
  double expected_frequency = (double)stream_size/num_buckets;
  
  // Reallocates the memory for upper_bins and lower_bins.
  delete upper_bins;
  delete lower_bins;
  upper_bins = new double[num_buckets+1];
  lower_bins = new double[num_buckets+1];
  for (int i = 1; i <= num_buckets; i++)
  {
    // Finds the upper and lower values from the normal distribution that
    // mark the interval, or bin.
    double lower_interval = normal_cdf_inverse(((double)i-1)/num_buckets, mean, SD);
    double upper_interval = normal_cdf_inverse((double)i/num_buckets, mean, SD);

    upper_bins[i] = upper_interval;
    lower_bins[i] = lower_interval;

    // Finds the upper and lower quantiles from the quantile_sketch at the
    // lower_interval and upper_interval values.
    double lower_value, upper_value;
    lower_value = (quantile_sketch->reverseQuantile(lower_interval, memory))/memory;
    upper_value = (quantile_sketch->reverseQuantile(upper_interval, memory))/memory;

    double observed_frequency = stream_size * (upper_value - lower_value);
    double lambda = fabs(observed_frequency - expected_frequency);
    chi_squared = chi_squared + ((lambda*lambda)/expected_frequency);
  }		
  return chi_squared;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movsd %xmm0, -0x18(%rbp)
movsd %xmm1, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x78(%rbp)
movq 0x20(%rax), %rdi
movq (%rdi), %rax
callq *0x10(%rax)
movq %rax, %rcx
movq -0x78(%rbp), %rax
movl %ecx, -0x24(%rbp)
cvtsi2sdl -0x24(%rbp), %xmm0
cvtsi2sdl -0xc(%rbp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x30(%rbp)
movq 0x10(%rax), %rax
movq %rax, -0x70(%rbp)
cmpq $0x0, %rax
je 0x5b62
movq -0x70(%rbp), %rdi
callq 0x21a0
movq -0x78(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x80(%rbp)
cmpq $0x0, %rax
je 0x5b7d
movq -0x80(%rbp), %rdi
callq 0x21a0
movl -0xc(%rbp), %eax
addl $0x1, %eax
cltq
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x2030
movq %rax, %rcx
movq -0x78(%rbp), %rax
movq %rcx, 0x10(%rax)
movl -0xc(%rbp), %eax
addl $0x1, %eax
cltq
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x2030
movq %rax, %rcx
movq -0x78(%rbp), %rax
movq %rcx, 0x18(%rax)
movl $0x1, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0xc(%rbp), %eax
jg 0x5d15
movq -0x78(%rbp), %rdi
cvtsi2sdl -0x34(%rbp), %xmm0
movsd 0xc3ff(%rip), %xmm1     # 0x12008
subsd %xmm1, %xmm0
cvtsi2sdl -0xc(%rbp), %xmm1
divsd %xmm1, %xmm0
movsd -0x18(%rbp), %xmm1
movsd -0x20(%rbp), %xmm2
callq 0x5d30
movq -0x78(%rbp), %rdi
movsd %xmm0, -0x40(%rbp)
cvtsi2sdl -0x34(%rbp), %xmm0
cvtsi2sdl -0xc(%rbp), %xmm1
divsd %xmm1, %xmm0
movsd -0x18(%rbp), %xmm1
movsd -0x20(%rbp), %xmm2
callq 0x5d30
movq -0x78(%rbp), %rax
movsd %xmm0, -0x48(%rbp)
movsd -0x48(%rbp), %xmm0
movq 0x10(%rax), %rcx
movslq -0x34(%rbp), %rdx
movsd %xmm0, (%rcx,%rdx,8)
movsd -0x40(%rbp), %xmm0
movq 0x18(%rax), %rcx
movslq -0x34(%rbp), %rdx
movsd %xmm0, (%rcx,%rdx,8)
movq 0x20(%rax), %rdi
movsd -0x40(%rbp), %xmm0
cvttsd2si 0x8(%rax), %esi
callq 0x2b60
movq -0x78(%rbp), %rax
divsd 0x8(%rax), %xmm0
movsd %xmm0, -0x50(%rbp)
movq 0x20(%rax), %rdi
movsd -0x48(%rbp), %xmm0
cvttsd2si 0x8(%rax), %esi
callq 0x2b60
movq -0x78(%rbp), %rax
divsd 0x8(%rax), %xmm0
movsd %xmm0, -0x58(%rbp)
cvtsi2sdl -0x24(%rbp), %xmm0
movsd -0x58(%rbp), %xmm1
subsd -0x50(%rbp), %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, -0x60(%rbp)
movsd -0x60(%rbp), %xmm0
subsd -0x30(%rbp), %xmm0
movaps 0xc4ad(%rip), %xmm1     # 0x12190
pand %xmm1, %xmm0
movsd %xmm0, -0x68(%rbp)
movsd (%rax), %xmm0
movsd -0x68(%rbp), %xmm1
mulsd -0x68(%rbp), %xmm1
divsd -0x30(%rbp), %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, (%rax)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x5bec
movq -0x78(%rbp), %rax
movsd (%rax), %xmm0
addq $0x80, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)