// File: /workspace/github_cpp_makefile_star2_O2/ashlall[P]StreamStats/ChiSquareExperiments/./../SourceCode/ChiSquare/ChiSquareContinuous.cpp
// Function: ChiSquareContinuous::calculate_statistic(int, double (ChiSquareContinuous::*)(double, double, double), double, double)
double ChiSquareContinuous::calculate_statistic(int num_buckets, double(ChiSquareContinuous::*distribution)(double, double, double), double location, double scale)
{		
  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 distribution that mark the bin.
    double lower_interval = (this->*distribution)(((double)i-1)/num_buckets, location, scale);
    double upper_interval = (this->*distribution)((double)i/num_buckets, location, scale);

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

    // Finds the upper and lower quantiles from quantile_sketch that correspond
    // to lower_interval and upper_interval.
    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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movsd %xmm1, -0x48(%rbp)
movsd %xmm0, -0x40(%rbp)
movq %rcx, %rbx
movq %rdx, %r14
movl %esi, %r12d
movq %rdi, %r15
movq 0x20(%rdi), %rdi
movq (%rdi), %rax
callq *0x10(%rax)
cvtsi2sd %eax, %xmm0
cvtsi2sd %r12d, %xmm1
movsd %xmm0, -0x58(%rbp)
movsd %xmm1, -0x38(%rbp)
divsd %xmm1, %xmm0
movsd %xmm0, -0x50(%rbp)
movq 0x10(%r15), %rdi
callq 0x21b0
movq 0x18(%r15), %rdi
callq 0x21b0
leal 0x1(%r12), %eax
shlq $0x3, %rax
cmpl $-0x1, %r12d
pushq $-0x1
popq %r13
cmovgeq %rax, %r13
movq %r13, %rdi
callq 0x2030
movq %rax, 0x10(%r15)
movq %r13, %rdi
callq 0x2030
movq %rax, 0x18(%r15)
addq %r15, %rbx
xorl %r13d, %r13d
testl %r12d, %r12d
cmovlel %r13d, %r12d
cmpq %r13, %r12
je 0x466d
movq %r14, %rax
testb $0x1, %r14b
je 0x459f
movq (%rbx), %rax
movq -0x1(%rax,%r14), %rax
cvtsi2sd %r13d, %xmm0
divsd -0x38(%rbp), %xmm0
movq %rbx, %rdi
movsd -0x40(%rbp), %xmm1
movsd -0x48(%rbp), %xmm2
callq *%rax
movsd %xmm0, -0x30(%rbp)
movq %r14, %rax
testb $0x1, %r14b
je 0x45ce
movq (%rbx), %rax
movq -0x1(%rax,%r14), %rax
leal 0x1(%r13), %ecx
cvtsi2sd %ecx, %xmm0
divsd -0x38(%rbp), %xmm0
movq %rbx, %rdi
movsd -0x40(%rbp), %xmm1
movsd -0x48(%rbp), %xmm2
callq *%rax
movsd %xmm0, -0x60(%rbp)
movq 0x10(%r15), %rax
movsd %xmm0, 0x8(%rax,%r13,8)
movq 0x18(%r15), %rax
movsd -0x30(%rbp), %xmm0
movsd %xmm0, 0x8(%rax,%r13,8)
movq 0x20(%r15), %rdi
cvttsd2si 0x8(%r15), %esi
callq 0x28d4
movsd 0x8(%r15), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x30(%rbp)
movq 0x20(%r15), %rdi
cvttsd2si %xmm1, %esi
movsd -0x60(%rbp), %xmm0
callq 0x28d4
divsd 0x8(%r15), %xmm0
subsd -0x30(%rbp), %xmm0
mulsd -0x58(%rbp), %xmm0
movsd -0x50(%rbp), %xmm1
subsd %xmm1, %xmm0
mulsd %xmm0, %xmm0
divsd %xmm1, %xmm0
addsd (%r15), %xmm0
movsd %xmm0, (%r15)
incq %r13
jmp 0x4585
movsd (%r15), %xmm0
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop