// File: /workspace/github_cpp_makefile_star2_O3/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)
movq %rax, -0x30(%rbp)
movq 0x10(%r15), %rdi
callq 0x21f0
movq 0x18(%r15), %rdi
callq 0x21f0
movq %r12, %rcx
movq %r12, -0x38(%rbp)
incl %r12d
leaq (,%r12,8), %rax
cmpl $-0x1, %ecx
movq $-0x1, %r13
cmovgeq %rax, %r13
movq %r13, %rdi
callq 0x2030
movq %rax, 0x10(%r15)
movq %r13, %rdi
callq 0x2030
movq -0x38(%rbp), %rcx
movq %rax, 0x18(%r15)
testl %ecx, %ecx
jle 0x4bcd
xorps %xmm0, %xmm0
cvtsi2sdl -0x30(%rbp), %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %ecx, %xmm1
movsd %xmm0, -0x58(%rbp)
movsd %xmm1, -0x38(%rbp)
divsd %xmm1, %xmm0
movsd %xmm0, -0x50(%rbp)
addq %r15, %rbx
decq %r12
xorl %r13d, %r13d
movq %r14, %rax
testb $0x1, %r14b
je 0x4af3
movq (%rbx), %rax
movq -0x1(%rax,%r14), %rax
xorps %xmm0, %xmm0
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 0x4b25
movq (%rbx), %rax
movq -0x1(%rax,%r14), %rax
leal 0x1(%r13), %ecx
xorps %xmm0, %xmm0
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 0x293a
movsd 0x8(%r15), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x30(%rbp)
movq 0x20(%r15), %rdi
cvttsd2si %xmm1, %esi
movsd -0x60(%rbp), %xmm0
callq 0x293a
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
cmpq %r13, %r12
jne 0x4ae2
jmp 0x4bd2
movsd (%r15), %xmm0
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop